; Amusement Park 4k.
; (c)2003 Jonathan Cauldwell.
; An entry for the 2003 Minigame Competition, 4k category.

; Project started      18.07.2003 @ 22:00.
; Project completed    22.07.2003 @ 15:00

; Start address to which we're compiling.
; As we're working in 4k let's locate it in a REM statement.

       org 23770

; Draw the park first.

start  ld hl,map-23        ; map address.
       ld de,map-22
       ld bc,782           ; size = 23 x 34 = 782 bytes.
       ld a,b              ; b should be 3 here.
       ld (hl),0           ; start off as grass.
       ldir                ; blank it all out.

; Initialise all variables.

       ld (entfee),a
       ld hl,visits        ; map address.
       ld de,visits+1      ;
       ld bc,year-visits   ; these start at zero.
       ld (hl),b           ; b should be zero here.
       ldir                ; blank it all out.
       ld hl,1000          ; set up the money.
       ld (money),hl
       ld hl,2003          ; set up the year.
       ld (year),hl
       ld hl,rdtext+12
       ld (hl),32          ; change ghost train 2 to ghost train.
       defb 33,6,6         ; statistics for ghost train.
       ld (gtbits),hl      ; start off at 6, 6.

       ld a,1              ; 1 = path/road.
       ld hl,map+21        ; 2 chars from bottom.
       ld bc,8245          ; b=32, c=53.
       ld de,22            ; +1 = next square to the right.
road0  ld (hl),c           ; want a wall here.
       inc hl
       ld (hl),a           ; want a road here.
       add hl,de
       djnz road0

       ld hl,map+365       ; path into park.
       ld (hl),a
       inc hl              ; next square down.
       ld (hl),a
       call 8859           ; set border colour.

; Initialise the ride table.

       ld hl,ridtab
       ld de,ridtab+1
       ld bc,79
       ld (hl),b
       ldir

; Time initialisation.

nyear  ld a,(23672)        ; new time.
       ld (o23672),a       ; store new time.

; Initialise all the little people.

       ld hl,mentab
       ld de,mentab+1
       ld bc,159
       ld (hl),b
       ldir

; Make sure all rides are empty.

       ld hl,ridtab+3
       ld de,5
       ld b,16
init0  ld (hl),d           ; clear ride status.
       add hl,de           ; next ride.
       djnz init0          ; repeat until all done.

; This routine displays the room layout, ie platforms, walls etc.

edit   ld a,79
       ld (23693),a
       xor a
       ld (23658),a        ; set CAPS LOCK off.
       call 3503           ; clear screen.
edit4  call dmoney         ; show money etc. left.
edit1  call droom
       ld hl,(cursx)       ; coords.
       ld (tempx),hl       ; set up block coords.
       ld a,(bsel)         ; selected block.
       and a               ; anything selected?
       jr nz,edit2         ; yes - display this block.
       ld hl,(cursx)
       ld (dispx),hl       ; put in temporary field.
       call atadd          ; find attr. address.
       set 7,(hl)          ; make it flash.
edit3  xor a
       ld (23560),a
edit0  ld hl,o23672
       ld a,(23672)
       sub (hl)
       cp 10
       jp nc,promen        ; process little men.
       ld a,(23560)
       and a               ; anything pressed?
       jr z,edit0          ; no.
       res 7,(hl)          ; switch flashing square off.
       cp 'q'              ; up pressed.
       jp z,cursu
       cp 'a'              ; down key.
       jp z,cursd
       cp 'o'              ; cursor left.
       jp z,cursl
       cp 'p'              ; right key.
       jp z,cursr
       cp 'm'              ; place block key.
       jp z,place
       cp 49               ; number 1.
       jp z,path           ; select blocks.
       cp 50               ; number 2.
       jp z,path           ; select blocks.
       cp 51               ; number 3.
       jp z,selno          ; deselect block.
       cp 52               ; number 4.
       jp z,sel            ; select rides.
       cp 53               ; number 5.
       jp z,scen           ; scenery screen.
       cp 54               ; number 6.
       jp z,bud            ; budget screen.
       jr edit3

; Player has selected a block.  Find out what it is and display it.

edit2  cp 1
       jr z,editb1
       cp 2
       jr z,editb2
       cp 3
       jr z,editb3         ; ghost train.
       cp 4
       jr z,editb4         ; merry-go-round.
       cp 5
       jr z,editb5         ; bouncy castle.
       cp 6
       jr z,editb6         ; maze.
       cp 7
       jr z,editb7         ; the wheel.
       cp 8
       jr z,editb8         ; tree.
       cp 9
       jr z,editb9         ; statue.
edit3a jr edit3
editb1 ld hl,combo0        ; address of block to display.
       jr edtb2a           ; display it.
editb2 ld hl,combo1        ; address of block to display.
edtb2a ld b,1              ; height=1.
       jr edtb8a
editb3 ld hl,combo2        ; address of block to display.
edtb3a call dblok3         ; display it.
       jr edit3a
editb4 ld hl,combo3        ; address of block to display.
       jr edtb3a           ; display it.
editb5 ld hl,combo4        ; address of block to display.
       jr edtb3a           ; display it.
editb6 ld hl,combo5        ; address of block to display.
       jr edtb3a           ; display it.
editb7 ld hl,combo6        ; address of block to display.
       jr edtb3a           ; display it.
editb8 ld hl,combo7        ; address of block to display.
       ld b,3              ; tree height = 3.
edtb8a ld de,1             ; width = 1.
       call dblok          ; display it.
       jr edit3a
editb9 ld hl,combo8        ; address of block to display.
       ld b,2              ; statue height.
       jr edtb8a

cursu  ld hl,cursx         ; coordinate of cursor.
       ld a,(hl)
       and a               ; already zero?
       jr z,cursrs         ; can't go beyond here.
       dec (hl)            ; move cursor.
cursrs jp edit1            ; rejoin main loop.
cursd  ld hl,cursx         ; coordinate of cursor.
       ld a,(hl)
       cp 20               ; already at max?
       jr z,cursrs         ; can't go beyond here.
       inc (hl)            ; move cursor.
       jr cursrs           ; rejoin main loop.
cursl  ld hl,cursy         ; coordinate of cursor.
       ld a,(hl)
       and a               ; already zero?
       jr z,cursrs         ; can't go past here.
       dec (hl)            ; move cursor.
       jr cursrs           ; rejoin main loop.
cursr  ld hl,cursy         ; coordinate of cursor.
       ld a,(hl)
       cp 31               ; already zero?
       jr z,cursrs         ; can't go past here.
       inc (hl)            ; move cursor.
       jr cursrs           ; rejoin main loop.

path   sub 48
       ld (bsel),a
       jp edit4            ; back to editor screen.

sctxt0 defb 22,2,5
       defb 'Tree'
       defb 22,3,5
       defb 'Cost:20'
sctxt1 defb 22,6,5
       defb 'Statue'
       defb 22,7,5
       defb 'Cost:75'
scen   call 3503
       ld a,'A'            ; Only one item to select.
       ld (selt2),a
       ld de,sctxt0
       ld bc,sctxt1-sctxt0
       call 8252

; Display the tree.

       defb 33,2,1         ; coords.
       ld (tempx),hl
       ld hl,combo7        ; address of block to display.
       ld de,1
       ld b,3
       call dblok          ; display it.

       ld a,98             ; default = 1 item.
       ld (ridsel),a

       ld hl,(radexp)      ; total spent on research to date.
       ld de,500
       and a               ; clear carry flag.
       sbc hl,de           ; spent this much on R&D yet?
       jr c,scen1          ; no, skip statue bit.

       ld de,sctxt1
       ld bc,scen-sctxt1
       call 8252

; Display the statue.

       ld a,'B'            ; Two items to select.
       ld (selt2),a
       ld a,99             ; 2 items.
       ld (ridsel),a

       defb 33,6,1         ; coords.
       ld (tempx),hl
       ld hl,combo8        ; address of block to display.
       ld de,1
       ld b,2
       call dblok          ; display it.
scen1  call seldis         ; display 'select' text.
       ld a,(ridsel)       ; Last item we can select.
       ld b,a
scen0  ld a,(23560)
       cp 13               ; pressed ENTER key?
       jp z,selno          ; yes, nothing selected.
       cp 'a'
       jr c,scen0          ; loop round.
       cp b                ; max item to select.
       jr nc,scen0         ; not pressed so loop round.
scena  sub 89              ; a - 89 = item 7.
       jp selsom           ; select item and return to main screen.

sel    call 3503           ; clear screen.

; Display the ghost train.

       defb 33,2,1         ; coords.
       ld (tempx),hl
       ld hl,combo2        ; address of block to display.
       call dblok3         ; display it.

; Show the details.

       ld b,2              ; line 2.
       call detset         ; set x,y coords.
       ld de,rdtext        ; name.
       call disp14         ; display string of 14 chars.
       ld b,3              ; next row.
       ld hl,(rdtext+14)   ; cost.
       ld a,(rdtext+17)    ; capacity.
       call shcost         ; set x,y coords.

; Display the merry-go-round.

       defb 33,6,1         ; coords.
       ld (tempx),hl
       ld hl,combo3        ; address of block to display.
       call dblok3         ; display it.

; Show the details.

       ld b,6              ; line 6.
       call detset         ; set x,y coords.
       ld de,rdtext+18
       call disp14         ; display string of 14 chars.
       ld b,7              ; next row.
       ld hl,(rdtext+32)   ; cost.
       ld a,(rdtext+35)    ; capacity.
       call shcost         ; set x,y coords.

; Display the bouncy castle.

       defb 33,10,1        ; coords.
       ld (tempx),hl
       ld hl,combo4        ; address of block to display.
       call dblok3         ; display it.

; Show the details.

       ld b,10             ; line 6.
       call detset         ; set x,y coords.
       ld de,rdtext+36
       call disp14         ; display string of 14 chars.
       ld b,11             ; next row.
       ld hl,(rdtext+50)   ; cost.
       ld a,(rdtext+53)    ; capacity.
       call shcost         ; set x,y coords.

       ld a,100            ; default = 3 rides.
       ld (ridsel),a

       ld hl,(radexp)      ; total spent on research to date.
       ld de,250
       and a               ; clear carry flag.
       sbc hl,de           ; spent this much on R&D yet?
       jr c,sel1           ; no, skip maze bit.

       ld a,101            ; 4 rides available.
       ld (ridsel),a

; Display the maze.

       defb 33,14,1        ; coords.
       ld (tempx),hl
       ld hl,combo5        ; address of block to display.
       call dblok3         ; display it.

; Show the details.

       ld b,14             ; line 14.
       call detset         ; set x,y coords.
       ld de,rdtext+54
       call disp14         ; display string of 14 chars.
       ld b,15             ; next row.
       ld hl,(rdtext+68)   ; cost.
       ld a,(rdtext+71)    ; capacity.
       call shcost         ; set x,y coords.

       ld hl,(radexp)      ; total spent on research to date.
       ld de,1000
       and a               ; clear carry flag.
       sbc hl,de           ; spent this much on R&D yet?
       jr c,sel1           ; no, skip wheel bit.

       ld a,102            ; 5 rides available.
       ld (ridsel),a

; Display the wheel.

       defb 33,18,1        ; coords.
       ld (tempx),hl
       ld hl,combo6        ; address of block to display.
       call dblok3         ; display it.

; Show the details.

       ld b,18             ; line 18.
       call detset         ; set x,y coords.
       ld de,rdtext+72
       call disp14         ; display string of 14 chars.
       ld b,19             ; next row.
       ld hl,(rdtext+86)   ; cost.
       ld a,(rdtext+89)    ; capacity.
       call shcost         ; set x,y coords.

; General selection text.

sel1   ld a,(ridsel)       ; last letter to select.
       sub 33              ; want CAPITAL letter.
       ld (selt2),a        ; put into text.
       call seldis
       ld a,(ridsel)       ; Last ride we can select.
       ld b,a
       ld hl,23560
       ld (hl),0
sel0   ld a,(hl)
       cp 13               ; Enter?
       jp z,selno          ; Yes - nothing selected.
       cp 'a'
       jr c,sel0
       cp b
       jr nc,sel0
       sub 94              ; block number selected.
       jr selsom

tree   ld a,(cursx)        ; x coord.
       cp 17               ; below lowest point?
       jr nc,tree0         ; yes, can't put ride here.
       call calad          ; get address of top-left point.
       ld a,(hl)           ; what's there?
       and a               ; grass?
       jr nz,tree0         ; no, rejoin loop.
       inc hl              ; middle of tree.
       ld a,(hl)           ; what's there?
       and a               ; is it grass?
       jr nz,tree0         ; no, rejoin loop.
       inc hl              ; trunk.
       ld a,(hl)           ; what's there?
       and a               ; is this grass?
       jr nz,tree0         ; no, rejoin loop.
       ld hl,(money)       ; money in account.
       ld de,20            ; cost per tree.
       sbc hl,de
       jr c,tree0          ; not enough to pay for tree.
       ld (money),hl       ; new amount.
       call inctre         ; increment number of trees/park status.
tree1  jp plac7

selno  xor a               ; set flag to say no block selected.
selsom ld (bsel),a
tree0  jp edit4            ; back to editor loop.

statu  ld a,(cursx)        ; x coord.
       cp 17               ; below lowest point?
       jr nc,tree0         ; yes, can't put ride here.
       call calad          ; get address of top-left point.
       ld a,(hl)           ; what's there?
       and a               ; grass?
       jr nz,tree0         ; no, rejoin loop.
       inc hl              ; base of statue.
       ld a,(hl)           ; what's there?
       and a               ; is it grass?
       jr nz,tree0         ; no, rejoin loop.
       ld hl,(money)       ; money in account.
       ld de,75            ; cost per statue.
       sbc hl,de
       jr c,tree0          ; not enough to pay for statue.
       ld (money),hl       ; new amount.
       ld b,4              ; 4 times the benefit of a single tree.
statu0 call inctre
       djnz statu0
       ld de,combo8        ; statue.
       ld a,2              ; height = 2.
       jp plac7a

; Increment number of trees.

inctre ld hl,trees         ; number of trees.
       inc (hl)            ; increase it.
       ld a,(hl)           ; how many trees?
       and 3               ; every four trees?
       ret nz              ; no, skip this.
       ld hl,rating        ; park rating.
       inc (hl)            ; increment it.
       ret

; Displays 'select' text at top of screen.

seldis ld de,selt0         ; text.
       ld bc,selt1-selt0   ; length.
       jp 8252             ; display string.

place  ld hl,(cursx)       ; cursor position.
       ld (tempx),hl       ; default position for block.
       ld (dispx),hl       ; default position for block.
       ld a,(bsel)         ; block selected.
       and a
       jp z,path2          ; nothing selected - delete what's there.
       cp 1                ; selected path.
       jp z,path1          ; draw path.
       cp 2
       jp z,path1          ; queue area.
       cp 8
       jp z,tree           ; draw tree.
       cp 9
       jp z,statu          ; draw statue.

; We can't have more than 16 rides in the park.

       ld a,(nrides)       ; number of rides.
       cp 15               ; reached maximum?
       ret nc              ; yes, can't have any more.

; First we make sure there's room for this ride.

       ld a,(cursx)        ; x coord.
       cp 17               ; below lowest point?
       jp nc,edit4         ; yes, can't put ride here.
       call calad          ; get address of top-left point.
       ld de,20
       ld c,3              ; 3 columns.
spchk1 ld b,3
spchk0 ld a,(hl)
       cp 3                ; must be grass/path.
       jp nc,edit4         ; something there.
       inc hl
       djnz spchk0         ; continue space check.
       add hl,de           ; next column.
       dec c
       jr nz,spchk1        ; continue until we're in the clear.


; Check that we have enough money for this ride.

       ld a,(bsel)         ; block selected.
       sub 3               ; ride 0-13.
       ld hl,rdtext+14
       call getrid         ; get ride data.

; hl now points to cost of ride.

       ld c,(hl)
       inc hl
       ld b,(hl)           ; bc is cost of ride.
       ld hl,(money)       ; hl is money.
       and a               ; clear carry flag.
       sbc hl,bc           ; subtract cost from money.
       jp c,edit4          ; insufficient funds.

       ld (money),hl       ; store money temporarily.
       ld hl,nrides        ; number of rides.
       inc (hl)            ; increment it.

; Now we want a new entry in the rides table.

       ld ix,ridtab
       ld b,15             ; 16 rides - last one drops through.
       ld de,5             ; entries are 5 bytes long.
cride0 ld a,(ix)           ; is there a ride set up here?
       and a               ; zero means entry is blank.
       jr z,cride          ; create ride.
       add ix,de
       djnz cride0
cride  ld a,(bsel)         ; block selected.
       ld (ix),a
       sub 3
       ld hl,rdtext+16
       call getrid         ; find rating for this ride.
       ld a,(rating)       ; add to park rating.
       add a,(hl)
       ld (rating),a       ; new park rating.
       inc hl              ; next byte is the capacity.
       ld a,(hl)
       ld (ix+4),a         ; individual ride's capacity.
       ld (ix+3),0         ; state = ready.
       ld a,(cursx)
       ld (ix+1),a         ; ride x coord.
       ld a,(cursy)
       ld (ix+2),a         ; ride y coord.

       ld a,(bsel)         ; block selected.
       cp 3
       jp z,plac2
       cp 4
       jp z,plac3
       cp 5
       jp z,plac4
       cp 6
       jp z,plac5
       cp 7
       jp z,plac6
       jr pathr

; Displays a string of 14 characters.

disp14 ld bc,14            ; length.
       jp 8252             ; display name.

; Place a path or queuing area at the cursor position.

path1  call path3          ; make sure we can put a path here.
       call calad          ; address of character at this point.
       ld a,(hl)           ; what's there?
       and a
       jr nz,pathr         ; already something there.
       ld de,(money)       ; money remaining.
       ld a,d
       or e                ; anything left in kitty?
       jr z,pathr          ; no, can't have a path then.
       dec de
       ld (money),de
       ld a,(bsel)         ; selected path.
       ld (hl),a           ; put it down here.
pathr  jp edit4            ; back to main loop.

; Remove a path or queuing area from the cursor position.

path2  call path3          ; make sure we can put a path here.
       call calad          ; address of character at this point.
       ld a,(hl)           ; see what's there.
       cp 3                ; path is 1 or 2, grass is zero.
       jr nc,pathr         ; no path there.
       ld (hl),0           ; put grass there.
       jr pathr
path3  pop hl              ; pop return address off stack.
       ld a,(cursx)        ; x coord of cursor.
       and a               ; is it at the top?
       jr z,pathr          ; yes - can't put path there.
       ld a,(cursy)        ; y coord of cursor.
       and a               ; is it at left edge?
       jr z,pathr          ; yes - can't put path there.
       push hl             ; return address back on stack.
       ret                 ; return to place we were called from.

shcost push af             ; store capacity.
       push hl
       call detset
       ld de,costxt
       ld bc,exctxt-costxt
       call 8252
       pop bc
       call 6683           ; show amount.
       ld a,23             ; show capacity on same line.
       rst 16
       ld a,15
       rst 16
       rst 16
       ld de,captxt        ; capacity text.
       ld bc,ecptxt-captxt
       call 8252
       pop af              ; restore capacity.
       add a,48            ; want ASCII char in range 0-9.
       rst 16
       ret
costxt defb 'Cost:'
exctxt equ $
;exctxt defb 'Excitement:'
captxt defb 'Capacity:'
ecptxt equ $

detset ld a,22             ; AT control code.
       rst 16
       ld a,b              ; row number.
       rst 16
       ld a,5
       rst 16
       ret

; Get ride data.  point hl to part of ride (eg rdtext+14), a=ride number
; and call this routine - hl returns pointing to data for relevant ride.

getrid rlca                ; multiply by 2.
       ld b,a
       rlca
       rlca
       rlca                ; multiply by 16.
       add a,b
       ld e,a
       ld d,0
       add hl,de           ; hl now points to cost of ride.
       ret

; Display status.

dmoney ld a,1              ; 1 = lower screen.
       call 5633           ; open channel.
       ld de,mtxt0
       ld bc,mtxt1-mtxt0
       call 8252
       ld bc,(money)       ; money.
       call 6683           ; show money.
;      call 11563          ; stack bc.
;      call 11747          ; display number on stack.
       ld a,32             ; 1st space.
       rst 16
       ld a,32             ; 2nd space.
       rst 16
       ld a,(month)        ; month number.
       ld e,a
       rlca                ; month * 2.
       add a,e
       ld e,a              ; e = month * 3.
       ld d,0
       ld hl,mtxt1         ; month names.
       add hl,de           ; point to month name.
       ex de,hl
       ld bc,3
       call 8252           ; display month.
       ld a,32             ; 1st space.
       rst 16
       ld bc,(year)
       call 6683
       ld a,23             ; spaces at end.
       rst 16
       ld a,32             ; spaces at end.
       rst 16
       rst 16
       ld a,2              ; 2 = upper screen.
       jp 5633
mtxt0  defb 22,1,0             ; position and colour control chars.
       defb 'Money: '
mtxt1  defb 'JanFebMarAprMayJun'
       defb 'JulAugSepOctNovDec'

plac2  ld de,combo2        ; address of ghost train.
       jr place2
plac3  ld de,combo3        ; merry-go-round.
       jr place2
plac4  ld de,combo4        ; bouncy castle.
       jr place2
plac5  ld de,combo5        ; maze.
       jr place2
plac7  ld de,combo7        ; tree.
       ld a,3              ; height = 3.
plac7a ld b,1
       jr place1
plac6  ld de,combo6        ; big wheel.
place2 ld a,3              ; height = 3.
       ld b,3              ; width = 3.
place1 ex af,af'           ; height.
       push bc             ; width.
       ld a,(tempy)        ; get y coord.
       ld (dispy),a        ; start of left side of block.
place0 push bc             ; width.
       push de
       call calad          ; find address of block at (cursx, cursy).
       ld a,(dispy)        ; current y coord.
       inc a               ; increment it.
       ld (dispy),a        ; store for next iteration.
       pop de
       pop bc              ; width.
       ld a,(de)           ; get byte here.
       ld (hl),a           ; place block.
       inc de              ; next bit of block data.
       djnz place0
       pop bc              ; restore width.
       ld hl,dispx
       inc (hl)            ; down 1 char.
       ex af,af'           ; height.
       dec a
       jr nz,place1
       jp edit1            ; back to loop.

; Calculate address of map character at (x,y).
; y * 23 + x + mapstart.

calad  ld hl,map
       ld a,(dispy)
       rlca
       rlca
       rlca                ; shift left 3 times = multiply by 8.
       ld e,a
       and 3
       ld d,a              ; high byte of x coord.
       ld a,e
       and 248
       ld e,a
       add hl,de
       add hl,de
       add hl,de           ; x*24.
       ld a,(dispy)
       ld e,a
       ld d,0              ; de now = y.
       and a               ; reset carry flag.
       sbc hl,de           ; subtract y, so hl=map+y*23.
       ld a,(dispx)        ; x coord.
       ld e,a
       add hl,de           ; add to beginning of map.
       ret

dblok3 ld de,3
       ld b,e              ; ld b,3.
dblok  ld a,(tempx)
       ld (dispx),a
dblok0 push bc             ; height.
       push de
       push hl             ; address of string.
       ld a,(tempy)
       ld (dispy),a
       ld b,e              ; width.
       call dstrn
       ld hl,dispx         ; x coord.
       inc (hl)            ; down one char.
       pop hl
       pop de              ; width.
       add hl,de
       pop bc              ; height.
       djnz dblok0
       ret                 ; done.

selt0  defb 22,0,0
       defb 'Select item A-'
selt2  defb 'Z, ENTER for None'
selt1  equ $

; display string of b blocks pointed to by hl.

dstrn  push hl
       push bc
       ld a,(hl)
       call dchar
       ld hl,dispy         ; y coord.
       inc (hl)            ; move along.
       ld a,(hl)
       and 31
       jr z,dstrn0         ; hit edge of screen.
       pop bc
       pop hl
       inc hl
       djnz dstrn
       ret
dstrn0 pop bc              ; don't show any more.
       pop hl              ; done with loop.
       ret                 ; return to calling routine.

dchar  push af             ; store byte we're displaying.
       rlca
       rlca
       rlca                ; multiply by 8.
       ld c,a
       and 7
       ld b,a              ; high order byte.
       ld a,c
       and 248
       ld c,a              ; low order byte.
       ld hl,blkgfx        ; address of graphic blocks.
       add hl,bc           ; add displacement.
       call findad

       call tchar          ; transfer char to screen.

       pop af              ; restore block number to display.
       ld hl,blkcol
       ld e,a
       ld d,0
       add hl,de
       ex de,hl
       call atadd          ; find address of attr.
       ld a,(de)           ; colour we're going to put there.
       ld (hl),a
       ret

tchar  ld b,8
draw   ld a,(hl)           ; block graphic data.
       ld (de),a           ; transfer to screen.
       inc hl
       inc d
       djnz draw
       ret

cursx  defb 12             ; cursor position.
cursy  defb 15

; Display map.

droom  xor a
       ld hl,0             ; initial coords to display map - (0,0).
       ld (dispx),hl
       ld hl,map
       ld b,32
droom2 push bc
       ld b,23
droom1 push bc
       push hl
       ld a,(hl)
droom4 push af
       rlca
       rlca
       rlca
       ld c,a
       and 7
       ld b,a
       ld a,c
       and 248
       ld c,a
       ex de,hl
       ld hl,blkgfx
       add hl,bc
       call findad
       ld b,8
droom5 ld a,(hl)           ; block graphic data.
       ld (de),a           ; transfer to screen.
       inc hl
       inc d
       djnz droom5
       pop af
       ld hl,blkcol
       ld e,a
       ld d,0
       add hl,de
       ex de,hl
       call atadd
       ld a,(de)
       ld (hl),a
       ld hl,dispx
       inc (hl)
       pop hl
       inc hl
       pop bc
       djnz droom1
       ld de,(dispx)
       inc d
       ld e,0
       ld (dispx),de
       pop bc
       djnz droom2
       ld b,h
       ld c,l

; This bit makes sure rides flash when they're operating.

rflash ld ix,ridtab        ; table of rides.
       ld b,16
rflas0 push bc
       ld a,(ix)
       and a               ; anything there?
       call nz,rflas1      ; yes - check ride status.
       ld de,5             ; 5 bytes per ride entry.
       add ix,de
       pop bc
       djnz rflas0
       ret
rflas1 ld l,(ix+1)         ; ride's x coord.
       ld h,(ix+2)         ; y coord.
       ld (dispx),hl
       call atadd          ; find address of attributes.
       ld a,(ix+3)         ; ride status.
       add a,64            ; only want 128-192 to flash.
       and 128             ; 128 = busy, also attribute flash.
       ld c,a
       ld a,3
rflas3 ex af,af'
       ld b,3
rflas2 ld a,(hl)
       and 127             ; switch flash off.
       or c                ; switch on if ride is going.
       ld (hl),a           ; put attribute back.
       inc l               ; next attribute position.
       djnz rflas2         ; repeat for all three columns.
       ld de,29            ; next row down.
       add hl,de
       ex af,af'
       dec a
       jr nz,rflas3        ; 3 rows to set.
       ret

; Find address displacement for character block at (dispx, dispy).

findad ld a,(dispx)
       ld e,a
       and 24              ; which segment, 1 to 3?
       add a,64            ; 64*256=16384.
       ld d,a
       ld a,e
       and 7               ; which row within segment?
       rrca                ; multiply row by 32.
       rrca
       rrca
       ld e,a
       ld a,(dispy)        ; add on y coordinate.
       add a,e
       ld e,a              ; displacement to screen address in DE.
       ret

; Calculate address of attribute for character at (dispx, dispy).

atadd  ld a,(dispx)
       rrca
       rrca
       rrca
       ld l,a
       and 3
       add a,88
       ld h,a
       ld a,l
       and 224
       ld l,a
       ld a,(dispy)
       add a,l
       ld l,a              ; hl=address of attributes.
       ret

prorid ld ix,ridtab
       ld b,16
prrid0 push bc
       ld a,(ix)
       and a
       call nz,prrid1      ; ride exists - see what it's up to.
       pop bc
       ld de,5             ; 5 bytes per ride.
       add ix,de           ; next person in list.
       djnz prrid0
       ret

prrid1 call rflas1         ; flashing/not flashing.
       ld a,(ix+3)         ; ride state.
       cp 128              ; in progress?
       jp c,prrid2         ; no.

; Ride is in progress.

       inc a               ; move the ride on a little.
       ld (ix+3),a
       cp 192              ; nearing the end yet?
       ret c               ; no.

; Okay, the ride is done so we need to tip the little people out
; so they can toddle off and find another ride to try.

; First we need to find the exit - look up first of all.

       ld l,(ix+1)         ; ride x coord.
       ld h,(ix+2)         ; ride y coord.
       dec l               ; 1 square up.
       ld (temp),hl        ; default position.
       ld (dispx),hl
       ld bc,770           ; c=2 (direction), b=3 (chars to check).
tip3   call calad          ; address of this block.
       ld a,(hl)
       cp 1                ; is it a path?
       jr z,tip2
       ld hl,dispy
       inc (hl)            ; look right.
       djnz tip3

; That didn't work so look down.

       ld a,(ix+1)         ; ride x coord.
       add a,3             ; 3 squares down.
       ld (dispx),a
       ld a,(ix+2)         ; ride y coord.
       ld (dispy),a
       ld bc,771           ; c=3 (direction), b=3 (chars to check).
tip5   call calad          ; address of this block.
       ld a,(hl)
       cp 1                ; is it a path?
       jr z,tip2
       ld hl,dispy
       inc (hl)            ; look right.
       djnz tip5
       jr tip4

tip2   ld hl,(dispx)       ; coordinates where people can emerge from ride.
       ld (temp),hl

; Now see who's on this ride and kick the little bastards out!

tip4   ld hl,mentab
       ld b,32             ; number of men.
tip1   push bc
       push hl
       ld a,(hl)
       rla                 ; is this man on a ride?
       jr nc,tip0          ; no, skip to next one.
       inc hl
       inc hl
       ld a,(hl)           ; man's x coord.
       cp (ix+1)           ; is he in this ride?
       jr nz,tip0          ; no, skip to next man.
       inc hl
       ld a,(hl)           ; man's y coord.
       cp (ix+2)           ; is he in this ride?
       jr nz,tip0          ; no, skip to next man.

       ld a,(temp+1)       ; ride exit y coord.
       ld (hl),a
       dec hl              ; man x.
       ld a,(temp)         ; ride exit x coord.
       ld (hl),a
       dec hl
       ld (hl),c           ; direction up/down on emerging from ride.
       dec hl
       ld (hl),2           ; wandering around the park.
;      dec (ix+3)          ; decrement number on ride.
       pop hl              ; don't tip 'em all out in one go!
       pop bc
       ret

tip0   pop hl
       ld de,5             ; 5 bytes/entry.
       add hl,de           ; next man in table.
       pop bc
       djnz tip1           ; go round until all men are ejected.
       ret

; This ride isn't busy - see who's queuing for it.

prrid2 ld hl,mentab
       ld b,32             ; number of men.
load1  push bc
       push hl
       ld a,(hl)
       cp 3                ; is this man queuing?
       jr nz,load0         ; no, skip to next one.
       inc hl
       inc hl
       ld a,(hl)           ; man's x coord.
       ld (dispx),a        ; store it so we can delete man later.
       inc a
       sub (ix+1)          ; is he queuing above this ride?
       jr z,load2          ; yes.
       cp 4                ; maybe he's queuing below then?
       jr nz,load0         ; no, skip to next man.
load2  inc hl
       ld a,(hl)           ; man's y coord.
       ld (dispy),a        ; store it for deletion purposes.
       sub (ix+2)          ; ride y coords.
       cp 3                ; is he within 2 chars of this ride?
       jr nc,load0         ; no, skip to next man.

; Okay, this man is about to be loaded onto the ride.
; Need to delete him first, and we stored his
; coords for this purpose in dispx and dispy earlier.

       push hl             ; store address of man.
       call calad          ; what we're standing on.
       ld a,(hl)           ; background graphic char at this point.
       call dchar          ; erase little man.
       pop hl              ; remember man's address.

; Set the coordinates of the man so we know he's on THIS ride!

       ld a,(ix+2)         ; ride y coord.
       ld (hl),a
       dec hl              ; man x.
       ld a,(ix+1)         ; ride x coord.
       ld (hl),a
       dec hl
       dec hl
       ld (hl),128         ; on the ride.

       inc (ix+3)          ; increment number on ride.
       pop hl              ; don't load 'em all in one go!
       pop bc
       ld a,(ix+3)         ; number loaded onto the ride.
       cp (ix+4)           ; ride capacity.
       ret c               ; not reached capacity yet.
       ld (ix+3),128       ; start it off!
       ret

load0  pop hl
       ld de,5             ; 5 bytes/entry.
       add hl,de           ; next man in table.
       pop bc
       djnz load1          ; go round until all men are ejected.
       ret

bankr  call 3503           ; clear screen.
       ld de,bkrtxt        ; bankrupt message.
       ld bc,yrtxt0-bkrtxt
       call 8252           ; show message.
       ld de,yrtxt2        ; enter to continue.
       ld bc,yrtxt3-yrtxt2
       call 8252           ; show message.
       call enterk         ; wait for keypress.
       jp start            ; start another game.

bkrtxt defb 22,11,8
       defb "You're bankrupt!"
yrtxt0 defb 22,2,8
       defb 'End of year'
       defb 22,6,5
       defb 'Number of rides :'
       defb 22,8,5
       defb 'Park rating :'
       defb 22,10,5
       defb 'Visitors so far :'
yrtxt2 defb 22,21,4
       defb 'Press ENTER to continue.'
yrtxt3 defb 22,16,5
       defb 'Bank balance : '
yrtxt1 equ $

; Budget screen routines.

budgl  ld a,(hl)
       and a
       jr z,bud1           ; reached minimum.
       dec (hl)            ; the reduced R&D budget.
       jr bud0
budgm  ld a,(hl)
       cp 99
       jr nc,bud1          ; reached maximum.
       inc (hl)            ; new ticket price.
       jr bud0

bud    call 3503
bud2   ld de,yrtxt2
       ld bc,yrtxt1-yrtxt2
       call 8252
       ld bc,(money)       ; show bank balance.
       call 6683           ; show money.
       ld a,23             ; trailing spaces.
       rst 16
       ld a,31             ; trailing spaces.
       rst 16
       rst 16
;      call 11563          ; stack bc.
;      call 11747          ; display number on stack.
bud0   ld de,bdtxt0
       ld bc,bdtxt1-bdtxt0
       call 8252
       ld a,(entfee)
       call atobc          ; show entry fee.
       ld a,32             ; trailing space.
       rst 16

       ld a,22             ; AT control code.
       rst 16
       ld a,11             ; row 11.
       rst 16
       ld a,24             ; column 23.
       rst 16
       ld a,(loan)
       call atobc          ; show loan amount.
       ld a,(loan)
       and a               ; has any money been borrowed?
       jr z,bud3           ; no.
       ld a,48             ; trailing zero.
       rst 16
       jr bud4
bud3   ld a,23             ; trailing spaces.
       rst 16
       ld a,31             ; trailing spaces.
       rst 16
       rst 16

bud4   ld a,22             ; AT control code.
       rst 16
       ld a,7              ; row 7.
       rst 16
       ld a,26             ; column 26.
       rst 16
       ld a,(rad)
       call atobc          ; show R&D budget.
       ld a,32             ; trailing space.
       rst 16

       ld a,22             ; AT control code.
       rst 16
       ld a,9              ; row 9.
       rst 16
       ld a,26             ; column 26.
       rst 16
       ld a,(adv)
       call atobc          ; show advertising budget.
       ld a,32             ; trailing space.
       ld (23560),a        ; clear last keypress.
       rst 16
bud1   ld a,(23560)
       cp 0
       jr z,bud1
       ld a,(23560)
       cp 'q'
       jr z,budq
       cp 'w'
       jr z,budw
       cp 'a'
       jr z,buda
       cp 's'
       jr z,buds
       cp 'z'
       jr z,budz
       cp 'x'
       jr z,budx
       cp 'o'
       jr z,budo
       cp 'p'
       jr z,budp
       cp 13
       jp z,edit4          ; back to editor loop.
       jr bud1
budq   ld hl,entfee
budlr  jp budgl
budw   ld hl,entfee
budmr  jp budgm
buda   ld hl,rad
       jr budlr
buds   ld hl,rad
       jr budmr
budz   ld hl,adv
       jr budlr
budx   ld hl,adv
       jr budmr
budo   ld a,(loan)         ; amount borrowed / 10.
       and a               ; is it zero?
       jr z,bud1           ; nothing to repay.
       ld hl,(money)       ; money in account.
       ld de,1000          ; loan amount.
       sbc hl,de
       jr c,bud1           ; not enough to repay loan.
       ld (money),hl       ; new money.
       xor a
       ld (loan),a         ; pay back loan.
       jp bud2             ; show new cash total.
budp   ld a,(loan)         ; amount borrowed / 10.
       and a               ; is it zero?
       jr nz,bud1          ; already have a loan.
       ld hl,(money)       ; money in account.
       ld de,1000          ; loan amount.
       add hl,de
       ld (money),hl       ; new money.
       ld a,100
       ld (loan),a         ; take out loan.
       jp bud2             ; display new amount.

bdtxt0 defb 22,2,13
       defb 'Budget'
       defb 22,5,4
       defb '<Q/W> Ticket price'
       defb 22,7,4
       defb '<A/S> R&D budget'
       defb 22,9,4
       defb '<Z/X> Advertising'
       defb 22,11,4
       defb '<O/P> Loan'
       defb 22,5,26
bdtxt1 equ $

; display number in accumulator.

atobc  ld c,a
       ld b,0
       jp 6683

promen ld a,(toggle)       ; toggle legs of men.
       xor 1
       ld (toggle),a       ; put it back.
       ld hl,count
       inc (hl)
       ld a,(hl)
       and 127             ; Move month along every 128.
       jp nz,prmen3
       ld a,(rad)          ; R&D budget for this month.
       ld e,a
       ld d,0
       ld hl,(money)       ; bank balance.
       and a
       sbc hl,de
       jr c,prmen4         ; not enough for R&D.
       ld (money),hl       ; new bank balance.
       ld hl,(radexp)      ; total spent on R&D so far.
       add hl,de           ; increase it.
       ld (radexp),hl
       ld de,750           ; 750 - improve ghost train.
       and a               ; clear carry flag.
       sbc hl,de           ; spent this much on R&D yet?
       call nc,impgt       ; yes, so update ghost train.
prmen4 ld a,(loan)         ; loan amount / 10.
       and a               ; is there an outstanding balance?
       jr z,prmen5         ; no - no loan outstanding.
       ld de,8             ; monthly interest = 8.
       ld hl,(money)       ; bank balance.
       sbc hl,de
       ld (money),hl       ; new balance.
       jp c,bankr          ; not enough for loan repayment.

prmen5 ld a,(adv)          ; advertising budget for the month.
       ld e,a
       ld d,0
       ld hl,(money)       ; bank balance.
       and a
       sbc hl,de
       jr c,prmen7         ; not enough for advertising.
       ld (money),hl       ; new balance.
       jr prmen6           ; keep budget.
prmen7 xor a               ; zeroise advertising budget.
       ld (adv),a
prmen6 ld hl,month         ; month.
       inc (hl)            ; next month in calendar.
       ld a,(hl)
       cp 12               ; reached end of year yet?
       jr c,prmen3         ; no, carry on.

; End of year code.

       ld (hl),0           ; January.
       call 3503           ; clear screen.
       ld de,yrtxt0
       ld bc,yrtxt1-yrtxt0
       call 8252
       ld bc,(money)       ; show bank balance.
       call 6683           ; show money.
;      call 11563          ; stack bc.
;      call 11747          ; display number on stack.
       ld a,22
       rst 16
       ld a,6
       rst 16
       ld a,23
       rst 16
       ld a,(nrides)
       call atobc          ; show number of rides.
       ld a,22
       rst 16
       ld a,8
       rst 16
       ld a,19
       rst 16
       ld a,(rating)
       call atobc          ; show park rating.
       ld a,22
       rst 16
       ld a,10
       rst 16
       ld a,23
       rst 16
       ld bc,(visits)
       call 6683           ; show number of rides.
       ld a,22
       rst 16
       ld a,2
       rst 16
       ld a,20
       rst 16
       ld bc,(year)
       call 6683           ; show year at top.

yrstat call enterk         ; wait for ENTER keypress.

       ld hl,(year)
       inc hl              ; happy new year!
       ld (year),hl
       pop hl              ; pop return address from the stack.
       jp nyear

enterk ld a,(23560)
       cp 13               ; ENTER pressed?
       jr nz,enterk        ; no, wait until it is.
       ret

prmen3 ld a,(23672)        ; new time.
       ld (o23672),a       ; store new time.
       call prorid         ; process rides.
       ld ix,mentab
       ld b,32
prmen0 push bc
       ld a,(ix)
       and a
       jr nz,prmen2        ; he's active - see what he's up to.

       call random
       ld b,a
       ld a,(adv)          ; advertising budget.
       sub b               ; subtract random number.
       jr c,prmen1         ; budget's too low - man unaware of park.

; only want one man per 32 iterations at most.

       call random
       and 31
       call z,iniman       ; start a man off.

prmen1 pop bc
       ld de,5             ; 5 bytes/person.
       add ix,de           ; next person in list.
       djnz prmen0
       call dmoney
       jp edit0            ; back to editor loop.

prmen2 ld a,(ix)           ; man status.
       rla                 ; on a ride?
       call nc,delman      ; no, delete man.
       call proman         ; move man.
       call shoman         ; draw man.
       jp prmen1           ; process other men.

; displays a little man.

shoman ld a,(ix+2)         ; x coord.
       ld (dispx),a
       ld a,(ix+3)         ; man y coord.
       ld (dispy),a
       ld a,(ix)           ; man state.
       and a               ; has he been removed?
       ret z               ; yes, don't show him.
       cp 128              ; on a ride?
       ret nc              ; yes, don't show him then.
       cp 3                ; queuing?
       jr z,shoque         ; yes - special graphic.
       ld a,(ix+1)         ; direction.
       cp 2                ; vertical = 2/3.
       jr nc,shover        ; walking up/down.
       cp 1                ; right = 1.
       jr z,shorgt         ; man walking right.
       ld a,(toggle)       ; man walking left, man 0/1.
shomn0 ld hl,mengfx
       rlca
       rlca
       rlca
       ld e,a
       ld d,0
       add hl,de           ; address of image we want.
       call findad         ; screen address to display man.
       jp tchar            ; transfer man.
shover ld a,(toggle)
       add a,4             ; man 4/5.
       jr shomn0
shorgt ld a,(toggle)
       add a,2             ; man 2/3.
       jr shomn0
shoque ld a,6              ; man 6 -queuing.
       jr shomn0

; deletes a little man.

delman ld l,(ix+2)         ; x coord.
       ld h,(ix+3)         ; man y coord.
       ld (dispx),hl
       call calad          ; what we're standing on.
       ld a,(hl)           ; background graphic char at this point.
       jp dchar            ; erase little man.

proman ld a,(ix)           ; state of man.
       cp 1
       jp z,wk2pk          ; walking to/from park.
       cp 3
       jp z,queu
       cp 128              ; on ride.
       ret nc

; state 2 (in park) now drops through to here.

       ld a,(ix+4)         ; time spent walking round park.
       inc a               ; increment it.
       jr z,inpkc          ; 255 is the max - don't reset to zero.
       ld (ix+4),a         ; increase time spent in park.
       jr inpk

; spent a long time in park - see if it's time to leave.

inpkc  ld a,(ix+2)         ; x coord.
       cp 22               ; out of the park?
       jr nz,inpk          ; no, keep walking.
       ld a,(ix+3)         ; y coord.
       cp 15               ; to the right of the exit?
       jr nc,inpk          ; yes, keep walking.
       ld (ix),1           ; to the left - don't come back.

inpk   ld l,(ix+2)         ; x coord.
       ld h,(ix+3)         ; man y coord.
       ld (dispx),hl
       call calad          ; where we're standing.
       ld de,23            ; rows per column.
       ld a,(ix+1)         ; direction.
       and a               ; left?
       jr z,inpkl
       dec a               ; right?
       jr z,inpkr
       dec a               ; up?
       jr z,inpku
inpkd  inc hl              ; 1 space down.
       ld a,(hl)           ; what's there?
       dec a               ; path will now be 0-1.
       cp 2
       jp nc,newdir        ; not a path - change direction.
       inc (ix+2)          ; move man down.
       jr lklr             ; may change direction to left/right.
inpku  dec hl              ; 1 space up.
       ld a,(hl)           ; what's there?
       dec a               ; path will now be 0-1.
       cp 2
       jr nc,newdir        ; not a path - change direction.
       dec (ix+2)          ; move man up.

; Check left/right directions.

lklr   call random         ; 'random' number.
       rra                 ; 50/50 chance.
       ret c               ; don't look left/right.
       ld l,(ix+2)         ; x coord.
       ld h,(ix+3)         ; y coord.
       ld (dispx),hl       ; general purpose x and y.
       call calad          ; get address at this point.
       ld a,r              ; sort of random number.
       rra                 ; 50/50 chance.
       jr c,lookr          ; look right.
       ld de,65513         ; effectively -23, column left.
       add hl,de           ; new address.
       ld a,(hl)           ; what's there?
       dec a               ; paths are now 0 and 1.
       cp 2                ; is it a path?
       ret nc              ; no.
       ld (ix+1),0         ; yes - change direction to left.
       ret
lookr  ld de,23            ; 23 = column to right.
       add hl,de           ; new address.
       ld a,(hl)           ; what's there?
       dec a               ; paths are now 0 and 1.
       cp 2                ; is it a path?
       ret nc              ; no.
       ld (ix+1),1         ; yes - change direction to right.
       ret

inpkr  add hl,de           ; 1 space right.
       ld a,(hl)           ; what's there?
       dec a               ; path will now be 0-1.
       cp 2
       jr nc,newdir        ; not a path - change direction.
       inc (ix+3)          ; move man right.
       jr lkud             ; look up and down for other paths.
inpkl  and a               ; clear carry.
       sbc hl,de           ; 1 space left.
       ld a,(hl)           ; what's there?
       dec a               ; path will now be 0-1.
       cp 2
       jr nc,newdir        ; not a path - change direction.
       dec (ix+3)          ; move man left.

; Check up/down directions.

lkud   call random         ; crap 'random' number.
       rra                 ; 50/50 chance.
       ret c               ; don't look left/right.
       ld l,(ix+2)         ; x coord.
       ld h,(ix+3)         ; y coord.
       ld (dispx),hl       ; general purpose x and y.
       call calad          ; get address at this point.
       ld a,r              ; sort of random number.
       rra                 ; 50/50 chance.
       jr c,lookd          ; look right.
       dec hl              ; look at row above.
       ld a,(hl)           ; what's there?
       dec a               ; paths are now 0 and 1.
       cp 2                ; is it a path?
       ret nc              ; no.
       ld (ix+1),2         ; yes - change direction to up.
       ret
lookd  inc hl              ; check row below.
       ld a,(hl)           ; what's there?
       dec a               ; paths are now 0 and 1.
       cp 2                ; is it a path?
       ret nc              ; no.
       ld (ix+1),3         ; yes - change direction to down.
       ret

; we've gone to the end of a path and may need to change direction.

newdir call calad          ; where we're currently standing.
       ld a,(hl)           ; what we're standing on.
       cp 2                ; queue area?
       jr nz,newdr0        ; no, just change direction.
       ld (temp),hl        ; store position.

; Let's queue up then.
; Look up to see if we can find a ride.

newdr1 dec hl              ; look up.
       ld a,(hl)           ; what's there?
       cp 2                ; is it a queue area?
       jr z,newdr1         ; let's queue up then.
       jr nc,queup         ; there's a ride here!

; Look down to see if we can find a ride.

       ld hl,(temp)        ; restore position.
newdr2 inc hl              ; look up.
       ld a,(hl)           ; what we're standing on.
       cp 2                ; queue area?
       jr z,newdr2         ; let's queue up then.
       jr nc,queup         ; there's a ride here!

newdr0 call random         ; random direction.
       and 3
       ld (ix+1),a         ; look that way next time.
       ret

queup  ld (ix),3           ; set queue flag.
       ret

; We're queuing up.  Make sure there's still a ride at the end of the path.

queu   ld a,(ix+2)         ; x coord.
       ld (dispx),a
       ld a,(ix+3)         ; man y coord.
       ld (dispy),a
       call calad          ; where we're standing.
       ld (temp),hl        ; store position.

; Look up to see if we can find a ride.

queu0  dec hl              ; look up.
       ld a,(hl)           ; what we're standing on.
       cp 2                ; queue area?
       jr z,queu0          ; let's queue up then.
       ret nc              ; there's a ride here!

; Look down to see if we can find a ride.

       ld hl,(temp)        ; restore position.
queu1  inc hl              ; look down.
       ld a,(hl)           ; what we're standing on.
       cp 2                ; queue area?
       jr z,queu1          ; let's queue up then.
       ret nc              ; there's a ride here!
;      ret
       ld (ix),2           ; don't queue.
       jp inpk             ; walk around a bit.

wk2pk  ld a,(ix+3)         ; y coord.
       and a               ; reached left edge?
       jr z,kilman         ; kill 'im off.
       cp 15               ; reached entrance?
       jr z,entpk          ; yes - decide if we're entering the park.
wk2pk0 dec (ix+3)          ; move left.
       ret

; Do we enter the park or not?

entpk  ld a,(rating)       ; park rating.
       rrca                ; divide by 2.
       and 127
       ld b,a
       call random
       and 3               ; random number 0-3.
       add a,b
       ld b,a              ; decides this person's opinion of park.
       ld a,(entfee)       ; ticket price.
       cp b                ; opinion higher than entry fee?
       jr nc,wk2pk0        ; no, just walk by.

       ld (ix),2           ; state = in park.
       ld (ix+1),2         ; direction = up.
       ld a,(entfee)       ; entry fee.
       ld e,a
       ld d,0
       ld hl,(money)       ; player's money.
       add hl,de           ; ker-ching!
       ld (money),hl
       ld hl,(visits)      ; visitors.
       inc hl
       ld (visits),hl
       ret

; kill the man off.

kilman call delman         ; delete him first.
       ld (ix),0
       ret

; initialise a man walking to the park.
; accumulator is always zero when entering this routine.

iniman ld (ix+1),a         ; direction = left.
       ld (ix+4),a         ; time spent in park.
       ld a,(nrides)       ; number of rides in park.
       and a               ; are there any there yet?
       ret z               ; no, nobody will want to come then.
       ld (ix),1           ; 1 = walking towards park.
       ld (ix+2),22        ; x coord.
       ld (ix+3),31        ; y coord.
       jp shoman           ; display him now.

blkgfx defb 0,170,0,85,0,170,0,85

; white path.
       defb 0,0,0,0,0,0,0,0

; yellow queue area.
       defb 0,0,0,0,0,0,0,0

; ghost train.
       defb 128,171,239,63,57,55,99,111,60,90,155,255,131,215,126,0
       defb 1,213,247,252,140,164,238,254,128,128,128,128,156,190,190,190
       defb 0,110,132,164,164,228,0,0,1,1,1,1,57,125,125,125
       defb 190,190,190,190,128,158,128,255,153,153,255,129,129,255,102,255
       defb 125,125,125,125,1,121,1,255

; merry-go-round.
       defb 255,145,187,255,255,0,16,16,255,17,187,255,255,0,8,8
       defb 255,17,187,255,255,0,4,4,16,22,23,62,124,124,84,84
       defb 8,9,11,63,63,126,74,74,4,134,198,30,30,30,22,22
       defb 16,16,0,255,255,221,136,255,8,8,0,255,255,221,136,255
       defb 4,4,0,255,255,221,137,255

; bouncy castle.
       defb 231,231,231,126,126,126,60,61,0,0,0,0,0,102,102,255
       defb 231,231,231,126,126,126,60,188,36,66,114,114,113,131,135,135
       defb 128,1,8,0,0,66,0,0,36,66,66,66,130,153,189,153
       defb 133,128,240,240,230,70,64,63,126,153,0,0,28,60,24,255
       defb 129,1,13,31,13,2,98,252

; maze.
       defb 0,3,12,48,195,132,137,139,255,0,0,255,68,16,255,0
       defb 0,192,48,12,195,33,95,145,137,137,133,195,176,204,163,132
       defb 0,248,72,24,200,72,104,223,181,159,161,195,13,49,195,9
       defb 144,194,144,196,49,12,3,0,72,122,200,76,72,73,72,255
       defb 65,1,165,3,12,48,192,0

; ferris wheel.
       defb 0,1,6,15,31,31,63,47,62,255,62,62,190,156,190,201
       defb 0,192,48,120,252,252,252,250,33,127,126,127,126,126,33,47
       defb 73,42,28,255,28,42,73,136,66,95,63,255,63,95,66,250
       defb 31,31,15,15,17,32,64,224,201,190,190,62,255,62,8,28
       defb 128,158,191,191,191,191,191,191

; tree.
       defb 60,66,129,195,189,209,169,209,165,209,169,209,165,209,169,82
       defb 60,60,60,60,60,60,60,24

; statue.
       defb 60,36,219,129,66,36,90,153,195,189,129,129,195,189,66,60

; wall.
       defb 223,223,0,253,253,0,223,223

blkcol defb 38             ; grass.
       defb 56             ; white path.
       defb 48             ; queuing area.

; ghost train.
       defb 57,14,57,8,15,8,24,40,24

; merry-go-round.
       defb 48,48,48,50,50,50,48,48,48

; bouncy castle.
       defb 34,34,34,16,16,16,16,16,16

; maze.
       defb 32,32,32,32,32,32,32,32,32

; ferris wheel.
       defb 38,34,33,35,32,39,33,34,40

; tree.
       defb 48,48,34

; statue.
       defb 56,56

; wall.
       defb 114

combo0 defb 1
combo1 defb 2
combo2 defb 3,4,5,6,7,8,9,10,11
combo3 defb 12,13,14,15,16,17,18,19,20
combo4 defb 21,22,23,24,25,26,27,28,29
combo5 defb 30,31,32,33,34,35,36,37,38
combo6 defb 39,40,41,42,43,44,45,46,47
combo7 defb 48,49,50
combo8 defb 51,52

; graphics for the little men.

mengfx defb 24,24,8,28,28,8,22,50
       defb 24,24,8,28,28,8,8,24

       defb 24,24,16,56,56,16,104,76
       defb 24,24,16,56,56,16,16,24

       defb 24,24,60,60,24,24,8,8
       defb 24,24,60,60,24,24,16,16
       defb 24,24,60,60,24,24,24,24

; Data for each of the rides.
; Format of data:
; Name (14), Cost (2), Excitement (1), Capacity (1).

rdtext defb 'Ghost Train   '
       defw 350
gtbits defb 6,6

       defb 'Merry-Go-Round'
       defw 300
       defb 4,5

       defb 'Bouncy Castle '
       defw 250
       defb 5,8

       defb 'Amazing Maze  '
       defw 350
       defb 3,8

       defb 'Ferris Wheel  '
       defw 500
       defb 7,8


impgt  ld hl,rdtext+12
       ld a,(hl)           ; get value there.
       cp '2'              ; has this ride been updated?
       ret z               ; yes, don't do it again.
       ld (hl),'2'         ; rename ghost train "ghost train 2".
       ld hl,gtbits
       inc (hl)            ; increase excitement.
       inc hl              ; capacity.
       inc (hl)
       inc (hl)            ; increase capacity by 2.
       ld hl,ridtab        ; table of rides.
       ld b,16             ; up to 16.
       ld de,5
impgt2 ld a,(hl)           ; what type is it?
       cp 3                ; ghost train?
       jr z,impgt1
       add hl,de
impgt0 djnz impgt2
       ret
impgt1 ld de,4
       add hl,de
       inc (hl)
       inc (hl)            ; increase capacity.
       inc hl
       ld a,(rating)       ; park rating.
       inc a               ; increase for each ride we've improved.
       ld (rating),a       ; improved park rating.
       jr impgt0

; Pseudo-random number generator.
; Steps a pointer through the ROM (held in seed), returning the contents
; of the byte at that location.

random ld hl,(seed)        ; Pointer
       ld a,h
       and 31              ; keep it within first 8k of ROM.
       ld h,a
       ld a,(hl)           ; Get "random" number from location.
       inc hl              ; Increment pointer.
       ld (seed),hl
       ret


entfee equ $               ; entrance fee.
money  equ entfee+1        ; money.
visits equ money+2         ; visitors.
rating equ visits+2        ; park rating.
rad    equ rating+1        ; monthly research and development expenditure.
radexp equ rad+1           ; total spent on R&D to date.

adv    equ radexp+2        ; monthly advertising budget.
ridsel equ adv+1           ; number of rides we can select.
dispx  equ ridsel+1        ; general purpose coordinates.
dispy  equ dispx+1
toggle equ dispy+1
nrides equ toggle+1        ; number of rides.
temp   equ nrides+1
tempx  equ temp+2
tempy  equ tempx+1

o23672 equ tempy+1         ; time we last processed little men.
count  equ o23672+1        ; general counter.
month  equ count+1
loan   equ month+1
bsel   equ loan+1          ; block selected.
trees  equ bsel+1          ; number of trees.
seed   equ trees+1
year   equ seed+2


; Rides table - format:

; byte 1 : ride type, 0=off.
; byte 2 : x coord.
; byte 3 : y coord.
; byte 4 : status, 0=ready, >1 = loading, >128=in progress.
; byte 5 : capacity.

ridtab equ year+2

; Men table - format:

; byte 1 : Current state.
;      0 = off
;      1 = going to park.
;      2 = walking round park.
;      3 = queuing for a ride.
;   >128 = on a ride.

; byte 2 : direction, 0=left, 1=right, 2=up, 3=down.
; byte 3 : x coordinate.
; byte 4 : y coordinate.
; byte 5 : time spent wandering around.

mentab equ ridtab + 80.

mapoff equ mentab+160
map    equ mapoff+23


