From d46f7ce2c880026ff4663d89d3e76117219b87a2 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 31 Jul 2016 21:54:35 +0100 Subject: Redstone fixes (#3285) * Comparators and pistons no longer update instantly * Fixes #3168. * Consolidated comparator code * As a result, fixed an issue where GetPowerLevel didn't consider block entities behind it (only GetFrontPowerLevel did) --- .../IncrementalRedstoneSimulator/PistonHandler.h | 29 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator/PistonHandler.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator/PistonHandler.h b/src/Simulator/IncrementalRedstoneSimulator/PistonHandler.h index 1ee68e521..f82ebdf94 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/PistonHandler.h +++ b/src/Simulator/IncrementalRedstoneSimulator/PistonHandler.h @@ -39,14 +39,37 @@ public: virtual cVector3iArray Update(const Vector3i & a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) override { // LOGD("Evaluating pisty the piston (%d %d %d)", a_Position.x, a_Position.y, a_Position.z); + auto Data = static_cast(m_World.GetRedstoneSimulator())->GetChunkData(); + auto DelayInfo = Data->GetMechanismDelayInfo(a_Position); - if (a_PoweringData.PowerLevel > 0) + // Delay is used here to prevent an infinite loop (#3168) + if (DelayInfo == nullptr) { - cBlockPistonHandler::ExtendPiston(a_Position, &m_World); + bool ShouldBeExtended = (a_PoweringData.PowerLevel != 0); + if (ShouldBeExtended != cBlockPistonHandler::IsExtended(a_Meta)) + { + Data->m_MechanismDelays[a_Position] = std::make_pair(1, ShouldBeExtended); + } } else { - cBlockPistonHandler::RetractPiston(a_Position, &m_World); + int DelayTicks; + bool ShouldBeExtended; + std::tie(DelayTicks, ShouldBeExtended) = *DelayInfo; + + if (DelayTicks == 0) + { + if (ShouldBeExtended) + { + cBlockPistonHandler::ExtendPiston(a_Position, &m_World); + } + else + { + cBlockPistonHandler::RetractPiston(a_Position, &m_World); + } + + Data->m_MechanismDelays.erase(a_Position); + } } return {}; -- cgit v1.2.3