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;
|
2021-07-31 09:45:38 +01:00
|
|
|
|
using QSB.Player;
|
2021-06-15 18:55:54 +01:00
|
|
|
|
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
|
|
|
|
|
2020-12-31 12:10:55 +00:00
|
|
|
|
namespace QSB.DeathSync.Patches
|
2020-08-13 21:46:16 +02:00
|
|
|
|
{
|
2021-10-15 21:06:51 +01: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
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
// TODO : Remove with future functionality.
|
|
|
|
|
[HarmonyPrefix]
|
|
|
|
|
[HarmonyPatch(typeof(ShipEjectionSystem), nameof(ShipEjectionSystem.OnPressInteract))]
|
|
|
|
|
public static bool DisableEjection()
|
|
|
|
|
=> false;
|
2021-02-07 11:09:05 +00:00
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
[HarmonyPrefix]
|
|
|
|
|
[HarmonyPatch(typeof(PlayerResources), nameof(PlayerResources.OnImpact))]
|
2021-12-26 21:25:26 -08:00
|
|
|
|
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);
|
2021-12-26 21:25:26 -08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
[HarmonyPrefix]
|
|
|
|
|
[HarmonyPatch(typeof(HighSpeedImpactSensor), nameof(HighSpeedImpactSensor.FixedUpdate))]
|
2021-05-25 10:04:26 +01:00
|
|
|
|
public static bool HighSpeedImpactSensor_FixedUpdate(
|
2021-12-26 21:25:26 -08:00
|
|
|
|
HighSpeedImpactSensor __instance
|
2021-05-25 10:04:26 +01:00
|
|
|
|
)
|
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
if (__instance._isPlayer && (PlayerState.IsAttached() || PlayerState.IsInsideShuttle() || PlayerState.UsingNomaiRemoteCamera()))
|
2021-05-25 10:04:26 +01:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:25:26 -08:00
|
|
|
|
if (__instance._dieNextUpdate && !__instance._dead)
|
2021-05-25 10:04:26 +01:00
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
__instance._dead = true;
|
|
|
|
|
__instance._dieNextUpdate = false;
|
2021-05-25 10:04:26 +01:00
|
|
|
|
if (__instance.gameObject.CompareTag("Player"))
|
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:25:26 -08:00
|
|
|
|
if (__instance._isPlayer && PlayerState.IsInsideShip())
|
2021-05-25 10:04:26 +01:00
|
|
|
|
{
|
|
|
|
|
var shipCenter = Locator.GetShipTransform().position + (Locator.GetShipTransform().up * 2f);
|
2021-12-26 21:25:26 -08:00
|
|
|
|
var distanceFromShip = Vector3.Distance(__instance._body.GetPosition(), shipCenter);
|
2021-05-25 10:04:26 +01:00
|
|
|
|
if (distanceFromShip > 8f)
|
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
__instance._body.SetPosition(shipCenter);
|
2021-05-25 10:04:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:25:26 -08:00
|
|
|
|
if (!__instance._dead)
|
2021-05-25 10:04:26 +01:00
|
|
|
|
{
|
2021-12-26 21:25:26 -08: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
|
|
|
|
{
|
2021-12-26 21:25:26 -08: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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:25:26 -08:00
|
|
|
|
var passiveReferenceFrame = __instance._sectorDetector.GetPassiveReferenceFrame();
|
|
|
|
|
if (!__instance._dead && passiveReferenceFrame != null)
|
2021-05-25 10:04:26 +01:00
|
|
|
|
{
|
2021-12-26 21:25:26 -08: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
|
|
|
|
{
|
2021-12-26 21:25:26 -08: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++)
|
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
if (__instance._raycastHits[i].rigidbody.mass > 10f && !__instance._raycastHits[i].rigidbody.Equals(__instance._body.GetRigidbody()))
|
2021-05-25 10:04:26 +01:00
|
|
|
|
{
|
2021-12-26 21:25:26 -08: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
|
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
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
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
__instance._body.AddVelocityChange(-a2);
|
|
|
|
|
__instance._impactSpeed = a2.magnitude;
|
2021-05-25 10:04:26 +01:00
|
|
|
|
if (!PlayerState.IsInsideTheEye())
|
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
__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
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 21:06:51 +01: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
|
|
|
|
|
2021-12-19 19:47:40 +00: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();
|
2021-12-19 19:47:40 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-07-31 09:45:38 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
RespawnOnDeath.Instance.ResetPlayer();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-13 21:46:16 +02:00
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
[HarmonyPostfix]
|
|
|
|
|
[HarmonyPatch(typeof(DeathManager), nameof(DeathManager.KillPlayer))]
|
2021-12-19 19:47:40 +00:00
|
|
|
|
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();
|
2021-12-19 19:47:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-23 15:51:45 +02:00
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
[HarmonyPostfix]
|
|
|
|
|
[HarmonyPatch(typeof(ShipDamageController), nameof(ShipDamageController.Awake))]
|
2021-12-26 21:25:26 -08:00
|
|
|
|
public static void ShipDamageController_Awake(ShipDamageController __instance)
|
|
|
|
|
=> __instance._exploded = true;
|
2021-02-07 11:09:05 +00:00
|
|
|
|
|
2021-10-15 21:06:51 +01:00
|
|
|
|
[HarmonyPrefix]
|
|
|
|
|
[HarmonyPatch(typeof(DestructionVolume), nameof(DestructionVolume.VanishShip))]
|
2021-12-26 21:25:26 -08:00
|
|
|
|
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)
|
2021-06-15 18:55:54 +01:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 11:26:13 +00:00
|
|
|
|
if (PlayerState.IsInsideShip() || PlayerState.UsingShipComputer() || PlayerState.AtFlightConsole())
|
|
|
|
|
{
|
2021-12-26 21:25:26 -08:00
|
|
|
|
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
|
|
|
|
}
|