Cuberite
A lightweight, fast and extensible game server for Minecraft
Chunk.h
Go to the documentation of this file.
1 
2 #pragma once
3 
5 #include "ChunkData.h"
6 
9 
10 #include "ChunkMap.h"
11 
12 
13 
14 
15 
16 class cWorld;
17 class cClientHandle;
18 class cPlayer;
19 class cChunkMap;
20 class cBoundingBox;
21 class cChunkDataCallback;
22 class cBlockArea;
23 class cBlockArea;
25 class cMobCensus;
26 class cMobSpawner;
28 
29 struct SetChunkData;
30 
31 // A convenience macro for calling GetChunkAndRelByAbsolute.
32 #define PREPARE_REL_AND_CHUNK(Position, OriginalChunk) cChunk * Chunk; Vector3i Rel; bool RelSuccess = (OriginalChunk).GetChunkAndRelByAbsolute(Position, &Chunk, Rel)
33 
34 
35 class cChunk
36 {
37 public:
38 
40  enum ePresence
41  {
45  };
46 
47  cChunk(
48  int a_ChunkX, int a_ChunkZ, // Chunk coords
49  cChunkMap * a_ChunkMap, cWorld * a_World // Parent objects
50  );
51  cChunk(const cChunk & Other) = delete;
52  ~cChunk();
53 
55  void BroadcastPendingChanges(void);
56 
58  bool IsValid(void) const {return (m_Presence == cpPresent); }
59 
61  bool IsQueued(void) const {return (m_Presence == cpQueued); }
62 
65  void SetPresence(ePresence a_Presence);
66 
68  void MarkRegenerating(void);
69 
71  bool IsDirty(void) const {return m_IsDirty; }
72 
73  bool CanUnload(void) const;
74 
76  bool CanUnloadAfterSaving(void) const;
77 
81  void OnUnload();
82 
83  bool IsLightValid(void) const {return m_IsLightValid; }
84 
85  /*
86  To save a chunk, the WSSchema must:
87  1. Mark the chunk as being saved (MarkSaving())
88  2. Get the chunk's data using GetAllData()
89  3. Mark the chunk as saved (MarkSaved())
90  If anywhere inside this sequence another thread mmodifies the chunk, the chunk will not get marked as saved in MarkSaved()
91  */
92  void MarkSaving(void); // Marks the chunk as being saved.
93  void MarkSaved(void); // Marks the chunk as saved, if it didn't change from the last call to MarkSaving()
94  void MarkLoaded(void); // Marks the chunk as freshly loaded. Fails if the chunk is already valid
95 
97  void MarkLoadFailed(void);
98 
100  void GetAllData(cChunkDataCallback & a_Callback) const;
101 
105  void SetAllData(SetChunkData && a_SetChunkData);
106 
107  void SetLight(
108  const cChunkDef::BlockNibbles & a_BlockLight,
109  const cChunkDef::BlockNibbles & a_SkyLight
110  );
111 
113  void WriteBlockArea(cBlockArea & a_Area, int a_MinBlockX, int a_MinBlockY, int a_MinBlockZ, int a_DataTypes);
114 
118  void Stay(bool a_Stay = true);
119 
121  void CollectMobCensus(cMobCensus & toFill);
122 
124  void SpawnMobs(cMobSpawner & a_MobSpawner);
125 
126  void Tick(std::chrono::milliseconds a_Dt);
127 
129  void TickBlock(const Vector3i a_RelPos);
130 
131  int GetPosX(void) const { return m_PosX; }
132  int GetPosZ(void) const { return m_PosZ; }
133  cChunkCoords GetPos() const { return {m_PosX, m_PosZ}; }
134 
135  cWorld * GetWorld(void) const { return m_World; }
136 
137  void SetBlock(Vector3i a_RelBlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
138  // SetBlock() does a lot of work (heightmap, tickblocks, blockentities) so a BlockIdx version doesn't make sense
139 
140  void FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta); // Doesn't force block updates on neighbors, use for simple changes such as grass growing etc.
141  void FastSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta)
142  {
143  FastSetBlock(a_RelPos.x, a_RelPos.y, a_RelPos.z, a_BlockType, a_BlockMeta);
144  }
145 
146  BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const { return m_BlockData.GetBlock({ a_RelX, a_RelY, a_RelZ }); }
147  BLOCKTYPE GetBlock(Vector3i a_RelCoords) const { return m_BlockData.GetBlock(a_RelCoords); }
148 
149  void GetBlockTypeMeta(Vector3i a_RelPos, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const;
150  void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const
151  {
152  GetBlockTypeMeta({ a_RelX, a_RelY, a_RelZ }, a_BlockType, a_BlockMeta);
153  }
154 
155  void GetBlockInfo(Vector3i a_RelPos, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight) const;
156 
165  bool GetChunkAndRelByAbsolute(const Vector3d & a_Position, cChunk ** a_Chunk, Vector3i & a_Rel);
166 
175  bool GetChunkAndRelByAbsolute(const Vector3i & a_Position, cChunk ** a_Chunk, Vector3i & a_Rel);
176 
179  cChunk * GetNeighborChunk(int a_BlockX, int a_BlockZ);
180 
183  cChunk * GetRelNeighborChunk(int a_RelX, int a_RelZ);
184 
189  cChunk * GetRelNeighborChunkAdjustCoords(Vector3i & a_RelPos) const;
190 
191  EMCSBiome GetBiomeAt(int a_RelX, int a_RelZ) const {return cChunkDef::GetBiome(m_BiomeMap, a_RelX, a_RelZ); }
192 
195  void SetBiomeAt(int a_RelX, int a_RelZ, EMCSBiome a_Biome);
196 
199  void SetAreaBiome(int a_MinRelX, int a_MaxRelX, int a_MinRelZ, int a_MaxRelZ, EMCSBiome a_Biome);
200 
201  int GetHeight( int a_X, int a_Z) const;
202 
204  bool IsWeatherSunnyAt(int a_RelX, int a_RelZ) const;
205 
207  bool IsWeatherWetAt(int a_RelX, int a_RelZ) const;
208 
211  bool IsWeatherWetAt(Vector3i a_Position) const;
212 
213  void SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_Client);
214 
216  bool AddClient(cClientHandle * a_Client);
217 
219  void RemoveClient(cClientHandle * a_Client);
220 
222  bool HasClient(cClientHandle * a_Client);
223 
225  bool HasAnyClients(void) const;
226 
227  void AddEntity(OwnedEntity a_Entity);
228 
231  OwnedEntity RemoveEntity(cEntity & a_Entity);
232 
233  bool HasEntity(UInt32 a_EntityID) const;
234 
236  bool ForEachEntity(cEntityCallback a_Callback) const; // Lua-accessible
237 
240  bool ForEachEntityInBox(const cBoundingBox & a_Box, cEntityCallback a_Callback) const; // Lua-accessible
241 
243  bool DoWithEntityByID(UInt32 a_EntityID, cEntityCallback a_Callback, bool & a_CallbackResult) const; // Lua-accessible
244 
246  bool ForEachBlockEntity(cBlockEntityCallback a_Callback); // Lua-accessible
247 
249  bool DoWithBlockEntityAt(Vector3i a_Position, cBlockEntityCallback a_Callback); // Lua-acessible
250 
253  bool UseBlockEntity(cPlayer * a_Player, int a_X, int a_Y, int a_Z); // [x, y, z] in world block coords
254 
255  void SendBlockEntity (int a_BlockX, int a_BlockY, int a_BlockZ, cClientHandle & a_Client);
256 
258  {
259  return PositionToWorldPosition(a_RelPos.x, a_RelPos.y, a_RelPos.z);
260  }
261 
262  void PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ, int & a_BlockX, int & a_BlockY, int & a_BlockZ);
263  Vector3i PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ);
264 
265  inline void MarkDirty(void)
266  {
267  m_IsDirty = true;
268  m_IsSaving = false;
269  }
270 
274  inline void SetNextBlockToTick(const Vector3i a_RelPos)
275  {
276  m_BlockToTick = a_RelPos;
277  }
278 
279  inline NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
280  {
281  return m_BlockData.GetMeta({ a_RelX, a_RelY, a_RelZ });
282  }
283 
284  NIBBLETYPE GetMeta(Vector3i a_RelPos) const { return m_BlockData.GetMeta(a_RelPos); }
285 
286  void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta)
287  {
288  SetMeta({ a_RelX, a_RelY, a_RelZ }, a_Meta);
289  }
290 
291  inline void SetMeta(Vector3i a_RelPos, NIBBLETYPE a_Meta)
292  {
293  m_BlockData.SetMeta(a_RelPos, a_Meta);
294  MarkDirty();
295  m_PendingSendBlocks.emplace_back(m_PosX, m_PosZ, a_RelPos.x, a_RelPos.y, a_RelPos.z, GetBlock(a_RelPos), a_Meta);
296  }
297 
299  NIBBLETYPE GetTimeAlteredLight(NIBBLETYPE a_Skylight) const;
300 
302  inline NIBBLETYPE GetBlockLight(Vector3i a_RelPos) const { return m_LightData.GetBlockLight(a_RelPos); }
303  inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const { return m_LightData.GetBlockLight({ a_RelX, a_RelY, a_RelZ }); }
304 
306  inline NIBBLETYPE GetSkyLight(Vector3i a_RelPos) const { return m_LightData.GetSkyLight(a_RelPos); }
307  inline NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const { return m_LightData.GetSkyLight({ a_RelX, a_RelY, a_RelZ }); }
308 
310  inline NIBBLETYPE GetSkyLightAltered(Vector3i a_RelPos) const { return GetTimeAlteredLight(m_LightData.GetSkyLight(a_RelPos)); }
311  inline NIBBLETYPE GetSkyLightAltered(int a_RelX, int a_RelY, int a_RelZ) const { return GetSkyLightAltered({ a_RelX, a_RelY, a_RelZ }); }
312 
315  bool UnboundedRelGetBlock(Vector3i a_RelCoords, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const;
316 
320  bool UnboundedRelGetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const
321  {
322  return UnboundedRelGetBlock({a_RelX, a_RelY, a_RelZ}, a_BlockType, a_BlockMeta);
323  }
324 
327  bool UnboundedRelGetBlockType(Vector3i a_RelCoords, BLOCKTYPE & a_BlockType) const;
328 
332  bool UnboundedRelGetBlockType(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType) const
333  {
334  return UnboundedRelGetBlockType({a_RelX, a_RelY, a_RelZ}, a_BlockType);
335  }
336 
339  bool UnboundedRelGetBlockMeta(Vector3i a_RelPos, NIBBLETYPE & a_BlockMeta) const;
340 
344  bool UnboundedRelGetBlockMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE & a_BlockMeta) const
345  {
346  return UnboundedRelGetBlockMeta({a_RelX, a_RelY, a_RelZ}, a_BlockMeta);
347  }
348 
351  bool UnboundedRelGetBlockBlockLight(Vector3i a_RelPos, NIBBLETYPE & a_BlockLight) const;
352 
356  bool UnboundedRelGetBlockBlockLight(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE & a_BlockLight) const
357  {
358  return UnboundedRelGetBlockBlockLight({a_RelX, a_RelY, a_RelZ}, a_BlockLight);
359  }
360 
363  bool UnboundedRelGetBlockSkyLight(Vector3i a_RelPos, NIBBLETYPE & a_SkyLight) const;
364 
368  bool UnboundedRelGetBlockSkyLight(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE & a_SkyLight) const
369  {
370  return UnboundedRelGetBlockSkyLight({a_RelX, a_RelY, a_RelZ}, a_SkyLight);
371  }
372 
375  bool UnboundedRelGetBlockLights(Vector3i a_RelPos, NIBBLETYPE & a_BlockLight, NIBBLETYPE & a_SkyLight) const;
376 
380  bool UnboundedRelGetBlockLights(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE & a_BlockLight, NIBBLETYPE & a_SkyLight) const
381  {
382  return UnboundedRelGetBlockLights({a_RelX, a_RelY, a_RelZ}, a_BlockLight, a_SkyLight);
383  }
384 
387  bool UnboundedRelSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
388 
392  bool UnboundedRelSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
393  {
394  return UnboundedRelSetBlock({a_RelX, a_RelY, a_RelZ}, a_BlockType, a_BlockMeta);
395  }
396 
399  bool UnboundedRelFastSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
400 
404  bool UnboundedRelFastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
405  {
406  return UnboundedRelFastSetBlock({a_RelX, a_RelY, a_RelZ}, a_BlockType, a_BlockMeta);
407  }
408 
409 
410 
411  // Per-chunk simulator data:
417 
421 
425  cBlockEntity * GetBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ) { return GetBlockEntity({a_BlockX, a_BlockY, a_BlockZ}); }
426 
431 
434  bool ShouldBeTicked(void) const;
435 
441  void SetAlwaysTicked(bool a_AlwaysTicked);
442 
443  const auto & GetAllClients(void) const
444  {
445  return m_LoadedByClient;
446  }
447 
450  Vector3i RelativeToAbsolute(Vector3i a_RelBlockPosition) const
451  {
452  return cChunkDef::RelativeToAbsolute(a_RelBlockPosition, {m_PosX, m_PosZ});
453  }
454 
456  bool IsSlimeChunk() const;
457 
458 private:
459 
460  friend class cChunkMap;
461 
463  {
469 
470  sSetBlockQueueItem(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Int64 a_Tick, BLOCKTYPE a_PreviousBlockType) :
471  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)
472  {
473  }
474  } ;
475 
476  typedef std::vector<sSetBlockQueueItem> sSetBlockQueueVector;
477 
478 
481 
482  bool m_IsLightValid; // True if the blocklight and skylight are calculated
483  bool m_IsDirty; // True if the chunk has changed since it was last saved
484  bool m_IsSaving; // True if the chunk is being saved
485 
490 
494  std::vector<cBlockEntity *> m_PendingSendBlockEntities;
495 
498  std::queue<Vector3i> m_BlocksToCheck;
499 
500  // A critical section is not needed, because all chunk access is protected by its parent ChunkMap's csLayers
501  std::vector<cClientHandle *> m_LoadedByClient;
502  std::vector<OwnedEntity> m_Entities;
504 
506  unsigned m_StayCount;
507 
511 
514 
517 
521 
522  cChunk * m_NeighborXM; // Neighbor at [X - 1, Z]
523  cChunk * m_NeighborXP; // Neighbor at [X + 1, Z]
524  cChunk * m_NeighborZM; // Neighbor at [X, Z - 1]
525  cChunk * m_NeighborZP; // Neighbor at [X, Z + 1]
526 
527  // Per-chunk simulator data:
533 
537  unsigned m_AlwaysTicked;
538 
539  // Pick up a random block of this chunk
540  void GetRandomBlockCoords(int & a_X, int & a_Y, int & a_Z);
541  void GetThreeRandomNumbers(int & a_X, int & a_Y, int & a_Z, int a_MaxX, int a_MaxY, int a_MaxZ);
542 
544  void AddBlockEntity(OwnedBlockEntity a_BlockEntity);
545 
547  void WakeUpSimulators(void);
548 
550  void CheckBlocks();
551 
553  void TickBlocks(void);
554 
556  void ApplyWeatherToTop(void);
557 
560  cItems PickupsFromBlock(Vector3i a_RelPos, const cEntity * a_Digger, const cItem * a_Tool);
561 
565  int GrowPlantAt(Vector3i a_RelPos, int a_NumStages = 1);
566 
568  void MoveEntityToNewChunk(OwnedEntity a_Entity);
569 
571  bool HasPlayerEntities() const;
572 };
EMCSBiome
Biome IDs The first batch corresponds to the clientside biomes, used by MineCraft.
Definition: BiomeDef.h:18
std::vector< sSetBlock > sSetBlockVector
Definition: ChunkDef.h:441
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
std::unique_ptr< cEntity > OwnedEntity
Definition: ChunkDef.h:32
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
unsigned int UInt32
Definition: Globals.h:157
signed long long Int64
Definition: Globals.h:151
cCoordWithIntList cFireSimulatorChunkData
Stores individual fire blocks in the chunk; the int data is used as the time [msec] the fire takes to...
Definition: FireSimulator.h:69
cCoordWithIntList cSandSimulatorChunkData
Per-chunk data for the simulator, specified individual chunks to simulate; Data is not used.
Definition: SandSimulator.h:12
Represents two sets of coords, minimum and maximum for each direction.
Definition: BoundingBox.h:24
Definition: Chunk.h:36
bool GetChunkAndRelByAbsolute(const Vector3d &a_Position, cChunk **a_Chunk, Vector3i &a_Rel)
Convert absolute coordinates into relative coordinates.
Definition: Chunk.cpp:1779
cFireSimulatorChunkData & GetFireSimulatorData(void)
Definition: Chunk.h:412
void AddBlockEntity(OwnedBlockEntity a_BlockEntity)
Takes ownership of a block entity, which MUST actually reside in this chunk.
Definition: Chunk.cpp:1402
NIBBLETYPE GetBlockLight(Vector3i a_RelPos) const
Get the level of artificial light illuminating the block (0 - 15)
Definition: Chunk.h:302
void GetAllData(cChunkDataCallback &a_Callback) const
Gets all chunk data, calls the a_Callback's methods for each data type.
Definition: Chunk.cpp:307
ePresence m_Presence
Holds the presence status of the chunk - if it is present, or in the loader / generator queue,...
Definition: Chunk.h:480
void TickBlocks(void)
Ticks several random blocks in the chunk.
Definition: Chunk.cpp:834
std::vector< OwnedEntity > m_Entities
Definition: Chunk.h:502
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:279
bool ShouldBeTicked(void) const
Returns true if the chunk should be ticked in the tick-thread.
Definition: Chunk.cpp:1447
cChunk * m_NeighborXM
Definition: Chunk.h:522
std::vector< cBlockEntity * > m_PendingSendBlockEntities
Block entities that have been touched and need to be sent to all clients.
Definition: Chunk.h:494
void CheckBlocks()
Checks the block scheduled for checking in m_ToTickBlocks[].
Definition: Chunk.cpp:812
EMCSBiome GetBiomeAt(int a_RelX, int a_RelZ) const
Definition: Chunk.h:191
void SpawnMobs(cMobSpawner &a_MobSpawner)
Try to Spawn Monsters inside chunk.
Definition: Chunk.cpp:611
void SendBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cClientHandle &a_Client)
Definition: Chunk.cpp:1943
unsigned m_AlwaysTicked
If greater than zero, the chunk is ticked even if it has no clients.
Definition: Chunk.h:537
bool IsSlimeChunk() const
Returns true if slimes should spawn in the chunk.
Definition: Chunk.cpp:1988
bool UnboundedRelGetBlockMeta(Vector3i a_RelPos, NIBBLETYPE &a_BlockMeta) const
Same as GetBlockMeta(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1050
int GrowPlantAt(Vector3i a_RelPos, int a_NumStages=1)
Grows the plant at the specified position by at most a_NumStages.
Definition: Chunk.cpp:999
bool UseBlockEntity(cPlayer *a_Player, int a_X, int a_Y, int a_Z)
Use block entity on coordinate.
Definition: Chunk.cpp:1474
void SetAllData(SetChunkData &&a_SetChunkData)
Sets all chunk data as either loaded from the storage or generated.
Definition: Chunk.cpp:331
bool DoWithBlockEntityAt(Vector3i a_Position, cBlockEntityCallback a_Callback)
Calls the callback for the block entity at the specified coords; returns false if there's no block en...
Definition: Chunk.cpp:1737
void SetLight(const cChunkDef::BlockNibbles &a_BlockLight, const cChunkDef::BlockNibbles &a_SkyLight)
Definition: Chunk.cpp:402
cFluidSimulatorData * GetLavaSimulatorData(void) const
Definition: Chunk.h:414
BLOCKTYPE GetBlock(Vector3i a_RelCoords) const
Definition: Chunk.h:147
int GetPosX(void) const
Definition: Chunk.h:131
bool ForEachEntity(cEntityCallback a_Callback) const
Calls the callback for each entity; returns true if all entities processed, false if the callback abo...
Definition: Chunk.cpp:1657
ChunkLightData m_LightData
Definition: Chunk.h:513
bool UnboundedRelGetBlockMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE &a_BlockMeta) const
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:344
void FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta)
Definition: Chunk.cpp:1296
void MarkRegenerating(void)
Marks all clients attached to this chunk as wanting this chunk.
Definition: Chunk.cpp:176
void WriteBlockArea(cBlockArea &a_Area, int a_MinBlockX, int a_MinBlockY, int a_MinBlockZ, int a_DataTypes)
Writes the specified cBlockArea at the coords specified.
Definition: Chunk.cpp:420
NIBBLETYPE GetMeta(Vector3i a_RelPos) const
Definition: Chunk.h:284
void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Definition: Chunk.h:150
BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:146
void AddEntity(OwnedEntity a_Entity)
Definition: Chunk.cpp:1582
Vector3i RelativeToAbsolute(Vector3i a_RelBlockPosition) const
Converts the coord relative to this chunk into an absolute coord.
Definition: Chunk.h:450
void GetThreeRandomNumbers(int &a_X, int &a_Y, int &a_Z, int a_MaxX, int a_MaxY, int a_MaxZ)
Definition: Chunk.cpp:577
NIBBLETYPE GetSkyLight(Vector3i a_RelPos) const
Get the level of sky light illuminating the block (0 - 15) independent of daytime.
Definition: Chunk.h:306
void MarkLoaded(void)
Definition: Chunk.cpp:282
cChunk * GetRelNeighborChunkAdjustCoords(Vector3i &a_RelPos) const
Returns the chunk into which the relatively-specified block belongs.
Definition: Chunk.cpp:1885
cChunkCoords GetPos() const
Definition: Chunk.h:133
bool HasClient(cClientHandle *a_Client)
Returns true if the specified client is present in this chunk.
Definition: Chunk.cpp:1564
int GetHeight(int a_X, int a_Z) const
Definition: Chunk.cpp:1177
int m_PosX
Definition: Chunk.h:508
cBlockEntity * GetBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ)
OBSOLETE, use the Vector3i-based overload instead.
Definition: Chunk.h:425
void WakeUpSimulators(void)
Wakes up each simulator for its specific blocks; through all the blocks in the chunk.
Definition: Chunk.cpp:1233
cChunk * m_NeighborXP
Definition: Chunk.h:523
void MarkDirty(void)
Definition: Chunk.h:265
void GetRandomBlockCoords(int &a_X, int &a_Y, int &a_Z)
Definition: Chunk.cpp:598
bool IsWeatherSunnyAt(int a_RelX, int a_RelZ) const
Returns true if it is sunny at the specified location.
Definition: Chunk.cpp:1187
NIBBLETYPE GetTimeAlteredLight(NIBBLETYPE a_Skylight) const
Light alterations based on time.
Definition: Chunk.cpp:1977
bool m_IsDirty
Definition: Chunk.h:483
void SetBlock(Vector3i a_RelBlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Definition: Chunk.cpp:1263
bool ForEachEntityInBox(const cBoundingBox &a_Box, cEntityCallback a_Callback) const
Calls the callback for each entity that has a nonempty intersection with the specified boundingbox.
Definition: Chunk.cpp:1674
bool CanUnload(void) const
Definition: Chunk.cpp:207
cBlockEntity * GetBlockEntity(Vector3i a_AbsPos)
Returns the block entity at the specified (absolute) coords.
Definition: Chunk.cpp:1418
void SetPresence(ePresence a_Presence)
Sets the chunk's presence.
Definition: Chunk.cpp:163
bool UnboundedRelGetBlockSkyLight(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE &a_SkyLight) const
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:368
bool IsLightValid(void) const
Definition: Chunk.h:83
bool UnboundedRelSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:392
void SetBiomeAt(int a_RelX, int a_RelZ, EMCSBiome a_Biome)
Sets the biome at the specified relative coords.
Definition: Chunk.cpp:1488
bool UnboundedRelSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Same as SetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:1135
void MarkLoadFailed(void)
Queues the chunk for generating.
Definition: Chunk.cpp:292
NIBBLETYPE GetSkyLightAltered(Vector3i a_RelPos) const
Get the level of sky light illuminating the block (0 - 15), taking daytime into a account.
Definition: Chunk.h:310
void MarkSaved(void)
Definition: Chunk.cpp:269
std::vector< sSetBlockQueueItem > sSetBlockQueueVector
Definition: Chunk.h:476
bool AddClient(cClientHandle *a_Client)
Adds a client to the chunk; returns true if added, false if already there.
Definition: Chunk.cpp:1520
bool UnboundedRelGetBlockSkyLight(Vector3i a_RelPos, NIBBLETYPE &a_SkyLight) const
Same as GetBlockSkyLight(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_Chun...
Definition: Chunk.cpp:1092
bool UnboundedRelGetBlockBlockLight(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE &a_BlockLight) const
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:356
cChunk * GetRelNeighborChunk(int a_RelX, int a_RelZ)
Returns the chunk into which the relatively-specified block belongs, by walking the neighbors.
Definition: Chunk.cpp:1818
void ApplyWeatherToTop(void)
Adds snow to the top of snowy biomes and hydrates farmland / fills cauldrons in rainy biomes.
Definition: Chunk.cpp:871
bool UnboundedRelFastSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Same as FastSetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1156
bool UnboundedRelGetBlockLights(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE &a_BlockLight, NIBBLETYPE &a_SkyLight) const
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:380
cFluidSimulatorData * m_WaterSimulatorData
Definition: Chunk.h:529
ePresence
Represents the presence state of the chunk.
Definition: Chunk.h:41
@ cpQueued
The chunk is not present, but is queued for loading / generation.
Definition: Chunk.h:43
@ cpPresent
The chunk is present.
Definition: Chunk.h:44
@ cpInvalid
The chunk is not present at all and is not queued in the loader / generator.
Definition: Chunk.h:42
cFireSimulatorChunkData m_FireSimulatorData
Definition: Chunk.h:528
NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:307
void Stay(bool a_Stay=true)
Sets or resets the internal flag that prevents chunk from being unloaded.
Definition: Chunk.cpp:529
void SetNextBlockToTick(const Vector3i a_RelPos)
Causes the specified block to be ticked on the next Tick() call.
Definition: Chunk.h:274
void SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle *a_Client)
Definition: Chunk.cpp:1373
cItems PickupsFromBlock(Vector3i a_RelPos, const cEntity *a_Digger, const cItem *a_Tool)
Returns the pickups that would be produced, if the specified block was dug up by a_Digger using a_Too...
Definition: Chunk.cpp:964
NIBBLETYPE GetSkyLightAltered(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:311
NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:303
bool CanUnloadAfterSaving(void) const
Returns true if the chunk could have been unloaded if it weren't dirty.
Definition: Chunk.cpp:221
cChunk * GetNeighborChunk(int a_BlockX, int a_BlockZ)
Returns the chunk into which the specified block belongs, by walking the neighbors.
Definition: Chunk.cpp:1806
cChunkDef::HeightMap m_HeightMap
Definition: Chunk.h:515
void TickBlock(const Vector3i a_RelPos)
Ticks a single block.
Definition: Chunk.cpp:757
int m_PosZ
Definition: Chunk.h:508
void MarkSaving(void)
Definition: Chunk.cpp:260
bool UnboundedRelGetBlock(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Same as GetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:1008
void FastSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta)
Definition: Chunk.h:141
void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta)
Definition: Chunk.h:286
bool IsQueued(void) const
Returns true iff the chunk is in the queue for loading / generating.
Definition: Chunk.h:61
ChunkBlockData m_BlockData
Definition: Chunk.h:512
cFluidSimulatorData * m_LavaSimulatorData
Definition: Chunk.h:530
bool IsWeatherWetAt(int a_RelX, int a_RelZ) const
Returns true if it is raining or storming at the specified location, taking into account biomes.
Definition: Chunk.cpp:1196
const auto & GetAllClients(void) const
Definition: Chunk.h:443
bool IsDirty(void) const
Returns true iff the chunk has changed since it was last saved.
Definition: Chunk.h:71
bool IsValid(void) const
Returns true iff the chunk block data is valid (loaded / generated)
Definition: Chunk.h:58
bool UnboundedRelGetBlockBlockLight(Vector3i a_RelPos, NIBBLETYPE &a_BlockLight) const
Same as GetBlockBlockLight(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_Ch...
Definition: Chunk.cpp:1071
cWorld * GetWorld(void) const
Definition: Chunk.h:135
cSandSimulatorChunkData & GetSandSimulatorData(void)
Definition: Chunk.h:415
int GetPosZ(void) const
Definition: Chunk.h:132
cChunk * m_NeighborZP
Definition: Chunk.h:525
cBlockEntity * GetBlockEntityRel(Vector3i a_RelPos)
Returns the block entity at the specified (relative) coords.
Definition: Chunk.cpp:1436
void GetBlockInfo(Vector3i a_RelPos, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_Meta, NIBBLETYPE &a_SkyLight, NIBBLETYPE &a_BlockLight) const
Definition: Chunk.cpp:1767
std::vector< cClientHandle * > m_LoadedByClient
Definition: Chunk.h:501
void CollectMobCensus(cMobCensus &toFill)
Recence all mobs proximities to players in order to know what to do with them.
Definition: Chunk.cpp:546
std::queue< Vector3i > m_BlocksToCheck
A queue of relative positions to call cBlockHandler::Check on.
Definition: Chunk.h:498
cBlockEntities m_BlockEntities
Definition: Chunk.h:503
bool UnboundedRelFastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:404
bool UnboundedRelGetBlockType(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE &a_BlockType) const
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:332
cWorld * m_World
Definition: Chunk.h:509
void MoveEntityToNewChunk(OwnedEntity a_Entity)
Called by Tick() when an entity moves out of this chunk into a neighbor; moves the entity and sends s...
Definition: Chunk.cpp:768
cChunk(int a_ChunkX, int a_ChunkZ, cChunkMap *a_ChunkMap, cWorld *a_World)
Definition: Chunk.cpp:41
void OnUnload()
Called when the chunkmap unloads unused chunks.
Definition: Chunk.cpp:235
cSandSimulatorChunkData m_SandSimulatorData
Definition: Chunk.h:531
bool ForEachBlockEntity(cBlockEntityCallback a_Callback)
Calls the callback for each block entity; returns true if all block entities processed,...
Definition: Chunk.cpp:1718
cFluidSimulatorData * GetWaterSimulatorData(void) const
Definition: Chunk.h:413
bool UnboundedRelGetBlockLights(Vector3i a_RelPos, NIBBLETYPE &a_BlockLight, NIBBLETYPE &a_SkyLight) const
Queries both BlockLight and SkyLight, relative coords needn't be in this chunk (uses m_Neighbor-s or ...
Definition: Chunk.cpp:1113
cRedstoneSimulatorChunkData * GetRedstoneSimulatorData(void) const
Definition: Chunk.h:416
unsigned m_StayCount
Number of times the chunk has been requested to stay (by various cChunkStay objects); if zero,...
Definition: Chunk.h:506
Vector3i m_BlockToTick
Relative coords of the block to tick first in the next Tick() call.
Definition: Chunk.h:520
bool UnboundedRelGetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
OBSOLETE, use the Vector3i-based overload.
Definition: Chunk.h:320
void SetMeta(Vector3i a_RelPos, NIBBLETYPE a_Meta)
Definition: Chunk.h:291
void BroadcastPendingChanges(void)
Flushes the pending block (entity) queue, and clients' outgoing data buffers.
Definition: Chunk.cpp:120
~cChunk()
Definition: Chunk.cpp:86
void Tick(std::chrono::milliseconds a_Dt)
Definition: Chunk.cpp:685
cChunkDef::BiomeMap m_BiomeMap
Definition: Chunk.h:516
sSetBlockVector m_PendingSendBlocks
Blocks that have changed and need to be sent to all clients.
Definition: Chunk.h:489
bool m_IsLightValid
Definition: Chunk.h:482
cRedstoneSimulatorChunkData * m_RedstoneSimulatorData
Definition: Chunk.h:532
OwnedEntity RemoveEntity(cEntity &a_Entity)
Releases ownership of the given entity if it was found in this chunk.
Definition: Chunk.cpp:1602
cChunk * m_NeighborZM
Definition: Chunk.h:524
bool m_IsSaving
Definition: Chunk.h:484
void SetAlwaysTicked(bool a_AlwaysTicked)
Increments (a_AlwaysTicked == true) or decrements (false) the m_AlwaysTicked counter.
Definition: Chunk.cpp:1456
cChunkMap * m_ChunkMap
Definition: Chunk.h:510
bool UnboundedRelGetBlockType(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType) const
Same as GetBlockType(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1029
void SetAreaBiome(int a_MinRelX, int a_MaxRelX, int a_MinRelZ, int a_MaxRelZ, EMCSBiome a_Biome)
Sets the biome in the specified relative coords area.
Definition: Chunk.cpp:1498
cChunk(const cChunk &Other)=delete
bool HasAnyClients(void) const
Returns true if theres any client in the chunk; false otherwise.
Definition: Chunk.cpp:1573
bool HasEntity(UInt32 a_EntityID) const
Definition: Chunk.cpp:1641
Vector3i PositionToWorldPosition(Vector3i a_RelPos)
Definition: Chunk.h:257
bool HasPlayerEntities() const
Check m_Entities for cPlayer objects.
Definition: Chunk.cpp:192
void GetBlockTypeMeta(Vector3i a_RelPos, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Definition: Chunk.cpp:1757
bool DoWithEntityByID(UInt32 a_EntityID, cEntityCallback a_Callback, bool &a_CallbackResult) const
Calls the callback if the entity with the specified ID is found, with the entity object as the callba...
Definition: Chunk.cpp:1700
void RemoveClient(cClientHandle *a_Client)
Removes the specified client from the chunk; ignored if client not in chunk.
Definition: Chunk.cpp:1536
NIBBLETYPE m_BlockMeta
Definition: Chunk.h:467
sSetBlockQueueItem(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Int64 a_Tick, BLOCKTYPE a_PreviousBlockType)
Definition: Chunk.h:470
BLOCKTYPE m_PreviousType
Definition: Chunk.h:468
NIBBLETYPE GetMeta(Vector3i a_Position) const
Definition: ChunkData.h:81
void SetMeta(Vector3i a_Position, NIBBLETYPE a_Meta)
Definition: ChunkData.h:87
BLOCKTYPE GetBlock(Vector3i a_Position) const
Definition: ChunkData.h:80
NIBBLETYPE GetSkyLight(Vector3i a_Position) const
Definition: ChunkData.h:120
NIBBLETYPE GetBlockLight(Vector3i a_Position) const
Definition: ChunkData.h:119
Wraps the chunk coords into a single structure.
Definition: ChunkDef.h:57
static Vector3i RelativeToAbsolute(Vector3i a_RelBlockPosition, cChunkCoords a_ChunkCoords)
Converts relative block coordinates into absolute coordinates with a known chunk location.
Definition: ChunkDef.h:174
HEIGHTTYPE HeightMap[Width *Width]
The type used for any heightmap operations and storage; idx = x + Width * z; Height points to the hig...
Definition: ChunkDef.h:132
NIBBLETYPE BlockNibbles[NumBlocks/2]
The type used for block data in nibble format, AXIS_ORDER ordering.
Definition: ChunkDef.h:143
static EMCSBiome GetBiome(const BiomeMap &a_BiomeMap, int a_X, int a_Z)
Definition: ChunkDef.h:319
EMCSBiome BiomeMap[Width *Width]
The type used for any biomemap operations and storage inside Cuberite, using Cuberite biomes (need no...
Definition: ChunkDef.h:137
Definition: Entity.h:76
Definition: Player.h:29
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
This class is used to collect information, for each Mob, what is the distance of the closest player i...
Definition: MobCensus.h:26
This class is used to determine which monster can be spawned in which place it is essentially static ...
Definition: MobSpawner.h:16
Contains the data for a loaded / generated chunk, ready to be set into a cWorld.
Definition: SetChunkData.h:12
This is a base class for all fluid simulator data classes.
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
Definition: World.h:53
std::unordered_map< size_t, OwnedBlockEntity > cBlockEntities
Definition: BlockEntity.h:17
std::unique_ptr< cBlockEntity > OwnedBlockEntity
Definition: BlockEntity.h:16