Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockSugarCane.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockPlant.h"
5 
6 
7 
8 
9 
11  public cBlockPlant<false>
12 {
14 
15 public:
16 
17  using Super::Super;
18 
19 private:
20 
21  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
22  {
23  return cItem(E_ITEM_SUGARCANE, 1, 0);
24  }
25 
26 
27 
28 
29 
30  virtual bool CanBeAt(const cChunk & a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
31  {
32  if (a_Position.y <= 0)
33  {
34  return false;
35  }
36 
37  switch (a_Chunk.GetBlock(a_Position.addedY(-1)))
38  {
39  case E_BLOCK_DIRT:
40  case E_BLOCK_GRASS:
41  case E_BLOCK_FARMLAND:
42  case E_BLOCK_SAND:
43  {
44  static const Vector3i Coords[] =
45  {
46  {-1, -1, 0},
47  { 1, -1, 0},
48  { 0, -1, -1},
49  { 0, -1, 1},
50  } ;
51  for (size_t i = 0; i < ARRAYCOUNT(Coords); i++)
52  {
54  NIBBLETYPE BlockMeta;
55  if (!a_Chunk.UnboundedRelGetBlock(a_Position + Coords[i], BlockType, BlockMeta))
56  {
57  // Too close to the edge, cannot simulate
58  return true;
59  }
61  {
62  return true;
63  }
64  } // for i - Coords[]
65  // Not directly neighboring a water block
66  return false;
67  }
68  case E_BLOCK_SUGARCANE:
69  {
70  return true;
71  }
72  }
73  return false;
74  }
75 
76 
77 
78 
79 
80  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
81  {
82  UNUSED(a_Meta);
83  return 7;
84  }
85 
86 
87 
88 
89 
90  virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) const override
91  {
92  // Check the total height of the sugarcane blocks here:
93  int top = a_RelPos.y + 1;
94  while (
95  (top < cChunkDef::Height) &&
96  (a_Chunk.GetBlock({a_RelPos.x, top, a_RelPos.z}) == E_BLOCK_SUGARCANE)
97  )
98  {
99  ++top;
100  }
101  int bottom = a_RelPos.y - 1;
102  while (
103  (bottom > 0) &&
104  (a_Chunk.GetBlock({a_RelPos.x, bottom, a_RelPos.z}) == E_BLOCK_SUGARCANE)
105  )
106  {
107  --bottom;
108  }
109 
110  // Grow by at most a_NumStages, but no more than max height:
111  auto toGrow = std::min(a_NumStages, a_Chunk.GetWorld()->GetMaxSugarcaneHeight() + 1 - (top - bottom));
112  Vector3i topPos(a_RelPos.x, top, a_RelPos.z);
113  for (int i = 0; i < toGrow; i++)
114  {
115  if (a_Chunk.GetBlock(topPos.addedY(i)) == E_BLOCK_AIR)
116  {
117  a_Chunk.SetBlock(topPos.addedY(i), E_BLOCK_SUGARCANE, 0);
118  }
119  else
120  {
121  return i;
122  }
123  } // for i
124  return toGrow;
125  }
126 
127  virtual PlantAction CanGrow(cChunk & a_Chunk, Vector3i a_RelPos) const override
128  {
129  // Only allow growing if there's an air block above:
130  if (((a_RelPos.y + 1) < cChunkDef::Height) && (a_Chunk.GetBlock(a_RelPos.addedY(1)) == E_BLOCK_AIR))
131  {
132  return Super::CanGrow(a_Chunk, a_RelPos);
133  }
134  return paStay;
135  }
136 } ;
137 
138 
139 
140 
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: BlockInfo.cpp:10
@ E_BLOCK_FARMLAND
Definition: BlockType.h:72
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_BLOCK_GRASS
Definition: BlockType.h:12
@ E_BLOCK_SUGARCANE
Definition: BlockType.h:97
@ E_BLOCK_DIRT
Definition: BlockType.h:13
@ E_BLOCK_SAND
Definition: BlockType.h:22
@ E_BLOCK_FROSTED_ICE
Definition: BlockType.h:231
@ E_ITEM_SUGARCANE
Definition: BlockType.h:382
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
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:231
Byte ColourID
Definition: Globals.h:162
#define UNUSED
Definition: Globals.h:72
BlockType
Definition: BlockTypes.h:4
Base class for plants that use light values to decide whether to grow or not.
Definition: BlockPlant.h:14
virtual PlantAction CanGrow(cChunk &a_Chunk, Vector3i a_RelPos) const
Checks whether a plant can grow grow, based on what is returned from cBlockPlant::HasEnoughLight and ...
Definition: BlockPlant.h:81
cBlockHandler Super
Definition: BlockPlant.h:15
PlantAction
The action the plant can take on an update.
Definition: BlockPlant.h:27
virtual PlantAction CanGrow(cChunk &a_Chunk, Vector3i a_RelPos) const override
Checks whether a plant can grow grow, based on what is returned from cBlockPlant::HasEnoughLight and ...
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 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.
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).
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: Chunk.h:36
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
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
cWorld * GetWorld(void) const
Definition: Chunk.h:135
static const int Height
Definition: ChunkDef.h:125
Definition: Item.h:37
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 x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
int GetMaxSugarcaneHeight(void) const
Definition: World.h:858