Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 256.98 KB | None | 0 0
  1. .include "src\includes\structures.asm"
  2. .include "src\includes\objects.asm" ;values for objects
  3. .include "src\includes\player_states.asm" ;values for player object state
  4. .include "src\includes\sound_values.asm" ;values for music/sfx
  5.  
  6. .def CurrentSprite $D506
  7. .def HorizontalVelocity $D516
  8. .def VerticalVelocity $D518
  9.  
  10. .def BackgroundXScroll $D172
  11. .def BackgroundYScroll $D173
  12.  
  13. .def CameraOffsetX $D288
  14. .def CameraOffsetY $D289 ;used for moving the camera when looking up or down
  15. .def GameState $D293 ; bit 1 = pause flag
  16. .def CurrentLevel $D295
  17. .def CurrentAct $D296
  18. .def LifeCounter $D298
  19. .def RingCounter $D299 ;BCD
  20. .def Score $D29C ;score stored in 3-byte BCD
  21. .def ZoneTimer $D2B9 ;level timer
  22.  
  23. .def MaxHorizontalVelocity $D36D
  24. .def IdleTimer $D3B4
  25.  
  26. .def PlayerUnderwater $D467 ;non-zero = player under water
  27.  
  28. ;Variables used by the continue screen
  29. .def ContinueScreen_Count $D2C4
  30. .def ContinueScreen_Timer $D2C3 ;when this fires the countdown is decreased
  31.  
  32. ;Variables used by demo levels
  33. .def ControlByte $D2D2 ;holds a pointer to the controller byte to be copied into $D137
  34. .def DemoNumber $D2D7
  35. .def DemoBank $D2D8 ;holds the bank number that $D2D2 points into. This should be paged in before dereferencing the pointer.
  36. .def NextControlByte $D2D9 ;holds a pointer to the next controller byte to be copied into $D2D2
  37.  
  38. .def RingArt_SrcAddress $D395
  39. .def RingArt_DestAddress $D397
  40.  
  41.  
  42. ;Variables used by palette control/update routines
  43. .def PaletteFadeTime $D2C9 ;TODO: Chek this one
  44. .def BgPaletteControl $D4E6 ;Triggers background palette fades (bit 6 = to black, bit 7 = to colour).
  45. .def BgPaletteIndex $D4E7 ;Current background palette (index into array of palettes)
  46. .def FgPaletteControl $D4E8 ;Triggers foreground palette fades (bit 6 = to black, bit 7 = to colour).
  47. .def FgPaletteIndex $D4E9 ;Current foreground palette (index into array of palettes)
  48. .def PaletteUpdatePending $D4EA ;Will trigger a CRAM update when set
  49.  
  50. .def CurrentMusicTrack $DD03
  51. .def NextMusicTrack $DD04 ;Write bytes here to play music/sfx
  52.  
  53. ;VDP Values
  54. .def ScreenMap $3800 ;location of the screen map (name table)
  55. .def SAT $3F00 ;location of the Sprite Attribute Table
  56. .def VDPRegister0 $D11E ;copies of the VDP registers stored in RAM
  57. .def VDPRegister1 $D11F
  58. .def VDPRegister2 $D120
  59. .def VDPRegister3 $D121
  60. .def VDPRegister4 $D122
  61. .def VDPRegister5 $D123
  62. .def VDPRegister6 $D124
  63. .def SATUpdateRequired $D134 ;Causes the SAT to be udpated when set.
  64. .def ShadowedCRAM $D4C6 ;copy of CRAM kept in work RAM.
  65.  
  66. .MEMORYMAP
  67. SLOTSIZE $4000
  68. SLOT 0 $0000
  69. SLOT 1 $4000
  70. SLOT 2 $8000
  71. DEFAULTSLOT 2
  72. .ENDME
  73.  
  74. .ROMBANKMAP
  75. BANKSTOTAL 32
  76. BANKSIZE $4000
  77. BANKS 32
  78. .ENDRO
  79.  
  80. .EMPTYFILL $FF
  81.  
  82. .BANK 0 SLOT 0
  83. .ORG $0000
  84.  
  85. _START:
  86. di
  87. im 1
  88. ld sp, $DFF0
  89. ;set page-2 to map to ROM as specified in register $FFFF
  90. ld a, $00
  91. ld ($FFFC), a
  92. ;set page-0 to ROM bank-0
  93. ld a, $00
  94. ld ($FFFD), a
  95. ;set page-1 to ROM bank-1
  96. inc a
  97. ld ($FFFE), a
  98. ;set page-2 to ROM bank-2
  99. inc a
  100. ld ($FFFF), a
  101. _RST_18H:
  102. ;read current scanline
  103. in a, ($7E)
  104. ;wait for scanline to = 176
  105. cp $B0
  106. jr nz, _RST_18H
  107. ;Clear a work ram ($C001 to $DFEF)
  108. ld hl, $C001
  109. ld de, $C002
  110. ld bc, $1FEE
  111. ld (hl), $00
  112. ldir
  113.  
  114. ld a, $01
  115. ld ($D12A), a
  116. ld a, $02
  117. ld ($D12B), a
  118. jp LABEL_457_3
  119.  
  120. _IRQ_HANDLER:
  121. _INT_38H:
  122. di
  123. push af
  124. in a, ($BF)
  125. rlca
  126. jp c, LABEL_4A5_57
  127. jp LABEL_5B2_58
  128.  
  129. ; Data from 43 to 65 (35 bytes)
  130. .db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
  131. .db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
  132. .db $00, $02, $00
  133.  
  134. _NMI_HANDLER:
  135. jp _Pause_Handler
  136.  
  137.  
  138. _LOCATION_69:
  139. .db $00, $00, $00, $00, $00, $00, $00, $32, $00, $00
  140.  
  141.  
  142. ErrorTrap: ;$0073
  143. call ClearScreen
  144. ld a, $01
  145. ld ($D2C7), a
  146. ld hl, $3A4C ;scribble to this vram address
  147. ld de, _error_msg
  148. ld bc, $0005
  149. call DrawText
  150. jr +
  151. _error_msg:
  152. .db "ERROR"
  153. +: ld b, $B4
  154. -: ei
  155. halt
  156. djnz -
  157. jp $0000
  158.  
  159.  
  160. .db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
  161. .db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
  162. .db $00, $00, $00, $00, $00, $00, $00, $00, $00
  163.  
  164. .db "MS SONIC", $A5, "THE", $A5, "HEDGEHOG.2 "
  165. .db "Ver1.00 1992/09/05 SEGA /Aspect Co.,Ltd "
  166.  
  167.  
  168. .db $00, $80, $40, $C0, $20, $A0, $60, $E0, $10
  169. .db $90, $50, $D0, $30, $B0, $70, $F0, $08, $88, $48, $C8, $28, $A8, $68, $E8, $18
  170. .db $98, $58, $D8, $38, $B8, $78, $F8, $04, $84, $44, $C4, $24, $A4, $64, $E4, $14
  171. .db $94, $54, $D4, $34, $B4, $74, $F4, $0C, $8C, $4C, $CC, $2C, $AC, $6C, $EC, $1C
  172. .db $9C, $5C, $DC, $3C, $BC, $7C, $FC, $02, $82, $42, $C2, $22, $A2, $62, $E2, $12
  173. .db $92, $52, $D2, $32, $B2, $72, $F2, $0A, $8A, $4A, $CA, $2A, $AA, $6A, $EA, $1A
  174. .db $9A, $5A, $DA, $3A, $BA, $7A, $FA, $06, $86, $46, $C6, $26, $A6, $66, $E6, $16
  175. .db $96, $56, $D6, $36, $B6, $76, $F6, $0E, $8E, $4E, $CE, $2E, $AE, $6E, $EE, $1E
  176. .db $9E, $5E, $DE, $3E, $BE, $7E, $FE, $01, $81, $41, $C1, $21, $A1, $61, $E1, $11
  177. .db $91, $51, $D1, $31, $B1, $71, $F1, $09, $89, $49, $C9, $29, $A9, $69, $E9, $19
  178. .db $99, $59, $D9, $39, $B9, $79, $F9, $05, $85, $45, $C5, $25, $A5, $65, $E5, $15
  179. .db $95, $55, $D5, $35, $B5, $75, $F5, $0D, $8D, $4D, $CD, $2D, $AD, $6D, $ED, $1D
  180. .db $9D, $5D, $DD, $3D, $BD, $7D, $FD, $03, $83, $43, $C3, $23, $A3, $63, $E3, $13
  181. .db $93, $53, $D3, $33, $B3, $73, $F3, $0B, $8B, $4B, $CB, $2B, $AB, $6B, $EB, $1B
  182. .db $9B, $5B, $DB, $3B, $BB, $7B, $FB, $07, $87, $47, $C7, $27, $A7, $67, $E7, $17
  183. .db $97, $57, $D7, $37, $B7, $77, $F7, $0F, $8F, $4F, $CF, $2F, $AF, $6F, $EF, $1F
  184. .db $9F, $5F, $DF, $3F, $BF, $7F, $FF
  185.  
  186. ;********************************************************
  187. ;* Main logic vtable - use extensively by object logic *
  188. ;********************************************************
  189. LABEL_200:
  190. jp LABEL_6144 ;$200 - find an available object slot from $D540 onwards
  191. jp LABEL_6165 ;$203 - find an available object slot from $D700 onwards
  192. jp LABEL_6248 ;$206
  193. VF_Engine_MoveObjectToPlayer:
  194. jp Engine_MoveObjectToPlayer ;$209
  195. jp LABEL_627C ;$20C
  196. VF_Engine_SetObjectVerticalSpeed:
  197. jp Engine_SetObjectVerticalSpeed ;$20F
  198. jp LABEL_631A ;$212
  199. jp LABEL_6344 ;$215
  200. jp LABEL_634F ;$218
  201. jp LABEL_6355 ;$21B
  202. jp LABEL_635B ;$21E
  203. jp LABEL_63A9 ;$221
  204. jp LABEL_63C0 ;$224
  205. jp LABEL_63F1 ;$227
  206. VF_DoNothing:
  207. jp DoNothingStub ;$22A
  208. jp LABEL_64B1 ;$22D
  209. VF_Engine_GetObjectIndexFromPointer:
  210. jp Engine_GetObjectIndexFromPointer ;$230
  211. VF_Engine_GetObjectDescriptorPointer:
  212. jp Engine_GetObjectDescriptorPointer ;$233
  213. jp LABEL_64D4 ;54
  214. jp LABEL_6544 ;57
  215. jp LABEL_657B ;60
  216. jp LABEL_6592
  217. jp LABEL_6596 ;$242
  218. jp LABEL_65AC ;$245
  219. jp LABEL_65B5 ;$248
  220. jp LABEL_65EB ;$24B
  221. jp LABEL_67EC ;$24E
  222. jp LABEL_67F1 ;$251
  223. jp LABEL_67F6 ;$254
  224. VF_Engine_UpdateObjectPosition:
  225. jp Engine_UpdateObjectPosition ;$257
  226. VF_Engine_CheckCollision:
  227. jp Engine_CheckCollision ;$25A
  228. VF_Engine_DisplayExplosionObject:
  229. jp Engine_DisplayExplosionObject ;$25D
  230. jp LABEL_75C5 ;$260
  231. jp Engine_GetCollisionValueForBlock ;$263 - collide with background tiles
  232. jp LABEL_2FCB ;$266
  233. VF_Player_HandleStanding:
  234. jp Player_HandleStanding ;$269
  235. VF_Engine_IncrementRingCounter:
  236. jp IncrementRingCounter ;$26C
  237. jp LABEL_39E8 ;$26F
  238. jp LABEL_399B ;$272
  239. jp LABEL_49B0 ;$275
  240. jp LABEL_49AA ;$278
  241. VF_Player_PlayHurtAnimation:
  242. jp Player_PlayHurtAnimation ;$27B
  243. jp LABEL_4037 ;$27E
  244. jp LABEL_46BB ;$281
  245. VF_Player_HandleVerticalSpring:
  246. jp Player_HandleVerticalSpring ;$284
  247. jp LABEL_34DA ;$287
  248. jp LABEL_3484 ;$28A
  249. VF_Player_HandleFalling:
  250. jp Player_HandleFalling ;$28D
  251. jp LABEL_41DD ;$290
  252. jp LABEL_40D6 ;$293
  253. jp LABEL_408E ;$296
  254. jp LABEL_4199 ;$299
  255. jp LABEL_40B2 ;$29C
  256. VF_Player_HandleJumping:
  257. jp Player_HandleJumping ;$29F
  258. jp LABEL_424A ;$2A2
  259. jp LABEL_343E ;$2A5
  260. VF_Player_HandleCrouched:
  261. jp Player_HandleCrouched ;$2A8
  262. VF_Player_HandleLookUp:
  263. jp Player_HandleLookUp ;$2AB
  264. jp LABEL_438A ;$2AE
  265. VF_Player_HandleRolling:
  266. jp Player_HandleRolling ;$2B1
  267. jp LABEL_359B ;$2B4 - loop motion
  268. jp LABEL_366F ;$2B7
  269. VF_Player_HandleRunning:
  270. jp Player_HandleRunning ;$2BA
  271. jp LABEL_34FD ;$2BD
  272. jp LABEL_3467 ;$2C0
  273. VF_Player_HandleIdle:
  274. jp Player_HandleIdle ;$2C3
  275. jp LABEL_375E ;$2C6
  276. jp LABEL_375F ;$2C9
  277. VF_Player_HandleSkidRight:
  278. jp Player_HandleSkidRight ;$2CC
  279. VF_Player_HandleSkidLeft:
  280. jp Player_HandleSkidLeft ;$2CF
  281. jp LABEL_46C0 ;$2D2
  282. jp LABEL_46EA ;$2D5
  283. VF_Player_HandleWalk:
  284. jp Player_HandleWalk ;$2D8
  285. jp Player_MineCart_Handle ;$2DB
  286. jp LABEL_7F15 ;$2DE
  287. jp LABEL_33B7 ;$2E1
  288. jp LABEL_7248 ;$2E4
  289. jp LABEL_348F ;$2E7
  290. jp LABEL_34A4 ;$2EA - collide with air bubble - reset air timer.
  291. jp LABEL_20FB ;$2ED
  292. jp Player_HandleBalance ;$2F0
  293. jp LABEL_49CF ;$2F3
  294. jp LABEL_49BC ;$2F6
  295. VF_Logic_ChangeFrameDisplayTime:
  296. jp LABEL_38EE ;$2F9 - change frame display time based on speed
  297. jp LABEL_3923 ;$2FC
  298. jp LABEL_34A9 ;$2FF
  299. VF_Engine_SetMinimumCameraX:
  300. jp Engine_SetMinimumCameraX ;$302
  301. VF_Engine_SetMaximumCameraX:
  302. jp Engine_SetMaximumCameraX ;$305
  303. jp LABEL_34E1 ;$308
  304. jp LABEL_25AC ;$30B
  305. jp LABEL_25DB ;$30E
  306. jp LABEL_6614 ;$311
  307. VF_Engine_ChangeLevelMusic:
  308. jp Engine_ChangeLevelMusic ;$314
  309. jp LABEL_1CB8 ;$317
  310. VF_Score_AddBossValue:
  311. jp Score_AddBossValue ;$31A
  312. jp LABEL_1CC4 ;$31D
  313. jp LABEL_1CCA ;$320
  314.  
  315. .db $00, $00, $00, $00, $00, $00
  316. .db $00, $00, $00, $00, $00, $00, $00
  317.  
  318. DATA_330:
  319. .db $00, $03, $06, $09, $0C, $0F, $12, $15, $19
  320. .db $1C, $1F, $22, $25, $28, $2B, $2E, $31, $34, $36, $39, $3C, $3F, $42, $44, $47
  321. .db $49, $4C, $4F, $51, $53, $56, $58, $5A, $5C, $5F, $61, $63, $65, $67, $68, $6A
  322. .db $6C, $6E, $6F, $71, $72, $73, $75, $76, $77, $78, $79, $7A, $7B, $7C, $7D, $7D
  323. .db $7E, $7E, $7F, $7F, $7F, $7F, $7F, $7F, $7F, $7F, $7F, $7F, $7E, $7E, $7D, $7D
  324. .db $7C, $7B, $7B, $7A, $79, $78, $77, $75, $74, $73, $71, $70, $6E, $6D, $6B, $69
  325. .db $68, $66, $64, $62, $60, $5E, $5B, $59, $57, $55, $52, $50, $4D, $4B, $48, $46
  326. .db $43, $40, $3D, $3B, $38, $35, $32, $2F, $2C, $29, $26, $23, $20, $1D, $1A, $17
  327. .db $14, $11, $0E, $0B, $07, $04, $01, $00, $FF, $FC, $F9, $F5, $F2, $EF, $EC, $E9
  328. .db $E6, $E3, $E0, $DD, $DA, $D7, $D4, $D1, $CE, $CB, $C8, $C5, $C3, $C0, $BD, $BA
  329. .db $B8, $B5, $B3, $B0, $AE, $AB, $A9, $A7, $A5, $A2, $A0, $9E, $9C, $9A, $98, $97
  330. .db $95, $93, $92, $90, $8F, $8D, $8C, $8B, $89, $88, $87, $86, $85, $85, $84, $83
  331. .db $83, $82, $82, $81, $81, $81, $81, $81, $81, $81, $81, $81, $81, $82, $82, $83
  332. .db $83, $84, $85, $86, $87, $88, $89, $8A, $8B, $8D, $8E, $8F, $91, $92, $94, $96
  333. .db $98, $99, $9B, $9D, $9F, $A1, $A4, $A6, $A8, $AA, $AD, $AF, $B1, $B4, $B7, $B9
  334. .db $BC, $BE, $C1, $C4, $C7, $CA, $CC, $CF, $D2, $D5, $D8, $DB, $DE, $E1, $E4, $E7
  335. .db $EB, $EE, $F1, $F4, $F7, $FA, $FD, $00
  336.  
  337. Start:
  338. LABEL_431_86:
  339. di
  340. ld sp, $DFF0
  341. call _VDP_Set_Register_1_flags_0_to_5
  342. ld hl, $C001 ;clear work RAM
  343. ld de, $C002
  344. ld bc, $1FEE
  345. ld (hl), $00
  346. ldir
  347. ;setup default paging
  348. ld a, $00
  349. ld ($FFFC), a
  350. ld a, $00
  351. ld ($FFFD), a
  352. inc a
  353. ld ($FFFE), a
  354. inc a
  355. ld ($FFFF), a
  356. LABEL_457_3:
  357. call LABEL_12A3_4 ;possibly wait for vblank routine
  358. call Sound_ResetChannels
  359. call Initialise_VDP_Registers
  360. ;clear screen?
  361. call ClearPaletteRAM
  362. call ClearVRAM
  363. call LoadLevelTiles
  364. call LevelSelectCheck
  365. call EnableFrameInterrupt
  366. call SetDisplayVisible
  367. LABEL_472:
  368. ld hl, $D292
  369. set 6, (hl)
  370. ld a, $04 ;set the palette fade timeout value (will fade every 4th VBLANK)
  371. ld ($D2C8), a
  372. call ChangeGameMode
  373. di
  374. call ClearScreen
  375. call ClearWorkingVRAM
  376. xor a
  377. ld ($D292), a
  378. call Engine_InitCounters
  379. ei
  380. halt
  381. ld hl, GameState
  382. ld (hl), $44
  383. jp Engine_UpdateGameState
  384.  
  385. LevelSelectCheck:
  386. xor a
  387. ld ($D12C), a
  388. in a, ($DD)
  389. cpl
  390. ;-----------------
  391. ;ld a, $0d
  392. ;nop
  393. ;-----------------
  394. cp $0D
  395. ret nz
  396. ld ($D12C), a
  397. ret
  398.  
  399. ;Main game loop
  400. _VSyncInterrupt:
  401. LABEL_4A5_57:
  402. ex af, af'
  403. push af
  404. push bc
  405. push de
  406. push hl
  407. exx
  408. push bc
  409. push de
  410. push hl
  411. push ix
  412. push iy
  413. ld a, ($D4A3) ;only updates palettes
  414. or a
  415. jp nz, LABEL_58D_61
  416. ld a, ($D136) ;Only allows sound driver to update
  417. or a
  418. jp nz, LABEL_55D_62
  419. call _VDP_Set_Register_1_flags_0_to_5
  420. ld bc, $0000
  421. ld a, ($D290) ;do we need to default the background scroll values?
  422. or a
  423. jp z, LABEL_4DD_63
  424. dec a
  425. ld ($D290), a
  426. and $06
  427. ld e, a
  428. ld d, $00
  429. ld hl, DefaultScrollValues
  430. add hl, de
  431. ld c, (hl)
  432. inc hl
  433. ld b, (hl)
  434. LABEL_4DD_63:
  435. ;Set the background X-scroll value (write to register VDP(8))
  436. ld a, (BackgroundXScroll)
  437. add a, b
  438. out ($BF), a
  439. ld a, $88
  440. out ($BF), a
  441. ;Set the background Y-scroll value (write to register VDP(9))
  442. ld a, (BackgroundYScroll)
  443. add a, c
  444. out ($BF), a
  445. ld a, $89
  446. out ($BF), a
  447.  
  448. ld a, ($D15E)
  449. bit 6, a
  450. jr z, LABEL_51B_64
  451. res 6, a
  452. ld ($D15E), a
  453. ld ix, $D15E
  454. bit 4, (ix+0)
  455. call nz, LABEL_5966_65
  456. bit 5, (ix+0)
  457. call nz, LABEL_58BA_71
  458. ld hl, ($D284)
  459. ld ($D174), hl
  460. ld hl, ($D286)
  461. ld ($D176), hl
  462. LABEL_51B_64:
  463. call UpdateSAT
  464. call LABEL_7FAE_77
  465. call SetDisplayVisible
  466. call _WriteToPaletteRAM
  467. call ReadInput
  468. call Engine_LoadPlayerTiles
  469.  
  470. ld a, (GameState)
  471. bit 1, a ;check whether game is paused
  472. jr nz, LABEL_55D_62
  473.  
  474. ;load ROM bank-9 into page-2 and call the palette update code
  475. ld a, :Bank09
  476. ld ($FFFF), a
  477. call Palette_Update
  478.  
  479. ld a, (GameState)
  480. bit 6, a
  481. jr z, LABEL_55D_62
  482. ld a, (GameState)
  483. bit 2, a
  484. jr nz, LABEL_55D_62
  485. ld a, :Bank31
  486. ld ($FFFF), a
  487. call Engine_UpdateSpriteAttribs
  488. call Engine_UpdateSpriteAttribsArt
  489. ld a, :Bank30 ;page in the bank with the cycling palette data
  490. ld ($FFFF), a
  491. call UpdateCyclingPalettes
  492. LABEL_55D_62:
  493. ;load ROM bank-2 into page-2
  494. ld a, :Bank2
  495. ld ($FFFF), a
  496. call LABEL_8000_92
  497. ld a, ($D292)
  498. or a
  499. call nz, LABEL_59E_218
  500. call Engine_Timer_Increment
  501. ld hl, $D12F
  502. inc (hl)
  503. ld a, ($D12B)
  504. ld ($FFFF), a
  505. ld hl, $D135 ;pause timer
  506. inc (hl)
  507. pop iy
  508. pop ix
  509. pop hl
  510. pop de
  511. pop bc
  512. exx
  513. pop hl
  514. pop de
  515. pop bc
  516. pop af
  517. ex af, af'
  518. pop af
  519. ei
  520. ret
  521.  
  522. LABEL_58D_61:
  523. call _WriteToPaletteRAM
  524. jp LABEL_55D_62
  525.  
  526.  
  527. WaitForInterrupt:
  528. LABEL_593:
  529. ei
  530. ld hl, $D135
  531. -: ld a, (hl)
  532. or a
  533. jr z, -
  534. ld (hl), $00
  535. ret
  536.  
  537. LABEL_59E_218:
  538. ld a, ($D157)
  539. and $30
  540. ret z
  541. ld hl, $D292
  542. set 7, (hl)
  543. ret
  544.  
  545.  
  546. ; Data from 5AA to 5B1 (8 bytes)
  547. DefaultScrollValues:
  548. _DATA_05AA:
  549. .db $FE, $00, $FE, $FE, $02, $02, $00, $02
  550.  
  551. LABEL_5B2_58:
  552. ;Set register VDP(10) to 255 (i.e. No line interrupt)
  553. ld a, $FF
  554. out ($BF), a
  555. ld a, $8A
  556. out ($BF), a
  557.  
  558. ld a, ($D132)
  559. inc a
  560. jr nz, _VDP_Palette_Setup
  561. ld a, (PlayerUnderwater)
  562. or a
  563. jp z, LABEL_656_60 ;bail out
  564. _VDP_Palette_Setup:
  565. push hl
  566. ;set up the VDP to write to palette RAM at address $0
  567. ld a, $00
  568. out ($BF), a
  569. ld a, $C0
  570. out ($BF), a
  571. ;write the 16 colours to the VRAM
  572. ld hl, DATA_65A
  573. ld a, (hl)
  574. out ($BE), a
  575. inc hl
  576. nop
  577. nop
  578. nop
  579. nop
  580. ld a, (hl)
  581. out ($BE), a
  582. inc hl
  583. nop
  584. nop
  585. nop
  586. nop
  587. ld a, (hl)
  588. out ($BE), a
  589. inc hl
  590. nop
  591. nop
  592. nop
  593. nop
  594. ld a, (hl)
  595. out ($BE), a
  596. inc hl
  597. nop
  598. nop
  599. nop
  600. nop
  601. ld a, (hl)
  602. out ($BE), a
  603. inc hl
  604. nop
  605. nop
  606. nop
  607. nop
  608. ld a, (hl)
  609. out ($BE), a
  610. inc hl
  611. nop
  612. nop
  613. nop
  614. nop
  615. ld a, (hl)
  616. out ($BE), a
  617. inc hl
  618. nop
  619. nop
  620. nop
  621. nop
  622. ld a, (hl)
  623. out ($BE), a
  624. inc hl
  625. nop
  626. nop
  627. nop
  628. nop
  629. ld a, (hl)
  630. out ($BE), a
  631. inc hl
  632. nop
  633. nop
  634. nop
  635. nop
  636. ld a, (hl)
  637. out ($BE), a
  638. inc hl
  639. nop
  640. nop
  641. nop
  642. nop
  643. ld a, (hl)
  644. out ($BE), a
  645. inc hl
  646. nop
  647. nop
  648. nop
  649. nop
  650. ld a, (hl)
  651. out ($BE), a
  652. inc hl
  653. nop
  654. nop
  655. nop
  656. nop
  657. ld a, (hl)
  658. out ($BE), a
  659. inc hl
  660. nop
  661. nop
  662. nop
  663. nop
  664. ld a, (hl)
  665. out ($BE), a
  666. inc hl
  667. nop
  668. nop
  669. nop
  670. nop
  671. ld a, (hl)
  672. out ($BE), a
  673. inc hl
  674. nop
  675. nop
  676. nop
  677. nop
  678. ld a, (hl)
  679. out ($BE), a
  680. inc hl
  681. nop
  682. nop
  683. nop
  684. nop
  685. ld (PaletteUpdatePending), a
  686. LABEL_656_60:
  687. pop hl
  688. pop af
  689. ei
  690. ret
  691.  
  692.  
  693. DATA_65A:
  694. ; Data from 65A to 671 (24 bytes)
  695. .db $10, $10, $24, $39, $39, $24, $34, $38, $28, $14, $3D, $3F, $00, $3D, $3E, $3F
  696. .db $3E, $00, $D3, $BF, $3E, $88, $D3, $BF
  697.  
  698. _Pause_Handler:
  699. push af
  700. ld a, (BgPaletteControl)
  701. or a
  702. jr nz, LABEL_68D_231
  703. ld a, ($D292)
  704. or a
  705. jr nz, LABEL_68D_231
  706. ld a, (GameState)
  707. and $FD
  708. cp $40
  709. jr nz, LABEL_68D_231
  710. ld a, $01
  711. ld ($D12E), a
  712. LABEL_68D_231:
  713. pop af
  714. retn
  715.  
  716.  
  717. Engine_UpdateGameState: ;$690
  718. call WaitForInterrupt
  719. ld a, (GameState)
  720. rlca
  721. jp c, + ;bit 7 - ending sequence
  722. rlca
  723. jp nc, LABEL_72F ;bit 6 - game over/continue
  724. rlca
  725. jp c, ScoreCard_NextAct ;bit 5 - end of act core card
  726. rlca
  727. jp c, ScoreCard_NextZone ;bit 4 - end of zone score card
  728. rlca
  729. jp c, Player_DoLoseLife ;bit 3 - player dead
  730. rlca
  731. jp c, LABEL_97F ;bit 2 - title card - load level
  732. rlca
  733. jp c, LABEL_9C4
  734. call Engine_UpdateLevelState
  735. call LABEL_A13
  736. jr Engine_UpdateGameState
  737.  
  738.  
  739. ;run the ending sequence
  740. +: call LABEL_21AA ;load the level
  741. call Engine_ChangeLevelMusic
  742. call LoadLevelPalette
  743. call LABEL_49AA
  744. ld a, Music_Ending
  745. ld ($DD04), a
  746. call LABEL_6F3
  747. xor a
  748. ld ($D4A6), a
  749. ld ($D4AE), a
  750. call PaletteFadeOut
  751. ld b, $3C
  752. -: ei
  753. halt
  754. djnz -
  755. xor a
  756. ld ($D2BD), a ;used by the continue screen routines
  757. ld ($D2C5), a
  758. ld hl, GameState
  759. res 7, (hl)
  760. res 6, (hl)
  761. res 4, (hl)
  762. res 3, (hl)
  763. jp Engine_UpdateGameState
  764.  
  765. LABEL_6F3: ;ending credits sequence
  766. ld a, $5D
  767. ld ($D700), a
  768. ld a, $01
  769. ld ($D500), a
  770. ld a, $2A
  771. ld ($D502), a
  772. xor a
  773. ld ($D2B8), a
  774. ld a, $0D ;set up the cycling palette
  775. ld ($D4A6), a
  776. ld a, :Bank29
  777. call Engine_SwapFrame2
  778. call LABEL_B29_B400 ;move the last 16 sprites in the SAT off of the screen.
  779. ld hl, $012C
  780. ld ($D46F), hl
  781. call WaitForInterrupt
  782. call Engine_UpdateLevelState
  783. ld a, :Bank29
  784. call Engine_SwapFrame2
  785. call LABEL_B29_B40C
  786. ld a, ($D701)
  787. cp $06
  788. jr nz, $EB
  789. ret
  790.  
  791. LABEL_72F:
  792. xor a
  793. ld ($DD03), a ;stop music
  794. ei
  795. halt
  796. call ClearWorkingVRAM ;clear all copies of VRAM values from work RAM.
  797. call Engine_ClearLevelAttributes ;clear level header
  798. call ClearScreen
  799. call GameState_CheckContinue ;load the continue screen if required
  800. ld a, ($D2BD) ;load the "Game Over" screen if bit 7 of $D2BD is reset.
  801. bit 7, a
  802. jr nz, GameState_DoContinue
  803. ld a, Music_GameOver
  804. ld ($DD04), a
  805. call GameOverScreen_DrawScreen
  806. ld b, $78
  807. -: ei
  808. halt
  809. djnz -
  810. ld bc, $0D98
  811. - call WaitForInterrupt
  812. ld a, ($D137) ;check to see if button 1/2 is pressed
  813. and $30
  814. jr nz, +
  815. dec bc
  816. ld a, b
  817. or c
  818. jr nz, -
  819. +: call PaletteFadeOut
  820. call FadeMusic
  821. ld b, $3C
  822. -: ei
  823. halt
  824. djnz -
  825. jp LABEL_472
  826.  
  827. GameState_DoContinue:
  828. LABEL_777:
  829. ld a, $03
  830. ld (LifeCounter), a
  831. xor a
  832. ld ($D29C), a
  833. ld ($D29D), a
  834. ld ($D29E), a
  835. ld hl, GameState
  836. res 3, (hl)
  837. set 2, (hl)
  838. set 6, (hl)
  839. jp Engine_UpdateGameState
  840.  
  841. GameState_CheckContinue:
  842. LABEL_792:
  843. ld hl, $D2BD
  844. res 7, (hl)
  845. ld a, (hl)
  846. or a
  847. ret z
  848. xor a ;reset the timer
  849. ld (ContinueScreen_Timer), a
  850. ld a, $09 ;set the countdown to 9
  851. ld (ContinueScreen_Count), a
  852. call ContinueScreen_DrawScreen
  853. call ContinueScreen_LoadNumberMappings
  854. ld ix, $D500
  855. ld (ix+0), $01
  856. ld (ix+1), $2F
  857. ld (ix+2), $2F
  858. call Engine_Engine_UpdateObjectState
  859. ld a, Music_Continue
  860. ld ($DD04), a
  861. ld hl, GameState
  862. set 6, (hl)
  863. call ContinueScreen_MainLoop
  864. ld hl, GameState
  865. res 6, (hl)
  866. call PaletteFadeOut
  867. call FadeMusic
  868. ld b, $3C
  869. -: ei
  870. halt
  871. djnz -
  872. ret
  873.  
  874. ContinueScreen_MainLoop: ;7DB
  875. call WaitForInterrupt
  876. call Engine_Engine_UpdateObjectState
  877. ld a, ($D147)
  878. and $30 ;is button 1/2 pressed?
  879. jr nz, +
  880. ld hl, ContinueScreen_Timer ;increase the timer
  881. inc (hl)
  882. ld a, (hl)
  883. cp $5A ;if the timer = $5A, decrement the countdown
  884. jr c, ContinueScreen_MainLoop
  885. ld (hl), $00
  886. inc hl
  887. dec (hl)
  888. ld a, (hl)
  889. cp $FF ;countown expired
  890. ret z
  891. call ContinueScreen_LoadNumberMappings
  892. ld a, SFX_LoseRings
  893. ld ($DD04), a
  894. jr ContinueScreen_MainLoop
  895. +: ld hl, $D2BD
  896. res 7, (hl)
  897. dec (hl)
  898. set 7, (hl)
  899. ld a, SFX_ExtraLife
  900. ld ($DD04), a
  901. ld b, $B4
  902. -: push bc
  903. call WaitForInterrupt
  904. call Engine_Engine_UpdateObjectState
  905. pop bc
  906. djnz -
  907. ret
  908.  
  909. LABEL_81D:
  910. jp Engine_UpdateGameState
  911.  
  912. ScoreCard_NextAct:
  913. LABEL_820:
  914. call LABEL_A27 ;reset $D2B8
  915. call LABEL_878
  916. ld b, $B4
  917. -: ei
  918. halt
  919. djnz -
  920. ld a, (LifeCounter) ;store number of lives when starting the act
  921. ld ($D297), a
  922. call LABEL_7D32 ;reset D4A6, D4AE, D397
  923. call PaletteFadeOut ;trigger FG & BG palette fade to black
  924. ld b, $1E
  925. -: ei
  926. halt
  927. djnz -
  928. ld a, $00 ;stop music
  929. ld ($DD04), a
  930. call ClearWorkingVRAM ;clear various blocks of RAM & prepare the SAT
  931. call Engine_ClearLevelAttributes
  932. call ClearScreen
  933. call LABEL_1EEE
  934. call TitleCard_LoadAndDraw ;also deals with loading score card tiles
  935. call ScoreCard_UpdateScore
  936. ld b, $3C
  937. -: ei
  938. halt
  939. djnz -
  940. call PaletteFadeOut ;trigger FG & BG palette fade to black
  941. ld b, $3C
  942. -: ei
  943. halt
  944. djnz -
  945. ld hl, GameState
  946. res 5, (hl)
  947. ;increment the act counter
  948. ld a, (CurrentAct)
  949. inc a
  950. ld (CurrentAct), a
  951. ld hl, GameState
  952. set 2, (hl)
  953. jp Engine_UpdateGameState
  954.  
  955. LABEL_878:
  956. ld a, ($D2C6)
  957. or a
  958. ret z
  959. ld a, ($D2BD)
  960. inc a
  961. ld ($D2BD), a
  962. ld a, (CurrentLevel)
  963. inc a
  964. ld b, a
  965. xor a
  966. scf
  967. -: rla
  968. djnz -
  969. ld b, a
  970. ld a, ($D2C5)
  971. or b
  972. ld ($D2C5), a
  973. xor a
  974. ld ($D2C6), a
  975. ret
  976.  
  977.  
  978. ScoreCard_NextZone:
  979. ;LABEL_89B:
  980. call LABEL_A27
  981. call LABEL_878
  982. ld bc, $012C
  983. LABEL_8A4:
  984. push bc
  985. call WaitForInterrupt
  986. call Engine_UpdateLevelState
  987. pop bc
  988. dec bc
  989. ld a, b
  990. or c
  991. jp nz, LABEL_8A4
  992. xor a ;stop music
  993. ld ($DD03), a
  994. call LABEL_7D32
  995. call PaletteFadeOut
  996. ld b, $1E
  997. -: ei
  998. halt
  999. djnz -
  1000. call ClearWorkingVRAM
  1001. call Engine_ClearLevelAttributes
  1002. call ClearScreen
  1003. call LABEL_1EEE
  1004. call TitleCard_LoadAndDraw
  1005. call ScoreCard_UpdateScore
  1006. ld b, $3C
  1007. -: ei
  1008. halt
  1009. djnz -
  1010. call PaletteFadeOut
  1011. ld b, $3C
  1012. -: ei
  1013. halt
  1014. djnz -
  1015. ;reset the act counter & increment level counter
  1016. xor a
  1017. ld (CurrentAct), a
  1018. ld ($D2C6), a
  1019. ld a, (CurrentLevel)
  1020. inc a
  1021. ld (CurrentLevel), a
  1022. cp $06
  1023. jr c, +
  1024. jr z, ++
  1025. ld a, ($D2C5)
  1026. and $3F
  1027. cp $3F
  1028. jr nz, LABEL_91B
  1029. ld a, $01
  1030. ld (CurrentAct), a
  1031. jp LABEL_91B
  1032.  
  1033. ++: ld a, ($D2C5)
  1034. and $1F
  1035. cp $1F
  1036. jr nz, LABEL_91B
  1037.  
  1038. +: ld hl, GameState ;play ending sequence
  1039. res 4, (hl)
  1040. set 2, (hl)
  1041. jp Engine_UpdateGameState
  1042.  
  1043. LABEL_91B:
  1044. ld a, $07
  1045. ld (CurrentLevel), a
  1046. ld hl, GameState
  1047. res 4, (hl)
  1048. set 7, (hl)
  1049. jp Engine_UpdateGameState
  1050.  
  1051. Player_DoLoseLife:
  1052. LABEL_92A:
  1053. call LABEL_A27
  1054. ld hl, LifeCounter
  1055. dec (hl)
  1056. call LABEL_25AC
  1057. call WaitForInterrupt
  1058. call Engine_Engine_UpdateObjectState
  1059. ld a, ($D502)
  1060. cp PlayerState_LostLife
  1061. jr z, $05
  1062. ld a, PlayerState_LostLife
  1063. ld ($D502), a
  1064. ld a, ($D51D)
  1065. cp $02
  1066. jr nz, $E7
  1067. ld b, $78
  1068. -: ei
  1069. halt
  1070. djnz -
  1071. ld a, $00 ;stop music
  1072. ld (NextMusicTrack), a
  1073. call LABEL_7D32
  1074. call PaletteFadeOut
  1075. ld b, $3C
  1076. -: ei
  1077. halt
  1078. djnz -
  1079. ld a, (LifeCounter)
  1080. or a
  1081. jr z, $0D
  1082. ld (LifeCounter), a
  1083. ld hl, GameState
  1084. res 3, (hl)
  1085. set 2, (hl)
  1086. jp Engine_UpdateGameState
  1087.  
  1088.  
  1089. ld hl, GameState
  1090. res 6, (hl)
  1091. jp Engine_UpdateGameState
  1092.  
  1093. LABEL_97F:
  1094. call ClearScreen
  1095. call TitleCard_LoadAndDraw
  1096. call ScrollingText_UpdateSprites
  1097. call PaletteFadeOut
  1098. call FadeMusic
  1099. ld b, $2A
  1100. -: ei
  1101. halt
  1102. djnz -
  1103. call LABEL_21AA
  1104. call CapLifeCounterValue
  1105. call LABEL_25DB
  1106. call Engine_Timer_SetSprites
  1107. call Engine_ChangeLevelMusic
  1108. call LoadLevelPalette
  1109. call LABEL_49AA
  1110. ld a, (CurrentLevel) ;check to see if we're on ALZ-2 and set the water level
  1111. cp $02
  1112. jr nz, +
  1113. ld a, (CurrentAct)
  1114. dec a
  1115. jr nz, +
  1116. ld hl, $0100 ;set water level
  1117. ld ($D4A4), hl
  1118. +: ld hl, GameState
  1119. res 2, (hl)
  1120. jp Engine_UpdateGameState
  1121.  
  1122. LABEL_9C4:
  1123. xor a
  1124. ld ($D2B8), a
  1125. -: ld a, ($D12E)
  1126. or a
  1127. jr z, -
  1128. xor a
  1129. ld ($D12E), a
  1130. xor a
  1131. ld ($DD08), a
  1132. ld a, $FF
  1133. ld ($D2B8), a
  1134. ld hl, GameState
  1135. res 1, (hl)
  1136. halt
  1137. jp Engine_UpdateGameState
  1138.  
  1139. Engine_UpdateLevelState: ;$9E4
  1140. ld ix, $D15E ;load the pointer to the level descriptor
  1141. bit 6, (ix+0)
  1142. ret nz
  1143. call Engine_UpdateCameraPos
  1144. di
  1145. call Engine_Engine_UpdateObjectState
  1146. ei
  1147. call Engine_UpdateAllObjects
  1148. call Engine_LoadSpriteTiles
  1149. call Engine_UpdateSHZ2Wind
  1150. ld a, ($D2D6)
  1151. and $0B
  1152. jp nz, + ;update object layout every 11th frame
  1153. ld a, :Bank30 ;load the level's sprite layout
  1154. call Engine_SwapFrame2
  1155. call LABEL_B30_8000
  1156. +: ld hl, $D2D6
  1157. inc (hl)
  1158. ret
  1159.  
  1160. LABEL_A13:
  1161. ld a, ($D12E)
  1162. or a
  1163. ret z
  1164. xor a
  1165. ld ($D12E), a
  1166. ld a, $80
  1167. ld ($DD08), a
  1168. ld hl, GameState
  1169. set 1, (hl)
  1170. ret
  1171.  
  1172. LABEL_A27:
  1173. xor a
  1174. ld ($D2B8), a
  1175. ret
  1176.  
  1177.  
  1178. .include "src\level_select.asm"
  1179. ;.include "src\sound_select.asm"
  1180.  
  1181.  
  1182. _Load_Intro_Level:
  1183. di
  1184. call Engine_ClearLevelAttributes
  1185. ld a, $09
  1186. ld (CurrentLevel), a
  1187. ld a, $00
  1188. ld (CurrentAct), a
  1189. di
  1190. call LABEL_21AA ;load the level
  1191.  
  1192. ld a, $7C
  1193. ld (CameraOffsetX), a
  1194. ld a, $74
  1195. ld (CameraOffsetY), a
  1196. call Engine_CalculateCameraBounds ;setup screen offsets
  1197. di
  1198.  
  1199. ld a, :Bank08 ;load background scenery
  1200. call Engine_SwapFrame2
  1201. ld hl, $0200
  1202. call VDPWrite
  1203. ld hl, Art_Intro_Scenery
  1204. xor a
  1205. call LoadTiles
  1206.  
  1207. ld a, :Bank19 ;load tails tiles
  1208. call Engine_SwapFrame2
  1209. ld hl, $0800
  1210. call VDPWrite
  1211. ld hl, Art_Intro_Tails
  1212. xor a
  1213. call LoadTiles
  1214.  
  1215.  
  1216. ld a, :Bank19 ;load robotnik tiles
  1217. call Engine_SwapFrame2
  1218. ld hl, $0B40
  1219. call VDPWrite
  1220. ld hl, Art_Intro_Tails_Eggman
  1221. xor a
  1222. call LoadTiles
  1223.  
  1224. di
  1225. ld a, $2C
  1226. ld ($D173), a ;store vert. scroll value
  1227. ld a, $5F
  1228. ld ($D740), a
  1229. ld a, $1C
  1230. ld ($D7C0), a
  1231. ld a, $1D
  1232. ld ($D800), a
  1233. ld ($D840),a
  1234. call LoadLevelPalette
  1235. ld a, $40
  1236. ld (GameState), a
  1237. call LABEL_49AA
  1238. ld a, Music_Intro ;Play intro music
  1239. ld (NextMusicTrack), a
  1240. LABEL_F41:
  1241. call WaitForInterrupt
  1242. call LABEL_107C
  1243. call Engine_UpdateLevelState
  1244. ld hl, ($D511)
  1245. inc hl
  1246. ld ($D511), hl
  1247. ld bc, $0170
  1248. xor a
  1249. sbc hl, bc
  1250. jp c, LABEL_F41
  1251. ld a, $1E
  1252. ld ($D780), a
  1253. ld bc, $030C
  1254. -: call LABEL_107C
  1255. push bc
  1256. call WaitForInterrupt
  1257. call Engine_UpdateLevelState
  1258. pop bc
  1259. dec bc
  1260. ld a, b
  1261. or c
  1262. jr nz, -
  1263. ld a, $10
  1264. ld ($D502), a
  1265. call LABEL_49B0
  1266. ld bc, $0078
  1267. -: call LABEL_107C
  1268. push bc
  1269. call WaitForInterrupt
  1270. call Engine_UpdateLevelState
  1271. pop bc
  1272. dec bc
  1273. ld a, b
  1274. or c
  1275. jr nz, -
  1276. ld bc, $003C
  1277. -: call LABEL_107C
  1278. push bc
  1279. call WaitForInterrupt
  1280. call Engine_UpdateLevelState
  1281. pop bc
  1282. dec bc
  1283. ld a, b
  1284. or c
  1285. jr nz, -
  1286. xor a
  1287. ld ($D700), a
  1288. ret
  1289.  
  1290. _Load_Title_Level:
  1291. di
  1292. call Engine_ClearLevelAttributes
  1293. call ClearWorkingVRAM
  1294. ld a, $09
  1295. ld (CurrentLevel), a
  1296. ld a, $01
  1297. ld (CurrentAct), a
  1298. call LABEL_21AA
  1299. LABEL_FB9:
  1300. di
  1301. call ClearScreen
  1302. ld a, :Bank24 ;page in bank 24
  1303. call Engine_SwapFrame2
  1304. ld hl, $2000
  1305. call VDPWrite
  1306. ld hl, Art_Title_Screen ;title screen compressed art
  1307. xor a
  1308. call LoadTiles ;load the tiles into VRAM
  1309. ld a, $08 ;page in bank 08
  1310. call Engine_SwapFrame2
  1311. ld hl, $38CC ;destination
  1312. ld de, Mappings_Title ;source
  1313. ld bc, $1214 ;row/col count
  1314. call Engine_LoadCardMappings ;load the mappings into VRAM
  1315. ld a, $2C ;set the background scroll values
  1316. ld (BackgroundYScroll), a
  1317. ld a, $F8
  1318. ld (BackgroundXScroll), a
  1319. di
  1320. ld hl, $DB00 ;clear the working SAT
  1321. ld de, $DB01
  1322. ld bc, $00BF
  1323. ld (hl), $00
  1324. ldir
  1325. ld a, $00 ;clear the first sprite
  1326. ld ($D500), a
  1327. ld a, :Bank08 ;swap in bank 8
  1328. call Engine_SwapFrame2
  1329. ld hl, $0200
  1330. call VDPWrite
  1331. ld hl, Art_Title_Sonic_Hand ;load sonic's animated hand
  1332. xor a
  1333. call LoadTiles
  1334.  
  1335. ld hl, $05C0
  1336. call VDPWrite
  1337. ld hl, Art_Title_Tails_Face ;load tails' animated eye
  1338. xor a
  1339. call LoadTiles
  1340.  
  1341. ld c, $50 ;sprite number
  1342. ld h, $00
  1343. call LABEL_6144 ;set up the hand sprite
  1344.  
  1345. ld c, $51 ;sprite number
  1346. ld h, $00
  1347. call LABEL_6144 ;set up the eye sprite
  1348.  
  1349. call LoadLevelPalette
  1350. ld a, $40
  1351. ld (GameState), a
  1352. xor a
  1353. ld ($D12F), a
  1354. ld bc, $001E
  1355. push bc
  1356. call WaitForInterrupt
  1357. call Engine_UpdateLevelState
  1358. call TitleScreen_ChangePressStartText
  1359. pop bc
  1360. dec bc
  1361. ld a, b
  1362. or c
  1363. jr nz, $F0
  1364. ld bc, $04B0
  1365. call LABEL_107C
  1366. push bc
  1367. call WaitForInterrupt
  1368. call Engine_UpdateLevelState
  1369. call TitleScreen_ChangePressStartText
  1370. pop bc
  1371. dec bc
  1372. ld a, b
  1373. or c
  1374. jr nz, $ED
  1375. ret
  1376.  
  1377. TitleScreen_ChangePressStartText:
  1378. LABEL_1060:
  1379. ;page in the bank containing the mappings
  1380. ld a, :Bank08
  1381. call Engine_SwapFrame2
  1382. ld a, ($D12F)
  1383. ld de, Mappings_Title + $258 ;"Press Start Button" text mappings
  1384. and $20
  1385. jr z, $03 ;alternate between "Press Start Button" and blank row
  1386. ld de, Mappings_Title ;title screen mappings
  1387. ld hl, $3C8C ;vram destination
  1388. ld bc, $0114 ;rows/cols
  1389. ;load the mappings into VRAM
  1390. call Engine_LoadCardMappings
  1391. ret
  1392.  
  1393. LABEL_107C:
  1394. ld hl, $D292
  1395. bit 7, (hl)
  1396. ret z
  1397. pop af
  1398. ret
  1399.  
  1400. LABEL_1084:
  1401. Engine_ChangeLevelMusic:
  1402. ld a, (CurrentLevel)
  1403. ld b, a
  1404. add a, a
  1405. add a, b
  1406. ld b, a
  1407. ld a, (CurrentAct)
  1408. add a, b
  1409. ld l, a
  1410. ld h, $00
  1411. ld de, ZoneMusicTracks_Start
  1412. add hl, de
  1413. ld a, (hl)
  1414. ld (NextMusicTrack), a
  1415. ret
  1416.  
  1417. ZoneMusicTracks_Start:
  1418. ; |---- Act ----|
  1419. ; | 1 | 2 | 3 |
  1420. .db $82, $82, $82 ;Under Ground Zone
  1421. .db $86, $86, $86 ;Sky High Zone
  1422. .db $81, $81, $81 ;Aqua Lake Zone
  1423. .db $85, $85, $85 ;Green Hills Zone
  1424. .db $83, $83, $83 ;Gimmick Mountain Zone
  1425. .db $87, $87, $87 ;Scrambled Egg Zone
  1426. .db $84, $84 ;Crystal Egg Zone
  1427. .db $89 ;Boss/Crystal Egg Act 3
  1428. .db $91, $91, $91 ;End Credits
  1429.  
  1430. FadeMusic:
  1431. LABEL_10B3:
  1432. ld hl, $DD09
  1433. ld (hl), $0C
  1434. inc hl
  1435. ld (hl), $01
  1436. inc hl
  1437. ld (hl), $02
  1438. ret
  1439.  
  1440.  
  1441. ;loads the player sprite tiles into VRAM
  1442. Engine_LoadPlayerTiles: ;$10BF
  1443. ld a, ($D34E)
  1444. and $A0
  1445. cp $A0 ;check for bits 5 & 7
  1446. ret nz
  1447. ld a, ($D34F) ;which sprite?
  1448. or a
  1449. jp z, Engine_ClearPlayerTiles
  1450. ld l, a ;calculate offset (aligned to 4-byte)
  1451. ld h, $00
  1452. add hl, hl
  1453. add hl, hl
  1454. ld de, Data_PlayerSprites - $04
  1455. ld a, ($D34E)
  1456. bit 6, a ;if bit 6 is set the sprite is facing left
  1457. jr z, +
  1458. ld de, Data_PlayerSprites_Mirrored - $04
  1459. +: add hl, de
  1460. ld a, :Bank31
  1461. ld ($FFFF), a
  1462. ld a, (hl) ;bank number
  1463. inc hl
  1464. ld e, (hl) ;art pointer
  1465. inc hl
  1466. ld d, (hl)
  1467. inc hl
  1468. ld b, (hl) ;tile count / 2
  1469. ld ($FFFF), a
  1470. ;set vram address to $0
  1471. ld a, $00
  1472. out ($BF), a
  1473. ld a, $00
  1474. or $40
  1475. out ($BF), a
  1476. Engine_LoadPlayerTiles_CopyTiles: ;copy 2 tiles (64 bytes) to vram
  1477. ld a, (de)
  1478. out ($BE), a
  1479. inc de
  1480. nop
  1481. ld a, (de)
  1482. out ($BE), a
  1483. inc de
  1484. nop
  1485. ld a, (de)
  1486. out ($BE), a
  1487. inc de
  1488. nop
  1489. ld a, (de)
  1490. out ($BE), a
  1491. inc de
  1492. nop
  1493. ld a, (de)
  1494. out ($BE), a
  1495. inc de
  1496. nop
  1497. ld a, (de)
  1498. out ($BE), a
  1499. inc de
  1500. nop
  1501. ld a, (de)
  1502. out ($BE), a
  1503. inc de
  1504. nop
  1505. ld a, (de)
  1506. out ($BE), a
  1507. inc de
  1508. nop
  1509. ld a, (de)
  1510. out ($BE), a
  1511. inc de
  1512. nop
  1513. ld a, (de)
  1514. out ($BE), a
  1515. inc de
  1516. nop
  1517. ld a, (de)
  1518. out ($BE), a
  1519. inc de
  1520. nop
  1521. ld a, (de)
  1522. out ($BE), a
  1523. inc de
  1524. nop
  1525. ld a, (de)
  1526. out ($BE), a
  1527. inc de
  1528. nop
  1529. ld a, (de)
  1530. out ($BE), a
  1531. inc de
  1532. nop
  1533. ld a, (de)
  1534. out ($BE), a
  1535. inc de
  1536. nop
  1537. ld a, (de)
  1538. out ($BE), a
  1539. inc de
  1540. nop
  1541. ld a, (de)
  1542. out ($BE), a
  1543. inc de
  1544. nop
  1545. ld a, (de)
  1546. out ($BE), a
  1547. inc de
  1548. nop
  1549. ld a, (de)
  1550. out ($BE), a
  1551. inc de
  1552. nop
  1553. ld a, (de)
  1554. out ($BE), a
  1555. inc de
  1556. nop
  1557. ld a, (de)
  1558. out ($BE), a
  1559. inc de
  1560. nop
  1561. ld a, (de)
  1562. out ($BE), a
  1563. inc de
  1564. nop
  1565. ld a, (de)
  1566. out ($BE), a
  1567. inc de
  1568. nop
  1569. ld a, (de)
  1570. out ($BE), a
  1571. inc de
  1572. nop
  1573. ld a, (de)
  1574. out ($BE), a
  1575. inc de
  1576. nop
  1577. ld a, (de)
  1578. out ($BE), a
  1579. inc de
  1580. nop
  1581. ld a, (de)
  1582. out ($BE), a
  1583. inc de
  1584. nop
  1585. ld a, (de)
  1586. out ($BE), a
  1587. inc de
  1588. nop
  1589. ld a, (de)
  1590. out ($BE), a
  1591. inc de
  1592. nop
  1593. ld a, (de)
  1594. out ($BE), a
  1595. inc de
  1596. nop
  1597. ld a, (de)
  1598. out ($BE), a
  1599. inc de
  1600. nop
  1601. ld a, (de)
  1602. out ($BE), a
  1603. inc de
  1604. nop
  1605. ld a, (de)
  1606. out ($BE), a
  1607. inc de
  1608. nop
  1609. ld a, (de)
  1610. out ($BE), a
  1611. inc de
  1612. nop
  1613. ld a, (de)
  1614. out ($BE), a
  1615. inc de
  1616. nop
  1617. ld a, (de)
  1618. out ($BE), a
  1619. inc de
  1620. nop
  1621. ld a, (de)
  1622. out ($BE), a
  1623. inc de
  1624. nop
  1625. ld a, (de)
  1626. out ($BE), a
  1627. inc de
  1628. nop
  1629. ld a, (de)
  1630. out ($BE), a
  1631. inc de
  1632. nop
  1633. ld a, (de)
  1634. out ($BE), a
  1635. inc de
  1636. nop
  1637. ld a, (de)
  1638. out ($BE), a
  1639. inc de
  1640. nop
  1641. ld a, (de)
  1642. out ($BE), a
  1643. inc de
  1644. nop
  1645. ld a, (de)
  1646. out ($BE), a
  1647. inc de
  1648. nop
  1649. ld a, (de)
  1650. out ($BE), a
  1651. inc de
  1652. nop
  1653. ld a, (de)
  1654. out ($BE), a
  1655. inc de
  1656. nop
  1657. ld a, (de)
  1658. out ($BE), a
  1659. inc de
  1660. nop
  1661. ld a, (de)
  1662. out ($BE), a
  1663. inc de
  1664. nop
  1665. ld a, (de)
  1666. out ($BE), a
  1667. inc de
  1668. nop
  1669. ld a, (de)
  1670. out ($BE), a
  1671. inc de
  1672. nop
  1673. ld a, (de)
  1674. out ($BE), a
  1675. inc de
  1676. nop
  1677. ld a, (de)
  1678. out ($BE), a
  1679. inc de
  1680. nop
  1681. ld a, (de)
  1682. out ($BE), a
  1683. inc de
  1684. nop
  1685. ld a, (de)
  1686. out ($BE), a
  1687. inc de
  1688. nop
  1689. ld a, (de)
  1690. out ($BE), a
  1691. inc de
  1692. nop
  1693. ld a, (de)
  1694. out ($BE), a
  1695. inc de
  1696. nop
  1697. ld a, (de)
  1698. out ($BE), a
  1699. inc de
  1700. nop
  1701. ld a, (de)
  1702. out ($BE), a
  1703. inc de
  1704. nop
  1705. ld a, (de)
  1706. out ($BE), a
  1707. inc de
  1708. nop
  1709. ld a, (de)
  1710. out ($BE), a
  1711. inc de
  1712. nop
  1713. ld a, (de)
  1714. out ($BE), a
  1715. inc de
  1716. nop
  1717. ld a, (de)
  1718. out ($BE), a
  1719. inc de
  1720. nop
  1721. ld a, (de)
  1722. out ($BE), a
  1723. inc de
  1724. nop
  1725. ld a, (de)
  1726. out ($BE), a
  1727. inc de
  1728. nop
  1729. ld a, (de)
  1730. out ($BE), a
  1731. inc de
  1732. nop
  1733. dec b
  1734. jp nz, Engine_LoadPlayerTiles_CopyTiles
  1735. ld hl, $D34E
  1736. res 7, (hl)
  1737. ret
  1738.  
  1739. ;*****************************************
  1740. ;* Resets the tile patters in VRAM. *
  1741. ;*****************************************
  1742. Engine_ClearPlayerTiles:
  1743. ;set vram address to $0
  1744. ld a, $00
  1745. out ($BF), a
  1746. ld a, $00
  1747. or $40
  1748. out ($BF), a
  1749. xor a
  1750. ld b, $20
  1751. -: out ($BE), a
  1752. out ($BE), a
  1753. out ($BE), a
  1754. out ($BE), a
  1755. out ($BE), a
  1756. out ($BE), a
  1757. out ($BE), a
  1758. out ($BE), a
  1759. out ($BE), a
  1760. out ($BE), a
  1761. out ($BE), a
  1762. out ($BE), a
  1763. out ($BE), a
  1764. out ($BE), a
  1765. out ($BE), a
  1766. out ($BE), a
  1767. djnz -
  1768. ret
  1769.  
  1770.  
  1771. ;updates sprite art flags + frame index
  1772. Engine_UpdateSpriteAttribsArt: ;$1274
  1773. ld hl, $D34E
  1774. ld a, ($D504)
  1775. rlca
  1776. rlca
  1777. and $40
  1778. or $20
  1779. ld (hl), a
  1780. ld a, ($D350)
  1781. cp (hl)
  1782. jr z, +
  1783. ld a, (hl)
  1784. ld ($D350), a
  1785. set 7, (hl)
  1786. +: ld a, ($D34F)
  1787. ld b, a
  1788. ld a, ($D506)
  1789. cp b
  1790. ret z
  1791. ld ($D34F), a ;animation frame art to load
  1792. set 7, (hl) ;set the "sprite art update required" flag
  1793. ret
  1794.  
  1795.  
  1796. Engine_SwapFrame2:
  1797. LABEL_129C:
  1798. ld ($D12B), a
  1799. ld ($FFFF), a
  1800. ret
  1801.  
  1802. LABEL_12A3_4:
  1803. ld bc, $0A96
  1804. ;read the VDP status flags
  1805. in a, ($BF)
  1806. -: in a, ($BF) ;- 12 T-states FIXME: why read a second time? Timing issue?
  1807. and a ;- 4 T-states
  1808. ;wait for no pending frame interrupt
  1809. jp p, -
  1810. ;run a busy-loop
  1811. -: dec bc
  1812. ld a, c
  1813. or b
  1814. jp nz, -
  1815. in a, ($BF)
  1816. and a
  1817. ld a, $01
  1818. ;jump if pending frame interrupt
  1819. jp m, +
  1820. dec a
  1821. +: ld ($D12D), a
  1822. or a
  1823. ret nz
  1824. ld a, $80
  1825. ld ($DE91), a
  1826. ret
  1827.  
  1828. PaletteFadeOut:
  1829. LABEL_12C8:
  1830. ld hl, BgPaletteControl
  1831. ld (hl), $00
  1832. set 6, (hl) ;flag - background palette fade to black.
  1833. inc hl
  1834. inc hl
  1835. ld (hl), $00
  1836. set 6, (hl) ;flag - foreground palette fade to black.
  1837. ret
  1838.  
  1839. LABEL_12D6_79:
  1840. _WriteToPaletteRAM:
  1841. ;check the PaletteUpdatePending flag
  1842. ld a, (PaletteUpdatePending)
  1843. or a
  1844. ;don't update if the flag is 0
  1845. ret z
  1846. ld hl, ShadowedCRAM
  1847. ld de, $C000
  1848. ld bc, $0020
  1849. call CopyToVRAM
  1850. ;reset the PaletteUpdatePending flag
  1851. xor a
  1852. ld (PaletteUpdatePending), a
  1853. ret
  1854.  
  1855. Initialise_VDP_Registers:
  1856. ;read/clear VDP status flags
  1857. in a, ($BF)
  1858. ld b, $0B
  1859. ld c, $80
  1860. ld de, VDPRegister0
  1861. ld hl, Initial_VDP_Register_Values
  1862. -: ld a, (hl)
  1863. out ($BF), a
  1864. ld (de), a
  1865. ld a, c
  1866. out ($BF), a
  1867. inc c
  1868. inc de
  1869. inc hl
  1870. djnz -
  1871. ret
  1872.  
  1873. Initial_VDP_Register_Values:
  1874. .db $26, $82, $FF, $FF, $FF, $FF, $FB, $00, $00, $00, $FF
  1875.  
  1876.  
  1877. ;**********************************************
  1878. ;* Write to VDP Register - NOTE: seems to be unused (debug?)
  1879. ;* Write data B to register C and maintain a
  1880. ;* copy in RAM.
  1881. ;* b - The data.
  1882. ;* c - The register.
  1883. ;**********************************************
  1884. Write_VDP_Register:
  1885. LABEL_1310:
  1886. push bc
  1887. push hl
  1888. ;update the VDP Register
  1889. ld a, b
  1890. out ($BF), a
  1891. ld a, c
  1892. or $80
  1893. out ($BF), a
  1894. ;update the RAM copy
  1895. ld a, b
  1896. ld b, $00
  1897. ld hl, VDPRegister0
  1898. add hl, bc
  1899. ld (hl), a
  1900. pop hl
  1901. pop bc
  1902. ret
  1903.  
  1904. LABEL_1325:
  1905. in a, ($BF)
  1906. ret
  1907.  
  1908. ;**********************************************
  1909. ;* Set VRAM Pointer
  1910. ;* Set up the VDP to write to a specific address.
  1911. ;* hl - The VRAM address.
  1912. ;**********************************************
  1913. VDPWrite:
  1914. LABEL_1328:
  1915. ;setup to write to VRAM at location hl
  1916. push af
  1917. ld a, l
  1918. out ($BF), a
  1919. ld a, h
  1920. or $40
  1921. out ($BF), a
  1922. pop af
  1923. ret
  1924.  
  1925.  
  1926. ;**********************************************
  1927. ;* Prepare VRAM Read
  1928. ;* Sends a VRAM Read command to the VDP
  1929. ;* with the address stored in HL
  1930. ;* hl - The VRAM address.
  1931. ;**********************************************
  1932. VDPRead:
  1933. LABEL_1333:
  1934. ld a, l
  1935. out ($BF), a
  1936. ld a, h
  1937. and $3F
  1938. out ($BF), a
  1939. push af
  1940. pop af
  1941. ret
  1942.  
  1943.  
  1944. ;NOTE: Probably unused code
  1945. LABEL_133E:
  1946. push af
  1947. call VDPWrite
  1948. pop af
  1949. out ($BE), a
  1950. ret
  1951.  
  1952. ;NOTE: Probably unused code
  1953. LABEL_1346:
  1954. call VDPRead
  1955. in a, ($BE)
  1956. ret
  1957.  
  1958. ;NOTE: Probably unused code
  1959. LABEL_134C:
  1960. push de
  1961. push af
  1962. call VDPWrite
  1963. pop af
  1964. ld d, a
  1965. ld a, d
  1966. out ($BE), a
  1967. push af
  1968. pop af
  1969. in a, ($BE)
  1970. dec bc
  1971. ld a, b
  1972. or c
  1973. jr nz, $F4
  1974. pop de
  1975. ret
  1976.  
  1977. ;**********************************************
  1978. ;* WriteToVRAM
  1979. ;* Copy a single value to a block of VRAM.
  1980. ;* hl - The VRAM address to copy to.
  1981. ;* de - The vale to copy to VRAM.
  1982. ;* bc - Byte count.
  1983. ;**********************************************
  1984. LABEL_1361_13:
  1985. WriteToVRAM:
  1986. call VDPWrite
  1987. -: ld a, e ;write DE to VRAM
  1988. out ($BE), a
  1989. push af
  1990. pop af
  1991. ld a, d
  1992. out ($BE), a
  1993. dec bc
  1994. ld a, b
  1995. or c
  1996. jr nz, -
  1997. ret
  1998.  
  1999. ;**********************************************
  2000. ;* CopyToVRAM
  2001. ;* Copy a block of data from RAM to VRAM.
  2002. ;* hl - The source address
  2003. ;* de - The VRAM address
  2004. ;* bc - Byte count
  2005. ;**********************************************
  2006. LABEL_1372_80:
  2007. CopyToVRAM:
  2008. ex de, hl ;set the VRAM pointer
  2009. call VDPWrite
  2010. -: ld a, (de) ;copy from (de) to VRAM
  2011. out ($BE), a
  2012. inc de
  2013. dec bc
  2014. ld a, b
  2015. or c
  2016. jr nz, -
  2017. ret
  2018.  
  2019. LABEL_1381:
  2020. SetDisplayVisible:
  2021. ;set register VDP(1) - mode control register 2
  2022. ld hl, VDPRegister1
  2023. ld a, (hl)
  2024. ;change all flags - make sure display is visible
  2025. or $40
  2026. ld (hl), a
  2027. out ($BF), a
  2028. ld a, $81
  2029. out ($BF), a
  2030. ret
  2031.  
  2032. _VDP_Set_Register_1_flags_0_to_5:
  2033. ;set register VDP(1) - mode control register 2
  2034. ld hl, VDPRegister1
  2035. ld a, (hl)
  2036. ;only set flags 0 to 5
  2037. and $BF
  2038. ld (hl), a
  2039. out ($BF), a
  2040. ld a, $81
  2041. out ($BF), a
  2042. ret
  2043.  
  2044. EnableFrameInterrupt:
  2045. ld hl, VDPRegister1
  2046. ld a, (hl)
  2047. or $20
  2048. ld (hl), a
  2049. out ($BF), a
  2050. ld a, $81
  2051. out ($BF), a
  2052. ret
  2053.  
  2054.  
  2055. ;********************************************
  2056. ;* Set the value for VDP(1) but leave the *
  2057. ;* "Frame Interrupt" bit unchanged. *
  2058. ;********************************************
  2059. SetVDPRegister1_LeaveInt:
  2060. LABEL_13AA:
  2061. ld hl, VDPRegister1
  2062. ld a, (hl)
  2063. and $DF
  2064. ld (hl), a
  2065. out ($BF), a
  2066. ld a, $81
  2067. out ($BF), a
  2068. ret
  2069.  
  2070. ;**********************************************
  2071. ;* DrawText
  2072. ;* Used by Level Select routine to draw text
  2073. ;* to the screen.
  2074. ;* hl - The VRAM address.
  2075. ;* de - Pointer to chars
  2076. ;* bc - Char count
  2077. ;**********************************************
  2078. DrawText:
  2079. LABEL_13B8:
  2080. di
  2081. call VDPWrite
  2082. push de
  2083. push bc
  2084. -: ld a, (de)
  2085. out ($BE), a ;write a char to the VDP memory
  2086. ld a, ($D2C7) ;retrieve the tile attributes byte
  2087. nop
  2088. nop
  2089. nop
  2090. out ($BE), a ;write the attributes byte to the VDP
  2091. inc de ;increment pointer to next char
  2092. dec bc ;decrement counter
  2093. ld a, c
  2094. or b
  2095. jr nz, - ;jump if not zero
  2096. pop bc
  2097. pop de
  2098. ret
  2099.  
  2100. LABEL_13D2:
  2101. push de
  2102. push bc
  2103. ld a, ($D292)
  2104. bit 7, a
  2105. jr nz, +
  2106. di
  2107. call VDPWrite
  2108. ld a, (de)
  2109. out ($BE), a
  2110. ld a, ($D2C7)
  2111. nop
  2112. nop
  2113. nop
  2114. out ($BE), a
  2115. ei
  2116. push bc
  2117. push de
  2118. push hl
  2119. ld b, $06
  2120. -: ei
  2121. halt
  2122. ld a, ($D137)
  2123. and $80
  2124. jr nz, ++
  2125. djnz -
  2126. ++: pop hl
  2127. pop de
  2128. pop bc
  2129. inc hl
  2130. inc hl
  2131. inc de
  2132. dec bc
  2133. ld a, c
  2134. or b
  2135. jr nz, $CE
  2136. +: pop bc
  2137. pop de
  2138. ret
  2139.  
  2140.  
  2141. ;******************************************************************************
  2142. ;* Updates SAT with data from $DB00 and $DB40. $DB00 contains VPOS attribues *
  2143. ;* that get copied into VRAM at $3F00 -> $3F3F. $DB40 contains HPOS and char *
  2144. ;* codes that get copied into $3F7F -> $3FFF. *
  2145. ;******************************************************************************
  2146. UpdateSAT:
  2147. LABEL_1409:
  2148. ld hl, SATUpdateRequired ;check to see if SAT update is required
  2149. xor a
  2150. or (hl)
  2151. ret z ;don't bother updating if $D134 = 0
  2152. ld (hl), $00 ;reset the "update required" flag
  2153. ld a, ($D12F) ;if bit 0 is set change the way data is copied
  2154. rrca
  2155. jp c, UpdateSAT_Descending
  2156. ld a, $00 ;set VRAM pointer to $3F00 (SAT)
  2157. out ($BF), a
  2158. ld a, $3F
  2159. or $40
  2160. out ($BF), a
  2161. ld hl, $DB00 ;copy 64 VPOS bytes.
  2162. ld c, $BE
  2163. outi
  2164. outi
  2165. outi
  2166. outi
  2167. outi
  2168. outi
  2169. outi
  2170. outi
  2171. outi
  2172. outi
  2173. outi
  2174. outi
  2175. outi
  2176. outi
  2177. outi
  2178. outi
  2179. outi
  2180. outi
  2181. outi
  2182. outi
  2183. outi
  2184. outi
  2185. outi
  2186. outi
  2187. outi
  2188. outi
  2189. outi
  2190. outi
  2191. outi
  2192. outi
  2193. outi
  2194. outi
  2195. outi
  2196. outi
  2197. outi
  2198. outi
  2199. outi
  2200. outi
  2201. outi
  2202. outi
  2203. outi
  2204. outi
  2205. outi
  2206. outi
  2207. outi
  2208. outi
  2209. outi
  2210. outi
  2211. outi
  2212. outi
  2213. outi
  2214. outi
  2215. outi
  2216. outi
  2217. outi
  2218. outi
  2219. outi
  2220. outi
  2221. outi
  2222. outi
  2223. outi
  2224. outi
  2225. outi
  2226. outi
  2227. ld a, $80 ;set VRAM pointer to SAT + $80
  2228. out ($BF), a
  2229. ld a, $3F
  2230. or $40
  2231. out ($BF), a
  2232. ld hl, $DB40 ;copy HPOS and char code attributes to VRAM at $3F7F.
  2233. ld c, $BE
  2234. outi
  2235. outi
  2236. outi
  2237. outi
  2238. outi
  2239. outi
  2240. outi
  2241. outi
  2242. outi
  2243. outi
  2244. outi
  2245. outi
  2246. outi
  2247. outi
  2248. outi
  2249. outi
  2250. outi
  2251. outi
  2252. outi
  2253. outi
  2254. outi
  2255. outi
  2256. outi
  2257. outi
  2258. outi
  2259. outi
  2260. outi
  2261. outi
  2262. outi
  2263. outi
  2264. outi
  2265. outi
  2266. outi
  2267. outi
  2268. outi
  2269. outi
  2270. outi
  2271. outi
  2272. outi
  2273. outi
  2274. outi
  2275. outi
  2276. outi
  2277. outi
  2278. outi
  2279. outi
  2280. outi
  2281. outi
  2282. outi
  2283. outi
  2284. outi
  2285. outi
  2286. outi
  2287. outi
  2288. outi
  2289. outi
  2290. outi
  2291. outi
  2292. outi
  2293. outi
  2294. outi
  2295. outi
  2296. outi
  2297. outi
  2298. outi
  2299. outi
  2300. outi
  2301. outi
  2302. outi
  2303. outi
  2304. outi
  2305. outi
  2306. outi
  2307. outi
  2308. outi
  2309. outi
  2310. outi
  2311. outi
  2312. outi
  2313. outi
  2314. outi
  2315. outi
  2316. outi
  2317. outi
  2318. outi
  2319. outi
  2320. outi
  2321. outi
  2322. outi
  2323. outi
  2324. outi
  2325. outi
  2326. outi
  2327. outi
  2328. outi
  2329. outi
  2330. outi
  2331. outi
  2332. outi
  2333. outi
  2334. outi
  2335. outi
  2336. outi
  2337. outi
  2338. outi
  2339. outi
  2340. outi
  2341. outi
  2342. outi
  2343. outi
  2344. outi
  2345. outi
  2346. outi
  2347. outi
  2348. outi
  2349. outi
  2350. outi
  2351. outi
  2352. outi
  2353. outi
  2354. outi
  2355. outi
  2356. outi
  2357. outi
  2358. outi
  2359. outi
  2360. outi
  2361. outi
  2362. ret
  2363.  
  2364.  
  2365. ;******************************************************************************
  2366. ;* Updates SAT with data from $DB00 and $DB40. Player sprites written first. *
  2367. ;* The remaining sprites are written in descending order. *
  2368. ;******************************************************************************
  2369. UpdateSAT_Descending:
  2370. LABEL_15B7_76:
  2371. ld a, $00 ;set VRAM pointer to SAT
  2372. out ($BF), a
  2373. ld a, $3F
  2374. or $40
  2375. out ($BF), a
  2376. ld hl, $DB00 ;write 8 bytes from $DB00 to VRAM
  2377. ld c, $BE
  2378. outi
  2379. outi
  2380. outi
  2381. outi
  2382. outi
  2383. outi
  2384. outi
  2385. outi
  2386. ld hl, $DB3F ;write 56 bytes from $DB3F (decremented) to VRAM
  2387. ld c, $BE
  2388. outd
  2389. outd
  2390. outd
  2391. outd
  2392. outd
  2393. outd
  2394. outd
  2395. outd
  2396. outd
  2397. outd
  2398. outd
  2399. outd
  2400. outd
  2401. outd
  2402. outd
  2403. outd
  2404. outd
  2405. outd
  2406. outd
  2407. outd
  2408. outd
  2409. outd
  2410. outd
  2411. outd
  2412. outd
  2413. outd
  2414. outd
  2415. outd
  2416. outd
  2417. outd
  2418. outd
  2419. outd
  2420. outd
  2421. outd
  2422. outd
  2423. outd
  2424. outd
  2425. outd
  2426. outd
  2427. outd
  2428. outd
  2429. outd
  2430. outd
  2431. outd
  2432. outd
  2433. outd
  2434. outd
  2435. outd
  2436. outd
  2437. outd
  2438. outd
  2439. outd
  2440. outd
  2441. outd
  2442. outd
  2443. outd
  2444. ld a, $80 ;set VRAM pointer to SAT + $80
  2445. out ($BF), a
  2446. ld a, $3F
  2447. or $40
  2448. out ($BF), a
  2449. ld hl, $DB40 ;write 16 bytes from $DB40 to SAT + $80
  2450. ld c, $BE
  2451. outi
  2452. outi
  2453. outi
  2454. outi
  2455. outi
  2456. outi
  2457. outi
  2458. outi
  2459. outi
  2460. outi
  2461. outi
  2462. outi
  2463. outi
  2464. outi
  2465. outi
  2466. outi
  2467. ld hl, $DBBE ;write $38 2-byte words descending from $DBBE
  2468. ld de, $FFFC ; -4
  2469. ld c, $BE
  2470. outi
  2471. outi
  2472. add hl, de
  2473. outi
  2474. outi
  2475. add hl, de
  2476. outi
  2477. outi
  2478. add hl, de
  2479. outi
  2480. outi
  2481. add hl, de
  2482. outi
  2483. outi
  2484. add hl, de
  2485. outi
  2486. outi
  2487. add hl, de
  2488. outi
  2489. outi
  2490. add hl, de
  2491. outi
  2492. outi
  2493. add hl, de
  2494. outi
  2495. outi
  2496. add hl, de
  2497. outi
  2498. outi
  2499. add hl, de
  2500. outi
  2501. outi
  2502. add hl, de
  2503. outi
  2504. outi
  2505. add hl, de
  2506. outi
  2507. outi
  2508. add hl, de
  2509. outi
  2510. outi
  2511. add hl, de
  2512. outi
  2513. outi
  2514. add hl, de
  2515. outi
  2516. outi
  2517. add hl, de
  2518. outi
  2519. outi
  2520. add hl, de
  2521. outi
  2522. outi
  2523. add hl, de
  2524. outi
  2525. outi
  2526. add hl, de
  2527. outi
  2528. outi
  2529. add hl, de
  2530. outi
  2531. outi
  2532. add hl, de
  2533. outi
  2534. outi
  2535. add hl, de
  2536. outi
  2537. outi
  2538. add hl, de
  2539. outi
  2540. outi
  2541. add hl, de
  2542. outi
  2543. outi
  2544. add hl, de
  2545. outi
  2546. outi
  2547. add hl, de
  2548. outi
  2549. outi
  2550. add hl, de
  2551. outi
  2552. outi
  2553. add hl, de
  2554. outi
  2555. outi
  2556. add hl, de
  2557. outi
  2558. outi
  2559. add hl, de
  2560. outi
  2561. outi
  2562. add hl, de
  2563. outi
  2564. outi
  2565. add hl, de
  2566. outi
  2567. outi
  2568. add hl, de
  2569. outi
  2570. outi
  2571. add hl, de
  2572. outi
  2573. outi
  2574. add hl, de
  2575. outi
  2576. outi
  2577. add hl, de
  2578. outi
  2579. outi
  2580. add hl, de
  2581. outi
  2582. outi
  2583. add hl, de
  2584. outi
  2585. outi
  2586. add hl, de
  2587. outi
  2588. outi
  2589. add hl, de
  2590. outi
  2591. outi
  2592. add hl, de
  2593. outi
  2594. outi
  2595. add hl, de
  2596. outi
  2597. outi
  2598. add hl, de
  2599. outi
  2600. outi
  2601. add hl, de
  2602. outi
  2603. outi
  2604. add hl, de
  2605. outi
  2606. outi
  2607. add hl, de
  2608. outi
  2609. outi
  2610. add hl, de
  2611. outi
  2612. outi
  2613. add hl, de
  2614. outi
  2615. outi
  2616. add hl, de
  2617. outi
  2618. outi
  2619. add hl, de
  2620. outi
  2621. outi
  2622. add hl, de
  2623. outi
  2624. outi
  2625. add hl, de
  2626. outi
  2627. outi
  2628. add hl, de
  2629. outi
  2630. outi
  2631. add hl, de
  2632. outi
  2633. outi
  2634. add hl, de
  2635. outi
  2636. outi
  2637. add hl, de
  2638. ret
  2639.  
  2640.  
  2641.  
  2642. ClearNameTable: ;179B
  2643. ei
  2644. halt
  2645. di
  2646. ld hl, ScreenMap ;address
  2647. ld bc, $0380 ;count
  2648. ld de, $0000 ;value
  2649. call WriteToVRAM
  2650. jr Engine_ClearSAT
  2651.  
  2652. ClearScreen: ;17AC
  2653. ei
  2654. halt
  2655. di
  2656. ld hl, $2000 ;clear the first level tile from VRAM (32-bytes starting at $2000)
  2657. ld bc, $0020
  2658. ld de, $0000
  2659. call WriteToVRAM
  2660. ld hl, ScreenMap ;set up all background tiles to point to the first "level tile"
  2661. ld bc, $0380
  2662. ld de, $0100
  2663. call WriteToVRAM
  2664.  
  2665. Engine_ClearSAT: ;17C7
  2666. ld hl, $DB00 ;fill $DB00->$DB3F with $F0, $DB40->$DBC0 with $00
  2667. ld de, $DB40 ;this will be copied into the SAT with the next update
  2668. xor a ;so that all sprites have VPOS=240
  2669. ld b, $40
  2670. -: ld (hl), $F0
  2671. inc hl
  2672. ld (de), a
  2673. inc de
  2674. ld (de), a
  2675. inc de
  2676. djnz -
  2677. ld a, $FF ;set flag for SAT update
  2678. ld (SATUpdateRequired), a
  2679. ret
  2680.  
  2681. Engine_UpdateSpriteAttribs: ;$17DF
  2682. ld a, (GameState)
  2683. bit 7, a
  2684. jp nz, LABEL_1937_190
  2685. ld ix, $D500 ;sprite info location
  2686. ld hl, $DB00
  2687. ld ($D369), hl ;store the SAT VPOS pointer
  2688. ld hl, $DB40
  2689. ld ($D36B), hl ;store the SAT HPOS pointer
  2690. ld b, $14
  2691. -: xor a
  2692. or (ix+1)
  2693. jr z, +
  2694. ld a, (ix+0)
  2695. dec a
  2696. cp $EF
  2697. jr nc, +
  2698. push bc
  2699. ld c, (ix+4)
  2700. bit 5, c ;sprite flashing
  2701. call nz, Engine_ToggleSpriteVisible
  2702. bit 7, c ;set = sprite not drawn
  2703. jr nz, ++
  2704. bit 6, c ;set = sprite not drawn
  2705. jr nz, ++
  2706. call Engine_GetSpriteScreenPos
  2707. call Engine_UpdateObjectVPOS
  2708. call Engine_UpdateObjectHPOS
  2709. ++: pop bc
  2710. +: ld de, $0040 ;move to next sprite
  2711. add ix, de
  2712. djnz -
  2713.  
  2714. ld hl, ($D369) ;update vpos attribs?
  2715. ld a, $32
  2716. sub l
  2717. jr c, +
  2718. inc a
  2719. ld c, a
  2720. ld b, $00
  2721. ld e, l
  2722. ld d, h
  2723. inc de
  2724. ld (hl), $E0 ;set remaining sprites VPOS = 224
  2725. ldir
  2726. +: ld a, $FF
  2727. ld (SATUpdateRequired), a
  2728. ret
  2729.  
  2730. ;********************************************************
  2731. ;* Updates the VPOS attributes for each of an object's *
  2732. ;* sprites. *
  2733. ;* *
  2734. ;* IX - Pointer to object descriptor. *
  2735. ;* ($D36B) - Pointer to the object's SAT VPOS attrib *
  2736. ;********************************************************
  2737. Engine_UpdateObjectVPOS: ;$1842
  2738. ld iy, ($D369) ;SAT VPOS
  2739. ld h, (ix+$2B)
  2740. ld l, (ix+$2A)
  2741. ld e, (hl)
  2742. inc hl
  2743. ld d, (hl)
  2744. ld l, (ix+$1C) ;object's vertical position on screen
  2745. ld h, (ix+$1D)
  2746. add hl, de
  2747. ld ($D393), hl
  2748. exx
  2749. ld d, (ix+$29) ;get a pointer to the sprite arrangement offsets
  2750. ld e, (ix+$28)
  2751. ld bc, ($D393)
  2752. exx
  2753. ld a, (ix+$05) ;sprite count
  2754. or a
  2755. ret z
  2756. ld b, a ;loop through each sprite
  2757. -: exx
  2758. ld a, (de) ;get the offset value for this sprite
  2759. ld l, a
  2760. inc de
  2761. ld a, (de)
  2762. ld h, a
  2763. inc de
  2764. add hl, bc ;adjust the vertical position for the sprite
  2765. ld (iy+0), l ;store in the working SAT
  2766. ld a, $40
  2767. add a, l
  2768. ld l, a
  2769. jr nc, +
  2770. inc h
  2771. +: ld a, h
  2772. or a
  2773. jr nz, +
  2774. ld a, l
  2775. cp $30
  2776. jr nc, ++
  2777. +: ld (iy+0), $E0 ;push the sprite to line 224
  2778. ++: inc de ;move to the next sprite offset value
  2779. inc de
  2780. inc iy ;move to the next VPOS in the working SAT
  2781. exx
  2782. djnz - ;update next sprite
  2783. ld ($D369), iy ;store current VPOS pointer
  2784. ret
  2785.  
  2786.  
  2787. ;********************************************************
  2788. ;* Updates the VPOS attributes for each of an object's *
  2789. ;* sprites. *
  2790. ;* *
  2791. ;* IX - Pointer to object descriptor. *
  2792. ;* ($D36B) - Pointer to the object's SAT HPOS attrib *
  2793. ;********************************************************
  2794. Engine_UpdateObjectHPOS: ;$1896
  2795. ld iy, ($D36B) ;SAT HPOS pointer
  2796. ld h, (ix+$2B)
  2797. ld l, (ix+$2A)
  2798. inc hl ;skip over vertical offset word
  2799. inc hl
  2800. ld e, (hl) ;get horizontal offset word
  2801. inc hl
  2802. ld d, (hl)
  2803. bit 4, (ix+4) ;which direction is the object facing?
  2804. jr z, + ;2's comp the offset if we need to.
  2805. dec de
  2806. ld a, e
  2807. cpl
  2808. ld e, a
  2809. ld a, d
  2810. cpl
  2811. ld d, a
  2812. +: inc hl
  2813. ld c, (hl) ;get the pointer to the object's char codes
  2814. inc hl
  2815. ld b, (hl)
  2816. ld ($D110), bc ;store the pointer for later
  2817. ld l, (ix+$1A) ;horiz. pos on screen
  2818. ld h, (ix+$1B)
  2819. add hl, de ;add the horiz. offset value to the object's position
  2820. ld ($D393), hl ;store the value for later
  2821. exx
  2822. ld d, (ix+$29) ;get the pointer to the sprite arrangement data
  2823. ld e, (ix+$28)
  2824. inc de ;skip over the VPOS offset value
  2825. inc de
  2826. ld a, (ix+$00)
  2827. dec a
  2828. jr z, +
  2829. bit 4, (ix+$04) ;adjust the pointer if the object is facing
  2830. jr z, + ;backwards
  2831. ld hl, $046C
  2832. add hl, de
  2833. ex de, hl
  2834. +: ld bc, ($D393) ;fetch the horizontal position value
  2835. exx
  2836. ld a, (ix+5) ;get the sprite count
  2837. or a
  2838. ret z
  2839. ld b, a ;loop thru each sprite
  2840. -: exx
  2841. ld a, (de) ;calculate the VPOS attribute
  2842. ld l, a
  2843. inc de
  2844. ld a, (de)
  2845. ld h, a
  2846. inc de
  2847. add hl, bc
  2848. ld (iy+0), l ;store the VPOS attribute
  2849. ld a, h
  2850. or a
  2851. jr z, +
  2852. ld (iy+0), $00 ;push the sprite off screen
  2853. +: inc de ;move to the next sprite offset value
  2854. inc de
  2855. inc iy ;move to the sprite's char code
  2856. ld hl, ($D110) ;get the pointer to the char codes
  2857. ld a, (hl)
  2858. bit 4, (ix+$04) ;which direction are we facing
  2859. jr z, + ;calculate the VRAM char code
  2860. add a, (ix+$09) ;add the VRAM tile offset for left-facing sprites
  2861. jr ++
  2862. +: add a, (ix+$08) ;add the VRAM tile offset for right-facing sprites
  2863. ++: ld (iy+0), a ;store the char code in the working SAT.
  2864. inc hl ;increment the char code pointer...
  2865. ld ($D110), hl ;...and store it for later
  2866. inc iy ;move to the next sprite's HPOS
  2867. exx
  2868. djnz - ;update the next sprite
  2869. ld ($D36B), iy ;store the current HPOS pointer
  2870. ret
  2871.  
  2872. Engine_ToggleSpriteVisible: ;$1923
  2873. inc (ix+$2E)
  2874. ld a, (ix+$2E)
  2875. rrca
  2876. rrca
  2877. jr c, + ;hide sprite when frame count % 2 == 0
  2878. res 7, (ix+4)
  2879. ret
  2880. +: set 7, (ix+4)
  2881. ret
  2882.  
  2883. LABEL_1937_190:
  2884. ld ix, $D500
  2885. ld hl, $DB00
  2886. ld ($D369), hl
  2887. ld hl, $DB40
  2888. ld ($D36B), hl
  2889. ld b, $10
  2890. -: xor a
  2891. or (ix+1)
  2892. jr z, ++
  2893. ld a, (ix+0)
  2894. dec a
  2895. cp $EF
  2896. jr nc, ++
  2897. push bc
  2898. ld c, (ix+4)
  2899. bit 5, c
  2900. call nz, Engine_ToggleSpriteVisible
  2901. bit 7, c
  2902. jr nz, +
  2903. bit 6, c
  2904. jr nz, +
  2905. call Engine_GetSpriteScreenPos
  2906. call Engine_UpdateObjectVPOS
  2907. call Engine_UpdateObjectHPOS
  2908. +: pop bc
  2909. ++: ld de, $0040
  2910. add ix, de
  2911. djnz -
  2912. ld a, $FF
  2913. ld (SATUpdateRequired), a
  2914. ret
  2915.  
  2916. ;******************************************
  2917. ;* Load Mappings
  2918. ;*
  2919. ;* HL - VRAM Address
  2920. ;* DE - Pointer to mappings
  2921. ;* B - Row Count
  2922. ;* C - Column Count
  2923. ;******************************************
  2924. Engine_LoadCardMappings: ;$197F
  2925. ;calculate vram address
  2926. call LABEL_19D7
  2927. Engine_LoadMappings ;$1982
  2928. push bc
  2929. push hl
  2930. ld b, c
  2931. call VDPWrite
  2932. -: ld a, (de)
  2933. out ($BE), a ;write tile 1
  2934. inc de
  2935. inc hl
  2936. ld a, (de)
  2937. nop
  2938. nop
  2939. out ($BE), a ;write attribute
  2940. inc de
  2941. inc hl
  2942. ld a, l
  2943. and $3F ;wrap at the 64th address
  2944. jp nz, +
  2945. push de
  2946. ld de, $0040
  2947. or a
  2948. sbc hl, de
  2949. call VDPWrite ;...and set VRAM pointer
  2950. pop de
  2951. +: djnz -
  2952. pop hl
  2953. ld bc, $0040
  2954. add hl, bc
  2955. ld a, h ;make sure we're not overwriting the SAT
  2956. cp $3F
  2957. jp nz, +
  2958. ld h, $38
  2959. +: pop bc ;move to the next row
  2960. djnz Engine_LoadMappings
  2961. ei
  2962. ret
  2963.  
  2964. LABEL_19B9: ;TODO: seems to be unused
  2965. ld a, l
  2966. and $3F
  2967. ret nz
  2968. push de
  2969. ld de, $0040
  2970. or a
  2971. sbc hl, de
  2972. call VDPWrite
  2973. pop de
  2974. ret
  2975.  
  2976. _Unknown_5: ;TODO: seems to be unused
  2977. ld a, h
  2978. cp $3F ;check for overflow from SAT
  2979. ret nz
  2980. ld h, $38
  2981. ret
  2982.  
  2983. _Unknown_6: ;TODO: seems to be unused
  2984. ld a, h
  2985. cp $38 ;check for overflow from screen map
  2986. ret nc
  2987. ld h, $3E
  2988. ret
  2989.  
  2990. LABEL_19D7:
  2991. push bc
  2992. ld a, l
  2993. and $C0
  2994. ld b, a
  2995. ld a, ($D174) ;horiz. offset in level
  2996. rrca
  2997. rrca
  2998. add a, l
  2999. and $3E
  3000. or b
  3001. ld l, a
  3002. push hl
  3003. ld a, ($D176) ;vert. offset in level
  3004. rrca
  3005. rrca
  3006. rrca
  3007. and $1F
  3008. rlca
  3009. ld c, a
  3010. ld b, 0
  3011. ld hl, DATA_59DB
  3012. add hl, bc
  3013. ld c, (hl)
  3014. inc hl
  3015. ld b, (hl)
  3016. pop hl
  3017. add hl, bc
  3018. ld a, h
  3019. cp $3F
  3020. jr c, 6
  3021. or a
  3022. ld bc, $0700
  3023. sbc hl, bc
  3024. pop bc
  3025. ret
  3026.  
  3027. Engine_UpdateMappingBlock:
  3028. LABEL_1A09:
  3029. di
  3030. call LABEL_1A13 ;calculate VRAM address
  3031. ld bc, $0404 ;load a mapping block
  3032. jp Engine_LoadMappings
  3033.  
  3034. LABEL_1A13:
  3035. ld hl, ($D358) ;collision vert. pos
  3036. ld a, l
  3037. and $E0 ;cap at line 224
  3038. ld l, a
  3039. srl h
  3040. rr l ;hl /= 2
  3041. srl h
  3042. rr l ;hl /= 2
  3043. ld bc, DATA_59DB
  3044. add hl, bc
  3045. ld c, (hl)
  3046. inc hl
  3047. ld b, (hl)
  3048. ld a, ($D356) ;collision horiz. pos
  3049. rrca
  3050. rrca
  3051. and $38
  3052. ld l, a
  3053. ld h, $38 ;offset into screen map
  3054. add hl, bc
  3055. ret
  3056.  
  3057. ReadInput: ;$1A35
  3058. ld hl, $D145
  3059. ld de, $D146
  3060. ld bc, $000F
  3061. lddr
  3062. ld hl, $D155
  3063. ld de, $D156
  3064. ld bc, $000F
  3065. lddr
  3066. call _Port1_Input
  3067. cpl
  3068. ld ($D137), a ;store joypad bitfield
  3069. ld a, ($D138)
  3070. xor $FF
  3071. ld b, a
  3072. ld a, ($D137) ;load joypad bitfield
  3073. and $BF ;using only joypad-1 bits
  3074. ld c, a
  3075. xor $FF
  3076. xor b
  3077. and c
  3078. ld ($D147), a
  3079. ld ($D157), a
  3080. ld a, ($D292) ;should cpu control sonic?
  3081. bit 3, a
  3082. call nz, MovePlayer ;should the cpu control the player?
  3083. in a, ($DD) ;is "reset" button pressed?
  3084. cpl
  3085. and $10
  3086. ret z
  3087. jp Start
  3088. ret
  3089.  
  3090. _Port1_Input: ;$1A7A
  3091. in a, ($DC)
  3092. or $C0
  3093. and $7F
  3094. ld b, a
  3095. ld c, $80
  3096. and $30 ;check buttons 1 & 2
  3097. jr nz, + ;jump if buttons not pressed
  3098. ld c, $00
  3099. +: ld a, c
  3100. or b
  3101. ret
  3102.  
  3103. ;*******************************************
  3104. ;* Used in the demo levels to move Sonic. *
  3105. ;*******************************************
  3106. MovePlayer:
  3107. LABEL_1A8C_85:
  3108. ld a, ($D2D8) ;page in the bank with the control sequences
  3109. ld ($FFFF), a
  3110. ld hl, (ControlByte) ;fetch the offset (offset for the current control sequence within the bank)
  3111. ld a, h ;bail out if offset = 0
  3112. or l
  3113. ret z
  3114. ld a, (hl) ;load the control byte
  3115. ld ($D137), a ;and store it for later
  3116. inc hl
  3117. ld a, (hl)
  3118. ld ($D147), a
  3119. inc hl
  3120. ld (ControlByte), hl ;fetch the next offset
  3121. ret
  3122.  
  3123.  
  3124. .include "src\tile_loading_routines.asm"
  3125.  
  3126.  
  3127. LABEL_1BDD:
  3128. ld d, $00
  3129. ld l, d
  3130. ld b, $08
  3131. -: add hl, hl
  3132. jr nc, +
  3133. add hl, de
  3134. +: djnz -
  3135. ret
  3136.  
  3137. LABEL_1BE9:
  3138. ld b, $10
  3139. xor a
  3140. -: add hl, hl
  3141. rla
  3142. cp e
  3143. jr c, +
  3144. sub e
  3145. set 0, l
  3146. +: djnz -
  3147. ret
  3148.  
  3149. LABEL_1BF7:
  3150. ld a, $10
  3151. -: sla e
  3152. rl d
  3153. adc hl, hl
  3154. jr c, +
  3155. sbc hl, bc
  3156. jr nc, ++
  3157. add hl, bc
  3158. dec a
  3159. jr nz, -
  3160. ret
  3161.  
  3162. +: or a
  3163. sbc hl, bc
  3164. ++: inc e
  3165. dec a
  3166. jr nz, -
  3167. ret
  3168.  
  3169. ScoreCard_UpdateScore:
  3170. LABEL_1C12:
  3171. -: ld a, ($D137) ;check for button press
  3172. and $30
  3173. jr nz, + ;if not pressing a button, wait for 1 frame
  3174. ei
  3175. halt
  3176.  
  3177. +: ld a, (RingCounter)
  3178. or a
  3179. jr z, +++
  3180. sub $01 ;decrement ring counter
  3181. daa
  3182. ld (RingCounter), a
  3183.  
  3184. ld hl, Score_BadnikValue
  3185. call LABEL_1CD0 ;update score value
  3186. call LABEL_1D4F ;update score graphics?
  3187. call LABEL_1D6F
  3188. ld a, ($D137) ;check for button press
  3189. and $30
  3190. jr z, +
  3191. ld a, ($D12F)
  3192. and $01
  3193. jr nz, ++
  3194. ld a, SFX_ScoreTick ;score 'tick' sound
  3195. ld (NextMusicTrack), a
  3196. jr ++
  3197. +: ld a, ($D12F)
  3198. and $03
  3199. jr nz, ++
  3200. ld a, SFX_ScoreTick ;score 'tick' sound
  3201. ld (NextMusicTrack), a
  3202. ++: jr -
  3203. +++:
  3204. ld b, $3C
  3205. -: ei
  3206. halt
  3207. djnz -
  3208. -: ld a, ($D137) ;check for button press
  3209. and $30
  3210. jr nz, +
  3211. ei
  3212. halt
  3213. +: xor a
  3214. ld de, $D2A2 ;score time value
  3215. ld hl, Score_BadnikValue
  3216. ld a, (de)
  3217. sbc a, (hl)
  3218. daa
  3219. ld (de), a
  3220. inc de
  3221. inc hl
  3222. ld a, (de)
  3223. sbc a, (hl)
  3224. daa
  3225. ld (de), a
  3226. ld hl, Score_BadnikValue
  3227. call LABEL_1CD0 ;update score value
  3228. call LABEL_1D60
  3229. call LABEL_1D6F
  3230. ld a, ($D137) ;check for button press
  3231. and $30
  3232. jr z, +
  3233. ld a, ($D12F)
  3234. and $01
  3235. jr nz, ++
  3236. ld a, SFX_ScoreTick ;score tick sound
  3237. ld (NextMusicTrack), a
  3238. jr ++
  3239. +: ld a, ($D12F)
  3240. and $03
  3241. jr nz, ++
  3242. ld a, SFX_ScoreTick ;score tick sound
  3243. ld (NextMusicTrack), a
  3244. ++: ld hl, $D2A2
  3245. ld a, (hl)
  3246. inc hl
  3247. or (hl)
  3248. jr nz, -
  3249. ld hl, $3C2A ;vram address
  3250. ld de, ScoreCard_Mappings_Blank ;mapping source
  3251. ld bc, $0202 ;rows,cols
  3252. call Engine_LoadCardMappings
  3253. ret
  3254.  
  3255. LABEL_1CB8:
  3256. ld hl, Score_BadnikValue
  3257. jp LABEL_1CD0
  3258.  
  3259. Score_AddBossValue: ;$1CBE
  3260. ld hl, Score_BossValue
  3261. jp LABEL_1CD0
  3262.  
  3263. LABEL_1CC4:
  3264. ld hl, DATA_1E76
  3265. jp LABEL_1CD0
  3266.  
  3267. LABEL_1CCA:
  3268. ld hl, DATA_1E94
  3269. jp LABEL_1CD0
  3270.  
  3271.  
  3272. ;Add the value pointed to by HL to the score
  3273. LABEL_1CD0:
  3274. ld a, ($D292)
  3275. or a
  3276. ret nz
  3277. xor a
  3278. ld de, Score
  3279. ld a, (de)
  3280. adc a, (hl)
  3281. daa
  3282. ld (de), a ;update first BCD byte
  3283. inc de
  3284. inc hl
  3285. ld a, (de)
  3286. adc a, (hl)
  3287. daa
  3288. ld (de), a ;update second BCD byte
  3289. inc de
  3290. inc hl
  3291. ld a, (de)
  3292. adc a, (hl)
  3293. daa
  3294. ld (de), a ;update third BCD byte
  3295.  
  3296. jr nc, + ;score overflow. cap at 999,990
  3297. ld hl, $D29F
  3298. ld (hl), $90
  3299. inc hl
  3300. ld (hl), $99
  3301. inc hl
  3302. ld (hl), $99
  3303. ld a, $02
  3304. jr ++
  3305. +: ld a, $01
  3306. ++: ld ($D2B5), a
  3307. call LABEL_1D05
  3308. jp LABEL_1D34
  3309.  
  3310. LABEL_1D05: ;BCD subtraction subroutine
  3311. xor a
  3312. ld de, $D29E
  3313. ld hl, $D2A1
  3314. ld a, (de)
  3315. sub (hl)
  3316. ret c
  3317. jr z, +
  3318. jr ++
  3319. +: dec hl
  3320. dec de
  3321. ld a, (de)
  3322. sub (hl)
  3323. ret c
  3324. jr z, +
  3325. jr ++
  3326. +: dec hl
  3327. dec de
  3328. ld a, (de)
  3329. sub (hl)
  3330. ret c
  3331. jr nc, ++
  3332. ++: ld hl, Score
  3333. ld de, $D29F
  3334. ld bc, $0003
  3335. ldir
  3336. ld a, $02
  3337. ld ($D2B5), a
  3338. ret
  3339.  
  3340. LABEL_1D34:
  3341. ld hl, $D2B6
  3342. ld a, (hl)
  3343. or a
  3344. ret nz
  3345. ld a, ($D29E)
  3346. cp $03
  3347. ret c
  3348. ld (hl), $01
  3349. ld hl, LifeCounter
  3350. inc (hl)
  3351. ld a, ($D293)
  3352. bit 4, a
  3353. ret nz
  3354. jp CapLifeCounterValue
  3355.  
  3356. LABEL_1D4F:
  3357. di
  3358. ld a, $01
  3359. ld ($D2B4), a
  3360. ld hl, RingCounter
  3361. ld de, $3BA4
  3362. call LABEL_1D7F
  3363. ei
  3364. ret
  3365.  
  3366. LABEL_1D60:
  3367. di
  3368. ld a, $02
  3369. ld ($D2B4), a
  3370. ld hl, $D2A2
  3371. ld de, $3C24
  3372. call LABEL_1D7F
  3373. LABEL_1D6F:
  3374. ld a, $03
  3375. ld ($D2B4), a
  3376. ld hl, Score
  3377. ld de, $3CA0
  3378. call LABEL_1D7F
  3379. ei
  3380. ret
  3381.  
  3382. LABEL_1D7F:
  3383. push de
  3384. push hl
  3385. ld de, $D2A8
  3386. ld bc, $0007
  3387. ldir
  3388. pop hl
  3389. call LABEL_1E37
  3390. pop hl
  3391. ld a, ($D2B4)
  3392. cp $02
  3393. jr c, +
  3394. jr z, ++
  3395. ld de, $D2B3
  3396. ld bc, $0605
  3397. jr LABEL_1DAF
  3398. ++: ld de, $D2B1
  3399. ld bc, $0403
  3400. jr LABEL_1DAF
  3401. +: ld de, $D2AF
  3402. ld bc, $0201
  3403. jr LABEL_1DAF ;FIXME: pointless
  3404.  
  3405.  
  3406. LABEL_1DAF:
  3407. ld ($D11C), hl
  3408. -: ld a, c
  3409. or a
  3410. jr z, +
  3411. dec c
  3412. ld a, (de)
  3413. cp $30
  3414. jr nz, +
  3415. ld a, $3A
  3416. jr ++
  3417. +: ld c, $00
  3418. ld a, (de)
  3419. ++: push bc
  3420. sub $30
  3421. add a, a
  3422. add a, a
  3423. push de
  3424. di
  3425. ld hl, ($D11C)
  3426. call VDPWrite
  3427. ld hl, ScoreCard_Mappings_Numbers
  3428. ld e, a
  3429. ld d, $00
  3430. add hl, de
  3431. ld a, (hl)
  3432. out ($BE), a
  3433. push af
  3434. pop af
  3435. inc hl
  3436. ld a, (hl)
  3437. out ($BE), a
  3438. push af
  3439. pop af
  3440. inc hl
  3441. push hl
  3442. ld hl, ($D11C)
  3443. inc hl
  3444. inc hl
  3445. ld ($D11C), hl
  3446. ld de, $3E
  3447. add hl, de
  3448. call VDPWrite
  3449. pop hl
  3450. ld a, (hl)
  3451. out ($BE), a
  3452. push af
  3453. pop af
  3454. inc hl
  3455. ld a, (hl)
  3456. out ($BE), a
  3457. push af
  3458. pop af
  3459. inc hl
  3460. ei
  3461. pop de
  3462. dec de
  3463. pop bc
  3464. djnz -
  3465. ret
  3466.  
  3467. ;mappings for scorecard digits 0-9
  3468. ScoreCard_Mappings_Numbers: ;$1E07
  3469. .db $4E, $11, $58, $11
  3470. .db $4F, $11, $59, $11
  3471. .db $50, $11, $5A, $11
  3472. .db $51, $11, $5B, $11
  3473. .db $52, $11, $5C, $11
  3474. .db $53, $11, $5B, $11
  3475. .db $54, $11, $48, $11
  3476. .db $55, $11, $5D, $11
  3477. .db $56, $11, $48, $11
  3478. .db $57, $11, $5B, $11
  3479.  
  3480. ;mappings for blank character
  3481. ScoreCard_Mappings_Blank: ;$1E2F
  3482. .db $70, $11
  3483. .db $70, $11
  3484. .db $70, $11
  3485. .db $70, $11
  3486.  
  3487.  
  3488. LABEL_1E37:
  3489. xor a
  3490. ld hl, $D2A8
  3491. ld de, $D2AE
  3492. ld a, ($D2B4)
  3493. ld c, a
  3494. ld b, a
  3495. cp $04
  3496. ccf
  3497. ret c
  3498. -: ld a, (hl)
  3499. and $0F
  3500. cp $0A
  3501. ccf
  3502. ret c
  3503. ld a, (hl)
  3504. and $F0
  3505. cp $A0
  3506. ccf
  3507. ret c
  3508. inc hl
  3509. djnz -
  3510. ld hl, $D2A8
  3511. xor a
  3512. ld b, c
  3513. -: rrd
  3514. or $30
  3515. ld (de), a
  3516. inc de
  3517. rrd
  3518. or $30
  3519. ld (de), a
  3520. inc de
  3521. inc hl
  3522. djnz -
  3523. ret
  3524.  
  3525. DATA_1E6D:
  3526. .db $00, $00, $00
  3527.  
  3528. Score_BadnikValue:
  3529. .db $01, $00, $00
  3530. Score_BossValue:
  3531. .db $50, $00, $00
  3532. DATA_1E76:
  3533. .db $00, $01, $00
  3534. DATA_1E79:
  3535. .db $10, $01, $00
  3536. DATA_1E7C:
  3537. .db $20, $01, $00
  3538. DATA_1E7F:
  3539. .db $30, $01, $00
  3540. DATA_1E82:
  3541. .db $40, $01, $00
  3542. DATA_1E85:
  3543. .db $50, $01, $00
  3544. DATA_1E88:
  3545. .db $60, $01, $00
  3546. DATA_1E8B:
  3547. .db $70, $01, $00
  3548. DATA_1E8E:
  3549. .db $80, $01, $00
  3550. DATA_1E91:
  3551. .db $90, $01, $00
  3552. DATA_1E94:
  3553. .db $00, $02, $00
  3554. DATA_1E97:
  3555. .db $10, $02, $00
  3556. DATA_1E9A:
  3557. .db $20, $02, $00
  3558. DATA_1E9D:
  3559. .db $30, $02, $00
  3560. DATA_1EA0:
  3561. .db $40, $02, $00
  3562. DATA_1EA3:
  3563. .db $50, $02, $00
  3564. DATA_1EA6:
  3565. .db $60, $02, $00
  3566. DATA_1EA9:
  3567. .db $70, $02, $00
  3568. DATA_1EAC:
  3569. .db $80, $02, $00
  3570. DATA_1EAF:
  3571. .db $90, $02, $00
  3572. DATA_1EB2:
  3573. .db $00, $03, $00
  3574. DATA_1EB5:
  3575. .db $10, $03, $00
  3576. DATA_1EB8:
  3577. .db $20, $03, $00
  3578. DATA_1EBB:
  3579. .db $30, $03, $00
  3580. DATA_1EBE:
  3581. .db $40, $03, $00
  3582. DATA_1EC1:
  3583. .db $50, $03, $00
  3584. DATA_1EC4:
  3585. .db $60, $03, $00
  3586. DATA_1EC7:
  3587. .db $70, $03, $00
  3588. DATA_1ECA:
  3589. .db $80, $03, $00
  3590. DATA_1ECD:
  3591. .db $90, $03, $00
  3592. DATA_1ED0:
  3593. .db $00, $05, $00
  3594. DATA_1ED3:
  3595. .db $00, $06, $00
  3596. DATA_1ED6:
  3597. .db $00, $07, $00
  3598. DATA_1ED9:
  3599. .db $00, $08, $00
  3600. DATA_1EDC:
  3601. .db $00, $09, $00
  3602. DATA_1EDF:
  3603. .db $00, $10, $00
  3604. DATA_1EE2:
  3605. .db $00, $15, $00
  3606. DATA_1EE5:
  3607. .db $00, $20, $00
  3608. DATA_1EE8:
  3609. .db $00, $25, $00
  3610. DATA_1EEB:
  3611. .db $00, $30, $00
  3612.  
  3613. LABEL_1EEE:
  3614. ld a, ($D2BA)
  3615. or a
  3616. jr nz, LABEL_1F0E
  3617. ld a, ($D2B9)
  3618. cp $20
  3619. jr nc, ++
  3620. ld a, $20
  3621. ++: sub $20
  3622. add a, a
  3623. ld l, a
  3624. ld h, $00
  3625. ld de, DATA_1F70
  3626. add hl, de
  3627. ld e, (hl)
  3628. inc hl
  3629. ld d, (hl)
  3630. ex de, hl
  3631. jp LABEL_2079
  3632.  
  3633. LABEL_1F0E:
  3634. +: xor a
  3635. ld ($D2A5), a
  3636. ld ($D2A6), a
  3637. ld ($D2A7), a
  3638. ld hl, (ZoneTimer) ;get the current zone time
  3639. ld de, $0959 ;subtract 9mins 59sec
  3640. xor a
  3641. sbc hl, de
  3642. jr z, +
  3643. ld hl, ($D2B9)
  3644. srl h
  3645. rr l
  3646. srl h
  3647. rr l
  3648. srl h
  3649. rr l
  3650. srl h
  3651. rr l
  3652. ld a, l
  3653. sub $10
  3654. jr nc, ++
  3655. xor a
  3656. ++: ld l, a
  3657. ld h, $00
  3658. ld de, DATA_1FE4
  3659. add hl, de
  3660. ld a, (hl)
  3661. ld ($D2A5), a
  3662. ld a, ($D2B9)
  3663. and $0F
  3664. cp $05
  3665. jr c, ++
  3666. ld a, ($D2A5)
  3667. cp $01
  3668. jr z, ++
  3669. sub $01
  3670. daa
  3671. ld ($D2A5), a
  3672. ++: xor a
  3673. ld ($D2A6), a
  3674. ld ($D2A7), a
  3675. ld hl, $D2A5
  3676. jp LABEL_2079
  3677.  
  3678. +: ld hl, DATA_1ED0
  3679. jp LABEL_2079
  3680.  
  3681.  
  3682. DATA_1F70:
  3683. .dw DATA_1EEB
  3684. .dw DATA_1EE8
  3685. .dw DATA_1EE5
  3686. .dw DATA_1EE2
  3687. .dw DATA_1EDF
  3688. .dw DATA_1EDC
  3689. .dw DATA_1ED9
  3690. .dw DATA_1ED6
  3691. .dw DATA_1ED3
  3692. .dw DATA_1ED0
  3693. .dw DATA_1E6D
  3694. .dw DATA_1E6D
  3695. .dw DATA_1E6D
  3696. .dw DATA_1E6D
  3697. .dw DATA_1E6D
  3698. .dw DATA_1E6D
  3699. .dw DATA_1ECD
  3700. .dw DATA_1ECA
  3701. .dw DATA_1EC7
  3702. .dw DATA_1EC4
  3703. .dw DATA_1EC1
  3704. .dw DATA_1EBE
  3705. .dw DATA_1EBB
  3706. .dw DATA_1EB8
  3707. .dw DATA_1EB5
  3708. .dw DATA_1EB2
  3709. .dw DATA_1E6D
  3710. .dw DATA_1E6D
  3711. .dw DATA_1E6D
  3712. .dw DATA_1E6D
  3713. .dw DATA_1E6D
  3714. .dw DATA_1E6D
  3715. .dw DATA_1EAF
  3716. .dw DATA_1EAC
  3717. .dw DATA_1EA9
  3718. .dw DATA_1EA6
  3719. .dw DATA_1EA3
  3720. .dw DATA_1EA0
  3721. .dw DATA_1E9D
  3722. .dw DATA_1E9A
  3723. .dw DATA_1E97
  3724. .dw DATA_1E94
  3725. .dw DATA_1E6D
  3726. .dw DATA_1E6D
  3727. .dw DATA_1E6D
  3728. .dw DATA_1E6D
  3729. .dw DATA_1E6D
  3730. .dw DATA_1E6D
  3731. .dw DATA_1E91
  3732. .dw DATA_1E8E
  3733. .dw DATA_1E8B
  3734. .dw DATA_1E88
  3735. .dw DATA_1E85
  3736. .dw DATA_1E82
  3737. .dw DATA_1E7F
  3738. .dw DATA_1E7C
  3739. .dw DATA_1E79
  3740. .dw DATA_1E76
  3741.  
  3742.  
  3743. DATA_1FE4:
  3744. .incbin "unknown\s2_1FE4.bin"
  3745.  
  3746. ;Used by the score card
  3747. ;add the value pointed to by HL to the score at $D2A2
  3748. Engine_Score_AddValue:
  3749. LABEL_2079:
  3750. ld a, ($D292)
  3751. or a
  3752. ret nz
  3753.  
  3754. xor a
  3755. ld de, $D2A2
  3756. ld a, (de)
  3757. adc a, (hl)
  3758. daa
  3759. ld (de), a
  3760.  
  3761. inc de
  3762. inc hl
  3763. ld a, (de)
  3764. adc a, (hl)
  3765. daa
  3766. ld (de), a
  3767.  
  3768. inc de
  3769. inc hl
  3770. ld a, (de)
  3771. adc a, (hl)
  3772. daa
  3773. ld (de), a
  3774.  
  3775. ret nc
  3776.  
  3777. ld hl, $D2A2
  3778. ld (hl), $90
  3779. inc hl
  3780. ld (hl), $99
  3781. inc hl
  3782. ld (hl), $99
  3783. ld a, $02
  3784. ret
  3785.  
  3786. ;increment the zone timer clock
  3787. Engine_Timer_Increment: ;$20A1
  3788. ld a, ($D2B8) ;is timer update required?
  3789. or a
  3790. ret z
  3791. ld hl, $D2BC
  3792. inc (hl)
  3793. ld a, $3C ;number of frames to wait before incrementing the zone timer
  3794. sub (hl)
  3795. ret nz
  3796. ld (hl), a ;reset the frame counter
  3797. ld hl, ZoneTimer ;increment the zone timer
  3798. ld a, $01 ;increment seconds
  3799. add a, (hl)
  3800. daa
  3801. ld (hl), a
  3802. sub $60 ;increment minutes if required
  3803. jp nz, Engine_Timer_SetSprites
  3804. ld (hl), a ;reset seconds
  3805. inc hl
  3806. ld a, $01 ;increment minutes
  3807. add a, (hl)
  3808. daa
  3809. ld (hl), a ;is the timer at 10 minutes?
  3810. sub $10
  3811. jp nz, Engine_Timer_SetSprites
  3812. ld a, $FF ;timer at 10 minutes. lose a life.
  3813. ld ($D49F), a
  3814. xor a
  3815. ld ($D2B8), a ;reset "timer update required" flag.
  3816. ret
  3817.  
  3818. ;calculate the sprites to display for the zone timer
  3819. Engine_Timer_SetSprites: ;$20D2
  3820. ld a, ($D292)
  3821. or a
  3822. ret nz
  3823. ld a, (ZoneTimer+1) ;get the minutes
  3824. rlca
  3825. and $1E ;calculate the sprite to display for the current value
  3826. add a, $10
  3827. ld ($DBB9), a
  3828. ld a, (ZoneTimer) ;get the seconds
  3829. rrca ;calculate the sprite to display for the most significant digit
  3830. rrca
  3831. rrca
  3832. and $1E
  3833. add a, $10
  3834. ld ($DBBD), a
  3835. ld a, (ZoneTimer) ;calculate the sprite to display for the lest significant digit
  3836. rlca
  3837. and $1E
  3838. add a, $10
  3839. ld ($DBBF), a
  3840. ret
  3841.  
  3842. LABEL_20FB:
  3843. ld a, (ix+$0B)
  3844. or a
  3845. ret z
  3846. ld d, $00
  3847. ld e, (ix+$0A)
  3848. ld hl, DATA_330
  3849. add hl, de
  3850. ld a, (hl)
  3851. and a
  3852. jp p, LABEL_211B
  3853. ld ($D100), a
  3854. ld a, $FF
  3855. ld ($D101), a
  3856. ld ($D106), a
  3857. jr +
  3858. LABEL_211B:
  3859. ld ($D100), a
  3860. xor a
  3861. ld ($D101), a
  3862. ld ($D106), a
  3863. +: ld hl, $0000
  3864. ld ($D104), hl
  3865. ld b, (ix+$0B)
  3866. -: ld hl, ($D104)
  3867. ld de, ($D100)
  3868. add hl, de
  3869. ld ($D104), hl
  3870. djnz -
  3871. push ix
  3872. pop hl
  3873. ld de, $0010
  3874. add hl, de
  3875. ld de, $D104
  3876. xor a
  3877. ld a, (de)
  3878. adc a, (hl)
  3879. ld (hl), a
  3880. inc de
  3881. inc hl
  3882. ld a, (de)
  3883. adc a, (hl)
  3884. ld (hl), a
  3885. inc de
  3886. inc hl
  3887. ld a, (de)
  3888. adc a, (hl)
  3889. ld (hl), a
  3890. ld a, (ix+$0a)
  3891. add a, $C0
  3892. ld e, a
  3893. ld d, $00
  3894. ld hl, DATA_330
  3895. add hl, de
  3896. ld a, (hl)
  3897. and a
  3898. jp p, LABEL_2171
  3899. ld ($D100), a
  3900. ld a, $FF
  3901. ld ($D101), a
  3902. ld ($D106), a
  3903. jr +
  3904. LABEL_2171:
  3905. ld ($D100), a
  3906. xor a
  3907. ld ($D101), a
  3908. ld ($D106), a
  3909. +: ld hl, $0000
  3910. ld ($D104), hl
  3911. ld b, (ix+$0B)
  3912. -: ld hl, ($D104)
  3913. ld de, ($D100)
  3914. add hl, de
  3915. ld ($D104), hl
  3916. djnz -
  3917. push ix
  3918. pop hl
  3919. ld de, $0013
  3920. add hl, de
  3921. ld de, $D104
  3922. xor a
  3923. ld a, (de)
  3924. adc a, (hl)
  3925. ld (hl), a
  3926. inc de
  3927. inc hl
  3928. ld a, (de)
  3929. adc a, (hl)
  3930. ld (hl), a
  3931. inc de
  3932. inc hl
  3933. ld a, (de)
  3934. adc a, (hl)
  3935. ld (hl), a
  3936. ret
  3937.  
  3938. LABEL_21AA: ;load level?
  3939. di
  3940. call ClearWorkingVRAM
  3941. call LoadLevelTiles
  3942. call Engine_InitStatusIcons
  3943. ld ix, $D15E
  3944. call Engine_ClearLevelAttributes ;clear level header
  3945. call LoadLevelHeader
  3946. call LoadLevel_LoadLayout
  3947. call LoadRingArtPointers
  3948. ld a, $01 ;set up the sonic sprite
  3949. ld ($D500), a
  3950. ld a, ($D2B7)
  3951. or a
  3952. jr nz, +
  3953. xor a
  3954. ld ($D2B9), a
  3955. ld ($D2BA), a
  3956. dec a
  3957. ld ($D2B8), a
  3958. ei
  3959. halt
  3960. ret
  3961.  
  3962. +: ei
  3963. halt
  3964. ret
  3965.  
  3966. ClearWorkingVRAM: ;21E0
  3967. ld hl, $D300 ;clear RAM from $D300 -> $DBBF
  3968. ld de, $D301
  3969. ld bc, $08BF
  3970. ld (hl), $00
  3971. ldir
  3972. ret
  3973.  
  3974. ;****************************************************
  3975. ;* Initialises the status icons' art and positions *
  3976. ;* on screen. Status icons occupy the last 12 *
  3977. ;* locations in the SAT so that they will be drawn *
  3978. ;* on top of everything else. *
  3979. ;****************************************************
  3980. Engine_InitStatusIcons: ;$21EE
  3981. ld a, ($D292)
  3982. or a
  3983. ret nz
  3984. di
  3985. call Engine_ClearSAT
  3986. ld hl, Engine_Data_StatusIconDefaults
  3987. ld ix, $DB34 ;VPOS for last 12 sprites in SAT
  3988. ld iy, $DBA8 ;HPOS/char for last 12 sprites
  3989. ld b, $0C
  3990. call Engine_SetStatusIconPositions
  3991. ld a, (CurrentLevel) ;check to see if we're on the last level
  3992. cp $06
  3993. jr nz, +
  3994. ld a, (CurrentAct) ;check to see if we're on the last act
  3995. cp $02
  3996. jr nz, +
  3997. ld a, $E0 ;we're on the last level
  3998. ld ($DB38), a ;SAT vpos attributes?
  3999. ld ($DB39), a
  4000. +: ei
  4001. ret
  4002.  
  4003. ;Initial positions of the status icons (lives/timer/rings)
  4004. ;This is for the top row of tiles only; the lower row is calculated
  4005. ;by taking subsequent tiles in VRAM and placing them directly below
  4006. ;the specified row.
  4007. Engine_Data_StatusIconDefaults: ;$221F
  4008. ; hpos
  4009. ; vpos | char
  4010. ; |----|----|----|
  4011. .db $AF, $28, $16 ;life icon position
  4012. .db $AF, $10, $28
  4013. .db $AF, $18, $2A
  4014. .db $AF, $20, $24
  4015. .db $00, $20, $10 ;ring icon position
  4016. .db $00, $28, $10
  4017. .db $00, $10, $3E
  4018. .db $00, $18, $40
  4019. .db $10, $10, $10 ;timer position
  4020. .db $10, $18, $26
  4021. .db $10, $20, $10
  4022. .db $10, $28, $10
  4023.  
  4024. ;****************************************************
  4025. ;* Copies the sprite defaults to the working copy *
  4026. ;* of the SAT held in RAM at $DB00. *
  4027. ;****************************************************
  4028. Engine_SetStatusIconPositions: ;$2243
  4029. ld a, (hl) ;load vpos
  4030. ld (ix+$00), a
  4031. inc hl
  4032. ld a, (hl) ;load hpos
  4033. ld (iy+$00), a
  4034. inc hl
  4035. ld a, (hl) ;load char
  4036. ld (iy+$01), a
  4037. inc hl
  4038. inc ix
  4039. inc iy
  4040. inc iy
  4041. djnz Engine_SetStatusIconPositions
  4042. ret
  4043.  
  4044. ;****************************************************
  4045. ;* Set the score/ring/life/etc. counters to their *
  4046. ;* default values. *
  4047. ;****************************************************
  4048. Engine_InitCounters: ;$225B
  4049. ld a, $03
  4050. ld (LifeCounter), a ;set the life count to 3
  4051. ld ($D297), a
  4052. xor a
  4053. ld (RingCounter), a ;reset the ring count
  4054. ld (Score), a ;reset the 3-byte Score
  4055. ld (Score+1), a
  4056. ld (Score+2), a
  4057. ld ($D2A2), a ;\
  4058. ld ($D2A3), a ; |
  4059. ld ($D2A4), a ; | used in score card tallying
  4060. ld ($D2A5), a ; |
  4061. ld ($D2A6), a ; |
  4062. ld ($D2A7), a ;/
  4063. ld ($D2C5), a
  4064. ld a, ($D294)
  4065. or a
  4066. ret nz
  4067. ld (CurrentLevel), a
  4068. ld (CurrentAct), a
  4069. ret
  4070.  
  4071. LoadRingArtPointers:
  4072. LABEL_2291:
  4073. xor a
  4074. ld (RingCounter), a
  4075. ld a, (CurrentLevel) ;get the pointer for the current level
  4076. add a, a
  4077. ld l, a
  4078. ld h, $00
  4079. ld de, RingArtPointers
  4080. add hl, de
  4081. ld e, (hl)
  4082. inc hl
  4083. ld d, (hl)
  4084.  
  4085. ld a, (CurrentAct) ;get the pointer for the current act
  4086. add a, a
  4087. ld l, a
  4088. ld h, $00
  4089. add hl, de
  4090. ld e, (hl)
  4091. inc hl
  4092. ld d, (hl)
  4093.  
  4094. ex de, hl
  4095. ld e, (hl)
  4096. inc hl
  4097. ld d, (hl)
  4098. inc hl
  4099. ld ($D2D4), de ;collision data pointer
  4100.  
  4101. ld e, (hl) ;Read Source address
  4102. inc hl
  4103. ld d, (hl)
  4104. inc hl
  4105. ld (RingArt_SrcAddress), de
  4106.  
  4107. ld e, (hl) ;Read VRAM destination
  4108. inc hl
  4109. ld d, (hl)
  4110. inc hl
  4111. ld (RingArt_DestAddress), de
  4112.  
  4113. ld a, (hl)
  4114. ld ($D4A6), a ;cycling palette index
  4115. inc hl
  4116.  
  4117. ld a, (hl)
  4118. ld ($D4AE), a
  4119. inc hl
  4120.  
  4121. ld a, (hl)
  4122. ld ($D4B6), a
  4123. inc hl
  4124.  
  4125. ld a, (hl)
  4126. ld ($D4BE), a
  4127. inc hl
  4128. ret
  4129.  
  4130. .include "src\ring_art_pointers.asm"
  4131.  
  4132.  
  4133. LABEL_2416:
  4134. ; 00000000
  4135. ; |||||||`-
  4136. ; ||||||`--
  4137. ; |||||`---
  4138. ; ||||`---- Load demo flag
  4139. ; |||`----- Load title screen flag
  4140. ; ||`------ Load intro flag
  4141. ; |`-------
  4142. ; `-------- Load level flag
  4143.  
  4144. ChangeGameMode:
  4145. ld a, ($D292)
  4146. rlca
  4147. jp c, LABEL_24BE_48 ;Load a level - jump if bit 0 is set
  4148. rlca
  4149. jr c, LABEL_242F_49
  4150. rlca
  4151. jp c, LABEL_2439_50 ;load the Intro level - jump if bit 2 is set
  4152. rlca
  4153. jp c, LABEL_2459_51 ;load title screen - jump if bit 3 is set
  4154. rlca
  4155. jp c, DemoSequence_PlayDemo ;load a demo - jump if bit 4 is set
  4156. jp ChangeGameMode
  4157.  
  4158. LABEL_242F_49:
  4159. ld hl, $D292
  4160. res 6, (hl) ;intro flag
  4161. set 5, (hl) ;title screen flag
  4162. jp ChangeGameMode
  4163.  
  4164. LABEL_2439_50:
  4165. call _Load_Intro_Level
  4166.  
  4167. LABEL_243C:
  4168. ld a, 0
  4169. ld ($D700), a
  4170. call PaletteFadeOut
  4171. call FadeMusic
  4172. ld b, $2A
  4173. -: ei
  4174. halt
  4175. djnz -
  4176. ld hl, $D292
  4177. res 5, (hl) ;clear load intro flag
  4178. res 7, (hl) ;clear load level flag
  4179. set 4, (hl) ;set load title screen flag
  4180. jp ChangeGameMode
  4181.  
  4182. LABEL_2459_51:
  4183. call _Load_Title_Level
  4184. call PaletteFadeOut ;fade the palette
  4185. ld b, $2A
  4186. -: ei
  4187. halt
  4188. djnz -
  4189. ld hl, $D292
  4190. res 4, (hl) ;clear load title screen flag
  4191. set 3, (hl) ;set load demo flag
  4192. jp ChangeGameMode
  4193.  
  4194. DemoSequence_PlayDemo: ;246F
  4195. di
  4196. call DemoSequence_ChangeLevel
  4197. ld a, $40 ;set the "Demo mode" flag
  4198. ld (GameState), a
  4199. call DemoSequence_LoadLevel
  4200.  
  4201. LABEL_247B:
  4202. ld hl, (NextControlByte)
  4203. ld (ControlByte), hl
  4204. ld bc, $05DC ;demo timer
  4205. call LABEL_2530
  4206. call LABEL_2576
  4207. ld hl, $D292
  4208. res 7, (hl)
  4209. ld hl, $0000
  4210. ld (ControlByte), hl
  4211. xor a
  4212. ld (GameState), a
  4213. ld (CurrentLevel), a
  4214. ld (CurrentAct), a
  4215. ld hl, $D292
  4216. bit 7, (hl)
  4217. jr z, +
  4218.  
  4219. ld hl, $D292
  4220. res 7, (hl)
  4221. res 3, (hl)
  4222. set 4, (hl)
  4223. jp ChangeGameMode
  4224.  
  4225. +: ld hl, $D292
  4226. res 7, (hl)
  4227. res 3, (hl)
  4228. set 6, (hl)
  4229. jp ChangeGameMode
  4230.  
  4231. LABEL_24BE_48:
  4232. ld a, ($D12C) ;check for level select cheat
  4233. cp $0D
  4234. jr nz, +
  4235. xor a
  4236. ld ($D294), a
  4237. call LevelSelectMenu ;run the level select
  4238. call PaletteFadeOut
  4239. ld b, $2A
  4240. -: ei
  4241. halt
  4242. djnz -
  4243. +: xor a
  4244. ld ($D292), a
  4245. ld (GameState), a
  4246. ret
  4247.  
  4248. DemoSequence_ChangeLevel:
  4249. ;LABEL_24DD_53:
  4250. ld a, (DemoNumber)
  4251. inc a
  4252. and $07
  4253. ld (DemoNumber), a
  4254. add a, a
  4255. add a, a
  4256. ld l, a
  4257. ld h, $00
  4258. ld de, LevelDemoHeaders
  4259. add hl, de
  4260. ld a, (hl)
  4261. ld (CurrentLevel), a ;which level is the demo for?
  4262. inc hl
  4263. ld a, (hl)
  4264. ld (DemoBank), a ;which bank is the control sequence in?
  4265. inc hl
  4266. ld e, (hl)
  4267. inc hl
  4268. ld d, (hl)
  4269. ld (NextControlByte), de ;where is the control sequence?
  4270. xor a
  4271. ld (CurrentAct), a ;default to the first act...
  4272. ld a, (CurrentLevel)
  4273. cp $01
  4274. ret nz
  4275. ld a, $01 ;...except for SHZ when we should use act 2
  4276. ld (CurrentAct), a
  4277. ret
  4278.  
  4279. LevelDemoHeaders:
  4280. ; Level
  4281. ; | Bank
  4282. ; | | Control sequence pointer
  4283. ; |----|----|--------|
  4284. ;.db $01, $1D, $00, $90 ;SHZ demo
  4285.  
  4286. ;SHZ demo
  4287. .db $01
  4288. .db :DemoControlSequence_SHZ
  4289. .dw DemoControlSequence_SHZ
  4290.  
  4291. ;GHZ demo
  4292. .db $03
  4293. .db :DemoControlSequence_GHZ
  4294. .dw DemoControlSequence_GHZ
  4295.  
  4296. ;ALZ demo
  4297. .db $02
  4298. .db :DemoControlSequence_ALZ
  4299. .dw DemoControlSequence_ALZ
  4300.  
  4301. ;SEZ demo
  4302. .db $05
  4303. .db :DemoControlSequence_SEZ
  4304. .dw DemoControlSequence_SEZ
  4305.  
  4306. ;SHZ demo
  4307. .db $01
  4308. .db :DemoControlSequence_SHZ
  4309. .dw DemoControlSequence_SHZ
  4310.  
  4311. ;GHZ demo
  4312. .db $03
  4313. .db :DemoControlSequence_GHZ
  4314. .dw DemoControlSequence_GHZ
  4315.  
  4316. ;ALZ demo
  4317. .db $02
  4318. .db :DemoControlSequence_ALZ
  4319. .dw DemoControlSequence_ALZ
  4320.  
  4321. ;SEZ demo
  4322. .db $05
  4323. .db :DemoControlSequence_SEZ
  4324. .dw DemoControlSequence_SEZ
  4325.  
  4326.  
  4327. LABEL_2530:
  4328. push bc
  4329. call WaitForInterrupt
  4330. call Engine_UpdateLevelState
  4331. pop bc
  4332. ld a, ($D292)
  4333. bit 7, a
  4334. ret nz
  4335. ld a, (GameState)
  4336. cp $40
  4337. ret nz
  4338. dec bc ;decrement the demo timer
  4339. ld a, b
  4340. or c
  4341. jr nz, LABEL_2530
  4342. ret
  4343.  
  4344. DemoSequence_LoadLevel: ;254A
  4345. di
  4346. ld hl, GameState
  4347. set 2, (hl) ;set "title card" flag
  4348. call ClearScreen
  4349. call TitleCard_LoadAndDraw
  4350. call ScrollingText_UpdateSprites
  4351. call PaletteFadeOut
  4352. ld b, $2A ;pause to load the level
  4353. -: ei
  4354. halt
  4355. djnz -
  4356. call LABEL_21AA ;load the level
  4357. call LoadLevelPalette
  4358. call LABEL_49AA ;unlocks camera
  4359. ld a, $20
  4360. ld (RingCounter), a ;start level with 32 rings
  4361. ld hl, GameState
  4362. res 2, (hl) ;reset "title card" flag
  4363. ret
  4364.  
  4365. LABEL_2576:
  4366. call WaitForInterrupt
  4367. call Engine_Engine_UpdateObjectState
  4368. ld a, ($D501)
  4369. cp PlayerState_LostLife
  4370. jr nz, +
  4371. ld a, ($D51D)
  4372. cp $02
  4373. jr nz, LABEL_2576
  4374. +: ld a, $00 ;stop music
  4375. ld ($DD04), a
  4376. call LABEL_7D32
  4377. call PaletteFadeOut
  4378. ld b, $3C
  4379. -: ei
  4380. halt
  4381. djnz -
  4382. ret
  4383.  
  4384.  
  4385. LABEL_259C:
  4386. ei
  4387. halt
  4388. push hl
  4389. pop hl
  4390. ld a, ($D292)
  4391. bit 7, a
  4392. ret nz
  4393. dec hl
  4394. ld a, h
  4395. or l
  4396. ret z
  4397. jr LABEL_259C ;$F0
  4398.  
  4399. CapLifeCounterValue:
  4400. LABEL_25AC:
  4401. ld a, ($D292)
  4402. or a
  4403. ret nz
  4404. ld a, (LifeCounter)
  4405. cp $09 ;cap the life display at 9
  4406. jr c, +
  4407. ld a, $09
  4408. +: rlca
  4409. and $1E
  4410. add a, $10
  4411. ld ($DBA9), a
  4412. ret
  4413.  
  4414. IncrementRingCounter:
  4415. LABEL_25C3:
  4416. ld a, (RingCounter)
  4417. add a, $01
  4418. daa
  4419. ld (RingCounter), a
  4420. ;check for 100 rings
  4421. or a
  4422. jr nz, LABEL_25DB
  4423. ;we have 100 rings; increment life counter
  4424. ld hl, LifeCounter
  4425. inc (hl)
  4426. call CapLifeCounterValue
  4427. ld a, SFX_ExtraLife
  4428. ld ($DD04), a
  4429. LABEL_25DB:
  4430. ld a, ($D292)
  4431. or a
  4432. ret nz
  4433. ld a, (CurrentLevel)
  4434. cp $06 ;check for crystal egg act 2
  4435. jr nz, +
  4436. ld a, (CurrentAct)
  4437. cp $02
  4438. ret z
  4439. +: ld a, (RingCounter)
  4440. rrca
  4441. rrca
  4442. rrca
  4443. and $1E
  4444. add a, $10
  4445. ld ($DBB1), a
  4446. ld a, (RingCounter)
  4447. rlca
  4448. and $1E
  4449. add a, $10
  4450. ld ($DBB3), a
  4451. ret
  4452.  
  4453.  
  4454. TitleCard_LoadAndDraw:
  4455. LABEL_2606:
  4456. di
  4457. call Engine_ClearLevelAttributes
  4458. ld de, $1170 ;copy $1170 to the name table
  4459. ld hl, ScreenMap
  4460. ld bc, $0380
  4461. call WriteToVRAM
  4462. call TitleCard_LoadTiles
  4463. ;FIXME: potential optimisations here.
  4464. ld a, (GameState)
  4465. bit 2, a
  4466. jr z, +
  4467. ld a, Music_TitleCard ;play title card music
  4468. ld ($DD04), a
  4469. +: ei
  4470. ld hl, BgPaletteControl ;trigger bg palette fade to colour.
  4471. ld (hl), $00
  4472. set 7, (hl)
  4473. inc hl
  4474. ld (hl), $03
  4475. ld a, (CurrentLevel)
  4476. add a, $1D
  4477. ld hl, FgPaletteControl ;trigger fg palette fade to colour.
  4478. ld (hl), $00
  4479. set 7, (hl)
  4480. inc hl
  4481. ld (hl), a
  4482. ld b, $0C
  4483. -: ei
  4484. halt
  4485. djnz -
  4486. call TitleCard_LoadText
  4487. call TitleCard_LoadActLogoMappings
  4488. call TitleCard_LoadZoneText
  4489. ret
  4490.  
  4491. ;****************************************
  4492. ;* Loads the mappings for the zone name *
  4493. ;* into VRAM *
  4494. ;****************************************
  4495. TitleCard_LoadText:
  4496. LABEL_264E:
  4497. ld hl, $0038
  4498. ld ($D110), hl
  4499. ld bc, $0101 ;load 1 row + 1 col to start with
  4500. ld ($D118), bc
  4501. ld a, (CurrentLevel)
  4502. add a, a
  4503. ld l, a
  4504. ld h, $00
  4505. ld de, TitleCard_Mappings ;pointers to title card mappings
  4506. add hl, de
  4507. ld e, (hl)
  4508. inc hl
  4509. ld d, (hl)
  4510. ld hl, $0038
  4511. add hl, de
  4512. ex de, hl
  4513. ld a, (GameState)
  4514. bit 2, a
  4515. jr nz, +
  4516. ld de, DATA_2D0C
  4517. +: ld ($D11A), de
  4518. ld hl, $3900
  4519. ld ($D11C), hl
  4520. ld b, $1C
  4521. call TitleCard_ScrollTextFromLeft
  4522. ret
  4523.  
  4524. ;****************************************
  4525. ;* Loads the mappings for the act logo *
  4526. ;* into VRAM *
  4527. ;****************************************
  4528. TitleCard_LoadActLogoMappings: ;$2688
  4529. ld hl, $001C
  4530. ld ($D110), hl
  4531. ld bc, $0101
  4532. ld ($D118), bc ;rows/cols
  4533. ld a, (CurrentAct) ;which logo do we need?
  4534. add a, a
  4535. ld l, a
  4536. ld h, $00
  4537. ld de, DATA_28FE ;calculate the offset to the pointer
  4538. add hl, de
  4539. ld e, (hl)
  4540. inc hl
  4541. ld d, (hl)
  4542. ld ($D11A), de ;source address
  4543. ld hl, $39FE ;VRAM destination
  4544. ld ($D11C), hl
  4545. ld b, $0E ;count
  4546. call TitleCard_ScrollActLogo
  4547. ret
  4548.  
  4549. ;****************************************
  4550. ;* Loads the mappings for the "Zone" *
  4551. ;* text into VRAM *
  4552. ;****************************************
  4553. TitleCard_LoadZoneText: ;$26B3
  4554. ld hl, $0028
  4555. ld ($D110), hl
  4556. ld bc, $0101
  4557. ld de, DATA_299C ;"Zone" text mappings
  4558. ld a, (GameState)
  4559. bit 5, a
  4560. jr nz, $04
  4561. bit 4, a
  4562. jr z, $03
  4563. ld de, DATA_2D6C
  4564. ld hl, $39C0
  4565. ld ($D118), bc ;rows/cols
  4566. ld ($D11A), de ;pointer to mappings
  4567. ld ($D11C), hl ;VRAM address
  4568. ld b, $14
  4569. call TitleCard_ScrollTextFromLeft
  4570. ret
  4571.  
  4572. TitleCard_ScrollTextFromLeft: ;$26E1
  4573. push bc
  4574. ei
  4575. halt
  4576. ld bc, ($D118) ;rows/cols
  4577. ld de, ($D11A) ;pointer to mappigns
  4578. ld hl, ($D11C) ;VRAM address
  4579. call Engine_LoadCardMappings
  4580. ld bc, ($D118)
  4581. ld de, ($D11A)
  4582. ld hl, ($D110)
  4583. add hl, de
  4584. ex de, hl
  4585. push de
  4586. ld hl, ($D11C)
  4587. ld de, $0040
  4588. add hl, de
  4589. pop de
  4590. call Engine_LoadCardMappings
  4591. ld bc, ($D118)
  4592. inc c
  4593. ld ($D118), bc
  4594. ld de, ($D11A)
  4595. dec de
  4596. dec de
  4597. ld ($D11A), de
  4598. pop bc
  4599. djnz TitleCard_ScrollTextFromLeft
  4600. ret
  4601.  
  4602. TitleCard_ScrollActLogo: ;$2722
  4603. push bc
  4604. ei
  4605. halt
  4606. ld bc, ($D118)
  4607. ld de, ($D11A)
  4608. ld hl, ($D11C)
  4609. call Engine_LoadCardMappings
  4610. ld bc, ($D118)
  4611. ld hl, ($D11A)
  4612. ld de, ($D110)
  4613. add hl, de
  4614. ex de, hl
  4615. push de
  4616. ld hl, ($D11C)
  4617. ld de, $0040
  4618. add hl, de
  4619. pop de
  4620. call Engine_LoadCardMappings
  4621. ld bc, ($D118)
  4622. ld hl, ($D11A)
  4623. ld de, ($D110)
  4624. add hl, de
  4625. add hl, de
  4626. ex de, hl
  4627. push de
  4628. ld hl, ($D11C)
  4629. ld de, $0080
  4630. add hl, de
  4631. pop de
  4632. call Engine_LoadCardMappings
  4633. ld bc, ($D118)
  4634. ld hl, ($D11A)
  4635. ld de, ($D110)
  4636. add hl, de
  4637. add hl, de
  4638. add hl, de
  4639. ex de, hl
  4640. push de
  4641. ld hl, ($D11C)
  4642. ld de, $00C0
  4643. add hl, de
  4644. pop de
  4645. call Engine_LoadCardMappings
  4646. ld bc, ($D118)
  4647. ld hl, ($D11A)
  4648. ld de, ($D110)
  4649. add hl, de
  4650. add hl, de
  4651. add hl, de
  4652. add hl, de
  4653. ex de, hl
  4654. push de
  4655. ld hl, ($D11C)
  4656. ld de, $0100
  4657. add hl, de
  4658. pop de
  4659. call Engine_LoadCardMappings
  4660. ld bc, ($D118)
  4661. inc c
  4662. ld ($D118), bc
  4663. ld hl, ($D11C)
  4664. dec hl
  4665. dec hl
  4666. ld ($D11C), hl
  4667. pop bc
  4668. dec b
  4669. jp nz, TitleCard_ScrollActLogo
  4670. ret
  4671.  
  4672. GameOverScreen_DrawScreen: ;27B4
  4673. di
  4674. call Engine_ClearLevelAttributes ;clear data from $D15E->$D290 (level header?)
  4675. ld de, $1107 ;clear the screen
  4676. ld hl, ScreenMap
  4677. ld bc, $0380
  4678. call WriteToVRAM
  4679. call GameOverScreen_LoadTiles ;load the "Game Over" text
  4680. ld a, :Bank15
  4681. call Engine_SwapFrame2 ;FIXME: this is probably unnecessary since the call to GameOverScreen_LoadTiles pages this in anyway
  4682. ld hl, $3AD0
  4683. ld de, GameOverScreen_Data_TextMappings
  4684. ld bc, $0212
  4685. call Engine_LoadCardMappings
  4686. ei
  4687. ld hl, BgPaletteControl ;fade bg palette to colour
  4688. ld (hl), $00
  4689. set 7, (hl)
  4690. inc hl
  4691. ld (hl), $03
  4692. ld hl, FgPaletteControl ;fade sprite palette to colour
  4693. ld (hl), $00
  4694. set 7, (hl)
  4695. inc hl
  4696. ld (hl), $1E
  4697. ret
  4698.  
  4699. ContinueScreen_DrawScreen: ;27EE
  4700. di
  4701. call Engine_ClearLevelAttributes ;clear data from $D15E->$D290 (level header?)
  4702. ld de, $010A ;clear the screen to blank tile $0A (offset from $2000 in VRAM)
  4703. ld hl, ScreenMap
  4704. ld bc, $0380
  4705. call WriteToVRAM
  4706. call ContinueScreen_LoadTiles
  4707. call ContinueScreen_LoadNumberTiles
  4708. ld a, :Bank15
  4709. call Engine_SwapFrame2
  4710. ld hl, $3A50
  4711. ld de, ContinueScreen_Data_TextMappings
  4712. ld bc, $0212
  4713. call Engine_LoadCardMappings
  4714. ei
  4715. ld hl, BgPaletteControl ;fade background palette to colour
  4716. ld (hl), $00
  4717. set 7, (hl)
  4718. inc hl
  4719. ld (hl), $03
  4720. ld hl, FgPaletteControl ;fade sprite palette to colour
  4721. ld (hl), $00
  4722. set 7, (hl)
  4723. inc hl
  4724. ld (hl), $1E
  4725. ret
  4726.  
  4727. ContinueScreen_LoadNumberMappings: ;282B
  4728. di
  4729. ld a, $0F
  4730. call Engine_SwapFrame2
  4731. ld a, (ContinueScreen_Count) ;calculate the offset to the mappings
  4732. add a, a
  4733. add a, a
  4734. add a, a
  4735. ld e, a
  4736. ld d, $00
  4737. ld hl, ContinueScreen_Data_NumberMappings
  4738. add hl, de
  4739. ex de, hl
  4740. ld hl, $3B5E ;load the mappings into VRAM
  4741. ld bc, $0202
  4742. call Engine_LoadCardMappings
  4743. ret
  4744.  
  4745.  
  4746. LABEL_2849: ;TODO: unused?
  4747. push bc
  4748. push de
  4749. push hl
  4750. ld bc, $0001
  4751. call CopyToVRAM
  4752. ei
  4753. halt
  4754. halt
  4755. pop hl
  4756. inc hl
  4757. pop de
  4758. inc de
  4759. pop bc
  4760. djnz LABEL_2849
  4761. ret
  4762.  
  4763. ScrollingText_UpdateSprites: ;285D
  4764. call ScrollingText_LoadSATValues
  4765. -: ld a, $FF ;flag for a SAT update
  4766. ld (SATUpdateRequired), a
  4767. call ScrollingText_UpdateWorkingSAT
  4768. call UpdateCyclingPalette_ScrollingText
  4769. ei
  4770. halt
  4771. ld a, ($D147) ;check for a buttons 1/2
  4772. and $30
  4773. ret nz
  4774. ld hl, ($D3BA) ;timer?
  4775. dec hl
  4776. ld ($D3BA), hl
  4777. ld a, h
  4778. or l ;is timer 0?
  4779. jp nz, -
  4780. ret
  4781.  
  4782. ScrollingText_LoadSATValues: ;2880
  4783. ld hl, $0078
  4784. ld ($D3BA), hl
  4785. ld hl, $DB00 ;working copy of SAT VPos attributes
  4786. ld b, $08 ;set vpos to $18 for 8 sprites
  4787. ld a, $18
  4788. -: ld (hl), a
  4789. inc hl
  4790. djnz -
  4791. ld b, $08 ;set vpos to $30 for 8 sprites
  4792. ld a, $30
  4793. -: ld (hl), a
  4794. inc hl
  4795. djnz -
  4796. ld hl, $DB40 ;working copy of SAT Hpos/char codes
  4797. ld de, ScrollingText_Data_CharCodes
  4798. ld b, $10 ;update 16 sprites
  4799. -: ld a, (de) ;copy char code to wokring copy of SAT
  4800. ld (hl), a
  4801. inc hl
  4802. ld a, $82 ;set HPOS = $82
  4803. dec b
  4804. bit 2, b
  4805. jr nz, +
  4806. ld a, $80 ;set HPOS = $80
  4807. +: inc b
  4808. ld (hl), a
  4809. inc hl ;next sprite
  4810. inc de ;next char code
  4811. djnz -
  4812. ret
  4813.  
  4814. ScrollingText_Data_CharCodes: ;$28B4
  4815. .db $D0, $20, $40, $D8, $40, $30, $20, $D8
  4816. .db $A0, $A8, $80, $70, $90, $A0, $60, $70
  4817.  
  4818.  
  4819. ScrollingText_UpdateWorkingSAT:
  4820. LABEL_28C4:
  4821. ld hl, $DB40 ;HPOS/char
  4822. ld de, $DB00 ;VPOS
  4823. ld b, $10 ;update 16 sprite entries
  4824. -: ld a, (hl) ;do we have a sprite here?
  4825. or a
  4826. call nz, ScrollingText_UpdateSprite
  4827. inc hl ;move to next HPOS/char
  4828. inc hl
  4829. inc de ;move to next VPOS
  4830. djnz -
  4831. ret
  4832.  
  4833. ScrollingText_UpdateSprite:
  4834. LABEL_28D7:
  4835. inc hl ;get the HPOS attribute
  4836. ld a, (hl)
  4837. dec hl
  4838. dec (hl)
  4839. cp $82 ;sprite char $82?
  4840. jr nz, +
  4841. dec (hl) ;move the sprite left.
  4842. +: ld a, (de) ;get the VPOS
  4843. ld c, $10
  4844. cp $18 ;vpos == $18?
  4845. jr z, +
  4846. ld c, $50
  4847. +: ld a, (hl)
  4848. cp c
  4849. ret nc
  4850. ld a, $D0 ;SAT terminator marker byte
  4851. ld (hl), a
  4852. ret
  4853.  
  4854.  
  4855.  
  4856. TitleCard_Mappings: ;28F0
  4857. .include "src\titlecard_mappings.asm"
  4858.  
  4859.  
  4860. Engine_Engine_UpdateObjectState:
  4861. LABEL_2FA8:
  4862. ld ix, $D500
  4863. ld a, ($D500)
  4864. or a
  4865. ret z ;check that there is a sprite in this slot
  4866. ld a, ($D49F)
  4867. or a
  4868. call nz, LABEL_3823 ;not zero = dead
  4869. ld a, :Bank31 ;page in bank 31
  4870. call Engine_SwapFrame2
  4871. call LABEL_603F
  4872. ld a, :Bank31
  4873. call Engine_SwapFrame2
  4874. call LABEL_6139
  4875. jp LABEL_47C9 ;check monitor collisions?
  4876.  
  4877.  
  4878. LABEL_2FCB:
  4879. ;check to see if we're on the intro screen
  4880. ld a, (CurrentLevel)
  4881. cp $09
  4882. jp z, +
  4883. push iy
  4884. ld (ix+$0A), $40
  4885. ld (ix+$0B), $00
  4886. ld (ix+$06), $10
  4887. ld (ix+$05), $08
  4888. ld hl, $0400
  4889. ld ($D36D), hl
  4890. call LABEL_48C4
  4891. res 4, (ix+$04)
  4892. pop iy
  4893. inc (ix+$02)
  4894. ret
  4895.  
  4896. +: push iy
  4897. ld (ix+$0A), $40
  4898. ld (ix+$0B), $00
  4899. ld (ix+$06), $10
  4900. ld (ix+$05), $04
  4901. pop iy
  4902. ld (ix+$02), $0f
  4903. ret
  4904.  
  4905. Player_SetState_Standing:
  4906. LABEL_3011:
  4907. res 0, (ix+$03)
  4908. res 1, (ix+$03)
  4909. ld (ix+$02), PlayerState_Standing
  4910. ld hl, $0000
  4911. ld (IdleTimer), hl
  4912. ld ($D516), hl
  4913. ld hl, $0400
  4914. ld ($D36D), hl
  4915. call LABEL_48C4
  4916. jp LABEL_38AD ;check to see if sonic is balancing on a ledge
  4917.  
  4918. Player_SetState_Walking:
  4919. LABEL_3032:
  4920. res 0, (ix+$03)
  4921. res 1, (ix+$03)
  4922. res 6, (ix+$03)
  4923. ld (ix+$02), PlayerState_Walking
  4924. ld hl, $0400
  4925. ld ($D36D), hl ;set maximum horizontal velocity
  4926. call LABEL_48C4
  4927. call CalculatePlayerDirection
  4928. ld a, $80
  4929. ld (CameraOffsetY), a
  4930. ret
  4931.  
  4932. Player_SetState_LookUp:
  4933. LABEL_3054:
  4934. ld hl, $0000
  4935. ld ($D516), hl
  4936. ld (ix+$02), PlayerState_LookUp
  4937. ret
  4938.  
  4939. Player_SetState_Crouch:
  4940. LABEL_305F:
  4941. ld hl, $0000
  4942. ld ($D516), hl
  4943. res 1, (ix+$03) ;lose rings on collision with enemy
  4944. res 6, (ix+$03)
  4945. ld (ix+$02), PlayerState_Crouch
  4946. ret
  4947.  
  4948. Player_SetState_Running:
  4949. LABEL_3072:
  4950. res 0, (ix+$03)
  4951. res 1, (ix+$03) ;lose rings on collision with enemy
  4952. ld (ix+$02), PlayerState_Running
  4953. ret
  4954.  
  4955. Player_SetState_SkidRight:
  4956. LABEL_307F:
  4957. bit 0, (ix+$03)
  4958. ret nz
  4959. ld a, SFX_Skid ;play "skid" sound
  4960. ld ($DD04), a
  4961. res 1, (ix+$03) ;lose rings on collision with enemy
  4962. ld (ix+$02), PlayerState_SkiddingRight
  4963. ret
  4964.  
  4965. Player_SetState_SkidLeft
  4966. LABEL_3092:
  4967. bit 0, (ix+$03)
  4968. ret nz
  4969. ld a, SFX_Skid ;play "skid" sound
  4970. ld ($DD04), a
  4971. res 1, (ix+$03) ;lose rings on collision with enemy
  4972. ld (ix+$02), PlayerState_SkiddingLeft
  4973. ret
  4974.  
  4975. Player_SetState_Roll:
  4976. LABEL_30A5:
  4977. ld a, (ix+$17)
  4978. or a
  4979. jp z, Player_SetState_Crouch
  4980. ld (ix+$02), PlayerState_Rolling
  4981. ld hl, $0600
  4982. ld ($D36D), hl
  4983. call LABEL_48C4
  4984. res 0, (ix+$03)
  4985. set 1, (ix+$03) ;set flag to destroy enemies on collision
  4986. ld a, SFX_Roll ;play "roll" sound
  4987. ld ($DD04), a
  4988. ret
  4989.  
  4990. Player_SetState_JumpFromRamp
  4991. LABEL_30C7:
  4992. ld (ix+$02), PlayerState_JumpFromRamp
  4993. set 0, (ix+$03)
  4994. set 1, (ix+$03)
  4995. res 1, (ix+$22)
  4996. ret
  4997.  
  4998. Player_SetState_VerticalSpring:
  4999. LABEL_30D8:
  5000. bit 7, (ix+$19)
  5001. ret nz
  5002. ld (ix+$02), PlayerState_VerticalSpring
  5003. ld (ix+$18), l ;set Y-axis velocity to HL
  5004. ld (ix+$19), h
  5005. set 0, (ix+$03)
  5006. res 1, (ix+$03)
  5007. res 2, (ix+$03)
  5008. res 1, (ix+$22) ;trigger the movement
  5009. ld a, SFX_Spring ;play "spring bounce" sound
  5010. ld ($dd04), a
  5011. ret
  5012.  
  5013. Player_SetState_DiagonalSpring:
  5014. LABEL_30FD:
  5015. bit 7, (ix+$19)
  5016. ret nz
  5017. ld (ix+$02), PlayerState_DiagonalSpring ;set sprite animation ($D502)
  5018. ld (ix+$18), l ;set Y-axis velocity to HL
  5019. ld (ix+$19), h
  5020. set 0, (ix+$03) ;flag movement on Y-axis
  5021. set 1, (ix+$03) ;flag movement on X-axis
  5022. res 1, (ix+$22) ;trigger the movement
  5023. ret
  5024.  
  5025. LABEL_3119: ; \;
  5026. ld (ix+$02), $09 ; |
  5027. ld (ix+$16), l ; |
  5028. ld (ix+$17), h ; |
  5029. ld ($d36d), hl ; |
  5030. res 0, (ix+$03) ; | Identical to subroutine below
  5031. set 1, (ix+$03) ; |
  5032. res 1, (ix+$22) ; |
  5033. ld a, $9c ; |
  5034. ld ($dd04), a ; |
  5035. ret ; /
  5036.  
  5037. Player_SetState_HorizontalSpring:
  5038. LABEL_3138:
  5039. ld (ix+$02), PlayerState_Rolling ;set sprite animation
  5040. ld (ix+$16), l ;set the X-axis speed to HL
  5041. ld (ix+$17), h
  5042. ld ($D36D), hl
  5043. res 0, (ix+$03) ;flag no movement on the Y-axis
  5044. set 1, (ix+$03) ;flag movement on X-axis
  5045. res 1, (ix+$22) ;trigger movement update
  5046. ld a, SFX_Spring ;play spring bounce sound
  5047. ld ($DD04), a
  5048. ret
  5049.  
  5050. Player_SetState_Falling: ;$3157
  5051. ld a, (ix+$01)
  5052. cp PlayerState_Jumping
  5053. ret z
  5054. ld (ix+$02), PlayerState_Falling
  5055. ld (ix+$18), $00 ;vertical velocity
  5056. ld (ix+$19), $01
  5057. set 0, (ix+$03) ;set player in air
  5058. res 1, (ix+$03) ;lose rings on collision with badnik
  5059. res 1, (ix+$22)
  5060. ret
  5061.  
  5062. LABEL_3176:
  5063. ld (ix+$02), $1d
  5064. ld (ix+$18), $80
  5065. ld (ix+$19), $00
  5066. set 0, (ix+$03)
  5067. res 1, (ix+$03)
  5068. res 1, (ix+$22)
  5069. ret
  5070.  
  5071. LABEL_318F:
  5072. ld (ix+$02), $1A
  5073. res 0, (ix+$03)
  5074. set 1, (ix+$22)
  5075. ld hl, $0400
  5076. ld ($D36D), hl
  5077. call LABEL_48C4
  5078. ld a, $80 ;reset camera offset
  5079. ld (CameraOffsetY), a
  5080. ret
  5081.  
  5082. CalculatePlayerDirection: ;31AA
  5083. ld a, ($D137)
  5084. rlca
  5085. rlca
  5086. and $30
  5087. ret z
  5088. and $10
  5089. ld b, a
  5090. ld a, (ix+$04)
  5091. and $EF
  5092. or b
  5093. ld (ix+$04), a
  5094. ret
  5095.  
  5096. ;******************************************************************
  5097. ;* Handle a button press when the player is standing (not idle). *
  5098. ;******************************************************************
  5099. Player_HandleStanding: ;$31BF
  5100. LABEL_31BF:
  5101. ld a, $80
  5102. ld (CameraOffsetY), a
  5103. call LABEL_3A62 ;Check for input?
  5104. ld a, ($D502)
  5105. cp PlayerState_Standing ;Compare to "standing" animation
  5106. ret nz
  5107. ld hl, (HorizontalVelocity) ;Check to see if we should display the
  5108. ld a, h ;walking sprite
  5109. or l
  5110. jp nz, Player_SetState_Walking
  5111. ld hl, $D137
  5112. ld a, (hl)
  5113. and $0C ;check for left & right buttons
  5114. jp nz, Player_SetState_Walking
  5115. bit 0, (hl) ;check for up button
  5116. jp nz, Player_SetState_LookUp
  5117. bit 1, (hl) ;check for down button
  5118. jp nz, Player_SetState_Crouch
  5119. ld hl, (IdleTimer) ;increment the wait timer
  5120. inc hl
  5121. ld (IdleTimer), hl
  5122. ld a, h
  5123. cp $02 ;check for wait time >= 512 frames
  5124. ret c
  5125. ld (ix+$02), PlayerState_Idle ;change sprite to the "wait" animation
  5126. ret
  5127.  
  5128. ;*******************************************************************
  5129. ;* Handle a button press when the player is balancing on a ledge. *
  5130. ;*******************************************************************
  5131. Player_HandleBalance:
  5132. LABEL_31F8:
  5133. ld a, $80 ;reset the camera
  5134. ld (CameraOffsetY), a
  5135. call LABEL_3A62
  5136. ld a, ($D502)
  5137. cp PlayerState_Balance
  5138. ret nz
  5139. ld hl, (HorizontalVelocity)
  5140. ld a, h
  5141. or l
  5142. jp nz, Player_SetState_Walking
  5143. ld hl, $D137
  5144. ld a, (hl)
  5145. and $0C ;check for left/right buttons
  5146. jp nz, Player_SetState_Walking
  5147. bit 0, (hl) ;check for up button
  5148. jp nz, Player_SetState_LookUp
  5149. bit 1, (hl) ;check for down button
  5150. jp nz, Player_SetState_Crouch
  5151. ret
  5152.  
  5153. ;************************************************************
  5154. ;* Handle a button press when the player is standing idle. *
  5155. ;************************************************************
  5156. Player_HandleIdle:
  5157. LABEL_3222:
  5158. call LABEL_3A62
  5159. ld a, ($D502)
  5160. cp PlayerState_Idle
  5161. ret nz
  5162. ld hl, $D137
  5163. ld a, (hl)
  5164. and $30 ;check for button 1/2
  5165. jp nz, Player_SetState_Jumping
  5166. ld a, (hl)
  5167. and $0C ;check for left/right
  5168. jp nz, Player_SetState_Walking
  5169. bit 0, (hl) ;check for up
  5170. jp nz, Player_SetState_LookUp
  5171. bit 1, (hl) ;check for down
  5172. jp nz, Player_SetState_Crouch
  5173. ret
  5174.  
  5175. ;*********************************************************
  5176. ;* Handle a button press when the player is looking up. *
  5177. ;*********************************************************
  5178. Player_HandleLookUp: ;$3245
  5179. LABEL_3245:
  5180. ld a, $B8
  5181. ld (CameraOffsetY), a
  5182. call LABEL_3A62
  5183. ld a, ($D502)
  5184. cp PlayerState_LookUp
  5185. ret nz
  5186. ld hl, $D137
  5187. ld a, (hl)
  5188. and $0C ;check for left/right buttons
  5189. jp nz, Player_SetState_Walking
  5190. bit 1, (hl)
  5191. jp nz, Player_SetState_Crouch
  5192. bit 0, (hl)
  5193. jp z, Player_SetState_Standing
  5194. ret
  5195.  
  5196. ;*******************************************************
  5197. ;* Handle a button press when the player is crouched. *
  5198. ;*******************************************************
  5199. Player_HandleCrouched:
  5200. LABEL_3267:
  5201. ld a, $30
  5202. ld (CameraOffsetY), a
  5203. call LABEL_3A62
  5204. ld a, ($D502)
  5205. cp PlayerState_Crouch
  5206. ret nz
  5207. ld hl, $D137
  5208. bit 0, (hl) ;check for up button
  5209. jp nz, Player_SetState_LookUp
  5210. bit 1, (hl) ;check for down button
  5211. jp z, Player_SetState_Standing
  5212. ret
  5213.  
  5214. ;******************************************************
  5215. ;* Handle a button press when the player is walking. *
  5216. ;******************************************************
  5217. Player_HandleWalk:
  5218. LABEL_3283:
  5219. ld a, $80 ;reset the camera offset when we start
  5220. ld (CameraOffsetY), a ;to move.
  5221. call LABEL_3A62
  5222. ld a, (ix+$02) ;$D502
  5223. cp PlayerState_Walking
  5224. ret nz
  5225. call CalculatePlayerDirection ;which direction are we facing?
  5226. call LABEL_3676 ;check for collisions with loops?
  5227. ld a, (ix+$17) ;increase horizontal velocity
  5228. inc a
  5229. cp $02
  5230. jr c, +
  5231. ld hl, $D137
  5232. bit 1, (hl) ;check for down button
  5233. jp nz, Player_SetState_Roll
  5234. +: ld a, (PlayerUnderwater)
  5235. or a
  5236. jr nz, +
  5237. ld hl, (HorizontalVelocity)
  5238. bit 7, h ;if HorizontalVelocity is negative we need to
  5239. jr z, ++ ;2's comp the value.
  5240. dec hl
  5241. ld a, h
  5242. cpl
  5243. ld h, a
  5244. ld a, l
  5245. cpl
  5246. ld l, a
  5247. ++: ld a, ($D36E)
  5248. cp h
  5249. jp z, Player_SetState_Running
  5250. +: call LABEL_32C8
  5251. jp LABEL_3321
  5252.  
  5253. LABEL_32C8:
  5254. ld a, ($D137)
  5255. and $0C ;check for left/right buttons
  5256. ret nz
  5257. ld hl, (HorizontalVelocity)
  5258. bit 7, h ;if the value in HL is negative we'll
  5259. jr z, + ;need to 2's comp the value.
  5260. dec hl
  5261. ld a, h
  5262. cpl
  5263. ld h, a
  5264. ld a, l
  5265. cpl
  5266. ld l, a
  5267. +: ld a, l
  5268. and $E0
  5269. or h
  5270. jp z, Player_SetState_Standing
  5271. ret
  5272.  
  5273. LABEL_32E4:
  5274. ld hl, $D137
  5275. ld a, (HorizontalVelocity+1)
  5276. bit 7, a ;check the direction we're moving in
  5277. jr nz, + ;jump if we're moving left
  5278. bit 2, (hl) ;check for left button and skid if pressed
  5279. jr z, ++
  5280. jp Player_SetState_SkidRight
  5281. +: bit 3, (hl) ;check for right button and skid if pressed
  5282. jr z, ++
  5283. jp Player_SetState_SkidLeft
  5284. ++: ld a, ($D363) ;set when player is on a slope
  5285. or a
  5286. jr nz, +
  5287. ld hl, (HorizontalVelocity)
  5288. bit 7, h ;check which direction we're moving
  5289. jr z, ++
  5290. dec hl ;2's comp. the value
  5291. ld a, h
  5292. cpl
  5293. ld h, a
  5294. ld a, l
  5295. cpl
  5296. ld l, a
  5297. ++: ld a, l
  5298. and $F0
  5299. or h
  5300. ret nz
  5301. ld a, ($D137) ;if we get here the player is standing still
  5302. bit 1, a ;check the down button
  5303. jp nz, Player_SetState_Crouch ;crouch...
  5304. jp Player_SetState_Standing ;...or stand
  5305. +: ret
  5306.  
  5307. LABEL_3321:
  5308. ld hl, $D137
  5309. ld a, (HorizontalVelocity+1)
  5310. or a ;check the hi-byte of the HorizontalVelocity
  5311. ret z ;word for 0.
  5312. inc a
  5313. ret z
  5314. bit 7, (ix+$17) ;check which direction we're moving in
  5315. jr nz, LABEL_3337 ;jump if travelling left
  5316. bit 2, (hl) ;travelling right - check left button
  5317. jp nz, Player_SetState_SkidRight
  5318. ret
  5319. LABEL_3337:
  5320. bit 3, (hl) ;travelling left - check right button
  5321. jp nz, Player_SetState_SkidLeft
  5322. ret
  5323.  
  5324. ;******************************************************
  5325. ;* Handle a button press when the player is running. *
  5326. ;******************************************************
  5327. Player_HandleRunning: ;$333D
  5328. LABEL_333D:
  5329. call LABEL_3A62
  5330. ld a, ($D502)
  5331. cp PlayerState_Running
  5332. ret nz
  5333. call LABEL_3676
  5334. ld hl, $D137
  5335. bit 1, (hl)
  5336. jp nz, Player_SetState_Roll
  5337. ld a, (HorizontalVelocity+1)
  5338. bit 7, a
  5339. jr z, $02
  5340. neg
  5341. cp $04
  5342. jp c, Player_SetState_Walking
  5343. jp LABEL_3321
  5344.  
  5345. ;********************************************************************
  5346. ;* Handle a button press when the player is skidding to the right. *
  5347. ;********************************************************************
  5348. Player_HandleSkidRight:
  5349. LABEL_3362:
  5350. call LABEL_3A62
  5351. ld a, ($D502)
  5352. cp PlayerState_SkiddingRight
  5353. ret nz
  5354. call LABEL_32C8
  5355. call CalculatePlayerDirection
  5356. ld a, (HorizontalVelocity+1)
  5357. and $80
  5358. ld b, a
  5359. ld a, ($D137)
  5360. rrca
  5361. rrca
  5362. rrca
  5363. rrca
  5364. and $80
  5365. xor b
  5366. ret z
  5367. jp Player_SetState_Walking
  5368.  
  5369. ;********************************************************************
  5370. ;* Handle a button press when the player is skidding to the left. *
  5371. ;********************************************************************
  5372. Player_HandleSkidLeft:
  5373. LABEL_3385:
  5374. call LABEL_3A62
  5375. ld a, ($D502)
  5376. cp PlayerState_SkiddingLeft
  5377. ret nz
  5378. call LABEL_32C8
  5379. call CalculatePlayerDirection
  5380. ld a, (HorizontalVelocity+1)
  5381. and $80
  5382. ld b, a
  5383. ld a, ($D137) ;
  5384. rlca
  5385. rlca
  5386. rlca
  5387. rlca
  5388. and $80
  5389. xor b
  5390. ret z
  5391. jp Player_SetState_Walking
  5392.  
  5393. Player_HandleRolling: ;$33A8
  5394. LABEL_33A8:
  5395. call LABEL_3A62
  5396. ld a, ($D502)
  5397. cp PlayerState_Rolling
  5398. ret nz
  5399. call LABEL_3676
  5400. jp LABEL_32E4
  5401.  
  5402. LABEL_33B7:
  5403. call LABEL_3A62
  5404. ld a, ($D502)
  5405. cp PlayerState_JumpFromRamp
  5406. ret nz
  5407. bit 1, (ix+$23)
  5408. ret z
  5409. res 0, (ix+$03)
  5410. call LABEL_3676
  5411. ld a, ($D147)
  5412. and $30
  5413. jp nz, LABEL_3A8C
  5414. ld hl, ($D516)
  5415. bit 7, h
  5416. jr z, $07
  5417. dec hl
  5418. ld a, h
  5419. cpl
  5420. ld h, a
  5421. ld a, l
  5422. cpl
  5423. ld l, a
  5424. ld a, l
  5425. and $C0
  5426. or h
  5427. jp z, Player_SetState_Standing
  5428. ret
  5429.  
  5430. Player_HandleJumping:
  5431. LABEL_33EA:
  5432. ld hl, $D3AA ;increase D3AA as long as button 1/2 is held.
  5433. ld a, ($D137)
  5434. and $30
  5435. jr nz, +
  5436. ld (hl), $20
  5437. jr ++
  5438. +: inc (hl)
  5439. ld a, (hl)
  5440. cp $0E
  5441. jr nc, ++
  5442. ld hl, $FBC0
  5443. ld a, (PlayerUnderwater)
  5444. or a
  5445. jr z, +
  5446. ld hl, $FCC0
  5447. +: ld ($D518), hl
  5448. ++: call LABEL_3A62
  5449. ld a, ($D502)
  5450. cp PlayerState_Jumping
  5451. ret nz
  5452. bit 1, (ix+$23)
  5453. ret z
  5454. ld a, ($D366)
  5455. cp $8D
  5456. jp nz, Player_SetState_Walking
  5457. ret
  5458.  
  5459. Player_HandleVerticalSpring: ;$3424
  5460. LABEL_3424:
  5461. call LABEL_3A62
  5462. ld a, ($D502)
  5463. cp PlayerState_VerticalSpring
  5464. ret nz
  5465. bit 1, (ix+$23) ;check to see if we're standing on something
  5466. jp nz, Player_SetState_Walking
  5467. bit 7, (ix+$19) ;check sign bit of vertical velocity
  5468. jp z, Player_SetState_Falling
  5469. jp CalculatePlayerDirection
  5470.  
  5471. Player_HandleDiagonalSpring:
  5472. LABEL_343E:
  5473. call LABEL_3A62
  5474. ld a, ($D502)
  5475. cp PlayerState_DiagonalSpring
  5476. ret nz
  5477. bit 1, (ix+$23) ;check to see if we're standing on something
  5478. jp nz, Player_SetState_Walking
  5479. bit 7, (ix+$19) ;check sign bit of vertical velocity
  5480. jp z, Player_SetState_Falling
  5481. ret
  5482.  
  5483. Player_HandleFalling: ;$3456
  5484. LABEL_3456:
  5485. call LABEL_3A62
  5486. ld a, ($D502)
  5487. cp PlayerState_Falling
  5488. ret nz
  5489. bit 1, (ix+$23)
  5490. jp nz, Player_SetState_Walking
  5491. ret
  5492.  
  5493. LABEL_3467:
  5494. ld hl, $FF40 ;set HorizontalVelocity
  5495. ld (ix+$16), l
  5496. ld (ix+$17), h
  5497. call LABEL_3A62
  5498. bit 1, (ix+$23) ;check to see if we're standing on something
  5499. ret z
  5500. ld hl, $FF00
  5501. ld (ix+$16), l ;set HorizontalVelocity
  5502. ld (ix+$17), h
  5503. jp Player_SetState_Walking
  5504.  
  5505. LABEL_3484:
  5506. call LABEL_3A62
  5507. bit 1, (ix+$23) ;check to see if we're standing on something
  5508. jp nz, Player_SetState_Walking
  5509. ret
  5510.  
  5511. LABEL_348F:
  5512. res 7, (ix+$04)
  5513. call LABEL_7378
  5514. call LABEL_6938
  5515. call CalculatePlayerDirection
  5516. call LABEL_376E
  5517. xor a
  5518. ld ($d469), a
  5519. ret
  5520.  
  5521.  
  5522. ;Reset the air timer. Called after a collision with an air bubble.
  5523. LABEL_34A4:
  5524. xor a
  5525. ld ($D469), a
  5526. ret
  5527.  
  5528. LABEL_34A9:
  5529. ld b, $04
  5530. ld a, ($d365)
  5531. cp $07
  5532. jr c, +
  5533. ld b, $08
  5534. cp $10
  5535. jr c, ++
  5536. cp $17
  5537. jr nc, ++
  5538. +: ld a, b
  5539. ld ($D137), a
  5540. xor a
  5541. ld ($D147), a
  5542. call CalculatePlayerDirection
  5543. ++: call LABEL_3A62
  5544. ld a, ($D366)
  5545. cp $00
  5546. jp z, Player_SetState_Falling
  5547. ld a, ($D363)
  5548. or a
  5549. jp z, Player_SetState_Walking
  5550. ret
  5551.  
  5552. LABEL_34DA:
  5553. res 7, (ix+$04)
  5554. jp Object_UpdateVerticalVelocity
  5555.  
  5556. LABEL_34E1:
  5557. res 7, (ix+$04)
  5558. ld hl, $0080
  5559. ld a, ($D51C)
  5560. cp $D8
  5561. jr c, $08
  5562. ld hl, GameState
  5563. set 3, (hl)
  5564. ld hl, $1000
  5565. ld (VerticalVelocity), hl
  5566. jp Object_UpdateVerticalVelocity
  5567.  
  5568. LABEL_34FD:
  5569. ld a, (CurrentLevel)
  5570. cp $04
  5571. jr nz, $06
  5572. ld a, (CurrentAct)
  5573. or a
  5574. jr z, $59
  5575. xor a
  5576. ld (ix+$18), a
  5577. ld (ix+$19), a
  5578. res 4, (ix+$04)
  5579. ld de, ($D174) ;horizontal screen offset in level
  5580. ld l, (ix+$11) ;horizontal object offset in level
  5581. ld h, (ix+$12)
  5582. xor a
  5583. sbc hl, de
  5584. ld de, $0120
  5585. xor a
  5586. sbc hl, de
  5587. jr c, $1A
  5588.  
  5589. LABEL_352A:
  5590. xor a
  5591. ld (ix+$16), a
  5592. ld (ix+$17), a
  5593. ld a, (CurrentAct)
  5594. cp $02
  5595. jr nc, LABEL_353E
  5596. ld hl, GameState
  5597. set 5, (hl)
  5598. ret
  5599.  
  5600. LABEL_353E:
  5601. ld hl, GameState
  5602. set 4, (hl)
  5603. ret
  5604.  
  5605. LABEL_3544:
  5606. ld hl, (HorizontalVelocity)
  5607. bit 7, h
  5608. jr z, $06
  5609. ld hl, $0000
  5610. ld (HorizontalVelocity), hl
  5611. ld a, h
  5612. cp $06
  5613. jr nc, +
  5614. ld de, $0010
  5615. add hl, de
  5616. ld (ix+$16), l
  5617. ld (ix+$17), h
  5618. +: jp Engine_UpdateObjectPosition
  5619.  
  5620. LABEL_3563:
  5621. xor a
  5622. ld (ix+$18), a
  5623. ld (ix+$19), a
  5624. set 4, (ix+$04)
  5625. ld hl, ($D174)
  5626. ld e, (ix+$11)
  5627. ld d, (ix+$12)
  5628. xor a
  5629. sbc hl, de
  5630. jr c, LABEL_357F
  5631. jp LABEL_352A
  5632.  
  5633. LABEL_357F:
  5634. ld hl, ($D516)
  5635. bit 7, h
  5636. jr nz, $06
  5637. ld hl, $FFC0
  5638. ld ($D516), hl
  5639. ld a, h
  5640. cp $FB
  5641. jr c, +
  5642. ld de, $FFF0
  5643. add hl, de
  5644. ld ($D516), hl
  5645. +: jp Engine_UpdateObjectPosition
  5646.  
  5647. ;********************************
  5648. ;* Loads loop motion data. *
  5649. ;********************************
  5650. LABEL_359B:
  5651. ld a, :Bank09
  5652. call Engine_SwapFrame2
  5653. ld l, (ix+$16) ;get horizontal velocity
  5654. ld h, (ix+$17)
  5655. ld de, ($D399)
  5656. ld a, ($D39B)
  5657. add hl, de
  5658. jr nc, +
  5659. inc a
  5660. +: ld ($D399), hl
  5661. ld ($D39B), a
  5662. ld hl, ($D39A)
  5663. add hl, hl
  5664. ld de, LoopMotionData ;loop motion data?
  5665. add hl, de
  5666. ld e, (hl)
  5667. inc hl
  5668. ld d, (hl)
  5669. ld l, (ix+$3C)
  5670. ld h, (ix+$3D)
  5671. add hl, de
  5672. ld (ix+$14), l ;set vertical pos
  5673. ld (ix+$15), h
  5674. ld hl, ($D39A)
  5675. add hl, hl
  5676. ld de, $887D
  5677. add hl, de
  5678. ld e, (hl)
  5679. inc hl
  5680. ld d, (hl)
  5681. ld l, (ix+$3A)
  5682. ld h, (ix+$3B)
  5683. add hl, de
  5684. ld (ix+$11), l ;set horizontal pos
  5685. ld (ix+$12), h
  5686. ld hl, ($D39A)
  5687. xor a
  5688. ld de, $00b0
  5689. sbc hl, de
  5690. jr nc, $16
  5691. ld l, (ix+$16) ;get horizontal speed
  5692. ld h, (ix+$17)
  5693. ld de, $0007
  5694. xor a
  5695. sbc hl, de
  5696. jr c, $38
  5697. ld (ix+$16), l ;set horizontal speed
  5698. ld (ix+$17), h
  5699. jr $10
  5700. ld l, (ix+$16)
  5701. ld h, (ix+$17)
  5702. ld de, $000c
  5703. add hl, de
  5704. ld (ix+$16), l
  5705. ld (ix+$17), h
  5706. ld hl, ($D39A)
  5707. ld de, $0230
  5708. xor a
  5709. sbc hl, de
  5710. ret c
  5711. ld (ix+$02), $06
  5712. ld hl, ($D36D)
  5713. ld (ix+$16), l
  5714. ld (ix+$17), h
  5715. call LABEL_3A62
  5716. ld a, $10
  5717. ld ($D39C), a
  5718. ret
  5719.  
  5720. LABEL_3638:
  5721. ld l, (ix+$3a)
  5722. ld h, (ix+$3b)
  5723. ld e, (ix+$11)
  5724. ld d, (ix+$12)
  5725. xor a
  5726. sbc hl, de
  5727. jr c, $13
  5728. ld l, (ix+$11)
  5729. ld h, (ix+$12)
  5730. ld de, $0008
  5731. add hl, de
  5732. ld (ix+$11), l
  5733. ld (ix+$12), h
  5734. jp LABEL_3176
  5735.  
  5736. LABEL_365C:
  5737. ld l, (ix+$11)
  5738. ld h, (ix+$12)
  5739. ld de, $FFFE
  5740. add hl, de
  5741. ld (ix+$11), l
  5742. ld (ix+$12), h
  5743. jp LABEL_3176
  5744.  
  5745. LABEL_366F:
  5746. ld (ix+$02), PlayerState_Running
  5747. jp LABEL_3A62
  5748.  
  5749. LABEL_3676:
  5750. ld a, ($D39C)
  5751. or a
  5752. jr z, LABEL_3681
  5753. dec a
  5754. ld ($D39C), a
  5755. ret
  5756.  
  5757. LABEL_3681: ;collide with ALZ-1 loop
  5758. ld a, (CurrentLevel) ;check for ALZ
  5759. cp $02
  5760. jr nz, LABEL_36B2
  5761. ld a, (CurrentAct) ;check for ACT 1
  5762. or a
  5763. ret nz
  5764. ld hl, ($D511) ;horizontal pos in level
  5765. ld de, $075E
  5766. xor a
  5767. sbc hl, de
  5768. ret c
  5769. ld a, h
  5770. or a
  5771. ret nz
  5772. ld a, l
  5773. and $F8
  5774. ret nz
  5775. ld hl, ($D514) ;vertical pos in level
  5776. ld de, $0160
  5777. xor a
  5778. sbc hl, de
  5779. ret c
  5780. ld a, h
  5781. or a
  5782. ret nz
  5783. ld a, l
  5784. and $E0
  5785. ret nz
  5786. jp LABEL_3725
  5787.  
  5788. LABEL_36B2: ;collide with GHZ-1 loop
  5789. ld a, (CurrentLevel) ;check for GHZ
  5790. cp $03
  5791. ret nz
  5792. ld a, (CurrentAct) ;check for ACT 1
  5793. or a
  5794. jr nz, LABEL_3703 ;check for ACT 2
  5795. ld hl, ($D511) ;horizontal pos in level
  5796. ld de, $0D5E
  5797. xor a
  5798. sbc hl, de
  5799. jr c, LABEL_36E1
  5800. ld a, h
  5801. or a
  5802. jr nz, LABEL_36E1
  5803. ld a, l
  5804. and $F8
  5805. jr nz, LABEL_36E1
  5806. ld hl, ($D514) ;vertical pos in level
  5807. ld de, $0140
  5808. xor a
  5809. sbc hl, de
  5810. jr c, LABEL_36E1
  5811. jp LABEL_3725
  5812.  
  5813. LABEL_36E1:
  5814. ld hl, ($D511) ;horizontal pos in level
  5815. ld de, $0F3E
  5816. xor a
  5817. sbc hl, de
  5818. jr c, $17
  5819. ld a, h
  5820. or a
  5821. jr nz, $13
  5822. ld a, l
  5823. and $F8
  5824. jr nz, $0E
  5825. ld hl, ($D514) ;vertical pos in level
  5826. ld de, $0160
  5827. xor a
  5828. sbc hl, de
  5829. jr c, $03
  5830. jp LABEL_3725
  5831.  
  5832. LABEL_3702:
  5833. ret
  5834.  
  5835. LABEL_3703:
  5836. ld a, (CurrentAct) ;check for ACT 2
  5837. dec a
  5838. ret nz
  5839. ld hl, ($D511) ;horizontal pos in level
  5840. ld de, $0AFE
  5841. xor a
  5842. sbc hl, de
  5843. ret c
  5844. ld a, h
  5845. or a
  5846. ret nz
  5847. ld a, l
  5848. and $F8
  5849. ret nz
  5850. ld hl, ($D514) ;vertical pos in level
  5851. ld de, $0160
  5852. xor a
  5853. sbc hl, de
  5854. ret c
  5855. jr LABEL_3725 ;FIXME: why bother?
  5856.  
  5857. ;load loop motion data?
  5858. LABEL_3725:
  5859. ld a, (ix+$0A)
  5860. cp $80
  5861. jr nc, LABEL_3758
  5862. ld (ix+$02), $0C
  5863. ld l, (ix+$11)
  5864. ld h, (ix+$12)
  5865. ld (ix+$3A), l
  5866. ld (ix+$3B), h
  5867. ld l, (ix+$14)
  5868. ld h, (ix+$15)
  5869. ld de, $0000
  5870. add hl, de
  5871. ld (ix+$3C), l
  5872. ld (ix+$3D), h
  5873. xor a
  5874. ld ($D399), a
  5875. ld ($D39A), a
  5876. ld ($D39B), a
  5877. pop af
  5878. ret
  5879.  
  5880. LABEL_3758:
  5881. ld (ix+$02), $0D
  5882. pop af
  5883. ret
  5884.  
  5885. LABEL_375E:
  5886. ret
  5887.  
  5888. LABEL_375F:
  5889. ld hl, $0180
  5890. ld ($D36D), hl
  5891. ld hl, $0008
  5892. ld ($D36F), hl
  5893. jp Object_UpdateHorizontalVelocity
  5894.  
  5895. LABEL_376E:
  5896. bit 7, (ix+$03) ;check to see if we need to flash the sprite
  5897. jp nz, FlashSprite
  5898. ld a, ($D532) ;check for invincibility power-up
  5899. cp $02
  5900. jr nz, LABEL_3784
  5901. xor a
  5902. ld ($D3A8), a
  5903. ld ($D520), a
  5904. ret
  5905.  
  5906. LABEL_3784:
  5907. ld a, ($D3A8)
  5908. or a
  5909. jp z, LABEL_3796
  5910. xor a
  5911. ld ($D3A8), a
  5912. res 0, (ix+$23)
  5913. jp Player_SetState_Hurt
  5914.  
  5915. LABEL_3796:
  5916. ld a, ($d520)
  5917. or a
  5918. ret z
  5919. call Engine_GetObjectDescriptorPointer
  5920. ld a, (hl)
  5921. cp $27
  5922. jp z, LABEL_38A8
  5923. cp $29
  5924. jp z, LABEL_38A8
  5925. bit 1, (ix+$03)
  5926. jp nz, LABEL_384E
  5927.  
  5928. Player_SetState_Hurt: ;$37B0
  5929. ;LABEL_37B0: ;deals with "getting hurt"
  5930. ld a, (RingCounter) ;check for 0 rings
  5931. or a
  5932. jp z, LABEL_3823
  5933. rrca ;calculate the number of rings to
  5934. rrca ;drop based on the value of the
  5935. rrca ;high nibble of the ring counter.
  5936. rrca
  5937. and $0F
  5938. inc a
  5939. cp $08
  5940. jr c, +
  5941. ld a, $07 ;drop up to 7 rings
  5942. +: ld b, a
  5943. ld c, $08
  5944. ld h, $00
  5945. - push bc
  5946. call LABEL_6144
  5947. pop bc
  5948. inc h
  5949. djnz -
  5950.  
  5951. set 7, (ix+$03) ;Flash player sprite on/off
  5952. set 6, (ix+$03)
  5953. ld a, $78 ;for $78 frames
  5954. ld ($D3A9), a
  5955.  
  5956. xor a ;reset the ring counter
  5957. ld (RingCounter), a
  5958. call LABEL_25DB
  5959.  
  5960. ld a, SFX_LoseRings ;play the "Lose rings" sound effect
  5961. ld (NextMusicTrack), a
  5962. Player_PlayHurtAnimation: ;$37EA
  5963. ld (ix+$20), $00
  5964. ld (ix+$02), PlayerState_Hurt
  5965. set 0, (ix+$03)
  5966. res 2, (ix+$03)
  5967. res 1, (ix+$22)
  5968. ld hl, $FC00 ;make player sprite "bounce" out of the way
  5969. bit 0, (ix+$22)
  5970. jr z, +
  5971. ld hl, $0000
  5972. +: ld ($D518), hl ;move sprite up
  5973. ld hl, $0100
  5974. bit 3, (ix+$23)
  5975. jr nz, +
  5976. ld hl, $FF00
  5977. +: ld ($D516), hl ;move sprite back
  5978. ld hl, $0000
  5979. ld ($D36F), hl
  5980. ret
  5981.  
  5982. LABEL_3823:
  5983. ld (ix+$02), PlayerState_LostLife
  5984. set 0, (ix+$03)
  5985. ld (ix+$04), $00
  5986. ld hl, $FB00 ;make the sprite bounce up first
  5987. ld (VerticalVelocity), hl
  5988. ld hl, $0000
  5989. ld ($D36F), hl
  5990. res 1, (ix+$22)
  5991. ld hl, GameState
  5992. set 3, (hl)
  5993. xor a
  5994. ld ($D49F), a
  5995. ld a, Music_LoseLife ;play "Lost Life" sound
  5996. ld ($DD04), a
  5997. ret
  5998.  
  5999. LABEL_384E:
  6000. bit 4, (ix+$21)
  6001. jr nz, +
  6002. bit 5, (ix+$21)
  6003. jr nz, ++
  6004. -: xor a
  6005. ld ($D520), a
  6006. ret
  6007.  
  6008. +: set 0, (ix+$03)
  6009. ld hl, $0080
  6010. ld (VerticalVelocity), hl
  6011. jr -
  6012.  
  6013. ++: set 0, (ix+$03)
  6014. res 1, (ix+$22)
  6015. ld hl, $FD00
  6016. ld (VerticalVelocity), hl
  6017. jr -
  6018.  
  6019. FlashSprite:
  6020. LABEL_387B:
  6021. ld a, ($D3A9)
  6022. or a
  6023. jr z, ++ ;if counter = 0 reset the sprite flags
  6024. dec a
  6025. ld ($D3A9), a
  6026. rrca
  6027. rrca
  6028. jr c, +
  6029. -: res 7, (ix+$04) ;sprite = invisible
  6030. ret
  6031.  
  6032. +: set 7, (ix+$04) ;sprite = visible
  6033. ret
  6034.  
  6035. ++: res 7, (ix+$04)
  6036. res 7, (ix+$03)
  6037. res 6, (ix+$03)
  6038. xor a
  6039. ld ($D3A8), a
  6040. ld (ix+$20), a
  6041. jr -
  6042.  
  6043. LABEL_38A8:
  6044. ld (ix+$20), $00
  6045. ret
  6046.  
  6047. LABEL_38AD:
  6048. ld bc, $FFFC ;horizontal offset
  6049. ld de, $0004 ;vertical offset
  6050. call Engine_GetCollisionDataForBlock ;collide with level tiles
  6051. ld a, ($D362) ;get vertical projection value
  6052. ld b, a
  6053. push bc
  6054. ld bc, $0004 ;horizontal offset
  6055. ld de, $0004 ;vertical offset
  6056. call Engine_GetCollisionDataForBlock ;collide with level tiles
  6057. ld a, ($D362) ;get vertical projection value
  6058. pop bc
  6059. ld c, a
  6060. sub b
  6061. jr nc, + ;if left value is less than right value
  6062. neg
  6063. +: cp $10
  6064. ret c
  6065. ld a, c
  6066. cp b
  6067. jr z, LABEL_38E9 ;left == right
  6068. jr c, LABEL_38E0 ;left > right
  6069. ld (ix+$02), PlayerState_Balance
  6070. set 4, (ix+$04)
  6071. ret
  6072.  
  6073. LABEL_38E0:
  6074. ld (ix+$02), PlayerState_Balance
  6075. res 4, (ix+$04)
  6076. ret
  6077.  
  6078. LABEL_38E9:
  6079. ld (ix+$02), PlayerState_Standing
  6080. ret
  6081.  
  6082. LABEL_38EE:
  6083. ld hl, $D52F
  6084. inc (hl)
  6085. ld a, (hl)
  6086. cp $06
  6087. jr c, +
  6088. xor a
  6089. ld (hl), a
  6090. +: add a, $01
  6091. ld (ix+$06), a ;set animation frame
  6092. ld a, (HorizontalVelocity+1) ;get hi-byte of current speed
  6093. and a
  6094. jp p, + ;jump if positive number
  6095. neg
  6096. +: ld l, a ;change frame dispaly time based on current speed
  6097. ld h, $00
  6098. ld de, DATA_3913
  6099. add hl, de
  6100. ld a, (hl)
  6101. ld (ix+$07), a
  6102. ret
  6103.  
  6104. DATA_3913: ;frame display times
  6105. .db $0A, $08, $06, $04, $04, $04, $04, $04
  6106. .db $04, $04, $04, $04, $04, $04, $04, $04
  6107.  
  6108. LABEL_3923:
  6109. ld hl, $D52F
  6110. inc (hl)
  6111. ld a, (hl)
  6112. cp $14
  6113. jr c, +
  6114. xor a
  6115. ld (hl), a
  6116. + ld c, a
  6117. ld b, $00
  6118. ld hl, DATA_395C
  6119. add hl, bc
  6120. ld a, (hl)
  6121. ld (ix+$06), a
  6122. bit 1, (ix+$22)
  6123. jr z, LABEL_3980
  6124. res 4, (ix+$04)
  6125. ld a, ($D517) ;get hi-byte of h-speed
  6126. and a
  6127. jp p, +
  6128. neg
  6129. set 4, (ix+$04)
  6130. +: ld l, a
  6131. ld h, $00
  6132. ld de, DATA_3970
  6133. add hl, de
  6134. ld a, (hl)
  6135. ld (ix+$07), a
  6136. ret
  6137.  
  6138. DATA_395C:
  6139. .db $11, $12, $13, $14, $15, $12, $13, $14
  6140. .db $11, $15, $13, $14, $11, $12, $15, $14
  6141. .db $11, $12, $13, $15
  6142.  
  6143. DATA_3970:
  6144. .db $0A, $08, $06, $04, $04, $04, $04, $04
  6145. .db $04, $04, $04, $04, $04, $04, $04, $04
  6146.  
  6147. LABEL_3980:
  6148. ld a, ($D137)
  6149. bit 3, a
  6150. jr z, +
  6151. res 4, (ix+$04)
  6152. jr $08
  6153. +: bit 2, a
  6154. jr z, $04
  6155. set 4, (ix+$04)
  6156. ld a, $03
  6157. ld (ix+$07), a
  6158. ret
  6159.  
  6160. LABEL_399B:
  6161. ld hl, ($d53c)
  6162. ld de, ($d514)
  6163. xor a
  6164. sbc hl, de
  6165. srl l
  6166. srl l
  6167. srl l
  6168. srl l
  6169. ld h, $00
  6170. add hl, hl
  6171. ld de, DATA_39C4
  6172. add hl, de
  6173. ld a, (hl)
  6174. cp (ix+$06)
  6175. jr nz, $01 ; (+$01)
  6176. inc hl
  6177. ld a, (hl)
  6178. ld (ix+$06), a
  6179. ld (ix+$07), $06
  6180. ret
  6181.  
  6182. DATA_39C4:
  6183. .db $01, $03, $01, $03, $1E, $1F, $1E, $1F
  6184. .db $21, $20, $22, $23, $24, $25, $27, $26
  6185. .db $28, $29, $2A, $2B, $2D, $2C, $2E, $2F
  6186. .db $30, $31, $33, $32, $34, $35, $36, $37
  6187. .db $39, $38, $3A, $3B
  6188.  
  6189. LABEL_39E8:
  6190. ld hl, ($d39a)
  6191. ld e, $1b
  6192. call LABEL_1BE9
  6193. ld h, $00
  6194. add hl, hl
  6195. ld de, DATA_3A07
  6196. add hl, de
  6197. ld a, (hl)
  6198. cp (ix+$06)
  6199. jr nz, +
  6200. inc hl
  6201. +: ld a, (hl)
  6202. ld (ix+$06), a
  6203. ld (ix+$07), $06
  6204. ret
  6205.  
  6206. DATA_3A07:
  6207. .db $1E, $1F, $1E, $1F, $21, $20, $22, $23
  6208. .db $24, $25, $27, $26, $28, $29, $2A, $2B
  6209. .db $2D, $2C, $2E, $2F, $30, $31, $33, $32
  6210. .db $34, $35, $36, $37, $39, $38, $3A, $3B
  6211. .db $01, $03, $01, $03, $01, $03, $01, $03
  6212. .db $01, $03, $01, $03, $01, $03, $01, $03
  6213. .db $01, $03, $01, $03
  6214.  
  6215.  
  6216. ;********************************************************
  6217. ;* Calculates the position of the sprite within the *
  6218. ;* currently displayed screen. *
  6219. ;********************************************************
  6220. Engine_GetSpriteScreenPos: ;$3A3B
  6221. ld l, (ix+$11) ;sprite horiz. offset in level
  6222. ld h, (ix+$12)
  6223. ld de, ($D174) ;screen horiz. offset in level
  6224. xor a
  6225. sbc hl, de ;calculate sprite horiz. pos on screen.
  6226. ld (ix+$1A), l ;store horiz. pos
  6227. ld (ix+$1B), h
  6228. ld l, (ix+$14) ;sprite vert. offset in level
  6229. ld h, (ix+$15)
  6230. ld de, ($D176) ;screen vert. offset in level
  6231. xor a
  6232. sbc hl, de ;calculate sprite vert. pos on screen.
  6233. ld (ix+$1C), l ;store vert. pos
  6234. ld (ix+$1D), h
  6235. ret
  6236.  
  6237.  
  6238. LABEL_3A62:
  6239. ld a, (CurrentLevel)
  6240. cp $02
  6241. call z, LABEL_4887 ;under water stuff
  6242. call LABEL_3BDD
  6243. ld a, (ix+$01)
  6244. cp $05
  6245. jr nc, +
  6246. ld hl, $0000
  6247. ld ($D371), hl
  6248. +: call Object_UpdateHorizontalVelocity
  6249. call Object_UpdateVerticalVelocity
  6250. call LABEL_6BF2
  6251. call LABEL_376E
  6252. ld a, ($D147)
  6253. and $30 ;check for either button 1 or button2
  6254. ret z
  6255.  
  6256. Player_SetState_Jumping:
  6257. LABEL_3A8C:
  6258. ld a, ($D501)
  6259. cp $1A
  6260. ret z
  6261. bit 0, (ix+$03)
  6262. ret nz
  6263. bit 2, (ix+$03)
  6264. ret nz
  6265. xor a
  6266. ld ($D3AA),a
  6267. set 0, (ix+$03) ;set player in air
  6268. set 1, (ix+$03) ;destroy enemies on collision
  6269. ld (ix+$02), PlayerState_Jumping ;play jump animation
  6270. ld hl, $FBC0
  6271. ld a, (PlayerUnderwater)
  6272. or a
  6273. jr z, +
  6274. ld hl, $FCC0
  6275. +: ld (VerticalVelocity),hl
  6276. ld a, $80
  6277. ld (CameraOffsetY), a
  6278. res 1, (ix+$22)
  6279. ld a, SFX_Jump ;play jump sound effect
  6280. ld ($DD04), a
  6281. ret
  6282.  
  6283. ;called when jumping & colliding with horizontal spring
  6284. LABEL_3ACA:
  6285. set 0, (ix+$03) ;player rolling
  6286. set 1, (ix+$03) ;destroy enemy on collision
  6287. ld (ix+$02), PlayerState_Jumping
  6288. ld hl, $0000
  6289. ld (VerticalVelocity), hl
  6290. res 1, (ix+$22)
  6291. ret
  6292.  
  6293. Object_UpdateHorizontalVelocity: ;$3AE1
  6294. ld hl, (HorizontalVelocity)
  6295. ld de, ($D36F) ;velocity delta?
  6296. add hl, de
  6297. ld de, ($D371) ;gradient adjustment?
  6298. add hl, de
  6299. bit 7, h ;is negative?
  6300. jr nz, +
  6301.  
  6302. bit 2, (ix+$23)
  6303. jr nz, LABEL_3B44 ;set velocity & acceleration = 0
  6304. ld (ix+$0A), $40
  6305. ld a, (MaxHorizontalVelocity+1)
  6306. ld b, a ;limit horizontal velocity to maximum
  6307. ld a, h
  6308. cp b
  6309. jr c, ++
  6310. ld hl, (MaxHorizontalVelocity)
  6311. jr ++
  6312.  
  6313. +: bit 3, (ix+$23)
  6314. jr nz, $35
  6315. ld (ix+$0a), $C0
  6316. ld a, (MaxHorizontalVelocity+1)
  6317. neg
  6318. ld b, a
  6319. ld a, h
  6320. cp b
  6321. jr nc, ++
  6322. ld hl, (MaxHorizontalVelocity) ;limit horizontal velocity to minimum
  6323. dec hl ;2's comp the max velocity
  6324. ld a, h
  6325. cpl
  6326. ld h, a
  6327. ld a, l
  6328. cpl
  6329. ld l, a
  6330.  
  6331. ++: ld (HorizontalVelocity), hl ;set the horiz. velocity
  6332. ld c, $00
  6333. bit 7, h
  6334. jr z, +
  6335. dec c
  6336. +: xor a
  6337. ld de, ($D510)
  6338. add hl, de
  6339. ld ($D510), hl
  6340. ld a, $00
  6341. adc a, c
  6342. add a, (ix+$12)
  6343. ld ($D512), a
  6344. ret
  6345.  
  6346. LABEL_3B44:
  6347. ld hl, $0000
  6348. ld (HorizontalVelocity), hl
  6349. ld ($D36F), hl
  6350. ret
  6351.  
  6352. Object_UpdateVerticalVelocity:
  6353. LABEL_3B4E:
  6354. bit 0, (ix+$03) ;check to see if player is in air
  6355. jr nz, Object_CalcJumpDeceleration ;player is in air
  6356. ld a, ($D3B9)
  6357. or a
  6358. ret nz
  6359. ld hl, (VerticalVelocity)
  6360. ld a, h
  6361. and a
  6362. jp p, Object_SetVerticalVelocity
  6363. dec hl
  6364. ld a, h
  6365. cpl
  6366. ld h, a
  6367. ld a, l
  6368. cpl
  6369. ld l, a
  6370. jr Object_SetVerticalVelocity
  6371.  
  6372. Object_CalcJumpDeceleration: ;$3B6A
  6373. ld hl, (VerticalVelocity)
  6374. ld a, (PlayerUnderwater) ;check to see if player is underwater
  6375. or a
  6376. jr nz, Object_CalcJumpDeceleration_Underwater
  6377. ld a, (ix+$01)
  6378. ld de, $0018 ;deceleration from vertical spring
  6379. cp PlayerState_VerticalSpring
  6380. jr z, +
  6381. ld de, $0024 ;deceleration from ramp jump
  6382. cp PlayerState_JumpFromRamp
  6383. jr z, +
  6384. ld de, $0030 ;deceleration from normal jump
  6385. +: add hl, de
  6386. ld a, h
  6387. and a
  6388. jp m, Object_SetVerticalVelocity ;jump if velocity is negative
  6389. cp $07
  6390. jr c, +
  6391. ld hl, $0700
  6392. +: jr Object_SetVerticalVelocity
  6393.  
  6394. Object_CalcJumpDeceleration_Underwater: ;$3B96
  6395. ld a, (ix+$01)
  6396. ld de, $000C
  6397. cp PlayerState_VerticalSpring
  6398. jr z, +
  6399. ld de, $0012
  6400. cp PlayerState_JumpFromRamp
  6401. jr z, +
  6402. ld de, $0018
  6403. +: add hl, de
  6404. ld a, h
  6405. and a
  6406. jp m, Object_SetVerticalVelocity
  6407. cp $04
  6408. jr c, Object_SetVerticalVelocity
  6409. ld hl, $0400
  6410. Object_SetVerticalVelocity: ;$3BB7
  6411. bit 1, (ix+$22)
  6412. jr z, +
  6413. ld hl, $0700
  6414. +: ld (VerticalVelocity), hl
  6415. ld c, $00
  6416. bit 7, h ;is velocity negative?
  6417. jr z, +
  6418. dec c
  6419. +: xor a
  6420. ld de, ($D513)
  6421. add hl, de
  6422. ld ($D513), hl
  6423. ld a, $00
  6424. adc a, c
  6425. add a, (ix+$15)
  6426. ld ($D515), a
  6427. ret
  6428.  
  6429. LABEL_3BDD:
  6430. ld hl, ($D511) ;sprite horiz offset in level
  6431. ld de, ($D174) ;cam horiz offset in level
  6432. xor a
  6433. sbc hl, de ;calculate sprite offset on screen
  6434. ld a, l
  6435. ld bc, $0010 ;check to see if sprite is at left-edge of level.
  6436. cp $10
  6437. jp c, Engine_LimitScreenPos_Left ;jump if A < $10
  6438. ld bc, $00F7 ;check to see if sprite is at right-edge of level.
  6439. cp $F8
  6440. jp nc, Engine_LimitScreenPos_Right ;jump if A > $F8
  6441.  
  6442. ld a, (ix+$01)
  6443. cp PlayerState_Hurt
  6444. ret nc
  6445.  
  6446. ld a, (PlayerUnderwater)
  6447. or a
  6448. jp nz, LABEL_3C5F
  6449.  
  6450. ld a, (ix+$01)
  6451. add a, a
  6452. add a, a
  6453. ld l, a
  6454. ld h, $00
  6455. ld a, ($D137)
  6456. and $0C ;check for left/right buttons
  6457. jp z, LABEL_3CB8
  6458. ld de, DATA_3D96
  6459. bit 7, (ix+$17) ;is horizontal velocity negative?
  6460. jr nz, +
  6461. ld de, DATA_3D16
  6462. +: ld bc, $0000
  6463. bit 2, a
  6464. jr nz, LABEL_3C2B
  6465. ld bc, $0002
  6466. LABEL_3C2B:
  6467. add hl, bc
  6468. add hl, de
  6469. ld e, (hl)
  6470. inc hl
  6471. ld d, (hl)
  6472. ld hl, (HorizontalVelocity)
  6473. bit 7, h ;is horizontal velocity positive?
  6474. jr z, +
  6475. dec hl ;2's comp the value in HL
  6476. ld a, h
  6477. cpl
  6478. ld h, a
  6479. ld a, l
  6480. cpl
  6481. ld l, a
  6482. +: ld bc, $0080
  6483. add hl, bc
  6484. ld a, h
  6485. or a
  6486. jr nz, +
  6487. ex de, hl
  6488. add hl, hl
  6489. ex de, hl
  6490. +: ld ($D36F), de
  6491. ld a, ($D363) ;speed modifier value
  6492. ld l, a
  6493. ld h, $00
  6494. ld de, DATA_4016
  6495. add hl, de
  6496. ld e, (hl)
  6497. inc hl
  6498. ld d, (hl)
  6499. ld ($D371), de
  6500. ret
  6501.  
  6502. LABEL_3C5F:
  6503. ld a, (ix+$01) ;get current animation (sprite type)
  6504. add a, a
  6505. add a, a
  6506. ld l, a
  6507. ld h, $00
  6508. ld a, ($D137)
  6509. and $0C ;check left/right buttons
  6510. jr z, LABEL_3CB8
  6511. ld de, DATA_3F16
  6512. bit 7, (ix+$17) ;is horiz. velocity negative?
  6513. jr nz, +
  6514. ld de, DATA_3E96
  6515. +: ld bc, $0000
  6516. bit 2, a
  6517. jr nz, +
  6518. ld bc, $0002
  6519. +: add hl, bc
  6520. add hl, de
  6521. ld e, (hl)
  6522. inc hl
  6523. ld d, (hl)
  6524.  
  6525. ld hl, ($D516) ;hl = HorizontalVelocity
  6526. bit 7, h ;is horizontal velocity negative?
  6527. jr z, +
  6528. dec hl ;if hl is negative, 2's comp the value
  6529. ld a, h
  6530. cpl
  6531. ld h, a
  6532. ld a, l
  6533. cpl
  6534. ld l, a
  6535. +: ld bc, $0080 ;add 128 to hl
  6536. add hl, bc
  6537. ld a, h
  6538. or a
  6539. jr nz, +
  6540. ex de, hl
  6541. add hl, hl
  6542. ex de, hl
  6543. +: ld ($d36f), de
  6544. ld a, ($d363)
  6545. ld l, a
  6546. ld h, $00
  6547. ld de, DATA_4016
  6548. add hl, de
  6549. ld e, (hl)
  6550. inc hl
  6551. ld d, (hl)
  6552. ld ($D371), de
  6553. ret
  6554.  
  6555. LABEL_3CB8:
  6556. ld de, $0000
  6557. ld ($D36F), de
  6558. ld a, (ix+$16)
  6559. or (ix+$17)
  6560. ret z
  6561. ld de, DATA_3E16
  6562. ld bc, $0000
  6563. bit 7, (ix+$17)
  6564. jp nz, LABEL_3C2B
  6565. ld bc, $0002
  6566. jp LABEL_3C2B
  6567.  
  6568.  
  6569. ;FIXME: this subroutine is a duplicate. Replace with call to Engine_LimitScreenPos_Right.
  6570. Engine_LimitScreenPos_Left: ;$3CD9
  6571. ld hl, ($D174) ;horizontal cam offset in level
  6572. add hl, bc
  6573. ld ($D511), hl
  6574. xor a
  6575. ld ($D510), a
  6576. jr LABEL_3CF1
  6577.  
  6578. ;****************************************************
  6579. ;* Limits the sprite's position on screen. Called *
  6580. ;* when the sprite is at the horizontal extremes *
  6581. ;* of the level. *
  6582. ;****************************************************
  6583. Engine_LimitScreenPos_Right: ;$3CE6
  6584. ld hl, ($D174) ;horizontal cam offset
  6585. add hl, bc
  6586. ld ($D511), hl
  6587. xor a
  6588. ld ($D510), a
  6589. LABEL_3CF1:
  6590. ld hl, ($D510)
  6591. ld de, (HorizontalVelocity)
  6592. ld c, $00
  6593. bit 7, d ;is horiz velocity negative?
  6594. jr z, +
  6595. dec c
  6596. +: xor a
  6597. sbc hl, de
  6598. ld ($D510), hl
  6599. ld a, (ix+$12) ;hi-byte of horizontal position
  6600. sbc a, c
  6601. ld (ix+$12), a
  6602. ld hl, $0000 ;set velocity to 0
  6603. ld ($D36F), hl
  6604. ld (HorizontalVelocity), hl
  6605. ret
  6606.  
  6607.  
  6608. ;Note that the data at $3F16 spans across into the next bank.
  6609. ;This ORG directive keeps things aligned. It's hacky and probably
  6610. ;should be changed.
  6611.  
  6612. .org $3D16
  6613. DATA_3D16:
  6614. .incbin "unknown\s2_3D16.bin"
  6615.  
  6616. DATA_3D96:
  6617. .incbin "unknown\s2_3D96.bin"
  6618.  
  6619. DATA_3E16:
  6620. .incbin "unknown\s2_3E16.bin"
  6621.  
  6622. DATA_3E96:
  6623. .incbin "unknown\s2_3E96.bin"
  6624.  
  6625. DATA_3F16:
  6626. .incbin "unknown\s2_3F16.bin"
  6627.  
  6628. .BANK 1 SLOT 1
  6629. .ORG $0000
  6630.  
  6631.  
  6632. .db $00, $00, $05, $00, $FB, $FF, $00, $00
  6633. .db $00, $00, $00, $00, $00, $00, $00, $00
  6634. .db $00, $00, $00, $00, $00, $00
  6635.  
  6636. ;h-speed adjustment values
  6637. DATA_4016:
  6638. .dw $0000
  6639. .dw $FFE1
  6640. .dw $001F
  6641. .dw $FFE8
  6642. .dw $0018
  6643. .dw $FFF0
  6644. .dw $0010
  6645.  
  6646. LABEL_4024:
  6647. res 0, (ix+$03)
  6648. set 2, (ix+$03)
  6649. ld (ix+$02), $11
  6650. ld hl, $0000
  6651. ld ($d516), hl
  6652. ret
  6653.  
  6654. LABEL_4037:
  6655. res 0, (ix+$03)
  6656. set 2, (ix+$03)
  6657. ld (ix+$02), $12
  6658. ret
  6659.  
  6660. LABEL_4044:
  6661. bit 1, (ix+$22)
  6662. jr nz, LABEL_404F
  6663. ld (ix+$02), $13
  6664. ret
  6665.  
  6666. LABEL_404F:
  6667. set 0, (ix+$03)
  6668. ld (ix+$02), $13
  6669. ld (ix+$18), $00
  6670. ld (ix+$19), $fe
  6671. res 1, (ix+$22)
  6672. ret
  6673.  
  6674. LABEL_4064:
  6675. ld (ix+$02), PlayerState_HangGliderBack
  6676. ld (ix+$18), $00
  6677. ld (ix+$19), $fe
  6678. set 0, (ix+$03)
  6679. res 1, (ix+$22)
  6680. ret
  6681.  
  6682. LABEL_4079:
  6683. ld (ix+$02), PlayerState_HangGliderFwd
  6684. ld (ix+$18), $80
  6685. ld (ix+$19), $00
  6686. set 0, (ix+$03)
  6687. res 1, (ix+$22)
  6688. ret
  6689.  
  6690. LABEL_408E:
  6691. res 7, (ix+$04)
  6692. call LABEL_3A62
  6693. ld hl, ($D516)
  6694. bit 7, h
  6695. jr z, +
  6696. dec hl
  6697. ld a, h
  6698. cpl
  6699. ld h, a
  6700. ld a, l
  6701. cpl
  6702. ld l, a
  6703. +: ld a, l
  6704. and $F0
  6705. or h
  6706. jp nz, LABEL_4037
  6707. bit 1, (ix+$22)
  6708. jp z, LABEL_4044
  6709. ret
  6710.  
  6711. LABEL_40B2:
  6712. res 7, (ix+$04)
  6713. call LABEL_3A62
  6714. bit 1, (ix+$22)
  6715. jp z, LABEL_4044
  6716. ld hl, ($D516)
  6717. bit 7, h
  6718. jr z, +
  6719. dec hl
  6720. ld a, h
  6721. cpl
  6722. ld h, a
  6723. ld a, l
  6724. cpl
  6725. ld l, a
  6726. +: ld a, l
  6727. and $F0
  6728. or h
  6729. jp z, LABEL_4024
  6730. ret
  6731.  
  6732. LABEL_40D6:
  6733. res 7, (ix+$04)
  6734. call LABEL_4137
  6735. call LABEL_6BF2
  6736. LABEL_40E0:
  6737. call LABEL_376E
  6738. bit 1, (ix+$22)
  6739. jr z, LABEL_40F0
  6740. res 2, (ix+$03)
  6741. jp LABEL_3032
  6742.  
  6743. LABEL_40F0:
  6744. ld a, (ix+$22)
  6745. and $0F
  6746. jr nz, +
  6747. call LABEL_4109
  6748. jr c, +
  6749. ld a, ($D147)
  6750. and $30
  6751. ret z
  6752. +: res 2, (ix+$03)
  6753. jp Player_SetState_Falling
  6754.  
  6755. LABEL_4109:
  6756. ld de, ($D174)
  6757. ld hl, ($D511)
  6758. xor a
  6759. sbc hl, de
  6760. jp c, LABEL_4135
  6761. ld a, h
  6762. or a
  6763. jp nz, LABEL_4135
  6764. ld a, l
  6765. cp $F0
  6766. jp nc, LABEL_4135
  6767. ld de, ($D176)
  6768. ld hl, ($D514)
  6769. xor a
  6770. sbc hl, de
  6771. jp c, LABEL_4135
  6772. ld a, h
  6773. or a
  6774. jp nz, LABEL_4135
  6775. xor a
  6776. ret
  6777.  
  6778. LABEL_4135:
  6779. scf
  6780. ret
  6781.  
  6782. LABEL_4137:
  6783. call LABEL_4204
  6784. ld de, $0004
  6785. ld bc, $0200
  6786. call Engine_SetObjectVerticalSpeed
  6787. call Engine_UpdateObjectPosition
  6788. ld a, ($D137)
  6789. bit 2, a
  6790. jr nz, LABEL_415F
  6791. bit 3, a
  6792. jr nz, LABEL_415A
  6793. ld a, ($D440)
  6794. cp $02
  6795. jp z, LABEL_415F
  6796. ret
  6797.  
  6798. LABEL_415A:
  6799. ld (ix+$02), $15
  6800. ret
  6801.  
  6802. LABEL_415F:
  6803. ld e, (ix+$16)
  6804. ld d, (ix+$17)
  6805. ld l, e
  6806. ld h, d
  6807. ld bc, $01C0
  6808. xor a
  6809. sbc hl, bc
  6810. jr c, +
  6811. ld de, $01C0
  6812. +: ld l, (ix+$18)
  6813. ld h, (ix+$19)
  6814. add hl, de
  6815. srl h
  6816. rr l
  6817. bit 7, h
  6818. jr nz, +
  6819. ex de, hl
  6820. ld hl, $0000
  6821. xor a
  6822. sbc hl, de
  6823. ld de, $0080
  6824. add hl, de
  6825. jr c, +
  6826. ld (ix+$18), l
  6827. ld (ix+$19), h
  6828. +: ld (ix+$02), $14
  6829. ret
  6830.  
  6831. LABEL_4199:
  6832. res 7, (ix+$04)
  6833. call LABEL_41A6
  6834. call LABEL_6BF2
  6835. jp LABEL_40E0
  6836.  
  6837. LABEL_41A6:
  6838. call Engine_UpdateObjectPosition
  6839. call LABEL_4226
  6840. ld a, ($D137)
  6841. bit 3, a
  6842. jr nz, +
  6843. ld a, ($D440)
  6844. cp $02
  6845. ret z
  6846. +: ld de, $0008
  6847. ld bc, $0400
  6848. call Engine_SetObjectVerticalSpeed
  6849. ld a, ($D137)
  6850. bit 2, a
  6851. ret nz
  6852. ld a, (ix+$19)
  6853. bit 7, a
  6854. jr z, +
  6855. ld hl, $0000
  6856. ld (ix+$18), l
  6857. ld (ix+$19), h
  6858. +: ld (ix+$02), $13
  6859. ret
  6860.  
  6861. LABEL_41DD:
  6862. res 7, (ix+$04)
  6863. call LABEL_41EA
  6864. call LABEL_6BF2
  6865. jp LABEL_40E0
  6866.  
  6867. LABEL_41EA:
  6868. call LABEL_4226
  6869. ld de, $0010
  6870. ld bc, $0400
  6871. call Engine_SetObjectVerticalSpeed
  6872. call Engine_UpdateObjectPosition
  6873. ld a, ($D137)
  6874. bit 3, a
  6875. ret nz
  6876. ld (ix+$02), $13
  6877. ret
  6878.  
  6879. LABEL_4204:
  6880. ld l, (ix+$16)
  6881. ld h, (ix+$17)
  6882. ld de, $0006
  6883. add hl, de
  6884. ld e, l
  6885. ld d, h
  6886. bit 7, h
  6887. jr nz, +
  6888. ld bc, $0280
  6889. xor a
  6890. sbc hl, bc
  6891. jr c, +
  6892. ld de, $0280
  6893. +: ld (ix+$16), e
  6894. ld (ix+$17), d
  6895. ret
  6896.  
  6897. LABEL_4226:
  6898. ld l, (ix+$16)
  6899. ld h, (ix+$17)
  6900. ld de, $000A
  6901. xor a
  6902. sbc hl, de
  6903. ld e, l
  6904. ld d, h
  6905. bit 7, h
  6906. jr nz, +
  6907. ld bc, $0060
  6908. xor a
  6909. sbc hl, bc
  6910. jr nc, ++
  6911. +: ld de, $0060
  6912. ++: ld (ix+$16), e
  6913. ld (ix+$17), d
  6914. ret
  6915.  
  6916. LABEL_424A:
  6917. call LABEL_376E
  6918. res 7, (ix+$04)
  6919. ld a, ($D137)
  6920. and $0C
  6921. jr z, +
  6922. ld de, $0010
  6923. and $08
  6924. jr nz, ++
  6925. ld de, $FFF0
  6926. ++: ld hl, ($D3C7)
  6927. add hl, de
  6928. ld e, l
  6929. ld d, h
  6930. ld bc, $0300
  6931. xor a
  6932. sbc hl, bc
  6933. jr nc, ++
  6934. ld de, $0300
  6935. ++: ld l, e
  6936. ld h, d
  6937. ld bc, $07FF
  6938. xor a
  6939. sbc hl, bc
  6940. jr c, ++
  6941. ld de, $07FF
  6942. ++: ld ($D3C7), de
  6943. +: ld a, ($D3C6)
  6944. ld b, a
  6945. ld a, ($D3C8)
  6946. add a, b
  6947. ld ($D3C6), a
  6948. call LABEL_42B7
  6949. ld a, ($D147)
  6950. and $30
  6951. ret z
  6952. xor a
  6953. ld ($D3CD), a
  6954. ld (ix+$02), $0B
  6955. ld a, ($D3C6)
  6956. add a, $40
  6957. ld (ix+$0A), a
  6958. ld a, ($D3C8)
  6959. add a, a
  6960. add a, a
  6961. add a, a
  6962. add a, a
  6963. add a, a
  6964. ld (ix+$0B), a
  6965. call LABEL_64D4
  6966. ret
  6967.  
  6968. LABEL_42B7:
  6969. ld a, ($D3C6)
  6970. ld c, a
  6971. ld b, $00
  6972. ld hl, DATA_330
  6973. add hl, bc
  6974. ld a, (hl)
  6975. and a
  6976. jp m, LABEL_42E8
  6977. ld e, $50
  6978. ld hl, $D3CA
  6979. bit 7, (hl)
  6980. jr z, +
  6981. ld e, $38
  6982. +: ld hl, $0000
  6983. ld d, $00
  6984. ld b, $08
  6985. -: rrca
  6986. jr nc, +
  6987. add hl, de
  6988. +: sla e
  6989. rl d
  6990. djnz -
  6991. ld l, h
  6992. ld h, $00
  6993. jp LABEL_4310
  6994.  
  6995. LABEL_42E8:
  6996. neg
  6997. ld e, $50
  6998. ld hl, $D3CA
  6999. bit 7, (hl)
  7000. jr z, +
  7001. ld e, $38
  7002. +: ld hl, $0000
  7003. ld d, $00
  7004. ld b, $08
  7005. -: rrca
  7006. jr nc, +
  7007. add hl, de
  7008. +: sla e
  7009. rl d
  7010. djnz -
  7011. ld de, $0000
  7012. ex de, hl
  7013. xor a
  7014. sbc hl, de
  7015. ld l, h
  7016. ld h, $FF
  7017. LABEL_4310:
  7018. ld de, ($D3C9)
  7019. ld a, d
  7020. and $7F
  7021. ld d, a
  7022. add hl, de
  7023. ld (ix+$11), l
  7024. ld (ix+$12), h
  7025. ld a, ($D3C6)
  7026. add a, $C0
  7027. ld c, a
  7028. ld b, $00
  7029. ld hl, DATA_330
  7030. add hl, bc
  7031. ld a, (hl)
  7032. and a
  7033. jp m, LABEL_4352
  7034. ld e, $50
  7035. ld hl, $D3CA
  7036. bit 7, (hl)
  7037. jr z, +
  7038. ld e, $38
  7039. +: ld hl, $0000
  7040. ld d, $00
  7041. ld b, $08
  7042. -: rrca
  7043. jr nc, +
  7044. add hl, de
  7045. +: sla e
  7046. rl d
  7047. djnz -
  7048. ld l, h
  7049. ld h, $00
  7050. jp LABEL_437A
  7051.  
  7052. LABEL_4352:
  7053. neg
  7054. ld e, $50
  7055. ld hl, $D3CA
  7056. bit 7, (hl)
  7057. jr z, +
  7058. ld e, $38
  7059. +: ld hl, $0000
  7060. ld d, $00
  7061. ld b, $08
  7062. -: rrca
  7063. jr nc, +
  7064. add hl, de
  7065. +: sla e
  7066. rl d
  7067. djnz -
  7068. ld de, $0000
  7069. ex de, hl
  7070. xor a
  7071. sbc hl, de
  7072. ld l, h
  7073. ld h, $FF
  7074. LABEL_437A:
  7075. ld de, ($D3CB)
  7076. add hl, de
  7077. ld de, $000C
  7078. add hl, de
  7079. ld (ix+$14), l
  7080. ld (ix+$15), h
  7081. ret
  7082.  
  7083. LABEL_438A:
  7084. res 7, (ix+$04)
  7085. ld a, ($D3C4)
  7086. ld b, a
  7087. ld a, ($D365)
  7088. ld ($D3C4), a
  7089. cp b
  7090. jr z, +
  7091. xor a
  7092. ld ($D3C5), a
  7093. +: ld a, ($D365)
  7094. sub $30
  7095. ld b, a
  7096. jp c, LABEL_43AF
  7097. cp $10
  7098. jr nc, LABEL_43AF
  7099. jp LABEL_43BF
  7100.  
  7101. LABEL_43AF:
  7102. ld a, ($D367)
  7103. sub $30
  7104. ld b, a
  7105. jp c, LABEL_43F9
  7106. cp $10
  7107. jr nc, LABEL_43F9
  7108. jp LABEL_43C5
  7109.  
  7110. LABEL_43BF:
  7111. ld a, ($D3C5)
  7112. or a
  7113. jr nz, LABEL_43F3
  7114. LABEL_43C5:
  7115. ld a, b
  7116. add a, a
  7117. ld e, a
  7118. ld d, $00
  7119. ld hl, DATA_43D3
  7120. add hl, de
  7121. ld a, (hl)
  7122. inc hl
  7123. ld h, (hl)
  7124. ld l, a
  7125. jp (hl)
  7126.  
  7127. DATA_43D3:
  7128. .dw LABEL_4414
  7129. .dw LABEL_441A
  7130. .dw LABEL_45A8
  7131. .dw LABEL_45C6
  7132. .dw LABEL_45E4
  7133. .dw LABEL_4602
  7134. .dw LABEL_4420
  7135. .dw LABEL_4482
  7136. .dw LABEL_4546
  7137. .dw LABEL_44E4
  7138. .dw LABEL_4620
  7139. .dw LABEL_4408
  7140. .dw LABEL_440E
  7141. .dw LABEL_4623
  7142. .dw LABEL_4629
  7143. .dw LABEL_4408
  7144.  
  7145. LABEL_43F3:
  7146. call Engine_UpdateObjectPosition
  7147. jp LABEL_6BF2
  7148.  
  7149. LABEL_43F9:
  7150. ld a, SFX_LeaveTube
  7151. ld ($DD04), a
  7152. xor a
  7153. ld ($D3A8), a
  7154. ld (ix+$21), a
  7155. jp Player_SetState_JumpFromRamp
  7156.  
  7157. LABEL_4408
  7158. call LABEL_468D
  7159. jp LABEL_43F3
  7160.  
  7161. LABEL_440E:
  7162. call LABEL_468D
  7163. jp LABEL_43F3
  7164.  
  7165. LABEL_4414:
  7166. call LABEL_469D
  7167. jp LABEL_43F3
  7168.  
  7169. LABEL_441A:
  7170. call LABEL_468D
  7171. jp LABEL_43F3
  7172.  
  7173. LABEL_4420:
  7174. ld a, (ix+$17)
  7175. or a
  7176. jr z, LABEL_445F
  7177. bit 7, (ix+$17)
  7178. jp z, LABEL_4446
  7179. call LABEL_462F
  7180. jp nc, LABEL_43F3
  7181. ld a, ($D137)
  7182. bit 0, a
  7183. jr nz, LABEL_4440
  7184. call LABEL_465D
  7185. jp LABEL_43F3
  7186.  
  7187. LABEL_4440:
  7188. call LABEL_467D
  7189. jp LABEL_43F3
  7190.  
  7191. LABEL_4446:
  7192. call LABEL_4637
  7193. jp nc, LABEL_43F3
  7194. ld a, ($D137)
  7195. bit 0, a
  7196. jr nz, LABEL_4459
  7197. call LABEL_464D
  7198. jp LABEL_43F3
  7199.  
  7200. LABEL_4459:
  7201. call LABEL_467D
  7202. jp LABEL_43F3
  7203.  
  7204. LABEL_445F:
  7205. call LABEL_4640
  7206. jp nc, LABEL_43F3
  7207. ld a, ($D137)
  7208. bit 3, a ;right button
  7209. jr nz, LABEL_4476
  7210. bit 2, a ;left button
  7211. jr nz, LABEL_447C
  7212. call LABEL_467D
  7213. jp LABEL_43F3
  7214.  
  7215. LABEL_4476:
  7216. call LABEL_464D
  7217. jp LABEL_43F3
  7218.  
  7219.  
  7220. LABEL_447C:
  7221. call LABEL_465D
  7222. jp LABEL_43F3
  7223.  
  7224. LABEL_4482:
  7225. ld a, (ix+$17)
  7226. or a
  7227. jr z, LABEL_44C1
  7228. bit 7, (ix+$17)
  7229. jp z, LABEL_44A8
  7230. call LABEL_462F
  7231. jp nc, LABEL_43F3
  7232. ld a, ($D137)
  7233. bit 1, a
  7234. jr nz, LABEL_44A2
  7235. call LABEL_465D
  7236. jp LABEL_43F3
  7237.  
  7238. LABEL_44A2:
  7239. call LABEL_466D
  7240. jp LABEL_43F3
  7241.  
  7242. LABEL_44A8:
  7243. call LABEL_4637
  7244. jp nc, LABEL_43F3
  7245. ld a, ($D137)
  7246. bit 1, a
  7247. jr nz, LABEL_44BB
  7248. call LABEL_464D
  7249. jp LABEL_43F3
  7250.  
  7251. LABEL_44BB:
  7252. call LABEL_466D
  7253. jp LABEL_43F3
  7254. LABEL_44C1:
  7255. call LABEL_464B
  7256. jp nc, LABEL_43F3
  7257. ld a, ($D137)
  7258. bit 3, a
  7259. jr nz, LABEL_44D8
  7260. bit 2, a
  7261. jr nz, LABEL_44DE
  7262. call LABEL_466D
  7263. jp LABEL_43F3
  7264.  
  7265. LABEL_44D8:
  7266. call LABEL_464D
  7267. jp LABEL_43F3
  7268.  
  7269. LABEL_44DE:
  7270. call LABEL_465D
  7271. jp LABEL_43F3
  7272.  
  7273. LABEL_44E4:
  7274. ld a, (ix+$17)
  7275. or a
  7276. jr nz, LABEL_4523
  7277. bit 7, (ix+$19)
  7278. jp z, LABEL_450A
  7279. call LABEL_464B
  7280. jp nc, LABEL_43F3
  7281. ld a, ($D137)
  7282. bit 2, a
  7283. jr nz, LABEL_4504
  7284. call LABEL_467D
  7285. jp LABEL_43F3
  7286.  
  7287. LABEL_4504:
  7288. call LABEL_465D
  7289. jp LABEL_43F3
  7290.  
  7291. LABEL_450A:
  7292. call LABEL_4640
  7293. jp nc, LABEL_43F3
  7294. ld a, ($D137)
  7295. bit 2, a
  7296. jr nz, LABEL_451D
  7297. call LABEL_466D
  7298. jp LABEL_43F3
  7299.  
  7300. LABEL_451D:
  7301. call LABEL_465D
  7302. jp LABEL_43F3
  7303.  
  7304. LABEL_4523:
  7305. call LABEL_4637
  7306. jp nc, LABEL_43F3
  7307. ld a, ($D137)
  7308. bit 0, a
  7309. jr nz, LABEL_453A
  7310. bit 1, a
  7311. jr nz, LABEL_4540
  7312. call LABEL_465D
  7313. jp LABEL_43F3
  7314.  
  7315. LABEL_453A:
  7316. call LABEL_467D
  7317. jp LABEL_43F3
  7318.  
  7319. LABEL_4540:
  7320. call LABEL_466D
  7321. jp LABEL_43F3
  7322.  
  7323. LABEL_4546:
  7324. ld a, (ix+$17)
  7325. or a
  7326. jr nz, LABEL_4585
  7327. bit 7, (ix+$19)
  7328. jp z, LABEL_456C
  7329. call LABEL_464B
  7330. jp nc, LABEL_43F3
  7331. ld a, ($D137)
  7332. bit 3, a
  7333. jr nz, LABEL_4566
  7334. call LABEL_467D
  7335. jp LABEL_43F3
  7336.  
  7337. LABEL_4566:
  7338. call LABEL_464D
  7339. jp LABEL_43F3
  7340.  
  7341. LABEL_456C:
  7342. call LABEL_4640
  7343. jp nc, LABEL_43F3
  7344. ld a, ($D137)
  7345. bit 3, a
  7346. jr nz, LABEL_457F
  7347. call LABEL_466D
  7348. jp LABEL_43F3
  7349.  
  7350. LABEL_457F:
  7351. call LABEL_464D
  7352. jp LABEL_43F3
  7353.  
  7354. LABEL_4585:
  7355. call LABEL_462F
  7356. jp nc, LABEL_43F3
  7357. ld a, ($D137)
  7358. bit 0, a
  7359. jr nz, LABEL_459C
  7360. bit 1, a
  7361. jr nz, LABEL_45A2
  7362. call LABEL_464D
  7363. jp LABEL_43F3
  7364.  
  7365. LABEL_459C:
  7366. call LABEL_467D
  7367. jp LABEL_43F3
  7368.  
  7369. LABEL_45A2:
  7370. call LABEL_466D
  7371. jp LABEL_43F3
  7372.  
  7373. LABEL_45A8:
  7374. ld a, (ix+$17)
  7375. or a
  7376. jr nz, LABEL_45BA
  7377. call LABEL_464B
  7378. jp nc, LABEL_43F3
  7379. call LABEL_464D
  7380. jp LABEL_43F3
  7381.  
  7382. LABEL_45BA:
  7383. call LABEL_462F
  7384. jp nc, LABEL_43F3
  7385. call LABEL_466D
  7386. jp LABEL_43F3
  7387.  
  7388. LABEL_45C6:
  7389. ld a, (ix+$17)
  7390. or a
  7391. jr nz, LABEL_45D8
  7392. call LABEL_464B
  7393. jp nc, LABEL_43F3
  7394. call LABEL_465D
  7395. jp LABEL_43F3
  7396.  
  7397. LABEL_45D8:
  7398. call LABEL_4637
  7399. jp nc, LABEL_43F3
  7400. call LABEL_466D
  7401. jp LABEL_43F3
  7402.  
  7403. LABEL_45E4:
  7404. ld a, (ix+$17)
  7405. or a
  7406. jr nz, LABEL_45F6
  7407. call LABEL_4640
  7408. jp nc, LABEL_43F3
  7409. call LABEL_464D
  7410. jp LABEL_43F3
  7411.  
  7412. LABEL_45F6:
  7413. call LABEL_462F
  7414. jp nc, LABEL_43F3
  7415. call LABEL_467D
  7416. jp LABEL_43F3
  7417.  
  7418. LABEL_4602:
  7419. ld a, (ix+$17)
  7420. or a
  7421. jr nz, LABEL_4614
  7422. call LABEL_4640
  7423. jp nc, LABEL_43F3
  7424. call LABEL_465D
  7425. jp LABEL_43F3
  7426.  
  7427.  
  7428. LABEL_4614:
  7429. call LABEL_4637
  7430. jp nc, LABEL_43F3
  7431. call LABEL_467D
  7432. jp LABEL_43F3
  7433.  
  7434. LABEL_4620:
  7435. jp LABEL_43F3
  7436.  
  7437. LABEL_4623:
  7438. call LABEL_469D
  7439. jp LABEL_43F3
  7440.  
  7441. LABEL_4629:
  7442. call LABEL_469D
  7443. jp LABEL_43F3
  7444.  
  7445. LABEL_462F:
  7446. ld a, (ix+$11)
  7447. and $1F
  7448. cp $0E
  7449. ret
  7450.  
  7451. LABEL_4637:
  7452. ld a, (ix+$11)
  7453. and $1F
  7454. cp $0E
  7455. ccf
  7456. ret
  7457.  
  7458. LABEL_4640:
  7459. ld a, (ix+$14)
  7460. sub $0E
  7461. and $1F
  7462. cp $14
  7463. ccf
  7464. ret
  7465.  
  7466. LABEL_464B:
  7467. scf
  7468. ret
  7469.  
  7470. LABEL_464D:
  7471. call LABEL_469D
  7472. ld hl, $0000
  7473. ld ($D518), hl
  7474. ld hl, $0600
  7475. ld ($D516), hl
  7476. ret
  7477.  
  7478. LABEL_465D:
  7479. call LABEL_469D
  7480. ld hl, $0000
  7481. ld ($D518), hl
  7482. ld hl, $FA00
  7483. ld ($D516), hl
  7484. ret
  7485.  
  7486. LABEL_466D:
  7487. call LABEL_468D
  7488. ld hl, $0600
  7489. ld ($d518), hl
  7490. ld hl, $0000
  7491. ld ($d516), hl
  7492. ret
  7493.  
  7494. LABEL_467D:
  7495. call LABEL_468D
  7496. ld hl, $FA00
  7497. ld ($D518), hl
  7498. ld hl, $0000
  7499. ld ($D516), hl
  7500. ret
  7501.  
  7502. LABEL_468D:
  7503. ld a, (ix+$11)
  7504. and $E0
  7505. add a, $0E
  7506. ld (ix+$11), a
  7507. ld a, $FF
  7508. ld ($D3C5), a
  7509. ret
  7510.  
  7511. LABEL_469D:
  7512. ld l, (ix+$14)
  7513. ld h, (ix+$15)
  7514. ld de, $FFF0
  7515. add hl, de
  7516. ld a, l
  7517. and $E0
  7518. ld l, a
  7519. ld de, $002A
  7520. add hl, de
  7521. ld (ix+$14), l
  7522. ld (ix+$15), h
  7523. ld a, $FF
  7524. ld ($D3C5), a
  7525. ret
  7526.  
  7527. LABEL_46BB:
  7528. ld (ix+$02), $17
  7529. ret
  7530.  
  7531. ;called on initial collision with mine cart
  7532. LABEL_46C0:
  7533. res 7, (ix+$04)
  7534. ld hl, $D3A0
  7535. ld (hl), $00
  7536. inc hl
  7537. ld (hl), $02
  7538. inc hl
  7539. ld (hl), $04
  7540. inc hl
  7541. ld (hl), $6A
  7542. inc hl
  7543. ld (hl), $6C
  7544. inc hl
  7545. ld (hl), $6E
  7546. inc hl
  7547. ld (hl), $70
  7548. inc hl
  7549. ld (hl), $72
  7550. ld iy, ($D39E)
  7551. call LABEL_4727
  7552. set 7, (iy+$04)
  7553. ret
  7554.  
  7555. ;called once per frame when in a mine cart
  7556. LABEL_46EA:
  7557. ld iy, ($D39E)
  7558. call LABEL_4727
  7559. call LABEL_7378
  7560. call LABEL_376E
  7561. ld a, (ix+$02)
  7562. cp $1E
  7563. jr z, +
  7564. res 0, (ix+$03)
  7565. ld a, ($D137)
  7566. and $30
  7567. ret z
  7568. +: ld iy, ($D39E)
  7569. res 7, (iy+$04)
  7570. ld (iy+$1F), $10
  7571. set 6, (iy+$03)
  7572. ld a, (iy+$3F)
  7573. ld (iy+$02), a
  7574. ld hl, $0000
  7575. ld ($D39E), hl
  7576. jp Player_SetState_Jumping
  7577.  
  7578. LABEL_4727:
  7579. ld l, (iy+$11)
  7580. ld h, (iy+$12)
  7581. ld ($D511), hl
  7582. ld l, (iy+$14)
  7583. ld h, (iy+$15)
  7584. ld ($D514), hl
  7585. ld l, (iy+$16)
  7586. ld h, (iy+$17)
  7587. ld ($D516), hl
  7588. ld l, (iy+$18)
  7589. ld h, (iy+$19)
  7590. ld ($D518), hl
  7591. ld hl, $D503
  7592. set 1, (hl)
  7593. call LABEL_7102
  7594. ld a, (iy+$3F)
  7595. cp $02
  7596. jr z, LABEL_475F
  7597. set 4, (ix+$04)
  7598. ret
  7599.  
  7600. LABEL_475F:
  7601. res 4, (ix+$04)
  7602. ret
  7603.  
  7604. ;********************************************
  7605. ;* UGZ Mine Cart input handling routines *
  7606. ;********************************************
  7607. Player_MineCart_Handle:
  7608. LABEL_4764:
  7609. ld a, ($D137)
  7610. ld de, MineCart_LookingUp
  7611. bit 0, a ;check for up button
  7612. jr nz, +
  7613. ld de, MineCart_LookingDown
  7614. bit 1, a ;check for down button
  7615. jr nz, +
  7616. ld de, MineCart_LookingAhead
  7617. +: ld a, (ix+$1E) ;frame number
  7618. bit 7, (ix+$17)
  7619. jr nz, MineCart_UpdateAnimation
  7620. inc a
  7621. jr MineCart_SetSprite
  7622.  
  7623. MineCart_UpdateAnimation: ;4784
  7624. dec a ;next frame
  7625. MineCart_SetSprite: ;4785
  7626. and $03 ;cap at 4 frames
  7627. ld (ix+$1E), a ;store frame number
  7628. add a, a ;calculate offset
  7629. add a, (ix+$1E)
  7630. ld l, a
  7631. ld h, $00
  7632. add hl, de
  7633. ld a, (hl) ;sprite state?
  7634. ld (ix+$06), a
  7635. inc hl
  7636. ld a, (hl) ;wheel left
  7637. ld ($D3A6), a
  7638. inc hl
  7639. ld a, (hl) ;wheel right
  7640. ld ($D3A7), a
  7641. ld (ix+$07), $03 ;?
  7642. ret
  7643.  
  7644. ; Wheel (left)
  7645. ; ? | Wheel (right)
  7646. ; |----|----|----|
  7647. MineCart_LookingAhead:
  7648. .db $59, $70, $72
  7649. .db $59, $74, $76
  7650. .db $5A, $78, $7A
  7651. .db $5A, $7C, $7E
  7652.  
  7653. MineCart_LookingUp:
  7654. .db $5B, $70, $72
  7655. .db $5B, $74, $76
  7656. .db $5B, $78, $7A
  7657. .db $5B, $7C, $7E
  7658.  
  7659. MineCart_LookingDown:
  7660. .db $5C, $70, $72
  7661. .db $5C, $74, $76
  7662. .db $5C, $78, $7A
  7663. .db $5C, $7C, $7E
  7664.  
  7665.  
  7666. ;********************************************
  7667. ;* Power-up monitor collision routines. *
  7668. ;********************************************
  7669. LABEL_47C9:
  7670. ld a, ($D532) ;check for power-up
  7671. or a
  7672. jr z, LABEL_47F6
  7673. ld hl, ($D4A0) ;increment counter
  7674. inc hl
  7675. ld ($D4A0), hl
  7676. ld bc, $04B0 ;check to see if timer has expired
  7677. xor a
  7678. sbc hl, bc
  7679. jr c, LABEL_47F6 ;jump if timer hasn't expired
  7680. xor a ;reset power-up
  7681. ld ($D532), a
  7682. ld hl, $0000
  7683. ld ($D4A0), hl
  7684. ld a, ($D4A2)
  7685. or a
  7686. ret nz
  7687. ld a, ($D293)
  7688. and $BF
  7689. ret nz
  7690. jp Engine_ChangeLevelMusic
  7691.  
  7692. Collision_Monitor:
  7693. LABEL_47F6: ;collision with monitor
  7694. ld hl, $D39D
  7695. ld a, (hl)
  7696. bit 0, a
  7697. jr nz, Collision_Monitor_Rings
  7698. bit 1, a
  7699. jr nz, Collision_Monitor_Life
  7700. bit 4, a
  7701. jr nz, Collision_Monitor_Continue
  7702. bit 5, a
  7703. jr nz, LABEL_4845
  7704. bit 2, a
  7705. jr nz, Collision_Monitor_Sneakers
  7706. bit 3, a
  7707. jr nz, Collision_Monitor_Invincibility
  7708. bit 6, a
  7709. jr nz, LABEL_4884
  7710. ret
  7711.  
  7712. Collision_Monitor_Rings: ;4817
  7713. res 0, (hl)
  7714. ld a, (RingCounter)
  7715. add a, $10 ;add 10 rings to the counter (bcd)
  7716. daa
  7717. ld (RingCounter), a
  7718. ld a, SFX_10Rings
  7719. ld ($DD04), a
  7720. jp LABEL_25DB
  7721.  
  7722. Collision_Monitor_Life: ;482A
  7723. res 1, (hl)
  7724. ld a, ($D298)
  7725. inc a
  7726. ld (LifeCounter), a
  7727. ld a, SFX_ExtraLife
  7728. ld ($DD04), a
  7729. jp LABEL_25AC
  7730.  
  7731. Collision_Monitor_Continue:
  7732. LABEL_483B:
  7733. res 4, (hl)
  7734. ld a, ($D2BD) ;used by continue screen
  7735. inc a
  7736. ld ($D2BD), a
  7737. ret
  7738.  
  7739. LABEL_4845:
  7740. res 5, (hl)
  7741. ret
  7742.  
  7743. Collision_Monitor_Sneakers: ;4848
  7744. res 2, (hl)
  7745. ld a, Music_SpeedSneakers
  7746. ld ($DD04), a
  7747. ld a, $01 ;power-up type = speed sneakers
  7748. ld ($D532), a
  7749. ld hl, $0000 ;timer (count up)
  7750. ld ($D4A0), hl
  7751. ld c, Object_SpeedShoesStar
  7752. ld h, $00 ;set up first star sprite
  7753. call LABEL_6144
  7754. ld h, $01 ;set up second star sprite
  7755. jp LABEL_6144
  7756.  
  7757. Collision_Monitor_Invincibility:
  7758. LABEL_4866:
  7759. res 3, (hl)
  7760. ld a, Music_Invincibility
  7761. ld ($DD04), a
  7762. ld a, $02 ;power-up type = invincibility
  7763. ld ($D532), a
  7764. ld hl, $0000
  7765. ld ($D4A0), hl ;timer
  7766. ld c, Object_InvincibilityStar ;show the spinning stars
  7767. ld h, $00
  7768. call LABEL_6144
  7769. ld h, $01 ;causes the second sprite to be opposite the first
  7770. jp LABEL_6144
  7771.  
  7772.  
  7773. LABEL_4884:
  7774. res 6, (hl)
  7775. ret
  7776.  
  7777. LABEL_4887:
  7778. call LABEL_48F0 ;update the "UnderWater" flag for the current act
  7779. ld a, (PlayerUnderwater)
  7780. or a
  7781. jr nz, Engine_WaterLevel_IncAirTimer
  7782. xor a
  7783. ld ($D469), a
  7784. ret
  7785.  
  7786. Engine_WaterLevel_IncAirTimer:
  7787. LABEL_4895:
  7788. ld hl, $D468 ;air timer
  7789. inc (hl)
  7790. ld a, (hl)
  7791. cp $78
  7792. ret c
  7793. ld (hl), $00
  7794. call LABEL_48B8
  7795. ld hl, $D469
  7796. inc (hl)
  7797. ld a, (hl)
  7798. cp $0B
  7799. jr z, LABEL_48B0 ;create the countdown sprite
  7800. cp $11
  7801. jr z, LABEL_48B7
  7802. ret
  7803.  
  7804. LABEL_48B0:
  7805. ld c, $09 ;countdown sprite number
  7806. ld h, $00
  7807. jp LABEL_6144 ;allocate a slot for the sprite
  7808.  
  7809.  
  7810. LABEL_48B7:
  7811. ret
  7812.  
  7813. LABEL_48B8:
  7814. ld a, r
  7815. and $04
  7816. ret nz
  7817. ld c, $0A
  7818. ld h, $03
  7819. jp LABEL_6144
  7820.  
  7821. LABEL_48C4:
  7822. ld a, (PlayerUnderwater)
  7823. or a
  7824. ret z
  7825. ld hl, ($D36D)
  7826. srl h
  7827. rr l
  7828. ld ($D36D), hl
  7829. ret
  7830.  
  7831. LABEL_48D4:
  7832. ret
  7833.  
  7834. LABEL_48D5:
  7835. ld a, (PlayerUnderwater)
  7836. or a
  7837. ret z
  7838. inc (ix+$30)
  7839. ld a, (ix+$30)
  7840. and $03
  7841. ret z
  7842. ld de, $0000
  7843. ld ($D36F), de
  7844. ld ($D371), de
  7845. pop de
  7846. ret
  7847.  
  7848. LABEL_48F0:
  7849. ld a, (CurrentLevel)
  7850. cp $02
  7851. ret nz
  7852. ld a, (CurrentAct)
  7853. or a
  7854. jr z, LABEL_4944
  7855. dec a
  7856. jr z, Engine_UpdateUnderWaterFlag ;update for ALZ-2
  7857. ld hl, ($D511)
  7858. ld bc, $0880
  7859. xor a
  7860. sbc hl, bc
  7861. jr c, LABEL_4917 ;update for ALZ-3 (part 1)
  7862. ld hl, ($D511)
  7863. ld bc, $0900
  7864. xor a
  7865. sbc hl, bc
  7866. jr c, LABEL_4926 ;update for ALZ-3 (part 2)
  7867. jr LABEL_4935 ;update for ALZ-3 (part 3)
  7868.  
  7869.  
  7870. LABEL_4917:
  7871. ld hl, ($D514)
  7872. ld bc, $0120
  7873. xor a
  7874. sbc hl, bc
  7875. jp c, Engine_ClearUnderWater
  7876. jp Engine_SetUnderWater
  7877.  
  7878. LABEL_4926:
  7879. ld hl, ($D514)
  7880. ld bc, $0160
  7881. xor a
  7882. sbc hl, bc
  7883. jp c, Engine_ClearUnderWater
  7884. jp Engine_SetUnderWater
  7885.  
  7886. LABEL_4935
  7887. ld hl, ($D514)
  7888. ld bc, $0140
  7889. xor a
  7890. sbc hl, bc
  7891. jp c, Engine_ClearUnderWater
  7892. jp Engine_SetUnderWater
  7893.  
  7894. LABEL_4944: ;handle water level for ALZ-1
  7895. ld hl, ($D511)
  7896. ld bc, $0650
  7897. xor a
  7898. sbc hl, bc
  7899. jr c, LABEL_4951
  7900. jr LABEL_4960
  7901.  
  7902. LABEL_4951:
  7903. ld hl, ($D514)
  7904. ld bc, $01C0
  7905. xor a
  7906. sbc hl, bc
  7907. jp c, Engine_ClearUnderWater
  7908. jp Engine_SetUnderWater
  7909.  
  7910. LABEL_4960
  7911. ld hl, ($D514)
  7912. ld bc, $0220
  7913. xor a
  7914. sbc hl, bc
  7915. jp c, Engine_ClearUnderWater
  7916. jp Engine_SetUnderWater
  7917.  
  7918. Engine_UpdateUnderWaterFlag: ;$496F
  7919. ld hl, ($D514) ;check whether the player is below the
  7920. ld bc, ($D4A4) ;zone's water level
  7921. xor a
  7922. sbc hl, bc
  7923. jp c, Engine_ClearUnderWater
  7924. jp Engine_SetUnderWater
  7925.  
  7926.  
  7927. Engine_ClearUnderWater:
  7928. xor a
  7929. ld (PlayerUnderwater), a
  7930. ret
  7931.  
  7932. Engine_SetUnderWater:
  7933. ld a, $FF
  7934. ld (PlayerUnderwater), a
  7935. ret
  7936.  
  7937. Engine_UpdateCameraPos:
  7938. LABEL_498A:
  7939. ld ix, $D15E
  7940. bit 7, (ix+$00)
  7941. ret z
  7942. call LABEL_5D93
  7943. ld a, ($D162) ;bank with 32x32 mappings
  7944. call Engine_SwapFrame2
  7945. call Engine_UpdateCameraXPos
  7946. call Engine_UpdateCameraYPos
  7947. call Engine_CalculateBgScroll
  7948. set 6, (ix+$00)
  7949. ret
  7950.  
  7951. ;enable camera movement
  7952. LABEL_49AA:
  7953. ld hl, $D15E
  7954. set 7, (hl)
  7955. ret
  7956.  
  7957. ;lock camera movement
  7958. LABEL_49B0:
  7959. ld hl, $D15E
  7960. res 7, (hl)
  7961. ld hl, ($D174)
  7962. ld ($D280), hl
  7963. ret
  7964.  
  7965. LABEL_49BC:
  7966. ld hl, $D15E
  7967. set 7, (hl) ;lock camera
  7968. ld hl, $D15F
  7969. set 0, (hl) ;update camera pos
  7970. ld ($D2CE), bc ;horizontal camera offset
  7971. ld ($D2D0), de ;vertical camera offset
  7972. ret
  7973.  
  7974. LABEL_49CF:
  7975. ld hl, $D15E
  7976. set 7, (hl)
  7977. ld hl, $D15F
  7978. res 0, (hl)
  7979. ret
  7980.  
  7981. Engine_SetMinimumCameraX: ;$49DA
  7982. ld hl, ($D280) ;minimum camera X pos
  7983. ld de, ($D174) ;current horiz. cam position?
  7984. xor a
  7985. sbc hl, de
  7986. ret nc
  7987. ld ($D280), de
  7988. ret
  7989.  
  7990. Engine_SetMaximumCameraX: ;$49EA
  7991. ld hl, ($D282) ;maximum camera x pos
  7992. ld de, ($D174) ;current horiz. cam position?
  7993. xor a
  7994. sbc hl, de
  7995. ret c
  7996. ld ($D282), de
  7997. ret
  7998.  
  7999.  
  8000. ;update camera horizontal position
  8001. Engine_UpdateCameraXPos: ;$49FA
  8002. ld hl, ($D284) ;horiz sprite offset?
  8003. ld de, ($D280) ;minimum camera X pos
  8004. xor a
  8005. sbc hl, de
  8006. jr c, Engine_UpdateCameraXPos_Limit ;limit camera to minimum x pos
  8007. ld hl, ($D284)
  8008. ld de, ($D282) ;level width
  8009. xor a
  8010. sbc hl, de
  8011. jr nc, Engine_UpdateCameraXPos_Limit ;limit camera to level width
  8012. bit 3, (ix+$00)
  8013. jr nz, + ;FIXME: this seems a bit pointless. This subroutine is identical.
  8014.  
  8015. ld a, ($D174)
  8016. ld b, a
  8017. ld a, ($D284)
  8018. xor b
  8019. jp nz, Engine_LoadMappings32_Column ;load a column of tiles from the 32x32 mappings
  8020. ret
  8021.  
  8022. +: ld a, ($D174)
  8023. ld b, a
  8024. ld a, ($D284)
  8025. xor b
  8026. jp nz, Engine_LoadMappings32_Column
  8027. ret
  8028.  
  8029. ;limit the camera position
  8030. Engine_UpdateCameraXPos_Limit:
  8031. ld hl, ($D174)
  8032. ld ($D284), hl
  8033. ret
  8034.  
  8035.  
  8036. Engine_UpdateCameraYPos: ;$4A37
  8037. ld hl, ($D286) ;vertical offset
  8038. ld de, ($D27C) ;minimum camera y pos
  8039. xor a
  8040. sbc hl, de
  8041. jr c, Engine_UpdateCameraYPos_Limit ;limit camera to minimum y position
  8042. ld hl, ($D286)
  8043. ld de, ($D27E) ;level height
  8044. xor a
  8045. sbc hl, de
  8046. jr nc, Engine_UpdateCameraYPos_Limit ;limit camera to level height
  8047. bit 1, (ix+$00)
  8048. jr nz, + ;FIXME: subroutine identical to below. required?
  8049. ld a, ($D176)
  8050. and $F8
  8051. ld b, a
  8052. ld a, ($D286)
  8053. and $F8
  8054. xor b
  8055. jp nz, Engine_LoadMappings32_Row ;load a row of mappings from the 32x32 mappings
  8056. ret
  8057.  
  8058. +: ld a, ($D176)
  8059. and $F8
  8060. ld b, a
  8061. ld a, ($D286)
  8062. and $F8
  8063. xor b
  8064. jp nz, Engine_LoadMappings32_Row
  8065. ret
  8066.  
  8067. ;limit camera y position
  8068. Engine_UpdateCameraYPos_Limit: ;$4A75
  8069. ld hl, ($D176)
  8070. ld ($D286), hl
  8071. ret
  8072.  
  8073. Engine_CalculateBgScroll: ;$4A7C
  8074. ld a, ($D174) ;calculate horiz. scroll by 2's comp'ing the lo-byte
  8075. add a, $01 ;of the horizontal screen offset
  8076. neg
  8077. ld ($D172), a ;store h-scroll
  8078. ld hl, ($D176) ;Calculate the vertical scroll value
  8079. ld de, $0011
  8080. add hl, de
  8081. ld de, $00E0 ;224 (screen height)
  8082. -: xor a
  8083. sbc hl, de
  8084. jr nc, -
  8085. add hl, de
  8086. ld a, l
  8087. ld ($D173), a ;store v-scroll
  8088. ret
  8089.  
  8090. ;************************************************
  8091. ;* Calculate the current block X/Y position. *
  8092. ;* Stores the calculated value in HL and at *
  8093. ;* $D278. *
  8094. ;* BC - Horizontal adjustment value. *
  8095. ;* DE - Vertical adjustment value. *
  8096. ;************************************************
  8097. Engine_Mappings_GetBlockXY: ;$4A9B
  8098. ld hl, ($D174) ;horizontal offset in level
  8099. bit 2, (ix+$00) ;IX = $D15E
  8100. jr z, +
  8101. ld bc, $0008
  8102. add hl, bc
  8103. +: sla l ;calculate the block on the x-axis
  8104. rl h ;hl *= 2
  8105. sla l
  8106. rl h ;hl *= 2
  8107. sla l
  8108. rl h ;hl *= 2
  8109. ld (ix+$02), h ;$D160 = abs($D174 / 32)
  8110.  
  8111. ld hl, ($D176)
  8112. add hl, de ;calculate the block on the y-axis
  8113. sla l
  8114. rl h ;hl *= 2
  8115. sla l
  8116. rl h ;hl *= 2
  8117. sla l
  8118. rl h ;hl *= 2
  8119. ld (ix+$03), h ;$D161 = abs($D176 / 32)
  8120.  
  8121. ld a, h ;HL = H * 2
  8122. add a, a
  8123. ld l, a
  8124. ld h, $00
  8125. ld de, ($D168) ;get the pointer to the row offsets
  8126. add hl, de
  8127. ld e, (hl) ;get the offset for the current block row
  8128. inc hl
  8129. ld d, (hl)
  8130.  
  8131. ld l, (ix+$02) ;add the block x index to the row offset
  8132. ld h, $00
  8133. add hl, de
  8134. ld de, $C001 ;offset into the level data array
  8135. add hl, de
  8136. ld ($D278), hl
  8137. ret
  8138.  
  8139. ;Precalculated address offsets for each row of blocks
  8140. ;for a given level width
  8141. .include "src\block_row_offsets.asm"
  8142.  
  8143.  
  8144. ;****************************************
  8145. ;* Decompress level layout into RAM *
  8146. ;****************************************
  8147. LoadLevel_LoadLayout: ;$5305
  8148. di
  8149. ld ix, $D15E
  8150. ld a, (ix+$05)
  8151. call Engine_SwapFrame2
  8152. ld l, (ix+$08)
  8153. ld h, (ix+$09)
  8154. ld de, $C001 ;RAM destination for level layout
  8155. push hl
  8156. pop iy ;iy points to mappings
  8157. -: ld a, d
  8158. and $F0
  8159. cp $C0 ;only write to RAM between $C000 and $CFFF
  8160. jr nz, LoadLevel_DrawInitialScreen
  8161. ld a, (iy+$00) ;load a byte from the layout stream
  8162. cp $FD ;check for the compression marker byte
  8163. jp nz, LoadLevel_LoadLayout_CopyByte
  8164. ld a, (iy+$01) ;compression - repeat count
  8165. or a ;check for end-of-stream ($FD, $00)
  8166. jp z, LoadLevel_DrawInitialScreen
  8167. ld b, a
  8168. --: ld a, d
  8169. and $F0 ;only write to RAM between $C000 and $CFFF
  8170. cp $C0
  8171. jr nz, LoadLevel_DrawInitialScreen
  8172. ld a, (iy+$02) ;compression - the byte to repeat
  8173. ld (de), a
  8174. inc de
  8175. djnz --
  8176. ld bc, $0003
  8177. add iy, bc
  8178. jp -
  8179.  
  8180. ;********************************
  8181. ;* Copy a layout byte into RAM. *
  8182. ;********************************
  8183. LoadLevel_LoadLayout_CopyByte:
  8184. ld a, (iy+$00)
  8185. ld (de), a
  8186. inc de
  8187. inc iy
  8188. jp -
  8189.  
  8190.  
  8191. ;********************************************************
  8192. ;* Load the starting sprite & camera X/Y coordinates *
  8193. ;* for the current level/act, then load the tiles for *
  8194. ;* the screen viewport. *
  8195. ;********************************************************
  8196. LoadLevel_DrawInitialScreen: ;$5353
  8197. ei ;interrupt happens immediately after this
  8198. call LoadLevel_LoadLayoutInitialPositions
  8199. ld hl, ($D2CA) ;set the screen X pos
  8200. ld ($D174), hl
  8201. ld ($D284), hl
  8202. ld hl, ($D2CC) ;set the screen Y pos
  8203. ld ($D176), hl
  8204. ld ($D286), hl
  8205. ;load the tiles for the initial screen (one row at a time)
  8206. ld b, $1D ;29
  8207. -: di
  8208. push bc
  8209. ld hl, ($D176) ;get screen Y pos
  8210. ld de, $0008 ;move to next row
  8211. add hl, de
  8212. ld ($D286), hl
  8213. ld a, ($D162) ;bank for 32x32 mappings
  8214. call Engine_SwapFrame2
  8215. call Engine_LoadMappings32_Row ;load a row of tiles
  8216. set 6, (ix+$00)
  8217. call WaitForInterrupt
  8218. pop bc
  8219. djnz -
  8220.  
  8221. ld a, $80 ;setup camera offset
  8222. ld (CameraOffsetX), a
  8223. ld a, $78
  8224. ld (CameraOffsetY), a
  8225. call Engine_CalculateCameraBounds
  8226.  
  8227. ld hl, ($D2CA) ;set screen x pos
  8228. ld de, $0008
  8229. add hl, de
  8230. ld ($D284), hl
  8231. ld ($D174), hl
  8232.  
  8233. ld hl, ($D2CC) ;set screen y pos
  8234. ld ($D286), hl
  8235. ld ($D176), hl
  8236.  
  8237. ld de, $0010 ;calculate vertical bg scroll value
  8238. add hl, de
  8239. ld de, $00E0
  8240. -: xor a
  8241. sbc hl, de
  8242. jr nc, -
  8243. add hl, de
  8244. ld a, l
  8245. ld ($D173), a ;store v-scroll value
  8246. ei
  8247. ret
  8248.  
  8249. ;********************************************
  8250. ;* Loads initial camera X/Y and sprite X/Y *
  8251. ;* for each level/act. *
  8252. ;********************************************
  8253. LoadLevel_LoadLayoutInitialPositions: ;$53C0
  8254. ld a, (CurrentLevel)
  8255. ld l, a
  8256. ld h, $00
  8257. add hl, hl
  8258. ld de, LevelLayout_Data_InitPos
  8259. add hl, de
  8260. ld e, (hl)
  8261. inc hl
  8262. ld d, (hl)
  8263. ld a, (CurrentAct)
  8264. ld l, a
  8265. ld h, $00
  8266. add hl, hl
  8267. add hl, de
  8268. ld e, (hl)
  8269. inc hl
  8270. ld d, (hl)
  8271. push de
  8272. pop iy
  8273. ld l, (iy+$00) ;load initial screeen X pos
  8274. ld h, (iy+$01)
  8275. ld ($D2CA), hl
  8276. ld l, (iy+$02) ;load initial screen Y pos
  8277. ld h, (iy+$03)
  8278. ld ($D2CC), hl
  8279. ld l, (iy+$04) ;initial player x pos
  8280. ld h, (iy+$05)
  8281. ld ($D511), hl
  8282. ld l, (iy+$06) ;initial player y pos
  8283. ld h, (iy+$07)
  8284. ld ($D514), hl
  8285. ret
  8286.  
  8287. .include "src\level_layout_initial_positions.asm"
  8288.  
  8289.  
  8290. Engine_ClearLevelAttributes: ;$5531
  8291. ld hl, $D15E ;clear data from $D15E to $D290
  8292. ld de, $D15F
  8293. ld bc, $0132
  8294. ld (hl), $00
  8295. ldir
  8296. ret
  8297.  
  8298. LoadLevelHeader:
  8299. LABEL_553F:
  8300. ;load the pointer to the level header
  8301. ld a, (CurrentLevel)
  8302. ld l, a
  8303. ld h, $00
  8304. add hl, hl
  8305. ld de, LevelHeaders
  8306. add hl, de
  8307. ld e, (hl)
  8308. inc hl
  8309. ld d, (hl)
  8310. ;load the pointer for the current act
  8311. ld a, (CurrentAct)
  8312. ld l, a
  8313. ld h, $00
  8314. add hl, hl
  8315. add hl, de
  8316. ld e, (hl)
  8317. inc hl
  8318. ld d, (hl)
  8319. push de
  8320. pop iy
  8321. ld ix, $D15E ;destination for the level header
  8322.  
  8323. ld a, (iy+$00)
  8324. ld (ix+$04), a ;$D162 - Bank number for 32x32 mappings
  8325.  
  8326. ld a, (iy+$01) ;$D164-D165 - pointer to 32x32 mappings
  8327. ld (ix+$06), a
  8328. ld a, (iy+$02)
  8329. ld (ix+$07), a
  8330.  
  8331. ld a, (iy+$03) ;$D163 - Bank number for level layout
  8332. ld (ix+$05), a
  8333.  
  8334. ld a, (iy+$04) ;$D166-D167 - Pointer to level layout
  8335. ld (ix+$08), a
  8336. ld a, (iy+$05)
  8337. ld (ix+$09),a
  8338.  
  8339. ld a, (iy+$06) ;$D16C-D16D - level width in blocks?
  8340. ld (ix+$0E), a
  8341. ld a, (iy+$07)
  8342. ld (ix+$0F), a
  8343.  
  8344. ld a, (iy+$08) ;$D16A-D16B - 2's comp. level width
  8345. ld (ix+$0C), a
  8346. ld a, (iy+$09)
  8347. ld (ix+$0D), a
  8348.  
  8349. ld a, (iy+$0A) ;$D16E-D16F - Vertical offset into layout data
  8350. ld (ix+$10), a
  8351. ld a, (iy+$0B)
  8352. ld (ix+$11), a
  8353.  
  8354. ld l, (iy+$0C) ;minimum camera x pos
  8355. ld h, (iy+$0D)
  8356. ld ($D280), hl
  8357.  
  8358. ld l, (iy+$0E) ;minimum camera y pos
  8359. ld h, (iy+$0F)
  8360. ld ($D27C), hl
  8361.  
  8362. ld l, (iy+$10) ;maximum camera x pos
  8363. ld h, (iy+$11)
  8364. ld ($D282), hl
  8365.  
  8366. ld l, (iy+$12) ;maximum camera y pos
  8367. ld h, (iy+$13)
  8368. ld ($D27E), hl
  8369.  
  8370. ld a, (iy+$14) ;$D168
  8371. ld (ix+$0A), a
  8372. ld a, (iy+$15) ;$D169
  8373. ld (ix+$0B), a
  8374.  
  8375. ld a, $80
  8376. ld (CameraOffsetX), a
  8377. ld a, $78
  8378. ld (CameraOffsetY), a
  8379. call Engine_CalculateCameraBounds
  8380. ret
  8381.  
  8382. .include "src\level_headers.asm"
  8383.  
  8384.  
  8385. ;load a column of tiles from 32x32 mappings
  8386. Engine_LoadMappings32_Column: ;$585B
  8387. ld de, $0008
  8388. call Engine_Mappings_GetBlockXY
  8389. ld de, $0008
  8390. bit 3, (ix+$00)
  8391. jr nz, +
  8392. ld de, $0000
  8393. +: add hl, de
  8394. exx
  8395. ld de, $D178
  8396. exx
  8397. ld b, $08
  8398. -: push bc
  8399. push hl
  8400. ld e, (hl)
  8401. ld d, $00
  8402. ex de, hl
  8403. add hl, hl
  8404. ld bc, ($D164) ;pointer to 32x32 mappings
  8405. add hl, bc
  8406. ld e, (hl)
  8407. inc hl
  8408. ld d, (hl)
  8409. ld a, ($D174) ;horiz. offset
  8410. bit 2, (ix+$00)
  8411. jr z, +
  8412. add a, $08
  8413. +: rrca
  8414. rrca
  8415. and $06
  8416. ld l, a
  8417. ld h, $00
  8418. add hl, de
  8419. ld b, $04
  8420. push hl
  8421. exx
  8422. pop hl
  8423. exx
  8424. --: exx
  8425. ld a, (hl)
  8426. ld (de), a
  8427. inc hl
  8428. inc de
  8429. ld a, (hl)
  8430. ld (de), a
  8431. inc de
  8432. ld a, $07
  8433. add a, l
  8434. ld l, a
  8435. exx
  8436. djnz --
  8437. pop hl
  8438. ld de, ($D16C) ;level width in blocks
  8439. add hl, de
  8440. pop bc
  8441. djnz -
  8442. set 5, (ix+$00)
  8443. ret
  8444.  
  8445. LABEL_58BA_71:
  8446. ld hl, ($D176) ;vert offset in level
  8447. ld bc, $0008
  8448. add hl, bc
  8449. srl h
  8450. rr l
  8451. srl h
  8452. rr l
  8453. srl h
  8454. rr l
  8455. add hl, hl
  8456. ld bc, DATA_59DB
  8457. add hl, bc
  8458. ld c, (hl)
  8459. inc hl
  8460. ld b, (hl)
  8461. ld a, ($D174)
  8462. bit 2, (ix+0)
  8463. jr z, LABEL_58E0_72
  8464. add a, $08
  8465. LABEL_58E0_72:
  8466. rrca
  8467. rrca
  8468. and $3E
  8469. ld l, a
  8470. ld h, $78
  8471. add hl, bc
  8472. ld bc, $0040
  8473. ld d, $7F
  8474. ld e, $07
  8475. exx
  8476. ld a, ($D176)
  8477. add a, $08
  8478. rrca
  8479. rrca
  8480. and $06
  8481. ld l, a
  8482. ld h, $00
  8483. ld de, $D178
  8484. add hl, de
  8485. ld b, $36
  8486. ld c, $BE
  8487. -: exx
  8488. ld a, l
  8489. out ($BF), a
  8490. ld a, h
  8491. out ($BF), a
  8492. add hl, bc
  8493. ld a, h
  8494. cp d
  8495. jp c, +
  8496. sub e
  8497. ld h, a
  8498. +: exx
  8499. outi
  8500. outi
  8501. jp nz, -
  8502. res 5, (ix+0)
  8503. ret
  8504.  
  8505.  
  8506. Engine_LoadMappings32_Row: ;$5920
  8507. ld de, $0000
  8508. call Engine_Mappings_GetBlockXY
  8509. ld de, ($D16E)
  8510. bit 1, (ix+0)
  8511. jr nz, +
  8512. ld de, $0000
  8513. +: add hl, de
  8514. exx
  8515. ld de, $D1F8
  8516. exx
  8517. ld b, $09
  8518. -: push bc
  8519. push hl
  8520. ld e, (hl)
  8521. ld d, $00
  8522. ex de, hl
  8523. add hl, hl
  8524. ld bc, ($D164) ;pointer to 32x32 mappings
  8525. add hl, bc ;calculate offset into mappings pointer array
  8526. ld e, (hl) ;get the pointer to the tile data
  8527. inc hl
  8528. ld d, (hl)
  8529. ld a, ($D176) ;get the tile scroll value by removing the
  8530. and $18 ;"fine scroll" value from the lower 3 bits
  8531. ld l, a
  8532. ld h, $00
  8533. add hl, de
  8534. push hl
  8535. exx
  8536. pop hl
  8537. ld bc, $0008
  8538. ldir
  8539. exx
  8540. pop hl
  8541. inc hl
  8542. pop bc
  8543. djnz -
  8544. set 4, (ix+0)
  8545. ret
  8546.  
  8547. LABEL_5966_65:
  8548. ld a, ($D174) ;horiz offset in level
  8549. bit 2, (ix+0)
  8550. jr z, +
  8551. add a, $08
  8552. +: rrca
  8553. rrca
  8554. and $06
  8555. ld l, a
  8556. ld h, $00
  8557. ld de, $D1F8
  8558. add hl, de
  8559. ex de, hl
  8560. ld hl, ($D176)
  8561. srl h
  8562. rr l ;hl /= 2
  8563. srl h
  8564. rr l ;hl /= 2
  8565. srl h
  8566. rr l ;hl /= 2
  8567. add hl, hl ;FIXME: this is pointless. Remove one of the right shifts.
  8568. ld bc, DATA_59DB
  8569. add hl, bc
  8570. ld c, (hl)
  8571. inc hl
  8572. ld b, (hl)
  8573. ld a, ($D174)
  8574. bit 2, (ix+0)
  8575. jr z, +
  8576. add a, $08
  8577. +: rrca
  8578. rrca
  8579. and $3E
  8580. ld l, a
  8581. ld h, $78
  8582. add hl, bc
  8583. ld b, $21
  8584. bit 2, (ix+0)
  8585. jr z, +
  8586. dec b
  8587. +: ld a, l ;set up to write to the screen map in VRAM
  8588. out ($BF), a
  8589. ld a, h
  8590. out ($BF), a
  8591.  
  8592. -: ld a, (de)
  8593. out ($BE), a
  8594. inc de
  8595. inc hl
  8596. ld a, (de)
  8597. out ($BE), a
  8598. inc de
  8599. inc hl
  8600. ld a, l
  8601. and $3F
  8602. jp nz, +
  8603. push de
  8604. ld de, $0040
  8605. or a
  8606. sbc hl, de
  8607. ld a, l
  8608. out ($BF), a
  8609. ld a, h
  8610. out ($BF), a
  8611. pop de
  8612. +: djnz -
  8613. res 4, (ix+0)
  8614. ret
  8615.  
  8616.  
  8617. DATA_59DB:
  8618. .incbin "unknown\s2_59DB.bin"
  8619.  
  8620. LABEL_5D93: ;updates current position in level
  8621. ld a, ($D15E)
  8622. and $F0
  8623. ld ($D15E), a
  8624. bit 0, (ix+$01)
  8625. jp nz, LABEL_5EA1
  8626. call Engine_CameraAdjust
  8627. ld a, ($D28A)
  8628. ld b, a
  8629. ld hl, ($D511)
  8630. ld de, ($D174)
  8631. xor a
  8632. sbc hl, de
  8633. jr z, $40
  8634. ld a, l
  8635. cp b
  8636. jr c, $1E
  8637. ld a, ($D28B)
  8638. ld b, a
  8639. ld a, l
  8640. cp b
  8641. jr c, $34
  8642. sub b
  8643. cp $09
  8644. jr c, $02
  8645. ld a, $08
  8646. ld l,a
  8647. ld de, ($D174)
  8648. add hl, de
  8649. ld ($D284), hl
  8650. set 3, (ix+$00)
  8651. jr $1E
  8652. ld a, ($D28C)
  8653. ld b, a
  8654. ld a, l
  8655. cp b
  8656. jr nc, $16
  8657. sub b
  8658. cp $F7
  8659. jr nc, $02
  8660. ld a, $F8
  8661. ld l, a
  8662. ld h, $FF
  8663. ld de, ($D174)
  8664. add hl, de
  8665. ld ($D284), hl
  8666. set 2, (ix+$00)
  8667. ld a, ($D28D)
  8668. ld b, a
  8669. ld hl, ($D514)
  8670. ld de, ($D286)
  8671. xor a
  8672. sbc hl, de
  8673. ret z
  8674. ld a, l
  8675. cp b
  8676. jr c, $1C
  8677. ld a, ($D28E)
  8678. ld b, a
  8679. ld a, l
  8680. cp b
  8681. ret c
  8682. sub b
  8683. cp $09
  8684. jr c, $02
  8685. ld a, $08
  8686. ld l, a
  8687. ld de, ($D176)
  8688. add hl, de
  8689. ld ($D286), hl
  8690. set 1, (ix+$00)
  8691. ret
  8692.  
  8693. LABEL_5E24:
  8694. ld a, ($D28F)
  8695. ld b, a
  8696. ld a, l
  8697. cp b
  8698. ret nc
  8699. sub b
  8700. cp $F7
  8701. jr nc, +
  8702. ld a, $F8
  8703. +: ld l, a
  8704. ld h, $FF
  8705. ld de, ($D176)
  8706. add hl, de
  8707. ld ($D286), hl
  8708. set 0, (ix+$00)
  8709. ret
  8710.  
  8711.  
  8712. ;************************************************
  8713. ;* Adjust the camera position to look up/down *
  8714. ;* or left/right. *
  8715. ;************************************************
  8716. Engine_CameraAdjust:
  8717. LABEL_5E42:
  8718. ld a, ($D28A)
  8719. ld b, a
  8720. ld a, ($D288) ;horizontal cam adjustment
  8721. cp b
  8722. jr z, LABEL_5E62
  8723. jr c, + ;is adjustment < D28A?
  8724. inc b
  8725. inc b
  8726. jr ++
  8727. +: dec b
  8728. dec b
  8729. ++: ld a, b ;set camera bounds
  8730. ld ($D28A), a
  8731. sub $08
  8732. ld ($D28C), a
  8733. add a, $10
  8734. ld ($D28B), a
  8735. LABEL_5E62:
  8736. ld a, ($D28D)
  8737. ld b, a
  8738. ld a, (CameraOffsetY)
  8739. cp b
  8740. ret z
  8741. jr c, + ;is adjustment < $D28D?
  8742. inc b
  8743. jr ++
  8744. +: dec b
  8745. ++: ld a, b
  8746. ld ($D28D), a ;set camera bounds
  8747. sub $10
  8748. ld ($D28F), a
  8749. add a, $20
  8750. ld ($D28E), a
  8751. ret
  8752.  
  8753. Engine_CalculateCameraBounds: ;$5E80
  8754. ld a, (CameraOffsetX)
  8755. ld ($D28A), a
  8756. sub $08
  8757. ld ($D28C), a ;camera left bounds
  8758. add a, $10
  8759. ld ($D28B), a ;camera right bounds
  8760. ld a, (CameraOffsetY)
  8761. ld ($D28D), a
  8762. sub $10
  8763. ld ($D28F), a ;camera top bounds
  8764. add a, $20
  8765. ld ($D28E), a ;camera bottom bounds
  8766. ret
  8767.  
  8768. LABEL_5EA1:
  8769. ld hl, ($D2CE)
  8770. ld de, ($D174)
  8771. xor a
  8772. sbc hl, de
  8773. jr z, $16
  8774. jr c, $0B
  8775. inc de
  8776. ld ($D284), de
  8777. set 3, (ix+$00)
  8778. jr $09
  8779. dec de
  8780. ld ($D284), de
  8781. set 2, (ix+$00)
  8782. ld hl, ($D2D0)
  8783. ld de, ($D176)
  8784. xor a
  8785. sbc hl, de
  8786. ret z
  8787. jr c, $0A
  8788. inc de
  8789. ld ($D286), de
  8790. set 1, (ix+$00)
  8791. ret
  8792.  
  8793. LABEL_5EDA:
  8794. dec de
  8795. ld ($D286), de
  8796. set 0, (ix+$00)
  8797. ret
  8798.  
  8799. Engine_UpdateAllObjects:
  8800. LABEL_5EE4:
  8801. xor a
  8802. ld ($D521), a
  8803. ld ix, $D540
  8804. ld b, $13
  8805. -: push bc
  8806. di
  8807. call LABEL_5EFD
  8808. ei
  8809. ld de, $0040
  8810. add ix, de
  8811. pop bc
  8812. djnz -
  8813. ret
  8814.  
  8815.  
  8816. ;********************************************
  8817. ; Updates the object pointed to by the *
  8818. ; IX register. *
  8819. ;********************************************
  8820. LABEL_5EFD:
  8821. ld a, (ix+$00)
  8822. or a
  8823. ret z ;is there an object in this slot?
  8824. cp $F0 ;jump if object >= $F0
  8825. jr nc, LABEL_5F51
  8826. ld a, (ix+$00)
  8827. cp $50
  8828. jr nc, LABEL_5F3D ;object types >= 80 are in bank 30
  8829. cp $20
  8830. jr nc, LABEL_5F29 ;object types >= 32 < 80 are in bank 28
  8831. ld a, :Bank31 ;object types >= 0 < 32 are in bank 31
  8832. call Engine_SwapFrame2
  8833. call LABEL_604A
  8834. ld a, :Bank31
  8835. call Engine_SwapFrame2
  8836. call LABEL_6139
  8837. ld a, (ix+$01)
  8838. or a
  8839. call nz, LABEL_617C
  8840. ret
  8841.  
  8842. LABEL_5F29: ;badniks & minecart
  8843. ld a, :Bank28
  8844. call Engine_SwapFrame2
  8845. call LABEL_604A
  8846. ld a, :Bank28
  8847. call Engine_SwapFrame2
  8848. call LABEL_6139
  8849. call LABEL_617C
  8850. ret
  8851.  
  8852. LABEL_5F3D:
  8853. ld a, :Bank30
  8854. call Engine_SwapFrame2
  8855. call LABEL_604A
  8856. ld a, :Bank30
  8857. call Engine_SwapFrame2
  8858. call LABEL_6139
  8859. call LABEL_617C
  8860. ret
  8861.  
  8862. LABEL_5F51:
  8863. and $0F
  8864. add a, a
  8865. ld l, a
  8866. ld h, $00
  8867. ld de, DATA_5F60
  8868. add hl, de
  8869. ld e, (hl)
  8870. inc hl
  8871. ld d, (hl)
  8872. ex de, hl
  8873. jp (hl)
  8874.  
  8875. DATA_5F60:
  8876. .dw LABEL_5F80
  8877. .dw LABEL_5F80
  8878. .dw LABEL_5F80
  8879. .dw LABEL_5F80
  8880. .dw LABEL_5F80
  8881. .dw LABEL_5F80
  8882. .dw LABEL_5F80
  8883. .dw LABEL_5F80
  8884. .dw LABEL_5F80
  8885. .dw LABEL_5F80
  8886. .dw LABEL_5F80
  8887. .dw LABEL_5F80
  8888. .dw LABEL_5F80
  8889. .dw LABEL_5F80
  8890. .dw LABEL_6212
  8891. .dw LABEL_6248
  8892.  
  8893. LABEL_5F80:
  8894. ret
  8895.  
  8896.  
  8897. Logic_Pointers ;$5F81
  8898. .include "src\object_logic_pointers.asm"
  8899.  
  8900. LABEL_603F:
  8901. ld a, ($D501)
  8902. cp PlayerState_LostLife
  8903. jr z, +
  8904. cp $28
  8905. jr z, +
  8906. LABEL_604A:
  8907. ld a, (ix+$0E)
  8908. or (ix+$0F)
  8909. jr z, Engine_UpdateObjectStateAnim
  8910. ld a, (ix+$02) ;check to see if we need to update the object's state
  8911. cp (ix+$01)
  8912. jr nz, Engine_UpdateObjectState
  8913. +: dec (ix+$07) ;frame display counter?
  8914. jp z, LABEL_6087
  8915. ret
  8916.  
  8917. Engine_UpdateObjectState:
  8918. ld a, (ix+$02) ;copy from working copy of state variable to
  8919. ld (ix+$01), a ;current state.
  8920. Engine_UpdateObjectStateAnim: ;$6067
  8921. ld a, (ix+$00) ;calculate a pointer to the object's animations
  8922. dec a
  8923. add a, a
  8924. ld l, a
  8925. ld h, $00
  8926. ld de, Logic_Pointers
  8927. add hl, de
  8928. ld e, (hl)
  8929. inc hl
  8930. ld d, (hl)
  8931. ld a, (ix+$01) ;get a pointer to the anim for the current state
  8932. add a, a
  8933. ld l, a
  8934. ld h, $00
  8935. add hl, de
  8936. ld e, (hl)
  8937. inc hl
  8938. ld d, (hl)
  8939. ld (ix+$0E), e ;store the pointer here
  8940. ld (ix+$0F), d
  8941. LABEL_6087:
  8942. ld l, (ix+$0E) ;fetch the animation pointer
  8943. ld h, (ix+$0F)
  8944. ld a, (hl)
  8945. cp $FF ;check for & process a command byte
  8946. jp z, Logic_ProcessCommand
  8947. ld (ix+$07), a ;frame display counter
  8948. inc hl
  8949. ld a, (hl)
  8950. ld (ix+$06), a ;frame number
  8951. inc hl
  8952. ld a, (hl)
  8953. ld (ix+$0C), a ;pointer to logic subroutine
  8954. inc hl
  8955. ld a, (hl)
  8956. ld (ix+$0D), a
  8957. inc hl ;store next frame pointer
  8958. ld (ix+$0E), l
  8959. ld (ix+$0F), h
  8960. LABEL_60AC:
  8961. ld a, :Bank31
  8962. call Engine_SwapFrame2
  8963. ld l, (ix+$00) ;which object?
  8964. ld h, $00
  8965. add hl, hl
  8966. ld de, ObjectAnimations ;offset into animation pointer array in bank 31
  8967. add hl, de
  8968. ld e, (hl) ;get a pointer to the object's animation sequences
  8969. inc hl
  8970. ld d, (hl)
  8971. ld l, (ix+$06) ;object frame number
  8972. ld h, $00
  8973. add hl, hl
  8974. add hl, de ;get a pointer to the data for the current frame
  8975. ld e, (hl)
  8976. inc hl
  8977. ld d, (hl)
  8978. ex de, hl
  8979. ld a, (hl)
  8980. ld (ix+$05), a ;number of sprites in this object
  8981. inc hl
  8982. ld a, (hl)
  8983. ld (ix+$2C), a
  8984. inc hl
  8985. ld a, (hl)
  8986. ld (ix+$2D), a
  8987. inc hl
  8988. ld a, (hl)
  8989. ld (ix+$28), a ;pointer to sprite arrangement data
  8990. inc hl
  8991. ld a, (hl)
  8992. ld (ix+$29), a
  8993. inc hl
  8994. ld (ix+$2A), l ;pointer to sprite attribute data (horiz/vert
  8995. ld (ix+$2B), h ;sprite offsets & pointer to char codes)
  8996. ret
  8997.  
  8998. LABEL_60E9:
  8999. ld a, (ix+$00)
  9000. dec a
  9001. ret nz
  9002. ld a, (ix+$06)
  9003. ld ($D34F), a
  9004. cp $11
  9005. jr c, +
  9006. cp $16
  9007. jr c, LABEL_6131
  9008. +: ld hl, $D137
  9009. bit 2, (hl) ;check for left button
  9010. jr nz, LABEL_6127
  9011. bit 3, (hl) ;check for right button
  9012. jr nz, LABEL_611D
  9013. res 4, (ix+$04)
  9014. ld a, ($D350)
  9015. and $40
  9016. or $80
  9017. ld ($D34E), a
  9018. and $40
  9019. ret z
  9020. set 4, (ix+$04)
  9021. ret
  9022.  
  9023. LABEL_611D:
  9024. ld a, $80
  9025. ld ($D34E), a
  9026. res 4, (ix+$04)
  9027. ret
  9028.  
  9029. LABEL_6127:
  9030. ld a, $C0
  9031. ld ($D34E), a
  9032. set 4, (ix+$04)
  9033. ret
  9034.  
  9035. LABEL_6131:
  9036. bit 7, (ix+$17)
  9037. jr z, LABEL_611D
  9038. jr LABEL_6127
  9039.  
  9040. LABEL_6139: ;called on collision
  9041. ld l, (ix+$0C)
  9042. ld h, (ix+$0D)
  9043. ld a, h
  9044. or l
  9045. ret z
  9046. jp (hl)
  9047. ret
  9048.  
  9049. LABEL_6144: ;find an open object slot (entire range)
  9050. push ix
  9051. ld ix, $D540
  9052. ld b, $10
  9053. ld de, $0040
  9054. -: ld a, (ix+$00)
  9055. or a
  9056. jr z, LABEL_615C
  9057. add ix, de
  9058. djnz -
  9059. pop ix
  9060. ret
  9061.  
  9062. LABEL_615C:
  9063. ld (ix+$00), c
  9064. ld (ix+$3F), h
  9065. pop ix
  9066. ret
  9067.  
  9068. LABEL_6165: ;find an open object slot (badnik range)
  9069. ld iy, $D700
  9070. ld de, $0040
  9071. ld b, $0B
  9072. -: ld a, (iy+$00)
  9073. or a
  9074. jr z, +
  9075. add iy, de
  9076. djnz -
  9077. scf
  9078. ret
  9079. +: xor a
  9080. ret
  9081.  
  9082. LABEL_617C:
  9083. bit 1, (ix+$04)
  9084. ret nz
  9085. res 6, (ix+$04)
  9086. ld l, (ix+$11)
  9087. ld h, (ix+$12)
  9088. ld bc, $0020
  9089. add hl, bc
  9090. ld bc, ($D174)
  9091. xor a
  9092. sbc hl, bc
  9093. srl h
  9094. rr l
  9095. srl h
  9096. rr l
  9097. ld a, l
  9098. cp $50
  9099. jr nc, LABEL_61C2
  9100. ld l, (ix+$14)
  9101. ld h, (ix+$15)
  9102. ld bc, $0020
  9103. add hl, bc
  9104. ld bc, ($D176)
  9105. xor a
  9106. sbc hl, bc
  9107. srl h
  9108. rr l
  9109. srl h
  9110. rr l
  9111. ld a, l
  9112. cp $50
  9113. jr nc, LABEL_61C2
  9114. ret
  9115.  
  9116. LABEL_61C2:
  9117. bit 0, (ix+$04)
  9118. jp nz, LABEL_6248
  9119. set 6, (ix+$04)
  9120. ld l, (ix+$11)
  9121. ld h, (ix+$12)
  9122. ld bc, $0180
  9123. add hl, bc
  9124. ld bc, ($D174)
  9125. xor a
  9126. sbc hl, bc
  9127. ld a, h
  9128. cp $04
  9129. jr nc, LABEL_61FA
  9130. ld l, (ix+$14)
  9131. ld h, (ix+$15)
  9132. ld bc, $0180
  9133. add hl, bc
  9134. ld bc, ($d176)
  9135. xor a
  9136. sbc hl, bc
  9137. ld a, h
  9138. cp $04
  9139. jr nc, LABEL_61FA
  9140. ret
  9141.  
  9142. LABEL_61FA:
  9143. ld a, (ix+$3e)
  9144. or a
  9145. jr z, +
  9146. ld (ix+$00), $fe
  9147. ld (ix+$01), $00
  9148. ret
  9149.  
  9150. +: ld (ix+$00), $ff
  9151. ld (ix+$01), $00
  9152. ret
  9153.  
  9154. LABEL_6212:
  9155. ld l, (ix+$3a)
  9156. ld h, (ix+$3b)
  9157. ld bc, $0180
  9158. add hl, bc
  9159. ld bc, ($d174)
  9160. xor a
  9161. sbc hl, bc
  9162. ld a, h
  9163. cp $04
  9164. jr nc, LABEL_623F
  9165. ld l, (ix+$3c)
  9166. ld h, (ix+$3d)
  9167. ld bc, $0180
  9168. add hl, bc
  9169. ld bc, ($d176)
  9170. xor a
  9171. sbc hl, bc
  9172. ld a, h
  9173. cp $04
  9174. jr nc, LABEL_623F
  9175. ret
  9176.  
  9177. LABEL_623F:
  9178. ld (ix+$00), $ff
  9179. ld (ix+$01), $00
  9180. ret
  9181.  
  9182. LABEL_6248:
  9183. ld a, (ix+$3E)
  9184. or a
  9185. jp z, +
  9186. dec a
  9187. ld e, a
  9188. ld d, $00
  9189. ld hl, $D400
  9190. add hl, de
  9191. ld (hl), $00
  9192. +: push ix
  9193. pop hl
  9194. ld (hl), $00
  9195. ld e, l
  9196. ld d, h
  9197. inc de
  9198. ld bc, $003F
  9199. ldir
  9200. ret
  9201.  
  9202. Engine_MoveObjectToPlayer: ;$6267
  9203. ld bc, ($D511) ;copy player's position to another object
  9204. ld de, ($D514)
  9205. ld (ix+$11), c
  9206. ld (ix+$12), b
  9207. ld (ix+$14), e
  9208. ld (ix+$15), d
  9209. ret
  9210.  
  9211. LABEL_627C:
  9212. ld a, (ix+$21)
  9213. and $0F
  9214. ret z
  9215. ld a, ($D503) ;check the "destroy enemies on collision" flag
  9216. bit 1, a
  9217. ret z
  9218. ld (ix+$3F), $80
  9219. Engine_DisplayExplosionObject: ;$628C
  9220. LABEL_628C:
  9221. call LABEL_62A7
  9222. ld (ix+$00), $0F ;display an explosion
  9223. xor a
  9224. ld (ix+$01), a
  9225. ld (ix+$02), a
  9226. ld (ix+$04), a
  9227. ld (ix+$07), a
  9228. ld (ix+$0E), a
  9229. ld (ix+$0F), a
  9230. ret
  9231.  
  9232. ;updates score after a sonic -> object collision
  9233. LABEL_62A7:
  9234. ld a, (ix+$00)
  9235. sub $20
  9236. ret c
  9237. cp $20
  9238. ret nc
  9239. add a, a ;calculate offset using object type
  9240. ld e, a
  9241. ld d, $00
  9242. ld hl, DATA_62BD
  9243. add hl, de
  9244. ld a, (hl) ;get the jump vector
  9245. inc hl
  9246. ld h, (hl)
  9247. ld l, a
  9248. jp (hl) ;jump
  9249.  
  9250. DATA_62BD: ;object collision type jump vectors
  9251. .dw LABEL_62FD
  9252. .dw LABEL_62FD
  9253. .dw LABEL_62FD
  9254. .dw LABEL_1CB8 ;badnik score value
  9255. .dw LABEL_62FD
  9256. .dw LABEL_62FD
  9257. .dw LABEL_62FD
  9258. .dw LABEL_62FD
  9259. .dw LABEL_62FD
  9260. .dw LABEL_62FD
  9261. .dw LABEL_62FD
  9262. .dw LABEL_1CB8 ;badnik score value
  9263. .dw LABEL_62FD
  9264. .dw LABEL_62FD
  9265. .dw LABEL_62FD
  9266. .dw LABEL_62FD
  9267. .dw LABEL_62FD
  9268. .dw LABEL_62FD
  9269. .dw LABEL_1CB8 ;badnik score value
  9270. .dw LABEL_1CB8 ;badnik score value
  9271. .dw LABEL_1CB8 ;badnik score value
  9272. .dw LABEL_1CB8 ;badnik score value
  9273. .dw LABEL_62FD
  9274. .dw LABEL_1CB8 ;badnik score value
  9275. .dw LABEL_1CB8 ;badnik score value
  9276. .dw LABEL_62FD
  9277. .dw LABEL_1CB8 ;badnik score value
  9278. .dw LABEL_62FD
  9279. .dw LABEL_62FD
  9280. .dw LABEL_62FD
  9281. .dw LABEL_62FD
  9282. .dw LABEL_62FD
  9283.  
  9284. LABEL_62FD:
  9285. ret
  9286.  
  9287. ;****************************************************
  9288. ;* Sets a object's vertical velocity but enforces *
  9289. ;* a maximum value. *
  9290. ;* DE - Value to adjust velocity by. *
  9291. ;* BC - Maximum velocity. *
  9292. ;* Clobbers: HL, DE, A *
  9293. ;* Returns: Vertical velocity in DE. *
  9294. ;****************************************************
  9295. Engine_SetObjectVerticalSpeed: ;$62FE
  9296. ld l, (ix+$18) ;get vertical velocity
  9297. ld h, (ix+$19)
  9298. add hl, de
  9299. ld e, l
  9300. ld d, h
  9301. bit 7, h
  9302. jr nz, LABEL_6313 ;jump if object is moving up
  9303. xor a
  9304. sbc hl, bc ;set velocity if below threshold
  9305. jr c, LABEL_6313
  9306. ret
  9307.  
  9308. LABEL_6311:
  9309. ld e, c
  9310. ld d, b
  9311. LABEL_6313:
  9312. ld (ix+$18), e ;set vertical velocity
  9313. ld (ix+$19), d
  9314. ret
  9315.  
  9316. LABEL_631A:
  9317. ld bc, $0000
  9318. ld de, $0000
  9319. push hl
  9320. call Engine_GetCollisionValueForBlock ;collide with background tiles
  9321. pop hl
  9322. bit 7, a
  9323. jr z, LABEL_634C
  9324. ld e, (ix+$18)
  9325. ld d, (ix+$19)
  9326. bit 7, d
  9327. jr nz, LABEL_634C
  9328. xor a
  9329. sbc hl, de
  9330. jr c, LABEL_6344
  9331. ld hl, $0000 ;set vertical velocity to 0
  9332. ld (ix+$18), l
  9333. ld (ix+$19), h
  9334. xor a
  9335. scf
  9336. ret
  9337.  
  9338. LABEL_6344:
  9339. ld (ix+$18), l
  9340. ld (ix+$19), h
  9341. xor a
  9342. ret
  9343.  
  9344. LABEL_634C:
  9345. xor a
  9346. dec a
  9347. ret
  9348.  
  9349. LABEL_634F:
  9350. call Engine_CheckCollision
  9351. jp LABEL_63F1
  9352.  
  9353. LABEL_6355:
  9354. call Engine_CheckCollision
  9355. call LABEL_63F1
  9356. LABEL_635B:
  9357. ld a, ($D501) ;check for jumps/rolls
  9358. cp PlayerState_Jumping
  9359. jr z, LABEL_636B
  9360. cp PlayerState_Rolling
  9361. jr z, LABEL_636B
  9362. cp PlayerState_JumpFromRamp
  9363. jr z, LABEL_636B
  9364. ret
  9365.  
  9366. LABEL_636B:
  9367. ld a, (ix+$21)
  9368. and $0F
  9369. ret z
  9370. bit 0, a
  9371. jr nz, +
  9372. bit 1, a
  9373. jr nz, ++
  9374. ld hl, ($D518)
  9375. ld a, l
  9376. or h
  9377. jr z, ++
  9378. +: ld hl, $FC00 ;set vertical speed
  9379. ld ($D518), hl
  9380. ++: ld a, PlayerState_JumpFromRamp
  9381. ld ($D502), a
  9382. ld a, SFX_Bomb ;play "bomb" sound
  9383. ld ($DD04), a
  9384. ld bc, $0400
  9385. ld hl, ($D511) ;get player's horizontal position into HL
  9386. ld e, (ix+$11) ;get object's horizontal position into DE
  9387. ld d, (ix+$12)
  9388. xor a
  9389. sbc hl, de
  9390. jr nc, + ;jump if object is behind player
  9391. ld bc, $FC00
  9392. +: ld ($D516), bc ;horizonal velocity
  9393. ret
  9394.  
  9395. LABEL_63A9:
  9396. call LABEL_6355
  9397. ld a, (ix+$1f)
  9398. or a
  9399. jp nz, LABEL_63E5
  9400. res 5, (ix+$04)
  9401. res 7, (ix+$04)
  9402. ld (ix+$21), $00
  9403. ret
  9404.  
  9405. LABEL_63C0:
  9406. call LABEL_634F
  9407. ld a, (ix+$1f)
  9408. or a
  9409. jp nz, LABEL_63E5
  9410. res 5, (ix+$04)
  9411. res 7, (ix+$04)
  9412. ld a, (ix+$21)
  9413. and $0f
  9414. ret z
  9415. ld (ix+$1f), $40
  9416. dec (ix+$24)
  9417. ld a, $aa
  9418. ld ($dd04), a
  9419. ret
  9420.  
  9421. LABEL_63E5:
  9422. dec (ix+$1f)
  9423. set 5, (ix+$04)
  9424. ld (ix+$21), $00
  9425. ret
  9426.  
  9427. LABEL_63F1:
  9428. ld a, (ix+$21)
  9429. and $0f
  9430. ret z
  9431. add a, a
  9432. ld e, a
  9433. ld d, $00
  9434. ld hl, DATA_6404
  9435. add hl, de
  9436. ld a, (hl)
  9437. inc hl
  9438. ld h, (hl)
  9439. ld l, a
  9440. jp (hl)
  9441.  
  9442. DATA_6404:
  9443. .dw LABEL_6424
  9444. .dw LABEL_643C
  9445. .dw LABEL_6425
  9446. .dw LABEL_6424
  9447. .dw LABEL_6483
  9448. .dw LABEL_6424
  9449. .dw LABEL_6424
  9450. .dw LABEL_6424
  9451. .dw LABEL_6454
  9452. .dw LABEL_6424
  9453. .dw LABEL_6424
  9454. .dw LABEL_6424
  9455. .dw LABEL_6424
  9456. .dw LABEL_6424
  9457. .dw LABEL_6424
  9458. .dw LABEL_6424
  9459.  
  9460. LABEL_6424:
  9461. ret
  9462.  
  9463. LABEL_6425:
  9464. ld a, ($d523)
  9465. bit 1, a
  9466. ret nz
  9467. ld a, ($d52d)
  9468. ld e, a
  9469. ld d, $00
  9470. ld l, (ix+$14)
  9471. ld h, (ix+$15)
  9472. add hl, de
  9473. ld ($d514), hl
  9474. ret
  9475.  
  9476. LABEL_643C:
  9477. ld a, ($d523)
  9478. bit 0, a
  9479. ret nz
  9480. ld e, (ix+$2d)
  9481. ld d, $00
  9482. ld l, (ix+$14)
  9483. ld h, (ix+$15)
  9484. xor a
  9485. sbc hl, de
  9486. ld ($d514), hl
  9487. ret
  9488.  
  9489. LABEL_6454:
  9490. ld a, ($d523)
  9491. bit 3, a
  9492. ret nz
  9493. ld hl, ($d174)
  9494. ld de, $0020
  9495. add hl, de
  9496. ld de, ($d511)
  9497. xor a
  9498. sbc hl, de
  9499. ret nc
  9500. ld e, (ix+$2c)
  9501. ld d, $00
  9502. ld a, ($d52c)
  9503. ld l, a
  9504. ld h, $00
  9505. add hl, de
  9506. ex de, hl
  9507. ld l, (ix+$11)
  9508. ld h, (ix+$12)
  9509. xor a
  9510. sbc hl, de
  9511. ld ($d511), hl
  9512. ret
  9513.  
  9514. LABEL_6483:
  9515. ld a, ($d523)
  9516. bit 2, a
  9517. ret nz
  9518. ld hl, ($d174)
  9519. ld de, $00e0
  9520. add hl, de
  9521. ld de, ($d511)
  9522. xor a
  9523. sbc hl, de
  9524. ret c
  9525. ld e, (ix+$2c)
  9526. ld d, $00
  9527. ld a, ($d52c)
  9528. ld l, a
  9529. ld h, $00
  9530. add hl, de
  9531. ex de, hl
  9532. ld l, (ix+$11)
  9533. ld h, (ix+$12)
  9534. add hl, de
  9535. ld ($d511), hl
  9536. ret
  9537.  
  9538. DoNothingStub: ;$64B0
  9539. ret
  9540.  
  9541. LABEL_64B1:
  9542. xor a
  9543. ld (ix+$1f), a
  9544. ret
  9545.  
  9546. ;****************************************************
  9547. ;* Calculates the slot number occupied by the *
  9548. ;* object given a pointer to its descriptor. *
  9549. ;* *
  9550. ;* HL - Pointer to the object's descriptor. *
  9551. ;* Clobbers: DE, HL *
  9552. ;* Returns Slot number in A. *
  9553. ;****************************************************
  9554. Engine_GetObjectIndexFromPointer: ;64B6
  9555. ld de, $D500
  9556. xor a
  9557. sbc hl, de
  9558. ld a, h
  9559. sla l
  9560. rla
  9561. sla l
  9562. rla
  9563. inc a
  9564. ret
  9565.  
  9566. ;****************************************************
  9567. ;* Calculates the address of a sprite descriptor. *
  9568. ;* *
  9569. ;* A - The slot number. *
  9570. ;* Clobbers: DE, A *
  9571. ;* Returns pointer in HL. *
  9572. ;****************************************************
  9573. Engine_GetObjectDescriptorPointer: ;$64C5
  9574. dec a
  9575. ld h, a
  9576. xor a
  9577. srl h ;shift bit 0 into bit 7 of A register
  9578. rra
  9579. srl h ;shift bit 0 into bit 7 of A register
  9580. rra
  9581. ld l, a
  9582. ld de, $D500 ;offset from player sprite address
  9583. add hl, de
  9584. ret
  9585.  
  9586. LABEL_64D4:
  9587. xor a
  9588. ld (ix+$16), a
  9589. ld (ix+$17), a
  9590. ld (ix+$18), a
  9591. ld (ix+$19), a
  9592. ld a, (ix+$0b)
  9593. or a
  9594. ret z
  9595. ld d, $00
  9596. ld e, (ix+$0a)
  9597. ld hl, $0330
  9598. add hl, de
  9599. ld a, (hl)
  9600. ld e, a
  9601. and $80
  9602. rlca
  9603. neg
  9604. ld d, a
  9605. ld hl, $0000
  9606. ld b, (ix+$0b)
  9607. -: add hl, de
  9608. djnz -
  9609. ld a, l
  9610. sra h
  9611. rra
  9612. sra h
  9613. rra
  9614. sra h
  9615. rra
  9616. sra h
  9617. rra
  9618. ld (ix+$16), a
  9619. ld (ix+$17), h
  9620. ld a, (ix+$0a)
  9621. add a, $c0
  9622. ld e, a
  9623. ld d, $00
  9624. ld hl, $0330
  9625. add hl, de
  9626. ld a, (hl)
  9627. ld e, a
  9628. and $80
  9629. rlca
  9630. neg
  9631. ld d, a
  9632. ld hl, $0000
  9633. ld b, (ix+$0b)
  9634. -: add hl, de
  9635. djnz -
  9636. ld a, l
  9637. sra h
  9638. rra
  9639. sra h
  9640. rra
  9641. sra h
  9642. rra
  9643. sra h
  9644. rra
  9645. ld (ix+$18), a
  9646. ld (ix+$19), h
  9647. ret
  9648.  
  9649. LABEL_6544:
  9650. xor a
  9651. ld (ix+$22), a
  9652. ld l, (ix+$2C)
  9653. ld h, $00
  9654. bit 7, (ix+$17)
  9655. jr z, +
  9656. ex de, hl
  9657. ld hl, $0000
  9658. xor a
  9659. sbc hl, de
  9660. +: push hl
  9661. pop bc
  9662. ld de, $FFEF
  9663. call Engine_GetCollisionValueForBlock ;collide with background tiles
  9664. cp $81
  9665. jr z, LABEL_656B
  9666. cp $8d
  9667. jr z, LABEL_656B
  9668. ret
  9669.  
  9670. LABEL_656B:
  9671. bit 7, (ix+$17)
  9672. jr nz, LABEL_6576
  9673. set 2, (ix+$22)
  9674. ret
  9675. LABEL_6576:
  9676. set 3, (ix+$22)
  9677. ret
  9678.  
  9679. LABEL_657B:
  9680. ld h, (ix+$17)
  9681. ld a, (ix+$16)
  9682. or h
  9683. ret z
  9684. ld a, h
  9685. and $80
  9686. jr nz, LABEL_658D
  9687. res 4, (ix+$04)
  9688. ret
  9689. LABEL_658D:
  9690. set 4, (ix+$04)
  9691. ret
  9692.  
  9693. LABEL_6592:
  9694. ld de, ($d511)
  9695. LABEL_6596:
  9696. ld l, (ix+$11) ;get sprite's hpos
  9697. ld h, (ix+$12)
  9698. xor a
  9699. sbc hl, de
  9700. ret z
  9701. jr nc, +
  9702. res 4, (ix+$04)
  9703. ret
  9704. +: set 4, (ix+$04)
  9705. ret
  9706.  
  9707. LABEL_65AC:
  9708. ld hl, ($D511)
  9709. ld bc, $0400
  9710. jp LABEL_65B5 ;FIXME: why bother with this?
  9711.  
  9712. LABEL_65B5:
  9713. bit 4, (ix+$04) ;which direction is the sprite facing
  9714. jr z, +
  9715. dec de ;2's comp DE
  9716. ld a, d
  9717. cpl
  9718. ld d, a
  9719. ld a, e
  9720. cpl
  9721. ld e, a
  9722. +: add hl, de
  9723. ld a, l
  9724. and $F8
  9725. ld l, a
  9726. ld e, (ix+$11) ;get horizontal position in level
  9727. ld d, (ix+$12)
  9728. ld a, e
  9729. and $F8
  9730. ld e, a
  9731. xor a
  9732. sbc hl, de
  9733. jr nc, +
  9734. dec bc ;2's comp bc
  9735. ld a, b
  9736. cpl
  9737. ld b, a
  9738. ld a, c
  9739. cpl
  9740. ld c, a
  9741. +: ld a, l
  9742. or h
  9743. jr nz, +
  9744. ld bc, $0000
  9745. +: ld (ix+$17), b ;set horizontal velocity
  9746. ld (ix+$16), c
  9747. ret
  9748.  
  9749. LABEL_65EB:
  9750. add hl, de
  9751. ld a, l
  9752. and $FC
  9753. ld l, a
  9754. ld e, (ix+$14) ;get vertical position
  9755. ld d, (ix+$15)
  9756. ld a, e
  9757. and $FC
  9758. ld e, a
  9759. xor a
  9760. sbc hl, de
  9761. jr nc, +
  9762. dec bc
  9763. ld a, b
  9764. cpl
  9765. ld b, a
  9766. ld a, c
  9767. cpl
  9768. ld c, a
  9769. +: ld a, l
  9770. or h
  9771. jr nz, +
  9772. ld bc, $0000
  9773. +: ld (ix+$19), b ;set vertical velocity
  9774. ld (ix+$18), c
  9775. ret
  9776.  
  9777. LABEL_6614:
  9778. ld a, ($D12B)
  9779. push af
  9780. call Engine_GetCollisionValueForBlock ;collide with background tiles
  9781. ld a, ($D162)
  9782. call Engine_SwapFrame2
  9783. ld a, $FD
  9784. ld hl, ($D354)
  9785. ld (hl), a
  9786. ld l, a
  9787. ld h, $00
  9788. add hl, hl
  9789. ld a, ($d164)
  9790. ld c, a
  9791. ld a, ($d165)
  9792. ld b, a
  9793. add hl, bc
  9794. ld e, (hl)
  9795. inc hl
  9796. ld d, (hl)
  9797. call LABEL_1A09
  9798. ld hl, ($d356)
  9799. ld a, l
  9800. and $E0
  9801. ld l, a
  9802. ld ($d35a), hl
  9803. ld hl, ($d358)
  9804. ld a, l
  9805. and $e0
  9806. ld l, a
  9807. ld ($d35c), hl
  9808. ld c, $04
  9809. ld a, ($d353)
  9810. cp $f7
  9811. jr nz, +
  9812. ld c, $07
  9813. +: ld h, $00
  9814. call LABEL_6144
  9815. ld h, $01
  9816. call LABEL_6144
  9817. ld h, $02
  9818. call LABEL_6144
  9819. ld h, $03
  9820. call LABEL_6144
  9821. pop af
  9822. ld ($D12B), a
  9823. ld ($FFFF), a
  9824. ret
  9825.  
  9826.  
  9827. ;interprets a command in the object logic
  9828. Logic_ProcessCommand: ;$6675
  9829. inc hl
  9830. ld a, (hl) ;"command" byte
  9831. inc hl
  9832. ld (ix+$0E), l ;store an updated pointer to the object's logic
  9833. ld (ix+$0F), h
  9834. add a, a ;jump based on the value of the command byte
  9835. ld l, a
  9836. ld h, $00
  9837. ld de, Logic_CommandVTable
  9838. add hl, de
  9839. ld a, (hl)
  9840. inc hl
  9841. ld h, (hl)
  9842. ld l, a
  9843. jp (hl)
  9844.  
  9845. Logic_CommandVTable: ;$668B
  9846. .dw LABEL_66AE ;$00 - load next animation
  9847. .dw LABEL_66B1 ;$01 -
  9848. .dw LABEL_66B4 ;$02 - Execute logic using following 2 bytes as function pointer.
  9849. .dw LABEL_66CA ;$03 - do nothing stub
  9850. .dw LABEL_66CB ;$04 - Set sprite's horizontal/vertical velocity
  9851. .dw LABEL_66FB ;$05 - Change sprite state (e.g. write to $D502 for sonic) & load next animation frame
  9852. .dw LABEL_670F ;$06 - Load a new sprite.
  9853. .dw LABEL_6791 ;$07 - Run new input logic.
  9854. .dw LABEL_67B1 ;$08 - triggers loading of a monitor or chaos emerald
  9855. .dw LABEL_67C5 ;$09 -
  9856. .dw LABEL_66AB ;$0A - load the next animation frame
  9857. .dw LABEL_66AB ;$0B -
  9858. .dw LABEL_66AB ;$0C -
  9859. .dw LABEL_66AB ;$0D -
  9860. .dw LABEL_66AB ;$0E -
  9861. .dw LABEL_66AB ;$0F -
  9862.  
  9863. LABEL_66AB:
  9864. jp LABEL_6087
  9865.  
  9866. LABEL_66AE:
  9867. jp Engine_UpdateObjectStateAnim
  9868.  
  9869. LABEL_66B1:
  9870. jp LABEL_6248
  9871.  
  9872. LABEL_66B4: ;command sequence $FF $02 - jump to pointer
  9873. ld l, (ix+$0e)
  9874. ld h, (ix+$0f)
  9875. ld e, (hl) ;read the command's pointer operand
  9876. inc hl
  9877. ld d, (hl)
  9878. inc hl
  9879. ld (ix+$0e), l ;store pointer to next frame
  9880. ld (ix+$0f), h
  9881. ld hl, LABEL_6087 ;push this to the stack as the return address
  9882. push hl ;for the subroutine
  9883. ex de, hl
  9884. jp (hl) ;jump to subroutine,
  9885.  
  9886. LABEL_66CA:
  9887. ret
  9888.  
  9889. LABEL_66CB: ;command sequence $FF $04 - set the sprite's speed
  9890. ld l, (ix+$0e)
  9891. ld h, (ix+$0f)
  9892. ld e, (hl)
  9893. inc hl
  9894. ld d, (hl)
  9895. inc hl
  9896. ld c, (hl)
  9897. inc hl
  9898. ld b, (hl)
  9899. inc hl
  9900. ld (ix+$0e), l
  9901. ld (ix+$0f), h
  9902. bit 4, (ix+$04)
  9903. jr z, +
  9904. ld hl, $0000
  9905. xor a
  9906. sbc hl, de
  9907. ex de, hl
  9908. +: ld (ix+$16), e
  9909. ld (ix+$17), d
  9910. ld (ix+$18), c
  9911. ld (ix+$19), b
  9912. jp LABEL_6087
  9913.  
  9914. LABEL_66FB:
  9915. ld l, (ix+$0e)
  9916. ld h, (ix+$0f)
  9917. ld a, (hl)
  9918. inc hl
  9919. ld (ix+$0e), l
  9920. ld (ix+$0f), h
  9921. ld (ix+$02), a
  9922. jp LABEL_6087
  9923.  
  9924. LABEL_670F: ;command sequence $FF $06 - load a sprite
  9925. call LABEL_6165 ;find an open sprite slot (badnik range)
  9926. jr c, LABEL_677C ;if no sprite slots
  9927. ld l, (ix+$0E)
  9928. ld h, (ix+$0F)
  9929. ld a, (hl)
  9930. ld (iy+$00), a
  9931. inc hl
  9932. ld e, (hl)
  9933. inc hl
  9934. ld d, (hl)
  9935. inc hl
  9936. ld c, (hl)
  9937. inc hl
  9938. ld b, (hl)
  9939. inc hl
  9940. ld a, (hl)
  9941. ld (iy+$3F), a
  9942. inc hl
  9943. ld (ix+$0E), l
  9944. ld (ix+$0F), h
  9945. bit 4, (ix+$04)
  9946. jr z, +
  9947. ld hl, $0000
  9948. xor a
  9949. sbc hl, de
  9950. ex de, hl
  9951. +: ld l, (ix+$11)
  9952. ld h, (ix+$12)
  9953. add hl, de
  9954. ld (iy+$11), l
  9955. ld (iy+$12), h
  9956. ld (iy+$3A), l
  9957. ld (iy+$3B), h
  9958. ld l, (ix+$14)
  9959. ld h, (ix+$15)
  9960. add hl, bc
  9961. ld (iy+$14), l
  9962. ld (iy+$15), h
  9963. ld (iy+$3C), l
  9964. ld (iy+$3D), h
  9965. ld a, (ix+$04)
  9966. and $10
  9967. ld (iy+$04), a
  9968. ld a, (ix+$08)
  9969. ld (iy+$08), a
  9970. ld a, (ix+$09)
  9971. ld (iy+$09), a
  9972. jp LABEL_6087
  9973.  
  9974. LABEL_677C: ;no open sprite slots
  9975. ld l, (ix+$0E) ;skip over the data for the "load sprite"
  9976. ld h, (ix+$0F) ;command and continue with the next frame
  9977. inc hl
  9978. inc hl
  9979. inc hl
  9980. inc hl
  9981. inc hl
  9982. inc hl
  9983. ld (ix+$0E), l
  9984. ld (ix+$0F), h
  9985. jp LABEL_6087
  9986.  
  9987. LABEL_6791:
  9988. ld l, (ix+$0E)
  9989. ld h, (ix+$0F)
  9990. ld e, (hl) ;pointer to subroutine
  9991. inc hl
  9992. ld d, (hl)
  9993. inc hl
  9994. ld a, (hl)
  9995. ld (ix+$0c), a ;store new input handler
  9996. inc hl
  9997. ld a, (hl)
  9998. ld (ix+$0d), a
  9999. inc hl
  10000. ld (ix+$0E), l
  10001. ld (ix+$0F), h
  10002. ld hl, LABEL_60AC ;push this as the return address for the sub
  10003. push hl
  10004. ex de, hl
  10005. jp (hl) ;jump to subroutine
  10006.  
  10007. LABEL_67B1: ;triggers loading tiles for a monitor or chaos emerald
  10008. ld l, (ix+$0E)
  10009. ld h, (ix+$0F)
  10010. ld a, (hl) ;which tiles should we load?
  10011. inc hl
  10012. ld (ix+$0E), l
  10013. ld (ix+$0F), h
  10014. ld ($D3AB), a ;the tiles to load
  10015. jp LABEL_6087
  10016.  
  10017. LABEL_67C5: ;command sequence $FF $09 - play a sound
  10018. ld l, (ix+$0E)
  10019. ld h, (ix+$0F)
  10020. ld a, (hl) ;the sound to play
  10021. inc hl
  10022. ld (ix+$0E), l ;store the pointer to the next frame
  10023. ld (ix+$0F), h
  10024. bit 7, a ;is the sound value > $80?
  10025. jr z, +
  10026. bit 6, (ix+$04)
  10027. jp nz, LABEL_6087
  10028. ld ($DD04), a ;play the sound
  10029. jp LABEL_6087
  10030.  
  10031. +: or $80 ;add $80 to the sound value
  10032. ld ($DD04), a
  10033. jp LABEL_6087
  10034.  
  10035. LABEL_67EC:
  10036. res 4, (ix+$04)
  10037. ret
  10038.  
  10039. LABEL_67F1:
  10040. set 4, (ix+$04)
  10041. ret
  10042.  
  10043. LABEL_67F6:
  10044. ld a, (ix+$04)
  10045. xor $10 ;toggle bit 4 of (ix+4)
  10046. ld (ix+$04), a
  10047. ret
  10048.  
  10049. Engine_UpdateObjectPosition: ;$67FF
  10050. ld l, (ix+$10) ;horizontal position
  10051. ld h, (ix+$11)
  10052. ld e, (ix+$16) ;get horizontal speed
  10053. ld d, (ix+$17)
  10054. ld b, (ix+$12)
  10055. ld a, d
  10056. and $80
  10057. rlca
  10058. dec a
  10059. cpl
  10060. add hl, de
  10061. adc a, b
  10062. ld (ix+$12), a
  10063. ld (ix+$10), l
  10064. ld (ix+$11), h
  10065. ld l, (ix+$13)
  10066. ld h, (ix+$14)
  10067. ld e, (ix+$18)
  10068. ld d, (ix+$19)
  10069. ld b, (ix+$15)
  10070. ld a, d
  10071. and $80
  10072. rlca
  10073. dec a
  10074. cpl
  10075. add hl, de
  10076. adc a, b
  10077. ld (ix+$15), a
  10078. ld (ix+$13), l
  10079. ld (ix+$14), h
  10080. ret
  10081.  
  10082.  
  10083. ;Detect collision between sprite & player
  10084. Engine_CheckCollision: ;$6840
  10085. ld a, ($D532) ;check for invincibility power up
  10086. cp $02
  10087. jr nz, +
  10088. ld hl, $D503
  10089. set 1, (hl) ;flag player as invulnerable
  10090. set 7, (hl)
  10091. +: xor a
  10092. ld (ix+$20), a
  10093. ld a, (ix+$21)
  10094. and $F0
  10095. ld (ix+$21), a
  10096. ld a, ($D501)
  10097. cp PlayerState_LostLife
  10098. ret z
  10099. cp $28
  10100. ret z
  10101. bit 6, (ix+$03)
  10102. ret nz
  10103. bit 7, (ix+$03)
  10104. jr nz, +
  10105. ld a, ($D503)
  10106. bit 6, a
  10107. ret nz
  10108. +: ld hl, ($D511) ;player's horizontal position
  10109. ld e, (ix+$11) ;this sprite's current position
  10110. ld d, (ix+$12)
  10111. xor a
  10112. sbc hl, de
  10113. jr c, LABEL_6899 ;jump if sprite is left of the player
  10114. ld a, h ;sprite is to the right of the player
  10115. or a
  10116. jp nz, LABEL_692F ;not close enough - bail out
  10117. ld a, ($D52C)
  10118. add a, (ix+$2C)
  10119. cp l
  10120. jp c, LABEL_692F ;not close enough - bail out
  10121. ld c, l
  10122. set 2, (ix+$21)
  10123. jp LABEL_68B4 ;test for vertical collision
  10124.  
  10125. LABEL_6899: ;sprite is to the left of the player
  10126. ld a, h
  10127. inc a
  10128. jp nz, LABEL_692F
  10129. ld a, l
  10130. neg
  10131. jp z, LABEL_692F
  10132. ld l, a
  10133. ld a, ($D52C)
  10134. add a, (ix+$2C)
  10135. cp l
  10136. jp c, LABEL_692F
  10137. ld c, l
  10138. set 3, (ix+$21)
  10139. LABEL_68B4: ;test for vertical collision
  10140. ld hl, ($D514) ;player's vertical position
  10141. ld e, (ix+$14) ;current sprite's vertical position
  10142. ld d, (ix+$15)
  10143. xor a
  10144. sbc hl, de
  10145. jr c, LABEL_68D3 ;jump if player is above sprite
  10146. ld a, h
  10147. or a
  10148. jr nz, LABEL_692F ;not close enough - bail out
  10149. ld a, ($D52D) ;player sprite height?
  10150. cp l
  10151. jr c, LABEL_692F ;not close enough - bail out
  10152. set 1, (ix+$21)
  10153. jp LABEL_68E7
  10154.  
  10155. LABEL_68D3: ;player is above sprite
  10156. ld a, h
  10157. inc a
  10158. jr nz, LABEL_692F ;not close enough - bail out
  10159. ld a, l
  10160. neg
  10161. jr z, LABEL_692F ;not close enough - bail out
  10162. ld l, a
  10163. ld a, (ix+$2D)
  10164. cp l
  10165. jr c, LABEL_692F ;not close enough - bail out
  10166. set 0, (ix+$21)
  10167. LABEL_68E7:
  10168. ld a, l
  10169. cp c
  10170. ld b, $0c
  10171. jr c, +
  10172. ld b, $03
  10173. +: ld a, (ix+$21)
  10174. and b
  10175. ld (ix+$21), a
  10176. ld a, ($D503)
  10177. bit 7, a
  10178. jr nz, +
  10179. ld (ix+$20), $01
  10180. +: bit 7, (ix+$03)
  10181. jr nz, +
  10182. push ix
  10183. pop hl
  10184. call Engine_GetObjectIndexFromPointer
  10185. ld ($D520), a
  10186. +: ld a, (ix+$21)
  10187. and $0F
  10188. ld b, a
  10189. and $03
  10190. ld c, $0C
  10191. jr z, +
  10192. ld c, $03
  10193. +: ld a, b
  10194. xor c
  10195. rlca
  10196. rlca
  10197. rlca
  10198. rlca
  10199. ld b, a
  10200. ld a, ($D521)
  10201. and $0F
  10202. or b
  10203. ld ($D521), a
  10204. ret
  10205.  
  10206. LABEL_692F:
  10207. ld a, (ix+$21)
  10208. and $F0
  10209. ld (ix+$21), a
  10210. ret
  10211.  
  10212. LABEL_6938:
  10213. ld b, (ix+$22)
  10214. ld a, ($D3B9)
  10215. or a
  10216. jr nz, +
  10217. bit 1, (ix+$03) ;destroy enemies on collision
  10218. jr nz, ++
  10219. +: ld a, (ix+$21)
  10220. rrca
  10221. rrca
  10222. rrca
  10223. rrca
  10224. and $0F
  10225. or b
  10226. ld b, a
  10227. ++: ld (ix+$23), b
  10228. ret
  10229.  
  10230. ;SHZ-2 wind
  10231. Engine_UpdateSHZ2Wind:
  10232. LABEL_6956:
  10233. ld a, (CurrentLevel) ;check for SHZ-2
  10234. dec a
  10235. ret nz
  10236. ld a, (CurrentAct)
  10237. dec a
  10238. ret nz
  10239. call LABEL_6B61
  10240. ld a, ($D440) ;wind
  10241. and $03
  10242. add a, a
  10243. ld e, a
  10244. ld d, $00
  10245. ld hl, LABEL_6975
  10246. add hl, de
  10247. ld a, (hl)
  10248. inc hl
  10249. ld h, (hl)
  10250. ld l, a
  10251. jp (hl)
  10252.  
  10253. LABEL_6975:
  10254. .dw LABEL_697D
  10255. .dw LABEL_698F
  10256. .dw LABEL_69F5
  10257. .dw LABEL_6B47
  10258.  
  10259. LABEL_697D:
  10260. ld a, ($DB2C)
  10261. cp $E0
  10262. ret z
  10263. ld a, $E0
  10264. ld hl, $DB2C
  10265. ld b, $08
  10266. -: ld (hl), a
  10267. inc hl
  10268. djnz -
  10269. ret
  10270.  
  10271. LABEL_698F:
  10272. ld b, $08
  10273. ld de, DATA_6B27
  10274. exx
  10275. ld de, ($D176)
  10276. exx
  10277. ld ix, $D445
  10278. -: ld a, (de)
  10279. exx
  10280. ld l, a
  10281. ld h, $00
  10282. add hl, de
  10283. ld (ix+$00), l
  10284. ld (ix+$01), h
  10285. exx
  10286. inc ix
  10287. inc ix
  10288. inc de
  10289. inc de
  10290. djnz -
  10291. ld b, $08
  10292. ld de, DATA_6B27+1
  10293. exx
  10294. ld de, ($D174)
  10295. exx
  10296. ld ix, $d455
  10297. -: ld a, (de)
  10298. exx
  10299. ld l, a
  10300. ld h, $00
  10301. add hl, de
  10302. ld (ix+$00), l
  10303. ld (ix+$01), h
  10304. exx
  10305. inc ix
  10306. inc ix
  10307. inc de
  10308. inc de
  10309. djnz -
  10310. ld b, $08
  10311. ld hl, $db99
  10312. -: ld a, $f6
  10313. add a, b
  10314. ld (hl), a
  10315. inc hl
  10316. inc hl
  10317. djnz -
  10318. ld a, $02
  10319. ld ($D440), a
  10320. ld a, $08
  10321. ld ($D444), a
  10322. ld hl, $0078
  10323. ld ($D442), hl
  10324. ret
  10325.  
  10326. LABEL_69F5:
  10327. ld a, ($D441)
  10328. inc a
  10329. ld ($D441), a
  10330. ld b, $08
  10331. ld ix, $D445
  10332. ld iy, $D455
  10333. ld hl, $DB98
  10334. -: push hl
  10335. call LABEL_6A7E
  10336. pop hl
  10337. ld a, ($D441)
  10338. and $01
  10339. call z, LABEL_6B17
  10340. inc hl
  10341. inc hl
  10342. inc ix
  10343. inc ix
  10344. inc iy
  10345. inc iy
  10346. djnz -
  10347. jp LABEL_6A25 ;FIXME: why bother with this?
  10348.  
  10349. LABEL_6A25:
  10350. ld b, $08
  10351. ld ix, $D445
  10352. ld de, ($d176)
  10353. exx
  10354. ld de, $DB2C
  10355. exx
  10356. -: ld l, (ix+$00)
  10357. ld h, (ix+$01)
  10358. xor a
  10359. sbc hl, de
  10360. ld a, l
  10361. exx
  10362. ld (de), a
  10363. inc de
  10364. exx
  10365. inc ix
  10366. inc ix
  10367. djnz -
  10368. ld b, $08
  10369. ld ix, $D455
  10370. ld de, ($D174)
  10371. exx
  10372. ld de, $DB98
  10373. exx
  10374. -: ld l, (ix+$00)
  10375. ld h, (ix+$01)
  10376. xor a
  10377. sbc hl, de
  10378. ld a, l
  10379. exx
  10380. ld (de), a
  10381. inc de
  10382. inc de
  10383. exx
  10384. cp $08
  10385. jr nc, +
  10386. ld a, ($D444)
  10387. cp b
  10388. jr nc, +
  10389. exx
  10390. dec de
  10391. ld a, $FE
  10392. ld (de), a
  10393. inc de
  10394. exx
  10395. +: inc ix
  10396. inc ix
  10397. djnz -
  10398. ret
  10399.  
  10400. LABEL_6A7E:
  10401. ld l, (ix+$00)
  10402. ld h, (ix+$01)
  10403. ld de, $FFFC
  10404. add hl, de
  10405. ld (ix+$00), l
  10406. ld (ix+$01), h
  10407. ld de, $0040
  10408. add hl, de
  10409. ld de, ($D176)
  10410. xor a
  10411. sbc hl, de
  10412. jp c, LABEL_6ACD
  10413. ld de, $0010
  10414. xor a
  10415. sbc hl, de
  10416. jp c, LABEL_6ACD
  10417. ld l, (iy+$00)
  10418. ld h, (iy+$01)
  10419. ld de, $0005
  10420. add hl, de
  10421. ld (iy+$00), l
  10422. ld (iy+$01), h
  10423. ld de, $0040
  10424. add hl, de
  10425. ld de, ($D174)
  10426. xor a
  10427. sbc hl, de
  10428. jp c, LABEL_6ACD
  10429. ld de, $0140
  10430. xor a
  10431. sbc hl, de
  10432. jp nc, LABEL_6ACD
  10433. ret
  10434.  
  10435. LABEL_6ACD:
  10436. push ix
  10437. pop hl
  10438. ld de, $d445
  10439. xor a
  10440. sbc hl, de
  10441. ld a, ($d441)
  10442. add a, l
  10443. ld c, a
  10444. ld a, r ;point of interest - used for random value?
  10445. add a, c
  10446. ld c, a
  10447. ld a, ($d511)
  10448. add a, c
  10449. ld c, a
  10450. ld a, ($d2b9)
  10451. add a, c
  10452. ld c, a
  10453. ld a, ($d2bc)
  10454. add a, c
  10455. and $0f
  10456. add a, a
  10457. ld hl, DATA_6B27
  10458. add a, l
  10459. ld l, a
  10460. ld a, $00
  10461. adc a, h
  10462. ld h, a
  10463. ld a, (hl)
  10464. inc hl
  10465. ld c, (hl)
  10466. ld e, a
  10467. ld d, $00
  10468. ld hl, ($d176)
  10469. add hl, de
  10470. ld (ix+$00), l
  10471. ld (ix+$01), h
  10472. ld e, c
  10473. ld d, $00
  10474. ld hl, ($d174)
  10475. add hl, de
  10476. ld (iy+$00), l
  10477. ld (iy+$01), h
  10478. ret
  10479.  
  10480. LABEL_6B17:
  10481. inc hl
  10482. ld a, (hl)
  10483. cp $FE
  10484. jr z, +
  10485. inc a
  10486. cp $F9
  10487. jr c, ++
  10488. ld a, $F6
  10489. ++: ld (hl), a
  10490. +: dec hl
  10491. ret
  10492.  
  10493. DATA_6B27:
  10494. .db $E0, $00, $E0, $20, $C0, $40, $E0, $60
  10495. .db $C0, $80, $30, $00, $68, $08, $80, $10
  10496. .db $D0, $10, $C8, $28, $D0, $48, $C8, $50
  10497. .db $90, $00, $C4, $40, $68, $00, $A8, $08
  10498.  
  10499. LABEL_6B47:
  10500. xor a
  10501. ld ($D444), a
  10502. ld hl, ($D442)
  10503. dec hl
  10504. ld ($D442), hl
  10505. ld a, h
  10506. and $80
  10507. jr nz, +
  10508. ld a, h
  10509. or l
  10510. jp nz, LABEL_69F5
  10511. +: xor a
  10512. ld ($d440), a
  10513. ret
  10514.  
  10515. LABEL_6B61:
  10516. ld hl, ($D174)
  10517. ld a, h
  10518. and $0f
  10519. sla l
  10520. rla
  10521. sla l
  10522. rla
  10523. ld e, a
  10524. ld d, $00
  10525. ld hl, DATA_6BB2
  10526. add hl, de
  10527. ld a, (hl)
  10528. inc a
  10529. and $07
  10530. add a, a
  10531. ld e, a
  10532. ld d, $00
  10533. ld hl, LABEL_6B85
  10534. add hl, de
  10535. ld a, (hl)
  10536. inc hl
  10537. ld h, (hl)
  10538. ld l, a
  10539. jp (hl)
  10540.  
  10541. LABEL_6B85:
  10542. .dw LABEL_6B95
  10543. .dw LABEL_6B96
  10544. .dw LABEL_6B9B
  10545. .dw LABEL_6B95
  10546. .dw LABEL_6BA7
  10547. .dw LABEL_6B95
  10548. .dw LABEL_6B95
  10549. .dw LABEL_6B95
  10550.  
  10551. LABEL_6B95:
  10552. ret
  10553.  
  10554. LABEL_6B96:
  10555. xor a
  10556. ld ($d440), a
  10557. ret
  10558.  
  10559. LABEL_6B9B:
  10560. ld a, ($d440)
  10561. cp $02
  10562. ret z
  10563. ld a, $01
  10564. ld ($d440), a
  10565. ret
  10566.  
  10567. LABEL_6BA7:
  10568. ld a, ($d440)
  10569. or a
  10570. ret z
  10571. ld a, $03
  10572. ld ($d440), a
  10573. ret
  10574.  
  10575. DATA_6BB2:
  10576. .db $FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF
  10577. .db $FF, $FF, $FF, $03, $01, $03, $00, $FF
  10578. .db $FF, $FF, $FF, $03, $01, $01, $03, $00
  10579. .db $FF, $FF, $FF, $FF, $01, $01, $01, $03
  10580. .db $00, $FF, $FF, $FF, $FF, $FF, $FF, $FF
  10581. .db $FF, $FF, $FF, $FF, $FF, $FF, $FF, $03
  10582. .db $01, $01, $01, $01, $03, $01, $03, $00
  10583. .db $FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF
  10584.  
  10585. LABEL_6BF2: ;collision with ring object?
  10586. call LABEL_6C3C
  10587. call LABEL_7102
  10588. call LABEL_72B8
  10589. call LABEL_7378
  10590. jp LABEL_6938
  10591.  
  10592. LABEL_6C01:
  10593. ld a, ($D36D)
  10594. and a
  10595. jp p, +
  10596. neg
  10597. +: cp $04
  10598. jp nc, LABEL_6C2D
  10599. bit 0, (ix+$07)
  10600. jr z, LABEL_6C21
  10601. call LABEL_6C3C
  10602. call LABEL_72B8
  10603. call LABEL_7378
  10604. jp LABEL_6938
  10605.  
  10606. LABEL_6C21:
  10607. call LABEL_6C3C
  10608. call LABEL_7102
  10609. call LABEL_7378
  10610. jp LABEL_6938
  10611.  
  10612. LABEL_6C2D:
  10613. call LABEL_6C3C
  10614. call LABEL_7102
  10615. call LABEL_72B8
  10616. call LABEL_7378
  10617. jp LABEL_6938
  10618.  
  10619. LABEL_6C3C:
  10620. ld a, ($D363)
  10621. ld ($D364), a
  10622. xor a
  10623. ld ($D363), a
  10624. ld bc, $0000 ;horiz. offset
  10625. ld de, $0000 ;vert. offset
  10626. call Engine_GetCollisionDataForBlock
  10627. ld a, ($D353) ;get current block number
  10628. ld ($D365), a
  10629. ld ($D367), a
  10630. call LABEL_6F82
  10631. ld a, ($D35E)
  10632. ld ($D366), a
  10633. and $7F
  10634. add a, a
  10635. ld l, a
  10636. ld h, $00
  10637. ld de, DATA_6C70
  10638. add hl, de
  10639. ld e, (hl)
  10640. inc hl
  10641. ld d, (hl)
  10642. ex de, hl
  10643. jp (hl)
  10644.  
  10645. DATA_6C70:
  10646. .dw LABEL_6DAA
  10647. .dw LABEL_6CA0
  10648. .dw LABEL_6CA1
  10649. .dw LABEL_6C9C
  10650. .dw LABEL_6C9C
  10651. .dw LABEL_6CE2
  10652. .dw LABEL_6DAA
  10653. .dw LABEL_6DAA
  10654. .dw LABEL_6D49
  10655. .dw LABEL_6CA2 ;hit vertical spring
  10656. .dw LABEL_6DAA
  10657. .dw LABEL_6C9C
  10658. .dw LABEL_6D52
  10659. .dw LABEL_6CF7
  10660. .dw LABEL_6D0D
  10661. .dw LABEL_6C9C
  10662. .dw LABEL_6E30
  10663. .dw LABEL_6C9C
  10664. .dw LABEL_6C9D
  10665. .dw LABEL_6EB1
  10666. .dw LABEL_6CAD
  10667. .dw LABEL_6C9C
  10668.  
  10669. LABEL_6C9C:
  10670. ret
  10671.  
  10672. LABEL_6C9D:
  10673. jp LABEL_6CA1 ;FIXME: pointless
  10674.  
  10675. LABEL_6CA0:
  10676. ret
  10677.  
  10678. LABEL_6CA1:
  10679. ret
  10680.  
  10681. LABEL_6CA2: ;hit vertical spring
  10682. bit 1, (ix+$22)
  10683. ret z
  10684. ld hl, $F880 ;jump velocity
  10685. jp Player_SetState_VerticalSpring
  10686.  
  10687. LABEL_6CAD: ;diagonal spring?
  10688. bit 1, (ix+$22)
  10689. ret z
  10690. ld hl, $F880
  10691. set 4, (ix+$04)
  10692. ld a, ($d365)
  10693. cp $80
  10694. jr nc, +
  10695. ld hl, $0780
  10696. res 4, (ix+$04)
  10697. +: ld (ix+$16), l ;horizontal velocity?
  10698. ld (ix+$17), h
  10699. ld hl, $FAF0
  10700. ld a, (CurrentLevel)
  10701. cp $03 ;check to see if we're on GHZ
  10702. jr z, +
  10703. ld hl, $F900
  10704. +: ld a, SFX_Spring ;play spring "bounce" sound
  10705. ld ($DD04), a
  10706. jp Player_SetState_DiagonalSpring
  10707.  
  10708. LABEL_6CE2: ;hit a badnik?
  10709. ld a, ($D365)
  10710. and $fe
  10711. cp $f4
  10712. ret z
  10713. bit 1, (ix+$22)
  10714. ret z
  10715. bit 7, (ix+$03)
  10716. ret nz
  10717. jp Player_SetState_Hurt
  10718.  
  10719. LABEL_6CF7:
  10720. bit 1, (ix+$03)
  10721. ret z
  10722. ld hl, $fbc0
  10723. ld ($D518), hl
  10724. res 1, (ix+$22)
  10725. set 0, (ix+$03)
  10726. jp LABEL_7215
  10727.  
  10728. LABEL_6D0D:
  10729. bit 1, (ix+$22)
  10730. ret z
  10731. ld a, ($D365) ;search within 8 bytes of $6D41 for the value at ($D365)
  10732. ld hl, DATA_6D41
  10733. ld bc, $0008
  10734. cpir
  10735. jr z, +
  10736. ld hl, $0100
  10737. jr ++
  10738. +: ld hl, $ff00
  10739. ++: ld c, $00
  10740. bit 7, h
  10741. jr z, +
  10742. dec c
  10743. +: xor a
  10744. ld de, ($D510)
  10745. add hl, de
  10746. ld ($D510), hl
  10747. ld a, $00
  10748. adc a, c
  10749. add a, (ix+$12)
  10750. ld (ix+$12), a
  10751. ret
  10752.  
  10753. DATA_6D41:
  10754. .db $DF, $E0, $E3, $E4, $E5, $E6, $E7, $F0
  10755.  
  10756. LABEL_6D49:
  10757. ld a, (ix+$02)
  10758. cp $1a
  10759. ret z
  10760. jp LABEL_318F
  10761.  
  10762. LABEL_6D52:
  10763. ld a, ($d362)
  10764. cp $20
  10765. ret nz
  10766. ld a, ($d365)
  10767. ld c, $ff
  10768. cp $ef
  10769. jr nz, +
  10770. ld a, ($d511)
  10771. ld c, $ed
  10772. and $10
  10773. jr z, +
  10774. inc c
  10775. +: push bc
  10776. res 1, (ix+$22)
  10777. call LABEL_6165
  10778. pop bc
  10779. ret c
  10780. ld a, $b1 ;play bouncing on water sound?
  10781. ld ($DD04), a
  10782. ld (iy+$00), $2d
  10783. jp LABEL_6D81
  10784.  
  10785. LABEL_6D81:
  10786. ld a, ($D12B)
  10787. push af
  10788. ld a, ($D162) ;bank with 32x32 mappings
  10789. call Engine_SwapFrame2
  10790. ld a, c
  10791. ld hl, ($D354)
  10792. ld (hl), a
  10793. ld l, a
  10794. ld h, $00
  10795. add hl, hl
  10796. ld a, ($D164) ;BC = pointer to 32x32 mappings
  10797. ld c, a
  10798. ld a, ($D165)
  10799. ld b, a
  10800. add hl, bc
  10801. ld e, (hl)
  10802. inc hl
  10803. ld d, (hl)
  10804. call LABEL_1A09
  10805. pop af
  10806. call Engine_SwapFrame2
  10807. jp LABEL_6DD5
  10808.  
  10809. LABEL_6DAA:
  10810. res 1, (ix+$22)
  10811. ld a, ($D501)
  10812. cp PlayerState_Rolling
  10813. jr z, +
  10814. cp PlayerState_JumpFromRamp
  10815. jr z, +
  10816. jr LABEL_6DD5
  10817. +: ld a, ($d364)
  10818. or a
  10819. jr z, LABEL_6DD5
  10820. bit 7, (ix+$17)
  10821. jr nz, LABEL_6DCC
  10822. bit 1, a
  10823. jr z, LABEL_6DD5
  10824. jp Player_RampLaunch
  10825.  
  10826. LABEL_6DCC:
  10827. bit 1, a
  10828. jr nz, LABEL_6DD5
  10829. jp Player_RampLaunch
  10830.  
  10831. LABEL_6DD5:
  10832. call LABEL_6938
  10833. bit 1, (ix+$23)
  10834. ret nz
  10835. bit 0, (ix+$03) ;player in air?
  10836. ret nz
  10837. ld a, (ix+$01)
  10838. cp PlayerState_Rolling
  10839. jp nz, Player_SetState_Falling
  10840. jp LABEL_3ACA
  10841.  
  10842. ;called when launching from a ramp
  10843. Player_RampLaunch:
  10844. LABEL_6DED:
  10845. ld l, (ix+$16)
  10846. ld h, (ix+$17)
  10847. bit 7, h ;is horizontal velocity negative?
  10848. jr nz, LABEL_6E10
  10849. ld d, h ;take horizontal velocity, divide by 2 and add to vertical velocity
  10850. ld e, l
  10851. srl h
  10852. rr l
  10853. add hl, de
  10854. dec hl
  10855. ld a, h
  10856. cpl
  10857. ld h, a
  10858. ld a, l
  10859. cpl
  10860. ld l, a
  10861. ld ($D518), hl
  10862. ld a, SFX_Jump
  10863. ld ($DD04), a
  10864. jp Player_SetState_JumpFromRamp
  10865.  
  10866. ;launch from ramp with negative velocity
  10867. LABEL_6E10:
  10868. dec hl ;2's comp horizontal velocity, divide by 2 and
  10869. ld a, h ;add to vertical velocity.
  10870. cpl
  10871. ld h, a
  10872. ld a, l
  10873. cpl
  10874. ld l, a
  10875. ld d, h
  10876. ld e, l
  10877. srl h
  10878. rr l
  10879. add hl, de
  10880. dec hl
  10881. ld a, h
  10882. cpl
  10883. ld h, a
  10884. ld a, l
  10885. cpl
  10886. ld l, a
  10887. ld ($D518), hl
  10888. ld a, SFX_Jump ;play jump sound
  10889. ld ($DD04), a
  10890. jp Player_SetState_JumpFromRamp
  10891.  
  10892. LABEL_6E30:
  10893. ld a, ($D362)
  10894. bit 7, a
  10895. ret nz
  10896. bit 1, (ix+$22)
  10897. ret z
  10898. ld a, ($D365)
  10899. cp $2D
  10900. ret nz
  10901. ld hl, ($D516)
  10902. bit 7, h
  10903. ret nz
  10904. ld a, h
  10905. neg
  10906. ld h, a
  10907. ld a, l
  10908. neg
  10909. ld l, a
  10910. ld ($D516), hl
  10911. ld ($D518), hl
  10912. ret
  10913.  
  10914. LABEL_6E56:
  10915. ld a, ($D362)
  10916. bit 6, a
  10917. jr nz, LABEL_6E8B
  10918. and $3F
  10919. ld b, a
  10920. ld a, ($D358)
  10921. and $1F
  10922. add a, b
  10923. cp $20
  10924. ret c
  10925. sub $20
  10926. ld c, a
  10927. ld b, $00
  10928. ld h, (ix+$15)
  10929. ld l, (ix+$14)
  10930. xor a
  10931. sbc hl, bc
  10932. ld (ix+$15), h
  10933. ld (ix+$14), l
  10934. set 1, (ix+$22)
  10935. call LABEL_6938
  10936. ld a, ($D100)
  10937. ld ($D363), a
  10938. ret
  10939.  
  10940. LABEL_6E8B:
  10941. and $3f
  10942. ld b, a
  10943. ld a, ($d358)
  10944. and $1f
  10945. add a, c
  10946. sub b
  10947. ret nc
  10948. neg
  10949. ld c, a
  10950. ld b, $00
  10951. ld h, (ix+$15)
  10952. ld l, (ix+$14)
  10953. xor a
  10954. sbc hl, bc
  10955. ld (ix+$15), h
  10956. ld (ix+$14), l
  10957. set 1, (ix+$22)
  10958. jp LABEL_6938
  10959.  
  10960. LABEL_6EB1:
  10961. ld a, ($D365)
  10962. ld ($D367), a
  10963. LABEL_6EB7:
  10964. res 1, (ix+$22)
  10965. set 0, (ix+$03)
  10966. ld a, ($D353) ;get current block index
  10967. cp $3B
  10968. jr z, LABEL_6F03
  10969. cp $3C
  10970. jr z, LABEL_6ED6
  10971. cp $3D
  10972. jr z, LABEL_6F2E
  10973. cp $3E
  10974. jp z, LABEL_6F59
  10975. jp LABEL_6F81
  10976.  
  10977. LABEL_6ED6:
  10978. ld a, SFX_Roll
  10979. ld ($DD04), a
  10980. ld a, (ix+$01)
  10981. cp $21
  10982. jp z, LABEL_6F81
  10983. bit 7, (ix+$19)
  10984. jp z, LABEL_6F81
  10985. ld (ix+$02), $21
  10986. ld hl, $fa00
  10987. ld (ix+$18), l
  10988. ld (ix+$19), h
  10989. ld hl, $0000
  10990. ld (ix+$16), l
  10991. ld (ix+$17), h
  10992. jp LABEL_6F81
  10993.  
  10994. LABEL_6F03:
  10995. ld a, SFX_Roll
  10996. ld ($DD04), a
  10997. ld a, (ix+$01)
  10998. cp $21
  10999. jr z, LABEL_6F81
  11000. bit 7, (ix+$19)
  11001. jr nz, LABEL_6F81
  11002. ld (ix+$02), $21
  11003. ld hl, $0600
  11004. ld (ix+$18), l
  11005. ld (ix+$19), h
  11006. ld hl, $0000
  11007. ld (ix+$16), l
  11008. ld (ix+$17), h
  11009. jp LABEL_6F81
  11010.  
  11011. LABEL_6F2E:
  11012. ld a, SFX_Roll
  11013. ld ($DD04), a
  11014. ld a, (ix+$01)
  11015. cp $21
  11016. jr z, LABEL_6F81
  11017. bit 7, (ix+$17)
  11018. jr nz, LABEL_6F81
  11019. ld (ix+$02), $21
  11020. ld hl, $0600
  11021. ld (ix+$16), l
  11022. ld (ix+$17), h
  11023. ld hl, $0000
  11024. ld (ix+$18), l
  11025. ld (ix+$19), h
  11026. jp LABEL_6F81
  11027.  
  11028. LABEL_6F59:
  11029. ld a, SFX_Roll
  11030. ld ($DD04), a
  11031. ld a, (ix+$01)
  11032. cp $21
  11033. jr z, LABEL_6F81
  11034. bit 7, (ix+$17)
  11035. jr z, LABEL_6F81
  11036. ld (ix+$02), $21
  11037. ld hl, $fa00
  11038. ld (ix+$16), l
  11039. ld (ix+$17), h
  11040. ld hl, $0000
  11041. ld (ix+$18), l
  11042. ld (ix+$19), h
  11043. LABEL_6F81:
  11044. ret
  11045.  
  11046. LABEL_6F82:
  11047. cp $F2
  11048. jr nc, +
  11049. cp $DF
  11050. jr nc, LABEL_6FCE
  11051. cp $88
  11052. ret nc
  11053. cp $78
  11054. jr nc, ++
  11055. cp $70
  11056. ret nc
  11057. ++: cp $40
  11058. jp nc, LABEL_6FCE
  11059. +: ld a, ($D362)
  11060. and $3F
  11061. cp $20
  11062. call z, LABEL_7010
  11063. ld a, ($D358)
  11064. and $1F
  11065. ld c, a
  11066. ld a, ($D362)
  11067. and $3F
  11068. add a, c
  11069. cp $20
  11070. ret c
  11071. sub $20
  11072. ld c, a
  11073. ld b, $00
  11074. ld hl, ($D514)
  11075. xor a
  11076. sbc hl, bc
  11077. ld ($D514), hl ;set vertical position
  11078. set 1, (ix+$22)
  11079. call LABEL_6938
  11080. ld a, ($D100)
  11081. ld ($D363), a
  11082. ret
  11083.  
  11084. LABEL_6FCE:
  11085. bit 7, (ix+$19)
  11086. ret nz
  11087. ld a, ($D362) ;get vert projection value
  11088. and $3F
  11089. call z, LABEL_7010
  11090. res 1, (ix+$22)
  11091. ld a, (ix+$19) ;hi-byte of vertical velocity
  11092. add a, $09
  11093. ld b, a
  11094. ld a, ($D358) ;lo-byte of adjusted vert. pos
  11095. and $1F ;calculate current position in block
  11096. ld c, a
  11097. ld a, ($D362) ;get vert projection value
  11098. add a, c
  11099. cp $20
  11100. ret c
  11101. sub $20
  11102. cp b
  11103. ret nc
  11104. ld c, a
  11105. ld b, $00
  11106. ld hl, ($D514) ;adjust vertical pos in level
  11107. xor a
  11108. sbc hl, bc
  11109. ld ($D514), hl
  11110. set 1, (ix+$22)
  11111. call LABEL_6938
  11112. ld a, ($D100)
  11113. ld ($D363), a
  11114. ret
  11115.  
  11116. ;collide with level tiles - set vertical position
  11117. LABEL_7010:
  11118. push bc
  11119. ld a, ($D12B) ;store current frame for later
  11120. push af
  11121.  
  11122. ld a, :Bank30
  11123. ld ($D12B), a
  11124. ld ($FFFF), a
  11125.  
  11126. ld hl, ($D354) ;get position in level data
  11127. ld de, ($D16A) ;get 2's comp level width in blocks
  11128. add hl, de ;adjust pointer to previous row of blocks
  11129. ld a, (hl) ;get the value of the block
  11130.  
  11131. ld l, a
  11132. ld h, $00
  11133. add hl, hl
  11134. ld de, ($D2D4) ;pointer to collision data
  11135. add hl, de
  11136.  
  11137. ld e, (hl) ;get pointer to block's collision data
  11138. inc hl
  11139. ld d, (hl)
  11140. ex de, hl
  11141. ld a, (hl) ;get block collision data
  11142.  
  11143. cp $82
  11144. jr z, LABEL_706F
  11145. bit 7, a
  11146. jr z, LABEL_7066
  11147.  
  11148. inc hl
  11149. ld a, (hl)
  11150. ld ($D100), a
  11151.  
  11152. inc hl
  11153. ld c, (hl)
  11154. inc hl
  11155. ld b, (hl)
  11156. ex de, hl
  11157. ld a, ($D356) ;horizontal position (for collision)
  11158. and $1F ;calculate position on current block
  11159. ld l, a
  11160. ld h, $00
  11161. add hl, bc ;get collision data for current position
  11162. ld a, (hl)
  11163.  
  11164. and $3F
  11165. jr z, LABEL_7066
  11166. ld c, a
  11167. ld b, $00
  11168. ld h, (ix+$15) ;adjust vertical position
  11169. ld l, (ix+$14)
  11170. xor a
  11171. sbc hl, bc
  11172. ld (ix+$15), h
  11173. ld (ix+$14), l
  11174. LABEL_7066:
  11175. pop af
  11176. ld ($D12B), a
  11177. ld ($FFFF), a
  11178. pop bc
  11179. ret
  11180.  
  11181. LABEL_706F:
  11182. inc hl
  11183. inc hl
  11184. ld c, (hl) ;read the pointer
  11185. inc hl
  11186. ld b, (hl)
  11187. ex de, hl
  11188.  
  11189. ld a, ($D356) ;get horizontal position
  11190. and $1F ;get position in current block
  11191. ld l, a
  11192. ld h, $00
  11193.  
  11194. add hl, bc ;get collision data for current position
  11195. ld a, (hl)
  11196. and $3F
  11197. jr z, LABEL_7066
  11198. ld b, a
  11199. add a, $20
  11200. ld ($D362), a ;store vert projection value
  11201. ld b, a
  11202. jp LABEL_7066
  11203.  
  11204. LABEL_708D:
  11205. cp $88
  11206. ret nc
  11207. cp $40
  11208. jp nc, Engine_Collision_AdjustVerticalPos
  11209. ld a, ($D362)
  11210. and $3F
  11211. cp $20
  11212. call z, LABEL_7010
  11213. ld a, ($D358)
  11214. and $1F
  11215. ld c, a
  11216. ld a, ($D362)
  11217. and $3F
  11218. add a, c
  11219. cp $20
  11220. ret c
  11221. sub $20
  11222. ld c, a
  11223. ld b, $00
  11224. ld h, (ix+$15) ;move the player on the y-axis
  11225. ld l, (ix+$14)
  11226. xor a
  11227. sbc hl, bc
  11228. ld (ix+$15), h
  11229. ld (ix+$14), l
  11230. set 1, (ix+$22)
  11231. ret
  11232.  
  11233. Engine_Collision_AdjustVerticalPos: ;70C7
  11234. bit 7, (ix+$19)
  11235. ret nz
  11236. ld a, ($D362) ;vert projection value
  11237. and $3F
  11238. call z, LABEL_7010
  11239.  
  11240. ld a, (ix+$19) ;hi-byte of vertical velocity
  11241. add a, $07
  11242. ld b, a
  11243.  
  11244. ld a, ($D358) ;get lo-byte of adjusted vertical position
  11245. and $1F ;get current position on block
  11246. ld c, a
  11247. ld a, ($D362) ;vert projection value
  11248. add a, c
  11249. cp $20
  11250. ret c
  11251.  
  11252. sub $20 ;do we need to adjust the vertical
  11253. cp b ;position?
  11254. ret nc
  11255.  
  11256. ld c, a ;adjust vertical position
  11257. ld b, $00
  11258. ld h, (ix+$15)
  11259. ld l, (ix+$14)
  11260. xor a
  11261. sbc hl, bc
  11262. ld (ix+$15), h
  11263. ld (ix+$14), l
  11264. set 1, (ix+$22)
  11265. ret
  11266.  
  11267. LABEL_7102:
  11268. ld hl, $d522
  11269. res 2, (hl)
  11270. res 3, (hl)
  11271. call LABEL_7172
  11272. ld bc, $000a
  11273. ld de, $fff4
  11274. call Engine_GetCollisionDataForBlock
  11275. ld a, ($D35E)
  11276. cp $81
  11277. jr z, LABEL_7139
  11278. cp $0a
  11279. jr z, LABEL_7139
  11280. cp $05
  11281. jp z, LABEL_7267
  11282. cp $8d
  11283. jp z, LABEL_71C9
  11284. cp $13
  11285. jp z, LABEL_7130
  11286. ret
  11287.  
  11288. LABEL_7130:
  11289. ld a, ($d353)
  11290. ld ($d367), a
  11291. jp LABEL_6EB7
  11292.  
  11293. LABEL_7139:
  11294. ld a, ($d356)
  11295. and $1f
  11296. ld c, a
  11297. ld a, ($d361)
  11298. ld b, a
  11299. and $3f
  11300. ret z
  11301. cp $20
  11302. jr z, +
  11303. bit 6, b
  11304. ret z
  11305. +: add a, c
  11306. cp $20
  11307. ret c
  11308. sub $20
  11309. ld c, a
  11310. ld b, $00
  11311. ld hl, ($d511)
  11312. xor a
  11313. sbc hl, bc
  11314. ld ($d511), hl
  11315. set 2, (ix+$22)
  11316. call LABEL_6938
  11317. ld a, ($D35E)
  11318. cp $0A
  11319. ret nz
  11320. ld hl, $FA00
  11321. jp Player_SetState_HorizontalSpring
  11322.  
  11323. LABEL_7172:
  11324. ld bc, $fff6
  11325. ld de, $fff4
  11326. call Engine_GetCollisionDataForBlock
  11327. ld a, ($D35E)
  11328. cp $81
  11329. jr z, LABEL_7195
  11330. cp $0A
  11331. jr z, LABEL_7195
  11332. cp $05
  11333. jp z, LABEL_728A
  11334. cp $8D
  11335. jr z, LABEL_71EF
  11336. cp $13
  11337. jp z, LABEL_7130
  11338. ret
  11339.  
  11340. LABEL_7195:
  11341. ld a, ($D356) ;get horizontal position
  11342. and $1F ;calculate position on current block
  11343. ld c, a
  11344. ld a, ($D361) ;get horizontal collision value
  11345. ld b, a
  11346. and $3F
  11347. ret z
  11348. cp $20
  11349. jr z, +
  11350. bit 6, b
  11351. ret nz
  11352. +: cp c
  11353. ret c ;return if A < C
  11354. sub c
  11355. ld c, a
  11356. ld b, $00
  11357. ld hl, ($D511) ;get horizontal pos in level
  11358. add hl, bc ;adjust horizontal pos
  11359. ld ($D511), hl
  11360. set 3, (ix+$22)
  11361. call LABEL_6938
  11362. ld a, ($D35E) ;surface type?
  11363. cp $0A ;check for horizontal spring
  11364. ret nz
  11365. ld hl, $0600
  11366. jp LABEL_3119 ;seems to be identical to subroutine "Player_HitHorizontalSpring"
  11367.  
  11368. LABEL_71C9:
  11369. bit 1, (ix+$03)
  11370. jp z, LABEL_7139
  11371. ld a, ($D517)
  11372. bit 7, a
  11373. jr z, +
  11374. neg
  11375. +: cp $03
  11376. jp c, LABEL_7139
  11377. ld hl, ($D516)
  11378. ld a, h
  11379. cp $07
  11380. jr nc, +
  11381. ld bc, $0040
  11382. add hl, bc
  11383. ld ($D516), hl
  11384. +: jr LABEL_7215
  11385.  
  11386.  
  11387. LABEL_71EF:
  11388. bit 1, (ix+$03)
  11389. jp z, LABEL_7195
  11390. ld a, ($D517)
  11391. bit 7, a
  11392. jr z, +
  11393. neg
  11394. +: cp $03
  11395. jp c, LABEL_7195
  11396. ld hl, ($D516)
  11397. ld a, h
  11398. neg
  11399. cp $07
  11400. jr nc, LABEL_7215
  11401. ld bc, $FFC0
  11402. add hl, bc
  11403. ld ($D516), hl
  11404. LABEL_7215:
  11405. ld a, ($D162) ;load bank with 32x32 mappings
  11406. call Engine_SwapFrame2
  11407. ld a, $FD
  11408. ld hl, ($D354)
  11409. ld (hl), a
  11410. ld l, a
  11411. ld h, $00
  11412. add hl, hl
  11413. ld a, ($D164) ;get pointer to mappings
  11414. ld c, a
  11415. ld a, ($D165)
  11416. ld b, a
  11417. add hl, bc
  11418. ld e, (hl)
  11419. inc hl
  11420. ld d, (hl)
  11421. call LABEL_1A09
  11422. ld hl, ($D356)
  11423. ld a, l
  11424. and $E0
  11425. ld l, a
  11426. ld ($D35A), hl
  11427. ld hl, ($D358)
  11428. ld a, l
  11429. and $E0
  11430. ld l, a
  11431. ld ($D35C), hl
  11432. LABEL_7248:
  11433. ld c, $04
  11434. ld a, ($D353)
  11435. cp $F7
  11436. jr nz, +
  11437. ld c, $07
  11438. +: ld h, $00
  11439. call LABEL_6144
  11440. ld h, $01
  11441. call LABEL_6144
  11442. ld h, $02
  11443. call LABEL_6144
  11444. ld h, $03
  11445. jp LABEL_6144
  11446.  
  11447. LABEL_7267:
  11448. ld a, ($d353)
  11449. and $fe
  11450. cp $f2
  11451. jr z, +
  11452. ld a, ($d502)
  11453. cp $17
  11454. jp z, LABEL_71C9
  11455. cp $1e
  11456. ret z
  11457. +: call LABEL_7139
  11458. call LABEL_6938
  11459. ld a, ($d353)
  11460. cp $f4
  11461. ret nz
  11462. jp LABEL_72AA
  11463.  
  11464. LABEL_728A:
  11465. ld a, ($D353)
  11466. and $FE
  11467. cp $F2
  11468. jr z, +
  11469. ld a, ($D502) ;current player state
  11470. cp PlayerState_InMineCart
  11471. jp z, LABEL_71EF
  11472. cp PlayerState_Hurt
  11473. ret z
  11474. +: call LABEL_7195
  11475. call LABEL_6938
  11476. ld a, ($D353)
  11477. cp $F5
  11478. ret nz
  11479. LABEL_72AA:
  11480. bit 7, (ix+$03)
  11481. ret nz
  11482. ld hl, $0100
  11483. ld ($D518), hl
  11484. jp Player_SetState_Hurt ;"getting hurt"
  11485.  
  11486. LABEL_72B8:
  11487. bit 1, (ix+$22)
  11488. ret nz
  11489. res 0, (ix+$22)
  11490. ld bc, $0000
  11491. ld de, $FFE8
  11492. call Engine_GetCollisionDataForBlock
  11493. ld a, ($D35E)
  11494. cp $81
  11495. jr z, LABEL_72E6
  11496. cp $8d
  11497. jp z, LABEL_7215
  11498. cp $15
  11499. jp z, LABEL_731B
  11500. cp $13
  11501. jp z, LABEL_736F
  11502. cp $05
  11503. jp z, LABEL_7329 ;collide with damaging block (e.g. spikes).
  11504. ret
  11505.  
  11506. LABEL_72E6:
  11507. ld a, ($D358) ;adjusted vertical pos
  11508. and $1F ;get offset on block
  11509. ld c, a ;c = vert pos on block
  11510. ld a, ($D362) ;get block's vert. heightmap
  11511. ld b, a
  11512. and $3F
  11513. ret z
  11514. cp $20
  11515. jr z, +
  11516. bit 6, b
  11517. ret nz
  11518. +: cp c
  11519. ret c
  11520. sub c
  11521. ld c, a
  11522. ld b, $00
  11523. ld l, (ix+$14) ;get sprite's vpos
  11524. ld h, (ix+$15)
  11525. add hl, bc ;calculate the new vpos
  11526. ld (ix+$14), l
  11527. ld (ix+$15), h
  11528. set 0, (ix+$22)
  11529. call LABEL_6938
  11530. LABEL_7314:
  11531. ld hl, $0100
  11532. ld ($D518), hl
  11533. ret
  11534.  
  11535. LABEL_731B:
  11536. ld hl, $0780
  11537. ld ($D518), hl
  11538. ld a, SFX_Spring
  11539. ld ($DD04), a
  11540. jp Player_SetState_JumpFromRamp
  11541.  
  11542. LABEL_7329: ;collide with damaging mapping block (e.g. spikes).
  11543. ld a, ($D358) ;get lo-byte of vert pos
  11544. and $1F
  11545. ld c, a ;calculate offset into block
  11546. ld a, ($D362) ;get vert projection value
  11547. ld b, a
  11548. and $3F
  11549. ret z
  11550. cp $20
  11551. jr z, +
  11552. bit 6, b
  11553. ret z
  11554. +: cp c
  11555. ret c ;return if a < c (player not colliding with block)
  11556. sub c ;calculate number of pixels to move player
  11557. ld c, a
  11558. ld b, $00
  11559. ld l, (ix+$14)
  11560. ld h, (ix+$15)
  11561. add hl, bc
  11562. ld (ix+$14), l ;set vertical position
  11563. ld (ix+$15), h
  11564. set 0, (ix+$22)
  11565. call LABEL_6938
  11566. ld a, (ix+$01)
  11567. cp PlayerState_Hurt
  11568. ret z
  11569. ld a, ($D353) ;get current block number
  11570. and $FE
  11571. cp $F4
  11572. jp z, LABEL_7314 ;set vertical velocity
  11573. bit 7, (ix+$03)
  11574. ret nz
  11575. jp Player_SetState_Hurt ;"getting hurt"
  11576.  
  11577. LABEL_736F:
  11578. ld a, ($D353)
  11579. ld ($D367), a
  11580. jp LABEL_6EB7
  11581.  
  11582. LABEL_7378:
  11583. ld bc, $0000
  11584. ld de, $FFE6
  11585. bit 0, (ix+$07)
  11586. jr z, +
  11587. ld de, $FFF0
  11588. +: ld a, ($D465) ;copy the previous collision value.
  11589. ld ($D466), a
  11590. call Engine_GetCollisionValueForBlock ;collide with background tiles
  11591. ld ($D465), a ;store block collision value here
  11592. cp $07 ;was the collision with a block of rings?
  11593. jr z, Logic_CollideWithRingBlock
  11594. jp Logic_ALZ_WaterBounce
  11595.  
  11596.  
  11597. ;********************************************************
  11598. ;* Handles a player-ring block collision. Increments *
  11599. ;* the ring counter and updates the level layout to *
  11600. ;* remove the ring. *
  11601. ;********************************************************
  11602. Logic_CollideWithRingBlock: ;$739A
  11603. ld a, ($D162) ;bank with 32x32 mappings
  11604. call Engine_SwapFrame2
  11605. ld a, ($D353) ;get the block number
  11606. sub $70 ;subtract 112 (ring mappings start at index 112).
  11607. ld ($D352), a ;store the ring block number.
  11608. ld l, a
  11609. ld h, $00
  11610. add hl, hl
  11611. add hl, hl
  11612. ld de, DATA_7407
  11613. add hl, de
  11614. ld a, ($D356) ;get lo-byte of adjusted hpos
  11615. rrca
  11616. rrca
  11617. rrca
  11618. rrca
  11619. and $01
  11620. ld c, a
  11621. ld b, $00
  11622. add hl, bc
  11623. ld a, ($D358) ;get lo-byte of adjusted vpos
  11624. rrca
  11625. rrca
  11626. rrca
  11627. and $02
  11628. ld e, a
  11629. ld d, $00
  11630. add hl, de
  11631. ld a, (hl)
  11632. or a
  11633. ret z
  11634. ex de, hl
  11635. add hl, bc
  11636. ex de, hl
  11637. ld bc, $0020
  11638. add hl, bc
  11639. ld a, (hl)
  11640. ld hl, ($D354) ;get address of the 32x32 block
  11641. ld (hl), a ;and change it
  11642. ld l, a
  11643. ld h, $00
  11644. add hl, hl
  11645. ld a, ($D164) ;get pointer to mappings into BC
  11646. ld c, a
  11647. ld a, ($D165)
  11648. ld b, a
  11649. add hl, bc
  11650. ld e, (hl)
  11651. inc hl
  11652. ld d, (hl)
  11653. call Engine_UpdateMappingBlock
  11654. ld hl, ($D356) ;store copy of adjusted hpos
  11655. ld ($D35A), hl
  11656. ld hl, ($D358) ;store copy of adjusted vpos
  11657. ld ($D35C), hl
  11658. ld c, Object_RingSparkle ;display a ring "sparkle"
  11659. ld h, $00
  11660. call LABEL_6144 ;find an open object slot
  11661. ld a, SFX_Ring ;play "got ring" sound
  11662. ld ($DD04), a
  11663. jp IncrementRingCounter
  11664.  
  11665. ;****************************************************
  11666. ;* Mapping indices used by the above routine to *
  11667. ;* remove "collected" rings from the level layout *
  11668. ;****************************************************
  11669. DATA_7407:
  11670. .db $FF, $FF, $00, $00
  11671. .db $00, $00, $FF, $FF
  11672. .db $FF, $00, $00, $FF
  11673. .db $00, $FF, $FF, $00
  11674. .db $FF, $00, $00, $00
  11675. .db $00, $FF, $00, $00
  11676. .db $00, $00, $FF, $00
  11677. .db $00, $00, $00, $FF
  11678. .db $75, $74, $FE, $FE
  11679. .db $FE, $FE, $77, $76
  11680. .db $77, $FE, $FE, $74
  11681. .db $FE, $76, $75, $FE
  11682. .db $FE, $FE, $FE, $FE
  11683. .db $FE, $FE, $FE, $FE
  11684. .db $FE, $FE, $FE, $FE
  11685. .db $FE, $FE, $FE, $FE
  11686.  
  11687.  
  11688. Logic_ALZ_WaterBounce: ;$7447
  11689. ld a, (CurrentLevel)
  11690. cp $02 ;compare to ALZ
  11691. ret nz
  11692.  
  11693. ld a, ($D353) ;get current block value
  11694. or $01 ;check for the "water" block
  11695. cp $8F
  11696. ret nz
  11697.  
  11698. ld a, ($D466)
  11699. cp $06
  11700. ret z
  11701. ld a, ($D519) ;is sonic moving up or down?
  11702. bit 7, a
  11703. jr nz, + ;jump if moving up
  11704. ld c, $0B ;show the splash sprite
  11705. ld h, $00
  11706. call LABEL_6144 ;find an open sprite slot
  11707. +: bit 1, (ix+$03) ;is sonic rolling?
  11708. ret z
  11709. ld a, ($D517) ;is sonic travelling left or right?
  11710. bit 7, a
  11711. jr z, +
  11712. neg
  11713. +: cp $03 ;bail out if sonic's not moving fast enough
  11714. ret c
  11715. ld hl, $FD00 ;set the vertical velocity so that sonic
  11716. ld ($D518), hl ;bounces off of the water
  11717. ret
  11718.  
  11719. ;read collision attributes for the current mapping
  11720. ;bc = horizontal position adjustment
  11721. ;de = vertical position adjustment
  11722. Engine_GetCollisionDataForBlock: ;$7481
  11723. ld a, ($D12B) ;save current page for later
  11724. push af
  11725.  
  11726. ld a, :Bank30
  11727. ld ($D12B), a
  11728. ld ($FFFF), a
  11729. ld h, (ix+$12) ;horizontal pos in level
  11730. ld l, (ix+$11)
  11731. add hl, bc ;adjust horiz. position
  11732. ld ($D356), hl ;store position
  11733.  
  11734. sla l ;hl *= 2 calculate block on the x-axis
  11735. rl h
  11736. sla l ;hl *= 2
  11737. rl h
  11738. sla l ;hl *= 2
  11739. rl h
  11740. ld b, h ;B = abs(hl / 32)
  11741.  
  11742. ld h, (ix+$15) ;vertical pos in level
  11743. ld l, (ix+$14)
  11744. add hl, de ;adjust vertical position
  11745. ld de, $0012
  11746. add hl, de
  11747. ld ($D358), hl
  11748.  
  11749. sla l ;calculate block on the y-axis
  11750. rl h
  11751. sla l
  11752. rl h
  11753. sla l
  11754. rl h
  11755.  
  11756. ld a, h
  11757. add a, a
  11758. ld l, a
  11759. ld h, $00
  11760. ld de, ($D168) ;pointer to the row offsets
  11761. add hl, de
  11762. ld e, (hl) ;get offset for current row of blocks
  11763. inc hl
  11764. ld d, (hl)
  11765. ld l, b
  11766. ld h, $00
  11767. add hl, de
  11768. ld de, $C001 ;offset into level layout in work RAM
  11769. add hl, de
  11770.  
  11771. ld a, h
  11772. and $F0 ;make sure we're not overflowing the
  11773. cp $C0 ;layout size
  11774. jp nz, Engine_GetCollisionData_HandleOverflow
  11775.  
  11776. ld ($D354), hl ;store address of block
  11777. ld a, (hl) ;get the block value...
  11778. ld ($D353), a ;...and store here
  11779.  
  11780. ld l, a ;calculate index into collision data
  11781. ld h, $00
  11782. add hl, hl
  11783. ld de, ($D2D4) ;get pointer to the collision data
  11784. add hl, de
  11785. ld e, (hl) ;get the pointer for the current block
  11786. inc hl
  11787. ld d, (hl)
  11788. ex de, hl
  11789.  
  11790. ld a, (hl)
  11791. ld ($D35E), a ;surface type?
  11792. inc hl
  11793. ld a, (hl)
  11794. ld ($D100), a
  11795. inc hl
  11796. ld c, (hl) ;get first pointer
  11797. inc hl
  11798. ld b, (hl)
  11799.  
  11800. ex de, hl
  11801. ld a, ($D356) ;get adjusted horiz. position
  11802. and $1F ;get position on current block
  11803. ld l, a
  11804. ld h, $00
  11805. add hl, bc ;offset into the collision data
  11806. ld a, (hl)
  11807. ld ($D362), a ;store vertical projection value
  11808.  
  11809. ex de, hl
  11810. inc hl
  11811. ld c, (hl) ;get second pointer
  11812. inc hl
  11813. ld b, (hl)
  11814. ex de, hl
  11815.  
  11816. ld a, ($D358) ;get lo-byte of adjusted vertical position
  11817. and $1F ;get current position on block
  11818. ld l, a
  11819. ld h, $00
  11820. add hl, bc ;offset into collision data
  11821. ld a, (hl)
  11822. ld ($D361), a ;store horizontal projection value
  11823.  
  11824. ex de, hl
  11825. inc hl
  11826. ld c, (hl) ;get third pointer...
  11827. inc hl
  11828. ld b, (hl)
  11829. ld ($D35F), bc ;...and store here.
  11830.  
  11831. pop af ;restore previous page
  11832. ld ($D12B), a
  11833. ld ($FFFF), a
  11834. ret
  11835.  
  11836.  
  11837.  
  11838. Engine_GetCollisionValueForBlock: ;$752E
  11839. ld a, ($D12B) ;save current bank number for later
  11840. push af
  11841. ld a, :Bank30
  11842. ld ($D12B), a
  11843. ld ($FFFF), a
  11844.  
  11845. ld h, (ix+$12) ;Horizontal position in level
  11846. ld l, (ix+$11)
  11847. add hl, bc ;adjust position...
  11848. ld ($D356), hl ;...and store here
  11849.  
  11850. sla l ;calculate current block on
  11851. rl h ;the x-axis.
  11852. sla l
  11853. rl h
  11854. sla l
  11855. rl h
  11856. ld b, h ;B = current block
  11857.  
  11858. ld h, (ix+$15) ;vertical position in level
  11859. ld l, (ix+$14)
  11860. add hl, de ;adjust position...
  11861. ld de, $0012
  11862. add hl, de
  11863. ld ($D358), hl ;...and store here
  11864.  
  11865. sla l ;calculate current block on
  11866. rl h ;the y-axis.
  11867. sla l
  11868. rl h
  11869. sla l
  11870. rl h
  11871.  
  11872. ld a, h ;get the row address offset
  11873. add a, a ;for the current block
  11874. ld l, a
  11875. ld h, $00
  11876. ld de, ($D168) ;get the pointer to the level's row offsets
  11877. add hl, de
  11878. ld e, (hl)
  11879. inc hl
  11880. ld d, (hl)
  11881.  
  11882. ld l, b ;add the x-axis value
  11883. ld h, $00
  11884. add hl, de
  11885. ld de, $C001 ;offset into the level layout data
  11886. add hl, de ;HL = address of current mapping block
  11887.  
  11888. ld a, h ;make sure we're not overflowing
  11889. and $F0 ;the layout data area
  11890. cp $C0
  11891. jp nz, Engine_GetCollisionData_HandleOverflow
  11892.  
  11893. ld ($D354), hl ;store address of block here
  11894. ld a, (hl)
  11895. ld ($D353), a ;store block value here
  11896.  
  11897. ld l, a
  11898. ld h, $00
  11899. add hl, hl
  11900. ld de, ($D2D4) ;get pointer to collision data
  11901. add hl, de
  11902. ld e, (hl)
  11903. inc hl
  11904. ld d, (hl)
  11905. ld a, (de) ;get collision value for block...
  11906. ld ($D35E), a ;...and store here
  11907.  
  11908. Engine_GetCollisionData_CleanUp: ;$759F
  11909. pop af ;restore the previous frame
  11910. ld ($D12B), a
  11911. ld ($FFFF), a
  11912. ld a, ($D35E) ;restore collision value to A
  11913. ret
  11914.  
  11915. Engine_GetCollisionData_HandleOverflow: ;$75AA
  11916. ld hl, $CFFF ;store as address of block
  11917. ld ($D354), hl
  11918. ld a, $FF ;store as value of block
  11919. ld ($D353), a
  11920. ld a, $00 ;store as collision value
  11921. ld ($D35E), a
  11922. ld a, $00 ;FIXME: pointless opcode
  11923. ld ($D361), a ;store horizontal collision value
  11924. ld ($D362), a ;store vertical collision value
  11925. jp Engine_GetCollisionData_CleanUp
  11926.  
  11927. ;--------------------------------------------
  11928.  
  11929. LABEL_75C5:
  11930. res 1, (ix+$22)
  11931. ld bc, $0000
  11932. ld de, $0000
  11933. call Engine_GetCollisionDataForBlock ;collide with level tiles
  11934. call LABEL_708D
  11935. ld a, ($D35E)
  11936. and $7F
  11937. add a, a
  11938. ld l, a
  11939. ld h, $00
  11940. ld de, DATA_75E7
  11941. add hl, de
  11942. ld e, (hl)
  11943. inc hl
  11944. ld d, (hl)
  11945. ex de, hl
  11946. jp (hl)
  11947.  
  11948. DATA_75E7:
  11949. .dw LABEL_7614
  11950. .dw LABEL_7613
  11951. .dw LABEL_7613
  11952. .dw LABEL_763E
  11953. .dw LABEL_763E
  11954. .dw LABEL_763E
  11955. .dw LABEL_763E
  11956. .dw LABEL_763E
  11957. .dw LABEL_763E
  11958. .dw LABEL_763E
  11959. .dw LABEL_763E
  11960. .dw LABEL_763E
  11961. .dw LABEL_763E
  11962. .dw LABEL_763E
  11963. .dw LABEL_763E
  11964. .dw LABEL_763E
  11965. .dw LABEL_763E
  11966. .dw LABEL_763E
  11967. .dw LABEL_7613
  11968. .dw LABEL_763E
  11969. .dw LABEL_763E
  11970. .dw LABEL_763E
  11971.  
  11972. LABEL_7613:
  11973. ret
  11974.  
  11975. LABEL_7614:
  11976. bit 0, (ix+$03)
  11977. ret nz
  11978. set 4, (ix+$03)
  11979. ret
  11980.  
  11981. LABEL_761E: ;seems to be unused
  11982. bit 1, (ix+$22)
  11983. ret z
  11984. ld hl, $ff00
  11985. ld a, ($d353)
  11986. cp $f0
  11987. jr z, +
  11988. ld hl, $0100
  11989. +: ld e, (ix+$11)
  11990. ld d, (ix+$12)
  11991. add hl, de
  11992. ld (ix+$11), l
  11993. ld (ix+$12), h
  11994. ret
  11995.  
  11996. LABEL_763E:
  11997. ret
  11998.  
  11999.  
  12000. ;************************************************************
  12001. ;* Load a level's tileset (both background and sprites). *
  12002. ;************************************************************
  12003. LoadLevelTiles: ;763F
  12004. di
  12005. call ClearVRAM
  12006. ld a, (CurrentLevel) ;calculate the offset of the tileset header.
  12007. ld b, a
  12008. add a, a
  12009. add a, b
  12010. ld b, a
  12011. ld a, (CurrentAct)
  12012. add a, b
  12013. ld l, a
  12014. ld h, $00
  12015. ld c, a
  12016. ld b, $00
  12017. add hl, hl
  12018. add hl, hl
  12019. add hl, hl
  12020. xor a
  12021. sbc hl, bc
  12022. ld de, LevelTilesets
  12023. add hl, de
  12024. push hl
  12025. pop iy
  12026. ld a, (iy+0) ;bank number
  12027. call Engine_SwapFrame2
  12028. ld l, (iy+1) ;VRAM address
  12029. ld h, (iy+2)
  12030. call VDPWrite
  12031. ld l, (iy+3) ;tile source
  12032. ld h, (iy+4)
  12033. xor a
  12034. call LoadTiles
  12035. ld l, (iy+5) ;pointer to tileset entries
  12036. ld h, (iy+6)
  12037. push hl
  12038. pop iy
  12039. -: ld a, (iy+0) ;bank number/indexed tile flag
  12040. cp $FF ;continue until we reach an $FF byte
  12041. ret z
  12042. and $1F ;get the bank number and page it in
  12043. call Engine_SwapFrame2
  12044. ld l, (iy+1) ;VRAM address
  12045. ld h, (iy+2)
  12046. di
  12047. call VDPWrite ;set the VRAM pointer
  12048. ld l, (iy+3) ;tile source
  12049. ld h, (iy+4)
  12050. ld a, (iy+0) ;indexed tile flag
  12051. and $80
  12052. call LoadTiles ;decompress & copy the tiles to VRAM
  12053. ld bc, $0005 ;move to the next tileset entry
  12054. add iy, bc
  12055. jr -
  12056.  
  12057.  
  12058.  
  12059. LoadLevelPalette:
  12060. LABEL_76AD:
  12061. ld a, (CurrentLevel)
  12062. ld b, a
  12063. add a, a
  12064. add a, b
  12065. ld b, a
  12066. ld a, (CurrentAct)
  12067. add a, b
  12068. ld l, a
  12069. ld h, $00
  12070. add hl, hl
  12071. ld de, LevelPaletteValues
  12072. add hl, de
  12073. ld a, (hl) ;change palettes
  12074. ld (BgPaletteIndex), a
  12075. inc hl
  12076. ld a, (hl)
  12077. ld (FgPaletteIndex), a
  12078. ld hl, BgPaletteControl ;fade both palettes to colour.
  12079. ld (hl), $00
  12080. set 7, (hl)
  12081. inc hl
  12082. inc hl
  12083. ld (hl), $00
  12084. set 7, (hl)
  12085. ret
  12086.  
  12087. ;******************************************
  12088. ;* Loads the level select font into VRAM.
  12089. ;******************************************
  12090. LevelSelect_LoadFont: ;76D7
  12091. di
  12092. ld a, :Bank09 ;Page in the bank containing the font
  12093. call Engine_SwapFrame2
  12094. ld hl, $2400 ;Prepare to write to VRAM Address $2400
  12095. call VDPWrite
  12096. ld hl, Art_LevelSelect_Font
  12097. xor a
  12098. call LoadTiles ;Load tiles into VRAM
  12099. ret
  12100.  
  12101.  
  12102. TitleCard_LoadTiles: ;76EB
  12103. di
  12104. ld a, :Bank09
  12105. call Engine_SwapFrame2
  12106. ld hl, $2000 ;set up to write to VRAM at $2000
  12107. call VDPWrite
  12108. ld hl, Art_TitleCard_Text_Tiles
  12109. xor a
  12110. call LoadTiles
  12111. ld hl, $3020 ;set up to write to VRAM at $3020
  12112. call VDPWrite
  12113. ld hl, Art_TitleCard_Unknown
  12114. xor a
  12115. call LoadTiles
  12116. ld hl, $30C0 ;set up to write to VRAM at $30C0
  12117. call VDPWrite
  12118. ld hl, Art_TitleCard_Unknown2
  12119. xor a
  12120. call LoadTiles
  12121. ld a, :Bank07
  12122. call Engine_SwapFrame2
  12123. ld hl, $1000
  12124. call VDPWrite
  12125. ld hl, Art_Scrolling_Text_Background
  12126. xor a
  12127. call LoadTiles
  12128. ld a, (GameState) ;check to see if we need to display the score card
  12129. bit 2, a
  12130. jp z, ScoreCard_LoadMappings
  12131. ld a, :Bank25 ;load the level picture
  12132. call Engine_SwapFrame2
  12133. ld hl, $0000
  12134. call VDPWrite
  12135. ld a, (CurrentLevel) ;which level do we need the picture for?
  12136. add a, a ;calcuate the offset into the pointer array
  12137. add a, a
  12138. ld l, a
  12139. ld h, 0
  12140. ld de, TitleCard_PicturePointers
  12141. add hl, de
  12142. ld e, (hl) ;get the pointer to the mappings
  12143. inc hl
  12144. ld d, (hl)
  12145. push de
  12146. inc hl ;get the pointer to the tiles
  12147. ld e, (hl)
  12148. inc hl
  12149. ld d, (hl)
  12150. ex de, hl
  12151. xor a
  12152. call LoadTiles ;load the tiles into VRAM
  12153. pop de ;load the mappings into VRAM
  12154. ld bc, $0810
  12155. ld hl, $3B90
  12156. call Engine_LoadCardMappings
  12157. ret
  12158.  
  12159.  
  12160. TitleCard_PicturePointers:
  12161. _DATA_7761:
  12162. ; Mappings
  12163. ; | Tiles
  12164. ; |------|-----|
  12165. ;.dw $B806, $8000
  12166. ;.dw $B906, $89A0
  12167. ;.dw $BA06, $9410
  12168. ;.dw $BB06, $9DC0
  12169. ;.dw $BC06, $A446
  12170. ;.dw $BD06, $AC26
  12171. ;.dw $BE06, $B0A6
  12172.  
  12173. .dw UGZ_Title_Pic_Mappings, UGZ_Title_Pic_Art
  12174. .dw SHZ_Title_Pic_Mappings, SHZ_Title_Pic_Art
  12175. .dw ALZ_Title_Pic_Mappings, ALZ_Title_Pic_Art
  12176. .dw GHZ_Title_Pic_Mappings, GHZ_Title_Pic_Art
  12177. .dw GMZ_Title_Pic_Mappings, GMZ_Title_Pic_Art
  12178. .dw SEZ_Title_Pic_Mappings, SEZ_Title_Pic_Art
  12179. .dw CEZ_Title_Pic_Mappings, CEZ_Title_Pic_Art
  12180.  
  12181. GameOverScreen_LoadTiles: ;777D
  12182. di
  12183. ld a, :Bank15 ;switch to bank 15
  12184. call Engine_SwapFrame2
  12185. ld hl, $2000 ;write to VRAM at $2000
  12186. call VDPWrite
  12187. ld hl, GameOverScreen_Data_GameOverTiles
  12188. xor a
  12189. call LoadTiles ;load the art
  12190. ret
  12191.  
  12192. ;Used by end of game sequence?
  12193. ContinueScreen_LoadTiles: ;7791
  12194. di
  12195. ld a, :Bank15
  12196. call Engine_SwapFrame2
  12197. ld hl, $2000
  12198. call VDPWrite
  12199. ld hl, ContinueScreen_Data_ContinueTiles
  12200. xor a
  12201. call LoadTiles
  12202. ret
  12203.  
  12204. ContinueScreen_LoadNumberTiles: ;77A5
  12205. di
  12206. ld a, :Bank15
  12207. call Engine_SwapFrame2
  12208. ld hl, $2240
  12209. call VDPWrite
  12210. ld hl, ContinueScreen_Data_NumberTiles
  12211. xor a
  12212. call LoadTiles
  12213. ret
  12214.  
  12215. ScoreCard_LoadMappings: ;77B9
  12216. ld hl, $3B90
  12217. ld de, DATA_2D98
  12218. ld bc, $0205
  12219. call Engine_LoadCardMappings
  12220. ld hl, $3C10
  12221. ld de, DATA_2DAC
  12222. ld bc, $0205
  12223. call Engine_LoadCardMappings
  12224. ld hl, $3C90
  12225. ld de, DATA_2DC0
  12226. ld bc, $0205
  12227. call Engine_LoadCardMappings
  12228. ld hl, $3BA8
  12229. ld de, DATA_2DD4
  12230. ld bc, $0604
  12231. call Engine_LoadCardMappings
  12232. call LABEL_1D4F
  12233. call LABEL_1D60
  12234. call LABEL_1D6F
  12235. ret
  12236.  
  12237. LABEL_77F3_16:
  12238. ClearVRAM:
  12239. ;reset contents of VRAM.
  12240. di
  12241. call _VDP_Set_Register_1_flags_0_to_5
  12242. ld hl, $0000
  12243. ld de, $0000
  12244. ld bc, $0400
  12245. call VDPWrite
  12246. -: ld a, e
  12247. out ($BE), a
  12248. out ($BE), a
  12249. out ($BE), a
  12250. out ($BE), a
  12251. out ($BE), a
  12252. out ($BE), a
  12253. out ($BE), a
  12254. out ($BE), a
  12255. out ($BE), a
  12256. out ($BE), a
  12257. out ($BE), a
  12258. out ($BE), a
  12259. out ($BE), a
  12260. out ($BE), a
  12261. out ($BE), a
  12262. out ($BE), a
  12263. dec bc
  12264. ld a, b
  12265. or c
  12266. jr nz, -
  12267. call SetDisplayVisible
  12268. ret
  12269.  
  12270. LABEL_782D_12:
  12271. ClearPaletteRAM:
  12272. di
  12273. ;write to VRAM at address $C000
  12274. ld hl, $C000
  12275. ;write $0 to VRAM
  12276. ld de, $0000
  12277. ;loop 32 times
  12278. ld bc, $0020
  12279. call WriteToVRAM
  12280. ret
  12281.  
  12282.  
  12283. /**************************************************************************
  12284. This section deals with loading tiles for end-of-level events
  12285. (e.g. boss/signpost tiles).
  12286. ***************************************************************************/
  12287. .DEF BankNumber $D3AC ;Bank number to load tile data from
  12288. .DEF VRAMAddr $D3B0 ;Destination VRAM address
  12289. .DEF SourceAddr $D3AE ;Source ROM address (in bank 2 - i.e. >$8000)
  12290. .DEF ByteCount $D3AD ;Number of bytes to copy
  12291.  
  12292. Engine_LoadSpriteTiles:
  12293. LABEL_783B:
  12294. ld a, ($D3AB)
  12295. or a
  12296. ret z
  12297. ld a, (BankNumber)
  12298. or a
  12299. call z, LABEL_78CA ;load monitor or chaos emerald
  12300. ld a, ($D3AB)
  12301. or a
  12302. ret z
  12303. ld a, (ByteCount)
  12304. or a
  12305. call z, LABEL_78EB
  12306. di
  12307. ld a, (BankNumber)
  12308. bit 7, a
  12309. jr nz, LABEL_789D
  12310. call Engine_SwapFrame2 ;swap the correct bank into page 2
  12311. ld hl, (VRAMAddr)
  12312. call VDPWrite ;set the VRAM pointer
  12313. ld hl, (SourceAddr)
  12314. ld a, (ByteCount) ;check for >0 bytes
  12315. or a
  12316. jp z, Engine_LoadSpriteTiles_CleanUp ;bail out if nothing to do
  12317. cp $04
  12318. jr c, +
  12319. ld a, $04 ;copy 4 bitplanes
  12320. +: ld b, a
  12321. --: ld c, $20 ;do this 32 times
  12322. -: ld a, (hl) ;get a byte from the source...
  12323. out ($BE), a ;...and copy to VRAM.
  12324. inc hl
  12325. dec c
  12326. jp nz, -
  12327. djnz --
  12328. LABEL_7881:
  12329. ei
  12330. ld (SourceAddr), hl ;hl points to next tile. Store as new SourceAddr.
  12331. ld hl, (VRAMAddr)
  12332. ld bc, $0080
  12333. add hl, bc ;calculate offset of next tile in VRAM...
  12334. ld (VRAMAddr), hl ;...and store as new VRAMAddr
  12335. ld a, (ByteCount)
  12336. sub $04
  12337. ld (ByteCount), a ;subtract 4 bytes from ByteCount
  12338. ret nc ;return if >= 0
  12339. xor a
  12340. ld (ByteCount), a ;reset ByteCount to 0
  12341. ret
  12342.  
  12343. LABEL_789D:
  12344. and $1F
  12345. call Engine_SwapFrame2
  12346. ld hl, (VRAMAddr)
  12347. call VDPWrite
  12348. ld hl, (SourceAddr)
  12349. ld a, (ByteCount)
  12350. or a
  12351. jr z, LABEL_7910 ;clean up
  12352. cp $04
  12353. jr c, +
  12354. ld a, $04
  12355. +: ld b, a
  12356. ld d, $01
  12357. --: ld c, $20
  12358. -: ld e, (hl)
  12359. ld a, (de)
  12360. out ($BE), a
  12361. inc hl
  12362. dec c
  12363. jp nz, -
  12364. djnz --
  12365. jp LABEL_7881
  12366.  
  12367. ;load monitor or chaos emerald tiles
  12368. LABEL_78CA:
  12369. ld a, ($D3AB)
  12370. cp $10
  12371. jp c, LoadMonitorArt
  12372. cp $20
  12373. jp nc, LoadChaosEmerald
  12374. sub $10
  12375. add a, a
  12376. ld l, a
  12377. ld h, $00
  12378. ld de, EOL_SpriteHeaderPointers
  12379. add hl, de
  12380. ld e, (hl)
  12381. inc hl
  12382. ld d, (hl)
  12383. ld ($D3B2), de
  12384. jp LABEL_78EB
  12385.  
  12386. ;load sprite descriptor
  12387. LABEL_78EB:
  12388. ;check for $FF terminator byte
  12389. ld hl, ($D3B2)
  12390. ld a, (hl)
  12391. cp $FF
  12392. jr z, Engine_LoadSpriteTiles_CleanUp
  12393. ld (BankNumber), a ;Store bank number
  12394. inc hl
  12395. ld a, (hl)
  12396. ld (ByteCount), a
  12397. inc hl
  12398. ld e, (hl)
  12399. inc hl
  12400. ld d, (hl)
  12401. ld (VRAMAddr), de ;destination VRAM address
  12402. inc hl
  12403. ld e, (hl)
  12404. inc hl
  12405. ld d, (hl)
  12406. ld (SourceAddr), de ;source address
  12407. inc hl
  12408. ld ($D3B2), hl
  12409. ret
  12410.  
  12411. Engine_LoadSpriteTiles_CleanUp:
  12412. LABEL_7910:
  12413. xor a
  12414. ld ($D3AB),a
  12415. ld ($D3AC),a
  12416. ld (ByteCount),a
  12417. ld hl, $0000
  12418. ld (VRAMAddr),hl
  12419. ld (SourceAddr),hl
  12420. ret
  12421.  
  12422. ;Pointers to headers for various end-of-level events.
  12423. EOL_SpriteHeaderPointers:
  12424. _DATA_7924:
  12425. .dw EOL_PrisonCapsule ; $3A, $79 ;End of boss level part1
  12426. .dw EOL_PrisonCapsuleAnimals ; $41, $79 ;End of boss level part2
  12427. .dw EOL_SignPost ; $48, $79 ;End-of-level signpost
  12428. .dw EOL_GMZ_Boss ; $4F, $79 ;GMZ Boss
  12429. .dw EOL_SHZ_Boss ; $5C, $79 ;SHZ Boss
  12430. .dw EOL_ALZ_Boss ; $63, $79 ;ALZ Boss
  12431. .dw EOL_GHZ_Boss ; $70, $79 ;GHZ boss
  12432. .dw EOL_UGZ_Boss ; $77, $79 ;UGZ boss
  12433. .dw EOL_SEZ_Boss ; $7E, $79
  12434. .dw EOL_Unknown_2 ; $8B, $79
  12435. .dw EOL_Unknown_3 ; $8C, $79
  12436.  
  12437. EOL_SpriteHeaders:
  12438. ; Bank VRAM Address Terminator
  12439. ; | Count | ROM Addr. |
  12440. ; |----|----|---------|---------|----|
  12441.  
  12442. EOL_PrisonCapsule:
  12443. .db :Art_Prison_Capsule
  12444. .db $46
  12445. .dw $0C40
  12446. .dw Art_Prison_Capsule
  12447. .db $FF ;End of boss prison capsule
  12448.  
  12449. EOL_PrisonCapsuleAnimals: ;End of boss prison capsule animals
  12450. .db :Art_Animals
  12451. .db $30
  12452. .dw $1500
  12453. .dw Art_Animals
  12454. .db $FF
  12455.  
  12456. EOL_SignPost: ;End-of-level signpost
  12457. .db :Art_Signpost
  12458. .db $64
  12459. .dw $0C40
  12460. .dw Art_Signpost
  12461. .db $FF
  12462.  
  12463. EOL_GMZ_Boss:
  12464. .db :Art_GMZ_Boss
  12465. .db $4A
  12466. .dw $0C40
  12467. .dw Art_GMZ_Boss
  12468. .db :Art_GMZ_Boss + $80
  12469. .db $4A
  12470. .dw $1580
  12471. .dw Art_GMZ_Boss
  12472. .db $FF ;$794A - GMZ Boss
  12473.  
  12474. EOL_SHZ_Boss:
  12475. .db :Art_SHZ_Boss
  12476. .db $6E
  12477. .dw $0C40
  12478. .dw Art_SHZ_Boss ;$8940
  12479. .db $FF ;$795C - SHZ Boss
  12480.  
  12481. EOL_ALZ_Boss:
  12482. .db :Art_ALZ_Boss
  12483. .db $4C
  12484. .dw $0C40
  12485. .dw Art_ALZ_Boss
  12486. .db :Art_ALZ_Boss + $80
  12487. .db $4C
  12488. .dw $15C0
  12489. .dw Art_ALZ_Boss
  12490. .db $FF ;$7963 - ALZ Boss
  12491.  
  12492. EOL_GHZ_Boss: ;$7970 - GHZ Boss
  12493. .db :Art_GHZ_Boss ;$1A
  12494. .db $96
  12495. .dw $0C40
  12496. .dw Art_GHZ_Boss ;$A080
  12497. .db $FF
  12498.  
  12499. EOL_UGZ_Boss: ;$7977 - UGZ Boss
  12500. .db :Art_Boss_UGZ
  12501. .db $6A
  12502. .dw $0C40
  12503. .dw Art_Boss_UGZ ;$A102
  12504. .db $FF
  12505.  
  12506. EOL_SEZ_Boss:
  12507. .db :Art_SilverSonic
  12508. .db $44
  12509. .dw $0C40
  12510. .dw Art_SilverSonic
  12511. .db :Art_SilverSonic + $80
  12512. .db $44
  12513. .dw $14C0
  12514. .dw Art_SilverSonic
  12515. .db $FF
  12516.  
  12517. EOL_Unknown_2:
  12518. .db $FF
  12519.  
  12520. EOL_Unknown_3:
  12521. .db :Art_Tails
  12522. .db $48
  12523. .dw $0C40
  12524. .dw Art_Tails ;$B48A
  12525. .db $FF
  12526.  
  12527. LoadChaosEmerald:
  12528. LABEL_7993:
  12529. di
  12530. ld a, :Bank20
  12531. call Engine_SwapFrame2
  12532. ld a, ($D3AB)
  12533. sub $20
  12534. add a, a
  12535. ld l, a
  12536. ld h, $00
  12537. ld de, ChaosEmeraldData
  12538. add hl, de
  12539. ld e, (hl)
  12540. inc hl
  12541. ld d, (hl)
  12542. ex de, hl
  12543. ld de, $0A80
  12544. ld bc, $0080
  12545. call CopyToVRAM
  12546. ei
  12547. jp LABEL_7910
  12548.  
  12549. .include "src\chaos_emerald_pointers.asm"
  12550.  
  12551.  
  12552. LoadMonitorArt: ;79C7
  12553. di
  12554. ld a, :Bank07
  12555. call Engine_SwapFrame2
  12556. ld a, ($D3AB) ;which monitor needs loading?
  12557. add a, a
  12558. ld l, a
  12559. ld h, $00
  12560. ld de, Monitor_Art_Pointers-2
  12561. add hl, de
  12562. ld e, (hl) ;fetch the pointer to the tiles
  12563. inc hl
  12564. ld d, (hl)
  12565. ex de, hl ;copy the tiles to VRAM
  12566. ld de, $0A80
  12567. ld bc, $00C0
  12568. call CopyToVRAM
  12569. ei
  12570. jp LABEL_7910
  12571.  
  12572. Monitor_Art_Pointers: ;$79E9
  12573. .dw Art_Monitor_0
  12574. .dw Art_Monitor_1
  12575. .dw Art_Monitor_2
  12576. .dw Art_Monitor_3
  12577. .dw Art_Monitor_4
  12578. .dw Art_Monitor_5
  12579. .dw Art_Monitor_6
  12580. .dw Art_Monitor_7
  12581. .dw Art_Monitor_8
  12582.  
  12583.  
  12584. LevelTilesets: ;$79FB
  12585. .include "src\zone_tilesets.asm"
  12586.  
  12587. LevelPaletteValues:
  12588. .db $0E, $09
  12589. .db $0E, $09
  12590. .db $0E, $09
  12591. ;shz
  12592. .db $0F, $05
  12593. .db $10, $06
  12594. .db $0F, $05
  12595. ;alz
  12596. .db $11, $07
  12597. .db $12, $08
  12598. .db $11, $07
  12599. ;ghz
  12600. .db $13, $04
  12601. .db $13, $04
  12602. .db $13, $04
  12603.  
  12604. .db $14, $0A
  12605. .db $14, $0A
  12606. .db $14, $0A
  12607.  
  12608. .db $15, $0B
  12609. .db $15, $0B
  12610. .db $15, $0B
  12611.  
  12612. .db $16, $0C
  12613. .db $16, $0C
  12614. .db $17, $0D
  12615.  
  12616. .db $24, $25
  12617. .db $24, $25
  12618. .db $24, $25
  12619.  
  12620. .db $16, $0C
  12621. .db $16, $0C
  12622. .db $17, $0D
  12623.  
  12624. .db $1A, $18
  12625. .db $1B, $19
  12626. .db $1B, $19
  12627.  
  12628.  
  12629.  
  12630. UpdateCyclingPalettes: ;$7CE9
  12631. LABEL_7CE9_215:
  12632. ;check to see if we have palettes that should cycle
  12633. ld a, (BgPaletteControl)
  12634. or a
  12635. ret nz
  12636. ld iy, $D4A6 ;first cycling palette index
  12637. ld b, $02 ;update 2 cycling palettes
  12638. -: push bc
  12639. call UpdateCyclingPaletteBank
  12640. ld bc, $0008
  12641. add iy, bc
  12642. pop bc
  12643. djnz -
  12644. ret
  12645.  
  12646. UpdateCyclingPaletteBank: ;$7D01
  12647. ld a, (iy+0) ;get palette index number
  12648. add a, a
  12649. ld l, a
  12650. ld h, $00
  12651. ld de, UpdateCyclingPalette_JumpVectors
  12652. add hl, de
  12653. ld e, (hl)
  12654. inc hl
  12655. ld d, (hl)
  12656. ex de, hl
  12657. jp (hl)
  12658.  
  12659.  
  12660. ;Jump vectors to code that updates a specific cycling palette
  12661. UpdateCyclingPalette_JumpVectors:
  12662. .dw UpdateCyclingPalette_DoNothing
  12663. .dw UpdateCyclingPalette_DoNothing2
  12664. .dw UpdateCyclingPalette_Rain ;SHZ2 rain palette
  12665. .dw UpdateCyclingPalette_Unknown ;SHZ2 unknown
  12666. .dw UpdateCyclingPalette_Lava ;UGZ lava palette
  12667. .dw UpdateCyclingPalette_Water ;ALZ water palette
  12668. .dw UpdateCyclingPalette_Unknown2
  12669. .dw UpdateCyclingPalette_Conveyor ;GMZ conveyor & wheel palette
  12670. .dw UpdateCyclingPalette_Orb ;CEZ1 orb palette
  12671. .dw UpdateCyclingPalette_Lightning ;CEZ3 boss lightening palette
  12672. .dw UpdateCyclingPalette_Lightning2 ;CEZ3 boss lightening palette
  12673. .dw UpdateCyclingPalette_WallLighting ;CEZ3 wall lighting
  12674. .dw UpdateCyclingPalette_Orb ;CEZ1 orb palette
  12675. .dw LABEL_7F46 ;ending sequence
  12676. .dw LABEL_7F7E
  12677. .dw UpdateCyclingPalette_DoNothing
  12678.  
  12679. UpdateCyclingPalette_DoNothing:
  12680. ret
  12681.  
  12682. LABEL_7D32:
  12683. xor a ;clear the palette indices
  12684. ld ($D4A6), a
  12685. ld ($D4AE), a
  12686. ld hl, $0000
  12687. ld ($D397), hl
  12688. ret
  12689.  
  12690. UpdateCyclingPalette_DoNothing2:
  12691. ret
  12692.  
  12693. ;Update the cycling palette for SHZ2's rain
  12694. UpdateCyclingPalette_Rain:
  12695. LABEL_7D41:
  12696. inc (iy+$03)
  12697. ld a, (iy+$03)
  12698. cp $04
  12699. ret c
  12700. ld (iy+$03), $00
  12701. ld a, (iy+$02)
  12702. inc a
  12703. cp $03
  12704. jr c, $01
  12705. xor a
  12706. ld (iy+$02), a
  12707. add a, a
  12708. add a, (iy+$02)
  12709. ld e, a
  12710. ld d, $00
  12711. ld hl, DATA_B30_AF4A
  12712. add hl, de
  12713. ld de, $D4CA
  12714. ld bc, $0003
  12715. ldir
  12716. ld a, $FF
  12717. ld (PaletteUpdatePending), a
  12718. ret
  12719.  
  12720. ;unknown palette. gets called in SHZ2
  12721. UpdateCyclingPalette_Unknown:
  12722. LABEL_7D73:
  12723. inc (iy+$03)
  12724. ld a, (iy+$03)
  12725. cp $78
  12726. ret c
  12727. inc (iy+$02)
  12728. ld a, $3C
  12729. ld bc, $330F
  12730. bit 1, (iy+$02)
  12731. jr nz, $02
  12732. ld a, $10
  12733. ld bc, $1010
  12734. ld hl, $D4D1
  12735. ld (hl),a
  12736. ld a, $FF
  12737. ld (PaletteUpdatePending),a
  12738. ld a, (iy+$02)
  12739. cp $10
  12740. ret c
  12741. ld (iy+$03), $00
  12742. ld (iy+$02), $00
  12743. ret
  12744.  
  12745. ;Update the cycling palette for UGZ's lava
  12746. UpdateCyclingPalette_Lava:
  12747. LABEL_7DA7:
  12748. inc (iy+$03)
  12749. ld a, (iy+$03)
  12750. cp $08
  12751. ret c ;update every 8th frame
  12752. ld (iy+$03), $00
  12753. ld a, (iy+$02)
  12754. inc a
  12755. cp $03
  12756. jr c, +
  12757. xor a ;reset counter
  12758. +: ld (iy+$02), a
  12759. add a, a
  12760. add a, (iy+$02)
  12761. ld e, a
  12762. ld d, $00
  12763. ld hl, DATA_B30_AF41
  12764. add hl, de
  12765. ld de, $D4D3 ;update 3 colours in CRAM
  12766. ld bc, $0003
  12767. ldir
  12768. ld a, $FF
  12769. ld (PaletteUpdatePending), a
  12770. ret
  12771.  
  12772. ;update the cycling palette for ALZ's water
  12773. UpdateCyclingPalette_Water:
  12774. LABEL_7DD9:
  12775. inc (iy+$03)
  12776. ld a, (iy+$03)
  12777. cp $08
  12778. ret c
  12779. ld (iy+$03), $00
  12780. ld a, (iy+$02)
  12781. inc a
  12782. cp $03
  12783. jr c, $01
  12784. xor a
  12785. ld (iy+$02), a
  12786. add a,a
  12787. add a, (iy+$02)
  12788. ld e,a
  12789. ld d, $00
  12790. ld hl, DATA_B30_AF53
  12791. add hl, de
  12792. ld de, $D4D3
  12793. ld bc, $0003
  12794. ldir
  12795. ld a, $FF
  12796. ld (PaletteUpdatePending), a
  12797. ret
  12798.  
  12799. ;Update the GMZ conveyor belt and wheel palette
  12800. UpdateCyclingPalette_Conveyor:
  12801. LABEL_7E0B:
  12802. inc (iy+$03)
  12803. ld a, (iy+$03)
  12804. cp $04
  12805. ret c
  12806. ld (iy+$03), $00
  12807. ld a, (iy+$02)
  12808. inc a
  12809. cp $03
  12810. jr c, $01
  12811. xor a
  12812. ld (iy+$02), a
  12813. add a,a
  12814. add a, (iy+$02)
  12815. ld e, a
  12816. ld d, $00
  12817. ld hl, DATA_B30_AF5C
  12818. add hl, de
  12819. ld de, $D4D3
  12820. ld bc, $0003
  12821. ldir
  12822. ld a, $FF
  12823. ld (PaletteUpdatePending), a
  12824. ret
  12825.  
  12826. UpdateCyclingPalette_Unknown2:
  12827. LABEL_7E3D:
  12828. inc (iy+$03)
  12829. ld a, (iy+$03)
  12830. cp $04
  12831. ret c
  12832. ld (iy+$03), $00
  12833. ld a, (iy+$02)
  12834. inc a
  12835. cp $0C
  12836. jr c, $01
  12837. xor a
  12838. ld (iy+$02), a
  12839. add a, a
  12840. ld e, a
  12841. ld d, $00
  12842. ld hl, DATA_B30_AF65
  12843. add hl, de
  12844. ld de, $D4D1
  12845. ld bc, $0002
  12846. ldir
  12847. ld a, $FF
  12848. ld (PaletteUpdatePending), a
  12849. ret
  12850.  
  12851. ;Update the CEZ3 wall lights
  12852. UpdateCyclingPalette_WallLighting:
  12853. LABEL_7E6C:
  12854. inc (iy+$03)
  12855. ld a, (iy+$03)
  12856. cp $04
  12857. ret c
  12858. ld (iy+$03), $00
  12859. ld a, (iy+$02)
  12860. inc a
  12861. cp $06
  12862. jr c, $01
  12863. xor a
  12864. ld (iy+$02), a
  12865. ld e, a
  12866. ld d, $00
  12867. ld hl, DATA_B30_AF7D
  12868. add hl, de
  12869. ld a, (hl)
  12870. ld de, $D4CA
  12871. ld (de), a
  12872. ld a, $FF
  12873. ld (PaletteUpdatePending), a
  12874. ret
  12875.  
  12876. ;update the CEZ1 orb cycling palette
  12877. UpdateCyclingPalette_Orb:
  12878. LABEL_7E97:
  12879. inc (iy+$03)
  12880. ld a, (iy+$03)
  12881. cp $04
  12882. ret c
  12883. ld (iy+$03), $00
  12884. ld a, (iy+$02)
  12885. inc a
  12886. cp $0e
  12887. jr c, $01
  12888. xor a
  12889. ld (iy+$02), a
  12890. add a, a
  12891. ld e, a
  12892. ld d, $00
  12893. ld hl, DATA_B30_AF83
  12894. add hl, de
  12895. ld de, $D4D3
  12896. ld bc, $0002
  12897. ldir
  12898. ld a, $FF
  12899. ld (PaletteUpdatePending), a
  12900. ret
  12901.  
  12902. ;Update palette for CEZ3 boss lightening
  12903. UpdateCyclingPalette_Lightning:
  12904. LABEL_7EC6:
  12905. inc (iy+$03)
  12906. ld a, (iy+$03)
  12907. cp $02
  12908. ret c
  12909. ld (iy+$03), $00
  12910. ld a, (iy+$02)
  12911. inc a
  12912. cp $03
  12913. jr c, $01
  12914. xor a
  12915. ld (iy+$02), a
  12916. add a, a
  12917. add a, a
  12918. add a, a
  12919. add a, a
  12920. ld e, a
  12921. ld d, $00
  12922. ld hl, DATA_B30_AF9F
  12923. add hl, de
  12924. ld de, ShadowedCRAM
  12925. ld bc, $0010
  12926. ldir
  12927. ld a, $FF
  12928. ld (PaletteUpdatePending), a
  12929. ret
  12930.  
  12931. ;Update palette for CEZ3 boss lightning (part 2)
  12932. UpdateCyclingPalette_Lightning2:
  12933. LABEL_7EF8:
  12934. ld hl, DATA_B30_AF9F
  12935. ld de, ShadowedCRAM
  12936. ld bc, $0010
  12937. ldir
  12938. ld a, $FF
  12939. ld (PaletteUpdatePending), a
  12940. ld (iy+$00), $00
  12941. ld (iy+$03), $00
  12942. ld (iy+$02), $00
  12943. ret
  12944.  
  12945. UpdateCyclingPalette_ScrollingText: ;TODO: check this
  12946. LABEL_7F15:
  12947. ld a, ($D12F)
  12948. and $03
  12949. ret nz
  12950. ld a, ($D12B) ;save current bank so that we can page it back in later
  12951. push af
  12952. ld a, :Bank30
  12953. call Engine_SwapFrame2
  12954. ld hl, DATA_B30_AFCF
  12955. ld a, ($D12F) ;which palette?
  12956. bit 2, a
  12957. jr z, +
  12958. ld hl, DATA_B30_AFD5 ;copy the palette to working copy of CRAM
  12959. +: ld de, ShadowedCRAM + $09
  12960. ld bc, $0006
  12961. ldir
  12962. ld a, $FF ;flag for a palette update
  12963. ld (PaletteUpdatePending), a
  12964. pop af ;page the previous bank bank in
  12965. ld ($D12B), a
  12966. ld ($FFFF), a
  12967. ret
  12968.  
  12969. LABEL_7F46: ;update the palette for the end sequence
  12970. ld l, (iy+$04)
  12971. ld h, (iy+$05)
  12972. inc hl
  12973. ld (iy+$04), l
  12974. ld (iy+$05), h
  12975. ld bc, $0280
  12976. xor a
  12977. sbc hl, bc
  12978. ret c
  12979. ld (iy+$04), $00
  12980. ld (iy+$05), $00
  12981. inc (iy+$02)
  12982. ld a, (iy+$02)
  12983. cp $0A
  12984. jr c, LABEL_7F74
  12985. xor a
  12986. ld (iy+$00), a
  12987. ld (iy+$02), a
  12988. ret
  12989.  
  12990. LABEL_7F74:
  12991. add a, $25 ;load palette $25
  12992. ld hl, BgPaletteControl
  12993. set 5, (hl) ;reset palette
  12994. inc hl
  12995. ld (hl), a
  12996. ret
  12997.  
  12998. LABEL_7F7E:
  12999. inc (iy+$03)
  13000. ld a, (iy+$03)
  13001. cp $06
  13002. ret c
  13003. ld (iy+$03), $00
  13004. ld a, (iy+$02)
  13005. inc a
  13006. cp $06
  13007. jr c, $01
  13008. xor a
  13009. ld (iy+$02), a
  13010. add a, a
  13011. ld e, a
  13012. ld d, $00
  13013. ld hl, DATA_B30_AFDB
  13014. add hl, de
  13015. ld a, (hl)
  13016. ld ($D4CF), a
  13017. inc hl
  13018. ld a, (hl)
  13019. ld ($D4D5), a
  13020. ld a, $FF
  13021. ld ($D4EA), a
  13022. ret
  13023.  
  13024. LABEL_7FAE_77:
  13025. ld a, ($D12F)
  13026. and $07
  13027. ret nz
  13028. ld a, ($D351)
  13029. inc a
  13030. cp $06
  13031. jr c, LoadRingArt
  13032. xor a
  13033. LoadRingArt:
  13034. LABEL_7FBD_78:
  13035. ld ($D351), a
  13036. add a, a ;calculate the offset from the source address
  13037. add a, a
  13038. add a, a
  13039. add a, a
  13040. add a, a
  13041. ld l, a
  13042. ld h, $00
  13043. add hl, hl
  13044. add hl, hl
  13045. ld de, (RingArt_SrcAddress)
  13046. add hl, de
  13047. ld de, (RingArt_DestAddress)
  13048. ld a, d
  13049. or e
  13050. ret z
  13051. ld a, :Bank29 ;page in the bank with the ring art
  13052. ld ($FFFF), a
  13053. ld a, e ;prepare to write to VRAM at DestAddress
  13054. out ($BF), a
  13055. ld a, d
  13056. or $40
  13057. out ($BF), a
  13058. ld b, $80 ;write $80 bytes from HL
  13059. ld c, $BE ;to port $BE
  13060. otir
  13061. ret
  13062.  
  13063.  
  13064.  
  13065. .ORG $3FF0 ;offset within current bank
  13066.  
  13067. ROM_HEADER: ;$7FF0
  13068. .db "TMR SEGA"
  13069. .db $00, $00 ;reserved
  13070. .db $99, $5F ;checksum
  13071. .db $15, $90, $00 ;product code/version
  13072. .db $40 ;region code/rom size
  13073.  
  13074.  
  13075. ;===========================================================================
  13076. ; Bank 02 - Sound Driver
  13077. ;===========================================================================
  13078. .include "src\sound_driver.asm"
  13079.  
  13080. ;===========================================================================
  13081. ; Includes for remaining banks.
  13082. ;===========================================================================
  13083. .include "src\includes\banks.asm"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement