2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.objects ;
import engine.Enum ;
import engine.InterestManagement.WorldGrid ;
import engine.db.archive.DataWarehouse ;
import engine.db.archive.MineRecord ;
import engine.gameManager.* ;
import engine.net.ByteBufferWriter ;
import engine.net.client.msg.ErrorPopupMsg ;
import engine.server.MBServerStatics ;
import engine.session.SessionID ;
import org.pmw.tinylog.Logger ;
import java.net.UnknownHostException ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.time.LocalDateTime ;
import java.util.ArrayList ;
import java.util.concurrent.ConcurrentHashMap ;
2023-01-19 14:09:15 -05:00
import static engine.gameManager.DbManager.* ;
2022-04-30 09:41:17 -04:00
import static engine.math.FastMath.sqr ;
public class Mine extends AbstractGameObject {
2023-01-19 13:46:33 -05:00
private String zoneName ;
private Resource production ;
private boolean isActive = false ;
private float latitude ;
private float longitude ;
private float altitude ;
private Guild owningGuild ;
2023-01-19 15:22:12 -05:00
public PlayerCharacter lastClaimer ;
public SessionID lastClaimerSessionID ;
2023-01-19 13:27:26 -05:00
2023-01-19 13:46:33 -05:00
private int flags ;
private int buildingID ;
private Zone parentZone ;
private MineProduction mineType ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//flags 1: never been claimed (make active).
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
// Not persisted to DB
private String guildName ;
private GuildTag guildTag ;
private String nationName ;
private GuildTag nationTag ;
public static ConcurrentHashMap < Mine , Integer > mineMap = new ConcurrentHashMap <> ( MBServerStatics . CHM_INIT_CAP , MBServerStatics . CHM_LOAD , MBServerStatics . CHM_THREAD_LOW );
public static ConcurrentHashMap < Integer , Mine > towerMap = new ConcurrentHashMap <> ( MBServerStatics . CHM_INIT_CAP , MBServerStatics . CHM_LOAD , MBServerStatics . CHM_THREAD_LOW );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
private static long lastChange = System . currentTimeMillis ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
/**
* ResultSet Constructor
*/
public Mine ( ResultSet rs ) throws SQLException , UnknownHostException {
super ( rs );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . mineType = MineProduction . getByName ( rs . getString ( "mine_type" ));
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
float offsetX = rs . getFloat ( "mine_offsetX" );
float offsetZ = rs . getFloat ( "mine_offsetZ" );
int ownerUID = rs . getInt ( "mine_ownerUID" );
this . buildingID = rs . getInt ( "mine_buildingUID" );
this . flags = rs . getInt ( "flags" );
int parent = rs . getInt ( "parent" );
this . parentZone = ZoneManager . getZoneByUUID ( parent );
if ( parentZone != null ) {
this . latitude = parentZone . getLoc (). x + offsetX ;
this . longitude = parentZone . getLoc (). z + offsetZ ;
this . altitude = parentZone . getLoc (). y ;
if ( this . parentZone . getParent () != null )
this . zoneName = this . parentZone . getParent (). getName ();
else
this . zoneName = this . parentZone . getName ();
} else {
Logger . error ( "Missing parentZone of ID " + parent );
this . latitude = - 1000 ;
this . longitude = 1000 ;
this . altitude = 0 ;
this . zoneName = "Unknown Mine" ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . owningGuild = Guild . getGuild ( ownerUID );
Guild nation = null ;
2023-01-19 14:43:08 -05:00
if ( ! this . owningGuild . isErrant ()) {
2023-01-19 13:46:33 -05:00
this . guildName = this . owningGuild . getName ();
this . guildTag = this . owningGuild . getGuildTag ();
nation = this . owningGuild . getNation ();
2023-01-19 15:39:49 -05:00
this . setActive ( true );
2023-01-19 13:46:33 -05:00
} else {
this . guildName = "" ;
this . guildTag = GuildTag . ERRANT ;
nation = Guild . getErrantGuild ();
this . owningGuild = Guild . getErrantGuild ();
2023-01-19 15:39:49 -05:00
this . setActive ( false );
2023-01-19 13:46:33 -05:00
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( ! nation . isErrant ()) {
this . nationName = nation . getName ();
this . nationTag = nation . getGuildTag ();
} else {
this . nationName = "" ;
this . nationTag = GuildTag . ERRANT ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . production = Resource . valueOf ( rs . getString ( "mine_resource" ));
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . lastClaimerSessionID = null ;
2023-01-13 14:34:04 -05:00
2023-01-19 13:46:33 -05:00
}
2023-01-19 13:27:26 -05:00
2023-01-19 13:46:33 -05:00
public static void SendMineAttackMessage ( Building mine ) {
2022-04-30 09:41:17 -04:00
if ( mine . getBlueprint () == null )
return ;
if ( mine . getBlueprint (). getBuildingGroup () != Enum . BuildingGroup . MINE )
return ;
2023-01-19 13:46:33 -05:00
2022-04-30 09:41:17 -04:00
if ( mine . getGuild (). isErrant ())
2023-01-19 13:46:33 -05:00
return ;
2022-04-30 09:41:17 -04:00
if ( mine . getGuild (). getNation (). isErrant ())
return ;
if ( mine . getTimeStamp ( "MineAttack" ) > System . currentTimeMillis ())
return ;
mine . getTimestamps (). put ( "MineAttack" , System . currentTimeMillis () + MBServerStatics . ONE_MINUTE );
ChatManager . chatNationInfo ( mine . getGuild (). getNation (), mine . getName () + " in " + mine . getParentZone (). getParent (). getName () + " is Under attack!" );
}
2023-01-19 13:46:33 -05:00
public static void loadAllMines () {
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
try {
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//Load mine resources
MineProduction . addResources ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//pre-load all building sets
ArrayList < Mine > serverMines = MineQueries . GET_ALL_MINES_FOR_SERVER ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
for ( Mine mine : serverMines ) {
Mine . mineMap . put ( mine , mine . buildingID );
Mine . towerMap . put ( mine . buildingID , mine );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
} catch ( Exception e ) {
e . printStackTrace ();
}
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
/*
* Getters
*/
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public boolean changeProductionType ( Resource resource ) {
if ( ! this . validForMine ( resource ))
return false ;
//update resource in database;
if ( ! MineQueries . CHANGE_RESOURCE ( this , resource ))
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . production = resource ;
return true ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public MineProduction getMineType () {
return this . mineType ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public String getZoneName () {
return this . zoneName ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public Resource getProduction () {
return this . production ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public boolean getIsActive () {
return this . isActive ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public float getAltitude () {
return this . altitude ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public Guild getOwningGuild () {
2023-01-19 14:43:08 -05:00
if ( this . owningGuild == null )
return Guild . getErrantGuild ();
else
return this . owningGuild ;
2023-01-19 13:46:33 -05:00
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public int getFlags () {
return flags ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void setFlags ( int flags ) {
this . flags = flags ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public Zone getParentZone () {
return parentZone ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public GuildTag getGuildTag () {
return guildTag ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void setMineType ( String type ) {
this . mineType = MineProduction . getByName ( type );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void setActive ( boolean isAc ) {
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . isActive = isAc ;
Building building = BuildingManager . getBuildingFromCache ( this . buildingID );
if ( building != null && ! this . isActive )
building . isDeranking . compareAndSet ( true , false );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void setOwningGuild ( Guild owningGuild ) {
this . owningGuild = owningGuild ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public static Mine getMineFromTower ( int towerID ) {
return Mine . towerMap . get ( towerID );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public boolean validForMine ( Resource r ) {
if ( this . mineType == null )
return false ;
return this . mineType . validForMine ( r , this . isExpansion ());
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
/*
* Serialization
*/
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public static void serializeForClientMsg ( Mine mine , ByteBufferWriter writer ) {
writer . putInt ( mine . getObjectType (). ordinal ());
writer . putInt ( mine . getObjectUUID ());
writer . putInt ( mine . getObjectUUID ()); //actually a hash of mine
// writer.putInt(0x215C92BB); //this.unknown1);
writer . putString ( mine . mineType . name );
writer . putString ( mine . zoneName );
writer . putInt ( mine . production . hash );
writer . putInt ( mine . production . baseProduction );
writer . putInt ( mine . getModifiedProductionAmount ()); //TODO calculate range penalty here
writer . putInt ( 3600 ); //window in seconds
2023-01-19 10:38:49 -05:00
2023-01-19 13:59:52 -05:00
// Errant mines are currently open. Set time to now.
2023-01-19 10:38:49 -05:00
2023-01-19 13:46:33 -05:00
LocalDateTime mineOpenTime = LocalDateTime . now (). withMinute ( 0 ). withSecond ( 0 ). withNano ( 0 );
Guild mineOwnerGuild = mine . getOwningGuild ();
2023-01-19 10:38:49 -05:00
2023-01-19 13:46:33 -05:00
// Adjust the serialized mine time based upon whether
// the Guild's mine window has passed or not.
2023-01-19 13:59:52 -05:00
// If a mine is active serialize current datetime irrespective
// of any claim
2023-01-19 10:38:49 -05:00
2023-01-19 13:59:52 -05:00
if ( mineOwnerGuild . isErrant () == false && mine . isActive == false ) {
2023-01-19 10:38:49 -05:00
2023-01-19 13:46:33 -05:00
int guildWOO = mineOwnerGuild . getMineTime ();
LocalDateTime guildMineTime = mineOpenTime . withHour ( guildWOO );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( mineOpenTime . isAfter ( guildMineTime ))
mineOpenTime = guildMineTime . plusDays ( 1 );
else
mineOpenTime = guildMineTime ;
2023-01-19 13:57:43 -05:00
2023-01-19 13:46:33 -05:00
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:57:43 -05:00
2023-01-19 13:46:33 -05:00
writer . putLocalDateTime ( mineOpenTime );
writer . putLocalDateTime ( mineOpenTime . plusHours ( 1 ));
writer . put ( mine . isActive ? ( byte ) 0x01 : ( byte ) 0x00 );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
writer . putFloat ( mine . latitude );
writer . putFloat ( mine . altitude );
writer . putFloat ( mine . longitude );
writer . putInt ( mine . isExpansion () ? mine . mineType . xpacHash : mine . mineType . hash );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
writer . putString ( mine . guildName );
GuildTag . _serializeForDisplay ( mine . guildTag , writer );
writer . putString ( mine . nationName );
GuildTag . _serializeForDisplay ( mine . nationTag , writer );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void serializeForMineProduction ( ByteBufferWriter writer ) {
writer . putInt ( this . getObjectType (). ordinal ());
writer . putInt ( this . getObjectUUID ());
writer . putInt ( this . getObjectUUID ()); //actually a hash of mine
// writer.putInt(0x215C92BB); //this.unknown1);
writer . putString ( this . mineType . name );
writer . putString ( this . zoneName );
writer . putInt ( this . production . hash );
writer . putInt ( this . production . baseProduction );
writer . putInt ( this . getModifiedProductionAmount ()); //TODO calculate range penalty here
writer . putInt ( 3600 ); //window in seconds
writer . putInt ( this . isExpansion () ? this . mineType . xpacHash : this . mineType . hash );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public static ArrayList < Mine > getMinesForGuild ( int guildID ) {
ArrayList < Mine > mineList = new ArrayList <> ();
for ( Mine mine : Mine . mineMap . keySet ()) {
2023-01-19 14:43:08 -05:00
if ( mine . owningGuild . getObjectUUID () == guildID )
2023-01-19 13:46:33 -05:00
mineList . add ( mine );
}
return mineList ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public static long getLastChange () {
return lastChange ;
}
public static void setLastChange ( long lastChange ) {
Mine . lastChange = lastChange ;
}
/*
* Database
*/
public static Mine getMine ( int UID ) {
2022-04-30 09:41:17 -04:00
return MineQueries . GET_MINE ( UID );
2023-01-19 13:46:33 -05:00
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public static ArrayList < Mine > getMines () {
return new ArrayList <> ( mineMap . keySet ());
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
@Override
public void updateDatabase () {
// TODO Create update logic.
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public int getBuildingID () {
return buildingID ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void setBuildingID ( int buildingID ) {
this . buildingID = buildingID ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void handleStartMineWindow () {
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . setActive ( true );
ChatManager . chatSystemChannel ( this . zoneName + "'s Mine is now Active!" );
Logger . info ( this . zoneName + "'s Mine is now Active!" );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public static boolean validClaimer ( PlayerCharacter playerCharacter ) {
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
Guild playerGuild ;
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
//verify the player exists
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
if ( playerCharacter == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//verify the player is in valid guild
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
playerGuild = playerCharacter . getGuild ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( playerGuild . isErrant ())
return false ;
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
if ( SessionManager . getPlayerCharacterByID ( playerCharacter . getObjectUUID ()) == null )
return false ;
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
//Get a count of nation mines, can't go over capital tol rank.
City capital = playerGuild . getOwnedCity ();
City guildCity = playerGuild . getOwnedCity ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( guildCity == null )
return false ;
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
if ( capital == null )
return false ;
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
if ( guildCity . getWarehouse () == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
Building tol = capital . getTOL ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( tol == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
int rank = tol . getRank ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( rank < 1 )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
int mineCnt = 0 ;
2023-01-19 12:48:54 -05:00
2023-01-19 13:46:33 -05:00
mineCnt += Mine . getMinesForGuild ( playerGuild . getObjectUUID ()). size ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
for ( Guild guild : playerGuild . getSubGuildList ())
mineCnt += Mine . getMinesForGuild ( guild . getObjectUUID ()). size ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( mineCnt > rank )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
return true ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public void handleDestroyMine () {
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( ! this . isActive )
return ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//remove tags from mine
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . guildName = "" ;
this . nationName = "" ;
2023-01-19 14:43:08 -05:00
this . owningGuild = Guild . getErrantGuild ();
2023-01-19 13:46:33 -05:00
Mine . setLastChange ( System . currentTimeMillis ());
2023-01-13 11:24:41 -05:00
2023-01-19 13:46:33 -05:00
// Update database
2023-01-13 11:24:41 -05:00
2023-01-19 13:46:33 -05:00
DbManager . MineQueries . CHANGE_OWNER ( this , 0 );
2023-01-13 11:46:26 -05:00
2023-01-19 13:46:33 -05:00
// Update mesh
2023-01-13 11:46:26 -05:00
2023-01-19 13:46:33 -05:00
Building mineBuilding = BuildingManager . getBuildingFromCache ( this . buildingID );
2023-01-13 11:52:50 -05:00
2023-01-19 13:46:33 -05:00
if ( mineBuilding == null ) {
Logger . debug ( "Null mine building " + this . getObjectUUID () + ". Unable to Load Building with UID " + this . buildingID );
return ;
}
2023-01-13 11:46:26 -05:00
2023-01-19 13:46:33 -05:00
mineBuilding . setOwner ( null );
mineBuilding . refresh ( false );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
// remove hirelings
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
Building building = ( Building ) getObject ( Enum . GameObjectType . Building , this . buildingID );
BuildingManager . cleanupHirelings ( building );
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public boolean handleEndMineWindow () {
2023-01-11 15:00:52 -05:00
2023-01-19 13:46:33 -05:00
// No need to end the window of a mine which never opened.
2023-01-11 15:00:52 -05:00
2023-01-19 13:46:33 -05:00
if ( this . isActive == false )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
Building mineBuilding = BuildingManager . getBuildingFromCache ( this . buildingID );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( mineBuilding == null ) {
Logger . debug ( "Null mine building for Mine " + this . getObjectUUID () + " Building " + this . buildingID );
return false ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( mineBuilding . getRank () > 0 ) {
//never knocked down, let's just move on.
//hasn't been claimed since server start.
this . setActive ( false );
this . lastClaimerSessionID = null ;
this . lastClaimer = null ;
return true ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
// This mine does not have a valid claimer
// we will therefore set it to errant
2023-01-19 15:22:12 -05:00
// and keep the window open.
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( ! validClaimer ( this . lastClaimer )) {
this . lastClaimerSessionID = null ;
this . lastClaimer = null ;
2023-01-19 14:15:43 -05:00
this . updateGuildOwner ( null );
2023-01-19 15:22:12 -05:00
this . setActive ( true );
2023-01-19 13:46:33 -05:00
return false ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 14:43:08 -05:00
if ( this . owningGuild . isErrant () || this . owningGuild . getNation (). isErrant ())
2023-01-19 13:46:33 -05:00
return false ;
2023-01-13 08:43:20 -05:00
2023-01-19 13:46:33 -05:00
//Update ownership to map
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
this . guildName = this . owningGuild . getName ();
this . guildTag = this . owningGuild . getGuildTag ();
Guild nation = this . owningGuild . getNation ();
this . nationName = nation . getName ();
this . nationTag = nation . getGuildTag ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
setLastChange ( System . currentTimeMillis ());
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
mineBuilding . rebuildMine ();
WorldGrid . updateObject ( mineBuilding );
ChatManager . chatSystemChannel ( this . lastClaimer . getName () + " has claimed the mine in " + this . parentZone . getParent (). getName () + " for " + this . owningGuild . getName () + ". The mine is no longer active." );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
// Warehouse this claim event
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
MineRecord mineRecord = MineRecord . borrow ( this , this . lastClaimer , Enum . RecordEventType . CAPTURE );
DataWarehouse . pushToWarehouse ( mineRecord );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
mineBuilding . setRank ( mineBuilding . getRank ());
2023-01-19 13:47:11 -05:00
this . lastClaimerSessionID = null ;
this . lastClaimer = null ;
2023-01-19 13:46:33 -05:00
this . setActive ( false );
return true ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public boolean claimMine ( PlayerCharacter claimer ) {
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( claimer == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( ! validClaimer ( claimer ))
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( ! this . isActive ) {
ErrorPopupMsg . sendErrorMsg ( claimer , "Can not for to claim inactive mine." );
return false ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( ! updateGuildOwner ( claimer ))
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
// Not the Same session for this character?
// Claimers may not relog or they lose claim.
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( this . lastClaimer != null ) {
2023-01-19 13:27:26 -05:00
2023-01-19 13:46:33 -05:00
if ( SessionManager . getSession ( lastClaimer ). getSessionID () !=
this . lastClaimerSessionID ) {
this . lastClaimer = null ;
this . lastClaimerSessionID = null ;
Mine . setLastChange ( System . currentTimeMillis ());
return false ;
}
}
2023-01-19 13:27:26 -05:00
2023-01-19 13:46:33 -05:00
// Successful claim
return true ;
}
2023-01-19 13:27:26 -05:00
2023-01-19 13:46:33 -05:00
public boolean depositMineResources () {
2023-01-19 13:27:26 -05:00
2023-01-19 14:43:08 -05:00
if ( this . owningGuild . isErrant ())
2023-01-19 13:46:33 -05:00
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( this . owningGuild . getOwnedCity () == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( this . owningGuild . getOwnedCity (). getWarehouse () == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
ItemBase resourceIB = ItemBase . getItemBase ( this . production . UUID );
return this . owningGuild . getOwnedCity (). getWarehouse (). depositFromMine ( this , resourceIB , this . getModifiedProductionAmount ());
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
public boolean updateGuildOwner ( PlayerCharacter pc ) {
Building mineBuilding = BuildingManager . getBuildingFromCache ( this . buildingID );
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//should never return null, but let's check just in case.
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( mineBuilding == null ) {
ChatManager . chatSystemError ( pc , "Unable to find mine tower." );
Logger . debug ( "Failed to Update Mine with UID " + this . getObjectUUID () + ". Unable to Load Building with UID " + this . buildingID );
return false ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( pc == null ) {
2023-01-19 14:43:08 -05:00
this . owningGuild = Guild . getErrantGuild ();
2023-01-19 13:46:33 -05:00
this . guildName = "None" ;
this . guildTag = GuildTag . ERRANT ;
this . nationName = "None" ;
this . nationTag = GuildTag . ERRANT ;
//Update Building.
mineBuilding . setOwner ( null );
WorldGrid . updateObject ( mineBuilding );
return true ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( SessionManager . getSession ( pc ) != null ) {
this . lastClaimerSessionID = SessionManager . getSession ( pc ). getSessionID ();
} else {
Logger . error ( "Failed to find session for player " + pc . getObjectUUID ());
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
return false ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
Guild guild = pc . getGuild ();
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( guild . getOwnedCity () == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( ! MineQueries . CHANGE_OWNER ( this , guild . getObjectUUID ())) {
Logger . debug ( "Database failed to Change Ownership of Mine with UID " + this . getObjectUUID ());
ChatManager . chatSystemError ( pc , "Failed to claim Mine." );
return false ;
}
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//All tests passed.
//update mine.
this . owningGuild = guild ;
// this.guildName = this.owningGuild.getName();
// this.guildTag = this.owningGuild.getGuildTag();
//
// //nation will never return null, read getNation()
// Guild nation = this.owningGuild.getNation();
// this.nationName = nation.getName();
// this.nationTag = nation.getGuildTag();
//Update Building.
PlayerCharacter guildLeader = ( PlayerCharacter ) Guild . GetGL ( this . owningGuild );
if ( guildLeader != null )
mineBuilding . setOwner ( guildLeader );
WorldGrid . updateObject ( mineBuilding );
return true ;
}
public boolean isExpansion () {
2022-04-30 09:41:17 -04:00
return ( this . flags & 2 ) != 0 ;
}
2023-01-19 13:46:33 -05:00
public int getModifiedProductionAmount () {
//TODO Calculate Distance modifications.
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//calculate base values.
int baseProduction = this . production . baseProduction ;
float baseModValue = this . production . baseProduction * . 1f ;
float rankModValue = this . production . baseProduction * . 0143f ;
float totalModded = 0 ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
//get Mine Building.
Building mineBuilding = BuildingManager . getBuilding ( this . buildingID );
if ( mineBuilding == null )
return this . production . baseProduction ;
for ( AbstractCharacter harvester : mineBuilding . getHirelings (). keySet ()) {
totalModded += baseModValue ;
totalModded += rankModValue * harvester . getRank ();
}
//add base production on top;
totalModded += baseProduction ;
//skip distance check for expansion.
if ( this . isExpansion ())
return ( int ) totalModded ;
2022-04-30 09:41:17 -04:00
2023-01-19 14:43:08 -05:00
if ( this . owningGuild . isErrant () == false ) {
2023-01-19 13:46:33 -05:00
if ( this . owningGuild . getOwnedCity () != null ) {
float distanceSquared = this . owningGuild . getOwnedCity (). getLoc (). distanceSquared2D ( mineBuilding . getLoc ());
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
if ( distanceSquared > sqr ( 10000 * 3 ))
totalModded *= . 25f ;
else if ( distanceSquared > sqr ( 10000 * 2 ))
totalModded *= . 50f ;
else if ( distanceSquared > sqr ( 10000 ))
totalModded *= . 75f ;
}
}
return ( int ) totalModded ;
2022-04-30 09:41:17 -04:00
2023-01-19 13:46:33 -05:00
}
2022-04-30 09:41:17 -04:00
}