diff --git a/include/td/game/GameHistory.h b/include/td/game/GameHistory.h new file mode 100644 index 0000000..9407c61 --- /dev/null +++ b/include/td/game/GameHistory.h @@ -0,0 +1,57 @@ +#pragma once + +#include +#include + +namespace td { +namespace game { + +class GameHistory { + private: + using HistorySizeType = std::uint16_t; + + std::vector> m_History {std::numeric_limits::max()}; + + HistorySizeType m_Cursor = 0; + + public: + GameHistory(){} + + void SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep) { + m_History[a_Index] = std::move(a_LockStep); + } + + const protocol::LockStep& GetLockStep(HistorySizeType a_Index) const { + return *m_History[a_Index]; + } + + bool HasLockStep(HistorySizeType a_Index) const { + return m_History[a_Index].has_value(); + } + + const protocol::LockStep& GetNextStep() { + return GetLockStep(m_Cursor++); + } + + bool HasNextStep() const { + return HasLockStep(m_Cursor); + } + + void FromPacket(td::protocol::pdata::LockSteps&& a_Steps) { + for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { + protocol::LockStep& step = a_Steps.m_LockSteps[i]; + SetLockStep(i + a_Steps.m_FirstFrameNumber, std::move(step)); + } + } + + td::protocol::packets::LockSteps ToPacket(HistorySizeType a_StartIndex) { + std::array steps; + for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { + steps[i] = GetLockStep(a_StartIndex + i); + } + return {{a_StartIndex, std::move(steps)}}; + } +}; + +} // namespace game +} // namespace td