diff --git a/Schematics/LargeScrapPile1.csch b/Schematics/LargeScrapPile1.csch new file mode 100644 index 0000000000..71acadddb1 Binary files /dev/null and b/Schematics/LargeScrapPile1.csch differ diff --git a/Schematics/LargeScrapPile2.csch b/Schematics/LargeScrapPile2.csch new file mode 100644 index 0000000000..415cbbcfc6 Binary files /dev/null and b/Schematics/LargeScrapPile2.csch differ diff --git a/Schematics/LargeScrapPile3.csch b/Schematics/LargeScrapPile3.csch new file mode 100644 index 0000000000..3faf1e1d48 Binary files /dev/null and b/Schematics/LargeScrapPile3.csch differ diff --git a/Schematics/LargeScrapPile4.csch b/Schematics/LargeScrapPile4.csch new file mode 100644 index 0000000000..d444a71416 Binary files /dev/null and b/Schematics/LargeScrapPile4.csch differ diff --git a/Schematics/SchematicManager.cs b/Schematics/SchematicManager.cs index 0dda76c2b1..031a94261e 100644 --- a/Schematics/SchematicManager.cs +++ b/Schematics/SchematicManager.cs @@ -33,6 +33,16 @@ namespace CalamityMod.Schematics // Astral world gen structures ["Astral Beacon"] = CalamitySchematicIO.LoadSchematic("Schematics/AstralBeacon.csch"), + + // Sulphurous Sea scrap world gen structures + ["Sulphurous Scrap 1"] = CalamitySchematicIO.LoadSchematic("Schematics/SmallScrapPile1.csch").ShaveOffEdge(), + ["Sulphurous Scrap 2"] = CalamitySchematicIO.LoadSchematic("Schematics/SmallScrapPile2.csch").ShaveOffEdge(), + ["Sulphurous Scrap 3"] = CalamitySchematicIO.LoadSchematic("Schematics/SmallScrapPile3.csch").ShaveOffEdge(), + ["Sulphurous Scrap 4"] = CalamitySchematicIO.LoadSchematic("Schematics/SmallScrapPile4.csch").ShaveOffEdge(), + ["Large Sulphurous Scrap 1"] = CalamitySchematicIO.LoadSchematic("Schematics/LargeScrapPile1.csch").ShaveOffEdge(), + ["Large Sulphurous Scrap 2"] = CalamitySchematicIO.LoadSchematic("Schematics/LargeScrapPile2.csch").ShaveOffEdge(), + ["Large Sulphurous Scrap 3"] = CalamitySchematicIO.LoadSchematic("Schematics/LargeScrapPile3.csch").ShaveOffEdge(), + ["Large Sulphurous Scrap 4"] = CalamitySchematicIO.LoadSchematic("Schematics/LargeScrapPile4.csch").ShaveOffEdge(), }; } internal static void Unload() @@ -41,7 +51,18 @@ namespace CalamityMod.Schematics PilePlacementMaps = null; } #endregion + + #region Get Schematic Area + public static Vector2? GetSchematicArea(string name) + { + // If no schematic exists with this name, simply return null. + if (!TileMaps.TryGetValue(name, out SchematicMetaTile[,] schematic)) + return null; + return new Vector2(schematic.GetLength(0), schematic.GetLength(1)); + } + #endregion Get Schematic Area + #region Place Schematic public static void PlaceSchematic(string name, Point pos, SchematicAnchor anchorType, ref bool specialCondition, T chestDelegate = null) where T : Delegate { diff --git a/Schematics/SmallScrapPile1.csch b/Schematics/SmallScrapPile1.csch new file mode 100644 index 0000000000..71adf27c17 Binary files /dev/null and b/Schematics/SmallScrapPile1.csch differ diff --git a/Schematics/SmallScrapPile2.csch b/Schematics/SmallScrapPile2.csch new file mode 100644 index 0000000000..51fc4a598e Binary files /dev/null and b/Schematics/SmallScrapPile2.csch differ diff --git a/Schematics/SmallScrapPile3.csch b/Schematics/SmallScrapPile3.csch new file mode 100644 index 0000000000..6d291fcf13 Binary files /dev/null and b/Schematics/SmallScrapPile3.csch differ diff --git a/Schematics/SmallScrapPile4.csch b/Schematics/SmallScrapPile4.csch new file mode 100644 index 0000000000..0161115799 Binary files /dev/null and b/Schematics/SmallScrapPile4.csch differ diff --git a/Utilities/MiscUtils.cs b/Utilities/MiscUtils.cs index a3fd476ff2..31d3f21c91 100644 --- a/Utilities/MiscUtils.cs +++ b/Utilities/MiscUtils.cs @@ -44,6 +44,19 @@ namespace CalamityMod return array; } + public static T[,] ShaveOffEdge(this T[,] array) + { + T[,] result = new T[array.GetLength(0) - 2, array.GetLength(1) - 2]; + for (int i = 0; i < result.GetLength(0); i++) + { + for (int j = 0; j < result.GetLength(1); j++) + { + result[i, j] = array[i + 1, j + 1]; + } + } + return result; + } + /// /// Retrieves all the colors from a and returns them as a 2D array. /// diff --git a/World/SulphurousSea.cs b/World/SulphurousSea.cs index 9d1c719cc6..893443a221 100644 --- a/World/SulphurousSea.cs +++ b/World/SulphurousSea.cs @@ -1,11 +1,13 @@ using CalamityMod.Items; using CalamityMod.Items.Accessories; using CalamityMod.Items.Weapons.Summon; +using CalamityMod.Schematics; using CalamityMod.Tiles.Abyss; using CalamityMod.Walls; using Microsoft.Xna.Framework; using System; using System.Collections.Generic; +using System.Linq; using Terraria; using Terraria.ID; using Terraria.ModLoader; @@ -91,6 +93,7 @@ namespace CalamityMod.World PlaceColumns(); PlaceRustyChests(); CreateBeachNearSea(); + PlaceScrapPiles(); } #endregion @@ -837,6 +840,80 @@ namespace CalamityMod.World } #endregion + #region Scrap Piles + public static void PlaceScrapPiles() + { + List pastPlacementPostiion = new List(); + for (int i = 0; i < 3; i++) + { + int x = WorldGen.genRand.Next(75, BiomeWidth - 85); + if (!CalamityWorld.abyssSide) + x = Main.maxTilesX - x; + int y = WorldGen.genRand.Next(YStart + (int)(BlockDepth * 0.3f), YStart + (int)(BlockDepth * 0.8f)); + + Point pilePlacementPosition = new Point(x, y); + + // If the selected position is sitting inside of a tile, try again. + if (WorldGen.SolidTile(pilePlacementPosition.X, pilePlacementPosition.Y)) + { + i--; + continue; + } + + // If the selected position is close to other piles, try again. + if (pastPlacementPostiion.Any(p => Vector2.Distance(p, pilePlacementPosition.ToVector2()) < 85f)) + { + i--; + continue; + } + + // Otherwise, decide which pile should be created. + bool createLargePile = WorldGen.genRand.NextBool(3); + int pileVariant = WorldGen.genRand.Next(4); + string schematicName = $"{(createLargePile ? "Large " : string.Empty)}Sulphurous Scrap {pileVariant + 1}"; + Vector2? wrappedSchematicArea = SchematicManager.GetSchematicArea(schematicName); + + // Create a log message if for some reason the schematic in question doesn't exist. + if (!wrappedSchematicArea.HasValue) + { + CalamityMod.Instance.Logger.Warn($"Tried to place a schematic with name \"{schematicName}\". No matching schematic file found."); + continue; + } + + Vector2 schematicArea = wrappedSchematicArea.Value; + + // Decide the placement position by searching downward and looking for the lowest point. + // If the position is quite steep, try again. + Vector2 left = pilePlacementPosition.ToVector2() - Vector2.UnitX * schematicArea.X * 0.5f; + Vector2 right = pilePlacementPosition.ToVector2() + Vector2.UnitX * schematicArea.X * 0.5f; + while (!WorldGen.SolidTile(CalamityUtils.ParanoidTileRetrieval((int)left.X, (int)left.Y))) + left.Y++; + while (!WorldGen.SolidTile(CalamityUtils.ParanoidTileRetrieval((int)right.X, (int)right.Y))) + right.Y++; + + if (Math.Abs(left.Y - right.Y) >= 20f) + { + i--; + continue; + } + + // If the placement position ended up in the abyss, try again. + if (left.Y >= YStart + BlockDepth + 5 || right.Y >= YStart + BlockDepth + 5) + { + i--; + continue; + } + + // Pick the lowest point vertically. + Point bottomCenter = new Point(pilePlacementPosition.X, (int)Math.Max(left.Y, right.Y) + 6); + bool _ = false; + SchematicManager.PlaceSchematic>(schematicName, bottomCenter, SchematicAnchor.BottomCenter, ref _); + + pastPlacementPostiion.Add(bottomCenter.ToVector2()); + } + } + #endregion Scrap Piles + #region Misc Functions public static List YStartWhitelist = new List() {