Cuberite
A lightweight, fast and extensible game server for Minecraft
ChunkDesc.h
Go to the documentation of this file.
1 
2 // ChunkDesc.h
3 
4 // Declares the cChunkDesc class representing the chunk description used while generating a chunk. This class is also exported to Lua for HOOK_CHUNK_GENERATING.
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "../BlockArea.h"
13 #include "../Cuboid.h"
14 
15 
16 
17 
18 
19 // fwd: ../BlockArea.h
20 class cBlockArea;
21 
22 
23 
24 
25 
26 // tolua_begin
28 {
29 public:
30  // tolua_end
31 
36  typedef Byte Shape[256 * 16 * 16];
37 
40 
41 
42  cChunkDesc(cChunkCoords a_Coords);
43  ~cChunkDesc();
44 
45  void SetChunkCoords(cChunkCoords a_Coords);
46 
47  // tolua_begin
48 
49  int GetChunkX() const { return m_Coords.m_ChunkX; } // Prefer GetChunkCoords() instead
50  int GetChunkZ() const { return m_Coords.m_ChunkZ; } // Prefer GetChunkCoords() instead
51 
52  // tolua_end
53 
54  cChunkCoords GetChunkCoords() const { return m_Coords; }
55 
56  // tolua_begin
57 
58  void FillBlocks(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
59  void SetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
60 
61  // tolua_end
64  void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const;
65  // tolua_begin
66 
67  void SetBlockType(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType);
68  BLOCKTYPE GetBlockType(int a_RelX, int a_RelY, int a_RelZ) const;
69 
70  void SetBlockMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_BlockMeta);
71  NIBBLETYPE GetBlockMeta(int a_RelX, int a_RelY, int a_RelZ) const;
72 
73  void SetBiome(int a_RelX, int a_RelZ, EMCSBiome a_BiomeID);
74  EMCSBiome GetBiome(int a_RelX, int a_RelZ) const;
75 
76  // These operate on the heightmap, so they could get out of sync with the data
77  // Use UpdateHeightmap() to re-calculate heightmap from the block data
78  void SetHeight(int a_RelX, int a_RelZ, HEIGHTTYPE a_Height);
79  HEIGHTTYPE GetHeight(int a_RelX, int a_RelZ) const;
80 
81  // tolua_end
82 
85  void SetHeightFromShape(const Shape & a_Shape);
86 
88  void GetShapeFromHeight(Shape & a_Shape) const;
89 
91  inline static size_t MakeShapeIndex(int a_X, int a_Y, int a_Z)
92  {
93  return static_cast<size_t>(a_Y + a_X * cChunkDef::Height + a_Z * cChunkDef::Height * cChunkDef::Width);
94  }
95 
96  inline static void SetShapeIsSolidAt(Shape & a_Shape, int a_X, int a_Y, int a_Z, bool a_IsSolid)
97  {
98  auto index = MakeShapeIndex(a_X, a_Y, a_Z);
99  a_Shape[index] = a_IsSolid ? 1 : 0;
100  }
101 
102  inline static bool GetShapeIsSolidAt(const Shape & a_Shape, int a_X, int a_Y, int a_Z)
103  {
104  auto index = MakeShapeIndex(a_X, a_Y, a_Z);
105  return a_Shape[index];
106  }
107 
108  // tolua_begin
109 
110  // Default generation:
111  void SetUseDefaultBiomes(bool a_bUseDefaultBiomes);
112  bool IsUsingDefaultBiomes(void) const;
113  void SetUseDefaultHeight(bool a_bUseDefaultHeight);
114  bool IsUsingDefaultHeight(void) const;
115  void SetUseDefaultComposition(bool a_bUseDefaultComposition);
116  bool IsUsingDefaultComposition(void) const;
117  void SetUseDefaultFinish(bool a_bUseDefaultFinish);
118  bool IsUsingDefaultFinish(void) const;
119 
121  void WriteBlockArea(const cBlockArea & a_BlockArea, int a_RelX, int a_RelY, int a_RelZ, cBlockArea::eMergeStrategy a_MergeStrategy = cBlockArea::msOverwrite);
122 
124  void ReadBlockArea(cBlockArea & a_Dest, int a_MinRelX, int a_MaxRelX, int a_MinRelY, int a_MaxRelY, int a_MinRelZ, int a_MaxRelZ);
125 
127  HEIGHTTYPE GetMaxHeight(void) const;
128 
130  HEIGHTTYPE GetMinHeight(void) const;
131 
133  void FillRelCuboid(
134  int a_MinX, int a_MaxX,
135  int a_MinY, int a_MaxY,
136  int a_MinZ, int a_MaxZ,
137  BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta
138  );
139 
141  void FillRelCuboid(const cCuboid & a_RelCuboid, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
142  {
144  a_RelCuboid.p1.x, a_RelCuboid.p2.x,
145  a_RelCuboid.p1.y, a_RelCuboid.p2.y,
146  a_RelCuboid.p1.z, a_RelCuboid.p2.z,
147  a_BlockType, a_BlockMeta
148  );
149  }
150 
152  void ReplaceRelCuboid(
153  int a_MinX, int a_MaxX,
154  int a_MinY, int a_MaxY,
155  int a_MinZ, int a_MaxZ,
156  BLOCKTYPE a_SrcType, NIBBLETYPE a_SrcMeta,
157  BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta
158  );
159 
162  const cCuboid & a_RelCuboid,
163  BLOCKTYPE a_SrcType, NIBBLETYPE a_SrcMeta,
164  BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta
165  )
166  {
168  a_RelCuboid.p1.x, a_RelCuboid.p2.x,
169  a_RelCuboid.p1.y, a_RelCuboid.p2.y,
170  a_RelCuboid.p1.z, a_RelCuboid.p2.z,
171  a_SrcType, a_SrcMeta,
172  a_DstType, a_DstMeta
173  );
174  }
175 
177  void FloorRelCuboid(
178  int a_MinX, int a_MaxX,
179  int a_MinY, int a_MaxY,
180  int a_MinZ, int a_MaxZ,
181  BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta
182  );
183 
186  const cCuboid & a_RelCuboid,
187  BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta
188  )
189  {
191  a_RelCuboid.p1.x, a_RelCuboid.p2.x,
192  a_RelCuboid.p1.y, a_RelCuboid.p2.y,
193  a_RelCuboid.p1.z, a_RelCuboid.p2.z,
194  a_DstType, a_DstMeta
195  );
196  }
197 
199  void RandomFillRelCuboid(
200  int a_MinX, int a_MaxX,
201  int a_MinY, int a_MaxY,
202  int a_MinZ, int a_MaxZ,
203  BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta,
204  int a_RandomSeed, int a_ChanceOutOf10k
205  );
206 
209  const cCuboid & a_RelCuboid, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta,
210  int a_RandomSeed, int a_ChanceOutOf10k
211  )
212  {
214  a_RelCuboid.p1.x, a_RelCuboid.p2.x,
215  a_RelCuboid.p1.y, a_RelCuboid.p2.y,
216  a_RelCuboid.p1.z, a_RelCuboid.p2.z,
217  a_BlockType, a_BlockMeta,
218  a_RandomSeed, a_ChanceOutOf10k
219  );
220  }
221 
225  cBlockEntity * GetBlockEntity(int a_RelX, int a_RelY, int a_RelZ);
226 
229  void UpdateHeightmap(void);
230 
231  // tolua_end
232 
233  // Accessors used by cChunkGenerator::Generator descendants:
234  inline cChunkDef::BiomeMap & GetBiomeMap (void) { return m_BiomeMap; }
235  inline cChunkDef::BlockTypes & GetBlockTypes (void) { return *(reinterpret_cast<cChunkDef::BlockTypes *>(m_BlockArea.GetBlockTypes())); }
236  // CANNOT, different compression!
237  // inline cChunkDef::BlockNibbles & GetBlockMetas (void) { return *((cChunkDef::BlockNibbles *)m_BlockArea.GetBlockMetas()); }
238  inline BlockNibbleBytes & GetBlockMetasUncompressed(void) { return *(reinterpret_cast<BlockNibbleBytes *>(m_BlockArea.GetBlockMetas())); }
239  inline cChunkDef::HeightMap & GetHeightMap (void) { return m_HeightMap; }
240  inline cEntityList & GetEntities (void) { return m_Entities; }
241  inline cBlockEntities & GetBlockEntities (void) { return m_BlockEntities; }
242 
243  inline const cChunkDef::BiomeMap & GetBiomeMap() const { return m_BiomeMap; }
244  inline const cChunkDef::BlockTypes & GetBlockTypes() const { return *(reinterpret_cast<cChunkDef::BlockTypes *>(m_BlockArea.GetBlockTypes())); }
245  inline const cChunkDef::HeightMap & GetHeightMap() const { return m_HeightMap; }
246 
248  void CompressBlockMetas(cChunkDef::BlockNibbles & a_DestMetas);
249 
250  #ifndef NDEBUG
252  void VerifyHeightmap(void);
253  #endif // !NDEBUG
254 
255 private:
257 
262  cBlockEntities m_BlockEntities; // Individual block entities are NOT owned by this object!
263 
268 } ; // tolua_export
269 
270 
271 
272 
EMCSBiome
Biome IDs The first batch corresponds to the clientside biomes, used by MineCraft.
Definition: BiomeDef.h:18
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char HEIGHTTYPE
The type used by the heightmap.
Definition: ChunkDef.h:47
std::vector< OwnedEntity > cEntityList
Definition: ChunkDef.h:33
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
unsigned char Byte
Definition: Globals.h:161
NIBBLETYPE * GetBlockMetas(void) const
Definition: BlockArea.h:390
BLOCKTYPE * GetBlockTypes(void) const
Returns the internal pointer to the block types.
Definition: BlockArea.h:389
eMergeStrategy
The per-block strategy to use when merging another block area into this object.
Definition: BlockArea.h:59
@ msOverwrite
Definition: BlockArea.h:60
Wraps the chunk coords into a single structure.
Definition: ChunkDef.h:57
int m_ChunkZ
Definition: ChunkDef.h:60
int m_ChunkX
Definition: ChunkDef.h:59
BLOCKTYPE BlockTypes[NumBlocks]
The type used for block type operations and storage, AXIS_ORDER ordering.
Definition: ChunkDef.h:140
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
static const int Width
Definition: ChunkDef.h:124
NIBBLETYPE BlockNibbles[NumBlocks/2]
The type used for block data in nibble format, AXIS_ORDER ordering.
Definition: ChunkDef.h:143
static const int Height
Definition: ChunkDef.h:125
static const int NumBlocks
Definition: ChunkDef.h:126
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: Cuboid.h:10
Vector3i p2
Definition: Cuboid.h:13
Vector3i p1
Definition: Cuboid.h:13
HEIGHTTYPE GetHeight(int a_RelX, int a_RelZ) const
Definition: ChunkDesc.cpp:144
void ReadBlockArea(cBlockArea &a_Dest, int a_MinRelX, int a_MaxRelX, int a_MinRelY, int a_MaxRelY, int a_MinRelZ, int a_MaxRelZ)
Reads an area from the chunk into a cBlockArea, blocktypes and blockmetas.
Definition: ChunkDesc.cpp:280
static size_t MakeShapeIndex(int a_X, int a_Y, int a_Z)
Returns the index into the internal shape array for the specified coords.
Definition: ChunkDesc.h:91
bool IsUsingDefaultHeight(void) const
Definition: ChunkDesc.cpp:226
const cChunkDef::BiomeMap & GetBiomeMap() const
Definition: ChunkDesc.h:243
EMCSBiome GetBiome(int a_RelX, int a_RelZ) const
Definition: ChunkDesc.cpp:126
cBlockEntity * GetBlockEntity(int a_RelX, int a_RelY, int a_RelZ)
Returns the block entity at the specified coords.
Definition: ChunkDesc.cpp:573
void SetBlockType(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType)
Definition: ChunkDesc.cpp:81
void FloorRelCuboid(const cCuboid &a_RelCuboid, BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta)
Replaces the blocks in the cuboid by the dst blocks if they are considered non-floor (air,...
Definition: ChunkDesc.h:185
bool IsUsingDefaultBiomes(void) const
Definition: ChunkDesc.cpp:208
void RandomFillRelCuboid(int a_MinX, int a_MaxX, int a_MinY, int a_MaxY, int a_MinZ, int a_MaxZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_RandomSeed, int a_ChanceOutOf10k)
Fills the relative cuboid with specified block with a random chance; allows cuboid out of range of th...
Definition: ChunkDesc.cpp:537
cEntityList & GetEntities(void)
Definition: ChunkDesc.h:240
Byte Shape[256 *16 *16]
The datatype used to represent the entire chunk worth of shape.
Definition: ChunkDesc.h:36
const cChunkDef::HeightMap & GetHeightMap() const
Definition: ChunkDesc.h:245
void SetUseDefaultBiomes(bool a_bUseDefaultBiomes)
Definition: ChunkDesc.cpp:199
void SetUseDefaultHeight(bool a_bUseDefaultHeight)
Definition: ChunkDesc.cpp:217
void FillRelCuboid(int a_MinX, int a_MaxX, int a_MinY, int a_MaxY, int a_MinZ, int a_MaxZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Fills the relative cuboid with specified block; allows cuboid out of range of this chunk.
Definition: ChunkDesc.cpp:431
static void SetShapeIsSolidAt(Shape &a_Shape, int a_X, int a_Y, int a_Z, bool a_IsSolid)
Definition: ChunkDesc.h:96
int GetChunkX() const
Definition: ChunkDesc.h:49
void SetHeight(int a_RelX, int a_RelZ, HEIGHTTYPE a_Height)
Definition: ChunkDesc.cpp:135
void SetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Definition: ChunkDesc.cpp:63
cChunkDef::BlockTypes & GetBlockTypes(void)
Definition: ChunkDesc.h:235
bool IsUsingDefaultFinish(void) const
Definition: ChunkDesc.cpp:262
bool m_bUseDefaultHeight
Definition: ChunkDesc.h:265
static bool GetShapeIsSolidAt(const Shape &a_Shape, int a_X, int a_Y, int a_Z)
Definition: ChunkDesc.h:102
void SetUseDefaultComposition(bool a_bUseDefaultComposition)
Definition: ChunkDesc.cpp:235
void SetChunkCoords(cChunkCoords a_Coords)
Definition: ChunkDesc.cpp:45
cBlockArea m_BlockArea
Definition: ChunkDesc.h:259
void SetBlockMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_BlockMeta)
Definition: ChunkDesc.cpp:108
cBlockEntities & GetBlockEntities(void)
Definition: ChunkDesc.h:241
bool m_bUseDefaultBiomes
Definition: ChunkDesc.h:264
void CompressBlockMetas(cChunkDef::BlockNibbles &a_DestMetas)
Compresses the metas from the BlockArea format (1 meta per byte) into regular format (2 metas per byt...
Definition: ChunkDesc.cpp:637
cChunkDef::HeightMap & GetHeightMap(void)
Definition: ChunkDesc.h:239
BlockNibbleBytes & GetBlockMetasUncompressed(void)
Definition: ChunkDesc.h:238
NIBBLETYPE GetBlockMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: ChunkDesc.cpp:99
void FillBlocks(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Definition: ChunkDesc.cpp:54
cChunkDef::BiomeMap m_BiomeMap
Definition: ChunkDesc.h:258
void SetUseDefaultFinish(bool a_bUseDefaultFinish)
Definition: ChunkDesc.cpp:253
void FloorRelCuboid(int a_MinX, int a_MaxX, int a_MinY, int a_MaxY, int a_MinZ, int a_MaxZ, BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta)
Replaces the blocks in the cuboid by the dst blocks if they are considered non-floor (air,...
Definition: ChunkDesc.cpp:498
bool m_bUseDefaultComposition
Definition: ChunkDesc.h:266
void ReplaceRelCuboid(const cCuboid &a_RelCuboid, BLOCKTYPE a_SrcType, NIBBLETYPE a_SrcMeta, BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta)
Replaces the specified src blocks in the cuboid by the dst blocks; allows cuboid out of range of this...
Definition: ChunkDesc.h:161
cEntityList m_Entities
Definition: ChunkDesc.h:261
void FillRelCuboid(const cCuboid &a_RelCuboid, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Fills the relative cuboid with specified block; allows cuboid out of range of this chunk.
Definition: ChunkDesc.h:141
void VerifyHeightmap(void)
Verifies that the heightmap corresponds to blocktype contents; if not, asserts on that column.
Definition: ChunkDesc.cpp:652
void ReplaceRelCuboid(int a_MinX, int a_MaxX, int a_MinY, int a_MaxY, int a_MinZ, int a_MaxZ, BLOCKTYPE a_SrcType, NIBBLETYPE a_SrcMeta, BLOCKTYPE a_DstType, NIBBLETYPE a_DstMeta)
Replaces the specified src blocks in the cuboid by the dst blocks; allows cuboid out of range of this...
Definition: ChunkDesc.cpp:461
cBlockEntities m_BlockEntities
Definition: ChunkDesc.h:262
void RandomFillRelCuboid(const cCuboid &a_RelCuboid, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_RandomSeed, int a_ChanceOutOf10k)
Fills the relative cuboid with specified block with a random chance; allows cuboid out of range of th...
Definition: ChunkDesc.h:208
HEIGHTTYPE GetMinHeight(void) const
Returns the minimum height value in the heightmap.
Definition: ChunkDesc.cpp:414
void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Returns the BlockType and BlockMeta at the specified coords.
Definition: ChunkDesc.cpp:72
void GetShapeFromHeight(Shape &a_Shape) const
Sets the shape in a_Shape to match the heightmap stored currently in m_HeightMap.
Definition: ChunkDesc.cpp:175
bool m_bUseDefaultFinish
Definition: ChunkDesc.h:267
bool IsUsingDefaultComposition(void) const
Definition: ChunkDesc.cpp:244
BLOCKTYPE GetBlockType(int a_RelX, int a_RelY, int a_RelZ) const
Definition: ChunkDesc.cpp:90
const cChunkDef::BlockTypes & GetBlockTypes() const
Definition: ChunkDesc.h:244
cChunkCoords m_Coords
Definition: ChunkDesc.h:256
void SetBiome(int a_RelX, int a_RelZ, EMCSBiome a_BiomeID)
Definition: ChunkDesc.cpp:117
void WriteBlockArea(const cBlockArea &a_BlockArea, int a_RelX, int a_RelY, int a_RelZ, cBlockArea::eMergeStrategy a_MergeStrategy=cBlockArea::msOverwrite)
Writes the block area into the chunk, with its origin set at the specified relative coords.
Definition: ChunkDesc.cpp:271
cChunkCoords GetChunkCoords() const
Definition: ChunkDesc.h:54
cChunkDef::HeightMap m_HeightMap
Definition: ChunkDesc.h:260
cChunkDef::BiomeMap & GetBiomeMap(void)
Definition: ChunkDesc.h:234
NIBBLETYPE BlockNibbleBytes[cChunkDef::NumBlocks]
Uncompressed block metas, 1 meta per byte.
Definition: ChunkDesc.h:39
int GetChunkZ() const
Definition: ChunkDesc.h:50
void UpdateHeightmap(void)
Updates the heightmap to match the current contents.
Definition: ChunkDesc.cpp:612
void SetHeightFromShape(const Shape &a_Shape)
Sets the heightmap to match the given shape data.
Definition: ChunkDesc.cpp:153
HEIGHTTYPE GetMaxHeight(void) const
Returns the maximum height value in the heightmap.
Definition: ChunkDesc.cpp:397
cChunkDesc(cChunkCoords a_Coords)
Definition: ChunkDesc.cpp:16
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
std::unordered_map< size_t, OwnedBlockEntity > cBlockEntities
Definition: BlockEntity.h:17