-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNPCAssist.cs
346 lines (301 loc) · 14.6 KB
/
NPCAssist.cs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace BossAssist
{
class NPCAssist : GlobalNPC
{
public override void NPCLoot(NPC npc)
{
if (npc.type == NPCID.DD2Betsy)
{
WorldAssist.downedBetsy = true;
if (Main.netMode == NetmodeID.Server)
{
NetMessage.SendData(MessageID.WorldData); // Immediately inform clients of new world state.
}
}
string partName = npc.GetFullNetName().ToString();
if (BossAssist.ClientConfig.PillarMessages)
{
if (npc.type == NPCID.LunarTowerSolar || npc.type == NPCID.LunarTowerVortex || npc.type == NPCID.LunarTowerNebula || npc.type == NPCID.LunarTowerStardust)
{
if (Main.netMode == 0) Main.NewText("The " + npc.GetFullNetName().ToString() + " has been destroyed", Colors.RarityPurple);
else NetMessage.BroadcastChatMessage(NetworkText.FromLiteral("The " + npc.GetFullNetName().ToString() + " has been destroyed"), Colors.RarityPurple);
}
}
if (NPCisLimb(npc) && BossAssist.ClientConfig.LimbMessages)
{
if (npc.type == NPCID.SkeletronHand) partName = "Skeletron Hand";
if (Main.netMode == 0) Main.NewText("The " + partName + " is down!", Colors.RarityGreen);
else NetMessage.BroadcastChatMessage(NetworkText.FromLiteral("The " + npc.FullName + " is down!"), Colors.RarityGreen);
}
// Setting a record for fastest boss kill, and counting boss kills
// Twins check makes sure the other is not around before counting towards the record
if (SpecialBossCheck(npc) != -1) // Requires the player to participate in the boss fight
{
if (EaterOfWorldsCheck(npc))
{
if (Main.netMode == NetmodeID.SinglePlayer && npc.playerInteraction[Main.myPlayer])
{
Player player = Main.player[Main.myPlayer];
CheckRecords(npc, player, player.GetModPlayer<PlayerAssist>());
}
else
{
CheckRecordsMultiplayer(npc);
}
}
}
}
public void CheckRecords(NPC npc, Player player, PlayerAssist modplayer)
{
int recordIndex = SpecialBossCheck(npc);
int recordAttempt = modplayer.RecordTimers[recordIndex]; // Trying to set a new record
int currentRecord = modplayer.AllBossRecords[recordIndex].stat.fightTime;
int worstRecord = modplayer.AllBossRecords[recordIndex].stat.fightTime2;
modplayer.AllBossRecords[recordIndex].stat.fightTimeL = recordAttempt;
int brinkAttempt = modplayer.BrinkChecker[recordIndex]; // Trying to set a new record
int MaxLife = modplayer.MaxHealth[recordIndex];
int currentBrink = modplayer.AllBossRecords[recordIndex].stat.brink2;
int worstBrink = modplayer.AllBossRecords[recordIndex].stat.brink;
modplayer.AllBossRecords[recordIndex].stat.brinkL = brinkAttempt;
double lastHealth = (double)brinkAttempt / (double)MaxLife;
modplayer.AllBossRecords[recordIndex].stat.brinkPercentL = (int)(lastHealth * 100);
int dodgeTimeAttempt = modplayer.DodgeTimer[recordIndex];
int currentDodgeTime = modplayer.AllBossRecords[recordIndex].stat.dodgeTime;
int dodgeAttempt = modplayer.AttackCounter[recordIndex];
int currentDodges = modplayer.AllBossRecords[recordIndex].stat.totalDodges;
int worstDodges = modplayer.AllBossRecords[recordIndex].stat.totalDodges2;
modplayer.AllBossRecords[recordIndex].stat.dodgeTimeL = dodgeTimeAttempt;
modplayer.AllBossRecords[recordIndex].stat.totalDodgesL = dodgeAttempt;
// Increase kill count
modplayer.AllBossRecords[recordIndex].stat.kills++;
if (recordAttempt < currentRecord && currentRecord != 0 && worstRecord <= 0)
{
// First make the current record the worst record if no worst record has been made and a new record was made
modplayer.AllBossRecords[recordIndex].stat.fightTime2 = currentRecord;
}
if (recordAttempt < currentRecord || currentRecord <= 0)
{
//The player has beaten their best record, so we have to overwrite the old record with the new one
modplayer.AllBossRecords[recordIndex].stat.fightTime = recordAttempt;
}
else if (recordAttempt > worstRecord || worstRecord <= 0)
{
//The player has beaten their worst record, so we have to overwrite the old record with the new one
modplayer.AllBossRecords[recordIndex].stat.fightTime2 = recordAttempt;
}
if (brinkAttempt > currentBrink && currentBrink != 0 && worstBrink <= 0)
{
modplayer.AllBossRecords[recordIndex].stat.brink = currentBrink;
}
if (brinkAttempt > currentBrink || currentBrink <= 0)
{
modplayer.AllBossRecords[recordIndex].stat.brink2 = brinkAttempt;
double newHealth = (double)brinkAttempt / (double)MaxLife; // Casts may be redundant, but this setup doesn't work without them.
modplayer.AllBossRecords[recordIndex].stat.brinkPercent2 = (int)(newHealth * 100);
}
else if (brinkAttempt < worstBrink || worstBrink <= 0)
{
modplayer.AllBossRecords[recordIndex].stat.brink = brinkAttempt;
double newHealth = (double)brinkAttempt / (double)MaxLife; // Casts may be redundant, but this setup doesn't work without them.
modplayer.AllBossRecords[recordIndex].stat.brinkPercent = (int)(newHealth * 100);
}
if (dodgeTimeAttempt > currentDodgeTime || currentDodgeTime < 0)
{
// There is no "worse record" for this one so just overwrite any better records made
modplayer.AllBossRecords[recordIndex].stat.dodgeTime = dodgeTimeAttempt;
}
if (dodgeAttempt < currentDodges || currentDodges < 0)
{
modplayer.AllBossRecords[recordIndex].stat.totalDodges = dodgeAttempt;
if (worstDodges == 0) modplayer.AllBossRecords[recordIndex].stat.totalDodges2 = currentDodges;
}
else if (dodgeAttempt > worstDodges || worstDodges < 0)
{
modplayer.AllBossRecords[recordIndex].stat.totalDodges2 = dodgeAttempt;
}
modplayer.DodgeTimer[recordIndex] = 0;
modplayer.AttackCounter[recordIndex] = 0;
// If a new record was made, notify the player
if ((recordAttempt < currentRecord || currentRecord <= 0) || (brinkAttempt > currentBrink || currentBrink <= 0) || (dodgeAttempt < currentDodges || dodgeAttempt <= 0))
{
CombatText.NewText(player.getRect(), Color.LightYellow, "New Record!", true);
}
}
public void CheckRecordsMultiplayer(NPC npc)
{
int recordIndex = SpecialBossCheck(npc);
for (int i = 0; i < 255; i++)
{
Player player = Main.player[i];
PlayerAssist modPlayer = player.GetModPlayer<PlayerAssist>();
if (!player.active || !npc.playerInteraction[i]) continue; // Players must be active AND have interacted with the boss
if (Main.netMode == NetmodeID.Server)
{
Console.WriteLine("<<<<<<<<<<<<<<<<<<< Starting the Server NPCLoot stuff");
List<BossStats> list = BossAssist.ServerCollectedRecords[i];
BossStats oldRecord = list[recordIndex];
// Establish the new records for comparing
BossStats newRecord = new BossStats()
{
fightTimeL = modPlayer.RecordTimers[recordIndex],
totalDodgesL = modPlayer.AttackCounter[recordIndex],
dodgeTimeL = modPlayer.DodgeTimer[recordIndex],
brinkL = modPlayer.BrinkChecker[recordIndex],
brinkPercentL = (int)(((double)modPlayer.BrinkChecker[recordIndex] / modPlayer.MaxHealth[recordIndex]) * 100),
};
Console.WriteLine("<<<<<<<<<<<<<<<<<<< Declared new and old records");
// Compare the records
RecordID specificRecord = RecordID.None;
if (newRecord.fightTimeL < oldRecord.fightTime)
{
specificRecord |= RecordID.ShortestFightTime;
BossAssist.ServerCollectedRecords[i][recordIndex].fightTime = newRecord.fightTime;
}
if (newRecord.fightTimeL > oldRecord.fightTime2)
{
specificRecord |= RecordID.LongestFightTime;
BossAssist.ServerCollectedRecords[i][recordIndex].fightTime2 = newRecord.fightTime2;
}
BossAssist.ServerCollectedRecords[i][recordIndex].fightTimeL = newRecord.fightTimeL;
if (newRecord.brink2 > oldRecord.brink2)
{
specificRecord |= RecordID.BestBrink;
BossAssist.ServerCollectedRecords[i][recordIndex].brink2 = newRecord.brink;
BossAssist.ServerCollectedRecords[i][recordIndex].brinkPercent2 = newRecord.brinkPercent2;
}
if (newRecord.brink < oldRecord.brink)
{
specificRecord |= RecordID.WorstBrink;
BossAssist.ServerCollectedRecords[i][recordIndex].brink = newRecord.brink;
BossAssist.ServerCollectedRecords[i][recordIndex].brinkPercent = newRecord.brinkPercent;
}
BossAssist.ServerCollectedRecords[i][recordIndex].brinkL = newRecord.brinkL;
BossAssist.ServerCollectedRecords[i][recordIndex].brinkPercentL = newRecord.brinkPercentL;
if (newRecord.totalDodges < oldRecord.totalDodges)
{
specificRecord |= RecordID.LeastHits;
BossAssist.ServerCollectedRecords[i][recordIndex].totalDodges = newRecord.totalDodges;
}
if (newRecord.totalDodges2 > oldRecord.totalDodges2)
{
specificRecord |= RecordID.MostHits;
BossAssist.ServerCollectedRecords[i][recordIndex].totalDodges2 = newRecord.totalDodges2;
}
BossAssist.ServerCollectedRecords[i][recordIndex].totalDodgesL = newRecord.totalDodgesL;
if (newRecord.dodgeTime > oldRecord.dodgeTime)
{
specificRecord |= RecordID.DodgeTime;
BossAssist.ServerCollectedRecords[i][recordIndex].dodgeTime = newRecord.dodgeTime;
}
BossAssist.ServerCollectedRecords[i][recordIndex].dodgeTimeL = newRecord.dodgeTimeL;
Console.WriteLine("<<<<<<<<<<<<<<<<<<< Updated recrods apporiately");
// Make the packet
ModPacket packet = mod.GetPacket();
packet.Write((byte)MessageType.RecordUpdate);
packet.Write((int)specificRecord);
packet.Write((int)recordIndex);
// Kills update by 1 automatically
// Deaths have to be sent elsewhere (NPCLoot wont run if the player dies)
if (specificRecord.HasFlag(RecordID.ShortestFightTime)) packet.Write(newRecord.fightTime);
if (specificRecord.HasFlag(RecordID.LongestFightTime)) packet.Write(newRecord.fightTime2);
packet.Write(newRecord.fightTimeL);
if (specificRecord.HasFlag(RecordID.BestBrink))
{
packet.Write(newRecord.brink2);
packet.Write(newRecord.brinkPercent2);
}
if (specificRecord.HasFlag(RecordID.WorstBrink))
{
packet.Write(newRecord.brink);
packet.Write(newRecord.brinkPercent);
}
packet.Write(newRecord.brinkL);
packet.Write(newRecord.brinkPercentL);
if (specificRecord.HasFlag(RecordID.LeastHits)) packet.Write(newRecord.totalDodges);
if (specificRecord.HasFlag(RecordID.MostHits)) packet.Write(newRecord.totalDodges2);
packet.Write(newRecord.totalDodgesL);
if (specificRecord.HasFlag(RecordID.DodgeTime)) packet.Write(newRecord.dodgeTime);
packet.Write(newRecord.dodgeTimeL);
Console.WriteLine("<<<<<<<<<<<<<<<<<<< Making Packet");
// ORDER MATTERS
packet.Send(toClient: i);
Console.WriteLine("<<<<<<<<<<<<<<<<<<< PACKET SENT!");
}
}
}
public static int GetListNum(NPC boss)
{
List<BossInfo> BL = BossAssist.instance.setup.SortedBosses;
if (boss.type == NPCID.MoonLordCore) return BL.FindIndex(x => x.id == NPCID.MoonLordHead);
if (boss.type == NPCID.Spazmatism) return BL.FindIndex(x => x.id == NPCID.Retinazer);
if (boss.type < Main.maxNPCTypes) return BL.FindIndex(x => x.id == boss.type);
else return BL.FindIndex(x => x.name == boss.FullName && x.source == boss.modNPC.mod.Name);
}
public bool NPCisLimb(NPC npcType)
{
return npcType.type == NPCID.PrimeSaw
|| npcType.type == NPCID.PrimeLaser
|| npcType.type == NPCID.PrimeCannon
|| npcType.type == NPCID.PrimeVice
|| npcType.type == NPCID.SkeletronHand
|| npcType.type == NPCID.GolemFistLeft
|| npcType.type == NPCID.GolemFistRight
|| npcType.type == NPCID.GolemHead
|| (npcType.type == NPCID.Retinazer && Main.npc.Any(otherBoss => otherBoss.type == NPCID.Spazmatism && otherBoss.active))
|| (npcType.type == NPCID.Spazmatism && Main.npc.Any(otherBoss => otherBoss.type == NPCID.Retinazer && otherBoss.active));
}
public static int SpecialBossCheck(NPC npc)
{
List<BossInfo> BL = BossAssist.instance.setup.SortedBosses;
if (npc.type == NPCID.MoonLordCore) return BL.FindIndex(x => x.id == NPCID.MoonLordHead);
else if (TwinsCheck(npc)) return BL.FindIndex(x => x.id == NPCID.Retinazer);
else return GetListNum(npc);
}
public static bool TwinsCheck(NPC npc)
{
if (npc.type == NPCID.Retinazer)
{
return (Main.npc.All(otherBoss => otherBoss.type != NPCID.Spazmatism))
|| (Main.npc.Any(otherBoss => otherBoss.type == NPCID.Spazmatism && (!otherBoss.active || otherBoss.life <= 0)));
}
if (npc.type == NPCID.Spazmatism)
{
return (Main.npc.All(otherBoss => otherBoss.type != NPCID.Retinazer))
|| (Main.npc.Any(otherBoss => otherBoss.type == NPCID.Retinazer && (!otherBoss.active || otherBoss.life <= 0)));
}
return false; // Neither Boss was selected
}
public bool EaterOfWorldsCheck(NPC npc)
{
return ((npc.type >= 13 && npc.type <= 15) && npc.boss) || (npc.type != 13 && npc.type != 14 && npc.type != 15);
}
public override void OnChatButtonClicked(NPC npc, bool firstButton)
{
/*
if (npc.type == NPCID.Dryad && !firstButton)
{
MapAssist.LocateNearestEvil();
}
*/
}
public void UpdateRecordServerSide(int fromWho, NPC npc, string recordType, int recordValue)
{
ModPacket packet = mod.GetPacket(); // Create a packet
packet.Write(npc.whoAmI);
packet.Write(recordType);
packet.Write(recordValue);
packet.Send(fromWho); // Send it to the record maker's client?
NetMessage.BroadcastChatMessage(NetworkText.FromLiteral("Packet Sent"), Colors.RarityPurple);
}
}
}