From 4e5ab02a589582e2fa908909e3ee30360dd08be5 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 26 Jul 2020 14:15:00 +0100 Subject: Use SimulateChunk in redstone simulator + Improved performance, reduces bottleneck in chunkmap lookup * Stop allocating and throwing away lots of small vectors in Update/GetValidSourcePositions return values - Remove unused GetPowerLevel virtual --- .../RedstoneTorchHandler.h | 45 +++++++++++----------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator/RedstoneTorchHandler.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator/RedstoneTorchHandler.h b/src/Simulator/IncrementalRedstoneSimulator/RedstoneTorchHandler.h index 16f060939..c935e1f1b 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/RedstoneTorchHandler.h +++ b/src/Simulator/IncrementalRedstoneSimulator/RedstoneTorchHandler.h @@ -7,7 +7,7 @@ -class cRedstoneTorchHandler : public cRedstoneHandler +class cRedstoneTorchHandler final : public cRedstoneHandler { public: @@ -33,12 +33,12 @@ public: } } - virtual unsigned char GetPowerDeliveredToPosition(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override + virtual unsigned char GetPowerDeliveredToPosition(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override { if ( IsOn(a_BlockType) && (a_QueryPosition != (a_Position + GetOffsetAttachedTo(a_Position, a_Meta))) && - (cIncrementalRedstoneSimulator::IsMechanism(a_QueryBlockType) || (cBlockInfo::FullyOccupiesVoxel(a_QueryBlockType) && (a_QueryPosition == (a_Position + OffsetYP())))) + (cIncrementalRedstoneSimulator::IsMechanism(a_QueryBlockType) || (cBlockInfo::FullyOccupiesVoxel(a_QueryBlockType) && (a_QueryPosition == (a_Position + OffsetYP)))) ) { return 15; @@ -46,24 +46,19 @@ public: 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 + virtual void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override { // LOGD("Evaluating torchy the redstone torch (%i %i %i)", a_Position.x, a_Position.y, a_Position.z); - auto Data = static_cast(a_World.GetRedstoneSimulator())->GetChunkData(); - auto DelayInfo = Data->GetMechanismDelayInfo(a_Position); + auto & Data = DataForChunk(a_Chunk); + auto DelayInfo = Data.GetMechanismDelayInfo(a_Position); if (DelayInfo == nullptr) { bool ShouldBeOn = (a_PoweringData.PowerLevel == 0); if (ShouldBeOn != IsOn(a_BlockType)) { - Data->m_MechanismDelays[a_Position] = std::make_pair(1, ShouldBeOn); + Data.m_MechanismDelays[a_Position] = std::make_pair(1, ShouldBeOn); } } else @@ -72,24 +67,28 @@ public: bool ShouldPowerOn; std::tie(DelayTicks, ShouldPowerOn) = *DelayInfo; - if (DelayTicks == 0) + if (DelayTicks != 0) { - a_World.SetBlock(a_Position.x, a_Position.y, a_Position.z, ShouldPowerOn ? E_BLOCK_REDSTONE_TORCH_ON : E_BLOCK_REDSTONE_TORCH_OFF, a_Meta); - Data->m_MechanismDelays.erase(a_Position); + return; + } - cVector3iArray RelativePositions = GetRelativeAdjacents(); - RelativePositions.erase(std::remove(RelativePositions.begin(), RelativePositions.end(), GetOffsetAttachedTo(a_Position, a_Meta)), RelativePositions.end()); - return GetAdjustedRelatives(a_Position, RelativePositions); + a_Chunk.SetBlock(a_Position, ShouldPowerOn ? E_BLOCK_REDSTONE_TORCH_ON : E_BLOCK_REDSTONE_TORCH_OFF, a_Meta); + Data.m_MechanismDelays.erase(a_Position); + + for (const auto Adjacent : RelativeAdjacents) + { + if (Adjacent != GetOffsetAttachedTo(a_Position, a_Meta)) + { + UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position + Adjacent); + } } } - - return {}; } - virtual cVector3iArray GetValidSourcePositions(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override + virtual void ForValidSourcePositions(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, SourceCallback Callback) const override { - UNUSED(a_World); + UNUSED(a_Chunk); UNUSED(a_BlockType); - return { (a_Position + GetOffsetAttachedTo(a_Position, a_Meta)) }; + Callback(a_Position + GetOffsetAttachedTo(a_Position, a_Meta)); } }; -- cgit v1.2.3