2012-01-29 19:28:19 +00:00
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
2011-10-03 18:41:19 +00:00
#include "BlockID.h"
#include "cWorld.h"
2011-11-09 01:31:19 +00:00
#include "cRedstone.h"
2012-03-14 20:56:09 +00:00
#include "ChunkDef.h"
2011-10-03 18:41:19 +00:00
#include "cClientHandle.h"
#include "cPickup.h"
#include "cBlockToPickup.h"
#include "cPlayer.h"
#include "cServer.h"
#include "cItem.h"
#include "cRoot.h"
#include "../iniFile/iniFile.h"
#include "cChunkMap.h"
2011-12-26 20:57:12 +00:00
#include "cSimulatorManager.h"
2011-10-03 18:41:19 +00:00
#include "cWaterSimulator.h"
2011-11-06 09:23:20 +00:00
#include "cLavaSimulator.h"
2011-12-28 21:00:35 +00:00
#include "cFireSimulator.h"
2011-12-26 20:57:12 +00:00
#include "cSandSimulator.h"
2012-03-01 15:18:59 +00:00
#include "cRedstoneSimulator.h"
2011-10-03 18:41:19 +00:00
#include "cChicken.h"
#include "cSpider.h"
2011-10-25 23:46:01 +00:00
#include "cCow.h" //cow
#include "cSquid.h" //Squid
#include "cWolf.h" //wolf
#include "cSlime.h" //slime
#include "cSkeleton.h" //Skeleton
#include "cSilverfish.h" //Silverfish
#include "cPig.h" //pig
#include "cSheep.h" //sheep
#include "cZombie.h" //zombie
#include "cEnderman.h" //enderman
#include "cCreeper.h" //creeper
#include "cCavespider.h" //cavespider
#include "cGhast.h" //Ghast
#include "cZombiepigman.h" //Zombiepigman
2011-11-01 21:57:08 +00:00
#include "cMakeDir.h"
2011-12-24 23:34:30 +00:00
#include "cChunkGenerator.h"
2011-12-26 09:09:47 +00:00
#include "MersenneTwister.h"
2012-02-13 21:47:03 +00:00
#include "cTracer.h"
2012-05-25 07:18:52 +00:00
#include "Trees.h"
2011-10-03 18:41:19 +00:00
#include "packets/cPacket_TimeUpdate.h"
2011-11-09 23:24:51 +00:00
#include "packets/cPacket_NewInvalidState.h"
#include "packets/cPacket_Thunderbolt.h"
2011-10-03 18:41:19 +00:00
#include "Vector3d.h"
#include <time.h>
#include "tolua++.h"
#ifndef _WIN32
2012-01-29 19:28:19 +00:00
#include <stdlib.h>
2011-10-03 18:41:19 +00:00
#endif
2011-11-06 09:23:20 +00:00
2012-01-29 19:28:19 +00:00
2012-02-13 21:47:03 +00:00
/// Up to this many m_SpreadQueue elements are handled each world tick
const int MAX_LIGHTING_SPREAD_PER_TICK = 10 ;
2011-10-03 18:41:19 +00:00
float cWorld :: m_Time = 0.f ;
2012-02-13 21:47:03 +00:00
2012-02-18 17:53:22 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cWorldLoadProgress:
/// A simple thread that displays the progress of world loading / saving in cWorld::InitializeSpawn()
class cWorldLoadProgress :
public cIsThread
{
public :
cWorldLoadProgress ( cWorld * a_World ) :
cIsThread ( "cWorldLoadProgress" ),
m_World ( a_World )
{
Start ();
}
2012-05-25 07:18:52 +00:00
void Stop ( void )
{
m_ShouldTerminate = true ;
Wait ();
}
2012-02-18 17:53:22 +00:00
protected :
cWorld * m_World ;
virtual void Execute ( void ) override
{
for (;;)
{
LOG ( "%d chunks to load, %d chunks to generate" ,
m_World -> GetStorage (). GetLoadQueueLength (),
m_World -> GetGenerator (). GetQueueLength ()
);
// Wait for 2 sec, but be "reasonably wakeable" when the thread is to finish
for ( int i = 0 ; i < 20 ; i ++ )
{
cSleep :: MilliSleep ( 100 );
2012-03-10 17:37:00 +00:00
if ( m_ShouldTerminate )
2012-02-18 17:53:22 +00:00
{
return ;
}
}
} // for (-ever)
}
} ;
2012-05-25 07:18:52 +00:00
/// A simple thread that displays the progress of world lighting in cWorld::InitializeSpawn()
class cWorldLightingProgress :
public cIsThread
{
public :
cWorldLightingProgress ( cLightingThread * a_Lighting ) :
cIsThread ( "cWorldLightingProgress" ),
m_Lighting ( a_Lighting )
{
Start ();
}
void Stop ( void )
{
m_ShouldTerminate = true ;
Wait ();
}
protected :
cLightingThread * m_Lighting ;
virtual void Execute ( void ) override
{
for (;;)
{
LOG ( "%d chunks remaining to light" , m_Lighting -> GetQueueLength ()
);
// Wait for 2 sec, but be "reasonably wakeable" when the thread is to finish
for ( int i = 0 ; i < 20 ; i ++ )
{
cSleep :: MilliSleep ( 100 );
if ( m_ShouldTerminate )
{
return ;
}
}
} // for (-ever)
}
} ;
2012-02-18 17:53:22 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cWorld:
2011-10-03 18:41:19 +00:00
cWorld * cWorld :: GetWorld ()
{
2012-03-10 22:27:24 +00:00
LOGWARN ( "WARNING: Using deprecated function cWorld::GetWorld() use cRoot::Get()->GetDefaultWorld() instead!" );
return cRoot :: Get () -> GetDefaultWorld ();
2011-10-03 18:41:19 +00:00
}
2012-02-08 12:36:54 +00:00
2011-10-03 18:41:19 +00:00
cWorld ::~ cWorld ()
{
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSEntities );
while ( m_AllEntities . begin () != m_AllEntities . end () )
{
cEntity * Entity = * m_AllEntities . begin ();
m_AllEntities . remove ( Entity );
if ( ! Entity -> IsDestroyed () )
{
Entity -> Destroy ();
}
delete Entity ;
}
2011-10-03 18:41:19 +00:00
}
2011-12-26 20:57:12 +00:00
delete m_SimulatorManager ;
delete m_SandSimulator ;
2011-10-03 18:41:19 +00:00
delete m_WaterSimulator ;
2011-11-06 09:23:20 +00:00
delete m_LavaSimulator ;
2011-12-28 21:00:35 +00:00
delete m_FireSimulator ;
2012-03-01 15:18:59 +00:00
delete m_RedstoneSimulator ;
2011-10-03 18:41:19 +00:00
2012-02-13 21:47:03 +00:00
m_Generator . Stop ();
2012-03-11 22:29:15 +00:00
m_ChunkSender . Stop ();
2011-10-03 18:41:19 +00:00
2012-02-13 21:47:03 +00:00
UnloadUnusedChunks ();
m_Storage . WaitForFinish ();
2011-12-28 16:01:15 +00:00
2012-02-13 21:47:03 +00:00
delete m_ChunkMap ;
2011-10-03 18:41:19 +00:00
}
2012-02-08 12:36:54 +00:00
2012-02-13 21:47:03 +00:00
cWorld :: cWorld ( const AString & a_WorldName )
: m_SpawnMonsterTime ( 0.f )
2011-11-09 01:31:19 +00:00
, m_RSList ( 0 )
2012-03-07 13:36:30 +00:00
, m_Weather ( eWeather_Sunny )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
LOG ( "cWorld::cWorld(%s)" , a_WorldName . c_str ());
m_WorldName = a_WorldName ;
2012-03-12 19:39:41 +00:00
m_IniFileName = m_WorldName + "/world.ini" ;
2011-10-03 18:41:19 +00:00
2012-02-13 21:47:03 +00:00
cMakeDir :: MakeDir ( m_WorldName . c_str ());
2011-10-03 18:41:19 +00:00
2011-12-26 09:09:47 +00:00
MTRand r1 ;
m_SpawnX = ( double )(( r1 . randInt () % 1000 ) - 500 );
2012-03-14 20:56:09 +00:00
m_SpawnY = cChunkDef :: Height ;
2011-12-26 09:09:47 +00:00
m_SpawnZ = ( double )(( r1 . randInt () % 1000 ) - 500 );
2012-03-07 13:36:30 +00:00
m_GameMode = eGameMode_Creative ;
2011-10-03 18:41:19 +00:00
2012-02-18 17:53:22 +00:00
AString StorageSchema ( "Default" );
2011-12-26 23:23:05 +00:00
2012-03-12 19:39:41 +00:00
cIniFile IniFile ( m_IniFileName );
2011-10-03 18:41:19 +00:00
if ( IniFile . ReadFile () )
{
m_SpawnX = IniFile . GetValueF ( "SpawnPosition" , "X" , m_SpawnX );
m_SpawnY = IniFile . GetValueF ( "SpawnPosition" , "Y" , m_SpawnY );
m_SpawnZ = IniFile . GetValueF ( "SpawnPosition" , "Z" , m_SpawnZ );
2012-03-07 13:36:30 +00:00
m_GameMode = ( eGameMode ) IniFile . GetValueI ( "GameMode" , "GameMode" , m_GameMode );
2012-02-18 17:53:22 +00:00
StorageSchema = IniFile . GetValue ( "Storage" , "Schema" , StorageSchema );
2011-10-03 18:41:19 +00:00
}
else
{
IniFile . SetValueF ( "SpawnPosition" , "X" , m_SpawnX );
IniFile . SetValueF ( "SpawnPosition" , "Y" , m_SpawnY );
IniFile . SetValueF ( "SpawnPosition" , "Z" , m_SpawnZ );
2011-11-01 23:05:47 +00:00
IniFile . SetValueI ( "GameMode" , "GameMode" , m_GameMode );
2012-02-18 17:53:22 +00:00
IniFile . SetValue ( "Storage" , "Schema" , StorageSchema );
2011-10-03 18:41:19 +00:00
if ( ! IniFile . WriteFile () )
{
2012-03-12 19:39:41 +00:00
LOG ( "WARNING: Could not write to %s" , m_IniFileName . c_str ());
2011-10-03 18:41:19 +00:00
}
}
2012-05-25 07:18:52 +00:00
m_Lighting . Start ( this );
2012-02-13 21:47:03 +00:00
m_Storage . Start ( this , StorageSchema );
2012-05-25 07:18:52 +00:00
m_Generator . Start ( this , IniFile );
2011-10-03 18:41:19 +00:00
m_bAnimals = true ;
m_SpawnMonsterRate = 10 ;
cIniFile IniFile2 ( "settings.ini" );
if ( IniFile2 . ReadFile () )
{
m_bAnimals = IniFile2 . GetValueB ( "Monsters" , "AnimalsOn" , true );
m_SpawnMonsterRate = ( float ) IniFile2 . GetValueF ( "Monsters" , "AnimalSpawnInterval" , 10 );
2011-12-23 23:58:54 +00:00
SetMaxPlayers ( IniFile2 . GetValueI ( "Server" , "MaxPlayers" , 9001 ));
m_Description = IniFile2 . GetValue ( "Server" , "Description" , "MCServer! - It's OVER 9000!" ). c_str ();
2011-10-03 18:41:19 +00:00
}
2012-01-29 21:40:21 +00:00
m_ChunkMap = new cChunkMap ( this );
2012-03-05 16:41:57 +00:00
m_ChunkSender . Start ( this );
2011-10-03 18:41:19 +00:00
m_Time = 0 ;
m_WorldTimeFraction = 0.f ;
m_WorldTime = 0 ;
m_LastSave = 0 ;
m_LastUnload = 0 ;
2011-12-26 20:57:12 +00:00
//Simulators:
2011-10-03 18:41:19 +00:00
m_WaterSimulator = new cWaterSimulator ( this );
2011-11-06 09:23:20 +00:00
m_LavaSimulator = new cLavaSimulator ( this );
2011-12-26 20:57:12 +00:00
m_SandSimulator = new cSandSimulator ( this );
2011-12-28 21:00:35 +00:00
m_FireSimulator = new cFireSimulator ( this );
2012-03-01 15:18:59 +00:00
m_RedstoneSimulator = new cRedstoneSimulator ( this );
2011-12-26 20:57:12 +00:00
2011-12-26 23:23:05 +00:00
m_SimulatorManager = new cSimulatorManager ();
m_SimulatorManager -> RegisterSimulator ( m_WaterSimulator , 6 );
m_SimulatorManager -> RegisterSimulator ( m_LavaSimulator , 12 );
2011-12-26 20:57:12 +00:00
m_SimulatorManager -> RegisterSimulator ( m_SandSimulator , 1 );
2011-12-28 21:00:35 +00:00
m_SimulatorManager -> RegisterSimulator ( m_FireSimulator , 10 );
2012-03-01 15:18:59 +00:00
m_SimulatorManager -> RegisterSimulator ( m_RedstoneSimulator , 1 );
2011-10-03 18:41:19 +00:00
}
2012-02-08 12:36:54 +00:00
2012-03-07 13:36:30 +00:00
void cWorld :: SetWeather ( eWeather a_Weather )
2011-11-09 23:24:51 +00:00
{
2012-03-07 13:36:30 +00:00
switch ( a_Weather )
{
case eWeather_Sunny :
{
m_Weather = a_Weather ;
cPacket_NewInvalidState WeatherPacket ;
WeatherPacket . m_Reason = 2 ; //stop rain
Broadcast ( WeatherPacket );
}
break ;
case eWeather_Rain :
{
m_Weather = a_Weather ;
cPacket_NewInvalidState WeatherPacket ;
WeatherPacket . m_Reason = 1 ; //begin rain
Broadcast ( WeatherPacket );
}
break ;
case eWeather_ThunderStorm :
{
m_Weather = a_Weather ;
cPacket_NewInvalidState WeatherPacket ;
WeatherPacket . m_Reason = 1 ; //begin rain
Broadcast ( WeatherPacket );
CastThunderbolt ( 0 , 0 , 0 ); //start thunderstorm with a lightning strike at 0, 0, 0. >:D
}
break ;
default :
LOGWARN ( "Trying to set unknown weather %d" , a_Weather );
break ;
2011-11-09 23:24:51 +00:00
}
}
2012-02-13 21:47:03 +00:00
2012-03-07 13:36:30 +00:00
void cWorld :: CastThunderbolt ( int a_X , int a_Y , int a_Z )
2012-02-13 21:47:03 +00:00
{
2011-11-09 23:24:51 +00:00
cPacket_Thunderbolt ThunderboltPacket ;
2012-03-07 13:36:30 +00:00
ThunderboltPacket . m_xLBPos = a_X ;
ThunderboltPacket . m_yLBPos = a_Y ;
ThunderboltPacket . m_zLBPos = a_Z ;
Broadcast ( ThunderboltPacket ); // FIXME: Broadcast to chunk instead of entire world
2011-11-09 23:24:51 +00:00
}
2012-02-08 12:36:54 +00:00
2012-05-29 14:59:43 +00:00
bool cWorld :: IsPlacingItemLegal ( Int16 a_ItemType , int a_BlockX , int a_BlockY , int a_BlockZ )
{
BLOCKTYPE SurfaceBlock = GetBlock ( a_BlockX , a_BlockY - 1 , a_BlockZ );
switch ( a_ItemType )
{
case E_BLOCK_YELLOW_FLOWER : // Can ONLY be placed on dirt/grass
case E_BLOCK_RED_ROSE :
case E_BLOCK_SAPLING :
{
switch ( SurfaceBlock )
{
case E_BLOCK_DIRT :
case E_BLOCK_GRASS :
case E_BLOCK_FARMLAND :
{
return true ;
}
}
return false ;
}
case E_BLOCK_BROWN_MUSHROOM : // Can be placed on pretty much anything, with exceptions
case E_BLOCK_RED_MUSHROOM :
{
switch ( SurfaceBlock )
{
case E_BLOCK_GLASS :
case E_BLOCK_YELLOW_FLOWER :
case E_BLOCK_RED_ROSE :
case E_BLOCK_BROWN_MUSHROOM :
case E_BLOCK_RED_MUSHROOM :
case E_BLOCK_CACTUS :
{
return false ;
}
}
return true ;
}
case E_BLOCK_CACTUS :
{
if (( SurfaceBlock != E_BLOCK_SAND ) && ( SurfaceBlock != E_BLOCK_CACTUS ))
{
// Cactus can only be placed on sand and itself
return false ;
}
// Check surroundings. Cacti may ONLY be surrounded by air
if (
( GetBlock ( a_BlockX - 1 , a_BlockY , a_BlockZ ) != E_BLOCK_AIR ) ||
( GetBlock ( a_BlockX + 1 , a_BlockY , a_BlockZ ) != E_BLOCK_AIR ) ||
( GetBlock ( a_BlockX , a_BlockY , a_BlockZ - 1 ) != E_BLOCK_AIR ) ||
( GetBlock ( a_BlockX , a_BlockY , a_BlockZ + 1 ) != E_BLOCK_AIR )
)
{
return false ;
}
return true ;
}
case E_ITEM_SEEDS :
case E_ITEM_MELON_SEEDS :
case E_ITEM_PUMPKIN_SEEDS :
{
// Seeds can go only on the farmland block:
return ( SurfaceBlock == E_BLOCK_FARMLAND );
}
} // switch (a_Packet->m_ItemType)
return true ;
}
2012-05-30 21:29:51 +00:00
void cWorld :: SetNextBlockTick ( int a_BlockX , int a_BlockY , int a_BlockZ )
{
return m_ChunkMap -> SetNextBlockTick ( a_BlockX , a_BlockY , a_BlockZ );
}
2012-05-25 07:18:52 +00:00
void cWorld :: InitializeSpawn ( void )
2011-10-03 18:41:19 +00:00
{
int ChunkX = 0 , ChunkY = 0 , ChunkZ = 0 ;
BlockToChunk ( ( int ) m_SpawnX , ( int ) m_SpawnY , ( int ) m_SpawnZ , ChunkX , ChunkY , ChunkZ );
2012-02-18 17:53:22 +00:00
2012-02-20 16:39:00 +00:00
// For the debugging builds, don't make the server build too much world upon start:
#ifdef _DEBUG
int ViewDist = 9 ;
#else
2012-02-18 19:20:05 +00:00
int ViewDist = 20 ; // Always prepare an area 20 chunks across, no matter what the actual cClientHandle::VIEWDISTANCE is
2012-02-20 16:39:00 +00:00
#endif // _DEBUG
2012-02-18 17:53:22 +00:00
2012-05-25 07:18:52 +00:00
LOG ( "Preparing spawn area in world \" %s \" ..." , m_WorldName . c_str ());
2012-02-13 21:47:03 +00:00
for ( int x = 0 ; x < ViewDist ; x ++ )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
for ( int z = 0 ; z < ViewDist ; z ++ )
2011-10-03 18:41:19 +00:00
{
2012-05-25 07:18:52 +00:00
m_ChunkMap -> TouchChunk ( x + ChunkX - ( ViewDist - 1 ) / 2 , ZERO_CHUNK_Y , z + ChunkZ - ( ViewDist - 1 ) / 2 ); // Queue the chunk in the generator / loader
2011-10-03 18:41:19 +00:00
}
}
2012-02-13 21:47:03 +00:00
2012-05-25 07:18:52 +00:00
{
// Display progress during this process:
cWorldLoadProgress Progress ( this );
// Wait for the loader to finish loading
m_Storage . WaitForQueuesEmpty ();
// Wait for the generator to finish generating
m_Generator . WaitForQueueEmpty ();
Progress . Stop ();
}
2012-02-18 17:53:22 +00:00
2012-05-25 07:18:52 +00:00
// Light all chunks that have been newly generated:
LOG ( "Lighting spawn area in world \" %s \" ..." , m_WorldName . c_str ());
2012-02-18 17:53:22 +00:00
2012-05-25 07:18:52 +00:00
for ( int x = 0 ; x < ViewDist ; x ++ )
{
int ChX = x + ChunkX - ( ViewDist - 1 ) / 2 ;
for ( int z = 0 ; z < ViewDist ; z ++ )
{
int ChZ = z + ChunkZ - ( ViewDist - 1 ) / 2 ;
if ( ! m_ChunkMap -> IsChunkLighted ( ChX , ChZ ))
{
m_Lighting . QueueChunk ( ChX , ChZ ); // Queue the chunk in the lighting thread
}
} // for z
} // for x
2012-02-18 17:53:22 +00:00
2012-05-25 07:18:52 +00:00
{
cWorldLightingProgress Progress ( & m_Lighting );
m_Lighting . WaitForQueueEmpty ();
Progress . Stop ();
}
// TODO: Better spawn detection - move spawn out of the water if it isn't set in the INI already
2012-02-18 17:53:22 +00:00
m_SpawnY = ( double ) GetHeight ( ( int ) m_SpawnX , ( int ) m_SpawnZ ) + 1.6f ; // +1.6f eye height
2011-10-03 18:41:19 +00:00
}
2012-02-08 12:36:54 +00:00
2011-10-03 18:41:19 +00:00
void cWorld :: Tick ( float a_Dt )
{
2012-02-13 21:47:03 +00:00
m_Time += a_Dt / 1000.f ;
2011-10-03 18:41:19 +00:00
2011-11-06 09:23:20 +00:00
CurrentTick ++ ;
2011-10-03 18:41:19 +00:00
bool bSendTime = false ;
2012-02-13 21:47:03 +00:00
m_WorldTimeFraction += a_Dt / 1000.f ;
while ( m_WorldTimeFraction > 1.f )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
m_WorldTimeFraction -= 1.f ;
m_WorldTime += 20 ;
2011-10-03 18:41:19 +00:00
bSendTime = true ;
}
2012-02-13 21:47:03 +00:00
m_WorldTime %= 24000 ; // 24000 units in a day
if ( bSendTime )
{
Broadcast ( cPacket_TimeUpdate ( ( m_WorldTime ) ) );
}
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSEntities );
for ( cEntityList :: iterator itr = m_AllEntities . begin (); itr != m_AllEntities . end ();)
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
if (( * itr ) -> IsDestroyed ())
{
LOG ( "Destroying entity #%i" , ( * itr ) -> GetUniqueID ());
cEntity * RemoveMe = * itr ;
itr = m_AllEntities . erase ( itr );
m_RemoveEntityQueue . push_back ( RemoveMe );
continue ;
}
( * itr ) -> Tick ( a_Dt );
itr ++ ;
2011-10-03 18:41:19 +00:00
}
}
2011-12-27 22:57:33 +00:00
2012-02-08 12:36:54 +00:00
m_ChunkMap -> Tick ( a_Dt , m_TickRand );
2011-12-26 20:57:12 +00:00
GetSimulatorManager () -> Simulate ( a_Dt );
2012-02-08 12:36:54 +00:00
TickWeather ( a_Dt );
2011-11-09 23:24:51 +00:00
2012-02-18 20:10:57 +00:00
// Asynchronously set blocks:
sSetBlockList FastSetBlockQueueCopy ;
2012-02-13 21:47:03 +00:00
{
cCSLock Lock ( m_CSFastSetBlock );
2012-02-18 20:10:57 +00:00
std :: swap ( FastSetBlockQueueCopy , m_FastSetBlockQueue );
2012-02-13 21:47:03 +00:00
}
2012-02-18 20:10:57 +00:00
m_ChunkMap -> FastSetBlocks ( FastSetBlockQueueCopy );
2012-05-25 07:18:52 +00:00
if ( ! FastSetBlockQueueCopy . empty ())
2011-12-24 23:34:30 +00:00
{
2012-02-18 20:10:57 +00:00
// Some blocks failed, store them for next tick:
cCSLock Lock ( m_CSFastSetBlock );
m_FastSetBlockQueue . splice ( m_FastSetBlockQueue . end (), FastSetBlockQueueCopy );
2011-12-24 23:34:30 +00:00
}
2011-11-09 23:24:51 +00:00
2012-02-13 21:47:03 +00:00
if ( m_Time - m_LastSave > 60 * 5 ) // Save each 5 minutes
2011-10-03 18:41:19 +00:00
{
SaveAllChunks ();
}
2012-02-13 21:47:03 +00:00
if ( m_Time - m_LastUnload > 10 ) // Unload every 10 seconds
2011-10-03 18:41:19 +00:00
{
UnloadUnusedChunks ();
}
2012-02-13 21:47:03 +00:00
// Delete entities queued for removal:
for ( cEntityList :: iterator itr = m_RemoveEntityQueue . begin (); itr != m_RemoveEntityQueue . end (); ++ itr )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
delete * itr ;
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
m_RemoveEntityQueue . clear ();
2011-11-09 01:31:19 +00:00
2012-02-13 21:47:03 +00:00
TickSpawnMobs ( a_Dt );
2011-11-09 01:31:19 +00:00
std :: vector < int > m_RSList_copy ( m_RSList );
2012-02-13 21:47:03 +00:00
m_RSList . clear ();
2011-11-09 01:31:19 +00:00
2011-12-24 23:34:30 +00:00
std :: vector < int >:: const_iterator cii ; // FIXME - Please rename this variable, WTF is cii??? Use human readable variable names or common abbreviations (i, idx, itr, iter)
2011-11-09 01:31:19 +00:00
for ( cii = m_RSList_copy . begin (); cii != m_RSList_copy . end ();)
{
2012-02-13 21:47:03 +00:00
int tempX = * cii ; cii ++ ;
int tempY = * cii ; cii ++ ;
int tempZ = * cii ; cii ++ ;
int state = * cii ; cii ++ ;
2011-11-09 01:31:19 +00:00
2012-02-13 21:47:03 +00:00
if ( ( state == 11111 ) && ( ( int ) GetBlock ( tempX , tempY , tempZ ) == E_BLOCK_REDSTONE_TORCH_OFF ) )
{
2011-11-09 01:31:19 +00:00
FastSetBlock ( tempX , tempY , tempZ , E_BLOCK_REDSTONE_TORCH_ON , ( int ) GetBlockMeta ( tempX , tempY , tempZ ) );
cRedstone Redstone ( this );
Redstone . ChangeRedstone ( tempX , tempY , tempZ , true );
2012-02-13 21:47:03 +00:00
}
else if ( ( state == 00000 ) && ( ( int ) GetBlock ( tempX , tempY , tempZ ) == E_BLOCK_REDSTONE_TORCH_ON ) )
{
2011-11-09 03:04:56 +00:00
FastSetBlock ( tempX , tempY , tempZ , E_BLOCK_REDSTONE_TORCH_OFF , ( int ) GetBlockMeta ( tempX , tempY , tempZ ) );
cRedstone Redstone ( this );
Redstone . ChangeRedstone ( tempX , tempY , tempZ , false );
2011-11-09 01:31:19 +00:00
}
}
m_RSList_copy . erase ( m_RSList_copy . begin (), m_RSList_copy . end ());
2011-10-03 18:41:19 +00:00
}
2012-02-08 12:36:54 +00:00
void cWorld :: TickWeather ( float a_Dt )
{
if ( GetWeather () == 0 ) // if sunny
{
if ( CurrentTick % 19 == 0 ) //every 20 ticks random weather
{
unsigned randWeather = ( m_TickRand . randInt () % 10000 );
if ( randWeather == 0 )
{
LOG ( "Starting Rainstorm!" );
2012-03-07 13:36:30 +00:00
SetWeather ( eWeather_Rain );
2012-02-08 12:36:54 +00:00
}
else if ( randWeather == 1 )
{
LOG ( "Starting Thunderstorm!" );
2012-03-07 13:36:30 +00:00
SetWeather ( eWeather_ThunderStorm );
2012-02-08 12:36:54 +00:00
}
}
}
if ( GetWeather () != 0 ) // if raining or thunderstorm
{
if ( CurrentTick % 19 == 0 ) // every 20 ticks random weather
{
unsigned randWeather = ( m_TickRand . randInt () % 4999 );
if ( randWeather == 0 ) //2% chance per second
{
LOG ( "Back to sunny!" );
2012-03-07 13:36:30 +00:00
SetWeather ( eWeather_Sunny );
2012-02-08 12:36:54 +00:00
}
else if ( ( randWeather > 4000 ) && ( GetWeather () != 2 ) ) // random chance for rainstorm to turn into thunderstorm.
{
LOG ( "Starting Thunderstorm!" );
2012-03-07 13:36:30 +00:00
SetWeather ( eWeather_ThunderStorm );
2012-02-08 12:36:54 +00:00
}
}
}
if ( GetWeather () == 2 ) // if thunderstorm
{
if ( m_TickRand . randInt () % 199 == 0 ) // 0.5% chance per tick of thunderbolt
{
2012-03-07 13:36:30 +00:00
CastThunderbolt ( 0 , 0 , 0 ); // TODO: find random possitions near players to cast thunderbolts.
2012-02-08 12:36:54 +00:00
}
}
}
2012-02-13 21:47:03 +00:00
void cWorld :: TickSpawnMobs ( float a_Dt )
{
if ( ! m_bAnimals || ( m_Time - m_SpawnMonsterTime <= m_SpawnMonsterRate ))
{
return ;
}
m_SpawnMonsterTime = m_Time ;
Vector3d SpawnPos ;
{
cCSLock Lock ( m_CSPlayers );
if ( m_Players . size () <= 0 )
{
return ;
}
int RandomPlayerIdx = m_TickRand . randInt () & m_Players . size ();
cPlayerList :: iterator itr = m_Players . begin ();
for ( int i = 1 ; i < RandomPlayerIdx ; i ++ )
{
itr ++ ;
}
SpawnPos = ( * itr ) -> GetPosition ();
}
cMonster * Monster = NULL ;
int dayRand = m_TickRand . randInt () % 6 ;
int nightRand = m_TickRand . randInt () % 10 ;
SpawnPos += Vector3d ( ( double )( m_TickRand . randInt () % 64 ) - 32 , ( double )( m_TickRand . randInt () % 64 ) - 32 , ( double )( m_TickRand . randInt () % 64 ) - 32 );
2012-02-18 17:53:22 +00:00
int Height = GetHeight ( ( int ) SpawnPos . x , ( int ) SpawnPos . z );
2012-02-13 21:47:03 +00:00
if ( m_WorldTime >= 12000 + 1000 )
{
if ( nightRand == 0 ) //random percent to spawn for night
Monster = new cSpider ();
else if ( nightRand == 1 )
Monster = new cZombie ();
else if ( nightRand == 2 )
Monster = new cEnderman ();
else if ( nightRand == 3 )
Monster = new cCreeper ();
else if ( nightRand == 4 )
Monster = new cCavespider ();
else if ( nightRand == 5 )
Monster = new cGhast ();
else if ( nightRand == 6 )
Monster = new cZombiepigman ();
else if ( nightRand == 7 )
Monster = new cSlime ();
else if ( nightRand == 8 )
Monster = new cSilverfish ();
else if ( nightRand == 9 )
Monster = new cSkeleton ();
//end random percent to spawn for night
}
else
{
if ( dayRand == 0 ) //random percent to spawn for day
Monster = new cChicken ();
else if ( dayRand == 1 )
Monster = new cCow ();
else if ( dayRand == 2 )
Monster = new cPig ();
else if ( dayRand == 3 )
Monster = new cSheep ();
else if ( dayRand == 4 )
Monster = new cSquid ();
else if ( dayRand == 5 )
Monster = new cWolf ();
//end random percent to spawn for day
}
if ( Monster )
{
Monster -> Initialize ( this );
Monster -> TeleportTo ( SpawnPos . x , ( double )( Height ) + 2 , SpawnPos . z );
Monster -> SpawnOn ( 0 );
}
}
2012-05-25 07:18:52 +00:00
void cWorld :: GrowTree ( int a_X , int a_Y , int a_Z )
2012-02-18 17:53:22 +00:00
{
2012-05-25 07:18:52 +00:00
if ( GetBlock ( a_X , a_Y , a_Z ) == E_BLOCK_SAPLING )
2012-02-18 17:53:22 +00:00
{
2012-05-25 07:18:52 +00:00
// There is a sapling here, grow a tree according to its type:
GrowTreeFromSapling ( a_X , a_Y , a_Z , GetBlockMeta ( a_X , a_Y , a_Z ));
2012-02-18 17:53:22 +00:00
}
2012-05-25 07:18:52 +00:00
else
2012-02-18 17:53:22 +00:00
{
2012-05-25 07:18:52 +00:00
// There is nothing here, grow a tree based on the current biome here:
GrowTreeByBiome ( a_X , a_Y , a_Z );
2012-02-18 17:53:22 +00:00
}
}
2012-05-25 07:18:52 +00:00
void cWorld :: GrowTreeFromSapling ( int a_X , int a_Y , int a_Z , char a_SaplingMeta )
2011-10-03 18:41:19 +00:00
{
2012-05-25 07:18:52 +00:00
cNoise Noise ( m_Generator . GetSeed ());
sSetBlockVector Blocks ;
switch ( a_SaplingMeta & 0x07 )
2011-10-03 18:41:19 +00:00
{
2012-05-25 07:18:52 +00:00
case E_META_SAPLING_APPLE : GetAppleTreeImage ( a_X , a_Y , a_Z , Noise , ( int )( m_WorldTime & 0xffffffff ), Blocks ); break ;
case E_META_SAPLING_BIRCH : GetBirchTreeImage ( a_X , a_Y , a_Z , Noise , ( int )( m_WorldTime & 0xffffffff ), Blocks ); break ;
case E_META_SAPLING_CONIFER : GetConiferTreeImage ( a_X , a_Y , a_Z , Noise , ( int )( m_WorldTime & 0xffffffff ), Blocks ); break ;
case E_META_SAPLING_JUNGLE : GetJungleTreeImage ( a_X , a_Y , a_Z , Noise , ( int )( m_WorldTime & 0xffffffff ), Blocks ); break ;
2011-10-03 18:41:19 +00:00
}
2012-05-25 07:18:52 +00:00
GrowTreeImage ( Blocks );
}
void cWorld :: GrowTreeByBiome ( int a_X , int a_Y , int a_Z )
{
cNoise Noise ( m_Generator . GetSeed ());
sSetBlockVector Blocks ;
GetTreeImageByBiome ( a_X , a_Y , a_Z , Noise , ( int )( m_WorldTime & 0xffffffff ), ( EMCSBiome ) GetBiomeAt ( a_X , a_Z ), Blocks );
GrowTreeImage ( Blocks );
}
2011-10-03 18:41:19 +00:00
2012-05-25 07:18:52 +00:00
void cWorld :: GrowTreeImage ( const sSetBlockVector & a_Blocks )
{
// Check that the tree has place to grow
// Make a copy of the log blocks:
sSetBlockVector b2 ;
for ( sSetBlockVector :: const_iterator itr = a_Blocks . begin (); itr != a_Blocks . end (); ++ itr )
2012-02-18 19:18:16 +00:00
{
2012-05-25 07:18:52 +00:00
if ( itr -> BlockType == E_BLOCK_LOG )
2012-02-18 19:18:16 +00:00
{
2012-05-25 07:18:52 +00:00
b2 . push_back ( * itr );
}
} // for itr - a_Blocks[]
// Query blocktypes and metas at those log blocks:
if ( ! GetBlocks ( b2 , false ))
{
return ;
}
// Check that at each log's coord there's an block allowed to be overwritten:
for ( sSetBlockVector :: const_iterator itr = b2 . begin (); itr != b2 . end (); ++ itr )
{
switch ( itr -> BlockType )
{
CASE_TREE_ALLOWED_BLOCKS :
2012-02-18 19:18:16 +00:00
{
2012-05-25 07:18:52 +00:00
break ;
2011-10-03 18:41:19 +00:00
}
2012-05-25 07:18:52 +00:00
default :
2012-02-18 19:18:16 +00:00
{
2012-05-25 07:18:52 +00:00
return ;
2012-02-18 19:18:16 +00:00
}
2011-10-03 18:41:19 +00:00
}
2012-05-25 07:18:52 +00:00
} // for itr - b2[]
// All ok, replace blocks with the tree image:
m_ChunkMap -> ReplaceTreeBlocks ( a_Blocks );
}
2011-10-03 18:41:19 +00:00
2012-05-25 07:18:52 +00:00
int cWorld :: GetBiomeAt ( int a_BlockX , int a_BlockZ )
{
return m_ChunkMap -> GetBiomeAt ( a_BlockX , a_BlockZ );
2011-10-03 18:41:19 +00:00
}
2011-12-24 23:34:30 +00:00
2011-10-03 18:41:19 +00:00
void cWorld :: SetBlock ( int a_X , int a_Y , int a_Z , char a_BlockType , char a_BlockMeta )
{
2012-02-21 16:27:30 +00:00
m_ChunkMap -> SetBlock ( a_X , a_Y , a_Z , a_BlockType , a_BlockMeta );
2011-10-03 18:41:19 +00:00
2012-02-21 16:27:30 +00:00
GetSimulatorManager () -> WakeUp ( a_X , a_Y , a_Z );
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
2011-10-03 18:41:19 +00:00
void cWorld :: FastSetBlock ( int a_X , int a_Y , int a_Z , char a_BlockType , char a_BlockMeta )
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSFastSetBlock );
2012-02-18 20:10:57 +00:00
m_FastSetBlockQueue . push_back ( sSetBlock ( a_X , a_Y , a_Z , a_BlockType , a_BlockMeta ));
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
char cWorld :: GetBlock ( int a_X , int a_Y , int a_Z )
2011-10-03 18:41:19 +00:00
{
2012-02-21 12:29:05 +00:00
// First check if it isn't queued in the m_FastSetBlockQueue:
2012-02-13 21:47:03 +00:00
{
2012-02-21 12:29:05 +00:00
int X = a_X , Y = a_Y , Z = a_Z ;
int ChunkX , ChunkY , ChunkZ ;
AbsoluteToRelative ( X , Y , Z , ChunkX , ChunkY , ChunkZ );
cCSLock Lock ( m_CSFastSetBlock );
for ( sSetBlockList :: iterator itr = m_FastSetBlockQueue . begin (); itr != m_FastSetBlockQueue . end (); ++ itr )
{
if (( itr -> x == X ) && ( itr -> y == Y ) && ( itr -> z == Z ) && ( itr -> ChunkX == ChunkX ) && ( itr -> ChunkZ == ChunkZ ))
{
return itr -> BlockType ;
}
} // for itr - m_FastSetBlockQueue[]
2012-02-13 21:47:03 +00:00
}
2012-02-21 12:29:05 +00:00
return m_ChunkMap -> GetBlock ( a_X , a_Y , a_Z );
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
2011-10-03 18:41:19 +00:00
char cWorld :: GetBlockMeta ( int a_X , int a_Y , int a_Z )
{
2012-02-21 12:29:05 +00:00
// First check if it isn't queued in the m_FastSetBlockQueue:
2012-02-13 21:47:03 +00:00
{
2012-02-21 12:29:05 +00:00
cCSLock Lock ( m_CSFastSetBlock );
for ( sSetBlockList :: iterator itr = m_FastSetBlockQueue . begin (); itr != m_FastSetBlockQueue . end (); ++ itr )
{
if (( itr -> x == a_X ) && ( itr -> y == a_Y ) && ( itr -> y == a_Y ))
{
return itr -> BlockMeta ;
}
} // for itr - m_FastSetBlockQueue[]
2012-02-13 21:47:03 +00:00
}
2012-02-21 12:29:05 +00:00
return m_ChunkMap -> GetBlockMeta ( a_X , a_Y , a_Z );
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
2011-10-03 18:41:19 +00:00
void cWorld :: SetBlockMeta ( int a_X , int a_Y , int a_Z , char a_MetaData )
{
2012-02-21 16:27:30 +00:00
m_ChunkMap -> SetBlockMeta ( a_X , a_Y , a_Z , a_MetaData );
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
2012-03-16 15:48:40 +00:00
char cWorld :: GetBlockSkyLight ( int a_X , int a_Y , int a_Z )
{
return m_ChunkMap -> GetBlockSkyLight ( a_X , a_Y , a_Z );
}
2012-05-25 07:18:52 +00:00
void cWorld :: ReplaceBlocks ( const sSetBlockVector & a_Blocks , BLOCKTYPE a_FilterBlockType )
{
m_ChunkMap -> ReplaceBlocks ( a_Blocks , a_FilterBlockType );
}
bool cWorld :: GetBlocks ( sSetBlockVector & a_Blocks , bool a_ContinueOnFailure )
{
return m_ChunkMap -> GetBlocks ( a_Blocks , a_ContinueOnFailure );
}
2011-10-03 18:41:19 +00:00
bool cWorld :: DigBlock ( int a_X , int a_Y , int a_Z , cItem & a_PickupItem )
{
2012-02-21 16:27:30 +00:00
bool res = m_ChunkMap -> DigBlock ( a_X , a_Y , a_Z , a_PickupItem );
if ( res )
2011-10-03 18:41:19 +00:00
{
2011-12-26 20:57:12 +00:00
GetSimulatorManager () -> WakeUp ( a_X , a_Y , a_Z );
2011-10-03 18:41:19 +00:00
}
2012-02-21 16:27:30 +00:00
return res ;
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
void cWorld :: SendBlockTo ( int a_X , int a_Y , int a_Z , cPlayer * a_Player )
2011-10-03 18:41:19 +00:00
{
2012-02-21 16:27:30 +00:00
m_ChunkMap -> SendBlockTo ( a_X , a_Y , a_Z , a_Player );
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
// TODO: This interface is dangerous!
cBlockEntity * cWorld :: GetBlockEntity ( int a_X , int a_Y , int a_Z )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
return NULL ;
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
2012-02-18 17:53:22 +00:00
int cWorld :: GetHeight ( int a_X , int a_Z )
2011-10-03 18:41:19 +00:00
{
2012-02-18 17:53:22 +00:00
return m_ChunkMap -> GetHeight ( a_X , a_Z );
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
const double & cWorld :: GetSpawnY ( void )
2011-10-03 18:41:19 +00:00
{
return m_SpawnY ;
}
2012-02-13 21:47:03 +00:00
2012-02-29 08:40:15 +00:00
void cWorld :: Broadcast ( const cPacket & a_Packet , cClientHandle * a_Exclude )
2011-11-08 02:02:13 +00:00
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSPlayers );
for ( cPlayerList :: iterator itr = m_Players . begin (); itr != m_Players . end (); ++ itr )
2011-11-08 02:02:13 +00:00
{
2012-02-29 08:40:15 +00:00
cClientHandle * ch = ( * itr ) -> GetClientHandle ();
if (( ch == a_Exclude ) || ( ch == NULL ) || ! ch -> IsLoggedIn () || ch -> IsDestroyed ())
2012-02-13 21:47:03 +00:00
{
continue ;
}
2011-11-08 02:02:13 +00:00
( * itr ) -> GetClientHandle () -> Send ( a_Packet );
}
}
2011-12-23 23:58:54 +00:00
2012-02-13 21:47:03 +00:00
2011-12-23 23:58:54 +00:00
2012-02-28 08:10:51 +00:00
void cWorld :: BroadcastToChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , const cPacket & a_Packet , cClientHandle * a_Exclude )
2012-02-20 16:39:00 +00:00
{
m_ChunkMap -> BroadcastToChunk ( a_ChunkX , a_ChunkY , a_ChunkZ , a_Packet , a_Exclude );
}
2012-02-16 13:42:35 +00:00
void cWorld :: BroadcastToChunkOfBlock ( int a_X , int a_Y , int a_Z , cPacket * a_Packet , cClientHandle * a_Exclude )
{
m_ChunkMap -> BroadcastToChunkOfBlock ( a_X , a_Y , a_Z , a_Packet , a_Exclude );
}
void cWorld :: MarkChunkDirty ( int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
m_ChunkMap -> MarkChunkDirty ( a_ChunkX , a_ChunkY , a_ChunkZ );
}
void cWorld :: MarkChunkSaving ( int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
m_ChunkMap -> MarkChunkSaving ( a_ChunkX , a_ChunkY , a_ChunkZ );
}
void cWorld :: MarkChunkSaved ( int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
m_ChunkMap -> MarkChunkSaved ( a_ChunkX , a_ChunkY , a_ChunkZ );
}
2012-05-25 07:18:52 +00:00
void cWorld :: SetChunkData (
2012-03-14 20:56:09 +00:00
int a_ChunkX , int a_ChunkY , int a_ChunkZ ,
const BLOCKTYPE * a_BlockTypes ,
2012-05-25 07:18:52 +00:00
const NIBBLETYPE * a_BlockMeta ,
const NIBBLETYPE * a_BlockLight ,
const NIBBLETYPE * a_BlockSkyLight ,
2012-03-14 20:56:09 +00:00
const cChunkDef :: HeightMap * a_HeightMap ,
2012-05-25 07:18:52 +00:00
const cChunkDef :: BiomeMap * a_BiomeMap ,
2012-03-14 20:56:09 +00:00
cEntityList & a_Entities ,
2012-05-25 07:18:52 +00:00
cBlockEntityList & a_BlockEntities ,
bool a_MarkDirty
2012-03-14 20:56:09 +00:00
)
2012-02-16 13:42:35 +00:00
{
2012-05-25 07:18:52 +00:00
// Validate biomes, if needed:
cChunkDef :: BiomeMap BiomeMap ;
const cChunkDef :: BiomeMap * Biomes = a_BiomeMap ;
if ( a_BiomeMap == NULL )
{
// The biomes are not assigned, get them from the generator:
Biomes = & BiomeMap ;
m_Generator . GenerateBiomes ( a_ChunkX , a_ChunkZ , BiomeMap );
}
m_ChunkMap -> SetChunkData (
a_ChunkX , a_ChunkY , a_ChunkZ ,
a_BlockTypes , a_BlockMeta , a_BlockLight , a_BlockSkyLight ,
a_HeightMap , * Biomes ,
a_Entities , a_BlockEntities ,
a_MarkDirty
);
// If a client is requesting this chunk, send it to them:
if ( m_ChunkMap -> HasChunkAnyClients ( a_ChunkX , a_ChunkY , a_ChunkZ ))
{
m_ChunkSender . ChunkReady ( a_ChunkX , a_ChunkY , a_ChunkZ );
}
// Notify the lighting thread that the chunk has become valid (in case it is a neighbor of a postponed chunk):
m_Lighting . ChunkReady ( a_ChunkX , a_ChunkZ );
2012-02-16 13:42:35 +00:00
}
2012-05-25 07:18:52 +00:00
void cWorld :: ChunkLighted (
int a_ChunkX , int a_ChunkZ ,
const cChunkDef :: BlockNibbles & a_BlockLight ,
const cChunkDef :: BlockNibbles & a_SkyLight
2012-03-14 20:56:09 +00:00
)
2012-02-16 13:42:35 +00:00
{
2012-05-25 07:18:52 +00:00
m_ChunkMap -> ChunkLighted ( a_ChunkX , a_ChunkZ , a_BlockLight , a_SkyLight );
2012-02-16 13:42:35 +00:00
}
2012-03-09 13:42:28 +00:00
bool cWorld :: GetChunkData ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , cChunkDataCallback & a_Callback )
2012-02-16 13:42:35 +00:00
{
2012-03-09 13:42:28 +00:00
return m_ChunkMap -> GetChunkData ( a_ChunkX , a_ChunkY , a_ChunkZ , a_Callback );
2012-02-16 13:42:35 +00:00
}
2012-03-14 20:56:09 +00:00
bool cWorld :: GetChunkBlockTypes ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , BLOCKTYPE * a_BlockTypes )
2012-02-18 19:18:16 +00:00
{
2012-03-14 20:56:09 +00:00
return m_ChunkMap -> GetChunkBlockTypes ( a_ChunkX , a_ChunkY , a_ChunkZ , a_BlockTypes );
2012-02-18 19:18:16 +00:00
}
2012-03-14 20:56:09 +00:00
bool cWorld :: GetChunkBlockData ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , BLOCKTYPE * a_BlockData )
2012-03-05 16:41:57 +00:00
{
return m_ChunkMap -> GetChunkBlockData ( a_ChunkX , a_ChunkY , a_ChunkZ , a_BlockData );
}
2012-02-16 13:42:35 +00:00
bool cWorld :: IsChunkValid ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) const
{
return m_ChunkMap -> IsChunkValid ( a_ChunkX , a_ChunkY , a_ChunkZ );
2012-02-16 17:45:26 +00:00
}
bool cWorld :: HasChunkAnyClients ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) const
{
return m_ChunkMap -> HasChunkAnyClients ( a_ChunkX , a_ChunkY , a_ChunkZ );
2012-02-16 13:42:35 +00:00
}
2012-02-20 16:39:00 +00:00
void cWorld :: UnloadUnusedChunks ( void )
{
m_LastUnload = m_Time ;
m_ChunkMap -> UnloadUnusedChunks ();
}
void cWorld :: CollectPickupsByPlayer ( cPlayer * a_Player )
{
m_ChunkMap -> CollectPickupsByPlayer ( a_Player );
}
2011-12-23 23:58:54 +00:00
void cWorld :: SetMaxPlayers ( int iMax )
{
2012-02-13 21:47:03 +00:00
m_MaxPlayers = MAX_PLAYERS ;
2011-12-23 23:58:54 +00:00
if ( iMax > 0 && iMax < MAX_PLAYERS )
{
2012-02-13 21:47:03 +00:00
m_MaxPlayers = iMax ;
2011-12-23 23:58:54 +00:00
}
}
2012-02-08 18:57:04 +00:00
2011-10-03 18:41:19 +00:00
void cWorld :: AddPlayer ( cPlayer * a_Player )
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSPlayers );
2012-02-29 20:25:11 +00:00
ASSERT ( std :: find ( m_Players . begin (), m_Players . end (), a_Player ) == m_Players . end ()); // Is it already in the list? HOW?
2012-02-13 21:47:03 +00:00
m_Players . remove ( a_Player ); // Make sure the player is registered only once
m_Players . push_back ( a_Player );
2011-10-03 18:41:19 +00:00
}
2012-02-08 18:57:04 +00:00
2011-10-03 18:41:19 +00:00
void cWorld :: RemovePlayer ( cPlayer * a_Player )
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSPlayers );
m_Players . remove ( a_Player );
}
2012-02-16 22:28:19 +00:00
bool cWorld :: ForEachPlayer ( cPlayerListCallback & a_Callback )
2012-02-13 21:47:03 +00:00
{
// Calls the callback for each player in the list
cCSLock Lock ( m_CSPlayers );
for ( cPlayerList :: iterator itr = m_Players . begin (); itr != m_Players . end (); ++ itr )
{
2012-02-16 22:28:19 +00:00
if ( a_Callback . Item ( * itr ))
2012-02-13 21:47:03 +00:00
{
return false ;
}
} // for itr - m_Players[]
return true ;
2011-10-03 18:41:19 +00:00
}
2012-02-08 18:57:04 +00:00
2012-02-13 21:47:03 +00:00
// TODO: This interface is dangerous!
2011-10-03 18:41:19 +00:00
cPlayer * cWorld :: GetPlayer ( const char * a_PlayerName )
{
cPlayer * BestMatch = 0 ;
unsigned int MatchedLetters = 0 ;
unsigned int NumMatches = 0 ;
bool bPerfectMatch = false ;
unsigned int NameLength = strlen ( a_PlayerName );
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSPlayers );
for ( cPlayerList :: iterator itr = m_Players . begin (); itr != m_Players . end (); itr ++ )
2011-10-03 18:41:19 +00:00
{
std :: string Name = ( * itr ) -> GetName ();
if ( NameLength > Name . length () ) continue ; // Definitely not a match
2012-02-13 21:47:03 +00:00
for ( unsigned int i = 0 ; i < NameLength ; i ++ )
2011-10-03 18:41:19 +00:00
{
char c1 = ( char ) toupper ( a_PlayerName [ i ] );
char c2 = ( char ) toupper ( Name [ i ] );
if ( c1 == c2 )
{
if ( i + 1 > MatchedLetters )
{
MatchedLetters = i + 1 ;
BestMatch = * itr ;
}
if ( i + 1 == NameLength )
{
NumMatches ++ ;
if ( NameLength == Name . length () )
{
bPerfectMatch = true ;
break ;
}
}
}
else
{
if ( BestMatch == * itr ) BestMatch = 0 ;
break ;
}
if ( bPerfectMatch )
break ;
}
}
2012-02-13 21:47:03 +00:00
if ( NumMatches == 1 )
{
2011-10-03 18:41:19 +00:00
return BestMatch ;
2012-02-13 21:47:03 +00:00
}
2011-10-03 18:41:19 +00:00
2012-02-13 21:47:03 +00:00
// More than one matches, so it's undefined. Return NULL instead
return NULL ;
2011-10-03 18:41:19 +00:00
}
2012-02-01 22:38:03 +00:00
2012-02-13 21:47:03 +00:00
cPlayer * cWorld :: FindClosestPlayer ( const Vector3f & a_Pos , float a_SightLimit )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
cTracer LineOfSight ( this );
float ClosestDistance = a_SightLimit ;
cPlayer * ClosestPlayer = NULL ;
cCSLock Lock ( m_CSPlayers );
for ( cPlayerList :: const_iterator itr = m_Players . begin (); itr != m_Players . end (); ++ itr )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
Vector3f Pos = ( * itr ) -> GetPosition ();
float Distance = ( Pos - a_Pos ). Length ();
if ( Distance <= a_SightLimit )
{
if ( ! LineOfSight . Trace ( a_Pos ,( Pos - a_Pos ),( int )( Pos - a_Pos ). Length ()))
{
if ( Distance < ClosestDistance )
{
ClosestDistance = Distance ;
ClosestPlayer = * itr ;
}
}
}
2011-10-03 18:41:19 +00:00
}
2012-02-13 21:47:03 +00:00
return ClosestPlayer ;
2011-10-03 18:41:19 +00:00
}
2012-02-08 18:57:04 +00:00
2012-02-13 21:47:03 +00:00
void cWorld :: SendPlayerList ( cPlayer * a_DestPlayer )
{
// Sends the playerlist to a_DestPlayer
cCSLock Lock ( m_CSPlayers );
for ( cPlayerList :: iterator itr = m_Players . begin (); itr != m_Players . end (); ++ itr )
{
2012-02-29 08:40:15 +00:00
cClientHandle * ch = ( * itr ) -> GetClientHandle ();
if (( ch != NULL ) && ! ch -> IsDestroyed ())
2012-02-13 21:47:03 +00:00
{
cPacket_PlayerListItem PlayerListItem (( * itr ) -> GetColor () + ( * itr ) -> GetName (), true , ( * itr ) -> GetClientHandle () -> GetPing ());
a_DestPlayer -> GetClientHandle () -> Send ( PlayerListItem );
}
}
}
2011-10-03 18:41:19 +00:00
2012-02-08 18:57:04 +00:00
2012-02-16 17:20:28 +00:00
bool cWorld :: DoWithEntity ( int a_UniqueID , cEntityCallback & a_Callback )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSEntities );
for ( cEntityList :: iterator itr = m_AllEntities . begin (); itr != m_AllEntities . end (); ++ itr )
2011-10-03 18:41:19 +00:00
{
2012-02-13 21:47:03 +00:00
if ( ( * itr ) -> GetUniqueID () == a_UniqueID )
{
2012-02-16 17:20:28 +00:00
return a_Callback . Item ( * itr );
2012-02-13 21:47:03 +00:00
}
2012-02-16 17:20:28 +00:00
} // for itr - m_AllEntities[]
return false ;
2011-10-03 18:41:19 +00:00
}
2012-02-08 18:57:04 +00:00
2012-02-20 16:39:00 +00:00
void cWorld :: RemoveEntityFromChunk ( cEntity * a_Entity , int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
m_ChunkMap -> RemoveEntityFromChunk ( a_Entity , a_ChunkX , a_ChunkY , a_ChunkZ );
}
void cWorld :: MoveEntityToChunk ( cEntity * a_Entity , int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
m_ChunkMap -> MoveEntityToChunk ( a_Entity , a_ChunkX , a_ChunkY , a_ChunkZ );
}
void cWorld :: CompareChunkClients ( int a_ChunkX1 , int a_ChunkY1 , int a_ChunkZ1 , int a_ChunkX2 , int a_ChunkY2 , int a_ChunkZ2 , cClientDiffCallback & a_Callback )
2011-10-03 18:41:19 +00:00
{
2012-02-20 16:39:00 +00:00
m_ChunkMap -> CompareChunkClients ( a_ChunkX1 , a_ChunkY1 , a_ChunkZ1 , a_ChunkX2 , a_ChunkY2 , a_ChunkZ2 , a_Callback );
2011-10-03 18:41:19 +00:00
}
2012-02-08 18:57:04 +00:00
2012-02-21 15:18:02 +00:00
bool cWorld :: AddChunkClient ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , cClientHandle * a_Client )
{
return m_ChunkMap -> AddChunkClient ( a_ChunkX , a_ChunkY , a_ChunkZ , a_Client );
}
2012-02-21 16:27:30 +00:00
void cWorld :: RemoveChunkClient ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , cClientHandle * a_Client )
{
m_ChunkMap -> RemoveChunkClient ( a_ChunkX , a_ChunkY , a_ChunkZ , a_Client );
}
2012-03-22 15:53:40 +00:00
void cWorld :: RemoveClientFromChunks ( cClientHandle * a_Client )
2012-02-21 15:18:02 +00:00
{
2012-03-22 15:53:40 +00:00
m_ChunkMap -> RemoveClientFromChunks ( a_Client );
2012-02-21 15:18:02 +00:00
}
2012-03-06 14:52:44 +00:00
void cWorld :: SendChunkTo ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , cClientHandle * a_Client )
{
m_ChunkSender . QueueSendChunkTo ( a_ChunkX , a_ChunkY , a_ChunkZ , a_Client );
}
void cWorld :: RemoveClientFromChunkSender ( cClientHandle * a_Client )
{
m_ChunkSender . RemoveClient ( a_Client );
}
2012-02-21 16:27:30 +00:00
void cWorld :: TouchChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
m_ChunkMap -> TouchChunk ( a_ChunkX , a_ChunkY , a_ChunkZ );
}
2012-02-28 10:45:53 +00:00
bool cWorld :: LoadChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
return m_ChunkMap -> LoadChunk ( a_ChunkX , a_ChunkY , a_ChunkZ );
}
void cWorld :: LoadChunks ( const cChunkCoordsList & a_Chunks )
{
m_ChunkMap -> LoadChunks ( a_Chunks );
}
2012-02-28 12:11:14 +00:00
void cWorld :: ChunkLoadFailed ( int a_ChunkX , int a_ChunkY , int a_ChunkZ )
{
m_ChunkMap -> ChunkLoadFailed ( a_ChunkX , a_ChunkY , a_ChunkZ );
}
2012-02-21 16:27:30 +00:00
void cWorld :: UpdateSign ( int a_X , int a_Y , int a_Z , const AString & a_Line1 , const AString & a_Line2 , const AString & a_Line3 , const AString & a_Line4 )
{
m_ChunkMap -> UpdateSign ( a_X , a_Y , a_Z , a_Line1 , a_Line2 , a_Line3 , a_Line4 );
}
2012-02-28 10:45:53 +00:00
void cWorld :: ChunksStay ( const cChunkCoordsList & a_Chunks , bool a_Stay )
2012-02-26 16:15:09 +00:00
{
2012-02-28 10:45:53 +00:00
m_ChunkMap -> ChunksStay ( a_Chunks , a_Stay );
2012-02-26 16:15:09 +00:00
}
2012-04-10 11:22:11 +00:00
void cWorld :: RegenerateChunk ( int a_ChunkX , int a_ChunkZ )
{
m_ChunkMap -> MarkChunkRegenerating ( a_ChunkX , a_ChunkZ );
// Trick: use Y=1 to force the chunk generation even though the chunk data is already present
2012-05-25 07:18:52 +00:00
m_Generator . QueueGenerateChunk ( a_ChunkX , 1 , a_ChunkZ );
}
void cWorld :: GenerateChunk ( int a_ChunkX , int a_ChunkZ )
{
m_Generator . QueueGenerateChunk ( a_ChunkX , ZERO_CHUNK_Y , a_ChunkZ );
}
void cWorld :: QueueLightChunk ( int a_ChunkX , int a_ChunkZ , cChunkCoordCallback * a_Callback )
{
m_Lighting . QueueChunk ( a_ChunkX , a_ChunkZ , a_Callback );
}
bool cWorld :: IsChunkLighted ( int a_ChunkX , int a_ChunkZ )
{
return m_ChunkMap -> IsChunkLighted ( a_ChunkX , a_ChunkZ );
2012-04-10 11:22:11 +00:00
}
2012-05-25 07:18:52 +00:00
void cWorld :: SaveAllChunks ( void )
2011-10-03 18:41:19 +00:00
{
LOG ( "Saving all chunks..." );
m_LastSave = m_Time ;
m_ChunkMap -> SaveAllChunks ();
}
2012-02-08 18:57:04 +00:00
2011-10-03 18:41:19 +00:00
/************************************************************************/
/* Get and set */
/************************************************************************/
2011-11-01 21:57:08 +00:00
// void cWorld::AddClient( cClientHandle* a_Client )
// {
2012-02-13 21:47:03 +00:00
// m_m_Clients.push_back( a_Client );
2011-11-01 21:57:08 +00:00
// }
// cWorld::ClientList & cWorld::GetClients()
// {
2012-02-13 21:47:03 +00:00
// return m_m_Clients;
2011-11-01 21:57:08 +00:00
// }
2012-02-08 18:57:04 +00:00
2011-10-03 18:41:19 +00:00
void cWorld :: AddEntity ( cEntity * a_Entity )
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSEntities );
m_AllEntities . push_back ( a_Entity );
2011-10-03 18:41:19 +00:00
}
2012-02-08 18:57:04 +00:00
2011-10-03 18:41:19 +00:00
unsigned int cWorld :: GetNumPlayers ()
{
2012-02-13 21:47:03 +00:00
cCSLock Lock ( m_CSPlayers );
return m_Players . size ();
2011-11-01 23:05:47 +00:00
}
2012-02-08 18:57:04 +00:00
2012-02-13 21:47:03 +00:00
int cWorld :: GetNumChunks ( void ) const
2012-01-01 16:20:52 +00:00
{
2012-02-13 21:47:03 +00:00
return m_ChunkMap -> GetNumChunks ();
2012-02-08 18:57:04 +00:00
}
2012-05-25 07:18:52 +00:00
void cWorld :: GetChunkStats ( int & a_NumValid , int & a_NumDirty , int & a_NumInLightingQueue )
{
m_ChunkMap -> GetChunkStats ( a_NumValid , a_NumDirty );
a_NumInLightingQueue = ( int ) m_Lighting . GetQueueLength ();
}