GenericAIUtils as CommonProjectileAI

This commit is contained in:
Flowaria authored and Cameron Ewell committed 2025-12-16 17:46:11 -05:00
1 parent c98700aa2d
commit f25ae56157
51 files changed
+624 -689

No files matched your search

+623
View File
@@ -0,0 +1,623 @@
using System;
using CalamityMod.CalPlayer;
using CalamityMod.Enums;
using CalamityMod.Projectiles.Summon;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using static Terraria.ModLoader.ModContent;
namespace CalamityMod.Projectiles;
internal static class CommonProjectileAI
{
public static void ChargingMinionAI(this Projectile projectile, float range, float maxPlayerDist, float extraMaxPlayerDist, float safeDist, int initialUpdates, float chargeDelayTime, float goToSpeed, float goBackSpeed, Vector2 returnOffset, float chargeCounterMax, float chargeSpeed, bool tileVision, bool ignoreTilesWhenCharging, int updateDifference = 1)
{
Player player = Main.player[projectile.owner];
CalamityPlayer modPlayer = player.Calamity();
projectile.MinionAntiClump();
//Breather time between charges as like a reset
bool chargeDelay = false;
if (projectile.ai[0] == 2f)
{
projectile.ai[1] += 1f;
projectile.extraUpdates = initialUpdates + updateDifference;
if (projectile.ai[1] > chargeDelayTime)
{
projectile.ai[1] = 1f;
projectile.ai[0] = 0f;
projectile.extraUpdates = initialUpdates;
// CIT 2OCT2024: This line was breaking how the game counted minions,
// which resulted in minions being counted extra times and despawning other minions, most notoriously with Resurrection Butterfly.
// As such, I have commented it out.
// projectile.numUpdates = 0;
projectile.netUpdate = true;
}
else
{
chargeDelay = true;
}
}
if (chargeDelay)
{
return;
}
//Find a target
float maxDist = range;
Vector2 targetVec = projectile.position;
bool foundTarget = false;
bool isButterfly = projectile.type == ProjectileType<PurpleButterfly>();
//Prioritize the targeted enemy if possible
if (player.HasMinionAttackTargetNPC)
{
NPC npc = Main.npc[player.MinionAttackTargetNPC];
bool fishronCheck = npc.type == NPCID.DukeFishron && npc.active && isButterfly;
if (npc.CanBeChasedBy(projectile, false) || fishronCheck)
{
//Check the size of the target to make it easier to hit fat targets like Levi
float extraDist = (npc.width / 2) + (npc.height / 2);
float targetDist = Vector2.Distance(npc.Center, projectile.Center);
//Some minions will ignore tiles when choosing a target like Ice Claspers, others will not
bool canHit = true;
if (extraDist < maxDist && !tileVision)
canHit = Collision.CanHit(projectile.Center, 1, 1, npc.Center, 1, 1);
if (!foundTarget && targetDist < (maxDist + extraDist) && canHit)
{
maxDist = targetDist;
targetVec = npc.Center;
foundTarget = true;
}
}
}
//If no npc is specifically targetted or the selected enemy can't be found, check through the entire array
if (!foundTarget)
{
for (int npcIndex = 0; npcIndex < Main.maxNPCs; npcIndex++)
{
NPC npc = Main.npc[npcIndex];
bool fishronCheck = npc.type == NPCID.DukeFishron && npc.active && isButterfly;
if (npc.CanBeChasedBy(projectile, false) || fishronCheck)
{
float extraDist = (npc.width / 2) + (npc.height / 2);
float targetDist = Vector2.Distance(npc.Center, projectile.Center);
bool canHit = true;
if (extraDist < maxDist && !tileVision)
canHit = Collision.CanHit(projectile.Center, 1, 1, npc.Center, 1, 1);
if (!foundTarget && targetDist < (maxDist + extraDist) && canHit)
{
maxDist = targetDist;
targetVec = npc.Center;
foundTarget = true;
}
}
}
}
//If the player is too far, return to the player. Range is increased while attacking something.
float distBeforeForcedReturn = maxPlayerDist;
if (foundTarget)
{
distBeforeForcedReturn = extraMaxPlayerDist;
}
if (Vector2.Distance(player.Center, projectile.Center) > distBeforeForcedReturn)
{
projectile.ai[0] = 1f;
projectile.netUpdate = true;
}
//Go to the target if you found one
if (foundTarget && projectile.ai[0] == 0f)
{
//Some minions don't ignore tiles while charging like brittle stars
projectile.tileCollide = !ignoreTilesWhenCharging;
Vector2 targetSpot = targetVec - projectile.Center;
float targetDist = targetSpot.Length();
targetSpot.Normalize();
//Tries to get the minion in the sweet spot of 200 pixels away but the minion also charges so idk what good it does
if (targetDist > 200f)
{
float speed = goToSpeed; //8
targetSpot *= speed;
projectile.velocity = (projectile.velocity * 40f + targetSpot) / 41f;
}
else
{
float speed = -goBackSpeed; //-4
targetSpot *= speed;
projectile.velocity = (projectile.velocity * 40f + targetSpot) / 41f; //41
}
}
//Movement for idle or returning to the player
else
{
//Ignore tiles so they don't get stuck everywhere like Optic Staff
projectile.tileCollide = false;
bool returningToPlayer = false;
if (!returningToPlayer)
{
returningToPlayer = projectile.ai[0] == 1f;
}
//Player distance calculations
Vector2 playerVec = player.Center - projectile.Center + returnOffset;
float playerDist = playerVec.Length();
//If the minion is actively returning, move faster
float playerHomeSpeed = 6f;
if (returningToPlayer)
{
playerHomeSpeed = 15f;
}
//Move somewhat faster if the player is kinda far~ish
if (playerDist > 200f && playerHomeSpeed < 8f)
{
playerHomeSpeed = 8f;
}
//Return to normal if close enough to the player
if (playerDist < safeDist && returningToPlayer && !Collision.SolidCollision(projectile.position, projectile.width, projectile.height))
{
projectile.ai[0] = 0f;
projectile.netUpdate = true;
}
//Teleport to the player if abnormally far
if (playerDist > 2000f)
{
projectile.position.X = player.Center.X - (float)(projectile.width / 2);
projectile.position.Y = player.Center.Y - (float)(projectile.height / 2);
projectile.netUpdate = true;
}
//If more than 70 pixels away, move toward the player
if (playerDist > 70f)
{
playerVec.Normalize();
playerVec *= playerHomeSpeed;
projectile.velocity = (projectile.velocity * 40f + playerVec) / 41f;
}
//Minions never stay still
else if (projectile.velocity.X == 0f && projectile.velocity.Y == 0f)
{
projectile.velocity.X = -0.15f;
projectile.velocity.Y = -0.05f;
}
}
//Increment attack counter randomly
if (projectile.ai[1] > 0f)
{
projectile.ai[1] += (float)Main.rand.Next(1, 4);
}
//If high enough, prepare to attack
if (projectile.ai[1] > chargeCounterMax)
{
projectile.ai[1] = 0f;
projectile.netUpdate = true;
}
//Charge at an enemy if not on cooldown
if (projectile.ai[0] == 0f)
{
if (projectile.ai[1] == 0f && foundTarget && maxDist < 500f)
{
projectile.ai[1] += 1f;
if (Main.myPlayer == projectile.owner)
{
projectile.ai[0] = 2f;
Vector2 targetPos = targetVec - projectile.Center;
targetPos.Normalize();
projectile.velocity = targetPos * chargeSpeed; //8
projectile.netUpdate = true;
}
}
}
}
public static void FloatingPetAI(this Projectile projectile, bool faceRight, float tiltFloat, bool lightPet = false)
{
Player player = Main.player[projectile.owner];
//anti sticking movement as a failsafe
float SAImovement = 0.05f;
for (int k = 0; k < Main.maxProjectiles; k++)
{
Projectile otherProj = Main.projectile[k];
// Short circuits to make the loop as fast as possible
if (!otherProj.active || otherProj.owner != projectile.owner || !Main.projPet[otherProj.type] || k == projectile.whoAmI)
continue;
// If the other projectile is indeed another pet owned by the same player and they're too close, nudge them away.
bool isPet = Main.projPet[otherProj.type];
float taxicabDist = Math.Abs(projectile.position.X - otherProj.position.X) + Math.Abs(projectile.position.Y - otherProj.position.Y);
if (isPet && taxicabDist < projectile.width)
{
if (projectile.position.X < otherProj.position.X)
projectile.velocity.X -= SAImovement;
else
projectile.velocity.X += SAImovement;
if (projectile.position.Y < otherProj.position.Y)
projectile.velocity.Y -= SAImovement;
else
projectile.velocity.Y += SAImovement;
}
}
float passiveMvtFloat = 0.5f;
projectile.tileCollide = false;
float range = 100f;
Vector2 projPos = projectile.Center;
float xDist = player.Center.X - projPos.X;
float yDist = player.Center.Y - projPos.Y;
yDist += Main.rand.NextFloat(-10, 20);
xDist += Main.rand.NextFloat(-10, 20);
//Light pets lead the player, normal pets trail the player
xDist += 60f * (lightPet ? (float)player.direction : -(float)player.direction);
yDist -= 60f;
Vector2 playerVector = new Vector2(xDist, yDist);
float playerDist = playerVector.Length();
float returnSpeed = 18f;
//If player is close enough, resume normal
if (playerDist < range && player.velocity.Y == 0f &&
projectile.Bottom.Y <= player.Bottom.Y &&
!Collision.SolidCollision(projectile.position, projectile.width, projectile.height))
{
if (projectile.velocity.Y < -6f)
{
projectile.velocity.Y = -6f;
}
}
//Teleport to player if too far
if (playerDist > 2000f)
{
projectile.position.X = player.Center.X - projectile.width / 2;
projectile.position.Y = player.Center.Y - projectile.height / 2;
projectile.netUpdate = true;
}
if (playerDist < 50f)
{
if (Math.Abs(projectile.velocity.X) > 2f || Math.Abs(projectile.velocity.Y) > 2f)
{
projectile.velocity *= 0.99f;
}
passiveMvtFloat = 0.01f;
}
else
{
if (playerDist < 100f)
{
passiveMvtFloat = 0.1f;
}
if (playerDist > 300f)
{
passiveMvtFloat = 1f;
}
playerDist = returnSpeed / playerDist;
playerVector.X *= playerDist;
playerVector.Y *= playerDist;
}
if (projectile.velocity.X < playerVector.X)
{
projectile.velocity.X += passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.X < 0f)
{
projectile.velocity.X += passiveMvtFloat;
}
}
if (projectile.velocity.X > playerVector.X)
{
projectile.velocity.X -= passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.X > 0f)
{
projectile.velocity.X -= passiveMvtFloat;
}
}
if (projectile.velocity.Y < playerVector.Y)
{
projectile.velocity.Y += passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.Y < 0f)
{
projectile.velocity.Y += passiveMvtFloat * 2f;
}
}
if (projectile.velocity.Y > playerVector.Y)
{
projectile.velocity.Y -= passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.Y > 0f)
{
projectile.velocity.Y -= passiveMvtFloat * 2f;
}
}
if (projectile.velocity.X >= 0.25f)
{
projectile.direction = faceRight ? 1 : -1;
}
else if (projectile.velocity.X < -0.25f)
{
projectile.direction = faceRight ? -1 : 1;
}
//Tilting and change directions
projectile.spriteDirection = projectile.direction;
projectile.rotation = projectile.velocity.X * tiltFloat;
}
public static void HealingProjectile(this Projectile projectile, int healing, int playerToHeal, float homingVelocity, float inertia, bool autoHomes = true, int timeCheck = 120)
{
int target = playerToHeal;
Player player = Main.player[target];
float homingSpeed = homingVelocity;
if (player.lifeMagnet)
homingSpeed *= 1.5f;
Vector2 playerVector = player.Center - projectile.Center;
float playerDist = playerVector.Length();
if (playerDist < 50f && projectile.position.X < player.position.X + player.width && projectile.position.X + projectile.width > player.position.X && projectile.position.Y < player.position.Y + player.height && projectile.position.Y + projectile.height > player.position.Y)
{
if (projectile.owner == Main.myPlayer && !Main.LocalPlayer.moonLeech)
{
int healAmt = healing;
player.HealPlayer(healAmt, HealTextType.Local);
NetMessage.SendData(MessageID.SpiritHeal, -1, -1, null, target, healAmt, 0f, 0f, 0, 0, 0);
}
projectile.Kill();
}
if (autoHomes)
{
playerDist = homingSpeed / playerDist;
playerVector.X *= playerDist;
playerVector.Y *= playerDist;
projectile.velocity.X = (projectile.velocity.X * inertia + playerVector.X) / (inertia + 1f);
projectile.velocity.Y = (projectile.velocity.Y * inertia + playerVector.Y) / (inertia + 1f);
}
else if (player.lifeMagnet && projectile.timeLeft < timeCheck)
{
playerDist = homingVelocity / playerDist;
playerVector.X *= playerDist;
playerVector.Y *= playerDist;
projectile.velocity.X = (projectile.velocity.X * inertia + playerVector.X) / (inertia + 1f);
projectile.velocity.Y = (projectile.velocity.Y * inertia + playerVector.Y) / (inertia + 1f);
}
}
public static void MinionAntiClump(this Projectile projectile, float pushForce = 0.05f)
{
for (int k = 0; k < Main.maxProjectiles; k++)
{
Projectile otherProj = Main.projectile[k];
// Short circuits to make the loop as fast as possible
if (!otherProj.active || otherProj.owner != projectile.owner || !otherProj.minion || k == projectile.whoAmI)
continue;
// If the other projectile is indeed the same owned by the same player and they're too close, nudge them away.
bool sameProjType = otherProj.type == projectile.type;
float taxicabDist = Math.Abs(projectile.position.X - otherProj.position.X) + Math.Abs(projectile.position.Y - otherProj.position.Y);
if (sameProjType && taxicabDist < projectile.width)
{
if (projectile.position.X < otherProj.position.X)
projectile.velocity.X -= pushForce;
else
projectile.velocity.X += pushForce;
if (projectile.position.Y < otherProj.position.Y)
projectile.velocity.Y -= pushForce;
else
projectile.velocity.Y += pushForce;
}
}
}
/// <summary>
/// Call this function in ModifyHitNPC to make your projectiles stick to enemies, needs StickyProjAI to be called in the AI of the projectile
/// </summary>
/// <param name="projectile">The projectile you're giving sticky behaviour to</param>
/// <param name="maxStick">How many projectiles of this type can stick to one enemy</param>
public static void ModifyHitNPCSticky(this Projectile projectile, int maxStick)
{
Player player = Main.player[projectile.owner];
Rectangle myRect = projectile.Hitbox;
if (projectile.owner == Main.myPlayer)
{
for (int npcIndex = 0; npcIndex < Main.maxNPCs; npcIndex++)
{
NPC npc = Main.npc[npcIndex];
//covers most edge cases like voodoo dolls
if (npc.active && !npc.dontTakeDamage &&
((projectile.friendly && (!npc.friendly || (npc.type == NPCID.Guide && projectile.owner < Main.maxPlayers && player.killGuide) || (npc.type == NPCID.Clothier && projectile.owner < Main.maxPlayers && player.killClothier))) ||
(projectile.hostile && npc.friendly && !npc.dontTakeDamageFromHostiles)) && (projectile.owner < 0 || npc.immune[projectile.owner] == 0 || projectile.maxPenetrate == 1))
{
if (npc.noTileCollide || !projectile.ownerHitCheck)
{
bool stickingToNPC;
//Solar Crawltipede tail has special collision
if (npc.type == NPCID.SolarCrawltipedeTail)
{
Rectangle rect = npc.Hitbox;
int crawltipedeHitboxMod = 8;
rect.X -= crawltipedeHitboxMod;
rect.Y -= crawltipedeHitboxMod;
rect.Width += crawltipedeHitboxMod * 2;
rect.Height += crawltipedeHitboxMod * 2;
stickingToNPC = projectile.Colliding(myRect, rect);
}
else
{
stickingToNPC = projectile.Colliding(myRect, npc.Hitbox);
}
if (stickingToNPC)
{
//reflect projectile if the npc can reflect it (like Selenians)
if (npc.reflectsProjectiles && projectile.CanBeReflected())
{
npc.ReflectProjectile(projectile);
return;
}
//let the projectile know it is sticking and the npc it is sticking too
projectile.ai[0] = 1f;
projectile.ai[1] = (float)npcIndex;
//follow the NPC
projectile.velocity = (npc.Center - projectile.Center);
projectile.netUpdate = true;
//Count how many projectiles are attached, delete as necessary
Point[] array2 = new Point[maxStick];
int projCount = 0;
for (int projIndex = 0; projIndex < Main.maxProjectiles; projIndex++)
{
Projectile proj = Main.projectile[projIndex];
if (projIndex != projectile.whoAmI && proj.active && proj.owner == Main.myPlayer && proj.type == projectile.type && proj.ai[0] == 1f && proj.ai[1] == (float)npcIndex)
{
array2[projCount++] = new Point(projIndex, proj.timeLeft);
if (projCount >= array2.Length)
{
break;
}
}
}
if (projCount >= array2.Length)
{
int stuckProjAmt = 0;
for (int m = 1; m < array2.Length; m++)
{
if (array2[m].Y < array2[stuckProjAmt].Y)
{
stuckProjAmt = m;
}
}
Main.projectile[array2[stuckProjAmt].X].Kill();
}
}
}
}
}
}
}
public static void StickToTiles(this Projectile projectile, bool ignorePlatforms, bool stickToEverything)
{
try
{
int xLeft = (int)(projectile.position.X / 16f) - 1;
int xRight = (int)((projectile.position.X + (float)projectile.width) / 16f) + 2;
int yBottom = (int)(projectile.position.Y / 16f) - 1;
int yTop = (int)((projectile.position.Y + (float)projectile.height) / 16f) + 2;
if (xLeft < 0)
{
xLeft = 0;
}
if (xRight > Main.maxTilesX)
{
xRight = Main.maxTilesX;
}
if (yBottom < 0)
{
yBottom = 0;
}
if (yTop > Main.maxTilesY)
{
yTop = Main.maxTilesY;
}
for (int x = xLeft; x < xRight; x++)
{
for (int y = yBottom; y < yTop; y++)
{
Tile tile = Main.tile[x, y];
bool platformCheck = true;
if (ignorePlatforms)
platformCheck = !TileID.Sets.Platforms[tile.TileType] && tile.TileType != TileID.PlanterBox;
bool tableCheck = false;
if (stickToEverything)
tableCheck = Main.tileSolidTop[tile.TileType] && tile.TileFrameY == 0;
if (tile != null && tile.HasUnactuatedTile && platformCheck && (Main.tileSolid[tile.TileType] || tableCheck))
{
Vector2 tileSize;
tileSize.X = (float)(x * 16);
tileSize.Y = (float)(y * 16);
if (projectile.position.X + (float)projectile.width - 4f > tileSize.X && projectile.position.X + 4f < tileSize.X + 16f && projectile.position.Y + (float)projectile.height - 4f > tileSize.Y && projectile.position.Y + 4f < tileSize.Y + 16f)
{
projectile.velocity.X = 0f;
projectile.velocity.Y = -0.2f;
}
}
}
}
}
catch
{
}
}
/// <summary>
/// Call this function in the ai of your projectile so it can stick to enemies, also requires ModifyHitNPCSticky to be called in ModifyHitNPC
/// </summary>
/// <param name="projectile">The projectile you're adding sticky behaviour to</param>
/// <param name="timeLeft">Number of seconds you want a projectile to cling to an NPC</param>
public static void StickyProjAI(this Projectile projectile, int timeLeft, bool findNewNPC = false)
{
if (projectile.ai[0] == 1f)
{
int seconds = timeLeft;
bool killProj = false;
bool spawnDust = false;
//the projectile follows the NPC, even if it goes into blocks
projectile.tileCollide = false;
//timer for triggering hit effects
projectile.localAI[0]++;
if (projectile.localAI[0] % 30f == 0f)
{
spawnDust = true;
}
//So AI knows what NPC it is sticking to
int npcIndex = (int)projectile.ai[1];
NPC npc = Main.npc[npcIndex];
//Kill projectile after so many seconds or if the NPC it is stuck to no longer exists
if (projectile.localAI[0] >= (float)(60 * seconds))
{
killProj = true;
}
else if (!npcIndex.WithinBounds(Main.maxNPCs))
{
killProj = true;
}
else if (npc.active && !npc.dontTakeDamage)
{
//follow the NPC
projectile.Center = npc.Center - projectile.velocity * 2f;
projectile.gfxOffY = npc.gfxOffY;
//if attached to npc, trigger npc hit effects every half a second
if (spawnDust)
{
npc.HitEffect(0, 1.0);
}
}
else
{
killProj = true;
}
//Kill the projectile or reset stats if needed
if (killProj)
{
if (findNewNPC)
projectile.ai[0] = 0f;
else
projectile.Kill();
}
}
}
}
@@ -140,7 +140,6 @@ namespace CalamityMod.Projectiles.DraedonsArsenal
PlayerSeparationAnxietyAI(player, ref minimumFrame, ref maximumFrame);
FallThroughYPoint = player.Top.Y;
}
Projectile.MinionAntiClump();
ManipulateFrames(minimumFrame, maximumFrame);
}
-1
View File
@@ -24,7 +24,6 @@ namespace CalamityMod.Projectiles.Healing
public override void AI()
{
Projectile.velocity *= 0.95f;
Projectile.HealingProjectile(3, (int)Projectile.ai[0], 6f, 15f, false);
int dusty = Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, DustID.GoldCoin, 0f, 0f, 100, default, 1f);
Dust dust = Main.dust[dusty];
-1
View File
@@ -25,7 +25,6 @@ namespace CalamityMod.Projectiles.Healing
public override void AI()
{
Projectile.velocity.Y *= 0.98f;
Projectile.HealingProjectile(15, Projectile.owner, 12f, 15f, false);
int dusty = Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, DustID.TerraBlade, 0f, 0f, 100, new Color(0, 200, 0), 1.5f);
Dust dust = Main.dust[dusty];
-1
View File
@@ -37,7 +37,6 @@ namespace CalamityMod.Projectiles.Healing
{
Projectile.frame = 0;
}
Projectile.HealingProjectile((int)Projectile.ai[1], (int)Projectile.ai[0], 3.5f, 15f);
return;
}
+1 -1
View File
@@ -62,7 +62,7 @@ namespace CalamityMod.Projectiles.Healing
public void PassiveBehavior()
{
float maxYVelocity = 2f;
CalamityUtils.StickToTiles(Projectile, false, false);
CommonProjectileAI.StickToTiles(Projectile, false, false);
Projectile.velocity.X *= 0.99f;
if (Projectile.velocity.Y < maxYVelocity)
Projectile.velocity.Y += 0.02f;
-1
View File
@@ -48,7 +48,6 @@ namespace CalamityMod.Projectiles.Magic
{
Projectile.rotation = Projectile.velocity.ToRotation();
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
Projectile.frameCounter++;
-1
View File
@@ -47,7 +47,6 @@ namespace CalamityMod.Projectiles.Magic
{
Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.Pi;
}
//Sticky Behaviour
Projectile.StickyProjAI(5);
}
@@ -46,7 +46,6 @@ namespace CalamityMod.Projectiles.Melee
}
Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.PiOver2;
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
}
-1
View File
@@ -57,7 +57,6 @@ namespace CalamityMod.Projectiles.Pets
{
Projectile.timeLeft = 2;
}
Projectile.FloatingPetAI(true, 0.1f);
//Dust
-1
View File
@@ -69,7 +69,6 @@ namespace CalamityMod.Projectiles.Pets
UpdateForm(player);
UpdateFrames();
Projectile.FloatingPetAI(true, 0.05f);
}
-2
View File
@@ -123,8 +123,6 @@ namespace CalamityMod.Projectiles.Ranged
// Clamp the center so that it does not go too far from the return position.
if (Projectile.Distance(returnPosition) > 120f)
Projectile.Center = returnPosition + Projectile.DirectionFrom(returnPosition) * 120f;
// Disallow heads from getting too close to each other.
Projectile.MinionAntiClump(0.15f);
}
@@ -138,8 +138,6 @@ namespace CalamityMod.Projectiles.Rogue
laser.DamageType = RogueDamageClass.Instance;
}
}
// Stay stuck to the target.
Projectile.StickyProjAI(6, true);
// If still attached to a target, do nothing.
@@ -209,8 +207,6 @@ namespace CalamityMod.Projectiles.Rogue
// Provide a fixed amount of grind time so that DPS can't vary wildly.
Projectile.timeLeft = 90;
}
// Apply sticky AI.
Projectile.ModifyHitNPCSticky(3);
}
-2
View File
@@ -87,8 +87,6 @@ namespace CalamityMod.Projectiles.Rogue
}
}
}
// Sticky behavior. Lets the projectile impale enemies and sticks to its impaled enemy automatically.
Projectile.StickyProjAI(10);
}
-1
View File
@@ -43,7 +43,6 @@ namespace CalamityMod.Projectiles.Rogue
flame.velocity = Vector2.Zero;
flame.noGravity = true;
}
Projectile.StickyProjAI(50);
if (Projectile.ai[0] == 1f)
-1
View File
@@ -51,7 +51,6 @@ namespace CalamityMod.Projectiles.Rogue
Main.dust[dust].noGravity = true;
}
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
}
@@ -67,7 +67,6 @@ namespace CalamityMod.Projectiles.Rogue
ReturnToPlayer();
break;
case 2:
// Will target the targetted NPC that minions use btw
Projectile.ChargingMinionAI(1200f, 1500f, 2200f, 150f, 1, 40f, 12f, 6f, new Vector2(0f, -60f), 40f, 12f, true, true);
Projectile.localAI[0] += 1f;
if (Projectile.localAI[0] >= 180)
-1
View File
@@ -65,7 +65,6 @@ namespace CalamityMod.Projectiles.Rogue
}
}
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
}
-1
View File
@@ -37,7 +37,6 @@ namespace CalamityMod.Projectiles.Rogue
if (Projectile.velocity.Y > 16f)
Projectile.velocity.Y = 16f;
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
}
-1
View File
@@ -32,7 +32,6 @@ namespace CalamityMod.Projectiles.Rogue
{
Dust.NewDust(Projectile.position + Projectile.velocity, Projectile.width, Projectile.height, DustID.ChlorophyteWeapon, Projectile.velocity.X * 0.5f, Projectile.velocity.Y * 0.5f);
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
if (Projectile.ai[0] != 1f)
{
-1
View File
@@ -45,7 +45,6 @@ namespace CalamityMod.Projectiles.Rogue
Projectile.rotation += 0.4f * (float)Projectile.direction;
Projectile.spriteDirection = Projectile.direction;
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
if (Projectile.ai[2] < 30f && Projectile.ai[0] != 1f)
{
-1
View File
@@ -47,7 +47,6 @@ namespace CalamityMod.Projectiles.Rogue
Projectile.rotation += 0.4f * (float)Projectile.direction;
Projectile.spriteDirection = Projectile.direction;
}
//Sticky Behaviour
Projectile.StickyProjAI(15);
if (Projectile.ai[2] < 30f && Projectile.ai[0] != 1f)
{
-2
View File
@@ -66,8 +66,6 @@ namespace CalamityMod.Projectiles.Summon
// Does a dust effect where the minion spawns.
Projectile.rotation += Projectile.velocity.X * 0.04f;
// Make the minion spin depending on how fast it is.
Projectile.ChargingMinionAI(1200f, 1500f, 2200f, 150f, 0, 24f, 15f, 4f, new Vector2(0f, -60f), 12f, 12f, false, false, 1);
}
-2
View File
@@ -62,8 +62,6 @@ namespace CalamityMod.Projectiles.Summon
}
Projectile.localAI[0] += 1f;
}
//anti sticking movement
Projectile.MinionAntiClump();
//get in a line
-1
View File
@@ -32,7 +32,6 @@ namespace CalamityMod.Projectiles.Summon
FollowPlayer();
if (Target != null)
AttackTarget();
Projectile.MinionAntiClump();
Projectile.spriteDirection = Target == null ? MathF.Sign(Projectile.velocity.X) : MathF.Sign(Target.Center.X - Projectile.Center.X);
-2
View File
@@ -114,8 +114,6 @@ namespace CalamityMod.Projectiles.Summon
Projectile.timeLeft = 2;
}
}
//Anti sticky movement to prevent overlapping minions
Projectile.MinionAntiClump();
float maxDistance = 700f;
-1
View File
@@ -73,7 +73,6 @@ namespace CalamityMod.Projectiles.Summon
Projectile.timeLeft = 2;
}
}
Projectile.MinionAntiClump();
Projectile.frameCounter++;
-1
View File
@@ -66,7 +66,6 @@ namespace CalamityMod.Projectiles.Summon
Projectile.timeLeft = 2;
}
}
Projectile.MinionAntiClump();
Projectile.frameCounter++;
-1
View File
@@ -79,7 +79,6 @@ namespace CalamityMod.Projectiles.Summon
{
Projectile.frame = 0;
}
Projectile.ChargingMinionAI(Calamitamini.Range, Calamitamini.SeparationAnxietyMin, Calamitamini.SeparationAnxietyMax, Calamitamini.SafeDist, 0, 40f, 8f, 4f, new Vector2(0f, -60f), 40f, 8f, false, true);
}
-2
View File
@@ -71,8 +71,6 @@ namespace CalamityMod.Projectiles.Summon
Projectile.timeLeft = 2;
}
}
//Anti sticky movement to prevent minions from stacking
Projectile.MinionAntiClump();
//Set tile collision for only when trying to return to the player
-1
View File
@@ -58,7 +58,6 @@ namespace CalamityMod.Projectiles.Summon
}
}
Projectile.rotation += 0.075f;
Projectile.ChargingMinionAI(1600f, 1800f, 2500f, 400f, 1, 30f, 24f, 12f, new Vector2(0f, -60f), 30f, 16f, true, true);
// Destroy trees in GFB, because it's an axe, and axes should do that
-1
View File
@@ -111,7 +111,6 @@ namespace CalamityMod.Projectiles.Summon
float colorScale = (float)Projectile.alpha / 255f;
Lighting.AddLight((int)Projectile.Center.X / 16, (int)Projectile.Center.Y / 16, 1f * colorScale, 0.1f * colorScale, 1f * colorScale);
Projectile.MinionAntiClump();
float detectRange = 2200f;
@@ -97,7 +97,6 @@ namespace CalamityMod.Projectiles.Summon
{
Projectile.frame = (Projectile.localAI[1] == 1f).ToInt() * 4;
}
Projectile.MinionAntiClump(0.1f);
if (Projectile.Distance(player.Center) > 2700f)
{
@@ -78,7 +78,6 @@ namespace CalamityMod.Projectiles.Summon
}
CurrentState.Invoke();
Projectile.MinionAntiClump(0.25f);
if (!Main.dedServ)
-1
View File
@@ -137,7 +137,6 @@ namespace CalamityMod.Projectiles.Summon
if (limbs.type != ModContent.ProjectileType<EndoCooperLimbs>() || !limbs.active)
Projectile.Kill();
Projectile.MinionAntiClump();
bool accelerate = false;
if (Projectile.ai[0] == 2f)
-1
View File
@@ -156,7 +156,6 @@ namespace CalamityMod.Projectiles.Summon
{
Projectile.Center = returnPosition + Projectile.DirectionFrom(returnPosition) * 120f;
}
Projectile.MinionAntiClump(0.15f);
AdjustOldVelocityArray();
}
-1
View File
@@ -31,7 +31,6 @@ namespace CalamityMod.Projectiles.Summon
{
if (!Main.dedServ && Projectile.velocity.Length() > 5f)
Dust.NewDustPerfect(Projectile.Center, (int)CalamityDusts.SulphurousSeaAcid).noGravity = true;
Projectile.StickyProjAI(3);
}
public override void OnKill(int timeLeft)
-1
View File
@@ -146,7 +146,6 @@ namespace CalamityMod.Projectiles.Summon
public void DoPlayerHoverMovement()
{
// Move away from other minions of the same type.
Projectile.MinionAntiClump(0.1f);
Vector2 hoverDestination = Owner.Top - Vector2.UnitY * 50f;
-2
View File
@@ -187,8 +187,6 @@ namespace CalamityMod.Projectiles.Summon
// Clamp the center so that it does not go too far from the return position.
if (Projectile.Distance(returnPosition) > 120f)
Projectile.Center = returnPosition + Projectile.DirectionFrom(returnPosition) * 120f;
// Disallow heads from getting too close to each other.
Projectile.MinionAntiClump(0.15f);
}
-2
View File
@@ -92,8 +92,6 @@ namespace CalamityMod.Projectiles.Summon
Projectile.timeLeft = 2;
}
}
//Anti sticky movement to prevent overlapping minions
Projectile.MinionAntiClump();
//Find a target
-2
View File
@@ -47,8 +47,6 @@ namespace CalamityMod.Projectiles.Summon
Projectile.rotation = (Projectile.spriteDirection == -1) ? Projectile.velocity.ToRotation() + MathHelper.Pi : Projectile.velocity.ToRotation();
Projectile.spriteDirection = (Projectile.velocity.X > 0).ToDirectionInt();
// Although the sprite is invisible, we'll keep track of it's direction and rotation so we can later apply it to the decorative herrings.
Projectile.ChargingMinionAI(EnemyDistanceDetection, 1500f, 2200f, 150f, 0, 24f, 15f, 4f, new Vector2(0f, -60f), 12f, 12f, CalamityPlayer.areThereAnyDamnBosses, true, 1); // The AI of the minion.
Projectile.netUpdate = true;
-1
View File
@@ -56,7 +56,6 @@ namespace CalamityMod.Projectiles.Summon
RamState();
break;
}
Projectile.MinionAntiClump(0.5f);
if (!Main.dedServ)
-1
View File
@@ -131,7 +131,6 @@ namespace CalamityMod.Projectiles.Summon
Projectile.Center = player.Center;
Projectile.netUpdate = true;
}
Projectile.MinionAntiClump(0.35f);
Projectile.rotation = Projectile.velocity.X * 0.03f;
}
-1
View File
@@ -82,7 +82,6 @@ namespace CalamityMod.Projectiles.Summon
hasSetTimers = true;
Projectile.netUpdate = true;
}
Projectile.MinionAntiClump();
Player owner = Owner;
@@ -60,7 +60,6 @@ namespace CalamityMod.Projectiles.Summon
}
if (!modPlayer.plaguebringerPatronSet)
Projectile.Kill();
Projectile.MinionAntiClump();
Projectile.ai[0]++;
@@ -97,7 +97,6 @@ namespace CalamityMod.Projectiles.Summon
lightScalar *= Main.essScale;
Lighting.AddLight(Projectile.Center, 0.7f * lightScalar, 0.6f * lightScalar, 0f * lightScalar);
}
Projectile.MinionAntiClump();
if (Vector2.Distance(player.Center, Projectile.Center) > 400f)
-1
View File
@@ -36,7 +36,6 @@ namespace CalamityMod.Projectiles.Summon
public override void AI()
{
// Prevent clumping of other slime puppets near this one.
Projectile.MinionAntiClump();
NPC potentialTarget = Projectile.Center.MinionHoming(800f, Owner);
-2
View File
@@ -191,8 +191,6 @@ namespace CalamityMod.Projectiles.Summon
Main.projectile[projIndex].minion = false;
}
}
// Prevent minion clumping while firing.
Projectile.MinionAntiClump(0.25f);
}
}
-1
View File
@@ -73,7 +73,6 @@ namespace CalamityMod.Projectiles.Summon
{
Projectile.spriteDirection = -Projectile.direction;
}
Projectile.ChargingMinionAI(700f, 800f, 1200f, 150f, 0, 40f, 8f, 4f, new Vector2(0f, -60f), 40f, 8f, false, true);
//Give off some light
@@ -95,8 +95,6 @@ namespace CalamityMod.Projectiles.Typeless
{
Dust.NewDust(Projectile.position + Projectile.velocity, Projectile.width, Projectile.height, DustID.BlueFairy, Projectile.velocity.X * 0.05f, Projectile.velocity.Y * 0.05f);
}
//Anti-sticky movement failsafe
Projectile.MinionAntiClump();
//If too far from player, increase speed to chase after player
-626
View File
@@ -1,626 +0,0 @@
using System;
using CalamityMod.CalPlayer;
using CalamityMod.Enums;
using CalamityMod.Projectiles.Summon;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using static Terraria.ModLoader.ModContent;
namespace CalamityMod
{
public static partial class CalamityUtils
{
public static void ChargingMinionAI(this Projectile projectile, float range, float maxPlayerDist, float extraMaxPlayerDist, float safeDist, int initialUpdates, float chargeDelayTime, float goToSpeed, float goBackSpeed, Vector2 returnOffset, float chargeCounterMax, float chargeSpeed, bool tileVision, bool ignoreTilesWhenCharging, int updateDifference = 1)
{
Player player = Main.player[projectile.owner];
CalamityPlayer modPlayer = player.Calamity();
//Anti sticky movement to prevent stacking
projectile.MinionAntiClump();
//Breather time between charges as like a reset
bool chargeDelay = false;
if (projectile.ai[0] == 2f)
{
projectile.ai[1] += 1f;
projectile.extraUpdates = initialUpdates + updateDifference;
if (projectile.ai[1] > chargeDelayTime)
{
projectile.ai[1] = 1f;
projectile.ai[0] = 0f;
projectile.extraUpdates = initialUpdates;
// CIT 2OCT2024: This line was breaking how the game counted minions,
// which resulted in minions being counted extra times and despawning other minions, most notoriously with Resurrection Butterfly.
// As such, I have commented it out.
// projectile.numUpdates = 0;
projectile.netUpdate = true;
}
else
{
chargeDelay = true;
}
}
if (chargeDelay)
{
return;
}
//Find a target
float maxDist = range;
Vector2 targetVec = projectile.position;
bool foundTarget = false;
bool isButterfly = projectile.type == ProjectileType<PurpleButterfly>();
//Prioritize the targeted enemy if possible
if (player.HasMinionAttackTargetNPC)
{
NPC npc = Main.npc[player.MinionAttackTargetNPC];
bool fishronCheck = npc.type == NPCID.DukeFishron && npc.active && isButterfly;
if (npc.CanBeChasedBy(projectile, false) || fishronCheck)
{
//Check the size of the target to make it easier to hit fat targets like Levi
float extraDist = (npc.width / 2) + (npc.height / 2);
float targetDist = Vector2.Distance(npc.Center, projectile.Center);
//Some minions will ignore tiles when choosing a target like Ice Claspers, others will not
bool canHit = true;
if (extraDist < maxDist && !tileVision)
canHit = Collision.CanHit(projectile.Center, 1, 1, npc.Center, 1, 1);
if (!foundTarget && targetDist < (maxDist + extraDist) && canHit)
{
maxDist = targetDist;
targetVec = npc.Center;
foundTarget = true;
}
}
}
//If no npc is specifically targetted or the selected enemy can't be found, check through the entire array
if (!foundTarget)
{
for (int npcIndex = 0; npcIndex < Main.maxNPCs; npcIndex++)
{
NPC npc = Main.npc[npcIndex];
bool fishronCheck = npc.type == NPCID.DukeFishron && npc.active && isButterfly;
if (npc.CanBeChasedBy(projectile, false) || fishronCheck)
{
float extraDist = (npc.width / 2) + (npc.height / 2);
float targetDist = Vector2.Distance(npc.Center, projectile.Center);
bool canHit = true;
if (extraDist < maxDist && !tileVision)
canHit = Collision.CanHit(projectile.Center, 1, 1, npc.Center, 1, 1);
if (!foundTarget && targetDist < (maxDist + extraDist) && canHit)
{
maxDist = targetDist;
targetVec = npc.Center;
foundTarget = true;
}
}
}
}
//If the player is too far, return to the player. Range is increased while attacking something.
float distBeforeForcedReturn = maxPlayerDist;
if (foundTarget)
{
distBeforeForcedReturn = extraMaxPlayerDist;
}
if (Vector2.Distance(player.Center, projectile.Center) > distBeforeForcedReturn)
{
projectile.ai[0] = 1f;
projectile.netUpdate = true;
}
//Go to the target if you found one
if (foundTarget && projectile.ai[0] == 0f)
{
//Some minions don't ignore tiles while charging like brittle stars
projectile.tileCollide = !ignoreTilesWhenCharging;
Vector2 targetSpot = targetVec - projectile.Center;
float targetDist = targetSpot.Length();
targetSpot.Normalize();
//Tries to get the minion in the sweet spot of 200 pixels away but the minion also charges so idk what good it does
if (targetDist > 200f)
{
float speed = goToSpeed; //8
targetSpot *= speed;
projectile.velocity = (projectile.velocity * 40f + targetSpot) / 41f;
}
else
{
float speed = -goBackSpeed; //-4
targetSpot *= speed;
projectile.velocity = (projectile.velocity * 40f + targetSpot) / 41f; //41
}
}
//Movement for idle or returning to the player
else
{
//Ignore tiles so they don't get stuck everywhere like Optic Staff
projectile.tileCollide = false;
bool returningToPlayer = false;
if (!returningToPlayer)
{
returningToPlayer = projectile.ai[0] == 1f;
}
//Player distance calculations
Vector2 playerVec = player.Center - projectile.Center + returnOffset;
float playerDist = playerVec.Length();
//If the minion is actively returning, move faster
float playerHomeSpeed = 6f;
if (returningToPlayer)
{
playerHomeSpeed = 15f;
}
//Move somewhat faster if the player is kinda far~ish
if (playerDist > 200f && playerHomeSpeed < 8f)
{
playerHomeSpeed = 8f;
}
//Return to normal if close enough to the player
if (playerDist < safeDist && returningToPlayer && !Collision.SolidCollision(projectile.position, projectile.width, projectile.height))
{
projectile.ai[0] = 0f;
projectile.netUpdate = true;
}
//Teleport to the player if abnormally far
if (playerDist > 2000f)
{
projectile.position.X = player.Center.X - (float)(projectile.width / 2);
projectile.position.Y = player.Center.Y - (float)(projectile.height / 2);
projectile.netUpdate = true;
}
//If more than 70 pixels away, move toward the player
if (playerDist > 70f)
{
playerVec.Normalize();
playerVec *= playerHomeSpeed;
projectile.velocity = (projectile.velocity * 40f + playerVec) / 41f;
}
//Minions never stay still
else if (projectile.velocity.X == 0f && projectile.velocity.Y == 0f)
{
projectile.velocity.X = -0.15f;
projectile.velocity.Y = -0.05f;
}
}
//Increment attack counter randomly
if (projectile.ai[1] > 0f)
{
projectile.ai[1] += (float)Main.rand.Next(1, 4);
}
//If high enough, prepare to attack
if (projectile.ai[1] > chargeCounterMax)
{
projectile.ai[1] = 0f;
projectile.netUpdate = true;
}
//Charge at an enemy if not on cooldown
if (projectile.ai[0] == 0f)
{
if (projectile.ai[1] == 0f && foundTarget && maxDist < 500f)
{
projectile.ai[1] += 1f;
if (Main.myPlayer == projectile.owner)
{
projectile.ai[0] = 2f;
Vector2 targetPos = targetVec - projectile.Center;
targetPos.Normalize();
projectile.velocity = targetPos * chargeSpeed; //8
projectile.netUpdate = true;
}
}
}
}
public static void MinionAntiClump(this Projectile projectile, float pushForce = 0.05f)
{
for (int k = 0; k < Main.maxProjectiles; k++)
{
Projectile otherProj = Main.projectile[k];
// Short circuits to make the loop as fast as possible
if (!otherProj.active || otherProj.owner != projectile.owner || !otherProj.minion || k == projectile.whoAmI)
continue;
// If the other projectile is indeed the same owned by the same player and they're too close, nudge them away.
bool sameProjType = otherProj.type == projectile.type;
float taxicabDist = Math.Abs(projectile.position.X - otherProj.position.X) + Math.Abs(projectile.position.Y - otherProj.position.Y);
if (sameProjType && taxicabDist < projectile.width)
{
if (projectile.position.X < otherProj.position.X)
projectile.velocity.X -= pushForce;
else
projectile.velocity.X += pushForce;
if (projectile.position.Y < otherProj.position.Y)
projectile.velocity.Y -= pushForce;
else
projectile.velocity.Y += pushForce;
}
}
}
public static void FloatingPetAI(this Projectile projectile, bool faceRight, float tiltFloat, bool lightPet = false)
{
Player player = Main.player[projectile.owner];
//anti sticking movement as a failsafe
float SAImovement = 0.05f;
for (int k = 0; k < Main.maxProjectiles; k++)
{
Projectile otherProj = Main.projectile[k];
// Short circuits to make the loop as fast as possible
if (!otherProj.active || otherProj.owner != projectile.owner || !Main.projPet[otherProj.type] || k == projectile.whoAmI)
continue;
// If the other projectile is indeed another pet owned by the same player and they're too close, nudge them away.
bool isPet = Main.projPet[otherProj.type];
float taxicabDist = Math.Abs(projectile.position.X - otherProj.position.X) + Math.Abs(projectile.position.Y - otherProj.position.Y);
if (isPet && taxicabDist < projectile.width)
{
if (projectile.position.X < otherProj.position.X)
projectile.velocity.X -= SAImovement;
else
projectile.velocity.X += SAImovement;
if (projectile.position.Y < otherProj.position.Y)
projectile.velocity.Y -= SAImovement;
else
projectile.velocity.Y += SAImovement;
}
}
float passiveMvtFloat = 0.5f;
projectile.tileCollide = false;
float range = 100f;
Vector2 projPos = projectile.Center;
float xDist = player.Center.X - projPos.X;
float yDist = player.Center.Y - projPos.Y;
yDist += Main.rand.NextFloat(-10, 20);
xDist += Main.rand.NextFloat(-10, 20);
//Light pets lead the player, normal pets trail the player
xDist += 60f * (lightPet ? (float)player.direction : -(float)player.direction);
yDist -= 60f;
Vector2 playerVector = new Vector2(xDist, yDist);
float playerDist = playerVector.Length();
float returnSpeed = 18f;
//If player is close enough, resume normal
if (playerDist < range && player.velocity.Y == 0f &&
projectile.Bottom.Y <= player.Bottom.Y &&
!Collision.SolidCollision(projectile.position, projectile.width, projectile.height))
{
if (projectile.velocity.Y < -6f)
{
projectile.velocity.Y = -6f;
}
}
//Teleport to player if too far
if (playerDist > 2000f)
{
projectile.position.X = player.Center.X - projectile.width / 2;
projectile.position.Y = player.Center.Y - projectile.height / 2;
projectile.netUpdate = true;
}
if (playerDist < 50f)
{
if (Math.Abs(projectile.velocity.X) > 2f || Math.Abs(projectile.velocity.Y) > 2f)
{
projectile.velocity *= 0.99f;
}
passiveMvtFloat = 0.01f;
}
else
{
if (playerDist < 100f)
{
passiveMvtFloat = 0.1f;
}
if (playerDist > 300f)
{
passiveMvtFloat = 1f;
}
playerDist = returnSpeed / playerDist;
playerVector.X *= playerDist;
playerVector.Y *= playerDist;
}
if (projectile.velocity.X < playerVector.X)
{
projectile.velocity.X += passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.X < 0f)
{
projectile.velocity.X += passiveMvtFloat;
}
}
if (projectile.velocity.X > playerVector.X)
{
projectile.velocity.X -= passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.X > 0f)
{
projectile.velocity.X -= passiveMvtFloat;
}
}
if (projectile.velocity.Y < playerVector.Y)
{
projectile.velocity.Y += passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.Y < 0f)
{
projectile.velocity.Y += passiveMvtFloat * 2f;
}
}
if (projectile.velocity.Y > playerVector.Y)
{
projectile.velocity.Y -= passiveMvtFloat;
if (passiveMvtFloat > 0.05f && projectile.velocity.Y > 0f)
{
projectile.velocity.Y -= passiveMvtFloat * 2f;
}
}
if (projectile.velocity.X >= 0.25f)
{
projectile.direction = faceRight ? 1 : -1;
}
else if (projectile.velocity.X < -0.25f)
{
projectile.direction = faceRight ? -1 : 1;
}
//Tilting and change directions
projectile.spriteDirection = projectile.direction;
projectile.rotation = projectile.velocity.X * tiltFloat;
}
public static void HealingProjectile(this Projectile projectile, int healing, int playerToHeal, float homingVelocity, float inertia, bool autoHomes = true, int timeCheck = 120)
{
int target = playerToHeal;
Player player = Main.player[target];
float homingSpeed = homingVelocity;
if (player.lifeMagnet)
homingSpeed *= 1.5f;
Vector2 playerVector = player.Center - projectile.Center;
float playerDist = playerVector.Length();
if (playerDist < 50f && projectile.position.X < player.position.X + player.width && projectile.position.X + projectile.width > player.position.X && projectile.position.Y < player.position.Y + player.height && projectile.position.Y + projectile.height > player.position.Y)
{
if (projectile.owner == Main.myPlayer && !Main.LocalPlayer.moonLeech)
{
int healAmt = healing;
player.HealPlayer(healAmt, HealTextType.Local);
NetMessage.SendData(MessageID.SpiritHeal, -1, -1, null, target, healAmt, 0f, 0f, 0, 0, 0);
}
projectile.Kill();
}
if (autoHomes)
{
playerDist = homingSpeed / playerDist;
playerVector.X *= playerDist;
playerVector.Y *= playerDist;
projectile.velocity.X = (projectile.velocity.X * inertia + playerVector.X) / (inertia + 1f);
projectile.velocity.Y = (projectile.velocity.Y * inertia + playerVector.Y) / (inertia + 1f);
}
else if (player.lifeMagnet && projectile.timeLeft < timeCheck)
{
playerDist = homingVelocity / playerDist;
playerVector.X *= playerDist;
playerVector.Y *= playerDist;
projectile.velocity.X = (projectile.velocity.X * inertia + playerVector.X) / (inertia + 1f);
projectile.velocity.Y = (projectile.velocity.Y * inertia + playerVector.Y) / (inertia + 1f);
}
}
/// <summary>
/// Call this function in the ai of your projectile so it can stick to enemies, also requires ModifyHitNPCSticky to be called in ModifyHitNPC
/// </summary>
/// <param name="projectile">The projectile you're adding sticky behaviour to</param>
/// <param name="timeLeft">Number of seconds you want a projectile to cling to an NPC</param>
public static void StickyProjAI(this Projectile projectile, int timeLeft, bool findNewNPC = false)
{
if (projectile.ai[0] == 1f)
{
int seconds = timeLeft;
bool killProj = false;
bool spawnDust = false;
//the projectile follows the NPC, even if it goes into blocks
projectile.tileCollide = false;
//timer for triggering hit effects
projectile.localAI[0]++;
if (projectile.localAI[0] % 30f == 0f)
{
spawnDust = true;
}
//So AI knows what NPC it is sticking to
int npcIndex = (int)projectile.ai[1];
NPC npc = Main.npc[npcIndex];
//Kill projectile after so many seconds or if the NPC it is stuck to no longer exists
if (projectile.localAI[0] >= (float)(60 * seconds))
{
killProj = true;
}
else if (!npcIndex.WithinBounds(Main.maxNPCs))
{
killProj = true;
}
else if (npc.active && !npc.dontTakeDamage)
{
//follow the NPC
projectile.Center = npc.Center - projectile.velocity * 2f;
projectile.gfxOffY = npc.gfxOffY;
//if attached to npc, trigger npc hit effects every half a second
if (spawnDust)
{
npc.HitEffect(0, 1.0);
}
}
else
{
killProj = true;
}
//Kill the projectile or reset stats if needed
if (killProj)
{
if (findNewNPC)
projectile.ai[0] = 0f;
else
projectile.Kill();
}
}
}
/// <summary>
/// Call this function in ModifyHitNPC to make your projectiles stick to enemies, needs StickyProjAI to be called in the AI of the projectile
/// </summary>
/// <param name="projectile">The projectile you're giving sticky behaviour to</param>
/// <param name="maxStick">How many projectiles of this type can stick to one enemy</param>
public static void ModifyHitNPCSticky(this Projectile projectile, int maxStick)
{
Player player = Main.player[projectile.owner];
Rectangle myRect = projectile.Hitbox;
if (projectile.owner == Main.myPlayer)
{
for (int npcIndex = 0; npcIndex < Main.maxNPCs; npcIndex++)
{
NPC npc = Main.npc[npcIndex];
//covers most edge cases like voodoo dolls
if (npc.active && !npc.dontTakeDamage &&
((projectile.friendly && (!npc.friendly || (npc.type == NPCID.Guide && projectile.owner < Main.maxPlayers && player.killGuide) || (npc.type == NPCID.Clothier && projectile.owner < Main.maxPlayers && player.killClothier))) ||
(projectile.hostile && npc.friendly && !npc.dontTakeDamageFromHostiles)) && (projectile.owner < 0 || npc.immune[projectile.owner] == 0 || projectile.maxPenetrate == 1))
{
if (npc.noTileCollide || !projectile.ownerHitCheck)
{
bool stickingToNPC;
//Solar Crawltipede tail has special collision
if (npc.type == NPCID.SolarCrawltipedeTail)
{
Rectangle rect = npc.Hitbox;
int crawltipedeHitboxMod = 8;
rect.X -= crawltipedeHitboxMod;
rect.Y -= crawltipedeHitboxMod;
rect.Width += crawltipedeHitboxMod * 2;
rect.Height += crawltipedeHitboxMod * 2;
stickingToNPC = projectile.Colliding(myRect, rect);
}
else
{
stickingToNPC = projectile.Colliding(myRect, npc.Hitbox);
}
if (stickingToNPC)
{
//reflect projectile if the npc can reflect it (like Selenians)
if (npc.reflectsProjectiles && projectile.CanBeReflected())
{
npc.ReflectProjectile(projectile);
return;
}
//let the projectile know it is sticking and the npc it is sticking too
projectile.ai[0] = 1f;
projectile.ai[1] = (float)npcIndex;
//follow the NPC
projectile.velocity = (npc.Center - projectile.Center);
projectile.netUpdate = true;
//Count how many projectiles are attached, delete as necessary
Point[] array2 = new Point[maxStick];
int projCount = 0;
for (int projIndex = 0; projIndex < Main.maxProjectiles; projIndex++)
{
Projectile proj = Main.projectile[projIndex];
if (projIndex != projectile.whoAmI && proj.active && proj.owner == Main.myPlayer && proj.type == projectile.type && proj.ai[0] == 1f && proj.ai[1] == (float)npcIndex)
{
array2[projCount++] = new Point(projIndex, proj.timeLeft);
if (projCount >= array2.Length)
{
break;
}
}
}
if (projCount >= array2.Length)
{
int stuckProjAmt = 0;
for (int m = 1; m < array2.Length; m++)
{
if (array2[m].Y < array2[stuckProjAmt].Y)
{
stuckProjAmt = m;
}
}
Main.projectile[array2[stuckProjAmt].X].Kill();
}
}
}
}
}
}
}
public static void StickToTiles(this Projectile projectile, bool ignorePlatforms, bool stickToEverything)
{
try
{
int xLeft = (int)(projectile.position.X / 16f) - 1;
int xRight = (int)((projectile.position.X + (float)projectile.width) / 16f) + 2;
int yBottom = (int)(projectile.position.Y / 16f) - 1;
int yTop = (int)((projectile.position.Y + (float)projectile.height) / 16f) + 2;
if (xLeft < 0)
{
xLeft = 0;
}
if (xRight > Main.maxTilesX)
{
xRight = Main.maxTilesX;
}
if (yBottom < 0)
{
yBottom = 0;
}
if (yTop > Main.maxTilesY)
{
yTop = Main.maxTilesY;
}
for (int x = xLeft; x < xRight; x++)
{
for (int y = yBottom; y < yTop; y++)
{
Tile tile = Main.tile[x, y];
bool platformCheck = true;
if (ignorePlatforms)
platformCheck = !TileID.Sets.Platforms[tile.TileType] && tile.TileType != TileID.PlanterBox;
bool tableCheck = false;
if (stickToEverything)
tableCheck = Main.tileSolidTop[tile.TileType] && tile.TileFrameY == 0;
if (tile != null && tile.HasUnactuatedTile && platformCheck && (Main.tileSolid[tile.TileType] || tableCheck))
{
Vector2 tileSize;
tileSize.X = (float)(x * 16);
tileSize.Y = (float)(y * 16);
if (projectile.position.X + (float)projectile.width - 4f > tileSize.X && projectile.position.X + 4f < tileSize.X + 16f && projectile.position.Y + (float)projectile.height - 4f > tileSize.Y && projectile.position.Y + 4f < tileSize.Y + 16f)
{
projectile.velocity.X = 0f;
projectile.velocity.Y = -0.2f;
}
}
}
}
}
catch
{
}
}
}
}