; Worn paths (M5.2, R7). ; ; Every world cell has a walk count (walk_counts, laid out like world_tiles). ; A person stepping into a cell adds one (up to 255); every count loses one ; each 60 seconds (DECAY_CELLS cells every DECAY_FRAMES frames: 4000 cells in ; 3600 frames). A land, grass or tree cell whose count reaches PATH_ON becomes ; the matching path tile; a path whose count falls below PATH_OFF turns back. ; ; A changed tile is written to world_tiles at once and queued. Each frame the ; main loop redraws queued cells in the hidden buffer only, so the shown map ; is never written while it is displayed; a cell waits in the queue until both ; buffers have it. The redraw keeps the people's dots in the cell (a dot is the ; only %11 pixel) and updates the saved bytes under them, so restoring a dot ; later puts back the new tile. If the hidden buffer is in the middle of a ; rebuild, the rest of the rebuild draws every cell (its skip test compares ; world tiles, which have changed underneath it) and the changed cells are ; redrawn when it is ready. ; ; The test build (PATH_TEST, tools/run.ps1 -PathTest) uses small thresholds ; and fast decay so paths can be watched and checked within a minute. .ifdef PATH_TEST PATH_ON = 4 PATH_OFF = 2 DECAY_FRAMES = 1 DECAY_CELLS = 10 ; every count loses one each 400 frames .else PATH_ON = 128 PATH_OFF = 64 DECAY_FRAMES = 9 DECAY_CELLS = 10 ; every count loses one each 3600 frames .endif CHANGE_SLOTS = 8 cnt_ptr = $56 ; walk count row ; The walk counts live in the free RAM at $C000-$CF9F (no VIC bank uses it): ; below VIC bank 1 there is no room for them next to the program. walk_counts = $C000 COUNT_BYTES = WORLD_WIDTH * WORLD_HEIGHT .export walk_counts ap_bmp = $58 ; cell being redrawn: bitmap ap_scr = $5A ; screen RAM .segment "CODE" ; New world: all counts 0, no queued changes, decay from the first cell. reset_paths: lda #walk_counts sta cnt_ptr+1 ldx #>COUNT_BYTES ; whole pages lda #0 tay @clear: sta (cnt_ptr),y iny bne @clear inc cnt_ptr+1 dex bne @clear @rest: sta (cnt_ptr),y ; the last part page: exactly 4,000 bytes, so iny ; nothing after the counts is touched cpy #(walk_counts - world_tiles) sta cnt_ptr+1 rts ; X = person: move it to the candidate position; a step into another cell ; counts for that cell. store_step: lda cand_cx eor people_cx,x sta new_cell lda cand_cy eor people_cy,x ora new_cell sta new_cell 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 lda new_cell bne count_step rts ; A person stepped into cell (cand_cx, cand_cy). count_step: ldx cand_cy ldy cand_cx jsr point_counts lda (cnt_ptr),y cmp #255 beq @full clc adc #1 sta (cnt_ptr),y @full: cmp #PATH_ON bcc @done jsr point_world_row lda (world_ptr),y cmp #STONE ; land, grass or trees (paths are above STONE) bcs @done clc adc #PATH_LAND - LAND jmp change_tile @done: rts ; Main loop, every frame: every DECAY_FRAMES frames, the next DECAY_CELLS ; counts lose one; a path whose count is below PATH_OFF turns back. decay_counts: dec decay_wait bne @done lda #DECAY_FRAMES sta decay_wait lda #DECAY_CELLS sta decay_left @cell: ldx decay_cy ldy decay_cx jsr point_counts lda (cnt_ptr),y beq @check sec sbc #1 sta (cnt_ptr),y @check: cmp #PATH_OFF bcs @next jsr point_world_row lda (world_ptr),y cmp #PATH_LAND bcc @next cmp #PATH_TREE + 1 bcs @next sec sbc #PATH_LAND - LAND jsr change_tile @next: inc decay_cx lda decay_cx cmp #WORLD_WIDTH bne @more lda #0 sta decay_cx inc decay_cy lda decay_cy cmp #WORLD_HEIGHT bne @more lda #0 sta decay_cy @more: dec decay_left bne @cell @done: rts ; A = new tile for the cell at column Y of the world row world_ptr points ; to, X = its row. Store it and queue the cell for both buffers. If the ; queue is full nothing changes; the count is checked again next time. change_tile: sta chg_tile stx chg_ty ldx chg_count cpx #CHANGE_SLOTS bcs change_full lda chg_tile sta (world_ptr),y path_changed: ; check hook: A = new tile, Y = column tya sta chg_cx,x lda chg_ty sta chg_cy,x lda #0 sta chg_done,x inc chg_count change_full: rts ; Main loop, before choose_rebuild_target: bring the hidden buffer up to ; date with the queued cells, then drop cells that both buffers have. apply_changes: lda chg_count bne @some rts @some: ldx hidden stx ap_buf lda buffer_bit,x sta ap_bit lda buf_ready,x bne @ready ldy chg_count ; being rebuilt: is a queued cell new to it? @mark: dey bmi @drop ; no: the rebuild already allowed for them all lda chg_done,y and ap_bit bne @mark cpx rb_buf bne @drop lda #0 ; yes: the rest of the rebuild draws every cell, sta rb_diff ; also in the row it is on (its skip test lda #no_old_tiles ; are redrawn once it is ready, which mends a sta rb_old+2 ; changed cell the rebuild had already drawn. jmp @drop ; (Restarting the rebuild cost up to 8 frames.) @ready: ldy #0 @entry: cpy chg_count beq @drop lda chg_done,y and ap_bit bne @have sty ap_index jsr redraw_cell ldy ap_index lda chg_done,y ora ap_bit sta chg_done,y @have: iny bne @entry @drop: ldx #0 ; keep the entries one buffer still lacks ldy #0 @keep: cpx chg_count beq @kept lda chg_done,x cmp #3 beq @gone lda chg_cx,x sta chg_cx,y lda chg_cy,x sta chg_cy,y lda chg_done,x sta chg_done,y iny @gone: inx bne @keep @kept: sty chg_count rts ; Redraw queued cell Y in buffer ap_buf (complete, hidden) if it lies inside ; the buffer: colour, eight bitmap bytes with the dots kept, and the saved ; bytes of the dots in the cell. redraw_cell: ldx ap_buf lda chg_cx,y sec sbc buf_cx,x cmp #MAP_COLS_DRAWN bcs @out sta ap_col lda chg_cy,y sec sbc buf_cy,x cmp buf_rows,x bcc @in @out: rts @in: sta ap_row lda chg_cx,y ; the cell's tile now sta ap_wx ldx chg_cy,y ldy ap_wx jsr point_world_row lda (world_ptr),y sta ap_tile ldy ap_row ; screen RAM colour ldx ap_buf lda screen_row_low,y clc adc ap_col sta ap_scr lda screen_row_high,y adc buffer_screen_high,x sta ap_scr+1 ldx ap_tile lda tile_colors,x ldy #0 sta (ap_scr),y lda tile_times8,x sta ap_pat lda #0 ; ap_bmp = row base + col * 8, in buffer ap_buf sta ap_bmp+1 lda ap_col asl a rol ap_bmp+1 asl a rol ap_bmp+1 asl a rol ap_bmp+1 ldy ap_row clc adc bitmap_row_low,y sta ap_bmp lda ap_bmp+1 adc bitmap_row_high,y ldx ap_buf clc adc buffer_bitmap_offsets,x sta ap_bmp+1 ldy #7 @byte: lda (ap_bmp),y ; dots: the %11 pixels (no tile uses %11) sta ap_cur lsr a and ap_cur and #$55 sta ap_dots asl a ora ap_dots sta ap_dots tya clc adc ap_pat tax lda tile_patterns,x ora ap_dots sta (ap_bmp),y dey bpl @byte ldx ap_buf ; saved bytes under dots in this cell jsr point_lists ldy drawn_count,x @saved: tya beq @done dey lda (lst_high),y cmp ap_bmp+1 bne @saved lda (lst_low),y sec sbc ap_bmp cmp #8 ; cells never straddle a page bcs @saved clc adc ap_pat tax lda tile_patterns,x sta (lst_byte),y jmp @saved @done: rts .segment "RODATA" buffer_bit: .byte 1, 2 .segment "BSS" chg_count: .res 1 new_cell: .res 1 chg_tile: .res 1 chg_ty: .res 1 chg_cx: .res CHANGE_SLOTS chg_cy: .res CHANGE_SLOTS chg_done: .res CHANGE_SLOTS ; bit 0: buffer A has it, bit 1: buffer B decay_cx: .res 1 decay_cy: .res 1 decay_wait: .res 1 decay_left: .res 1 ap_buf: .res 1 ap_bit: .res 1 ap_index: .res 1 ap_col: .res 1 ap_row: .res 1 ap_wx: .res 1 ap_tile: .res 1 ap_pat: .res 1 ap_cur: .res 1 ap_dots: .res 1 .assert * <= SCREEN_A, lderror, "Path data must fit below VIC bank 1"