Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockDirt.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
5 #include "../FastRandom.h"
6 #include "../Root.h"
7 #include "../Bindings/PluginManager.h"
8 
9 
10 
11 
12 
16  public cBlockHandler
17 {
19 
20 public:
21 
23  super(a_BlockType)
24  {
25  }
26 
27 
28 
29 
30 
31  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
32  {
33  if (a_BlockMeta == E_META_DIRT_COARSE)
34  {
35  // Drop the coarse block (dirt, meta 1)
37  }
38  else
39  {
41  }
42  }
43 
44 
45 
46 
47 
48  virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
49  {
51  {
52  return;
53  }
54 
55  // Make sure that there is enough light at the source block to spread
56  if (!a_Chunk.GetWorld()->IsChunkLighted(a_Chunk.GetPosX(), a_Chunk.GetPosZ()))
57  {
58  a_Chunk.GetWorld()->QueueLightChunk(a_Chunk.GetPosX(), a_Chunk.GetPosZ());
59  return;
60  }
61  else if ((a_RelY < cChunkDef::Height - 1))
62  {
63  BLOCKTYPE above = a_Chunk.GetBlock(a_RelX, a_RelY + 1, a_RelZ);
64 
65  // Grass turns back to dirt when the block above it is not transparent or water.
66  // It does not turn to dirt when a snow layer is above.
67  if ((above != E_BLOCK_SNOW) &&
68  (!cBlockInfo::IsTransparent(above) || IsBlockWater(above)))
69  {
70  a_Chunk.FastSetBlock(a_RelX, a_RelY, a_RelZ, E_BLOCK_DIRT, E_META_DIRT_NORMAL);
71  return;
72  }
73 
74  NIBBLETYPE light = std::max(a_Chunk.GetBlockLight(a_RelX, a_RelY + 1, a_RelZ), a_Chunk.GetTimeAlteredLight(a_Chunk.GetSkyLight(a_RelX, a_RelY + 1, a_RelZ)));
75  // Source block is not bright enough to spread
76  if (light < 9)
77  {
78  return;
79  }
80 
81  }
82 
83  // Grass spreads to adjacent dirt blocks:
84  auto & rand = GetRandomProvider();
85  for (int i = 0; i < 2; i++) // Pick two blocks to grow to
86  {
87  int OfsX = rand.RandInt(-1, 1);
88  int OfsY = rand.RandInt(-3, 1);
89  int OfsZ = rand.RandInt(-1, 1);
90 
91  BLOCKTYPE DestBlock;
92  NIBBLETYPE DestMeta;
93  if (!cChunkDef::IsValidHeight(a_RelY + OfsY))
94  {
95  // Y Coord out of range
96  continue;
97  }
98  Vector3i pos(a_RelX + OfsX, a_RelY + OfsY, a_RelZ + OfsZ);
99  auto chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(pos);
100  if (chunk == nullptr)
101  {
102  // Unloaded chunk
103  continue;
104  }
105  chunk->GetBlockTypeMeta(pos, DestBlock, DestMeta);
106  if ((DestBlock != E_BLOCK_DIRT) || (DestMeta != E_META_DIRT_NORMAL))
107  {
108  // Not a regular dirt block
109  continue;
110  }
111  auto abovePos = pos.addedY(1);
112  BLOCKTYPE above = chunk->GetBlock(abovePos);
113  NIBBLETYPE light = std::max(chunk->GetBlockLight(abovePos), chunk->GetTimeAlteredLight(chunk->GetSkyLight(abovePos)));
114  if ((light > 4) &&
115  cBlockInfo::IsTransparent(above) &&
116  (!IsBlockLava(above)) &&
117  (!IsBlockWaterOrIce(above))
118  )
119  {
120  auto absPos = chunk->RelativeToAbsolute(pos);
121  if (!cRoot::Get()->GetPluginManager()->CallHookBlockSpread(*chunk->GetWorld(), absPos.x, absPos.y, absPos.z, ssGrassSpread))
122  {
123  chunk->FastSetBlock(pos, E_BLOCK_GRASS, 0);
124  }
125  }
126  } // for i - repeat twice
127  }
128 
129 
130 
131 
132 
133  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
134  {
135  UNUSED(a_Meta);
136  switch (m_BlockType)
137  {
138  case E_BLOCK_DIRT: return 10;
139  case E_BLOCK_GRASS: return 1;
140  default:
141  {
142  ASSERT(!"Unhandled blocktype in dirt handler!");
143  return 0;
144  }
145  }
146  }
147 } ;
148 
149 
150 
151 
cWorld * GetWorld(void) const
Definition: Chunk.h:153
NIBBLETYPE GetSkyLight(Vector3i a_RelPos) const
Get the level of sky light illuminating the block (0 - 15) independent of daytime.
Definition: Chunk.h:419
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: Defines.h:436
cChunk * GetRelNeighborChunkAdjustCoords(Vector3i &a_RelPos) const
Returns the chunk into which the relatively-specified block belongs.
Definition: Chunk.cpp:2358
NIBBLETYPE GetTimeAlteredLight(NIBBLETYPE a_Skylight) const
Light alterations based on time.
Definition: Chunk.cpp:2450
NIBBLETYPE GetBlockLight(Vector3i a_RelPos) const
Get the level of artificial light illuminating the block (0 - 15)
Definition: Chunk.h:415
void GetBlockTypeMeta(Vector3i a_RelPos, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Definition: Chunk.cpp:2230
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
MTRand & GetRandomProvider()
Returns the current thread&#39;s random number source.
Definition: FastRandom.cpp:20
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 when the block gets ticked either by a random tick or by a queued tick.
Definition: BlockDirt.h:48
static bool IsValidHeight(int a_Height)
Validates a height-coordinate.
Definition: ChunkDef.h:212
cBlockHandler(BLOCKTYPE a_BlockType)
BLOCKTYPE m_BlockType
Definition: BlockHandler.h:213
cBlockDirtHandler(BLOCKTYPE a_BlockType)
Definition: BlockDirt.h:22
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
static const int Height
Definition: ChunkDef.h:135
Vector3< T > addedY(T a_AddY) const
Returns a copy of this vector moved by the specified amount on the y axis.
Definition: Vector3.h:299
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
int GetPosX(void) const
Definition: Chunk.h:150
bool IsChunkLighted(int a_ChunkX, int a_ChunkZ)
Definition: World.cpp:2961
#define ASSERT(x)
Definition: Globals.h:335
bool IsBlockWaterOrIce(BLOCKTYPE a_BlockType)
Definition: Defines.h:466
#define UNUSED
Definition: Globals.h:152
BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:177
static bool IsTransparent(BLOCKTYPE a_Type)
Definition: BlockInfo.h:32
Byte ColourID
Definition: Globals.h:118
int GetPosZ(void) const
Definition: Chunk.h:151
static cRoot * Get()
Definition: Root.h:51
Handler used for all types of dirt and grassblock.
Definition: BlockDirt.h:15
Definition: Entity.h:73
void QueueLightChunk(int a_ChunkX, int a_ChunkZ, std::unique_ptr< cChunkCoordCallback > a_Callback={})
Queues a chunk for lighting; a_Callback is called after the chunk is lighted.
Definition: World.cpp:2952
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: BlockDirt.h:31
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
Definition: Item.h:36
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
bool IsBlockLava(BLOCKTYPE a_BlockType)
Definition: Defines.h:475
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: BlockDirt.h:133