Skip to content

Commit

Permalink
first round of bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
killzoms committed Aug 18, 2023
1 parent ec215e7 commit da6c280
Show file tree
Hide file tree
Showing 36 changed files with 344 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -29,7 +29,7 @@ public void NegotiateShouldStartTheClientOnTheContext()
Disconnected connectionState = new Disconnected();

// Act
connectionState.NegotiateReservationAsync(connectionContext);
connectionState.NegotiateReservationAsync(connectionContext).RunSynchronously();

// Assert
serverClient.IsConnected.Should().BeTrue();
Expand All @@ -52,7 +52,7 @@ public void NegotiateShouldSendMultiplayerSessionPolicyRequestPacketToClient()
Disconnected connectionState = new Disconnected();

// Act
connectionState.NegotiateReservationAsync(connectionContext);
connectionState.NegotiateReservationAsync(connectionContext).RunSynchronously();

// Assert
serverClient.Received().Send(Arg.Any<MultiplayerSessionPolicyRequest>());
Expand All @@ -75,7 +75,7 @@ public void NegotiateShouldTransitionToEstablishingSessionPolicyState()
Disconnected connectionState = new Disconnected();

// Act
connectionState.NegotiateReservationAsync(connectionContext);
connectionState.NegotiateReservationAsync(connectionContext).RunSynchronously();

// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<EstablishingSessionPolicy>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections;
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.GameLogic;
using NitroxClient.MonoBehaviours;
using NitroxClient.Unity.Helper;
using NitroxModel_Subnautica.DataStructures;
using NitroxModel.DataStructures.Util;
using NitroxModel.Packets;
using UnityEngine;

namespace NitroxClient.Communication.Packets.Processors;

public class BasicMovementProcessor : ClientPacketProcessor<BasicMovement>
{
public BasicMovementProcessor()
{
}

public override void Process(BasicMovement movement)
{
if (NitroxEntity.TryGetObjectFrom(movement.Id, out GameObject gameObject))
{
MovementController mc = gameObject.EnsureComponent<MovementController>();
mc.TargetPosition = movement.Position.ToUnity();
mc.TargetRotation = movement.Rotation.ToUnity();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public PlayerMovementProcessor(PlayerManager remotePlayerManager)

public override void Process(PlayerMovement movement)
{
Optional<RemotePlayer> remotePlayer = remotePlayerManager.Find(movement.PlayerId);
Optional<RemotePlayer> remotePlayer = remotePlayerManager.Find(movement.Id);
if (!remotePlayer.HasValue)
{
return;
}

remotePlayer.Value.UpdatePosition(movement.Position.ToUnity(),
movement.BodyRotation.ToUnity(),
movement.Rotation.ToUnity(),
movement.AimingRotation.ToUnity());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NitroxClient.Communication.Abstract;
using NitroxClient.Communication.Abstract;
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.GameLogic;
using NitroxClient.MonoBehaviours;
Expand Down Expand Up @@ -37,6 +37,8 @@ public override void Process(SimulationOwnershipResponse response)
{
RemoveRemoteController(response.Id);
}

SwapMovementController(response.Id, response.LockAcquired);
}

private void RemoveRemoteController(NitroxId id)
Expand All @@ -49,5 +51,17 @@ private void RemoveRemoteController(NitroxId id)
Object.Destroy(remotelyControlled);
}
}

private void SwapMovementController(NitroxId id, bool lockAcquired)
{
Optional<GameObject> gameObject = NitroxEntity.GetObjectFrom(id);

if (gameObject.HasValue)
{
MovementController movementController = gameObject.Value.GetComponent<MovementController>();
movementController.SetBroadcasting(lockAcquired);
movementController.SetReceiving(!lockAcquired);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.GameLogic;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.DataStructures.Util;
Expand All @@ -20,7 +20,7 @@ public VehicleMovementProcessor(PlayerManager remotePlayerManager, Vehicles vehi
public override void Process(VehicleMovement vehicleMovement)
{
VehicleMovementData vehicleModel = vehicleMovement.VehicleMovementData;
Optional<RemotePlayer> player = remotePlayerManager.Find(vehicleMovement.PlayerId);
Optional<RemotePlayer> player = remotePlayerManager.Find(vehicleMovement.Id);
vehicles.UpdateVehiclePosition(vehicleModel, player);
}
}
Expand Down
3 changes: 2 additions & 1 deletion NitroxClient/Debuggers/NetworkDebugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class NetworkDebugger : BaseDebugger, INetworkDebugger
private readonly List<string> filter = new()
{
nameof(PlayerMovement), nameof(EntityTransformUpdates), nameof(PlayerStats), nameof(SpawnEntities), nameof(VehicleMovement), nameof(PlayerCinematicControllerCall),
nameof(PlayFMODAsset), nameof(PlayFMODCustomEmitter), nameof(PlayFMODStudioEmitter), nameof(PlayFMODCustomLoopingEmitter), nameof(SimulationOwnershipChange)
nameof(PlayFMODAsset), nameof(PlayFMODCustomEmitter), nameof(PlayFMODStudioEmitter), nameof(PlayFMODCustomLoopingEmitter), nameof(SimulationOwnershipChange),
nameof(BasicMovement)
};
private readonly List<PacketDebugWrapper> packets = new List<PacketDebugWrapper>(PACKET_STORED_COUNT);

Expand Down
4 changes: 2 additions & 2 deletions NitroxClient/GameLogic/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Entities

private readonly Dictionary<Type, IEntitySpawner> entitySpawnersByType = new Dictionary<Type, IEntitySpawner>();

public Entities(IPacketSender packetSender, ThrottledPacketSender throttledPacketSender, PlayerManager playerManager, ILocalNitroxPlayer localPlayer)
public Entities(IPacketSender packetSender, ThrottledPacketSender throttledPacketSender, PlayerManager playerManager, ILocalNitroxPlayer localPlayer, SimulationOwnership simulationOwnership)
{
this.packetSender = packetSender;
this.throttledPacketSender = throttledPacketSender;
Expand All @@ -40,7 +40,7 @@ public Entities(IPacketSender packetSender, ThrottledPacketSender throttledPacke
entitySpawnersByType[typeof(InstalledBatteryEntity)] = new InstalledBatteryEntitySpawner();
entitySpawnersByType[typeof(InventoryEntity)] = new InventoryEntitySpawner();
entitySpawnersByType[typeof(InventoryItemEntity)] = new InventoryItemEntitySpawner();
entitySpawnersByType[typeof(WorldEntity)] = new WorldEntitySpawner(playerManager, localPlayer);
entitySpawnersByType[typeof(WorldEntity)] = new WorldEntitySpawner(playerManager, localPlayer, simulationOwnership);
entitySpawnersByType[typeof(PlaceholderGroupWorldEntity)] = entitySpawnersByType[typeof(WorldEntity)];
entitySpawnersByType[typeof(EscapePodWorldEntity)] = entitySpawnersByType[typeof(WorldEntity)];
entitySpawnersByType[typeof(PlayerWorldEntity)] = entitySpawnersByType[typeof(WorldEntity)];
Expand Down
14 changes: 11 additions & 3 deletions NitroxClient/GameLogic/LocalPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class LocalPlayer : ILocalNitroxPlayer
private readonly Lazy<GameObject> body;
private readonly Lazy<GameObject> playerModel;
private readonly Lazy<GameObject> bodyPrototype;
private readonly NitroxEntity entity;

public GameObject Body => body.Value;

Expand All @@ -34,6 +35,7 @@ public class LocalPlayer : ILocalNitroxPlayer

public string PlayerName => multiplayerSession.AuthenticationContext.Username;
public ushort PlayerId => multiplayerSession.Reservation.PlayerId;

public PlayerSettings PlayerSettings => multiplayerSession.PlayerSettings;

public Perms Permissions;
Expand All @@ -47,18 +49,24 @@ public LocalPlayer(IMultiplayerSession multiplayerSession, IPacketSender packetS
playerModel = new Lazy<GameObject>(() => Body.RequireGameObject("player_view"));
bodyPrototype = new Lazy<GameObject>(CreateBodyPrototype);
Permissions = Perms.PLAYER;

if (!Player.mainObject.TryGetComponent(out NitroxEntity nitroxEntity))
{
nitroxEntity = Player.mainObject.AddComponent<NitroxEntity>();
}
entity = nitroxEntity;
}

public void BroadcastLocation(Vector3 location, Vector3 velocity, Quaternion bodyRotation, Quaternion aimingRotation, Optional<VehicleMovementData> vehicle)
public void BroadcastLocation(Vector3 location, Quaternion bodyRotation, Quaternion aimingRotation, Optional<VehicleMovementData> vehicle)
{
Movement movement;
if (vehicle.HasValue)
{
movement = new VehicleMovement(multiplayerSession.Reservation.PlayerId, vehicle.Value);
movement = new VehicleMovement(entity.Id, vehicle.Value);
}
else
{
movement = new PlayerMovement(multiplayerSession.Reservation.PlayerId, location.ToDto(), bodyRotation.ToDto(), aimingRotation.ToDto());
movement = new PlayerMovement(entity.Id, location.ToDto(), bodyRotation.ToDto(), aimingRotation.ToDto());
}

packetSender.Send(movement);
Expand Down
11 changes: 6 additions & 5 deletions NitroxClient/GameLogic/MobileVehicleBay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class MobileVehicleBay
public static GameObject MostRecentlyCrafted { get; set; }

private readonly IPacketSender packetSender;
private readonly Vehicles vehicles;
private readonly SimulationOwnership simulationOwnership;

public MobileVehicleBay(IPacketSender packetSender, Vehicles vehicles)
public MobileVehicleBay(IPacketSender packetSender, Vehicles vehicles, SimulationOwnership simulationOwnership)
{
this.packetSender = packetSender;
this.vehicles = vehicles;
this.simulationOwnership = simulationOwnership;
}

public void BeginCrafting(ConstructorInput constructor, GameObject constructedObject, TechType techType, float duration)
Expand All @@ -42,11 +42,12 @@ public void BeginCrafting(ConstructorInput constructor, GameObject constructedOb

NitroxId constructedObjectId = NitroxEntity.GenerateNewId(constructedObject);

MovementController mc = constructedObject.AddComponent<MovementController>();

VehicleWorldEntity vehicleEntity = new(constructorId, DayNightCycle.main.timePassedAsFloat, constructedObject.transform.ToLocalDto(), string.Empty, false, constructedObjectId, techType.ToDto(), null);
VehicleChildEntityHelper.PopulateChildren(constructedObjectId, constructedObject.GetFullHierarchyPath(), vehicleEntity.ChildEntities, constructedObject);

packetSender.Send(new EntitySpawnedByClient(vehicleEntity));

constructor.StartCoroutine(vehicles.UpdateVehiclePositionAfterSpawn(constructedObjectId, techType, constructedObject, duration + 10.0f));
simulationOwnership.RequestSimulationLock(constructedObjectId, SimulationLockType.TRANSIENT);
}
}
12 changes: 9 additions & 3 deletions NitroxClient/GameLogic/RemotePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void UpdatePosition(Vector3 position, Quaternion bodyRotation, Quaternion
SetVehicle(null);
SetPilotingChair(null);

MovementController.IsMoving = true;
MovementController.SetReceiving(true);

MovementController.TargetPosition = position;
MovementController.TargetRotation = bodyRotation;
Expand Down Expand Up @@ -169,7 +169,10 @@ public void SetPilotingChair(PilotingChair newPilotingChair)

RigidBody.isKinematic = AnimationController["cyclops_steering"] = isInPilotingChair;
RigidBody.interpolation = isInPilotingChair ? RigidbodyInterpolation.None : RigidbodyInterpolation.Interpolate;
MovementController.IsMoving = !isInPilotingChair;
if (isInPilotingChair)
{
MovementController.SetReceiving(false);
}
}
}

Expand Down Expand Up @@ -246,7 +249,10 @@ public void SetVehicle(Vehicle newVehicle)

RigidBody.interpolation = Vehicle ? RigidbodyInterpolation.None : RigidbodyInterpolation.Interpolate;
RigidBody.isKinematic = Vehicle;
MovementController.IsMoving = !Vehicle;
if (Vehicle)
{
MovementController.SetReceiving(false);
}

AnimationController["in_seamoth"] = newVehicle is SeaMoth;
AnimationController["in_exosuit"] = AnimationController["using_mechsuit"] = newVehicle is Exosuit;
Expand Down
4 changes: 2 additions & 2 deletions NitroxClient/GameLogic/Simulation/LockRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NitroxModel.DataStructures;
using NitroxModel.DataStructures;

namespace NitroxClient.GameLogic.Simulation
{
Expand All @@ -19,7 +19,7 @@ public override void LockRequestComplete(NitroxId id, bool lockAquired)
{
if (onComplete != null)
{
onComplete(id, lockAquired, (T)context);
onComplete(id, lockAquired, context);
}
}

Expand Down
15 changes: 14 additions & 1 deletion NitroxClient/GameLogic/SimulationOwnership.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using NitroxClient.Communication.Abstract;
using NitroxClient.GameLogic.Simulation;
using NitroxModel.DataStructures;
Expand All @@ -13,6 +14,9 @@ public class SimulationOwnership
private readonly Dictionary<NitroxId, SimulationLockType> simulatedIdsByLockType = new Dictionary<NitroxId, SimulationLockType>();
private readonly Dictionary<NitroxId, LockRequestBase> lockRequestsById = new Dictionary<NitroxId, LockRequestBase>();

public event Action<NitroxId> StoppedSimulatingEntity;
public event Action<NitroxId> StartedSimulatingEntity;

public SimulationOwnership(IMultiplayerSession muliplayerSession, IPacketSender packetSender)
{
this.muliplayerSession = muliplayerSession;
Expand Down Expand Up @@ -68,11 +72,20 @@ public void ReceivedSimulationLockResponse(NitroxId id, bool lockAquired, Simula
public void SimulateEntity(NitroxId id, SimulationLockType lockType)
{
simulatedIdsByLockType[id] = lockType;
if (StartedSimulatingEntity != null)
{
StartedSimulatingEntity(id);
}
}

public void StopSimulatingEntity(NitroxId id)
{
simulatedIdsByLockType.Remove(id);

if (StoppedSimulatingEntity != null)
{
StoppedSimulatingEntity(id);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.DataStructures.GameLogic.Entities;
using NitroxModel.DataStructures.Util;
Expand Down Expand Up @@ -33,7 +33,7 @@ public IEnumerator SpawnAsync(WorldEntity entity, Optional<GameObject> parent, E
}

life.initialized = true;
life.SpawnPlants();
yield return life.SpawnPlants();
foreach (Entity childEntity in entity.ChildEntities)
{
if (childEntity is WorldEntity worldChild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NitroxClient.MonoBehaviours;
using NitroxClient.MonoBehaviours.Overrides;
using NitroxClient.Unity.Helper;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic.Entities;
using NitroxModel.DataStructures.Util;
using NitroxModel.Helper;
Expand All @@ -17,6 +18,12 @@ public class VehicleWorldEntitySpawner : IWorldEntitySpawner
// that they are within allowed range. However, this range is a bit restrictive. We will allow constructor spawning up to a specified
// distance - anything more will simply use world spawning (no need to play the animation anyways).
private const float ALLOWED_CONSTRUCTOR_DISTANCE = 100.0f;
private readonly SimulationOwnership simulationOwnership;

internal VehicleWorldEntitySpawner(SimulationOwnership simulationOwnership)
{
this.simulationOwnership = simulationOwnership;
}

public IEnumerator SpawnAsync(WorldEntity entity, Optional<GameObject> parent, EntityCell cellRoot, TaskResult<Optional<GameObject>> result)
{
Expand All @@ -35,12 +42,17 @@ public IEnumerator SpawnAsync(WorldEntity entity, Optional<GameObject> parent, E
{
MobileVehicleBay.TransmitLocalSpawns = false;
yield return SpawnViaConstructor(vehicleEntity, constructor, result);
result.value.Value.EnsureComponent<MovementController>();

simulationOwnership.RequestSimulationLock(vehicleEntity.Id, SimulationLockType.TRANSIENT);
MobileVehicleBay.TransmitLocalSpawns = true;
yield break;
}
}

yield return SpawnInWorld(vehicleEntity, result, parent);
yield return SpawnInWorld(vehicleEntity, result, parent);
result.value.Value.EnsureComponent<MovementController>();
simulationOwnership.RequestSimulationLock(vehicleEntity.Id, SimulationLockType.TRANSIENT);
}

private IEnumerator SpawnInWorld(VehicleWorldEntity vehicleEntity, TaskResult<Optional<GameObject>> result, Optional<GameObject> parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ namespace NitroxClient.GameLogic.Spawning.WorldEntities;
public class WorldEntitySpawnerResolver
{
private readonly DefaultWorldEntitySpawner defaultEntitySpawner = new();
private readonly VehicleWorldEntitySpawner vehicleWorldEntitySpawner = new();
private readonly VehicleWorldEntitySpawner vehicleWorldEntitySpawner;

private readonly PlaceholderGroupWorldEntitySpawner prefabWorldEntitySpawner;
private readonly PlayerWorldEntitySpawner playerWorldEntitySpawner;

private readonly Dictionary<TechType, IWorldEntitySpawner> customSpawnersByTechType = new();

public WorldEntitySpawnerResolver(PlayerManager playerManager, ILocalNitroxPlayer localPlayer)
public WorldEntitySpawnerResolver(PlayerManager playerManager, ILocalNitroxPlayer localPlayer, SimulationOwnership simulationOwnership)
{
customSpawnersByTechType[TechType.Crash] = new CrashEntitySpawner();
customSpawnersByTechType[TechType.Reefback] = new ReefbackWorldEntitySpawner(defaultEntitySpawner);
customSpawnersByTechType[TechType.EscapePod] = new EscapePodWorldEntitySpawner();
prefabWorldEntitySpawner = new PlaceholderGroupWorldEntitySpawner(this, defaultEntitySpawner);
playerWorldEntitySpawner = new PlayerWorldEntitySpawner(playerManager, localPlayer);
vehicleWorldEntitySpawner = new VehicleWorldEntitySpawner(simulationOwnership);
}

public IWorldEntitySpawner ResolveEntitySpawner(WorldEntity entity)
Expand Down
Loading

0 comments on commit da6c280

Please sign in to comment.