diff options
5 files changed, 135 insertions, 0 deletions
diff --git a/src/Blocks/BlockObserver.h b/src/Blocks/BlockObserver.h index 30c19ba2e..78a4c05b2 100644 --- a/src/Blocks/BlockObserver.h +++ b/src/Blocks/BlockObserver.h @@ -16,4 +16,27 @@ public: { } + inline static Vector3i GetObservingFaceOffset(NIBBLETYPE a_Meta) + { + return -GetSignalOutputOffset(a_Meta); + } + + inline static Vector3i GetSignalOutputOffset(NIBBLETYPE a_Meta) + { + switch (a_Meta & 0x7) + { + case 0x00: return { 0, 1, 0 }; + case 0x01: return { 0, -1, 0 }; + case 0x02: return { 0, 0, 1 }; + case 0x03: return { 0, 0, -1 }; + case 0x04: return { 1, 0, 0 }; + case 0x05: return { -1, 0, 0 }; + default: + { + LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); + ASSERT(!"Unknown metadata while determining orientation of observer!"); + return { 0, 0, 0 }; + } + } + } }; diff --git a/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt b/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt index fada3e662..0c2346322 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt +++ b/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt @@ -22,6 +22,7 @@ set (HDRS PistonHandler.h SmallGateHandler.h NoteBlockHandler.h + ObserverHandler.h TNTHandler.h TrappedChestHandler.h TripwireHookHandler.h diff --git a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp index 6be03c947..4e20c9407 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp +++ b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp @@ -18,6 +18,7 @@ #include "PistonHandler.h" #include "SmallGateHandler.h" #include "NoteBlockHandler.h" +#include "ObserverHandler.h" #include "TNTHandler.h" #include "PoweredRailHandler.h" #include "PressurePlateHandler.h" @@ -90,6 +91,8 @@ std::unique_ptr<cRedstoneHandler> cIncrementalRedstoneSimulator::CreateComponent case E_BLOCK_REDSTONE_TORCH_OFF: case E_BLOCK_REDSTONE_TORCH_ON: return cpp14::make_unique<cRedstoneTorchHandler>(); + case E_BLOCK_OBSERVER: return cpp14::make_unique<cObserverHandler>(); + case E_BLOCK_PISTON: case E_BLOCK_STICKY_PISTON: return cpp14::make_unique<cPistonHandler>(); diff --git a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.h index 4ef9801a3..8213eddae 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.h @@ -61,6 +61,7 @@ public: case E_BLOCK_JUNGLE_DOOR: case E_BLOCK_JUNGLE_FENCE_GATE: case E_BLOCK_NOTE_BLOCK: + case E_BLOCK_OBSERVER: case E_BLOCK_PISTON: case E_BLOCK_POWERED_RAIL: case E_BLOCK_REDSTONE_LAMP_OFF: @@ -127,6 +128,7 @@ public: case E_BLOCK_LEVER: case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: case E_BLOCK_NOTE_BLOCK: + case E_BLOCK_OBSERVER: case E_BLOCK_POWERED_RAIL: case E_BLOCK_REDSTONE_LAMP_OFF: case E_BLOCK_REDSTONE_LAMP_ON: diff --git a/src/Simulator/IncrementalRedstoneSimulator/ObserverHandler.h b/src/Simulator/IncrementalRedstoneSimulator/ObserverHandler.h new file mode 100644 index 000000000..cb5bc74fc --- /dev/null +++ b/src/Simulator/IncrementalRedstoneSimulator/ObserverHandler.h @@ -0,0 +1,106 @@ + +#pragma once + +#include "RedstoneHandler.h" +#include "../../Blocks/BlockObserver.h" + + + + + +class cObserverHandler : public cRedstoneHandler +{ +public: + + inline static bool IsOn(NIBBLETYPE a_Meta) + { + return (a_Meta & 0x8) == 0x8; + } + + static bool ShouldPowerOn(cWorld & a_World, const Vector3i a_Position, NIBBLETYPE a_Meta, cIncrementalRedstoneSimulatorChunkData * a_Data) + { + BLOCKTYPE BlockType; + NIBBLETYPE BlockMeta; + if (!a_World.GetBlockTypeMeta(a_Position + cBlockObserverHandler::GetObservingFaceOffset(a_Meta), BlockType, BlockMeta)) + { + return false; + } + + // Cache the last seen block type and meta in the power data for this position + auto Observed = PoweringData(BlockType, BlockMeta); + auto Previous = a_Data->ExchangeUpdateOncePowerData(a_Position, Observed); + + // Determine if to signal an update based on the block previously observed changed + return (Previous.PoweringBlock != Observed.PoweringBlock) || (Previous.PowerLevel != Observed.PowerLevel); + } + + virtual unsigned char GetPowerDeliveredToPosition(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override + { + if (IsOn(a_Meta) && (a_QueryPosition == (a_Position + cBlockObserverHandler::GetSignalOutputOffset(a_Meta)))) + { + return 15; + } + + return 0; + } + + virtual unsigned char GetPowerLevel(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override + { + return IsOn(a_BlockType) ? 15 : 0; + } + + virtual cVector3iArray Update(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override + { + // LOGD("Evaluating Lenny the observer (%i %i %i)", a_Position.x, a_Position.y, a_Position.z); + + auto Data = static_cast<cIncrementalRedstoneSimulator *>(a_World.GetRedstoneSimulator())->GetChunkData(); + auto DelayInfo = Data->GetMechanismDelayInfo(a_Position); + + if (DelayInfo == nullptr) + { + if (!ShouldPowerOn(a_World, a_Position, a_Meta, Data)) + { + return {}; + } + + // From rest, we've determined there was a block update + // Schedule power-on 1 tick in the future + Data->m_MechanismDelays[a_Position] = std::make_pair(1, true); + + return {}; + } + + int DelayTicks; + bool ShouldPowerOn; + std::tie(DelayTicks, ShouldPowerOn) = *DelayInfo; + + if (DelayTicks != 0) + { + return {}; + } + + if (ShouldPowerOn) + { + // Remain on for 1 tick before resetting + *DelayInfo = std::make_pair(1, false); + a_World.SetBlockMeta(a_Position.x, a_Position.y, a_Position.z, a_Meta | 0x8, a_Meta); + } + else + { + // We've reset. Erase delay data in preparation for detecting further updates + Data->m_MechanismDelays.erase(a_Position); + a_World.SetBlockMeta(a_Position.x, a_Position.y, a_Position.z, a_Meta & ~0x8, a_Meta); + } + + return { a_Position + cBlockObserverHandler::GetSignalOutputOffset(a_Meta) }; + } + + virtual cVector3iArray GetValidSourcePositions(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override + { + UNUSED(a_World); + UNUSED(a_Position); + UNUSED(a_BlockType); + UNUSED(a_BlockType); + return {}; + } +}; |