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 
18  super(a_BlockType)
19  {
20  }
21 
22 
23 
24 
25 
26  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
27  {
28  return cItem(E_ITEM_SUGARCANE, 1, 0);
29  }
30 
31 
32 
33 
34 
35  virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
36  {
37  if (a_RelY <= 0)
38  {
39  return false;
40  }
41 
42  switch (a_Chunk.GetBlock(a_RelX, a_RelY - 1, a_RelZ))
43  {
44  case E_BLOCK_DIRT:
45  case E_BLOCK_GRASS:
46  case E_BLOCK_FARMLAND:
47  case E_BLOCK_SAND:
48  {
49  static const struct
50  {
51  int x, z;
52  } Coords[] =
53  {
54  {-1, 0},
55  { 1, 0},
56  { 0, -1},
57  { 0, 1},
58  } ;
59  a_RelY -= 1;
60  for (size_t i = 0; i < ARRAYCOUNT(Coords); i++)
61  {
62  BLOCKTYPE BlockType;
63  NIBBLETYPE BlockMeta;
64  if (!a_Chunk.UnboundedRelGetBlock(a_RelX + Coords[i].x, a_RelY, a_RelZ + Coords[i].z, BlockType, BlockMeta))
65  {
66  // Too close to the edge, cannot simulate
67  return true;
68  }
69  if (IsBlockWater(BlockType) || (BlockType == E_BLOCK_FROSTED_ICE))
70  {
71  return true;
72  }
73  } // for i - Coords[]
74  // Not directly neighboring a water block
75  return false;
76  }
77  case E_BLOCK_SUGARCANE:
78  {
79  return true;
80  }
81  }
82  return false;
83  }
84 
85 
86 
87 
88 
89  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
90  {
91  UNUSED(a_Meta);
92  return 7;
93  }
94 
95 
96 
97 
98 
99  virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) override
100  {
101  // Check the total height of the sugarcane blocks here:
102  int top = a_RelPos.y + 1;
103  while (
104  (top < cChunkDef::Height) &&
105  (a_Chunk.GetBlock({a_RelPos.x, top, a_RelPos.z}) == E_BLOCK_SUGARCANE)
106  )
107  {
108  ++top;
109  }
110  int bottom = a_RelPos.y - 1;
111  while (
112  (bottom > 0) &&
113  (a_Chunk.GetBlock({a_RelPos.x, bottom, a_RelPos.z}) == E_BLOCK_SUGARCANE)
114  )
115  {
116  --bottom;
117  }
118 
119  // Grow by at most a_NumStages, but no more than max height:
120  auto toGrow = std::min(a_NumStages, a_Chunk.GetWorld()->GetMaxSugarcaneHeight() + 1 - (top - bottom));
121  Vector3i topPos(a_RelPos.x, top, a_RelPos.z);
122  for (int i = 0; i < toGrow; i++)
123  {
124  if (a_Chunk.GetBlock(topPos.addedY(i)) == E_BLOCK_AIR)
125  {
126  a_Chunk.SetBlock(topPos.addedY(i), E_BLOCK_SUGARCANE, 0);
127  }
128  else
129  {
130  return i;
131  }
132  } // for i
133  return toGrow;
134  }
135 
136 
137 
138 
139 
140 protected:
141 
142  virtual PlantAction CanGrow(cChunk & a_Chunk, Vector3i a_RelPos) override
143  {
144  // Only allow growing if there's an air block above:
145  if (((a_RelPos.y + 1) < cChunkDef::Height) && (a_Chunk.GetBlock(a_RelPos.addedY(1)) == E_BLOCK_AIR))
146  {
147  return super::CanGrow(a_Chunk, a_RelPos);
148  }
149  return paStay;
150  }
151 } ;
152 
153 
154 
155 
cWorld * GetWorld(void) const
Definition: Chunk.h:153
T x
Definition: Vector3.h:17
cBlockSugarcaneHandler(BLOCKTYPE a_BlockType)
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: Defines.h:436
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
void SetBlock(Vector3i a_RelBlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Definition: Chunk.cpp:1313
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
static const int Height
Definition: ChunkDef.h:135
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:299
int GetMaxSugarcaneHeight(void) const
Definition: World.h:1049
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...
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.
virtual PlantAction CanGrow(cChunk &a_Chunk, Vector3i a_RelPos)
Checks whether a plant can grow grow, based on what is returned from cBlockPlant::HasEnoughLight and ...
Definition: BlockPlant.h:115
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).
virtual PlantAction CanGrow(cChunk &a_Chunk, Vector3i a_RelPos) override
Checks whether a plant can grow grow, based on what is returned from cBlockPlant::HasEnoughLight and ...
#define UNUSED
Definition: Globals.h:152
bool UnboundedRelGetBlock(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Same as GetBlock(), but relative coords needn&#39;t be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:997
BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:177
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
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.
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:290
Definition: Item.h:36
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
PlantAction
The action the plant can take on an update.
Definition: BlockPlant.h:56