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:
17  : cBlockHandler(a_BlockType)
18  {
19 
20  }
21 
22 
23 
24 
25 
26  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
27  {
28  // No pickups
29  return {};
30  }
31 
32 
33 
34 
35 
36  virtual bool DoesIgnoreBuildCollision(cChunkInterface & a_ChunkInterface, Vector3i a_Pos, cPlayer & a_Player, NIBBLETYPE a_Meta) override
37  {
38  return true;
39  }
40 
41 
42 
43 
44 
45  virtual void Check(
46  cChunkInterface & a_ChunkInterface, cBlockPluginInterface & a_PluginInterface,
47  Vector3i a_RelPos,
48  cChunk & a_Chunk
49  ) override
50  {
51  switch (m_BlockType)
52  {
54  {
55  a_Chunk.FastSetBlock(a_RelPos, E_BLOCK_LAVA, a_Chunk.GetMeta(a_RelPos));
56  break;
57  }
59  {
60  a_Chunk.FastSetBlock(a_RelPos, E_BLOCK_WATER, a_Chunk.GetMeta(a_RelPos));
61  break;
62  }
63  }
64  super::Check(a_ChunkInterface, a_PluginInterface, a_RelPos, a_Chunk);
65  }
66 
67 
68 
69 
70 
71  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
72  {
73  UNUSED(a_Meta);
75  {
76  return 12;
77  }
78  ASSERT(!"Unhandled blocktype in fluid/water handler!");
79  return 0;
80  }
81 
82  virtual bool CanSustainPlant(BLOCKTYPE a_Plant) override
83  {
84  return (
85  (a_Plant == E_BLOCK_BEETROOTS) ||
86  (a_Plant == E_BLOCK_CROPS) ||
87  (a_Plant == E_BLOCK_CARROTS) ||
88  (a_Plant == E_BLOCK_POTATOES) ||
89  (a_Plant == E_BLOCK_MELON_STEM) ||
90  (a_Plant == E_BLOCK_PUMPKIN_STEM)
91  );
92  }
93 } ;
94 
95 
96 
97 
98 
100  public cBlockFluidHandler
101 {
103 public:
104 
106  super(a_BlockType)
107  {
108  }
109 
111  virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
112  {
113  if (a_Chunk.GetWorld()->ShouldLavaSpawnFire())
114  {
115  // Try to start up to 5 fires:
116  for (int i = 0; i < 5; i++)
117  {
118  TryStartFireNear(a_RelX, a_RelY, a_RelZ, a_Chunk);
119  }
120  }
121  }
122 
124  static bool TryStartFireNear(int a_RelX, int a_RelY, int a_RelZ, cChunk & a_Chunk)
125  {
126  // Pick a block next to this lava block:
127  int rnd = a_Chunk.GetWorld()->GetTickRandomNumber(cChunkDef::NumBlocks * 8) / 7;
128  int x = (rnd % 3) - 1; // -1 .. 1
129  int y = ((rnd / 4) % 4) - 1; // -1 .. 2
130  int z = ((rnd / 16) % 3) - 1; // -1 .. 1
131 
132  // Check if it's fuel:
133  BLOCKTYPE BlockType;
134  if (
135  ((a_RelY + y < 0) || (a_RelY + y >= cChunkDef::Height)) ||
136  !a_Chunk.UnboundedRelGetBlockType(a_RelX + x, a_RelY + y, a_RelZ + z, BlockType) ||
137  !cFireSimulator::IsFuel(BlockType)
138  )
139  {
140  return false;
141  }
142 
143  // Try to set it on fire:
144  static struct
145  {
146  int x, y, z;
147  } CrossCoords[] =
148  {
149  {-1, 0, 0},
150  { 1, 0, 0},
151  { 0, -1, 0},
152  { 0, 1, 0},
153  { 0, 0, -1},
154  { 0, 0, 1},
155  } ;
156  int RelX = a_RelX + x;
157  int RelY = a_RelY + y;
158  int RelZ = a_RelZ + z;
159  for (size_t i = 0; i < ARRAYCOUNT(CrossCoords); i++)
160  {
161  if (
162  ((RelY + CrossCoords[i].y >= 0) && (RelY + CrossCoords[i].y < cChunkDef::Height)) &&
163  a_Chunk.UnboundedRelGetBlockType(RelX + CrossCoords[i].x, RelY + CrossCoords[i].y, RelZ + CrossCoords[i].z, BlockType) &&
164  (BlockType == E_BLOCK_AIR)
165  )
166  {
167  // This is an air block next to a fuel next to lava, light it up:
168  a_Chunk.UnboundedRelSetBlock(RelX + CrossCoords[i].x, RelY + CrossCoords[i].y, RelZ + CrossCoords[i].z, E_BLOCK_FIRE, 0);
169  return true;
170  }
171  } // for i - CrossCoords[]
172  return false;
173  }
174 
175  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
176  {
177  UNUSED(a_Meta);
178  return 4;
179  }
180 
181  virtual bool CanSustainPlant(BLOCKTYPE a_Plant) override
182  {
183  return false;
184  }
185 } ;
186 
187 
188 
189 
cBlockLavaHandler(BLOCKTYPE a_BlockType)
Definition: BlockFluid.h:105
cWorld * GetWorld(void) const
Definition: Chunk.h:153
cBlockFluidHandler super
Definition: BlockFluid.h:102
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: Defines.h:436
virtual void Check(cChunkInterface &ChunkInterface, cBlockPluginInterface &a_PluginInterface, Vector3i a_RelPos, cChunk &a_Chunk)
Called when one of the neighbors gets set; equivalent to MC block update.
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
virtual bool DoesIgnoreBuildCollision(cChunkInterface &a_ChunkInterface, Vector3i a_Pos, cPlayer &a_Player, NIBBLETYPE a_Meta) override
Checks if the player can build "inside" this block.
Definition: BlockFluid.h:36
static const int NumBlocks
Definition: ChunkDef.h:136
bool UnboundedRelGetBlockType(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType) const
Same as GetBlockType(), but relative coords needn&#39;t be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1018
Definition: Player.h:27
BLOCKTYPE m_BlockType
Definition: BlockHandler.h:213
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockFluid.h:71
static const int Height
Definition: ChunkDef.h:135
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity *a_BlockEntity, const cEntity *a_Digger, const cItem *a_Tool) override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
Definition: BlockFluid.h:26
int GetTickRandomNumber(int a_Range)
Returns a random number in range [0 .
Definition: World.cpp:3271
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
static bool TryStartFireNear(int a_RelX, int a_RelY, int a_RelZ, cChunk &a_Chunk)
Tries to start a fire near the lava at given coords.
Definition: BlockFluid.h:124
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:380
bool UnboundedRelSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Same as SetBlock(), but relative coords needn&#39;t be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:1124
#define ASSERT(x)
Definition: Globals.h:335
virtual void OnUpdate(cChunkInterface &cChunkInterface, cWorldInterface &a_WorldInterface, cBlockPluginInterface &a_PluginInterface, cChunk &a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
Called to tick the block.
Definition: BlockFluid.h:111
bool ShouldLavaSpawnFire(void) const
Definition: World.h:147
#define UNUSED
Definition: Globals.h:152
cBlockHandler super
Definition: BlockFluid.h:13
Byte ColourID
Definition: Globals.h:118
virtual void Check(cChunkInterface &a_ChunkInterface, cBlockPluginInterface &a_PluginInterface, Vector3i a_RelPos, cChunk &a_Chunk) override
Called when one of the neighbors gets set; equivalent to MC block update.
Definition: BlockFluid.h:45
Definition: Entity.h:73
virtual bool CanSustainPlant(BLOCKTYPE a_Plant) override
Checks whether the block has an effect on growing the plant.
Definition: BlockFluid.h:181
virtual bool CanSustainPlant(BLOCKTYPE a_Plant) override
Checks whether the block has an effect on growing the plant.
Definition: BlockFluid.h:82
cBlockFluidHandler(BLOCKTYPE a_BlockType)
Definition: BlockFluid.h:16
void FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta, bool a_SendToClients=true)
Definition: Chunk.cpp:1402
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:290
Definition: Item.h:36
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockFluid.h:175
static bool IsFuel(BLOCKTYPE a_BlockType)
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234