game history

This commit is contained in:
2024-10-13 15:31:15 +02:00
parent 0354ce776b
commit fc405eeba1

View File

@@ -0,0 +1,57 @@
#pragma once
#include <optional>
#include <td/protocol/command/Commands.h>
namespace td {
namespace game {
class GameHistory {
private:
using HistorySizeType = std::uint16_t;
std::vector<std::optional<protocol::LockStep>> m_History {std::numeric_limits<HistorySizeType>::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<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