1
0

Use SimulateChunk in redstone simulator

+ Improved performance, reduces bottleneck in chunkmap lookup
* Stop allocating and throwing away lots of small vectors in Update/GetValidSourcePositions return values
- Remove unused GetPowerLevel virtual
This commit is contained in:
Tiger Wang
2020-07-26 14:15:00 +01:00
parent de06ae7add
commit 4e5ab02a58
27 changed files with 785 additions and 847 deletions

View File

@@ -15,37 +15,27 @@ class cRedstoneRepeaterHandler:
public:
virtual unsigned char GetPowerDeliveredToPosition(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override
virtual unsigned char GetPowerDeliveredToPosition(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override
{
return (
(a_QueryPosition == (a_Position + cBlockRedstoneRepeaterHandler::GetFrontCoordinateOffset(a_Meta))) ?
GetPowerLevel(a_World, a_Position, a_BlockType, a_Meta) : 0
);
return ((a_QueryPosition == (a_Position + cBlockRedstoneRepeaterHandler::GetFrontCoordinateOffset(a_Meta))) && IsOn(a_BlockType)) ? 15 : 0;
}
virtual unsigned char GetPowerLevel(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override
{
UNUSED(a_World);
UNUSED(a_Position);
UNUSED(a_Meta);
return IsOn(a_BlockType) ? 15 : 0;
}
virtual cVector3iArray Update(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override
virtual void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override
{
// LOGD("Evaluating loopy the repeater (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
auto Data = static_cast<cIncrementalRedstoneSimulator *>(a_World.GetRedstoneSimulator())->GetChunkData();
auto DelayInfo = Data->GetMechanismDelayInfo(a_Position);
auto & Data = DataForChunk(a_Chunk);
const auto DelayInfo = Data.GetMechanismDelayInfo(a_Position);
// If the repeater is locked by another, ignore and forget all power changes:
if (IsLocked(a_World, a_Position, a_Meta))
if (IsLocked(a_Chunk, a_Position, a_Meta))
{
if (DelayInfo != nullptr)
{
Data->m_MechanismDelays.erase(a_Position);
Data.m_MechanismDelays.erase(a_Position);
}
return {};
return;
}
if (DelayInfo == nullptr)
@@ -53,7 +43,7 @@ public:
bool ShouldBeOn = (a_PoweringData.PowerLevel != 0);
if (ShouldBeOn != IsOn(a_BlockType))
{
Data->m_MechanismDelays[a_Position] = std::make_pair((((a_Meta & 0xC) >> 0x2) + 1), ShouldBeOn);
Data.m_MechanismDelays[a_Position] = std::make_pair((((a_Meta & 0xC) >> 0x2) + 1), ShouldBeOn);
}
}
else
@@ -64,18 +54,23 @@ public:
if (DelayTicks == 0)
{
a_World.SetBlock(a_Position.x, a_Position.y, a_Position.z, ShouldPowerOn ? E_BLOCK_REDSTONE_REPEATER_ON : E_BLOCK_REDSTONE_REPEATER_OFF, a_Meta);
Data->m_MechanismDelays.erase(a_Position);
return cVector3iArray{ cBlockRedstoneRepeaterHandler::GetFrontCoordinateOffset(a_Meta) + a_Position };
const auto NewType = ShouldPowerOn ? E_BLOCK_REDSTONE_REPEATER_ON : E_BLOCK_REDSTONE_REPEATER_OFF;
a_Chunk.FastSetBlock(a_Position, NewType, a_Meta);
Data.m_MechanismDelays.erase(a_Position);
// While sleeping, we ignore any power changes and apply our saved ShouldBeOn when sleep expires
// Now, we need to recalculate to be aware of any new changes that may e.g. cause a new output change
// FastSetBlock doesn't wake simulators, so manually update ourselves:
Update(a_Chunk, CurrentlyTicking, a_Position, NewType, a_Meta, a_PoweringData);
UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position + cBlockRedstoneRepeaterHandler::GetFrontCoordinateOffset(a_Meta));
}
}
return {};
}
virtual cVector3iArray GetValidSourcePositions(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override
virtual void ForValidSourcePositions(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, SourceCallback Callback) const override
{
return { cBlockRedstoneRepeaterHandler::GetRearCoordinateOffset(a_Meta) + a_Position };
Callback(cBlockRedstoneRepeaterHandler::GetRearCoordinateOffset(a_Meta) + a_Position);
}
private:
@@ -87,12 +82,12 @@ private:
/** Returns a pair with first element indicating if the block at the given position is an activated repeater.
If it is activated, the second element is the repeater metadata. */
static std::pair<bool, NIBBLETYPE> IsOnRepeater(cWorld & a_World, const Vector3i a_Position)
static std::pair<bool, NIBBLETYPE> IsOnRepeater(cChunk & Chunk, const Vector3i a_Position)
{
BLOCKTYPE Type;
NIBBLETYPE Meta;
if (!a_World.GetBlockTypeMeta(a_Position, Type, Meta))
if (!Chunk.UnboundedRelGetBlock(a_Position, Type, Meta))
{
return std::make_pair(false, 0);
}
@@ -103,20 +98,20 @@ private:
/** Determine if a repeater is locked.
A locked repeater is one with another powered repeater facing them, to their immediate left or right sides.
"Left" is relative to the direction the repeater output faces, naturally. */
inline static bool IsLocked(cWorld & a_World, const Vector3i a_Position, const NIBBLETYPE a_Meta)
inline static bool IsLocked(cChunk & Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta)
{
// The left hand side offset. Will be negated to get the rhs offset
const auto LhsOffset = cBlockRedstoneRepeaterHandler::GetLeftCoordinateOffset(a_Meta);
// Test the block to the left of us
const auto Lhs = IsOnRepeater(a_World, LhsOffset + a_Position);
const auto Lhs = IsOnRepeater(Chunk, LhsOffset + a_Position);
if (Lhs.first && DoesLhsLockMe(Lhs.second, a_Meta))
{
return true;
}
// Test the right side, flipping the argument order to DoesLhsLockMe
const auto Rhs = IsOnRepeater(a_World, -LhsOffset + a_Position);
const auto Rhs = IsOnRepeater(Chunk, -LhsOffset + a_Position);
return Rhs.first && DoesLhsLockMe(a_Meta, Rhs.second);
}