From c1ea5f982dc4665c5683dbbe7a3ffe280f6a09cf Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 17 Apr 2020 09:40:26 +0100 Subject: [PATCH] Re-implement repeater locking --- src/BlockType.h | 7 ++ src/Blocks/BlockRedstoneRepeater.h | 34 +++---- .../RedstoneRepeaterHandler.h | 90 +++++++++++++++++-- 3 files changed, 111 insertions(+), 20 deletions(-) diff --git a/src/BlockType.h b/src/BlockType.h index 9f8c8fb55..50422b346 100644 --- a/src/BlockType.h +++ b/src/BlockType.h @@ -778,6 +778,13 @@ enum ENUM_BLOCK_META : NIBBLETYPE E_META_RED_SANDSTONE_ORNAMENT = 1, E_META_RED_SANDSTONE_SMOOTH = 2, + // E_BLOCK_REDSTONE_REPEATER_ON / E_BLOCK_REDSTONE_REPEATER_OFF metas: + E_META_REDSTONE_REPEATER_FACING_ZM = 0, + E_META_REDSTONE_REPEATER_FACING_XP = 1, + E_META_REDSTONE_REPEATER_FACING_ZP = 2, + E_META_REDSTONE_REPEATER_FACING_XM = 3, + E_META_REDSTONE_REPEATER_FACING_MASK = 3, + // E_BLOCK_SAND metas: E_META_SAND_NORMAL = 0, E_META_SAND_RED = 1, diff --git a/src/Blocks/BlockRedstoneRepeater.h b/src/Blocks/BlockRedstoneRepeater.h index 89cba2d35..25aa9b795 100644 --- a/src/Blocks/BlockRedstoneRepeater.h +++ b/src/Blocks/BlockRedstoneRepeater.h @@ -77,38 +77,42 @@ public: return 11; } - - inline static Vector3i GetRearCoordinateOffset(NIBBLETYPE a_Meta) + inline static Vector3i GetLeftCoordinateOffset(NIBBLETYPE a_Meta) { - switch (a_Meta & 0x3) // We only want the direction (bottom) bits + switch (a_Meta & E_META_REDSTONE_REPEATER_FACING_MASK) // We only want the direction (bottom) bits { - case 0x0: return {0, 0, 1}; - case 0x1: return {-1, 0, 0}; - case 0x2: return {0, 0, -1}; - case 0x3: return {1, 0, 0}; + case E_META_REDSTONE_REPEATER_FACING_ZM: return { -1, 0, 0 }; + case E_META_REDSTONE_REPEATER_FACING_XP: return { 0, 0, -1 }; + case E_META_REDSTONE_REPEATER_FACING_ZP: return { 1, 0, 0 }; + case E_META_REDSTONE_REPEATER_FACING_XM: return { 0, 0, 1 }; + default: { LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); ASSERT(!"Unknown metadata while determining orientation of repeater!"); - return {0, 0, 0}; + return { 0, 0, 0 }; } } } - inline static Vector3i GetFrontCoordinateOffset(NIBBLETYPE a_Meta) { - switch (a_Meta & 0x3) // We only want the direction (bottom) bits + return -GetRearCoordinateOffset(a_Meta); + } + + inline static Vector3i GetRearCoordinateOffset(NIBBLETYPE a_Meta) + { + switch (a_Meta & E_META_REDSTONE_REPEATER_FACING_MASK) // We only want the direction (bottom) bits { - case 0x0: return {0, 0, -1}; - case 0x1: return {1, 0, 0}; - case 0x2: return {0, 0, 1}; - case 0x3: return {-1, 0, 0}; + case E_META_REDSTONE_REPEATER_FACING_ZM: return { 0, 0, 1 }; + case E_META_REDSTONE_REPEATER_FACING_XP: return { -1, 0, 0 }; + case E_META_REDSTONE_REPEATER_FACING_ZP: return { 0, 0, -1 }; + case E_META_REDSTONE_REPEATER_FACING_XM: return { 1, 0, 0 }; default: { LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); ASSERT(!"Unknown metadata while determining orientation of repeater!"); - return {0, 0, 0}; + return { 0, 0, 0 }; } } } diff --git a/src/Simulator/IncrementalRedstoneSimulator/RedstoneRepeaterHandler.h b/src/Simulator/IncrementalRedstoneSimulator/RedstoneRepeaterHandler.h index 9a0e26100..0cd65be53 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/RedstoneRepeaterHandler.h +++ b/src/Simulator/IncrementalRedstoneSimulator/RedstoneRepeaterHandler.h @@ -15,11 +15,6 @@ class cRedstoneRepeaterHandler: public: - inline static bool IsOn(BLOCKTYPE a_Block) - { - return (a_Block == E_BLOCK_REDSTONE_REPEATER_ON); - } - virtual unsigned char GetPowerDeliveredToPosition(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override { return ( @@ -42,6 +37,17 @@ public: auto Data = static_cast(a_World.GetRedstoneSimulator())->GetChunkData(); auto DelayInfo = Data->GetMechanismDelayInfo(a_Position); + // If the repeater is locked by another, ignore and forget all power changes: + if (IsLocked(a_World, a_Position, a_Meta)) + { + if (DelayInfo != nullptr) + { + Data->m_MechanismDelays.erase(a_Position); + } + + return {}; + } + if (DelayInfo == nullptr) { bool ShouldBeOn = (a_PoweringData.PowerLevel != 0); @@ -71,4 +77,78 @@ public: { return { cBlockRedstoneRepeaterHandler::GetRearCoordinateOffset(a_Meta) + a_Position }; } + +private: + + inline static bool IsOn(BLOCKTYPE a_Block) + { + return (a_Block == E_BLOCK_REDSTONE_REPEATER_ON); + } + + /** Returns a pair with first element indicating if the block at the given position is an activated repeater. + If it is activated, the second element is the repeater metadata. */ + static std::pair IsOnRepeater(cWorld & a_World, const Vector3i a_Position) + { + BLOCKTYPE Type; + NIBBLETYPE Meta; + + if (!a_World.GetBlockTypeMeta(a_Position, Type, Meta)) + { + return std::make_pair(false, 0); + } + + return std::make_pair(IsOn(Type), Meta); + } + + /** Determine if a repeater is locked. + A locked repeater is one with another powered repeater facing them, to their immediate left or right sides. + "Left" is relative to the direction the repeater output faces, naturally. */ + inline static bool IsLocked(cWorld & a_World, const Vector3i a_Position, const NIBBLETYPE a_Meta) + { + // The left hand side offset. Will be negated to get the rhs offset + const auto LhsOffset = cBlockRedstoneRepeaterHandler::GetLeftCoordinateOffset(a_Meta); + + // Test the block to the left of us + const auto Lhs = IsOnRepeater(a_World, LhsOffset + a_Position); + if (Lhs.first && DoesLhsLockMe(Lhs.second, a_Meta)) + { + return true; + } + + // Test the right side, flipping the argument order to DoesLhsLockMe + const auto Rhs = IsOnRepeater(a_World, -LhsOffset + a_Position); + return Rhs.first && DoesLhsLockMe(a_Meta, Rhs.second); + } + + /** Determine, from the metadata of a repeater on our left side, if they lock us. + To test a repeater on our right, simply invert the order of arguments provided. + "Left" is relative to the direction the repeater output faces, naturally. */ + static bool DoesLhsLockMe(NIBBLETYPE a_MetaLhs, NIBBLETYPE a_MyMeta) + { + // Get the direction bits + a_MetaLhs &= E_META_REDSTONE_REPEATER_FACING_MASK; + a_MyMeta &= E_META_REDSTONE_REPEATER_FACING_MASK; + + /* + Check for a valid locking configuration, where they are perpendicular and one snuggles into the other. + + Order of comparisons: + XP >^ ZM + ZP |_ XP + XM <| ZP + ZP ^< xM + + Key: + ^ Facing up + _ Facing right + | Facing down + < Facing left + */ + return + ((a_MetaLhs == E_META_REDSTONE_REPEATER_FACING_XP) && (a_MyMeta == E_META_REDSTONE_REPEATER_FACING_ZM)) || + ((a_MetaLhs == E_META_REDSTONE_REPEATER_FACING_ZP) && (a_MyMeta == E_META_REDSTONE_REPEATER_FACING_XP)) || + ((a_MetaLhs == E_META_REDSTONE_REPEATER_FACING_XM) && (a_MyMeta == E_META_REDSTONE_REPEATER_FACING_ZP)) || + ((a_MetaLhs == E_META_REDSTONE_REPEATER_FACING_ZM) && (a_MyMeta == E_META_REDSTONE_REPEATER_FACING_XM)) + ; + } };