From 6de4384443260590e346c61b9208f7a3768e3625 Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 2 Jan 2022 20:58:02 -0600 Subject: [PATCH 1/3] Fixed Rancor and Mechworm offset problems --- ILEditing/ILChangesLoading.cs | 2 + ILEditing/VanillaBugfixingILChanges.cs | 66 +++++++++++++++++++++++++ Projectiles/CalamityGlobalProjectile.cs | 9 ++-- Projectiles/Magic/RancorLaserbeam.cs | 8 +-- Projectiles/Magic/RancorMagicCircle.cs | 5 +- Projectiles/Summon/MechwormBody.cs | 1 + 6 files changed, 83 insertions(+), 8 deletions(-) diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 222f8d54cb..3266b47f7a 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -88,6 +88,7 @@ namespace CalamityMod.ILEditing // Fix vanilla bugs exposed by Calamity mechanics On.Terraria.Main.InitLifeBytes += BossRushLifeBytes; IL.Terraria.NPC.NPCLoot += FixSplittingWormBannerDrops; + IL.Terraria.Main.DoUpdate += FixProjectileUpdatePriorityProblems; } /// @@ -160,6 +161,7 @@ namespace CalamityMod.ILEditing // Fix vanilla bugs exposed by Calamity mechanics On.Terraria.Main.InitLifeBytes -= BossRushLifeBytes; IL.Terraria.NPC.NPCLoot -= FixSplittingWormBannerDrops; + IL.Terraria.Main.DoUpdate -= FixProjectileUpdatePriorityProblems; } } } diff --git a/ILEditing/VanillaBugfixingILChanges.cs b/ILEditing/VanillaBugfixingILChanges.cs index 8c065b6390..3f451c662f 100644 --- a/ILEditing/VanillaBugfixingILChanges.cs +++ b/ILEditing/VanillaBugfixingILChanges.cs @@ -4,7 +4,9 @@ using Mono.Cecil.Cil; using MonoMod.Cil; using System; using System.Collections.Generic; +using System.Linq; using Terraria; +using Terraria.GameInput; using Terraria.ID; namespace CalamityMod.ILEditing @@ -127,6 +129,70 @@ namespace CalamityMod.ILEditing } #endregion Fixing Splitting Worm Banner Spam in Deathmode + #region Fix Projectile Update Priority Problems + + // CONTEXT FOR FIX: The way projectile updating works is via looping, starting from 0 and ending at 999. For most cases this works sufficiently. + // However, in contexts where two projectile's update logic are dependent on each-other in some way (such as mechworm segment movement) it is possible + // that one projectile will update unexpectedly before the other, creating gaps. By making the update logic ordered based on a priority system, this can be + // alleviated. + private static void FixProjectileUpdatePriorityProblems(ILContext il) + { + var cursor = new ILCursor(il); + + // Locate the location where the projectile loop starts. + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallOrCallvirt("SetUP"))) + { + LogFailure("Projectile Update Priority fix", "Could not locate the LockOnHelper.SetUP method."); + return; + } + + int startIndex = cursor.Index; + + // Then go to where it ends. + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallOrCallvirt("SetDOWN"))) + { + LogFailure("Projectile Update Priority fix", "Could not locate the LockOnHelper.SetDOWN method."); + return; + } + + int endIndex = cursor.Index; + + // Go back to the start. + cursor.Goto(startIndex); + + // And delete the range that handles update logic intent to replace it. + cursor.RemoveRange(Math.Abs(endIndex - startIndex)); + + // And do projectile update logic manually. + cursor.EmitDelegate(() => + { + List orderedProjectiles = Main.projectile.Take(Main.maxProjectiles).OrderByDescending(p => p.active ? p.Calamity().UpdatePriority : 0f).ToList(); + for (int i = 0; i < Main.maxProjectiles; i++) + { + Projectile projectile = orderedProjectiles[i]; + Main.ProjectileUpdateLoopIndex = Array.IndexOf(Main.projectile, projectile); + + // Yes, this is completely ridiculous, but it's probably best to leave it this way to be as in-accordance with + // vanilla's normal update logic as possible. + if (Main.ignoreErrors) + { + try + { + projectile.Update(Main.ProjectileUpdateLoopIndex); + } + catch + { + Main.projectile[Main.ProjectileUpdateLoopIndex] = new Projectile(); + } + } + else + projectile.Update(i); + } + Main.ProjectileUpdateLoopIndex = -1; + }); + } + #endregion Fix Projectile Update Priority Problems + #region Make Mouse Hover Items Work with Animated Items private static void MakeMouseHoverItemsSupportAnimations(ILContext il) { diff --git a/Projectiles/CalamityGlobalProjectile.cs b/Projectiles/CalamityGlobalProjectile.cs index 40a38ad66b..c5ffe745f1 100644 --- a/Projectiles/CalamityGlobalProjectile.cs +++ b/Projectiles/CalamityGlobalProjectile.cs @@ -122,8 +122,11 @@ namespace CalamityMod.Projectiles public int ExplosiveEnchantCountdown = 0; public const int ExplosiveEnchantTime = 2400; - #region SetDefaults - public override void SetDefaults(Projectile projectile) + // Update priority variable. + public float UpdatePriority = 0f; + + #region SetDefaults + public override void SetDefaults(Projectile projectile) { switch (projectile.type) { @@ -414,7 +417,7 @@ namespace CalamityMod.Projectiles // Disable Lunatic Cultist's homing resistance globally ProjectileID.Sets.Homing[projectile.type] = false; - } + } #endregion #region PreAI diff --git a/Projectiles/Magic/RancorLaserbeam.cs b/Projectiles/Magic/RancorLaserbeam.cs index 0460320de4..1e8e7446ac 100644 --- a/Projectiles/Magic/RancorLaserbeam.cs +++ b/Projectiles/Magic/RancorLaserbeam.cs @@ -50,7 +50,7 @@ namespace CalamityMod.Projectiles.Magic projectile.hide = true; projectile.Calamity().PierceResistHarshness = 0.06f; projectile.Calamity().PierceResistCap = 0.4f; - } + } public override void AI() { @@ -66,7 +66,7 @@ namespace CalamityMod.Projectiles.Magic // Decide where to position the laserbeam. Vector2 circlePointDirection = projectile.velocity.SafeNormalize(Vector2.UnitX * Owner.direction); - projectile.Center = MagicCircle.Center + projectile.velocity * projectile.scale * -12f + Owner.velocity; + projectile.Center = MagicCircle.Center; // Update the laser length. float[] laserLengthSamplePoints = new float[24]; @@ -170,11 +170,11 @@ namespace CalamityMod.Projectiles.Magic GameShaders.Misc["CalamityMod:Flame"].UseImage("Images/Misc/Perlin"); - Vector2[] basePoints = new Vector2[12]; + Vector2[] basePoints = new Vector2[24]; for (int i = 0; i < basePoints.Length; i++) basePoints[i] = projectile.Center + projectile.velocity * i / (basePoints.Length - 1f) * LaserLength; - Vector2 overallOffset = projectile.Size * 0.5f - Main.screenPosition; + Vector2 overallOffset = -Main.screenPosition; RayDrawer.Draw(basePoints, overallOffset, 92); return false; } diff --git a/Projectiles/Magic/RancorMagicCircle.cs b/Projectiles/Magic/RancorMagicCircle.cs index 284c344de7..a2161574c6 100644 --- a/Projectiles/Magic/RancorMagicCircle.cs +++ b/Projectiles/Magic/RancorMagicCircle.cs @@ -34,6 +34,9 @@ namespace CalamityMod.Projectiles.Magic public override void AI() { + // Always update before the laserbeam, so that it doesn't recieve strange offsets. + projectile.Calamity().UpdatePriority = 1f; + // If the owner is no longer able to cast the circle, kill it. if (!Owner.channel || Owner.noItems || Owner.CCed) { @@ -55,7 +58,7 @@ namespace CalamityMod.Projectiles.Magic // Decide where to position the magic circle. Vector2 circlePointDirection = projectile.velocity.SafeNormalize(Vector2.UnitX * Owner.direction); - projectile.Center = Owner.Center + Vector2.UnitX * Owner.direction * 30f + circlePointDirection * projectile.scale * 56f; + projectile.Center = Owner.Center + circlePointDirection * projectile.scale * 56f; // Adjust the owner's direction. Owner.ChangeDir(projectile.direction); diff --git a/Projectiles/Summon/MechwormBody.cs b/Projectiles/Summon/MechwormBody.cs index 14f97109f5..bfe3e12b62 100644 --- a/Projectiles/Summon/MechwormBody.cs +++ b/Projectiles/Summon/MechwormBody.cs @@ -110,6 +110,7 @@ namespace CalamityMod.Projectiles.Summon // Accumulate the total segments of the worm. segmentAhead.localAI[0] = projectile.localAI[0] + 1f; + segmentAhead.Calamity().UpdatePriority = segmentAhead.localAI[0]; // Locate the head segment by looping through the projectile array. // Doing identity checks across every segment would be much more expensive than doing just this one loop. From 91c74e34021d7ffbf3c94c3ca5c807dec518613f Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 3 Jan 2022 00:45:33 -0600 Subject: [PATCH 2/3] Made the update priority edit more specific --- ILEditing/VanillaBugfixingILChanges.cs | 63 ++++++++++---------------- 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/ILEditing/VanillaBugfixingILChanges.cs b/ILEditing/VanillaBugfixingILChanges.cs index 3f451c662f..832b5db2dc 100644 --- a/ILEditing/VanillaBugfixingILChanges.cs +++ b/ILEditing/VanillaBugfixingILChanges.cs @@ -5,6 +5,7 @@ using MonoMod.Cil; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using Terraria; using Terraria.GameInput; using Terraria.ID; @@ -72,6 +73,8 @@ namespace CalamityMod.ILEditing NPCID.AncientCultistSquidhead, }; + public static readonly List OrderedProjectiles = new List(); + #region Fixing NPC HP Sync Byte Counts in Boss Rush // CONTEXT FOR FIX: When NPCs sync they have a pre-determined amount of bytes that are used to store HP/Max NPC information in packets for efficiency. // However, this may not always coincide with the true HP of the NPC when it's created if it has more HP than the allocated bytes can sufficiently store, which can @@ -140,56 +143,38 @@ namespace CalamityMod.ILEditing var cursor = new ILCursor(il); // Locate the location where the projectile loop starts. - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallOrCallvirt("SetUP"))) + if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchCallOrCallvirt("SetUP"))) { LogFailure("Projectile Update Priority fix", "Could not locate the LockOnHelper.SetUP method."); return; } - int startIndex = cursor.Index; - - // Then go to where it ends. - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallOrCallvirt("SetDOWN"))) + // Before doing anything else, prepare the list of ordered projectiles. + cursor.EmitDelegate>>(() => { - LogFailure("Projectile Update Priority fix", "Could not locate the LockOnHelper.SetDOWN method."); + return Main.projectile.Take(Main.maxProjectiles).OrderByDescending(p => p.active ? p.Calamity().UpdatePriority : 0f).ToList(); + }); + cursor.Emit(OpCodes.Stsfld, typeof(ILChanges).GetField("OrderedProjectiles", BindingFlags.Static | BindingFlags.Public)); + + // Go before the declaration of the projectile loop index and declare the ordered projectile. + if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchStsfld
("ProjectileUpdateLoopIndex"))) + { + LogFailure("Projectile Update Priority fix", "Could not locate the ProjectileUpdateLoopIndex field."); return; } - int endIndex = cursor.Index; - - // Go back to the start. - cursor.Goto(startIndex); - - // And delete the range that handles update logic intent to replace it. - cursor.RemoveRange(Math.Abs(endIndex - startIndex)); - - // And do projectile update logic manually. - cursor.EmitDelegate(() => + // Replace the projectile reference on the Projectile.Update call. + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdelemRef())) { - List orderedProjectiles = Main.projectile.Take(Main.maxProjectiles).OrderByDescending(p => p.active ? p.Calamity().UpdatePriority : 0f).ToList(); - for (int i = 0; i < Main.maxProjectiles; i++) - { - Projectile projectile = orderedProjectiles[i]; - Main.ProjectileUpdateLoopIndex = Array.IndexOf(Main.projectile, projectile); + LogFailure("Projectile Update Priority fix", $"Could not locate the Main.projectile index load reference."); + return; + } - // Yes, this is completely ridiculous, but it's probably best to leave it this way to be as in-accordance with - // vanilla's normal update logic as possible. - if (Main.ignoreErrors) - { - try - { - projectile.Update(Main.ProjectileUpdateLoopIndex); - } - catch - { - Main.projectile[Main.ProjectileUpdateLoopIndex] = new Projectile(); - } - } - else - projectile.Update(i); - } - Main.ProjectileUpdateLoopIndex = -1; - }); + // Pop the Main.projectile[i] reference and replace it with OrderedProjectiles[i]. + cursor.Emit(OpCodes.Pop); + cursor.Emit(OpCodes.Ldsfld, typeof(ILChanges).GetField("OrderedProjectiles", BindingFlags.Static | BindingFlags.Public)); + cursor.Emit(OpCodes.Ldloc, 29); + cursor.Emit(OpCodes.Callvirt, typeof(List).GetMethod("get_Item")); } #endregion Fix Projectile Update Priority Problems From 6283dd8216e4bf80e3edbaca21c2d4e3ff4c15e3 Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 3 Jan 2022 07:45:06 -0600 Subject: [PATCH 3/3] whoAmI definition fix --- ILEditing/VanillaBugfixingILChanges.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ILEditing/VanillaBugfixingILChanges.cs b/ILEditing/VanillaBugfixingILChanges.cs index 832b5db2dc..db34ceaedb 100644 --- a/ILEditing/VanillaBugfixingILChanges.cs +++ b/ILEditing/VanillaBugfixingILChanges.cs @@ -175,6 +175,11 @@ namespace CalamityMod.ILEditing cursor.Emit(OpCodes.Ldsfld, typeof(ILChanges).GetField("OrderedProjectiles", BindingFlags.Static | BindingFlags.Public)); cursor.Emit(OpCodes.Ldloc, 29); cursor.Emit(OpCodes.Callvirt, typeof(List).GetMethod("get_Item")); + + // Remove the direct i reference in the Update call and replace it with Array.IndexOf(Main.projectile, OrderedProjectiles[i]). + cursor.Remove(); + cursor.Emit(OpCodes.Ldloc, 29); + cursor.EmitDelegate>(i => Array.IndexOf(Main.projectile, OrderedProjectiles[i])); } #endregion Fix Projectile Update Priority Problems