221 lines
6.7 KiB
C#
Raw Normal View History

2021-10-12 15:32:24 +01:00
using HarmonyLib;
2021-12-23 17:07:29 -08:00
using QSB.DeathSync.Messages;
using QSB.Messaging;
2020-12-14 20:31:31 +01:00
using QSB.Patches;
using QSB.Player;
using QSB.ShipSync;
2022-01-14 22:31:48 -08:00
using QSB.ShipSync.TransformSync;
2021-05-25 10:04:26 +01:00
using QSB.Utility;
2020-12-14 21:20:53 +00:00
using System.Linq;
2021-05-25 10:04:26 +01:00
using UnityEngine;
2020-08-13 21:46:16 +02:00
namespace QSB.DeathSync.Patches
2020-08-13 21:46:16 +02:00
{
[HarmonyPatch]
2020-12-02 21:23:01 +00:00
public class DeathPatches : QSBPatch
{
2021-03-25 21:10:20 +00:00
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
2020-11-03 21:11:10 +00:00
// TODO : Remove with future functionality.
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipEjectionSystem), nameof(ShipEjectionSystem.OnPressInteract))]
public static bool DisableEjection()
=> false;
// TODO : Remove with future functionality.
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipDetachableLeg), nameof(ShipDetachableLeg.Detach))]
2021-06-18 21:54:32 +01:00
public static bool ShipDetachableLeg_Detach(ref OWRigidbody __result)
2021-02-09 17:18:01 +00:00
{
2021-06-18 21:54:32 +01:00
__result = null;
return false;
}
// TODO : Remove with future functionality.
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipDetachableModule), nameof(ShipDetachableModule.Detach))]
2021-06-18 21:54:32 +01:00
public static bool ShipDetachableModule_Detach(ref OWRigidbody __result)
{
__result = null;
return false;
2021-05-25 10:04:26 +01:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerResources), nameof(PlayerResources.OnImpact))]
public static bool PlayerResources_OnImpact(PlayerResources __instance, ImpactData impact)
2021-05-25 10:04:26 +01:00
{
if (PlayerState.IsInsideShip())
{
return false;
}
2021-05-25 17:09:36 +01:00
2021-05-25 10:04:26 +01:00
var speed = Mathf.Clamp01((impact.speed - __instance.GetMinImpactSpeed()) / (__instance.GetMaxImpactSpeed() - __instance.GetMinImpactSpeed()));
var tookDamage = __instance.ApplyInstantDamage(100f * speed, InstantDamageType.Impact);
if (tookDamage && __instance._currentHealth <= 0f && !PlayerState.IsDead())
2021-05-25 10:04:26 +01:00
{
Locator.GetDeathManager().SetImpactDeathSpeed(impact.speed);
Locator.GetDeathManager().KillPlayer(DeathType.Impact);
}
2021-06-18 22:38:32 +01:00
2021-05-25 10:04:26 +01:00
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(HighSpeedImpactSensor), nameof(HighSpeedImpactSensor.FixedUpdate))]
2021-05-25 10:04:26 +01:00
public static bool HighSpeedImpactSensor_FixedUpdate(
HighSpeedImpactSensor __instance
2021-05-25 10:04:26 +01:00
)
{
if (__instance._isPlayer && (PlayerState.IsAttached() || PlayerState.IsInsideShuttle() || PlayerState.UsingNomaiRemoteCamera()))
2021-05-25 10:04:26 +01:00
{
return false;
}
if (__instance._dieNextUpdate && !__instance._dead)
2021-05-25 10:04:26 +01:00
{
__instance._dead = true;
__instance._dieNextUpdate = false;
2021-05-25 10:04:26 +01:00
if (__instance.gameObject.CompareTag("Player"))
{
Locator.GetDeathManager().SetImpactDeathSpeed(__instance._impactSpeed);
2021-05-25 10:04:26 +01:00
Locator.GetDeathManager().KillPlayer(DeathType.Impact);
}
else if (__instance.gameObject.CompareTag("Ship"))
{
2021-12-26 21:08:36 -08:00
__instance.GetComponent<ShipDamageController>().Explode();
2021-05-25 10:04:26 +01:00
}
}
if (__instance._isPlayer && PlayerState.IsInsideShip())
2021-05-25 10:04:26 +01:00
{
var shipCenter = Locator.GetShipTransform().position + (Locator.GetShipTransform().up * 2f);
var distanceFromShip = Vector3.Distance(__instance._body.GetPosition(), shipCenter);
2021-05-25 10:04:26 +01:00
if (distanceFromShip > 8f)
{
__instance._body.SetPosition(shipCenter);
2021-05-25 10:04:26 +01:00
}
if (!__instance._dead)
2021-05-25 10:04:26 +01:00
{
var a = __instance._body.GetVelocity() - Locator.GetShipBody().GetPointVelocity(__instance._body.GetPosition());
if (a.sqrMagnitude > __instance._sqrCheckSpeedThreshold)
2021-05-25 10:04:26 +01:00
{
__instance._impactSpeed = a.magnitude;
__instance._body.AddVelocityChange(-a);
2021-05-25 10:04:26 +01:00
}
}
2021-06-18 22:38:32 +01:00
2021-05-25 10:04:26 +01:00
return false;
}
var passiveReferenceFrame = __instance._sectorDetector.GetPassiveReferenceFrame();
if (!__instance._dead && passiveReferenceFrame != null)
2021-05-25 10:04:26 +01:00
{
var relativeVelocity = __instance._body.GetVelocity() - passiveReferenceFrame.GetOWRigidBody().GetPointVelocity(__instance._body.GetPosition());
if (relativeVelocity.sqrMagnitude > __instance._sqrCheckSpeedThreshold)
2021-05-25 10:04:26 +01:00
{
var hitCount = Physics.RaycastNonAlloc(__instance.transform.TransformPoint(__instance._localOffset), relativeVelocity, __instance._raycastHits, (relativeVelocity.magnitude * Time.deltaTime) + __instance._radius, OWLayerMask.physicalMask, QueryTriggerInteraction.Ignore);
2021-05-25 10:04:26 +01:00
for (var i = 0; i < hitCount; i++)
{
if (__instance._raycastHits[i].rigidbody.mass > 10f && !__instance._raycastHits[i].rigidbody.Equals(__instance._body.GetRigidbody()))
2021-05-25 10:04:26 +01:00
{
var owRigidbody = __instance._raycastHits[i].rigidbody.GetComponent<OWRigidbody>();
2021-05-25 10:04:26 +01:00
if (owRigidbody == null)
{
DebugLog.ToConsole("Rigidbody does not have attached OWRigidbody!!!", OWML.Common.MessageType.Error);
Debug.Break();
}
else
{
relativeVelocity = __instance._body.GetVelocity() - owRigidbody.GetPointVelocity(__instance._body.GetPosition());
var a2 = Vector3.Project(relativeVelocity, __instance._raycastHits[i].normal);
if (a2.sqrMagnitude > __instance._sqrCheckSpeedThreshold)
2021-05-25 10:04:26 +01:00
{
__instance._body.AddVelocityChange(-a2);
__instance._impactSpeed = a2.magnitude;
2021-05-25 10:04:26 +01:00
if (!PlayerState.IsInsideTheEye())
{
__instance._dieNextUpdate = true;
2021-05-25 10:04:26 +01:00
}
2021-06-18 22:38:32 +01:00
2021-05-25 10:04:26 +01:00
break;
}
}
}
}
}
}
return false;
2021-02-09 17:18:01 +00:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(DeathManager), nameof(DeathManager.KillPlayer))]
2021-06-18 21:54:32 +01:00
public static bool DeathManager_KillPlayer_Prefix(DeathType deathType)
2020-12-02 21:23:01 +00:00
{
2020-12-19 18:49:46 +00:00
if (RespawnOnDeath.Instance == null)
{
return true;
}
2020-12-02 21:23:01 +00:00
if (RespawnOnDeath.Instance.AllowedDeathTypes.Contains(deathType))
{
return true;
}
2020-08-13 21:46:16 +02:00
if (QSBPlayerManager.LocalPlayer.IsDead)
{
return false;
}
var deadPlayersCount = QSBPlayerManager.PlayerList.Count(x => x.IsDead);
if (deadPlayersCount == QSBPlayerManager.PlayerList.Count - 1)
{
2021-12-22 22:26:53 -08:00
new EndLoopMessage().Send();
return true;
}
2020-12-02 21:23:01 +00:00
RespawnOnDeath.Instance.ResetPlayer();
return false;
}
2020-08-13 21:46:16 +02:00
[HarmonyPostfix]
[HarmonyPatch(typeof(DeathManager), nameof(DeathManager.KillPlayer))]
public static void DeathManager_KillPlayer_Postfix(DeathType deathType)
{
if (!QSBPlayerManager.LocalPlayer.IsDead)
{
2021-12-19 21:38:50 +00:00
QSBPlayerManager.LocalPlayer.IsDead = true;
2021-12-22 20:45:25 -08:00
new PlayerDeathMessage(deathType).Send();
}
}
2020-08-23 15:51:45 +02:00
[HarmonyPostfix]
[HarmonyPatch(typeof(ShipDamageController), nameof(ShipDamageController.Awake))]
public static void ShipDamageController_Awake(ShipDamageController __instance)
=> __instance._exploded = true;
[HarmonyPrefix]
[HarmonyPatch(typeof(DestructionVolume), nameof(DestructionVolume.VanishShip))]
public static bool DestructionVolume_VanishShip(DestructionVolume __instance)
2021-02-07 11:26:13 +00:00
{
2021-02-07 12:51:35 +00:00
if (RespawnOnDeath.Instance == null)
{
return true;
}
2022-01-14 22:31:48 -08:00
if (!ShipTransformSync.LocalInstance.hasAuthority)
{
return false;
}
2021-02-07 11:26:13 +00:00
if (PlayerState.IsInsideShip() || PlayerState.UsingShipComputer() || PlayerState.AtFlightConsole())
{
Locator.GetDeathManager().KillPlayer(__instance._deathType);
2021-02-07 11:26:13 +00:00
}
2021-06-20 14:33:14 +01:00
return true;
2021-02-07 11:26:13 +00:00
}
2020-12-02 21:23:01 +00:00
}
2020-08-13 21:46:16 +02:00
}