-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathobjectLoading.s
More file actions
679 lines (585 loc) · 10.7 KB
/
Copy pathobjectLoading.s
File metadata and controls
679 lines (585 loc) · 10.7 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
m_section_free Objects_1 namespace objectData
.ifdef ROM_AGES
.include "objects/ages/extraData1.s"
.include "objects/ages/enemyData.s"
.include "objects/ages/extraData2.s"
.else
.include "objects/seasons/extraData1.s"
.include "objects/seasons/enemyData.s"
.include "objects/seasons/extraData2.s"
.endif
;;
parseObjectData:
xor a
ld (wNumEnemies),a
ld ($cfc0),a
ld hl,wTmpcec0
ld b,$20
call clearMemory
call addRoomToEnemiesKilledList
call generateRandomBuffer
.ifdef ROM_AGES
callab getObjectDataAddress
.else; ROM_SEASONS
ld a,(wActiveGroup)
ld hl,objectDataGroupTable
rst_addDoubleIndex
ldi a,(hl)
ld h,(hl)
ld l,a
ld a,(wActiveRoom)
ld e,a
ld d,$00
add hl,de
add hl,de
ldi a,(hl)
ld d,(hl)
ld e,a
.endif
;;
; @param de Address of object data to parse
parseGivenObjectData:
; BUG(?): Not initializing the $cec0 memory block!
;
; I've seen this affect the target carts minigame, where because wEnemyPlacement.numEnemies
; is uninitialized, it writes to an address in the $cfxx memory range and messes up the
; room's collisions after loading the crystals. But that was in rando, not sure if it can
; happen in vanilla.
;
; However, it could be dangerous to add such a memory clear call here; there could be some
; poorly designed code that's using that memory region for something else even while it's
; used for object loading here. Would need to do some testing to make sure it doesn't break
; anything.
ld a,(de)
cp $fe
jr nz,+
pop de
+
ld a,(de)
cp $ff
ret z
inc de
and $0f
rst_jumpTable
.dw objectDataOp0
.dw objectDataOp1
.dw objectDataOp2
.dw objectDataOp3
.dw objectDataOp4
.dw objectDataOp5
.dw objectDataOp6
.dw objectDataOp7
.dw objectDataOp8
.dw objectDataOp9
.ifdef AGES_ENGINE
.dw objectDataOpA
.endif
; Appears to be unused
func_55f8:
jr parseGivenObjectData
;;
parseGivenObjectData_hl:
ld e,l
ld d,h
jr parseGivenObjectData
; 1st byte is the base size of the opcode, 2nd byte is size of each subsequent
; use of the opcode (as multiple uses of the same opcode saves space)
objectDataOpcodeSizes:
.db $02 $00
.db $01 $02
.db $01 $04
.db $03 $00
.db $03 $00
.db $03 $00
.db $02 $02
.db $02 $04
.db $01 $03
.db $01 $06
.ifdef AGES_ENGINE
.db $02 $02 ; Item drop ($0a)
.else
.db $00 $00
.endif
.db $00 $00
.db $00 $00
.db $00 $00
.db $01 $00
.db $01 $00
;;
; Only use objects when certain room properties are set
; Used a lot in jabu-jabu
objectDataOp0:
ld a,(wRoomStateModifier)
ld hl,bitTable
add l
ld l,a
ld a,(hl)
ld b,a
ld a,(de)
inc de
and b
jp nz,parseGivenObjectData
ld b,$00
ld l,e
ld h,d
@nextOpcode:
ld a,(hl)
; Parse new conditionals
cp $f0
jr z,parseGivenObjectData_hl
; Parse return opcode
cp $fe
jr z,parseGivenObjectData_hl
; Check return opcode
cp $ff
ret z
; Otherwise, skip the next opcode
and $0f
ld de,objectDataOpcodeSizes
call addDoubleIndexToDe
ld a,(de)
ld c,a
add hl,bc
inc de
ld a,(de)
ld c,a
-
add hl,bc
bit 7,(hl)
jr nz,@nextOpcode
jr -
;;
; No-value interaction
objectDataOp1:
call continueObjectLoopIfOpDone
call getFreeInteractionSlot
jr nz,skipToOpEnd_2byte
call read2Bytes
jr objectDataOp1
;;
skipToOpEnd_2byte:
inc de
inc de
ld a,(de)
cp $f0
jp c,skipToOpEnd_2byte
jp parseGivenObjectData
;;
; Double-value interaction
objectDataOp2:
call continueObjectLoopIfOpDone
call getFreeInteractionSlot
jr nz,skipToOpEnd_4byte
call read2Bytes
ld l,Interaction.yh
call readCoordinates
jr objectDataOp2
skipToOpEnd_4byte:
inc de
inc de
inc de
inc de
ld a,(de)
cp $f0
jp c,skipToOpEnd_4byte
jp parseGivenObjectData
;;
; In addition to checking wcc05, this appears to have a mechanism to prevent
; the pointer from being read if link enters from a certain direction.
; @param[out] @zflag Set if the pointer should be skipped.
checkSkipPointer:
.ifdef ROM_AGES
ld a,(wcc05)
bit 1,a
ret z
.endif
ld a,(wcc85)
bit 7,a
jr z,++
and $03
ld b,a
xor a
ld (wcc85),a
ld a,(wScreenTransitionDirection)
cp b
ret z
-
or d
ret
++
or a
jr z,-
xor a
ret
;;
skipPointer:
inc de
inc de
jp parseGivenObjectData
;;
parsePointer:
ld l,e
ld h,d
inc de
inc de
push de
ldi a,(hl)
ld d,(hl)
ld e,a
jp parseGivenObjectData
;;
; Object pointer
objectDataOp3:
call checkSkipPointer
jr z,skipPointer
jr parsePointer
;;
; "Before Event" pointer: use the pointer if bit 7 of room flags is not set.
objectDataOp4:
call checkSkipPointer
jr z,skipPointer
call getThisRoomFlags
bit 7,a
jr nz,skipPointer
jr parsePointer
;;
; "After Event" pointer: use the pointer if bit 7 of room flags is set.
objectDataOp5:
call checkSkipPointer
jr z,skipPointer
call getThisRoomFlags
bit 7,a
jr z,skipPointer
jr parsePointer
;;
; Random enemy
objectDataOp6:
; Flags
ld a,(de)
inc de
ld b,a
and $1f
ldh (<hFF8B),a
; Count
ld a,b
swap a
rrca
and $07
ldh (<hFF8C),a
; Main ID
ld a,(de)
inc de
ldh (<hFF8F),a
; Sub ID
ld a,(de)
inc de
ldh (<hFF8E),a
@nextRandomEnemy:
ld a,$01
ldh (<hFF8D),a
ldh a,(<hFF8B)
and $01
jr nz,+
call checkEnemyKilled
jr nc,+++
+
call getFreeEnemySlot
jp nz,parseGivenObjectData
call decEnemyCounterIfApplicable
; Write ID
ldh a,(<hFF8F)
ldi (hl),a
ldh a,(<hFF8E)
ldi (hl),a
ld a,h
ldh (<hFF91),a
push de
call assignRandomPositionToEnemy
pop de
.ifndef ENABLE_US_BUGFIXES
; JP BUG: 'h' register not restored to point to the enemy object. This is only a problem
; when a random-position enemy fails to be placed somewhere.
; In this case, "hl" will point to wEnemyPlacement.randomPlacementAttemptCounter. This won't
; really affect anything. The real problem is that, by not deleting the enemy, it will have
; the default position 0,0 (top-left corner of the screen).
; This should be very rare, because it's unlikely that the game will fail to place the enemy
; anywhere at all, unless the room is full of walls.
jr nc,++
ld l,Enemy.enabled
ld (hl),$00
jr +++
.else
; US BUG: If the enemy is not successfully placed, this does not clear the ID/SubID values.
; This shouldn't matter in most cases because they will usually be overwritten when an enemy
; is spawned. But, supposing there is a case where the game expects the subid to be
; 0 and doesn't write the value explicitly, maybe this could affect something?
;
; ALSO (and this appies to both regions), this does not decrement "wNumEnemies", which could
; make it impossible to clear certain rooms where you're supposed to kill all enemies, if
; one of the enemies fails to spawn! (But again, this is rare)
ldh a,(<hFF91)
ld h,a
jr nc,++
ld l,Enemy.enabled
ld (hl),$00
jr +++
.endif
++
ld l,Enemy.enabled
ldh a,(<hFF8D)
ld (hl),a
+++
ldh a,(<hFF8C)
dec a
ldh (<hFF8C),a
jr nz,@nextRandomEnemy
jp parseGivenObjectData
;;
; Specific position enemy
objectDataOp7:
; Flags
ld a,(de)
inc de
ldh (<hFF8B),a
@nextSpecificEnemy:
ld a,(de)
bit 7,a
jp nz,parseGivenObjectData
; Default value for "enabled" byte
ld a,$01
ldh (<hFF8D),a
ldh a,(<hFF8B)
and $01
jr nz,+
call checkEnemyKilled
jr c,+
inc de
inc de
inc de
inc de
jr @nextSpecificEnemy
+
call getFreeEnemySlot
jp nz,skipToOpEnd_4byte
call decEnemyCounterIfApplicable
; Get ID
call read2Bytes
; Get X/Y
ld l,Enemy.yh
call readCoordinates
; l = Enemy.xh
ldd a,(hl)
and $f0
swap a
ld c,a
; l = Enemy.yh
dec l
ld a,(hl)
and $f0
or c
ld c,a
; c now contains the object's tile / shortened position (YX)
call addPositionToPlacedEnemyPositions
ld l,Enemy.enabled
ldh a,(<hFF8D)
ld (hl),a
jr @nextSpecificEnemy
;;
; "Parts" (owl statues etc)
objectDataOp8:
ld a,(de)
bit 7,a
jp nz,parseGivenObjectData
call getFreePartSlot
jp nz,++
call read2Bytes
ld a,(de)
ld c,a
inc de
ld l,Part.yh
call setShortPosition
call addPositionToPlacedEnemyPositions
jr objectDataOp8
++
inc de
inc de
inc de
jr objectDataOp8
;;
; Object with parameter
objectDataOp9:
call continueObjectLoopIfOpDone
call @allocateObjectType
jr nz,@allocationFailure
; Read ID
inc de
ld a,(de)
inc de
ldi (hl),a
ld a,(de)
inc de
ldi (hl),a
; Read Object.var03
ld a,(de)
inc de
ldi (hl),a
; Read Y
ld a,l
and $c0
add Object.yh
ld l,a
ld a,(de)
inc de
ldi (hl),a
; Read X
inc l
ld a,(de)
inc de
ld (hl),a
jr objectDataOp9
@allocationFailure:
ld a,$06
call addAToDe
jr objectDataOp9
@allocateObjectType:
ld a,(de)
rst_jumpTable
.dw getFreeInteractionSlot
.dw getFreeEnemySlot_uncounted
.dw getFreePartSlot
.ifdef AGES_ENGINE
;;
; Item drops. Seasons uses ENEMY_ITEM_DROP_PRODUCER directly instead of this.
objectDataOpA:
ld a,(de)
inc de
ldh (<hFF8B),a
@nextOpA:
ld a,(de)
bit 7,a
jp nz,parseGivenObjectData
; Default value for "enabled" byte
ld a,$01
ldh (<hFF8D),a
ldh a,(<hFF8B)
and $01
jr nz,++
call checkEnemyKilled
jr c,++
inc de
inc de
jr @nextOpA
++
call getFreeEnemySlot_uncounted
jp nz,skipToOpEnd_2byte
; Set ID
ld (hl),ENEMY_ITEM_DROP_PRODUCER
inc l
ld a,(de)
inc de
ld (hl),a
; Set YX
ld l,Enemy.yh
ld a,(de)
inc de
call setShortPosition
call addPositionToPlacedEnemyPositions
ld l,Enemy.enabled
ldh a,(<hFF8D)
ld (hl),a
jr @nextOpA
.endif
;;
continueObjectLoopIfOpDone:
ld a,(de)
cp $f0
ret c
pop bc
jp parseGivenObjectData
;;
; @param de Source
; @param hl Destination
read2Bytes:
ld a,(de)
inc de
ldi (hl),a
ld a,(de)
inc de
ld (hl),a
ret
;;
; @param de Source
; @param hl Destination (an Object.yh variable)
readCoordinates:
ld a,(de)
inc de
ldi (hl),a
inc l
ld a,(de)
inc de
ld (hl),a
ret
;;
; @param hFF8B Flags that came with the enemy data
decEnemyCounterIfApplicable:
ldh a,(<hFF8B)
and $02
ret z
ld a,(wNumEnemies)
dec a
ld (wNumEnemies),a
ret
;;
; @param c Enemy position?
addPositionToPlacedEnemyPositions:
push hl
ld a,(wEnemyPlacement.numEnemies)
ld hl,wEnemyPlacement.placedEnemyPositions
rst_addAToHl
ld (hl),c
ld a,(wEnemyPlacement.numEnemies)
inc a
and $0f
ld (wEnemyPlacement.numEnemies),a
pop hl
ret
;;
; @param[out] cflag c if a valid position couldn't be found
assignRandomPositionToEnemy:
call getRandomPositionForEnemy
ret c
ld a,(wEnemyPlacement.enemyPos)
ld c,a
call addPositionToPlacedEnemyPositions
ldh a,(<hFF91)
ld h,a
ld l,Enemy.yh
call setShortPosition_paramC
xor a
ret
;;
; Calculates the value for an enemy's "enabled" byte (stored in hFF8D) such that the enemy
; is assigned an index, which allows the game to remember which enemies have been killed.
;
; @param[out] cflag nc if the enemy has been killed already (so it shouldn't spawn)
checkEnemyKilled:
ld a,(wEnemyPlacement.numKillableEnemies)
cp $07
jr nc,+
inc a
ld (wEnemyPlacement.numKillableEnemies),a
ld hl,bitTable
add l
ld l,a
ld a,(wEnemyPlacement.killedEnemiesBitset)
and (hl)
ret nz
; Calculate the value for the "enabled" byte, which contains the enemy's index.
ld a,(wEnemyPlacement.numKillableEnemies)
swap a
or $01
ldh (<hFF8D),a
+
scf
ret
.ends