Skip to content

Commit

Permalink
When spawning items, sets them as secured so they aren't dropped when…
Browse files Browse the repository at this point in the history
… dying.
  • Loading branch information
tornac1234 committed Feb 19, 2024
1 parent ad9749c commit b432878
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 68 deletions.

This file was deleted.

4 changes: 4 additions & 0 deletions NitroxClient/GameLogic/Spawning/InventoryItemEntitySpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ private void SetupObject(InventoryItemEntity entity, GameObject gameObject, Game
Pickupable pickupable = gameObject.RequireComponent<Pickupable>();
pickupable.Initialize();

// Items eventually get "secured" once a player gets into a SubRoot (or for other reasons) so we need to force this state by default
// so that player don't risk their whole inventory if they reconnect in the water.
pickupable.destroyOnDeath = false;

using (PacketSuppressor<EntityReparented>.Suppress())
using (PacketSuppressor<PlayerQuickSlotsBindingChanged>.Suppress())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void ApplyMetadata(GameObject gameObject, EntityMetadata metadata)
newerMetadataById.TryGetValue(objectId, out EntityMetadata newMetadata))
{
metadata = newMetadata;
newerMetadataById.Remove(objectId);
}
Optional<IEntityMetadataProcessor> metadataProcessor = FromMetaData(metadata);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

namespace NitroxClient.GameLogic.Spawning.WorldEntities;

public class GeyserWorldEntitySpawner(Entities entities) : IWorldEntitySpawner, IWorldEntitySyncSpawner
public class GeyserWorldEntitySpawner : IWorldEntitySpawner, IWorldEntitySyncSpawner
{
private readonly Entities entities = entities;
private readonly Entities entities;

public GeyserWorldEntitySpawner(Entities entities)
{
this.entities = entities;
}

public IEnumerator SpawnAsync(WorldEntity entity, Optional<GameObject> parent, EntityCell cellRoot, TaskResult<Optional<GameObject>> result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,30 @@ private static bool VerifyCanSpawnOrError(ReefbackEntity entity, GameObject pref
return false;
}

private static void SetupObject(ReefbackEntity entity, GameObject gameObject, EntityCell cellRoot, ReefbackLife reefbackLife)
private static void SetupObject(ReefbackEntity entity, GameObject gameObject, EntityCell entityCell, ReefbackLife reefbackLife)
{
Transform transform = gameObject.transform;
transform.localPosition = entity.Transform.Position.ToUnity();
transform.localRotation = entity.Transform.Rotation.ToUnity();
transform.localScale = entity.Transform.LocalScale.ToUnity();
transform.SetParent(cellRoot.liveRoot.transform);
entityCell.EnsureRoot();
transform.SetParent(entityCell.liveRoot.transform);

// Replicate only the useful parts of ReefbackLife.Initialize
reefbackLife.initialized = true;
reefbackLife.needToRemovePlantPhysics = false;
reefbackLife.hasCorals = gameObject.transform.localScale.x > 0.8f;

if (reefbackLife.hasCorals)
if (reefbackLife.hasCorals && LargeWorld.main)
{
if (LargeWorld.main)
string biome = LargeWorld.main.GetBiome(entity.OriginalPosition.ToUnity());
if (!string.IsNullOrEmpty(biome) && biome.StartsWith("grassyplateaus", StringComparison.OrdinalIgnoreCase))
{
string biome = LargeWorld.main.GetBiome(entity.OriginalPosition.ToUnity());
if (!string.IsNullOrEmpty(biome) && biome.StartsWith("grassyplateaus", StringComparison.OrdinalIgnoreCase))
{
reefbackLife.grassIndex = 0;
}
else
{
reefbackLife.grassIndex = entity.GrassIndex;
}
reefbackLife.grassIndex = 0;
}
else
{
reefbackLife.grassIndex = entity.GrassIndex;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public static BuildEntity MakeEmpty()
return new BuildEntity();
}

/// <remarks>Used for deserialization</remarks>
/// <remarks>
/// Used for deserialization.
/// <see cref="WorldEntity.SpawnedByServer"/> is set to true because this entity is meant to receive simulation locks
/// </remarks>
public BuildEntity(BaseData baseData, NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) :
base(transform, level, classId, true, id, techType, metadata, parentId, childEntities)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public static InteriorPieceEntity MakeEmpty()
return new InteriorPieceEntity();
}

/// <remarks>Used for deserialization</remarks>
/// <remarks>
/// Used for deserialization.
/// <see cref="WorldEntity.SpawnedByServer"/> is set to true because this entity is meant to receive simulation locks
/// </remarks>
public InteriorPieceEntity(NitroxBaseFace baseFace, NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) :
base(transform, level, classId, true, id, techType, metadata, parentId, childEntities)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public MapRoomEntity(NitroxId id, NitroxId parentId, NitroxInt3 cell)
Transform = new();
}

/// <remarks>Used for deserialization</remarks>
/// <remarks>
/// Used for deserialization.
/// <see cref="WorldEntity.SpawnedByServer"/> is set to true because this entity is meant to receive simulation locks
/// </remarks>
public MapRoomEntity(NitroxInt3 cell, NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) :
base(transform, level, classId, true, id, techType, metadata, parentId, childEntities)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public static ModuleEntity MakeEmpty()
return new ModuleEntity();
}

/// <remarks>Used for deserialization</remarks>
/// <remarks>
/// Used for deserialization.
/// <see cref="WorldEntity.SpawnedByServer"/> is set to true because this entity is meant to receive simulation locks
/// </remarks>
public ModuleEntity(float constructedAmount, bool isInside, NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) :
base(transform, level, classId, true, id, techType, metadata, parentId, childEntities)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public MoonpoolEntity(NitroxId id, NitroxId parentId, NitroxInt3 cell)
Cell = cell;
}

/// <remarks>Used for deserialization</remarks>
/// <remarks>
/// Used for deserialization.
/// <see cref="WorldEntity.SpawnedByServer"/> is set to true because this entity is meant to receive simulation locks
/// </remarks>
public MoonpoolEntity(NitroxInt3 cell, NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) :
base(transform, level, classId, true, id, techType, metadata, parentId, childEntities)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public PlanterEntity(NitroxId id, NitroxId parentId)
ParentId = parentId;
}

/// <remarks>Used for deserialization</remarks>
/// <remarks>Used for deserialization. Should not be used for something else</remarks>
public PlanterEntity(NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) :
base(transform, level, classId, true, id, techType, metadata, parentId, childEntities) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ protected PlayerWorldEntity()
// Constructor for serialization. Has to be "protected" for json serialization.
}

/// <remarks>Used for deserialization</remarks>
/// <remarks>
/// Used for deserialization.
/// <see cref="WorldEntity.SpawnedByServer"/> is set to true because this entity is meant to receive simulation locks
/// </remarks>
public PlayerWorldEntity(NitroxTransform transform, int level, string classId, bool spawnedByServer, NitroxId id, NitroxTechType techType, EntityMetadata metadata, NitroxId parentId, List<Entity> childEntities) :
base(transform, level, classId, true, id, techType, metadata, parentId, childEntities) {}

Expand Down
3 changes: 1 addition & 2 deletions NitroxPatcher/Patches/Dynamic/CrashHome_Spawn_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public static void BroadcastFishCreated(GameObject crashFishObject)
{
return;
}
NitroxId crashFishId = new();
NitroxEntity.SetNewId(crashFishObject, crashFishId);
NitroxId crashFishId = NitroxEntity.GenerateNewId(crashFishObject);
LargeWorldEntity largeWorldEntity = crashFishObject.GetComponent<LargeWorldEntity>();
UniqueIdentifier uniqueIdentifier = crashFishObject.GetComponent<UniqueIdentifier>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public sealed partial class CreatureDeath_OnKillAsync_Patch : NitroxPatch, IDyna
* gameObject.GetComponent<Rigidbody>().angularDrag = base.gameObject.GetComponent<Rigidbody>().angularDrag * 3f;
* UnityEngine.Object.Destroy(base.gameObject);
* result = null;
* CreatureDeath_OnKillAsync_Patch.BroadcastCookedSpawned(this, gameObject, cookedData);
* CreatureDeath_OnKillAsync_Patch.BroadcastCookedSpawned(this, gameObject, cookedData); <---- INSERTED LINE
*
* 2nd injection:
* base.Invoke("RemoveCorpse", this.removeCorpseAfterSeconds);
* CreatureDeath_OnKillAsync_Patch.
* CreatureDeath_OnKillAsync_Patch.BroadcastRemoveCorpse(this); <---- INSERTED LINE
*
* 3rd injection:
* this.eatable.SetDecomposes(true);
* CreatureDeath_OnKillAsync_Patch.BroadcastCookedSpawned(this.eatable);
* CreatureDeath_OnKillAsync_Patch.BroadcastCookedSpawned(this.eatable); <---- INSERTED LINE
*/
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public static void BroadcastSpawnedRespawner(Respawn respawn)
{
int cellLevel = respawn.TryGetComponent(out LargeWorldEntity largeWorldEntity) ? (int)largeWorldEntity.cellLevel : 0;
string classId = respawn.GetComponent<UniqueIdentifier>().ClassId;
NitroxId respawnId = new();
NitroxEntity.SetNewId(respawn.gameObject, respawnId);
NitroxId respawnId = NitroxEntity.GenerateNewId(respawn.gameObject);

NitroxId parentId = null;
if (respawn.transform.parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Prepare(ref WorldEntity entity, DeterministicGenerator generator)
return;
}

// In case the grassIndex will is chosen randomly
// In case the grassIndex is chosen randomly
int grassIndex = XORRandom.NextIntRange(1, GRASS_VARIANTS_COUNT);

entity = new ReefbackEntity(entity.Transform, entity.Level, entity.ClassId,
Expand Down
22 changes: 4 additions & 18 deletions NitroxServer/GameLogic/Entities/WorldEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class WorldEntityManager
/// <summary>
/// World entities can disappear if you go out of range.
/// </summary>
private readonly Dictionary<NitroxInt3, Dictionary<NitroxId, WorldEntity>> worldEntitiesByBatchId;

private readonly Dictionary<AbsoluteEntityCell, Dictionary<NitroxId, WorldEntity>> worldEntitiesByCell;

/// <summary>
Expand All @@ -42,10 +40,6 @@ public WorldEntityManager(EntityRegistry entityRegistry, BatchEntitySpawner batc

globalRootEntitiesById = entityRegistry.GetEntities<GlobalRootEntity>().ToDictionary(entity => entity.Id);

worldEntitiesByBatchId = worldEntities.Where(entity => entity is not GlobalRootEntity)
.GroupBy(entity => entity.AbsoluteEntityCell.BatchId)
.ToDictionary(group => group.Key, group => group.ToDictionary(entity => entity.Id, entity => entity));

worldEntitiesByCell = worldEntities.Where(entity => entity is not GlobalRootEntity)
.GroupBy(entity => entity.AbsoluteEntityCell)
.ToDictionary(group => group.Key, group => group.ToDictionary(entity => entity.Id, entity => entity));
Expand Down Expand Up @@ -166,12 +160,6 @@ public void RegisterWorldEntityInCell(WorldEntity entity, AbsoluteEntityCell cel
{
lock (worldEntitiesLock)
{
if (!worldEntitiesByBatchId.TryGetValue(cell.BatchId, out Dictionary<NitroxId, WorldEntity> worldEntitiesInBatch))
{
worldEntitiesInBatch = worldEntitiesByBatchId[cell.BatchId] = [];
}
worldEntitiesInBatch[entity.Id] = entity;

if (!worldEntitiesByCell.TryGetValue(cell, out Dictionary<NitroxId, WorldEntity> worldEntitiesInCell))
{
worldEntitiesInCell = worldEntitiesByCell[cell] = [];
Expand All @@ -192,11 +180,6 @@ public void UnregisterWorldEntityFromCell(NitroxId entityId, AbsoluteEntityCell
{
lock (worldEntitiesLock)
{
if (worldEntitiesByBatchId.TryGetValue(cell.BatchId, out Dictionary<NitroxId, WorldEntity> worldEntitiesInBatch))
{
worldEntitiesInBatch.Remove(entityId);
}

if (worldEntitiesByCell.TryGetValue(cell, out Dictionary<NitroxId, WorldEntity> worldEntitiesInCell))
{
worldEntitiesInCell.Remove(entityId);
Expand Down Expand Up @@ -333,6 +316,9 @@ public void AddOrUpdateGlobalRootEntity(GlobalRootEntity entity, bool addOrUpdat
}
}

/// <summary>
/// Iterative breadth-first search which gets all children player entities in <paramref name="parentEntity"/>'s hierarchy.
/// </summary>
private List<PlayerWorldEntity> FindPlayerEntitiesInChildren(Entity parentEntity)
{
List<PlayerWorldEntity> playerWorldEntities = [];
Expand All @@ -349,7 +335,7 @@ private List<PlayerWorldEntity> FindPlayerEntitiesInChildren(Entity parentEntity
}
else
{
entitiesToSearch.AddRange(currentEntity.ChildEntities);
entitiesToSearch.InsertRange(0, currentEntity.ChildEntities);
}
}
return playerWorldEntities;
Expand Down

0 comments on commit b432878

Please sign in to comment.