Advertisement
InanZen

AutoBossSpawner.cs

Feb 13th, 2012
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.79 KB | None | 0 0
  1. using Terraria;
  2. using System;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using Hooks;
  6. using TShockAPI;
  7.  
  8. namespace AutoBossSpawner
  9. {
  10.     [APIVersion(1, 11)]
  11.     public class AutoBossSpawner : TerrariaPlugin
  12.     {
  13.         private DateTime LastCheck = DateTime.UtcNow;
  14.         private DateTime OtherLastCheck = DateTime.UtcNow;
  15.         public ABSconfig configObj { get; set; }
  16.         internal static string ABSconfigPath { get { return Path.Combine(TShock.SavePath, "ABSconfig.json"); } }
  17.         private TShockAPI.DB.Region arenaregion = new TShockAPI.DB.Region();
  18.         private int BossTimer = 30;
  19.         private List<Terraria.NPC> bossList = new List<Terraria.NPC>();
  20.         private bool BossToggle = false;
  21.         private int BossForce = -1;
  22.         Random rndGen = new Random();
  23.  
  24.  
  25.         public override string Name
  26.         {
  27.             get { return "Auto Boss Spawner"; }
  28.         }
  29.         public override string Author
  30.         {
  31.             get { return "by InanZen"; }
  32.         }
  33.         public override string Description
  34.         {
  35.             get { return "Auto spawn bosses every night"; }
  36.         }
  37.         public override Version Version
  38.         {
  39.             get { return new Version("1.3"); }
  40.         }
  41.         public override void Initialize()
  42.         {
  43.             GameHooks.Update += OnUpdate;
  44.             GameHooks.Initialize += OnInitialize;
  45.         }
  46.         protected override void Dispose(bool disposing)
  47.         {
  48.             if (disposing)
  49.             {
  50.                 GameHooks.Update -= OnUpdate;
  51.                 GameHooks.Initialize -= OnInitialize;
  52.             }
  53.             base.Dispose(disposing);
  54.         }
  55.         public AutoBossSpawner(Main game) : base(game)
  56.         {
  57.             Order = 5;
  58.         }
  59.         public void OnUpdate()
  60.         {
  61.             if (BossToggle && ((DateTime.UtcNow - LastCheck).TotalSeconds >= 1))
  62.             {
  63.                 LastCheck = DateTime.UtcNow;
  64.                 if (!Main.dayTime && BossTimer<601)
  65.                 {
  66.                     if (BossTimer == configObj.BossTimer)
  67.                     {
  68.                         if (configObj.BossText.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossText, Color.Aquamarine); }
  69.                     }
  70.                     else if (BossTimer == 10)
  71.                     {
  72.                         if (configObj.BossText10s.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossText10s, Color.Aquamarine); }
  73.                     }
  74.                     else if (BossTimer == 0)
  75.                     {
  76.                         if (configObj.BossText0s.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossText0s, Color.Aquamarine); }
  77.                         startBossBattle();                        
  78.                     }
  79.                     else if ((BossTimer < 0) && (BossTimer % configObj.MinionsTimer == 0))
  80.                     {
  81.                         bool bossActive = false;
  82.                         for (int i = 0; i < bossList.Count; i++)
  83.                         {
  84.                             if (bossList[i].active) bossActive = true;
  85.                         }
  86.                         if (bossActive) spawnMinions();
  87.                         else
  88.                         {
  89.                             if (configObj.BossDefeat.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossDefeat, Color.Aquamarine); }
  90.                             if (configObj.BossContinuous)
  91.                                 BossTimer = configObj.BossTimer + 1;
  92.                             else
  93.                                 BossTimer = 601;
  94.                             for (int i = 0; i < Main.npc.Length; i++)
  95.                             {
  96.                                 if (Main.npc[i].type == 70 || Main.npc[i].type == 72)
  97.                                 {
  98.                                     if (arenaregion.Area.Intersects(new Rectangle(((int)Main.npc[i].position.X) / 16, ((int)Main.npc[i].position.Y) / 16, 20, 20)))
  99.                                     {
  100.                                         TSPlayer.Server.StrikeNPC(i, 9999, 90f, 1);
  101.                                     }                                    
  102.                                 }
  103.                             }
  104.                         }
  105.                     }
  106.                     BossTimer--;                  
  107.                 }
  108.                 else if (BossTimer != configObj.BossTimer && Main.dayTime)
  109.                 {
  110.                     BossTimer = configObj.BossTimer;
  111.                     for (int i = 0; i < Main.npc.Length; i++)
  112.                     {
  113.                         if (Main.npc[i].type == 70 || Main.npc[i].type == 72)
  114.                         {
  115.                             if (arenaregion.Area.Intersects(new Rectangle(((int)Main.npc[i].position.X) / 16, ((int)Main.npc[i].position.Y) / 16, 20, 20)))
  116.                             {
  117.                                 TSPlayer.Server.StrikeNPC(i, 9999, 90f, 1);
  118.                             }
  119.                         }
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.  
  125.         private void startBossBattle()
  126.         {
  127.             NPC npc = new NPC();
  128.             arenaregion = TShock.Regions.GetRegionByName("arena");
  129.             int arenaX = arenaregion.Area.X + (arenaregion.Area.Width / 2);
  130.             int arenaY = arenaregion.Area.Y + (arenaregion.Area.Height / 2);
  131.             string broadcastString = "Boss selected:";
  132.             BossSet bossSet;
  133.             if (BossForce > -1)
  134.             {
  135.                 bossSet = getBossByID(BossForce);
  136.                 BossForce = -1;
  137.             }
  138.             else
  139.                 bossSet = configObj.BossList[rndGen.Next(0, configObj.BossList.Count)];
  140.             foreach (BossObj b in bossSet.bosses)
  141.             {
  142.                 npc = TShockAPI.TShock.Utils.GetNPCById(b.id);
  143.                 TSPlayer.Server.SpawnNPC(npc.type, npc.name, b.amt, arenaX, arenaY, (arenaregion.Area.Width / 2), (arenaregion.Area.Height / 2));
  144.                 broadcastString += " " + b.amt + "x " + npc.name + " +";
  145.             }
  146.             broadcastString = broadcastString.Remove(broadcastString.Length - 2);
  147.             TShockAPI.TShock.Utils.Broadcast(broadcastString, Color.Aquamarine);
  148.             bossList = new List<Terraria.NPC>();
  149.             for (int i = 0; i < Main.npc.Length; i++)
  150.             {
  151.                 if (Main.npc[i].boss && Main.npc[i].active && Main.npc[i].type != 113) bossList.Add(Main.npc[i]);
  152.             }
  153.         }
  154.         private void spawnMinions()
  155.         {
  156.             //TODO: num of henchmen based on players in arena, spawn life when boss is at half health.
  157.             NPC npc = new NPC();
  158.             npc = TShockAPI.TShock.Utils.GetNPCById(configObj.MinionsList[rndGen.Next(0, configObj.MinionsList.Length)]);
  159.             arenaregion = TShock.Regions.GetRegionByName("arena");
  160.             int arenaX = arenaregion.Area.X + (arenaregion.Area.Width / 2);
  161.             int arenaY = arenaregion.Area.Y + (arenaregion.Area.Height / 2);
  162.             int henchmenNumber = rndGen.Next(configObj.MinionsMinMax[0], configObj.MinionsMinMax[1] + 1);
  163.             TSPlayer.Server.SpawnNPC(npc.type, npc.name, henchmenNumber, arenaX, arenaY, (arenaregion.Area.Width / 2), (arenaregion.Area.Height / 2));
  164.             if (configObj.MinionsAnnounce) { TShockAPI.TShock.Utils.Broadcast("Spawning Boss Minions: " + henchmenNumber + "x " + npc.name + "!", Color.SteelBlue); }
  165.         }
  166.        
  167.         #region Commands
  168.         public void OnInitialize()
  169.         {
  170.             configObj = new ABSconfig();
  171.             SetupConfig();
  172.  
  173.             Commands.ChatCommands.Add(new Command("autoboss", ABScommand, "abs"));
  174.         }
  175.         public BossSet getBossByID(int id)
  176.         {
  177.             foreach (BossSet bossSet in configObj.BossList)
  178.             {
  179.                 if (bossSet.setID == id) return bossSet;
  180.             }
  181.             return configObj.BossList[0];
  182.         }
  183.  
  184.         public void ABScommand(CommandArgs args)
  185.         {
  186.             string cmd = "";
  187.             if (args.Parameters.Count > 0)
  188.             {
  189.                 cmd = args.Parameters[0].ToLower();
  190.             }
  191.             switch (cmd)
  192.             {
  193.                 case "toggle":
  194.                     {
  195.                         BossToggle = !BossToggle;
  196.                         if (BossToggle == true)
  197.                         {
  198.                             foreach (TShockAPI.DB.Region reg in TShock.Regions.ListAllRegions(Main.worldID.ToString()))
  199.                             {
  200.                                 if (reg.Name == "arena") { arenaregion = reg; }
  201.                             }
  202.                             if (arenaregion.Name != "arena") { TShockAPI.TShock.Utils.Broadcast("Error: Region 'arena' is not defined.", Color.Red); BossToggle = false; }
  203.                         }
  204.                         args.Player.SendMessage("Boss battles now: " + ((BossToggle) ? "Enabled" : "Disabled"), Color.Aquamarine);
  205.                         BossTimer = configObj.BossTimer;
  206.                         break;
  207.                     }
  208.                 case "reload":
  209.                     {
  210.                         SetupConfig();
  211.                         args.Player.SendMessage("AutoBossSpawner config reloaded.", Color.Aquamarine);
  212.                         break;
  213.                     }
  214.                 case "force":
  215.                     {
  216.                         if (args.Parameters.Count > 1)
  217.                         {
  218.                             int forceID = -1;
  219.                             int.TryParse(args.Parameters[1], out forceID);
  220.                             if (forceID > -1)
  221.                             {
  222.                                 args.Player.SendMessage("Forcing BossSet " + forceID, Color.Aquamarine);
  223.                                 BossForce = forceID;
  224.                                 BossTimer = 1;
  225.                                 if (Main.dayTime)                              
  226.                                     TSPlayer.Server.SetTime(false, 0.0);                                
  227.                             }
  228.                             else
  229.                                 args.Player.SendMessage("Invalid syntax! Use: /abs force SetID", Color.Red);
  230.                         }
  231.                         else
  232.                             args.Player.SendMessage("Invalid syntax! Use: /abs force SetID", Color.Red);
  233.                         break;
  234.                     }
  235.                 default:
  236.                     {
  237.                         args.Player.SendMessage("Available commands:", Color.Aquamarine);
  238.                         args.Player.SendMessage("/abs toggle - enables/disables automatic boss battles", Color.Aquamarine);
  239.                         args.Player.SendMessage("/abs force bossID - forces the start of bossID battle:", Color.Aquamarine);
  240.                         args.Player.SendMessage("/abs reload - reloads the ABSconfig.json", Color.Aquamarine);
  241.                         break;
  242.                     }
  243.             }
  244.         }
  245.      
  246.         #endregion
  247.         public void SetupConfig()
  248.         {
  249.             try
  250.             {
  251.                 if (File.Exists(ABSconfigPath))
  252.                 {
  253.                     configObj = new ABSconfig();
  254.                     configObj = ABSconfig.Read(ABSconfigPath);
  255.                     BossTimer = configObj.BossTimer;
  256.                     BossToggle = configObj.BossStartEnabled;
  257.                 }
  258.                 else { configObj.Write(ABSconfigPath); }
  259.             }
  260.             catch (Exception ex)
  261.             {
  262.                 Console.ForegroundColor = ConsoleColor.Red;
  263.                 Console.WriteLine("Error in config file");
  264.                 Console.ForegroundColor = ConsoleColor.Gray;
  265.                 Log.Error("Config Exception");
  266.                 Log.Error(ex.ToString());
  267.             }
  268.         }
  269.     }
  270.        
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement