From 04cc8aa0f57ac3fb3293740d4ffd741231d5925d Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 5 Jun 2016 16:08:47 +0100 Subject: Comparators and pistons no longer update instantly * Fixes #3168. --- .../IncrementalRedstoneSimulator/PistonHandler.h | 29 +++++++++++++++-- .../RedstoneComparatorHandler.h | 38 +++++++++++++++++----- 2 files changed, 55 insertions(+), 12 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator') 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 {}; diff --git a/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h b/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h index 95881990d..9a250a1fe 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h +++ b/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h @@ -99,22 +99,42 @@ public: virtual cVector3iArray Update(const Vector3i & a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) override { + // Note that a_PoweringData here contains the maximum *side* power level, as specified by GetValidSourcePositions // LOGD("Evaluating ALU the comparator (%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 (GetPowerLevel(a_Position, a_BlockType, a_Meta) > 0) + // Delay is used here to prevent an infinite loop (#3168) + if (DelayInfo == nullptr) { - m_World.SetBlockMeta(a_Position, a_Meta | 0x8); + auto Power = GetFrontPowerLevel(a_Position, a_BlockType, a_Meta, a_PoweringData.PowerLevel); + auto PreviousFrontPower = static_cast(m_World.GetRedstoneSimulator())->GetChunkData()->ExchangeUpdateOncePowerData(a_Position, PoweringData(a_PoweringData.PoweringBlock, Power)); + + bool ShouldBeOn = (GetPowerLevel(a_Position, a_BlockType, a_Meta) > 0); // Provide visual indication by examining *rear* power level + bool ShouldUpdate = (Power != PreviousFrontPower.PowerLevel); // "Business logic" (:P) - determine by examining *side* power levels + + if (ShouldUpdate || (ShouldBeOn != cBlockComparatorHandler::IsOn(a_Meta))) + { + Data->m_MechanismDelays[a_Position] = std::make_pair(1, ShouldBeOn); + } } else { - m_World.SetBlockMeta(a_Position, a_Meta & 0x7); - } + int DelayTicks; + bool ShouldPowerOn; + std::tie(DelayTicks, ShouldPowerOn) = *DelayInfo; - auto Power = GetFrontPowerLevel(a_Position, a_BlockType, a_Meta, a_PoweringData.PowerLevel); - auto PreviousFrontPower = static_cast(m_World.GetRedstoneSimulator())->GetChunkData()->ExchangeUpdateOncePowerData(a_Position, PoweringData(a_PoweringData.PoweringBlock, Power)); - if (Power != PreviousFrontPower.PowerLevel) - { - return GetAdjustedRelatives(a_Position, GetRelativeLaterals()); + if (DelayTicks == 0) + { + m_World.SetBlockMeta(a_Position, ShouldPowerOn ? (a_Meta | 0x8) : (a_Meta & 0x7)); + Data->m_MechanismDelays.erase(a_Position); + + // Assume that an update (to front power) is needed. + // Note: potential inconsistencies will arise as power data is updated before-delay due to limitations of the power data caching functionality (only stores one bool) + // This means that other mechanisms like wires may get our new power data before our delay has finished + // This also means that we have to manually update ourselves to be aware of any changes that happened in the previous redstone tick + return StaticAppend(GetAdjustedRelatives(a_Position, GetRelativeLaterals()), { a_Position }); + } } return {}; -- cgit v1.2.3