begin serversimulation
This commit is contained in:
14
src/main.cpp
14
src/main.cpp
@@ -1,14 +1,14 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
#include <fstream>
|
||||
#include <td/game/World.h>
|
||||
#include <td/input/Display.h>
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
#include <td/render/renderer/EntityRenderer.h>
|
||||
#include <td/render/renderer/TowerRenderer.h>
|
||||
#include <td/render/renderer/WorldRenderer.h>
|
||||
#include <td/simulation/RealTimeSimulation.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/extensions/Compress.h>
|
||||
@@ -59,8 +59,8 @@ td::game::World GetWorld() {
|
||||
WorldApply wa(w);
|
||||
|
||||
td::protocol::PacketDispatcher d;
|
||||
d.RegisterHandler(td::protocol::PacketID::WorldData, &wa);
|
||||
d.RegisterHandler(td::protocol::PacketID::WorldHeader, &wa);
|
||||
d.RegisterHandler(td::protocol::PacketID::WorldData, &wa);
|
||||
|
||||
d.Dispatch(*header);
|
||||
d.Dispatch(*data);
|
||||
@@ -83,7 +83,7 @@ td::sim::GameHistory GetCustomHistory() {
|
||||
td::sim::GameHistory gh(MAX_COUNT);
|
||||
|
||||
auto spawn = td::protocol::CommandPtr(
|
||||
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
|
||||
std::make_shared<td::protocol::commands::SpawnTroopCommand>(td::EntityType::Zombie, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
|
||||
gh[0].push_back(spawn);
|
||||
|
||||
auto tower = td::protocol::CommandPtr(
|
||||
@@ -114,17 +114,17 @@ int main(int argc, char** argv) {
|
||||
cam.SetCamPos({77, 7, 13});
|
||||
cam.UpdatePerspective(display.GetAspectRatio());
|
||||
|
||||
td::sim::RealTimeSimulation simulation(w, 50);
|
||||
td::sim::ClientSimulation simulation(w, 50);
|
||||
|
||||
display.OnKeyDown.Connect([&simulation](SDL_Keycode key) {
|
||||
static int counter = 0;
|
||||
if (key == SDLK_A) {
|
||||
auto spawn = td::protocol::CommandPtr(
|
||||
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
|
||||
std::make_shared<td::protocol::commands::SpawnTroopCommand>(td::EntityType::Zombie, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
|
||||
std::array<td::protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps{};
|
||||
steps[0].push_back(spawn);
|
||||
td::protocol::packets::LockStepsPacket packet{counter * LOCKSTEP_BUFFER_SIZE * 3, steps};
|
||||
simulation.HandlePacket(packet);
|
||||
simulation.Handle(packet);
|
||||
counter++;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
#include <td/simulation/RealTimeSimulation.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
const protocol::LockStep RealTimeSimulation::EMPTY_LOCKSTEP;
|
||||
const protocol::LockStep ClientSimulation::EMPTY_LOCKSTEP;
|
||||
|
||||
std::uint64_t GetTime() {
|
||||
return static_cast<std::uint64_t>(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
|
||||
}
|
||||
|
||||
RealTimeSimulation::RealTimeSimulation(game::World& a_World, GameHistory&& a_History, std::uint64_t a_StepTime) :
|
||||
ClientSimulation::ClientSimulation(game::World& a_World, GameHistory&& a_History, std::uint64_t a_StepTime) :
|
||||
m_StepTime(a_StepTime),
|
||||
m_World(a_World),
|
||||
m_CurrentTime(0),
|
||||
@@ -27,7 +27,7 @@ RealTimeSimulation::RealTimeSimulation(game::World& a_World, GameHistory&& a_His
|
||||
Step();
|
||||
}
|
||||
|
||||
RealTimeSimulation::RealTimeSimulation(game::World& a_World, std::uint64_t a_StepTime) :
|
||||
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()),
|
||||
@@ -39,7 +39,7 @@ RealTimeSimulation::RealTimeSimulation(game::World& a_World, std::uint64_t a_Ste
|
||||
Step();
|
||||
}
|
||||
|
||||
float RealTimeSimulation::Update() {
|
||||
float ClientSimulation::Update() {
|
||||
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
||||
m_CurrentTime += GetTime() - m_LastTime;
|
||||
m_LastTime = GetTime();
|
||||
@@ -50,7 +50,7 @@ float RealTimeSimulation::Update() {
|
||||
return (float)m_CurrentTime / (float)m_StepTime;
|
||||
}
|
||||
|
||||
void RealTimeSimulation::Step() {
|
||||
void ClientSimulation::Step() {
|
||||
const auto& step = m_History[m_CurrentStep];
|
||||
if (step.has_value()) {
|
||||
m_LastSnapshot = m_World.Tick(step.value(), FpFloat(m_StepTime) / FpFloat(1000));
|
||||
@@ -61,7 +61,7 @@ void RealTimeSimulation::Step() {
|
||||
m_CurrentStep++;
|
||||
}
|
||||
|
||||
void RealTimeSimulation::HandlePacket(const protocol::packets::LockStepsPacket& a_LockSteps) {
|
||||
void ClientSimulation::Handle(const protocol::packets::LockStepsPacket& a_LockSteps) {
|
||||
const auto& steps = a_LockSteps->m_LockSteps;
|
||||
for (std::size_t i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
|
||||
m_History[a_LockSteps->m_FirstFrameNumber + i] = steps[i];
|
||||
@@ -69,15 +69,17 @@ void RealTimeSimulation::HandlePacket(const protocol::packets::LockStepsPacket&
|
||||
FastReplay();
|
||||
}
|
||||
|
||||
void RealTimeSimulation::HandlePacket(const protocol::packets::PredictCommandPacket& a_Predict) {}
|
||||
void ClientSimulation::Handle(const protocol::packets::PredictCommandPacket& a_Predict) {
|
||||
|
||||
}
|
||||
|
||||
void RealTimeSimulation::FastForward(std::size_t a_Count) {
|
||||
void ClientSimulation::FastForward(std::size_t a_Count) {
|
||||
for (std::size_t i = 0; i < a_Count; i++) {
|
||||
Step();
|
||||
}
|
||||
}
|
||||
|
||||
void RealTimeSimulation::FastReplay() {
|
||||
void ClientSimulation::FastReplay() {
|
||||
if (m_LastValidStep >= m_CurrentStep)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user