; People: 16 walkers stored as world positions, one multicolour pixel each. ; A position is a world cell (cx 0-79, cy 0-49) plus the pixel inside it ; (sx 0-3 across, sy 0-7 down). Water and stone tiles block movement. PEOPLE_COUNT = 16 DIR_N = 0 DIR_E = 1 DIR_S = 2 DIR_W = 3 TURN_TRIES = 5 ; current direction, then all four from a random start .assert STONE + 1 = WATER, error, "Blocking tiles must be contiguous" .assert WATER_SE + 1 = ROAD_H, error, "Blocking tiles must be contiguous" .export people_cx, people_moved .segment "CODE" ; Put each person on a random walkable cell; person N goes in sector N mod 4, ; so the 16 people are spread over all four screens of the world. place_people: lda #0 sta drawn_count sta drawn_count+1 sta move_frame tax @person: stx person @retry_x: jsr random_byte and #63 cmp #SECTOR_WIDTH bcs @retry_x sta cand_cx txa and #3 tax lda cand_cx clc adc sector_left,x sta cand_cx @retry_y: jsr random_byte and #31 cmp #SECTOR_HEIGHT bcs @retry_y clc adc sector_top,x sta cand_cy jsr candidate_blocked ldx person bcs @person lda cand_cx sta people_cx,x lda cand_cy sta people_cy,x jsr random_byte and #3 sta people_sx,x jsr random_byte and #7 sta people_sy,x jsr random_byte and #3 sta people_dir,x inx cpx #PEOPLE_COUNT bne @person rts ; Move two people per frame: each person takes one pixel step every 8 frames. move_people: inc move_frame lda move_frame and #7 tax jsr move_person lda move_frame and #7 ora #8 tax ; fall through ; X = person. Occasionally pick a new direction; if the step is blocked by ; water, stone or the world edge, try other directions. Never steps onto a ; blocking tile. move_person: stx person jsr random_byte and #15 bne @start jsr random_byte and #3 sta people_dir,x @start: lda #TURN_TRIES sta tries @try: ldx person lda people_cx,x sta cand_cx lda people_sx,x sta cand_sx lda people_cy,x sta cand_cy lda people_sy,x sta cand_sy lda people_dir,x beq @north cmp #DIR_E beq @east cmp #DIR_S beq @south dec cand_sx ; west bpl @check lda #3 sta cand_sx dec cand_cx bpl @check bmi @blocked @east: inc cand_sx lda cand_sx cmp #4 bne @check lda #0 sta cand_sx inc cand_cx lda cand_cx cmp #WORLD_WIDTH bne @check beq @blocked @north: dec cand_sy bpl @check lda #7 sta cand_sy dec cand_cy bpl @check bmi @blocked @south: inc cand_sy lda cand_sy cmp #8 bne @check lda #0 sta cand_sy inc cand_cy lda cand_cy cmp #WORLD_HEIGHT beq @blocked @check: jsr candidate_blocked bcs @blocked ldx person lda cand_cx sta people_cx,x lda cand_sx sta people_sx,x lda cand_cy sta people_cy,x lda cand_sy sta people_sy,x rts @blocked: ldx person lda tries cmp #TURN_TRIES bne @rotate jsr random_byte ; first block: random direction jmp @turn @rotate: lda people_dir,x clc adc #1 @turn: and #3 sta people_dir,x dec tries beq @stay jmp @try @stay: rts ; boxed in: stay put this step ; Carry set if the tile at (cand_cx, cand_cy) is water or stone. candidate_blocked: ldx cand_cy ldy cand_cx jsr point_world_row lda (world_ptr),y cmp #STONE bcc @open cmp #WATER_SE+1 bcs @open sec rts @open: clc rts ; Put back the bitmap bytes saved when people were last drawn into the hidden ; buffer (two frames ago), newest first, so people that share a byte restore ; correctly. Only those bytes change: the rest of the map is never redrawn. restore_people: ldx hidden lda list_starts,x sta list_start clc adc drawn_count,x tax ; X = one past the newest saved byte cpx list_start beq @done @loop: dex lda drawn_low,x sta bmp lda drawn_high,x sta bmp+1 lda drawn_byte,x ldy #0 sta (bmp),y cpx list_start bne @loop @done: ldx hidden lda #0 sta drawn_count,x rts ; Draw every person inside the view (the top-left screen) into the hidden ; buffer, saving the byte under each one in that buffer's list. ; Call restore_people first. draw_people: ldx hidden lda list_starts,x sta list_start sta list_pos lda buffer_bitmap_offsets,x sta bitmap_offset ldx #0 @person: lda people_cy,x cmp #VIEW_HEIGHT bcs @next tay lda people_cx,x cmp #VIEW_WIDTH bcs @next lda #0 ; bmp = row base + cx * 8 + sy sta bmp+1 lda people_cx,x asl a rol bmp+1 asl a rol bmp+1 asl a rol bmp+1 ora people_sy,x clc adc bitmap_row_low,y sta bmp lda bmp+1 adc bitmap_row_high,y clc adc bitmap_offset ; buffer A or B sta bmp+1 ldy #0 lda (bmp),y ldy list_pos sta drawn_byte,y lda bmp sta drawn_low,y lda bmp+1 sta drawn_high,y lda drawn_byte,y ldy people_sx,x ora pixel_masks,y ; pixel %11 = colour RAM = light red ldy #0 sta (bmp),y inc list_pos @next: inx cpx #PEOPLE_COUNT bne @person lda list_pos sec sbc list_start ldx hidden sta drawn_count,x rts .segment "RODATA" pixel_masks: .byte $C0, $30, $0C, $03 list_starts: .byte 0, PEOPLE_COUNT buffer_bitmap_offsets: .byte 0, >(BITMAP_B - BITMAP_A) bitmap_row_low: .repeat VIEW_HEIGHT, row .byte <(BITMAP_A + 320 * row) .endrepeat bitmap_row_high: .repeat VIEW_HEIGHT, row .byte >(BITMAP_A + 320 * row) .endrepeat .segment "BSS" ; Keep these five tables together: tools/check-people.ps1 saves them as one block. people_cx: .res PEOPLE_COUNT people_sx: .res PEOPLE_COUNT people_cy: .res PEOPLE_COUNT people_sy: .res PEOPLE_COUNT people_dir: .res PEOPLE_COUNT move_frame: .res 1 person: .res 1 tries: .res 1 cand_cx: .res 1 cand_sx: .res 1 cand_cy: .res 1 cand_sy: .res 1 ; Saved-byte lists: entries 0-15 belong to buffer A, 16-31 to buffer B. drawn_count: .res 2 drawn_low: .res 2 * PEOPLE_COUNT drawn_high: .res 2 * PEOPLE_COUNT drawn_byte: .res 2 * PEOPLE_COUNT list_start: .res 1 list_pos: .res 1 bitmap_offset: .res 1 .assert * <= SCREEN_A, lderror, "People data must fit below VIC bank 1"