From a61a9b164403ae0b88ac2d2d6e4f978f795b6282 Mon Sep 17 00:00:00 2001 From: Dillon Skaggs Date: Wed, 30 Oct 2024 13:01:00 -0500 Subject: [PATCH] fix(server/state): properly reassign entities that are set to `EntityOrphanMode::KeepEntity` - this swaps `MoveEntityToCanidate` to use `ShouldServerKeepEntity` instead of having specific logic for orphan mode - makes 'entitiesToDestroy' use `ShouldServerKeepEntity` instead of `IsOwnedByServerScript`so we reassign if orphan mode is set to keep the entity --- .../include/state/ServerGameState.h | 8 ++++++++ .../citizen-server-impl/src/state/ServerGameState.cpp | 10 ++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/code/components/citizen-server-impl/include/state/ServerGameState.h b/code/components/citizen-server-impl/include/state/ServerGameState.h index 7516297ded..9c243ea1f7 100644 --- a/code/components/citizen-server-impl/include/state/ServerGameState.h +++ b/code/components/citizen-server-impl/include/state/ServerGameState.h @@ -962,6 +962,14 @@ struct SyncEntityState } return scriptHash; } + + /// + /// Checks of the entity is set to be kept by the server via orphan mode or by being owned by a server script. + /// + inline bool ShouldServerKeepEntity() + { + return IsOwnedByServerScript() || orphanMode == EntityOrphanMode::KeepEntity; + } inline bool IsOwnedByScript() { diff --git a/code/components/citizen-server-impl/src/state/ServerGameState.cpp b/code/components/citizen-server-impl/src/state/ServerGameState.cpp index 0c4b9cfaf0..84d7b17c74 100644 --- a/code/components/citizen-server-impl/src/state/ServerGameState.cpp +++ b/code/components/citizen-server-impl/src/state/ServerGameState.cpp @@ -1515,7 +1515,7 @@ void ServerGameState::Tick(fx::ServerInstanceBase* instance) if (entityClient && entityClient->GetNetId() == client->GetNetId()) { // if this entity is owned by a server script, reassign to nobody and wait until someone else owns it - if (entity->IsOwnedByServerScript()) + if (entity->ShouldServerKeepEntity()) { ReassignEntity(entity->handle, {}); } @@ -2812,10 +2812,9 @@ bool ServerGameState::MoveEntityToCandidate(const fx::sync::SyncEntityPtr& entit if (candidates.empty()) // no candidate? { - GS_LOG("no candidates for entity %d, assigning as unowned\n", entity->handle); - - if (entity->IsOwnedByServerScript()) + if (entity->ShouldServerKeepEntity()) { + GS_LOG("no candidates for entity %d, assigning as unowned\n", entity->handle); ReassignEntity(entity->handle, {}); } else @@ -2924,8 +2923,7 @@ void ServerGameState::HandleClientDrop(const fx::ClientSharedPtr& client, uint16 { ReassignEntity(entity->handle, firstOwner); } - // we don't want to add these to the list to remove if they're set to be kept when orphaned - else if (entity->orphanMode != sync::KeepEntity) + else { toErase.insert(entity->handle); }