begin client-server
This commit is contained in:
@@ -14,6 +14,7 @@ class IServerSocket {
|
||||
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
|
||||
|
||||
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
|
||||
void Broadcast(const protocol::PacketBase& a_Packet);
|
||||
|
||||
IServerSocket() {}
|
||||
virtual ~IServerSocket() {}
|
||||
|
||||
@@ -9,10 +9,6 @@ namespace server {
|
||||
class Server;
|
||||
|
||||
class IServerState : private utils::SlotGuard {
|
||||
protected:
|
||||
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
|
||||
void SetNewState(const std::shared_ptr<IServerState>& a_NewState);
|
||||
|
||||
public:
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
@@ -22,6 +18,12 @@ class IServerState : private utils::SlotGuard {
|
||||
IServerState();
|
||||
virtual ~IServerState();
|
||||
|
||||
protected:
|
||||
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
|
||||
void BroadcastPacket(const protocol::PacketBase& a_Packet);
|
||||
void SetNewState(const std::shared_ptr<IServerState>& a_NewState);
|
||||
virtual void Init() {}
|
||||
|
||||
private:
|
||||
Server* m_Server;
|
||||
|
||||
|
||||
@@ -35,6 +35,14 @@ class PlayerIds {
|
||||
m_PeerToPlayer.erase(a_PeerId);
|
||||
m_PlayerToPeer.erase(playerId);
|
||||
}
|
||||
|
||||
auto begin() {
|
||||
return m_PeerToPlayer.begin();
|
||||
}
|
||||
|
||||
auto end() {
|
||||
return m_PeerToPlayer.end();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <memory>
|
||||
#include <server/IServerSocket.h>
|
||||
#include <server/IServerState.h>
|
||||
#include <chrono>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
@@ -11,19 +12,23 @@ class Server {
|
||||
private:
|
||||
std::shared_ptr<IServerSocket> m_Socket;
|
||||
std::shared_ptr<IServerState> m_State;
|
||||
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
|
||||
|
||||
public:
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket), m_LastTime(std::chrono::system_clock::now()) {}
|
||||
|
||||
void Update(float a_Delta) {
|
||||
m_State->Update(a_Delta);
|
||||
}
|
||||
void Update();
|
||||
|
||||
void UpdateState(const std::shared_ptr<IServerState>& a_State) {
|
||||
m_State = a_State;
|
||||
m_State->SetServer(this);
|
||||
}
|
||||
|
||||
private:
|
||||
void Update(float a_Delta) {
|
||||
m_State->Update(a_Delta);
|
||||
}
|
||||
|
||||
friend class IServerState;
|
||||
};
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@ class FakeSocket : public IServerSocket {
|
||||
|
||||
utils::Signal<PeerID, const protocol::PacketBase&> OnSend;
|
||||
|
||||
void ConnectFakePeer(PeerID a_Peer);
|
||||
void DisconnectFakePeer(PeerID a_Peer);
|
||||
|
||||
protected:
|
||||
virtual void SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) override;
|
||||
};
|
||||
|
||||
@@ -1,21 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/IServerState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ServerSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class GameState : public IServerState{
|
||||
class GameStateHandler;
|
||||
|
||||
class GameState : public IServerState {
|
||||
private:
|
||||
/* data */
|
||||
std::shared_ptr<game::World> m_World;
|
||||
sim::ServerSimulation m_Simulation;
|
||||
float m_Time;
|
||||
|
||||
public:
|
||||
GameState(/* args */) {}
|
||||
GameState(const std::shared_ptr<game::World>& a_World);
|
||||
~GameState() {}
|
||||
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
virtual void OnPlayerJoin(PlayerID a_Id) override;
|
||||
virtual void OnPlayerLeave(PlayerID a_Id) override;
|
||||
|
||||
protected:
|
||||
virtual void Init() override;
|
||||
|
||||
friend class GameStateHandler;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
||||
25
include/server/state/GameStateHandler.h
Normal file
25
include/server/state/GameStateHandler.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class GameState;
|
||||
|
||||
class GameStateHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
GameState& m_GameState;
|
||||
PlayerID m_PlayerId;
|
||||
|
||||
using protocol::PacketHandler::Handle;
|
||||
|
||||
public:
|
||||
GameStateHandler(GameState& a_GameState, PlayerID a_PlayerId);
|
||||
|
||||
virtual void Handle(const protocol::packets::SpawnTroopPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::PlaceTowerPacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -5,11 +5,12 @@
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
// this class is temporary useless
|
||||
class LobbyState : public IServerState {
|
||||
private:
|
||||
/* data */
|
||||
std::shared_ptr<game::World> m_World;
|
||||
public:
|
||||
LobbyState(/* args */) {}
|
||||
LobbyState(const std::shared_ptr<game::World>& a_World) : m_World(a_World) {}
|
||||
~LobbyState() {}
|
||||
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <sp/common/DataBufferOperators.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
@@ -183,6 +183,35 @@ T Lerp(T v0, T v1, T t) {
|
||||
|
||||
} // namespace maths
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec2<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec2<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec3<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y << a_Vec.z;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec3<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y >> a_Vec.z;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec4<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y << a_Vec.z << a_Vec.w;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec4<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y >> a_Vec.z >> a_Vec.w;
|
||||
}
|
||||
|
||||
} // namespace td
|
||||
|
||||
@@ -12,6 +12,10 @@ namespace td {
|
||||
|
||||
using FpFloat = fpm::fixed_16_16;
|
||||
|
||||
using StepTime = std::uint16_t;
|
||||
|
||||
constexpr int STEP_TIME = 50;
|
||||
|
||||
enum class TeamColor : std::int8_t {
|
||||
None = -1,
|
||||
Blue,
|
||||
@@ -61,15 +65,11 @@ using TowerID = std::uint16_t;
|
||||
using PlayerID = std::uint8_t;
|
||||
using EntityID = std::uint16_t;
|
||||
|
||||
struct TowerCoords {
|
||||
std::int16_t x;
|
||||
std::int16_t y;
|
||||
};
|
||||
using TowerCoords = Vec2<std::int16_t>;
|
||||
|
||||
using EntityCoords = Vec2<FpFloat>;
|
||||
|
||||
using PeerID = std::uint16_t;
|
||||
using StepsType = std::uint16_t;
|
||||
|
||||
enum class Direction : std::uint8_t {
|
||||
PositiveX = 1 << 0,
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
#include <td/game/WorldTypes.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class World {
|
||||
protected:
|
||||
// header
|
||||
TowerTileColorPalette m_TowerPlacePalette;
|
||||
Color m_WalkablePalette;
|
||||
std::vector<Color> m_DecorationPalette;
|
||||
Color m_Background;
|
||||
|
||||
ChunkList m_Chunks;
|
||||
|
||||
SpawnColorPalette m_SpawnColorPalette;
|
||||
|
||||
TilePalette m_TilePalette;
|
||||
|
||||
//data
|
||||
ChunkList m_Chunks;
|
||||
|
||||
std::shared_ptr<sim::WorldSnapshot> m_CurrentState;
|
||||
std::shared_ptr<sim::WorldSnapshot> m_NextState;
|
||||
|
||||
@@ -28,6 +28,7 @@ class World {
|
||||
|
||||
public:
|
||||
World();
|
||||
World(World&&) = default;
|
||||
|
||||
bool LoadMap(const protocol::pdata::WorldHeader& worldHeader);
|
||||
bool LoadMap(const protocol::pdata::WorldData& worldData);
|
||||
@@ -130,8 +131,9 @@ class World {
|
||||
private:
|
||||
void TickMobs(std::uint64_t delta);
|
||||
void CleanDeadMobs();
|
||||
|
||||
};
|
||||
|
||||
using WorldPtr = std::shared_ptr<World>;
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
@@ -7,7 +7,11 @@
|
||||
#include <td/game/WorldTypes.h>
|
||||
|
||||
// Make it dynamic ?
|
||||
#ifdef NDEBUG
|
||||
#define LOCKSTEP_BUFFER_SIZE 10
|
||||
#else
|
||||
#define LOCKSTEP_BUFFER_SIZE 1
|
||||
#endif
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
@@ -45,7 +49,7 @@ struct PlayerLeave {
|
||||
|
||||
struct PredictCommand {
|
||||
CommandPtr m_Command;
|
||||
std::uint16_t m_FrameNumber;
|
||||
StepTime m_FrameNumber;
|
||||
};
|
||||
|
||||
/** Keep alive */
|
||||
@@ -73,7 +77,7 @@ struct BeginGame {
|
||||
};
|
||||
|
||||
struct LockSteps {
|
||||
std::uint16_t m_FirstFrameNumber;
|
||||
StepTime m_FirstFrameNumber;
|
||||
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
||||
};
|
||||
|
||||
@@ -95,6 +99,7 @@ struct WorldData {
|
||||
game::ChunkList m_Chunks;
|
||||
};
|
||||
|
||||
// TODO: spawn multiple troops at the same time
|
||||
struct SpawnTroop {
|
||||
sp::BitField<EntityType, 5> m_Type;
|
||||
sp::BitField<std::uint8_t, 3> m_Level;
|
||||
|
||||
@@ -4,36 +4,7 @@
|
||||
|
||||
namespace td {
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec2<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec2<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec3<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y << a_Vec.z;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec3<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y >> a_Vec.z;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec4<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y << a_Vec.z << a_Vec.w;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec4<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y >> a_Vec.z >> a_Vec.w;
|
||||
}
|
||||
|
||||
namespace game {
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <td/protocol/command/Commands.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class GameHistory {
|
||||
private:
|
||||
using HistorySizeType = StepsType;
|
||||
|
||||
std::vector<std::optional<protocol::LockStep>> m_History;
|
||||
|
||||
public:
|
||||
GameHistory();
|
||||
|
||||
void SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep);
|
||||
|
||||
const protocol::LockStep& GetLockStep(HistorySizeType a_Index) const;
|
||||
|
||||
bool HasLockStep(HistorySizeType a_Index) const;
|
||||
|
||||
void FromPacket(td::protocol::pdata::LockSteps&& a_Steps);
|
||||
|
||||
td::protocol::packets::LockStepsPacket ToPacket(HistorySizeType a_StartIndex);
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
@@ -25,6 +25,8 @@ class ServerSimulation : public protocol::CommandHandler {
|
||||
|
||||
protocol::packets::LockStepsPacket Update();
|
||||
|
||||
protocol::packets::LockStepsPacket MakePacket();
|
||||
|
||||
// no checks are done !
|
||||
|
||||
virtual void Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) override;
|
||||
|
||||
Reference in New Issue
Block a user