quantum-space-buddies/QSB/DeathSync/Patches/DeathPatches.cs

230 lines
6.7 KiB
C#
Raw Normal View History

2021-10-12 14:32:24 +00:00
using HarmonyLib;
2021-12-24 01:07:29 +00:00
using QSB.DeathSync.Messages;
using QSB.Messaging;
2020-12-14 19:31:31 +00:00
using QSB.Patches;
using QSB.Player;
using QSB.ShipSync;
2021-05-25 09:04:26 +00:00
using QSB.Utility;
2020-12-14 21:20:53 +00:00
using System.Linq;
2021-05-25 09:04:26 +00:00
using UnityEngine;
2020-08-13 19:46:16 +00:00
namespace QSB.DeathSync.Patches
2020-08-13 19:46:16 +00: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 20:54:32 +00:00
public static bool ShipDetachableLeg_Detach(ref OWRigidbody __result)
2021-02-09 17:18:01 +00:00
{
2021-06-18 20:54:32 +00:00
__result = null;
return false;
}
// TODO : Remove with future functionality.
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipDetachableModule), nameof(ShipDetachableModule.Detach))]
2021-06-18 20:54:32 +00:00
public static bool ShipDetachableModule_Detach(ref OWRigidbody __result)
{
__result = null;
return false;
2021-05-25 09:04:26 +00:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerResources), nameof(PlayerResources.OnImpact))]
2021-05-25 09:04:26 +00:00
public static bool PlayerResources_OnImpact(ImpactData impact, PlayerResources __instance, float ____currentHealth)
{
if (PlayerState.IsInsideShip())
{
return false;
}
2021-05-25 16:09:36 +00:00
2021-05-25 09:04:26 +00:00
var speed = Mathf.Clamp01((impact.speed - __instance.GetMinImpactSpeed()) / (__instance.GetMaxImpactSpeed() - __instance.GetMinImpactSpeed()));
var tookDamage = __instance.ApplyInstantDamage(100f * speed, InstantDamageType.Impact);
if (tookDamage && ____currentHealth <= 0f && !PlayerState.IsDead())
{
Locator.GetDeathManager().SetImpactDeathSpeed(impact.speed);
Locator.GetDeathManager().KillPlayer(DeathType.Impact);
}
2021-06-18 21:38:32 +00:00
2021-05-25 09:04:26 +00:00
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(HighSpeedImpactSensor), nameof(HighSpeedImpactSensor.FixedUpdate))]
2021-05-25 09:04:26 +00:00
public static bool HighSpeedImpactSensor_FixedUpdate(
HighSpeedImpactSensor __instance,
bool ____isPlayer,
ref bool ____dead,
ref bool ____dieNextUpdate,
OWRigidbody ____body,
ref float ____impactSpeed,
float ____sqrCheckSpeedThreshold,
RaycastHit[] ____raycastHits,
SectorDetector ____sectorDetector,
float ____radius,
Vector3 ____localOffset
)
{
if (____isPlayer && (PlayerState.IsAttached() || PlayerState.IsInsideShuttle() || PlayerState.UsingNomaiRemoteCamera()))
{
return false;
}
if (____dieNextUpdate && !____dead)
{
____dead = true;
____dieNextUpdate = false;
if (__instance.gameObject.CompareTag("Player"))
{
Locator.GetDeathManager().SetImpactDeathSpeed(____impactSpeed);
Locator.GetDeathManager().KillPlayer(DeathType.Impact);
}
else if (__instance.gameObject.CompareTag("Ship"))
{
__instance.GetComponent<ShipDamageController>().Explode(false);
}
}
if (____isPlayer && PlayerState.IsInsideShip())
{
var shipCenter = Locator.GetShipTransform().position + (Locator.GetShipTransform().up * 2f);
var distanceFromShip = Vector3.Distance(____body.GetPosition(), shipCenter);
if (distanceFromShip > 8f)
{
____body.SetPosition(shipCenter);
}
if (!____dead)
{
var a = ____body.GetVelocity() - Locator.GetShipBody().GetPointVelocity(____body.GetPosition());
if (a.sqrMagnitude > ____sqrCheckSpeedThreshold)
{
____impactSpeed = a.magnitude;
____body.AddVelocityChange(-a);
}
}
2021-06-18 21:38:32 +00:00
2021-05-25 09:04:26 +00:00
return false;
}
var passiveReferenceFrame = ____sectorDetector.GetPassiveReferenceFrame();
if (!____dead && passiveReferenceFrame != null)
{
var relativeVelocity = ____body.GetVelocity() - passiveReferenceFrame.GetOWRigidBody().GetPointVelocity(____body.GetPosition());
if (relativeVelocity.sqrMagnitude > ____sqrCheckSpeedThreshold)
{
var hitCount = Physics.RaycastNonAlloc(__instance.transform.TransformPoint(____localOffset), relativeVelocity, ____raycastHits, (relativeVelocity.magnitude * Time.deltaTime) + ____radius, OWLayerMask.physicalMask, QueryTriggerInteraction.Ignore);
for (var i = 0; i < hitCount; i++)
{
if (____raycastHits[i].rigidbody.mass > 10f && !____raycastHits[i].rigidbody.Equals(____body.GetRigidbody()))
{
var owRigidbody = ____raycastHits[i].rigidbody.GetComponent<OWRigidbody>();
if (owRigidbody == null)
{
DebugLog.ToConsole("Rigidbody does not have attached OWRigidbody!!!", OWML.Common.MessageType.Error);
Debug.Break();
}
else
{
relativeVelocity = ____body.GetVelocity() - owRigidbody.GetPointVelocity(____body.GetPosition());
var a2 = Vector3.Project(relativeVelocity, ____raycastHits[i].normal);
if (a2.sqrMagnitude > ____sqrCheckSpeedThreshold)
{
____body.AddVelocityChange(-a2);
____impactSpeed = a2.magnitude;
if (!PlayerState.IsInsideTheEye())
{
____dieNextUpdate = true;
}
2021-06-18 21:38:32 +00:00
2021-05-25 09:04:26 +00:00
break;
}
}
}
}
}
}
return false;
2021-02-09 17:18:01 +00:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(DeathManager), nameof(DeathManager.KillPlayer))]
2021-06-18 20:54:32 +00: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 19:46:16 +00:00
if (QSBPlayerManager.LocalPlayer.IsDead)
{
return false;
}
var deadPlayersCount = QSBPlayerManager.PlayerList.Count(x => x.IsDead);
if (deadPlayersCount == QSBPlayerManager.PlayerList.Count - 1)
{
2021-12-23 06:26:53 +00:00
new EndLoopMessage().Send();
return true;
}
2020-12-02 21:23:01 +00:00
RespawnOnDeath.Instance.ResetPlayer();
return false;
}
2020-08-13 19:46:16 +00: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-23 04:45:25 +00:00
new PlayerDeathMessage(deathType).Send();
}
}
2020-08-23 13:51:45 +00:00
[HarmonyPostfix]
[HarmonyPatch(typeof(ShipDamageController), nameof(ShipDamageController.Awake))]
2021-06-18 20:54:32 +00:00
public static void ShipDamageController_Awake(ref bool ____exploded)
2021-02-09 09:11:35 +00:00
=> ____exploded = true;
[HarmonyPrefix]
[HarmonyPatch(typeof(DestructionVolume), nameof(DestructionVolume.VanishShip))]
2021-02-07 11:26:13 +00:00
public static bool DestructionVolume_VanishShip(DeathType ____deathType)
{
2021-02-07 12:51:35 +00:00
if (RespawnOnDeath.Instance == null)
{
return true;
}
if (!ShipManager.Instance.HasAuthority)
{
return false;
}
2021-02-07 11:26:13 +00:00
if (PlayerState.IsInsideShip() || PlayerState.UsingShipComputer() || PlayerState.AtFlightConsole())
{
Locator.GetDeathManager().KillPlayer(____deathType);
}
2021-06-20 13:33:14 +00:00
return true;
2021-02-07 11:26:13 +00:00
}
2020-12-02 21:23:01 +00:00
}
2020-08-13 19:46:16 +00:00
}