From 7fe67be7566d992b0fceb7380d5f5294ca8aa207 Mon Sep 17 00:00:00 2001 From: Nathanael Overton Date: Wed, 29 Apr 2026 19:15:09 -0400 Subject: [PATCH] Add dungeon tp logic --- CalPlayer/CalamityPlayer.cs | 28 +++++++++++++++++++++++ Items/Potions/TheElixir.cs | 22 ++++++++++++------ Systems/World/WorldgenManagementSystem.cs | 19 +++++++++++++++ World/DungeonArchive.cs | 3 +++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/CalPlayer/CalamityPlayer.cs b/CalPlayer/CalamityPlayer.cs index dc5c607f5e..09d6b06b2c 100644 --- a/CalPlayer/CalamityPlayer.cs +++ b/CalPlayer/CalamityPlayer.cs @@ -4325,6 +4325,34 @@ namespace CalamityMod.CalPlayer return null; } + public static Vector2? GetDungeonArchivePosition(Player player) + { + if (WorldgenManagementSystem.DungeonArchivePos == Point.Zero) + return null; + + bool canSpawn = false; + + // Define an area around the center to search for a valid spot to tp + int rangeX = 10; + int rangeY = 30; + int teleportStartX = WorldgenManagementSystem.DungeonArchivePos.X - rangeX; + int teleportStartY = WorldgenManagementSystem.DungeonArchivePos.Y; + + Player.RandomTeleportationAttemptSettings settings = new Player.RandomTeleportationAttemptSettings + { + mostlySolidFloor = true, + avoidAnyLiquid = true, + avoidLava = true, + avoidHurtTiles = true, + avoidWalls = false, + attemptsBeforeGivingUp = 1000, + maximumFallDistanceFromOrignalPoint = 30 + }; + + Vector2 vector = player.CheckForGoodTeleportationSpot(ref canSpawn, teleportStartX, rangeX, teleportStartY, rangeY, settings); + + return canSpawn ? vector : null; + } public static void ModTeleport(Player player, Vector2 pos, bool playSound = true, int style = TeleportationStyleID.RecallPotion) { diff --git a/Items/Potions/TheElixir.cs b/Items/Potions/TheElixir.cs index 5024c09e1a..52306d855f 100644 --- a/Items/Potions/TheElixir.cs +++ b/Items/Potions/TheElixir.cs @@ -1,4 +1,5 @@ using CalamityMod.CalPlayer; +using CalamityMod.Systems; using Microsoft.Xna.Framework; using Terraria; using Terraria.Audio; @@ -41,20 +42,27 @@ namespace CalamityMod.Items.Potions { if (player.whoAmI == Main.myPlayer) { - if (Main.rand.NextBool(2)) + if (Main.rand.NextBool(1)) { - //If it fails, inflict the player with 5 seconds of Chaos State player.AddBuff(BuffID.ChaosState, 300, true); - Vector2? abyss = CalamityPlayer.GetAbyssPosition(player); - if (!abyss.HasValue) + + Vector2? location; + + bool archiveStoredPositionExists = WorldgenManagementSystem.DungeonArchivePos != Point.Zero; + if (archiveStoredPositionExists && Main.rand.NextBool()) + location = CalamityPlayer.GetDungeonArchivePosition(player); + else + location = CalamityPlayer.GetAbyssPosition(player); + + if (!location.HasValue) return false; - CalamityPlayer.ModTeleport(player, abyss.Value, false, TeleportationStyleID.RodOfDiscord); + + CalamityPlayer.ModTeleport(player, location.Value, false, TeleportationStyleID.RecallPotion); } //If it doesn't fail, just act like a Potion of Return else - { player.DoPotionOfReturnTeleportationAndSetTheComebackPoint(); - } + //Dust and Teleport sounds happen regardless of if it fails or not Rectangle rect = player.getRect(); int dustAmt = rect.Width * rect.Height / 5; diff --git a/Systems/World/WorldgenManagementSystem.cs b/Systems/World/WorldgenManagementSystem.cs index 5399d037a4..0ccd7c76dc 100644 --- a/Systems/World/WorldgenManagementSystem.cs +++ b/Systems/World/WorldgenManagementSystem.cs @@ -12,6 +12,7 @@ using Terraria.GameContent.Generation; using Terraria.ID; using Terraria.Localization; using Terraria.ModLoader; +using Terraria.ModLoader.IO; using Terraria.WorldBuilding; using static CalamityMod.World.CalamityWorld; @@ -19,10 +20,28 @@ namespace CalamityMod.Systems { public class WorldgenManagementSystem : ModSystem { + #region StructurePosStoring + public static Point DungeonArchivePos = Point.Zero; + + public override void SaveWorldData(TagCompound tag) + { + tag["DungeonArchivePos"] = DungeonArchivePos; + } + + public override void LoadWorldData(TagCompound tag) + { + if (tag.ContainsKey("DungeonArchivePos")) + DungeonArchivePos = tag.Get("DungeonArchivePos"); + else + DungeonArchivePos = Point.Zero; + } + #endregion + #region PreWorldGen public override void PreWorldGen() { Abyss.TotalPlacedIslandsSoFar = 0; + DungeonArchivePos = Point.Zero; //roxShrinePlaced = false; // This will only be applied at world-gen time to new worlds. diff --git a/World/DungeonArchive.cs b/World/DungeonArchive.cs index a1958e2331..59fd429795 100644 --- a/World/DungeonArchive.cs +++ b/World/DungeonArchive.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using CalamityMod.Schematics; +using CalamityMod.Systems; using Microsoft.Xna.Framework; using Terraria; using Terraria.ID; @@ -72,6 +73,8 @@ namespace CalamityMod.World if (placedArchive) { + WorldgenManagementSystem.DungeonArchivePos = new Point(i, j); + bool firstItem = false; if (dungeonArchiveColor == 0)