refactor GameHistory

This commit is contained in:
2024-10-19 11:17:57 +02:00
parent ec1344bc37
commit 8ad5dad046
2 changed files with 55 additions and 31 deletions

View File

@@ -2,6 +2,7 @@
#include <optional> #include <optional>
#include <td/protocol/command/Commands.h> #include <td/protocol/command/Commands.h>
#include <td/protocol/packet/Packets.h>
namespace td { namespace td {
namespace game { namespace game {
@@ -10,47 +11,26 @@ class GameHistory {
private: private:
using HistorySizeType = std::uint16_t; using HistorySizeType = std::uint16_t;
std::vector<std::optional<protocol::LockStep>> m_History {std::numeric_limits<HistorySizeType>::max()}; std::vector<std::optional<protocol::LockStep>> m_History;
HistorySizeType m_Cursor = 0; HistorySizeType m_Cursor;
public: public:
GameHistory(){} GameHistory();
void SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep) { 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 { const protocol::LockStep& GetLockStep(HistorySizeType a_Index) const;
return *m_History[a_Index];
}
bool HasLockStep(HistorySizeType a_Index) const { bool HasLockStep(HistorySizeType a_Index) const;
return m_History[a_Index].has_value();
}
const protocol::LockStep& GetNextStep() { const protocol::LockStep& GetNextStep();
return GetLockStep(m_Cursor++);
}
bool HasNextStep() const { bool HasNextStep() const;
return HasLockStep(m_Cursor);
}
void FromPacket(td::protocol::pdata::LockSteps&& a_Steps) { 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) { td::protocol::packets::LockSteps ToPacket(HistorySizeType a_StartIndex);
std::array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> 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 game

View File

@@ -0,0 +1,44 @@
#include <td/game/GameHistory.h>
namespace td {
namespace game {
GameHistory::GameHistory() : m_History(std::numeric_limits<HistorySizeType>::max()), m_Cursor(0) {}
void GameHistory::SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep) {
m_History[a_Index] = std::move(a_LockStep);
}
const protocol::LockStep& GameHistory::GetLockStep(HistorySizeType a_Index) const {
return *m_History[a_Index];
}
bool GameHistory::HasLockStep(HistorySizeType a_Index) const {
return m_History[a_Index].has_value();
}
const protocol::LockStep& GameHistory::GetNextStep() {
return GetLockStep(m_Cursor++);
}
bool GameHistory::HasNextStep() const {
return HasLockStep(m_Cursor);
}
void GameHistory::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 GameHistory::ToPacket(HistorySizeType a_StartIndex) {
std::array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> 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