Repository navigation
Expand file tree
/
Copy pathRandomizer.cs
More file actions
347 lines (295 loc) · 14 KB
/
Copy pathRandomizer.cs
File metadata and controls
347 lines (295 loc) · 14 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
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
347
using BepInEx.Logging;
using System.Collections.Generic;
using UnityEngine;
namespace SilksongLoadoutRandomizer
{
public static class Randomizer
{
// This will be called whenever the player hits a randomizer trigger
// (like resting at a bench, or defeating a boss)
public static void RandomizeLoadout()
{
if (!Plugin.IsModEnabled.Value) return;
Plugin.Logger.LogInfo("Triggering Loadout Randomization...");
ToolCrest currentCrest = null;
// 1. Determine which Crest to equip
if (Plugin.IncludeCrests.Value)
{
currentCrest = RandomizeCrest();
}
if (currentCrest == null)
{
currentCrest = ToolItemManager.GetCrestByName(PlayerData.instance.CurrentCrestID);
}
// 2. Randomize the Tools (Red, Blue, Yellow) and Silk Skills
if (currentCrest != null)
{
RandomizeToolsAndSkills(currentCrest);
}
// 3. Force the game engine to recalculate the silk spool limit in case the Cursed Crest was equipped or unequipped
if (HeroController.instance != null)
{
HeroController.instance.UpdateSilkCursed();
}
Plugin.Logger.LogInfo("Loadout successfully randomized!");
ShowNotification("Loadout Randomized");
}
public static void ShowNotification(string message)
{
// Attempt to display native UI notification
try
{
Sprite diceSprite = null;
// Try to find the Magnetite Dice to use its icon
ToolItem diceTool = ToolItemManager.GetToolByName("Magnetite Dice");
if (diceTool != null)
{
diceSprite = diceTool.GetPopupIcon();
}
RandomizerNotificationItem notification = new RandomizerNotificationItem(message, diceSprite);
CollectableUIMsg.Spawn(notification, null, false);
}
catch (System.Exception e)
{
Plugin.Logger.LogWarning($"Failed to spawn UI notification: {e.Message}");
}
}
public static void ScrubIllegitimateItems()
{
if (PlayerData.instance == null) return;
Plugin.Logger.LogInfo("Scrubbing illegitimate items from active loadout...");
ToolCrest currentCrest = ToolItemManager.GetCrestByName(PlayerData.instance.CurrentCrestID);
// Revert cursed crest or unowned crest
if (currentCrest == GlobalSettings.Gameplay.CursedCrest || currentCrest == null || !currentCrest.IsUnlocked)
{
string fallbackCrest = "Hunter";
foreach (string cName in LoadoutManager.AllCrestNames)
{
ToolCrest c = ToolItemManager.GetCrestByName(cName);
if (c != null && c.IsUnlocked)
{
if (cName == "Hunter_v3") { fallbackCrest = cName; break; }
else if (cName == "Hunter_v2" && fallbackCrest == "Hunter") { fallbackCrest = cName; }
else if (!cName.StartsWith("Hunter")) { fallbackCrest = cName; break; }
}
}
ToolItemManager.SetEquippedCrest(fallbackCrest);
}
PlayerData.instance.IsCurrentCrestTemp = false;
// Revert all unowned tools
var validCrestNames = PlayerData.instance.ToolEquips.GetValidNames();
foreach (string crestName in LoadoutManager.AllCrestNames)
{
if (validCrestNames == null || !validCrestNames.Contains(crestName)) continue;
List<ToolItem> equips = ToolItemManager.GetEquippedToolsForCrest(crestName);
if (equips != null)
{
List<string> scrubbedEquips = new List<string>();
bool changed = false;
for (int i = 0; i < equips.Count; i++)
{
ToolItem tool = equips[i];
if (tool != null && tool.IsUnlockedNotHidden)
{
scrubbedEquips.Add(tool.name);
}
else
{
scrubbedEquips.Add("");
if (tool != null) changed = true;
}
}
if (changed)
{
ToolItemManager.SetEquippedTools(crestName, scrubbedEquips);
}
}
}
ToolItemManager.SendEquippedChangedEvent(true);
}
public static List<ToolCrest> GetAvailableCrestsPool()
{
List<ToolCrest> availableCrests = new List<ToolCrest>();
foreach (string crestName in LoadoutManager.AllCrestNames)
{
ToolCrest c = ToolItemManager.GetCrestByName(crestName);
if (c != null && (c.IsUnlocked || Plugin.GiveAllCrests.Value))
{
// Ensure the crest actually exists in PlayerData memory to avoid NREs
if (PlayerData.instance != null)
{
var names = PlayerData.instance.ToolEquips.GetValidNames();
if (names == null || !names.Contains(crestName))
{
if (!Plugin.GiveAllCrests.Value)
{
continue;
}
// The crest isn't initialized in PlayerData, but the user wants to force-equip it.
// We must initialize its structure in memory to avoid native NREs.
PlayerData.instance.ToolEquips.SetData(crestName, c.SaveData);
}
}
availableCrests.Add(c);
}
}
// Filter out lower tier Hunter crests if higher tiers are unlocked, UNLESS Randomize Hunter Tier is enabled
if (!Plugin.RandomizeHunterTier.Value)
{
bool hasV3 = availableCrests.Exists(c => c.name == "Hunter_v3");
bool hasV2 = availableCrests.Exists(c => c.name == "Hunter_v2");
if (hasV3)
{
availableCrests.RemoveAll(c => c.name == "Hunter" || c.name == "Hunter_v2");
}
else if (hasV2)
{
availableCrests.RemoveAll(c => c.name == "Hunter");
}
}
// Add the cursed crest if the option is enabled. Cursed Crest is a special global instance.
if (Plugin.IncludeCursedCrest.Value && GlobalSettings.Gameplay.CursedCrest != null)
{
availableCrests.Add(GlobalSettings.Gameplay.CursedCrest);
}
return availableCrests;
}
private static ToolCrest RandomizeCrest()
{
List<ToolCrest> availableCrests = GetAvailableCrestsPool();
if (availableCrests.Count > 0)
{
ToolCrest chosenCrest = availableCrests[UnityEngine.Random.Range(0, availableCrests.Count)];
// Equip the crest
if (chosenCrest.name != PlayerData.instance.CurrentCrestID)
{
PlayerData.instance.PreviousCrestID = PlayerData.instance.CurrentCrestID;
}
ToolItemManager.SetEquippedCrest(chosenCrest.name);
bool isCursed = chosenCrest == GlobalSettings.Gameplay.CursedCrest;
PlayerData.instance.IsCurrentCrestTemp = isCursed;
return chosenCrest;
}
return null;
}
private static void RandomizeToolsAndSkills(ToolCrest crest)
{
List<string> newEquips = GenerateRandomLoadoutForCrest(crest);
// Apply to the game
ToolItemManager.SetEquippedTools(crest.name, newEquips);
ToolItemManager.SendEquippedChangedEvent();
// Log the exact loadout that was applied
Plugin.Logger.LogInfo($"[Randomizer] Successfully equipped Crest: {crest.name}");
Plugin.Logger.LogInfo($"[Randomizer] Equipped Tools:");
for (int i = 0; i < newEquips.Count; i++)
{
string toolStr = string.IsNullOrEmpty(newEquips[i]) ? "[EMPTY]" : newEquips[i];
Plugin.Logger.LogInfo($" - Slot {i + 1}: {toolStr}");
}
}
// Extracted for Integration Testing
public static List<string> GenerateRandomLoadoutForCrest(ToolCrest crest)
{
// Build the pools of available tools based on configuration toggles
List<ToolItem> redPool = new List<ToolItem>();
List<ToolItem> bluePool = new List<ToolItem>();
List<ToolItem> yellowPool = new List<ToolItem>();
List<ToolItem> skillPool = new List<ToolItem>();
foreach (ToolItem tool in ToolItemManager.GetAllTools())
{
bool isUnlocked = tool.IsUnlockedNotHidden;
if (!isUnlocked)
{
if (tool.Type == ToolItemType.Red && Plugin.GiveAllRedTools.Value) isUnlocked = true;
else if (tool.Type == ToolItemType.Blue && Plugin.GiveAllBlueTools.Value) isUnlocked = true;
else if (tool.Type == ToolItemType.Yellow && Plugin.GiveAllYellowTools.Value) isUnlocked = true;
else if (tool.Type == ToolItemType.Skill && Plugin.GiveAllSilkSkills.Value) isUnlocked = true;
}
if (!isUnlocked) continue;
// The Needle Phial is a temporary quest item and shouldn't take up a slot
if (tool.name.Replace(" ", "").Replace("_", "").Equals("Extractor", System.StringComparison.OrdinalIgnoreCase)) continue;
if (tool.Type == ToolItemType.Red && Plugin.IncludeRedTools.Value) redPool.Add(tool);
else if (tool.Type == ToolItemType.Blue && Plugin.IncludeBlueTools.Value) bluePool.Add(tool);
else if (tool.Type == ToolItemType.Yellow && Plugin.IncludeYellowTools.Value) yellowPool.Add(tool);
else if (tool.Type == ToolItemType.Skill && Plugin.IncludeSilkSkills.Value) skillPool.Add(tool);
}
List<string> newEquips = new List<string>();
ToolCrestsData.Data crestData = crest.SaveData;
bool dataModified = false;
if (crestData.Slots == null)
{
crestData.Slots = new System.Collections.Generic.List<ToolCrestsData.SlotData>();
dataModified = true;
}
while (crestData.Slots.Count < crest.Slots.Length)
{
crestData.Slots.Add(new ToolCrestsData.SlotData { EquippedTool = "", IsUnlocked = false });
dataModified = true;
}
// Iterate over the slots of the crest
for (int i = 0; i < crest.Slots.Length; i++)
{
bool isSlotUnlocked = !crest.Slots[i].IsLocked; // Base state
// Check if save data overrides it (e.g. Memory Lockets)
if (crestData.Slots[i].IsUnlocked)
{
isSlotUnlocked = true;
}
else if (Plugin.GiveAllSlots.Value)
{
// User wants to force unlock all slots. Update the memory structure so native engine accepts the tools
var sd = crestData.Slots[i];
sd.IsUnlocked = true;
crestData.Slots[i] = sd;
isSlotUnlocked = true;
dataModified = true;
}
if (!isSlotUnlocked)
{
newEquips.Add(""); // Slot is locked, empty it
continue;
}
// Try to fill the slot from the matching pool
var slotInfo = crest.Slots[i];
ToolItem selectedTool = null;
if (slotInfo.Type == ToolItemType.Red && redPool.Count > 0)
{
int r = UnityEngine.Random.Range(0, redPool.Count);
selectedTool = redPool[r];
redPool.RemoveAt(r);
}
else if (slotInfo.Type == ToolItemType.Blue && bluePool.Count > 0)
{
int r = UnityEngine.Random.Range(0, bluePool.Count);
selectedTool = bluePool[r];
bluePool.RemoveAt(r);
}
else if (slotInfo.Type == ToolItemType.Yellow && yellowPool.Count > 0)
{
int r = UnityEngine.Random.Range(0, yellowPool.Count);
selectedTool = yellowPool[r];
yellowPool.RemoveAt(r);
}
else if (slotInfo.Type == ToolItemType.Skill && skillPool.Count > 0)
{
int r = UnityEngine.Random.Range(0, skillPool.Count);
selectedTool = skillPool[r];
skillPool.RemoveAt(r);
}
if (selectedTool != null)
{
newEquips.Add(selectedTool.name);
}
else
{
newEquips.Add(""); // Ran out of tools for this color, leave empty
}
}
if (dataModified && PlayerData.instance != null)
{
PlayerData.instance.ToolEquips.SetData(crest.name, crestData);
}
return newEquips;
}
}
}