Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockFluid.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
5 
6 
7 
8 
9 
11  public cBlockHandler
12 {
14 
15 public:
16 
17  using Super::Super;
18 
19 protected:
20 
21  ~cBlockFluidHandler() = default;
22 
23 private:
24 
25  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
26  {
27  // No pickups
28  return {};
29  }
30 
31 
32 
33 
34 
35  virtual bool DoesIgnoreBuildCollision(const cWorld & a_World, const cItem & a_HeldItem, const Vector3i a_Position, const NIBBLETYPE a_Meta, const eBlockFace a_ClickedBlockFace, const bool a_ClickedDirectly) const override
36  {
37  return true;
38  }
39 } ;
40 
41 
42 
43 
44 
45 class cBlockLavaHandler final :
46  public cBlockFluidHandler
47 {
49 
50 public:
51 
52  using Super::Super;
53 
54 private:
55 
56  virtual void OnUpdate(
57  cChunkInterface & a_ChunkInterface,
58  cWorldInterface & a_WorldInterface,
59  cBlockPluginInterface & a_PluginInterface,
60  cChunk & a_Chunk,
61  const Vector3i a_RelPos
62  ) const override
63  {
64  if (a_Chunk.GetWorld()->ShouldLavaSpawnFire())
65  {
66  // Try to start up to 5 fires:
67  for (int i = 0; i < 5; i++)
68  {
69  TryStartFireNear(a_RelPos, a_Chunk);
70  }
71  }
72  }
73 
74 
75 
76 
77 
79  static bool TryStartFireNear(const Vector3i a_RelPos, cChunk & a_Chunk)
80  {
81  // Pick a random block next to this lava block:
82  int rnd = a_Chunk.GetWorld()->GetTickRandomNumber(cChunkDef::NumBlocks * 8) / 7;
83  int x = (rnd % 3) - 1; // -1 .. 1
84  int y = ((rnd / 4) % 4) - 1; // -1 .. 2
85  int z = ((rnd / 16) % 3) - 1; // -1 .. 1
86  auto Pos = a_RelPos + Vector3i(x, y, z);
87 
88  // Check if it's fuel:
90  if (
92  !a_Chunk.UnboundedRelGetBlockType(Pos, BlockType) ||
94  )
95  {
96  return false;
97  }
98 
99  // Try to set it on fire:
100  static Vector3i CrossCoords[] =
101  {
102  {-1, 0, 0},
103  { 1, 0, 0},
104  { 0, -1, 0},
105  { 0, 1, 0},
106  { 0, 0, -1},
107  { 0, 0, 1},
108  } ;
109  for (size_t i = 0; i < ARRAYCOUNT(CrossCoords); i++)
110  {
111  auto NeighborPos = Pos + CrossCoords[i];
112  if (
113  cChunkDef::IsValidHeight(NeighborPos) &&
114  a_Chunk.UnboundedRelGetBlockType(NeighborPos, BlockType) &&
116  )
117  {
118  // This is an air block next to a fuel next to lava, light the fuel block up:
119  a_Chunk.UnboundedRelSetBlock(NeighborPos, E_BLOCK_FIRE, 0);
120  return true;
121  }
122  } // for i - CrossCoords[]
123  return false;
124  }
125 
126 
127 
128 
129 
130  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
131  {
132  UNUSED(a_Meta);
133  return 4;
134  }
135 
136 
137 
138 
139 
140  virtual bool CanSustainPlant(BLOCKTYPE a_Plant) const override
141  {
142  return false;
143  }
144 } ;
145 
146 
147 
148 
149 
150 class cBlockWaterHandler final :
151  public cBlockFluidHandler
152 {
153 public:
154 
155  using cBlockFluidHandler::cBlockFluidHandler;
156 
157 private:
158 
159  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
160  {
161  UNUSED(a_Meta);
163  {
164  return 12;
165  }
166  ASSERT(!"Unhandled blocktype in fluid/water handler!");
167  return 0;
168  }
169 
170  virtual bool CanSustainPlant(BLOCKTYPE a_Plant) const override
171  {
172  return (
173  (a_Plant == E_BLOCK_BEETROOTS) ||
174  (a_Plant == E_BLOCK_CROPS) ||
175  (a_Plant == E_BLOCK_CARROTS) ||
176  (a_Plant == E_BLOCK_POTATOES) ||
177  (a_Plant == E_BLOCK_MELON_STEM) ||
178  (a_Plant == E_BLOCK_PUMPKIN_STEM)
179  );
180  }
181 };
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: BlockInfo.cpp:10
@ E_BLOCK_CARROTS
Definition: BlockType.h:156
@ E_BLOCK_POTATOES
Definition: BlockType.h:157
@ E_BLOCK_MELON_STEM
Definition: BlockType.h:120
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_BLOCK_CROPS
Definition: BlockType.h:71
@ E_BLOCK_FIRE
Definition: BlockType.h:61
@ E_BLOCK_BEETROOTS
Definition: BlockType.h:226
@ E_BLOCK_PUMPKIN_STEM
Definition: BlockType.h:119
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
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:231
Byte ColourID
Definition: Globals.h:162
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
BlockType
Definition: BlockTypes.h:4
Vector3< int > Vector3i
Definition: Vector3.h:487
cBlockHandler Super
Definition: BlockFluid.h:13
~cBlockFluidHandler()=default
virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem *const a_Tool) const override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
Definition: BlockFluid.h:25
virtual bool DoesIgnoreBuildCollision(const cWorld &a_World, const cItem &a_HeldItem, const Vector3i a_Position, const NIBBLETYPE a_Meta, const eBlockFace a_ClickedBlockFace, const bool a_ClickedDirectly) const override
Checks if the player can build "inside" this block.
Definition: BlockFluid.h:35
virtual void OnUpdate(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cBlockPluginInterface &a_PluginInterface, cChunk &a_Chunk, const Vector3i a_RelPos) const override
Called when the block gets ticked either by a random tick or by a queued tick.
Definition: BlockFluid.h:56
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockFluid.h:130
virtual bool CanSustainPlant(BLOCKTYPE a_Plant) const override
Checks whether the block has an effect on growing the plant.
Definition: BlockFluid.h:140
static bool TryStartFireNear(const Vector3i a_RelPos, cChunk &a_Chunk)
Tries to start a fire near the lava at given coords.
Definition: BlockFluid.h:79
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockFluid.h:159
virtual bool CanSustainPlant(BLOCKTYPE a_Plant) const override
Checks whether the block has an effect on growing the plant.
Definition: BlockFluid.h:170
constexpr cBlockHandler(BLOCKTYPE a_BlockType)
Definition: BlockHandler.h:29
const BLOCKTYPE m_BlockType
Definition: BlockHandler.h:205
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
Definition: Chunk.h:36
bool UnboundedRelSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Same as SetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:1135
cWorld * GetWorld(void) const
Definition: Chunk.h:135
bool UnboundedRelGetBlockType(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType) const
Same as GetBlockType(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1029
static bool IsValidHeight(Vector3i a_BlockPosition)
Validates a height-coordinate.
Definition: ChunkDef.h:185
static const int NumBlocks
Definition: ChunkDef.h:126
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
static bool IsFuel(BLOCKTYPE a_BlockType)
Definition: World.h:53
bool ShouldLavaSpawnFire(void) const
Definition: World.h:129
int GetTickRandomNumber(int a_Range)
Returns a random number in range [0 .
Definition: World.cpp:2978