Cuberite
A lightweight, fast and extensible game server for Minecraft
ChunkData.h
Go to the documentation of this file.
1 
2 // ChunkData.h
3 
4 // Declares the cChunkData class that represents the block's type, meta, blocklight and skylight storage for a chunk
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "FunctionRef.h"
13 #include "ChunkDef.h"
14 
15 
16 
17 
18 
19 template <class ElementType, size_t ElementCount, ElementType DefaultValue>
21 {
22  using Type = std::array<ElementType, ElementCount>;
23 
26 
29  ElementType Get(Vector3i a_Position) const;
30 
33  Type * GetSection(size_t a_Y) const;
34 
37  void Set(Vector3i a_Position, ElementType a_Value);
38 
41  void SetSection(const ElementType (& a_Source)[ElementCount], size_t a_Y);
42 
45  void SetAll(const ElementType (& a_Source)[cChunkDef::NumSections * ElementCount]);
46 
48  std::unique_ptr<Type> Store[cChunkDef::NumSections];
49 };
50 
51 
52 
53 
54 
56 {
57 public:
58 
60  static constexpr size_t SectionMetaCount = SectionBlockCount / 2;
61 
62  static constexpr BLOCKTYPE DefaultValue = 0x00;
63  static constexpr NIBBLETYPE DefaultMetaValue = 0x00;
64 
67 
68 private:
69 
72 
73 public:
74 
75  using BlockArray = decltype(m_Blocks)::Type;
76  using MetaArray = decltype(m_Metas)::Type;
77 
78  void Assign(const ChunkBlockData & a_Other);
79 
80  BLOCKTYPE GetBlock(Vector3i a_Position) const { return m_Blocks.Get(a_Position); }
81  NIBBLETYPE GetMeta(Vector3i a_Position) const { return m_Metas.Get(a_Position); }
82 
83  BlockArray * GetSection(size_t a_Y) const { return m_Blocks.GetSection(a_Y); }
84  MetaArray * GetMetaSection(size_t a_Y) const { return m_Metas.GetSection(a_Y); }
85 
86  void SetBlock(Vector3i a_Position, BLOCKTYPE a_Block) { m_Blocks.Set(a_Position, a_Block); }
87  void SetMeta(Vector3i a_Position, NIBBLETYPE a_Meta) { m_Metas.Set(a_Position, a_Meta); }
88 
89  void SetAll(const cChunkDef::BlockTypes & a_BlockSource, const cChunkDef::BlockNibbles & a_MetaSource);
90  void SetSection(const SectionType & a_BlockSource, const SectionMetaType & a_MetaSource, size_t a_Y);
91 };
92 
93 
94 
95 
96 
98 {
99 public:
100 
102 
103  static constexpr NIBBLETYPE DefaultBlockLightValue = 0x00;
104  static constexpr NIBBLETYPE DefaultSkyLightValue = 0xFF;
105 
107 
108 private:
109 
112 
113 public:
114 
115  using LightArray = decltype(m_BlockLights)::Type;
116 
117  void Assign(const ChunkLightData & a_Other);
118 
119  NIBBLETYPE GetBlockLight(Vector3i a_Position) const { return m_BlockLights.Get(a_Position); }
120  NIBBLETYPE GetSkyLight(Vector3i a_Position) const { return m_SkyLights.Get(a_Position); }
121 
122  LightArray * GetBlockLightSection(size_t a_Y) const { return m_BlockLights.GetSection(a_Y); }
123  LightArray * GetSkyLightSection(size_t a_Y) const { return m_SkyLights.GetSection(a_Y); }
124 
125  void SetAll(const cChunkDef::BlockNibbles & a_BlockLightSource, const cChunkDef::BlockNibbles & a_SkyLightSource);
126  void SetSection(const SectionType & a_BlockLightSource, const SectionType & a_SkyLightSource, size_t a_Y);
127 };
128 
129 
130 
131 
132 
136 #define ChunkDef_ForEachSection(BlockData, LightData, Callback) \
137  do \
138  { \
139  for (size_t Y = 0; Y < cChunkDef::NumSections; ++Y) \
140  { \
141  const auto Blocks = BlockData.GetSection(Y); \
142  const auto Metas = BlockData.GetMetaSection(Y); \
143  const auto BlockLights = LightData.GetBlockLightSection(Y); \
144  const auto SkyLights = LightData.GetSkyLightSection(Y); \
145  if ((Blocks != nullptr) || (Metas != nullptr) || (BlockLights != nullptr) || (SkyLights != nullptr)) \
146  { \
147  Callback \
148  } \
149  } \
150  } while (false)
151 
152 
153 
154 
155 
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
std::array< ElementType, ElementCount > Type
Definition: ChunkData.h:22
void SetSection(const ElementType(&a_Source)[ElementCount], size_t a_Y)
Copies the data from the specified flat section array into the internal representation.
Definition: ChunkData.cpp:139
Type * GetSection(size_t a_Y) const
Returns a raw pointer to the internal representation of the specified section.
Definition: ChunkData.cpp:98
ElementType Get(Vector3i a_Position) const
Gets one value at the given position.
Definition: ChunkData.cpp:73
void Assign(const ChunkDataStore< ElementType, ElementCount, DefaultValue > &a_Other)
Copy assign from another ChunkDataStore.
Definition: ChunkData.cpp:55
void SetAll(const ElementType(&a_Source)[cChunkDef::NumSections *ElementCount])
Copies the data from the specified flat array into the internal representation.
Definition: ChunkData.cpp:160
std::unique_ptr< Type > Store[cChunkDef::NumSections]
Contains all the sections this ChunkDataStore manages.
Definition: ChunkData.h:48
void Set(Vector3i a_Position, ElementType a_Value)
Sets one value at the given position.
Definition: ChunkData.cpp:108
decltype(m_Blocks)::Type BlockArray
Definition: ChunkData.h:75
void SetAll(const cChunkDef::BlockTypes &a_BlockSource, const cChunkDef::BlockNibbles &a_MetaSource)
Definition: ChunkData.cpp:182
MetaArray * GetMetaSection(size_t a_Y) const
Definition: ChunkData.h:84
static constexpr size_t SectionBlockCount
Definition: ChunkData.h:59
NIBBLETYPE GetMeta(Vector3i a_Position) const
Definition: ChunkData.h:81
static constexpr BLOCKTYPE DefaultValue
Definition: ChunkData.h:62
static constexpr NIBBLETYPE DefaultMetaValue
Definition: ChunkData.h:63
NIBBLETYPE[SectionMetaCount] SectionMetaType
Definition: ChunkData.h:66
static constexpr size_t SectionMetaCount
Definition: ChunkData.h:60
decltype(m_Metas)::Type MetaArray
Definition: ChunkData.h:76
void Assign(const ChunkBlockData &a_Other)
Definition: ChunkData.cpp:172
void SetSection(const SectionType &a_BlockSource, const SectionMetaType &a_MetaSource, size_t a_Y)
Definition: ChunkData.cpp:192
BlockArray * GetSection(size_t a_Y) const
Definition: ChunkData.h:83
void SetMeta(Vector3i a_Position, NIBBLETYPE a_Meta)
Definition: ChunkData.h:87
BLOCKTYPE GetBlock(Vector3i a_Position) const
Definition: ChunkData.h:80
void SetBlock(Vector3i a_Position, BLOCKTYPE a_Block)
Definition: ChunkData.h:86
BLOCKTYPE[SectionBlockCount] SectionType
Definition: ChunkData.h:65
ChunkDataStore< NIBBLETYPE, SectionMetaCount, DefaultMetaValue > m_Metas
Definition: ChunkData.h:71
ChunkDataStore< BLOCKTYPE, SectionBlockCount, DefaultValue > m_Blocks
Definition: ChunkData.h:70
static constexpr NIBBLETYPE DefaultSkyLightValue
Definition: ChunkData.h:104
void SetAll(const cChunkDef::BlockNibbles &a_BlockLightSource, const cChunkDef::BlockNibbles &a_SkyLightSource)
Definition: ChunkData.cpp:212
NIBBLETYPE[SectionLightCount] SectionType
Definition: ChunkData.h:106
decltype(m_BlockLights)::Type LightArray
Definition: ChunkData.h:115
LightArray * GetSkyLightSection(size_t a_Y) const
Definition: ChunkData.h:123
ChunkDataStore< NIBBLETYPE, SectionLightCount, DefaultBlockLightValue > m_BlockLights
Definition: ChunkData.h:110
void SetSection(const SectionType &a_BlockLightSource, const SectionType &a_SkyLightSource, size_t a_Y)
Definition: ChunkData.cpp:222
LightArray * GetBlockLightSection(size_t a_Y) const
Definition: ChunkData.h:122
NIBBLETYPE GetSkyLight(Vector3i a_Position) const
Definition: ChunkData.h:120
static constexpr NIBBLETYPE DefaultBlockLightValue
Definition: ChunkData.h:103
ChunkDataStore< NIBBLETYPE, SectionLightCount, DefaultSkyLightValue > m_SkyLights
Definition: ChunkData.h:111
NIBBLETYPE GetBlockLight(Vector3i a_Position) const
Definition: ChunkData.h:119
static constexpr size_t SectionLightCount
Definition: ChunkData.h:101
void Assign(const ChunkLightData &a_Other)
Definition: ChunkData.cpp:202
BLOCKTYPE BlockTypes[NumBlocks]
The type used for block type operations and storage, AXIS_ORDER ordering.
Definition: ChunkDef.h:140
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 size_t NumSections
Definition: ChunkDef.h:129
static const int SectionHeight
Definition: ChunkDef.h:128