using System.Collections.Generic; using System.Linq; using CalamityMod.Tiles.Abyss; using CalamityMod.Tiles.Abyss.AbyssAmbient; using CalamityMod.Tiles.Astral; using CalamityMod.Tiles.AstralDesert; using CalamityMod.Tiles.AstralSnow; using CalamityMod.Tiles.Crags; using CalamityMod.Tiles.Ores; using CalamityMod.Tiles.SunkenSea; using CalamityMod.Tiles.SunkenSea.Ambient; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Systems { public class TileFramingSystem : ModSystem { // This enum is intentionally not visible to the remainder of the codebase. It is only used here. #region Similarity Enum private enum Similarity { Same, MergeLink, None } private static Similarity GetSimilarity(Tile check, int myType, int mergeType) { if (!check.HasTile) return Similarity.None; if (check.TileType == myType || Main.tileMerge[myType][check.TileType]) return Similarity.Same; else if (check.TileType == mergeType) return Similarity.MergeLink; return Similarity.None; } #endregion #region Merge Frame Data public sealed class MergeFrameData { public byte[,] AdjacencyData { get; } public int TileID { get; } public int FrameOffsetX { get; } public int FrameOffsetY { get; } public Asset BlendSheet { get; } public MergeFrameData(byte[,] adjacencyData, int tileId, string blendSheetPath, int frameOffsetX = 0, int frameOffsetY = 0) { AdjacencyData = adjacencyData; TileID = tileId; FrameOffsetX = frameOffsetX; FrameOffsetY = frameOffsetY; if (!cachedBlendSheets.TryGetValue(blendSheetPath, out var blendSheet)) { blendSheet = cachedBlendSheets[blendSheetPath] = ModContent.Request(blendSheetPath); } BlendSheet = blendSheet; } } #endregion // This tiny GlobalTile exists for two purposes: // 1 - Ensure merge data is loaded and cached // 2 - Properly and automatically render merges in all cases for all tiles #region Sealed Global Tile private sealed class MergeableTileGlobalTile : GlobalTile { public override void PostDraw(int i, int j, int type, SpriteBatch spriteBatch) { DrawUniversalMergeFrames(i, j, GetOrCreateTileAdjacencies(type)); } public override bool TileFrame(int i, int j, int type, ref bool resetFrame, ref bool noBreak) { // Custom plant framing for (int k = 0; k < PlantTypes.Length; k++) if (type == PlantTypes[k]) { PlantFrame(i, j); return false; } // Custom vine framing if (type == TileID.Vines || type == TileID.CrimsonVines || type == TileID.HallowedVines || type == ModContent.TileType()) VineFrame(i, j); foreach (var adjacency in GetOrCreateTileAdjacencies(type)) GetAdjacencyData(i, j, adjacency); return base.TileFrame(i, j, type, ref resetFrame, ref noBreak); } } #endregion // CONSIDER -- This is a triangle array, but does it need to be? Main.tileMerge is a triangle array as well public static bool[][] tileMergeTypes; private static Dictionary> tileAdjacencyMap = []; private static Dictionary> cachedBlendSheets = []; #region Plant Stuff private static ushort[] PlantTypes = [ TileID.Plants, TileID.CorruptPlants, TileID.JunglePlants, TileID.MushroomPlants, TileID.Plants2, TileID.JunglePlants2, TileID.HallowedPlants, TileID.HallowedPlants2, TileID.CrimsonPlants, (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), (ushort)ModContent.TileType(), ]; private static int[][] PlantValidGrounds; private static Dictionary VineToGrass; #endregion #region ModSystem Hooks (Load / Unload) // All tiles from all mods are guaranteed to be loaded by the execution time of this hook. public override void PostAddRecipes() { PlantValidGrounds = new int[TileLoader.TileCount][]; PlantValidGrounds[TileID.Plants] = new int[3] { TileID.Grass, TileID.PlanterBox, TileID.ClayPot }; PlantValidGrounds[TileID.CorruptPlants] = new int[1] { TileID.CorruptGrass }; PlantValidGrounds[TileID.JunglePlants] = new int[1] { TileID.JungleGrass }; PlantValidGrounds[TileID.MushroomPlants] = new int[1] { TileID.MushroomGrass }; PlantValidGrounds[TileID.Plants2] = new int[3] { TileID.Grass, TileID.PlanterBox, TileID.ClayPot }; PlantValidGrounds[TileID.JunglePlants2] = new int[1] { TileID.JungleGrass }; PlantValidGrounds[TileID.HallowedPlants] = new int[1] { TileID.HallowedGrass }; PlantValidGrounds[TileID.HallowedPlants2] = new int[1] { TileID.HallowedGrass }; PlantValidGrounds[TileID.CrimsonPlants] = new int[1] { TileID.CrimsonGrass }; PlantValidGrounds[ModContent.TileType()] = new int[1] { ModContent.TileType() }; PlantValidGrounds[ModContent.TileType()] = new int[1] { ModContent.TileType() }; PlantValidGrounds[ModContent.TileType()] = new int[1] { ModContent.TileType() }; PlantValidGrounds[ModContent.TileType()] = new int[1] { ModContent.TileType() }; PlantValidGrounds[ModContent.TileType()] = new int[1] { ModContent.TileType() }; PlantValidGrounds[ModContent.TileType()] = new int[1] { ModContent.TileType() }; PlantValidGrounds[ModContent.TileType()] = new int[2] { ModContent.TileType(), ModContent.TileType() }; PlantValidGrounds[ModContent.TileType()] = new int[1] { ModContent.TileType() }; VineToGrass = new Dictionary { [TileID.Vines] = TileID.Grass, [TileID.Vines] = TileID.LeafBlock, [TileID.CrimsonVines] = TileID.CrimsonGrass, [TileID.HallowedVines] = TileID.HallowedGrass, [(ushort)ModContent.TileType()] = (ushort)ModContent.TileType() }; tileMergeTypes = new bool[TileLoader.TileCount][]; for (var i = 0; i < tileMergeTypes.Length; ++i) tileMergeTypes[i] = new bool[TileLoader.TileCount]; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[TileID.Sandstone][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; tileMergeTypes[ModContent.TileType()][ModContent.TileType()] = true; } public override void Unload() { PlantValidGrounds = null; VineToGrass?.Clear(); VineToGrass = null; tileMergeTypes = null; tileAdjacencyMap?.Clear(); tileAdjacencyMap = null; cachedBlendSheets?.Clear(); cachedBlendSheets = null; } #endregion #region Tile Variation Helpers public static int GetVariation4x4_012_Low0(int i, int j) { int xRel = i & 0b0011; int yRel = j & 0b0011; var output = xRel switch { 0 => (yRel switch { 0 => 0, 1 => 2, 2 => 1, _ => 2 }), 1 => (yRel switch { 0 => 2, 1 => 0, 2 => 2, _ => 2 }), 2 => (yRel switch { 0 => 2, 1 => 0, 2 => 1, _ => 2 }), _ => (yRel switch { 0 => 1, 1 => 2, 2 => 0, _ => 2 }), }; return output; } public static int GetVariation4x4_01_Low0(int i, int j) { int xRel = i & 0b0011; int yRel = j & 0b0011; var output = xRel switch { 0 => (yRel switch { 0 => 0, 1 => 0, 2 => 1, _ => 1 }), 1 => (yRel switch { 0 => 1, 1 => 0, 2 => 1, _ => 1 }), 2 => (yRel switch { 0 => 1, 1 => 0, 2 => 0, _ => 1 }), _ => (yRel switch { 0 => 0, 1 => 1, 2 => 0, _ => 1 }), }; return output; } public static int GetVariation3x3_01234_Low3(int i, int j) { int xRel = i % 3; int yRel = j % 3; var output = xRel switch { 0 => (yRel switch { 0 => 0, 1 => 1, _ => 2 }), 1 => (yRel switch { 0 => 2, 1 => 3, _ => 4 }), _ => (yRel switch { 0 => 4, 1 => 0, _ => 1 }), }; return output; } #endregion #region Framing Helpers private static bool GetMerge(Tile myTile, Tile mergeTile) { if (!mergeTile.HasTile) return false; int myTileID = myTile.TileType; int otherTileID = mergeTile.TileType; return myTileID == otherTileID || Main.tileMerge[myTileID][otherTileID]; } private static bool GetBlendSpecific(Tile myTile, Tile mergeTile, int blendType, bool includeSame) { if (!mergeTile.HasTile) return false; int myTileID = myTile.TileType; int otherTileID = mergeTile.TileType; return otherTileID == blendType || includeSame && myTileID == otherTileID; } private static void GetAdjacentTiles(int x, int y, out bool up, out bool down, out bool left, out bool right, out bool upLeft, out bool upRight, out bool downLeft, out bool downRight) { // These all get null checked in the GetMerge function var tile = Main.tile[x, y]; var north = Main.tile[x, y - 1]; var south = Main.tile[x, y + 1]; var west = Main.tile[x - 1, y]; var east = Main.tile[x + 1, y]; var southwest = Main.tile[x - 1, y + 1]; var southeast = Main.tile[x + 1, y + 1]; var northwest = Main.tile[x - 1, y - 1]; var northeast = Main.tile[x + 1, y - 1]; left = false; right = false; up = false; down = false; upLeft = false; upRight = false; downLeft = false; downRight = false; if (GetMerge(tile, north) && (north.Slope == 0 || north.Slope == SlopeType.SlopeDownLeft || north.Slope == SlopeType.SlopeDownRight)) up = true; if (GetMerge(tile, south) && (south.Slope == 0 || south.Slope == SlopeType.SlopeUpLeft || south.Slope == SlopeType.SlopeUpRight)) down = true; if (GetMerge(tile, west) && (west.Slope == 0 || west.Slope == SlopeType.SlopeDownRight || west.Slope == SlopeType.SlopeUpRight)) left = true; if (GetMerge(tile, east) && (east.Slope == 0 || east.Slope == SlopeType.SlopeDownLeft || east.Slope == SlopeType.SlopeUpLeft)) right = true; if (GetMerge(tile, north) && GetMerge(tile, west) && GetMerge(tile, northwest) && (northwest.Slope == 0 || northwest.Slope == SlopeType.SlopeDownRight) && (north.Slope == 0 || north.Slope == SlopeType.SlopeDownLeft || north.Slope == SlopeType.SlopeUpLeft) && (west.Slope == 0 || west.Slope == SlopeType.SlopeUpLeft || west.Slope == SlopeType.SlopeUpRight)) upLeft = true; if (GetMerge(tile, north) && GetMerge(tile, east) && GetMerge(tile, northeast) && (northeast.Slope == 0 || northeast.Slope == SlopeType.SlopeDownLeft) && (north.Slope == 0 || north.Slope == SlopeType.SlopeDownRight || north.Slope == SlopeType.SlopeUpRight) && (east.Slope == 0 || east.Slope == SlopeType.SlopeUpLeft || east.Slope == SlopeType.SlopeUpRight)) upRight = true; if (GetMerge(tile, south) && GetMerge(tile, west) && GetMerge(tile, southwest) && !southwest.IsHalfBlock && (southwest.Slope == 0 || southwest.Slope == SlopeType.SlopeUpRight) && (south.Slope == 0 || south.Slope == SlopeType.SlopeDownLeft || south.Slope == SlopeType.SlopeUpLeft) && (west.Slope == 0 || west.Slope == SlopeType.SlopeDownLeft || west.Slope == SlopeType.SlopeDownRight)) downLeft = true; if (GetMerge(tile, south) && GetMerge(tile, east) && GetMerge(tile, southeast) && !southeast.IsHalfBlock && (southeast.Slope == 0 || southeast.Slope == SlopeType.SlopeUpLeft) && (south.Slope == 0 || south.Slope == SlopeType.SlopeDownRight || south.Slope == SlopeType.SlopeUpRight) && (east.Slope == 0 || east.Slope == SlopeType.SlopeDownLeft || east.Slope == SlopeType.SlopeDownRight)) downRight = true; } private static void SetFrameAt(int x, int y, int frameX, int frameY) { var tile = Main.tile[x, y]; if (tile != null) { tile.TileFrameX = (short)frameX; tile.TileFrameY = (short)frameY; } } #endregion #region Specific Framing Code internal static void PlantFrame(int x, int y) { if (x < 0 || x >= Main.maxTilesX) return; if (y < 0 || y >= Main.maxTilesY) return; // If the tile below is off the bottom of the map, then assume it's invalid placement var tile = Main.tile[x, y]; int plantType = tile.TileType; if (y + 1 >= Main.maxTilesY) { WorldGen.KillTile(x, y); return; } // If tile below is not elligible for growing plants, we kill the tile immediately var below = Main.tile[x, y + 1]; if (!below.HasTile || !below.HasUnactuatedTile || below.IsHalfBlock || below.Slope != SlopeType.Solid) { WorldGen.KillTile(x, y); return; } // Check if tile below is valid for given grass type, If so we don't need to update this var belowTileType = (int)below.TileType; if (PlantValidGrounds[plantType] is not null && PlantValidGrounds[plantType].Contains(belowTileType)) return; var newPlantType = plantType; if ((plantType == TileID.Plants || plantType == TileID.Plants2) && belowTileType != TileID.Grass && tile.TileFrameX >= 162) { Main.tile[x, y].TileFrameX = 126; } if (plantType == TileID.JunglePlants2 && belowTileType != TileID.JungleGrass && tile.TileFrameX >= 162) { Main.tile[x, y].TileFrameX = 126; } #region Biome Grass Replacements if (belowTileType == TileID.CorruptGrass) { newPlantType = TileID.CorruptPlants; if (tile.TileFrameX >= 162) { Main.tile[x, y].TileFrameX = 126; } } else if (belowTileType == TileID.Grass) { newPlantType = (plantType == TileID.HallowedPlants2 ? TileID.Plants2 : TileID.Plants); } else if (belowTileType == TileID.HallowedGrass) { newPlantType = (plantType == TileID.Plants2 ? TileID.HallowedPlants2 : TileID.HallowedPlants); } else if (belowTileType == TileID.CrimsonGrass) { newPlantType = TileID.CrimsonPlants; } else if (belowTileType == TileID.MushroomGrass) { newPlantType = TileID.MushroomPlants; while (Main.tile[x, y].TileFrameX > 72) { Main.tile[x, y].TileFrameX -= 72; } } else if (belowTileType == ModContent.TileType()) { var isShortPlant = plantType == TileID.Plants || plantType == TileID.CorruptPlants || plantType == TileID.CrimsonPlants || plantType == TileID.HallowedPlants || plantType == TileID.MushroomPlants || plantType == TileID.JunglePlants; newPlantType = isShortPlant ? ModContent.TileType() : ModContent.TileType(); } #endregion // If the tile type is not the same as the plant type, then set it equal. Otherwise, destroy it. if (plantType != newPlantType) { Main.tile[x, y].TileType = (ushort)newPlantType; } } internal static void VineFrame(int x, int y) { if (x < 0 || x >= Main.maxTilesX) return; if (y < 0 || y >= Main.maxTilesY) return; var tile = Main.tile[x, y]; int myType = tile.TileType; // Get the type of the tile above this vine. If that tile doesn't exist, just assume it's another vine. var north = y <= 0 ? default : Main.tile[x, y - 1]; var northType = north == default(Tile) ? myType : !north.HasTile || north.BottomSlope ? -1 : north.TileType; // Make this vine match the tile above it if that's another vine or a grass tile. var vines = VineToGrass.Keys.ToArray(); for (var i = 0; i < vines.Length; ++i) { var correspondingGrass = VineToGrass[vines[i]]; if (myType != vines[i] && (northType == correspondingGrass || northType == vines[i])) { Main.tile[x, y].TileType = vines[i]; WorldGen.SquareTileFrame(x, y, true); return; } } // If the tile above is an identical vine, nothing else needs to be done. if (northType == myType) return; // If the tile above isn't sloped correctly or otherwise isn't a valid anchor for this vine, check whether the vine must die. var tileMustDie = northType == -1; if (northType != -1) { // Vanilla vines can hang from vanilla grass and vanilla leaf blocks. if (myType == TileID.Vines && northType != TileID.Grass && northType != TileID.LeafBlock) { tileMustDie = true; } else if (myType != TileID.Vines) { for (var i = 0; i < vines.Length; ++i) { // Not matching grass? Die. if (myType == vines[i] && northType != VineToGrass[vines[i]]) { tileMustDie = true; break; } } } } if (tileMustDie) WorldGen.KillTile(x, y, false, false, false); } internal static bool BetterGemsparkFraming(int x, int y, bool resetFrame) { if (x < 0 || x >= Main.maxTilesX) return false; if (y < 0 || y >= Main.maxTilesY) return false; var tile = Main.tile[x, y]; if (tile.Slope > 0 && TileID.Sets.HasSlopeFrames[tile.TileType]) { return true; } GetAdjacentTiles(x, y, out var up, out var down, out var left, out var right, out var upLeft, out var upRight, out var downLeft, out var downRight); // Reset the tile's random frame style if the frame is being reset. int randomFrame; if (resetFrame) { randomFrame = WorldGen.genRand.Next(3); Main.tile[x, y].Get().TileFrameNumber = randomFrame; } else { randomFrame = Main.tile[x, y].TileFrameNumber; } /* 8 2 9 4 - 5 6 3 7 */ #region L States if (!up && down && !left && right && !downRight) { tile.TileFrameX = 13 * 18; tile.TileFrameY = 0; return false; } if (!up && down && left && !right && !downLeft) { tile.TileFrameX = 15 * 18; tile.TileFrameY = 0; return false; } if (up && !down && !left && right && !upRight) { tile.TileFrameX = 13 * 18; tile.TileFrameY = 2 * 18; return false; } if (up && !down && left && !right && !upLeft) { tile.TileFrameX = 15 * 18; tile.TileFrameY = 2 * 18; return false; } #endregion #region T States if (!up && down && left && right && !downLeft && !downRight) { tile.TileFrameX = 14 * 18; tile.TileFrameY = 0; return false; } if (up && !down && left && right && !upLeft && !upRight) { tile.TileFrameX = 14 * 18; tile.TileFrameY = 2 * 18; return false; } if (up && down && !left && right && !downRight && !upRight) { tile.TileFrameX = 13 * 18; tile.TileFrameY = 18; return false; } if (up && down && left && !right && !downLeft && !upLeft) { tile.TileFrameX = 15 * 18; tile.TileFrameY = 18; return false; } #endregion #region X State if (up && down && left && right && !downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 14 * 18; tile.TileFrameY = 18; return false; } #endregion #region Inner Corner x1 if (up && down && left && right && !downLeft && downRight && upLeft && upRight) { tile.TileFrameX = 15 * 18; tile.TileFrameY = 3 * 18; return false; } if (up && down && left && right && downLeft && !downRight && upLeft && upRight) { tile.TileFrameX = 14 * 18; tile.TileFrameY = 3 * 18; return false; } if (up && down && left && right && downLeft && downRight && !upLeft && upRight) { tile.TileFrameX = 15 * 18; tile.TileFrameY = 4 * 18; return false; } if (up && down && left && right && downLeft && downRight && upLeft && !upRight) { tile.TileFrameX = 14 * 18; tile.TileFrameY = 4 * 18; return false; } #endregion #region Inner Corner x2 (same side) if (up && down && left && right && !downLeft && !downRight && upLeft && upRight) { tile.TileFrameX = (short)(6 * 18 + randomFrame * 18); tile.TileFrameY = 2 * 18; return false; } if (up && down && left && right && downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = (short)(6 * 18 + randomFrame * 18); tile.TileFrameY = 1 * 18; return false; } if (up && down && left && right && !downLeft && downRight && !upLeft && upRight) { tile.TileFrameX = 10 * 18; tile.TileFrameY = (short)(randomFrame * 18); return false; } if (up && down && left && right && downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 11 * 18; tile.TileFrameY = (short)(randomFrame * 18); return false; } #endregion #region Inner Corner x2 (opposite corners) if (up && down && left && right && !downLeft && downRight && upLeft && !upRight) { tile.TileFrameX = 16 * 18; tile.TileFrameY = 4 * 18; return false; } if (up && down && left && right && downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 17 * 18; tile.TileFrameY = 4 * 18; return false; } #endregion #region Inner Corner x3 if (up && down && left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 12 * 18; tile.TileFrameY = 4 * 18; return false; } if (up && down && left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 12 * 18; tile.TileFrameY = 3 * 18; return false; } if (up && down && left && right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 13 * 18; tile.TileFrameY = 4 * 18; return false; } if (up && down && left && right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 13 * 18; tile.TileFrameY = 3 * 18; return false; } #endregion #region Corner and Side if (!up && down && left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 17 * 18; tile.TileFrameY = 2 * 18; return false; } if (!up && down && left && right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 16 * 18; tile.TileFrameY = 2 * 18; return false; } if (up && !down && left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 17 * 18; tile.TileFrameY = 3 * 18; return false; } if (up && !down && left && right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 16 * 18; tile.TileFrameY = 3 * 18; return false; } if (up && down && !left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 16 * 18; tile.TileFrameY = 0; return false; } if (up && down && !left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 16 * 18; tile.TileFrameY = 18; return false; } if (up && down && left && !right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 17 * 18; tile.TileFrameY = 0; return false; } if (up && down && left && !right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 17 * 18; tile.TileFrameY = 18; return false; } #endregion return true; } internal static bool BrimstoneFraming(int x, int y, bool resetFrame) { if (x < 0 || x >= Main.maxTilesX) return false; if (y < 0 || y >= Main.maxTilesY) return false; var tile = Main.tile[x, y]; if (tile.Slope > 0 && TileID.Sets.HasSlopeFrames[tile.TileType]) return true; GetAdjacentTiles(x, y, out var up, out var down, out var left, out var right, out var upLeft, out var upRight, out var downLeft, out var downRight); // Reset the tile's random frame style if the frame is being reset. int randomFrame; if (resetFrame) { randomFrame = WorldGen.genRand.Next(3); Main.tile[x, y].Get().TileFrameNumber = randomFrame; } else randomFrame = Main.tile[x, y].TileFrameNumber; var randomFrameX54 = randomFrame * 54; /* 8 2 9 4 - 5 6 3 7 */ #region L States if (!up && down && !left && right && !downRight) { tile.TileFrameX = (short)(16 * 18 + randomFrameX54); tile.TileFrameY = 0; return false; } if (!up && down && left && !right && !downLeft) { tile.TileFrameX = (short)(18 * 18 + randomFrameX54); tile.TileFrameY = 0; return false; } if (up && !down && !left && right && !upRight) { tile.TileFrameX = (short)(16 * 18 + randomFrameX54); tile.TileFrameY = 2 * 18; return false; } if (up && !down && left && !right && !upLeft) { tile.TileFrameX = (short)(18 * 18 + randomFrameX54); tile.TileFrameY = 2 * 18; return false; } #endregion #region T States if (!up && down && left && right && !downLeft && !downRight) { tile.TileFrameX = (short)(17 * 18 + randomFrameX54); tile.TileFrameY = 0; return false; } if (up && !down && left && right && !upLeft && !upRight) { tile.TileFrameX = (short)(17 * 18 + randomFrameX54); tile.TileFrameY = 2 * 18; return false; } if (up && down && !left && right && !downRight && !upRight) { tile.TileFrameX = (short)(16 * 18 + randomFrameX54); tile.TileFrameY = 18; return false; } if (up && down && left && !right && !downLeft && !upLeft) { tile.TileFrameX = (short)(18 * 18 + randomFrameX54); tile.TileFrameY = 18; return false; } #endregion #region X State if (up && down && left && right && !downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = (short)(17 * 18 + randomFrameX54); tile.TileFrameY = 18; return false; } #endregion #region Inner Corner x1 if (up && down && left && right && !downLeft && downRight && upLeft && upRight) { tile.TileFrameX = 14 * 18; tile.TileFrameY = (short)(5 * 18 + randomFrame * 36); return false; } if (up && down && left && right && downLeft && !downRight && upLeft && upRight) { tile.TileFrameX = 13 * 18; tile.TileFrameY = (short)(5 * 18 + randomFrame * 36); return false; } if (up && down && left && right && downLeft && downRight && !upLeft && upRight) { tile.TileFrameX = 14 * 18; tile.TileFrameY = (short)(6 * 18 + randomFrame * 36); return false; } if (up && down && left && right && downLeft && downRight && upLeft && !upRight) { tile.TileFrameX = 13 * 18; tile.TileFrameY = (short)(6 * 18 + randomFrame * 36); return false; } #endregion #region Inner Corner x2 (same side) if (up && down && left && right && !downLeft && !downRight && upLeft && upRight) { tile.TileFrameX = (short)(6 * 18 + randomFrame * 18); tile.TileFrameY = 2 * 18; return false; } if (up && down && left && right && downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = (short)(6 * 18 + randomFrame * 18); tile.TileFrameY = 1 * 18; return false; } if (up && down && left && right && !downLeft && downRight && !upLeft && upRight) { tile.TileFrameX = 10 * 18; tile.TileFrameY = (short)(randomFrame * 18); return false; } if (up && down && left && right && downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 11 * 18; tile.TileFrameY = (short)(randomFrame * 18); return false; } #endregion #region Inner Corner x2 (opposite corners) if (up && down && left && right && !downLeft && downRight && upLeft && !upRight) { tile.TileFrameX = (short)(10 * 18 + randomFrame * 18); tile.TileFrameY = 4 * 18; return false; } if (up && down && left && right && downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = (short)(13 * 18 + randomFrame * 18); tile.TileFrameY = 4 * 18; return false; } #endregion #region Inner Corner x3 if (up && down && left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 15 * 18; tile.TileFrameY = (short)(6 * 18 + randomFrame * 36); return false; } if (up && down && left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 15 * 18; tile.TileFrameY = (short)(5 * 18 + randomFrame * 36); return false; } if (up && down && left && right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 16 * 18; tile.TileFrameY = (short)(6 * 18 + randomFrame * 36); return false; } if (up && down && left && right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 16 * 18; tile.TileFrameY = (short)(5 * 18 + randomFrame * 36); return false; } #endregion #region Corner and Side if (!up && down && left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = (short)(17 * 18 + randomFrame * 36); tile.TileFrameY = 3 * 18; return false; } if (!up && down && left && right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = (short)(16 * 18 + randomFrame * 36); tile.TileFrameY = 3 * 18; return false; } if (up && !down && left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = (short)(17 * 18 + randomFrame * 36); tile.TileFrameY = 4 * 18; return false; } if (up && !down && left && right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = (short)(16 * 18 + randomFrame * 36); tile.TileFrameY = 4 * 18; return false; } if (up && down && !left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 17 * 18; tile.TileFrameY = (short)(5 * 18 + randomFrame * 36); return false; } if (up && down && !left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 17 * 18; tile.TileFrameY = (short)(6 * 18 + randomFrame * 36); return false; } if (up && down && left && !right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 18 * 18; tile.TileFrameY = (short)(5 * 18 + randomFrame * 36); return false; } if (up && down && left && !right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 18 * 18; tile.TileFrameY = (short)(6 * 18 + randomFrame * 36); return false; } #endregion return true; } internal static void CompactFraming(int x, int y, bool resetFrame = true) { if (x < 0 || x >= Main.maxTilesX) return; if (y < 0 || y >= Main.maxTilesY) return; var tile = Main.tile[x, y]; if (tile.Slope > 0 && TileID.Sets.HasSlopeFrames[tile.TileType]) return; // Reset the tile's random frame style if the frame is being reset. int randomFrame; if (resetFrame) { randomFrame = WorldGen.genRand.Next(3); Main.tile[x, y].Get().TileFrameNumber = (byte)randomFrame; } else randomFrame = Main.tile[x, y].TileFrameNumber; GetAdjacentTiles(x, y, out var up, out var down, out var left, out var right, out var upLeft, out var upRight, out var downLeft, out var downRight); #region Middle State if (up && down && left && right && upLeft && upRight && downLeft && downRight) { tile.TileFrameX = 18; tile.TileFrameY = 18; return; } #endregion #region Single State if (!up && !down && !left && !right) { tile.TileFrameX = 54; tile.TileFrameY = 54; return; } #endregion #region Edges if (!up && down && left && right && downLeft && downRight) { tile.TileFrameX = 18; tile.TileFrameY = 0; return; } if (up && down && !left && right && upRight && downRight) { tile.TileFrameX = 0; tile.TileFrameY = 18; return; } if (up && !down && left && right && upLeft && upRight) { tile.TileFrameX = 18; tile.TileFrameY = 36; return; } if (up && down && left && !right && upLeft && downLeft) { tile.TileFrameX = 36; tile.TileFrameY = 18; return; } #endregion #region Edge Corners if (!up && down && !left && right && downRight) { tile.TileFrameX = 0; tile.TileFrameY = 0; return; } if (!up && down && left && !right && downLeft) { tile.TileFrameX = 36; tile.TileFrameY = 0; return; } if (up && !down && !left && right && upRight) { tile.TileFrameX = 0; tile.TileFrameY = 36; return; } if (up && !down && left && !right && upLeft) { tile.TileFrameX = 36; tile.TileFrameY = 36; return; } #endregion #region I States if (up && down && !left && !right) { tile.TileFrameX = 54; tile.TileFrameY = 18; return; } if (!up && !down && left && right) { tile.TileFrameX = 18; tile.TileFrameY = 54; return; } #endregion #region I End States if (!up && down && !left && !right) { tile.TileFrameX = 54; tile.TileFrameY = 0; return; } if (up && !down && !left && !right) { tile.TileFrameX = 54; tile.TileFrameY = 36; return; } if (!up && !down && !left && right) { tile.TileFrameX = 0; tile.TileFrameY = 54; return; } if (!up && !down && left && !right) { tile.TileFrameX = 36; tile.TileFrameY = 54; return; } #endregion #region L States if (!up && down && !left && right && !downRight) { tile.TileFrameX = 72; tile.TileFrameY = 0; return; } if (!up && down && left && !right && !downLeft) { tile.TileFrameX = 108; tile.TileFrameY = 0; return; } if (up && !down && !left && right && !upRight) { tile.TileFrameX = 72; tile.TileFrameY = 36; return; } if (up && !down && left && !right && !upLeft) { tile.TileFrameX = 108; tile.TileFrameY = 36; return; } #endregion #region T States if (!up && down && left && right && !downLeft && !downRight) { tile.TileFrameX = 90; tile.TileFrameY = 0; return; } if (up && !down && left && right && !upLeft && !upRight) { tile.TileFrameX = 90; tile.TileFrameY = 36; return; } if (up && down && !left && right && !downRight && !upRight) { tile.TileFrameX = 72; tile.TileFrameY = 18; return; } if (up && down && left && !right && !downLeft && !upLeft) { tile.TileFrameX = 108; tile.TileFrameY = 18; return; } #endregion #region X State if (up && down && left && right && !downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 90; tile.TileFrameY = 18; return; } #endregion #region Inner Corner x1 if (up && down && left && right && !downLeft && downRight && upLeft && upRight) { tile.TileFrameX = 144; tile.TileFrameY = 36; return; } if (up && down && left && right && downLeft && !downRight && upLeft && upRight) { tile.TileFrameX = 126; tile.TileFrameY = 36; return; } if (up && down && left && right && downLeft && downRight && !upLeft && upRight) { tile.TileFrameX = 144; tile.TileFrameY = 54; return; } if (up && down && left && right && downLeft && downRight && upLeft && !upRight) { tile.TileFrameX = 126; tile.TileFrameY = 54; return; } #endregion #region Inner Corner x2 (same side) if (up && down && left && right && !downLeft && !downRight && upLeft && upRight) { tile.TileFrameX = 198; tile.TileFrameY = 0; return; } if (up && down && left && right && downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 198; tile.TileFrameY = 18; return; } if (up && down && left && right && !downLeft && downRight && !upLeft && upRight) { tile.TileFrameX = 198; tile.TileFrameY = 36; return; } if (up && down && left && right && downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 198; tile.TileFrameY = 54; return; } #endregion #region Inner Corner x2 (opposite corners) if (up && down && left && right && !downLeft && downRight && upLeft && !upRight) { tile.TileFrameX = 108; tile.TileFrameY = 54; return; } if (up && down && left && right && downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 90; tile.TileFrameY = 54; return; } #endregion #region Inner Corner x3 if (up && down && left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 126; tile.TileFrameY = 18; return; } if (up && down && left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 126; tile.TileFrameY = 0; return; } if (up && down && left && right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 144; tile.TileFrameY = 18; return; } if (up && down && left && right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 144; tile.TileFrameY = 0; return; } #endregion #region Corner and Side if (!up && down && left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 180; tile.TileFrameY = 0; return; } if (!up && down && left && right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 162; tile.TileFrameY = 0; return; } if (up && !down && left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 180; tile.TileFrameY = 18; return; } if (up && !down && left && right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 162; tile.TileFrameY = 18; return; } if (up && down && !left && right && !downLeft && !downRight && !upLeft && upRight) { tile.TileFrameX = 162; tile.TileFrameY = 36; return; } if (up && down && !left && right && !downLeft && downRight && !upLeft && !upRight) { tile.TileFrameX = 162; tile.TileFrameY = 54; return; } if (up && down && left && !right && !downLeft && !downRight && upLeft && !upRight) { tile.TileFrameX = 180; tile.TileFrameY = 36; return; } if (up && down && left && !right && downLeft && !downRight && !upLeft && !upRight) { tile.TileFrameX = 180; tile.TileFrameY = 54; return; } #endregion } internal static void SlopedGlowmask(ref readonly Tile tile, int i, int j, Texture2D texture, Rectangle? sourceRectangle, Color drawColor, Vector2 positionOffset) { int frameX = tile.TileFrameX; int frameY = tile.TileFrameY; int width = 16; int height = 16; if (sourceRectangle != null) { frameX = ((Rectangle)sourceRectangle).X; frameY = ((Rectangle)sourceRectangle).Y; } int iX16 = i * 16; int jX16 = j * 16; Vector2 location = new Vector2(iX16, jX16); Vector2 zero = Main.drawToScreen ? Vector2.Zero : new Vector2(Main.offScreenRange, Main.offScreenRange); Vector2 offsets = -Main.screenPosition + zero + positionOffset; Vector2 drawCoordinates = location + offsets; if ((tile.Slope == 0 && !tile.IsHalfBlock) || (Main.tileSolid[tile.TileType] && Main.tileSolidTop[tile.TileType])) //second one should be for platforms { Main.spriteBatch.Draw(texture, drawCoordinates, new Rectangle(frameX, frameY, width, height), drawColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); } else if (tile.IsHalfBlock) { Main.spriteBatch.Draw(texture, new Vector2(drawCoordinates.X, drawCoordinates.Y + 8), new Rectangle(frameX, frameY, width, 8), drawColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); } else { byte b = (byte)tile.Slope; Rectangle TileFrame; Vector2 drawPos; if (b == 1 || b == 2) { int length; int height2; for (int a = 0; a < 8; ++a) { int aX2 = a * 2; if (b == 2) { length = 16 - aX2 - 2; height2 = 14 - aX2; } else { length = aX2; height2 = 14 - length; } TileFrame = new Rectangle(frameX + length, frameY, 2, height2); drawPos = new Vector2(iX16 + length, jX16 + aX2) + offsets; Main.spriteBatch.Draw(texture, drawPos, TileFrame, drawColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } TileFrame = new Rectangle(frameX, frameY + 14, 16, 2); drawPos = new Vector2(iX16, jX16 + 14) + offsets; Main.spriteBatch.Draw(texture, drawPos, TileFrame, drawColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } else { int length; int height2; for (int a = 0; a < 8; ++a) { int aX2 = a * 2; if (b == 3) { length = aX2; height2 = 16 - length; } else { length = 16 - aX2 - 2; height2 = 16 - aX2; } TileFrame = new Rectangle(frameX + length, frameY + 16 - height2, 2, height2); drawPos = new Vector2(iX16 + length, jX16) + offsets; Main.spriteBatch.Draw(texture, drawPos, TileFrame, drawColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } drawPos = new Vector2(iX16, jX16) + offsets; if (tile.TileType != EutrophicGlass.TypeCache) { TileFrame = new Rectangle(frameX, frameY, 16, 2); Main.spriteBatch.Draw(texture, drawPos, TileFrame, drawColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } } } // Contribuited by Vortex } #endregion #region Generic Custom Framing Code internal static void CustomMergeFrameExplicit(int x, int y, int myType, int mergeType, out bool mergedUp, out bool mergedLeft, out bool mergedRight, out bool mergedDown, bool forceSameDown = false, bool forceSameUp = false, bool forceSameLeft = false, bool forceSameRight = false, bool resetFrame = true, bool myTypeBrimFrame = false) { if (x < 0 || x >= Main.maxTilesX || y < 0 || y >= Main.maxTilesY) { mergedUp = mergedLeft = mergedRight = mergedDown = false; return; } // Disable vanilla trying to merge these tiles automtaically. Main.tileMerge[myType][mergeType] = false; // These all get null checked in the GetSimilarity and GetMerge functions var tileLeft = Main.tile[x - 1, y]; var tileRight = Main.tile[x + 1, y]; var tileUp = Main.tile[x, y - 1]; var tileDown = Main.tile[x, y + 1]; var tileTopLeft = Main.tile[x - 1, y - 1]; var tileTopRight = Main.tile[x + 1, y - 1]; var tileBottomLeft = Main.tile[x - 1, y + 1]; var tileBottomRight = Main.tile[x + 1, y + 1]; // Cardinal directions var leftSim = forceSameLeft ? Similarity.Same : GetSimilarity(tileLeft, myType, mergeType); var rightSim = forceSameRight ? Similarity.Same : GetSimilarity(tileRight, myType, mergeType); var upSim = forceSameUp ? Similarity.Same : GetSimilarity(tileUp, myType, mergeType); var downSim = forceSameDown ? Similarity.Same : GetSimilarity(tileDown, myType, mergeType); // Diagonal directions var topLeftSim = GetSimilarity(tileTopLeft, myType, mergeType); var topRightSim = GetSimilarity(tileTopRight, myType, mergeType); var bottomLeftSim = GetSimilarity(tileBottomLeft, myType, mergeType); var bottomRightSim = GetSimilarity(tileBottomRight, myType, mergeType); // Reset the tile's random frame style if the frame is being reset. int randomFrame; if (resetFrame) { randomFrame = WorldGen.genRand.Next(3); Main.tile[x, y].Get().TileFrameNumber = (byte)randomFrame; } else { randomFrame = Main.tile[x, y].TileFrameNumber; } // Initialize all merged variables to false. mergedDown = mergedLeft = mergedRight = mergedUp = false; #region Custom Merge Conditional Tree if (leftSim == Similarity.None) { if (upSim == Similarity.Same) { if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { SetFrameAt(x, y, 0, 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { mergedRight = true; SetFrameAt(x, y, 234 + 18 * randomFrame, 36); return; } SetFrameAt(x, y, 90, 18 * randomFrame); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { mergedDown = true; SetFrameAt(x, y, 72, 90 + 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 108 + 18 * randomFrame, 54); return; } mergedDown = true; SetFrameAt(x, y, 126, 90 + 18 * randomFrame); return; } if (rightSim == Similarity.Same) { SetFrameAt(x, y, 36 * randomFrame, 72); return; } SetFrameAt(x, y, 108 + 18 * randomFrame, 54); return; } else if (upSim == Similarity.MergeLink) { if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { mergedUp = true; SetFrameAt(x, y, 72, 144 + 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 108 + 18 * randomFrame, 0); return; } mergedUp = true; SetFrameAt(x, y, 126, 144 + 18 * randomFrame); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { SetFrameAt(x, y, 162, 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } mergedUp = true; mergedDown = true; SetFrameAt(x, y, 108, 216 + 18 * randomFrame); return; } if (rightSim == Similarity.Same) { SetFrameAt(x, y, 162, 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } mergedUp = true; SetFrameAt(x, y, 108, 144 + 18 * randomFrame); return; } if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { SetFrameAt(x, y, 36 * randomFrame, 54); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 108 + 18 * randomFrame, 0); return; } SetFrameAt(x, y, 108 + 18 * randomFrame, 0); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { SetFrameAt(x, y, 162, 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } mergedDown = true; SetFrameAt(x, y, 108, 90 + 18 * randomFrame); return; } if (rightSim == Similarity.Same) { SetFrameAt(x, y, 162, 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { mergedRight = true; SetFrameAt(x, y, 54 + 18 * randomFrame, 234); return; } SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } else if (leftSim == Similarity.MergeLink) { if (upSim == Similarity.Same) { if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { mergedLeft = true; SetFrameAt(x, y, 162, 126 + 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { mergedLeft = true; mergedRight = true; SetFrameAt(x, y, 180, 126 + 18 * randomFrame); return; } mergedLeft = true; SetFrameAt(x, y, 234 + 18 * randomFrame, 54); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { mergedLeft = mergedDown = true; SetFrameAt(x, y, 36, 108 + 36 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { mergedLeft = mergedRight = mergedDown = true; SetFrameAt(x, y, 198, 144 + 18 * randomFrame); return; } SetFrameAt(x, y, 108 + 18 * randomFrame, 54); return; } if (rightSim == Similarity.Same) { mergedLeft = true; SetFrameAt(x, y, 18 * randomFrame, 216); return; } SetFrameAt(x, y, 108 + 18 * randomFrame, 54); return; } else if (upSim == Similarity.MergeLink) { if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { mergedUp = mergedLeft = true; SetFrameAt(x, y, 36, 90 + 36 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { mergedLeft = mergedRight = mergedUp = true; SetFrameAt(x, y, 198, 90 + 18 * randomFrame); return; } SetFrameAt(x, y, 108 + 18 * randomFrame, 0); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { mergedUp = mergedLeft = mergedDown = true; SetFrameAt(x, y, 216, 90 + 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { mergedDown = mergedLeft = mergedRight = mergedUp = true; SetFrameAt(x, y, 108 + 18 * randomFrame, 198); return; } SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } if (rightSim == Similarity.Same) { SetFrameAt(x, y, 162, 18 * randomFrame); return; } SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { mergedLeft = true; SetFrameAt(x, y, 18 * randomFrame, 198); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 108 + 18 * randomFrame, 0); return; } SetFrameAt(x, y, 108 + 18 * randomFrame, 0); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { SetFrameAt(x, y, 162, 18 * randomFrame); return; } else if (rightSim == Similarity.MergeLink) { SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } SetFrameAt(x, y, 162 + 18 * randomFrame, 54); return; } if (rightSim == Similarity.Same) { mergedLeft = true; SetFrameAt(x, y, 18 * randomFrame, 252); return; } else if (rightSim == Similarity.MergeLink) { mergedRight = mergedLeft = true; SetFrameAt(x, y, 162 + 18 * randomFrame, 198); return; } mergedLeft = true; SetFrameAt(x, y, 18 * randomFrame, 234); return; } if (upSim == Similarity.Same) { if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { #region FULL TILE STUFF if (topLeftSim == Similarity.MergeLink || topRightSim == Similarity.MergeLink || bottomLeftSim == Similarity.MergeLink || bottomRightSim == Similarity.MergeLink) { if (bottomRightSim == Similarity.MergeLink) { SetFrameAt(x, y, 0, 90 + 36 * randomFrame); return; } else if (bottomLeftSim == Similarity.MergeLink) { SetFrameAt(x, y, 18, 90 + 36 * randomFrame); return; } else if (topRightSim == Similarity.MergeLink) { SetFrameAt(x, y, 0, 108 + 36 * randomFrame); return; } SetFrameAt(x, y, 18, 108 + 36 * randomFrame); return; } if (topLeftSim == Similarity.Same) { if (topRightSim == Similarity.Same) { if (bottomLeftSim == Similarity.Same) { SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; } if (bottomRightSim == Similarity.Same) { SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; } SetFrameAt(x, y, 108 + 18 * randomFrame, 36); return; } if (bottomLeftSim == Similarity.Same) { if (bottomRightSim == Similarity.Same) { if (topRightSim == Similarity.MergeLink) { SetFrameAt(x, y, 0, 108 + 36 * randomFrame); return; } SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; } SetFrameAt(x, y, 198, 18 * randomFrame); return; } } else if (topLeftSim == Similarity.None) { if (topRightSim == Similarity.Same) { if (bottomRightSim == Similarity.Same) { if (bottomLeftSim == Similarity.Same) { SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; } SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; } if (bottomLeftSim == Similarity.Same) { SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; } } SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; } SetFrameAt(x, y, 18 + 18 * randomFrame, 18); return; #endregion } else if (rightSim == Similarity.MergeLink) { mergedRight = true; SetFrameAt(x, y, 144, 126 + 18 * randomFrame); return; } SetFrameAt(x, y, 72, 18 * randomFrame); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { mergedDown = true; SetFrameAt(x, y, 144 + 18 * randomFrame, 90); return; } else if (rightSim == Similarity.MergeLink) { mergedDown = mergedRight = true; SetFrameAt(x, y, 54, 108 + 36 * randomFrame); return; } mergedDown = true; SetFrameAt(x, y, 90, 90 + 18 * randomFrame); return; } if (rightSim == Similarity.Same) { SetFrameAt(x, y, 18 + 18 * randomFrame, 36); return; } else if (rightSim == Similarity.MergeLink) { mergedRight = true; SetFrameAt(x, y, 54 + 18 * randomFrame, 216); return; } SetFrameAt(x, y, 18 + 36 * randomFrame, 72); return; } else if (upSim == Similarity.MergeLink) { if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { mergedUp = true; SetFrameAt(x, y, 144 + 18 * randomFrame, 108); return; } else if (rightSim == Similarity.MergeLink) { mergedRight = mergedUp = true; SetFrameAt(x, y, 54, 90 + 36 * randomFrame); return; } mergedUp = true; SetFrameAt(x, y, 90, 144 + 18 * randomFrame); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { mergedUp = mergedDown = true; SetFrameAt(x, y, 144 + 18 * randomFrame, 180); return; } else if (rightSim == Similarity.MergeLink) { mergedUp = mergedRight = mergedDown = true; SetFrameAt(x, y, 216, 144 + 18 * randomFrame); return; } SetFrameAt(x, y, 216, 18 * randomFrame); return; } if (rightSim == Similarity.Same) { mergedUp = true; SetFrameAt(x, y, 234 + 18 * randomFrame, 18); return; } SetFrameAt(x, y, 216, 18 * randomFrame); return; } if (downSim == Similarity.Same) { if (rightSim == Similarity.Same) { SetFrameAt(x, y, 18 + 18 * randomFrame, 0); return; } else if (rightSim == Similarity.MergeLink) { mergedRight = true; SetFrameAt(x, y, 54 + 18 * randomFrame, 198); return; } SetFrameAt(x, y, 18 + 36 * randomFrame, 54); return; } else if (downSim == Similarity.MergeLink) { if (rightSim == Similarity.Same) { mergedDown = true; SetFrameAt(x, y, 234 + 18 * randomFrame, 0); return; } SetFrameAt(x, y, 216, 18 * randomFrame); return; } if (rightSim == Similarity.Same) { SetFrameAt(x, y, 108 + 18 * randomFrame, 72); return; } else if (rightSim == Similarity.MergeLink) { mergedRight = true; SetFrameAt(x, y, 54 + 18 * randomFrame, 252); return; } SetFrameAt(x, y, 216, 18 * randomFrame); return; #endregion } internal static void CustomMergeFrame(int x, int y, int myType, int mergeType, bool forceSameDown = false, bool forceSameUp = false, bool forceSameLeft = false, bool forceSameRight = false, bool resetFrame = true) => CustomMergeFrameExplicit(x, y, myType, mergeType, out _, out _, out _, out _, forceSameDown, forceSameUp, forceSameLeft, forceSameRight, resetFrame); internal static void CustomMergeFrame(int x, int y, int myType, int mergeType) { if (x < 0 || x >= Main.maxTilesX) return; if (y < 0 || y >= Main.maxTilesY) return; var forceSameUp = false; var forceSameDown = false; var forceSameLeft = false; var forceSameRight = false; var north = Main.tile[x, y - 1]; var south = Main.tile[x, y + 1]; var west = Main.tile[x - 1, y]; var east = Main.tile[x + 1, y]; if (north != null && north.HasTile && tileMergeTypes[myType][north.TileType]) { // Register this tile as not automatically merging with the tile above it. CalamityUtils.SetMerge(myType, north.TileType, false); TileID.Sets.ChecksForMerge[myType] = true; // Properly frame the adjacent tile given this constraint. CustomMergeFrameExplicit(x, y - 1, north.TileType, myType, out _, out _, out _, out forceSameUp, false, false, false, false, false); } if (west != null && west.HasTile && tileMergeTypes[myType][west.TileType]) { // Register this tile as not automatically merging with the tile to the left of it. CalamityUtils.SetMerge(myType, west.TileType, false); TileID.Sets.ChecksForMerge[myType] = true; // Properly frame the adjacent tile given this constraint. CustomMergeFrameExplicit(x - 1, y, west.TileType, myType, out _, out _, out forceSameLeft, out _, false, false, false, false, false); } if (east != null && east.HasTile && tileMergeTypes[myType][east.TileType]) { // Register this tile as not automatically merging with the tile to the right of it. CalamityUtils.SetMerge(myType, east.TileType, false); TileID.Sets.ChecksForMerge[myType] = true; // Properly frame the adjacent tile given this constraint. CustomMergeFrameExplicit(x + 1, y, east.TileType, myType, out _, out forceSameRight, out _, out _, false, false, false, false, false); } if (south != null && south.HasTile && tileMergeTypes[myType][south.TileType]) { // Register this tile as not automatically merging with the tile below it. CalamityUtils.SetMerge(myType, south.TileType, false); TileID.Sets.ChecksForMerge[myType] = true; // Properly frame the adjacent tile given this constraint. CustomMergeFrameExplicit(x, y + 1, south.TileType, myType, out forceSameDown, out _, out _, out _, false, false, false, false, false); } // With all constraints determined, properly frame the tile a final time. CustomMergeFrameExplicit(x, y, myType, mergeType, out _, out _, out _, out _, forceSameDown, forceSameUp, forceSameLeft, forceSameRight, true); } #endregion #region Universal Merge Code // Code responsible for the 'Universal Tile Merge' code created to get around the issues with merging brimstone tile framing // It'll work for any tile regardless of framing and could in theory be added to vanilla tiles as well if desired // A single tile could also be set to blend with multiple other tiles, though a new adjacency data array will be needed for each tile type it'll merge with as well as a new call of each function so it's probably not the best // I chose to use bytes and byte masks to store and check the merge data so that the fully merge conditional tree wouldn't need to be called every frame for every tile // Not sure if this is the best option, I thought it would minimise the amount of data required to store the adjacency info while being reasonably efficient // Apologies for how unreadable this makes the code, I did my best to comment what each part of the merge tree in DrawUniversalMergeFrames is responsible for to compensate for this but it's still not ideal // - Alta internal static void RegisterUniversalMerge(int tileId, int mergeType, string blendSheetPath) { SetUpUniversalMerge(tileId, mergeType, blendSheetPath, out var data); GetOrCreateTileAdjacencies(tileId).Add(data); } private static List GetOrCreateTileAdjacencies(int tileId) { if (!tileAdjacencyMap.TryGetValue(tileId, out var adjacencies)) tileAdjacencyMap[tileId] = adjacencies = []; return adjacencies; } /// /// Call this in SetStaticDefaults. Used to set up the adjacency data array with the correct dimensions. /// internal static void SetUpUniversalMerge(int myType, int mergeType, string blendSheetPath, out MergeFrameData adjacencyData) { CalamityUtils.SetMerge(myType, mergeType, true); adjacencyData = new MergeFrameData(new byte[Main.tile.Width, Main.tile.Height], mergeType, blendSheetPath); } /// /// Call this in PostDraw. Uses adjacency data generated by GetAdjacencyData to draw the correct blend state over a given tile, regardless of what tile frame that tile is using /// internal static void DrawUniversalMergeFrames(int x, int y, List mergeFrames) { var shadingColour = Lighting.GetColor(x, y); var myTile = Main.tile[x, y]; foreach (var frame in mergeFrames) { // If it hasn't yet been initialized somehow... for some reason. if (frame is null) continue; DrawUniversalMergeFrames(x, y, frame, shadingColour, myTile); } } internal static void DrawUniversalMergeFrames(int x, int y, MergeFrameData mergeFrameData, Color shadingColour, Tile myTile) { var adjacencyData = mergeFrameData.AdjacencyData; var blendLayer = mergeFrameData.BlendSheet.Value; var frameOffsetX = mergeFrameData.FrameOffsetX; var frameOffsetY = mergeFrameData.FrameOffsetY; var zero = Main.drawToScreen ? Vector2.Zero : new Vector2(Main.offScreenRange); var drawOffset = new Vector2(x * 16 - Main.screenPosition.X, y * 16 - Main.screenPosition.Y) + zero; // Used for blending with metatiles/composite tiles (as in the tile being blended with is a metatile, not this tile) var subFrameX = frameOffsetX * 270; var subFrameY = frameOffsetY * 198; var thisTileData = adjacencyData[x, y]; // Get tile variant number var randomFrame = myTile.TileFrameNumber; var randomFrameX18 = randomFrame * 18; var randomFrameX36 = randomFrame * 36; // Couple of commonly used masks // bit to relative tile pos for reference // 4 0 5 // 3 X 1 // 7 2 6 byte maskAdjSides = 0b11110000; byte maskAdjCorners = 0b00001111; #region Merge Checks & Drawing // Switch statement for the mutually exclusive states switch (thisTileData & maskAdjSides) { case 0b11110000: // All sides Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX18 + subFrameX, 18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b01110000: // All except north switch (thisTileData & 0b00001100) { case 0b00001100: // Extend to both north corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 90 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00001000: // Extend to northwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(198 + randomFrameX18 + subFrameX, 180 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000100: // Extend to northeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 180 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 144 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } case 0b10110000: // All except east switch (thisTileData & 0b00000110) { case 0b00000110: // Extend to both east corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(108 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000100: // Extend to northeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(234 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000010: // Extend to southeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(198 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(162 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } case 0b11010000: // All except south switch (thisTileData & 0b00000011) { case 0b00000011: // Extend to both south corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 72 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000010: // Extend to southeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 162 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000001: // Extend to southwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(198 + randomFrameX18 + subFrameX, 162 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 126 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } case 0b11100000: // All except west switch (thisTileData & 0b00001001) { case 0b00001001: // Extend to both west corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(126 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000001: // Extend to southwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(216 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00001000: // Extend to northwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(252 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(180 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } case 0b10010000: // North & West edges if ((thisTileData & 0b01100010) == 0b00000010) { // Blending tile at southeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36 + subFrameX, 54 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } switch (thisTileData & 0b00000101) { case 0b00000101: // Extend to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36 + subFrameX, 90 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000100: // Extend to northeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(234 + subFrameX, 54 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000001: // Extend to southwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(198 + subFrameX, 54 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36 + subFrameX, 126 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } case 0b11000000: // North & East edges if ((thisTileData & 0b00110001) == 0b00000001) { // Blending tile at southwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 54 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } switch (thisTileData & 0b00001010) { case 0b00001010: // Extend to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 90 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00001000: // Extend to northwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(252 + subFrameX, 54 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000010: // Extend to southeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(216 + subFrameX, 54 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 126 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } case 0b01100000: // South & East edges if ((thisTileData & 0b10011000) == 0b00001000) { // Blending tile at northwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 72 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } switch (thisTileData & 0b00000101) { case 0b00000101: // Extend to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 108 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000100: // Extend to northeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(216 + subFrameX, 72 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000001: // Extend to southwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(252 + subFrameX, 72 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 144 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } case 0b00110000: // South & West edges if ((thisTileData & 0b11000100) == 0b00000100) { // Blending tile at northeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36 + subFrameX, 72 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } switch (thisTileData & 0b00001010) { case 0b00001010: // Extend to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36, 108 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00001000: // Extend to northwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(198 + subFrameX, 72 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; case 0b00000010: // Extend to southeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(234 + subFrameX, 72 + randomFrameX36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; default: // Extend to neither corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36 + subFrameX, 144 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); return; } } // Handle edges and corners, which due to how they can be overlayed in so many ways kind of need their own way of doing things // North Edge if ((thisTileData & 0b11010000) == 0b10000000) { switch (thisTileData & 0b00001100) { case 0b00000000: // Narrow Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 108 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00001000: // Extends to Northwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(126 + subFrameX, 108 + randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00000100: // Extends to Northeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(108 + subFrameX, 108 + randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; default: // Extends to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX18 + subFrameX, 36 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; } } // East Edge if ((thisTileData & 0b11100000) == 0b01000000) { switch (thisTileData & 0b00000110) { case 0b00000000: // Narrow Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(90 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00000100: // Extends to Northeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX18 + subFrameX, 180 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00000010: // Extends to Southeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX18 + subFrameX, 162 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; default: // Extends to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(0 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; } } // South Edge if ((thisTileData & 0b01110000) == 0b00100000) { switch (thisTileData & 0b00000011) { case 0b00000000: // Narrow Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + randomFrameX18 + subFrameX, 54 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00000010: // Extends to Southeast corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(108 + subFrameX, 54 + randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00000001: // Extends to Southwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(126 + subFrameX, 54 + randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; default: // Extends to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX18 + subFrameX, 0 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; } } // West Edge if ((thisTileData & 0b10110000) == 0b00010000) { switch (thisTileData & 0b00001001) { case 0b00000000: // Narrow Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(144 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00000001: // Extends to Southwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(54 + randomFrameX18 + subFrameX, 162 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; case 0b00001000: // Extends to Northwest corner Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(54 + randomFrameX18 + subFrameX, 180 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; default: // Extends to both corners Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(72 + subFrameX, randomFrameX18 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); break; } } // Northwest Corner if ((thisTileData & 0b10011000) == 0b00001000) { Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 72 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } // Northeast Corner if ((thisTileData & 0b11000100) == 0b00000100) { Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36 + subFrameX, 72 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } // Southeast Corner if ((thisTileData & 0b01100010) == 0b00000010) { Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(randomFrameX36 + subFrameX, 54 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } // Southwest Corner if ((thisTileData & 0b00110001) == 0b00000001) { Main.spriteBatch.Draw(blendLayer, drawOffset, new Rectangle?(new Rectangle(18 + randomFrameX36 + subFrameX, 54 + subFrameY, 18, 18)), shadingColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } #endregion } /// /// Call this in TileFrame. Used to generate adjacency data used in DrawUniversalMergeFrames. Tiles using this need a 2d byte array /// internal static void GetAdjacencyData(int x, int y, MergeFrameData data) { var blendType = data.TileID; // bit to relative tile pos for reference // 4 0 5 // 3 X 1 // 7 2 6 // 01234567 ref byte adjacencyData = ref data.AdjacencyData[x, y]; adjacencyData = 0b00000000; if (x < 0 || x >= Main.maxTilesX) return; if (y < 0 || y >= Main.maxTilesY) return; var tile = Main.tile[x, y]; var north = Main.tile[x, y - 1]; var south = Main.tile[x, y + 1]; var west = Main.tile[x - 1, y]; var east = Main.tile[x + 1, y]; var southwest = Main.tile[x - 1, y + 1]; var southeast = Main.tile[x + 1, y + 1]; var northwest = Main.tile[x - 1, y - 1]; var northeast = Main.tile[x + 1, y - 1]; // North if (GetBlendSpecific(tile, north, blendType, false) && (north.Slope == 0 || north.Slope == SlopeType.SlopeDownLeft || north.Slope == SlopeType.SlopeDownRight)) adjacencyData |= 0b10000000; // East if (GetBlendSpecific(tile, east, blendType, false) && (east.Slope == 0 || east.Slope == SlopeType.SlopeDownLeft || east.Slope == SlopeType.SlopeUpLeft)) adjacencyData |= 0b01000000; // South if (GetBlendSpecific(tile, south, blendType, false) && (south.Slope == 0 || south.Slope == SlopeType.SlopeUpLeft || south.Slope == SlopeType.SlopeUpRight)) adjacencyData |= 0b00100000; // West if (GetBlendSpecific(tile, west, blendType, false) && (west.Slope == 0 || west.Slope == SlopeType.SlopeDownRight || west.Slope == SlopeType.SlopeUpRight)) adjacencyData |= 0b00010000; // Northwest if (GetCornerBlendCondition(tile, northwest, north, west, blendType) && (northwest.Slope == 0 || northwest.Slope == SlopeType.SlopeDownRight) && (north.Slope == 0 || north.Slope == SlopeType.SlopeDownLeft || north.Slope == SlopeType.SlopeUpLeft) && (west.Slope == 0 || west.Slope == SlopeType.SlopeUpLeft || west.Slope == SlopeType.SlopeUpRight)) adjacencyData |= 0b00001000; // Northeast if (GetCornerBlendCondition(tile, northeast, north, east, blendType) && (northeast.Slope == 0 || northeast.Slope == SlopeType.SlopeDownLeft) && (north.Slope == 0 || north.Slope == SlopeType.SlopeDownRight || north.Slope == SlopeType.SlopeUpRight) && (east.Slope == 0 || east.Slope == SlopeType.SlopeUpLeft || east.Slope == SlopeType.SlopeUpRight)) adjacencyData |= 0b00000100; // Southeast if (GetCornerBlendCondition(tile, southeast, south, east, blendType) && !southeast.IsHalfBlock && (southeast.Slope == 0 || southeast.Slope == SlopeType.SlopeUpLeft) && (south.Slope == 0 || south.Slope == SlopeType.SlopeDownRight || south.Slope == SlopeType.SlopeUpRight) && (east.Slope == 0 || east.Slope == SlopeType.SlopeDownLeft || east.Slope == SlopeType.SlopeDownRight)) adjacencyData |= 0b00000010; // Southwest if (GetCornerBlendCondition(tile, southwest, south, west, blendType) && !southwest.IsHalfBlock && (southwest.Slope == 0 || southwest.Slope == SlopeType.SlopeUpRight) && (south.Slope == 0 || south.Slope == SlopeType.SlopeDownLeft || south.Slope == SlopeType.SlopeUpLeft) && (west.Slope == 0 || west.Slope == SlopeType.SlopeDownLeft || west.Slope == SlopeType.SlopeDownRight)) adjacencyData |= 0b00000001; } /// /// Used to get the correct corner adjacency data because it's more complex than the simpler cardinal directions /// private static bool GetCornerBlendCondition(Tile myTile, Tile mergeTileCorner, Tile mergeTileEdgeA, Tile mergeTileEdgeB, int blendType) { int cornerType = mergeTileCorner.TileType; int edgeAType = mergeTileEdgeA.TileType; int edgeBType = mergeTileEdgeB.TileType; var isBlendTypeAdjacent = cornerType == blendType || edgeAType == blendType || edgeBType == blendType; var areAllAdjacentsBlendable = GetBlendSpecific(myTile, mergeTileCorner, blendType, true) && GetBlendSpecific(myTile, mergeTileEdgeA, blendType, true) && GetBlendSpecific(myTile, mergeTileEdgeB, blendType, true); return isBlendTypeAdjacent && areAllAdjacentsBlendable; } #endregion } }