Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockCocoaPod.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "BlockHandler.h"
4 #include "../FastRandom.h"
5 
6 
7 
8 
9 
11  public cBlockHandler
12 {
14 
15 public:
17  super(a_BlockType)
18  {
19  }
20 
21 
22 
23 
24 
25  virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
26  {
27  eBlockFace BlockFace = MetaToBlockFace(a_Chunk.GetMeta(a_RelX, a_RelY, a_RelZ));
28  AddFaceDirection(a_RelX, a_RelY, a_RelZ, BlockFace, true);
29 
30  BLOCKTYPE BlockType;
31  NIBBLETYPE BlockMeta;
32  a_Chunk.UnboundedRelGetBlock(a_RelX, a_RelY, a_RelZ, BlockType, BlockMeta);
33 
34  return ((BlockType == E_BLOCK_LOG) && ((BlockMeta & 0x3) == E_META_LOG_JUNGLE));
35  }
36 
37 
38 
39 
40 
41  virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
42  {
43  if (GetRandomProvider().RandBool(0.20))
44  {
45  Grow(a_Chunk, {a_RelX, a_RelY, a_RelZ});
46  }
47  }
48 
49 
50 
51 
52 
53  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
54  {
55  // If fully grown, give 3 items, otherwise just one:
56  auto growState = a_BlockMeta >> 2;
57  return cItem(E_ITEM_DYE, ((growState >= 2) ? 3 : 1), E_META_DYE_BROWN);
58  }
59 
60 
61 
62 
63 
64  virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) override
65  {
66  auto meta = a_Chunk.GetMeta(a_RelPos);
67  auto typeMeta = meta & 0x03;
68  auto growState = meta >> 2;
69 
70  if (growState >= 3)
71  {
72  return 0;
73  }
74  auto newState = std::min(growState + a_NumStages, 3);
75  a_Chunk.SetMeta(a_RelPos, static_cast<NIBBLETYPE>(newState << 2 | typeMeta));
76  return newState - growState;
77  }
78 
79 
80 
81 
82 
84  {
85  switch (a_Meta & 0x03)
86  {
87  case 0: return BLOCK_FACE_ZM;
88  case 1: return BLOCK_FACE_XP;
89  case 2: return BLOCK_FACE_ZP;
90  case 3: return BLOCK_FACE_XM;
91  default:
92  {
93  ASSERT(!"Bad meta");
94  return BLOCK_FACE_NONE;
95  }
96  }
97  }
98 
99 
100 
101 
102 
104  {
105  switch (a_BlockFace)
106  {
107  case BLOCK_FACE_ZM: return 0;
108  case BLOCK_FACE_XM: return 3;
109  case BLOCK_FACE_XP: return 1;
110  case BLOCK_FACE_ZP: return 2;
111  case BLOCK_FACE_NONE:
112  case BLOCK_FACE_YM:
113  case BLOCK_FACE_YP:
114  {
115  ASSERT(!"Unknown face");
116  return 0;
117  }
118  }
119  UNREACHABLE("Unsupported block face");
120  }
121 
122 
123 
124 
125 
126  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
127  {
128  UNUSED(a_Meta);
129  return 34;
130  }
131 } ;
132 
133 
134 
135 
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...
virtual bool CanBeAt(cChunkInterface &a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk &a_Chunk) override
Checks if the block can stay at the specified relative coords in the chunk.
Definition: BlockCocoaPod.h:25
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
cBlockHandler(BLOCKTYPE a_BlockType)
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
virtual int Grow(cChunk &a_Chunk, Vector3i a_RelPos, int a_NumStages=1) override
Grows this block, if it supports growing, by the specified amount of stages (at most).
Definition: BlockCocoaPod.h:64
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse=false)
Definition: Defines.h:859
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:380
#define ASSERT(x)
Definition: Globals.h:335
static NIBBLETYPE BlockFaceToMeta(eBlockFace a_BlockFace)
#define UNUSED
Definition: Globals.h:152
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: BlockCocoaPod.h:53
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
bool UnboundedRelGetBlock(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Same as GetBlock(), but relative coords needn&#39;t be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:997
Byte ColourID
Definition: Globals.h:118
Definition: Entity.h:73
static eBlockFace MetaToBlockFace(NIBBLETYPE a_Meta)
Definition: BlockCocoaPod.h:83
void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta, bool a_ShouldMarkDirty=true, bool a_ShouldInformClients=true)
Definition: Chunk.h:387
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: BlockCocoaPod.h:41
Definition: Item.h:36
#define UNREACHABLE(x)
Use to mark code that should be impossible to reach.
Definition: Globals.h:344
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
cBlockCocoaPodHandler(BLOCKTYPE a_BlockType)
Definition: BlockCocoaPod.h:16