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 
10 class cBlockCocoaPodHandler final :
11  public cBlockHandler
12 {
14 
15 public:
16 
17  using Super::Super;
18 
20  {
21  switch (a_BlockFace)
22  {
23  case BLOCK_FACE_ZM: return 0;
24  case BLOCK_FACE_XM: return 3;
25  case BLOCK_FACE_XP: return 1;
26  case BLOCK_FACE_ZP: return 2;
27  case BLOCK_FACE_NONE:
28  case BLOCK_FACE_YM:
29  case BLOCK_FACE_YP:
30  {
31  break;
32  }
33  }
34  UNREACHABLE("Unsupported block face");
35  }
36 
37 private:
38 
39  virtual bool CanBeAt(const cChunk & a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
40  {
41  // Check that we're attached to a jungle log block:
42  eBlockFace BlockFace = MetaToBlockFace(a_Meta);
43  auto LogPos = AddFaceDirection(a_Position, BlockFace, true);
45  NIBBLETYPE BlockMeta;
46  a_Chunk.UnboundedRelGetBlock(LogPos, BlockType, BlockMeta);
47  return ((BlockType == E_BLOCK_LOG) && ((BlockMeta & 0x03) == E_META_LOG_JUNGLE));
48  }
49 
50 
51 
52 
53 
54  virtual void OnUpdate(
55  cChunkInterface & a_ChunkInterface,
56  cWorldInterface & a_WorldInterface,
57  cBlockPluginInterface & a_PluginInterface,
58  cChunk & a_Chunk,
59  const Vector3i a_RelPos
60  ) const override
61  {
62  if (GetRandomProvider().RandBool(0.20))
63  {
64  Grow(a_Chunk, a_RelPos);
65  }
66  }
67 
68 
69 
70 
71 
72  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
73  {
74  // If fully grown, give 3 items, otherwise just one:
75  auto growState = a_BlockMeta >> 2;
76  return cItem(E_ITEM_DYE, ((growState >= 2) ? 3 : 1), E_META_DYE_BROWN);
77  }
78 
79 
80 
81 
82 
83  virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) const override
84  {
85  auto meta = a_Chunk.GetMeta(a_RelPos);
86  auto typeMeta = meta & 0x03;
87  auto growState = meta >> 2;
88 
89  if (growState >= 2)
90  {
91  return 0;
92  }
93  auto newState = std::min(growState + a_NumStages, 2);
94  a_Chunk.SetMeta(a_RelPos, static_cast<NIBBLETYPE>(newState << 2 | typeMeta));
95  return newState - growState;
96  }
97 
98 
99 
100 
101 
103  {
104  switch (a_Meta & 0x03)
105  {
106  case 0: return BLOCK_FACE_ZM;
107  case 1: return BLOCK_FACE_XP;
108  case 2: return BLOCK_FACE_ZP;
109  case 3: return BLOCK_FACE_XM;
110  default:
111  {
112  ASSERT(!"Bad meta");
113  return BLOCK_FACE_NONE;
114  }
115  }
116  }
117 
118 
119 
120 
121 
122  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
123  {
124  UNUSED(a_Meta);
125  return 34;
126  }
127 } ;
128 
129 
130 
131 
@ E_META_LOG_JUNGLE
Definition: BlockType.h:742
@ E_BLOCK_LOG
Definition: BlockType.h:27
@ E_ITEM_DYE
Definition: BlockType.h:396
@ E_META_DYE_BROWN
Definition: BlockType.h:1048
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
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse)
Modifies the specified coords so that they point to the block adjacent to the one specified through i...
Definition: Defines.cpp:378
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
@ BLOCK_FACE_XP
Definition: Defines.h:41
@ BLOCK_FACE_YP
Definition: Defines.h:43
@ BLOCK_FACE_YM
Definition: Defines.h:42
@ BLOCK_FACE_ZM
Definition: Defines.h:44
@ BLOCK_FACE_ZP
Definition: Defines.h:45
@ BLOCK_FACE_XM
Definition: Defines.h:40
@ BLOCK_FACE_NONE
Definition: Defines.h:39
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
#define UNREACHABLE(x)
Definition: Globals.h:288
Byte ColourID
Definition: Globals.h:162
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
BlockType
Definition: BlockTypes.h:4
static NIBBLETYPE BlockFaceToMeta(eBlockFace a_BlockFace)
Definition: BlockCocoaPod.h:19
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: BlockCocoaPod.h:54
virtual bool CanBeAt(const cChunk &a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
Checks if the block can stay at the specified relative coords in the chunk.
Definition: BlockCocoaPod.h:39
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...
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: BlockCocoaPod.h:72
static eBlockFace MetaToBlockFace(NIBBLETYPE a_Meta)
virtual int Grow(cChunk &a_Chunk, Vector3i a_RelPos, int a_NumStages=1) const override
Grows this block, if it supports growing, by the specified amount of stages (at most).
Definition: BlockCocoaPod.h:83
constexpr cBlockHandler(BLOCKTYPE a_BlockType)
Definition: BlockHandler.h:29
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
Definition: Chunk.h:36
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:279
bool UnboundedRelGetBlock(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Same as GetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:1008
void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta)
Definition: Chunk.h:286
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215