diff options
author | Mattes D <github@xoft.cz> | 2019-10-11 11:02:53 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2019-10-28 10:45:43 +0100 |
commit | 61904af626b036b6e4e045ca219b2a361aa45a6e (patch) | |
tree | 60b99ab37c9ec87ca96d403b3254a4da023cf6ac /src/Blocks/BlockPlant.h | |
parent | Update README.md (#4423) (diff) | |
download | cuberite-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/BlockPlant.h')
-rw-r--r-- | src/Blocks/BlockPlant.h | 61 |
1 files changed, 44 insertions, 17 deletions
diff --git a/src/Blocks/BlockPlant.h b/src/Blocks/BlockPlant.h index 02092fc38..0e249bbde 100644 --- a/src/Blocks/BlockPlant.h +++ b/src/Blocks/BlockPlant.h @@ -1,19 +1,20 @@ - #pragma once -#include "BlockHandler.h" +#include "BlockHandler.h" -/** Base class for plants that use light values to decide whether to grow or not. */ +/** Base class for plants that use light values to decide whether to grow or not. +On block update, the plant decides whether to grow, die or stay as-is, based on the CanGrow() overridable method result. */ template <bool NeedsLightToGrow> class cBlockPlant: public cBlockHandler { using super = cBlockHandler; + public: cBlockPlant(BLOCKTYPE a_BlockType): @@ -22,6 +23,33 @@ public: } + + + + virtual void OnUpdate(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override + { + Vector3i relPos(a_RelX, a_RelY, a_RelZ); + auto action = CanGrow(a_Chunk, relPos); + switch (action) + { + case paGrowth: + { + Grow(a_Chunk, relPos); + break; + } + case paDeath: + { + a_ChunkInterface.DigBlock(a_WorldInterface, a_Chunk.RelativeToAbsolute(relPos)); + break; + } + case paStay: break; // do nothing + } + } + + + + + protected: /** The action the plant can take on an update. */ @@ -40,9 +68,8 @@ protected: If the plant doesn't require light to grow, then it returns paGrowth. If the plant requires light to grow and there is enough light, it returns paGrowth. If the plant requires light to grow and there isn't enough light, it returns paStay. - If the plant requires light to grow and there is too little light, it returns paDeath. - */ - PlantAction HasEnoughLight(cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) + If the plant requires light to grow and there is too little light, it returns paDeath. */ + PlantAction HasEnoughLight(cChunk & a_Chunk, Vector3i a_RelPos) { // If the plant requires light to grow, check to see if there is enough light // Otherwise, return true @@ -50,8 +77,8 @@ protected: { return paGrowth; } - NIBBLETYPE Blocklight = a_Chunk.GetBlockLight(a_RelX, a_RelY, a_RelZ); - NIBBLETYPE SkyLight = a_Chunk.GetSkyLight (a_RelX, a_RelY, a_RelZ); + NIBBLETYPE Blocklight = a_Chunk.GetBlockLight(a_RelPos); + NIBBLETYPE SkyLight = a_Chunk.GetSkyLight (a_RelPos); NIBBLETYPE Light = a_Chunk.GetTimeAlteredLight(SkyLight); // If the amount of light provided by blocks is greater than the sky light, use it instead @@ -85,27 +112,27 @@ protected: paDeath is returned when there isn't enough light for the plant to survive. Plants that don't require light will never have a paDeath returned */ - virtual PlantAction CanGrow(cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) + virtual PlantAction CanGrow(cChunk & a_Chunk, Vector3i a_RelPos) { // Plant can grow if it has the required amount of light, and it passes a random chance based on surrounding blocks - PlantAction Action = HasEnoughLight(a_Chunk, a_RelX, a_RelY, a_RelZ); - if ((Action == paGrowth) && !GetRandomProvider().RandBool(1.0 / GetGrowthChance(a_Chunk, a_RelX, a_RelY, a_RelZ))) + auto action = HasEnoughLight(a_Chunk, a_RelPos); + if ((action == paGrowth) && !GetRandomProvider().RandBool(1.0 / GetGrowthChance(a_Chunk, a_RelPos))) { - Action = paStay; + action = paStay; } - return Action; + return action; } - /** Generates a int value between 4 and 25 based on surrounding blocks that affect how quickly the plant grows. + /** Generates an int value between 4 and 25 based on surrounding blocks that affect how quickly the plant grows. The higher the value, the less likely the plant is to grow */ - virtual int GetGrowthChance(cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) + virtual int GetGrowthChance(cChunk & a_Chunk, Vector3i a_RelPos) { float Chance = 1.0f; - a_RelY -= 1; + a_RelPos.y -= 1; for (int x = -1; x < 2; ++x) { for (int z = -1; z < 2; ++z) @@ -115,7 +142,7 @@ protected: NIBBLETYPE Meta; // If the chunk we are trying to get the block information from is loaded - if (a_Chunk.UnboundedRelGetBlock(a_RelX + x, a_RelY, a_RelZ + z, Block, Meta)) + if (a_Chunk.UnboundedRelGetBlock(a_RelPos + Vector3i(x, 0, z), Block, Meta)) { cBlockHandler * Handler = BlockHandler(Block); |