2012-02-08 07:36:54 -05:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
// cChunkMap.h
|
|
|
|
|
|
|
|
// Interfaces to the cChunkMap class representing the chunk storage for a single world
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#pragma once
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
#include "cChunk.h"
|
|
|
|
|
2012-02-08 07:36:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-30 20:52:20 -04:00
|
|
|
class cWorld;
|
2011-10-03 14:41:19 -04:00
|
|
|
class cEntity;
|
2012-02-21 11:27:30 -05:00
|
|
|
class cItem;
|
2012-02-08 07:36:54 -05:00
|
|
|
class MTRand;
|
2012-02-26 11:15:09 -05:00
|
|
|
class cChunkStay;
|
2012-02-08 07:36:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
class cChunkMap
|
|
|
|
{
|
|
|
|
public:
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
static const int LAYER_SIZE = 32;
|
|
|
|
|
2012-01-29 16:40:21 -05:00
|
|
|
cChunkMap(cWorld* a_World );
|
2011-10-03 14:41:19 -04:00
|
|
|
~cChunkMap();
|
|
|
|
|
2012-02-15 09:22:44 -05:00
|
|
|
// Direct action methods:
|
2012-02-20 11:39:00 -05:00
|
|
|
/// Broadcast a_Packet to all clients in the chunk specified
|
2012-02-28 03:10:51 -05:00
|
|
|
void BroadcastToChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ, const cPacket & a_Packet, cClientHandle * a_Exclude = NULL);
|
2012-02-20 11:39:00 -05:00
|
|
|
|
2012-02-15 09:22:44 -05:00
|
|
|
/// Broadcasts a_Packet to all clients in the chunk where block [x, y, z] is, except to client a_Exclude
|
2012-02-28 03:10:51 -05:00
|
|
|
void BroadcastToChunkOfBlock(int a_X, int a_Y, int a_Z, const cPacket * a_Packet, cClientHandle * a_Exclude = NULL);
|
2012-02-20 11:39:00 -05:00
|
|
|
|
|
|
|
/// a_Player rclked block entity at the coords specified, handle it
|
|
|
|
void UseBlockEntity(cPlayer * a_Player, int a_X, int a_Y, int a_Z);
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-18 12:53:22 -05:00
|
|
|
void MarkChunkDirty (int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
void MarkChunkSaving (int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
void MarkChunkSaved (int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
void ChunkDataLoaded (int a_ChunkX, int a_ChunkY, int a_ChunkZ, const char * a_BlockData, cEntityList & a_Entities, cBlockEntityList & a_BlockEntities);
|
|
|
|
void ChunkDataGenerated (int a_ChunkX, int a_ChunkY, int a_ChunkZ, const char * a_BlockData, cEntityList & a_Entities, cBlockEntityList & a_BlockEntities);
|
|
|
|
void GetChunkData (int a_ChunkX, int a_ChunkY, int a_ChunkZ, cChunkDataCallback * a_Callback);
|
2012-03-05 11:41:57 -05:00
|
|
|
|
|
|
|
/// Gets the chunk's blocks, only the block types
|
2012-02-18 14:18:16 -05:00
|
|
|
bool GetChunkBlocks (int a_ChunkX, int a_ChunkY, int a_ChunkZ, char * a_Blocks);
|
2012-03-05 11:41:57 -05:00
|
|
|
|
|
|
|
/// Gets the chunk's blockdata, the entire array
|
|
|
|
bool GetChunkBlockData (int a_ChunkX, int a_ChunkY, int a_ChunkZ, char * a_BlockData);
|
|
|
|
|
2012-02-18 12:53:22 -05:00
|
|
|
bool IsChunkValid (int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
bool HasChunkAnyClients (int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
void SpreadChunkLighting(int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
int GetHeight (int a_BlockX, int a_BlockZ);
|
2012-02-18 15:10:57 -05:00
|
|
|
void FastSetBlocks (sSetBlockList & a_BlockList);
|
2012-02-20 11:39:00 -05:00
|
|
|
void CollectPickupsByPlayer(cPlayer * a_Player);
|
2012-02-21 07:29:05 -05:00
|
|
|
char GetBlock (int a_X, int a_Y, int a_Z);
|
|
|
|
char GetBlockMeta (int a_X, int a_Y, int a_Z);
|
2012-02-21 11:27:30 -05:00
|
|
|
void SetBlockMeta (int a_X, int a_Y, int a_Z, char a_BlockMeta);
|
|
|
|
void SetBlock (int a_X, int a_Y, int a_Z, char a_BlockType, char a_BlockMeta);
|
|
|
|
bool DigBlock (int a_X, int a_Y, int a_Z, cItem & a_PickupItem);
|
|
|
|
void SendBlockTo (int a_X, int a_Y, int a_Z, cPlayer * a_Player);
|
2012-02-20 11:39:00 -05:00
|
|
|
|
|
|
|
/// Compares clients of two chunks, calls the callback accordingly
|
|
|
|
void CompareChunkClients(int a_ChunkX1, int a_ChunkY1, int a_ChunkZ1, int a_ChunkX2, int a_ChunkY2, int a_ChunkZ2, cClientDiffCallback & a_Callback);
|
2012-02-21 10:18:02 -05:00
|
|
|
|
|
|
|
/// Adds client to a chunk, if not already present; returns true if added, false if present
|
|
|
|
bool AddChunkClient(int a_ChunkX, int a_ChunkY, int a_ChunkZ, cClientHandle * a_Client);
|
|
|
|
|
2012-02-21 11:27:30 -05:00
|
|
|
/// Removes the client from the chunk
|
|
|
|
void RemoveChunkClient(int a_ChunkX, int a_ChunkY, int a_ChunkZ, cClientHandle * a_Client);
|
|
|
|
|
2012-02-21 10:18:02 -05:00
|
|
|
/// Removes the client from all chunks specified
|
|
|
|
void RemoveClientFromChunks(cClientHandle * a_Client, const cChunkCoordsList & a_Chunks);
|
|
|
|
|
2012-02-20 11:39:00 -05:00
|
|
|
/// Moves the entity from its current chunk to the new chunk specified
|
|
|
|
void MoveEntityToChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
|
|
|
|
/// Removes the entity from the chunk specified
|
|
|
|
void RemoveEntityFromChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
2012-02-21 11:27:30 -05:00
|
|
|
|
|
|
|
/// Touches the chunk, causing it to be loaded or generated
|
|
|
|
void TouchChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
|
2012-02-28 05:45:53 -05:00
|
|
|
/// Loads the chunk, if not already loaded. Doesn't generate. Returns true if chunk valid (even if already loaded before)
|
|
|
|
bool LoadChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
|
|
|
|
/// Loads the chunks specified. Doesn't report failure, other than chunks being !IsValid()
|
|
|
|
void LoadChunks(const cChunkCoordsList & a_Chunks);
|
|
|
|
|
2012-02-28 07:11:14 -05:00
|
|
|
/// Marks the chunk as failed-to-load:
|
|
|
|
void ChunkLoadFailed(int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
|
2012-02-21 11:27:30 -05:00
|
|
|
void 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);
|
2012-02-26 11:15:09 -05:00
|
|
|
|
2012-02-28 05:45:53 -05:00
|
|
|
/// Marks (a_Stay == true) or unmarks (a_Stay == false) chunks as non-unloadable; to be used only by cChunkStay!
|
|
|
|
void ChunksStay(const cChunkCoordsList & a_Chunks, bool a_Stay = true);
|
2012-02-16 08:42:35 -05:00
|
|
|
|
2012-02-08 07:36:54 -05:00
|
|
|
void Tick( float a_Dt, MTRand & a_TickRand );
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
void UnloadUnusedChunks();
|
|
|
|
void SaveAllChunks();
|
2011-12-24 18:34:30 -05:00
|
|
|
|
2012-02-15 09:22:44 -05:00
|
|
|
cWorld * GetWorld() { return m_World; }
|
2012-01-01 11:20:52 -05:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
int GetNumChunks(void);
|
2012-02-08 07:36:54 -05:00
|
|
|
|
2012-02-15 09:22:44 -05:00
|
|
|
/// Converts absolute block coords into relative (chunk + block) coords:
|
|
|
|
inline static void AbsoluteToRelative(/* in-out */ int & a_X, int & a_Y, int & a_Z, /* out */ int & a_ChunkX, int & a_ChunkZ )
|
|
|
|
{
|
|
|
|
BlockToChunk(a_X, a_Y, a_Z, a_ChunkX, a_ChunkZ);
|
|
|
|
|
2012-03-03 15:55:16 -05:00
|
|
|
a_X = a_X - a_ChunkX * cChunk::c_ChunkWidth;
|
|
|
|
a_Z = a_Z - a_ChunkZ * cChunk::c_ChunkWidth;
|
2012-02-15 09:22:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts absolute block coords to chunk coords:
|
|
|
|
inline static void BlockToChunk( int a_X, int a_Y, int a_Z, int & a_ChunkX, int & a_ChunkZ )
|
|
|
|
{
|
|
|
|
(void)a_Y;
|
2012-03-03 15:55:16 -05:00
|
|
|
a_ChunkX = a_X / cChunk::c_ChunkWidth;
|
|
|
|
if ((a_X < 0) && (a_X % cChunk::c_ChunkWidth != 0))
|
2012-02-15 09:22:44 -05:00
|
|
|
{
|
|
|
|
a_ChunkX--;
|
|
|
|
}
|
2012-03-03 15:55:16 -05:00
|
|
|
a_ChunkZ = a_Z / cChunk::c_ChunkWidth;
|
|
|
|
if ((a_Z < 0) && (a_Z % cChunk::c_ChunkWidth != 0))
|
2012-02-15 09:22:44 -05:00
|
|
|
{
|
|
|
|
a_ChunkZ--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-18 12:53:22 -05:00
|
|
|
void ChunkValidated(void); // Called by chunks that have become valid
|
2012-02-15 09:22:44 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
private:
|
2012-02-08 07:36:54 -05:00
|
|
|
|
2012-02-21 11:27:30 -05:00
|
|
|
friend class cChunk; // Temporary (until we have a separate Lighting thread), so that cChunk's lighting calc can ask for neighbor chunks
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
class cChunkLayer
|
|
|
|
{
|
|
|
|
public:
|
2012-02-13 16:47:03 -05:00
|
|
|
cChunkLayer(int a_LayerX, int a_LayerZ, cChunkMap * a_Parent);
|
2012-02-23 16:21:37 -05:00
|
|
|
~cChunkLayer();
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
/// Always returns an assigned chunkptr, but the chunk needn't be valid (loaded / generated) - callers must check
|
2012-02-17 12:56:25 -05:00
|
|
|
cChunkPtr GetChunk( int a_ChunkX, int a_ChunkY, int a_ChunkZ );
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
int GetX(void) const {return m_LayerX; }
|
|
|
|
int GetZ(void) const {return m_LayerZ; }
|
2012-02-17 12:56:25 -05:00
|
|
|
|
|
|
|
int GetNumChunksLoaded(void) const ;
|
2012-01-30 08:54:39 -05:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
void Save(void);
|
|
|
|
void UnloadUnusedChunks(void);
|
|
|
|
|
|
|
|
void Tick( float a_Dt, MTRand & a_TickRand );
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
cChunkPtr m_Chunks[LAYER_SIZE * LAYER_SIZE];
|
|
|
|
int m_LayerX;
|
|
|
|
int m_LayerZ;
|
|
|
|
cChunkMap * m_Parent;
|
2011-10-03 14:41:19 -04:00
|
|
|
int m_NumChunksLoaded;
|
|
|
|
};
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
typedef std::list<cChunkLayer *> cChunkLayerList;
|
|
|
|
// TODO: Use smart pointers for cChunkLayerList as well, so that ticking and saving needn't lock the entire layerlist
|
|
|
|
// This however means that cChunkLayer needs to interlock its m_Chunks[]
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
cChunkLayer * GetLayerForChunk( int a_ChunkX, int a_ChunkZ ); // Creates the layer if it doesn't already exist
|
|
|
|
cChunkLayer * GetLayer( int a_LayerX, int a_LayerZ ); // Creates the layer if it doesn't already exist
|
|
|
|
void RemoveLayer( cChunkLayer* a_Layer );
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
cCriticalSection m_CSLayers;
|
|
|
|
cChunkLayerList m_Layers;
|
2012-02-18 12:53:22 -05:00
|
|
|
cEvent m_evtChunkValid; // Set whenever any chunk becomes valid, via ChunkValidated()
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
cWorld * m_World;
|
2012-02-21 11:27:30 -05:00
|
|
|
|
2012-02-28 05:45:53 -05:00
|
|
|
cChunkPtr GetChunk (int a_ChunkX, int a_ChunkY, int a_ChunkZ); // Also queues the chunk for loading / generating if not valid
|
|
|
|
cChunkPtr GetChunkNoGen (int a_ChunkX, int a_ChunkY, int a_ChunkZ); // Also queues the chunk for loading if not valid; doesn't generate
|
|
|
|
cChunkPtr GetChunkNoLoad(int a_ChunkX, int a_ChunkY, int a_ChunkZ); // Doesn't load, doesn't generate
|
2011-10-29 17:19:06 -04:00
|
|
|
};
|
2012-02-08 07:36:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-26 11:15:09 -05:00
|
|
|
|
|
|
|
/** Makes chunks stay loaded until this object is cleared or destroyed
|
2012-02-28 05:45:53 -05:00
|
|
|
Works by setting internal flags in the cChunk that it should not be unloaded.
|
|
|
|
To optimize for speed, cChunkStay has an Enabled flag, it will "stay" the chunks only when enabled and it will refuse manipulations when enabled
|
|
|
|
The object itself is not made thread-safe, it's supposed to be used from a single thread only.
|
2012-02-26 11:15:09 -05:00
|
|
|
*/
|
|
|
|
class cChunkStay
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cChunkStay(cWorld * a_World);
|
|
|
|
~cChunkStay();
|
|
|
|
|
|
|
|
void Clear(void);
|
|
|
|
|
|
|
|
void Add(int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
void Remove(int a_ChunkX, int a_ChunkY, int a_ChunkZ);
|
|
|
|
|
2012-02-28 05:45:53 -05:00
|
|
|
void Enable(void);
|
|
|
|
void Disable(void);
|
|
|
|
|
|
|
|
// Allow cChunkStay be passed to functions expecting a const cChunkCoordsList &
|
|
|
|
operator const cChunkCoordsList(void) const {return m_Chunks; }
|
|
|
|
|
2012-02-26 11:15:09 -05:00
|
|
|
protected:
|
|
|
|
|
|
|
|
cWorld * m_World;
|
|
|
|
|
2012-02-28 05:45:53 -05:00
|
|
|
bool m_IsEnabled;
|
|
|
|
|
2012-02-26 11:15:09 -05:00
|
|
|
cChunkCoordsList m_Chunks;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|