Skip to content

Commit

Permalink
add some Kinematic prevention patches
Browse files Browse the repository at this point in the history
  • Loading branch information
killzoms committed Aug 16, 2023
1 parent 2da9191 commit ec215e7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 7 deletions.
13 changes: 10 additions & 3 deletions NitroxClient/MonoBehaviours/MovementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void Start()
private void Update()
{
BeforeUpdate();
if (!rigidbody)
if (!rigidbody && IsMoving)
{
transform.position = Vector3.SmoothDamp(transform.position, TargetPosition, ref Velocity, Scalar * Time.deltaTime);
transform.rotation = Quaternion.Lerp(transform.rotation, TargetRotation, Scalar * Time.deltaTime);
Expand All @@ -44,7 +44,7 @@ private void Update()
private void FixedUpdate()
{
BeforeFixedUpdate();
if (rigidbody)
if (rigidbody && IsMoving)
{
float timing = Scalar * Time.fixedDeltaTime;
Vector3 newPos = Vector3.SmoothDamp(transform.position, TargetPosition, ref Velocity, timing);
Expand All @@ -61,7 +61,14 @@ private void FixedUpdate()

Quaternion delta = TargetRotation * transform.rotation.GetInverse();
delta.ToAngleAxis(out float angle, out Vector3 axis);
rigidbody.angularVelocity = .9f * Mathf.Deg2Rad * angle / timing * axis;
if (!float.IsInfinity(axis.x) && !float.IsInfinity(axis.y) && !float.IsInfinity(axis.z))
{
rigidbody.angularVelocity = .9f * Mathf.Deg2Rad * angle / timing * axis;
}
else
{
rigidbody.angularVelocity = Vector3.zero;
}
}
}
AfterFixedUpdate();
Expand Down
1 change: 1 addition & 0 deletions NitroxModel/DataStructures/GameLogic/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class Entity
[DataMember(Order = 4)]
public NitroxId ParentId { get; set; }

[DataMember(Order = 5)]
public List<Entity> ChildEntities { get; set; } = new List<Entity>();

[IgnoreConstructor]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using NitroxClient.GameLogic;
using NitroxClient.MonoBehaviours;
using NitroxModel.Helper;
using UnityEngine;

namespace NitroxPatcher.Patches.Dynamic
{
public sealed partial class FreezeRigidbodyWhenFar_FixedUpdate_Patch : NitroxPatch, IDynamicPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((FreezeRigidbodyWhenFar t) => t.FixedUpdate());

public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> instructionList = instructions.ToList();
for (int i = 0; i < instructionList.Count; i++)
{
CodeInstruction instruction = instructionList[i];

if (instruction.opcode == OpCodes.Call && instruction.operand.Equals(Reflect.Method((Component c) => c.GetComponent<Rigidbody>())))
{
yield return instruction;
yield return instructionList[i+1];
object jmpLabel = null;

for (int j = i; j < instructionList.Count; j++) // search for branch instruction
{
if (instructionList[j].opcode == OpCodes.Ble_Un)
{
jmpLabel = instructionList[j].operand;
break;
}
}
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call, Reflect.Property((Component c) => c.gameObject).GetGetMethod());
yield return new CodeInstruction(OpCodes.Call, Reflect.Method(() => IsMoving(default)));
yield return new CodeInstruction(OpCodes.Brtrue, jmpLabel);
i = i + 1;
continue;
}

yield return instruction;
}
}

public static bool IsMoving(GameObject go)
{
return go.TryGetComponent(out MovementController mc) && mc.IsMoving;
}
}
}
20 changes: 20 additions & 0 deletions NitroxPatcher/Patches/Dynamic/Vehicle_ShouldSetKinematic_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Reflection;
using NitroxClient.MonoBehaviours;
using NitroxModel.Helper;

namespace NitroxPatcher.Patches.Dynamic;

public sealed partial class Vehicle_ShouldSetKinematic_Patch : NitroxPatch, IDynamicPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((Vehicle t) => t.ShouldSetKinematic());

public static bool Prefix(Vehicle __instance, ref bool __result)
{
if (__instance.TryGetComponent(out MovementController movementController))
{
__result = !movementController.IsMoving;
return false;
}
return true;
}
}
5 changes: 1 addition & 4 deletions NitroxServer/GameLogic/Entities/EntityData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ private void ProtoAfterDeserialization()
// TODO: Rework system to no longer persist children entities because they are duplicates.
foreach (Entity entity in Entities)
{
if (entity.ChildEntities == null)
{
entity.ChildEntities = new List<Entity>();
}
entity.ChildEntities.Clear();
}

foreach (Entity entity in Entities)
Expand Down

0 comments on commit ec215e7

Please sign in to comment.