Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockCrops.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockPlant.h"
5 #include "../FastRandom.h"
6 
7 
8 
9 
10 
12 template <NIBBLETYPE RipeMeta>
13 class cBlockCropsHandler final :
14  public cBlockPlant<true>
15 {
17 
18 public:
19 
20  using Super::Super;
21 
22 private:
23 
25  static char CalculateSeedCount(char a_Min, char a_BaseRolls, unsigned char a_FortuneLevel)
26  {
27  std::binomial_distribution<> Binomial(a_BaseRolls + a_FortuneLevel, 0.57);
28  return static_cast<char>(a_Min + Binomial(GetRandomProvider().Engine()));
29  }
30 
31 
32 
33 
34 
35  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
36  {
37  auto & rand = GetRandomProvider();
38 
39  // If not fully grown, drop the "seed" of whatever is growing:
40  if (a_BlockMeta < RipeMeta)
41  {
42  switch (m_BlockType)
43  {
45  case E_BLOCK_CROPS: return cItem(E_ITEM_SEEDS);
46  case E_BLOCK_CARROTS: return cItem(E_ITEM_CARROT);
47  case E_BLOCK_POTATOES: return cItem(E_ITEM_POTATO);
48  }
49 
50  ASSERT(!"Unhandled block type");
51  return {};
52  }
53 
54  // Fully grown, drop the crop's produce:
55  cItems Res;
56 
57  switch (m_BlockType)
58  {
59  case E_BLOCK_BEETROOTS:
60  {
61  const auto SeedCount = CalculateSeedCount(0, 3, ToolFortuneLevel(a_Tool));
62  Res.Add(E_ITEM_BEETROOT_SEEDS, SeedCount);
63  Res.Add(E_ITEM_BEETROOT);
64  break;
65  }
66  case E_BLOCK_CROPS:
67  {
68  // https://minecraft.wiki/w/Seeds_(Wheat)
69  Res.Add(E_ITEM_WHEAT);
70  const auto SeedCount = CalculateSeedCount(1, 3, ToolFortuneLevel(a_Tool));
71  Res.Add(E_ITEM_SEEDS, SeedCount);
72  break;
73  }
74  case E_BLOCK_CARROTS:
75  {
76  // https://minecraft.wiki/w/Carrot#Breaking
77  const auto CarrotCount = CalculateSeedCount(1, 4, ToolFortuneLevel(a_Tool));
78  Res.Add(E_ITEM_CARROT, CarrotCount);
79  break;
80  }
81  case E_BLOCK_POTATOES:
82  {
83  // https://minecraft.wiki/w/Potato#Breaking
84  const auto PotatoCount = CalculateSeedCount(2, 3, ToolFortuneLevel(a_Tool));
85  Res.Add(E_ITEM_POTATO, PotatoCount);
86  if (rand.RandBool(0.02))
87  {
88  // https://minecraft.wiki/w/Poisonous_Potato#Obtaining
89  // With a 2% chance, drop a poisonous potato as well:
91  }
92  break;
93  }
94  default:
95  {
96  ASSERT(!"Unhandled block type");
97  break;
98  }
99  } // switch (m_BlockType)
100  return Res;
101  }
102 
103 
104 
105 
106 
107  virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) const override
108  {
109  const auto OldMeta = a_Chunk.GetMeta(a_RelPos);
110  const auto NewMeta = std::clamp<NIBBLETYPE>(static_cast<NIBBLETYPE>(OldMeta + a_NumStages), 0, RipeMeta);
111  a_Chunk.SetMeta(a_RelPos, NewMeta);
112  return NewMeta - OldMeta;
113  }
114 
115 
116 
117 
118 
119  virtual bool CanBeAt(const cChunk & a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
120  {
121  return (a_Position.y > 0) && (a_Chunk.GetBlock(a_Position.addedY(-1)) == E_BLOCK_FARMLAND);
122  }
123 
124 
125 
126 
127 
128  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
129  {
130  UNUSED(a_Meta);
131  return 7;
132  }
133 };
@ E_BLOCK_CARROTS
Definition: BlockType.h:156
@ E_BLOCK_POTATOES
Definition: BlockType.h:157
@ E_BLOCK_FARMLAND
Definition: BlockType.h:72
@ E_BLOCK_CROPS
Definition: BlockType.h:71
@ E_BLOCK_BEETROOTS
Definition: BlockType.h:226
@ E_ITEM_POISONOUS_POTATO
Definition: BlockType.h:440
@ E_ITEM_WHEAT
Definition: BlockType.h:340
@ E_ITEM_CARROT
Definition: BlockType.h:437
@ E_ITEM_BEETROOT_SEEDS
Definition: BlockType.h:482
@ E_ITEM_SEEDS
Definition: BlockType.h:339
@ E_ITEM_BEETROOT
Definition: BlockType.h:481
@ E_ITEM_POTATO
Definition: BlockType.h:438
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
Byte ColourID
Definition: Globals.h:162
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
Common class that takes care of beetroots, carrots, potatoes and wheat.
Definition: BlockCrops.h:15
static char CalculateSeedCount(char a_Min, char a_BaseRolls, unsigned char a_FortuneLevel)
Calculate the number of seeds to drop when the crop is broken.
Definition: BlockCrops.h:25
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: BlockCrops.h:107
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: BlockCrops.h:119
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...
Definition: BlockCrops.h:128
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: BlockCrops.h:35
static unsigned char ToolFortuneLevel(const cItem *a_Tool)
Returns the fortune level of a tool, if it is a valid tool.
const BLOCKTYPE m_BlockType
Definition: BlockHandler.h:205
Base class for plants that use light values to decide whether to grow or not.
Definition: BlockPlant.h:14
cBlockHandler Super
Definition: BlockPlant.h:15
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 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
void Add(const cItem &a_Item)
Definition: Item.h:233
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