-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathWorldAssist.cs
300 lines (258 loc) · 12.1 KB
/
WorldAssist.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
using BossChecklist.UIElements;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace BossChecklist
{
public class WorldAssist : ModSystem {
public static List<WorldRecord> WorldRecordsForWorld = new List<WorldRecord>(); // A list of all world records for each boss, saved to each world individually
public static List<WorldRecord> WorldRecordsForWorld_Unloaded = new List<WorldRecord>(); // A list of all the world records for unloadeded bosses
public static int[] ActiveNPCEntryFlags; // Used for despawn messages, which will occur when the npc is unflagged
public static HashSet<string> HiddenEntries = new HashSet<string>();
public static HashSet<string> MarkedEntries = new HashSet<string>();
public static bool downedBloodMoon;
public static bool downedFrostMoon;
public static bool downedPumpkinMoon;
public static bool downedSolarEclipse;
public static bool downedDarkMage;
public static bool downedOgre;
public static bool downedFlyingDutchman;
public static bool downedMartianSaucer;
public override void Load() {
On_Main.UpdateTime_StartDay += OnStartDay_CheckMoonEvents;
On_Main.UpdateTime_StartNight += OnStartNight_CheckEclipseDown;
}
/// <summary>
/// Before varibles are change for day (dawn), check for any moon events and mark as defeated it so.
/// </summary>
internal static void OnStartDay_CheckMoonEvents(On_Main.orig_UpdateTime_StartDay orig, ref bool stopEvents) {
if (Main.bloodMoon) {
AnnounceEventEnd("BloodMoon"); // Sends a message to all players that the moon event has ended
Networking.DownedEntryCheck(ref downedBloodMoon);
}
if (Main.snowMoon) {
AnnounceEventEnd("FrostMoon");
Networking.DownedEntryCheck(ref downedFrostMoon);
}
if (Main.pumpkinMoon) {
AnnounceEventEnd("PumpkinMoon");
Networking.DownedEntryCheck(ref downedPumpkinMoon);
}
orig(ref stopEvents);
}
/// <summary>
/// Before varibles are change for night (dusk), check for the eclipse event and mark as defeated it so.
/// </summary>
internal static void OnStartNight_CheckEclipseDown(On_Main.orig_UpdateTime_StartNight orig, ref bool stopEvents) {
if (Main.eclipse) {
AnnounceEventEnd("Eclipse");
Networking.DownedEntryCheck(ref downedSolarEclipse);
}
orig(ref stopEvents); // Original method turns off any moon states
}
public override void ClearWorld() {
HiddenEntries.Clear();
MarkedEntries.Clear();
WorldRecordsForWorld.Clear();
WorldRecordsForWorld_Unloaded.Clear();
downedBloodMoon = downedFrostMoon = downedPumpkinMoon = downedSolarEclipse = false; // clear moon downs
downedDarkMage = downedOgre = downedFlyingDutchman = downedMartianSaucer = false; // clear mini-boss downs
ActiveNPCEntryFlags = new int[Main.maxNPCs];
}
public override void OnWorldLoad() {
HiddenEntries.Clear();
MarkedEntries.Clear();
WorldRecordsForWorld.Clear();
WorldRecordsForWorld_Unloaded.Clear();
// Record related lists that should be the same count of record tracking entries
ActiveNPCEntryFlags = new int[Main.maxNPCs];
for (int i = 0; i < Main.maxNPCs; i++) {
ActiveNPCEntryFlags[i] = -1;
}
}
public override void SaveWorldData(TagCompound tag) {
var HiddenBossesList = new List<string>(HiddenEntries);
var MarkedAsDownedList = new List<string>(MarkedEntries);
var downed = new List<string>();
if (downedBloodMoon)
downed.Add("bloodmoon");
if (downedFrostMoon)
downed.Add("frostmoon");
if (downedPumpkinMoon)
downed.Add("pumpkinmoon");
if (downedSolarEclipse)
downed.Add("solareclipse");
if (downedDarkMage)
downed.Add("darkmage");
if (downedOgre)
downed.Add("ogre");
if (downedFlyingDutchman)
downed.Add("flyingdutchman");
if (downedMartianSaucer)
downed.Add("martiansaucer");
tag["downed"] = downed;
tag["HiddenBossesList"] = HiddenBossesList;
tag["downed_Forced"] = MarkedAsDownedList;
// All world record data, loaded or not, needs to be serialized and saved
TagCompound WorldRecordTag = new TagCompound();
foreach (WorldRecord record in WorldRecordsForWorld) {
if (record.CanBeSaved)
WorldRecordTag.Add(record.BossKey, record.SerializeData());
}
foreach (WorldRecord record in WorldRecordsForWorld_Unloaded) {
if (!WorldRecordTag.ContainsKey(record.BossKey))
WorldRecordTag.Add(record.BossKey, record.SerializeData());
}
tag["World_Record_Data"] = WorldRecordTag;
}
public override void LoadWorldData(TagCompound tag) {
if (tag.TryGet("World_Record_Data", out TagCompound savedData)) {
List<WorldRecord> SavedWorldRecords = new List<WorldRecord>();
foreach (KeyValuePair<string, object> data in savedData) {
SavedWorldRecords.Add(WorldRecord.DESERIALIZER(data.Value as TagCompound)); // deserialize the saved world record data
}
// Iterate through the saved data and store any records that are not loaded/active with the current mods
foreach (WorldRecord record in SavedWorldRecords) {
if (!BossChecklist.bossTracker.BossRecordKeys.Contains(record.BossKey))
WorldRecordsForWorld_Unloaded.Add(record); // any saved records from an unloaded boss must be perserved
}
// Iterate through the boss record keys to assign each record to where itshould be placed
foreach (string key in BossChecklist.bossTracker.BossRecordKeys) {
int index = SavedWorldRecords.FindIndex(x => x.BossKey == key);
WorldRecordsForWorld.Add(index == -1 ? new WorldRecord(key) : SavedWorldRecords[index]); // create a new entry if not in the list, otherwise use the saved data
}
}
else {
BossChecklist.bossTracker.BossRecordKeys.ForEach(key => WorldRecordsForWorld.Add(new WorldRecord(key))); // create a new entry if no saved data was found
}
var HiddenBossesList = tag.GetList<string>("HiddenBossesList");
foreach (var bossKey in HiddenBossesList) {
HiddenEntries.Add(bossKey);
}
var MarkedAsDownedList = tag.GetList<string>("downed_Forced");
foreach (var bossKey in MarkedAsDownedList) {
MarkedEntries.Add(bossKey);
}
var downed = tag.GetList<string>("downed");
downedBloodMoon = downed.Contains("bloodmoon");
downedFrostMoon = downed.Contains("frostmoon");
downedPumpkinMoon = downed.Contains("pumpkinmoon");
downedSolarEclipse = downed.Contains("solareclipse");
downedDarkMage = downed.Contains("darkmage");
downedOgre = downed.Contains("ogre");
downedFlyingDutchman = downed.Contains("flyingdutchman");
downedMartianSaucer = downed.Contains("martiansaucer");
}
public override void NetSend(BinaryWriter writer) {
writer.WriteFlags(downedBloodMoon, downedFrostMoon, downedPumpkinMoon, downedSolarEclipse, downedDarkMage, downedOgre, downedFlyingDutchman, downedMartianSaucer);
writer.Write(HiddenEntries.Count);
foreach (var bossKey in HiddenEntries) {
writer.Write(bossKey);
}
writer.Write(MarkedEntries.Count);
foreach (var bossKey in MarkedEntries) {
writer.Write(bossKey);
}
}
public override void NetReceive(BinaryReader reader) {
reader.ReadFlags(out downedBloodMoon, out downedFrostMoon, out downedPumpkinMoon, out downedSolarEclipse, out downedDarkMage, out downedOgre, out downedFlyingDutchman, out downedMartianSaucer);
HiddenEntries.Clear();
int count = reader.ReadInt32();
for (int i = 0; i < count; i++) {
HiddenEntries.Add(reader.ReadString());
}
MarkedEntries.Clear();
count = reader.ReadInt32();
for (int i = 0; i < count; i++) {
MarkedEntries.Add(reader.ReadString());
}
// Update checklist to match Hidden and Marked Downed entries
if (BossChecklistUI.Visible)
BossUISystem.Instance.bossChecklistUI.UpdateCheckboxes();
if (BossUISystem.Instance.BossLog.BossLogVisible && BossUISystem.Instance.BossLog.PageNum == -1) {
BossUISystem.Instance.BossLog.RefreshPageContent();
}
}
public override void PreUpdateWorld() {
HandleDespawnFlags();
}
public static string DetermineMoonAnnoucement(string eventType) {
if (BossChecklist.FeatureConfig.MoonMessages == "Generic") {
string eventTypeLocal = Language.Exists($"Bestiary_Events.{eventType}") ? Language.GetTextValue($"Bestiary_Events.{eventType}") : Language.GetTextValue($"Bestiary_Invasions.{eventType}");
if (eventType == "Eclipse")
eventTypeLocal = eventTypeLocal.ToLower();
return Language.GetText($"{NPCAssist.LangChat}.EventEnd.Generic").Format(eventTypeLocal);
}
else if (BossChecklist.FeatureConfig.MoonMessages == "Unique") {
return Language.GetTextValue($"{NPCAssist.LangChat}.EventEnd.{eventType}");
}
return null;
}
public static void AnnounceEventEnd(string eventType) {
if (Main.netMode == NetmodeID.SinglePlayer && DetermineMoonAnnoucement(eventType) is string message) {
Main.NewText(message, new Color(50, 255, 130));
}
else if (Main.netMode == NetmodeID.Server) {
// Send a packet to all multiplayer clients. Moon messages are client based, so they will need to read their own configs to determine the message.
foreach (Player player in Main.ActivePlayers) {
ModPacket packet = BossChecklist.instance.GetPacket();
packet.Write((byte)PacketMessageType.SendClientConfigMessage);
packet.Write((byte)ClientMessageType.Moon);
packet.Write(eventType);
packet.Send(player.whoAmI); // Server --> Multiplayer client
}
}
}
/// <summary>
/// Loops through all NPCs to check their active status.
/// Once inactive, the entry is unflagged and will have its despawn message displayed in chat.
/// Any record trackers currently active will stop if all instances of the entry's NPCs are no longer active.
/// </summary>
public void HandleDespawnFlags() {
foreach (NPC npc in Main.npc) {
if (npc.whoAmI >= Main.maxNPCs || ActiveNPCEntryFlags[npc.whoAmI] == -1 || npc.active)
continue; // skip unflagged entries. If flagged, don't trigger despawn message or stop trackers if the npc is still active
EntryInfo selectedEntry = BossChecklist.bossTracker.SortedEntries[ActiveNPCEntryFlags[npc.whoAmI]];
ActiveNPCEntryFlags[npc.whoAmI] = -1; // if the npc tracked is inactive, remove entry value
if (ActiveNPCEntryFlags.Contains(selectedEntry.GetIndex))
continue; // do nothing if any other npcs are apart of the entry and are still active
// Now that the entry no longer exists within ActiveNPCEntryFlags, it is determined to have despawned
// The Moon Lord has a special case, since it technically despawns when its killed
if (selectedEntry.GetDespawnMessage(npc) is LocalizedText message && (selectedEntry.Key != "Terraria MoonLord" || npc.life > 0)) {
if (Main.netMode == NetmodeID.SinglePlayer) {
Main.NewText(message.Format(npc.FullName), Colors.RarityPurple);
}
else if (Main.netMode == NetmodeID.Server) {
//ChatHelper.BroadcastChatMessage(NetworkText.FromLiteral(message.Format(npc.FullName)), Colors.RarityPurple);
// Send a packet to all multiplayer clients. Limb messages are client based, so they will need to read their own configs to determine the message.
foreach (Player player in Main.ActivePlayers) {
ModPacket packet = BossChecklist.instance.GetPacket();
packet.Write((byte)PacketMessageType.SendClientConfigMessage);
packet.Write((byte)ClientMessageType.Despawn);
packet.Write(npc.whoAmI);
packet.Send(player.whoAmI); // Server --> Multiplayer client
}
}
}
// When a boss despawns, stop tracking it for all players
if (!selectedEntry.IsRecordIndexed(out int recordIndex))
continue;
if (Main.netMode is NetmodeID.SinglePlayer) {
Main.LocalPlayer.GetModPlayer<PlayerAssist>().RecordsForWorld?[recordIndex].StopTracking(false, npc.playerInteraction[Main.LocalPlayer.whoAmI]);
}
else if (Main.netMode is NetmodeID.Server) {
foreach (Player player in Main.ActivePlayers) {
BossChecklist.ServerCollectedRecords[player.whoAmI][recordIndex].StopTracking_Server(player.whoAmI, false, npc.playerInteraction[player.whoAmI]);
}
WorldRecordsForWorld[recordIndex].UpdateGlobalDeaths(npc.playerInteraction.GetTrueIndexes());
}
}
}
}
}