Merge manual projectile ordering

This commit is contained in:
Cameron Ewell authored and GitHub committed 2022-01-03 23:04:25 -05:00
commit 72ba883bbc
6 files changed
+72 -7

No files matched your search

+2
View File
@@ -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;
}
/// <summary>
@@ -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;
}
}
}
+56
View File
@@ -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<Projectile> OrderedProjectiles = new List<Projectile>();
#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<LockOnHelper>("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<Func<List<Projectile>>>(() =>
{
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<Main>("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<Projectile>).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<Func<int, int>>(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)
{
+6 -3
View File
@@ -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
+3 -3
View File
@@ -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;
}
+4 -1
View File
@@ -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);
+1
View File
@@ -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.