Skip to content

Commit

Permalink
tweak(gamestate/server): Apply routing bucket changes to child attach…
Browse files Browse the repository at this point in the history
…ments

This applies routing bucket changes to child attachments of the entity that got its bucket changed.
  • Loading branch information
tens0rfl0w committed Oct 7, 2024
1 parent aaab438 commit d327b58
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,78 @@ namespace fx
void DisownEntityScript(const fx::sync::SyncEntityPtr& entity);
}

namespace
{
bool IsAttachedToRootEntity(const fx::sync::SyncEntityPtr& entity,
const fx::sync::SyncEntityPtr& rootEntity,
const fwRefContainer<fx::ServerGameState>& gameState,
std::unordered_set<uint32_t>& visitedEntities,
std::unordered_set<uint32_t>& rootConnectedEntities)
{
if (rootConnectedEntities.find(entity->handle) != rootConnectedEntities.end())
{
return true;
}

if (visitedEntities.find(entity->handle) != visitedEntities.end())
{
return false;
}

visitedEntities.insert(entity->handle);

if (entity->syncTree)
{
const fx::sync::CBaseAttachNodeData* attachment = entity->syncTree->GetAttachment();

if (attachment && attachment->attached && attachment->attachedTo)
{
const auto parentEntity = gameState->GetEntity(0, attachment->attachedTo);

if (parentEntity)
{
if (parentEntity == rootEntity || IsAttachedToRootEntity(parentEntity, rootEntity, gameState, visitedEntities, rootConnectedEntities))
{
if (entity->type == fx::sync::NetObjEntityType::Player)
{
if (const auto client = entity->GetClient())
{
auto [lock, clientData] = gameState->ExternalGetClientData(client);
gameState->ClearClientFromWorldGrid(client);
clientData->routingBucket = parentEntity->routingBucket;
}
}

entity->routingBucket = parentEntity->routingBucket;

rootConnectedEntities.insert(entity->handle);

return true;
}
}
}
}

return false;
}

void SetRoutingBucketForAttachedEntities(const fx::sync::SyncEntityPtr& rootEntity)
{
const auto resourceManager = fx::ResourceManager::GetCurrent();
const auto instance = resourceManager->GetComponent<fx::ServerInstanceBaseRef>()->Get();
const auto gameState = instance->GetComponent<fx::ServerGameState>();

std::unordered_set<uint32_t> visitedEntities;
std::unordered_set<uint32_t> rootConnectedEntities;

std::shared_lock lock(gameState->m_entityListMutex);
for (auto& entity : gameState->m_entityList)
{
IsAttachedToRootEntity(entity, rootEntity, gameState, visitedEntities, rootConnectedEntities);
}
}
}

static void Init()
{
auto makeEntityFunction = [](auto fn, uintptr_t defaultValue = 0)
Expand Down Expand Up @@ -1550,6 +1622,7 @@ static void Init()
if (playerEntity)
{
playerEntity->routingBucket = bucket;
SetRoutingBucketForAttachedEntities(playerEntity);
}
}
}
Expand All @@ -1571,6 +1644,7 @@ static void Init()
if (bucket >= 0)
{
entity->routingBucket = bucket;
SetRoutingBucketForAttachedEntities(entity);
}
}

Expand Down

0 comments on commit d327b58

Please sign in to comment.