Cuberite
A lightweight, fast and extensible game server for Minecraft
FluidSimulator.cpp
Go to the documentation of this file.
1 #include "Globals.h"
2 
3 #include "FluidSimulator.h"
4 #include "../World.h"
5 
6 
7 
8 
9 
10 cFluidSimulator::cFluidSimulator(cWorld & a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid) :
11  super(a_World),
12  m_FluidBlock(a_Fluid),
13  m_StationaryFluidBlock(a_StationaryFluid)
14 {
15 }
16 
17 
18 
19 
20 
22 {
23  return ((a_BlockType == m_FluidBlock) || (a_BlockType == m_StationaryFluidBlock));
24 }
25 
26 
27 
28 
29 
31 {
32  switch (a_BlockType)
33  {
36  case E_BLOCK_BEETROOTS:
37  case E_BLOCK_BIG_FLOWER:
39  case E_BLOCK_CACTUS:
40  case E_BLOCK_COBWEB:
41  case E_BLOCK_CROPS:
42  case E_BLOCK_DEAD_BUSH:
45  case E_BLOCK_LILY_PAD:
47  case E_BLOCK_RAIL:
54  case E_BLOCK_RED_ROSE:
55  case E_BLOCK_SNOW:
56  case E_BLOCK_SUGARCANE:
57  case E_BLOCK_TALL_GRASS:
58  case E_BLOCK_TORCH:
60  case E_BLOCK_TRIPWIRE:
62  {
63  return true;
64  }
65  default:
66  {
67  return false;
68  }
69  }
70 }
71 
72 
73 
74 
75 
77 {
78  return !IsPassableForFluid(a_BlockType);
79 }
80 
81 
82 
83 
84 
86 {
87  return (
88  (a_BlockType == E_BLOCK_AIR) ||
89  (a_BlockType == E_BLOCK_FIRE) ||
90  IsAllowedBlock(a_BlockType) ||
91  CanWashAway(a_BlockType)
92  );
93 }
94 
95 
96 
97 
98 
100 {
101  if (a_Meta1 == 0)
102  {
103  // Source block is higher than anything, even itself.
104  return true;
105  }
106  if ((a_Meta1 & 0x08) != 0)
107  {
108  // Falling fluid is higher than anything, including self
109  return true;
110  }
111 
112  if (a_Meta2 == 0)
113  {
114  // Second block is a source and first block isn't
115  return false;
116  }
117  if ((a_Meta2 & 0x08) != 0)
118  {
119  // Second block is falling and the first one is neither a source nor falling
120  return false;
121  }
122 
123  // All special cases have been handled, now it's just a raw comparison:
124  return (a_Meta1 < a_Meta2);
125 }
126 
127 
128 
129 
130 
132 {
133  if (!cChunkDef::IsValidHeight(a_Y))
134  {
135  return {};
136  }
137 
138  if (!IsAllowedBlock(m_World.GetBlock(a_X, a_Y, a_Z))) // No Fluid -> No Flowing direction :D
139  {
140  return {};
141  }
142 
143  const auto HeightFromMeta = [](NIBBLETYPE a_BlockMeta) -> NIBBLETYPE
144  {
145  // Falling water blocks are always full height (0)
146  return ((a_BlockMeta & 0x08) != 0) ? 0 : a_BlockMeta;
147  };
148 
149  auto BlockMeta = m_World.GetBlockMeta(a_X, a_Y, a_Z);
150  NIBBLETYPE CentralPoint = HeightFromMeta(BlockMeta);
151  NIBBLETYPE LevelPoint[4];
152 
153  // blocks around the checking pos
154  Vector3i Points[]
155  {
156  { a_X + 1, a_Y, a_Z },
157  { a_X, a_Y, a_Z + 1 },
158  { a_X - 1, a_Y, a_Z },
159  { a_X, a_Y, a_Z - 1 }
160  };
161 
162  for (size_t i = 0; i < ARRAYCOUNT(LevelPoint); i++)
163  {
164  if (IsAllowedBlock(m_World.GetBlock(Points[i])))
165  {
166  LevelPoint[i] = HeightFromMeta(m_World.GetBlockMeta(Points[i]));
167  }
168  else
169  {
170  LevelPoint[i] = CentralPoint;
171  }
172  }
173 
175 
176  // Calculate the flow direction
177 
178  Direction.x = (LevelPoint[0] - LevelPoint[2]) / 2.0f;
179  Direction.z = (LevelPoint[1] - LevelPoint[3]) / 2.0f;
180 
181  if ((BlockMeta & 0x08) != 0) // Test falling bit
182  {
183  Direction.y = -1.0f;
184  }
185 
186  return Direction;
187 }
188 
virtual bool IsAllowedBlock(BLOCKTYPE a_BlockType) override
Returns true if the specified block type is "interesting" for this simulator.
BLOCKTYPE GetBlock(Vector3i a_BlockPos)
Returns the block type at the specified position.
Definition: World.h:416
bool IsSolidBlock(BLOCKTYPE a_BlockType)
T x
Definition: Vector3.h:17
BLOCKTYPE m_FluidBlock
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
bool IsPassableForFluid(BLOCKTYPE a_BlockType)
BLOCKTYPE m_StationaryFluidBlock
static bool IsValidHeight(int a_Height)
Validates a height-coordinate.
Definition: ChunkDef.h:212
bool IsHigherMeta(NIBBLETYPE a_Meta1, NIBBLETYPE a_Meta2)
Returns true if a_Meta1 is a higher fluid than a_Meta2.
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
cWorld & m_World
Definition: Simulator.h:60
T y
Definition: Vector3.h:17
NIBBLETYPE GetBlockMeta(Vector3i a_BlockPos)
Returns the block meta at the specified position.
Definition: World.h:431
T z
Definition: Vector3.h:17
virtual Vector3f GetFlowingDirection(int a_X, int a_Y, int a_Z)
Returns a unit vector in the direction the fluid is flowing or a zero-vector if not flowing...
Base class for all block-based physics simulators (such as fluid, fire, falling blocks etc...
Definition: Simulator.h:19
static bool CanWashAway(BLOCKTYPE a_BlockType)
cFluidSimulator(cWorld &a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid)
Direction
Definition: World.h:65
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:290