Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

652 рядки
18KB

  1. ; People: up to 255 walkers stored as world positions, one multicolour pixel
  2. ; each; people_count of them exist (16 in a new world). A position is a world
  3. ; cell (cx 0-79, cy 0-49) plus the pixel inside it (sx 0-3 across, sy 0-7
  4. ; down). Water and stone tiles block movement.
  5. ;
  6. ; Memory (M7.1): every field is its own 256-byte table indexed by person, so
  7. ; any count up to 255 needs no other change. The tables sit in RAM the VIC-II
  8. ; never shows: the gaps in the two video banks (see the asserts below).
  9. PEOPLE_MAX = 255
  10. .ifndef PEOPLE_START
  11. PEOPLE_START = 16 ; people in a new world (checks may build with more)
  12. .endif
  13. .assert PEOPLE_START >= 1 .and PEOPLE_START <= PEOPLE_MAX, error, "PEOPLE_START must be 1-255"
  14. ATTR_COUNT = 6 ; STR, DEX, CON, INT, WIS, CHA
  15. NAME_LENGTH = 10
  16. people_cx = $4C00 ; bank 1, after the second text screen
  17. people_sx = $4D00
  18. people_cy = $4E00
  19. people_sy = $4F00
  20. people_dir = $5800 ; bank 1, after the font
  21. people_sex = $5900 ; 0 = male, 1 = female
  22. people_attr = $5A00 ; one page per score: STR at $5A00 ... CHA at $5F00
  23. drawn_low_a = $8C00 ; bank 2, after its second text screen: saved-byte
  24. drawn_high_a = $8D00 ; lists, three pages per buffer (B = A + 3 pages)
  25. drawn_byte_a = $8E00
  26. drawn_low_b = $8F00
  27. people_name = $9200 ; one page per letter: letter K of person N at
  28. ; people_name + K * 256 + N (screen codes)
  29. LIST_PAGES = 3
  30. .assert people_cx >= SCREEN_A + $0C00, error, "People tables overlap bank 1's text screens"
  31. .assert people_sy + $100 <= FONT_A, error, "People tables overlap bank 1's font"
  32. .assert people_dir >= FONT_A + $0800, error, "People tables overlap bank 1's font"
  33. .assert people_attr + ATTR_COUNT * $100 <= BITMAP_A, error, "People tables overlap bitmap A"
  34. .assert drawn_low_a >= SCREEN_B + $0C00, error, "People tables overlap bank 2's text screens"
  35. .assert drawn_low_b = drawn_low_a + LIST_PAGES * $100, error, "List layout"
  36. .assert people_name >= drawn_low_b + LIST_PAGES * $100, error, "Names overlap the lists"
  37. .assert people_name + NAME_LENGTH * $100 <= BITMAP_B, error, "Names overlap bitmap B"
  38. ; Zero page ($44-$4B: BASIC's area, unused here).
  39. lst_low = $44 ; saved-byte list of one buffer: addresses
  40. lst_high = $46
  41. lst_byte = $48 ; and the bytes under the dots
  42. tab_ptr = $4A ; a score or name-letter table
  43. DIR_N = 0
  44. DIR_E = 1
  45. DIR_S = 2
  46. DIR_W = 3
  47. TURN_TRIES = 5 ; current direction, then all four from a random start
  48. .assert STONE + 1 = WATER, error, "Blocking tiles must be contiguous"
  49. .assert WATER_SE + 1 = ROAD_H, error, "Blocking tiles must be contiguous"
  50. .assert BRIDGE_X + 1 = TILE_COUNT, error, "Bridge tiles must come last"
  51. .export people_cx, people_sx, people_cy, people_sy, people_dir, people_moved, people_attr
  52. .export people_name, people_sex, shown_person, draw_skip, people_count, move_frame
  53. .export drawn_low_a, drawn_high_a, drawn_byte_a, drawn_low_b
  54. .segment "CODE"
  55. ; New world: PEOPLE_START people, each on a random walkable cell; person N
  56. ; goes in sector N mod 4, so they are spread over all four screens of the
  57. ; world. Person 1 is shown.
  58. place_people:
  59. lda #0
  60. sta drawn_count
  61. sta drawn_count+1
  62. sta move_frame
  63. sta shown_person
  64. lda irq_frames ; no catching up on the time spent making the world
  65. sta moved_frames
  66. lda #PEOPLE_START
  67. sta people_count
  68. ldx #0
  69. @next: jsr new_person
  70. inx
  71. cpx people_count
  72. bne @next
  73. lda #1
  74. sta shown_dirty
  75. rts
  76. ; X = person: a random walkable position in sector X mod 4, a direction, a
  77. ; sex, a name and six scores. Returns X = person.
  78. new_person:
  79. stx person
  80. @retry_x:
  81. jsr random_byte
  82. and #63
  83. cmp #SECTOR_WIDTH
  84. bcs @retry_x
  85. sta cand_cx
  86. lda person
  87. and #3
  88. tax
  89. lda cand_cx
  90. clc
  91. adc sector_left,x
  92. sta cand_cx
  93. @retry_y:
  94. jsr random_byte
  95. and #31
  96. cmp #SECTOR_HEIGHT
  97. bcs @retry_y
  98. clc
  99. adc sector_top,x
  100. sta cand_cy
  101. jsr random_byte
  102. and #3
  103. sta cand_sx
  104. jsr random_byte
  105. and #7
  106. sta cand_sy
  107. jsr candidate_blocked
  108. bcs @retry_x
  109. ldx person
  110. lda cand_cx
  111. sta people_cx,x
  112. lda cand_sx
  113. sta people_sx,x
  114. lda cand_cy
  115. sta people_cy,x
  116. lda cand_sy
  117. sta people_sy,x
  118. jsr random_byte
  119. and #3
  120. sta people_dir,x
  121. jsr roll_attributes
  122. jsr roll_name
  123. ldx person
  124. rts
  125. ; + key (M7.3): one more person, if fewer than PEOPLE_MAX.
  126. add_person:
  127. ldx people_count
  128. cpx #PEOPLE_MAX
  129. bcs @full
  130. jsr new_person
  131. inc people_count
  132. lda #1
  133. sta shown_dirty
  134. @full: rts
  135. ; - key (M7.3): remove the last person, keeping at least one. Their dot goes
  136. ; when each buffer's saved bytes are next put back (people_update).
  137. remove_person:
  138. ldx people_count
  139. cpx #2
  140. bcc @last
  141. dex
  142. stx people_count
  143. cpx shown_person ; the shown person was the last: show the new last
  144. bne @shown
  145. dex
  146. stx shown_person
  147. @shown: lda #1
  148. sta shown_dirty
  149. @last: rts
  150. ; Roll person's six scores (R8): 3d6 each, 3-18.
  151. roll_attributes:
  152. lda #0
  153. sta tab_ptr
  154. lda #>people_attr
  155. sta tab_ptr+1
  156. ldy person
  157. @score: lda #0
  158. sta attr_sum
  159. ldx #3 ; three dice
  160. @die: jsr random_byte ; keeps X and Y
  161. and #7
  162. cmp #6
  163. bcs @die ; 6 or 7: roll again, so 0-5 are equally likely
  164. sec ; + 1: 1-6
  165. adc attr_sum
  166. sta attr_sum
  167. dex
  168. bne @die
  169. sta (tab_ptr),y
  170. inc tab_ptr+1 ; next score's page
  171. lda tab_ptr+1
  172. cmp #>people_attr + ATTR_COUNT
  173. bne @score
  174. rts
  175. ; Give person a sex (0 = male, 1 = female, 50/50) and a name (R8): a start
  176. ; syllable, half the time a middle one, and an end syllable from the male or
  177. ; female list; at most 4 + 2 + 4 = 10 letters, padded with spaces.
  178. roll_name:
  179. lda #0
  180. sta tab_ptr
  181. lda #>people_name
  182. sta tab_ptr+1
  183. ldy person
  184. lda #' '
  185. @blank: sta (tab_ptr),y
  186. inc tab_ptr+1
  187. ldx tab_ptr+1
  188. cpx #>people_name + NAME_LENGTH
  189. bne @blank
  190. lda #0
  191. sta name_pos
  192. jsr random_byte
  193. and #1
  194. sta people_sex,y
  195. jsr random_byte
  196. and #15
  197. jsr add_syllable ; start: syllables 0-15
  198. jsr random_byte
  199. bmi @end ; half the names have no middle syllable
  200. and #7
  201. ora #SYL_MIDDLE ; middle: syllables 16-23
  202. jsr add_syllable
  203. @end: jsr random_byte
  204. and #15
  205. ldy person
  206. ldx people_sex,y
  207. clc
  208. adc syl_end_base,x ; end: male 24-39, female 40-55
  209. ; fall through
  210. ; A = syllable number: append its letters (up to 4) to person's name at
  211. ; letter name_pos.
  212. add_syllable:
  213. asl a
  214. asl a
  215. tax
  216. lda #4
  217. sta syl_left
  218. @char: lda name_pos
  219. clc
  220. adc #>people_name
  221. sta tab_ptr+1
  222. lda syllables,x
  223. beq @done
  224. ldy person
  225. sta (tab_ptr),y
  226. inc name_pos
  227. inx
  228. dec syl_left
  229. bne @char
  230. @done: rts
  231. ; Move every eighth person per frame, starting with person move_frame mod 8:
  232. ; each person takes one pixel step every 8 frames, however many there are
  233. ; (more people take more time; the user chose speed over frame rate, M7).
  234. ; Called from the main loop, outside the vertical blank.
  235. ; Every frame one eighth of the people step, so each person steps every 8
  236. ; frames. If the main loop took longer than a frame (many people), the frames
  237. ; it missed are caught up here, up to MOVE_CATCH_UP of them, so people keep
  238. ; their speed while the picture updates less often (M7.3).
  239. MOVE_CATCH_UP = 4
  240. move_people:
  241. lda irq_frames
  242. sec
  243. sbc moved_frames ; frames since the last call
  244. beq @done
  245. cmp #MOVE_CATCH_UP + 1
  246. bcc @due
  247. lda #MOVE_CATCH_UP
  248. @due: sta move_due
  249. lda irq_frames
  250. sta moved_frames
  251. @frame: inc move_frame
  252. lda move_frame
  253. and #7
  254. @next: cmp people_count
  255. bcs @last
  256. sta move_index
  257. tax
  258. jsr move_person
  259. lda move_index
  260. clc
  261. adc #8
  262. bcc @next ; past 255: done
  263. @last: dec move_due
  264. bne @frame
  265. @done: rts
  266. ; X = person. Occasionally pick a new direction; if the step is blocked by
  267. ; water, stone or the world edge, try other directions. Never steps onto a
  268. ; blocking tile.
  269. move_person:
  270. stx person
  271. jsr random_byte
  272. and #15
  273. bne @start
  274. jsr random_byte
  275. and #3
  276. sta people_dir,x
  277. @start: lda #TURN_TRIES
  278. sta tries
  279. @try: ldx person
  280. lda people_cx,x
  281. sta cand_cx
  282. lda people_sx,x
  283. sta cand_sx
  284. lda people_cy,x
  285. sta cand_cy
  286. lda people_sy,x
  287. sta cand_sy
  288. lda people_dir,x
  289. beq @north
  290. cmp #DIR_E
  291. beq @east
  292. cmp #DIR_S
  293. beq @south
  294. dec cand_sx ; west
  295. bpl @check
  296. lda #3
  297. sta cand_sx
  298. dec cand_cx
  299. bpl @check
  300. bmi @blocked
  301. @east: inc cand_sx
  302. lda cand_sx
  303. cmp #4
  304. bne @check
  305. lda #0
  306. sta cand_sx
  307. inc cand_cx
  308. lda cand_cx
  309. cmp #WORLD_WIDTH
  310. bne @check
  311. beq @blocked
  312. @north: dec cand_sy
  313. bpl @check
  314. lda #7
  315. sta cand_sy
  316. dec cand_cy
  317. bpl @check
  318. bmi @blocked
  319. @south: inc cand_sy
  320. lda cand_sy
  321. cmp #8
  322. bne @check
  323. lda #0
  324. sta cand_sy
  325. inc cand_cy
  326. lda cand_cy
  327. cmp #WORLD_HEIGHT
  328. beq @blocked
  329. @check: jsr candidate_blocked
  330. bcs @blocked
  331. ldx person
  332. jmp store_step ; paths.inc: also counts a step into a new cell
  333. @blocked:
  334. ldx person
  335. lda tries
  336. cmp #TURN_TRIES
  337. bne @rotate
  338. jsr random_byte ; first block: random direction
  339. jmp @turn
  340. @rotate:
  341. lda people_dir,x
  342. clc
  343. adc #1
  344. @turn: and #3
  345. sta people_dir,x
  346. dec tries
  347. beq @stay
  348. jmp @try
  349. @stay: rts ; boxed in: stay put this step
  350. ; Carry set if the pixel at (cand_cx/cand_sx, cand_cy/cand_sy) is water or
  351. ; stone: a water or stone tile, or a blue %01 water pixel of a bridge tile
  352. ; (bridges are walkable only on their road pixels). Changes A, X and Y.
  353. candidate_blocked:
  354. ldx cand_cy
  355. ldy cand_cx
  356. jsr point_world_row
  357. lda (world_ptr),y
  358. cmp #STONE
  359. bcc @open
  360. cmp #WATER_SE+1
  361. bcc @blocked
  362. cmp #BRIDGE_H
  363. bcc @open
  364. asl a ; tile * 8 (TILE_COUNT <= 32)
  365. asl a
  366. asl a
  367. ora cand_sy
  368. tax
  369. lda tile_patterns,x
  370. ldy cand_sx
  371. and pixel_masks,y
  372. and #$55 ; a set low bit of the pair = %01 = water
  373. beq @open
  374. @blocked:
  375. sec
  376. rts
  377. @open: clc
  378. rts
  379. ; Main loop, every frame, after the rebuild target is chosen (M7.2): people
  380. ; are drawn only into the hidden buffer, never into the shown one, so the
  381. ; vertical blank does no people work however many there are. If the hidden
  382. ; buffer is complete, its old dots are put back and everyone in it is drawn
  383. ; again (buf_dots marks it). A crossing scroll step waits for buf_dots (see
  384. ; apply_view). With the view still, no step wanted and the hidden buffer at
  385. ; the view's own cell, it is flipped in at the next vertical blank.
  386. people_update:
  387. ldx hidden
  388. lda buf_ready,x
  389. beq @done ; being rebuilt: drawn once it is complete
  390. stx people_buf
  391. jsr restore_people
  392. jsr draw_people
  393. ldx hidden
  394. lda #1
  395. sta buf_dots,x
  396. jsr get_steps ; this frame's keys, zero at the world edges
  397. lda view_moved
  398. ora step_x ; a step is wanted: the hidden buffer must
  399. ora step_y ; stay free for that crossing's rebuild
  400. bne @done
  401. ldx hidden
  402. lda buf_cx,x
  403. cmp view_cx
  404. bne @done
  405. lda buf_cy,x
  406. cmp view_cy
  407. bne @done
  408. lda #1
  409. sta people_flip_pending
  410. @done: rts
  411. ; X = buffer: point lst_low, lst_high and lst_byte at its saved-byte list.
  412. ; Keeps X and Y.
  413. point_lists:
  414. lda #0
  415. sta lst_low
  416. sta lst_high
  417. sta lst_byte
  418. lda list_page_offset,x
  419. clc
  420. adc #>drawn_low_a
  421. sta lst_low+1
  422. adc #1 ; carry is clear
  423. sta lst_high+1
  424. adc #1
  425. sta lst_byte+1
  426. rts
  427. ; Put back the bitmap bytes saved when people were last drawn into buffer
  428. ; people_buf, newest first, so people that share a byte restore correctly.
  429. restore_people:
  430. ldx people_buf
  431. jsr point_lists
  432. ldy drawn_count,x
  433. beq @done
  434. ldx #0
  435. @loop: dey
  436. lda (lst_low),y
  437. sta bmp
  438. lda (lst_high),y
  439. sta bmp+1
  440. lda (lst_byte),y
  441. sta (bmp,x) ; X = 0
  442. tya
  443. bne @loop
  444. @done: ldx people_buf
  445. lda #0
  446. sta drawn_count,x
  447. rts
  448. ; Draw every person inside buffer people_buf (relative to its top-left world
  449. ; cell), saving the byte under each one in that buffer's list. The person
  450. ; shown in the status lines blinks: it is left out (draw_skip) for 16 of
  451. ; every 32 frames. Call restore_people first.
  452. draw_people:
  453. lda #$FF
  454. sta draw_skip
  455. lda move_frame
  456. and #16
  457. beq @blink
  458. lda shown_person
  459. sta draw_skip
  460. @blink:
  461. lda #0
  462. sta list_pos
  463. ldx people_buf
  464. jsr point_lists
  465. lda buffer_bitmap_offsets,x
  466. sta bitmap_offset
  467. lda buf_cx,x
  468. sta draw_cx
  469. lda buf_cy,x
  470. sta draw_cy
  471. lda buf_rows,x
  472. sta draw_rows
  473. ldx #0
  474. @person:
  475. cpx draw_skip
  476. beq @next
  477. lda people_cy,x
  478. sec
  479. sbc draw_cy
  480. cmp draw_rows ; also rejects people above the buffer
  481. bcs @next
  482. tay ; Y = buffer row
  483. lda people_cx,x
  484. sec
  485. sbc draw_cx
  486. cmp #MAP_COLS_DRAWN
  487. bcs @next
  488. sta draw_col
  489. lda #0 ; bmp = row base + col * 8 + sy
  490. sta bmp+1
  491. lda draw_col
  492. asl a
  493. rol bmp+1
  494. asl a
  495. rol bmp+1
  496. asl a
  497. rol bmp+1
  498. ora people_sy,x
  499. clc
  500. adc bitmap_row_low,y
  501. sta bmp
  502. lda bmp+1
  503. adc bitmap_row_high,y
  504. clc
  505. adc bitmap_offset ; buffer A or B
  506. sta bmp+1
  507. ldy #0
  508. lda (bmp),y
  509. ldy list_pos
  510. sta (lst_byte),y
  511. lda bmp
  512. sta (lst_low),y
  513. lda bmp+1
  514. sta (lst_high),y
  515. lda (lst_byte),y
  516. ldy people_sx,x
  517. ora pixel_masks,y ; pixel %11 = colour RAM = white
  518. ldy #0
  519. sta (bmp),y
  520. inc list_pos
  521. @next: inx
  522. cpx people_count
  523. bcc @person
  524. lda list_pos
  525. ldx people_buf
  526. sta drawn_count,x
  527. rts
  528. .segment "RODATA"
  529. pixel_masks: .byte $C0, $30, $0C, $03
  530. ; Name syllables, 4 bytes each, unused bytes 0. Start syllables 0-15, middle
  531. ; 16-23 (at most 2 letters, so a name fits in 10), male ends 24-39, female
  532. ; ends 40-55.
  533. .macro SYL text
  534. .byte text
  535. .res 4 - .strlen(text), 0
  536. .endmacro
  537. SYL_MIDDLE = 16
  538. syllables:
  539. SYL "AL"
  540. SYL "BRAN"
  541. SYL "CAR"
  542. SYL "DOR"
  543. SYL "EL"
  544. SYL "FIN"
  545. SYL "GAR"
  546. SYL "HAL"
  547. SYL "IS"
  548. SYL "JOR"
  549. SYL "KEL"
  550. SYL "LOR"
  551. SYL "MAR"
  552. SYL "NOR"
  553. SYL "OS"
  554. SYL "TAR"
  555. SYL "A" ; middle
  556. SYL "E"
  557. SYL "I"
  558. SYL "O"
  559. SYL "AN"
  560. SYL "EL"
  561. SYL "OR"
  562. SYL "IN"
  563. SYL "DRIC" ; male ends
  564. SYL "RIC"
  565. SYL "DAN"
  566. SYL "GAR"
  567. SYL "MOR"
  568. SYL "TON"
  569. SYL "BERT"
  570. SYL "WIN"
  571. SYL "RUS"
  572. SYL "VAN"
  573. SYL "DOR"
  574. SYL "LAN"
  575. SYL "ROK"
  576. SYL "MUND"
  577. SYL "SON"
  578. SYL "THAS"
  579. SYL "A" ; female ends
  580. SYL "IA"
  581. SYL "ENA"
  582. SYL "LYN"
  583. SYL "WEN"
  584. SYL "ELLE"
  585. SYL "ISA"
  586. SYL "RA"
  587. SYL "INA"
  588. SYL "ETH"
  589. SYL "NIA"
  590. SYL "ANE"
  591. SYL "ORA"
  592. SYL "ESSA"
  593. SYL "ELL"
  594. SYL "IE"
  595. .assert * - syllables = 56 * 4, error, "56 syllables"
  596. syl_end_base: .byte 24, 40
  597. list_page_offset: .byte 0, LIST_PAGES
  598. buffer_bitmap_offsets: .byte 0, >(BITMAP_B - BITMAP_A)
  599. bitmap_row_low:
  600. .repeat BUF_ROWS, row
  601. .byte <(BITMAP_A + 320 * row)
  602. .endrepeat
  603. bitmap_row_high:
  604. .repeat BUF_ROWS, row
  605. .byte >(BITMAP_A + 320 * row)
  606. .endrepeat
  607. .segment "BSS"
  608. people_count: .res 1 ; 1-255 people exist: persons 0 .. people_count - 1
  609. move_frame: .res 1
  610. moved_frames: .res 1 ; irq_frames at the last move_people
  611. move_due: .res 1
  612. move_index: .res 1
  613. draw_skip: .res 1 ; person left out of the last draw, $FF = none
  614. shown_person: .res 1 ; whose name and scores the status lines show
  615. shown_dirty: .res 1 ; nonzero: the status lines must be rewritten
  616. attr_sum: .res 1
  617. name_pos: .res 1
  618. syl_left: .res 1
  619. person: .res 1
  620. tries: .res 1
  621. cand_cx: .res 1
  622. cand_sx: .res 1
  623. cand_cy: .res 1
  624. cand_sy: .res 1
  625. drawn_count: .res 2 ; entries in each buffer's saved-byte list
  626. list_pos: .res 1
  627. bitmap_offset: .res 1
  628. people_buf: .res 1 ; buffer that restore/draw_people work on
  629. draw_cx: .res 1
  630. draw_cy: .res 1
  631. draw_rows: .res 1
  632. draw_col: .res 1
  633. .assert * <= SCREEN_A, lderror, "People data must fit below VIC bank 1"

Powered by TurnKey Linux.