-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStatRandomizer.cs
More file actions
722 lines (643 loc) · 28.1 KB
/
StatRandomizer.cs
File metadata and controls
722 lines (643 loc) · 28.1 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Z2Randomizer.RandomizerCore.Enemy;
namespace Z2Randomizer.RandomizerCore;
/// <summary>
/// Responsible for randomizing stats of all kinds. Each array should
/// be randomized at most once. If you have to re-roll for some reason,
/// a new StatRandomizer object should be created.
/// </summary>
public class StatRandomizer
{
public const int LIFE_EFFECTIVENESS_ROWS = 7;
public const int MAGIC_EFFECTIVENESS_ROWS = 8;
public byte[] AttackEffectivenessTable { get; private set; } = null!;
public byte[] LifeEffectivenessTable { get; private set; } = null!;
public byte[] MagicEffectivenessTable { get; private set; } = null!;
public int[] ExperienceToLevelTable { get; private set; } = null!;
public byte[] WestEnemyHpTable { get; private set; } = null!;
public byte[] EastEnemyHpTable { get; private set; } = null!;
public byte[] Palace125EnemyHpTable { get; private set; } = null!;
public byte[] Palace346EnemyHpTable { get; private set; } = null!;
public byte[] GpEnemyHpTable { get; private set; } = null!;
public byte[] BossHpTable { get; private set; } = null!;
public byte[] WestEnemyStatsTable { get; private set; } = null!;
public byte[] EastEnemyStatsTable { get; private set; } = null!;
public byte[] Palace125EnemyStatsTable { get; private set; } = null!;
public byte[] Palace346EnemyStatsTable { get; private set; } = null!;
public byte[] GpEnemyStatsTable { get; private set; } = null!;
public byte[] BossExpTable { get; private set; } = null!;
protected RandomizerProperties props { get; }
#if DEBUG
private bool hasRandomized = false;
private bool hasWritten = false;
#endif
public StatRandomizer(ROM rom, RandomizerProperties props)
{
this.props = props;
ReadExperienceToLevel(rom);
ReadAttackEffectiveness(rom);
ReadLifeEffectiveness(rom);
ReadMagicEffectiveness(rom);
ReadEnemyHp(rom);
ReadEnemyStats(rom);
}
public void Randomize(Random r)
{
#if DEBUG
Debug.Assert(!hasRandomized);
hasRandomized = true;
#endif
ExperienceToLevelTable = RandomizeExperienceToLevel(ExperienceToLevelTable, r,
[props.ShuffleAtkExp, props.ShuffleMagicExp, props.ShuffleLifeExp],
[props.AttackCap, props.MagicCap, props.LifeCap], props.ScaleLevels);
RandomizeAttackEffectiveness(r, props.AttackEffectiveness);
RandomizeLifeEffectiveness(r, props.LifeEffectiveness);
RandomizeMagicEffectiveness(r, props.MagicEffectiveness);
RandomizeRegularEnemyHp(r);
RandomizeBossHp(r);
FixRebonackHorseKillBug();
RandomizeEnemyStats(r);
}
public void Write(ROM rom)
{
#if DEBUG
Debug.Assert(!hasWritten);
hasWritten = true;
#endif
WriteExperienceToLevel(rom);
WriteAttackEffectiveness(rom);
WriteLifeEffectiveness(rom);
WriteMagicEffectiveness(rom);
WriteEnemyHp(rom);
WriteEnemyStats(rom);
}
[Conditional("DEBUG")]
public void AssertHasRandomized(bool value = true)
{
#if DEBUG
Debug.Assert(hasRandomized == value);
#endif
}
[Conditional("DEBUG")]
public void AssertHasWritten(bool value = true) {
#if DEBUG
Debug.Assert(hasWritten == value);
#endif
}
protected void ReadExperienceToLevel(ROM rom)
{
const int startAddr = RomMap.EXPERIENCE_TO_LEVEL_TABLE;
ExperienceToLevelTable = new int[24];
for (int i = 0; i < 24; i++)
{
ExperienceToLevelTable[i] = rom.GetShort(startAddr + i, startAddr + 24 + i);
}
}
protected void WriteExperienceToLevel(ROM rom)
{
const int startAddr = RomMap.EXPERIENCE_TO_LEVEL_TABLE;
for (int i = 0; i < ExperienceToLevelTable.Length; i++)
{
rom.PutShort(startAddr + i, startAddr + 24 + i, ExperienceToLevelTable[i]);
}
for (int i = 0; i < ExperienceToLevelTable.Length; i++)
{
int n = ExperienceToLevelTable[i];
var digit1 = ROM.DigitToZ2TextByte(n / 1000);
n %= 1000;
var digit2 = ROM.DigitToZ2TextByte(n / 100);
n %= 100;
var digit3 = ROM.DigitToZ2TextByte(n / 10);
rom.Put(RomMap.EXPERIENCE_TO_LEVEL_TEXT_TABLE + 48 + i, digit1);
rom.Put(RomMap.EXPERIENCE_TO_LEVEL_TEXT_TABLE + 24 + i, digit2);
rom.Put(RomMap.EXPERIENCE_TO_LEVEL_TEXT_TABLE + 0 + i, digit3);
}
}
protected void ReadAttackEffectiveness(ROM rom)
{
AttackEffectivenessTable = rom.GetBytes(RomMap.ATTACK_EFFECTIVENESS_TABLE, 8);
}
protected void WriteAttackEffectiveness(ROM rom)
{
rom.Put(RomMap.ATTACK_EFFECTIVENESS_TABLE, AttackEffectivenessTable);
}
protected void ReadLifeEffectiveness(ROM rom)
{
// There are 7 different damage codes for which damage taken scales with Life-level
// Each of those 7 codes have 8 values corresponding to each Life-level.
// (This is followed by an 8th row that is OHKO regardless of Life-level.)
LifeEffectivenessTable = rom.GetBytes(RomMap.LIFE_EFFECTIVENESS_TABLE, LIFE_EFFECTIVENESS_ROWS * 8);
}
protected void WriteLifeEffectiveness(ROM rom)
{
rom.Put(RomMap.LIFE_EFFECTIVENESS_TABLE, LifeEffectivenessTable);
}
protected void ReadMagicEffectiveness(ROM rom)
{
// 8 Spell costs by 8 Magic levels
MagicEffectivenessTable = rom.GetBytes(RomMap.MAGIC_EFFECTIVENESS_TABLE, MAGIC_EFFECTIVENESS_ROWS * 8);
}
protected void WriteMagicEffectiveness(ROM rom)
{
rom.Put(RomMap.MAGIC_EFFECTIVENESS_TABLE, MagicEffectivenessTable);
}
protected void ReadEnemyHp(ROM rom)
{
WestEnemyHpTable = rom.GetBytes(RomMap.WEST_ENEMY_HP_TABLE, 0x24);
EastEnemyHpTable = rom.GetBytes(RomMap.EAST_ENEMY_HP_TABLE, 0x24);
Palace125EnemyHpTable = rom.GetBytes(RomMap.PALACE125_ENEMY_HP_TABLE, 0x24);
Palace346EnemyHpTable = rom.GetBytes(RomMap.PALACE346_ENEMY_HP_TABLE, 0x24);
GpEnemyHpTable = rom.GetBytes(RomMap.GP_ENEMY_HP_TABLE, 0x24);
BossHpTable = [.. RomMap.bossHpAddresses.Select(rom.GetByte)];
}
protected void WriteEnemyHp(ROM rom)
{
rom.Put(RomMap.WEST_ENEMY_HP_TABLE, WestEnemyHpTable);
rom.Put(RomMap.EAST_ENEMY_HP_TABLE, EastEnemyHpTable);
rom.Put(RomMap.PALACE125_ENEMY_HP_TABLE, Palace125EnemyHpTable);
rom.Put(RomMap.PALACE346_ENEMY_HP_TABLE, Palace346EnemyHpTable);
rom.Put(RomMap.GP_ENEMY_HP_TABLE, GpEnemyHpTable);
for (int i = 0; i < RomMap.bossHpAddresses.Count; i++) {
rom.Put(RomMap.bossHpAddresses[i], BossHpTable[i]);
}
}
protected void ReadEnemyStats(ROM rom)
{
WestEnemyStatsTable = rom.GetBytes(RomMap.WEST_ENEMY_STATS_TABLE, 0x48);
EastEnemyStatsTable = rom.GetBytes(RomMap.EAST_ENEMY_STATS_TABLE, 0x48);
Palace125EnemyStatsTable = rom.GetBytes(RomMap.PALACE125_ENEMY_STATS_TABLE, 0x48);
Palace346EnemyStatsTable = rom.GetBytes(RomMap.PALACE346_ENEMY_STATS_TABLE, 0x48);
GpEnemyStatsTable = rom.GetBytes(RomMap.GP_ENEMY_STATS_TABLE, 0x48);
BossExpTable = [.. RomMap.bossExpAddresses.Select(rom.GetByte)]; // we only have exp byte here
}
protected void WriteEnemyStats(ROM rom)
{
rom.Put(RomMap.WEST_ENEMY_STATS_TABLE, WestEnemyStatsTable);
rom.Put(RomMap.EAST_ENEMY_STATS_TABLE, EastEnemyStatsTable);
rom.Put(RomMap.PALACE125_ENEMY_STATS_TABLE, Palace125EnemyStatsTable);
rom.Put(RomMap.PALACE346_ENEMY_STATS_TABLE, Palace346EnemyStatsTable);
rom.Put(RomMap.GP_ENEMY_STATS_TABLE, GpEnemyStatsTable);
for (int i = 0; i < RomMap.bossExpAddresses.Count; i++)
{
rom.Put(RomMap.bossExpAddresses[i], BossExpTable[i]);
}
}
protected static int[] RandomizeExperienceToLevel(int[] baseExpTable, Random r, bool[] shuffleStat, int[] levelCap, bool scaleLevels)
{
const int STAT_COUNT = 3;
const int LEVELS_PER_STAT = 8;
const int TABLE_SIZE = STAT_COUNT * LEVELS_PER_STAT;
const double lower = 0.75;
const double upper = 1.25;
int[] newTable = new int[TABLE_SIZE];
ReadOnlySpan<int> preRandomSpan;
if (scaleLevels)
{
int[] preRandomTable = new int[TABLE_SIZE];
for (int stat = 0; stat < STAT_COUNT; stat++)
{
var statStartIndex = stat * LEVELS_PER_STAT;
var cap = levelCap[stat];
if (cap > 7)
{
Array.Copy(baseExpTable, statStartIndex, preRandomTable, statStartIndex, LEVELS_PER_STAT);
continue;
}
// using cubic function: ax³ + bx² + cx + d
(double a, double b, double c, double d) = stat switch
{
0 => (36.111, -183.333, 709.127, -400.0), // attack coefficients
1 => (33.333, -197.619, 661.905, -428.571), // magic coefficients
2 => (13.889, -31.548, 118.849, -57.143), // life coefficients
_ => throw new NotImplementedException(),
};
int i;
for (i = 0; i < cap - 1; i++)
{
double x = (7.0 / (cap - 1)) * (i + 1);
double y = a * x*x*x + b * x*x + c * x + d;
Debug.Assert(y > 10 && y < 9990);
preRandomTable[statStartIndex + i] = (int)Math.Round(y);
}
for (; i < LEVELS_PER_STAT; i++)
{
preRandomTable[statStartIndex + i] = 9990;
}
}
preRandomSpan = preRandomTable;
}
else
{
preRandomSpan = baseExpTable;
}
Span<int> randomizedSpan = newTable;
for (int stat = 0; stat < STAT_COUNT; stat++)
{
var statStartIndex = stat * LEVELS_PER_STAT;
if (!shuffleStat[stat])
{
preRandomSpan.Slice(statStartIndex, LEVELS_PER_STAT).CopyTo(randomizedSpan.Slice(statStartIndex, LEVELS_PER_STAT));
continue;
}
for (int i = 0; i < LEVELS_PER_STAT; i++)
{
var vanilla = preRandomSpan[statStartIndex + i];
int nextMin = (int)(vanilla * lower);
int nextMax = (int)(vanilla * upper);
if (i == 0)
{
newTable[statStartIndex + i] = r.Next(Math.Max(10, nextMin), nextMax);
}
else
{
newTable[statStartIndex + i] = r.Next(Math.Max(newTable[statStartIndex + i - 1], nextMin), Math.Min(nextMax, 9990));
}
}
}
for (int i = 0; i < newTable.Length; i++)
{
newTable[i] = newTable[i] / 10 * 10; //wtf is this line of code? -digshake, 2020
}
// Make sure all 1-up levels are the same value,
// one that is higher than any regular level
int highestRegularLevelExp = 0;
for (int stat = 0; stat < STAT_COUNT; stat++)
{
var cap = levelCap[stat];
var statStartIndex = stat * LEVELS_PER_STAT;
int statMaxLevelIndex = Math.Max(0, statStartIndex + cap - 2);
var maxExp = newTable[statMaxLevelIndex];
if (highestRegularLevelExp < maxExp) { highestRegularLevelExp = maxExp; }
}
int exp1upMin = Math.Min(highestRegularLevelExp + 10, 9990);
int exp1up = r.Next(exp1upMin, 9999) / 10 * 10;
for (int stat = 0; stat < STAT_COUNT; stat++)
{
var cap = levelCap[stat];
Debug.Assert(cap >= 1 && cap < 9);
var statStartIndex = stat * LEVELS_PER_STAT;
for (int i = cap - 1; i < LEVELS_PER_STAT; i++)
{
newTable[statStartIndex + i] = exp1up;
}
}
return newTable;
}
protected void RandomizeAttackEffectiveness(Random r, AttackEffectiveness attackEffectiveness)
{
if (attackEffectiveness == AttackEffectiveness.VANILLA)
{
return;
}
if (attackEffectiveness == AttackEffectiveness.OHKO)
{
// handled in RandomizeEnemyStats()
return;
}
byte[] newTable = new byte[8];
for (int i = 0; i < 8; i++)
{
int nextVal;
byte vanilla = AttackEffectivenessTable[i];
switch (attackEffectiveness)
{
case AttackEffectiveness.LOW:
//the naieve approach here gives a curve of 1,2,2,4,5,6 which is weird, or a different
//irregular curve in digshake's old approach. Just use a linear increase for the first 6 levels on low
if (i < 6)
{
nextVal = i + 1;
}
else
{
nextVal = (int)Math.Round(vanilla * .5, MidpointRounding.ToPositiveInfinity);
}
break;
case AttackEffectiveness.AVERAGE_LOW:
nextVal = RandomInRange(r, vanilla * .5, vanilla);
if (i == 1)
{
nextVal = Math.Max(nextVal, 2); // set minimum 2 damage at level 2
}
break;
case AttackEffectiveness.AVERAGE:
nextVal = RandomInRange(r, vanilla * .667, vanilla * 1.5);
if (i == 0)
{
nextVal = Math.Max(nextVal, 2); // set minimum 2 damage at start
}
break;
case AttackEffectiveness.AVERAGE_HIGH:
nextVal = RandomInRange(r, vanilla, vanilla * 1.5);
break;
case AttackEffectiveness.HIGH:
nextVal = (int)Math.Round(vanilla * 1.5);
break;
default:
throw new NotImplementedException("Invalid Attack Effectiveness");
}
if (i > 0)
{
byte lastValue = newTable[i - 1];
if (nextVal < lastValue)
{
nextVal = lastValue; // levelling up should never be worse
}
}
newTable[i] = (byte)nextVal;
}
AttackEffectivenessTable = newTable;
}
protected void RandomizeLifeEffectiveness(Random r, LifeEffectiveness statEffectiveness)
{
if (statEffectiveness == LifeEffectiveness.VANILLA)
{
return;
}
if (statEffectiveness == LifeEffectiveness.OHKO)
{
Array.Fill<byte>(LifeEffectivenessTable, 0xFF);
return;
}
if (statEffectiveness == LifeEffectiveness.INVINCIBLE)
{
Array.Fill<byte>(LifeEffectivenessTable, 0x00);
return;
}
byte[] newTable = new byte[LIFE_EFFECTIVENESS_ROWS * 8];
// The values we are randomizing are actually *enemy damage* values
for (int level = 0; level < 8; level++)
{
for (int damageCode = 0; damageCode < LIFE_EFFECTIVENESS_ROWS; damageCode++)
{
int index = damageCode * 8 + level;
byte nextVal;
byte vanilla = (byte)(LifeEffectivenessTable[index] >> 1);
int min = (int)(vanilla * .75);
int max = Math.Min((int)(vanilla * 1.5), 120);
switch (statEffectiveness)
{
case LifeEffectiveness.AVERAGE_LOW:
nextVal = (byte)r.Next(vanilla, max);
break;
case LifeEffectiveness.AVERAGE:
nextVal = (byte)r.Next(min, max);
break;
case LifeEffectiveness.AVERAGE_HIGH:
nextVal = (byte)r.Next(min, vanilla);
break;
case LifeEffectiveness.HIGH:
nextVal = (byte)(vanilla * .5);
break;
default:
throw new NotImplementedException("Invalid Life Effectiveness");
}
if (level > 0)
{
byte lastVal = (byte)(newTable[index - 1] >> 1);
if (nextVal > lastVal)
{
nextVal = lastVal; // levelling up should never be worse
}
}
newTable[index] = (byte)(nextVal << 1);
}
}
LifeEffectivenessTable = newTable;
}
protected void RandomizeMagicEffectiveness(Random r, MagicEffectiveness statEffectiveness)
{
if (statEffectiveness == MagicEffectiveness.VANILLA)
{
return;
}
if (statEffectiveness == MagicEffectiveness.FREE)
{
Array.Fill<byte>(MagicEffectivenessTable, 0);
return;
}
byte[] newTable = new byte[MAGIC_EFFECTIVENESS_ROWS * 8];
for (int level = 0; level < 8; level++)
{
for (int spellIndex = 0; spellIndex < MAGIC_EFFECTIVENESS_ROWS; spellIndex++)
{
int index = spellIndex * 8 + level;
byte nextVal;
byte vanilla = (byte)(MagicEffectivenessTable[index] >> 1);
int min = (int)(vanilla * .5);
int max = Math.Min((int)(vanilla * 1.5), 120);
switch (statEffectiveness)
{
case MagicEffectiveness.HIGH_COST:
nextVal = (byte)max;
break;
case MagicEffectiveness.AVERAGE_HIGH_COST:
nextVal = (byte)r.Next(vanilla, max);
break;
case MagicEffectiveness.AVERAGE:
nextVal = (byte)r.Next(min, max);
break;
case MagicEffectiveness.AVERAGE_LOW_COST:
nextVal = (byte)r.Next(min, vanilla);
break;
case MagicEffectiveness.LOW_COST:
nextVal = (byte)min;
break;
default:
throw new Exception("Invalid Magic Effectiveness");
}
if (level > 0)
{
byte lastVal = (byte)(newTable[index - 1] >> 1);
if (nextVal > lastVal)
{
nextVal = lastVal; // levelling up should never be worse
}
}
newTable[index] = (byte)(nextVal << 1);
}
}
MagicEffectivenessTable = newTable;
}
protected void RandomizeRegularEnemyHp(Random r)
{
RandomRangeDoubleAttribute? range = props.ShuffleEnemyHP.GetRandomRangeDouble();
if (range == null)
{
Debug.Assert(props.ShuffleEnemyHP == EnemyLifeOption.VANILLA);
return;
}
double low = range.Low;
double high = range.High;
int i;
for (i = (int)EnemiesWest.MYU; i < 0x23; i++) { RandomizeInsideArray(r, WestEnemyHpTable, i, low, high); }
for (i = (int)EnemiesEast.MYU; i < 0x1e; i++) { RandomizeInsideArray(r, EastEnemyHpTable, i, low, high); }
// keeping old behavior where some non-enemies are not randomized (strike for jar, unused enemies etc.)
for (i = (int)EnemiesPalace125.MYU; i < (int)EnemiesPalace125.STRIKE_FOR_RED_JAR; i++) { RandomizeInsideArray(r, Palace125EnemyHpTable, i, low, high); }
for (i = (int)EnemiesPalace125.SLOW_BUBBLE; i < (int)EnemiesPalace125.HORSEHEAD; i++) { RandomizeInsideArray(r, Palace125EnemyHpTable, i, low, high); }
for (i = (int)EnemiesPalace125.BLUE_STALFOS; i < 0x24; i++) { RandomizeInsideArray(r, Palace125EnemyHpTable, i, low, high); }
for (i = (int)EnemiesPalace346.MYU; i < (int)EnemiesPalace346.STRIKE_FOR_RED_JAR_OR_IRON_KNUCKLE; i++) { RandomizeInsideArray(r, Palace346EnemyHpTable, i, low, high); }
for (i = (int)EnemiesPalace346.SLOW_BUBBLE; i < (int)EnemiesPalace346.REBONAK; i++) { RandomizeInsideArray(r, Palace346EnemyHpTable, i, low, high); }
for (i = (int)EnemiesPalace346.BLUE_STALFOS; i < 0x24; i++) { RandomizeInsideArray(r, Palace346EnemyHpTable, i, low, high); }
for (i = (int)EnemiesGreatPalace.MYU; i < (int)EnemiesGreatPalace.STRIKE_FOR_RED_JAR_OR_FOKKA; i++) { RandomizeInsideArray(r, GpEnemyHpTable, i, low, high); }
for (i = (int)EnemiesGreatPalace.ORANGE_MOA; i < (int)EnemiesGreatPalace.BUBBLE_GENERATOR; i++) { RandomizeInsideArray(r, GpEnemyHpTable, i, low, high); }
for (i = (int)EnemiesGreatPalace.RED_DEELER; i < (int)EnemiesGreatPalace.ELEVATOR; i++) { RandomizeInsideArray(r, GpEnemyHpTable, i, low, high); }
for (i = (int)EnemiesGreatPalace.SLOW_BUBBLE; i < 0x1b; i++) { RandomizeInsideArray(r, GpEnemyHpTable, i, low, high); }
for (i = (int)EnemiesGreatPalace.FOKKERU; i < (int)EnemiesGreatPalace.KING_BOT; i++) { RandomizeInsideArray(r, GpEnemyHpTable, i, low, high); }
}
protected void RandomizeBossHp(Random r)
{
RandomRangeDoubleAttribute? range = props.ShuffleBossHP.GetRandomRangeDouble();
if (range == null)
{
Debug.Assert(props.ShuffleBossHP == EnemyLifeOption.VANILLA);
return;
}
double low = range.Low;
double high = range.High;
for (int i = 0; i < BossHpTable.Length; i++) { RandomizeInsideArray(r, BossHpTable, i, low, high); }
}
protected void RandomizeEnemyStats(Random r)
{
RandomizeEnemyAttributes(r, WestEnemyStatsTable, Enemies.WestGroundEnemies, Enemies.WestFlyingEnemies, Enemies.WestGenerators);
RandomizeEnemyAttributes(r, EastEnemyStatsTable, Enemies.EastGroundEnemies, Enemies.EastFlyingEnemies, Enemies.EastGenerators);
RandomizeEnemyAttributes(r, Palace125EnemyStatsTable, Enemies.Palace125GroundEnemies, Enemies.Palace125FlyingEnemies, Enemies.Palace125Generators);
RandomizeEnemyAttributes(r, Palace346EnemyStatsTable, Enemies.Palace346GroundEnemies, Enemies.Palace346FlyingEnemies, Enemies.Palace346Generators);
RandomizeEnemyAttributes(r, GpEnemyStatsTable, Enemies.GPGroundEnemies, Enemies.GPFlyingEnemies, Enemies.GPGenerators);
RandomizeEnemyExp(r, BossExpTable, props.EnemyXPDrops); // randomize boss XP separately
}
protected void RandomizeEnemyAttributes<T>(Random r, byte[] bytes, T[] groundEnemies, T[] flyingEnemies, T[] generators) where T : Enum
{
List<T> allEnemies = [.. groundEnemies, .. flyingEnemies, .. generators];
var vanillaEnemyBytes1 = allEnemies.Select(n => bytes[(int)(object)n]).ToArray();
var vanillaEnemyBytes2 = allEnemies.Select(n => bytes[(int)(object)n + 0x24]).ToArray();
byte[] enemyBytes1 = vanillaEnemyBytes1.ToArray();
byte[] enemyBytes2 = vanillaEnemyBytes2.ToArray();
// enemy attributes byte1
// ..x. .... sword immune
// ...x .... steals exp
// .... xxxx exp
const int SWORD_IMMUNE_BIT = 0b00100000;
const int XP_STEAL_BIT = 0b00010000;
if (props.ShuffleSwordImmunity)
{
RandomizeBits(r, enemyBytes1, SWORD_IMMUNE_BIT);
}
if (props.ShuffleEnemyStealExp)
{
RandomizeBits(r, enemyBytes1, XP_STEAL_BIT);
}
RandomizeEnemyExp(r, enemyBytes1, props.EnemyXPDrops);
// enemy attributes byte2
// ..x. .... immune to projectiles
const int PROJECTILE_IMMUNE_BIT = 0b00100000;
for (int i = 0; i < allEnemies.Count; i++)
{
if ((enemyBytes1[i] & SWORD_IMMUNE_BIT) != 0)
{
// if an enemy is becoming sword immune, make it not fire immune
if ((vanillaEnemyBytes1[i] & SWORD_IMMUNE_BIT) == 0)
{
enemyBytes2[i] &= PROJECTILE_IMMUNE_BIT ^ 0xFF;
}
}
}
// For future reference:
// byte4 could be used to randomize thunder immunity
// (then we must probably exclude generators so thunder doesn't destroy them)
// x... .... immune to thunder
for (int i = 0; i < allEnemies.Count; i++)
{
int index = (int)(object)allEnemies[i];
bytes[index] = enemyBytes1[i];
bytes[index + 0x24] = enemyBytes2[i];
}
}
protected static void RandomizeEnemyExp(Random r, byte[] bytes, XPEffectiveness effectiveness)
{
RandomRangeIntAttribute? range = effectiveness.GetRandomRangeInt();
if (range == null)
{
Debug.Assert(effectiveness == XPEffectiveness.VANILLA);
return;
}
int low = range.Low;
int high = range.High;
for (int i = 0; i < bytes.Length; i++)
{
int byt = bytes[i];
int nibble = byt & 0x0f;
nibble = r.Next(nibble + low, nibble + high + 1);
nibble = Math.Min(Math.Max(nibble, 0), 15);
bytes[i] = (byte)((byt & 0xf0) | nibble);
}
}
protected static int RandomInRange(Random r, double minVal, double maxVal)
{
int nextVal = (int)Math.Round(r.NextDouble() * (maxVal - minVal) + minVal);
nextVal = (int)Math.Min(nextVal, maxVal);
nextVal = (int)Math.Max(nextVal, minVal);
return nextVal;
}
protected static void RandomizeInsideArray(Random r, byte[] array, int index, double lower, double upper)
{
var vanillaVal = array[index];
int minVal = (int)(vanillaVal * lower);
int maxVal = (int)(vanillaVal * upper);
var newVal = (byte)Math.Min(r.Next(minVal, maxVal), 255);
array[index] = newVal;
}
/// <summary>
/// For a given set of bytes, set a masked portion of the value of each byte on or off (all 1's or all 0's) at a rate
/// equal to the proportion of values at the addresses that have that masked portion set to a nonzero value.
/// In effect, turn some values in a range on or off randomly in the proportion of the number of such values that are on in vanilla.
/// </summary>
/// <param name="bytes">Bytes to randomize.</param>
/// <param name="mask">What part of the byte value at each address contains the configuration bit(s) we care about.</param>
public static void RandomizeBits(Random r, byte[] bytes, int mask)
{
if (bytes.Length == 0) { return; }
int notMask = mask ^ 0xFF;
double vanillaBitSetCount = bytes.Where(b => (b & mask) != 0).Count();
//proportion of the bytes that have nonzero values in the masked portion
double fraction = vanillaBitSetCount / bytes.Length;
for (int i = 0; i < bytes.Length; i++)
{
int v = bytes[i] & notMask;
if (r.NextDouble() <= fraction)
{
v |= mask;
}
bytes[i] = (byte)v;
}
}
/// When Rebonack's HP is set to exactly 2 * your damage, it will
/// trigger a bug where you kill Rebo's horse while de-horsing him.
/// This causes an additional key to drop, as well as softlocking
/// the player if they die before killing Rebo. It seems to also
/// trigger if you have exactly damage == Rebo HP (very high damage).
///
/// This has to be called after RandomizeEnemyStats and
/// RandomizeAttackEffectiveness.
///
/// (In Vanilla Zelda 2, your sword damage is never this high.)
public void FixRebonackHorseKillBug()
{
byte[] attackValues = AttackEffectivenessTable;
byte reboHp = BossHpTable[2];
while (attackValues.Any(v => v * 2 == reboHp || v == reboHp))
{
reboHp++;
BossHpTable[2] = reboHp;
}
}
public int GetSpellCost(Collectable spell, int level)
{
var spellIndex = spell.VanillaSpellOrder();
int index = spellIndex * 8 + level - 1;
return MagicEffectivenessTable[index];
}
}