257 lines
7.8 KiB
C#
Raw Normal View History

using HarmonyLib;
using OWML.Utils;
2021-04-13 17:25:00 +01:00
using QSB.Events;
2021-12-24 20:07:50 -08:00
using QSB.Messaging;
2021-04-12 11:31:21 +01:00
using QSB.Patches;
2021-12-24 20:07:50 -08:00
using QSB.ShipSync.Messages.Component;
using QSB.ShipSync.Messages.Hull;
2021-12-23 17:07:29 -08:00
using QSB.ShipSync.TransformSync;
2021-12-24 20:07:50 -08:00
using QSB.ShipSync.WorldObjects;
2021-04-13 21:09:26 +01:00
using QSB.Utility;
using QSB.WorldSync;
2021-12-07 15:56:08 +00:00
using System;
2021-04-12 12:02:08 +01:00
using UnityEngine;
2021-04-12 11:31:21 +01:00
namespace QSB.ShipSync.Patches
{
[HarmonyPatch]
2021-05-15 21:31:29 +01:00
internal class ShipPatches : QSBPatch
2021-04-12 11:31:21 +01:00
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(typeof(HatchController), nameof(HatchController.OnPressInteract))]
2021-04-12 11:31:21 +01:00
public static bool HatchController_OnPressInteract()
{
2021-04-13 17:25:00 +01:00
if (!PlayerState.IsInsideShip())
{
2021-04-13 18:50:15 +01:00
ShipManager.Instance.ShipTractorBeam.ActivateTractorBeam();
2021-06-14 16:13:32 +01:00
QSBEventManager.FireEvent(EventNames.QSBEnableFunnel);
2021-04-13 17:25:00 +01:00
}
2021-06-18 22:38:32 +01:00
QSBEventManager.FireEvent(EventNames.QSBHatchState, true);
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(HatchController), nameof(HatchController.OnEntry))]
2021-04-12 12:02:08 +01:00
public static bool HatchController_OnEntry(GameObject hitObj)
{
if (hitObj.CompareTag("PlayerDetector"))
{
QSBEventManager.FireEvent(EventNames.QSBHatchState, false);
}
2021-06-18 22:38:32 +01:00
2021-04-12 11:31:21 +01:00
return true;
}
2021-04-13 17:25:00 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipTractorBeamSwitch), nameof(ShipTractorBeamSwitch.OnTriggerExit))]
public static bool ShipTractorBeamSwitch_OnTriggerExit(ShipTractorBeamSwitch __instance, Collider hitCollider)
2021-04-13 17:25:00 +01:00
{
if (!__instance._isPlayerInShip && __instance._functional && hitCollider.CompareTag("PlayerDetector") && !ShipManager.Instance.HatchController._hatchObject.activeSelf)
2021-04-13 17:25:00 +01:00
{
2021-04-13 18:50:15 +01:00
ShipManager.Instance.HatchController.Invoke("CloseHatch");
ShipManager.Instance.ShipTractorBeam.DeactivateTractorBeam();
2021-04-13 17:25:00 +01:00
QSBEventManager.FireEvent(EventNames.QSBHatchState, false);
}
2021-06-18 22:38:32 +01:00
2021-04-13 17:25:00 +01:00
return false;
}
2021-04-13 21:09:26 +01:00
[HarmonyReversePatch]
[HarmonyPatch(typeof(SingleInteractionVolume), nameof(SingleInteractionVolume.UpdateInteractVolume))]
2021-10-29 23:00:13 +01:00
public static void SingleInteractionVolume_UpdateInteractVolume_Stub(object instance) => throw new NotImplementedException();
[HarmonyPrefix]
[HarmonyPatch(typeof(InteractZone), nameof(InteractZone.UpdateInteractVolume))]
public static bool InteractZone_UpdateInteractVolume(InteractZone __instance)
2021-04-13 21:09:26 +01:00
{
/* Angle for interaction with the ship hatch
*
2021-04-13 21:09:26 +01:00
* \ 80° / - If in ship
* \ /
* \ /
* [=====] - Hatch
* / \
* / \
* / 280° \ - If not in ship
*
2021-04-13 21:09:26 +01:00
*/
if (!WorldObjectManager.AllObjectsReady || __instance != ShipManager.Instance.HatchInteractZone)
2021-04-13 21:09:26 +01:00
{
return true;
}
var angle = 2f * Vector3.Angle(__instance._playerCam.transform.forward, __instance.transform.forward);
2021-04-13 21:09:26 +01:00
__instance._focused = PlayerState.IsInsideShip()
2021-06-19 11:26:05 +01:00
? angle <= 80
2021-04-13 21:09:26 +01:00
: angle >= 280;
2021-10-29 23:00:13 +01:00
SingleInteractionVolume_UpdateInteractVolume_Stub(__instance);
2021-04-13 21:09:26 +01:00
return false;
}
2021-06-19 12:14:45 +01:00
[HarmonyReversePatch]
[HarmonyPatch(typeof(ShipComponent), nameof(ShipComponent.OnEnterShip))]
2021-10-29 23:00:13 +01:00
public static void ShipComponent_OnEnterShip_Stub(object instance) => throw new NotImplementedException();
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipElectricalComponent), nameof(ShipElectricalComponent.OnEnterShip))]
public static bool ShipElectricalComponent_OnEnterShip(ShipElectricalComponent __instance)
{
2021-10-29 23:00:13 +01:00
ShipComponent_OnEnterShip_Stub(__instance);
2021-06-19 12:14:45 +01:00
return false;
}
[HarmonyReversePatch]
[HarmonyPatch(typeof(ShipComponent), nameof(ShipComponent.OnExitShip))]
2021-10-29 23:00:13 +01:00
public static void ShipComponent_OnExitShip_Stub(object instance) => throw new NotImplementedException();
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipElectricalComponent), nameof(ShipElectricalComponent.OnExitShip))]
public static bool ShipElectricalComponent_OnExitShip(ShipElectricalComponent __instance)
2021-06-19 12:14:45 +01:00
{
2021-10-29 23:00:13 +01:00
ShipComponent_OnExitShip_Stub(__instance);
2021-06-19 12:14:45 +01:00
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipComponent), nameof(ShipComponent.SetDamaged))]
public static bool ShipComponent_SetDamaged(ShipComponent __instance, bool damaged)
2021-06-19 15:48:32 +01:00
{
if (__instance._damaged == damaged)
{
return false;
}
2021-12-25 16:54:34 -08:00
var qsbShipComponent = QSBWorldSync.GetWorldFromUnity<QSBShipComponent>(__instance);
if (damaged)
{
__instance._damaged = true;
__instance._repairFraction = 0f;
__instance.GetType().GetAnyMethod("OnComponentDamaged").Invoke(__instance, null);
2021-07-07 09:00:54 +01:00
__instance.RaiseEvent("OnDamaged", __instance);
2021-12-25 16:54:34 -08:00
qsbShipComponent
.SendMessage(new ComponentDamagedMessage());
}
else
{
__instance._damaged = false;
__instance._repairFraction = 1f;
__instance.GetType().GetAnyMethod("OnComponentRepaired").Invoke(__instance, null);
2021-07-07 09:00:54 +01:00
__instance.RaiseEvent("OnRepaired", __instance);
2021-12-25 16:54:34 -08:00
qsbShipComponent
2021-12-24 20:07:50 -08:00
.SendMessage(new ComponentRepairedMessage());
}
__instance.GetType().GetAnyMethod("UpdateColliderState").Invoke(__instance, null);
if (__instance._damageEffect)
{
__instance._damageEffect.SetEffectBlend(1f - __instance._repairFraction);
}
return false;
2021-06-19 15:48:32 +01:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipHull), nameof(ShipHull.FixedUpdate))]
public static bool ShipHull_FixedUpdate(ShipHull __instance, ref ImpactData ____dominantImpact, ref float ____integrity, ref bool ____damaged, DamageEffect ____damageEffect, ShipComponent[] ____components)
2021-06-19 15:48:32 +01:00
{
if (____dominantImpact != null)
{
var damage = Mathf.InverseLerp(30f, 200f, ____dominantImpact.speed);
if (damage > 0f)
{
var num2 = 0.15f;
if (damage < num2 && ____integrity > 1f - num2)
{
damage = num2;
}
____integrity = Mathf.Max(____integrity - damage, 0f);
if (!____damaged)
{
____damaged = true;
2021-07-07 09:00:54 +01:00
__instance.RaiseEvent("OnDamaged", __instance);
2021-12-25 16:54:34 -08:00
QSBWorldSync.GetWorldFromUnity<QSBShipHull>(__instance)
.SendMessage(new HullDamagedMessage());
}
if (____damageEffect != null)
{
____damageEffect.SetEffectBlend(1f - ____integrity);
}
QSBEventManager.FireEvent(EventNames.QSBHullChangeIntegrity, __instance, ____integrity);
}
foreach (var component in ____components)
{
if (!(component == null) && !component.isDamaged)
{
if (component.ApplyImpact(____dominantImpact))
{
break;
}
}
}
2021-07-07 09:00:54 +01:00
__instance.RaiseEvent("OnImpact", ____dominantImpact, damage);
QSBEventManager.FireEvent(EventNames.QSBHullImpact, __instance, ____dominantImpact, damage);
____dominantImpact = null;
}
__instance.enabled = false;
return false;
2021-06-19 15:48:32 +01:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipDamageController), nameof(ShipDamageController.OnImpact))]
2021-07-04 22:34:38 +01:00
public static bool ShipDamageController_OnImpact()
2021-12-12 03:14:00 -08:00
=> ShipTransformSync.LocalInstance == null || ShipManager.Instance.HasAuthority;
[HarmonyPostfix]
[HarmonyPatch(typeof(ShipComponent), nameof(ShipComponent.RepairTick))]
2021-12-24 21:14:23 -08:00
public static void ShipComponent_RepairTick(ShipComponent __instance) =>
QSBWorldSync.GetWorldFromUnity<QSBShipComponent>(__instance)
.SendMessage(new ComponentRepairTickMessage(__instance._repairFraction));
2021-06-20 12:43:02 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipHull), nameof(ShipHull.RepairTick))]
public static bool ShipHull_RepairTick(ShipHull __instance, ref float ____integrity, ref bool ____damaged, DamageEffect ____damageEffect, float ____repairTime)
2021-06-20 12:43:02 +01:00
{
if (!____damaged)
{
return false;
}
2021-11-20 19:49:50 +00:00
____integrity = Mathf.Min(____integrity + (Time.deltaTime / ____repairTime), 1f);
2021-12-24 21:14:23 -08:00
var qsbShipHull = QSBWorldSync.GetWorldFromUnity<QSBShipHull>(__instance);
qsbShipHull
.SendMessage(new HullRepairTickMessage(____integrity));
if (____integrity >= 1f)
{
____damaged = false;
2021-07-07 09:00:54 +01:00
__instance.RaiseEvent("OnRepaired", __instance);
2021-12-24 21:14:23 -08:00
qsbShipHull
2021-12-24 20:07:50 -08:00
.SendMessage(new HullRepairedMessage());
}
if (____damageEffect != null)
{
____damageEffect.SetEffectBlend(1f - ____integrity);
}
return false;
2021-06-20 12:43:02 +01:00
}
2021-04-12 11:31:21 +01:00
}
2021-12-24 21:14:23 -08:00
}