less serialize code

This commit is contained in:
2025-08-01 13:21:31 +02:00
parent fa663d0481
commit ced20ca991
15 changed files with 38 additions and 30 deletions

View File

@@ -82,10 +82,12 @@ td::sim::GameHistory GetCustomHistory() {
td::sim::GameHistory gh(MAX_COUNT);
auto spawn = std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0);
auto spawn = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
gh[0].push_back(spawn);
auto tower = std::make_shared<td::protocol::commands::PlaceTowerCommand>(td::TowerType::Archer, 0, td::TowerCoords{77, 13});
auto tower = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::PlaceTowerCommand>(td::TowerType::Archer, 0, td::TowerCoords{77, 13}));
gh[0].push_back(tower);
return gh;
@@ -116,9 +118,9 @@ int main(int argc, char** argv) {
display.OnKeyDown.Connect([&simulation](SDL_Keycode key) {
if (key == SDLK_A) {
auto spawn =
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0);
td::Array<td::protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps{};
auto spawn = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 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{0, steps};
simulation.HandlePacket(packet);

View File

@@ -2,6 +2,7 @@
#include <sp/common/DataBuffer.h>
#include <sp/common/ByteSwapping.h>
#include <sp/common/DataBufferOperators.h>
namespace td {

View File

@@ -3,17 +3,7 @@
namespace td {
namespace game {
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const ChunkPtr& a_Chunk) {
a_Buffer << a_Chunk->m_Palette;
a_Buffer << a_Chunk->m_Data;
return a_Buffer;
}
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, ChunkPtr& a_Chunk) {
a_Chunk = std::make_shared<td::game::Chunk>();
a_Buffer >> a_Chunk->m_Palette;
return a_Buffer >> a_Chunk->m_Data;
}
} // namespace game
} // namespace td

View File

@@ -1,6 +1,8 @@
#include <td/protocol/packet/PacketData.h>
#include <td/protocol/packet/PacketSerialize.h>
#include <sp/common/DataBufferOperators.h>
namespace td {
namespace game {
@@ -15,5 +17,17 @@ sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle) {
return a_Buffer;
}
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Spawn& a_Spawn) {
return a_Buffer << a_Spawn.GetCenterX() << a_Spawn.GetCenterY();
}
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Spawn& a_Spawn) {
float x, y;
a_Buffer >> x >> y;
a_Spawn.SetCenter({x, y});
return a_Buffer;
}
} // namespace game
} // namespace td

View File

@@ -27,7 +27,7 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) {
td::game::TileIndex tileIndex = chunk->GetTileIndex(tileNumber);
td::game::TilePtr tile = world->GetTilePtr(tileIndex);
if (tile == nullptr)
if (!tile)
continue;
positions.insert(

View File

@@ -33,7 +33,7 @@ void GameHistory::FromPacket(protocol::pdata::LockSteps&& a_Steps) {
}
protocol::packets::LockStepsPacket GameHistory::ToPacket(HistorySizeType a_StartIndex) {
Array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps;
std::array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps;
for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
steps[i] = GetLockStep(a_StartIndex + i);
}

View File

@@ -12,7 +12,7 @@ std::uint64_t GetTime() {
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
}
RealTimeSimulation::RealTimeSimulation(game::World& a_World, const GameHistory& a_History, std::uint64_t a_StepTime) :
RealTimeSimulation::RealTimeSimulation(game::World& a_World, GameHistory&& a_History, std::uint64_t a_StepTime) :
m_StepTime(a_StepTime),
m_World(a_World),
m_CurrentTime(0),
@@ -21,8 +21,8 @@ RealTimeSimulation::RealTimeSimulation(game::World& a_World, const GameHistory&
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
m_LastValidStep(0) {
m_History.reserve(a_History.size());
for (const auto& lockstep : a_History) {
m_History.emplace_back(lockstep);
for (auto&& lockstep : a_History) {
m_History.emplace_back(std::move(lockstep));
}
Step();
}