4 Commits

Author SHA1 Message Date
8ad5dad046 refactor GameHistory 2024-10-19 11:17:57 +02:00
ec1344bc37 format 2024-10-19 11:17:41 +02:00
d0dc4408ba set c++ standard 2024-10-19 11:13:58 +02:00
cc8a87368e missing include 2024-10-19 11:05:21 +02:00
6 changed files with 59 additions and 33 deletions

View File

@@ -6,6 +6,7 @@
*/
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <cstring>

View File

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

View File

@@ -1,10 +1,10 @@
#include <iostream>
// #include <td/protocol/packet/PacketVisitor.h>
#include <td/game/GameHistory.h>
#include <td/protocol/command/CommandDispatcher.h>
#include <td/protocol/command/CommandFactory.h>
#include <td/protocol/command/CommandSerializer.h>
#include <td/protocol/command/CommandVisitor.h>
#include <td/game/GameHistory.h>
class Test : public td::protocol::CommandVisitor {};

View File

@@ -1,7 +1,7 @@
#include <td/common/VarInt.h>
#include <td/common/DataBuffer.h>
#include <stdexcept>
#include <td/common/DataBuffer.h>
namespace td {

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

View File

@@ -1,6 +1,7 @@
add_rules("mode.debug", "mode.release")
add_requires("fpm")
set_languages("c++17")
target("Tower-Defense2")
add_includedirs("include", {public = true})