forked from OoTRandomizer/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.py
More file actions
2540 lines (2261 loc) · 117 KB
/
Copy pathScene.py
File metadata and controls
2540 lines (2261 loc) · 117 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
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
from dataclasses import dataclass
from os import path, walk
from typing import Any, Optional
import xml.etree.ElementTree as ET
import logging
from FileDataRelocator import segment_address_offset, create_segment_address, DataRecord, FileDataRelocator, FileType
from Utils import data_path
from SceneList import SCENE_TABLE, RecordType, SCENE_EXTERNAL_REFERENCES, SCENE_TABLE_ADDRESS
from Cutscenes import Cutscene, CutsceneCommandID, ACTOR_CUE_COMMANDS, GENERIC_COMMANDS, CAMERA_COMMANDS, NULL_COMMANDS
from FileList import SCENE_AND_ROOM_FILES
from Rom import Rom, Vec3s
from SaveContext import SceneIDs
class SceneFileAddressException(Exception):
def __init__(self, file: FileDataRelocator, segment: int, cursor: int, resource_name: str) -> None:
super().__init__(f'Unsupported room segment address segment 0x{segment:0>2x} for {resource_name} address at offset 0x{cursor - file.start:0>6x} (address 0x{cursor:0>8x}) in {file.name}. Offsets are only supported within the current scene file (segment 0x02).')
class RoomFileAddressException(Exception):
def __init__(self, file: FileDataRelocator, segment: int, cursor: int, resource_name: str) -> None:
super().__init__(f'Unsupported room segment address segment 0x{segment:0>2x} for {resource_name} address at offset 0x{cursor - file.start:0>6x} (address 0x{cursor:0>8x}) in {file.name}. Offsets are only supported within the current room file (segment 0x03) or parent scene file (segment 0x02).')
def str_to_s16(raw_bytes: str) -> int:
return int.from_bytes(int(raw_bytes, 16).to_bytes(2, 'big', signed=False), 'big', signed=True)
def s32_to_u32(num: int) -> int:
return int.from_bytes(num.to_bytes(4, 'big', signed=True), 'big', signed=False)
class SceneDataRelocator(FileDataRelocator):
def __init__(self, rom: Rom, name: str, start: int, end: int) -> None:
self.rooms: list[RoomDataRelocator] = []
self.headers: list[Optional[SceneHeader]] = [None]
self.id: int = -1
self.description: str = ''
for scene_id, scene_name, scene_description, _, _, _, _, _, _, _, _ in SCENE_TABLE.values():
if scene_name == name:
self.id = scene_id
self.description = scene_description
break
if scene_id == -1:
raise Exception(f'Could not locate scene file {name} in vanilla scene table')
super().__init__(rom, name, start, end, FileType.Scene)
def parse_file_header(self, alternate: Optional[int] = None) -> DataRecord:
self.headers[0] = SceneHeader.decode(self)
return self.headers[0]
def get_offset(self, cursor: int) -> tuple[int, Optional[FileDataRelocator]]:
segment = self.rom.read_byte(cursor)
offset = self.rom.read_int24(cursor + 1)
if segment == 0x00 and offset == 0:
return (0, None) # null
if segment == 0x02:
return (offset, self) # scene
return (-1, None) # unknown
# Assumes MQ Dungeons only change the main/first header
def apply_mq_patch(self, patch: dict) -> None:
scene = self.headers[0]
if 'TActors' in patch.keys() and len(patch['TActors']) > 0:
scene.transition_actor_list.apply_patch(patch['TActors'])
if 'Paths' in patch.keys() and len(patch['Paths']) > 0:
scene.path_list = ScenePathList.from_json(self, patch['Paths'])
else:
scene.path_list = None
if 'ColDelta' in patch.keys():
scene.collision_header.bgCamList.apply_patch(patch['ColDelta']['Cams'])
scene.collision_header.polyList.apply_patch(patch['ColDelta']['Polys'])
scene.collision_header.surfaceTypeList.apply_patch(patch['ColDelta']['PolyTypes'])
if 'Rooms' in patch.keys():
for room_data in patch['Rooms']:
room: RoomHeader = self.rooms[room_data['Id']].headers[0]
if 'Objects' in room_data.keys():
if room.object_list == None:
room.object_list = RoomObjectList(room.file, room.file.end - room.file.start + 1)
room.object_list.apply_patch(room_data['Objects'])
if 'Actors' in room_data.keys():
if room.actor_list == None:
room.actor_list = RoomActorList(room.file, room.file.end - room.file.start + 2)
room.actor_list.apply_patch(room_data['Actors'])
if self.id == SceneIDs.ICE_CAVERN:
# Delete alternate header command.
# This does not delete the alternate headers
# themselves, but they become unused data when
# not referenced in the main header
self.headers[0].alt_header_list = None
if self.id == SceneIDs.SPIRIT_TEMPLE:
# Create an alternate room setup for the
# shortcut hallway as adult. Modify the main header
# so that the silver block is always outside the
# hole to permit shooting the switch to drop the chest there.
room6 = self.rooms[6]
adult_header = room6.headers[0].copy()
room6.headers[0].alt_header_list = SceneAltHeaderList(room6, adult_header.offset + 1)
room6.headers[0].alt_header_list.headers.extend([None, adult_header, None, None, None, None])
room6.headers.extend(room6.headers[0].alt_header_list.headers)
adult_header.actor_list = room6.headers[0].actor_list.copy()
room6.headers[0].actor_list.actors.pop(0)
def write(self, rom: Rom) -> int:
aligned_file_end = super().write(rom)
addresses = bytearray()
addresses.extend(self.start.to_bytes(4, 'big'))
addresses.extend(self.end.to_bytes(4, 'big'))
rom.write_bytes(SCENE_TABLE_ADDRESS + self.id * 0x14, addresses)
return aligned_file_end
def to_json(self) -> dict[str, Any]:
return {
**super().to_json(),
'rooms': [x.to_json() for x in self.rooms],
}
# Always 16 byte aligned in vanilla
class SceneHeader(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.SceneHeader, file.start, offset, length)
self.alt_header_list: SceneAltHeaderList = None
self.sound_settings: SceneSoundSettings = None
self.room_list: SceneRoomList = None
self.transition_actor_list: SceneTransitionActorList = None
self.misc_settings: SceneMiscSettings = None
self.collision_header: SceneCollisionHeader = None
self.entrance_list: SceneEntranceList = None
self.special_objects: SceneSpecialSettings = None
self.path_list: ScenePathList = None
self.spawn_points: SceneSpawnPointList = None
self.skybox_settings: SceneSkyboxSettings = None
self.exit_list: SceneExitList = None
self.light_settings: SceneLightSettingsList = None
self.cutscene_data: SceneCutsceneData = None
self.actor_list: RoomActorList = None
self.align = 16
@staticmethod
def decode(file: FileDataRelocator, offset: int = 0, length: Optional[int] = -1) -> SceneHeader:
existing_record = file.get_existing_record_by_offset(offset, RecordType.SceneHeader)
if existing_record is not None:
return existing_record
setup = SceneHeader(file, offset, length)
command = 0
setup_start = setup.start + setup.offset
cursor = setup_start
# Process the current setup header.
# Command byte conditions are listed in the same order as
# the convention used in the rom.
while command != 0x14: # header terminator
command = file.rom.read_byte(cursor)
if command == 0x18: # Alternate header list
list_offset, list_file = file.get_offset(cursor + 0x04)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'alternate header list')
setup.alt_header_list = SceneAltHeaderList.decode(list_file, list_offset)
elif command == 0x15: # sound settings
setup.sound_settings = SceneSoundSettings.decode(file.rom, cursor)
elif command == 0x04: # room list
list_offset, list_file = file.get_offset(cursor + 0x04)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'room list')
num_rooms = file.rom.read_byte(cursor + 0x01)
setup.room_list = SceneRoomList.decode(list_file, list_offset, num_rooms * 0x08)
elif command == 0x0E: # Transition actor list
list_offset, list_file = file.get_offset(cursor + 0x04)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'transition actor list')
num_actors = file.rom.read_byte(cursor + 0x01)
setup.transition_actor_list = SceneTransitionActorList.decode(list_file, list_offset, num_actors * 0x10)
elif command == 0x19: # Misc settings
setup.misc_settings = SceneMiscSettings.decode(file.rom, cursor)
elif command == 0x03: # Collision Header
header_offset, header_file = file.get_offset(cursor + 0x04)
if header_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'collision header list')
setup.collision_header = SceneCollisionHeader.decode(file, header_offset, 0x2C)
elif command == 0x06: # Entrance List
# Size of entrance list is undefined.
# ZAPD parses all data from the entrance
# list segment address to the next resource
# segment address, or the end of the file.
list_offset, list_file = file.get_offset(cursor + 0x04)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'entrance list')
setup.entrance_list = SceneEntranceList.decode(list_file, list_offset)
elif command == 0x07: # Special object
setup.special_objects = SceneSpecialSettings.decode(file.rom, cursor)
elif command == 0x0D: # Path list
# Most scenes only have 1 path list, if any.
# Some have a second. None of the list lengths
# are defined in the ROM. Some path lists are
# defined in the ZAPD XML files and will have
# valid records defined before the header is
# processed.
list_offset, list_file = file.get_offset(cursor + 0x04)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'path list')
setup.path_list = ScenePathList.decode(list_file, list_offset)
elif command == 0x00: # Spawn point list
list_offset, list_file = file.get_offset(cursor + 0x04)
num_actors = file.rom.read_byte(cursor + 0x01)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'spawn point list')
setup.spawn_points = SceneSpawnPointList.decode(list_file, list_offset, num_actors * 0x10)
elif command == 0x11: # Skybox settings
setup.skybox_settings = SceneSkyboxSettings.decode(file.rom, cursor)
elif command == 0x13: # Exit List
# Same deal as the entrance list
list_offset, list_file = file.get_offset(cursor + 0x04)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'exit list')
setup.exit_list = SceneExitList.decode(list_file, list_offset)
elif command == 0x0F: # Lighting settings
list_offset, list_file = file.get_offset(cursor + 0x04)
num_lights = file.rom.read_byte(cursor + 0x01)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'light settings list')
setup.light_settings = SceneLightSettingsList.decode(list_file, list_offset, num_lights * 0x16)
elif command == 0x17: # Cutscene List
# Not all cutscenes are listed in scene headers.
# Unreferenced cutscenes are defined in the XMLs
# and do not need to be linked here.
cutscene_offset, cutscene_file = file.get_offset(cursor + 0x04)
if cutscene_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'cutscene data')
setup.cutscene_data = SceneCutsceneData.decode(cutscene_file, cutscene_offset)
elif command == 0x01: # actor list
# Scene files do not typically have actor lists,
# but the following do:
# Spirit Temple
# Gerudo's Fortress
# Death Mountain Trail
# Goron City
list_offset, list_file = file.get_offset(cursor + 0x04)
if list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'actor list')
num_actors = file.rom.read_byte(cursor + 0x01)
setup.actor_list = RoomActorList.decode(list_file, list_offset, num_actors * 0x10)
elif command == 0x14: # end list
pass
else:
raise Exception(
f'Unexpected command 0x{command:02X} at 0x{cursor - setup.start:08X} in {file.name}')
cursor += 0x08
setup.length = cursor - setup_start
setup.refresh_rom_data()
return setup
def encode(self) -> bytearray:
bytes = bytearray()
if self.alt_header_list is not None:
bytes.extend(int.to_bytes(0x18 << 0x18, 4, 'big'))
bytes.extend(self.alt_header_list.get_segment_address_bytes())
if self.sound_settings is not None:
bytes.extend(int.to_bytes(0x15, 1, 'big'))
bytes.extend(self.sound_settings.specID.to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 4, 'big'))
bytes.extend(self.sound_settings.natureAmbienceId.to_bytes(1, 'big'))
bytes.extend(self.sound_settings.seqId.to_bytes(1, 'big'))
if self.room_list is not None:
bytes.extend(int.to_bytes(0x04, 1, 'big'))
bytes.extend(len(self.room_list.rooms).to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.room_list.get_segment_address_bytes())
if self.transition_actor_list is not None:
bytes.extend(int.to_bytes(0x0E, 1, 'big'))
bytes.extend(len(self.transition_actor_list.actors).to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.transition_actor_list.get_segment_address_bytes())
if self.misc_settings is not None:
bytes.extend(int.to_bytes(0x19, 1, 'big'))
bytes.extend(self.misc_settings.sceneCamType.to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 5, 'big'))
bytes.extend(self.misc_settings.worldMapLocation.to_bytes(1, 'big'))
if self.collision_header is not None:
bytes.extend(int.to_bytes(0x03 << 0x18, 4, 'big'))
bytes.extend(self.collision_header.get_segment_address_bytes())
if self.entrance_list is not None:
bytes.extend(int.to_bytes(0x06 << 0x18, 4, 'big'))
bytes.extend(self.entrance_list.get_segment_address_bytes())
if self.special_objects is not None:
bytes.extend(int.to_bytes(0x07, 1, 'big'))
bytes.extend(self.special_objects.naviQuestHintFileId.to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 4, 'big'))
bytes.extend(self.special_objects.keepObjectId.to_bytes(2, 'big'))
if self.path_list is not None:
bytes.extend(int.to_bytes(0x0D << 0x18, 4, 'big'))
bytes.extend(self.path_list.get_segment_address_bytes())
if self.spawn_points is not None:
bytes.extend(int.to_bytes(0x00, 1, 'big'))
bytes.extend(len(self.spawn_points.spawns).to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.spawn_points.get_segment_address_bytes())
if self.actor_list is not None:
bytes.extend(int.to_bytes(0x01, 1, 'big'))
bytes.extend(len(self.actor_list.actors).to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.actor_list.get_segment_address_bytes())
if self.skybox_settings is not None:
bytes.extend(int.to_bytes(0x11, 1, 'big'))
bytes.extend(int.to_bytes(0, 3, 'big'))
bytes.extend(self.skybox_settings.skyboxID.to_bytes(1, 'big'))
bytes.extend(self.skybox_settings.skyboxConfig.to_bytes(1, 'big'))
bytes.extend(self.skybox_settings.envLightMode.to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 1, 'big'))
if self.exit_list is not None:
bytes.extend(int.to_bytes(0x13 << 0x18, 4, 'big'))
bytes.extend(self.exit_list.get_segment_address_bytes())
if self.light_settings is not None:
bytes.extend(int.to_bytes(0x0F, 1, 'big'))
bytes.extend(len(self.light_settings.lights).to_bytes(1, 'big'))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.light_settings.get_segment_address_bytes())
if self.cutscene_data is not None:
bytes.extend(int.to_bytes(0x17 << 0x18, 4, 'big'))
bytes.extend(self.cutscene_data.get_segment_address_bytes())
bytes.extend(int.to_bytes(0x14 << 0x18, 4, 'big'))
bytes.extend(int.to_bytes(0, 4, 'big'))
return bytes
# Always 8 byte aligned in vanilla, but may be artifact of only coming after the first header
# with 8 byte long commands
class SceneAltHeaderList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.AlternateHeaders, file.start, offset, length, True)
self.headers: list[Optional[SceneHeader | RoomHeader]] = []
self.align = 8
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> SceneAltHeaderList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.AlternateHeaders)
if existing_record is not None:
return existing_record
return SceneAltHeaderList(file, offset, length)
def decode_late(self) -> None:
cursor = self.start + self.offset
self.length = self.file.get_record_length_from_neighbor(self)
self.refresh_rom_data()
num_headers = int(self.length / 0x04)
for _ in range(0, num_headers):
header_offset, header_file = self.file.get_offset(cursor)
if header_offset == 0 and header_file is None:
self.headers.append(None) # NULL entry
elif header_file is None:
raise SceneFileAddressException(self.file, self.file.rom.read_byte(cursor), cursor, 'alternate header')
else:
if self.file.type == FileType.Scene:
setup = SceneHeader.decode(header_file, header_offset)
elif self.file.type == FileType.Room:
setup = RoomHeader.decode(header_file, header_offset)
else:
raise Exception(f'Unsupported file type {self.file.type} for alternate header list parsing in {self.file.name} at offset 0x{self.offset:0>6x}.')
self.headers.append(setup)
cursor += 0x04
if isinstance(self.file, SceneDataRelocator) or isinstance(self.file, RoomDataRelocator):
if len(self.file.headers) > 1:
raise Exception(f'Unable to parse multiple alternate header lists in {self.file.name}')
self.file.headers.extend(self.headers)
self.delay_parsing = False
def encode(self) -> bytearray:
bytes = bytearray()
for setup in self.headers:
if setup is None:
bytes.extend(int.to_bytes(0, 4, 'big'))
else:
bytes.extend(setup.get_segment_address_bytes())
return bytes
# Data only, part of the scene header
class SceneSoundSettings():
def __init__(self, specId: int, natureAmbienceId: int, seqId: int) -> None:
self.specID: int = specId
self.natureAmbienceId: int = natureAmbienceId
self.seqId: int = seqId
@staticmethod
def decode(rom: Rom, scene_cmd_addr: int) -> SceneSoundSettings:
return SceneSoundSettings(
rom.read_byte(scene_cmd_addr + 1),
rom.read_byte(scene_cmd_addr + 6),
rom.read_byte(scene_cmd_addr + 7),
)
def encode(self) -> bytearray:
bytes: bytearray = bytearray()
bytes.extend(int.to_bytes(0x15, 1, 'big'))
bytes.extend(self.specID.to_bytes(1, 'big'))
bytes.extend(bytearray([0, 0, 0, 0]))
bytes.extend(self.natureAmbienceId.to_bytes(1, 'big'))
bytes.extend(self.seqId.to_bytes(1, 'big'))
return bytes
# 4 byte aligned in vanilla
class SceneRoomList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.RoomList, file.start, offset, length)
self.rooms: list[RoomDataRelocator] = []
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> SceneRoomList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.RoomList)
if existing_record is not None:
return existing_record
if not isinstance(file, SceneDataRelocator):
raise Exception(f'Cannot process room list in non-scene file {file.name}')
num_rooms = int(length / 0x08)
room_list = SceneRoomList(file, offset, length)
cursor = room_list.start + room_list.offset
for i in range(0, num_rooms):
room_start = file.rom.read_int32(cursor)
room_end = file.rom.read_int32(cursor + 0x04)
room_entry: RoomDataRelocator = None
for room in file.rooms:
if room.start == room_start and room.end == room_end:
room_entry = room
break
if room_entry is None:
room_entry = RoomDataRelocator(file.rom, f'{file.name.replace("_scene", "_room")}_{i}', room_start, room_end, file)
file.rooms.append(room_entry)
room_list.rooms.append(room_entry)
cursor += 0x08
return room_list
def encode(self) -> bytearray:
bytes: bytearray = bytearray()
for room in self.rooms:
bytes.extend(room.start.to_bytes(4, 'big'))
bytes.extend(room.end.to_bytes(4, 'big'))
return bytes
# 4 byte aligned in vanilla
class SceneTransitionActorList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.TransitionActorList, file.start, offset, length)
self.actors: list[TransitionActor] = []
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> SceneTransitionActorList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.TransitionActorList)
if existing_record is not None:
return existing_record
num_actors = int(length / 0x10)
actor_list = SceneTransitionActorList(file, offset, length)
cursor = actor_list.start + actor_list.offset
for i in range(0, num_actors):
actor_list.actors.append(TransitionActor.decode(file.rom, cursor + i * 0x10))
return actor_list
def apply_patch(self, patch_data: list[str]) -> None:
self.actors = []
for actor in patch_data:
self.actors.append(TransitionActor.from_json(actor))
def encode(self) -> bytearray:
bytes: bytearray = bytearray()
for actor in self.actors:
bytes.extend(actor.encode())
return bytes
# Data only, parent class used by room actor lists, scene spawn position lists,
# and scene transition actor lists in order to iterate through different actor
# types when patching. See get_actor_list in Patches.py.
class ActorData:
id: int
pos: Vec3s
rot: Vec3s
params: int
# Data only, part of the transition actor list
class TransitionActor(ActorData):
def __init__(self, front: TransitionActorSide, back: TransitionActorSide, id: int, pos: Vec3s, rot: Vec3s, params: int):
self.sides: list[TransitionActorSide] = [front, back]
self.id: int = id
self.pos: Vec3s = pos
self.rot: Vec3s = rot # only y variable is used
self.params: int = params
def decode(rom: Rom, cursor: int) -> TransitionActor:
return TransitionActor(
TransitionActorSide(
rom.read_byte(cursor),
rom.read_byte(cursor + 0x01)
),
TransitionActorSide(
rom.read_byte(cursor + 0x02),
rom.read_byte(cursor + 0x03)
),
rom.read_s16(cursor + 0x04),
Vec3s.decode(rom, cursor + 0x06),
Vec3s(0, rom.read_s16(cursor + 0x0C), 0),
rom.read_s16(cursor + 0x0E)
)
@staticmethod
def from_json(patch_data: str) -> TransitionActor:
raw_bytes = patch_data.replace(' ', '')
return TransitionActor(
TransitionActorSide(
int(raw_bytes[0:2], 16),
int(raw_bytes[2:4], 16)
),
TransitionActorSide(
int(raw_bytes[4:6], 16),
int(raw_bytes[6:8], 16)
),
str_to_s16(raw_bytes[8:12]),
Vec3s(
str_to_s16(raw_bytes[12:16]),
str_to_s16(raw_bytes[16:20]),
str_to_s16(raw_bytes[20:24])
),
Vec3s(0, str_to_s16(raw_bytes[24:28]), 0),
str_to_s16(raw_bytes[28:32])
)
def encode(self) -> bytearray:
bytes: bytearray = bytearray()
bytes.extend(self.sides[0].room.to_bytes(1, 'big'))
bytes.extend(self.sides[0].bgCamIndex.to_bytes(1, 'big'))
bytes.extend(self.sides[1].room.to_bytes(1, 'big'))
bytes.extend(self.sides[1].bgCamIndex.to_bytes(1, 'big'))
bytes.extend(self.id.to_bytes(2, 'big', signed=True))
bytes.extend(self.pos.encode())
bytes.extend(self.rot.y.to_bytes(2, 'big', signed=True))
bytes.extend(self.params.to_bytes(2, 'big', signed=True))
return bytes
@dataclass
class TransitionActorSide:
room: int
bgCamIndex: int
# Data only, part of the scene header
class SceneMiscSettings():
def __init__(self, sceneCamType: int, worldMapLocation: int) -> None:
self.sceneCamType: int = sceneCamType
self.worldMapLocation: int = worldMapLocation
@staticmethod
def decode(rom: Rom, cursor: int) -> SceneMiscSettings:
return SceneMiscSettings(
rom.read_byte(cursor + 0x01),
rom.read_byte(cursor + 0x07),
)
# 4 byte aligned in vanilla
class SceneCollisionHeader(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.CollisionHeader, file.start, offset, length)
self.minBounds: Vec3s = None
self.maxBounds: Vec3s = None
self.numVertices: int = 0
self.vtxList: CollisionVtxList = None
self.numPolygons: int = 0
self.polyList: CollisionPolyList = None
self.surfaceTypeList: CollisionSurfaceTypeList = None
self.bgCamList: CollisionBgCamInfoList = None
self.numWaterBoxes: int = 0
self.waterBoxes: Optional[CollisionWaterBoxList] = None
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> SceneCollisionHeader:
existing_record = file.get_existing_record_by_offset(offset, RecordType.CollisionHeader)
if existing_record is not None:
return existing_record
collision_header = SceneCollisionHeader(file, offset, length)
cursor = collision_header.start + collision_header.offset
vtx_list_address = file.rom.read_int32(cursor + 0x10)
poly_list_address = file.rom.read_int32(cursor + 0x18)
surface_list_address = file.rom.read_int32(cursor + 0x1C)
camdata_list_address = file.rom.read_int32(cursor + 0x20)
waterbox_list_address = file.rom.read_int32(cursor + 0x28)
collision_header.minBounds = Vec3s.decode(file.rom, cursor + 0x00)
collision_header.maxBounds = Vec3s.decode(file.rom, cursor + 0x06)
collision_header.numVertices = file.rom.read_int16(cursor + 0x0C)
vtx_list_offset, vtx_list_file = file.get_offset(cursor + 0x10)
if vtx_list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x10), cursor + 0x10, 'vertex list')
collision_header.vtxList = CollisionVtxList.decode(vtx_list_file, vtx_list_offset, collision_header.numVertices * 0x06)
collision_header.numPolygons = file.rom.read_int16(cursor + 0x14)
poly_list_offset, poly_list_file = file.get_offset(cursor + 0x18)
if poly_list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x18), cursor + 0x18, 'polygon list')
collision_header.polyList = CollisionPolyList.decode(poly_list_file, poly_list_offset, collision_header.numPolygons * 0x10)
surface_list_offset, surface_list_file = file.get_offset(cursor + 0x1C)
if surface_list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x1C), cursor + 0x1C, 'surface type')
collision_header.surfaceTypeList = CollisionSurfaceTypeList.decode(surface_list_file, surface_list_offset, collision_header.polyList.numPolygonTypes * 0x08)
# ZAPD heuristics to guess the bgCamList size.
# See ZCollision.cpp line 93
if camdata_list_address != 0:
upper_camera_boundary = segment_address_offset(surface_list_address)
if not upper_camera_boundary:
upper_camera_boundary = segment_address_offset(poly_list_address)
if not upper_camera_boundary:
upper_camera_boundary = segment_address_offset(vtx_list_address)
if not upper_camera_boundary:
upper_camera_boundary = segment_address_offset(waterbox_list_address)
if not upper_camera_boundary:
upper_camera_boundary = cursor
if upper_camera_boundary < segment_address_offset(camdata_list_address):
offset = segment_address_offset(camdata_list_address)
cam_search1 = file.rom.read_byte(file.start + offset)
cam_search2 = file.rom.read_byte(file.start + offset + 0x04)
while cam_search1 == 0x00 and cam_search2 == 0x02:
offset += 0x08
cam_search1 = file.rom.read_byte(file.start + offset)
cam_search2 = file.rom.read_byte(file.start + offset + 0x20)
upper_camera_boundary = offset
camdata_list_length = upper_camera_boundary - segment_address_offset(camdata_list_address)
if camdata_list_length <= 0:
raise Exception(f'Camera data list length could not be determined for file {file.name} at segment address 0x{camdata_list_address:0>8x}')
camdata_list_offset, camdata_list_file = file.get_offset(cursor + 0x20)
if camdata_list_file is None:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x20), cursor + 0x20, 'camera definitions')
collision_header.bgCamList = CollisionBgCamInfoList.decode(camdata_list_file, camdata_list_offset, camdata_list_length)
collision_header.numWaterBoxes = file.rom.read_int16(cursor + 0x24)
waterbox_list_offset, waterbox_list_file = file.get_offset(cursor + 0x28)
if waterbox_list_file is None and waterbox_list_offset != 0:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x28), cursor + 0x28, 'waterbox list')
if waterbox_list_file is not None:
collision_header.waterBoxes = CollisionWaterBoxList.decode(waterbox_list_file, waterbox_list_offset, collision_header.numWaterBoxes * 0x10)
return collision_header
def encode(self) -> bytearray:
bytes: bytearray = bytearray()
bytes.extend(self.minBounds.encode())
bytes.extend(self.maxBounds.encode())
bytes.extend(len(self.vtxList.vertices).to_bytes(2, 'big', signed=True))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.vtxList.get_segment_address_bytes())
bytes.extend(len(self.polyList.polygons).to_bytes(2, 'big', signed=True))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.polyList.get_segment_address_bytes())
bytes.extend(self.surfaceTypeList.get_segment_address_bytes())
bytes.extend(self.bgCamList.get_segment_address_bytes())
if self.waterBoxes is not None:
bytes.extend(len(self.waterBoxes.waterboxes).to_bytes(2, 'big', signed=True))
bytes.extend(int.to_bytes(0, 2, 'big'))
bytes.extend(self.waterBoxes.get_segment_address_bytes())
else:
bytes.extend(int.to_bytes(0, 8, 'big'))
return bytes
# 4 byte aligned in vanilla
class CollisionVtxList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.Vertices, file.start, offset, length)
self.vertices: list[Vec3s] = []
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> CollisionVtxList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.Vertices)
if existing_record is not None:
return existing_record
num_vertices = int(length / 0x06)
vtx_list = CollisionVtxList(file, offset, length)
cursor = vtx_list.start + vtx_list.offset
for i in range(0, num_vertices):
vtx_list.vertices.append(Vec3s.decode(file.rom, cursor + 0x06 * i))
return vtx_list
def encode(self) -> bytearray:
bytes = bytearray()
for vtx in self.vertices:
bytes.extend(vtx.encode())
return bytes
# 4 byte aligned in vanilla
class CollisionPolyList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.Polys, file.start, offset, length)
self.polygons: list[CollisionPoly] = []
self.numPolygonTypes: int = 0
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> CollisionPolyList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.Polys)
if existing_record is not None:
return existing_record
num_polygons = int(length / 0x10)
poly_list = CollisionPolyList(file, offset, length)
cursor = poly_list.start + poly_list.offset
for i in range(0, num_polygons):
poly = CollisionPoly.decode(file.rom, cursor + 0x10 * i)
poly_list.polygons.append(poly)
if poly.type > poly_list.numPolygonTypes:
poly_list.numPolygonTypes = poly.type
poly_list.numPolygonTypes += 1
return poly_list
def apply_patch(self, patch_data: list[dict[str, int]]) -> None:
for item in patch_data:
id = item['Id']
t = item['Type']
flags = item['Flags']
poly = self.polygons[id]
poly.type = t
poly.flags_vIA = (flags << 13)
def encode(self) -> bytearray:
bytes = bytearray()
for poly in self.polygons:
bytes.extend(poly.encode())
return bytes
# Data only, part of collision polygon lists
class CollisionPoly:
def __init__(self) -> None:
self.type: int = 0
self.vtxData: tuple[int, int, int] = (0, 0, 0)
self.flags_vIA: int = 0
self.flags_vIB: int = 0
self.flags_vIC: int = 0
self.normal: Vec3s = Vec3s()
self.dist: int = 0
@staticmethod
def decode(rom: Rom, cursor: int) -> CollisionPoly:
poly = CollisionPoly()
poly.type = rom.read_int16(cursor)
vtx1 = rom.read_int16(cursor + 0x02)
vtx2 = rom.read_int16(cursor + 0x04)
vtx3 = rom.read_int16(cursor + 0x06)
poly.vtxData = (
vtx1 & 0x1FFF,
vtx2 & 0x1FFF,
vtx3 & 0x1FFF
)
poly.flags_vIA = vtx1 & 0xE000
poly.flags_vIB = vtx2 & 0xE000
poly.flags_vIC = vtx3 & 0xE000
poly.normal = Vec3s.decode(rom, cursor + 0x08)
poly.dist = rom.read_s16(cursor + 0x0E)
return poly
def encode(self) -> bytearray:
bytes = bytearray()
bytes.extend(self.type.to_bytes(2, 'big'))
bytes.extend((self.vtxData[0] | self.flags_vIA).to_bytes(2, 'big'))
bytes.extend((self.vtxData[1] | self.flags_vIB).to_bytes(2, 'big'))
bytes.extend((self.vtxData[2] | self.flags_vIC).to_bytes(2, 'big'))
bytes.extend(self.normal.encode())
bytes.extend(self.dist.to_bytes(2, 'big', signed=True))
return bytes
# 4 byte aligned
class CollisionSurfaceTypeList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.SurfaceTypes, file.start, offset, length)
self.surfaces: list[CollisionSurfaceType] = []
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> CollisionSurfaceTypeList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.Polys)
if existing_record is not None:
return existing_record
surface_list = CollisionSurfaceTypeList(file, offset, length)
num_surfaces = int(length / 0x08)
cursor = surface_list.start + surface_list.offset
for i in range(0, num_surfaces):
surface_list.surfaces.append(CollisionSurfaceType.decode(file.rom, cursor + 0x08 * i))
return surface_list
def apply_patch(self, patch_data: list[dict[str, int]]) -> None:
for item in patch_data:
id = item['Id']
high = s32_to_u32(item['High'])
low = s32_to_u32(item['Low'])
if id == len(self.surfaces):
self.surfaces.append(CollisionSurfaceType(high, low))
else:
self.surfaces[id].data = (high, low)
def encode(self) -> bytearray:
bytes = bytearray()
for surface in self.surfaces:
bytes.extend(surface.encode())
return bytes
# Data only, part of collision surface type lists
class CollisionSurfaceType:
def __init__(self, type1: int = 0, type2: int = 0) -> None:
self.data: tuple[int, int] = (type1, type2)
@staticmethod
def decode(rom: Rom, cursor: int) -> CollisionSurfaceType:
surface = CollisionSurfaceType()
surface.data = (
rom.read_int32(cursor),
rom.read_int32(cursor + 0x04)
)
return surface
def encode(self) -> bytearray:
bytes = bytearray()
bytes.extend(self.data[0].to_bytes(4, 'big'))
bytes.extend(self.data[1].to_bytes(4, 'big'))
return bytes
# 4 byte aligned in vanilla
class CollisionBgCamInfoList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.Cams, file.start, offset, length)
self.cams: list[CollisionBgCamInfo] = []
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> CollisionBgCamInfoList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.Cams)
if existing_record is not None:
return existing_record
cam_info_list = CollisionBgCamInfoList(file, offset, length)
cursor = cam_info_list.start + cam_info_list.offset
list_end_address = cam_info_list.start + cam_info_list.offset + cam_info_list.length
while cursor < list_end_address:
cam_info_list.cams.append(CollisionBgCamInfo.decode(file.rom, cursor, file, cam_info_list.offset))
cursor += 0x08
return cam_info_list
def apply_patch(self, patch_data: list[dict[str, int]]) -> None:
vanilla_cams = self.cams
self.cams = []
for cam in patch_data:
pos_index = cam['PositionIndex']
cam_data = cam['Data']
if pos_index < 0:
cam_record = None
else:
cam_record = vanilla_cams[pos_index].bgCamFuncData
cam_setting = int.from_bytes(cam_data.to_bytes(4, 'big')[0:2], 'big')
cam_count = int.from_bytes(cam_data.to_bytes(4, 'big')[2:4], 'big', signed=True)
self.cams.append(CollisionBgCamInfo(cam_setting, cam_count, cam_record))
def encode(self) -> bytearray:
bytes = bytearray()
for cam in self.cams:
bytes.extend(cam.encode())
return bytes
# Data only, part of collision camera lists
class CollisionBgCamInfo:
def __init__(self, setting: int = 0, count: int = 0, data: Optional[CollisionCamPosData] = None) -> None:
self.setting: int = setting
self.count: int = count
self.bgCamFuncData: Optional[CollisionCamPosData] = data
@staticmethod
def decode(rom: Rom, cursor: int, file: FileDataRelocator, cam_info_list_offset: int) -> CollisionBgCamInfo:
cam_info = CollisionBgCamInfo()
cam_info.setting = rom.read_int16(cursor)
cam_info.count = rom.read_s16(cursor + 0x02)
cam_data_offset, cam_data_file = file.get_offset(cursor + 0x04)
if cam_data_file is None and cam_data_offset != 0:
raise SceneFileAddressException(file, file.rom.read_byte(cursor + 0x04), cursor + 0x04, 'camera function data')
if cam_data_file is not None:
# ZAPD assumes all data between the CamPosData list start and the start of
# the camera settings list belongs to the CamPosData list. Note that this
# is not true for modded scenes produced from SharpOcarina, which apparently
# stores the data after the settings. CollisionBgCamFuncData only supports
# the vanilla scene file behavior of position data before camera settings.
cam_info.bgCamFuncData = CollisionCamPosData.decode(file, cam_data_offset, cam_info_list_offset - cam_data_offset)
return cam_info
def encode(self) -> bytearray:
bytes = bytearray()
bytes.extend(self.setting.to_bytes(2, 'big'))
bytes.extend(self.count.to_bytes(2, 'big', signed=True))
if self.bgCamFuncData is not None:
bytes.extend(self.bgCamFuncData.get_segment_address_bytes())
else:
bytes.extend(int.to_bytes(0, 4, 'big'))
return bytes
# Wrapper class to allow merging records and referencing via array index/pointer offset
class CollisionCamPosData:
def __init__(self, record: CollisionBgCamFuncData, record_offset: int = 0) -> None:
self.record: CollisionBgCamFuncData = record
self.record_offset: int = record_offset
def get_segment_address_bytes(self) -> bytes:
record_address = create_segment_address(int(self.record.file.type.value), self.record.offset + self.record_offset)
return record_address.to_bytes(4, 'big')
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> CollisionBgCamFuncData:
record = CollisionBgCamFuncData.decode(file, offset, length)
return CollisionCamPosData(record)
# 4 byte aligned in vanilla
class CollisionBgCamFuncData(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.CamPosData, file.start, offset, length)
# Data is either a set of 6 Vec3s (crawlspaces/Camera_Subj4 only) or a more
# complicated struct the same length as 3 Vec3s (0x12). See BgCamFuncData
# and its comments in z64bgcheck.h in decomp.
# Assume this is always a list of Vec3s for simplicity.
self.positions: list[Vec3s] = []
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> CollisionBgCamFuncData:
existing_record = file.get_existing_record_by_offset(offset, RecordType.CamPosData)
if existing_record is not None:
return existing_record
cam_func_data = CollisionBgCamFuncData(file, offset, length)
cursor = cam_func_data.start + cam_func_data.offset
num_vertices = int(length / 0x06)
# Length may not be aligned with data due to using
# next record offset to calculate length
cam_func_data.length = num_vertices * 0x06
cam_func_data.refresh_rom_data()
for i in range(0, num_vertices):
cam_func_data.positions.append(Vec3s.decode(file.rom, cursor + i * 0x06))
return cam_func_data
def encode(self) -> bytearray:
bytes = bytearray()
for pos in self.positions:
bytes.extend(pos.encode())
return bytes
def merge(self, other_record: CollisionBgCamFuncData) -> None:
low_record, high_record, lower_overlap = super().merge(other_record)
lower_overlap_index = int(lower_overlap / 0x06)
if lower_overlap / 0x06 != lower_overlap_index:
raise Exception(f'Overlapping {self.type.value} records are not aligned at offset {low_record.offset:0>8x}, length {low_record.length:0>8x} and offset {high_record.offset:0>8x}, length {high_record.length:0>8x}')
i = lower_overlap_index
# Check that stored positions haven't changed in case raw bytes were not refreshed after position changes
while i < len(low_record.positions):
if low_record.positions[i] != high_record.positions[i - lower_overlap_index]:
raise Exception(f'Tried to merge mismatching {self.type.value} records at 0x{self.offset:0>8x}, length 0x{self.length:0>8x} and 0x{other_record.offset:0>8x}, length 0x{other_record.length:0>8x}. Mismatch at 0x{low_record.offset + i * 0x06:0>8x} (lower: {low_record.positions[i]}, upper: {high_record.positions[i - lower_overlap_index]})')
i += 1
low_record.positions.extend(high_record.positions[i:])
def _merge_in_file(self, file: FileDataRelocator, other_record: DataRecord, record_offset: int):
cams: list[CollisionBgCamInfoList] = list(filter(lambda r: r.type == RecordType.Cams, file.data_records))
for cam in cams:
i = 0
while i < len(cam.cams):
if cam.cams[i].bgCamFuncData.record is other_record:
cam.cams[i].bgCamFuncData = CollisionCamPosData(self, record_offset)
break
i += 1
# 4 byte aligned in vanilla
class CollisionWaterBoxList(DataRecord):
def __init__(self, file: FileDataRelocator, offset: int, length: Optional[int] = -1) -> None:
super().__init__(file, RecordType.Waterboxes, file.start, offset, length)
self.waterboxes: list[CollisionWaterBox] = []
@staticmethod
def decode(file: FileDataRelocator, offset: int, length: int) -> CollisionWaterBoxList:
existing_record = file.get_existing_record_by_offset(offset, RecordType.Waterboxes)
if existing_record is not None:
return existing_record
waterbox_list = CollisionWaterBoxList(file, offset, length)
cursor = waterbox_list.start + waterbox_list.offset
num_waterboxes = int(length / 0x10)
for i in range(0, num_waterboxes):
waterbox_list.waterboxes.append(CollisionWaterBox.decode(file.rom, cursor + i * 0x10))
return waterbox_list
def encode(self) -> bytearray:
bytes = bytearray()
for waterbox in self.waterboxes:
bytes.extend(waterbox.encode())
return bytes
# Data only, referenced in waterbox list
class CollisionWaterBox:
def __init__(self) -> None:
self.xMin: int = 0
self.ySurface: int = 0
self.zMin: int = 0
self.xLength: int = 0