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>
14  public cBlockPlant<true>
15 {
17 
18 public:
19 
21  super(a_BlockType)
22  {
23  }
24 
25 
26 
27 
28 
29  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
30  {
31  auto & rand = GetRandomProvider();
32 
33  // If not fully grown, drop the "seed" of whatever is growing:
34  if (a_BlockMeta < RipeMeta)
35  {
36  switch (m_BlockType)
37  {
38  case E_BLOCK_BEETROOTS: return cItem(E_ITEM_BEETROOT_SEEDS, 1, 0); break;
39  case E_BLOCK_CROPS: return cItem(E_ITEM_SEEDS, 1, 0); break;
40  case E_BLOCK_CARROTS: return cItem(E_ITEM_CARROT, 1, 0); break;
41  case E_BLOCK_POTATOES: return cItem(E_ITEM_POTATO, 1, 0); break;
42  }
43  ASSERT(!"Unhandled block type");
44  return {};
45  }
46 
47  // Fully grown, drop the crop's produce:
48  cItems res;
49  switch (m_BlockType)
50  {
51  case E_BLOCK_BEETROOTS:
52  {
53  char SeedCount = 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2); // [1 .. 3] with high preference of 2
54  res.Add(E_ITEM_BEETROOT_SEEDS, SeedCount, 0);
55  char BeetrootCount = 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2); // [1 .. 3] with high preference of 2
56  res.Add(E_ITEM_BEETROOT, BeetrootCount, 0);
57  break;
58  }
59  case E_BLOCK_CROPS:
60  {
61  res.Add(E_ITEM_WHEAT, 1, 0);
62  res.Add(E_ITEM_SEEDS, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
63  break;
64  }
65  case E_BLOCK_CARROTS:
66  {
67  res.Add(E_ITEM_CARROT, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
68  break;
69  }
70  case E_BLOCK_POTATOES:
71  {
72  res.Add(E_ITEM_POTATO, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
73  if (rand.RandBool(0.05))
74  {
75  // With a 5% chance, drop a poisonous potato as well
76  res.emplace_back(E_ITEM_POISONOUS_POTATO, 1, 0);
77  }
78  break;
79  }
80  default:
81  {
82  ASSERT(!"Unhandled block type");
83  break;
84  }
85  } // switch (m_BlockType)
86  return res;
87  }
88 
89 
90 
91 
92 
93  virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) override
94  {
95  auto oldMeta = a_Chunk.GetMeta(a_RelPos);
96  if (oldMeta >= RipeMeta)
97  {
98  // Already ripe
99  return 0;
100  }
101  auto newMeta = std::min<int>(oldMeta + a_NumStages, RipeMeta);
102  ASSERT(newMeta > oldMeta);
103  a_Chunk.GetWorld()->SetBlock(a_Chunk.RelativeToAbsolute(a_RelPos), m_BlockType, static_cast<NIBBLETYPE>(newMeta));
104  return newMeta - oldMeta;
105  }
106 
107 
108 
109 
110 
111  virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
112  {
113  return ((a_RelY > 0) && (a_Chunk.GetBlock(a_RelX, a_RelY - 1, a_RelZ) == E_BLOCK_FARMLAND));
114  }
115 
116 
117 
118  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
119  {
120  UNUSED(a_Meta);
121  return 7;
122  }
123 } ;
124 
125 
126 
127 
cWorld * GetWorld(void) const
Definition: Chunk.h:153
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
Vector3i RelativeToAbsolute(Vector3i a_RelBlockPosition)
Converts the coord relative to this chunk into an absolute coord.
Definition: Chunk.h:569
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: BlockCrops.h:111
BLOCKTYPE m_BlockType
Definition: BlockHandler.h:213
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
cBlockCropsHandler(BLOCKTYPE a_BlockType)
Definition: BlockCrops.h:20
Definition: Chunk.h:49
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:380
void Add(const cItem &a_Item)
Definition: Item.h:254
Common class that takes care of beetroots, carrots, potatoes and wheat.
Definition: BlockCrops.h:13
#define ASSERT(x)
Definition: Globals.h:335
#define UNUSED
Definition: Globals.h:152
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: BlockCrops.h:118
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: BlockCrops.h:29
BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:177
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: BlockCrops.h:93
Byte ColourID
Definition: Globals.h:118
Base class for plants that use light values to decide whether to grow or not.
Definition: BlockPlant.h:12
Definition: Entity.h:73
void SetBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Sets the block at the specified coords to the specified value.
Definition: World.cpp:1873
Definition: Item.h:36
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234