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.FurnitureOccult;
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;
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 null check.
Tile tile = Main.tile[x, y];
if (tile is null)
return baseName;
int left = x;
int top = y;
if (tile.frameX % 36 != 0)
left--;
if (tile.frameY != 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)
{
if (Main.tile[x, y] is null)
return;
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].frameX / 18 % tileX;
int y = j - Main.tile[i, j].frameY / 18 % tileY;
for (int l = x; l < x + tileX; l++)
{
for (int m = y; m < y + tileY; m++)
{
if (Main.tile[l, m] == null)
{
Main.tile[l, m] = new Tile();
}
if (Main.tile[l, m].active() && Main.tile[l, m].type == type)
{
if (Main.tile[l, m].frameX < (18 * tileX))
{
Main.tile[l, m].frameX += (short)(18 * tileX);
}
else
{
Main.tile[l, m].frameX -= (short)(18 * tileX);
}
}
}
}
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);
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(i * 16 - (int)Main.screenPosition.X - (width - 16f) / 2f + shakeX, j * 16 - (int)Main.screenPosition.Y + shakeY + yOffset) + zero, new Rectangle(tile.frameX + offsetX, tile.frameY + 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].frameX;
int yPos = Main.tile[i, j].frameY;
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;
}
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;
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(item.position.X - Main.screenPosition.X + item.width * 0.5f + shakeX, item.position.Y - Main.screenPosition.Y + item.height - flameTexture.Height * 0.5f + 2f + 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].frameX;
int frameY = Main.tile[i, j].frameY;
// 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();
Tile tile = Main.tile[x, y];
if (tile is null)
{
tile = new Tile();
Main.tile[x, y] = tile;
}
return tile;
}
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).active() && ParanoidTileRetrieval(x, y).type == 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(),
});
///
/// 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 Charred Ore.
///
/// 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(),
});
///
/// 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(),
// Abyss
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.nactive() && (Main.tileSolid[tile.type] || Main.tileSolidTop[tile.type]);
}
}