2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "IncrementalRedstoneSimulator.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../../Chunk.h"
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
#include "CommandBlockHandler.h"
|
|
|
|
#include "DoorHandler.h"
|
|
|
|
#include "RedstoneTorchHandler.h"
|
|
|
|
#include "RedstoneWireHandler.h"
|
|
|
|
#include "RedstoneRepeaterHandler.h"
|
|
|
|
#include "RedstoneToggleHandler.h"
|
|
|
|
#include "SolidBlockHandler.h"
|
|
|
|
#include "RedstoneLampHandler.h"
|
|
|
|
#include "RedstoneBlockHandler.h"
|
|
|
|
#include "PistonHandler.h"
|
|
|
|
#include "SmallGateHandler.h"
|
|
|
|
#include "NoteBlockHandler.h"
|
2020-04-16 17:27:04 -04:00
|
|
|
#include "ObserverHandler.h"
|
2015-06-26 18:24:51 -04:00
|
|
|
#include "TNTHandler.h"
|
|
|
|
#include "PoweredRailHandler.h"
|
|
|
|
#include "PressurePlateHandler.h"
|
|
|
|
#include "TripwireHookHandler.h"
|
|
|
|
#include "DropSpenserHandler.h"
|
|
|
|
#include "RedstoneComparatorHandler.h"
|
|
|
|
#include "TrappedChestHandler.h"
|
2020-03-27 08:03:28 -04:00
|
|
|
#include "HopperHandler.h"
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
const cRedstoneHandler * cIncrementalRedstoneSimulator::GetComponentHandler(BLOCKTYPE a_BlockType)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
struct sComponents :
|
2017-07-14 22:09:55 -04:00
|
|
|
public std::array<std::unique_ptr<cRedstoneHandler>, 256>
|
|
|
|
{
|
|
|
|
sComponents()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i != 256; ++i)
|
|
|
|
{
|
|
|
|
(*this)[i] = cIncrementalRedstoneSimulator::CreateComponent(static_cast<BLOCKTYPE>(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static sComponents Components;
|
|
|
|
return Components[a_BlockType].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<cRedstoneHandler> cIncrementalRedstoneSimulator::CreateComponent(BLOCKTYPE a_BlockType)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_ACTIVATOR_RAIL:
|
|
|
|
case E_BLOCK_DETECTOR_RAIL:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_POWERED_RAIL: return cpp14::make_unique<cPoweredRailHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_ACTIVE_COMPARATOR:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_INACTIVE_COMPARATOR: return cpp14::make_unique<cRedstoneComparatorHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_DISPENSER:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_DROPPER: return cpp14::make_unique<cDropSpenserHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_STONE_PRESSURE_PLATE:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_WOODEN_PRESSURE_PLATE: return cpp14::make_unique<cPressurePlateHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2017-04-28 19:15:47 -04:00
|
|
|
case E_BLOCK_ACACIA_FENCE_GATE:
|
|
|
|
case E_BLOCK_BIRCH_FENCE_GATE:
|
|
|
|
case E_BLOCK_DARK_OAK_FENCE_GATE:
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_FENCE_GATE:
|
|
|
|
case E_BLOCK_IRON_TRAPDOOR:
|
2017-04-28 19:15:47 -04:00
|
|
|
case E_BLOCK_JUNGLE_FENCE_GATE:
|
|
|
|
case E_BLOCK_SPRUCE_FENCE_GATE:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_TRAPDOOR: return cpp14::make_unique<cSmallGateHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_REDSTONE_LAMP_OFF:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_REDSTONE_LAMP_ON: return cpp14::make_unique<cRedstoneLampHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_REDSTONE_REPEATER_OFF:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_REDSTONE_REPEATER_ON: return cpp14::make_unique<cRedstoneRepeaterHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_REDSTONE_TORCH_OFF:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_REDSTONE_TORCH_ON: return cpp14::make_unique<cRedstoneTorchHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2020-04-16 17:27:04 -04:00
|
|
|
case E_BLOCK_OBSERVER: return cpp14::make_unique<cObserverHandler>();
|
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_PISTON:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_STICKY_PISTON: return cpp14::make_unique<cPistonHandler>();
|
2016-07-01 15:51:18 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
case E_BLOCK_LEVER:
|
|
|
|
case E_BLOCK_STONE_BUTTON:
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_WOODEN_BUTTON: return cpp14::make_unique<cRedstoneToggleHandler>();
|
|
|
|
|
|
|
|
case E_BLOCK_BLOCK_OF_REDSTONE: return cpp14::make_unique<cRedstoneBlockHandler>();
|
|
|
|
case E_BLOCK_COMMAND_BLOCK: return cpp14::make_unique<cCommandBlockHandler>();
|
2020-03-27 08:03:28 -04:00
|
|
|
case E_BLOCK_HOPPER: return cpp14::make_unique<cHopperHandler>();
|
2017-07-14 22:09:55 -04:00
|
|
|
case E_BLOCK_NOTE_BLOCK: return cpp14::make_unique<cNoteBlockHandler>();
|
|
|
|
case E_BLOCK_REDSTONE_WIRE: return cpp14::make_unique<cRedstoneWireHandler>();
|
|
|
|
case E_BLOCK_TNT: return cpp14::make_unique<cTNTHandler>();
|
|
|
|
case E_BLOCK_TRAPPED_CHEST: return cpp14::make_unique<cTrappedChestHandler>();
|
|
|
|
case E_BLOCK_TRIPWIRE_HOOK: return cpp14::make_unique<cTripwireHookHandler>();
|
2015-06-26 18:24:51 -04:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (cBlockDoorHandler::IsDoorBlockType(a_BlockType))
|
|
|
|
{
|
2017-07-14 22:09:55 -04:00
|
|
|
return cpp14::make_unique<cDoorHandler>();
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cBlockInfo::FullyOccupiesVoxel(a_BlockType))
|
|
|
|
{
|
2017-07-14 22:09:55 -04:00
|
|
|
return cpp14::make_unique<cSolidBlockHandler>();
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
void cIncrementalRedstoneSimulator::SimulateChunk(std::chrono::milliseconds a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
auto & ChunkData = *static_cast<cIncrementalRedstoneSimulatorChunkData *>(a_Chunk->GetRedstoneSimulatorData());
|
|
|
|
for (auto & DelayInfo : ChunkData.m_MechanismDelays)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
if ((--DelayInfo.second.first) == 0)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
ChunkData.WakeUp(DelayInfo.first);
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build our work queue
|
2020-07-26 09:15:00 -04:00
|
|
|
auto & WorkQueue = ChunkData.GetActiveBlocks();
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
// Process the work queue
|
|
|
|
while (!WorkQueue.empty())
|
|
|
|
{
|
|
|
|
// Grab the first element and remove it from the list
|
2020-07-26 09:15:00 -04:00
|
|
|
Vector3i CurrentLocation = WorkQueue.top();
|
|
|
|
WorkQueue.pop();
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
const auto NeighbourChunk = a_Chunk->GetRelNeighborChunkAdjustCoords(CurrentLocation);
|
|
|
|
if ((NeighbourChunk == nullptr) || !NeighbourChunk->IsValid())
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
ProcessWorkItem(*NeighbourChunk, *a_Chunk, CurrentLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto Position : ChunkData.AlwaysTickedPositions)
|
|
|
|
{
|
|
|
|
ChunkData.WakeUp(Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2020-07-24 04:11:40 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
void cIncrementalRedstoneSimulator::ProcessWorkItem(cChunk & Chunk, cChunk & TickingSource, const Vector3i Position)
|
|
|
|
{
|
|
|
|
auto & ChunkData = *static_cast<cIncrementalRedstoneSimulatorChunkData *>(Chunk.GetRedstoneSimulatorData());
|
|
|
|
|
|
|
|
BLOCKTYPE CurrentBlock;
|
|
|
|
NIBBLETYPE CurrentMeta;
|
|
|
|
Chunk.GetBlockTypeMeta(Position, CurrentBlock, CurrentMeta);
|
|
|
|
|
|
|
|
auto CurrentHandler = GetComponentHandler(CurrentBlock);
|
|
|
|
if (CurrentHandler == nullptr) // Block at CurrentPosition doesn't have a corresponding redstone handler
|
|
|
|
{
|
|
|
|
// Clean up cached PowerData for CurrentPosition
|
|
|
|
ChunkData.ErasePowerData(Position);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoweringData Power;
|
|
|
|
CurrentHandler->ForValidSourcePositions(Chunk, Position, CurrentBlock, CurrentMeta, [&Chunk, Position, CurrentBlock, &Power](Vector3i Location)
|
|
|
|
{
|
|
|
|
if (!cChunk::IsValidHeight(Location.y))
|
|
|
|
{
|
|
|
|
return;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
const auto NeighbourChunk = Chunk.GetRelNeighborChunkAdjustCoords(Location);
|
|
|
|
if ((NeighbourChunk == nullptr) || !NeighbourChunk->IsValid())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
BLOCKTYPE PotentialBlock;
|
|
|
|
NIBBLETYPE PotentialMeta;
|
|
|
|
NeighbourChunk->GetBlockTypeMeta(Location, PotentialBlock, PotentialMeta);
|
|
|
|
|
|
|
|
auto PotentialSourceHandler = GetComponentHandler(PotentialBlock);
|
|
|
|
if (PotentialSourceHandler == nullptr)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
2020-07-26 09:15:00 -04:00
|
|
|
|
|
|
|
const PoweringData PotentialPower(
|
|
|
|
PotentialBlock,
|
|
|
|
PotentialSourceHandler->GetPowerDeliveredToPosition(
|
|
|
|
*NeighbourChunk, Location, PotentialBlock, PotentialMeta,
|
|
|
|
cIncrementalRedstoneSimulatorChunkData::RebaseRelativePosition(Chunk, *NeighbourChunk, Position), CurrentBlock
|
|
|
|
)
|
|
|
|
);
|
|
|
|
Power = std::max(Power, PotentialPower);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Inform the handler to update
|
|
|
|
CurrentHandler->Update(Chunk, TickingSource, Position, CurrentBlock, CurrentMeta, Power);
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
2020-04-05 15:20:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIncrementalRedstoneSimulator::AddBlock(Vector3i a_Block, cChunk * a_Chunk)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
// Can't inspect block, ignore:
|
|
|
|
if ((a_Chunk == nullptr) || (!a_Chunk->IsValid()))
|
2020-04-05 15:20:52 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
auto & ChunkData = *static_cast<cIncrementalRedstoneSimulatorChunkData *>(a_Chunk->GetRedstoneSimulatorData());
|
|
|
|
const auto Relative = cChunkDef::AbsoluteToRelative(a_Block, a_Chunk->GetPos());
|
|
|
|
const auto CurrentBlock = a_Chunk->GetBlock(Relative);
|
2020-04-05 15:20:52 -04:00
|
|
|
|
|
|
|
// Always update redstone devices
|
2020-07-26 09:15:00 -04:00
|
|
|
if (IsRedstone(CurrentBlock))
|
2020-04-05 15:20:52 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
if (IsAlwaysTicked(CurrentBlock))
|
|
|
|
{
|
|
|
|
ChunkData.AlwaysTickedPositions.emplace(Relative);
|
|
|
|
}
|
|
|
|
ChunkData.WakeUp(Relative);
|
2020-04-05 15:20:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never update blocks without a handler
|
2020-07-26 09:15:00 -04:00
|
|
|
if (GetComponentHandler(CurrentBlock) == nullptr)
|
2020-04-05 15:20:52 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
ChunkData.ErasePowerData(Relative);
|
2020-04-05 15:20:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only update others if there is a redstone device nearby
|
|
|
|
for (int x = -1; x < 2; ++x)
|
|
|
|
{
|
|
|
|
for (int y = -1; y < 2; ++y)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(Relative.y + y))
|
2020-04-05 15:20:52 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int z = -1; z < 2; ++z)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
auto CheckPos = Relative + Vector3i{x, y, z};
|
2020-04-05 15:20:52 -04:00
|
|
|
BLOCKTYPE Block;
|
|
|
|
NIBBLETYPE Meta;
|
|
|
|
|
|
|
|
// If we can't read the block, assume it is a mechanism
|
|
|
|
if (
|
|
|
|
!a_Chunk->UnboundedRelGetBlock(CheckPos, Block, Meta) ||
|
|
|
|
IsRedstone(Block)
|
|
|
|
)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
ChunkData.WakeUp(Relative);
|
2020-04-05 15:20:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|