serversimulation
This commit is contained in:
@@ -1,21 +1,34 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace sim {
|
namespace sim {
|
||||||
|
|
||||||
// TODO: OnEnd signal
|
// TODO: OnEnd signal
|
||||||
class ServerSimulation {
|
/**
|
||||||
|
* \note This class is thread safe
|
||||||
|
*/
|
||||||
|
class ServerSimulation : public protocol::CommandHandler {
|
||||||
private:
|
private:
|
||||||
|
std::mutex m_Mutex;
|
||||||
game::World& m_World;
|
game::World& m_World;
|
||||||
std::uint64_t m_StepTime;
|
std::uint64_t m_StepTime;
|
||||||
std::uint64_t m_CurrentTime;
|
std::uint64_t m_CurrentTime;
|
||||||
|
std::vector<td::protocol::LockStep> m_History;
|
||||||
|
|
||||||
|
using protocol::CommandHandler::Handle;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ServerSimulation(game::World& a_World, std::uint64_t a_StepTime);
|
ServerSimulation(game::World& a_World, std::uint64_t a_StepTime);
|
||||||
|
|
||||||
void Update();
|
protocol::packets::LockStepsPacket Update();
|
||||||
|
|
||||||
|
// no checks are done !
|
||||||
|
|
||||||
|
virtual void Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) override;
|
||||||
|
virtual void Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sim
|
} // namespace sim
|
||||||
|
|||||||
36
src/td/simulation/ServerSimulation.cpp
Normal file
36
src/td/simulation/ServerSimulation.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <td/simulation/ServerSimulation.h>
|
||||||
|
|
||||||
|
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) {}
|
||||||
|
|
||||||
|
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++;
|
||||||
|
|
||||||
|
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>
|
||||||
|
void AddToCommandHistory(protocol::LockStep& a_LockStep, const T& a_Cmd) {
|
||||||
|
a_LockStep.push_back({std::make_shared<T>(a_Cmd)});
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSimulation::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
|
||||||
|
std::lock_guard<std::mutex> lock(m_Mutex);
|
||||||
|
AddToCommandHistory(m_History[m_CurrentTime + LOCKSTEP_BUFFER_SIZE], a_SpawnTroop);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSimulation::Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) {
|
||||||
|
std::lock_guard<std::mutex> lock(m_Mutex);
|
||||||
|
AddToCommandHistory(m_History[m_CurrentTime + LOCKSTEP_BUFFER_SIZE], a_PlaceTower);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sim
|
||||||
|
} // namespace td
|
||||||
Reference in New Issue
Block a user