Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockTallGrass.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
5 #include "ChunkInterface.h"
6 
7 
8 
9 
10 
13  public cBlockHandler
14 {
16 
17 public:
18 
19  using Super::Super;
20 
21 private:
22 
23  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
24  {
25  return true;
26  }
27 
28 
29 
30 
31 
32  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
33  {
34  // If using shears, drop self:
35  if ((a_Tool != nullptr) && (a_Tool->m_ItemType == E_ITEM_SHEARS))
36  {
37  return cItem(m_BlockType, 1, a_BlockMeta);
38  }
39 
40  // Drop seeds, depending on bernoulli trial result:
41  if (GetRandomProvider().RandBool(0.875)) // 87.5% chance of dropping nothing
42  {
43  return {};
44  }
45 
46  // 12.5% chance of dropping 0 or more seeds.
47  const auto DropNum = FortuneDiscreteRandom(1, 1, 2 * ToolFortuneLevel(a_Tool));
48  return cItem(E_ITEM_SEEDS, DropNum);
49  }
50 
51 
52 
53 
54 
55  virtual bool CanBeAt(const cChunk & a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
56  {
57  if (a_Position.y <= 0)
58  {
59  return false;
60  }
61 
62  BLOCKTYPE BelowBlock = a_Chunk.GetBlock(a_Position.addedY(-1));
63  return IsBlockTypeOfDirt(BelowBlock);
64  }
65 
66 
67 
68 
69 
71  virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) const override
72  {
73  if (a_RelPos.y > (cChunkDef::Height - 2))
74  {
75  return 0;
76  }
77  auto blockMeta = a_Chunk.GetMeta(a_RelPos);
78  NIBBLETYPE largeFlowerMeta;
79  switch (blockMeta)
80  {
81  case E_META_TALL_GRASS_GRASS: largeFlowerMeta = E_META_BIG_FLOWER_DOUBLE_TALL_GRASS; break;
82  case E_META_TALL_GRASS_FERN: largeFlowerMeta = E_META_BIG_FLOWER_LARGE_FERN; break;
83  default: return 0;
84  }
85  a_Chunk.SetBlock(a_RelPos, E_BLOCK_BIG_FLOWER, largeFlowerMeta);
87  return 1;
88  }
89 
90 
91 
92 
93 
94  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
95  {
96  UNUSED(a_Meta);
97  return 7;
98  }
99 } ;
100 
101 
102 
103 
bool IsBlockTypeOfDirt(BLOCKTYPE a_BlockType)
Definition: BlockInfo.cpp:86
@ E_META_TALL_GRASS_FERN
Definition: BlockType.h:932
@ E_META_TALL_GRASS_GRASS
Definition: BlockType.h:931
@ E_META_BIG_FLOWER_TOP
Definition: BlockType.h:562
@ E_META_BIG_FLOWER_DOUBLE_TALL_GRASS
Definition: BlockType.h:557
@ E_META_BIG_FLOWER_LARGE_FERN
Definition: BlockType.h:558
@ E_BLOCK_BIG_FLOWER
Definition: BlockType.h:194
@ E_ITEM_SEEDS
Definition: BlockType.h:339
@ E_ITEM_SHEARS
Definition: BlockType.h:404
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
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
Byte ColourID
Definition: Globals.h:162
#define UNUSED
Definition: Globals.h:72
static unsigned char ToolFortuneLevel(const cItem *a_Tool)
Returns the fortune level of a tool, if it is a valid tool.
static char FortuneDiscreteRandom(char a_MinDrop, char a_DefaultMax, unsigned char a_BonusMax, char a_DropCap=25)
Returns a random number of drops taking into account fortune.
constexpr cBlockHandler(BLOCKTYPE a_BlockType)
Definition: BlockHandler.h:29
const BLOCKTYPE m_BlockType
Definition: BlockHandler.h:205
Handles the grass that is 1 block tall.
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 int Grow(cChunk &a_Chunk, Vector3i a_RelPos, int a_NumStages=1) const override
Growing a tall grass produces a big flower (2-block high fern or double-tall grass).
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.
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.
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: Chunk.h:36
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:279
BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:146
void SetBlock(Vector3i a_RelBlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Definition: Chunk.cpp:1263
static const int Height
Definition: ChunkDef.h:125
Definition: Item.h:37
short m_ItemType
Definition: Item.h:163
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
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:314
T y
Definition: Vector3.h:17
Definition: World.h:53