Files
CalamityModPublic/Utilities/ProjectileUtils.cs
T

1106 lines
35 KiB
C#

using CalamityMod.CalPlayer;
using CalamityMod.Projectiles.Magic;
using CalamityMod.Projectiles.Summon;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using static Terraria.ModLoader.ModContent;
namespace CalamityMod
{
public static partial class CalamityUtils
{
public static int CountProjectiles(int Type) => Main.projectile.Count(proj => proj.type == Type && proj.active);
public static int CountHookProj() => Main.projectile.Count(proj => Main.projHook[proj.type] && proj.ai[0] == 2f && proj.active && proj.owner == Main.myPlayer);
public static bool FinalExtraUpdate(this Projectile proj) => proj.numUpdates == -1;
public static bool IsSummon(this Projectile proj) => proj.minion || proj.sentry || CalamityLists.projectileMinionList.Contains(proj.type) || ProjectileID.Sets.MinionShot[proj.type] || ProjectileID.Sets.SentryShot[proj.type];
public static void KillAllHostileProjectiles()
{
for (int x = 0; x < Main.maxProjectiles; x++)
{
Projectile projectile = Main.projectile[x];
if (projectile.active && projectile.hostile && !projectile.friendly && projectile.damage > 0)
{
projectile.Kill();
}
}
}
public static void KillShootProjectiles(bool shouldBreak, int projType, Player player)
{
for (int x = 0; x < Main.maxProjectiles; x++)
{
Projectile proj = Main.projectile[x];
if (proj.active && proj.owner == player.whoAmI && proj.type == projType)
{
proj.Kill();
if (shouldBreak)
break;
}
}
}
public static void KillShootProjectileMany(Player player, params int[] projTypes)
{
for (int x = 0; x < Main.maxProjectiles; x++)
{
Projectile proj = Main.projectile[x];
if (proj.active && proj.owner == player.whoAmI && projTypes.Contains(proj.type))
{
proj.Kill();
}
}
}
public static int FindFirstProjectile(int Type)
{
int index = -1;
for (int x = 0; x < Main.maxProjectiles; x++)
{
Projectile proj = Main.projectile[x];
if (proj.active && proj.type == Type)
{
index = x;
break;
}
}
return index;
}
public static void OnlyOneSentry(Player player, int Type)
{
int existingTurrets = player.ownedProjectileCounts[Type];
if (existingTurrets > 0)
{
for (int i = 0; i < Main.maxProjectiles; i++)
{
if (Main.projectile[i].type == Type &&
Main.projectile[i].owner == player.whoAmI &&
Main.projectile[i].active)
{
Main.projectile[i].Kill();
existingTurrets--;
if (existingTurrets <= 0)
break;
}
}
}
}
/// <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>
/// <param name="constantDamage">Decides if you want the projectile to deal damage while its sticked to enemies or not</param>
public static void ModifyHitNPCSticky(this Projectile projectile, int maxStick, bool constantDamage)
{
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 || projectile.CanHit(npc))
{
bool stickingToNPC;
//Solar Crawltipede tail has special collision
if (npc.type == NPCID.SolarCrawltipedeTail)
{
Rectangle rect = npc.Hitbox;
int num5 = 8;
rect.X -= num5;
rect.Y -= num5;
rect.Width += num5 * 2;
rect.Height += num5 * 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.reflectingProjectiles && projectile.CanReflect())
{
npc.ReflectProjectile(projectile.whoAmI);
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) * 0.75f;
projectile.netUpdate = true;
//Set projectile damage to 0 if desired
if (!constantDamage)
projectile.damage = 0;
//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 num30 = 0;
for (int m = 1; m < array2.Length; m++)
{
if (array2[m].Y < array2[num30].Y)
{
num30 = m;
}
}
Main.projectile[array2[num30].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.type] && tile.type != TileID.PlanterBox;
bool tableCheck = false;
if (stickToEverything)
tableCheck = Main.tileSolidTop[tile.type] && tile.frameY == 0;
if (tile != null && tile.nactive() && platformCheck && (Main.tileSolid[tile.type] || 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
{
}
}
public static Projectile ProjectileRain(Vector2 targetPos, float xLimit, float xVariance, float yLimitLower, float yLimitUpper, float projSpeed, int projType, int damage, float knockback, int owner)
{
float x = targetPos.X + Main.rand.NextFloat(-xLimit, xLimit);
if (projType == ProjectileType<AstralStarMagic>())
x = targetPos.X + xLimit;
float y = targetPos.Y - Main.rand.NextFloat(yLimitLower, yLimitUpper);
Vector2 source = new Vector2(x, y);
Vector2 velocity = targetPos - source;
velocity.X += Main.rand.NextFloat(-xVariance, xVariance);
float speed = projSpeed;
float targetDist = velocity.Length();
targetDist = speed / targetDist;
velocity.X *= targetDist;
velocity.Y *= targetDist;
return Projectile.NewProjectileDirect(source, velocity, projType, damage, knockback, owner);
}
public static Projectile ProjectileBarrage(Vector2 originVec, Vector2 targetPos, bool fromRight, float xOffsetMin, float xOffsetMax, float yOffsetMin, float yOffsetMax, float projSpeed, int projType, int damage, float knockback, int owner, bool clamped = false, float inaccuracyOffset = 5f)
{
float xPos = originVec.X + Main.rand.NextFloat(xOffsetMin, xOffsetMax) * fromRight.ToDirectionInt();
float yPos = originVec.Y + Main.rand.NextFloat(yOffsetMin, yOffsetMax) * Main.rand.NextBool().ToDirectionInt();
Vector2 spawnPosition = new Vector2(xPos, yPos);
Vector2 velocity = targetPos - spawnPosition;
velocity.X += Main.rand.NextFloat(-inaccuracyOffset, inaccuracyOffset);
velocity.Y += Main.rand.NextFloat(-inaccuracyOffset, inaccuracyOffset);
velocity.Normalize();
velocity *= projSpeed * (clamped ? 150f : 1f);
//This clamp means the spawned projectiles only go at diagnals and are not accurate
if (clamped)
{
velocity.X = MathHelper.Clamp(velocity.X, -15f, 15f);
velocity.Y = MathHelper.Clamp(velocity.Y, -15f, 15f);
}
return Projectile.NewProjectileDirect(spawnPosition, velocity, projType, damage, knockback, owner);
}
public static int DamageSoftCap(double dmgInput, int cap)
{
int newDamage = (int)dmgInput;
if (newDamage > cap)
{
newDamage = (int)((dmgInput - cap) * 0.1) + cap;
}
if (newDamage < 1)
newDamage = 1;
return newDamage;
}
public static Vector2 RandomVelocity(float directionMult, float speedLowerLimit, float speedCap, float speedMult = 0.1f)
{
Vector2 velocity = new Vector2(Main.rand.NextFloat(-directionMult, directionMult), Main.rand.NextFloat(-directionMult, directionMult));
//Rerolling to avoid dividing by zero
while (velocity.X == 0f && velocity.Y == 0f)
{
velocity = new Vector2(Main.rand.NextFloat(-directionMult, directionMult), Main.rand.NextFloat(-directionMult, directionMult));
}
velocity.Normalize();
velocity *= Main.rand.NextFloat(speedLowerLimit, speedCap) * speedMult;
return velocity;
}
public static void SporeSacAI(this Projectile projectile)
{
Player player = Main.player[projectile.owner];
float scaleAmt = 1f - (float)projectile.alpha / 255f;
scaleAmt *= projectile.scale;
Lighting.AddLight(projectile.Center, 0.25f * scaleAmt, 0.025f * scaleAmt, 0.275f * scaleAmt);
projectile.localAI[0] += 1f;
if (projectile.localAI[0] >= 90f)
{
projectile.localAI[0] *= -1f;
}
if (projectile.localAI[0] >= 0f)
{
projectile.scale += 0.003f;
}
else
{
projectile.scale -= 0.003f;
}
projectile.rotation += 0.0025f * projectile.scale;
float yVel = 1f;
float xVel = 1f;
if (projectile.identity % 6 == 0)
{
xVel *= -1f;
}
if (projectile.identity % 6 == 1)
{
yVel *= -1f;
}
if (projectile.identity % 6 == 2)
{
xVel *= -1f;
yVel *= -1f;
}
if (projectile.identity % 6 == 3)
{
xVel = 0f;
}
if (projectile.identity % 6 == 4)
{
yVel = 0f;
}
projectile.localAI[1] += 1f;
if (projectile.localAI[1] > 60f)
{
projectile.localAI[1] = -180f;
}
if (projectile.localAI[1] >= -60f)
{
projectile.velocity.X += 0.002f * xVel;
projectile.velocity.Y += 0.002f * yVel;
}
else
{
projectile.velocity.X -= 0.002f * xVel;
projectile.velocity.Y -= 0.002f * yVel;
}
projectile.ai[0] += 1f;
if (projectile.ai[0] > 5400f)
{
projectile.damage = 0;
projectile.ai[1] = 1f;
if (projectile.alpha < 255)
{
projectile.alpha += 5;
if (projectile.alpha > 255)
{
projectile.alpha = 255;
}
}
else if (projectile.owner == Main.myPlayer)
{
projectile.Kill();
}
}
else
{
float playerDist = (projectile.Center - player.Center).Length() / 100f;
if (playerDist > 4f)
{
playerDist *= 1.1f;
}
if (playerDist > 5f)
{
playerDist *= 1.2f;
}
if (playerDist > 6f)
{
playerDist *= 1.3f;
}
if (playerDist > 7f)
{
playerDist *= 1.4f;
}
if (playerDist > 8f)
{
playerDist *= 1.5f;
}
if (playerDist > 9f)
{
playerDist *= 1.6f;
}
if (playerDist > 10f)
{
playerDist *= 1.7f;
}
projectile.ai[0] += playerDist;
if (projectile.alpha > 50)
{
projectile.alpha -= 10;
if (projectile.alpha < 50)
{
projectile.alpha = 50;
}
}
}
if (projectile.Calamity().lineColor != 1 || projectile.timeLeft < (3300 + projectile.localAI[0]))
{
bool foundTarget = false;
Vector2 targetPos = new Vector2(0f, 0f);
float maxDist = 600f;
for (int i = 0; i < Main.maxNPCs; i++)
{
NPC npc = Main.npc[i];
if (npc.CanBeChasedBy(projectile, false))
{
float targetDist = Vector2.Distance(projectile.Center, npc.Center);
if (targetDist < maxDist)
{
targetPos = npc.Center;
foundTarget = true;
break;
}
}
}
if (foundTarget)
{
if (projectile.Calamity().lineColor == 1)
projectile.extraUpdates = 5;
Vector2 targetVec = targetPos - projectile.Center;
targetVec.Normalize();
targetVec *= 0.75f;
projectile.velocity = (projectile.velocity * 10f + targetVec) / 11f;
return;
}
}
if (projectile.velocity.Length() > 0.2f)
{
projectile.velocity *= 0.98f;
}
}
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 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;
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 = new Vector2(projectile.Center.X, projectile.Center.Y);
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.position.Y + projectile.height <= player.position.Y + player.height &&
!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 N, 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.player[Main.myPlayer].moonLeech)
{
int healAmt = healing;
player.HealEffect(healAmt, false);
player.statLife += healAmt;
if (player.statLife > player.statLifeMax2)
{
player.statLife = player.statLifeMax2;
}
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 * N + playerVector.X) / (N + 1f);
projectile.velocity.Y = (projectile.velocity.Y * N + playerVector.Y) / (N + 1f);
}
else if (player.lifeMagnet && projectile.timeLeft < timeCheck)
{
playerDist = homingVelocity / playerDist;
playerVector.X *= playerDist;
playerVector.Y *= playerDist;
projectile.velocity.X = (projectile.velocity.X * N + playerVector.X) / (N + 1f);
projectile.velocity.Y = (projectile.velocity.Y * N + playerVector.Y) / (N + 1f);
}
}
public static bool DrawBeam(this Projectile projectile, float length, float width, Color lightColor, Texture2D texture = null)
{
if (texture is null)
texture = Main.projectileTexture[projectile.type];
float widthOffset = (float)(texture.Width - projectile.width) * 0.5f + (float)projectile.width * 0.5f;
float heightOffset = (float)(projectile.height / 2);
Vector2 origin = new Vector2(widthOffset, heightOffset);
SpriteEffects spriteEffects = SpriteEffects.None;
if (projectile.spriteDirection == -1)
{
spriteEffects = SpriteEffects.FlipHorizontally;
}
Rectangle roughScreenBounds = new Rectangle((int)Main.screenPosition.X - 500, (int)Main.screenPosition.Y - 500, Main.screenWidth + 1000, Main.screenHeight + 1000);
if (projectile.getRect().Intersects(roughScreenBounds))
{
Vector2 drawPos = projectile.position - Main.screenPosition + origin;
drawPos.Y += projectile.gfxOffY;
float maxTrailPoints = length;
float wide = width;
if (projectile.ai[1] == 1f)
{
maxTrailPoints = (int)projectile.localAI[0];
}
for (int i = 1; i <= (int)projectile.localAI[0]; i++)
{
Vector2 offset = Vector2.Normalize(projectile.velocity) * (float)i * wide;
Color color = projectile.GetAlpha(lightColor);
color *= (maxTrailPoints - (float)i) / maxTrailPoints;
color.A = 0;
Main.spriteBatch.Draw(texture, drawPos - offset, null, color, projectile.rotation, origin, projectile.scale, spriteEffects, 0f);
}
}
return false;
}
public static void ExplodeandDestroyTiles(Projectile projectile, int explosionRadius, bool checkExplosions, List<int> tilesToCheck, List<int> wallsToCheck)
{
int minTileX = (int)projectile.position.X / 16 - explosionRadius;
int maxTileX = (int)projectile.position.X / 16 + explosionRadius;
int minTileY = (int)projectile.position.Y / 16 - explosionRadius;
int maxTileY = (int)projectile.position.Y / 16 + explosionRadius;
if (minTileX < 0)
{
minTileX = 0;
}
if (maxTileX > Main.maxTilesX)
{
maxTileX = Main.maxTilesX;
}
if (minTileY < 0)
{
minTileY = 0;
}
if (maxTileY > Main.maxTilesY)
{
maxTileY = Main.maxTilesY;
}
bool canKillWalls = false;
for (int x = minTileX; x <= maxTileX; x++)
{
for (int y = minTileY; y <= maxTileY; y++)
{
Vector2 explodeArea = new Vector2(Math.Abs(x - projectile.position.X / 16f), Math.Abs(y - projectile.position.Y / 16f));
float distance = explodeArea.Length();
if (distance < explosionRadius && Main.tile[x, y] != null && Main.tile[x, y].wall == WallID.None)
{
canKillWalls = true;
break;
}
}
}
List<int> tileExcludeList = new List<int>()
{
TileID.DemonAltar,
TileID.ElderCrystalStand
};
for (int i = 0; i < tilesToCheck.Count; ++i)
tileExcludeList.Add(tilesToCheck[i]);
List<int> wallExcludeList = new List<int>();
for (int i = 0; i < wallsToCheck.Count; ++i)
wallExcludeList.Add(wallsToCheck[i]);
List<int> explosionCheckList = new List<int>()
{
TileID.DemonAltar,
TileID.Cobalt,
TileID.Mythril,
TileID.Adamantite,
TileID.Palladium,
TileID.Orichalcum,
TileID.Titanium,
TileID.Chlorophyte,
TileID.LihzahrdBrick,
TileID.LihzahrdAltar,
TileID.DesertFossil
};
CalamityUtils.AddWithCondition<int>(explosionCheckList, TileID.Hellstone, !Main.hardMode);
for (int i = minTileX; i <= maxTileX; i++)
{
for (int j = minTileY; j <= maxTileY; j++)
{
Tile tile = Main.tile[i, j];
bool t = 1 == 1; bool f = 1 == 2;
Vector2 explodeArea = new Vector2(Math.Abs(i - projectile.position.X / 16f), Math.Abs(j - projectile.position.Y / 16f));
float distance = explodeArea.Length();
if (distance < explosionRadius)
{
bool canKillTile = true;
if (tile != null && tile.active())
{
if (checkExplosions)
{
if (Main.tileDungeon[tile.type] || explosionCheckList.Contains(tile.type))
{
canKillTile = false;
}
if (!TileLoader.CanExplode(i, j))
{
canKillTile = false;
}
}
if (Main.tileContainer[tile.type])
canKillTile = false;
if (!TileLoader.CanKillTile(i, j, tile.type, ref t) || !TileLoader.CanKillTile(i, j, tile.type, ref f))
canKillTile = false;
if (tileExcludeList.Contains(tile.type))
canKillTile = false;
if (canKillTile)
{
WorldGen.KillTile(i, j, false, false, false);
if (!tile.active() && Main.netMode != NetmodeID.SinglePlayer)
{
NetMessage.SendData(MessageID.TileChange, -1, -1, null, 0, i, j, 0f, 0, 0, 0);
}
}
}
if (canKillTile)
{
for (int x = i - 1; x <= i + 1; x++)
{
for (int y = j - 1; y <= j + 1; y++)
{
bool canExplode = true;
if (checkExplosions)
canExplode = WallLoader.CanExplode(x, y, Main.tile[x, y].wall);
if (wallExcludeList.Any() && wallExcludeList.Contains(Main.tile[x, y].wall))
canKillWalls = false;
if (Main.tile[x, y] != null && Main.tile[x, y].wall > WallID.None && canKillWalls && canExplode)
{
WorldGen.KillWall(x, y, false);
if (Main.tile[x, y].wall == WallID.None && Main.netMode != NetmodeID.SinglePlayer)
{
NetMessage.SendData(MessageID.TileChange, -1, -1, null, 2, x, y, 0f, 0, 0, 0);
}
}
}
}
}
}
}
}
}
}
}