begin client-server

This commit is contained in:
2025-08-06 20:10:56 +02:00
parent 89213e9a97
commit c813c49707
26 changed files with 264 additions and 188 deletions

View File

@@ -2,6 +2,8 @@
#include <chrono>
#include <iostream>
namespace td {
namespace sim {
@@ -30,13 +32,13 @@ ClientSimulation::ClientSimulation(game::World& a_World, GameHistory&& a_History
ClientSimulation::ClientSimulation(game::World& a_World, std::uint64_t a_StepTime) :
m_StepTime(a_StepTime),
m_World(a_World),
m_History(std::numeric_limits<std::uint16_t>::max()),
m_History(std::numeric_limits<StepTime>::max()),
m_CurrentTime(0),
m_LastTime(GetTime()),
m_CurrentStep(0),
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
m_LastValidStep(0) {
Step();
// Step();
}
float ClientSimulation::Update() {
@@ -45,7 +47,7 @@ float ClientSimulation::Update() {
m_LastTime = GetTime();
if (m_CurrentTime > m_StepTime) {
Step();
m_CurrentTime -= m_StepTime;
m_CurrentTime %= m_StepTime;
}
return (float)m_CurrentTime / (float)m_StepTime;
}
@@ -69,25 +71,25 @@ void ClientSimulation::Handle(const protocol::packets::LockStepsPacket& a_LockSt
FastReplay();
}
void ClientSimulation::Handle(const protocol::packets::PredictCommandPacket& a_Predict) {
}
void ClientSimulation::Handle(const protocol::packets::PredictCommandPacket& a_Predict) {}
void ClientSimulation::FastForward(std::size_t a_Count) {
for (std::size_t i = 0; i < a_Count; i++) {
Step();
}
std::cout << "Was behind " << a_Count << " ticks !\n";
}
void ClientSimulation::FastReplay() {
if (m_LastValidStep >= m_CurrentStep)
if (m_LastValidStep + 1 >= m_CurrentStep)
return;
m_World.ResetSnapshots(m_LastSnapshot, m_LastSnapshot);
// TODO: cover holes
const std::size_t stepCount = m_CurrentStep - m_LastValidStep;
m_CurrentStep = m_LastValidStep;
FastForward(stepCount);
}

View File

@@ -1,36 +0,0 @@
#include <td/simulation/GameHistory.h>
namespace td {
namespace game {
GameHistory::GameHistory() : m_History(std::numeric_limits<HistorySizeType>::max()) {}
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();
}
void GameHistory::FromPacket(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));
}
}
protocol::packets::LockStepsPacket 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

@@ -4,20 +4,24 @@ namespace td {
namespace sim {
ServerSimulation::ServerSimulation(game::World& a_World, std::uint64_t a_StepTime) :
m_World(a_World), m_StepTime(a_StepTime), m_CurrentTime(0) {}
m_World(a_World), m_StepTime(a_StepTime), m_CurrentTime(0), m_History(std::numeric_limits<StepTime>::max()) {}
protocol::packets::LockStepsPacket ServerSimulation::Update() {
std::lock_guard<std::mutex> lock(m_Mutex);
m_World.Tick(m_History[m_CurrentTime], FpFloat(m_StepTime) / FpFloat(1000));
m_CurrentTime++;
return MakePacket();
}
protocol::packets::LockStepsPacket ServerSimulation::MakePacket() {
std::array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> nextSteps;
std::copy(m_History.begin() + m_CurrentTime, m_History.begin() + m_CurrentTime + nextSteps.size(), nextSteps.begin());
return {m_CurrentTime, std::move(nextSteps)};
}
template<typename T>
template <typename T>
void AddToCommandHistory(protocol::LockStep& a_LockStep, const T& a_Cmd) {
a_LockStep.push_back({std::make_shared<T>(a_Cmd)});
}