From 72fc355b652d2e39bad20ac05ecb7ea81f1add24 Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Thu, 14 Apr 2022 13:42:17 -0400 Subject: [PATCH] Rely on in-world tile immediately after meta tile assignment --- Schematics/SchematicManager.cs | 55 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/Schematics/SchematicManager.cs b/Schematics/SchematicManager.cs index e7e8f583e3..463beb6466 100644 --- a/Schematics/SchematicManager.cs +++ b/Schematics/SchematicManager.cs @@ -190,28 +190,39 @@ namespace CalamityMod.Schematics for (int y = 0; y < height; ++y) { SchematicMetaTile smt = schematic[x, y]; - string modChestStr = TileLoader.GetTile(smt.TileType)?.ContainerName.GetDefault() ?? ""; - bool isChest = smt.TileType == TileID.Containers || modChestStr != ""; + smt.ApplyTo(x + cornerX, y + cornerY, originalTiles[x, y]); + Tile worldTile = Main.tile[x + cornerX, y + cornerY]; + + // FRAME DATA SEEMS CORRUPTED. Not sure what's going on. + // NOTE + // UNCOMMENTING THIS LINE MAKES THE GAME CRASH WITH NO ERROR IN LOG + // Framing.SelfFrame8Way(x + cornerX, y + cornerY, worldTile, true); // If the determined tile type is a chest and this is its top left corner, define it appropriately. - if (isChest && smt.miscState.TileFrameX % 36 == 0 && smt.miscState.TileFrameY == 0) + // Skip this step if this schematic position preserves tiles. + bool isChest = worldTile.TileType == TileID.Containers || TileID.Sets.BasicChest[worldTile.TileType]; + if (!smt.keepTile && isChest && worldTile.TileFrameX % 36 == 0 && worldTile.TileFrameY == 0) { - Chest chest = PlaceChest(x + cornerX, y + cornerY, smt.TileType); - // Use the appropriate chest delegate function to fill the chest. - if (chestDelegate is Action) + // If a chest already exists "near" this position, then the corner was likely already defined. + // Do not do anything if a chest was already defined. + // FindChestByGuessing checks a 2x2 space starting in the given position, so nudge it up and left by 1. + int chestIndex = Chest.FindChestByGuessing(x + cornerX - 1, y + cornerY - 1); + if (chestIndex == -1) { - (chestDelegate as Action)?.Invoke(chest, smt.TileType, specialCondition); - specialCondition = true; + chestIndex = Chest.CreateChest(x + cornerX, y + cornerY, -1); + Chest chest = Main.chest[chestIndex]; + // Use the appropriate chest delegate function to fill the chest. + if (chestDelegate is Action) + { + (chestDelegate as Action)?.Invoke(chest, worldTile.TileType, specialCondition); + specialCondition = true; + } + else if (chestDelegate is Action) + (chestDelegate as Action)?.Invoke(chest); } - else if (chestDelegate is Action) - (chestDelegate as Action)?.Invoke(chest); } - // This is where the meta tile keep booleans are applied. - smt.ApplyTo(x + cornerX, y + cornerY, originalTiles[x, y]); - // Now that the tile data is correctly set, place appropriate tile entities. - Tile worldTile = Main.tile[x + cornerX, y + cornerY]; TryToPlaceTileEntities(x + cornerX, y + cornerY, worldTile); // Activate the pile placement function if defined. @@ -242,22 +253,6 @@ namespace CalamityMod.Schematics else if (tileType == ModContent.TileType()) TileEntity.PlaceEntityNet(x, y, ModContent.TileEntityType()); } - - private static Chest PlaceChest(int x, int y, int chestType) - { - int chestIndex = Chest.FindEmptyChest(x, y, chestType); - Main.chest[chestIndex] = new Chest() - { - x = x, - y = y - }; - Main.chest[chestIndex].item = new Item[Main.chest[chestIndex].item.Length]; - for (int i = 0; i < Main.chest[chestIndex].item.Length; i++) - { - Main.chest[chestIndex].item[i] = new Item(); - } - return Main.chest[chestIndex]; - } #endregion } }