Files
CalamityModPublic/Tiles/CalamityGlobalTile.cs
T

313 lines
13 KiB
C#

using CalamityMod.Events;
using CalamityMod.Items.VanillaArmorChanges;
using CalamityMod.Items.Weapons.Melee;
using CalamityMod.Tiles.Abyss;
using CalamityMod.Tiles.Astral;
using CalamityMod.Tiles.AstralDesert;
using CalamityMod.Tiles.DraedonStructures;
using CalamityMod.Tiles.DraedonSummoner;
using CalamityMod.Tiles.Furniture.CraftingStations;
using CalamityMod.Tiles.SunkenSea;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Tiles
{
public class CalamityGlobalTile : GlobalTile
{
internal static readonly MethodInfo ActiveFountainColorMethod = typeof(SceneMetrics).GetMethod("set_ActiveFountainColor", BindingFlags.NonPublic | BindingFlags.Instance);
public static void SetActiveFountainColor(int fountainID)
{
ActiveFountainColorMethod.Invoke(Main.SceneMetrics, new object[]
{
fountainID
});
}
public static ushort[] PlantTypes = new ushort[]
{
TileID.Plants,
TileID.CorruptPlants,
TileID.JunglePlants,
TileID.MushroomPlants,
TileID.Plants2,
TileID.JunglePlants2,
TileID.HallowedPlants,
TileID.HallowedPlants2,
TileID.CrimsonPlants,
(ushort)ModContent.TileType<AstralShortPlants>(),
(ushort)ModContent.TileType<AstralTallPlants>()
};
public static List<int> GrowthTiles = new List<int>()
{
ModContent.TileType<SeaPrism>(),
ModContent.TileType<Navystone>(),
ModContent.TileType<Voidstone>()
};
public override void SetStaticDefaults()
{
Main.tileSpelunker[TileID.LunarOre] = true;
Main.tileOreFinderPriority[TileID.LunarOre] = 900;
}
public override bool PreHitWire(int i, int j, int type)
{
return !BossRushEvent.BossRushActive;
}
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])
{
TileFraming.PlantFrame(i, j);
return false;
}
}
// Custom vine framing
if (type == TileID.Vines || type == TileID.CrimsonVines || type == TileID.HallowedVines || type == ModContent.TileType<AstralVines>())
TileFraming.VineFrame(i, j);
return base.TileFrame(i, j, type, ref resetFrame, ref noBreak);
}
public override void PostDraw(int i, int j, int type, SpriteBatch spriteBatch)
{
// Guaranteed not null at this point
Tile tile = Main.tile[i, j];
// This function is only for Astral Cactus. If the tile isn't even cactus, forget about it.
if (type != TileID.Cactus)
return;
Vector2 zero = new Vector2(Main.offScreenRange, Main.offScreenRange);
if (Main.drawToScreen)
zero = Vector2.Zero;
int frameX = tile.TileFrameX;
int frameY = tile.TileFrameY;
// Search down the cactus to find out whether the block it is planted in is Astral Sand.
int xTile = i;
if (frameX == 36) // Cactus segment which splits left
xTile--;
if (frameX == 54) // Cactus segment which splits right
xTile++;
if (frameX == 108) // Cactus segment which splits both directions
xTile += (frameY == 18) ? -1 : 1;
int yTile = j;
bool slidingDownCactus = Main.tile[xTile, yTile] != null && Main.tile[xTile, yTile].TileType == TileID.Cactus && Main.tile[xTile, yTile].HasTile;
while (!Main.tile[xTile, yTile].HasTile || !Main.tileSolid[Main.tile[xTile, yTile].TileType] || !slidingDownCactus)
{
if (Main.tile[xTile, yTile].TileType == TileID.Cactus && Main.tile[xTile, yTile].HasTile)
{
slidingDownCactus = true;
}
yTile++;
// Cacti are assumed to be no more than 20 blocks tall.
if (yTile > i + 20)
break;
}
bool astralCactus = Main.tile[xTile, yTile].TileType == (ushort)ModContent.TileType<AstralSand>();
// If it is actually astral cactus, then draw its glowmask.
if (astralCactus)
{
spriteBatch.Draw(ModContent.Request<Texture2D>("CalamityMod/Tiles/AstralDesert/AstralCactusGlow").Value, new Vector2((float)(i * 16 - (int)Main.screenPosition.X), (float)(j * 16 - (int)Main.screenPosition.Y)) + zero, new Rectangle((int)frameX, (int)frameY, 16, 18), Color.White * 0.75f, 0f, default, 1f, SpriteEffects.None, 0f);
}
}
// This function has two purposes:
// 1 - Shatters adjacent Lumenyl or Sea Prism crystals when a neighboring solid tile is destroyed
// 2 - Gives the player breath back when breaking blocks with Reaver set bonus
public override void KillTile(int i, int j, int type, ref bool fail, ref bool effectOnly, ref bool noItem)
{
Tile tile = Main.tile[i, j];
// Helper function to shatter crystals attached to neighboring solid tiles.
static void CheckShatterCrystal(int xPos, int yPos)
{
if (xPos < 0 || xPos >= Main.maxTilesX || yPos < 0 || yPos >= Main.maxTilesY)
return;
Tile t = Main.tile[xPos, yPos];
if (t.HasTile && (t.TileType == ModContent.TileType<LumenylCrystals>() || t.TileType == ModContent.TileType<SeaPrismCrystals>()))
{
WorldGen.KillTile(xPos, yPos, false, false, false);
if (!Main.tile[xPos, yPos].HasTile && Main.netMode != NetmodeID.SinglePlayer)
NetMessage.SendData(MessageID.TileManipulation, -1, -1, null, 0, xPos, yPos, 0f, 0, 0, 0);
}
}
// CONSIDER -- Lumenyl Crystals and Sea Prism Crystals aren't solid. They shouldn't need to be checked here.
if (Main.tileSolid[tile.TileType] && tile.TileType != ModContent.TileType<LumenylCrystals>() && tile.TileType != ModContent.TileType<SeaPrismCrystals>())
{
CheckShatterCrystal(i + 1, j);
CheckShatterCrystal(i - 1, j);
CheckShatterCrystal(i, j + 1);
CheckShatterCrystal(i, j - 1);
}
// Cumbling Dungeon Bricks have a 100% chance to crumble. This causes an effect similar to the Vein Miner mod.
if (Main.netMode != NetmodeID.MultiplayerClient && tile.TileType >= TileID.CrackedBlueDungeonBrick && tile.TileType <= TileID.CrackedPinkDungeonBrick)
{
for (int m = 0; m < 8; m++)
{
int x = i;
int y = j;
switch (m)
{
case 0:
x--;
break;
case 1:
x++;
break;
case 2:
y--;
break;
case 3:
y++;
break;
case 4:
x--;
y--;
break;
case 5:
x++;
y--;
break;
case 6:
x--;
y++;
break;
case 7:
x++;
y++;
break;
}
Tile tile3 = Main.tile[x, y];
if (tile3.HasTile && tile3.TileType >= TileID.CrackedBlueDungeonBrick && tile3.TileType <= TileID.CrackedPinkDungeonBrick)
{
tile.Get<TileWallWireStateData>().HasTile = false;
WorldGen.KillTile(x, y, fail: false, effectOnly: false, noItem: true);
if (Main.netMode == NetmodeID.Server)
NetMessage.TrySendData(17, -1, -1, null, 20, x, y);
}
}
int projectileType = tile.TileType - TileID.CrackedBlueDungeonBrick + ProjectileID.BlueDungeonDebris;
int damage = 20;
if (Main.netMode == NetmodeID.SinglePlayer)
{
Projectile.NewProjectile(new EntitySource_TileBreak(i, j), i * 16 + 8, j * 16 + 8, 0f, 0.41f, projectileType, damage, 0f, Main.myPlayer);
}
else if (Main.netMode == NetmodeID.Server)
{
int proj = Projectile.NewProjectile(new EntitySource_TileBreak(i, j), i * 16 + 8, j * 16 + 8, 0f, 0.41f, projectileType, damage, 0f, Main.myPlayer);
Main.projectile[proj].netUpdate = true;
}
}
Player player = Main.LocalPlayer;
if (player is null || !player.active)
return;
if (player.Calamity().reaverExplore && !fail)
{
player.breath += 20;
if (player.breath > player.breathMax)
player.breath = player.breathMax;
}
// Mining set gives a chance for additional ore. This can be abused for infinite ore but it has a cooldown to prevent too much abuse
if (player.Calamity().miningSet && player.Calamity().miningSetCooldown <= 0 && !fail)
{
int item = tile.GetOreItemID();
Vector2 pos = new Vector2(i, j) * 16;
// 25% chance for additional ore
if (Main.rand.NextBool(MiningArmorSetChange.BonusOreChance) && item != -1)
{
Item.NewItem(new EntitySource_TileBreak(i, j), pos, item);
// Cooldown varies between 3 and 6 seconds
player.Calamity().miningSetCooldown = Main.rand.Next(MiningArmorSetChange.CooldownMin, MiningArmorSetChange.CooldownMax + 1);
}
}
}
public override bool Drop(int i, int j, int type)
{
Tile tileAtPosition = CalamityUtils.ParanoidTileRetrieval(i, j);
if (tileAtPosition.TileFrameX % 36 == 0 && tileAtPosition.TileFrameY % 36 == 0)
{
if (type == TileID.DemonAltar && Main.hardMode)
{
Vector2 pos = new Vector2(i, j) * 16;
if (CalamityConfig.Instance.EarlyHardmodeProgressionRework)
{
WorldGen.altarCount++;
int quantity = 4;
for (int k = 0; k < quantity; k += 1)
{
pos.X += Main.rand.NextFloat(-32f, 32f);
pos.Y += Main.rand.NextFloat(-32f, 32f);
Item.NewItem(new EntitySource_TileBreak(i, j), pos, ItemID.SoulofNight);
}
}
if (WorldGen.altarCount % 12 == 0 && WorldGen.altarCount > 1)
Item.NewItem(new EntitySource_TileBreak(i, j), pos, ModContent.ItemType<EvilSmasher>());
}
}
return true;
}
public static bool ShouldNotBreakDueToAboveTile(int x, int y)
{
int[] invincibleTiles = new int[]
{
ModContent.TileType<DraedonLabTurret>(),
ModContent.TileType<AstralBeacon>(),
ModContent.TileType<CodebreakerTile>(),
ModContent.TileType<SCalAltar>()
};
Tile checkTile = CalamityUtils.ParanoidTileRetrieval(x, y);
Tile aboveTile = CalamityUtils.ParanoidTileRetrieval(x, y - 1);
// Prevent tiles below invincible tiles from being destroyed. This is like chests in vanilla.
return aboveTile.HasTile && checkTile.TileType != aboveTile.TileType && invincibleTiles.Contains(aboveTile.TileType);
}
public override bool CanExplode(int i, int j, int type)
{
if (ShouldNotBreakDueToAboveTile(i, j))
return false;
return base.CanExplode(i, j, type);
}
public override bool CanKillTile(int i, int j, int type, ref bool blockDamaged)
{
if (ShouldNotBreakDueToAboveTile(i, j))
return false;
return base.CanKillTile(i, j, type, ref blockDamaged);
}
}
}