2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.objects ;
import engine.Enum ;
import engine.InterestManagement.HeightMap ;
import engine.db.archive.DataWarehouse ;
import engine.gameManager.DbManager ;
import engine.gameManager.ZoneManager ;
import engine.math.Bounds ;
import engine.math.Vector2f ;
import engine.math.Vector3fImmutable ;
import engine.net.ByteBufferWriter ;
import engine.server.MBServerStatics ;
import org.pmw.tinylog.Logger ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Set ;
import java.util.concurrent.ConcurrentHashMap ;
public class Zone extends AbstractGameObject {
2023-07-15 09:23:48 -04:00
public final Set < Building > zoneBuildingSet = Collections . newSetFromMap ( new ConcurrentHashMap <> ());
public final Set < NPC > zoneNPCSet = Collections . newSetFromMap ( new ConcurrentHashMap <> ());
public final Set < Mob > zoneMobSet = Collections . newSetFromMap ( new ConcurrentHashMap <> ());
2023-09-20 15:53:41 -04:00
public final int playerCityUUID ;
2023-09-20 15:43:01 -04:00
public final String zoneName ;
2023-09-20 15:53:41 -04:00
public final float xOffset ;
public final float zOffset ;
public final float yOffset ;
public final int zoneTemplate ;
2023-09-20 15:43:01 -04:00
public final byte peaceZone ;
public final String Icon1 ;
public final String Icon2 ;
public final String Icon3 ;
2023-07-15 09:23:48 -04:00
public float absX = 0 . 0f ;
public float absY = 0 . 0f ;
public float absZ = 0 . 0f ;
public int minLvl ;
public int maxLvl ;
public boolean hasBeenHotzone = false ;
2023-09-20 15:43:01 -04:00
public ArrayList < Zone > nodes = null ;
public int parentZoneID ;
public Zone parent = null ;
public Bounds bounds ;
public boolean isNPCCity = false ;
2023-09-20 15:53:41 -04:00
public boolean isGuildZone ;
2023-09-20 15:43:01 -04:00
public String hash ;
2023-09-12 14:51:04 -04:00
public float worldAltitude = 0 ;
2023-09-20 15:43:01 -04:00
public float seaLevel = 0f ;
2023-08-01 19:19:39 -05:00
public static final Set < Mob > respawnQue = Collections . newSetFromMap ( new ConcurrentHashMap <> ());
2023-07-27 21:22:20 -05:00
public static long lastRespawn = 0 ;
2023-09-20 15:53:41 -04:00
public Bounds blendBounds ;
2023-09-17 07:42:46 -04:00
2023-07-15 09:23:48 -04:00
/**
* ResultSet Constructor
*/
public Zone ( ResultSet rs ) throws SQLException {
super ( rs );
this . parentZoneID = rs . getInt ( "parent" );
2023-09-20 15:53:41 -04:00
this . playerCityUUID = rs . getInt ( "playerCityUUID" );
this . isGuildZone = this . playerCityUUID != 0 ;
2023-09-20 16:00:16 -04:00
this . zoneName = rs . getString ( "zoneName" );
2023-09-20 15:53:41 -04:00
this . xOffset = rs . getFloat ( "xOffset" );
this . zOffset = rs . getFloat ( "zOffset" );
this . yOffset = rs . getFloat ( "yOffset" );
2023-09-20 16:01:01 -04:00
this . zoneTemplate = rs . getInt ( "template" );
2023-09-20 16:03:55 -04:00
this . peaceZone = rs . getByte ( "peaceZone" );
2023-09-20 15:53:41 -04:00
this . Icon1 = rs . getString ( "icon1" );
this . Icon2 = rs . getString ( "icon2" );
this . Icon3 = rs . getString ( "icon3" );
this . minLvl = rs . getInt ( "min_level" );
this . maxLvl = rs . getInt ( "max_level" );
2023-07-15 09:23:48 -04:00
this . hash = rs . getString ( "hash" );
2023-09-17 23:46:00 -04:00
//this needs to be here specifically for new zones created after server boot (e.g. player city zones)
Zone parentZone = ZoneManager . getZoneByUUID ( parentZoneID );
2023-07-15 09:23:48 -04:00
this . setParent ( parentZone );
if ( this . minLvl == 0 && parentZone != null ) {
this . minLvl = parentZone . minLvl ;
this . maxLvl = parentZone . maxLvl ;
}
if ( parentZone != null )
parentZone . addNode ( this );
// If zone doesn't yet hava a hash then write it back to the zone table
if ( hash == null )
setHash ();
}
public static void serializeForClientMsg ( Zone zone , ByteBufferWriter writer ) {
2023-09-20 15:53:41 -04:00
if ( zone . zoneTemplate == 0 && zone . playerCityUUID == 0 )
2023-07-15 09:23:48 -04:00
Logger . warn ( "Warning! WorldServerMap with ID " + zone . getObjectUUID () + " has a loadnum of 0 (player city) and no city linked. This will probably crash the client!" );
// Player City Terraform values serialized here.
2023-09-20 15:53:41 -04:00
if ( zone . playerCityUUID > 0 ) {
2023-07-15 09:23:48 -04:00
writer . put (( byte ) 1 ); // Player City - True
2023-09-17 12:23:06 -04:00
writer . putFloat ( Enum . CityBoundsType . ZONE . halfExtents );
writer . putFloat ( Enum . CityBoundsType . ZONE . halfExtents );
2023-07-15 09:23:48 -04:00
} else
writer . put (( byte ) 0 ); // Player City - False
2023-09-20 15:53:41 -04:00
writer . putFloat ( zone . xOffset );
writer . putFloat ( zone . zOffset );
writer . putFloat ( zone . yOffset );
2023-07-15 09:23:48 -04:00
writer . putInt ( 0 );
writer . putInt ( 0 );
2023-09-20 15:53:41 -04:00
writer . putInt ( zone . zoneTemplate );
2023-07-15 09:23:48 -04:00
2023-09-20 15:53:41 -04:00
if ( zone . playerCityUUID > 0 ) {
City k = City . getCity ( zone . playerCityUUID );
2023-07-15 09:23:48 -04:00
if ( k != null ) {
writer . putInt ( k . getObjectType (). ordinal ());
writer . putInt ( k . getObjectUUID ());
} else
writer . putLong ( 0x0 );
} else {
writer . putInt ( zone . getObjectType (). ordinal ());
writer . putInt ( zone . getObjectUUID ());
}
writer . putInt ( zone . nodes . size ());
2023-09-20 15:53:41 -04:00
City city = City . getCity ( zone . playerCityUUID );
2023-07-15 09:23:48 -04:00
if ( city != null )
writer . putString ( city . getCityName ());
else
writer . putString ( zone . zoneName );
2023-09-20 15:43:01 -04:00
writer . put ( zone . peaceZone );
2023-07-15 09:23:48 -04:00
writer . putString ( zone . Icon1 );
writer . putString ( zone . Icon2 );
writer . putString ( zone . Icon3 );
writer . put (( byte ) 0 ); // Pad
2023-05-23 10:27:03 -04:00
2023-07-15 09:23:48 -04:00
for ( Zone child : zone . nodes ) {
Zone . serializeForClientMsg ( child , writer );
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
/* Method sets a default value for player cities
* otherwise using values derived from the loadnum
* field in the obj_zone database table.
*/
public void setBounds () {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Set initial bounds object
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
this . bounds = Bounds . borrow ();
2022-04-30 09:41:17 -04:00
2023-09-20 15:53:41 -04:00
Vector2f zoneSize = ZoneManager . _zone_size_data . get ( this . zoneTemplate );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Default to player zone size on error? Maybe log this
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( zoneSize != null )
this . bounds . setBounds ( new Vector2f ( this . absX , this . absZ ), zoneSize , 0 . 0f );
else
2023-09-17 12:23:06 -04:00
bounds . setBounds ( new Vector2f ( this . absX , this . absZ ), new Vector2f ( Enum . CityBoundsType . ZONE . halfExtents , Enum . CityBoundsType . ZONE . halfExtents ), 0 . 0f );
2022-04-30 09:41:17 -04:00
2023-09-17 07:42:46 -04:00
HeightMap heightMap = this . getHeightMap ();
// Set heightmap blending bounds
2023-09-17 12:13:46 -04:00
if ( heightMap == null ) {
2023-09-20 15:53:41 -04:00
this . blendBounds = bounds ;
2023-09-17 12:13:46 -04:00
} else {
2023-09-20 15:53:41 -04:00
this . blendBounds = Bounds . borrow ();
this . blendBounds . setBounds ( new Vector2f ( this . absX , this . absZ ), bounds . getHalfExtents (). subtract ( heightMap . zone_minBlend , heightMap . zone_minBlend ), 0 . 0f );
2023-09-17 07:42:46 -04:00
}
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void setParent ( final Zone value ) {
2023-05-23 10:27:03 -04:00
2023-07-15 09:23:48 -04:00
this . parent = value ;
this . parentZoneID = ( this . parent != null ) ? this . parent . getObjectUUID () : 0 ;
2023-05-23 10:27:03 -04:00
2023-09-12 15:06:26 -04:00
// Seafloor
if ( this . parent == null ) {
2023-09-20 15:53:41 -04:00
this . absX = this . xOffset ;
2023-09-12 15:09:52 -04:00
this . absY = MBServerStatics . SEA_FLOOR_ALTITUDE ;
2023-09-20 15:53:41 -04:00
this . absZ = this . zOffset ;
2023-09-12 14:57:47 -04:00
this . seaLevel = 0 ;
2023-09-17 23:53:06 -04:00
this . setBounds ();
2023-09-12 14:57:47 -04:00
return ;
}
2023-09-20 15:53:41 -04:00
this . absX = this . xOffset + parent . absX ;
this . absY = this . yOffset + parent . absY ;
this . absZ = this . zOffset + parent . absZ ;
2023-09-12 15:09:52 -04:00
if ( this . minLvl == 0 || this . maxLvl == 0 ) {
this . minLvl = this . parent . minLvl ;
this . maxLvl = this . parent . maxLvl ;
}
2023-09-17 23:53:06 -04:00
this . setBounds ();
2023-09-14 12:07:08 -04:00
this . worldAltitude = ZoneManager . caclulateWorldAltitude ( this );
2023-09-20 15:43:01 -04:00
if ( this . parent == null ) {
2023-09-14 12:07:08 -04:00
this . seaLevel = MBServerStatics . SEA_FLOOR_ALTITUDE ;
2023-09-14 09:13:02 -04:00
return ;
}
if ( this . getHeightMap () == null ) {
this . seaLevel = this . parent . seaLevel ;
return ;
}
2023-09-17 12:53:56 -04:00
if ( this . getHeightMap (). seaLevel != 0 )
this . seaLevel = this . worldAltitude + this . getHeightMap (). seaLevel ;
2023-09-12 14:57:47 -04:00
else
this . seaLevel = this . parent . seaLevel ;
2023-05-23 10:27:03 -04:00
2023-07-15 09:23:48 -04:00
}
2023-05-23 10:27:03 -04:00
2023-07-15 09:23:48 -04:00
public boolean isMacroZone () {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Macro zones have icons.
2022-04-30 09:41:17 -04:00
2023-09-20 15:53:41 -04:00
if ( this . isGuildZone == true )
2023-07-15 09:23:48 -04:00
return false ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( this . parent == null )
return false ;
2022-04-30 09:41:17 -04:00
2023-09-20 15:43:01 -04:00
return ! Icon1 . equals ( "" );
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public Vector3fImmutable getLoc () {
return new Vector3fImmutable ( this . absX , this . absY , this . absZ );
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public int getParentZoneID () {
return this . parentZoneID ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public ArrayList < Zone > getNodes () {
if ( this . nodes == null ) {
this . nodes = DbManager . ZoneQueries . GET_MAP_NODES ( super . getObjectUUID ());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//Add reverse lookup for child->parent
if ( this . nodes != null )
for ( Zone zone : this . nodes ) {
zone . setParent ( this );
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return nodes ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
/*
* Serializing
*/
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void addNode ( Zone child ) {
this . nodes . add ( child );
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
@Override
public void updateDatabase () {
// TODO Auto-generated method stub
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public boolean isContinent () {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( this . equals ( ZoneManager . getSeaFloor ()))
return false ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( this . getNodes (). isEmpty ())
return false ;
2023-03-21 03:20:57 -04:00
2023-07-15 09:23:48 -04:00
if ( this . getNodes (). get ( 0 ). isMacroZone ())
return true ;
2023-03-21 03:20:57 -04:00
2023-09-20 15:43:01 -04:00
return this . parent . equals ( ZoneManager . getSeaFloor ());
2023-04-03 15:56:36 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void setHash () {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
this . hash = DataWarehouse . hasher . encrypt ( this . getObjectUUID ());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Write hash to player character table
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
DataWarehouse . writeHash ( Enum . DataRecordType . ZONE , this . getObjectUUID ());
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Return heightmap for this Zone.
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public HeightMap getHeightMap () {
2023-05-23 10:27:03 -04:00
2023-09-20 15:53:41 -04:00
if ( this . isGuildZone )
2023-07-15 09:23:48 -04:00
return HeightMap . PlayerCityHeightMap ;
2022-04-30 09:41:17 -04:00
2023-09-20 15:53:41 -04:00
return HeightMap . heightmapByLoadNum . get ( this . zoneTemplate );
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
}