-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulligan.cs
More file actions
533 lines (500 loc) · 21.3 KB
/
Mulligan.cs
File metadata and controls
533 lines (500 loc) · 21.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
using System.Reflection;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Localization;
using Landfall.Haste;
using Landfall.Modding;
using Zorro.Settings;
using Zorro.Core;
namespace Mulligan
{
/// <summary>
/// This helper encapsulates the meta progression logic that normally runs
/// in the PostGameScreen. It awards currency and unlocks items as needed.
/// </summary>
public static class MetaProgressionHelper
{
public static void AwardMetaProgression()
{
// Award currency from shard completion, if any.
int? currencyAwarded = SingletonAsset<MetaProgression>.Instance
.GetCurrencyAwardedAfterShardComplete();
if (currencyAwarded.HasValue)
{
int currencyValue = currencyAwarded.GetValueOrDefault();
if (currencyValue > 0)
{
MetaProgression.AddResource(currencyValue);
Debug.Log("Awarded " + currencyValue +
" meta progression currency.");
}
}
else
{
Debug.LogWarning("No currency rewarded for this run.");
}
// Unlock items if enough meta progression resource has been gathered.
System.Random randomInstance = RunHandler.GetCurrentLevelRandomInstance();
while (FactSystem.GetFact(MetaProgression.MetaProgressionResourceForItemUnlock)
>= (float)SingletonAsset<MetaProgression>.Instance.itemEveryCurrency)
{
ItemInstance itemToUnlock = ItemDatabase.GetRandomItem(Player.localPlayer,
randomInstance,
GetRandomItemFlags.Major | GetRandomItemFlags.IncludeLocked |
GetRandomItemFlags.ExcludeUnlocked
);
if (itemToUnlock != null)
{
FactSystem.AddToFact(
MetaProgression.MetaProgressionResourceForItemUnlock,
-SingletonAsset<MetaProgression>.Instance.itemEveryCurrency
);
Debug.Log("Meta progression unlock: " + itemToUnlock.itemName);
FactSystem.SetFact(new Fact(itemToUnlock.itemName + "_ShowItem"), 1f);
itemToUnlock.IsUnlocked = true;
if (ItemDatabase.HasShownAllItems())
{
AchievementHandler.Unlock(Achievements.ACH_DISCOVER_ALL_ITEMS);
}
}
else
{
Debug.Log("Meta progression: no more items left to unlock.");
break;
}
}
// Save the progression changes.
SaveSystem.Save();
}
}
/// <summary>
/// This mod enables a “Mulligan” system on various triggers.
/// Before clearing and restarting the run, it awards meta progression rewards and
/// updates run stats so the player gains all progression and stat updates as if the run
/// ended normally.
/// </summary>
[LandfallPlugin]
public class Mulligan
{
// Settings flags that control mulligan behavior.
private static bool mulliganEnabled = false; // self explanatory
private static bool mulliganSeedEnabled = true; // keep seed or not on full reset
private static bool mulliganRestart = false; // full reset or just level reset
private static bool // triggers
mulliganOnLanding,
mulliganOnRank,
mulliganOnLose,
mulliganOnDeath,
mulliganOnHit
= false;
private static int // thresholds
hitThreshold,
deathThreshold,
loseThreshold,
landingThreshold,
rankThreshold;
/// <summary>
/// Checks whether a mulligan should trigger based on the current run state
/// and a supplied level threshold. If so, it awards meta progression rewards
/// and updates the run stats (via HasteStats.OnRunEnd) before restarting the run/level.
/// </summary>
private static bool MulliganCheck(bool checkAgainst, int threshold)
{
int tempId = RunHandler.RunData.shardID;
RunConfig tempConfig = RunHandler.config;
int tempSeed = RunHandler.RunData.currentSeed;
RunConfigRuntimeData tempRuntimeData = RunHandler.RunData.runConfigRuntimeData;
Debug.Log("Mulligan checked on level: " +
RunHandler.RunData.currentLevel);
Debug.Log("Seed of check: " + tempSeed);
if (MulliganThresholdCheck(threshold) &&
RunHandler.InRun &&
!UI_TransitionHandler.IsTransitioning &&
mulliganEnabled &&
checkAgainst)
{
Debug.Log("Mulligan triggered on level: " +
RunHandler.RunData.currentLevel);
Debug.Log("Seed of trigger: " + tempSeed);
if (mulliganRestart) // check if restarting just level or whole run
{
UI_TransitionHandler.instance.Transition(() => // transition back to level start
{
SceneManager.LoadScene(
SceneManager.GetActiveScene().path);
}, "Dots", 0.3f, 0.5f, null);
}
else
{
// Award meta progression rewards.
MetaProgressionHelper.AwardMetaProgression();
// Also update run stats so the player gets relevant stat changes.
// (Here, we pass false to indicate a non-winning run; adjust if needed.)
HasteStats.OnRunEnd(false, RunHandler.RunData.shardID, false);
//RunHandler.ClearCurrentRun(); // dont do this probably
if (mulliganSeedEnabled) // check if keeping seed,
{
RunHandler.StartAndPlayNewRun(tempConfig, tempId, tempSeed, tempRuntimeData); // restart run with same stuff
}
else
{
RunHandler.StartAndPlayNewRun(
tempConfig, tempId, RunHandler.GenerateSeed(), tempRuntimeData); // restart with new seed
}
}
return true;
}
else
{
return false;
}
}
private static bool MulliganThresholdCheck(int setting) // check if current level meets threshold
{
return ((setting < 0) ? true : (RunHandler.RunData.currentLevel <= setting));
}
// Hook into various game events using MonoMod hooks.
static Mulligan()
{
On.Player.TakeDamage += (orig, self, damage,
sourceTransform, sourceName, source) =>
{
if (UI_TransitionHandler.IsTransitioning)
{
return;
}
if (MulliganCheck(mulliganOnHit, hitThreshold))
{
return;
}
float tempDamage = damage;
if (tempDamage < 0f)
{
tempDamage *= -1f;
}
tempDamage *= (Player.localPlayer.stats.damageMultiplier.multiplier) * //modify by difficulty settings
(Player.localPlayer.stats.damageMultiplier.baseValue);
tempDamage *= GameDifficulty.currentDif.damageTaken;
if ((Player.localPlayer.data.currentHealth - tempDamage) <= 0f) // check if damage would kill player
{
if (MulliganCheck(mulliganOnDeath, deathThreshold))
{
Player.localPlayer.data.currentHealth =
Player.localPlayer.stats.maxHealth.multiplier *
Player.localPlayer.stats.maxHealth.baseValue;
}
else
{
orig(self, damage, sourceTransform, sourceName, source);
}
}
else
{
orig(self, damage, sourceTransform, sourceName, source);
}
};
/*On.Player.Die += (orig, self) => //old one
{
if (MulliganCheck(mulliganOnDeath, deathThreshold))
{
return;
}
else
{
orig(self);
}
};
*/
On.GM_Run.PlayerDied += (orig, self, player) => // second check?
{
if (MulliganCheck(mulliganOnDeath, deathThreshold))
{
return;
}
else
{
orig(self, player);
}
};
On.RunHandler.LoseRun += (orig, transitionOverride, transitionDelay) =>
{
if (MulliganCheck(mulliganOnLose, loseThreshold))
{
// When mulliganing on lose, ensure the player is fully healed and
// lives are restored.
Player.localPlayer.data.lives =
(int)(Player.localPlayer.stats.lives.baseValue *
Player.localPlayer.stats.lives.multiplier);
Player.localPlayer.data.currentHealth =
Player.localPlayer.stats.maxHealth.multiplier *
Player.localPlayer.stats.maxHealth.baseValue;
return;
}
else
{
orig(transitionOverride, transitionDelay);
}
};
On.PlayerMovement.Land += (orig, self, landing) =>
{
if (UI_TransitionHandler.IsTransitioning)
{
return;
}
float landVal = (float)landing.GetType()
.GetField("landingScore")
.GetValue(landing);
Debug.Log(landVal);
if (landVal < 0.95f) //check if landing score was "perfect"
{
if (MulliganCheck(mulliganOnLanding, landingThreshold))
{
return;
}
else
{
orig(self, landing);
}
}
else
{
orig(self, landing);
}
};
On.GM_Run.PlayerEnteredPortal += (orig, self, player) =>
{
if (self.teirTime_S < self.GetCounter())
{
if (MulliganCheck(mulliganOnRank, rankThreshold))
{
return;
}
else
{
orig(self, player);
}
}
else
{
orig(self, player);
}
};
On.EscapeMenuAbandonPage.OnAbandonConfirmButtonClicked += (orig, self) =>
{
// Override the abandon button to simulate run completion.
MethodInfo compRun = typeof(RunHandler)
.GetMethod("CompleteRun",
BindingFlags.NonPublic | BindingFlags.Static);
object[] parameters = new object[] { false, false, 0f };
compRun.Invoke(null, parameters);
Singleton<EscapeMenu>.Instance.Close();
};
}
// ===== Settings Classes =====
[HasteSetting]
public class MulliganEnabledSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganEnabled = base.Value == OffOnMode.OFF;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.OFF;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Enable Mulligan?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "EnabledGraphicOption"),
new LocalizedString("Settings", "DisabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganSeedSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganSeedEnabled = base.Value == OffOnMode.ON;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.ON;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Use Same Seed for Mulligan?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "DisabledGraphicOption"),
new LocalizedString("Settings", "EnabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganRestartSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganRestart = base.Value == OffOnMode.OFF;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.OFF;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Restart From Same Level?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "EnabledGraphicOption"),
new LocalizedString("Settings", "DisabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganOnHitSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganOnHit = base.Value == OffOnMode.OFF;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.OFF;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Trigger Mulligan On Hit?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "EnabledGraphicOption"),
new LocalizedString("Settings", "DisabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganHitThreshold : IntSetting, IExposedSetting
{
public override void ApplyValue() => hitThreshold = Value - 1;
protected override int GetDefaultValue() => 1;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("On Hit Level Threshold:");
public string GetCategory() => "Mulligan";
}
[HasteSetting]
public class MulliganOnDeathSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganOnDeath = base.Value == OffOnMode.OFF;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.OFF;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Trigger Mulligan On Death?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "EnabledGraphicOption"),
new LocalizedString("Settings", "DisabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganDeathThreshold : IntSetting, IExposedSetting
{
public override void ApplyValue() => deathThreshold = Value - 1;
protected override int GetDefaultValue() => 1;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("On Death Level Threshold:");
public string GetCategory() => "Mulligan";
}
[HasteSetting]
public class MulliganOnLoseSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganOnLose = base.Value == OffOnMode.OFF;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.OFF;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Trigger Mulligan On Lose?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "EnabledGraphicOption"),
new LocalizedString("Settings", "DisabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganLoseThreshold : IntSetting, IExposedSetting
{
public override void ApplyValue() => loseThreshold = Value - 1;
protected override int GetDefaultValue() => 1;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("On Lose Level Threshold:");
public string GetCategory() => "Mulligan";
}
[HasteSetting]
public class MulliganOnLandingSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganOnLanding = base.Value == OffOnMode.OFF;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.OFF;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Trigger Mulligan On Non-Perfect Landing?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "EnabledGraphicOption"),
new LocalizedString("Settings", "DisabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganLandingThreshold : IntSetting, IExposedSetting
{
public override void ApplyValue() => landingThreshold = Value - 1;
protected override int GetDefaultValue() => 1;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("On Non-Perfect Landing Level Threshold:");
public string GetCategory() => "Mulligan";
}
[HasteSetting]
public class MulliganOnRankSetting : OffOnSetting, IExposedSetting
{
public override void ApplyValue()
{
Mulligan.mulliganOnRank = base.Value == OffOnMode.OFF;
}
public string GetCategory() => "Mulligan";
protected override OffOnMode GetDefaultValue() => OffOnMode.OFF;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("Trigger Mulligan On Non-S Rank?");
public override List<LocalizedString> GetLocalizedChoices()
{
return new List<LocalizedString>
{
new LocalizedString("Settings", "EnabledGraphicOption"),
new LocalizedString("Settings", "DisabledGraphicOption")
};
}
}
[HasteSetting]
public class MulliganRankThreshold : IntSetting, IExposedSetting
{
public override void ApplyValue() => rankThreshold = Value - 1;
protected override int GetDefaultValue() => 1;
public LocalizedString GetDisplayName() =>
new UnlocalizedString("On Non-S Rank Threshold:");
public string GetCategory() => "Mulligan";
}
}
}