Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync name and colors for vehicles, rocket and cyclops #2090

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix children referencing when updating an entity
  • Loading branch information
tornac1234 committed Dec 3, 2023
commit 56fac800f97fe46f1701b331b8b56d2ad4283eff
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void SetupPlayerEntity(Player player)
NitroxTransform transform = new(player.Position, player.Rotation, NitroxVector3.One);

PlayerWorldEntity playerEntity = new PlayerWorldEntity(transform, 0, null, false, player.GameObjectId, NitroxTechType.None, null, null, new List<Entity>());
entityRegistry.AddEntity(playerEntity);
entityRegistry.AddOrUpdate(playerEntity);
world.WorldEntityManager.TrackEntityInTheWorld(playerEntity);
playerManager.SendPacketToOtherPlayers(new SpawnEntities(playerEntity), player);
}
Expand Down
18 changes: 14 additions & 4 deletions NitroxServer/GameLogic/Entities/EntityRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public void AddEntity(Entity entity)
}
}

/// <summary>
/// Registers or updates an entity and its children.
/// </summary>
public void AddOrUpdate(Entity entity)
{
if (!entitiesById.TryAdd(entity.Id, entity))
Expand Down Expand Up @@ -96,11 +99,21 @@ public void AddEntities(IEnumerable<Entity> entities)
/// example a dropped InventoryEntity turns into a WorldEntity but keeps its
/// battery inside (already known).
/// </summary>
/// <remarks>
/// Updates entities if they already exist
/// </remarks>
public void AddEntitiesIgnoringDuplicate(IEnumerable<Entity> entities)
{
foreach (Entity entity in entities)
{
entitiesById.TryAdd(entity.Id, entity);
if (entitiesById.TryGetValue(entity.Id, out Entity currentEntity))
{
entitiesById.TryUpdate(entity.Id, entity, currentEntity);
}
else
{
entitiesById.TryAdd(entity.Id, entity);
}
AddEntitiesIgnoringDuplicate(entity.ChildEntities);
}
}
Expand Down Expand Up @@ -137,8 +150,6 @@ public void RemoveFromParent(Entity entity)
{
if (entity.ParentId != null && TryGetEntityById(entity.ParentId, out Entity parentEntity))
{
// TODO: Either use this solution (to remove duplicated entities also) to avoid wrongly-referenced children
// Or try fixing wrongly-referenced children (when entities are overwritten by AddOrUpdate)
parentEntity.ChildEntities.RemoveAll(childEntity => childEntity.Id.Equals(entity.Id));
entity.ParentId = null;
}
Expand Down Expand Up @@ -198,7 +209,6 @@ public void TransferChildren(NitroxId parentId, NitroxId newParentId, Func<Entit

public void TransferChildren(Entity parent, Entity newParent, Func<Entity, bool> filter = null)
{
Log.Debug($"Moving {parent.ChildEntities.Count} children from {parent.Id} to {newParent.Id}");
IEnumerable<Entity> childrenToMove = filter != null ?
parent.ChildEntities.Where(filter) : parent.ChildEntities;

Expand Down
6 changes: 2 additions & 4 deletions NitroxServer/GameLogic/Entities/WorldEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ public void AddGlobalRootEntity(GlobalRootEntity entity, bool addToRegistry = tr
{
if (addToRegistry)
{
entityRegistry.AddEntity(entity);
entityRegistry.AddEntitiesIgnoringDuplicate(entity.ChildEntities);
entityRegistry.AddOrUpdate(entity);
}
globalRootEntitiesById.Add(entity.Id, entity);
}
Expand Down Expand Up @@ -164,8 +163,7 @@ public Optional<Entity> RemoveGlobalRootEntity(NitroxId entityId, bool removeFro
childPlayerEntity.ParentId = null;

// Make sure the PlayerEntity is correctly registered
globalRootEntitiesById[childPlayerEntity.Id] = childPlayerEntity;
entityRegistry.AddOrUpdate(childPlayerEntity);
UpdateGlobalRootEntity(childPlayerEntity);
}
}
removedEntity = entityRegistry.RemoveEntity(entityId);
Expand Down
3 changes: 1 addition & 2 deletions NitroxServer/GameLogic/EscapePodManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ private EscapePodWorldEntity CreateNewEscapePod()
escapePod.ChildEntities.Add(new PrefabChildEntity(new NitroxId(), "9f16d82b-11f4-4eeb-aedf-f2fa2bfca8e3", new NitroxTechType("Fabricator"), 0, null, escapePod.Id));
escapePod.ChildEntities.Add(new InventoryEntity(0, new NitroxId(), new NitroxTechType("SmallStorage"), null, escapePod.Id, new List<Entity>()));

entityRegistry.AddEntity(escapePod);
entityRegistry.AddEntities(escapePod.ChildEntities);
entityRegistry.AddOrUpdate(escapePod);

return escapePod;
}
Expand Down
4 changes: 0 additions & 4 deletions NitroxServer/Serialization/World/WorldPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ public World CreateWorld(PersistedWorldData pWorldData, NitroxGameMode gameMode)

EntityRegistry entityRegistry = NitroxServiceLocator.LocateService<EntityRegistry>();
entityRegistry.AddEntities(pWorldData.EntityData.Entities);
foreach (Entity entity in pWorldData.GlobalRootData.Entities)
{
Log.Debug($"Adding GlobalRootEntity: {entity.Id} of type: {entity.GetType()}");
}
entityRegistry.AddEntitiesIgnoringDuplicate(pWorldData.GlobalRootData.Entities.OfType<Entity>().ToList());

World world = new()
Expand Down