|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Reflection; |
| 3 | +using System.Reflection.Emit; |
| 4 | +using HarmonyLib; |
| 5 | +using NitroxClient.GameLogic; |
| 6 | +using NitroxClient.GameLogic.PlayerLogic; |
| 7 | +using NitroxModel.DataStructures; |
| 8 | +using NitroxModel.Helper; |
| 9 | +using UnityEngine; |
| 10 | + |
| 11 | +namespace NitroxPatcher.Patches.Dynamic; |
| 12 | + |
| 13 | +public sealed partial class PrisonPredatorSwimToPlayer_Perform_Patch : NitroxPatch, IDynamicPatch |
| 14 | +{ |
| 15 | + internal static readonly MethodInfo TARGET_METHOD = Reflect.Method((PrisonPredatorSwimToPlayer t) => t.Evaluate(default(Creature), default(float))); |
| 16 | + |
| 17 | + internal static readonly EcoRegion.TargetFilter isTargetValidFilter = new(IsTargetValid); |
| 18 | + |
| 19 | + public static bool Prefix(PrisonPredatorSwimToPlayer __instance) |
| 20 | + { |
| 21 | + if (!__instance.TryGetNitroxId(out NitroxId creatureId) || |
| 22 | + Resolve<SimulationOwnership>().HasAnyLockType(creatureId)) |
| 23 | + { |
| 24 | + return true; |
| 25 | + } |
| 26 | + |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) |
| 31 | + { |
| 32 | + Label jumpLabel = generator.DefineLabel(); |
| 33 | + |
| 34 | + return new CodeMatcher(instructions).MatchStartForward([ |
| 35 | + new CodeMatch((i) => i.opcode == OpCodes.Bge_Un || i.opcode == OpCodes.Bge_Un_S), |
| 36 | + new CodeMatch(OpCodes.Ldc_R4, 0f), |
| 37 | + new CodeMatch(OpCodes.Ret), |
| 38 | + new CodeMatch(OpCodes.Ldsfld, Reflect.Field(() => Player.main)) |
| 39 | + ]) |
| 40 | + .Set(OpCodes.Bge_Un, jumpLabel) |
| 41 | + .MatchStartForward(new CodeMatch(OpCodes.Ldsfld, Reflect.Field(() => Player.main))) |
| 42 | + .RemoveInstructions(36) |
| 43 | + .InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_1) |
| 44 | + { |
| 45 | + labels = [jumpLabel] |
| 46 | + }) |
| 47 | + .Insert(new CodeInstruction(OpCodes.Call, Reflect.Method(() => EvaluatePriority(default)))) |
| 48 | + .InstructionEnumeration(); |
| 49 | + } |
| 50 | + |
| 51 | + public static bool EvaluatePriority(Creature creature) |
| 52 | + { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + public static bool IsTargetValid(IEcoTarget ecoTarget) |
| 57 | + { |
| 58 | + GameObject target = ecoTarget.GetGameObject(); |
| 59 | + if (!target) |
| 60 | + { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + if (target == Player.main.gameObject && !Player.main.CanBeAttacked()) |
| 65 | + { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + if (target.TryGetComponent(out RemotePlayerIdentifier remotePlayerIdentifier) && !remotePlayerIdentifier.RemotePlayer.CanBeAttacked()) |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + public static IEcoTarget GetNearestTarget(PrisonPredatorSwimToPlayer creatureAction) |
| 78 | + { |
| 79 | + IEcoTarget ecoTarget = EcoRegionManager.main.FindNearestTarget( |
| 80 | + RemotePlayer.PLAYER_ECO_TARGET_TYPE, |
| 81 | + creatureAction.transform.position, |
| 82 | + isTargetValidFilter, |
| 83 | + maxRings: 1 |
| 84 | + ); |
| 85 | + |
| 86 | + return ecoTarget; |
| 87 | + } |
| 88 | +} |
0 commit comments