using CalamityMod.Tiles; using CalamityMod.Tiles.Abyss; using CalamityMod.Tiles.Astral; using CalamityMod.Tiles.AstralDesert; using CalamityMod.Tiles.AstralSnow; using CalamityMod.Tiles.Crags; using CalamityMod.Tiles.FurnitureAbyss; using CalamityMod.Tiles.FurnitureAshen; using CalamityMod.Tiles.FurnitureEutrophic; using CalamityMod.Tiles.FurnitureOtherworldly; using CalamityMod.Tiles.FurnitureProfaned; using CalamityMod.Tiles.FurnitureVoid; using CalamityMod.Tiles.Ores; using CalamityMod.Tiles.SunkenSea; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using Terraria; using Terraria.ID; using Terraria.ModLoader; using Terraria.ObjectData; using static Terraria.ModLoader.ModContent; using System.Linq; using System.Collections.Generic; namespace CalamityMod { public static partial class CalamityUtils { public static string GetMapChestName(string baseName, int x, int y) { // Bounds check. if (!WorldGen.InWorld(x, y, 2)) return baseName; Tile tile = Main.tile[x, y]; int left = x; int top = y; if (tile.TileFrameX % 36 != 0) left--; if (tile.TileFrameY != 0) top--; int chest = Chest.FindChest(left, top); // Valid chest index check. if (chest < 0) return baseName; string name = baseName; // Concatenate the chest's custom name if it has one. if (!string.IsNullOrEmpty(Main.chest[chest].name)) name += $": {Main.chest[chest].name}"; return name; } public static void SafeSquareTileFrame(int x, int y, bool resetFrame = true) { for (int xIter = x - 1; xIter <= x + 1; ++xIter) { if (xIter < 0 || xIter >= Main.maxTilesX) continue; for (int yIter = y - 1; yIter <= y + 1; yIter++) { if (yIter < 0 || yIter >= Main.maxTilesY) continue; if (xIter == x && yIter == y) WorldGen.TileFrame(x, y, resetFrame, false); else WorldGen.TileFrame(xIter, yIter, false, false); } } } public static void LightHitWire(int type, int i, int j, int tileX, int tileY) { int x = i - Main.tile[i, j].TileFrameX / 18 % tileX; int y = j - Main.tile[i, j].TileFrameY / 18 % tileY; int tileXX18 = 18 * tileX; for (int l = x; l < x + tileX; l++) { for (int m = y; m < y + tileY; m++) { if (Main.tile[l, m].HasTile && Main.tile[l, m].TileType == type) { if (Main.tile[l, m].TileFrameX < tileXX18) Main.tile[l, m].TileFrameX += (short)(tileXX18); else Main.tile[l, m].TileFrameX -= (short)(tileXX18); } } } if (Wiring.running) { for (int k = 0; k < tileX; k++) { for (int l = 0; l < tileY; l++) Wiring.SkipWire(x + k, y + l); } } } public static void DrawFlameEffect(Texture2D flameTexture, int i, int j, int offsetX = 0, int offsetY = 0) { Tile tile = Main.tile[i, j]; Vector2 zero = Main.drawToScreen ? Vector2.Zero : new Vector2(Main.offScreenRange, Main.offScreenRange); int width = 16; int height = 16; int yOffset = TileObjectData.GetTileData(tile).DrawYOffset; ulong num190 = Main.TileFrameSeed ^ (ulong)((long)j << 32 | (long)(uint)i); float drawPositionX = i * 16 - (int)Main.screenPosition.X - (width - 16f) / 2f; float drawPositionY = j * 16 - (int)Main.screenPosition.Y; for (int c = 0; c < 7; c++) { float shakeX = Utils.RandomInt(ref num190, -10, 11) * 0.15f; float shakeY = Utils.RandomInt(ref num190, -10, 1) * 0.35f; Main.spriteBatch.Draw(flameTexture, new Vector2(drawPositionX + shakeX, drawPositionY + shakeY + yOffset) + zero, new Rectangle(tile.TileFrameX + offsetX, tile.TileFrameY + offsetY, width, height), new Color(100, 100, 100, 0), 0f, default(Vector2), 1f, SpriteEffects.None, 0f); } } public static void DrawStaticFlameEffect(Texture2D flameTexture, int i, int j, int offsetX = 0, int offsetY = 0) { int xPos = Main.tile[i, j].TileFrameX; int yPos = Main.tile[i, j].TileFrameY; Color drawColour = new Color(100, 100, 100, 0); Vector2 zero = Main.drawToScreen ? Vector2.Zero : new Vector2(Main.offScreenRange); Vector2 drawOffset = new Vector2(i * 16 - Main.screenPosition.X, j * 16 - Main.screenPosition.Y) + zero; for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { Vector2 flameOffset = new Vector2(x, y).SafeNormalize(Vector2.Zero); flameOffset *= 1.5f; Main.spriteBatch.Draw(flameTexture, drawOffset + flameOffset, new Rectangle?(new Rectangle(xPos + offsetX, yPos + offsetY, 18, 18)), drawColour, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0f); } } } public static void DrawFlameSparks(int dustType, int rarity, int i, int j) { if (!Main.gamePaused && Main.instance.IsActive && (!Lighting.UpdateEveryFrame || Main.rand.NextBool(4))) { if (Main.rand.NextBool(rarity)) { int dust = Dust.NewDust(new Vector2(i * 16 + 4, j * 16 + 2), 4, 4, dustType, 0f, 0f, 100, default, 1f); if (Main.rand.Next(3) != 0) Main.dust[dust].noGravity = true; // Prevent lag. Main.dust[dust].noLightEmittence = true; Main.dust[dust].velocity *= 0.3f; Main.dust[dust].velocity.Y = Main.dust[dust].velocity.Y - 1.5f; } } } public static void DrawItemFlame(Texture2D flameTexture, Item item) { int width = flameTexture.Width; int height = flameTexture.Height; float drawPositionX = item.position.X - Main.screenPosition.X + item.width * 0.5f; float drawPositionY = item.position.Y - Main.screenPosition.Y + item.height - flameTexture.Height * 0.5f + 2f; for (int c = 0; c < 7; c++) { float shakeX = Main.rand.Next(-10, 11) * 0.15f; float shakeY = Main.rand.Next(-10, 1) * 0.35f; Main.spriteBatch.Draw(flameTexture, new Vector2(drawPositionX + shakeX, drawPositionY + shakeY), new Rectangle(0, 0, width, height), new Color(100, 100, 100, 0), 0f, default, 1f, SpriteEffects.None, 0f); } } /// /// Generates an framing offset for individual tiles to be animated off-sync. DOES NOT WORK WITHOUT EVEN ANIMATIONS!!! (e.g. Flak Hermit Cages don't work with this.) /// /// The ModTile which is being initialized. /// X position of the tile. /// Y position of the tile. /// The number of frames the tile has. /// The number of pixels of one tile of the furniture in the X direction (ImageHeight / frameAmt / xTiles). /// The number of pixels of one tile of the furniture in the Y direction (ImageWidth / frameAmt / yTiles). /// The number of tiles wide the furniture is. /// The number of tiles tall the furniture is. /// This is animationFrameHeight in vertical animated tiles and animationFrameWidth in horizontal animated tiles. /// The offset for the animation. This is set to frameXOffset in horizontal animations and frameYOffset in vertical animations in the AnimateIndividualTile() function. internal static int GetAnimationOffset(this ModTile mt, int i, int j, int frameAmt, int xLength, int yLength, int xTiles, int yTiles, int animationFrameLength) { int frameX = Main.tile[i, j].TileFrameX; int frameY = Main.tile[i, j].TileFrameY; // Tweak the frame drawn so tiles next to each other are off-sync and look much more interesting. frameX %= (xLength * xTiles); i -= frameX / xLength; frameY %= (yLength * yTiles); j -= frameY / yLength; int uniqueAnimationFrame = Main.tileFrame[mt.Type] + j; if (i % 2 == 0) uniqueAnimationFrame += 3; if (i % 3 == 0) uniqueAnimationFrame += 3; if (i % 4 == 0) uniqueAnimationFrame += 3; if (j % 2 == 0) uniqueAnimationFrame += 3; if (j % 3 == 0) uniqueAnimationFrame += 3; if (j % 4 == 0) uniqueAnimationFrame += 3; uniqueAnimationFrame %= frameAmt; return uniqueAnimationFrame * animationFrameLength; } public static Tile ParanoidTileRetrieval(int x, int y) { if (!WorldGen.InWorld(x, y)) return new Tile(); return Main.tile[x, y]; } public static bool AnySolidTileInSelection(int x, int y, int width, int height) { for (int i = x; i != x + width; i += Math.Sign(width)) { for (int j = y; j != y + height; j += Math.Sign(height)) { if (WorldGen.InWorld(i, j)) continue; if (WorldGen.SolidTile(Framing.GetTileSafely(i, j))) return true; } } return false; } public static bool TileSelectionSolid(int x, int y, int width, int height) { for (int i = x; i != x + width; i += Math.Sign(width)) { for (int j = y; y != y + height; j += Math.Sign(height)) { if (!WorldGen.InWorld(i, j)) return false; if (!WorldGen.SolidTile(Framing.GetTileSafely(i, j))) return false; } } return true; } public static bool TileSelectionSolidSquare(int x, int y, int width, int height) { for (int i = x - width; i != x + width; i += Math.Sign(width)) { for (int j = y - height; y != y + height; j += Math.Sign(height)) { if (!WorldGen.InWorld(i, j)) return false; if (!WorldGen.SolidTile(Framing.GetTileSafely(i, j))) return false; } } return true; } public static bool TileActiveAndOfType(int x, int y, int type) { return ParanoidTileRetrieval(x, y).HasTile && ParanoidTileRetrieval(x, y).TileType == type; } /// /// Sets the mergeability state of two tiles. By default, enables tile merging. /// /// The first tile type which should merge (or not). /// The second tile type which should merge (or not). /// The mergeability state of the tiles. Defaults to true if omitted. public static void SetMerge(int type1, int type2, bool merge = true) { if (type1 != type2) { Main.tileMerge[type1][type2] = merge; Main.tileMerge[type2][type1] = merge; } } /// /// Makes the first tile type argument merge with all the other tile type arguments. Also accepts arrays. /// /// The tile whose merging properties will be set. /// Every tile that should be merged with. public static void MergeWithSet(int myType, params int[] otherTypes) { for (int i = 0; i < otherTypes.Length; ++i) SetMerge(myType, otherTypes[i]); } /// /// Makes the specified tile merge with the most common types of tiles found in world generation.

/// Notably excludes Ice. ///
/// The tile whose merging properties will be set. public static void MergeWithGeneral(int type) => MergeWithSet(type, new int[] { // Soils TileID.Dirt, TileID.Mud, TileID.ClayBlock, // Stones TileID.Stone, TileID.Ebonstone, TileID.Crimstone, TileID.Pearlstone, // Sands TileID.Sand, TileID.Ebonsand, TileID.Crimsand, TileID.Pearlsand, // Snows TileID.SnowBlock, // Calamity Tiles TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), }); /// /// Makes the specified tile merge with all ores, vanilla and Calamity. Particularly useful for stone blocks. /// /// The tile whose merging properties will be set. public static void MergeWithOres(int type) => MergeWithSet(type, new int[] { // Vanilla Ores TileID.Copper, TileID.Tin, TileID.Iron, TileID.Lead, TileID.Silver, TileID.Tungsten, TileID.Gold, TileID.Platinum, TileID.Demonite, TileID.Crimtane, TileID.Cobalt, TileID.Palladium, TileID.Mythril, TileID.Orichalcum, TileID.Adamantite, TileID.Titanium, TileID.LunarOre, // Calamity Ores TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), }); /// /// Makes the specified tile merge with all types of desert tiles, including the Calamity Sunken Sea. /// /// The tile whose merging properties will be set. public static void MergeWithDesert(int type) => MergeWithSet(type, new int[] { // Sands TileID.Sand, TileID.Ebonsand, TileID.Crimsand, TileID.Pearlsand, // Hardened Sands TileID.HardenedSand, TileID.CorruptHardenedSand, TileID.CrimsonHardenedSand, TileID.HallowHardenedSand, // Sandstones TileID.Sandstone, TileID.CorruptSandstone, TileID.CrimsonSandstone, TileID.HallowSandstone, // Miscellaneous Desert Tiles TileID.FossilOre, TileID.DesertFossil, // Astral Desert TileType(), TileType(), TileType(), TileType(), // Sunken Sea TileType(), TileType(), TileType(), }); /// /// Makes the specified tile merge with all types of snow and ice tiles. /// /// The tile whose merging properties will be set. public static void MergeWithSnow(int type) => MergeWithSet(type, new int[] { // Snows TileID.SnowBlock, // Ices TileID.IceBlock, TileID.CorruptIce, TileID.FleshIce, TileID.HallowedIce, // Astral Snow TileType(), TileType(), TileType(), }); /// /// Makes the specified tile merge with all tiles which generate in hell. Does not include Infernal Suevite. /// /// The tile whose merging properties will be set. public static void MergeWithHell(int type) => MergeWithSet(type, new int[] { TileID.Ash, TileID.Hellstone, TileID.ObsidianBrick, TileID.HellstoneBrick, TileType(), TileType(), TileType(), TileType(), }); /// /// Makes the specified tile merge with all tiles which generate in the Abyss or the Sulphurous Sea. Includes Chaotic Ore. /// /// The tile whose merging properties will be set. public static void MergeWithAbyss(int type) => MergeWithSet(type, new int[] { // Sulphurous Sea TileType(), TileType(), TileType(), // Abyss TileType(), TileType(), TileType(), TileType(), TileType(), TileType(), }); /// /// Makes the tile merge with all the tile types that generate within various types of astral tiles /// /// public static void MergeAstralTiles(int type) { //Astral SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); //Astral Desert SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); //Astral Snow SetMerge(type, TileType()); SetMerge(type, TileType()); } /// /// Makes the tile merge with all the decorative 'smooth' tiles /// /// public static void MergeSmoothTiles(int type) { //Vanilla SetMerge(type, TileID.MarbleBlock); SetMerge(type, TileID.GraniteBlock); //Calam SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); } /// /// Makes the tile merge with other mergable decorative tiles /// /// public static void MergeDecorativeTiles(int type) { //Vanilla decor Main.tileBrick[type] = true; //Calam SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); SetMerge(type, TileType()); } /// /// Determines if a tile is solid ground based on whether it's active and not actuated or if the tile is solid in any way, including just the top. /// /// The tile to check. public static bool IsTileSolidGround(this Tile tile) => tile != null && tile.HasUnactuatedTile && (Main.tileSolid[tile.TileType] || Main.tileSolidTop[tile.TileType]); /// /// Determines if a tile is solid based on whether it's active and not actuated or if the tile is solid. This will not count platforms and other non-solid ground tiles /// /// The tile to check. public static bool IsTileSolid(this Tile tile) => tile != null && tile.HasUnactuatedTile && Main.tileSolid[tile.TileType] && !TileID.Sets.Platforms[tile.TileType]; /// /// Determines if a tile is "full" based on if the tile is solid. This will count platforms and actuated tiles but no other non-solid ground tiles. /// /// The tile to check. public static bool IsTileFull(this Tile tile) => tile != null && tile.HasTile && Main.tileSolid[tile.TileType]; /// /// Returns a random number between 0 and 1 that always remains the same based on the tile's coordinates. /// /// The tile position to grab the rng from /// An extra offset. Useful if you need multiple counts of rng for the same time public static float GetTileRNG(this Point tilePos, int shift = 0) => (float)(Math.Sin(tilePos.X * 17.07947 + shift * 36) + Math.Sin(tilePos.Y * 25.13274)) * 0.25f + 0.5f; /// /// Grabs the nearest tile point to the origin, in the specified direction /// /// /// /// public static Point GetNearestPointInDirection(this Point origin, float direction) { return origin + new Point((int)Math.Round(Math.Cos(direction)), (int)Math.Round(Math.Sin(direction))); } /// /// Just like Vector2.ToTileCoordinates, but also clamps the position to the tile grid. /// /// /// The tile coordinates public static Point ToSafeTileCoordinates(this Vector2 vec) { return new Point((int)MathHelper.Clamp((int)vec.X >> 4, 0, Main.maxTilesX), (int)MathHelper.Clamp((int)vec.Y >> 4, 0, Main.maxTilesY)); } /// /// Is a tile valid to be grappled onto /// A straight rip of the private method Projectile.AI_007_GrapplingHooks_CanTileBeLatchedOnTo() /// /// /// Wether or not the tile may be grappled onto public static bool CanTileBeLatchedOnTo(this Tile theTile, bool grappleOnTrees = false) => Main.tileSolid[theTile.TileType] | (theTile.TileType == 314) | (grappleOnTrees && TileID.Sets.IsATreeTrunk[theTile.TileType]) | (grappleOnTrees && theTile.TileType == 323); /// /// Gets the required pickaxe power of a tile, accounting for both the ModTile and the vanilla tile pick requirements /// /// /// The pickaxe power required to break a tile public static int GetRequiredPickPower(this Tile tile, int i, int j) { int pickReq = 0; if (Main.tileNoFail[tile.TileType]) return pickReq; ModTile moddedTile = TileLoader.GetTile(tile.TileType); //Getting the pickaxe requirement of a modded tile is shrimple. if (moddedTile != null) pickReq = moddedTile.MinPick; //Getting the pickaxe requirement of a vanilla tile is quite clamplicated //This was lifted from code in onyx excavator, which likely was lifted from vanilla. It might need 1.4 updating. else { switch (tile.TileType) { case TileID.Chlorophyte: pickReq = 200; break; case TileID.Ebonstone: case TileID.Crimstone: case TileID.Pearlstone: case TileID.Hellstone: pickReq = 65; break; case TileID.Obsidian: pickReq = 55; break; case TileID.Meteorite: pickReq = 50; break; case TileID.Demonite: case TileID.Crimtane: if (j > Main.worldSurface) pickReq = 55; break; case TileID.LihzahrdBrick: case TileID.LihzahrdAltar: pickReq = 210; break; case TileID.Cobalt: case TileID.Palladium: pickReq = 100; break; case TileID.Mythril: case TileID.Orichalcum: pickReq = 110; break; case TileID.Adamantite: case TileID.Titanium: pickReq = 150; break; default: break; } } if (Main.tileDungeon[tile.TileType] && j > Main.worldSurface) pickReq = 100; return pickReq; } /// /// Returns if a tile is safe to be mined in terms of it being "important" /// /// /// If abyss terrain blocks should be considered unsafe to mine /// public static bool ShouldBeMined(this Tile tile, bool ignoreAbyss = true) { List tileExcludeList = new List() { TileID.DemonAltar, TileID.ElderCrystalStand, TileID.LihzahrdAltar, TileID.Dressers, TileID.Containers }; if (ignoreAbyss) { tileExcludeList.Add(ModContent.TileType()); tileExcludeList.Add(ModContent.TileType()); tileExcludeList.Add(ModContent.TileType()); tileExcludeList.Add(ModContent.TileType()); } return !Main.tileContainer[tile.TileType] && !tileExcludeList.Contains(tile.TileType); } /// /// Gets the item drop of an ore tile /// /// /// The item a tile breaks when drops if it's an ore public static int GetOreItemID(this Tile tile) { int item = -1; // If it's not ore, then return if (!TileID.Sets.Ore[tile.TileType]) return item; ModTile moddedTile = TileLoader.GetTile(tile.TileType); //Getting the item drop of a modded tile is pretty easy. if (moddedTile != null) item = TileLoader.GetItemDropFromTypeAndStyle(moddedTile.Type); //There is no easy way for getting vanilla item drops :( else { switch (tile.TileType) { case TileID.LunarOre: item = ItemID.LunarOre; break; case TileID.Chlorophyte: item = ItemID.ChlorophyteOre; break; case TileID.Titanium: item = ItemID.TitaniumOre; break; case TileID.Adamantite: item = ItemID.AdamantiteOre; break; case TileID.Orichalcum: item = ItemID.OrichalcumOre; break; case TileID.Mythril: item = ItemID.MythrilOre; break; case TileID.Palladium: item = ItemID.PalladiumOre; break; case TileID.Cobalt: item = ItemID.CobaltOre; break; case TileID.Hellstone: item = ItemID.Hellstone; break; case TileID.Obsidian: item = ItemID.Obsidian; break; case TileID.Meteorite: item = ItemID.Meteorite; break; case TileID.Demonite: item = ItemID.DemoniteOre; break; case TileID.Crimtane: item = ItemID.CrimtaneOre; break; case TileID.Platinum: item = ItemID.PlatinumOre; break; case TileID.Gold: item = ItemID.GoldOre; break; case TileID.Tungsten: item = ItemID.TungstenOre; break; case TileID.Silver: item = ItemID.SilverOre; break; case TileID.Lead: item = ItemID.LeadOre; break; case TileID.Iron: item = ItemID.IronOre; break; case TileID.Tin: item = ItemID.TinOre; break; case TileID.Copper: item = ItemID.CopperOre; break; default: break; } } return item; } } }