Rebased AI refactors safely in preparation for 1.4

This commit is contained in:
Dominic authored and Cameron Ewell committed 2022-04-02 14:02:13 -04:00
1 parent 0e9959d864
commit 3a2bdb8a02
4 files changed
+272 -257

No files matched your search

+3 -254
View File
@@ -7,6 +7,7 @@ using CalamityMod.NPCs.Astral;
using CalamityMod.NPCs.Crags;
using CalamityMod.NPCs.NormalNPCs;
using CalamityMod.NPCs.PlagueEnemies;
using CalamityMod.NPCs.VanillaNPCOverrides.RegularEnemies;
using CalamityMod.Projectiles.Boss;
using CalamityMod.World;
using Microsoft.Xna.Framework;
@@ -28,258 +29,6 @@ namespace CalamityMod.NPCs
{
#region Death Mode NPC AI
#region Demon Eye AI
public static void DemonEyeBatMovement(NPC npc, float maxXSpeed = 6f, float maxYSpeed = 3.5f,
float xAccel = 0.1f, float xAccelBoost1 = 0.06f, float xAccelBoost2 = 0.25f,
float yAccel = 0.12f, float yAccelBoost1 = 0.07f, float yAccelBoost2 = 0.2f)
{
if (npc.direction == -1 && npc.velocity.X > -maxXSpeed)
{
npc.velocity.X -= xAccel;
if (npc.velocity.X > maxXSpeed)
{
npc.velocity.X -= xAccelBoost1;
}
else if (npc.velocity.X > 0f)
{
npc.velocity.X -= xAccelBoost2;
}
if (npc.velocity.X < -maxXSpeed)
{
npc.velocity.X = -maxXSpeed;
}
}
else if (npc.direction == 1 && npc.velocity.X < maxXSpeed)
{
npc.velocity.X += xAccel;
if (npc.velocity.X < -maxXSpeed)
{
npc.velocity.X += xAccelBoost1;
}
else if (npc.velocity.X < 0f)
{
npc.velocity.X += xAccelBoost2;
}
if (npc.velocity.X > maxXSpeed)
{
npc.velocity.X = maxXSpeed;
}
}
if (npc.directionY == -1 && npc.velocity.Y > -maxYSpeed)
{
npc.velocity.Y -= yAccel;
if (npc.velocity.Y > maxYSpeed)
{
npc.velocity.Y -= yAccelBoost1;
}
else if (npc.velocity.Y > 0f)
{
npc.velocity.Y -= yAccelBoost2;
}
if (npc.velocity.Y < -maxYSpeed)
{
npc.velocity.Y = -maxYSpeed;
}
}
else if (npc.directionY == 1 && npc.velocity.Y < maxYSpeed)
{
npc.velocity.Y += yAccel;
if (npc.velocity.Y < -maxYSpeed)
{
npc.velocity.Y += yAccelBoost1;
}
else if (npc.velocity.Y < 0f)
{
npc.velocity.Y += yAccelBoost2;
}
if (npc.velocity.Y > maxYSpeed)
{
npc.velocity.Y = maxYSpeed;
}
}
}
public static bool BuffedDemonEyeAI(NPC npc, Mod mod)
{
// Subtypes of enemies with this AI. Made for programmer convenience.
List<int> nightTimeEnemies = new List<int>()
{
NPCID.DemonEye,
NPCID.WanderingEye,
NPCID.CataractEye,
NPCID.SleepyEye,
NPCID.DialatedEye,
NPCID.GreenEye,
NPCID.PurpleEye,
NPCID.DemonEyeOwl,
NPCID.DemonEyeSpaceship,
ModContent.NPCType<BlightedEye>()
};
List<int> pigrons = new List<int>()
{
NPCID.PigronCorruption,
NPCID.PigronCrimson,
NPCID.PigronHallow
};
// Pigron noises
if (pigrons.Contains(npc.type) && Main.rand.NextBool(1000))
{
Main.PlaySound(SoundID.Zombie, (int)npc.position.X, (int)npc.position.Y, 9, 1f, 0f);
}
npc.noGravity = true;
if (!npc.noTileCollide)
{
// Bounce off of tiles on the X axis.
if (npc.collideX)
{
npc.velocity.X = npc.oldVelocity.X * -0.5f;
if (npc.direction == -1 && npc.velocity.X > 0f && npc.velocity.X < 2f)
{
npc.velocity.X = 2f;
}
if (npc.direction == 1 && npc.velocity.X < 0f && npc.velocity.X > -2f)
{
npc.velocity.X = -2f;
}
}
// Bounce off of tiles on the Y axis.
if (npc.collideY)
{
npc.velocity.Y = npc.oldVelocity.Y * -0.5f;
if (npc.velocity.Y > 0f && npc.velocity.Y < 1f)
{
npc.velocity.Y = 1f;
}
if (npc.velocity.Y < 0f && npc.velocity.Y > -1f)
{
npc.velocity.Y = -1f;
}
}
}
if (Main.dayTime && npc.position.Y <= Main.worldSurface * 16.0 && nightTimeEnemies.Contains(npc.type))
{
if (npc.timeLeft > 10)
{
npc.timeLeft = 10;
}
// Adjust directions
npc.directionY = -1;
if (npc.velocity.Y > 0f)
{
npc.direction = 1;
}
npc.direction = -1;
if (npc.velocity.X > 0f)
{
npc.direction = 1;
}
}
else
{
npc.TargetClosest(true);
}
if (pigrons.Contains(npc.type) || npc.type == ModContent.NPCType<CalamityEye>() || npc.type == ModContent.NPCType<BlightedEye>())
{
if (Collision.CanHit(npc.position, npc.width, npc.height, Main.player[npc.target].position, Main.player[npc.target].width, Main.player[npc.target].height))
{
if (npc.ai[1] > 0f && !Collision.SolidCollision(npc.position, npc.width, npc.height))
{
npc.ai[1] = 0f;
npc.ai[0] = 0f;
npc.netUpdate = true;
}
}
else if (npc.ai[1] == 0f)
{
npc.ai[0] += 1f;
}
if (npc.ai[0] >= 300f)
{
npc.ai[1] = 1f;
npc.ai[0] = 0f;
npc.netUpdate = true;
}
if (npc.ai[1] == 0f)
{
npc.alpha = 0;
npc.noTileCollide = false;
}
else
{
npc.wet = false;
npc.alpha = 200;
npc.noTileCollide = true;
}
npc.rotation = npc.velocity.Y * 0.1f * npc.direction;
npc.TargetClosest(true);
DemonEyeBatMovement(npc);
}
else if (npc.type == NPCID.TheHungryII)
{
npc.TargetClosest(true);
// Why do the Hungry create light??
Lighting.AddLight((int)npc.Center.X / 16, (int)npc.Center.Y / 16, 0.3f, 0.2f, 0.1f);
DemonEyeBatMovement(npc, 8f, 3.5f, 0.12f, 0.12f, 0.25f, 0.06f, 0.07f, 0.2f);
if (Main.rand.NextBool(40))
{
int idx = Dust.NewDust(new Vector2(npc.position.X, npc.position.Y + (float)npc.height * 0.25f), npc.width, (int)((float)npc.height * 0.5f), DustID.Blood, npc.velocity.X, 2f, 0, default, 1f);
Dust dust = Main.dust[idx];
dust.velocity.X *= 0.5f;
dust.velocity.Y *= 0.1f;
}
}
else if (npc.type == NPCID.WanderingEye)
{
if (npc.life < (double)npc.lifeMax * 0.5)
{
DemonEyeBatMovement(npc, 8f, 6f, 0.12f, 0.12f, 0.07f, 0.12f, 0.12f, 0.07f);
}
else
{
DemonEyeBatMovement(npc, 6f, 2.5f, 0.12f, 0.12f, 0.07f, 0.06f, 0.07f, 0.05f);
}
}
else
{
float maxSpeedX = 6f;
float maxSpeedY = 2.5f;
maxSpeedX *= 1f + (1f - npc.scale);
maxSpeedY *= 1f + (1f - npc.scale);
DemonEyeBatMovement(npc, maxSpeedX, maxSpeedY, 0.08f, 0.08f, 0.03f, 0.02f, 0.03f, 0.015f);
}
if ((npc.type == NPCID.DemonEye ||
npc.type == NPCID.WanderingEye ||
npc.type == ModContent.NPCType<CalamityEye>() ||
npc.type == ModContent.NPCType<BlightedEye>() ||
(npc.type >= NPCID.CataractEye && npc.type <= NPCID.PurpleEye))
&& Main.rand.NextBool(40))
{
int num4 = Dust.NewDust(new Vector2(npc.position.X, npc.position.Y + (float)npc.height * 0.25f), npc.width, (int)((float)npc.height * 0.5f), DustID.Blood, npc.velocity.X, 2f, 0, default(Color), 1f);
Dust dust = Main.dust[num4];
dust.velocity.X *= 0.5f;
dust.velocity.Y *= 0.1f;
}
if (npc.wet && !pigrons.Contains(npc.type))
{
if (npc.velocity.Y > 0f)
{
npc.velocity.Y *= 0.95f;
}
npc.velocity.Y -= 0.7f;
if (npc.velocity.Y < -6f)
{
npc.velocity.Y = -6f;
}
npc.TargetClosest(true);
}
return false;
}
#endregion
#region Fighter AI
// Methods for AIs that are always terminated via a return in the Fighter AI code. More specialized and general
@@ -5750,7 +5499,7 @@ namespace CalamityMod.NPCs
yAccelBoost1 = 0.07f;
yAccelBoost2 = 0.05f;
}
DemonEyeBatMovement(npc, maxSpeedX, maxSpeedY, xAccel, xAccelBoost1, xAccelBoost2, yAccel, yAccelBoost1, yAccelBoost2);
DemonEyeAI.DemonEyeBatMovement(npc, maxSpeedX, maxSpeedY, xAccel, xAccelBoost1, xAccelBoost2, yAccel, yAccelBoost1, yAccelBoost2);
if (npc.type == NPCID.CaveBat ||
npc.type == NPCID.JungleBat ||
npc.type == NPCID.Hellbat ||
@@ -5799,7 +5548,7 @@ namespace CalamityMod.NPCs
yAccelBoost1 = 0.07f;
yAccelBoost2 = 0.05f;
}
DemonEyeBatMovement(npc, maxSpeedX, maxSpeedY, xAccel, xAccelBoost1, xAccelBoost2, yAccel, yAccelBoost1, yAccelBoost2);
DemonEyeAI.DemonEyeBatMovement(npc, maxSpeedX, maxSpeedY, xAccel, xAccelBoost1, xAccelBoost2, yAccel, yAccelBoost1, yAccelBoost2);
}
if (npc.type == NPCID.Harpy && npc.wet)
{
+3 -2
View File
@@ -72,6 +72,7 @@ using Terraria.Utilities;
using static Terraria.ModLoader.ModContent;
using CalamityMod.NPCs.ExoMechs;
using CalamityMod.NPCs.VanillaNPCOverrides.Bosses;
using CalamityMod.NPCs.VanillaNPCOverrides.RegularEnemies;
using CalamityMod.Balancing;
namespace CalamityMod.NPCs
@@ -2828,7 +2829,7 @@ namespace CalamityMod.NPCs
case 2:
if (npc.type == NPCType<BlightedEye>() || npc.type == NPCType<CalamityEye>())
{
return CalamityGlobalAI.BuffedDemonEyeAI(npc, mod);
return DemonEyeAI.BuffedDemonEyeAI(npc, mod);
}
else
{
@@ -2847,7 +2848,7 @@ namespace CalamityMod.NPCs
case NPCID.PurpleEye:
case NPCID.DemonEyeOwl:
case NPCID.DemonEyeSpaceship:
return CalamityGlobalAI.BuffedDemonEyeAI(npc, mod);
return DemonEyeAI.BuffedDemonEyeAI(npc, mod);
}
}
break;
@@ -0,0 +1,265 @@
using CalamityMod.NPCs.AcidRain;
using CalamityMod.NPCs.Crags;
using CalamityMod.NPCs.NormalNPCs;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.NPCs.VanillaNPCOverrides.RegularEnemies
{
public static class DemonEyeAI
{
// Subtypes of enemies with this AI. Made for programmer convenience.
public static List<int> NightTimeEnemies => new List<int>()
{
NPCID.DemonEye,
NPCID.WanderingEye,
NPCID.CataractEye,
NPCID.SleepyEye,
NPCID.DialatedEye,
NPCID.GreenEye,
NPCID.PurpleEye,
NPCID.DemonEyeOwl,
NPCID.DemonEyeSpaceship,
ModContent.NPCType<BlightedEye>()
};
public static List<int> Pigrons => new List<int>()
{
NPCID.PigronCorruption,
NPCID.PigronCrimson,
NPCID.PigronHallow
};
public const int FadeThroughWallsDelay = 300;
public static void DemonEyeBatMovement(NPC npc, float maxXSpeed = 6f, float maxYSpeed = 3.5f,
float xAccel = 0.1f, float xAccelBoost1 = 0.06f, float xAccelBoost2 = 0.25f,
float yAccel = 0.12f, float yAccelBoost1 = 0.07f, float yAccelBoost2 = 0.2f)
{
if (npc.direction == -1 && npc.velocity.X > -maxXSpeed)
{
npc.velocity.X -= xAccel;
if (npc.velocity.X > maxXSpeed)
{
npc.velocity.X -= xAccelBoost1;
}
else if (npc.velocity.X > 0f)
{
npc.velocity.X -= xAccelBoost2;
}
if (npc.velocity.X < -maxXSpeed)
{
npc.velocity.X = -maxXSpeed;
}
}
else if (npc.direction == 1 && npc.velocity.X < maxXSpeed)
{
npc.velocity.X += xAccel;
if (npc.velocity.X < -maxXSpeed)
{
npc.velocity.X += xAccelBoost1;
}
else if (npc.velocity.X < 0f)
{
npc.velocity.X += xAccelBoost2;
}
if (npc.velocity.X > maxXSpeed)
{
npc.velocity.X = maxXSpeed;
}
}
if (npc.directionY == -1 && npc.velocity.Y > -maxYSpeed)
{
npc.velocity.Y -= yAccel;
if (npc.velocity.Y > maxYSpeed)
{
npc.velocity.Y -= yAccelBoost1;
}
else if (npc.velocity.Y > 0f)
{
npc.velocity.Y -= yAccelBoost2;
}
if (npc.velocity.Y < -maxYSpeed)
{
npc.velocity.Y = -maxYSpeed;
}
}
else if (npc.directionY == 1 && npc.velocity.Y < maxYSpeed)
{
npc.velocity.Y += yAccel;
if (npc.velocity.Y < -maxYSpeed)
{
npc.velocity.Y += yAccelBoost1;
}
else if (npc.velocity.Y < 0f)
{
npc.velocity.Y += yAccelBoost2;
}
if (npc.velocity.Y > maxYSpeed)
{
npc.velocity.Y = maxYSpeed;
}
}
}
public static bool BuffedDemonEyeAI(NPC npc, Mod mod)
{
// Randomly play pigron noises if the NPC is a pigron.
if (Pigrons.Contains(npc.type) && Main.rand.NextBool(1000))
Main.PlaySound(SoundID.Zombie, (int)npc.position.X, (int)npc.position.Y, 9, 1f, 0f);
// Disable gravity.
npc.noGravity = true;
// Handle tile collision rebounds if applicable.
if (!npc.noTileCollide)
{
// Bounce off of tiles on the X axis.
if (npc.collideX)
{
npc.velocity.X = npc.oldVelocity.X * -0.5f;
if (npc.direction == -1 && npc.velocity.X > 0f && npc.velocity.X < 2f)
npc.velocity.X = 2f;
if (npc.direction == 1 && npc.velocity.X < 0f && npc.velocity.X > -2f)
{
npc.velocity.X = -2f;
}
}
// Bounce off of tiles on the Y axis.
if (npc.collideY)
{
npc.velocity.Y = npc.oldVelocity.Y * -0.5f;
if (npc.velocity.Y > 0f && npc.velocity.Y < 1f)
npc.velocity.Y = 1f;
if (npc.velocity.Y < 0f && npc.velocity.Y > -1f)
npc.velocity.Y = -1f;
}
}
// If the NPC is supposed to only appear during the night, disappear if above-ground and it's daytime.
if (Main.dayTime && npc.position.Y <= Main.worldSurface * 16.0 && NightTimeEnemies.Contains(npc.type))
{
if (npc.timeLeft > 10)
npc.timeLeft = 10;
// Adjust directions
npc.direction = (npc.velocity.X > 0f).ToDirectionInt();
npc.directionY = (npc.velocity.X > 0f).ToDirectionInt();
}
// Otherwise constantly search for the closest target.
else
npc.TargetClosest();
Player target = Main.player[npc.target];
ref float fadeThroughWallsTimer = ref npc.ai[0];
ref float fadeThroughWallsFlag = ref npc.ai[1];
// Fade away and go through walls if the NPC is a pigron or Calamity eye.
if (Pigrons.Contains(npc.type) || npc.type == ModContent.NPCType<CalamityEye>() || npc.type == ModContent.NPCType<BlightedEye>())
{
// Stop going through walls if the target can be reached.
if (Collision.CanHit(npc.position, npc.width, npc.height, target.position, target.width, target.height))
{
if (fadeThroughWallsFlag != 0f && !Collision.SolidCollision(npc.position, npc.width, npc.height))
{
fadeThroughWallsFlag = 0f;
fadeThroughWallsTimer = 0f;
npc.netUpdate = true;
}
}
else if (fadeThroughWallsFlag == 0f)
fadeThroughWallsTimer++;
if (fadeThroughWallsTimer >= FadeThroughWallsDelay)
{
fadeThroughWallsFlag = 1f;
fadeThroughWallsTimer = 0f;
npc.netUpdate = true;
}
if (fadeThroughWallsFlag == 0f)
{
npc.alpha = 0;
npc.noTileCollide = false;
}
else
{
npc.wet = false;
npc.alpha = 200;
npc.noTileCollide = true;
}
npc.rotation = npc.velocity.Y * 0.1f * npc.direction;
npc.TargetClosest(true);
DemonEyeBatMovement(npc);
}
else if (npc.type == NPCID.TheHungryII)
{
npc.TargetClosest(true);
// Emit light for some reason.
Lighting.AddLight((int)npc.Center.X / 16, (int)npc.Center.Y / 16, 0.3f, 0.2f, 0.1f);
DemonEyeBatMovement(npc, 8f, 3.5f, 0.12f, 0.12f, 0.25f, 0.06f, 0.07f, 0.2f);
if (Main.rand.NextBool(40))
{
Vector2 dustSpawnTopLeft = new Vector2(npc.position.X, npc.position.Y + npc.height * 0.25f);
Dust blood = Dust.NewDustDirect(dustSpawnTopLeft, npc.width, npc.height / 2, DustID.Blood, npc.velocity.X, 2f, 0, default, 1f);
blood.velocity.X *= 0.5f;
blood.velocity.Y *= 0.1f;
}
}
else if (npc.type == NPCID.WanderingEye)
{
// Move faster when close to dying.
if (npc.life < npc.lifeMax * 0.5)
DemonEyeBatMovement(npc, 8f, 6f, 0.12f, 0.12f, 0.07f, 0.12f, 0.12f, 0.07f);
else
DemonEyeBatMovement(npc, 6f, 2.5f, 0.12f, 0.12f, 0.07f, 0.06f, 0.07f, 0.05f);
}
else
{
float maxSpeedX = 6f;
float maxSpeedY = 2.5f;
maxSpeedX *= 1f + (1f - npc.scale);
maxSpeedY *= 1f + (1f - npc.scale);
DemonEyeBatMovement(npc, maxSpeedX, maxSpeedY, 0.08f, 0.08f, 0.03f, 0.02f, 0.03f, 0.015f);
}
// Make actual eyes emit blood randomly
if (npc.type == NPCID.DemonEye ||
npc.type == NPCID.WanderingEye ||
npc.type == ModContent.NPCType<CalamityEye>() ||
npc.type == ModContent.NPCType<BlightedEye>() ||
(npc.type >= NPCID.CataractEye && npc.type <= NPCID.PurpleEye))
{
if (Main.rand.NextBool(40))
{
Vector2 dustSpawnTopLeft = new Vector2(npc.position.X, npc.position.Y + npc.height * 0.25f);
Dust blood = Dust.NewDustDirect(dustSpawnTopLeft, npc.width, npc.height / 2, DustID.Blood, npc.velocity.X, 2f, 0, default, 1f);
blood.velocity.X *= 0.5f;
blood.velocity.Y *= 0.1f;
}
}
// Avoid entering water. This does not apply to pigrons.
if (npc.wet && !Pigrons.Contains(npc.type))
{
if (npc.velocity.Y > 0f)
npc.velocity.Y *= 0.95f;
npc.velocity.Y -= 0.7f;
if (npc.velocity.Y < -6f)
npc.velocity.Y = -6f;
npc.TargetClosest(true);
}
return false;
}
}
}
@@ -7,7 +7,7 @@ using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses
namespace CalamityMod.NPCs.VanillaNPCOverrides.RegularEnemies
{
public static class SlimeAI
{