My Project
BlockChunk.h
1 #pragma once
2 #include <vector>
3 #include <set>
4 #include "BlockTemplate.h"
5 
6 namespace ParaEngine
7 {
8  class VerticalChunkIterator;
9  class BlockRegion;
10  class BlockChunk;
11  class BlockTemplate;
12  class CBlockWorld;
13 
14  class Block
15  {
16  public:
17  friend class BlockChunk;
18 
19  Block() :m_pTemplate(nullptr), m_blockData(0), m_nInstanceCount(0)
20  {
21  }
22  Block(BlockTemplate *pTemplate, uint32_t nData = 0) :m_pTemplate(pTemplate), m_blockData(nData), m_nInstanceCount(0)
23  {
24  }
25 
26  inline BlockTemplate* GetTemplate()
27  {
28  return m_pTemplate;
29  }
30 
31  inline uint16_t GetTemplateId()
32  {
33  return m_pTemplate->GetID();
34  }
35 
36  inline uint32_t GetUserData(){return m_blockData;}
37 
38  /* get the texture index into the main block template. */
39  int32 GetTextureIndex();
40  protected:
41  inline void SetTemplate(BlockTemplate *pTemplate){
42  m_pTemplate = pTemplate;
43  }
44  inline void SetUserData(uint32_t data){
45  m_blockData = (uint16)data;
46  }
47  void PushEmptySlotIndex(uint16 nIndex){
48  m_pTemplate = NULL;
49  m_blockData = nIndex;
50  m_nInstanceCount = 0;
51  }
52  /* return the next empty slot index. */
53  uint16 PopEmptySlot(){
54  uint16 nNextEmptySlotIndex = m_blockData;
55  m_blockData = 0;
56  return nNextEmptySlotIndex;
57  }
58 
59  bool IsEmptySlot(){
60  return m_nInstanceCount == 0;
61  }
62 
63  uint16 GetInstanceCount(){
64  return m_nInstanceCount;
65  }
66 
67  uint16 DecreaseInstanceCount(){
68  --m_nInstanceCount;
69  return m_nInstanceCount;
70  }
71  uint16 IncreaseInstanceCount(){
72  ++m_nInstanceCount;
73  return m_nInstanceCount;
74  }
75  protected:
76  BlockTemplate* m_pTemplate;
77  uint16 m_blockData;
78  uint16 m_nInstanceCount;
79  };
80 
83  ChunkDataMask_HasMaskData = 0x1<<31,
84  ChunkDataMask_HasCustomData = 0x1<<30,
85  ChunkDataMask_HasBlockData = 0x1<<29,
86  };
87 
96  ChunkCustomDataType_Biomes,
97  // compress using same integer algorithm
98  ChunkCustomDataType_Biomes_SameInteger,
99  ChunkCustomDataType_TimeStamp,
100  // compress using same integer algorithm
101  ChunkCustomDataType_TimeStamp_SameInteger,
102  ChunkCustomDataType_Heightmap,
103  ChunkCustomDataType_LightValues,
104  // chunk data: following is 16*16*16 chunk data which may further contain custom data.
105  // this is always the last parent custom data since it does not specify size, and we will write to end of file.
106  ChunkCustomDataType_ChunksData = 100,
107  };
108 
110  class LightData
111  {
112  public:
113  inline LightData():m_value(0)
114  {
115  }
116 
117  uint8_t GetBrightness(bool isSunLight);
118 
119  void SetBrightness(uint8_t value,bool isSunLight);
120 
121  uint8_t GetMaxBrightness(CBlockWorld* pBlockWorld);
122 
124  void LoadBrightness(uint8_t sun, uint8_t torch);
125 
126  inline bool IsInfluencedBySun()
127  {
128  return (m_value & 0xF0) > 0;
129  }
130 
131  inline bool IsZero(){ return m_value == 0; }
132  private:
133  //bit usage:[0,3] point light brightness,[4,7] sun light
134  uint8 m_value;
135  };
136 
139  {
140  public:
141  /* 16*16*16 index for blocks (fixed sized at initialization). Index is -1 if block not exist. */
142  std::vector<int16_t> m_blockIndices;
143 
144  /* set of indices of all light emitting blocks in current chunk. */
145  std::set<uint16_t> m_lightBlockIndices;
146 
148  std::vector<LightData> m_lightmapArray;
149 
150  // in world space
151  Uint16x3 m_minBlockId_ws;
152  // in region space
153  Uint16x3 m_minBlockId_rs;
154  // in region space
155  Uint16x3 m_chunkId_rs;
156 
158  static int s_total_chunks;
159  protected:
160  // blocks pool that grows automatically as new blocks are added, removed.
161  std::vector<Block> m_blocks;
162  uint32 m_nDirty;
163  uint16 m_isBoundaryChunk;
164  uint16 m_emptyBlockSlotIndex;
165  BlockRegion* m_ownerBlockRegion;
166  int16_t m_packedChunkID;
167 
168  inline bool IsBoundaryChunk() const { return m_isBoundaryChunk>0; }
169  void SetBoundaryChunk(bool val) { m_isBoundaryChunk = val ? 1:0; }
171  int16 FindBlock(BlockTemplate* pTemplate);
172  int16 FindBlock(BlockTemplate* pTemplate, uint32 nData);
173  void SetBlockEmpty(uint16_t nBlockIndex, Block& block);
174  bool RecycleBlock(uint16 nIndex, Block& block);
175  Block* CreateBlock(uint16_t nBlockIndex);
176  public:
177  BlockChunk(uint16_t nPackedChunkId, BlockRegion* pRegion);
178  ~BlockChunk();
179 
180  static int GetTotalChunksInMemory();
181 
182  // reserve blocks
183  void ReserveBlocks(int nCount);
184 
186  int GetTotalBytes();
187 
191  bool CanBlockSeeTheSkyWS(uint16 x, uint16 y, uint16 z);
192 
193  uint32 GetBlockCount();
194 
195  inline bool IsDirty() const { return m_nDirty > 0; }
197  void SetDirty(bool val);
199  void SetDirtyByNeighbor();
200  void SetLightDirty();
202  bool IsDirtyByNeighbor();
204  bool IsDirtyByLight();
206  bool IsDirtyByBlockChange();
207 
208  void SetLightingInitialized(bool bInitialized);
209  bool IsLightingInitialized();
210  // reset all blocks
211  void Reset();
212 
214  Block& GetBlockByIndex(uint16_t nBlockIndex);
215 
219  Block* GetBlock(uint16_t nBlockIndex);
220 
224  int LoadBlocks(const std::vector<uint16_t>& blockIndices, BlockTemplate* pTemplate);
225 
226  Uint16x3 GetBlockPosRs(uint16 nBlockIndex);
227 
228  void UpdateHeightMapAtLoadTime(uint16_t nBlockIndex, bool isTransparent);
229 
230  void LoadBlock(uint16_t nBlockIndex, BlockTemplate* pTemplate);
231  void SetBlockTemplate(uint16_t nBlockIndex, BlockTemplate* pTemplate);
232 
233  BlockTemplate* GetBlockTemplate(uint16_t nBlockIndex);
234  void SetBlockData(uint16_t nBlockIndex, uint32 nData);
236  void SetBlock(uint16_t nBlockIndex, BlockTemplate* pTemplate, uint32 nData);
237 
239  CBlockWorld* GetBlockWorld();
240 
244  bool SetBlockToAir(Uint16x3& blockId_r);
245 
249  bool RemoveLight(Uint16x3& blockId_r);
250 
252  void AddLight(Uint16x3& blockId_r);
253  void AddLight(uint16 nPackedBlockID);
254 
258  LightData* GetLightData(uint16_t nIndex);
259 
261  bool IsInfluenceBySunLight();
262 
263  //This is a approximate function to determine if block is visible
264  //for non-transparent-liquid block return true if :
265  //1. all neighbors are solid
266  //2. block is at the boundary of region
267  //for transparent-liquid block return true if any neighbor is not the same
268  //kind template.
269  //@param index: packed chunk space block index [0,4096)
270  bool IsVisibleBlock(int32_t index,Block* pBlock = NULL);
271 
276  void QueryNeighborBlockData(const Uint16x3& blockId_cs,Block** pBlockData, int nFrom = 0, int nTo = 26);
277 
279  bool IsNearbyChunksLoaded();
280 
282  std::vector<Uint16x3> refreshBlockVisible(uint16_t blockTemplateId);
283 
284  void ClearAllLight();
285  };
286 }
using skip 1 algorithm.
Definition: BlockChunk.h:93
raw block index array.
Definition: BlockChunk.h:91
different physics engine has different winding order.
Definition: EventBinding.h:32
Definition: BlockChunk.h:14
ChunkDataMask
chunk data mask
Definition: BlockChunk.h:82
basic block world coordinate
Definition: BlockCoordinate.h:72
compress using delta.
Definition: BlockChunk.h:95
base class for an instance of block world
Definition: BlockWorld.h:35
Definition: enum_maker.hpp:46
Chunk is a 16*16*16 inside a region.
Definition: BlockChunk.h:138
block template base class.
Definition: BlockTemplate.h:15
static int s_total_chunks
total number of chunks
Definition: BlockChunk.h:158
ChunkCustomDataType
custom chunk data
Definition: BlockChunk.h:89
Block Light Data.
Definition: BlockChunk.h:110
std::vector< LightData > m_lightmapArray
16*16*16 of light data (fixed sized at initialization)
Definition: BlockChunk.h:148
512*512 region
Definition: BlockRegion.h:22