2020-08-08 13:22:16 -04:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "ForEachSourceCallback.h"
|
|
|
|
#include "../../BlockInfo.h"
|
|
|
|
#include "../../Chunk.h"
|
2020-08-19 16:14:40 -04:00
|
|
|
#include "IncrementalRedstoneSimulator.h"
|
|
|
|
#include "RedstoneHandler.h"
|
2020-08-08 13:22:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ForEachSourceCallback::ForEachSourceCallback(const cChunk & Chunk, const Vector3i Position, const BLOCKTYPE CurrentBlock) :
|
2020-08-20 15:04:28 -04:00
|
|
|
Power(0),
|
2020-08-08 13:22:16 -04:00
|
|
|
m_Chunk(Chunk),
|
|
|
|
m_Position(Position),
|
|
|
|
m_CurrentBlock(CurrentBlock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ForEachSourceCallback::operator()(Vector3i Location)
|
|
|
|
{
|
2021-01-18 11:09:10 -05:00
|
|
|
if (!cChunkDef::IsValidHeight(Location.y))
|
2020-08-08 13:22:16 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto NeighbourChunk = m_Chunk.GetRelNeighborChunkAdjustCoords(Location);
|
|
|
|
if ((NeighbourChunk == nullptr) || !NeighbourChunk->IsValid())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto PotentialSourceBlock = NeighbourChunk->GetBlock(Location);
|
|
|
|
const auto NeighbourRelativeQueryPosition = cIncrementalRedstoneSimulatorChunkData::RebaseRelativePosition(m_Chunk, *NeighbourChunk, m_Position);
|
|
|
|
|
2020-09-21 15:39:58 -04:00
|
|
|
if (ShouldQueryLinkedPosition(PotentialSourceBlock))
|
2020-08-08 13:22:16 -04:00
|
|
|
{
|
|
|
|
Power = std::max(Power, QueryLinkedPower(*NeighbourChunk, NeighbourRelativeQueryPosition, m_CurrentBlock, Location));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-20 15:04:28 -04:00
|
|
|
Power = std::max(
|
|
|
|
Power,
|
|
|
|
RedstoneHandler::GetPowerDeliveredToPosition(
|
|
|
|
*NeighbourChunk, Location, PotentialSourceBlock,
|
|
|
|
NeighbourRelativeQueryPosition, m_CurrentBlock, false
|
|
|
|
)
|
|
|
|
);
|
2020-08-08 13:22:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-21 15:39:58 -04:00
|
|
|
void ForEachSourceCallback::CheckIndirectPower()
|
|
|
|
{
|
|
|
|
const Vector3i OffsetYP(0, 1, 0);
|
|
|
|
const auto Above = m_Position + OffsetYP;
|
|
|
|
|
|
|
|
if (Above.y == cChunkDef::Height)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object representing restarted power calculation where the
|
|
|
|
// block above this piston, dropspenser is requesting a power level.
|
|
|
|
ForEachSourceCallback QuasiQueryCallback(m_Chunk, Above, m_Chunk.GetBlock(Above));
|
|
|
|
|
|
|
|
// Manually feed the callback object all positions that may deliver power to Above:
|
2020-10-05 06:27:14 -04:00
|
|
|
for (const auto & QuasiPowerOffset : cSimulator::GetLinkedOffsets(OffsetYP))
|
2020-09-21 15:39:58 -04:00
|
|
|
{
|
|
|
|
QuasiQueryCallback(m_Position + QuasiPowerOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the results:
|
|
|
|
Power = std::max(Power, QuasiQueryCallback.Power);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ForEachSourceCallback::ShouldQueryLinkedPosition(const BLOCKTYPE Block)
|
|
|
|
{
|
|
|
|
switch (Block)
|
|
|
|
{
|
2020-12-18 20:42:34 -05:00
|
|
|
// Normally we don't ask solid blocks for power because they don't have any (stone, dirt, etc.)
|
2020-09-21 15:39:58 -04:00
|
|
|
// However, these are mechanisms that are IsSolid, but still give power. Don't ignore them:
|
|
|
|
case E_BLOCK_BLOCK_OF_REDSTONE:
|
2020-12-18 20:42:34 -05:00
|
|
|
case E_BLOCK_DAYLIGHT_SENSOR:
|
|
|
|
case E_BLOCK_INVERTED_DAYLIGHT_SENSOR:
|
2020-09-21 15:39:58 -04:00
|
|
|
case E_BLOCK_OBSERVER:
|
|
|
|
case E_BLOCK_TRAPPED_CHEST: return false;
|
|
|
|
|
|
|
|
// Pistons are solid but don't participate in link powering:
|
|
|
|
case E_BLOCK_PISTON:
|
|
|
|
case E_BLOCK_PISTON_EXTENSION:
|
|
|
|
case E_BLOCK_STICKY_PISTON: return false;
|
|
|
|
|
|
|
|
// If a mechanism asks for power from a block, redirect the query to linked positions if:
|
|
|
|
default: return cBlockInfo::IsSolid(Block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
PowerLevel ForEachSourceCallback::QueryLinkedPower(const cChunk & Chunk, const Vector3i QueryPosition, const BLOCKTYPE QueryBlock, const Vector3i SolidBlockPosition)
|
2020-08-08 13:22:16 -04:00
|
|
|
{
|
2020-08-20 15:04:28 -04:00
|
|
|
PowerLevel Power = 0;
|
2020-08-08 13:22:16 -04:00
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
// Loop through all linked powerable offsets in the direction requested:
|
2020-10-05 06:27:14 -04:00
|
|
|
for (const auto & Offset : cSimulator::GetLinkedOffsets(SolidBlockPosition - QueryPosition))
|
2020-08-08 13:22:16 -04:00
|
|
|
{
|
|
|
|
auto SourcePosition = QueryPosition + Offset;
|
2021-01-18 11:09:10 -05:00
|
|
|
if (!cChunkDef::IsValidHeight(SourcePosition.y))
|
2020-08-08 13:22:16 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto NeighbourChunk = Chunk.GetRelNeighborChunkAdjustCoords(SourcePosition);
|
|
|
|
if ((NeighbourChunk == nullptr) || !NeighbourChunk->IsValid())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
// Conduit block's position, relative to NeighbourChunk.
|
2020-08-08 13:22:16 -04:00
|
|
|
const auto NeighbourRelativeSolidBlockPosition = cIncrementalRedstoneSimulatorChunkData::RebaseRelativePosition(Chunk, *NeighbourChunk, SolidBlockPosition);
|
2020-08-20 15:04:28 -04:00
|
|
|
|
|
|
|
// Do a standard power query, but the requester's position is actually the solid block that will conduct power:
|
|
|
|
Power = std::max(
|
|
|
|
Power,
|
|
|
|
RedstoneHandler::GetPowerDeliveredToPosition(
|
|
|
|
*NeighbourChunk, SourcePosition, NeighbourChunk->GetBlock(SourcePosition),
|
|
|
|
NeighbourRelativeSolidBlockPosition, QueryBlock, true
|
|
|
|
)
|
|
|
|
);
|
2020-08-08 13:22:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Power;
|
|
|
|
}
|