2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
#include <stack>
|
|
|
|
|
2020-12-21 08:54:04 -05:00
|
|
|
#include "Chunk.h"
|
|
|
|
#include "BlockState.h"
|
|
|
|
#include "Simulator/RedstoneSimulator.h"
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
using PowerLevel = unsigned char;
|
2020-07-26 09:15:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-08 13:22:16 -04:00
|
|
|
class cIncrementalRedstoneSimulatorChunkData final : public cRedstoneSimulatorChunkData
|
2020-07-26 09:15:00 -04:00
|
|
|
{
|
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-08-20 15:04:28 -04:00
|
|
|
PowerLevel 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);
|
2020-08-20 15:04:28 -04:00
|
|
|
return (Result == m_CachedPowerLevels.end()) ? 0 : Result->second;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
void SetCachedPowerData(const Vector3i Position, const PowerLevel PowerLevel)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-08-20 15:04:28 -04:00
|
|
|
m_CachedPowerLevels[Position] = PowerLevel;
|
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);
|
2020-08-08 13:22:16 -04:00
|
|
|
WireStates.erase(Position);
|
2020-08-20 15:04:28 -04:00
|
|
|
ObserverCache.erase(Position);
|
2016-02-07 09:27:24 -05:00
|
|
|
}
|
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
PowerLevel ExchangeUpdateOncePowerData(const Vector3i & a_Position, PowerLevel Power)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 11:06:57 -04:00
|
|
|
auto Result = m_CachedPowerLevels.find(a_Position);
|
2020-08-20 15:04:28 -04:00
|
|
|
|
2020-07-26 11:06:57 -04:00
|
|
|
if (Result == m_CachedPowerLevels.end())
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-08-20 15:04:28 -04:00
|
|
|
m_CachedPowerLevels[a_Position] = Power;
|
|
|
|
return 0;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
2020-08-20 15:04:28 -04:00
|
|
|
|
2020-09-12 14:57:44 -04:00
|
|
|
return std::exchange(Result->second, Power);
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
/** Adjust From-relative coordinates into To-relative coordinates. */
|
2020-08-08 13:22:16 -04:00
|
|
|
inline static Vector3i RebaseRelativePosition(const cChunk & From, const cChunk & To, const Vector3i Position)
|
2020-07-26 09:15:00 -04:00
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
Position.x + (From.GetPosX() - To.GetPosX()) * cChunkDef::Width,
|
|
|
|
Position.y,
|
|
|
|
Position.z + (From.GetPosZ() - To.GetPosZ()) * cChunkDef::Width
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-08 13:22:16 -04:00
|
|
|
/** Temporary, should be chunk data: wire block store, to avoid recomputing states every time. */
|
2020-12-21 08:54:04 -05:00
|
|
|
std::unordered_map<Vector3i, BlockState, VectorHasher<int>> WireStates;
|
2020-08-08 13:22:16 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
std::unordered_set<Vector3i, VectorHasher<int>> AlwaysTickedPositions;
|
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
/** Structure storing an observer's last seen block. */
|
|
|
|
std::unordered_map<Vector3i, std::pair<BLOCKTYPE, NIBBLETYPE>, VectorHasher<int>> ObserverCache;
|
|
|
|
|
2020-08-08 13:22:16 -04:00
|
|
|
/** Structure storing position of mechanism + it's delay ticks (countdown) & if to power on. */
|
2015-06-26 18:24:51 -04:00
|
|
|
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-08-20 15:04:28 -04:00
|
|
|
std::unordered_map<Vector3i, PowerLevel, VectorHasher<int>> m_CachedPowerLevels;
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
friend class cRedstoneHandlerFactory;
|
|
|
|
};
|