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_CARROTS:
41  case E_BLOCK_COBWEB:
42  case E_BLOCK_CROPS:
43  case E_BLOCK_DEAD_BUSH:
46  case E_BLOCK_LILY_PAD:
47  case E_BLOCK_POTATOES:
49  case E_BLOCK_RAIL:
56  case E_BLOCK_RED_ROSE:
57  case E_BLOCK_SNOW:
58  case E_BLOCK_SUGARCANE:
59  case E_BLOCK_TALL_GRASS:
60  case E_BLOCK_TORCH:
61  case E_BLOCK_TRIPWIRE:
64  {
65  return true;
66  }
67  default:
68  {
69  return false;
70  }
71  }
72 }
73 
74 
75 
76 
77 
79 {
80  return !IsPassableForFluid(a_BlockType);
81 }
82 
83 
84 
85 
86 
88 {
89  return (
90  (a_BlockType == E_BLOCK_AIR) ||
91  (a_BlockType == E_BLOCK_FIRE) ||
92  IsAllowedBlock(a_BlockType) ||
93  CanWashAway(a_BlockType)
94  );
95 }
96 
97 
98 
99 
100 
102 {
103  if (a_Meta1 == 0)
104  {
105  // Source block is higher than anything, even itself.
106  return true;
107  }
108  if ((a_Meta1 & 0x08) != 0)
109  {
110  // Falling fluid is higher than anything, including self
111  return true;
112  }
113 
114  if (a_Meta2 == 0)
115  {
116  // Second block is a source and first block isn't
117  return false;
118  }
119  if ((a_Meta2 & 0x08) != 0)
120  {
121  // Second block is falling and the first one is neither a source nor falling
122  return false;
123  }
124 
125  // All special cases have been handled, now it's just a raw comparison:
126  return (a_Meta1 < a_Meta2);
127 }
128 
129 
130 
131 
132 
134 {
135  if (!cChunkDef::IsValidHeight(a_Pos))
136  {
137  return {};
138  }
139 
140  if (!IsAllowedBlock(m_World.GetBlock(a_Pos))) // No Fluid -> No Flowing direction :D
141  {
142  return {};
143  }
144 
145  const auto HeightFromMeta = [](NIBBLETYPE a_BlockMeta) -> NIBBLETYPE
146  {
147  // Falling water blocks are always full height (0)
148  return ((a_BlockMeta & 0x08) != 0) ? 0 : a_BlockMeta;
149  };
150 
151  auto BlockMeta = m_World.GetBlockMeta(a_Pos);
152  NIBBLETYPE CentralPoint = HeightFromMeta(BlockMeta);
153  NIBBLETYPE LevelPoint[4];
154 
155  // blocks around the checking pos
156  std::array<Vector3i, 4> Offsets
157  {
158  {
159  { 1, 0, 0 },
160  { 0, 0, 1 },
161  { 1, 0, 0 },
162  { 0, 0, 1 }
163  }
164  };
165 
166  for (size_t i = 0; i < Offsets.size(); i++)
167  {
168  if (IsAllowedBlock(m_World.GetBlock(a_Pos + Offsets[i])))
169  {
170  LevelPoint[i] = HeightFromMeta(m_World.GetBlockMeta(Offsets[i]));
171  }
172  else
173  {
174  LevelPoint[i] = CentralPoint;
175  }
176  }
177 
179 
180  // Calculate the flow direction
181 
182  Direction.x = (LevelPoint[0] - LevelPoint[2]) / 2.0f;
183  Direction.z = (LevelPoint[1] - LevelPoint[3]) / 2.0f;
184 
185  if ((BlockMeta & 0x08) != 0) // Test falling bit
186  {
187  Direction.y = -1.0f;
188  }
189 
190  return Direction;
191 }
192 
@ E_BLOCK_CARROTS
Definition: BlockType.h:156
@ E_BLOCK_REDSTONE_TORCH_ON
Definition: BlockType.h:90
@ E_BLOCK_POTATOES
Definition: BlockType.h:157
@ E_BLOCK_DEAD_BUSH
Definition: BlockType.h:42
@ E_BLOCK_REDSTONE_REPEATER_ON
Definition: BlockType.h:109
@ E_BLOCK_ACTIVATOR_RAIL
Definition: BlockType.h:174
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_BLOCK_LILY_PAD
Definition: BlockType.h:126
@ E_BLOCK_CROPS
Definition: BlockType.h:71
@ E_BLOCK_FIRE
Definition: BlockType.h:61
@ E_BLOCK_COBWEB
Definition: BlockType.h:40
@ E_BLOCK_CACTUS
Definition: BlockType.h:95
@ E_BLOCK_BEETROOTS
Definition: BlockType.h:226
@ E_BLOCK_INACTIVE_COMPARATOR
Definition: BlockType.h:165
@ E_BLOCK_TRIPWIRE
Definition: BlockType.h:147
@ E_BLOCK_POWERED_RAIL
Definition: BlockType.h:37
@ E_BLOCK_ACTIVE_COMPARATOR
Definition: BlockType.h:166
@ E_BLOCK_YELLOW_FLOWER
Definition: BlockType.h:283
@ E_BLOCK_SNOW
Definition: BlockType.h:92
@ E_BLOCK_BROWN_MUSHROOM
Definition: BlockType.h:49
@ E_BLOCK_DETECTOR_RAIL
Definition: BlockType.h:38
@ E_BLOCK_REDSTONE_WIRE
Definition: BlockType.h:65
@ E_BLOCK_BIG_FLOWER
Definition: BlockType.h:194
@ E_BLOCK_SUGARCANE
Definition: BlockType.h:97
@ E_BLOCK_RED_ROSE
Definition: BlockType.h:284
@ E_BLOCK_TORCH
Definition: BlockType.h:60
@ E_BLOCK_REDSTONE_TORCH_OFF
Definition: BlockType.h:89
@ E_BLOCK_RED_MUSHROOM
Definition: BlockType.h:50
@ E_BLOCK_REDSTONE_REPEATER_OFF
Definition: BlockType.h:108
@ E_BLOCK_RAIL
Definition: BlockType.h:79
@ E_BLOCK_TRIPWIRE_HOOK
Definition: BlockType.h:146
@ E_BLOCK_TALL_GRASS
Definition: BlockType.h:41
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
Direction
static bool IsValidHeight(Vector3i a_BlockPosition)
Validates a height-coordinate.
Definition: ChunkDef.h:185
bool IsHigherMeta(NIBBLETYPE a_Meta1, NIBBLETYPE a_Meta2)
Returns true if a_Meta1 is a higher fluid than a_Meta2.
bool IsSolidBlock(BLOCKTYPE a_BlockType)
BLOCKTYPE m_StationaryFluidBlock
static bool CanWashAway(BLOCKTYPE a_BlockType)
bool IsAllowedBlock(BLOCKTYPE a_BlockType)
cFluidSimulator(cWorld &a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid)
BLOCKTYPE m_FluidBlock
virtual Vector3f GetFlowingDirection(Vector3i a_Pos)
Returns a unit vector in the direction the fluid is flowing or a zero-vector if not flowing.
bool IsPassableForFluid(BLOCKTYPE a_BlockType)
Base class for all block-based physics simulators (such as fluid, fire, falling blocks etc....
Definition: Simulator.h:22
cWorld & m_World
Definition: Simulator.h:71
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
NIBBLETYPE GetBlockMeta(Vector3i a_BlockPos) const
Returns the block meta at the specified position.
Definition: World.h:370