summaryrefslogtreecommitdiffstats
path: root/src/Blocks/BlockSugarcane.h
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2019-10-11 11:02:53 +0200
committerMattes D <github@xoft.cz>2019-10-28 10:45:43 +0100
commit61904af626b036b6e4e045ca219b2a361aa45a6e (patch)
tree60b99ab37c9ec87ca96d403b3254a4da023cf6ac /src/Blocks/BlockSugarcane.h
parentUpdate README.md (#4423) (diff)
downloadcuberite-61904af626b036b6e4e045ca219b2a361aa45a6e.tar
cuberite-61904af626b036b6e4e045ca219b2a361aa45a6e.tar.gz
cuberite-61904af626b036b6e4e045ca219b2a361aa45a6e.tar.bz2
cuberite-61904af626b036b6e4e045ca219b2a361aa45a6e.tar.lz
cuberite-61904af626b036b6e4e045ca219b2a361aa45a6e.tar.xz
cuberite-61904af626b036b6e4e045ca219b2a361aa45a6e.tar.zst
cuberite-61904af626b036b6e4e045ca219b2a361aa45a6e.zip
Diffstat (limited to 'src/Blocks/BlockSugarcane.h')
-rw-r--r--src/Blocks/BlockSugarcane.h65
1 files changed, 52 insertions, 13 deletions
diff --git a/src/Blocks/BlockSugarcane.h b/src/Blocks/BlockSugarcane.h
index 622f82138..3c929e37d 100644
--- a/src/Blocks/BlockSugarcane.h
+++ b/src/Blocks/BlockSugarcane.h
@@ -82,13 +82,9 @@ public:
return false;
}
- virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
- {
- if (CanGrow(a_Chunk, a_RelX, a_RelY, a_RelZ) == paGrowth)
- {
- a_Chunk.GetWorld()->GrowSugarcane(a_RelX + a_Chunk.GetPosX() * cChunkDef::Width, a_RelY, a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width, 1);
- }
- }
+
+
+
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
{
@@ -97,17 +93,60 @@ public:
}
-protected:
- virtual PlantAction CanGrow(cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
+
+
+ virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) override
{
- auto Action = paStay;
- if (((a_RelY + 1) < cChunkDef::Height) && (a_Chunk.GetBlock(a_RelX, a_RelY + 1, a_RelZ) == E_BLOCK_AIR))
+ // Check the total height of the sugarcane blocks here:
+ int top = a_RelPos.y + 1;
+ while (
+ (top < cChunkDef::Height) &&
+ (a_Chunk.GetBlock({a_RelPos.x, top, a_RelPos.z}) == E_BLOCK_SUGARCANE)
+ )
{
- Action = super::CanGrow(a_Chunk, a_RelX, a_RelY, a_RelZ);
+ ++top;
}
+ int bottom = a_RelPos.y - 1;
+ while (
+ (bottom > 0) &&
+ (a_Chunk.GetBlock({a_RelPos.x, bottom, a_RelPos.z}) == E_BLOCK_SUGARCANE)
+ )
+ {
+ --bottom;
+ }
+
+ // Grow by at most a_NumStages, but no more than max height:
+ auto toGrow = std::min(a_NumStages, a_Chunk.GetWorld()->GetMaxSugarcaneHeight() + 1 - (top - bottom));
+ Vector3i topPos(a_RelPos.x, top, a_RelPos.z);
+ for (int i = 0; i < toGrow; i++)
+ {
+ if (a_Chunk.GetBlock(topPos.addedY(i)) == E_BLOCK_AIR)
+ {
+ a_Chunk.SetBlock(topPos.addedY(i), E_BLOCK_SUGARCANE, 0);
+ }
+ else
+ {
+ return i;
+ }
+ } // for i
+ return toGrow;
+ }
+
+
- return Action;
+
+
+protected:
+
+ virtual PlantAction CanGrow(cChunk & a_Chunk, Vector3i a_RelPos) override
+ {
+ // Only allow growing if there's an air block above:
+ if (((a_RelPos.y + 1) < cChunkDef::Height) && (a_Chunk.GetBlock(a_RelPos.addedY(1)) == E_BLOCK_AIR))
+ {
+ return super::CanGrow(a_Chunk, a_RelPos);
+ }
+ return paStay;
}
} ;