diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index f2beb36ff8..ef2a89146b 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -90,6 +90,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; } /// @@ -163,6 +164,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..db34ceaedb 100644 --- a/ILEditing/VanillaBugfixingILChanges.cs +++ b/ILEditing/VanillaBugfixingILChanges.cs @@ -4,7 +4,10 @@ using Mono.Cecil.Cil; using MonoMod.Cil; using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; using Terraria; +using Terraria.GameInput; using Terraria.ID; namespace CalamityMod.ILEditing @@ -70,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 @@ -127,6 +132,57 @@ 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.Before, i => i.MatchCallOrCallvirt("SetUP"))) + { + LogFailure("Projectile Update Priority fix", "Could not locate the LockOnHelper.SetUP method."); + return; + } + + // Before doing anything else, prepare the list of ordered projectiles. + cursor.EmitDelegate>>(() => + { + 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; + } + + // Replace the projectile reference on the Projectile.Update call. + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdelemRef())) + { + LogFailure("Projectile Update Priority fix", $"Could not locate the Main.projectile index load reference."); + return; + } + + // 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")); + + // 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 + #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 e988e1a6d8..bc94b86cdd 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) { @@ -419,7 +422,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 008eacd3dd..426351fddc 100644 --- a/Projectiles/Magic/RancorLaserbeam.cs +++ b/Projectiles/Magic/RancorLaserbeam.cs @@ -67,7 +67,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]; @@ -171,11 +171,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.