-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoBossSpawner.cs
More file actions
201 lines (192 loc) · 8.08 KB
/
AutoBossSpawner.cs
File metadata and controls
201 lines (192 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using Terraria;
using System;
using System.IO;
using System.Collections.Generic;
using Hooks;
using TShockAPI;
namespace AutoBossSpawner
{
[APIVersion(1, 11)]
public class AutoBossSpawner : TerrariaPlugin
{
private DateTime LastCheck = DateTime.UtcNow;
private DateTime OtherLastCheck = DateTime.UtcNow;
public ABSconfig configObj { get; set; }
internal static string ABSconfigPath { get { return Path.Combine(TShock.SavePath, "ABSconfig.json"); } }
private TShockAPI.DB.Region arenaregion = new TShockAPI.DB.Region();
private int BossTimer = 30;
private List<Terraria.NPC> bossList = new List<Terraria.NPC>();
private bool BossToggle = false;
Random rndGen = new Random();
public override string Name
{
get { return "Auto Boss Spawner"; }
}
public override string Author
{
get { return "by InanZen"; }
}
public override string Description
{
get { return "Auto spawn bosses every night"; }
}
public override Version Version
{
get { return new Version("1.2"); }
}
public override void Initialize()
{
GameHooks.Update += OnUpdate;
GameHooks.Initialize += OnInitialize;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
GameHooks.Update -= OnUpdate;
GameHooks.Initialize -= OnInitialize;
}
base.Dispose(disposing);
}
public AutoBossSpawner(Main game) : base(game)
{
Order = 1;
}
public void OnUpdate()
{
if (BossToggle && ((DateTime.UtcNow - LastCheck).TotalSeconds >= 1))
{
LastCheck = DateTime.UtcNow;
if (!Main.dayTime && BossTimer<601)
{
if (BossTimer == configObj.BossTimer)
{
if (configObj.BossText.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossText, Color.Aquamarine); }
}
else if (BossTimer == 10)
{
if (configObj.BossText10s.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossText10s, Color.Aquamarine); }
}
else if (BossTimer == 0)
{
if (configObj.BossText0s.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossText0s, Color.Aquamarine); }
startBossBattle();
}
else if ((BossTimer < 0) && (BossTimer % 20 == 0))
{
bool bossActive = false;
for (int i = 0; i < bossList.Count; i++)
{
if (bossList[i].active) bossActive = true;
}
if (bossActive) spawnMinions();
else
{
if (configObj.BossDefeat.Length > 1) { TShockAPI.TShock.Utils.Broadcast(configObj.BossDefeat, Color.Aquamarine); }
BossTimer = 601;
for (int i = 0; i < Main.npc.Length; i++)
{
if (Main.npc[i].active && (Main.npc[i].type == 70 || Main.npc[i].type == 72))
{
TSPlayer.Server.StrikeNPC(i, 9999, 90f, 1);
}
}
}
}
BossTimer--;
}
else if (BossTimer != configObj.BossTimer && Main.dayTime)
{
BossTimer = configObj.BossTimer;
}
}
}
private void startBossBattle()
{
NPC npc = new NPC();
arenaregion = TShock.Regions.GetRegionByName("arena");
int arenaX = arenaregion.Area.X + (arenaregion.Area.Width / 2);
int arenaY = arenaregion.Area.Y + (arenaregion.Area.Height / 2);
string broadcastString = "Boss selected:";
BossSet bossSet = configObj.BossList[rndGen.Next(0, configObj.BossList.Count)];
foreach (BossObj b in bossSet.bosses)
{
npc = TShockAPI.TShock.Utils.GetNPCById(b.id);
TSPlayer.Server.SpawnNPC(npc.type, npc.name, b.amt, arenaX, arenaY, 30, 30);
broadcastString += " " + b.amt + "x " + npc.name + " +";
}
broadcastString = broadcastString.Remove(broadcastString.Length - 2);
TShockAPI.TShock.Utils.Broadcast(broadcastString, Color.Aquamarine);
for (int i = 0; i < Main.npc.Length; i++)
{
if (Main.npc[i].boss) bossList.Add(Main.npc[i]);
}
}
private void spawnMinions()
{
//TODO: num of henchmen based on players in arena, spawn life when boss is at half health.
NPC npc = new NPC();
npc = TShockAPI.TShock.Utils.GetNPCById(configObj.MinionsList[rndGen.Next(0, configObj.MinionsList.Length)]);
arenaregion = TShock.Regions.GetRegionByName("arena");
int arenaX = arenaregion.Area.X + (arenaregion.Area.Width / 2);
int arenaY = arenaregion.Area.Y + (arenaregion.Area.Height / 2);
int henchmenNumber = rndGen.Next(configObj.MinionsMinMax[0], configObj.MinionsMinMax[1] + 1);
TSPlayer.Server.SpawnNPC(npc.type, npc.name, henchmenNumber, arenaX, arenaY, 30, 30);
if (configObj.MinionsAnnounce) { TShockAPI.TShock.Utils.Broadcast("Spawning Boss Minions: " + henchmenNumber + "x " + npc.name + "!", Color.SteelBlue); }
}
#region Commands
public void OnInitialize()
{
configObj = new ABSconfig();
SetupConfig();
Commands.ChatCommands.Add(new Command("autoboss", ABStoggle, "abstoggle"));
Commands.ChatCommands.Add(new Command("autoboss", ABSreload, "absreload"));
//Commands.ChatCommands.Add(new Command("autoboss", ABSdebug, "absdebug"));
}
public void ABStoggle(CommandArgs args)
{
BossToggle = !BossToggle;
if (BossToggle == true)
{
foreach (TShockAPI.DB.Region reg in TShock.Regions.ListAllRegions(Main.worldID.ToString()))
{
if (reg.Name == "arena") { arenaregion = reg; }
}
if (arenaregion.Name != "arena") { TShockAPI.TShock.Utils.Broadcast("Error: Region 'arena' is not defined.", Color.Red); BossToggle = false; }
}
args.Player.SendMessage("Boss battles now: "+ ((BossToggle)?"Enabled":"Disabled"));
BossTimer = configObj.BossTimer;
}
public void ABSreload(CommandArgs args)
{
SetupConfig();
args.Player.SendMessage("AutoBossSpawner config reloaded.");
}
/* public void ABSdebug(CommandArgs args)
{
args.Player.SendMessage("arena x: " + arenaregion.Area.X);
}*/
#endregion
public void SetupConfig()
{
try
{
if (File.Exists(ABSconfigPath))
{
configObj = new ABSconfig();
configObj = ABSconfig.Read(ABSconfigPath);
BossTimer = configObj.BossTimer;
}
else { configObj.Write(ABSconfigPath); }
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error in config file");
Console.ForegroundColor = ConsoleColor.Gray;
Log.Error("Config Exception");
Log.Error(ex.ToString());
}
}
}
}