1
0

Consolidated comparator code

* As a result, fixed an issue where GetPowerLevel didn't consider block
entities behind it (only GetFrontPowerLevel did)
This commit is contained in:
Tiger Wang 2016-06-05 16:53:14 +01:00
parent 04cc8aa0f5
commit 62090e7bed
3 changed files with 46 additions and 50 deletions

View File

@ -1,11 +1,11 @@
version: 1.0.{build} version: 1.0.{build}
configuration: Debug configuration: Debug
clone_depth: 50 clone_depth: 1
before_build: before_build:
- echo %TIME% - echo %TIME%
- git submodule update --init - git submodule update --init
- echo %TIME% - echo %TIME%
- cmake -G "Visual Studio 12" . - cmake .
- echo %TIME% - echo %TIME%
build: build:
project: Cuberite.sln project: Cuberite.sln

View File

@ -60,6 +60,11 @@ public:
return true; return true;
} }
inline static bool IsInSubtractionMode(NIBBLETYPE a_Meta)
{
return ((a_Meta & 0x4) == 0x4);
}
inline static bool IsOn(NIBBLETYPE a_Meta) inline static bool IsOn(NIBBLETYPE a_Meta)
{ {
return ((a_Meta & 0x8) == 0x8); return ((a_Meta & 0x8) == 0x8);

View File

@ -18,54 +18,17 @@ public:
{ {
} }
unsigned char GetFrontPowerLevel(const Vector3i & a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, unsigned char a_HighestSidePowerLevel) unsigned char GetFrontPowerLevel(const Vector3i & a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, unsigned char a_HighestSidePowerLevel, unsigned char a_HighestRearPowerLevel)
{ {
class cContainerCallback : public cBlockEntityCallback if (cBlockComparatorHandler::IsInSubtractionMode(a_Meta))
{
public:
cContainerCallback() : m_SignalStrength(0)
{
}
virtual bool Item(cBlockEntity * a_BlockEntity) override
{
auto & Contents = static_cast<cBlockEntityWithItems *>(a_BlockEntity)->GetContents();
float Fullness = 0; // Is a floating-point type to allow later calculation to produce a non-truncated value
for (int Slot = 0; Slot != Contents.GetNumSlots(); ++Slot)
{
Fullness += Contents.GetSlot(Slot).m_ItemCount / Contents.GetSlot(Slot).GetMaxStackSize();
}
m_SignalStrength = static_cast<unsigned char>(1 + (Fullness / Contents.GetNumSlots()) * 14);
return false;
}
unsigned char m_SignalStrength;
} CCB;
auto RearCoordinate = cBlockComparatorHandler::GetRearCoordinate(a_Position, a_Meta & 0x3);
m_World.DoWithBlockEntityAt(RearCoordinate.x, RearCoordinate.y, RearCoordinate.z, CCB);
auto RearPower = CCB.m_SignalStrength;
auto PotentialSourceHandler = cIncrementalRedstoneSimulator::CreateComponent(m_World, m_World.GetBlock(RearCoordinate), static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData());
if (PotentialSourceHandler != nullptr)
{
BLOCKTYPE Type;
NIBBLETYPE Meta;
if (m_World.GetBlockTypeMeta(RearCoordinate.x, RearCoordinate.y, RearCoordinate.z, Type, Meta))
{
RearPower = std::max(CCB.m_SignalStrength, PotentialSourceHandler->GetPowerDeliveredToPosition(RearCoordinate, Type, Meta, a_Position, a_BlockType));
}
}
if ((a_Meta & 0x4) == 0x4)
{ {
// Subtraction mode // Subtraction mode
return static_cast<unsigned char>(std::max(static_cast<char>(RearPower) - a_HighestSidePowerLevel, 0)); return static_cast<unsigned char>(std::max(static_cast<char>(a_HighestRearPowerLevel) - a_HighestSidePowerLevel, 0));
} }
else else
{ {
// Comparison mode // Comparison mode
return (std::max(a_HighestSidePowerLevel, RearPower) == a_HighestSidePowerLevel) ? 0 : RearPower; return (std::max(a_HighestSidePowerLevel, a_HighestRearPowerLevel) == a_HighestSidePowerLevel) ? 0 : a_HighestRearPowerLevel;
} }
} }
@ -82,7 +45,34 @@ public:
UNUSED(a_Position); UNUSED(a_Position);
UNUSED(a_BlockType); UNUSED(a_BlockType);
class cContainerCallback : public cBlockEntityCallback
{
public:
cContainerCallback() : m_SignalStrength(0)
{
}
virtual bool Item(cBlockEntity * a_BlockEntity) override
{
auto & Contents = static_cast<cBlockEntityWithItems *>(a_BlockEntity)->GetContents();
float Fullness = 0; // Is a floating-point type to allow later calculation to produce a non-truncated value
for (int Slot = 0; Slot != Contents.GetNumSlots(); ++Slot)
{
Fullness += static_cast<float>(Contents.GetSlot(Slot).m_ItemCount) / Contents.GetSlot(Slot).GetMaxStackSize();
}
m_SignalStrength = (Fullness < 0.001 /* container empty? */) ? 0 : static_cast<unsigned char>(1 + (Fullness / Contents.GetNumSlots()) * 14);
return false;
}
unsigned char m_SignalStrength;
} CCB;
auto RearCoordinate = cBlockComparatorHandler::GetRearCoordinate(a_Position, a_Meta & 0x3); auto RearCoordinate = cBlockComparatorHandler::GetRearCoordinate(a_Position, a_Meta & 0x3);
m_World.DoWithBlockEntityAt(RearCoordinate.x, RearCoordinate.y, RearCoordinate.z, CCB);
auto RearPower = CCB.m_SignalStrength;
auto PotentialSourceHandler = cIncrementalRedstoneSimulator::CreateComponent(m_World, m_World.GetBlock(RearCoordinate), static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData()); auto PotentialSourceHandler = cIncrementalRedstoneSimulator::CreateComponent(m_World, m_World.GetBlock(RearCoordinate), static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData());
if (PotentialSourceHandler != nullptr) if (PotentialSourceHandler != nullptr)
{ {
@ -90,16 +80,16 @@ public:
NIBBLETYPE Meta; NIBBLETYPE Meta;
if (m_World.GetBlockTypeMeta(RearCoordinate.x, RearCoordinate.y, RearCoordinate.z, Type, Meta)) if (m_World.GetBlockTypeMeta(RearCoordinate.x, RearCoordinate.y, RearCoordinate.z, Type, Meta))
{ {
return PotentialSourceHandler->GetPowerDeliveredToPosition(RearCoordinate, Type, Meta, a_Position, a_BlockType); RearPower = std::max(CCB.m_SignalStrength, PotentialSourceHandler->GetPowerDeliveredToPosition(RearCoordinate, Type, Meta, a_Position, a_BlockType));
} }
} }
return 0; return RearPower;
} }
virtual cVector3iArray Update(const Vector3i & a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) override virtual cVector3iArray Update(const Vector3i & a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) override
{ {
// Note that a_PoweringData here contains the maximum *side* power level, as specified by GetValidSourcePositions // Note that a_PoweringData here contains the maximum * side * power level, as specified by GetValidSourcePositions
// LOGD("Evaluating ALU the comparator (%d %d %d)", a_Position.x, a_Position.y, a_Position.z); // LOGD("Evaluating ALU the comparator (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
auto Data = static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData(); auto Data = static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData();
auto DelayInfo = Data->GetMechanismDelayInfo(a_Position); auto DelayInfo = Data->GetMechanismDelayInfo(a_Position);
@ -107,11 +97,12 @@ public:
// Delay is used here to prevent an infinite loop (#3168) // Delay is used here to prevent an infinite loop (#3168)
if (DelayInfo == nullptr) if (DelayInfo == nullptr)
{ {
auto Power = GetFrontPowerLevel(a_Position, a_BlockType, a_Meta, a_PoweringData.PowerLevel); auto RearPower = GetPowerLevel(a_Position, a_BlockType, a_Meta);
auto PreviousFrontPower = static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData()->ExchangeUpdateOncePowerData(a_Position, PoweringData(a_PoweringData.PoweringBlock, Power)); auto FrontPower = GetFrontPowerLevel(a_Position, a_BlockType, a_Meta, a_PoweringData.PowerLevel, RearPower);
auto PreviousFrontPower = static_cast<cIncrementalRedstoneSimulator *>(m_World.GetRedstoneSimulator())->GetChunkData()->ExchangeUpdateOncePowerData(a_Position, PoweringData(a_PoweringData.PoweringBlock, FrontPower));
bool ShouldBeOn = (GetPowerLevel(a_Position, a_BlockType, a_Meta) > 0); // Provide visual indication by examining *rear* power level bool ShouldBeOn = (RearPower > 0); // Provide visual indication by examining * rear * power level
bool ShouldUpdate = (Power != PreviousFrontPower.PowerLevel); // "Business logic" (:P) - determine by examining *side* power levels bool ShouldUpdate = (FrontPower != PreviousFrontPower.PowerLevel); // "Business logic" (:P) - determine by examining *side* power levels
if (ShouldUpdate || (ShouldBeOn != cBlockComparatorHandler::IsOn(a_Meta))) if (ShouldUpdate || (ShouldBeOn != cBlockComparatorHandler::IsOn(a_Meta)))
{ {