Cuberite
A lightweight, fast and extensible game server for Minecraft
SetChunkData.cpp
Go to the documentation of this file.
1 
2 // SetChunkData.cpp
3 
4 // Implements the cSetChunkData class used for sending loaded / generated chunk
5 
6 #include "Globals.h"
7 #include "SetChunkData.h"
9 #include "Entities/Entity.h"
10 
11 namespace
12 {
13 struct sMemCallbacks:
14  cAllocationPool<cChunkData::sChunkSection>::cStarvationCallbacks
15 {
16  virtual void OnStartUsingReserve() override {}
17  virtual void OnEndUsingReserve() override {}
18  virtual void OnOutOfReserve() override {}
19 };
20 } // namespace (anonymous)
21 
22 
23 
24 
25 
26 cSetChunkData::cSetChunkData(int a_ChunkX, int a_ChunkZ, bool a_ShouldMarkDirty) :
27  m_ChunkX(a_ChunkX),
28  m_ChunkZ(a_ChunkZ),
29  m_Pool(cpp14::make_unique<sMemCallbacks>(), 0u, cChunkData::NumSections),
30  m_ChunkData(m_Pool),
31  m_IsLightValid(false),
32  m_IsHeightMapValid(false),
33  m_AreBiomesValid(false),
34  m_ShouldMarkDirty(a_ShouldMarkDirty)
35 {
36 }
37 
38 
39 
40 
41 
43  int a_ChunkX, int a_ChunkZ,
44  const BLOCKTYPE * a_BlockTypes,
45  const NIBBLETYPE * a_BlockMetas,
46  const NIBBLETYPE * a_BlockLight,
47  const NIBBLETYPE * a_SkyLight,
48  const cChunkDef::HeightMap * a_HeightMap,
49  const cChunkDef::BiomeMap * a_Biomes,
50  cEntityList && a_Entities,
51  cBlockEntities && a_BlockEntities,
52  bool a_ShouldMarkDirty
53 ) :
54  cSetChunkData(a_ChunkX, a_ChunkZ, a_ShouldMarkDirty)
55 {
56  // Check the params' validity:
57  ASSERT(a_BlockTypes != nullptr);
58  ASSERT(a_BlockMetas != nullptr);
59 
60  // Copy block types and metas:
61  m_ChunkData.SetBlockTypes(a_BlockTypes);
62  m_ChunkData.SetMetas(a_BlockMetas);
63 
64  // Copy lights, if both given:
65  if ((a_BlockLight != nullptr) && (a_SkyLight != nullptr))
66  {
67  m_ChunkData.SetBlockLight(a_BlockLight);
68  m_ChunkData.SetSkyLight(a_SkyLight);
69  m_IsLightValid = true;
70  }
71 
72  // Copy the heightmap, if available:
73  if (a_HeightMap != nullptr)
74  {
75  memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap));
76  m_IsHeightMapValid = true;
77  }
78 
79  // Copy biomes, if available:
80  if (a_Biomes != nullptr)
81  {
82  memcpy(m_Biomes, a_Biomes, sizeof(m_Biomes));
83  m_AreBiomesValid = true;
84  }
85 
86  // Move entities and blockentities:
87  m_Entities = std::move(a_Entities);
88  m_BlockEntities = std::move(a_BlockEntities);
89 }
90 
91 
92 
93 
94 
96 {
97  // Find the heighest present section in the chunk
98  size_t MaxSection = 0;
99  for (size_t i = cChunkData::NumSections - 1; i != 0; --i)
100  {
101  if (m_ChunkData.GetSection(i) != nullptr)
102  {
103  MaxSection = i;
104  break;
105  }
106  }
107  const int MaxHeight = static_cast<int>(MaxSection + 1) * cChunkData::SectionHeight - 1;
108 
109  for (int x = 0; x < cChunkDef::Width; x++)
110  {
111  for (int z = 0; z < cChunkDef::Width; z++)
112  {
113  for (int y = MaxHeight; y > -1; y--)
114  {
115  if (m_ChunkData.GetBlock({x, y, z}) != E_BLOCK_AIR)
116  {
117  m_HeightMap[x + z * cChunkDef::Width] = static_cast<HEIGHTTYPE>(y);
118  break;
119  }
120  } // for y
121  } // for z
122  } // for x
123  m_IsHeightMapValid = true;
124 }
125 
126 
127 
128 
129 
131 {
132  for (cBlockEntities::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end();)
133  {
134  cBlockEntity * BlockEntity = itr->second;
135  BLOCKTYPE EntityBlockType = BlockEntity->GetBlockType();
136  BLOCKTYPE WorldBlockType = m_ChunkData.GetBlock({BlockEntity->GetRelX(), BlockEntity->GetPosY(), BlockEntity->GetRelZ()});
137  if (EntityBlockType != WorldBlockType)
138  {
139  // Bad blocktype, remove the block entity:
140  FLOGD("Block entity blocktype mismatch at {0}: entity for blocktype {1}({2}) in block {3}({4}). Deleting the block entity.",
141  BlockEntity->GetPos(),
142  ItemTypeToString(EntityBlockType), EntityBlockType,
143  ItemTypeToString(WorldBlockType), WorldBlockType
144  );
145  delete BlockEntity;
146  itr = m_BlockEntities.erase(itr);
147  }
148  else
149  {
150  // Good blocktype, keep the block entity:
151  ++itr;
152  }
153  } // for itr - m_BlockEntities[]
154 }
155 
156 
157 
158 
std::map< size_t, cBlockEntity * > cBlockEntities
Definition: ChunkDef.h:34
cBlockEntities m_BlockEntities
Definition: SetChunkData.h:98
cChunkDef::HeightMap m_HeightMap
Definition: SetChunkData.h:95
std::unique_ptr< T > make_unique(Args &&...args)
Definition: Globals.h:381
BLOCKTYPE GetBlockType() const
Definition: BlockEntity.h:111
int GetPosY() const
Definition: BlockEntity.h:106
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
bool m_IsHeightMapValid
Definition: SetChunkData.h:101
const sChunkSection * GetSection(size_t a_SectionNum) const
Return a pointer to the chunk section or nullptr if all air.
Definition: ChunkData.cpp:288
BLOCKTYPE GetBlock(Vector3i a_RelPos) const
Definition: ChunkData.cpp:136
static const int Width
Definition: ChunkDef.h:134
void SetBlockLight(const NIBBLETYPE *a_Src)
Copies the blocklight data from the specified flat array into the internal representation.
Definition: ChunkData.cpp:612
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
void CalculateHeightMap(void)
Calculates the heightmap based on the contained blocktypes and marks it valid.
unsigned char HEIGHTTYPE
The type used by the heightmap.
Definition: ChunkDef.h:48
void SetBlockTypes(const BLOCKTYPE *a_Src)
Copies the blocktype data from the specified flat array into the internal representation.
Definition: ChunkData.cpp:546
cSetChunkData(int a_ChunkX, int a_ChunkZ, bool a_ShouldMarkDirty)
Constructs a new instance with empty data.
std::vector< OwnedEntity > cEntityList
Definition: ChunkDef.h:33
Vector3i GetPos() const
Definition: BlockEntity.h:104
#define ASSERT(x)
Definition: Globals.h:335
bool m_AreBiomesValid
Definition: SetChunkData.h:102
void SetSkyLight(const NIBBLETYPE *a_Src)
Copies the skylight data from the specified flat array into the internal representation.
Definition: ChunkData.cpp:648
cChunkDef::BiomeMap m_Biomes
Definition: SetChunkData.h:96
cEntityList m_Entities
Definition: SetChunkData.h:97
void RemoveInvalidBlockEntities(void)
Removes the block entities that don&#39;t have a proper blocktype at their corresponding coords...
#define FLOGD(...)
Definition: LoggerSimple.h:48
int GetRelZ() const
Definition: BlockEntity.h:119
int GetRelX() const
Definition: BlockEntity.h:118
static const size_t NumSections
Definition: ChunkData.h:24
cChunkData m_ChunkData
Definition: SetChunkData.h:94
void SetMetas(const NIBBLETYPE *a_Src)
Copies the metadata from the specified flat array into the internal representation.
Definition: ChunkData.cpp:579
Definition: Globals.h:378
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:142
EMCSBiome BiomeMap[Width *Width]
The type used for any biomemap operations and storage inside Cuberite, using Cuberite biomes (need no...
Definition: ChunkDef.h:147
AString ItemTypeToString(short a_ItemType)
Translates itemtype into a string.
Definition: BlockID.cpp:270
static const int SectionHeight
Definition: ChunkData.h:23