My Project
BlockLightGridBase.h
1 #pragma once
2 
3 #include "BlockCommon.h"
4 #include "BlockConfig.h"
5 #include "IAttributeFields.h"
6 
7 namespace ParaEngine
8 {
9  class CBlockWorld;
10  class BlockChunk;
11  class LightData;
12 
14  class LightBlock
15  {
16  public:
17  Uint16x3 blockId;
18  int8_t brightness;
19 
20  LightBlock()
21  :blockId(0,0,0),brightness(0)
22  {
23  }
24 
25  LightBlock(uint16_t x,uint16_t y,uint16_t z,uint8_t lightValue)
26  :blockId(x,y,z),brightness(lightValue)
27  {
28  }
29 
30  LightBlock(const Uint16x3& pos,uint8_t lightValue)
31  :blockId(pos),brightness(lightValue)
32  {
33  }
34  };
35 
38  {
39  public:
41  struct Light
42  {
43  Uint16x3 blockId;
44  // 0 means normal refresh, 1 means force update neighbor within 1 blocks. - 1 to disable
45  int8_t sunlightUpdateRange;
46  int8_t pointLightUpdateRange;
47 
48  Light(Uint16x3& blockId_ws, uint8_t sunValue, uint8_t pointValue)
49  :blockId(blockId_ws), sunlightUpdateRange(sunValue), pointLightUpdateRange(pointValue)
50  {
51  }
52  };
53 
54  enum CellType
55  {
56  cp_empty,
57  cp_light,
58  cp_obstructure,
59  };
60 
61  public:
62  CBlockLightGridBase(CBlockWorld* pBlockWorld);
63  virtual ~CBlockLightGridBase();
64  ATTRIBUTE_DEFINE_CLASS(CBlockLightGridBase);
66  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
67 
68  ATTRIBUTE_METHOD1(CBlockLightGridBase, GetDirtyColumnCount_s, int*) { *p1 = cls->GetDirtyColumnCount(); return S_OK; }
69  ATTRIBUTE_METHOD1(CBlockLightGridBase, GetDirtyBlockCount_s, int*) { *p1 = cls->GetDirtyBlockCount(); return S_OK; }
70  ATTRIBUTE_METHOD1(CBlockLightGridBase, GetLightGridSize_s, int*) { *p1 = cls->GetLightGridSize(); return S_OK; }
71 
72  ATTRIBUTE_METHOD1(CBlockLightGridBase, GetLightCalculationStep_s, int*) { *p1 = cls->GetLightCalculationStep(); return S_OK; }
73  ATTRIBUTE_METHOD1(CBlockLightGridBase, SetLightCalculationStep_s, int) { cls->SetLightCalculationStep(p1); return S_OK; }
74 
75  public:
76  virtual void OnEnterWorld();
77  virtual void OnLeaveWorld();
78  virtual void OnWorldMove(uint16_t centerChunkX, uint16_t centerChunkZ);
79  virtual void UpdateLighting();
80 
81  //get light brightness of current and 26 nearby block
82  // @param brightness: pointer to uint8_t blockBrightness[27];
83  // @param nSize: if 1, it will only get the center block.
84  // @param nLightType: -1 means Max light. 0 means emissive block light, 1 means sun light. 2 means both. where brightness[i] is block light and brightness[i+nSize] is sunlight
85  // @return : true if exist. false if there is no block at the position.
86  virtual bool GetBrightness(Uint16x3& blockId, uint8_t* brightness, int nSize = 27, int nLightType = -1);
87 
91  virtual void SetLightDirty(Uint16x3& blockId_ws, bool isSunLight, int8 nUpdateRange = 0);
92 
93  virtual void NotifyBlockHeightChanged(uint16_t blockIdX_ws, uint16_t blockIdZ_ws, ChunkMaxHeight& prevBlockHeight);
94 
96  virtual void AddDirtyColumn(uint16_t chunkX_ws,uint16_t chunkZ_ws);
98  virtual void SetColumnPreloaded(uint16_t chunkX_ws, uint16_t chunkZ_ws);
100  virtual void SetColumnUnloaded(uint16_t chunkX_ws, uint16_t chunkZ_ws);
101 
103  virtual int GetDirtyColumnCount();
105  virtual int GetDirtyBlockCount();
106 
108  virtual int GetLightGridSize();
109 
111  virtual void SetLightGridSize(int nSize);
112 
117  virtual void SetLightCalculationStep(uint32 nTicks);
118  virtual uint32 GetLightCalculationStep() const;
119 
126  virtual int ForceAddChunkColumn(int nChunkWX, int nChunkWZ);
128  virtual int GetForcedChunkColumnCount();
129 
130  public:
131  //ignore all SetLightDirty calls
132  void SuspendLightUpdate();
133 
134  void ResumeLightUpdate();
135 
136  bool IsLightUpdateSuspended();
137  protected:
138  BlockIndex CalcLightDataIndex(const Uint16x3& blockId, bool bCreateIfNotExist = true);
139  LightData* GetLightData(const BlockIndex& index);
140  LightData* GetLightData(uint16_t x, uint16_t y, uint16_t z, bool bCreateIfNotExist = true);
141  BlockChunk* GetChunk(uint16_t x, uint16_t y, uint16_t z, bool bCreateIfNotExist = true);
142 
143  protected:
144  bool m_suspendLightUpdate;
145  CBlockWorld* m_pBlockWorld;
146  int32 m_nLightGridChunkSize;
147  uint32 m_nLightCalculationStep;
148  };
149 }
data structure used when resolving light
Definition: BlockLightGridBase.h:14
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
different physics engine has different winding order.
Definition: EventBinding.h:32
basic block world coordinate
Definition: BlockCoordinate.h:72
base class for an instance of block world
Definition: BlockWorld.h:35
Chunk is a 16*16*16 inside a region.
Definition: BlockChunk.h:138
Definition: BlockIndex.h:7
A common interface for all classes implementing IAttributeFields By implementing this class's virtual...
Definition: IAttributeFields.h:59
per block light data
Definition: BlockLightGridBase.h:41
Block Light Data.
Definition: BlockChunk.h:110
base class for block light grid.
Definition: BlockLightGridBase.h:37
chunk column's height map data
Definition: ChunkMaxHeight.h:6