Skip to content

Commit

Permalink
Fix bugs: not being able to drive a vehicle, rate limiter entries not…
Browse files Browse the repository at this point in the history
… being deleted when the vehicle is gone, being too far from the piloting chair, destroying a cyclop's objects would bug for players outside the cyclops, remote player wrong rotation in bases
  • Loading branch information
tornac1234 committed Nov 11, 2024
1 parent 3211798 commit b6b86ca
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
5 changes: 2 additions & 3 deletions NitroxClient/GameLogic/RemotePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void UpdatePosition(Vector3 position, Vector3 velocity, Quaternion bodyRo
AnimationController.Velocity = MovementHelper.GetCorrectedVelocity(position, velocity, Body, Time.fixedDeltaTime);

// If in a subroot the position will be relative to the subroot
if (SubRoot && !SubRoot.isBase)
if (SubRoot && SubRoot.isBase)
{
Quaternion vehicleAngle = SubRoot.transform.rotation;
position = vehicleAngle * position;
Expand All @@ -162,13 +162,12 @@ public void UpdatePosition(Vector3 position, Vector3 velocity, Quaternion bodyRo

public void UpdatePositionInCyclops(Vector3 localPosition, Quaternion localRotation)
{
if (Pawn == null)
if (Pawn == null || PilotingChair)
{
return;
}

SetVehicle(null);
SetPilotingChair(null);

AnimationController.AimingRotation = localRotation;
AnimationController.UpdatePlayerAnimations = true;
Expand Down
7 changes: 4 additions & 3 deletions NitroxClient/GameLogic/SimulationOwnership.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public void TreatSimulatedEntity(SimulatedEntity simulatedEntity)
{
bool isLocalPlayerNewOwner = multiplayerSession.Reservation.PlayerId == simulatedEntity.PlayerId;

if (TreatVehicleEntity(simulatedEntity.Id, isLocalPlayerNewOwner, simulatedEntity.LockType))
if (TreatVehicleEntity(simulatedEntity.Id, isLocalPlayerNewOwner, simulatedEntity.LockType) ||
newerSimulationById.ContainsKey(simulatedEntity.Id))
{
return;
}
Expand Down Expand Up @@ -127,7 +128,7 @@ public bool TryGetLockType(NitroxId nitroxId, out SimulationLockType simulationL
return simulatedIdsByLockType.TryGetValue(nitroxId, out simulationLockType);
}

private bool TreatVehicleEntity(NitroxId entityId, bool isLocalPlayerNewOwner, SimulationLockType simulationLockType)
public bool TreatVehicleEntity(NitroxId entityId, bool isLocalPlayerNewOwner, SimulationLockType simulationLockType)
{
if (!NitroxEntity.TryGetObjectFrom(entityId, out GameObject gameObject) || !IsVehicle(gameObject))
{
Expand Down Expand Up @@ -180,8 +181,8 @@ public void ApplyNewerSimulation(NitroxId nitroxId)
{
if (newerSimulationById.TryGetValue(nitroxId, out SimulatedEntity simulatedEntity))
{
TreatSimulatedEntity(simulatedEntity);
newerSimulationById.Remove(nitroxId);
TreatSimulatedEntity(simulatedEntity);
}
}

Expand Down
5 changes: 5 additions & 0 deletions NitroxClient/GameLogic/Vehicles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void SetOnPilotMode(GameObject gameObject, ushort playerId, bool isPiloti
}
else if (gameObject.GetComponent<SubRoot>())
{
if (!isPiloting)
{
remotePlayer.SetPilotingChair(null);
return;
}
PilotingChair pilotingChair = FindPilotingChairWithCache(gameObject, TechType.Cyclops);
remotePlayer.SetPilotingChair(pilotingChair);
}
Expand Down
2 changes: 1 addition & 1 deletion NitroxClient/MonoBehaviours/MovementBroadcaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void RegisterWatched(GameObject gameObject, NitroxId entityId)

if (!Instance.watchedEntries.ContainsKey(entityId))
{
Instance.watchedEntries.Add(entityId, new(gameObject.transform));
Instance.watchedEntries.Add(entityId, new(entityId, gameObject.transform));
}
}

Expand Down
11 changes: 10 additions & 1 deletion NitroxClient/MonoBehaviours/Vehicles/WatchedEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class WatchedEntry
/// <inheritdoc cref="MAX_TIME_WITHOUT_BROADCAST"/>
private const float SAFETY_BROADCAST_WINDOW = 0.2f;

private readonly NitroxId Id;
private readonly Transform transform;
private readonly Vehicle vehicle;
private readonly SubControl subControl;
Expand All @@ -30,8 +31,9 @@ public class WatchedEntry
private Vector3 latestLocalPositionSent;
private Quaternion latestLocalRotationSent;

public WatchedEntry(Transform transform)
public WatchedEntry(NitroxId Id, Transform transform)
{
this.Id = Id;
this.transform = transform;
vehicle = transform.GetComponent<Vehicle>();
subControl = transform.GetComponent<SubControl>();
Expand Down Expand Up @@ -114,6 +116,13 @@ private bool HasVehicleMoved()
/// </remarks>
public bool ShouldBroadcastMovement()
{
// Watched entry validity check (e.g. for vehicle death)
if (!transform)
{
MovementBroadcaster.UnregisterWatched(Id);
return false;
}

float deltaTimeSinceBroadcast = DayNightCycle.main.timePassedAsFloat - latestBroadcastTime;

if (IsDrivenCyclops() || IsDrivenVehicle() || deltaTimeSinceBroadcast < 0 || HasVehicleMoved())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public sealed partial class Constructable_ProgressDeconstruction_Patch : NitroxP

public static void Prefix(Constructable __instance)
{
if (__instance.constructedAmount <= 0f &&
__instance.transform.parent && __instance.transform.parent.TryGetComponent(out NitroxCyclops nitroxCyclops))
if (__instance.constructedAmount <= 0f && __instance.transform.parent &&
__instance.transform.parent.TryGetComponent(out NitroxCyclops nitroxCyclops) && nitroxCyclops.Virtual)
{
nitroxCyclops.Virtual.UnregisterConstructable(__instance.gameObject);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;
using NitroxClient.GameLogic;
using NitroxClient.GameLogic.Simulation;
using NitroxClient.MonoBehaviours;
Expand Down Expand Up @@ -45,7 +45,11 @@ private static void ReceivedSimulationLockResponse(NitroxId id, bool lockAquired
{
if (lockAquired)
{
EntityPositionBroadcaster.WatchEntity(id);
// In case what we grabbed wasn't a vehicle, we'll be watching it with the regular entity position broadcast system
if (!Resolve<SimulationOwnership>().TreatVehicleEntity(id, true, SimulationLockType.EXCLUSIVE))
{
EntityPositionBroadcaster.WatchEntity(id);
}

skipPrefixPatch = true;
context.Cannon.GrabObject(context.GrabbedObject);
Expand Down

0 comments on commit b6b86ca

Please sign in to comment.