From 90d552a702a435f644c29f75fc2a85c0d71ee510 Mon Sep 17 00:00:00 2001 From: bionext03 Date: Fri, 27 Jul 2018 17:01:53 +0800 Subject: Add new flow direction calculating algorithm (#4160) --- src/Simulator/FluidSimulator.cpp | 103 ++++++++++++--------------------------- src/Simulator/FluidSimulator.h | 8 +-- 2 files changed, 36 insertions(+), 75 deletions(-) (limited to 'src/Simulator') diff --git a/src/Simulator/FluidSimulator.cpp b/src/Simulator/FluidSimulator.cpp index 10f2ed544..230517b19 100644 --- a/src/Simulator/FluidSimulator.cpp +++ b/src/Simulator/FluidSimulator.cpp @@ -128,100 +128,61 @@ bool cFluidSimulator::IsHigherMeta(NIBBLETYPE a_Meta1, NIBBLETYPE a_Meta2) -// TODO Not working very well yet :s -Direction cFluidSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over) +Vector3f cFluidSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z) { if (!cChunkDef::IsValidHeight(a_Y)) { - return NONE; + return {}; } - BLOCKTYPE BlockID = m_World.GetBlock(a_X, a_Y, a_Z); - if (!IsAllowedBlock(BlockID)) // No Fluid -> No Flowing direction :D - { - return NONE; - } - - /* - Disabled because of causing problems and being useless atm - char BlockBelow = m_World.GetBlock(a_X, a_Y - 1, a_Z); // If there is nothing or fluid below it -> dominating flow is down :D - if ((BlockBelow == E_BLOCK_AIR) || IsAllowedBlock(BlockBelow)) - { - return Y_MINUS; - } - */ - - NIBBLETYPE LowestPoint = m_World.GetBlockMeta(a_X, a_Y, a_Z); // Current Block Meta so only lower points will be counted - int X = 0, Z = 0; // Lowest Pos will be stored here - if (IsAllowedBlock(m_World.GetBlock(a_X, a_Y + 1, a_Z)) && a_Over) // check for upper block to flow because this also affects the flowing direction + if (!IsAllowedBlock(m_World.GetBlock(a_X, a_Y, a_Z))) // No Fluid -> No Flowing direction :D { - return GetFlowingDirection(a_X, a_Y + 1, a_Z, false); + return {}; } - std::vector< Vector3i * > Points; + const auto HeightFromMeta = [](NIBBLETYPE a_BlockMeta) -> NIBBLETYPE + { + // Falling water blocks are always full height (0) + return ((a_BlockMeta & 0x08) != 0) ? 0 : a_BlockMeta; + }; - Points.reserve(4); // Already allocate 4 places :D + auto BlockMeta = m_World.GetBlockMeta(a_X, a_Y, a_Z); + NIBBLETYPE CentralPoint = HeightFromMeta(BlockMeta); + NIBBLETYPE LevelPoint[4]; - // add blocks around the checking pos - Points.push_back(new Vector3i(a_X - 1, a_Y, a_Z)); - Points.push_back(new Vector3i(a_X + 1, a_Y, a_Z)); - Points.push_back(new Vector3i(a_X, a_Y, a_Z + 1)); - Points.push_back(new Vector3i(a_X, a_Y, a_Z - 1)); + // blocks around the checking pos + Vector3i Points[] + { + { a_X + 1, a_Y, a_Z }, + { a_X, a_Y, a_Z + 1 }, + { a_X - 1, a_Y, a_Z }, + { a_X, a_Y, a_Z - 1 } + }; - for (auto itr = Points.cbegin(), end = Points.cend(); itr != end; ++itr) + for (size_t i = 0; i < ARRAYCOUNT(LevelPoint); i++) { - Vector3i * Pos = (*itr); - auto PosBlockID = m_World.GetBlock(Pos->x, Pos->y, Pos->z); - if (IsAllowedBlock(PosBlockID)) + if (IsAllowedBlock(m_World.GetBlock(Points[i]))) { - NIBBLETYPE Meta = m_World.GetBlockMeta(Pos->x, Pos->y, Pos->z); - - if (Meta > LowestPoint) - { - LowestPoint = Meta; - X = Pos->x; - Z = Pos->z; - } + LevelPoint[i] = HeightFromMeta(m_World.GetBlockMeta(Points[i])); } - else if (PosBlockID == E_BLOCK_AIR) + else { - LowestPoint = 9; // This always dominates - X = Pos->x; - Z = Pos->z; - + LevelPoint[i] = CentralPoint; } - delete Pos; - Pos = nullptr; } - if (LowestPoint == m_World.GetBlockMeta(a_X, a_Y, a_Z)) - { - return NONE; - } + Vector3f Direction; - if (a_X - X > 0) - { - return X_MINUS; - } - - if (a_X - X < 0) - { - return X_PLUS; - } + // Calculate the flow direction - if (a_Z - Z > 0) - { - return Z_MINUS; - } + Direction.x = (LevelPoint[0] - LevelPoint[2]) / 2.0f; + Direction.z = (LevelPoint[1] - LevelPoint[3]) / 2.0f; - if (a_Z - Z < 0) + if ((BlockMeta & 0x08) != 0) // Test falling bit { - return Z_PLUS; + Direction.y = -1.0f; } - return NONE; + return Direction; } - - - diff --git a/src/Simulator/FluidSimulator.h b/src/Simulator/FluidSimulator.h index 86bcfb116..7e79c2751 100644 --- a/src/Simulator/FluidSimulator.h +++ b/src/Simulator/FluidSimulator.h @@ -28,7 +28,7 @@ class cFluidSimulatorData { public: virtual ~cFluidSimulatorData() {} -} ; +}; @@ -45,8 +45,8 @@ public: // cSimulator overrides: virtual bool IsAllowedBlock(BLOCKTYPE a_BlockType) override; - /** Gets the flowing direction. If a_Over is true also the block over the current block affects the direction (standard) */ - virtual Direction GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over = true); + /** Returns a unit vector in the direction the fluid is flowing or a zero-vector if not flowing. */ + virtual Vector3f GetFlowingDirection(int a_X, int a_Y, int a_Z); /** Creates a ChunkData object for the simulator to use. The simulator returns the correct object type. */ virtual cFluidSimulatorData * CreateChunkData(void) { return nullptr; } @@ -66,7 +66,7 @@ public: protected: BLOCKTYPE m_FluidBlock; // The fluid block type that needs simulating BLOCKTYPE m_StationaryFluidBlock; // The fluid block type that indicates no simulation is needed -} ; +}; -- cgit v1.2.3