mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 15:39:20 +02:00
multiplayer stage 2: peer identity, stable vehicle ids and a session lobby
TTrain::id() numbers cabs with a local counter, in the order each peer happens to create them, so it cannot address a vehicle across the session. Neither was there any way for a player to see what is available to drive. - network/entities: PeerId (the host is peer 1, remote peers are numbered from 2, so it is a participant rather than a special case) and NetworkEntityId, plus a registry that numbers the drivable vehicles of the loaded scenario in name order and resolves ids back to TDynamicObject - the server owns the numbering and publishes it as VEHICLE_LIST - name, crew occupancy, crew capacity, AI state, availability - on every change and at least every 120 frames, out of band so it stays out of the replay backbuffer; a client binds the received ids to its own copy of the scenario by name - SERVER_HELLO hands the joining peer its PeerId - a multiplayer lobby listing that roster, with vehicles taken by other players shown as such rather than hidden; entering one goes through the existing entervehicle command, the vehicle is never moved The lobby lives inside the driver mode instead of a mode of its own: the world has to keep running while somebody is choosing (a client stops consuming the server's frames the moment its mode is not a command processor), and the same screen is what a player needs to change vehicles later on. Crew occupancy is still derived from the presence of a cab instance; the real crew model, claim handshake and AI takeover follow in stage 3.
This commit is contained in:
@@ -33,6 +33,7 @@ void network::server_hello::serialize(std::ostream &stream) const
|
||||
sn_utils::ls_int64(stream, config);
|
||||
sn_utils::s_str(stream, scenario);
|
||||
sn_utils::s_str(stream, app_version);
|
||||
sn_utils::ls_uint32(stream, peer_id);
|
||||
}
|
||||
|
||||
void network::server_hello::deserialize(std::istream &stream)
|
||||
@@ -42,6 +43,41 @@ void network::server_hello::deserialize(std::istream &stream)
|
||||
config = sn_utils::ld_int64(stream);
|
||||
scenario = sn_utils::d_str(stream);
|
||||
app_version = sn_utils::d_str(stream);
|
||||
peer_id = sn_utils::ld_uint32(stream);
|
||||
}
|
||||
|
||||
void network::vehicle_list::serialize(std::ostream &stream) const
|
||||
{
|
||||
sn_utils::ls_uint32(stream, (uint32_t)vehicles.size());
|
||||
for (auto const &entry : vehicles)
|
||||
{
|
||||
sn_utils::ls_uint32(stream, entry.id);
|
||||
sn_utils::s_str(stream, entry.name);
|
||||
sn_utils::s_uint8(stream, entry.crew_count);
|
||||
sn_utils::s_uint8(stream, entry.crew_capacity);
|
||||
sn_utils::s_uint8(stream, (uint8_t)((entry.ai_active ? 1 : 0) | (entry.claimable ? 2 : 0)));
|
||||
}
|
||||
}
|
||||
|
||||
void network::vehicle_list::deserialize(std::istream &stream)
|
||||
{
|
||||
vehicles.clear();
|
||||
|
||||
uint32_t const count = sn_utils::ld_uint32(stream);
|
||||
vehicles.reserve(count);
|
||||
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
{
|
||||
vehicle_entry entry;
|
||||
entry.id = sn_utils::ld_uint32(stream);
|
||||
entry.name = sn_utils::d_str(stream);
|
||||
entry.crew_count = sn_utils::d_uint8(stream);
|
||||
entry.crew_capacity = sn_utils::d_uint8(stream);
|
||||
uint8_t const flags = sn_utils::d_uint8(stream);
|
||||
entry.ai_active = (flags & 1) != 0;
|
||||
entry.claimable = (flags & 2) != 0;
|
||||
vehicles.emplace_back(entry);
|
||||
}
|
||||
}
|
||||
|
||||
void ::network::request_command::serialize(std::ostream &stream) const
|
||||
@@ -131,6 +167,8 @@ std::shared_ptr<network::message> network::deserialize_message(std::istream &str
|
||||
msg = std::make_shared<request_command>();
|
||||
else if (type == message::SERVER_REJECT)
|
||||
msg = std::make_shared<server_reject>();
|
||||
else if (type == message::VEHICLE_LIST)
|
||||
msg = std::make_shared<vehicle_list>();
|
||||
|
||||
if (!msg) {
|
||||
// unknown message type; hand back a marker the peer handlers treat as a protocol error
|
||||
|
||||
Reference in New Issue
Block a user