|
- ; People: up to 255 walkers stored as world positions, one multicolour pixel
- ; each; people_count of them exist (16 in a new world). 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.
- ;
- ; Memory (M7.1): every field is its own 256-byte table indexed by person, so
- ; any count up to 255 needs no other change. The tables sit in RAM the VIC-II
- ; never shows: the gaps in the two video banks (see the asserts below).
- PEOPLE_MAX = 255
- .ifndef PEOPLE_START
- PEOPLE_START = 16 ; people in a new world (checks may build with more)
- .endif
- .assert PEOPLE_START >= 1 .and PEOPLE_START <= PEOPLE_MAX, error, "PEOPLE_START must be 1-255"
- ATTR_COUNT = 6 ; STR, DEX, CON, INT, WIS, CHA
- NAME_LENGTH = 10
- people_cx = $4C00 ; bank 1, after the second text screen
- people_sx = $4D00
- people_cy = $4E00
- people_sy = $4F00
- people_dir = $5800 ; bank 1, after the font
- people_sex = $5900 ; 0 = male, 1 = female
- people_attr = $5A00 ; one page per score: STR at $5A00 ... CHA at $5F00
- drawn_low_a = $8C00 ; bank 2, after its second text screen: saved-byte
- drawn_high_a = $8D00 ; lists, three pages per buffer (B = A + 3 pages)
- drawn_byte_a = $8E00
- drawn_low_b = $8F00
- people_name = $9200 ; one page per letter: letter K of person N at
- ; people_name + K * 256 + N (screen codes)
- LIST_PAGES = 3
- .assert people_cx >= SCREEN_A + $0C00, error, "People tables overlap bank 1's text screens"
- .assert people_sy + $100 <= FONT_A, error, "People tables overlap bank 1's font"
- .assert people_dir >= FONT_A + $0800, error, "People tables overlap bank 1's font"
- .assert people_attr + ATTR_COUNT * $100 <= BITMAP_A, error, "People tables overlap bitmap A"
- .assert drawn_low_a >= SCREEN_B + $0C00, error, "People tables overlap bank 2's text screens"
- .assert drawn_low_b = drawn_low_a + LIST_PAGES * $100, error, "List layout"
- .assert people_name >= drawn_low_b + LIST_PAGES * $100, error, "Names overlap the lists"
- .assert people_name + NAME_LENGTH * $100 <= BITMAP_B, error, "Names overlap bitmap B"
- ; Zero page ($44-$4B: BASIC's area, unused here).
- lst_low = $44 ; saved-byte list of one buffer: addresses
- lst_high = $46
- lst_byte = $48 ; and the bytes under the dots
- tab_ptr = $4A ; a score or name-letter table
- 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"
- .assert BRIDGE_X + 1 = TILE_COUNT, error, "Bridge tiles must come last"
-
- .export people_cx, people_sx, people_cy, people_sy, people_dir, people_moved, people_attr
- .export people_name, people_sex, shown_person, draw_skip, people_count, move_frame
- .export drawn_low_a, drawn_high_a, drawn_byte_a, drawn_low_b
-
- .segment "CODE"
- ; New world: PEOPLE_START people, each on a random walkable cell; person N
- ; goes in sector N mod 4, so they are spread over all four screens of the
- ; world. Person 1 is shown.
- place_people:
- lda #0
- sta drawn_count
- sta drawn_count+1
- sta move_frame
- sta shown_person
- lda irq_frames ; no catching up on the time spent making the world
- sta moved_frames
- lda #PEOPLE_START
- sta people_count
- ldx #0
- @next: jsr new_person
- inx
- cpx people_count
- bne @next
- lda #1
- sta shown_dirty
- rts
-
- ; X = person: a random walkable position in sector X mod 4, a direction, a
- ; sex, a name and six scores. Returns X = person.
- new_person:
- stx person
- @retry_x:
- jsr random_byte
- and #63
- cmp #SECTOR_WIDTH
- bcs @retry_x
- sta cand_cx
- lda person
- 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 random_byte
- and #3
- sta cand_sx
- jsr random_byte
- and #7
- sta cand_sy
- jsr candidate_blocked
- bcs @retry_x
- 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
- jsr random_byte
- and #3
- sta people_dir,x
- jsr roll_attributes
- jsr roll_name
- ldx person
- rts
-
- ; + key (M7.3): one more person, if fewer than PEOPLE_MAX.
- add_person:
- ldx people_count
- cpx #PEOPLE_MAX
- bcs @full
- jsr new_person
- inc people_count
- lda #1
- sta shown_dirty
- @full: rts
-
- ; - key (M7.3): remove the last person, keeping at least one. Their dot goes
- ; when each buffer's saved bytes are next put back (people_update).
- remove_person:
- ldx people_count
- cpx #2
- bcc @last
- dex
- stx people_count
- cpx shown_person ; the shown person was the last: show the new last
- bne @shown
- dex
- stx shown_person
- @shown: lda #1
- sta shown_dirty
- @last: rts
-
- ; Roll person's six scores (R8): 3d6 each, 3-18.
- roll_attributes:
- lda #0
- sta tab_ptr
- lda #>people_attr
- sta tab_ptr+1
- ldy person
- @score: lda #0
- sta attr_sum
- ldx #3 ; three dice
- @die: jsr random_byte ; keeps X and Y
- and #7
- cmp #6
- bcs @die ; 6 or 7: roll again, so 0-5 are equally likely
- sec ; + 1: 1-6
- adc attr_sum
- sta attr_sum
- dex
- bne @die
- sta (tab_ptr),y
- inc tab_ptr+1 ; next score's page
- lda tab_ptr+1
- cmp #>people_attr + ATTR_COUNT
- bne @score
- rts
-
- ; Give person a sex (0 = male, 1 = female, 50/50) and a name (R8): a start
- ; syllable, half the time a middle one, and an end syllable from the male or
- ; female list; at most 4 + 2 + 4 = 10 letters, padded with spaces.
- roll_name:
- lda #0
- sta tab_ptr
- lda #>people_name
- sta tab_ptr+1
- ldy person
- lda #' '
- @blank: sta (tab_ptr),y
- inc tab_ptr+1
- ldx tab_ptr+1
- cpx #>people_name + NAME_LENGTH
- bne @blank
- lda #0
- sta name_pos
- jsr random_byte
- and #1
- sta people_sex,y
- jsr random_byte
- and #15
- jsr add_syllable ; start: syllables 0-15
- jsr random_byte
- bmi @end ; half the names have no middle syllable
- and #7
- ora #SYL_MIDDLE ; middle: syllables 16-23
- jsr add_syllable
- @end: jsr random_byte
- and #15
- ldy person
- ldx people_sex,y
- clc
- adc syl_end_base,x ; end: male 24-39, female 40-55
- ; fall through
-
- ; A = syllable number: append its letters (up to 4) to person's name at
- ; letter name_pos.
- add_syllable:
- asl a
- asl a
- tax
- lda #4
- sta syl_left
- @char: lda name_pos
- clc
- adc #>people_name
- sta tab_ptr+1
- lda syllables,x
- beq @done
- ldy person
- sta (tab_ptr),y
- inc name_pos
- inx
- dec syl_left
- bne @char
- @done: rts
-
- ; Move every eighth person per frame, starting with person move_frame mod 8:
- ; each person takes one pixel step every 8 frames, however many there are
- ; (more people take more time; the user chose speed over frame rate, M7).
- ; Called from the main loop, outside the vertical blank.
- ; Every frame one eighth of the people step, so each person steps every 8
- ; frames. If the main loop took longer than a frame (many people), the frames
- ; it missed are caught up here, up to MOVE_CATCH_UP of them, so people keep
- ; their speed while the picture updates less often (M7.3).
- MOVE_CATCH_UP = 4
- move_people:
- lda irq_frames
- sec
- sbc moved_frames ; frames since the last call
- beq @done
- cmp #MOVE_CATCH_UP + 1
- bcc @due
- lda #MOVE_CATCH_UP
- @due: sta move_due
- lda irq_frames
- sta moved_frames
- @frame: inc move_frame
- lda move_frame
- and #7
- @next: cmp people_count
- bcs @last
- sta move_index
- tax
- jsr move_person
- lda move_index
- clc
- adc #8
- bcc @next ; past 255: done
- @last: dec move_due
- bne @frame
- @done: rts
-
- ; 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
- jmp store_step ; paths.inc: also counts a step into a new cell
- @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 pixel at (cand_cx/cand_sx, cand_cy/cand_sy) is water or
- ; stone: a water or stone tile, or a blue %01 water pixel of a bridge tile
- ; (bridges are walkable only on their road pixels). Changes A, X and Y.
- candidate_blocked:
- ldx cand_cy
- ldy cand_cx
- jsr point_world_row
- lda (world_ptr),y
- cmp #STONE
- bcc @open
- cmp #WATER_SE+1
- bcc @blocked
- cmp #BRIDGE_H
- bcc @open
- asl a ; tile * 8 (TILE_COUNT <= 32)
- asl a
- asl a
- ora cand_sy
- tax
- lda tile_patterns,x
- ldy cand_sx
- and pixel_masks,y
- and #$55 ; a set low bit of the pair = %01 = water
- beq @open
- @blocked:
- sec
- rts
- @open: clc
- rts
-
- ; Main loop, every frame, after the rebuild target is chosen (M7.2): people
- ; are drawn only into the hidden buffer, never into the shown one, so the
- ; vertical blank does no people work however many there are. If the hidden
- ; buffer is complete, its old dots are put back and everyone in it is drawn
- ; again (buf_dots marks it). A crossing scroll step waits for buf_dots (see
- ; apply_view). With the view still, no step wanted and the hidden buffer at
- ; the view's own cell, it is flipped in at the next vertical blank.
- people_update:
- ldx hidden
- lda buf_ready,x
- beq @done ; being rebuilt: drawn once it is complete
- stx people_buf
- jsr restore_people
- jsr draw_people
- ldx hidden
- lda #1
- sta buf_dots,x
- jsr get_steps ; this frame's keys, zero at the world edges
- lda view_moved
- ora step_x ; a step is wanted: the hidden buffer must
- ora step_y ; stay free for that crossing's rebuild
- bne @done
- ldx hidden
- lda buf_cx,x
- cmp view_cx
- bne @done
- lda buf_cy,x
- cmp view_cy
- bne @done
- lda #1
- sta people_flip_pending
- @done: rts
-
- ; X = buffer: point lst_low, lst_high and lst_byte at its saved-byte list.
- ; Keeps X and Y.
- point_lists:
- lda #0
- sta lst_low
- sta lst_high
- sta lst_byte
- lda list_page_offset,x
- clc
- adc #>drawn_low_a
- sta lst_low+1
- adc #1 ; carry is clear
- sta lst_high+1
- adc #1
- sta lst_byte+1
- rts
-
- ; Put back the bitmap bytes saved when people were last drawn into buffer
- ; people_buf, newest first, so people that share a byte restore correctly.
- restore_people:
- ldx people_buf
- jsr point_lists
- ldy drawn_count,x
- beq @done
- ldx #0
- @loop: dey
- lda (lst_low),y
- sta bmp
- lda (lst_high),y
- sta bmp+1
- lda (lst_byte),y
- sta (bmp,x) ; X = 0
- tya
- bne @loop
- @done: ldx people_buf
- lda #0
- sta drawn_count,x
- rts
-
- ; Draw every person inside buffer people_buf (relative to its top-left world
- ; cell), saving the byte under each one in that buffer's list. The person
- ; shown in the status lines blinks: it is left out (draw_skip) for 16 of
- ; every 32 frames. Call restore_people first.
- draw_people:
- lda #$FF
- sta draw_skip
- lda move_frame
- and #16
- beq @blink
- lda shown_person
- sta draw_skip
- @blink:
- lda #0
- sta list_pos
- ldx people_buf
- jsr point_lists
- lda buffer_bitmap_offsets,x
- sta bitmap_offset
- lda buf_cx,x
- sta draw_cx
- lda buf_cy,x
- sta draw_cy
- lda buf_rows,x
- sta draw_rows
- ldx #0
- @person:
- cpx draw_skip
- beq @next
- lda people_cy,x
- sec
- sbc draw_cy
- cmp draw_rows ; also rejects people above the buffer
- bcs @next
- tay ; Y = buffer row
- lda people_cx,x
- sec
- sbc draw_cx
- cmp #MAP_COLS_DRAWN
- bcs @next
- sta draw_col
- lda #0 ; bmp = row base + col * 8 + sy
- sta bmp+1
- lda draw_col
- 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 (lst_byte),y
- lda bmp
- sta (lst_low),y
- lda bmp+1
- sta (lst_high),y
- lda (lst_byte),y
- ldy people_sx,x
- ora pixel_masks,y ; pixel %11 = colour RAM = white
- ldy #0
- sta (bmp),y
- inc list_pos
- @next: inx
- cpx people_count
- bcc @person
- lda list_pos
- ldx people_buf
- sta drawn_count,x
- rts
-
- .segment "RODATA"
- pixel_masks: .byte $C0, $30, $0C, $03
- ; Name syllables, 4 bytes each, unused bytes 0. Start syllables 0-15, middle
- ; 16-23 (at most 2 letters, so a name fits in 10), male ends 24-39, female
- ; ends 40-55.
- .macro SYL text
- .byte text
- .res 4 - .strlen(text), 0
- .endmacro
- SYL_MIDDLE = 16
- syllables:
- SYL "AL"
- SYL "BRAN"
- SYL "CAR"
- SYL "DOR"
- SYL "EL"
- SYL "FIN"
- SYL "GAR"
- SYL "HAL"
- SYL "IS"
- SYL "JOR"
- SYL "KEL"
- SYL "LOR"
- SYL "MAR"
- SYL "NOR"
- SYL "OS"
- SYL "TAR"
- SYL "A" ; middle
- SYL "E"
- SYL "I"
- SYL "O"
- SYL "AN"
- SYL "EL"
- SYL "OR"
- SYL "IN"
- SYL "DRIC" ; male ends
- SYL "RIC"
- SYL "DAN"
- SYL "GAR"
- SYL "MOR"
- SYL "TON"
- SYL "BERT"
- SYL "WIN"
- SYL "RUS"
- SYL "VAN"
- SYL "DOR"
- SYL "LAN"
- SYL "ROK"
- SYL "MUND"
- SYL "SON"
- SYL "THAS"
- SYL "A" ; female ends
- SYL "IA"
- SYL "ENA"
- SYL "LYN"
- SYL "WEN"
- SYL "ELLE"
- SYL "ISA"
- SYL "RA"
- SYL "INA"
- SYL "ETH"
- SYL "NIA"
- SYL "ANE"
- SYL "ORA"
- SYL "ESSA"
- SYL "ELL"
- SYL "IE"
- .assert * - syllables = 56 * 4, error, "56 syllables"
- syl_end_base: .byte 24, 40
- list_page_offset: .byte 0, LIST_PAGES
- buffer_bitmap_offsets: .byte 0, >(BITMAP_B - BITMAP_A)
- bitmap_row_low:
- .repeat BUF_ROWS, row
- .byte <(BITMAP_A + 320 * row)
- .endrepeat
- bitmap_row_high:
- .repeat BUF_ROWS, row
- .byte >(BITMAP_A + 320 * row)
- .endrepeat
-
- .segment "BSS"
- people_count: .res 1 ; 1-255 people exist: persons 0 .. people_count - 1
- move_frame: .res 1
- moved_frames: .res 1 ; irq_frames at the last move_people
- move_due: .res 1
- move_index: .res 1
- draw_skip: .res 1 ; person left out of the last draw, $FF = none
- shown_person: .res 1 ; whose name and scores the status lines show
- shown_dirty: .res 1 ; nonzero: the status lines must be rewritten
- attr_sum: .res 1
- name_pos: .res 1
- syl_left: .res 1
- person: .res 1
- tries: .res 1
- cand_cx: .res 1
- cand_sx: .res 1
- cand_cy: .res 1
- cand_sy: .res 1
- drawn_count: .res 2 ; entries in each buffer's saved-byte list
- list_pos: .res 1
- bitmap_offset: .res 1
- people_buf: .res 1 ; buffer that restore/draw_people work on
- draw_cx: .res 1
- draw_cy: .res 1
- draw_rows: .res 1
- draw_col: .res 1
- .assert * <= SCREEN_A, lderror, "People data must fit below VIC bank 1"
|