2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "IncrementalRedstoneSimulator.h"
|
|
|
|
#include "Chunk.h"
|
|
|
|
|
|
|
|
#include "CommandBlockHandler.h"
|
|
|
|
#include "DoorHandler.h"
|
|
|
|
#include "RedstoneHandler.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"
|
|
|
|
#include "TNTHandler.h"
|
|
|
|
#include "PoweredRailHandler.h"
|
|
|
|
#include "PressurePlateHandler.h"
|
|
|
|
#include "TripwireHookHandler.h"
|
|
|
|
#include "DropSpenserHandler.h"
|
|
|
|
#include "RedstoneComparatorHandler.h"
|
|
|
|
#include "TrappedChestHandler.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
const cRedstoneHandler * cIncrementalRedstoneSimulator::GetComponentHandler(BLOCKTYPE a_BlockType)
|
|
|
|
{
|
|
|
|
struct sComponents:
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
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
|
|
|
|
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>();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIncrementalRedstoneSimulator::Simulate(float a_dt)
|
|
|
|
{
|
|
|
|
for (auto & DelayInfo : m_Data.m_MechanismDelays)
|
|
|
|
{
|
|
|
|
if ((--DelayInfo.second.first) == 0)
|
|
|
|
{
|
|
|
|
m_Data.GetActiveBlocks().emplace_back(DelayInfo.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build our work queue
|
|
|
|
cVector3iArray WorkQueue;
|
|
|
|
std::swap(WorkQueue, m_Data.GetActiveBlocks());
|
|
|
|
|
|
|
|
// Process the work queue
|
|
|
|
while (!WorkQueue.empty())
|
|
|
|
{
|
|
|
|
// Grab the first element and remove it from the list
|
|
|
|
Vector3i CurrentLocation = WorkQueue.back();
|
|
|
|
WorkQueue.pop_back();
|
|
|
|
|
|
|
|
BLOCKTYPE CurrentBlock;
|
|
|
|
NIBBLETYPE CurrentMeta;
|
|
|
|
if (!m_World.GetBlockTypeMeta(CurrentLocation.x, CurrentLocation.y, CurrentLocation.z, CurrentBlock, CurrentMeta))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
auto CurrentHandler = GetComponentHandler(CurrentBlock);
|
2016-02-07 09:27:24 -05:00
|
|
|
if (CurrentHandler == nullptr) // Block at CurrentPosition doesn't have a corresponding redstone handler
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2016-02-07 09:27:24 -05:00
|
|
|
// Clean up cached PowerData for CurrentPosition
|
|
|
|
static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData()->ErasePowerData(CurrentLocation);
|
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cRedstoneHandler::PoweringData Power;
|
2017-07-14 22:09:55 -04:00
|
|
|
for (const auto & Location : CurrentHandler->GetValidSourcePositions(m_World, CurrentLocation, CurrentBlock, CurrentMeta))
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2016-06-04 08:16:35 -04:00
|
|
|
if (!cChunk::IsValidHeight(Location.y))
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-04 08:16:35 -04:00
|
|
|
BLOCKTYPE PotentialBlock;
|
|
|
|
NIBBLETYPE PotentialMeta;
|
2015-06-26 18:24:51 -04:00
|
|
|
m_World.GetBlockTypeMeta(Location.x, Location.y, Location.z, PotentialBlock, PotentialMeta);
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
auto PotentialSourceHandler = GetComponentHandler(PotentialBlock);
|
2015-06-26 18:24:51 -04:00
|
|
|
if (PotentialSourceHandler == nullptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
decltype(Power) PotentialPower(PotentialBlock, PotentialSourceHandler->GetPowerDeliveredToPosition(m_World, Location, PotentialBlock, PotentialMeta, CurrentLocation, CurrentBlock));
|
2015-06-26 18:24:51 -04:00
|
|
|
Power = std::max(Power, PotentialPower);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inform the handler to update
|
2017-07-14 22:09:55 -04:00
|
|
|
cVector3iArray Updates = CurrentHandler->Update(m_World, CurrentLocation, CurrentBlock, CurrentMeta, Power);
|
2015-06-26 18:24:51 -04:00
|
|
|
WorkQueue.insert(WorkQueue.end(), Updates.begin(), Updates.end());
|
|
|
|
|
|
|
|
if (IsAlwaysTicked(CurrentBlock))
|
|
|
|
{
|
|
|
|
m_Data.GetActiveBlocks().emplace_back(CurrentLocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|