diff options
Diffstat (limited to '')
-rw-r--r-- | src/Chunk.h | 149 |
1 files changed, 89 insertions, 60 deletions
diff --git a/src/Chunk.h b/src/Chunk.h index b2cf90cb9..d944af10a 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -63,7 +63,9 @@ typedef cItemCallback<cCommandBlockEntity> cCommandBlockCallback; typedef cItemCallback<cMobHeadEntity> cMobHeadCallback; typedef cItemCallback<cFlowerPotEntity> cFlowerPotCallback; - +// A convenience macro for calling GetChunkAndRelByAbsolute. +#define PREPARE_REL_AND_CHUNK(Position, OriginalChunk) cChunk * Chunk; Vector3i Rel; bool RelSuccess = (OriginalChunk).GetChunkAndRelByAbsolute(Position, &Chunk, Rel); +#define PREPARE_BLOCKDATA BLOCKTYPE BlockType; NIBBLETYPE BlockMeta; // This class is not to be used directly @@ -109,9 +111,9 @@ public: bool IsDirty(void) const {return m_IsDirty; } bool CanUnload(void); - + bool IsLightValid(void) const {return m_IsLightValid; } - + /* To save a chunk, the WSSchema must: 1. Mark the chunk as being saved (MarkSaving()) @@ -126,34 +128,34 @@ public: /** Marks the chunk as failed to load. If m_ShouldGenerateIfLoadFailed is set, queues the chunk for generating. */ void MarkLoadFailed(void); - + /** Gets all chunk data, calls the a_Callback's methods for each data type */ void GetAllData(cChunkDataCallback & a_Callback); - + /** Sets all chunk data as either loaded from the storage or generated. BlockLight and BlockSkyLight are optional, if not present, chunk will be marked as unlighted. Modifies the BlockEntity list in a_SetChunkData - moves the block entities into the chunk. */ void SetAllData(cSetChunkData & a_SetChunkData); - + void SetLight( const cChunkDef::BlockNibbles & a_BlockLight, const cChunkDef::BlockNibbles & a_SkyLight ); - + /** Copies m_BlockData into a_BlockTypes, only the block types */ void GetBlockTypes(BLOCKTYPE * a_BlockTypes); - + /** Writes the specified cBlockArea at the coords specified. Note that the coords may extend beyond the chunk! */ void WriteBlockArea(cBlockArea & a_Area, int a_MinBlockX, int a_MinBlockY, int a_MinBlockZ, int a_DataTypes); /** Returns true if there is a block entity at the coords specified */ bool HasBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ); - + /** Sets or resets the internal flag that prevents chunk from being unloaded. The flag is cumulative - it can be set multiple times and then needs to be un-set that many times before the chunk is unloadable again. */ void Stay(bool a_Stay = true); - + /** Recence all mobs proximities to players in order to know what to do with them */ void CollectMobCensus(cMobCensus & toFill); @@ -161,22 +163,22 @@ public: void SpawnMobs(cMobSpawner & a_MobSpawner); void Tick(std::chrono::milliseconds a_Dt); - + /** Ticks a single block. Used by cWorld::TickQueuedBlocks() to tick the queued blocks */ void TickBlock(int a_RelX, int a_RelY, int a_RelZ); int GetPosX(void) const { return m_PosX; } int GetPosZ(void) const { return m_PosZ; } - + cWorld * GetWorld(void) const { return m_World; } void SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, bool a_SendToClients = true); // SetBlock() does a lot of work (heightmap, tickblocks, blockentities) so a BlockIdx version doesn't make sense void SetBlock( const Vector3i & a_RelBlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { SetBlock( a_RelBlockPos.x, a_RelBlockPos.y, a_RelBlockPos.z, a_BlockType, a_BlockMeta); } - + /** Queues block for ticking (m_ToTickQueue) */ void QueueTickBlock(int a_RelX, int a_RelY, int a_RelZ); - + /** Queues all 6 neighbors of the specified block for ticking (m_ToTickQueue). If any are outside the chunk, relays the checking to the proper neighboring chunk */ void QueueTickBlockNeighbors(int a_RelX, int a_RelY, int a_RelZ); @@ -185,18 +187,38 @@ public: BLOCKTYPE GetBlock(const Vector3i & a_RelCoords) const { return GetBlock(a_RelCoords.x, a_RelCoords.y, a_RelCoords.z); } void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const; void GetBlockInfo (int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight); - + + /** Convert absolute coordinates into relative coordinates. + Returns false on failure to obtain a valid chunk. Returns true otherwise. + @param a_Position The position you'd like to convert, a_Position need not be in the calling chunk and can safely be out + of its bounds, but for best performance, it should not be too far from the calling chunk. + @param a_Chunk Returns the chunk in which a_Position is in. If a_Position is within the calling chunk's bounds, + returns the calling chunk. For best performance, a_Position shouldn't be too far from the calling chunk. + @param a_Rel Returns the converted relative position. Note that it is relative to the returned a_Chunk. + The vector will not be modified if the function returns false. */ + bool GetChunkAndRelByAbsolute(const Vector3d & a_Position, cChunk ** a_Chunk, Vector3i & a_Rel); + + /** Convert absolute coordinates into relative coordinates. + Returns false on failure to obtain a valid chunk. Returns true otherwise. + @param a_Position The position you'd like to convert, a_Position need not be in the calling chunk and can safely be out + of its bounds, but for best performance, it should not be too far from the calling chunk. + @param a_Chunk Returns the chunk in which a_Position is in. If a_Position is within the calling chunk's bounds, + returns the calling chunk. For best performance, a_Position shouldn't be too far from the calling chunk. + @param a_Rel Returns the converted relative position. Note that it is relative to the returned a_Chunk. + The vector will not be modified if the function returns false. */ + bool GetChunkAndRelByAbsolute(const Vector3i & a_Position, cChunk ** a_Chunk, Vector3i & a_Rel); + /** Returns the chunk into which the specified block belongs, by walking the neighbors. Will return self if appropriate. Returns nullptr if not reachable through neighbors. */ cChunk * GetNeighborChunk(int a_BlockX, int a_BlockZ); - + /** Returns the chunk into which the relatively-specified block belongs, by walking the neighbors. Will return self if appropriate. Returns nullptr if not reachable through neighbors. */ cChunk * GetRelNeighborChunk(int a_RelX, int a_RelZ); - + /** Returns the chunk into which the relatively-specified block belongs. Also modifies the relative coords from this-relative to return-relative. @@ -204,19 +226,19 @@ public: Will try walking the neighbors first; if that fails, will query the chunkmap */ cChunk * GetRelNeighborChunkAdjustCoords(int & a_RelX, int & a_RelZ) const; - + EMCSBiome GetBiomeAt(int a_RelX, int a_RelZ) const {return cChunkDef::GetBiome(m_BiomeMap, a_RelX, a_RelZ); } - + /** Sets the biome at the specified relative coords. Doesn't resend the chunk to clients. */ void SetBiomeAt(int a_RelX, int a_RelZ, EMCSBiome a_Biome); - + /** Sets the biome in the specified relative coords area. All the coords are inclusive. Sends the chunk to all relevant clients. */ void SetAreaBiome(int a_MinRelX, int a_MaxRelX, int a_MinRelZ, int a_MaxRelZ, EMCSBiome a_Biome); - + void CollectPickupsByPlayer(cPlayer & a_Player); - + /** Sets the sign text. Returns true if successful. Also sends update packets to all clients in the chunk */ bool SetSignLines(int a_RelX, int a_RelY, int a_RelZ, const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4); @@ -226,20 +248,20 @@ public: /** Adds a client to the chunk; returns true if added, false if already there */ bool AddClient(cClientHandle * a_Client); - + /** Removes the specified client from the chunk; ignored if client not in chunk. */ void RemoveClient(cClientHandle * a_Client); - + /** Returns true if the specified client is present in this chunk. */ bool HasClient(cClientHandle * a_Client); - + /** Returns true if theres any client in the chunk; false otherwise */ bool HasAnyClients(void) const; void AddEntity(cEntity * a_Entity); void RemoveEntity(cEntity * a_Entity); bool HasEntity(UInt32 a_EntityID); - + /** Calls the callback for each entity; returns true if all entities processed, false if the callback aborted by returning true */ bool ForEachEntity(cEntityCallback & a_Callback); // Lua-accessible @@ -270,10 +292,10 @@ public: /** Calls the callback for each furnace; returns true if all furnaces processed, false if the callback aborted by returning true */ bool ForEachFurnace(cFurnaceCallback & a_Callback); // Lua-accessible - + /** Calls the callback for the block entity at the specified coords; returns false if there's no block entity at those coords, true if found */ bool DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBlockEntityCallback & a_Callback); // Lua-acessible - + /** Calls the callback for the beacon at the specified coords; returns false if there's no beacon at those coords, true if found */ bool DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback); // Lua-acessible @@ -300,7 +322,7 @@ public: /** Calls the callback for the command block at the specified coords; returns false if there's no command block at those coords or callback returns true, returns true if found */ bool DoWithCommandBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cCommandBlockCallback & a_Callback); - + /** Calls the callback for the mob head block at the specified coords; returns false if there's no mob head block at those coords or callback returns true, returns true if found */ bool DoWithMobHeadAt(int a_BlockX, int a_BlockY, int a_BlockZ, cMobHeadCallback & a_Callback); @@ -341,14 +363,14 @@ public: void BroadcastSpawnEntity (cEntity & a_Entity, const cClientHandle * a_Exclude = nullptr); void BroadcastThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ, const cClientHandle * a_Exclude = nullptr); void BroadcastUseBed (const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ); - + void SendBlockEntity (int a_BlockX, int a_BlockY, int a_BlockZ, cClientHandle & a_Client); Vector3i PositionToWorldPosition(const Vector3i & a_RelPos) { return PositionToWorldPosition(a_RelPos.x, a_RelPos.y, a_RelPos.z); } - + void PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ, int & a_BlockX, int & a_BlockY, int & a_BlockZ); Vector3i PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ); @@ -357,7 +379,7 @@ public: m_IsDirty = true; m_IsSaving = false; } - + /** Sets the blockticking to start at the specified block. Only one blocktick may be set, second call overwrites the first call */ inline void SetNextBlockTick(int a_RelX, int a_RelY, int a_RelZ) { @@ -365,7 +387,7 @@ public: m_BlockTickY = a_RelY; m_BlockTickZ = a_RelZ; } - + inline NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const { return m_ChunkData.GetMeta(a_RelX, a_RelY, a_RelZ); @@ -377,23 +399,32 @@ public: { MarkDirty(); m_IsRedstoneDirty = true; - + m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(a_RelX, a_RelY, a_RelZ), a_Meta)); } } + /** Light alterations based on time */ + NIBBLETYPE GetTimeAlteredLight(NIBBLETYPE a_Skylight) const; + + /** Get the level of artificial light illuminating the block (0 - 15) */ inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkData.GetBlockLight(a_RelX, a_RelY, a_RelZ); } + + /** Get the level of sky light illuminating the block (0 - 15) independent of daytime. */ inline NIBBLETYPE GetSkyLight (int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkData.GetSkyLight(a_RelX, a_RelY, a_RelZ); } - + + /** Get the level of sky light illuminating the block (0 - 15), taking daytime into a account. */ + inline NIBBLETYPE GetSkyLightAltered (int a_RelX, int a_RelY, int a_RelZ) const {return GetTimeAlteredLight(m_ChunkData.GetSkyLight(a_RelX, a_RelY, a_RelZ)); } + /** Same as GetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in such a case); returns true on success */ bool UnboundedRelGetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const; - + /** Same as GetBlockType(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in such a case); returns true on success */ bool UnboundedRelGetBlockType(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType) const; - + /** Same as GetBlockMeta(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in such a case); returns true on success */ bool UnboundedRelGetBlockMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE & a_BlockMeta) const; - + /** Same as GetBlockBlockLight(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in such a case); returns true on success */ bool UnboundedRelGetBlockBlockLight(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE & a_BlockLight) const; @@ -408,12 +439,10 @@ public: /** Same as FastSetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in such a case); returns true on success */ bool UnboundedRelFastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta); - + /** Same as QueueTickBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s in such a case), ignores unsuccessful attempts */ void UnboundedQueueTickBlock(int a_RelX, int a_RelY, int a_RelZ); - /** Light alterations based on time */ - NIBBLETYPE GetTimeAlteredLight(NIBBLETYPE a_Skylight) const; // Per-chunk simulator data: @@ -429,11 +458,11 @@ public: cBlockEntity * GetBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ); cBlockEntity * GetBlockEntity(const Vector3i & a_BlockPos) { return GetBlockEntity(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z); } - + /** Returns true if the chunk should be ticked in the tick-thread. Checks if there are any clients and if the always-tick flag is set */ bool ShouldBeTicked(void) const; - + /** Increments (a_AlwaysTicked == true) or decrements (false) the m_AlwaysTicked counter. If the m_AlwaysTicked counter is greater than zero, the chunk is ticked in the tick-thread regardless of whether it has any clients or not. @@ -450,7 +479,7 @@ public: private: friend class cChunkMap; - + struct sSetBlockQueueItem { Int64 m_Tick; @@ -458,7 +487,7 @@ private: BLOCKTYPE m_BlockType; NIBBLETYPE m_BlockMeta; BLOCKTYPE m_PreviousType; - + sSetBlockQueueItem(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Int64 a_Tick, BLOCKTYPE a_PreviousBlockType) : m_Tick(a_Tick), m_RelX(a_RelX), m_RelY(a_RelY), m_RelZ(a_RelZ), m_BlockType(a_BlockType), m_BlockMeta(a_BlockMeta), m_PreviousType(a_PreviousBlockType) { @@ -466,7 +495,7 @@ private: } ; typedef std::vector<sSetBlockQueueItem> sSetBlockQueueVector; - + /** Holds the presence status of the chunk - if it is present, or in the loader / generator queue, or unloaded */ ePresence m_Presence; @@ -478,15 +507,15 @@ private: bool m_IsDirty; // True if the chunk has changed since it was last saved bool m_IsSaving; // True if the chunk is being saved bool m_HasLoadFailed; // True if chunk failed to load and hasn't been generated yet since then - + std::vector<Vector3i> m_ToTickBlocks; sSetBlockVector m_PendingSendBlocks; ///< Blocks that have changed and need to be sent to all clients - + // A critical section is not needed, because all chunk access is protected by its parent ChunkMap's csLayers std::vector<cClientHandle *> m_LoadedByClient; cEntityList m_Entities; cBlockEntityList m_BlockEntities; - + /** Number of times the chunk has been requested to stay (by various cChunkStay objects); if zero, the chunk can be unloaded */ int m_StayCount; @@ -500,12 +529,12 @@ private: cChunkDef::BiomeMap m_BiomeMap; int m_BlockTickX, m_BlockTickY, m_BlockTickZ; - + cChunk * m_NeighborXM; // Neighbor at [X - 1, Z] cChunk * m_NeighborXP; // Neighbor at [X + 1, Z] cChunk * m_NeighborZM; // Neighbor at [X, Z - 1] cChunk * m_NeighborZP; // Neighbor at [X, Z + 1] - + // Per-chunk simulator data: cFireSimulatorChunkData m_FireSimulatorData; cFluidSimulatorData * m_WaterSimulatorData; @@ -517,12 +546,12 @@ private: /** Indicates if simulate-once blocks should be updated by the redstone simulator */ bool m_IsRedstoneDirty; - + /** If greater than zero, the chunk is ticked even if it has no clients. Manipulated by the SetAlwaysTicked() function, allows for nested calls of the function. This is the support for plugin-accessible chunk tick forcing. */ int m_AlwaysTicked; - + // Pick up a random block of this chunk void GetRandomBlockCoords(int & a_X, int & a_Y, int & a_Z); @@ -533,31 +562,31 @@ private: /** Creates a block entity for each block that needs a block entity and doesn't have one in the list */ void CreateBlockEntities(void); - + /** Wakes up each simulator for its specific blocks; through all the blocks in the chunk */ void WakeUpSimulators(void); /** Sends m_PendingSendBlocks to all clients */ void BroadcastPendingBlockChanges(void); - + /** Checks the block scheduled for checking in m_ToTickBlocks[] */ void CheckBlocks(); - + /** Ticks several random blocks in the chunk */ void TickBlocks(void); - + /** Adds snow to the top of snowy biomes and hydrates farmland / fills cauldrons in rainy biomes */ void ApplyWeatherToTop(void); - + /** Grows sugarcane by the specified number of blocks, but no more than 3 blocks high (used by both bonemeal and ticking) */ void GrowSugarcane (int a_RelX, int a_RelY, int a_RelZ, int a_NumBlocks); - + /** Grows cactus by the specified number of blocks, but no more than 3 blocks high (used by both bonemeal and ticking) */ void GrowCactus (int a_RelX, int a_RelY, int a_RelZ, int a_NumBlocks); /** Grows a melon or a pumpkin next to the block specified (assumed to be the stem) */ void GrowMelonPumpkin(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, MTRand & a_Random); - + /** Called by Tick() when an entity moves out of this chunk into a neighbor; moves the entity and sends spawn / despawn packet to clients */ void MoveEntityToNewChunk(cEntity * a_Entity); }; |