Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

2166 Zeilen
44KB

  1. ; ===========================================================================
  2. ; BLAST RUNNER - an auto-scrolling run-and-gun for the Commodore 64
  3. ;
  4. ; Controls: SPACE jump, CTRL shoot, A / D move left / right
  5. ; (joystick port 2 also works: up = jump, fire = shoot)
  6. ; Build : build.bat (python gfx.py, ca65, ld65)
  7. ;
  8. ; Technique notes
  9. ; - Multicolor text mode, 38 columns, hardware X fine scroll ($D016),
  10. ; 1 pixel per frame. Row 0 (HUD) is kept unscrolled by a raster split.
  11. ; - Coarse scroll copies rows top to bottom just after the frame IRQ,
  12. ; staying ahead of the beam. The next column is generated 4 frames
  13. ; earlier into a buffer, so the coarse-scroll frame only copies.
  14. ; - No multiplexing: sprite 0 player, 1-2 player bullets, 3-6 enemies
  15. ; and explosions, 7 enemy shot or bomb.
  16. ; - Terrain collision reads the screen: chars >= CH_SOLID are solid
  17. ; from above (grass and platforms). Object collision uses boxes in
  18. ; half-pixel X units so everything fits in 8 bits.
  19. ; ===========================================================================
  20. .include "gfx.inc"
  21. ; ---------------------------------------------------------------- hardware
  22. SCREEN = $0400
  23. COLRAM = $D800
  24. CHARSET = $2800
  25. SPRPTR = SCREEN + $03F8
  26. VIC_SPRXHI = $D010
  27. VIC_CTRL1 = $D011
  28. VIC_RASTER = $D012
  29. VIC_SPREN = $D015
  30. VIC_CTRL2 = $D016
  31. VIC_MEM = $D018
  32. VIC_IRQ = $D019
  33. VIC_IRQEN = $D01A
  34. VIC_SPRPRI = $D01B
  35. VIC_SPRMC = $D01C
  36. VIC_SPRDW = $D01D
  37. VIC_SPRDH = $D017
  38. VIC_BORDER = $D020
  39. VIC_BG0 = $D021
  40. VIC_BG1 = $D022
  41. VIC_BG2 = $D023
  42. VIC_SPRMC0 = $D025
  43. VIC_SPRMC1 = $D026
  44. VIC_SPR0COL = $D027
  45. SID = $D400
  46. CIA1_PRA = $DC00
  47. CIA1_PRB = $DC01
  48. CIA1_ICR = $DC0D
  49. CIA2_ICR = $DD0D
  50. IRQVEC = $0314
  51. KERNAL_IRQ_EXIT = $EA81
  52. ; ---------------------------------------------------------------- tuning
  53. PX_MIN = 32 ; player sprite X limits (screen relative)
  54. PX_MAX = 216
  55. PX_START = 64
  56. PY_MIN = 40
  57. GROUND_ROW = 21 ; grass row
  58. PLAT1_ROW = 17
  59. PLAT2_ROW = 13
  60. PIT_Y = 222 ; player sprite Y that counts as "fell in"
  61. GRAV = $0030 ; 8.8 px/frame^2 (0.19)
  62. JUMP_VEL = $FC40 ; -3.75 px/frame -> ~37 px high
  63. CUT_VEL = $FE80 ; releasing SPACE caps the rise at -1.5
  64. BOUNCE_VEL = $FB80 ; pit bounce while invulnerable
  65. MAX_FALL = $0400
  66. COYOTE = 5 ; frames after leaving a ledge you may jump
  67. JUMP_BUF = 6 ; frames a jump press is remembered
  68. FIRE_RATE = 11
  69. BUL_SPEED = 7
  70. BUL_LIFE = 30
  71. SPAWN_X = 340
  72. ; sprite Y that puts the feet on top of a character row
  73. .define ROW_Y(r) (30 + (r) * 8)
  74. ; key bits
  75. K_LEFT = $01
  76. K_RIGHT = $02
  77. K_JUMP = $04
  78. K_FIRE = $08
  79. ; states
  80. ST_TITLE = 0
  81. ST_PLAY = 1
  82. ST_DYING = 2
  83. ST_OVER = 3
  84. ; object types
  85. T_NONE = 0
  86. T_PLAYER = 1
  87. T_PBUL = 2
  88. T_EXPL = 3
  89. T_SOLDIER = 4 ; types >= T_SOLDIER are hostile
  90. T_DRONE = 5
  91. T_ESHOT = 6
  92. T_BOMB = 7
  93. ; sound effects
  94. SFX_SHOT = 0
  95. SFX_JUMP = 1
  96. SFX_BOOM = 2
  97. SFX_ESHOT = 3
  98. SFX_DIE = 4
  99. SFX_BOMB = 5
  100. ; colours
  101. COL_PLAYER = 13
  102. COL_SOLDIER = 2
  103. COL_DRONE = 15
  104. COL_EXPL = 7
  105. ; ---------------------------------------------------------------- macros
  106. .macro SET_PTRS row, col, str
  107. lda #<(SCREEN+(row)*40+(col))
  108. sta ptr
  109. lda #>(SCREEN+(row)*40+(col))
  110. sta ptr+1
  111. lda #<str
  112. sta ptr2
  113. lda #>str
  114. sta ptr2+1
  115. .endmacro
  116. .macro PRINT_AT row, col, str
  117. SET_PTRS row, col, str
  118. jsr print_hires
  119. .endmacro
  120. ; ---------------------------------------------------------------- zero page
  121. .segment "ZEROPAGE"
  122. ptr: .res 2
  123. ptr2: .res 2
  124. ptr3: .res 2
  125. state: .res 1
  126. frame_flag: .res 1
  127. frame: .res 1
  128. scrolling: .res 1
  129. fine: .res 1
  130. fine_cur: .res 1 ; read by the line-58 IRQ
  131. tmp: .res 1
  132. tmp2: .res 1
  133. seed: .res 2
  134. keys: .res 1
  135. keys_prev: .res 1
  136. keys_new: .res 1
  137. timer: .res 1
  138. ; player
  139. px: .res 1
  140. py_f: .res 1
  141. py: .res 1
  142. pvy_l: .res 1
  143. pvy_h: .res 1
  144. on_ground: .res 1
  145. coyote: .res 1
  146. jbuf: .res 1
  147. fire_cd: .res 1
  148. panim: .res 1
  149. invuln: .res 1
  150. lives: .res 1
  151. ; score (BCD, s0 = lowest two digits)
  152. s0: .res 1
  153. s1: .res 1
  154. s2: .res 1
  155. h0: .res 1
  156. h1: .res 1
  157. h2: .res 1
  158. ; world generator
  159. gen_col: .res 1
  160. g_on: .res 1
  161. g_len: .res 1
  162. g_age: .res 1
  163. p1_on: .res 1
  164. p1_len: .res 1
  165. p1_age: .res 1
  166. p2_on: .res 1
  167. p2_len: .res 1
  168. p2_age: .res 1
  169. b_h: .res 1
  170. b_len: .res 1
  171. colpar: .res 1
  172. spawn_cnt: .res 1
  173. level: .res 1
  174. dist: .res 1
  175. ; solid_at arguments
  176. sx_l: .res 1
  177. sx_h: .res 1
  178. sy: .res 1
  179. t0: .res 1
  180. t1: .res 1
  181. sfx_sx: .res 1
  182. ; ---------------------------------------------------------------- BSS
  183. .segment "BSS"
  184. o_type: .res 8
  185. o_xl: .res 8
  186. o_xh: .res 8
  187. o_y: .res 8
  188. o_ptr: .res 8
  189. o_col: .res 8
  190. o_t: .res 8
  191. o_t2: .res 8
  192. o_base: .res 8
  193. o_bl: .res 8 ; collision box, X in half pixels
  194. o_w: .res 8
  195. o_bt: .res 8
  196. o_h: .res 8
  197. colbuf: .res 25
  198. sv_t: .res 3
  199. sv_freq: .res 3
  200. sv_delta: .res 3
  201. sv_wave: .res 3
  202. ; ---------------------------------------------------------------- BASIC stub
  203. .segment "LOADADDR"
  204. .word $0801
  205. .segment "CODE"
  206. basic: .word basic_end, 10 ; 10 SYS 2061
  207. .byte $9E, "2061", 0
  208. basic_end:
  209. .word 0
  210. ; ===========================================================================
  211. start:
  212. sei
  213. cld
  214. ldx #$FF
  215. txs
  216. lda #$7F ; CIA interrupts off (no KERNAL keyscan)
  217. sta CIA1_ICR
  218. sta CIA2_ICR
  219. lda CIA1_ICR
  220. lda CIA2_ICR
  221. lda #$FF ; CIA1 port A out (keyboard columns)
  222. sta $DC02
  223. lda #$00 ; port B in (keyboard rows)
  224. sta $DC03
  225. jsr copy_charset
  226. jsr init_sid
  227. ; --- VIC setup
  228. lda #0
  229. sta VIC_BORDER
  230. lda #6 ; night sky
  231. sta VIC_BG0
  232. lda #9 ; %01 brown
  233. sta VIC_BG1
  234. lda #11 ; %10 dark grey
  235. sta VIC_BG2
  236. lda #$1A ; screen $0400, charset $2800
  237. sta VIC_MEM
  238. lda #$1B
  239. sta VIC_CTRL1
  240. lda #$10 ; multicolor, 38 columns, xscroll 0
  241. sta VIC_CTRL2
  242. lda #0
  243. sta VIC_SPREN
  244. sta VIC_SPRPRI
  245. sta VIC_SPRDW
  246. sta VIC_SPRDH
  247. sta VIC_SPRMC0 ; black
  248. lda #10
  249. sta VIC_SPRMC1 ; pink
  250. lda #0
  251. sta h0
  252. sta h1
  253. sta h2
  254. sta frame_flag
  255. sta frame
  256. lda #$FF
  257. sta keys_prev ; ignore keys held at start-up
  258. lda #$5A
  259. sta seed
  260. lda #$C3
  261. sta seed+1
  262. ; --- raster IRQ
  263. lda #<irq_bottom
  264. sta IRQVEC
  265. lda #>irq_bottom
  266. sta IRQVEC+1
  267. lda #251
  268. sta VIC_RASTER
  269. lda #1
  270. sta VIC_IRQEN
  271. lda #$FF
  272. sta VIC_IRQ
  273. jsr new_game
  274. cli
  275. ; ===========================================================================
  276. main_loop:
  277. lda frame_flag
  278. beq main_loop
  279. lda #0
  280. sta frame_flag
  281. inc frame
  282. ; sprites first: we are in the lower border, nothing is drawn yet
  283. jsr show_sprites
  284. ; ---- scrolling
  285. lda scrolling
  286. beq @no_scroll
  287. dec fine
  288. bpl @fine_only
  289. lda #7
  290. sta fine
  291. sta fine_cur ; must be set before raster line 58
  292. jsr shift_screen
  293. lda #39
  294. sta gen_col
  295. jsr write_column
  296. jmp @no_scroll
  297. @fine_only:
  298. lda fine
  299. sta fine_cur
  300. cmp #4 ; build the next column ahead of time
  301. bne @no_scroll
  302. jsr gen_column
  303. jsr column_tick
  304. @no_scroll:
  305. jsr read_input
  306. .ifdef AUTOTEST
  307. jsr autopilot
  308. .endif
  309. lda state
  310. cmp #ST_TITLE
  311. bne :+
  312. jmp do_title
  313. : cmp #ST_PLAY
  314. bne :+
  315. jmp do_play
  316. : cmp #ST_DYING
  317. bne :+
  318. jmp do_dying
  319. : jmp do_over
  320. ; ---------------------------------------------------------------- TITLE
  321. do_title:
  322. lda keys_new
  323. and #(K_JUMP|K_FIRE)
  324. beq frame_end
  325. jsr start_game
  326. jmp frame_end
  327. ; ---------------------------------------------------------------- PLAY
  328. do_play:
  329. jsr update_player
  330. jsr update_objects
  331. jsr collisions
  332. jmp frame_end
  333. ; ---------------------------------------------------------------- DYING
  334. do_dying:
  335. dec timer
  336. lda timer
  337. cmp #74
  338. bcc @hide
  339. sbc #74 ; 0..15, high = early
  340. lsr
  341. lsr
  342. tay
  343. lda expl_seq,y
  344. sta o_ptr
  345. lda frame
  346. and #2
  347. lsr
  348. clc
  349. adc #7 ; flicker yellow/orange
  350. sta o_col
  351. jmp @cont
  352. @hide:
  353. lda #T_NONE
  354. sta o_type
  355. @cont:
  356. lda timer
  357. bne @go
  358. lda lives
  359. beq @over
  360. jsr respawn
  361. jmp @go
  362. @over:
  363. jsr game_over
  364. jmp frame_end
  365. @go:
  366. jsr update_objects
  367. jsr collisions
  368. jmp frame_end
  369. ; ---------------------------------------------------------------- GAME OVER
  370. do_over:
  371. lda timer
  372. beq @ready
  373. dec timer
  374. bne frame_end
  375. PRINT_AT 10, 10, msg_again
  376. jmp frame_end
  377. @ready:
  378. lda keys_new
  379. and #(K_JUMP|K_FIRE)
  380. beq frame_end
  381. jsr new_game
  382. ; fall through
  383. frame_end:
  384. jsr update_sfx
  385. jmp main_loop
  386. ; ===========================================================================
  387. ; Game flow
  388. ; ===========================================================================
  389. new_game:
  390. lda #ST_TITLE
  391. sta state
  392. lda #0
  393. sta scrolling
  394. sta s0
  395. sta s1
  396. sta s2
  397. jsr clear_objects
  398. lda #3
  399. sta lives
  400. jsr build_world
  401. jsr init_colors
  402. jsr draw_hud
  403. PRINT_AT 4, 8, msg_title
  404. PRINT_AT 7, 6, msg_ctrl1
  405. PRINT_AT 9, 5, msg_ctrl2
  406. PRINT_AT 11, 10, msg_start
  407. rts
  408. start_game:
  409. lda frame ; seed from player timing
  410. eor #$A5
  411. sta seed
  412. ora #1
  413. sta seed+1
  414. lda #0
  415. sta s0
  416. sta s1
  417. sta s2
  418. sta level
  419. sta dist
  420. lda #3
  421. sta lives
  422. lda #20
  423. sta spawn_cnt
  424. jsr clear_objects
  425. jsr build_world
  426. jsr init_colors
  427. jsr draw_hud
  428. lda #PX_START
  429. sta px
  430. lda #ROW_Y(GROUND_ROW)
  431. sta py
  432. lda #0
  433. sta py_f
  434. sta pvy_l
  435. sta pvy_h
  436. sta jbuf
  437. sta fire_cd
  438. sta invuln
  439. lda #T_PLAYER
  440. sta o_type
  441. lda #SP_PRUN_A
  442. sta o_ptr
  443. jsr set_player_obj
  444. lda #ST_PLAY
  445. sta state
  446. lda #1
  447. sta scrolling
  448. rts
  449. respawn:
  450. lda #ST_PLAY
  451. sta state
  452. lda #PX_START
  453. sta px
  454. lda #60
  455. sta py
  456. lda #0
  457. sta py_f
  458. sta pvy_l
  459. sta pvy_h
  460. sta coyote
  461. sta jbuf
  462. lda #150
  463. sta invuln
  464. lda #T_PLAYER
  465. sta o_type
  466. lda #SP_PJUMP
  467. sta o_ptr
  468. jmp set_player_obj
  469. kill_player:
  470. lda #ST_DYING
  471. sta state
  472. lda #T_EXPL
  473. sta o_type
  474. lda #90
  475. sta timer
  476. dec lives
  477. jsr draw_hud
  478. lda #SFX_DIE
  479. jmp play_sfx
  480. game_over:
  481. lda #ST_OVER
  482. sta state
  483. lda #0
  484. sta scrolling
  485. lda #100
  486. sta timer
  487. ; new high score?
  488. lda s2
  489. cmp h2
  490. bcc @done
  491. bne @new
  492. lda s1
  493. cmp h1
  494. bcc @done
  495. bne @new
  496. lda s0
  497. cmp h0
  498. bcc @done
  499. @new:
  500. lda s0
  501. sta h0
  502. lda s1
  503. sta h1
  504. lda s2
  505. sta h2
  506. jsr draw_hud
  507. @done:
  508. PRINT_AT 8, 14, msg_over
  509. rts
  510. clear_objects:
  511. ldx #7
  512. lda #T_NONE
  513. : sta o_type,x
  514. dex
  515. bpl :-
  516. rts
  517. ; ===========================================================================
  518. ; Player
  519. ; ===========================================================================
  520. update_player:
  521. lda invuln
  522. beq :+
  523. dec invuln
  524. :
  525. ; --- left / right
  526. lda keys
  527. and #K_LEFT
  528. beq @noleft
  529. lda px
  530. sec
  531. sbc #2
  532. cmp #PX_MIN
  533. bcs :+
  534. lda #PX_MIN
  535. : sta px
  536. @noleft:
  537. lda keys
  538. and #K_RIGHT
  539. beq @noright
  540. lda px
  541. clc
  542. adc #2
  543. cmp #PX_MAX+1
  544. bcc :+
  545. lda #PX_MAX
  546. : sta px
  547. @noright:
  548. ; --- jump (buffered press + coyote time)
  549. lda keys_new
  550. and #K_JUMP
  551. beq :+
  552. lda #JUMP_BUF
  553. sta jbuf
  554. : lda jbuf
  555. beq @nojump
  556. dec jbuf
  557. lda coyote
  558. beq @nojump
  559. lda #<JUMP_VEL
  560. sta pvy_l
  561. lda #>JUMP_VEL
  562. sta pvy_h
  563. lda #0
  564. sta coyote
  565. sta jbuf
  566. lda #SFX_JUMP
  567. jsr play_sfx
  568. @nojump:
  569. ; releasing jump while rising cuts the jump short
  570. lda keys
  571. and #K_JUMP
  572. bne @nocut
  573. lda pvy_h
  574. bpl @nocut
  575. lda pvy_l
  576. cmp #<CUT_VEL
  577. lda pvy_h
  578. sbc #>CUT_VEL
  579. bvc :+
  580. eor #$80
  581. : bpl @nocut ; already slower than CUT_VEL
  582. lda #<CUT_VEL
  583. sta pvy_l
  584. lda #>CUT_VEL
  585. sta pvy_h
  586. @nocut:
  587. ; --- gravity
  588. lda pvy_l
  589. clc
  590. adc #<GRAV
  591. sta pvy_l
  592. lda pvy_h
  593. adc #>GRAV
  594. sta pvy_h
  595. bmi @noclamp
  596. cmp #>MAX_FALL
  597. bcc @noclamp
  598. lda #<MAX_FALL
  599. sta pvy_l
  600. lda #>MAX_FALL
  601. sta pvy_h
  602. @noclamp:
  603. lda py_f
  604. clc
  605. adc pvy_l
  606. sta py_f
  607. lda py
  608. adc pvy_h
  609. sta py
  610. cmp #PY_MIN
  611. bcs :+
  612. lda #PY_MIN
  613. sta py
  614. lda #0
  615. sta pvy_l
  616. sta pvy_h
  617. :
  618. ; --- landing (only while falling, within the top 5 lines of a row)
  619. lda #0
  620. sta on_ground
  621. lda pvy_h
  622. bmi @air
  623. lda py
  624. clc
  625. adc #21
  626. sta sy
  627. sec
  628. sbc #51
  629. and #7
  630. cmp #5
  631. bcs @air
  632. lda #0
  633. sta sx_h
  634. lda px
  635. clc
  636. adc #6
  637. sta sx_l
  638. jsr solid_at
  639. bcs @land
  640. lda px
  641. clc
  642. adc #18
  643. sta sx_l
  644. jsr solid_at
  645. bcc @air
  646. @land:
  647. lda sy
  648. sec
  649. sbc #51
  650. and #$F8
  651. clc
  652. adc #(51-21)
  653. sta py
  654. lda #0
  655. sta py_f
  656. sta pvy_l
  657. sta pvy_h
  658. lda #1
  659. sta on_ground
  660. lda #COYOTE
  661. sta coyote
  662. jmp @after
  663. @air:
  664. lda coyote
  665. beq :+
  666. dec coyote
  667. : lda py
  668. cmp #PIT_Y
  669. bcc @after
  670. .ifndef INVINCIBLE
  671. lda invuln
  672. bne @bounce
  673. jmp kill_player
  674. .endif
  675. @bounce:
  676. lda #<BOUNCE_VEL
  677. sta pvy_l
  678. lda #>BOUNCE_VEL
  679. sta pvy_h
  680. @after:
  681. ; --- shooting (hold CTRL for auto-fire)
  682. lda fire_cd
  683. beq :+
  684. dec fire_cd
  685. jmp @noshoot
  686. : lda keys
  687. and #K_FIRE
  688. beq @noshoot
  689. ldx #1
  690. lda o_type+1
  691. beq @slot
  692. inx
  693. lda o_type+2
  694. bne @noshoot
  695. @slot: lda #T_PBUL
  696. sta o_type,x
  697. lda px
  698. clc
  699. adc #20
  700. sta o_xl,x
  701. lda #0
  702. adc #0
  703. sta o_xh,x
  704. lda py
  705. clc
  706. adc #7
  707. sta o_y,x
  708. lda #SP_PBUL
  709. sta o_ptr,x
  710. lda #7
  711. sta o_col,x
  712. lda #BUL_LIFE
  713. sta o_t,x
  714. lda #FIRE_RATE
  715. sta fire_cd
  716. lda #SFX_SHOT
  717. jsr play_sfx
  718. @noshoot:
  719. ; --- animation
  720. lda on_ground
  721. beq @jumpf
  722. inc panim
  723. lda panim
  724. lsr
  725. lsr
  726. and #3
  727. tay
  728. lda run_seq,y
  729. bne @setp
  730. @jumpf: lda #SP_PJUMP
  731. @setp: sta o_ptr
  732. ; fall through
  733. set_player_obj:
  734. lda px
  735. sta o_xl
  736. lda #0
  737. sta o_xh
  738. lda py
  739. sta o_y
  740. lda #COL_PLAYER
  741. sta o_col
  742. rts
  743. ; ---------------------------------------------------------------------------
  744. ; solid_at: is the character under sprite-coordinate point (sx_h:sx_l, sy)
  745. ; solid? Returns C=1 if solid. Points right of the screen count as solid
  746. ; (enemies wait there before scrolling in). Preserves X.
  747. solid_at:
  748. lda sx_l
  749. sec
  750. sbc #24
  751. sta t0
  752. lda sx_h
  753. sbc #0
  754. sta t1
  755. lda t0
  756. sec
  757. sbc fine
  758. sta t0
  759. lda t1
  760. sbc #0
  761. bmi @no ; left of the screen
  762. lsr
  763. ror t0
  764. lsr
  765. ror t0
  766. lsr
  767. ror t0
  768. lda t0 ; column
  769. cmp #40
  770. bcs @yes
  771. lda sy
  772. sec
  773. sbc #51
  774. bcc @no
  775. lsr
  776. lsr
  777. lsr
  778. cmp #25
  779. bcs @no
  780. tay
  781. lda row_lo,y
  782. sta ptr3
  783. lda row_hi,y
  784. sta ptr3+1
  785. ldy t0
  786. lda (ptr3),y
  787. cmp #CH_SOLID ; C=1 if solid
  788. rts
  789. @yes: sec
  790. rts
  791. @no: clc
  792. rts
  793. ; ===========================================================================
  794. ; Objects
  795. ; ===========================================================================
  796. update_objects:
  797. ; --- player bullets
  798. ldx #1
  799. @b: lda o_type,x
  800. cmp #T_PBUL
  801. bne @bn
  802. lda o_xl,x
  803. clc
  804. adc #BUL_SPEED
  805. sta o_xl,x
  806. lda o_xh,x
  807. adc #0
  808. sta o_xh,x
  809. dec o_t,x
  810. beq @kill
  811. lda o_xh,x
  812. beq @bn
  813. lda o_xl,x
  814. cmp #<344
  815. bcc @bn
  816. @kill: lda #T_NONE
  817. sta o_type,x
  818. @bn: inx
  819. cpx #3
  820. bne @b
  821. ; --- enemies, explosions, enemy shots
  822. ldx #3
  823. @e: lda o_type,x
  824. cmp #T_SOLDIER
  825. bne :+
  826. jsr upd_soldier
  827. jmp @en
  828. : cmp #T_DRONE
  829. bne :+
  830. jsr upd_drone
  831. jmp @en
  832. : cmp #T_EXPL
  833. bne :+
  834. jsr upd_expl
  835. jmp @en
  836. : cmp #T_ESHOT
  837. bne :+
  838. jsr upd_eshot
  839. jmp @en
  840. : cmp #T_BOMB
  841. bne @en
  842. jsr upd_bomb
  843. @en: inx
  844. cpx #8
  845. bne @e
  846. rts
  847. ; obj_sub_x: move object X left by A pixels; frees it once off the left edge
  848. obj_sub_x:
  849. sta tmp
  850. lda o_xl,x
  851. sec
  852. sbc tmp
  853. sta o_xl,x
  854. lda o_xh,x
  855. sbc #0
  856. sta o_xh,x
  857. bpl :+
  858. lda #T_NONE
  859. sta o_type,x
  860. : rts
  861. ; feet sample point for object X: (x + A, y + 21)
  862. obj_feet:
  863. clc
  864. adc o_xl,x
  865. sta sx_l
  866. lda o_xh,x
  867. adc #0
  868. sta sx_h
  869. lda o_y,x
  870. clc
  871. adc #21
  872. sta sy
  873. rts
  874. upd_soldier:
  875. ; walk left on odd frames if there is floor ahead
  876. lda frame
  877. and #1
  878. beq @nowalk
  879. lda #4
  880. jsr obj_feet
  881. jsr solid_at
  882. bcc @nowalk
  883. lda #1
  884. jsr obj_sub_x
  885. @nowalk:
  886. lda #1 ; scroll with the world
  887. jsr obj_sub_x
  888. lda o_type,x
  889. bne :+
  890. rts
  891. : ; gravity
  892. lda #12
  893. jsr obj_feet
  894. jsr solid_at
  895. bcc @fall
  896. lda sy ; snap feet to the top of the row
  897. sec
  898. sbc #51
  899. and #$F8
  900. clc
  901. adc #(51-21)
  902. sta o_y,x
  903. jmp @anim
  904. @fall: lda o_y,x
  905. clc
  906. adc #3
  907. sta o_y,x
  908. cmp #245
  909. bcc @anim
  910. lda #T_NONE
  911. sta o_type,x
  912. rts
  913. @anim: lda frame
  914. lsr
  915. lsr
  916. lsr
  917. and #1
  918. clc
  919. adc #SP_SOLD_A
  920. sta o_ptr,x
  921. ; fire at the player now and then
  922. dec o_t,x
  923. bne @done
  924. jsr rand
  925. and #63
  926. clc
  927. adc #45
  928. sta o_t,x
  929. lda o_type+7
  930. bne @done
  931. lda o_xh,x
  932. bne @done
  933. lda o_xl,x
  934. sec
  935. sbc px
  936. bcc @done ; behind the player: hold fire
  937. cmp #32
  938. bcc @done
  939. lda #T_ESHOT
  940. sta o_type+7
  941. lda o_xl,x
  942. sta o_xl+7
  943. lda #0
  944. sta o_xh+7
  945. lda o_y,x
  946. clc
  947. adc #7
  948. sta o_y+7
  949. lda #SP_ESHOT
  950. sta o_ptr+7
  951. lda #10
  952. sta o_col+7
  953. lda #SFX_ESHOT
  954. jsr play_sfx
  955. @done: rts
  956. upd_drone:
  957. lda #2
  958. jsr obj_sub_x
  959. lda o_type,x
  960. beq @done
  961. inc o_t,x
  962. lda o_t,x
  963. and #31
  964. tay
  965. lda sintab,y
  966. clc
  967. adc o_base,x
  968. sta o_y,x
  969. lda frame
  970. lsr
  971. lsr
  972. and #1
  973. clc
  974. adc #SP_DRONE_A
  975. sta o_ptr,x
  976. ; drop one bomb when just ahead of the player
  977. lda o_t2,x
  978. bne @done
  979. lda o_type+7
  980. bne @done
  981. lda o_xh,x
  982. bne @done
  983. lda o_xl,x
  984. sec
  985. sbc px
  986. bcc @done
  987. cmp #24
  988. bcs @done
  989. inc o_t2,x
  990. lda #T_BOMB
  991. sta o_type+7
  992. lda o_xl,x
  993. clc
  994. adc #10
  995. sta o_xl+7
  996. lda #0
  997. sta o_xh+7
  998. lda o_y,x
  999. clc
  1000. adc #12
  1001. sta o_y+7
  1002. lda #1
  1003. sta o_t+7 ; fall speed
  1004. lda #0
  1005. sta o_t2+7
  1006. lda #SP_BOMB
  1007. sta o_ptr+7
  1008. lda #1
  1009. sta o_col+7
  1010. lda #SFX_BOMB
  1011. jsr play_sfx
  1012. @done: rts
  1013. upd_expl:
  1014. lda #1
  1015. jsr obj_sub_x
  1016. dec o_t,x
  1017. bne :+
  1018. lda #T_NONE
  1019. sta o_type,x
  1020. rts
  1021. : lda o_t,x
  1022. lsr
  1023. lsr
  1024. tay
  1025. lda expl_seq,y
  1026. sta o_ptr,x
  1027. lda frame
  1028. and #2
  1029. lsr
  1030. clc
  1031. adc #7
  1032. sta o_col,x
  1033. rts
  1034. upd_eshot:
  1035. lda #3
  1036. jmp obj_sub_x
  1037. upd_bomb:
  1038. lda #1
  1039. jsr obj_sub_x
  1040. lda o_type,x
  1041. beq @done
  1042. inc o_t2,x ; accelerate every 6 frames
  1043. lda o_t2,x
  1044. cmp #6
  1045. bcc :+
  1046. lda #0
  1047. sta o_t2,x
  1048. lda o_t,x
  1049. cmp #4
  1050. bcs :+
  1051. inc o_t,x
  1052. : lda o_y,x
  1053. clc
  1054. adc o_t,x
  1055. sta o_y,x
  1056. cmp #240
  1057. bcs @gone
  1058. ; hit the ground or a platform?
  1059. lda #2
  1060. clc
  1061. adc o_xl,x
  1062. sta sx_l
  1063. lda o_xh,x
  1064. adc #0
  1065. sta sx_h
  1066. lda o_y,x
  1067. clc
  1068. adc #5
  1069. sta sy
  1070. jsr solid_at
  1071. bcc @done
  1072. ; burst: explosion centred on the bomb
  1073. lda #8
  1074. jsr obj_sub_x
  1075. lda o_type,x
  1076. beq @done
  1077. lda o_y,x
  1078. sec
  1079. sbc #12
  1080. sta o_y,x
  1081. jsr make_expl
  1082. lda #SFX_BOOM
  1083. jmp play_sfx
  1084. @gone: lda #T_NONE
  1085. sta o_type,x
  1086. @done: rts
  1087. ; turn object X into an explosion
  1088. make_expl:
  1089. lda #T_EXPL
  1090. sta o_type,x
  1091. lda #15
  1092. sta o_t,x
  1093. lda #SP_EXPL_1
  1094. sta o_ptr,x
  1095. lda #COL_EXPL
  1096. sta o_col,x
  1097. rts
  1098. ; ---------------------------------------------------------------------------
  1099. collisions:
  1100. ; boxes for every live object
  1101. ldx #7
  1102. @box: ldy o_type,x
  1103. beq @bn
  1104. lda o_xh,x
  1105. lsr
  1106. lda o_xl,x
  1107. ror ; X / 2
  1108. clc
  1109. adc box_x0,y
  1110. sta o_bl,x
  1111. lda box_w,y
  1112. sta o_w,x
  1113. lda o_y,x
  1114. clc
  1115. adc box_y0,y
  1116. sta o_bt,x
  1117. lda box_h,y
  1118. sta o_h,x
  1119. @bn: dex
  1120. bpl @box
  1121. ; player bullets vs hostiles
  1122. ldx #1
  1123. @bl: lda o_type,x
  1124. cmp #T_PBUL
  1125. bne @bnext
  1126. ldy #3
  1127. @el: lda o_type,y
  1128. cmp #T_SOLDIER
  1129. bcc @enext
  1130. jsr overlap
  1131. bcc @enext
  1132. lda #T_NONE
  1133. sta o_type,x
  1134. jsr enemy_hit
  1135. jmp @bnext
  1136. @enext: iny
  1137. cpy #8
  1138. bne @el
  1139. @bnext: inx
  1140. cpx #3
  1141. bne @bl
  1142. ; player vs hostiles
  1143. lda state
  1144. cmp #ST_PLAY
  1145. bne @done
  1146. lda invuln
  1147. bne @done
  1148. .ifdef INVINCIBLE
  1149. rts
  1150. .endif
  1151. ldx #0
  1152. ldy #3
  1153. @pl: lda o_type,y
  1154. cmp #T_SOLDIER
  1155. bcc @pn
  1156. jsr overlap
  1157. bcc @pn
  1158. jmp kill_player
  1159. @pn: iny
  1160. cpy #8
  1161. bne @pl
  1162. @done: rts
  1163. ; overlap: do the boxes of objects X and Y overlap? C=1 yes. Keeps X, Y.
  1164. ; with d = left(X) - left(Y): overlap iff (d + wX - 1) < (wX + wY - 1)
  1165. overlap:
  1166. lda o_w,x
  1167. clc
  1168. adc o_w,y
  1169. sec
  1170. sbc #1
  1171. sta tmp
  1172. lda o_bl,x
  1173. sec
  1174. sbc o_bl,y
  1175. clc
  1176. adc o_w,x
  1177. sec
  1178. sbc #1
  1179. cmp tmp
  1180. bcs @no
  1181. lda o_h,x
  1182. clc
  1183. adc o_h,y
  1184. sec
  1185. sbc #1
  1186. sta tmp
  1187. lda o_bt,x
  1188. sec
  1189. sbc o_bt,y
  1190. clc
  1191. adc o_h,x
  1192. sec
  1193. sbc #1
  1194. cmp tmp
  1195. bcs @no
  1196. sec
  1197. rts
  1198. @no: clc
  1199. rts
  1200. ; a player bullet hit object Y. Keeps X.
  1201. enemy_hit:
  1202. stx tmp2
  1203. tya
  1204. tax
  1205. lda o_type,x
  1206. cmp #T_SOLDIER
  1207. bne :+
  1208. jsr make_expl
  1209. lda #$01 ; 100
  1210. ldy #$00
  1211. jmp @score
  1212. : cmp #T_DRONE
  1213. bne :+
  1214. jsr make_expl
  1215. lda #$02 ; 250
  1216. ldy #$50
  1217. jmp @score
  1218. : lda #T_NONE ; shot or bomb: just pop it
  1219. sta o_type,x
  1220. lda #$00 ; 10
  1221. ldy #$10
  1222. jsr add_score
  1223. ldx tmp2
  1224. lda #SFX_ESHOT
  1225. jmp play_sfx
  1226. @score: jsr add_score
  1227. ldx tmp2
  1228. lda #SFX_BOOM
  1229. jmp play_sfx
  1230. ; ---------------------------------------------------------------------------
  1231. ; show_sprites: write the object table straight to the VIC (called in the
  1232. ; lower border, before the next frame starts drawing)
  1233. show_sprites:
  1234. lda #0
  1235. sta tmp ; enable
  1236. sta tmp2 ; X msb
  1237. sta t0 ; multicolor
  1238. ldx #7
  1239. @l: ldy o_type,x
  1240. beq @off
  1241. cpx #0
  1242. bne :+
  1243. lda invuln ; blink while invulnerable
  1244. and #4
  1245. bne @off
  1246. : txa
  1247. asl
  1248. tay ; Y = register offset x*2
  1249. lda o_xl,x
  1250. sta $D000,y
  1251. lda o_y,x
  1252. sta $D001,y
  1253. lda o_ptr,x
  1254. sta SPRPTR,x
  1255. lda o_col,x
  1256. sta VIC_SPR0COL,x
  1257. ldy o_type,x
  1258. lda type_mc,y
  1259. lsr
  1260. rol t0
  1261. lda o_xh,x
  1262. lsr
  1263. rol tmp2
  1264. sec
  1265. rol tmp
  1266. jmp @n
  1267. @off: asl t0
  1268. asl tmp2
  1269. asl tmp
  1270. @n: dex
  1271. bpl @l
  1272. lda tmp2
  1273. sta VIC_SPRXHI
  1274. lda t0
  1275. sta VIC_SPRMC
  1276. lda tmp
  1277. sta VIC_SPREN
  1278. rts
  1279. ; ===========================================================================
  1280. ; Input
  1281. ; ===========================================================================
  1282. read_input:
  1283. lda #0
  1284. sta tmp
  1285. lda #$FD ; column 1: A = row 2
  1286. sta CIA1_PRA
  1287. lda CIA1_PRB
  1288. and #$04
  1289. bne :+
  1290. lda #K_LEFT
  1291. sta tmp
  1292. : lda #$FB ; column 2: D = row 2
  1293. sta CIA1_PRA
  1294. lda CIA1_PRB
  1295. and #$04
  1296. bne :+
  1297. lda tmp
  1298. ora #K_RIGHT
  1299. sta tmp
  1300. : lda #$7F ; column 7: CTRL = row 2, SPACE = row 4
  1301. sta CIA1_PRA
  1302. lda CIA1_PRB
  1303. tax
  1304. and #$04
  1305. bne :+
  1306. lda tmp
  1307. ora #K_FIRE
  1308. sta tmp
  1309. : txa
  1310. and #$10
  1311. bne :+
  1312. lda tmp
  1313. ora #K_JUMP
  1314. sta tmp
  1315. : ; joystick port 2
  1316. lda #$FF
  1317. sta CIA1_PRA
  1318. lda CIA1_PRA
  1319. eor #$FF
  1320. tax
  1321. and #$01 ; up
  1322. beq :+
  1323. lda tmp
  1324. ora #K_JUMP
  1325. sta tmp
  1326. : txa
  1327. and #$04 ; left
  1328. beq :+
  1329. lda tmp
  1330. ora #K_LEFT
  1331. sta tmp
  1332. : txa
  1333. and #$08 ; right
  1334. beq :+
  1335. lda tmp
  1336. ora #K_RIGHT
  1337. sta tmp
  1338. : txa
  1339. and #$10 ; fire
  1340. beq :+
  1341. lda tmp
  1342. ora #K_FIRE
  1343. sta tmp
  1344. : lda tmp
  1345. sta keys
  1346. lda keys_prev
  1347. eor #$FF
  1348. and keys
  1349. sta keys_new
  1350. lda keys
  1351. sta keys_prev
  1352. rts
  1353. .ifdef AUTOTEST
  1354. ; test build: start at once, hold fire, jump at pit edges and now and then
  1355. autopilot:
  1356. lda state
  1357. cmp #ST_PLAY
  1358. beq @play
  1359. lda frame
  1360. and #63
  1361. bne :+
  1362. lda #K_JUMP
  1363. sta keys_new
  1364. : rts
  1365. @play: lda py ; debug: py and on_ground in the HUD
  1366. ldx #24
  1367. jsr put_hex
  1368. lda on_ground
  1369. ldx #26
  1370. jsr put_hex
  1371. lda keys
  1372. ora #K_FIRE
  1373. sta keys
  1374. lda frame
  1375. and #31
  1376. bne :+
  1377. lda keys_new
  1378. ora #K_JUMP
  1379. sta keys_new
  1380. lda keys
  1381. ora #K_JUMP
  1382. sta keys
  1383. : rts
  1384. put_hex:
  1385. pha
  1386. lsr
  1387. lsr
  1388. lsr
  1389. lsr
  1390. tay
  1391. lda hexd,y
  1392. sta SCREEN,x
  1393. pla
  1394. and #$0F
  1395. tay
  1396. lda hexd,y
  1397. sta SCREEN+1,x
  1398. rts
  1399. hexd: .byte "0123456789",1,2,3,4,5,6
  1400. .endif
  1401. ; ===========================================================================
  1402. ; World generator
  1403. ; ===========================================================================
  1404. ; column_tick: once per column of travel - distance score, difficulty, spawns
  1405. column_tick:
  1406. lda state
  1407. cmp #ST_PLAY
  1408. beq :+
  1409. rts
  1410. : inc dist
  1411. lda dist
  1412. and #3
  1413. bne :+
  1414. lda #$00 ; 10 points per 4 columns
  1415. ldy #$10
  1416. jsr add_score
  1417. : lda dist
  1418. and #63
  1419. bne :+
  1420. lda level
  1421. cmp #8
  1422. bcs :+
  1423. inc level
  1424. : dec spawn_cnt
  1425. beq :+
  1426. rts
  1427. : lda #14
  1428. sec
  1429. sbc level
  1430. sta spawn_cnt
  1431. jsr rand
  1432. and #3
  1433. clc
  1434. adc spawn_cnt
  1435. sta spawn_cnt
  1436. ; fall through
  1437. spawn_enemy:
  1438. ldx #3
  1439. : lda o_type,x
  1440. beq @got
  1441. inx
  1442. cpx #7
  1443. bne :-
  1444. rts
  1445. @got:
  1446. lda #<SPAWN_X
  1447. sta o_xl,x
  1448. lda #>SPAWN_X
  1449. sta o_xh,x
  1450. jsr rand
  1451. sta o_t,x
  1452. lda level
  1453. lsr
  1454. clc
  1455. adc #2
  1456. sta tmp ; drone chance in eighths
  1457. jsr rand
  1458. and #7
  1459. cmp tmp
  1460. bcc @drone
  1461. ; soldier: on a platform (if one runs on) or on the ground
  1462. jsr rand
  1463. and #1
  1464. beq @ground
  1465. lda p2_on
  1466. beq :+
  1467. lda p2_len
  1468. cmp #3
  1469. bcc :+
  1470. lda #ROW_Y(PLAT2_ROW)
  1471. bne @soldier
  1472. : lda p1_on
  1473. beq @ground
  1474. lda p1_len
  1475. cmp #3
  1476. bcc @ground
  1477. lda #ROW_Y(PLAT1_ROW)
  1478. bne @soldier
  1479. @ground:
  1480. lda g_on
  1481. beq @drone
  1482. lda g_len
  1483. cmp #3
  1484. bcc @drone
  1485. lda #ROW_Y(GROUND_ROW)
  1486. @soldier:
  1487. sta o_y,x
  1488. lda #T_SOLDIER
  1489. sta o_type,x
  1490. lda o_t,x
  1491. and #31
  1492. clc
  1493. adc #20
  1494. sta o_t,x ; first shot delay
  1495. lda #SP_SOLD_A
  1496. sta o_ptr,x
  1497. lda #COL_SOLDIER
  1498. sta o_col,x
  1499. rts
  1500. @drone:
  1501. jsr rand
  1502. and #63
  1503. clc
  1504. adc #56
  1505. sta o_base,x
  1506. sta o_y,x
  1507. lda #T_DRONE
  1508. sta o_type,x
  1509. lda #0
  1510. sta o_t2,x
  1511. lda #SP_DRONE_A
  1512. sta o_ptr,x
  1513. lda #COL_DRONE
  1514. sta o_col,x
  1515. rts
  1516. ; build_world: fresh generator state and a full screen of terrain
  1517. build_world:
  1518. lda #0
  1519. sta fine
  1520. sta fine_cur
  1521. sta colpar
  1522. sta p1_on
  1523. sta p2_on
  1524. sta b_h
  1525. lda #1
  1526. sta g_on
  1527. sta g_age ; no left edge on column 0
  1528. sta b_len
  1529. lda #48 ; safe run-up: 40 columns + 8 ahead
  1530. sta g_len
  1531. lda #16
  1532. sta p1_len
  1533. lda #6
  1534. sta p2_len
  1535. lda #0
  1536. sta gen_col
  1537. : jsr gen_column
  1538. jsr write_column
  1539. inc gen_col
  1540. lda gen_col
  1541. cmp #40
  1542. bne :-
  1543. rts
  1544. ; gen_column: build the next column of the world into colbuf[1..24]
  1545. gen_column:
  1546. ldx #24
  1547. lda #' '
  1548. : sta colbuf,x
  1549. dex
  1550. bne :-
  1551. ; --- stars
  1552. jsr rand
  1553. and #7
  1554. bne @nostar
  1555. jsr rand
  1556. and #7
  1557. clc
  1558. adc #2
  1559. tax
  1560. jsr rand
  1561. and #1
  1562. clc
  1563. adc #CH_STAR1
  1564. sta colbuf,x
  1565. @nostar:
  1566. ; --- skyline
  1567. lda b_len
  1568. bne @bcont
  1569. lda b_h
  1570. beq @newb
  1571. lda #0 ; building ends: gap of 1-2
  1572. sta b_h
  1573. jsr rand
  1574. and #1
  1575. clc
  1576. adc #1
  1577. sta b_len
  1578. bne @bcont
  1579. @newb: jsr rand
  1580. and #3
  1581. clc
  1582. adc #2
  1583. sta b_h ; 2..5 rows
  1584. jsr rand
  1585. and #3
  1586. clc
  1587. adc #3
  1588. sta b_len ; 3..6 wide
  1589. @bcont: dec b_len
  1590. lda b_h
  1591. beq @nob
  1592. lda #GROUND_ROW
  1593. sec
  1594. sbc b_h
  1595. tax
  1596. lda #CH_BTOP
  1597. sta colbuf,x
  1598. @bl: inx
  1599. cpx #GROUND_ROW
  1600. bcs @nob
  1601. jsr rand
  1602. and #1
  1603. clc
  1604. adc #CH_BLDA
  1605. sta colbuf,x
  1606. bne @bl
  1607. @nob:
  1608. ; --- low platform (row 17)
  1609. lda p1_len
  1610. bne @p1c
  1611. lda p1_on
  1612. eor #1
  1613. sta p1_on
  1614. beq @p1gap
  1615. jsr rand
  1616. and #3
  1617. clc
  1618. adc #4
  1619. sta p1_len ; 4..7 long
  1620. lda #0
  1621. sta p1_age
  1622. beq @p1c
  1623. @p1gap: jsr rand
  1624. and #7
  1625. clc
  1626. adc #4
  1627. sta p1_len ; 4..11 gap
  1628. @p1c: lda p1_on
  1629. beq @p1off
  1630. ldx #CH_PLM
  1631. lda p1_age
  1632. bne :+
  1633. ldx #CH_PLL
  1634. : lda p1_len
  1635. cmp #1
  1636. bne :+
  1637. ldx #CH_PLR
  1638. : stx colbuf+PLAT1_ROW
  1639. inc p1_age
  1640. @p1off: dec p1_len
  1641. ; --- high platform (row 13): only above a low one, so it is reachable
  1642. lda p2_len
  1643. bne @p2c
  1644. lda p2_on
  1645. beq @p2try
  1646. lda #0
  1647. sta p2_on
  1648. lda #6
  1649. sta p2_len
  1650. bne @p2c
  1651. @p2try: lda p1_on
  1652. beq @p2wait
  1653. lda p1_len
  1654. cmp #3
  1655. bcc @p2wait
  1656. jsr rand
  1657. and #1
  1658. beq @p2wait
  1659. lda #1
  1660. sta p2_on
  1661. lda #0
  1662. sta p2_age
  1663. jsr rand
  1664. and #3
  1665. clc
  1666. adc #3
  1667. sta p2_len ; 3..6 long
  1668. bne @p2c
  1669. @p2wait:
  1670. lda #1
  1671. sta p2_len
  1672. @p2c: lda p2_on
  1673. beq @p2off
  1674. ldx #CH_PLM
  1675. lda p2_age
  1676. bne :+
  1677. ldx #CH_PLL
  1678. : lda p2_len
  1679. cmp #1
  1680. bne :+
  1681. ldx #CH_PLR
  1682. : stx colbuf+PLAT2_ROW
  1683. inc p2_age
  1684. @p2off: dec p2_len
  1685. ; --- ground and pits
  1686. lda g_len
  1687. bne @gc
  1688. lda g_on
  1689. eor #1
  1690. sta g_on
  1691. beq @gpit
  1692. jsr rand
  1693. and #7
  1694. clc
  1695. adc #6
  1696. sta g_len ; 6..13 ground
  1697. lda #0
  1698. sta g_age
  1699. beq @gc
  1700. @gpit: jsr rand
  1701. and #1
  1702. clc
  1703. adc #3 ; 3..4 pit
  1704. ldx level
  1705. cpx #4 ; C=1 from level 4 on: one column wider
  1706. adc #0
  1707. sta g_len
  1708. @gc: lda g_on
  1709. beq @gpitd
  1710. lda colpar
  1711. eor #1
  1712. sta colpar
  1713. tax
  1714. lda grass_tab,x
  1715. ldy dirt_tab,x
  1716. sty tmp
  1717. ldy dirt_tab+1,x ; other parity for the middle dirt row
  1718. sty tmp2
  1719. ldy g_age
  1720. bne :+
  1721. lda #CH_DL
  1722. sta tmp
  1723. sta tmp2
  1724. lda #CH_GL
  1725. : ldy g_len
  1726. cpy #1
  1727. bne :+
  1728. lda #CH_DR
  1729. sta tmp
  1730. sta tmp2
  1731. lda #CH_GR
  1732. : sta colbuf+GROUND_ROW
  1733. lda tmp
  1734. sta colbuf+GROUND_ROW+1
  1735. sta colbuf+GROUND_ROW+3
  1736. lda tmp2
  1737. sta colbuf+GROUND_ROW+2
  1738. inc g_age
  1739. @gpitd: dec g_len
  1740. rts
  1741. ; write_column: colbuf[1..24] -> screen column gen_col
  1742. write_column:
  1743. ldx #1
  1744. : lda row_lo,x
  1745. sta ptr3
  1746. lda row_hi,x
  1747. sta ptr3+1
  1748. ldy gen_col
  1749. lda colbuf,x
  1750. sta (ptr3),y
  1751. inx
  1752. cpx #25
  1753. bne :-
  1754. rts
  1755. ; 16-bit xorshift (John Metcalf), returns A; keeps X and Y
  1756. rand:
  1757. lda seed+1
  1758. lsr
  1759. lda seed
  1760. ror
  1761. eor seed+1
  1762. sta seed+1
  1763. ror
  1764. eor seed
  1765. sta seed
  1766. eor seed+1
  1767. sta seed+1
  1768. rts
  1769. ; ===========================================================================
  1770. ; Screen
  1771. ; ===========================================================================
  1772. ; coarse scroll: rows 1..24, columns 0..38 <- 1..39, top to bottom.
  1773. ; Three adjacent bytes per loop pass (39 = 3 x 13) keeps ahead of the beam.
  1774. ; Must run strictly left to right, or it reads cells it already wrote.
  1775. shift_screen:
  1776. .repeat 24, R
  1777. ldx #0
  1778. : lda SCREEN+(R+1)*40+1,x
  1779. sta SCREEN+(R+1)*40,x
  1780. lda SCREEN+(R+1)*40+2,x
  1781. sta SCREEN+(R+1)*40+1,x
  1782. lda SCREEN+(R+1)*40+3,x
  1783. sta SCREEN+(R+1)*40+2,x
  1784. inx
  1785. inx
  1786. inx
  1787. cpx #39
  1788. bne :-
  1789. .endrepeat
  1790. rts
  1791. init_colors:
  1792. ldx #0
  1793. @r: lda row_lo,x
  1794. sta ptr
  1795. lda row_hi,x
  1796. clc
  1797. adc #>(COLRAM-SCREEN)
  1798. sta ptr+1
  1799. lda row_color,x
  1800. ldy #39
  1801. : sta (ptr),y
  1802. dey
  1803. bpl :-
  1804. inx
  1805. cpx #25
  1806. bne @r
  1807. rts
  1808. draw_hud:
  1809. ldx #39
  1810. lda #' '
  1811. : sta SCREEN,x
  1812. dex
  1813. bpl :-
  1814. SET_PTRS 0, 1, msg_score
  1815. jsr print_str
  1816. SET_PTRS 0, 16, msg_lives
  1817. jsr print_str
  1818. SET_PTRS 0, 28, msg_hi
  1819. jsr print_str
  1820. jsr draw_score
  1821. lda lives
  1822. ora #'0'
  1823. sta SCREEN+22
  1824. ldx #31
  1825. lda h2
  1826. jsr put_bcd
  1827. lda h1
  1828. jsr put_bcd
  1829. lda h0
  1830. jmp put_bcd
  1831. draw_score:
  1832. ldx #7
  1833. lda s2
  1834. jsr put_bcd
  1835. lda s1
  1836. jsr put_bcd
  1837. lda s0
  1838. ; fall through
  1839. ; put_bcd: two BCD digits of A at row 0, column X; X += 2
  1840. put_bcd:
  1841. pha
  1842. lsr
  1843. lsr
  1844. lsr
  1845. lsr
  1846. ora #'0'
  1847. sta SCREEN,x
  1848. pla
  1849. and #$0F
  1850. ora #'0'
  1851. sta SCREEN+1,x
  1852. inx
  1853. inx
  1854. rts
  1855. ; add_score: A = BCD hundreds/thousands, Y = BCD tens/ones. Keeps nothing.
  1856. add_score:
  1857. sta tmp
  1858. sed
  1859. clc
  1860. tya
  1861. adc s0
  1862. sta s0
  1863. lda s1
  1864. adc tmp
  1865. sta s1
  1866. lda s2
  1867. adc #0
  1868. sta s2
  1869. cld
  1870. jmp draw_score
  1871. ; copy ASCII string at (ptr2) to (ptr) as screen codes; returns Y = length
  1872. print_str:
  1873. ldy #0
  1874. @l: lda (ptr2),y
  1875. beq @d
  1876. and #$3F
  1877. sta (ptr),y
  1878. iny
  1879. bne @l
  1880. @d: rts
  1881. ; as print_str, then colour the cells white in hi-res (readable over the
  1882. ; multicolor playfield)
  1883. print_hires:
  1884. jsr print_str
  1885. lda ptr+1
  1886. pha
  1887. clc
  1888. adc #>(COLRAM-SCREEN)
  1889. sta ptr+1
  1890. lda #1
  1891. @c: dey
  1892. bmi @x
  1893. sta (ptr),y
  1894. jmp @c
  1895. @x: pla
  1896. sta ptr+1
  1897. rts
  1898. ; ===========================================================================
  1899. ; Sound
  1900. ; ===========================================================================
  1901. ; play_sfx: A = effect number. Keeps X.
  1902. play_sfx:
  1903. stx sfx_sx
  1904. asl
  1905. asl
  1906. asl
  1907. tay
  1908. ldx sfx_tab,y ; voice 0..2
  1909. lda sfx_tab+3,y
  1910. sta sv_wave,x
  1911. lda sfx_tab+4,y
  1912. sta sv_freq,x
  1913. lda sfx_tab+5,y
  1914. sta sv_delta,x
  1915. lda sfx_tab+6,y
  1916. sta sv_t,x
  1917. lda voice_off,x
  1918. tax
  1919. lda #0
  1920. sta SID+4,x
  1921. lda sfx_tab+1,y
  1922. sta SID+5,x
  1923. lda sfx_tab+2,y
  1924. sta SID+6,x
  1925. lda #0
  1926. sta SID+0,x
  1927. lda sfx_tab+4,y
  1928. sta SID+1,x
  1929. lda #$08
  1930. sta SID+3,x ; 50% pulse width
  1931. lda sfx_tab+3,y
  1932. ora #1 ; gate on
  1933. sta SID+4,x
  1934. ldx sfx_sx
  1935. rts
  1936. update_sfx:
  1937. ldx #2
  1938. @l: lda sv_t,x
  1939. beq @n
  1940. dec sv_t,x
  1941. bne @sweep
  1942. ldy voice_off,x
  1943. lda sv_wave,x ; gate off, release
  1944. sta SID+4,y
  1945. jmp @n
  1946. @sweep: lda sv_freq,x
  1947. clc
  1948. adc sv_delta,x
  1949. sta sv_freq,x
  1950. ldy voice_off,x
  1951. sta SID+1,y
  1952. @n: dex
  1953. bpl @l
  1954. rts
  1955. init_sid:
  1956. ldx #$18
  1957. lda #0
  1958. : sta SID,x
  1959. dex
  1960. bpl :-
  1961. lda #0
  1962. sta sv_t
  1963. sta sv_t+1
  1964. sta sv_t+2
  1965. lda #$0F ; volume 15
  1966. sta SID+$18
  1967. rts
  1968. ; ===========================================================================
  1969. ; Init
  1970. ; ===========================================================================
  1971. copy_charset:
  1972. lda #$33 ; char ROM visible at $D000
  1973. sta $01
  1974. ldx #0
  1975. : lda $D000,x ; ROM chars 0..63 (letters, digits)
  1976. sta CHARSET,x
  1977. lda $D100,x
  1978. sta CHARSET+$100,x
  1979. inx
  1980. bne :-
  1981. lda #$37
  1982. sta $01
  1983. ldx #0
  1984. : lda custom_chars,x ; chars $40..$5F
  1985. sta CHARSET+CUSTOM_FIRST*8,x
  1986. inx
  1987. bne :-
  1988. rts
  1989. ; ===========================================================================
  1990. ; IRQs
  1991. ; ===========================================================================
  1992. irq_bottom:
  1993. lda #$FF
  1994. sta VIC_IRQ
  1995. lda #$10 ; HUD row: no fine scroll
  1996. sta VIC_CTRL2
  1997. inc frame_flag
  1998. lda #<irq_top
  1999. sta IRQVEC
  2000. lda #>irq_top
  2001. sta IRQVEC+1
  2002. lda #58
  2003. sta VIC_RASTER
  2004. jmp KERNAL_IRQ_EXIT
  2005. irq_top:
  2006. lda #$FF
  2007. sta VIC_IRQ
  2008. lda fine_cur
  2009. ora #$10
  2010. sta VIC_CTRL2
  2011. lda #<irq_bottom
  2012. sta IRQVEC
  2013. lda #>irq_bottom
  2014. sta IRQVEC+1
  2015. lda #251
  2016. sta VIC_RASTER
  2017. jmp KERNAL_IRQ_EXIT
  2018. ; ===========================================================================
  2019. ; Data
  2020. ; ===========================================================================
  2021. .segment "RODATA"
  2022. row_lo:
  2023. .repeat 25, R
  2024. .byte <(SCREEN + R*40)
  2025. .endrepeat
  2026. row_hi:
  2027. .repeat 25, R
  2028. .byte >(SCREEN + R*40)
  2029. .endrepeat
  2030. ; colour RAM per row: 0 HUD hi-res white, 1-10 stars white, 11-20 lit
  2031. ; windows and platform tops yellow, 21 grass green, 22-24 dirt
  2032. row_color:
  2033. .byte 1
  2034. .byte 9,9,9,9,9,9,9,9,9,9
  2035. .byte 15,15,15,15,15,15,15,15,15,15
  2036. .byte 13
  2037. .byte 8,8,8
  2038. grass_tab: .byte CH_G1, CH_G2
  2039. dirt_tab: .byte CH_D1, CH_D2, CH_D1
  2040. run_seq: .byte SP_PRUN_A, SP_PRUN_B, SP_PRUN_C, SP_PRUN_B
  2041. ; indexed by timer/4: high = early
  2042. expl_seq: .byte SP_EXPL_3, SP_EXPL_2, SP_EXPL_2, SP_EXPL_1
  2043. ; per object type: none player pbul expl soldier drone eshot bomb
  2044. type_mc: .byte 0, 1, 0, 1, 1, 1, 0, 0
  2045. box_x0: .byte 0, 3, 0, 0, 3, 0, 0, 0
  2046. box_w: .byte 0, 6, 4, 0, 6, 12, 2, 2
  2047. box_y0: .byte 0, 2, 0, 0, 2, 3, 0, 0
  2048. box_h: .byte 0, 18, 3, 0, 18, 10, 3, 5
  2049. voice_off: .byte 0, 7, 14
  2050. ; voice, AD, SR, waveform, start freq hi, freq step per frame, frames, pad
  2051. sfx_tab:
  2052. .byte 0, $08, $00, $80, $28, $FC, 5, 0 ; SFX_SHOT noise zap
  2053. .byte 1, $06, $00, $10, $10, $03, 8, 0 ; SFX_JUMP triangle chirp
  2054. .byte 2, $0A, $00, $80, $14, $FF, 18, 0 ; SFX_BOOM noise burst
  2055. .byte 1, $05, $00, $20, $40, $FA, 6, 0 ; SFX_ESHOT saw zap
  2056. .byte 2, $0C, $00, $80, $30, $FF, 40, 0 ; SFX_DIE long rumble
  2057. .byte 1, $05, $00, $10, $30, $FE, 12, 0 ; SFX_BOMB falling whistle
  2058. msg_score: .byte "SCORE", 0
  2059. msg_lives: .byte "LIVES", 0
  2060. msg_hi: .byte "HI", 0
  2061. msg_title: .byte "B L A S T R U N N E R", 0
  2062. msg_ctrl1: .byte "SPACE - JUMP CTRL - SHOOT", 0
  2063. msg_ctrl2: .byte "A / D - MOVE (OR JOYSTICK 2)", 0
  2064. msg_start: .byte "PRESS SPACE TO START", 0
  2065. msg_over: .byte "GAME OVER", 0
  2066. msg_again: .byte "PRESS SPACE TO PLAY", 0
  2067. .assert * <= $2000, error, "program overlaps sprite data at $2000"

Powered by TurnKey Linux.