You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
7.4KB

  1. ; People: 16 walkers stored as world positions, one multicolour pixel each.
  2. ; A position is a world cell (cx 0-79, cy 0-49) plus the pixel inside it
  3. ; (sx 0-3 across, sy 0-7 down). Water and stone tiles block movement.
  4. PEOPLE_COUNT = 16
  5. DIR_N = 0
  6. DIR_E = 1
  7. DIR_S = 2
  8. DIR_W = 3
  9. TURN_TRIES = 5 ; current direction, then all four from a random start
  10. .assert STONE + 1 = WATER, error, "Blocking tiles must be contiguous"
  11. .assert WATER_SE + 1 = ROAD_H, error, "Blocking tiles must be contiguous"
  12. .export people_cx, people_moved
  13. .segment "CODE"
  14. ; Put each person on a random walkable cell; person N goes in sector N mod 4,
  15. ; so the 16 people are spread over all four screens of the world.
  16. place_people:
  17. lda #0
  18. sta drawn_count
  19. sta drawn_count+1
  20. sta move_frame
  21. tax
  22. @person:
  23. stx person
  24. @retry_x:
  25. jsr random_byte
  26. and #63
  27. cmp #SECTOR_WIDTH
  28. bcs @retry_x
  29. sta cand_cx
  30. txa
  31. and #3
  32. tax
  33. lda cand_cx
  34. clc
  35. adc sector_left,x
  36. sta cand_cx
  37. @retry_y:
  38. jsr random_byte
  39. and #31
  40. cmp #SECTOR_HEIGHT
  41. bcs @retry_y
  42. clc
  43. adc sector_top,x
  44. sta cand_cy
  45. jsr candidate_blocked
  46. ldx person
  47. bcs @person
  48. lda cand_cx
  49. sta people_cx,x
  50. lda cand_cy
  51. sta people_cy,x
  52. jsr random_byte
  53. and #3
  54. sta people_sx,x
  55. jsr random_byte
  56. and #7
  57. sta people_sy,x
  58. jsr random_byte
  59. and #3
  60. sta people_dir,x
  61. inx
  62. cpx #PEOPLE_COUNT
  63. bne @person
  64. rts
  65. ; Move two people per frame: each person takes one pixel step every 8 frames.
  66. move_people:
  67. inc move_frame
  68. lda move_frame
  69. and #7
  70. tax
  71. jsr move_person
  72. lda move_frame
  73. and #7
  74. ora #8
  75. tax
  76. ; fall through
  77. ; X = person. Occasionally pick a new direction; if the step is blocked by
  78. ; water, stone or the world edge, try other directions. Never steps onto a
  79. ; blocking tile.
  80. move_person:
  81. stx person
  82. jsr random_byte
  83. and #15
  84. bne @start
  85. jsr random_byte
  86. and #3
  87. sta people_dir,x
  88. @start: lda #TURN_TRIES
  89. sta tries
  90. @try: ldx person
  91. lda people_cx,x
  92. sta cand_cx
  93. lda people_sx,x
  94. sta cand_sx
  95. lda people_cy,x
  96. sta cand_cy
  97. lda people_sy,x
  98. sta cand_sy
  99. lda people_dir,x
  100. beq @north
  101. cmp #DIR_E
  102. beq @east
  103. cmp #DIR_S
  104. beq @south
  105. dec cand_sx ; west
  106. bpl @check
  107. lda #3
  108. sta cand_sx
  109. dec cand_cx
  110. bpl @check
  111. bmi @blocked
  112. @east: inc cand_sx
  113. lda cand_sx
  114. cmp #4
  115. bne @check
  116. lda #0
  117. sta cand_sx
  118. inc cand_cx
  119. lda cand_cx
  120. cmp #WORLD_WIDTH
  121. bne @check
  122. beq @blocked
  123. @north: dec cand_sy
  124. bpl @check
  125. lda #7
  126. sta cand_sy
  127. dec cand_cy
  128. bpl @check
  129. bmi @blocked
  130. @south: inc cand_sy
  131. lda cand_sy
  132. cmp #8
  133. bne @check
  134. lda #0
  135. sta cand_sy
  136. inc cand_cy
  137. lda cand_cy
  138. cmp #WORLD_HEIGHT
  139. beq @blocked
  140. @check: jsr candidate_blocked
  141. bcs @blocked
  142. ldx person
  143. lda cand_cx
  144. sta people_cx,x
  145. lda cand_sx
  146. sta people_sx,x
  147. lda cand_cy
  148. sta people_cy,x
  149. lda cand_sy
  150. sta people_sy,x
  151. rts
  152. @blocked:
  153. ldx person
  154. lda tries
  155. cmp #TURN_TRIES
  156. bne @rotate
  157. jsr random_byte ; first block: random direction
  158. jmp @turn
  159. @rotate:
  160. lda people_dir,x
  161. clc
  162. adc #1
  163. @turn: and #3
  164. sta people_dir,x
  165. dec tries
  166. beq @stay
  167. jmp @try
  168. @stay: rts ; boxed in: stay put this step
  169. ; Carry set if the tile at (cand_cx, cand_cy) is water or stone.
  170. candidate_blocked:
  171. ldx cand_cy
  172. ldy cand_cx
  173. jsr point_world_row
  174. lda (world_ptr),y
  175. cmp #STONE
  176. bcc @open
  177. cmp #WATER_SE+1
  178. bcs @open
  179. sec
  180. rts
  181. @open: clc
  182. rts
  183. ; Put back the bitmap bytes saved when people were last drawn into the hidden
  184. ; buffer (two frames ago), newest first, so people that share a byte restore
  185. ; correctly. Only those bytes change: the rest of the map is never redrawn.
  186. restore_people:
  187. ldx hidden
  188. lda list_starts,x
  189. sta list_start
  190. clc
  191. adc drawn_count,x
  192. tax ; X = one past the newest saved byte
  193. cpx list_start
  194. beq @done
  195. @loop: dex
  196. lda drawn_low,x
  197. sta bmp
  198. lda drawn_high,x
  199. sta bmp+1
  200. lda drawn_byte,x
  201. ldy #0
  202. sta (bmp),y
  203. cpx list_start
  204. bne @loop
  205. @done: ldx hidden
  206. lda #0
  207. sta drawn_count,x
  208. rts
  209. ; Draw every person inside the view (the top-left screen) into the hidden
  210. ; buffer, saving the byte under each one in that buffer's list.
  211. ; Call restore_people first.
  212. draw_people:
  213. ldx hidden
  214. lda list_starts,x
  215. sta list_start
  216. sta list_pos
  217. lda buffer_bitmap_offsets,x
  218. sta bitmap_offset
  219. ldx #0
  220. @person:
  221. lda people_cy,x
  222. cmp #VIEW_HEIGHT
  223. bcs @next
  224. tay
  225. lda people_cx,x
  226. cmp #VIEW_WIDTH
  227. bcs @next
  228. lda #0 ; bmp = row base + cx * 8 + sy
  229. sta bmp+1
  230. lda people_cx,x
  231. asl a
  232. rol bmp+1
  233. asl a
  234. rol bmp+1
  235. asl a
  236. rol bmp+1
  237. ora people_sy,x
  238. clc
  239. adc bitmap_row_low,y
  240. sta bmp
  241. lda bmp+1
  242. adc bitmap_row_high,y
  243. clc
  244. adc bitmap_offset ; buffer A or B
  245. sta bmp+1
  246. ldy #0
  247. lda (bmp),y
  248. ldy list_pos
  249. sta drawn_byte,y
  250. lda bmp
  251. sta drawn_low,y
  252. lda bmp+1
  253. sta drawn_high,y
  254. lda drawn_byte,y
  255. ldy people_sx,x
  256. ora pixel_masks,y ; pixel %11 = colour RAM = light red
  257. ldy #0
  258. sta (bmp),y
  259. inc list_pos
  260. @next: inx
  261. cpx #PEOPLE_COUNT
  262. bne @person
  263. lda list_pos
  264. sec
  265. sbc list_start
  266. ldx hidden
  267. sta drawn_count,x
  268. rts
  269. .segment "RODATA"
  270. pixel_masks: .byte $C0, $30, $0C, $03
  271. list_starts: .byte 0, PEOPLE_COUNT
  272. buffer_bitmap_offsets: .byte 0, >(BITMAP_B - BITMAP_A)
  273. bitmap_row_low:
  274. .repeat VIEW_HEIGHT, row
  275. .byte <(BITMAP_A + 320 * row)
  276. .endrepeat
  277. bitmap_row_high:
  278. .repeat VIEW_HEIGHT, row
  279. .byte >(BITMAP_A + 320 * row)
  280. .endrepeat
  281. .segment "BSS"
  282. ; Keep these five tables together: tools/check-people.ps1 saves them as one block.
  283. people_cx: .res PEOPLE_COUNT
  284. people_sx: .res PEOPLE_COUNT
  285. people_cy: .res PEOPLE_COUNT
  286. people_sy: .res PEOPLE_COUNT
  287. people_dir: .res PEOPLE_COUNT
  288. move_frame: .res 1
  289. person: .res 1
  290. tries: .res 1
  291. cand_cx: .res 1
  292. cand_sx: .res 1
  293. cand_cy: .res 1
  294. cand_sy: .res 1
  295. ; Saved-byte lists: entries 0-15 belong to buffer A, 16-31 to buffer B.
  296. drawn_count: .res 2
  297. drawn_low: .res 2 * PEOPLE_COUNT
  298. drawn_high: .res 2 * PEOPLE_COUNT
  299. drawn_byte: .res 2 * PEOPLE_COUNT
  300. list_start: .res 1
  301. list_pos: .res 1
  302. bitmap_offset: .res 1
  303. .assert * <= SCREEN_A, lderror, "People data must fit below VIC bank 1"

Powered by TurnKey Linux.