Skip to content

Commit e99c433

Browse files
committed
Handle ship piloting server-side to handle player death
1 parent 11f473f commit e99c433

14 files changed

Lines changed: 101 additions & 15 deletions

File tree

include/ClientLib/ClientSessionHandler.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ namespace tsom
6868
void HandlePacket(Packets::S_EnvironmentsUpdateOwner&& envOwnerUpdate);
6969
void HandlePacket(Packets::S_GameData&& gameData);
7070
void HandlePacket(Packets::S_NetworkStrings&& networkStrings);
71+
void HandlePacket(Packets::S_PilotShip&& pilotShip);
72+
void HandlePacket(Packets::S_PilotShipFinish&& pilotShipFinish);
7173
void HandlePacket(Packets::S_PlayerJoin&& playerJoin);
7274
void HandlePacket(Packets::S_PlayerLeave&& playerLeave);
7375
void HandlePacket(Packets::S_PlayerNameUpdate&& playerNameUpdate);

include/CommonLib/Protocol/PacketList.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ TSOM_NETWORK_PACKET(S_EnvironmentDestroy)
4646
TSOM_NETWORK_PACKET(S_EnvironmentsUpdateOwner)
4747
TSOM_NETWORK_PACKET(S_GameData)
4848
TSOM_NETWORK_PACKET(S_NetworkStrings)
49+
TSOM_NETWORK_PACKET(S_PilotShip)
50+
TSOM_NETWORK_PACKET(S_PilotShipFinish)
4951
TSOM_NETWORK_PACKET(S_PlayerJoin)
5052
TSOM_NETWORK_PACKET(S_PlayerLeave)
5153
TSOM_NETWORK_PACKET_LAST(S_PlayerNameUpdate)

include/CommonLib/Protocol/Packets.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ namespace tsom
332332
std::vector<SecuredString<1024>> strings;
333333
};
334334

335+
struct S_PilotShip
336+
{
337+
Helper::EntityId shipEntity;
338+
};
339+
340+
struct S_PilotShipFinish
341+
{
342+
};
343+
335344
struct S_PlayerLeave
336345
{
337346
PlayerIndex index;
@@ -378,6 +387,8 @@ namespace tsom
378387
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_EnvironmentsUpdateOwner& data);
379388
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_GameData& data);
380389
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_NetworkStrings& data);
390+
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_PilotShip& data);
391+
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_PilotShipFinish& data);
381392
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_PlayerJoin& data);
382393
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_PlayerLeave& data);
383394
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, S_PlayerNameUpdate& data);

include/ServerLib/ServerPlayer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ namespace tsom
4848

4949
void ExecuteConsoleCommand(std::string_view command);
5050

51+
void ExitPiloting();
52+
5153
inline const std::shared_ptr<CharacterController>& GetCharacterController();
5254
inline EntityReference& GetControlledEntityReference();
5355
inline const EntityReference& GetControlledEntityReference() const;
@@ -72,6 +74,8 @@ namespace tsom
7274

7375
inline bool IsInEnvironment(const ServerEnvironment* environment);
7476

77+
void PilotShip(EntityReference shipEntity, const Nz::Quaternionf& rotation);
78+
7579
void PushInputs(const PlayerInputs& inputs);
7680

7781
void RemoveFromEnvironment(ServerEnvironment* environment);

include/ServerLib/SessionVisibilityHandler.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ namespace tsom
5151
inline bool GetEntityByNetworkId(Packets::Helper::EntityId networkId, entt::handle* entity) const;
5252
inline Packets::Helper::EnvironmentId GetEnvironmentId(ServerEnvironment* environment) const;
5353

54+
inline void SetControlledShip(entt::handle entity);
55+
5456
inline void TriggerEntityRpc(entt::handle entity, Nz::UInt32 rpcIndex);
5557

5658
inline void UpdateControlledEntity(entt::handle entity, CharacterController* controller);
@@ -84,8 +86,8 @@ namespace tsom
8486
void DispatchEntitiesProperties(Nz::UInt16 tickIndex);
8587
void DispatchEntitiesRpcs(Nz::UInt16 tickIndex);
8688
void DispatchEntitiesStates(Nz::UInt16 tickIndex);
87-
8889
void DispatchEnvironments(Nz::UInt16 tickIndex);
90+
8991
void HandleEntityCreation(std::vector<Packets::Helper::EntityData>& entities, entt::handle entity, CreateEntityData&& createEntityData);
9092
void HandleEntityDestruction(entt::handle entity);
9193

@@ -168,6 +170,7 @@ namespace tsom
168170
tsl::hopscotch_map<const ServerEnvironment*, EnvironmentId> m_environmentIndices;
169171
tsl::hopscotch_set<entt::handle, HandlerHasher> m_deletedEntities;
170172
tsl::hopscotch_set<entt::handle, HandlerHasher> m_movingEntities;
173+
std::optional<entt::handle> m_nextPilotedShip;
171174
std::shared_ptr<std::size_t> m_activeChunkUpdates;
172175
std::vector<ServerEnvironment*> m_destroyedEnvironments;
173176
std::vector<ChunkData> m_visibleChunks;

include/ServerLib/SessionVisibilityHandler.inl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ namespace tsom
5151
return Nz::Retrieve(m_environmentIndices, environment);
5252
}
5353

54+
inline void SessionVisibilityHandler::SetControlledShip(entt::handle entity)
55+
{
56+
m_nextPilotedShip = entity;
57+
}
58+
5459
inline void SessionVisibilityHandler::TriggerEntityRpc(entt::handle entity, Nz::UInt32 rpcIndex)
5560
{
5661
m_triggeredEntitiesRpc[entity].push_back(rpcIndex);

scripts/entities/computer.lua

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
local classData = EntityRegistry.ClassBuilder()
22

3-
-- TODO: Replace by RPC
4-
classData:AddProperty("active", { type = "bool", default = false, isNetworked = true })
5-
6-
classData:AddClientRPC("activate")
7-
8-
if CLIENT then
9-
classData:OnClientRPC("activate", function (self)
10-
ClientSession.EnableShipControl(true)
11-
end)
12-
end
13-
143
classData:On("init", function (self)
154
local physSettings = {
165
kind = "static",
@@ -38,8 +27,7 @@ if SERVER then
3827
local computerNode = self:GetComponent("node")
3928
local exteriorEntity = self:GetEnvironment():GetExteriorShipEntity()
4029

41-
player:GetController():SetShipController(ShipController.new(exteriorEntity, computerNode:GetRotation()))
42-
self:CallClientRPC("activate", player)
30+
player:PilotShip(exteriorEntity, computerNode:GetRotation())
4331
end)
4432
end
4533

src/ClientLib/ClientSessionHandler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,16 @@ namespace tsom
408408
GetSession()->GetStringStore().FillStore(networkStrings.startId, std::move(networkStrings.strings));
409409
}
410410

411+
void ClientSessionHandler::HandlePacket(Packets::S_PilotShip&& pilotShip)
412+
{
413+
OnShipControlUpdated(true);
414+
}
415+
416+
void ClientSessionHandler::HandlePacket(Packets::S_PilotShipFinish&& pilotShipFinish)
417+
{
418+
OnShipControlUpdated(false);
419+
}
420+
411421
void ClientSessionHandler::HandlePacket(Packets::S_PlayerJoin&& playerJoin)
412422
{
413423
if (playerJoin.index >= m_players.size())

src/CommonLib/Protocol/Packets.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,5 +430,14 @@ namespace tsom
430430
serializer &= data.index;
431431
serializer &= data.newNickname;
432432
}
433+
434+
void Serialize(PacketSerializer& serializer, S_PilotShip& data)
435+
{
436+
serializer &= data.shipEntity;
437+
}
438+
439+
void Serialize(PacketSerializer& serializer, S_PilotShipFinish& data)
440+
{
441+
}
433442
}
434443
}

src/ServerLib/Entities/ServerEntityClassLibrary.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ namespace tsom
5858
if (*healthValue == 0)
5959
{
6060
playerInstance.TriggerClientRpc(deathRpc, nullptr);
61+
62+
// FIXME: Make this automatic
63+
auto& playerControlled = playerEntity.get<ServerPlayerControlledComponent>();
64+
playerControlled.GetPlayer()->ExitPiloting();
65+
6166
playerEntity.destroy();
6267
}
6368
}

0 commit comments

Comments
 (0)