Skip to content

Commit 0baaddb

Browse files
authored
Fix SpawnAnyPendingChildren() (SubnauticaNitrox#1256)
* SpawnAnyPendingChildren() wasn't spawning childs * Renamed Int3 to NitroxInt3 and moved it to DataExtensions * Fix toString and public serialization constructor * oops * Revert previous commit This reverts commit 9c752de. * Moved Int3 to DataExtensions * Fix AnchoredFace serial
1 parent 85a7d62 commit 0baaddb

3 files changed

Lines changed: 31 additions & 41 deletions

File tree

NitroxClient/GameLogic/Entities.cs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
88
using NitroxModel.DataStructures.Util;
99
using NitroxModel.Helper;
10-
using NitroxModel.Logger;
1110
using NitroxModel.Packets;
1211
using NitroxModel_Subnautica.DataStructures;
1312
using UnityEngine;
@@ -36,9 +35,7 @@ public void BroadcastTransforms(Dictionary<NitroxId, GameObject> gameObjectsById
3635

3736
foreach (KeyValuePair<NitroxId, GameObject> gameObjectWithId in gameObjectsById)
3837
{
39-
GameObject go = gameObjectWithId.Value;
40-
41-
if (go)
38+
if (gameObjectWithId.Value)
4239
{
4340
update.AddUpdate(gameObjectWithId.Key, gameObjectWithId.Value.transform.position.ToDto(), gameObjectWithId.Value.transform.rotation.ToDto());
4441
}
@@ -49,21 +46,20 @@ public void BroadcastTransforms(Dictionary<NitroxId, GameObject> gameObjectsById
4946

5047
public void BroadcastMetadataUpdate(NitroxId id, EntityMetadata metadata)
5148
{
52-
EntityMetadataUpdate update = new EntityMetadataUpdate(id, metadata);
53-
packetSender.Send(update);
49+
packetSender.Send(new EntityMetadataUpdate(id, metadata));
5450
}
5551

5652
public void Spawn(List<Entity> entities)
5753
{
5854
foreach (Entity entity in entities)
5955
{
60-
LargeWorldStreamer.main.cellManager.UnloadBatchCells(ToInt3(entity.AbsoluteEntityCell.CellId)); // Just in case
56+
LargeWorldStreamer.main.cellManager.UnloadBatchCells(entity.AbsoluteEntityCell.CellId.ToUnity()); // Just in case
6157

62-
if (alreadySpawnedIds.Contains(entity.Id))
58+
if (WasSpawnedByServer(entity.Id))
6359
{
6460
UpdatePosition(entity);
6561
}
66-
else if (entity.ParentId != null && !alreadySpawnedIds.Contains(entity.ParentId))
62+
else if (entity.ParentId != null && !WasSpawnedByServer(entity.ParentId))
6763
{
6864
AddPendingParentEntity(entity);
6965
}
@@ -79,12 +75,11 @@ public void Spawn(List<Entity> entities)
7975
private EntityCell EnsureCell(Entity entity)
8076
{
8177
EntityCell entityCell;
82-
BatchCells batchCells;
8378

84-
Int3 batchId = ToInt3(entity.AbsoluteEntityCell.BatchId);
85-
Int3 cellId = ToInt3(entity.AbsoluteEntityCell.CellId);
79+
Int3 batchId = entity.AbsoluteEntityCell.BatchId.ToUnity();
80+
Int3 cellId = entity.AbsoluteEntityCell.CellId.ToUnity();
8681

87-
if (!batchCellsById.TryGetValue(batchId, out batchCells))
82+
if (!batchCellsById.TryGetValue(batchId, out BatchCells batchCells))
8883
{
8984
batchCells = LargeWorldStreamer.main.cellManager.InitializeBatchCells(batchId);
9085
}
@@ -102,11 +97,6 @@ private EntityCell EnsureCell(Entity entity)
10297
return entityCell;
10398
}
10499

105-
private Int3 ToInt3(NitroxModel.DataStructures.Int3 int3)
106-
{
107-
return new Int3(int3.X, int3.Y, int3.Z);
108-
}
109-
110100
private void Spawn(Entity entity, Optional<GameObject> parent)
111101
{
112102
alreadySpawnedIds.Add(entity.Id);
@@ -116,28 +106,27 @@ private void Spawn(Entity entity, Optional<GameObject> parent)
116106
IEntitySpawner entitySpawner = entitySpawnerResolver.ResolveEntitySpawner(entity);
117107
Optional<GameObject> gameObject = entitySpawner.Spawn(entity, parent, cellRoot);
118108

119-
foreach (Entity childEntity in entity.ChildEntities)
109+
if (!entitySpawner.SpawnsOwnChildren())
120110
{
121-
if (!alreadySpawnedIds.Contains(childEntity.Id) && !entitySpawner.SpawnsOwnChildren())
111+
foreach (Entity childEntity in entity.ChildEntities)
122112
{
123-
Spawn(childEntity, gameObject);
113+
if (!WasSpawnedByServer(childEntity.Id))
114+
{
115+
Spawn(childEntity, gameObject);
116+
}
124117
}
125-
126-
alreadySpawnedIds.Add(childEntity.Id);
127118
}
128119
}
129120

130121
private void SpawnAnyPendingChildren(Entity entity)
131122
{
132-
List<Entity> pendingEntities;
133-
134-
if (pendingParentEntitiesByParentId.TryGetValue(entity.Id, out pendingEntities))
123+
if (pendingParentEntitiesByParentId.TryGetValue(entity.Id, out List<Entity> pendingEntities))
135124
{
136125
Optional<GameObject> parent = NitroxEntity.GetObjectFrom(entity.Id);
137126

138127
foreach (Entity child in pendingEntities)
139128
{
140-
Spawn(entity, parent);
129+
Spawn(child, parent);
141130
}
142131

143132
pendingParentEntitiesByParentId.Remove(entity.Id);
@@ -163,9 +152,7 @@ private void UpdatePosition(Entity entity)
163152

164153
private void AddPendingParentEntity(Entity entity)
165154
{
166-
List<Entity> pendingEntities;
167-
168-
if (!pendingParentEntitiesByParentId.TryGetValue(entity.ParentId, out pendingEntities))
155+
if (!pendingParentEntitiesByParentId.TryGetValue(entity.ParentId, out List<Entity> pendingEntities))
169156
{
170157
pendingEntities = new List<Entity>();
171158
pendingParentEntitiesByParentId[entity.ParentId] = pendingEntities;
@@ -174,15 +161,8 @@ private void AddPendingParentEntity(Entity entity)
174161
pendingEntities.Add(entity);
175162
}
176163

177-
public bool WasSpawnedByServer(NitroxId id)
178-
{
179-
return alreadySpawnedIds.Contains(id);
180-
}
181-
182-
public bool RemoveEntity(NitroxId id)
183-
{
184-
return alreadySpawnedIds.Remove(id);
185-
}
164+
public bool WasSpawnedByServer(NitroxId id) => alreadySpawnedIds.Contains(id);
186165

166+
public bool RemoveEntity(NitroxId id) => alreadySpawnedIds.Remove(id);
187167
}
188168
}

NitroxModel-Subnautica/DataStructures/DataExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ namespace NitroxModel_Subnautica.DataStructures
66
{
77
public static class DataExtensions
88
{
9+
public static Int3 ToUnity(this NitroxModel.DataStructures.Int3 v)
10+
{
11+
return new Int3(v.X, v.Y, v.Z);
12+
}
13+
14+
public static NitroxModel.DataStructures.Int3 ToDto(this Int3 v)
15+
{
16+
return new NitroxModel.DataStructures.Int3(v.x, v.y, v.z);
17+
}
18+
919
public static NitroxVector3 ToDto(this Vector3 v)
1020
{
1121
return new NitroxVector3(v.x, v.y, v.z);

NitroxModel-Subnautica/DataStructures/GameLogic/Buildings/Rotation/Metadata/AnchoredFaceRotationMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class AnchoredFaceRotationMetadata : RotationMetadata
1818
[ProtoMember(3)]
1919
public int FaceType { get; set; }
2020

21-
public AnchoredFaceRotationMetadata() : base(typeof(BaseAddFaceGhost))
21+
protected AnchoredFaceRotationMetadata() : base(typeof(BaseAddFaceGhost))
2222
{
23-
// For serialization purposes
23+
// Constructor for serialization. Has to be "protected" for json serialization.
2424
}
2525

2626
public AnchoredFaceRotationMetadata(Int3 cell, int facedirection, int facetype) : base(typeof(BaseAddFaceGhost))

0 commit comments

Comments
 (0)