From f6654fcb2a5243b5932b7de784b1925a79524523 Mon Sep 17 00:00:00 2001 From: GinYuH <72181395+GinYuH@users.noreply.github.com> Date: Thu, 3 Apr 2025 22:46:06 -0700 Subject: [PATCH] npc tile detection util --- NPCs/SunkenSea/SunkenSeaNPC.cs | 15 ++++++++-- Utilities/NPCUtils.cs | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/NPCs/SunkenSea/SunkenSeaNPC.cs b/NPCs/SunkenSea/SunkenSeaNPC.cs index f533fa2c92..e27e00a189 100644 --- a/NPCs/SunkenSea/SunkenSeaNPC.cs +++ b/NPCs/SunkenSea/SunkenSeaNPC.cs @@ -181,10 +181,21 @@ namespace CalamityMod.NPCs.SunkenSea /// The tile location to check. /// if the tile is valid; otherwise, . protected bool SunkenSeaTileValidity(Point point) + { + return SunkenSeaTileValidity(NPC, point); + } + + /// + /// Checks whether a specific tile is valid for an NPC, considering water level and entity size. + /// + /// The npc to use for the check. + /// The tile location to check. + /// if the tile is valid; otherwise, . + public static bool SunkenSeaTileValidity(NPC npc, Point point) { Point actualFuckingPoint = new Point(point.X * 16, point.Y * 16); - return NPC.Hitbox.Contains(actualFuckingPoint) - || !NPC.GetIntersectingHitboxPoints( + return npc.Hitbox.Contains(actualFuckingPoint) + || !npc.GetIntersectingHitboxPoints( actualFuckingPoint, 10, 10).Any(a => Main.tile[a].IsTileSolidGround() || Main.tile[a].LiquidAmount < 255 || Main.tile[a].LiquidType != LiquidID.Water); } } diff --git a/Utilities/NPCUtils.cs b/Utilities/NPCUtils.cs index d07d9ebb3d..253250bc55 100644 --- a/Utilities/NPCUtils.cs +++ b/Utilities/NPCUtils.cs @@ -12,6 +12,7 @@ using CalamityMod.NPCs.NormalNPCs; using CalamityMod.NPCs.OldDuke; using CalamityMod.NPCs.Providence; using CalamityMod.NPCs.SlimeGod; +using CalamityMod.NPCs.SunkenSea; using CalamityMod.NPCs.Yharon; using CalamityMod.Packets; using CalamityMod.Projectiles.Boss; @@ -966,6 +967,58 @@ namespace CalamityMod public static bool HasSight(this NPC npc, Vector2 target) => Collision.CanHit(npc.Center, 1, 1, target, 1, 1); + + /// + /// Attempts to find a tile of a given type within a given radius + /// + /// The NPC to center it on + /// The tile type to check for + /// The radius of the check area in pixels + /// Whether or not SunkenSeaTileValidity should be used + /// The position of the tile, or null if unsuccessful + public static Vector2? NPCTileDetection(NPC npc, int tileType, float radius, bool usesSunkenSeaValidity = false) + { + Vector2? tileFoundPosition = null; + int? tileIndexFound = null; + for (int i = 0; i < 360 && tileFoundPosition == null; i += 15) + { + var points = GetIntersectingPointsInLine(npc.Center, npc.Center - Vector2.UnitY.RotatedBy(MathHelper.ToRadians(i)) * radius); + for (int j = points.Count - 1; j >= 0; j--) + { + if (Main.tile[points[j]].TileType == tileType) + { + tileIndexFound = j; + break; + } + } + + if (tileIndexFound == null) + continue; + + for (int k = tileIndexFound.Value; k >= 0; k--) + { + Vector2 worldPos = points[k].ToWorldCoordinates(); + bool seaWaterCheck = true; + if (usesSunkenSeaValidity) + { + seaWaterCheck = SunkenSeaNPC.SunkenSeaTileValidity(npc, points[k]); + } + if (npc.HasSight(worldPos) && seaWaterCheck) + { + tileFoundPosition = worldPos; + break; + } + } + + tileIndexFound = null; + } + + if (tileFoundPosition.HasValue) + return tileFoundPosition.Value; + + return null; + } + #region Boss Spawning /// /// Shortcut for the generic boss summon message.