Cuberite
A lightweight, fast and extensible game server for Minecraft
DelayedFluidSimulator.cpp
Go to the documentation of this file.
1 
2 // DelayedFluidSimulator.cpp
3 
4 // Interfaces to the cDelayedFluidSimulator class representing a fluid simulator that has a configurable delay
5 // before simulating a block. Each tick it takes a consecutive delay "slot" and simulates only blocks in that slot.
6 
7 #include "Globals.h"
8 
10 #include "../World.h"
11 #include "../Chunk.h"
12 
13 
14 
15 
16 
18 // cDelayedFluidSimulatorChunkData::cSlot
19 
20 bool cDelayedFluidSimulatorChunkData::cSlot::Add(int a_RelX, int a_RelY, int a_RelZ)
21 {
22  ASSERT(a_RelZ >= 0);
23  ASSERT(a_RelZ < static_cast<int>(ARRAYCOUNT(m_Blocks)));
24 
25  auto & Blocks = m_Blocks[a_RelZ];
26  const auto Index = cChunkDef::MakeIndex(a_RelX, a_RelY, a_RelZ);
27  for (const auto & Block : Blocks)
28  {
29  if (Block.Data == Index)
30  {
31  // Already present
32  return false;
33  }
34  } // for itr - Blocks[]
35  Blocks.emplace_back(a_RelX, a_RelY, a_RelZ, Index);
36  return true;
37 }
38 
39 
40 
41 
42 
44 // cDelayedFluidSimulatorChunkData:
45 
47  m_Slots(new cSlot[ToUnsigned(a_TickDelay)])
48 {
49 }
50 
51 
52 
53 
54 
56 {
57  delete[] m_Slots;
58  m_Slots = nullptr;
59 }
60 
61 
62 
63 
64 
66 // cDelayedFluidSimulator:
67 
68 cDelayedFluidSimulator::cDelayedFluidSimulator(cWorld & a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid, int a_TickDelay) :
69  Super(a_World, a_Fluid, a_StationaryFluid),
70  m_TickDelay(a_TickDelay),
71  m_AddSlotNum(a_TickDelay - 1),
72  m_SimSlotNum(0),
73  m_TotalBlocks(0)
74 {
75 }
76 
77 
78 
79 
80 
82 {
84  m_SimSlotNum += 1;
86  {
87  m_SimSlotNum = 0;
88  }
89 }
90 
91 
92 
93 
94 
95 void cDelayedFluidSimulator::SimulateChunk(std::chrono::milliseconds a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk)
96 {
97  auto ChunkDataRaw = (m_FluidBlock == E_BLOCK_WATER) ? a_Chunk->GetWaterSimulatorData() : a_Chunk->GetLavaSimulatorData();
98  cDelayedFluidSimulatorChunkData * ChunkData = static_cast<cDelayedFluidSimulatorChunkData *>(ChunkDataRaw);
100 
101  // Simulate all the blocks in the scheduled slot:
102  for (size_t i = 0; i < ARRAYCOUNT(Slot.m_Blocks); i++)
103  {
104  auto & Blocks = Slot.m_Blocks[i];
105  if (Blocks.empty())
106  {
107  continue;
108  }
109  for (const auto & Block : Blocks)
110  {
111  SimulateBlock(a_Chunk, Block.x, Block.y, Block.z);
112  }
113  m_TotalBlocks -= static_cast<int>(Blocks.size());
114  Blocks.clear();
115  }
116 }
117 
118 
119 
120 
121 
122 void cDelayedFluidSimulator::AddBlock(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_Block)
123 {
124  if ((a_Block != m_FluidBlock) && (a_Block != m_StationaryFluidBlock))
125  {
126  return;
127  }
128 
129  auto ChunkDataRaw = (m_FluidBlock == E_BLOCK_WATER) ? a_Chunk.GetWaterSimulatorData() : a_Chunk.GetLavaSimulatorData();
130  cDelayedFluidSimulatorChunkData * ChunkData = static_cast<cDelayedFluidSimulatorChunkData *>(ChunkDataRaw);
132 
133  // Add, if not already present:
134  if (!Slot.Add(a_Position.x, a_Position.y, a_Position.z))
135  {
136  return;
137  }
138 
139  ++m_TotalBlocks;
140 }
@ E_BLOCK_WATER
Definition: BlockType.h:18
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:231
auto ToUnsigned(T a_Val)
Definition: Globals.h:387
#define ASSERT(x)
Definition: Globals.h:276
Definition: Chunk.h:36
cFluidSimulatorData * GetLavaSimulatorData(void) const
Definition: Chunk.h:414
cFluidSimulatorData * GetWaterSimulatorData(void) const
Definition: Chunk.h:413
static size_t MakeIndex(int x, int y, int z)
Definition: ChunkDef.h:227
cSlot * m_Slots
Slots, one for each delay tick, each containing the blocks to simulate.
virtual ~cDelayedFluidSimulatorChunkData() override
std::vector< cCoordWithData< size_t > > m_Blocks[16]
Array of block containers, each item stores blocks for one Z coord size_t param is the block index (f...
bool Add(int a_RelX, int a_RelY, int a_RelZ)
Adds the specified block unless already present; returns true if added, false if the block was alread...
virtual void AddBlock(cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_Block) override
Called to simulate a new block.
virtual void SimulateChunk(std::chrono::milliseconds a_Dt, int a_ChunkX, int a_ChunkZ, cChunk *a_Chunk) override
virtual void SimulateBlock(cChunk *a_Chunk, int a_RelX, int a_RelY, int a_RelZ)=0
Called from SimulateChunk() to simulate each block in one slot of blocks.
cDelayedFluidSimulator(cWorld &a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid, int a_TickDelay)
virtual void Simulate(float a_Dt) override
BLOCKTYPE m_StationaryFluidBlock
BLOCKTYPE m_FluidBlock
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
Definition: World.h:53