From 57beb780d8470ff0d7d9ee344fd1f4fe8f65c98c Mon Sep 17 00:00:00 2001 From: Eboreg2 <157553222+Eboreg2@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:14:46 -0400 Subject: [PATCH 1/6] Update MissilePatch.cs Updated to fit new coding conventions --- SmartUse/MissilePatch.cs | 58 +++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/SmartUse/MissilePatch.cs b/SmartUse/MissilePatch.cs index 11e2f91..9e604b0 100644 --- a/SmartUse/MissilePatch.cs +++ b/SmartUse/MissilePatch.cs @@ -9,7 +9,6 @@ using XRL.World.AI; using System.Linq; using XRL.UI; -using LiveAndThink.Logic; /// /// A collection of Harmony patches that make creatures @@ -79,28 +78,12 @@ static bool MissileSafetyCheck(GameObject missileWeapon, Brain shooterBrain, Cel } GameObject projectile = null; string blueprint = null; - // We need a special exception here to handle stuff like bows and launchers - // that use existing objects as projectiles instead of just summoning them from a blueprint. - MagazineAmmoLoader magazineAmmoLoader = missileWeapon.GetPart(); - if (magazineAmmoLoader != null) - { - projectile = GetProjectileObjectEvent.GetFor(magazineAmmoLoader.Ammo, missileWeapon); - if (projectile != null && projectile.IsInvalid()) - { - projectile = null; // try again - } - } - // If we still don't have a projectile, try the normal event + GetMissileWeaponProjectileEvent.GetFor(missileWeapon, ref projectile, ref blueprint); if (projectile == null) { - GetMissileWeaponProjectileEvent.GetFor(missileWeapon, ref projectile, ref blueprint); - // If we don't have one after that, create a sample object from the ammo blueprint - if (projectile == null && blueprint != null) - { - projectile = GameObject.createSample(blueprint); - } + projectile = GameObject.CreateSample(blueprint); } - if(!GameObject.validate(projectile) || projectile.IsInGraveyard()) + if(!GameObject.Validate(projectile) || projectile.IsInGraveyard()) { UnityEngine.Debug.Log($"Missile weapon {missileWeapon.DebugName} has no projectile!"); return true; @@ -117,9 +100,9 @@ static bool MissileSafetyCheck(GameObject missileWeapon, Brain shooterBrain, Cel List bystanders = targetCell.FastFloodVisibility("Combat", dangerRadius); foreach (GameObject bystander in bystanders) { - UnityEngine.Debug.Log($"Thrower {shooterBrain.ParentObject.DebugName} is {shooterBrain.GetOpinion(bystander)} to {bystander.DebugName}"); + UnityEngine.Debug.Log($"Thrower {shooterBrain.ParentObject.DebugName} is {shooterBrain.GetFeelingLevel(bystander)} to {bystander.DebugName}"); UnityEngine.Debug.Log($"Bystander {bystander.DebugName} {(CanEndangerAlly(projectile, bystander) ? "can" : "cannot")} be damaged by {projectile.GetType().Name}"); - if (shooterBrain.IsBystander(bystander, includeSelf: true) && CanEndangerAlly(projectile, bystander)) + if (!shooterBrain.IsHostileTowards(bystander) && CanEndangerAlly(projectile, bystander)) { return false; } @@ -139,10 +122,9 @@ static bool MissileSafetyCheck(GameObject missileWeapon, Brain shooterBrain, Cel static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) { var codes = new List(instructions); - CodeInstruction ld10 = codes.Find(x => x.opcode == OpCodes.Ldloc_S && ((LocalBuilder)x.operand).LocalIndex == (byte) 10); - LocalBuilder local10 = (LocalBuilder) ld10.operand; + LocalBuilder local9 = (LocalBuilder) codes.Find(x => x.opcode == OpCodes.Ldloc_S && ((LocalBuilder)x.operand).LocalIndex == (byte) 9).operand; int bridx = -1; - for (int i=codes.IndexOf(ld10); i x.Is(OpCodes.Ldstr, "AIWantUseWeapon")); i Transpiler(IEnumerable inst } CodeInstruction leaveInstruction = codes[bridx].Clone(); codes.InsertRange(bridx+1, new CodeInstruction[] { - // ldloc.s local10 - new CodeInstruction(OpCodes.Ldloc_S, local10), + // ldloc.s local9 + new CodeInstruction(OpCodes.Ldloc_S, local9), // ldarg.0 // ldfld class XRL.Game.Brain XRL.Game.AI.GoalHandler::ParentBrain new CodeInstruction(OpCodes.Ldarg_0), CodeInstruction.LoadField(typeof(GoalHandler), nameof(GoalHandler.ParentBrain)), - // ldloc.3 - new CodeInstruction(OpCodes.Ldloc_3), + // ldloc.2 + new CodeInstruction(OpCodes.Ldloc_2), // call MissilePatch::MissileSafetyCheck(GameObject, Brain, Cell) CodeInstruction.Call(typeof(MissilePatch), nameof(MissileSafetyCheck)), leaveInstruction}); return codes; } + + /// + /// Modify MagazineAmmoLoader.HandleEvent(GetMissileWeaponProjectileEvent E) to work with bows and launchers + /// by setting E.Projectile to Ammo.GetProjectileObjectEvent.GetFor(Ammo, ParentObject) if Ammo is valid/non-null. + /// + [HarmonyPatch(typeof(MagazineAmmoLoader), nameof(MagazineAmmoLoader.HandleEvent), new Type[] { typeof(GetMissileWeaponProjectileEvent) })] + static bool Prefix(MagazineAmmoLoader __instance, GetMissileWeaponProjectileEvent E, ref bool __result) + { + if (__instance.Ammo != null && __instance.Ammo.IsValid()) + { + E.Projectile = GetProjectileObjectEvent.GetFor(__instance.Ammo, __instance.ParentObject); + __result = false; + return false; + } + return true; + } } -} \ No newline at end of file +} From e65b9a717e154a31a1e388f5fe5ea5b88c8846d1 Mon Sep 17 00:00:00 2001 From: Eboreg2 <157553222+Eboreg2@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:20:28 -0400 Subject: [PATCH 2/6] Update FiringConePatch.cs --- SmartUse/FiringConePatch.cs | 46 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/SmartUse/FiringConePatch.cs b/SmartUse/FiringConePatch.cs index bc83d17..2fd2158 100644 --- a/SmartUse/FiringConePatch.cs +++ b/SmartUse/FiringConePatch.cs @@ -34,7 +34,7 @@ public static class FiringConePatch static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) { var codes = new List(instructions); - LocalBuilder local10 = (LocalBuilder) codes.Find(x => x.opcode == OpCodes.Ldloc_S && ((LocalBuilder)x.operand).LocalIndex == (byte) 10).operand; + LocalBuilder local9 = (LocalBuilder) codes.Find(x => x.opcode == OpCodes.Ldloc_S && ((LocalBuilder)x.operand).LocalIndex == (byte) 9).operand; int callidx = codes.FindIndex(x => x.Calls(AccessTools.Method(typeof(Zone), "Line"))); int startidx = callidx - 8; if (callidx == -1) @@ -43,15 +43,16 @@ static IEnumerable Transpiler(IEnumerable inst } codes[callidx] = CodeInstruction.Call(typeof(FiringConePatch), nameof(GetFiringCone)); // ldarg.0; call get ParentObject - // ldloc.0; - // ldloc.s 10 + // ldarg.0; call get Target + // ldloc.s 9 // List