From 60dfaa09679219868e58a44d6d86f5d8d524ce24 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Sat, 15 Jul 2017 03:09:55 +0100 Subject: Allocate redstone component handlers upfront --- .../IncrementalRedstoneSimulator.cpp | 75 ++++++++++++++-------- 1 file changed, 49 insertions(+), 26 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp') diff --git a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp index 0ca7afbf3..966ee28ca 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp +++ b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp @@ -29,25 +29,48 @@ +const cRedstoneHandler * cIncrementalRedstoneSimulator::GetComponentHandler(BLOCKTYPE a_BlockType) +{ + struct sComponents: + public std::array, 256> + { + sComponents() + { + for (size_t i = 0; i != 256; ++i) + { + (*this)[i] = cIncrementalRedstoneSimulator::CreateComponent(static_cast(i)); + } + } + }; + + + static sComponents Components; + return Components[a_BlockType].get(); +} + + + + + -std::unique_ptr cIncrementalRedstoneSimulator::CreateComponent(cWorld & a_World, BLOCKTYPE a_BlockType, cIncrementalRedstoneSimulatorChunkData * a_Data) +std::unique_ptr cIncrementalRedstoneSimulator::CreateComponent(BLOCKTYPE a_BlockType) { switch (a_BlockType) { case E_BLOCK_ACTIVATOR_RAIL: case E_BLOCK_DETECTOR_RAIL: - case E_BLOCK_POWERED_RAIL: return cpp14::make_unique(a_World); + case E_BLOCK_POWERED_RAIL: return cpp14::make_unique(); case E_BLOCK_ACTIVE_COMPARATOR: - case E_BLOCK_INACTIVE_COMPARATOR: return cpp14::make_unique(a_World); + case E_BLOCK_INACTIVE_COMPARATOR: return cpp14::make_unique(); case E_BLOCK_DISPENSER: - case E_BLOCK_DROPPER: return cpp14::make_unique(a_World); + case E_BLOCK_DROPPER: return cpp14::make_unique(); case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE: case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: case E_BLOCK_STONE_PRESSURE_PLATE: - case E_BLOCK_WOODEN_PRESSURE_PLATE: return cpp14::make_unique(a_World); + case E_BLOCK_WOODEN_PRESSURE_PLATE: return cpp14::make_unique(); case E_BLOCK_ACACIA_FENCE_GATE: case E_BLOCK_BIRCH_FENCE_GATE: @@ -56,41 +79,41 @@ std::unique_ptr cIncrementalRedstoneSimulator::CreateComponent case E_BLOCK_IRON_TRAPDOOR: case E_BLOCK_JUNGLE_FENCE_GATE: case E_BLOCK_SPRUCE_FENCE_GATE: - case E_BLOCK_TRAPDOOR: return cpp14::make_unique(a_World); + case E_BLOCK_TRAPDOOR: return cpp14::make_unique(); case E_BLOCK_REDSTONE_LAMP_OFF: - case E_BLOCK_REDSTONE_LAMP_ON: return cpp14::make_unique(a_World); + case E_BLOCK_REDSTONE_LAMP_ON: return cpp14::make_unique(); case E_BLOCK_REDSTONE_REPEATER_OFF: - case E_BLOCK_REDSTONE_REPEATER_ON: return cpp14::make_unique(a_World); + case E_BLOCK_REDSTONE_REPEATER_ON: return cpp14::make_unique(); case E_BLOCK_REDSTONE_TORCH_OFF: - case E_BLOCK_REDSTONE_TORCH_ON: return cpp14::make_unique(a_World); + case E_BLOCK_REDSTONE_TORCH_ON: return cpp14::make_unique(); case E_BLOCK_PISTON: - case E_BLOCK_STICKY_PISTON: return cpp14::make_unique(a_World); + case E_BLOCK_STICKY_PISTON: return cpp14::make_unique(); case E_BLOCK_LEVER: case E_BLOCK_STONE_BUTTON: - case E_BLOCK_WOODEN_BUTTON: return cpp14::make_unique(a_World); - - case E_BLOCK_BLOCK_OF_REDSTONE: return cpp14::make_unique(a_World); - case E_BLOCK_COMMAND_BLOCK: return cpp14::make_unique(a_World); - case E_BLOCK_NOTE_BLOCK: return cpp14::make_unique(a_World); - case E_BLOCK_REDSTONE_WIRE: return cpp14::make_unique(a_World); - case E_BLOCK_TNT: return cpp14::make_unique(a_World); - case E_BLOCK_TRAPPED_CHEST: return cpp14::make_unique(a_World); - case E_BLOCK_TRIPWIRE_HOOK: return cpp14::make_unique(a_World); + case E_BLOCK_WOODEN_BUTTON: return cpp14::make_unique(); + + case E_BLOCK_BLOCK_OF_REDSTONE: return cpp14::make_unique(); + case E_BLOCK_COMMAND_BLOCK: return cpp14::make_unique(); + case E_BLOCK_NOTE_BLOCK: return cpp14::make_unique(); + case E_BLOCK_REDSTONE_WIRE: return cpp14::make_unique(); + case E_BLOCK_TNT: return cpp14::make_unique(); + case E_BLOCK_TRAPPED_CHEST: return cpp14::make_unique(); + case E_BLOCK_TRIPWIRE_HOOK: return cpp14::make_unique(); default: { if (cBlockDoorHandler::IsDoorBlockType(a_BlockType)) { - return cpp14::make_unique(a_World); + return cpp14::make_unique(); } if (cBlockInfo::FullyOccupiesVoxel(a_BlockType)) { - return cpp14::make_unique(a_World); + return cpp14::make_unique(); } return nullptr; } @@ -129,7 +152,7 @@ void cIncrementalRedstoneSimulator::Simulate(float a_dt) continue; } - auto CurrentHandler = cIncrementalRedstoneSimulator::CreateComponent(m_World, CurrentBlock, &m_Data); + auto CurrentHandler = GetComponentHandler(CurrentBlock); if (CurrentHandler == nullptr) // Block at CurrentPosition doesn't have a corresponding redstone handler { // Clean up cached PowerData for CurrentPosition @@ -139,7 +162,7 @@ void cIncrementalRedstoneSimulator::Simulate(float a_dt) } cRedstoneHandler::PoweringData Power; - for (const auto & Location : CurrentHandler->GetValidSourcePositions(CurrentLocation, CurrentBlock, CurrentMeta)) + for (const auto & Location : CurrentHandler->GetValidSourcePositions(m_World, CurrentLocation, CurrentBlock, CurrentMeta)) { if (!cChunk::IsValidHeight(Location.y)) { @@ -149,18 +172,18 @@ void cIncrementalRedstoneSimulator::Simulate(float a_dt) NIBBLETYPE PotentialMeta; m_World.GetBlockTypeMeta(Location.x, Location.y, Location.z, PotentialBlock, PotentialMeta); - auto PotentialSourceHandler = cIncrementalRedstoneSimulator::CreateComponent(m_World, PotentialBlock, &m_Data); + auto PotentialSourceHandler = GetComponentHandler(PotentialBlock); if (PotentialSourceHandler == nullptr) { continue; } - decltype(Power) PotentialPower(PotentialBlock, PotentialSourceHandler->GetPowerDeliveredToPosition(Location, PotentialBlock, PotentialMeta, CurrentLocation, CurrentBlock)); + decltype(Power) PotentialPower(PotentialBlock, PotentialSourceHandler->GetPowerDeliveredToPosition(m_World, Location, PotentialBlock, PotentialMeta, CurrentLocation, CurrentBlock)); Power = std::max(Power, PotentialPower); } // Inform the handler to update - cVector3iArray Updates = CurrentHandler->Update(CurrentLocation, CurrentBlock, CurrentMeta, Power); + cVector3iArray Updates = CurrentHandler->Update(m_World, CurrentLocation, CurrentBlock, CurrentMeta, Power); WorkQueue.insert(WorkQueue.end(), Updates.begin(), Updates.end()); if (IsAlwaysTicked(CurrentBlock)) -- cgit v1.2.3