2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "IncrementalRedstoneSimulator.h"
|
2021-03-28 10:09:01 -04:00
|
|
|
#include "BlockType.h"
|
2020-08-19 16:14:40 -04:00
|
|
|
#include "RedstoneHandler.h"
|
2021-03-28 10:09:01 -04:00
|
|
|
#include "RedstoneSimulatorChunkData.h"
|
2020-08-08 13:22:16 -04:00
|
|
|
#include "ForEachSourceCallback.h"
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-28 10:09:01 -04:00
|
|
|
bool cIncrementalRedstoneSimulator::IsAlwaysTicked(BLOCKTYPE a_Block)
|
|
|
|
{
|
|
|
|
switch (a_Block) // Call the appropriate simulator for the entry's block type
|
|
|
|
{
|
|
|
|
case E_BLOCK_DAYLIGHT_SENSOR:
|
|
|
|
case E_BLOCK_INVERTED_DAYLIGHT_SENSOR:
|
|
|
|
case E_BLOCK_TRIPWIRE_HOOK:
|
|
|
|
case E_BLOCK_WOODEN_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_STONE_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE: return true;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIncrementalRedstoneSimulator::IsRedstone(BLOCKTYPE a_Block)
|
|
|
|
|
|
|
|
{
|
|
|
|
switch (a_Block)
|
|
|
|
{
|
|
|
|
// All redstone devices, please alpha sort
|
|
|
|
case E_BLOCK_ACACIA_DOOR:
|
|
|
|
case E_BLOCK_ACACIA_FENCE_GATE:
|
|
|
|
case E_BLOCK_ACTIVATOR_RAIL:
|
|
|
|
case E_BLOCK_ACTIVE_COMPARATOR:
|
|
|
|
case E_BLOCK_BIRCH_DOOR:
|
|
|
|
case E_BLOCK_BIRCH_FENCE_GATE:
|
|
|
|
case E_BLOCK_BLOCK_OF_REDSTONE:
|
|
|
|
case E_BLOCK_COMMAND_BLOCK:
|
|
|
|
case E_BLOCK_DARK_OAK_DOOR:
|
|
|
|
case E_BLOCK_DARK_OAK_FENCE_GATE:
|
|
|
|
case E_BLOCK_DAYLIGHT_SENSOR:
|
|
|
|
case E_BLOCK_DETECTOR_RAIL:
|
|
|
|
case E_BLOCK_DISPENSER:
|
|
|
|
case E_BLOCK_DROPPER:
|
|
|
|
case E_BLOCK_FENCE_GATE:
|
|
|
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_HOPPER:
|
|
|
|
case E_BLOCK_INACTIVE_COMPARATOR:
|
|
|
|
case E_BLOCK_INVERTED_DAYLIGHT_SENSOR:
|
|
|
|
case E_BLOCK_IRON_DOOR:
|
|
|
|
case E_BLOCK_IRON_TRAPDOOR:
|
|
|
|
case E_BLOCK_JUNGLE_DOOR:
|
|
|
|
case E_BLOCK_JUNGLE_FENCE_GATE:
|
|
|
|
case E_BLOCK_LEVER:
|
|
|
|
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_NOTE_BLOCK:
|
|
|
|
case E_BLOCK_OBSERVER:
|
|
|
|
case E_BLOCK_POWERED_RAIL:
|
|
|
|
case E_BLOCK_REDSTONE_LAMP_OFF:
|
|
|
|
case E_BLOCK_REDSTONE_LAMP_ON:
|
|
|
|
case E_BLOCK_REDSTONE_REPEATER_OFF:
|
|
|
|
case E_BLOCK_REDSTONE_REPEATER_ON:
|
|
|
|
case E_BLOCK_REDSTONE_TORCH_OFF:
|
|
|
|
case E_BLOCK_REDSTONE_TORCH_ON:
|
|
|
|
case E_BLOCK_REDSTONE_WIRE:
|
|
|
|
case E_BLOCK_SPRUCE_DOOR:
|
|
|
|
case E_BLOCK_SPRUCE_FENCE_GATE:
|
|
|
|
case E_BLOCK_STICKY_PISTON:
|
|
|
|
case E_BLOCK_STONE_BUTTON:
|
|
|
|
case E_BLOCK_STONE_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_TNT:
|
|
|
|
case E_BLOCK_TRAPDOOR:
|
|
|
|
case E_BLOCK_TRAPPED_CHEST:
|
|
|
|
case E_BLOCK_TRIPWIRE_HOOK:
|
|
|
|
case E_BLOCK_WOODEN_BUTTON:
|
|
|
|
case E_BLOCK_WOODEN_DOOR:
|
|
|
|
case E_BLOCK_WOODEN_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_PISTON:
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIncrementalRedstoneSimulator::ProcessWorkItem(cChunk & Chunk, cChunk & TickingSource, const Vector3i Position)
|
|
|
|
{
|
|
|
|
BLOCKTYPE CurrentBlock;
|
|
|
|
NIBBLETYPE CurrentMeta;
|
|
|
|
Chunk.GetBlockTypeMeta(Position, CurrentBlock, CurrentMeta);
|
|
|
|
|
|
|
|
ForEachSourceCallback Callback(Chunk, Position, CurrentBlock);
|
|
|
|
RedstoneHandler::ForValidSourcePositions(Chunk, Position, CurrentBlock, CurrentMeta, Callback);
|
|
|
|
|
|
|
|
// Inform the handler to update
|
|
|
|
RedstoneHandler::Update(Chunk, TickingSource, Position, CurrentBlock, CurrentMeta, Callback.Power);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 11:08:20 -04:00
|
|
|
continue;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
ProcessWorkItem(*NeighbourChunk, *a_Chunk, CurrentLocation);
|
|
|
|
}
|
|
|
|
|
2020-10-05 06:27:14 -04:00
|
|
|
for (const auto & Position : ChunkData.AlwaysTickedPositions)
|
2020-07-26 09:15:00 -04:00
|
|
|
{
|
|
|
|
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-28 19:12:45 -04:00
|
|
|
void cIncrementalRedstoneSimulator::AddBlock(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_Block)
|
2020-04-05 15:20:52 -04:00
|
|
|
{
|
2020-08-19 16:14:40 -04:00
|
|
|
// Never update blocks without a handler:
|
2020-07-28 20:18:59 -04:00
|
|
|
if (!IsRedstone(a_Block))
|
2020-04-05 15:20:52 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:15:09 -04:00
|
|
|
auto & ChunkData = *static_cast<cIncrementalRedstoneSimulatorChunkData *>(a_Chunk.GetRedstoneSimulatorData());
|
|
|
|
|
2020-07-28 20:18:59 -04:00
|
|
|
if (IsAlwaysTicked(a_Block))
|
2020-04-05 15:20:52 -04:00
|
|
|
{
|
2020-07-28 20:18:59 -04:00
|
|
|
ChunkData.AlwaysTickedPositions.emplace(a_Position);
|
2020-04-05 15:20:52 -04:00
|
|
|
}
|
|
|
|
|
2020-08-08 13:22:16 -04:00
|
|
|
// Temporary: in the absence of block state support calculate our own:
|
|
|
|
if (a_Block == E_BLOCK_REDSTONE_WIRE)
|
|
|
|
{
|
2020-08-19 16:14:40 -04:00
|
|
|
RedstoneHandler::SetWireState(a_Chunk, a_Position);
|
2020-08-08 13:22:16 -04:00
|
|
|
}
|
|
|
|
|
2020-07-28 19:12:45 -04:00
|
|
|
// Always update redstone devices:
|
2020-07-28 20:18:59 -04:00
|
|
|
ChunkData.WakeUp(a_Position);
|
2020-04-05 15:20:52 -04:00
|
|
|
}
|
2020-07-29 15:15:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-28 10:09:01 -04:00
|
|
|
cRedstoneSimulatorChunkData * cIncrementalRedstoneSimulator::CreateChunkData()
|
|
|
|
{
|
|
|
|
return new cIncrementalRedstoneSimulatorChunkData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-29 15:15:09 -04:00
|
|
|
void cIncrementalRedstoneSimulator::WakeUp(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_Block)
|
|
|
|
{
|
|
|
|
// Having WakeUp called on us directly means someone called SetBlock (or WakeUp)
|
|
|
|
// Since the simulator never does this, something external changed. Clear cached data:
|
|
|
|
static_cast<cIncrementalRedstoneSimulatorChunkData *>(a_Chunk.GetRedstoneSimulatorData())->ErasePowerData(a_Position);
|
|
|
|
|
|
|
|
// Queue the block, in case the set block was redstone:
|
|
|
|
AddBlock(a_Chunk, a_Position, a_Block);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIncrementalRedstoneSimulator::WakeUp(cChunk & a_Chunk, Vector3i a_Position, Vector3i a_Offset, BLOCKTYPE a_Block)
|
|
|
|
{
|
|
|
|
// This is an automatic cross-coords wakeup by cSimulatorManager
|
|
|
|
// There is no need to erase power data; if a component was destroyed the 3-arg WakeUp will handle it
|
|
|
|
|
|
|
|
AddBlock(a_Chunk, a_Position, a_Block);
|
|
|
|
|
|
|
|
// The only thing to do go one block farther than this cross-coord, in the direction of Offset
|
|
|
|
// in order to notify linked-powered positions that there was a change
|
|
|
|
|
2020-10-05 06:27:14 -04:00
|
|
|
for (const auto & Offset : cSimulator::GetLinkedOffsets(a_Offset))
|
2020-08-08 13:22:16 -04:00
|
|
|
{
|
|
|
|
auto Relative = a_Position - a_Offset + Offset;
|
|
|
|
if (!cChunkDef::IsValidHeight(Relative.y))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(Relative);
|
|
|
|
if ((Chunk == nullptr) || !Chunk->IsValid())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto Block = Chunk->GetBlock(Relative);
|
|
|
|
AddBlock(*Chunk, Relative, Block);
|
|
|
|
}
|
2020-07-29 15:15:09 -04:00
|
|
|
}
|