2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
#include <stack>
|
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
#include "../RedstoneSimulator.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
struct PoweringData
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
public:
|
|
|
|
PoweringData(BLOCKTYPE a_PoweringBlock, unsigned char a_PowerLevel) :
|
|
|
|
PoweringBlock(a_PoweringBlock),
|
|
|
|
PowerLevel(a_PowerLevel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PoweringData(void) :
|
|
|
|
PoweringBlock(E_BLOCK_AIR),
|
|
|
|
PowerLevel(0)
|
|
|
|
{
|
|
|
|
}
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
BLOCKTYPE PoweringBlock;
|
|
|
|
unsigned char PowerLevel;
|
|
|
|
|
|
|
|
inline friend bool operator < (const PoweringData & Lhs, const PoweringData & Rhs)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
(Lhs.PowerLevel < Rhs.PowerLevel) ||
|
|
|
|
(
|
|
|
|
(Lhs.PowerLevel == Rhs.PowerLevel) &&
|
|
|
|
((Lhs.PoweringBlock == E_BLOCK_REDSTONE_WIRE) && (Rhs.PoweringBlock != E_BLOCK_REDSTONE_WIRE))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline friend bool operator == (const PoweringData & Lhs, const PoweringData & Rhs)
|
|
|
|
{
|
|
|
|
return (Lhs.PowerLevel == Rhs.PowerLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline friend bool operator != (const PoweringData & Lhs, const PoweringData & Rhs)
|
|
|
|
{
|
|
|
|
return !operator ==(Lhs, Rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cIncrementalRedstoneSimulatorChunkData : public cRedstoneSimulatorChunkData
|
|
|
|
{
|
2015-06-26 18:24:51 -04:00
|
|
|
public:
|
2020-07-26 09:15:00 -04:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
void WakeUp(const Vector3i & a_Position)
|
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
m_ActiveBlocks.push(a_Position);
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
auto & GetActiveBlocks()
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
return m_ActiveBlocks;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
const PoweringData GetCachedPowerData(const Vector3i Position) const
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
auto Result = m_CachedPowerLevels.find(Position);
|
|
|
|
return (Result == m_CachedPowerLevels.end()) ? PoweringData() : Result->second;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
void SetCachedPowerData(const Vector3i Position, PoweringData PoweringData)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
m_CachedPowerLevels[Position] = PoweringData;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
std::pair<int, bool> * GetMechanismDelayInfo(const Vector3i Position)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
auto Result = m_MechanismDelays.find(Position);
|
2015-06-26 18:24:51 -04:00
|
|
|
return (Result == m_MechanismDelays.end()) ? nullptr : &Result->second;
|
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
/** Erase all cached redstone data for position. */
|
|
|
|
void ErasePowerData(const Vector3i Position)
|
2016-02-07 09:27:24 -05:00
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
m_CachedPowerLevels.erase(Position);
|
2020-07-26 09:15:00 -04:00
|
|
|
m_MechanismDelays.erase(Position);
|
|
|
|
AlwaysTickedPositions.erase(Position);
|
2016-02-07 09:27:24 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
PoweringData ExchangeUpdateOncePowerData(const Vector3i & a_Position, PoweringData a_PoweringData)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
auto Result = m_CachedPowerLevels.find(a_Position);
|
|
|
|
if (Result == m_CachedPowerLevels.end())
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
m_CachedPowerLevels[a_Position] = a_PoweringData;
|
2020-07-26 09:15:00 -04:00
|
|
|
return PoweringData();
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
std::swap(Result->second, a_PoweringData);
|
|
|
|
return a_PoweringData;
|
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
/** Adjust From-relative coordinates into To-relative coordinates. */
|
|
|
|
inline static Vector3i RebaseRelativePosition(cChunk & From, cChunk & To, const Vector3i Position)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
Position.x + (From.GetPosX() - To.GetPosX()) * cChunkDef::Width,
|
|
|
|
Position.y,
|
|
|
|
Position.z + (From.GetPosZ() - To.GetPosZ()) * cChunkDef::Width
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_set<Vector3i, VectorHasher<int>> AlwaysTickedPositions;
|
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
/** Structure storing position of mechanism + it's delay ticks (countdown) & if to power on */
|
|
|
|
std::unordered_map<Vector3i, std::pair<int, bool>, VectorHasher<int>> m_MechanismDelays;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2020-07-26 11:06:57 -04:00
|
|
|
std::stack<Vector3i, std::vector<Vector3i>> m_ActiveBlocks;
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
// TODO: map<Vector3i, int> -> Position of torch + it's heat level
|
|
|
|
|
2020-07-26 11:06:57 -04:00
|
|
|
std::unordered_map<Vector3i, PoweringData, VectorHasher<int>> m_CachedPowerLevels;
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
friend class cRedstoneHandlerFactory;
|
|
|
|
|
|
|
|
};
|