forked from Roman971/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Patches.py
2981 lines (2571 loc) · 160 KB
/
Patches.py
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import datetime
import itertools
import random
import re
import struct
import sys
import zlib
from collections.abc import Callable, Iterable
from typing import Optional, Any
from Cutscenes import patch_cutscenes, patch_wondertalk2
from Entrance import Entrance
from HintList import get_hint
from Hints import GossipText, HintArea, write_gossip_stone_hints, build_altar_hints, \
build_ganon_text, build_misc_item_hints, build_misc_location_hints, get_simple_hint_no_prefix, get_item_generic_name
from Item import Item
from ItemList import REWARD_COLORS
from ItemPool import reward_list, song_list, trade_items, child_trade_items
from Location import Location, DisableType
from LocationList import business_scrubs
from Messages import read_messages, update_message_by_id, read_shop_items, update_warp_song_text, \
write_shop_items, remove_unused_messages, make_player_message, \
add_item_messages, repack_messages, shuffle_messages, \
get_message_by_id, TextCode, new_messages, COLOR_MAP
from OcarinaSongs import patch_songs
from MQ import patch_files, File, update_dmadata, insert_space, add_relocations
from Rom import Rom
from SaveContext import SaveContext, Scenes, FlagType
from SceneFlags import build_xflag_tables, build_xflags_from_world, get_alt_list_bytes
from Sounds import move_audiobank_table
from Spoiler import Spoiler
from TextBox import line_wrap
from Utils import data_path, get_version_bytes
from World import World
from ntype import BigStream
from texture_util import ci4_rgba16patch_to_ci8, rgba16_patch
from version import __version__, base_version, branch_identifier, supplementary_version
if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
TypeAlias = str
OverrideEntry: TypeAlias = "tuple[int, int, int, int, int, int]"
def patch_rom(spoiler: Spoiler, world: World, rom: Rom) -> Rom:
with open(data_path('generated/rom_patch.txt'), 'r') as stream:
for line in stream:
address, value = [int(x, 16) for x in line.split(',')]
rom.write_int32(address, value)
rom.scan_dmadata_update()
# Binary patches of certain assets.
bin_patches = [
(data_path('title.bin'), 0x01795300), # Randomizer title screen logo
(data_path('keaton.bin'), 0x8A7C00), # Fixes the typo of "Keatan Mask" in the item select screen
]
for (bin_path, write_address) in bin_patches:
with open(bin_path, 'rb') as stream:
bytes_compressed = stream.read()
bytes_diff = zlib.decompress(bytes_compressed)
original_bytes = rom.original.buffer[write_address: write_address + len(bytes_diff)]
new_bytes = bytearray([a ^ b for a, b in zip(bytes_diff, original_bytes)])
rom.write_bytes(write_address, new_bytes)
# Load models into the extended object table.
zobj_imports = (
('object_gi_triforce', data_path('items/Triforce.zobj'), 0x193), # Triforce Piece
('object_gi_keyring', data_path('items/KeyRing.zobj'), 0x195), # Key Rings
('object_gi_warpsong', data_path('items/Note.zobj'), 0x196), # Inverted Music Note
('object_gi_chubag', data_path('items/ChuBag.zobj'), 0x197), # Bombchu Bag
('object_gi_skeyforest', data_path('items/SmallForest.zobj'), 0x199), # Small Key (Forest)
('object_gi_skeyfire', data_path('items/SmallFire.zobj'), 0x19A), # Small Key (Fire)
('object_gi_skeywater', data_path('items/SmallWater.zobj'), 0x19B), # Small Key (Water)
('object_gi_skeyspirit', data_path('items/SmallSpirit.zobj'), 0x19C), # Small Key (Spirit)
('object_gi_skeyshadow', data_path('items/SmallShadow.zobj'), 0x19D), # Small Key (Shadow)
('object_gi_skeywell', data_path('items/SmallWell.zobj'), 0x19E), # Small Key (Well)
('object_gi_skeygtg', data_path('items/SmallGTG.zobj'), 0x19F), # Small Key (GTG)
('object_gi_skeythieves', data_path('items/SmallThieves.zobj'), 0x1A0), # Small Key (Thieves)
('object_gi_skeyganon', data_path('items/SmallGanon.zobj'), 0x1A1), # Small Key (Ganon)
('object_gi_skeyTCG', data_path('items/SmallTCG.zobj'), 0x1A2), # Small Key (Chest Game)
('object_gi_bkforest', data_path('items/BossForest.zobj'), 0x1A3), # Boss Key (Forest)
('object_gi_bkfire', data_path('items/BossFire.zobj'), 0x1A4), # Boss Key (Fire)
('object_gi_bkwater', data_path('items/BossWater.zobj'), 0x1A5), # Boss Key (Water)
('object_gi_bkspirit', data_path('items/BossSpirit.zobj'), 0x1A6), # Boss Key (Spirit)
('object_gi_bkshadow', data_path('items/BossShadow.zobj'), 0x1A7), # Boss Key (Shadow)
('object_gi_abutton', data_path('items/A_Button.zobj'), 0x1A8), # A button
('object_gi_cbutton', data_path('items/C_Button_Horizontal.zobj'), 0x1A9), # C button Horizontal
('object_gi_cbutton', data_path('items/C_Button_Vertical.zobj'), 0x1AA), # C button Vertical
('object_gi_magic_meter', data_path('items/MagicMeter.zobj'), 0x1B4), # Magic Meter
)
models_to_update = []
if 'keys' in world.settings.clearer_item_models:
models_to_update.extend((
(0x0071, 0x01A2, 0x89), # Small Key (Chest Game)
(0x0095, 0x01A3, 0x8A), # Forest Temple Boss Key
(0x0096, 0x01A4, 0x8B), # Fire Temple Boss Key
(0x0097, 0x01A5, 0x8C), # Water Temple Boss Key
(0x0098, 0x01A6, 0x8D), # Spirit Temple Boss Key
(0x0099, 0x01A7, 0x8E), # Shadow Temple Boss Key
(0x009A, 0x00B9, 0x8F), # Ganon's Castle Boss Key
(0x00AF, 0x0199, 0x80), # Forest Temple Small Key
(0x00B0, 0x019A, 0x81), # Fire Temple Small Key
(0x00B1, 0x019B, 0x82), # Water Temple Small Key
(0x00B2, 0x019C, 0x83), # Spirit Temple Small Key
(0x00B3, 0x019D, 0x84), # Shadow Temple Small Key
(0x00B4, 0x019E, 0x85), # Bottom of the Well Small Key
(0x00B5, 0x019F, 0x86), # Gerudo Training Small Key
(0x00B6, 0x01A0, 0x87), # Thieves' Hideout Small Key
(0x00B7, 0x01A1, 0x88), # Ganon's Castle Small Key
))
if 'keyrings' not in world.settings.clearer_item_models:
if 'keys' in world.settings.clearer_item_models:
models_to_update.extend((
(0x00CB, 0x0199, 0x80), # Small Key Ring (Forest Temple)
(0x00CC, 0x019A, 0x81), # Small Key Ring (Fire Temple)
(0x00CD, 0x019B, 0x82), # Small Key Ring (Water Temple)
(0x00CE, 0x019C, 0x83), # Small Key Ring (Spirit Temple)
(0x00CF, 0x019D, 0x84), # Small Key Ring (Shadow Temple)
(0x00D0, 0x019E, 0x85), # Small Key Ring (Bottom of the Well)
(0x00D1, 0x019F, 0x86), # Small Key Ring (Gerudo Training Ground)
(0x00D2, 0x01A0, 0x87), # Small Key Ring (Thieves Hideout)
(0x00D3, 0x01A1, 0x88), # Small Key Ring (Ganons Castle)
(0x00D7, 0x01A2, 0x89), # Small Key Ring (Chest Game)
))
else:
models_to_update.extend((
(0x00CB, 0x00AA, 0x02), # Small Key Ring (Forest Temple)
(0x00CC, 0x00AA, 0x02), # Small Key Ring (Fire Temple)
(0x00CD, 0x00AA, 0x02), # Small Key Ring (Water Temple)
(0x00CE, 0x00AA, 0x02), # Small Key Ring (Spirit Temple)
(0x00CF, 0x00AA, 0x02), # Small Key Ring (Shadow Temple)
(0x00D0, 0x00AA, 0x02), # Small Key Ring (Bottom of the Well)
(0x00D1, 0x00AA, 0x02), # Small Key Ring (Gerudo Training Ground)
(0x00D2, 0x00AA, 0x02), # Small Key Ring (Thieves Hideout)
(0x00D3, 0x00AA, 0x02), # Small Key Ring (Ganons Castle)
(0x00D7, 0x00AA, 0x02), # Small Key Ring (Chest Game)
))
if 'silver_rupee_pouches' not in world.settings.clearer_item_models:
models_to_update.extend((
(0x00EE, 0x0198, 0x72), # Silver Rupee Pouch (Dodongos Cavern Staircase)
(0x00EF, 0x0198, 0x72), # Silver Rupee Pouch (Ice Cavern Spinning Scythe)
(0x00F0, 0x0198, 0x72), # Silver Rupee Pouch (Ice Cavern Push Block)
(0x00F1, 0x0198, 0x72), # Silver Rupee Pouch (Bottom of the Well Basement)
(0x00F2, 0x0198, 0x72), # Silver Rupee Pouch (Shadow Temple Scythe Shortcut)
(0x00F3, 0x0198, 0x72), # Silver Rupee Pouch (Shadow Temple Invisible Blades)
(0x00F4, 0x0198, 0x72), # Silver Rupee Pouch (Shadow Temple Huge Pit)
(0x00F5, 0x0198, 0x72), # Silver Rupee Pouch (Shadow Temple Invisible Spikes)
(0x00F6, 0x0198, 0x72), # Silver Rupee Pouch (Gerudo Training Ground Slopes)
(0x00F7, 0x0198, 0x72), # Silver Rupee Pouch (Gerudo Training Ground Lava)
(0x00F8, 0x0198, 0x72), # Silver Rupee Pouch (Gerudo Training Ground Water)
(0x00F9, 0x0198, 0x72), # Silver Rupee Pouch (Spirit Temple Child Early Torches)
(0x00FA, 0x0198, 0x72), # Silver Rupee Pouch (Spirit Temple Adult Boulders)
(0x00FB, 0x0198, 0x72), # Silver Rupee Pouch (Spirit Temple Lobby and Lower Adult)
(0x00FC, 0x0198, 0x72), # Silver Rupee Pouch (Spirit Temple Sun Block)
(0x00FD, 0x0198, 0x72), # Silver Rupee Pouch (Spirit Temple Adult Climb)
(0x00FE, 0x0198, 0x72), # Silver Rupee Pouch (Ganons Castle Spirit Trial)
(0x00FF, 0x0198, 0x72), # Silver Rupee Pouch (Ganons Castle Light Trial)
(0x0100, 0x0198, 0x72), # Silver Rupee Pouch (Ganons Castle Fire Trial)
(0x0101, 0x0198, 0x72), # Silver Rupee Pouch (Ganons Castle Shadow Trial)
(0x0102, 0x0198, 0x72), # Silver Rupee Pouch (Ganons Castle Water Trial)
(0x0103, 0x0198, 0x72), # Silver Rupee Pouch (Ganons Castle Forest Trial)
))
if 'warp_songs' not in world.settings.clearer_item_models:
models_to_update.extend((
(0x00BB, 0x00B6, 0x03), # Minuet of Forest
(0x00BC, 0x00B6, 0x04), # Bolero of Fire
(0x00BD, 0x00B6, 0x05), # Serenade of Water
(0x00BE, 0x00B6, 0x06), # Requiem of Spirit
(0x00BF, 0x00B6, 0x07), # Nocturne of Shadow
(0x00C0, 0x00B6, 0x08), # Prelude of Light
))
for item_id, object_id, graphic_id in models_to_update:
item = read_rom_item(rom, item_id)
item['object_id'] = object_id
item['graphic_id'] = graphic_id
write_rom_item(rom, item_id, item)
extended_objects_start = start_address = rom.dma.free_space()
for name, zobj_path, object_id in zobj_imports:
with open(zobj_path, 'rb') as stream:
obj_data = stream.read()
rom.write_bytes(start_address, obj_data)
# Add it to the extended object table
end_address = ((start_address + len(obj_data) + 0x0F) >> 4) << 4
add_to_extended_object_table(rom, object_id, start_address, end_address)
start_address = end_address
# Make new models by applying patches to existing ones
zobj_patches = (
('object_gi_hearts', 0x014D9000, 0x014DA590, 0x194, ( # Heart Container -> Double Defense
(0x1294, [0xFF, 0xCF, 0x0F]), # Exterior Primary Color
(0x12B4, [0xFF, 0x46, 0x32]), # Exterior Env Color
(0x1474, [0xFF, 0xFF, 0xFF]), # Interior Primary Color
(0x1494, [0xFF, 0xFF, 0xFF]), # Interior Env Color
(0x12A8, [0xFC, 0x17, 0x3C, 0x60, 0x15, 0x0C, 0x93, 0x7F]), # Exterior Combine Mode
)),
('object_gi_rupy', 0x01914000, 0x01914800, 0x198, ( # Huge Rupee -> Silver Rupee
(0x052C, [0xAA, 0xAA, 0xAA]), # Inner Primary Color?
(0x0534, [0x5A, 0x5A, 0x5A]), # Inner Env Color?
(0x05CC, [0xFF, 0xFF, 0xFF]), # Outer Primary Color?
(0x05D4, [0xFF, 0xFF, 0xFF]), # Outer Env Color?
)),
('object_gi_egg', 0x015B6000, 0x015B7320, 0x1B5, ( # Weird Egg -> Pink Easter Egg
(0x0FF4, [0xDB, 0xA9, 0xD8]), # Primary Color
(0x0FFC, [0xD1, 0x7B, 0xCC]), # Env Color
)),
('object_gi_egg', 0x015B6000, 0x015B7320, 0x1B6, ( # Weird Egg -> Orange Easter Egg
(0x0FF4, [0xDB, 0xA9, 0x77]), # Primary Color
(0x0FFC, [0xD1, 0x7B, 0x25]), # Env Color
)),
('object_gi_egg', 0x015B6000, 0x015B7320, 0x1B7, ( # Weird Egg -> Green Easter Egg
(0x0FF4, [0x77, 0xDB, 0x77]), # Primary Color
(0x0FFC, [0x25, 0xD1, 0x25]), # Env Color
)),
('object_gi_egg', 0x015B6000, 0x015B7320, 0x1B8, ( # Weird Egg -> Blue Easter Egg
(0x0FF4, [0x77, 0x77, 0xDB]), # Primary Color
(0x0FFC, [0x25, 0x25, 0xD1]), # Env Color
)),
)
# Add the new models to the extended object file.
for name, start, end, object_id, patches in zobj_patches:
end_address = start_address + end - start
rom.buffer[start_address:end_address] = rom.buffer[start:end]
# Apply patches
for offset, patch in patches:
rom.write_bytes(start_address + offset, patch)
# Add it to the extended object table
add_to_extended_object_table(rom, object_id, start_address, end_address)
start_address = end_address
# Make new model files by splitting existing ones to fit into the get item memory slot
zobj_splits = (
('object_gi_jewel_emerald', 0x0145A000, 0x0145D680, (0x1240, 0x10E0), 0x1AB), # Kokiri Emerald
('object_gi_jewel_ruby', 0x0145A000, 0x0145D680, (0x20A0, 0x1FB0), 0x1AC), # Goron Ruby
('object_gi_jewel_sapphire', 0x0145A000, 0x0145D680, (0x3530, 0x3370), 0x1AD), # Zora Sapphire
('object_gi_medal_light', 0x014BB000, 0x014C0370, (0x5220, 0x0E18), 0x1AE), # Light Medallion
('object_gi_medal_forest', 0x014BB000, 0x014C0370, (0x0CB0, 0x0E18), 0x1AF), # Forest Medallion
('object_gi_medal_fire', 0x014BB000, 0x014C0370, (0x1AF0, 0x0E18), 0x1B0), # Fire Medallion
('object_gi_medal_water', 0x014BB000, 0x014C0370, (0x2830, 0x0E18), 0x1B1), # Water Medallion
('object_gi_medal_shadow', 0x014BB000, 0x014C0370, (0x4330, 0x0E18), 0x1B2), # Shadow Medallion
('object_gi_medal_spirit', 0x014BB000, 0x014C0370, (0x3610, 0x0E18), 0x1B3), # Spirit Medallion
)
for name, start, end, offsets, object_id in zobj_splits:
obj_file = File(name, start, end)
seen = {}
out = []
out_size = 0
for offset in offsets:
i = offset
while True:
data = rom.read_int32(obj_file.start + i)
op = data >> 24
i += 8
if op == 0xdf:
size = i - offset
break
segment = BigStream(rom.read_bytes(obj_file.start + offset, size))
def copy(addr, size):
nonlocal seen
nonlocal out_size
seg = addr >> 24
if seg != 0x06:
return addr
addr &= 0xffffff
seenAddr = seen.get(addr)
if seenAddr is not None:
return seenAddr
newAddr = out_size | 0x0600_0000
out_size += size
out.extend(rom.read_bytes(obj_file.start + addr, size))
seen[addr] = newAddr
return newAddr
for i in range(0, size, 8):
data = segment.read_int32(i)
op = data >> 24
if op == 0x01: # Vertices
count = (data >> 12) & 0xff
addr = segment.read_int32(i + 4)
newAddr = copy(addr, count * 0x10)
segment.write_int32(i + 4, newAddr)
elif op == 0xfd: # Texture or palette
data2 = segment.read_int32(i + 8 * 1)
op2 = data2 >> 24
if op2 == 0xf5: # Texture
fmt = (data >> 16) & 0xff
if fmt in (0x50, 0x90):
bpp = 4
elif fmt == 0x10:
bpp = 16
else:
raise ValueError(f'Unknown texture format 0x{fmt:02x}')
data3 = segment.read_int32(i + 8 * 6 + 4)
w = (((data3 >> 12) & 0xfff) / 4) + 1
h = (((data3 >> 0) & 0xfff) / 4) + 1
addr = segment.read_int32(i + 4)
newAddr = copy(addr, int((w * h * bpp) / 8))
segment.write_int32(i + 4, newAddr)
elif op2 == 0xe8: # Palette
addr = segment.read_int32(i + 4)
newAddr = copy(addr, 32)
segment.write_int32(i + 4, newAddr)
out_size += size
out.extend(segment.buffer)
if out_size % 16:
extra_size = 16 - (out_size % 16)
extra_buf = [0] * extra_size
out_size += extra_size
out.extend(extra_buf)
rom.write_bytes(start_address, out)
# Add it to the extended object table
end_address = ((start_address + len(out) + 0x0F) >> 4) << 4
add_to_extended_object_table(rom, object_id, start_address, end_address)
start_address = end_address
# Add the extended objects data to the DMA table.
rom.update_dmadata_record_by_key(None, extended_objects_start, end_address)
# Create the textures for pots/crates. Note: No copyrighted material can be distributed w/ the randomizer. Because of this, patch files are used to create the new textures from the original texture in ROM.
# Apply patches for custom textures for pots and crates and add as new files in rom
# Crates are ci4 textures in the normal ROM but for pot/crate textures match contents were upgraded to ci8 to support more colors
# Pot textures are rgba16
# texture list. See textures.h for texture IDs
# ID, texture_name, Rom Address CI4 Pallet Addr Size Patching function Patch file (None for default)
crate_textures = [
( 1, 'texture_pot_gold', 0x01738000, None, 2048, rgba16_patch, 'textures/pot/pot_gold_rgba16_patch.bin'),
( 2, 'texture_pot_key', 0x01738000, None, 2048, rgba16_patch, 'textures/pot/pot_key_rgba16_patch.bin'),
( 3, 'texture_pot_bosskey', 0x01738000, None, 2048, rgba16_patch, 'textures/pot/pot_bosskey_rgba16_patch.bin'),
( 4, 'texture_pot_skull', 0x01738000, None, 2048, rgba16_patch, 'textures/pot/pot_skull_rgba16_patch.bin'),
( 5, 'texture_crate_default', 0x18B6020, 0x018B6000, 4096, ci4_rgba16patch_to_ci8, None),
( 6, 'texture_crate_gold', 0x18B6020, 0x018B6000, 4096, ci4_rgba16patch_to_ci8, 'textures/crate/crate_gold_rgba16_patch.bin'),
( 7, 'texture_crate_key', 0x18B6020, 0x018B6000, 4096, ci4_rgba16patch_to_ci8, 'textures/crate/crate_key_rgba16_patch.bin'),
( 8, 'texture_crate_skull', 0x18B6020, 0x018B6000, 4096, ci4_rgba16patch_to_ci8, 'textures/crate/crate_skull_rgba16_patch.bin'),
( 9, 'texture_crate_bosskey', 0x18B6020, 0x018B6000, 4096, ci4_rgba16patch_to_ci8, 'textures/crate/crate_bosskey_rgba16_patch.bin'),
(10, 'texture_smallcrate_gold', 0xF7ECA0, None, 2048, rgba16_patch, 'textures/crate/smallcrate_gold_rgba16_patch.bin' ),
(11, 'texture_smallcrate_key', 0xF7ECA0, None, 2048, rgba16_patch, 'textures/crate/smallcrate_key_rgba16_patch.bin'),
(12, 'texture_smallcrate_skull', 0xF7ECA0, None, 2048, rgba16_patch, 'textures/crate/smallcrate_skull_rgba16_patch.bin'),
(13, 'texture_smallcrate_bosskey', 0xF7ECA0, None, 2048, rgba16_patch, 'textures/crate/smallcrate_bosskey_rgba16_patch.bin'),
(18, "texture_chest_front_gilded", 0xFEC798, None, 4096, rgba16_patch, 'textures/chest/chest_front_gilded_rgba16_patch.bin'),
(19, "texture_chest_base_gilded", 0xFED798, None, 2048, rgba16_patch, 'textures/chest/chest_base_gilded_rgba16_patch.bin'),
(20, "texture_chest_front_silver", 0xFEC798, None, 4096, rgba16_patch, 'textures/chest/chest_front_silver_rgba16_patch.bin'),
(21, "texture_chest_base_silver", 0xFED798, None, 2048, rgba16_patch, 'textures/chest/chest_base_silver_rgba16_patch.bin'),
(22, "texture_chest_front_skull", 0xFEC798, None, 4096, rgba16_patch, 'textures/chest/chest_front_skull_rgba16_patch.bin'),
(23, "texture_chest_base_skull", 0xFED798, None, 2048, rgba16_patch, 'textures/chest/chest_base_skull_rgba16_patch.bin'),
(24, "texture_chest_front_heart", 0xFEC798, None, 4096, rgba16_patch, 'textures/chest/chest_front_heart_rgba16_patch.bin'),
(25, "texture_chest_base_heart", 0xFED798, None, 2048, rgba16_patch, 'textures/chest/chest_base_heart_rgba16_patch.bin'),
(26, 'texture_pot_side_heart', 0x01738000, None, 2048, rgba16_patch, 'textures/pot/pot_side_heart_rgba16_patch.bin'),
(27, 'texture_pot_top_heart', 0x01739000, None, 256, rgba16_patch, 'textures/pot/pot_top_heart_rgba16_patch.bin'),
(28, 'texture_crate_heart', 0x18B6020, 0x018B6000, 4096, ci4_rgba16patch_to_ci8, 'textures/crate/crate_heart_rgba16_patch.bin'),
(29, 'texture_smallcrate_heart', 0xF7ECA0, None, 2048, rgba16_patch, 'textures/crate/smallcrate_heart_rgba16_patch.bin'),
]
# Loop through the textures and apply the patch. Add the new textures as a new file in rom.
extended_textures_start = start_address = rom.dma.free_space()
for texture_id, texture_name, rom_address_base, rom_address_palette, size, func, patch_file in crate_textures:
# Apply the texture patch. Resulting texture will be stored in texture_data as a bytearray
texture_data = func(rom, rom_address_base, rom_address_palette, size, data_path(patch_file) if patch_file else None)
rom.write_bytes(start_address, texture_data) # write the bytes to our new file
end_address = ((start_address + len(texture_data) + 0x0F) >> 4) << 4
# update the texture table with the rom addresses of the texture files
entry = read_rom_texture(rom, texture_id)
entry['file_vrom_start'] = start_address
entry['file_size'] = end_address - start_address
write_rom_texture(rom, texture_id, entry)
start_address = end_address
# Add the extended texture data to the DMA table.
rom.update_dmadata_record_by_key(None, extended_textures_start, end_address)
save_context = SaveContext()
# Create an option so that recovery hearts no longer drop by changing the code which checks Link's health when an item is spawned.
if world.settings.no_collectible_hearts:
symbol = rom.sym('NO_COLLECTIBLE_HEARTS')
rom.write_byte(symbol, 0x01)
# Remove color commands inside certain object display lists
rom.write_int32s(0x1455818, [0x00000000, 0x00000000, 0x00000000, 0x00000000]) # Small Key
rom.write_int32s(0x14B9CB8, [0x00000000, 0x00000000, 0x00000000, 0x00000000]) # Boss Key (Key)
rom.write_int32s(0x14B9F20, [0x00000000, 0x00000000, 0x00000000, 0x00000000]) # Boss Key (Gem)
# Force language to be English in the event a Japanese rom was submitted
rom.write_byte(0x3E, 0x45)
rom.force_patch.append(0x3E)
# Increase the instance size of Bombchus prevent the heap from becoming corrupt when
# a Dodongo eats a Bombchu. Does not fix stale pointer issues with the animation
rom.write_int32(0xD6002C, 0x1F0)
# Can always return to youth
rom.write_byte(0xCB6844, 0x35)
rom.write_byte(0x253C0E2, 0x03) # Moves sheik from pedestal
# Fix Ice Cavern Alcove Camera
if not world.dungeon_mq['Ice Cavern']:
rom.write_byte(0x2BECA25, 0x01)
rom.write_byte(0x2BECA2D, 0x01)
# Fix GS rewards to be static
rom.write_int32(0xEA3934, 0)
rom.write_bytes(0xEA3940, [0x10, 0x00])
# Fix horseback archery rewards to be static
rom.write_byte(0xE12BA5, 0x00)
rom.write_byte(0xE12ADD, 0x00)
# Fix deku theater rewards to be static
rom.write_bytes(0xEC9A7C, [0x00, 0x00, 0x00, 0x00]) # Sticks
rom.write_byte(0xEC9CD5, 0x00) # Nuts
# Fix deku scrub who sells stick upgrade
rom.write_bytes(0xDF8060, [0x00, 0x00, 0x00, 0x00])
# Fix deku scrub who sells nut upgrade
rom.write_bytes(0xDF80D4, [0x00, 0x00, 0x00, 0x00])
# Fix rolling goron as child reward to be static
rom.write_bytes(0xED2960, [0x00, 0x00, 0x00, 0x00])
# Fix proximity text boxes (Navi) (Part 1)
rom.write_bytes(0xDF8B84, [0x00, 0x00, 0x00, 0x00])
# Fix final magic bean to cost 99
rom.write_byte(0xE20A0F, 0x63)
rom.write_bytes(0x94FCDD, [0x08, 0x39, 0x39])
# Remove locked door to Boss Key Chest in Fire Temple
if not world.keysanity and (world.settings.shuffle_base_item_pool or world.settings.shuffle_smallkeys != 'vanilla') and not world.dungeon_mq['Fire Temple']:
rom.write_byte(0x22D82B7, 0x3F)
# Remove the unused locked door in water temple
if not world.dungeon_mq['Water Temple']:
rom.write_byte(0x25B8197, 0x3F)
if world.settings.free_bombchu_drops:
rom.write_int32(rom.sym('FREE_BOMBCHU_DROPS'), 1)
# include version info
rom.write_bytes(rom.sym('CFG_RANDO_VERSION_MAJOR'), get_version_bytes(base_version, branch_identifier, supplementary_version))
# initialize world ID
rom.write_byte(rom.sym('PLAYER_ID'), world.id + 1)
# set fallback player names
for player_name_id in range(256):
if player_name_id < 10:
fallback_name = [0xBA, 0xD0, 0xC5, 0xDD, 0xC9, 0xD6, 0xDF, player_name_id] # Player N
elif player_name_id < 100:
fallback_name = [0xBA, 0xD0, 0xC5, 0xDD, 0xC9, 0xD6, *divmod(player_name_id, 10)] # PlayerNN
else:
tens, ones = divmod(player_name_id, 10)
fallback_name = [0xBA, 0xD0, 0xC5, 0xDD, 0xD6, *divmod(tens, 10), ones] # PlayrNNN
rom.write_bytes(rom.sym('PLAYER_NAMES') + 8 * player_name_id, fallback_name)
# show seed info on file select screen
def make_bytes(txt: str, size: int) -> list[int]:
bytes = list(ord(c) for c in txt[:size-1]) + [0] * size
return bytes[:size]
line_len = 21
version_str = "version " + __version__
if len(version_str) > line_len:
version_str = "ver. " + __version__
if len(version_str) > line_len:
version_str = "v. " + __version__
if len(version_str) > line_len:
version_str = "v" + __version__
rom.write_bytes(rom.sym('VERSION_STRING_TXT'), make_bytes(version_str, 25))
if world.settings.create_spoiler:
rom.write_byte(rom.sym('SPOILER_AVAILABLE'), 0x01)
if world.settings.enable_distribution_file:
rom.write_byte(rom.sym('PLANDOMIZER_USED'), 0x01)
if world.settings.world_count > 1:
world_str = f"{world.id + 1} of {world.settings.world_count}"
else:
world_str = ''
rom.write_bytes(rom.sym('WORLD_STRING_TXT'), make_bytes(world_str, 12))
time_str = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M") + " UTC"
rom.write_bytes(rom.sym('TIME_STRING_TXT'), make_bytes(time_str, 25))
if world.settings.web_id > 0:
web_id_str = f'{world.settings.web_id:10}'
else:
web_id_str = ''
rom.write_bytes(rom.sym('WEB_ID_STRING_TXT'), make_bytes(web_id_str, 12))
if world.settings.show_seed_info:
rom.write_byte(rom.sym('CFG_SHOW_SETTING_INFO'), 0x01)
else:
rom.write_byte(rom.sym('CFG_SHOW_SETTING_INFO'), 0x00)
custom_msg = world.settings.user_message.strip()[:42]
if len(custom_msg) <= line_len:
msg = [custom_msg, ""]
else:
# try to split message
msg = [custom_msg[:line_len], custom_msg[line_len:]]
part1 = msg[0].split(' ')
if len(part1[-1]) + len(msg[1]) < line_len:
msg = [" ".join(part1[:-1]), part1[-1] + msg[1]]
else:
# Is it a URL?
part1 = msg[0].split('/')
if len(part1[-1]) + len(msg[1]) < line_len:
msg = ["/".join(part1[:-1]) + "/", part1[-1] + msg[1]]
for idx,part in enumerate(msg):
part_bytes = list(ord(c) for c in part) + [0] * (line_len+1)
part_bytes = part_bytes[:(line_len+1)]
symbol = rom.sym('CFG_CUSTOM_MESSAGE_{}'.format(idx+1))
rom.write_bytes(symbol, part_bytes)
# Change graveyard graves to not allow grabbing on to the ledge
# new floor type definition in the Graveyard
# first byte 0x24 causes you to fall off instead of jumping or grabbing the ledge
# otherwise identical to the originally used one
# overwrites zero-padding far past the end of the collision type array
rom.write_int32s(0x2026C04, [0x24000004, 0x00000FC8])
# indices from the array of polygons
floors_surrounding_graves = (range(494, 502), # fairy fountain
range(502, 510), # HP grave
range(487, 494), # Dampé's grave
range(651, 659)) # royal tomb
for grave in floors_surrounding_graves:
for poly in grave:
# use the new floor type
rom.write_int16(0x2020494 + poly * 0x10, 0x0D0D) # replaces 0x0014
grave_walls = (range(613, 621), # fairy fountain
range(623, 631), # HP grave
range(633, 641), # Dampé's grave
range(643, 651)) # royal tomb
for grave in grave_walls:
for poly in grave:
# use existing wall type that prevents grabbing ledges from midair
# otherwise identical to the originally used one
rom.write_int16(0x2020494 + poly * 0x10, 0x000F) # replaces 0x0000
# Fix Castle Courtyard to check for meeting Zelda, not Zelda fleeing, to block you
rom.write_bytes(0xCD5E76, [0x0E, 0xDC])
rom.write_bytes(0xCD5E12, [0x0E, 0xDC])
# Some types of locations (boss rewards, songs, and the fairy ocarina) have special behavior,
# but need to use the normal Get Item mechanism if shuffled into the main item pool.
rewards_as_items = (
world.settings.shuffle_dungeon_rewards not in ('vanilla', 'reward')
or world.distribution.rewards_as_items
or any(name in reward_list and record.count for name, record in world.settings.starting_items.items())
)
if rewards_as_items:
rom.write_byte(rom.sym('REWARDS_AS_ITEMS'), 1)
songs_as_items = (
world.settings.shuffle_song_items not in ('vanilla', 'song')
or world.distribution.songs_as_items
or any(name in song_list and record.count for name, record in world.settings.starting_items.items())
or world.settings.shuffle_individual_ocarina_notes
)
if songs_as_items:
rom.write_byte(rom.sym('SONGS_AS_ITEMS'), 1)
if world.settings.shuffle_ocarinas:
rom.write_byte(rom.sym('OCARINAS_SHUFFLED'), 0x01)
patch_cutscenes(rom, songs_as_items, world.settings)
patch_wondertalk2(rom, world.settings)
# Speed Pushing of All Pushable Objects (other than armos statues, which are handled in ASM)
rom.write_bytes(0xDD2B86, [0x40, 0x80]) # block speed
rom.write_bytes(0xDD2D26, [0x00, 0x01]) # block delay
rom.write_bytes(0xDD9682, [0x40, 0x80]) # milk crate speed
rom.write_bytes(0xDD981E, [0x00, 0x01]) # milk crate delay
rom.write_bytes(0xCE1BD0, [0x40, 0x80, 0x00, 0x00]) # amy puzzle speed
rom.write_bytes(0xCE0F0E, [0x00, 0x01]) # amy puzzle delay
rom.write_bytes(0xC77CA8, [0x40, 0x80, 0x00, 0x00]) # fire block speed
rom.write_bytes(0xC770C2, [0x00, 0x01]) # fire block delay
rom.write_bytes(0xCC5DBC, [0x29, 0xE1, 0x00, 0x01]) # forest basement puzzle delay
rom.write_bytes(0xDBCF70, [0x2B, 0x01, 0x00, 0x00]) # spirit cobra mirror startup
rom.write_bytes(0xDBCF70, [0x2B, 0x01, 0x00, 0x01]) # spirit cobra mirror delay
rom.write_bytes(0xDBA230, [0x28, 0x41, 0x00, 0x19]) # truth spinner speed
rom.write_bytes(0xDBA3A4, [0x24, 0x18, 0x00, 0x00]) # truth spinner delay
# Remove remaining owls
rom.write_bytes(0x1FE30CE, [0x01, 0x4B])
rom.write_bytes(0x1FE30DE, [0x01, 0x4B])
rom.write_bytes(0x1FE30EE, [0x01, 0x4B])
rom.write_bytes(0x205909E, [0x00, 0x3F])
rom.write_byte(0x2059094, 0x80)
# Zora moves quickly
rom.write_bytes(0xE56924, [0x00, 0x00, 0x00, 0x00])
# Ruto never disappears from Jabu Jabu's Belly
rom.write_byte(0xD01EA3, 0x00)
# Shift octorok in jabu forward
rom.write_bytes(0x275906E, [0xFF, 0xB3, 0xFB, 0x20, 0xF9, 0x56])
# Move fire/forest temple switches down 1 unit to make it easier to press
rom.write_bytes(0x24860A8, [0xFC, 0xF4]) # forest basement 1
rom.write_bytes(0x24860C8, [0xFC, 0xF4]) # forest basement 2
rom.write_bytes(0x24860E8, [0xFC, 0xF4]) # forest basement 3
rom.write_bytes(0x236C148, [0x11, 0x93]) # fire hammer room
# Speed up magic arrow equips
rom.write_int16(0xBB84CE, 0x0000) # Skips the initial growing glowing orb phase
rom.write_byte(0xBB84B7, 0xFF) # Set glowing orb above magic arrow to be small sized immediately
rom.write_byte(0xBB84CB, 0x01) # Sets timer for holding icon above magic arrow (1 frame)
rom.write_byte(0xBB7E67, 0x04) # speed up magic arrow icon -> bow icon interpolation (4 frames)
rom.write_byte(0xBB8957, 0x01) # Sets timer for holding icon above bow (1 frame)
rom.write_byte(0xBB854B, 0x05) # speed up bow icon -> c button interpolation (5 frames)
# Poacher's Saw no longer messes up Forest Stage
rom.write_bytes(0xAE72CC, [0x00, 0x00, 0x00, 0x00])
# Change Prelude CS to check for medallion
rom.write_bytes(0x00C805E6, [0x00, 0xA6])
rom.write_bytes(0x00C805F2, [0x00, 0x01])
# Change Nocturne CS to check for medallions
rom.write_bytes(0x00ACCD8E, [0x00, 0xA6])
rom.write_bytes(0x00ACCD92, [0x00, 0x01])
rom.write_bytes(0x00ACCD9A, [0x00, 0x02])
rom.write_bytes(0x00ACCDA2, [0x00, 0x04])
# Change King Zora to move even if Zora Sapphire is in inventory
rom.write_bytes(0x00E55BB0, [0x85, 0xCE, 0x8C, 0x3C])
rom.write_bytes(0x00E55BB4, [0x84, 0x4F, 0x0E, 0xDA])
# Remove extra Forest Temple medallions
rom.write_bytes(0x00D4D37C, [0x00, 0x00, 0x00, 0x00])
# Remove extra Fire Temple medallions
rom.write_bytes(0x00AC9754, [0x00, 0x00, 0x00, 0x00])
rom.write_bytes(0x00D0DB8C, [0x00, 0x00, 0x00, 0x00])
# Remove extra Water Temple medallions
rom.write_bytes(0x00D57F94, [0x00, 0x00, 0x00, 0x00])
# Remove extra Spirit Temple medallions
rom.write_bytes(0x00D370C4, [0x00, 0x00, 0x00, 0x00])
rom.write_bytes(0x00D379C4, [0x00, 0x00, 0x00, 0x00])
# Remove extra Shadow Temple medallions
rom.write_bytes(0x00D116E0, [0x00, 0x00, 0x00, 0x00])
# Change Mido, Saria, and Kokiri to check for Deku Tree complete flag
# bitwise pointer for 0x80
kokiri_addresses = [0xE52836, 0xE53A56, 0xE51D4E, 0xE51F3E, 0xE51D96, 0xE51E1E, 0xE51E7E, 0xE51EDE, 0xE51FC6, 0xE51F96, 0xE293B6, 0xE29B8E, 0xE62EDA, 0xE630D6, 0xE633AA, 0xE6369E]
for kokiri in kokiri_addresses:
rom.write_bytes(kokiri, [0x8C, 0x0C])
# Kokiri
rom.write_bytes(0xE52838, [0x94, 0x48, 0x0E, 0xD4])
rom.write_bytes(0xE53A58, [0x94, 0x49, 0x0E, 0xD4])
rom.write_bytes(0xE51D50, [0x94, 0x58, 0x0E, 0xD4])
rom.write_bytes(0xE51F40, [0x94, 0x4B, 0x0E, 0xD4])
rom.write_bytes(0xE51D98, [0x94, 0x4B, 0x0E, 0xD4])
rom.write_bytes(0xE51E20, [0x94, 0x4A, 0x0E, 0xD4])
rom.write_bytes(0xE51E80, [0x94, 0x59, 0x0E, 0xD4])
rom.write_bytes(0xE51EE0, [0x94, 0x4E, 0x0E, 0xD4])
rom.write_bytes(0xE51FC8, [0x94, 0x49, 0x0E, 0xD4])
rom.write_bytes(0xE51F98, [0x94, 0x58, 0x0E, 0xD4])
# Saria
rom.write_bytes(0xE293B8, [0x94, 0x78, 0x0E, 0xD4])
rom.write_bytes(0xE29B90, [0x94, 0x68, 0x0E, 0xD4])
# Mido
rom.write_bytes(0xE62EDC, [0x94, 0x6F, 0x0E, 0xD4])
rom.write_bytes(0xE630D8, [0x94, 0x4F, 0x0E, 0xD4])
rom.write_bytes(0xE633AC, [0x94, 0x68, 0x0E, 0xD4])
rom.write_bytes(0xE636A0, [0x94, 0x48, 0x0E, 0xD4])
# Change adult Kokiri Forest to check for Forest Temple complete flag
rom.write_bytes(0xE5369E, [0xB4, 0xAC])
rom.write_bytes(0xD5A83C, [0x80, 0x49, 0x0E, 0xDC])
# Change adult Goron City to check for Fire Temple complete flag
rom.write_bytes(0xED59DC, [0x80, 0xC9, 0x0E, 0xDC])
# Change Pokey to check DT complete flag
rom.write_bytes(0xE5400A, [0x8C, 0x4C])
rom.write_bytes(0xE5400E, [0xB4, 0xA4])
if world.settings.open_forest:
rom.write_bytes(0xE5401C, [0x14, 0x0B])
# Move Link spawn 40 units forwards to prevent Pokey trap
rom.write_byte(0x206F0C7, 0xA3)
# Remove the check on the number of days that passed for claim check.
rom.write_bytes(0xED4470, [0x00, 0x00, 0x00, 0x00])
rom.write_bytes(0xED4498, [0x00, 0x00, 0x00, 0x00])
# Fixed reward order for Bombchu Bowling
rom.write_bytes(0xE2D440, [0x24, 0x19, 0x00, 0x00])
# Offset kakariko carpenter starting position
rom.write_bytes(0x1FF93A4,
[0x01, 0x8D, 0x00, 0x11, 0x01, 0x6C, 0xFF, 0x92, 0x00, 0x00, 0x01, 0x78, 0xFF, 0x2E, 0x00, 0x00,
0x00, 0x03, 0xFD, 0x2B, 0x00, 0xC8, 0xFF, 0xF9, 0xFD, 0x03, 0x00, 0xC8, 0xFF, 0xA9, 0xFD, 0x5D,
0x00, 0xC8, 0xFE, 0x5F]) # re-order the carpenter's path
rom.write_byte(0x1FF93D0, 0x06) # set the path points to 6
rom.write_bytes(0x20160B6, [0x01, 0x8D, 0x00, 0x11, 0x01, 0x6C]) # set the carpenter's start position
# Give hp after first ocarina minigame round
rom.write_bytes(0xDF2204, [0x24, 0x03, 0x00, 0x02])
# Allow owl to always carry the kid down Death Mountain
rom.write_bytes(0xE304F0, [0x24, 0x0E, 0x00, 0x01])
# Fix Vanilla Dodongo's Cavern Gossip Stone to not use a permanent flag for the fairy
if not world.dungeon_mq['Dodongos Cavern']:
rom.write_byte(0x1F281FE, 0x38)
# Fix "...???" textbox outside Child Colossus Fairy to use the right flag and disappear once the wall is destroyed
rom.write_byte(0x21A026F, 0xDD)
# Forbid Sun's Song from a bunch of cutscenes
Suns_scenes = [0x2016FC9, 0x2017219, 0x20173D9, 0x20174C9, 0x2017679, 0x20C1539, 0x20C15D9, 0x21A0719, 0x21A07F9, 0x2E90129, 0x2E901B9, 0x2E90249, 0x225E829, 0x225E939, 0x306D009]
for address in Suns_scenes:
rom.write_byte(address,0x01)
# Tell Sheik at Ice Cavern we are always an Adult
rom.write_int32(0xC7B9C0, 0x00000000)
rom.write_int32(0xC7BAEC, 0x00000000)
rom.write_int32(0xc7BCA4, 0x00000000)
# Speed dig text for Dampe
rom.write_bytes(0x9532F8, [0x08, 0x08, 0x08, 0x59])
# Make item descriptions into a single box
short_item_descriptions = [0x92EC84, 0x92F9E3, 0x92F2B4, 0x92F37A, 0x92F513, 0x92F5C6, 0x92E93B, 0x92EA12]
for address in short_item_descriptions:
rom.write_byte(address, 0x02)
exit_updates = []
def generate_exit_lookup_table() -> dict[int, list[int]]:
# Assumes that the last exit on a scene's exit list cannot be 0000
exit_table = {
0x0028: [0xAC95C2], # Jabu with the fish is entered from a cutscene hardcode
}
def add_scene_exits(scene_start: int, offset: int = 0) -> None:
current = scene_start + offset
exit_list_start_off = 0
exit_list_end_off = 0
command = 0
while command != 0x14:
command = rom.read_byte(current)
if command == 0x18: # Alternate header list
header_list = scene_start + (rom.read_int32(current + 4) & 0x00FFFFFF)
for alt_id in range(0,3):
header_offset = rom.read_int32(header_list) & 0x00FFFFFF
if header_offset != 0:
add_scene_exits(scene_start, header_offset)
header_list += 4
if command == 0x13: # Exit List
exit_list_start_off = rom.read_int32(current + 4) & 0x00FFFFFF
if command == 0x0F: # Lighting list, follows exit list
exit_list_end_off = rom.read_int32(current + 4) & 0x00FFFFFF
current += 8
if exit_list_start_off == 0 or exit_list_end_off == 0:
return
# calculate the exit list length
list_length = (exit_list_end_off - exit_list_start_off) // 2
last_id = rom.read_int16(scene_start + exit_list_end_off - 2)
if last_id == 0:
list_length -= 1
# update
addr = scene_start + exit_list_start_off
for _ in range(0, list_length):
index = rom.read_int16(addr)
if index not in exit_table:
exit_table[index] = []
exit_table[index].append(addr)
addr += 2
scene_table = 0x00B71440
for scene in range(0x00, 0x65):
if scene in (0x45, 0x46):
# skip castle hedge maze scenes to avoid Ganon's Castle ER messing with the exit
continue
scene_start = rom.read_int32(scene_table + (scene * 0x14))
add_scene_exits(scene_start)
return exit_table
if world.settings.shuffle_bosses != 'off':
# Credit to rattus128 for this ASM block.
# Gohma's save/death warp is optimized to use immediate 0 for the
# deku tree respawn. Use the delay slot before the switch table
# to hold Gohma's jump entrance as actual data so we can substitute
# the entrance index later.
rom.write_int32(0xB06290, 0x240E0000) # li t6, 0
rom.write_int32(0xB062B0, 0xAE0E0000) # sw t6, 0(s0)
rom.write_int32(0xBC60AC, 0x24180000) # li t8, 0
rom.write_int32(0xBC6160, 0x24180000) # li t8, 0
rom.write_int32(0xBC6168, 0xAD380000) # sw t8, 0(t1)
# Credit to engineer124
# Update the Jabu-Jabu Boss Exit to actually useful coordinates (and to load the correct room)
rom.write_int16(0x273E08E, 0xF7F4) # Z coordinate of Jabu Boss Door Spawn
rom.write_byte(0x273E27B, 0x05) # Set Spawn Room to be correct
# Update the Water Temple Boss Exit to load the correct room
rom.write_byte(0x25B82E3, 0x0B)
def set_entrance_updates(entrances: Iterable[Entrance]) -> None:
for entrance in entrances:
if entrance.data is None or entrance.replaces is None or entrance.replaces.data is None:
continue
new_entrance = entrance.data
replaced_entrance = (entrance.replaces or entrance).data
# Fixup save/quit and death warping entrance IDs on bosses.
if 'savewarp_addresses' in replaced_entrance:
savewarp = (entrance.replaces or entrance).reverse.parent_region.savewarp.replaces
if savewarp is not None:
savewarp = savewarp.data['index']
for address in replaced_entrance['savewarp_addresses']:
rom.write_int16(address, savewarp)
for address in new_entrance.get('addresses', []):
rom.write_int16(address, replaced_entrance.get('child_index', replaced_entrance['index']))
if entrance.type == 'BlueWarp' and replaced_entrance['index'] < 0x1000:
# Blue warps have multiple hardcodes leading to them. The good news is
# the blue warps (excluding deku sprout and lake fill special cases) each
# have a nice consistent 4-entry in the table we can just shuffle. So just
# catch all the hardcode with entrance table rewrite. This covers the
# Forest temple and Water temple blue warp revisits. Deku sprout remains
# vanilla as it never took you to the exit and the lake fill is handled
# above by removing the cutscene completely. Child has problems with Adult
# blue warps, so always use the return entrance if a child.
#
# This method does not work with mixed entrance pools between dungeons
# and grottos as grotto entrances are not in the entrance table.
# Grotto entrance indices always have the format 0x1000 + grotto ID or
# 0x2000 + grotto ID, which is greater than any entrance table index.
# If the dungeon is in a grotto, edit the blue warp hardcodes to the grotto
# entrance index, otherwise use the "normal" entrance table method.
# This runs after and overrides the cutscene edits for Forest Temple
# and Water Temple if needed.
exit_updates.append((new_entrance['index'], replaced_entrance.get('child_index', replaced_entrance['index'])))
exit_updates.append((new_entrance['index'] + 1, replaced_entrance.get('child_index', replaced_entrance['index']) + 1))
exit_updates.append((new_entrance['index'] + 2, replaced_entrance['index'] + 2))
exit_updates.append((new_entrance['index'] + 3, replaced_entrance['index'] + 3))
elif entrance.type != 'Grotto':
exit_updates.append((new_entrance['index'], replaced_entrance.get('child_index', replaced_entrance['index'])))
exit_table = generate_exit_lookup_table()
if world.disable_trade_revert:
# Disable trade quest timers and prevent trade items from ever reverting
rom.write_byte(rom.sym('DISABLE_TIMERS'), 0x01)
rom.write_int16s(0xB6D460, [0x0030, 0x0035, 0x0036]) # Change trade items revert table to prevent all reverts
if world.settings.adult_trade_shuffle or world.settings.item_pool_value in ('plentiful', 'ludicrous'):
rom.write_byte(rom.sym('CFG_ADULT_TRADE_SHUFFLE'), 0x01)
move_fado_in_lost_woods(rom)
if world.settings.shuffle_child_trade or world.settings.logic_rules == 'glitched':
rom.write_byte(rom.sym('CFG_CHILD_TRADE_SHUFFLE'), 0x01)
if world.settings.shuffle_overworld_entrances:
rom.write_byte(rom.sym('OVERWORLD_SHUFFLED'), 1)
# Prevent the ocarina cutscene from leading straight to hyrule field
rom.write_byte(rom.sym('OCARINAS_SHUFFLED'), 1)
# Combine all fence hopping LLR exits to lead to the main LLR exit
for k in (0x028A, 0x028E, 0x0292): # Southern, Western, Eastern Gates
exit_table[0x01F9] += exit_table[k] # Hyrule Field entrance from Lon Lon Ranch (main land entrance)
del exit_table[k]
exit_table[0x01F9].append(0xD52722) # 0x0476, Front Gate
# Combine the water exits between Hyrule Field and Zora River to lead to the land entrance instead of the water entrance
exit_table[0x00EA] += exit_table[0x01D9] # Hyrule Field -> Zora River
exit_table[0x0181] += exit_table[0x0311] # Zora River -> Hyrule Field
del exit_table[0x01D9]
del exit_table[0x0311]
# Change Impa escorts to bring link at the hyrule castle grounds entrance from market, instead of hyrule field
rom.write_int16(0xACAA2E, 0x0138) # 1st Impa escort
rom.write_int16(0xD12D6E, 0x0138) # 2nd+ Impa escort
if world.settings.shuffle_hideout_entrances != 'off':
rom.write_byte(rom.sym('HIDEOUT_SHUFFLED'), 1)
if world.settings.shuffle_hideout_entrances == 'overworld_savewarp':
rom.write_byte(rom.sym('HIDEOUT_SAVEWARP_OVERRIDE'), 1)
if world.settings.shuffle_gerudo_fortress_heart_piece == 'remove':
save_context.write_permanent_flag(Scenes.GERUDO_FORTRESS, FlagType.COLLECT, 0x3, 0x02)
if world.shuffle_dungeon_entrances:
rom.write_byte(rom.sym('DUNGEONS_SHUFFLED'), 1)
if not world.settings.useful_cutscenes:
# Tell the well water we are always a child.
rom.write_int32(0xDD5BF4, 0x00000000)
# Make the Adult well blocking stone dissappear if the well has been drained by
# checking the well drain event flag instead of links age. This actor doesn't need a
# code check for links age as the stone is absent for child via the scene alternate
# lists. So replace the age logic with drain logic.
rom.write_int32(0xE2887C, rom.read_int32(0xE28870)) # relocate this to nop delay slot
rom.write_int32(0xE2886C, 0x95CEB4B0) # lhu
rom.write_int32(0xE28870, 0x31CE0080) # andi
remove_entrance_blockers(rom)
# Purge temp flags on entrance to spirit from colossus through the front door.
rom.write_byte(0x021862E3, 0xC2)
if world.shuffle_special_dungeon_entrances or world.settings.shuffle_ganon_tower or world.full_one_ways:
# Move Hyrule's Castle Courtyard exit spawn to be before the crates so players don't skip Talon
rom.write_int16(0x21F607A, 0x033A) # Position X
rom.write_int16(0x21F607C, 0x0623) # Position Y
rom.write_int16(0x21F607E, 0xFF22) # Position Z
# Move Ganon's Castle exit spawn to be on the small ledge near the castle and not over the void
rom.write_int16(0x292B082, 0xFEA8) # Position X
rom.write_int16(0x292B084, 0x065C) # Position Y
rom.write_int16(0x292B086, 0x0290) # Position Z
rom.write_int16(0x292B08A, 0x0700) # Rotation Y
rom.write_int16(0x292B08E, 0x0DFF) # Init Params (Stationary spawn)
# Change the Rainbow Bridge cutscene trigger area to include Ganon's Castle exit
rom.write_int16(0xE2B46A, 0xC3C8) # Min X = -400.0f
rom.write_int32(0xE2B7A0, 0x44160000) # Min Y = 600.0f
if world.spawn_positions:
# Fix save warping inside Link's House to not be a special case
rom.write_int32(0xB06318, 0x00000000)
# Set entrances to update, except grotto entrances which are handled on their own at a later point
patch_blue_warps = ( # Settings where blue warps need to be patched to fix a crash when child steps into an adult blue warp
world.settings.shuffle_overworld_entrances
or world.shuffle_dungeon_entrances
or world.settings.shuffle_bosses != 'off'
or world.settings.blue_warps in ('balanced', 'full')
or world.full_one_ways
)
set_entrance_updates(entrance for entrance in world.get_shufflable_entrances() if entrance.shuffled or (patch_blue_warps and entrance.type == 'BlueWarp'))
for k, v in exit_updates:
if k in exit_table:
for addr in exit_table[k]:
rom.write_int16(addr, v)
# Fix text for Pocket Cucco.
rom.write_byte(0xBEEF45, 0x0B)
# Fix stupid alcove cameras in Ice Cavern -- thanks to krim and mzx for the help
rom.write_byte(0x2BECA25,0x01)
rom.write_byte(0x2BECA2D,0x01)
configure_dungeon_info(rom, world)
rom.write_bytes(rom.sym('CFG_FILE_SELECT_HASH'), spoiler.file_hash)
rom.write_bytes(rom.sym('PASSWORD'), spoiler.password)
# Initial Save Data
if not world.settings.useful_cutscenes and 'Forest Temple' not in world.settings.dungeon_shortcuts:
save_context.write_bits(0x00D4 + 0x03 * 0x1C + 0x04 + 0x0, 0x08) # Forest Temple switch flag (Poe Sisters cutscene)
if 'Deku Tree' in world.settings.dungeon_shortcuts:
# Deku Tree, flags are the same between vanilla/MQ
save_context.write_permanent_flag(Scenes.DEKU_TREE, FlagType.SWITCH, 0x1, 0x01) # Deku Block down
save_context.write_permanent_flag(Scenes.DEKU_TREE, FlagType.CLEAR, 0x2, 0x02) # Deku 231/312
save_context.write_permanent_flag(Scenes.DEKU_TREE, FlagType.SWITCH, 0x3, 0x20) # Deku 1st Web
save_context.write_permanent_flag(Scenes.DEKU_TREE, FlagType.SWITCH, 0x3, 0x40) # Deku 2nd Web
if 'Dodongos Cavern' in world.settings.dungeon_shortcuts:
# Dodongo's Cavern, flags are the same between vanilla/MQ
save_context.write_permanent_flag(Scenes.DODONGOS_CAVERN, FlagType.SWITCH, 0x3, 0x80) # DC Entrance Mud Wall
save_context.write_permanent_flag(Scenes.DODONGOS_CAVERN, FlagType.SWITCH, 0x0, 0x04) # DC Mouth
# Extra permanent flag in MQ for the child route
if world.dungeon_mq['Dodongos Cavern']:
save_context.write_permanent_flag(Scenes.DODONGOS_CAVERN, FlagType.SWITCH, 0x0, 0x02) # Armos wall switch
if 'Jabu Jabus Belly' in world.settings.dungeon_shortcuts:
# Jabu
if not world.dungeon_mq['Jabu Jabus Belly']:
save_context.write_permanent_flag(Scenes.JABU_JABU, FlagType.SWITCH, 0x0, 0x20) # Jabu Pathway down
else: