Cuberite
A lightweight, fast and extensible game server for Minecraft
RedstoneSimulatorChunkData.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <stack>
5 
6 #include "Chunk.h"
7 #include "BlockState.h"
9 
10 
11 
12 
13 
14 using PowerLevel = unsigned char;
15 
16 
17 
18 
19 
21 {
22 public:
23 
24  void WakeUp(const Vector3i & a_Position)
25  {
26  m_ActiveBlocks.push(a_Position);
27  }
28 
29  auto & GetActiveBlocks()
30  {
31  return m_ActiveBlocks;
32  }
33 
34  PowerLevel GetCachedPowerData(const Vector3i Position) const
35  {
36  auto Result = m_CachedPowerLevels.find(Position);
37  return (Result == m_CachedPowerLevels.end()) ? 0 : Result->second;
38  }
39 
40  void SetCachedPowerData(const Vector3i Position, const PowerLevel PowerLevel)
41  {
42  m_CachedPowerLevels[Position] = PowerLevel;
43  }
44 
45  std::pair<int, bool> * GetMechanismDelayInfo(const Vector3i Position)
46  {
47  auto Result = m_MechanismDelays.find(Position);
48  return (Result == m_MechanismDelays.end()) ? nullptr : &Result->second;
49  }
50 
52  void ErasePowerData(const Vector3i Position)
53  {
54  m_CachedPowerLevels.erase(Position);
55  m_MechanismDelays.erase(Position);
56  AlwaysTickedPositions.erase(Position);
57  WireStates.erase(Position);
58  ObserverCache.erase(Position);
59  }
60 
62  {
63  auto Result = m_CachedPowerLevels.find(a_Position);
64 
65  if (Result == m_CachedPowerLevels.end())
66  {
67  m_CachedPowerLevels[a_Position] = Power;
68  return 0;
69  }
70 
71  return std::exchange(Result->second, Power);
72  }
73 
75  inline static Vector3i RebaseRelativePosition(const cChunk & From, const cChunk & To, const Vector3i Position)
76  {
77  return
78  {
79  Position.x + (From.GetPosX() - To.GetPosX()) * cChunkDef::Width,
80  Position.y,
81  Position.z + (From.GetPosZ() - To.GetPosZ()) * cChunkDef::Width
82  };
83  }
84 
86  std::unordered_map<Vector3i, BlockState, VectorHasher<int>> WireStates;
87 
88  std::unordered_set<Vector3i, VectorHasher<int>> AlwaysTickedPositions;
89 
91  std::unordered_map<Vector3i, std::pair<BLOCKTYPE, NIBBLETYPE>, VectorHasher<int>> ObserverCache;
92 
94  std::unordered_map<Vector3i, std::pair<int, bool>, VectorHasher<int>> m_MechanismDelays;
95 
96 private:
97 
98  std::stack<Vector3i, std::vector<Vector3i>> m_ActiveBlocks;
99 
100  // TODO: map<Vector3i, int> -> Position of torch + it's heat level
101 
102  std::unordered_map<Vector3i, PowerLevel, VectorHasher<int>> m_CachedPowerLevels;
103 
105 };
unsigned char PowerLevel
UInt32 From(const BlockState Block)
Definition: Palette_1_13.cpp:7
unsigned char Power(const BlockState Block)
Definition: Chunk.h:36
int GetPosX(void) const
Definition: Chunk.h:131
int GetPosZ(void) const
Definition: Chunk.h:132
static const int Width
Definition: ChunkDef.h:124
std::stack< Vector3i, std::vector< Vector3i > > m_ActiveBlocks
std::unordered_set< Vector3i, VectorHasher< int > > AlwaysTickedPositions
std::pair< int, bool > * GetMechanismDelayInfo(const Vector3i Position)
std::unordered_map< Vector3i, std::pair< int, bool >, VectorHasher< int > > m_MechanismDelays
Structure storing position of mechanism + it's delay ticks (countdown) & if to power on.
static Vector3i RebaseRelativePosition(const cChunk &From, const cChunk &To, const Vector3i Position)
Adjust From-relative coordinates into To-relative coordinates.
PowerLevel ExchangeUpdateOncePowerData(const Vector3i &a_Position, PowerLevel Power)
std::unordered_map< Vector3i, PowerLevel, VectorHasher< int > > m_CachedPowerLevels
PowerLevel GetCachedPowerData(const Vector3i Position) const
std::unordered_map< Vector3i, std::pair< BLOCKTYPE, NIBBLETYPE >, VectorHasher< int > > ObserverCache
Structure storing an observer's last seen block.
std::unordered_map< Vector3i, BlockState, VectorHasher< int > > WireStates
Temporary, should be chunk data: wire block store, to avoid recomputing states every time.
void SetCachedPowerData(const Vector3i Position, const PowerLevel PowerLevel)
void ErasePowerData(const Vector3i Position)
Erase all cached redstone data for position.
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17