From 3d15aaa89cc0fbbcd71847c3dba40100d4a67cdc Mon Sep 17 00:00:00 2001 From: Memes <114233643+Mr-Memes@users.noreply.github.com> Date: Mon, 3 Mar 2025 01:21:36 +0100 Subject: [PATCH] NYCRO HELP ME BRO (Fixed Compiling) --- NPCs/SunkenSea/Probesnout.cs | 38 ++++++++++++--------- NPCs/SunkenSea/Sharkoon.cs | 62 ++++++++++++++--------------------- Utilities/PathfindingUtils.cs | 16 +++++++-- 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/NPCs/SunkenSea/Probesnout.cs b/NPCs/SunkenSea/Probesnout.cs index 90f0d198b1..11679f0c83 100644 --- a/NPCs/SunkenSea/Probesnout.cs +++ b/NPCs/SunkenSea/Probesnout.cs @@ -18,7 +18,7 @@ using static Terraria.ModLoader.ModContent; namespace CalamityMod.NPCs.SunkenSea { - public class Probesnout : SunkenSeaNPC, IPathFinder + public class Probesnout : SunkenSeaNPC { #region Members @@ -94,7 +94,11 @@ namespace CalamityMod.NPCs.SunkenSea private Action _previousBehavior; - public PathfindingTask Path { get; set; } + private int HuntCooldown + { + get => (int)NPC.ai[3]; + set => NPC.ai[3] = value; + } public Vector2 Position => NPC.Center; @@ -128,6 +132,12 @@ namespace CalamityMod.NPCs.SunkenSea NPC.spriteDirection = Main.rand.NextBool().ToDirectionInt(); NPC.GravityMultiplier *= 2f; NPC.MaxFallSpeedMultiplier *= 2f; + pathfinding = new PathfindingManager(NPC) + { + Acceleration = 0.4f, + MaxSpeed = 8f, + MinimalPointDistance = 60f, + }; } public override void AI() @@ -181,14 +191,7 @@ namespace CalamityMod.NPCs.SunkenSea private void IdlingBehavior() { // At random, the mob will choose a random nearby point and pathfind there. - PathfindingTask parameters = null; - if (Main.rand.NextBool(150)) - { - _randomPathPoint = NPC.Center + Main.rand.NextVector2Unit() * 300f; - NPC.netUpdate = true; - parameters = new PathfindingTask(NPC.Center, _randomPathPoint, SunkenSeaTileValidity); - } - this.DoPathfinding(parameters); + pathfinding.DoPathfinding(new(NPC.Center, NPC.Center + Main.rand.NextVector2Unit() * Main.rand.NextFloat(500f, 3000f), SunkenSeaTileValidity)); } private void FleeingBehavior() @@ -216,12 +219,11 @@ namespace CalamityMod.NPCs.SunkenSea } while (!Main.tile[(NPC.Center + _randomPathPoint).ToTileCoordinates()].IsTileSolid()); NPC.netUpdate = true; - this.DoPathfinding(new PathfindingTask(NPC.Center, NPC.Center + _randomPathPoint, SunkenSeaTileValidity)); + pathfinding.DoPathfinding(new(NPC.Center, NPC.Center + _randomPathPoint, SunkenSeaTileValidity)); } else { NPC.velocity += NPC.DirectionFrom(CurrentPredator.Center) * Acceleration; - Path = null; // Cap the speed if MaxSpeed has been surpassed. if (NPC.velocity.LengthSquared() > MaxSpeed * MaxSpeed) @@ -238,8 +240,14 @@ namespace CalamityMod.NPCs.SunkenSea return; } + bool huntReady = HuntCooldown == 0; + if (huntReady) + HuntCooldown = Main.rand.Next(13, 30); + // With sight, just go straight at him. Without it, try to pathfind over them. - this.DoPathfinding(new PathfindingTask(NPC.Center, CurrentPrey.Center, SunkenSeaTileValidity), continuouslyUpdatePath: true); + pathfinding.DoPathfinding(new(NPC.Center, CurrentPrey.Center, SunkenSeaTileValidity), forceNewTask: huntReady); + + HuntCooldown--; } private void EatingBehavior() @@ -257,7 +265,7 @@ namespace CalamityMod.NPCs.SunkenSea } } else - this.DoPathfinding(new PathfindingTask(NPC.Center, SpongeFoundPosition, SunkenSeaTileValidity)); + pathfinding.DoPathfinding(new(NPC.Center, SpongeFoundPosition, SunkenSeaTileValidity)); } private void OutsideWaterBehavior() @@ -386,7 +394,7 @@ namespace CalamityMod.NPCs.SunkenSea { base.SetDefaults(); - // BannerItem = ItemType(); + BannerItem = ItemType(); NPC.lifeMax = 5; diff --git a/NPCs/SunkenSea/Sharkoon.cs b/NPCs/SunkenSea/Sharkoon.cs index 6a7045e509..4444fdfcb0 100644 --- a/NPCs/SunkenSea/Sharkoon.cs +++ b/NPCs/SunkenSea/Sharkoon.cs @@ -18,7 +18,7 @@ using static Terraria.ModLoader.ModContent; namespace CalamityMod.NPCs.SunkenSea { - public class Sharkoon : SunkenSeaNPC, IPathFinder + public class Sharkoon : SunkenSeaNPC { #region Fields & Properties @@ -133,6 +133,12 @@ namespace CalamityMod.NPCs.SunkenSea private Action _previousBehavior; + private int HuntCooldown + { + get => (int)NPC.ai[3]; + set => NPC.ai[3] = value; + } + #region SunkenSeaNPC Implementation protected override List PreyIDs => @@ -157,20 +163,6 @@ namespace CalamityMod.NPCs.SunkenSea #endregion - #region IPathFinder Implementation - - public PathfindingTask Path { get; set; } - - public Vector2 Position => NPC.Center; - - public Vector2 Velocity { get => NPC.velocity; set => NPC.velocity = value; } - - public float Acceleration { get; set; } = 0.25f; - - public float MaxSpeed { get; set; } = 6f; - - #endregion - #endregion #region SetDefaults @@ -227,6 +219,11 @@ namespace CalamityMod.NPCs.SunkenSea NPC.spriteDirection = Main.rand.NextBool().ToDirectionInt(); NPC.GravityMultiplier *= 2f; NPC.MaxFallSpeedMultiplier *= 2f; + pathfinding = new PathfindingManager(NPC) + { + Acceleration = 0.25f, + MaxSpeed = 6f, + }; } public override void AI() @@ -261,14 +258,7 @@ namespace CalamityMod.NPCs.SunkenSea private void IdlingBehavior() { // At random, the mob will choose a random nearby point and pathfind there. - PathfindingTask task = null; - if (Main.rand.NextBool(IdleRandomMovementUnlikeliness)) - { - _randomPathPoint = NPC.Center + Main.rand.NextVector2Unit() * Main.rand.NextFloat(IdleMinPathDistance, IdleMaxPathDistance); - NPC.netUpdate = true; - task = new PathfindingTask(NPC.Center, _randomPathPoint, SunkenSeaTileValidity); - } - this.DoPathfinding(task); + pathfinding.DoPathfinding(new(NPC.Center, NPC.Center + Main.rand.NextVector2Unit() * 2000f, SunkenSeaTileValidity)); } private void HuntingBehavior() @@ -280,15 +270,14 @@ namespace CalamityMod.NPCs.SunkenSea return; } + bool huntReady = HuntCooldown == 0; + if (huntReady) + HuntCooldown = Main.rand.Next(13, 30); + // With sight, just go straight at him. Without it, try to pathfind over them. - if (!NPC.HasSight(CurrentPrey.Center)) - this.DoPathfinding(new PathfindingTask(NPC.Center, CurrentPrey.Center, SunkenSeaTileValidity)); - else - { - NPC.velocity += NPC.DirectionTo(CurrentPrey.Center) * Acceleration; - if (NPC.velocity.LengthSquared() > MaxSpeed * MaxSpeed) - NPC.velocity = Vector2.Normalize(NPC.velocity) * MaxSpeed; - } + pathfinding.DoPathfinding(new(NPC.Center, CurrentPrey.Center, SunkenSeaTileValidity), forceNewTask: huntReady); + + HuntCooldown--; } private void FleeingBehavior() @@ -307,18 +296,19 @@ namespace CalamityMod.NPCs.SunkenSea // Try to manuever if there are any obstacles. if (!Main.tile[(NPC.Center + NPC.DirectionFrom(_avoidedEntity.Center) * FleeTileAnticipationDistance).ToTileCoordinates()].IsTileSolid()) { - NPC.velocity += NPC.DirectionFrom(_avoidedEntity.Center) * Acceleration; + NPC.velocity += NPC.DirectionFrom(_avoidedEntity.Center) * pathfinding.Acceleration; + pathfinding.ClearResults(); // Cap the speed if MaxSpeed has been surpassed. - if (NPC.velocity.LengthSquared() > MaxSpeed * MaxSpeed) - NPC.velocity = Vector2.Normalize(NPC.velocity) * MaxSpeed; + if (NPC.velocity.LengthSquared() > pathfinding.MaxSpeed * pathfinding.MaxSpeed) + NPC.velocity = Vector2.Normalize(NPC.velocity) * pathfinding.MaxSpeed; } else { float distanceFromAvoided = Vector2.Distance(NPC.Center, _avoidedEntity.Center); _randomPathPoint = NPC.Center + Main.rand.NextVector2Unit() * Utils.Remap(distanceFromAvoided, 0f, 960f, 80f, 3200f); NPC.netUpdate = true; - this.DoPathfinding(new PathfindingTask(NPC.Center, _randomPathPoint, SunkenSeaTileValidity)); + pathfinding.DoPathfinding(new(NPC.Center, _randomPathPoint, SunkenSeaTileValidity)); } // If it's capable of exploding and the predator's within distance, kaboom. @@ -397,8 +387,6 @@ namespace CalamityMod.NPCs.SunkenSea // When it gets outside of water, it'll try to gravitate downards towards the water. if (newBehavior == OutsideWaterBehavior) NPC.noGravity = false; - - Path = null; } protected override void OnPreyDetection(NPC prey) diff --git a/Utilities/PathfindingUtils.cs b/Utilities/PathfindingUtils.cs index b2b96a843b..9c2329f2e8 100644 --- a/Utilities/PathfindingUtils.cs +++ b/Utilities/PathfindingUtils.cs @@ -35,6 +35,8 @@ namespace CalamityMod #region Pathfinding Manager public class PathfindingManager { + public List Path { get => lastSuccessfulTask?.Result ?? []; } + /// /// The entity which this PathfindingManager manages. This cannot be changed after creation. /// @@ -44,13 +46,15 @@ namespace CalamityMod /// The acceleration this PathfindingManager will impart to its Entity when making it follow a found path.
/// This has no impact on the Entity's other behaviors, AI, etc. /// - internal float Acceleration { get; set; } + internal float Acceleration { get; set; } = 0.2f; /// /// The maximum speed this PathfindingManager allow its Entity to move at when making it follow a found path.
/// This has no impact on the Entity's other behaviors, AI, etc. ///
- internal float MaxSpeed { get; set; } + internal float MaxSpeed { get; set; } = 4f; + + internal float MinimalPointDistance { get; set; } = 48f; /// /// The last completed pathfinding task that this manager has performed.
@@ -183,7 +187,7 @@ namespace CalamityMod entity.velocity = entity.velocity.SafeNormalize(Vector2.UnitY) * MaxSpeed; // If the entity is within 48 pixels of its target point, consider the point reached. - if (Vector2.DistanceSquared(entity.Center, nextPoint) < 48f * 48f) + if (Vector2.DistanceSquared(entity.Center, nextPoint) < MinimalPointDistance * MinimalPointDistance) return true; // Otherwise, continue following. @@ -218,6 +222,12 @@ namespace CalamityMod DefaultIdleBehavior(); } + public void ClearResults() + { + currentTask = null; + lastSuccessfulTask = null; + } + #region Pathfinding Task /// /// Represents a task for calculating a complex path.