; **************************************
; *  T H R I L L E R    -   screen 3   *
; *   "Find your girl in the mansion   *
; *         before they do!"           *
; **************************************
; Designed in 1984 by Dale Lutz, on a
; sheet of paper. Written in 2026 by
; Dale and Claude, in Action!, for a
; 48K Atari 800 and one joystick.
;
; She is hiding somewhere in a dark
; mansion of twelve rooms, and the
; ghouls are in there looking for her.
; Your lamp lights only a little way
; around you. Every few seconds she
; calls for help: the louder the call
; (the VOICE meter shows it), the fewer
; doors there are between you and her.
; The button makes you call to her and
; she answers at once, but the ghouls
; hear it too and come on faster.
; GHOULS shows how many rooms they are
; from her. Get to her first. A ghoul
; in your way will grab you and hold
; you up: keep clear of the eyes.
;
; In this listing {inv}...{/inv} marks
; text typed in inverse video.
;
; HOW THIS PROGRAM WORKS
; The mansion is twelve rooms, four
; across and three down, numbered 0 to
; 11. Two small arrays say which rooms
; have a door in their east wall (de)
; and in their south wall (ds). PROC
; Maze knocks the doors through at
; random. PROC Dist then counts, for
; every room, how many doors it is
; from her room. Her calls, the
; glowing doorway and the ghouls'
; march all go by that count.
; Only the room he is in is drawn.
; PROC BuildRoom draws it, fully lit,
; in an array called room. The screen
; itself starts black inside the
; walls, and PROC Lamp copies from
; room to the screen just the cells
; near him.
; He and she are players (sprites).
; The ghouls are characters, like the
; walls and the furniture.
; Sixty times a second the loop in
; PROC Main moves him, the lamp, the
; lightning, the ghouls in his room,
; her voice, the ghoul pack (which
; goes from room to room toward her on
; a timer) and the music.
; You win the round by touching her.
; You lose a life if the pack reaches
; her room first, or if ghouls grab
; you too many times.

; the other files: SHAPES3 has the
; characters for the room and the
; ghouls, SHAPES has the shapes of him
; and her, MUSIC3 the tune, LAUGH the
; digitized laugh. (HAPPOKEY, the
; piece that greets a win, comes in
; further down.)
INCLUDE "D:SHAPES3.ACT" ; mset
INCLUDE "D:SHAPES.ACT"  ; hero,girl
INCLUDE "D:MUSIC3.ACT"  ; bass,lead,drum
INCLUDE "D:LAUGH.ACT"   ; laugh,LAUGHLEN

; MAXG: the most ghouls there can be
; on the screen. WALL to EYES: the
; numbers of characters in our own
; character set. SCALL to SDOWN:
; numbers for the sound effects (see
; PROC Effect).
DEFINE MAXG="4",     ; ghouls in a room
  WALL="65",BASE="66",FLOOR="67",
  PORT="238",        ; 110, in inverse
  CLOCK="114",
  GLOW="248",        ; 120, in inverse
  EYES="213",        ; 85, in inverse
  SCALL="1",SGRAB="2",SGROAN="3",
  SDOOR="4",STHUN="5",
  SFIND="6",SDOWN="7" ; sound effects

; the bytes the five programs share
; (the title program, the card
; program and the three screens).
; They are in page 6,
; which DOS leaves alone between loads.
; chain: 0 this screen was run by
;   itself, 1 the title program ran it
; wins: screens won so far this game
; result: 1 the screen was won, 0 the
;   game is over
BYTE chain=$6F0,wins=$6F1,lives=$6F2,
  result=$6F5
CARD score=$6F3
; ease: tries lost on this visit to
; the screen. Each one makes the
; next try a little easier.
BYTE ease

; places in the Atari itself: the
; clock that ticks every TV frame, the
; attract-mode timer, the top of
; memory, the START, SELECT and OPTION
; keys, two volume registers, and the
; left-to-right positions of the four
; players and the four missiles
BYTE rtclok=20,atract=77,ramtop=106,
  consol=53279,
  audc1=$D201,audc2=$D203,
  hposp0=$D000,hposp1=$D001,
  hposp2=$D002,hposp3=$D003,
  hposm0=$D004,hposm1=$D005,
  hposm2=$D006,hposm3=$D007
; scrn: the address of screen memory.
; pmb: of player-missile memory. chb:
; of our character set. ptm: frames
; until the ghoul pack moves a room.
; ptime: frames between its moves.
; pace, slack: see PROC NewLevel.
CARD scrn,pmb,chb,
  ptm,ptime, ; the ghouls' next move
  pace,slack

; him, her, and the house
; hp: the grabs he can still take
; before one costs a life (hpm to
; start with: 3 on night 1, 2 on
; night 2, then 1). caught: a grab
; has just used the last of them, and
; cg is the ghoul that did it.
BYTE hp,hpm,caught,cg
; hx,hy: where he is, in player
; coordinates (hx across, hy his top
; TV line). hdir: 0 facing right, 1
; facing left. hstep: counts the
; frames he has walked, to pick his
; pose. stun: frames a ghoul still
; holds him. hfree: frames in which no
; ghoul can grab him.
BYTE hx,hy,hdir,hstep,stun,hfree,
  hcx,hcy,  ; the cell he stands in
  lcx,lcy,  ; where the lamp was drawn
  lx,ly,    ; where she is hiding
  hroom,groom,proom, ; his room, hers,
            ; and the ghouls'
  ; level: the night, from 1. found:
  ; he has reached her. taken: the
  ; ghouls have reached her room. frm:
  ; counts frames.
  level,found,taken,frm,
  gl,     ; the doorway that glows
  bot,    ; bottles he is carrying
  lit,    ; frames of their light left
  bk1,bk2, ; the furniture that hides
          ; a bottle
  ; vlev: how loud her last call was
  ; (0 to 8). vtm: frames to her next
  ; call. vcool: frames until the
  ; button works again. gdel: frames
  ; between a ghoul's steps.
  vlev,vtm,vcool,gdel,
  flash   ; frames of lightning left
CARD ftm  ; frames to the next flash

; the music and the sound effects
; mpos: the step of the tune (0 to
; 127). mtm: frames left in this step.
; tempo: frames in a step. bnote,bvol,
; lnote,lvol, enote,evol: the note and
; volume of the bass, the lead and the
; lead's echo. dtyp,dvol,dpit: the
; drum. sfx: the sound effect playing
; (0 none). sfxt: frames it has left.
BYTE mpos,mtm,tempo,bnote,bvol,
  lnote,lvol,enote,evol,
  dtyp,dvol,dpit,sfx,sfxt

; The mansion: four rooms across and
; three down, numbered 0 to 11.
BYTE ARRAY de(12), ; a door to the east?
  ds(12),          ; to the south?
  ; seen: 1 he has been in this room,
  ; 2 its clock has chimed too (PROC
  ; Maze borrows it while it makes the
  ; doors)
  seen(12),
  dist(12),        ; doors from her room
  rgh(12),         ; ghouls living here
  ; three pieces for each room: ft the
  ; kind (0 table, 1 bookcase, 2
  ; armchair, 3 clock, 255 none),
  ; fx,fy its column and row
  ft(36),fx(36),fy(36), ; the furniture
  ff(36)   ; has he found it yet?

; the ghouls in this room: state (1
; walking, 2 dazed), column and row,
; a timer, which step, which way
BYTE ARRAY gst(MAXG),gx(MAXG),
  gy(MAXG),gtm(MAXG),gfr(MAXG),
  gdir(MAXG)

BYTE ARRAY room(880), ; this room, lit
  herol(156),         ; him, facing left
; how far the lamp reaches to each
; side, on each of its nine rows
  lw=[2 4 5 5 5 5 5 4 2],
  ; fan: not used in this program
  fan=[121 96 81 60 81 60 47 40]

; The piece that greets a win: the
; table happokey and the procedures
; that play it (HpStart, HpTick,
; HpStop). It has procedures in it,
; so it comes in after the
; declarations above.
INCLUDE "D:HAPPOKEY.ACT"

; The display list interrupt. It runs
; under the room and gives the two
; text lines their own colours:
;  PHA; LDA #$34; STA WSYNC; STA COLPF2
;  LDA #$0E; STA COLPF1; PLA; RTI
; (In a text line COLPF2 is the colour
; behind the letters and COLPF1 their
; brightness. Storing to WSYNC waits
; for the end of the TV line, so the
; change does not show part way
; across.)
PROC Dli=*()
[$48 $A9 $34 $8D $0A $D4 $8D $18 $D0
 $A9 $0E $8D $17 $D0 $68 $40]

; wait for the next sixtieth of a second
PROC Frame()
  BYTE f
  f=rtclok
  DO UNTIL rtclok<>f OD
  atract=0 frm==+1
RETURN

; start sound effect t, to last n
; frames. PROC Effect plays it, a
; frame at a time.
PROC Sfx(BYTE t,n)
  sfx=t sfxt=n
RETURN

; mirror n bytes of player shape
; (each byte is a row of eight dots,
; so its bits are put in the opposite
; order)
PROC Flip(BYTE ARRAY src,dst CARD n)
  CARD i
  BYTE b,m,k
  FOR i=0 TO n-1 DO
    b=src(i) m=0
    FOR k=0 TO 7 DO
      m=m LSH 1
      IF (b&1)=1 THEN m==+1 FI
      b=b RSH 1
    OD
    dst(i)=m
  OD
RETURN

; a character row with its four
; two-bit pixels in the other order
BYTE FUNC Rev2(BYTE b)
RETURN (((b&3) LSH 6)%((b&12) LSH 2)
       %((b&48) RSH 2)%(b RSH 6))

; mirror a ghoul (two characters wide,
; three high) into six more characters
; Row r of the ghoul is 16 bytes, two
; characters of 8, which is why
; p=r*16. The left and right
; characters change places as well.
PROC FlipG(BYTE ARRAY src,dst)
  BYTE r,k
  CARD p
  FOR r=0 TO 2 DO
    p=r*16
    FOR k=0 TO 7 DO
      dst(p+k)=Rev2(src(p+8+k))
      dst(p+8+k)=Rev2(src(p+k))
    OD
  OD
RETURN

; done once: memory and shapes
PROC Init()
  ; lower the top of memory by 16
  ; pages (4K) and keep what is above
  ; it for ourselves
  ramtop==-16  ; 2K of players and 1K
  Graphics(0)  ; of characters up top
  ; players at pmb, characters 2K
  ; (2048 bytes) further up
  pmb=ramtop*256 chb=pmb+2048
  Zero(pmb,2048)
  ; Start with a copy of the Atari's
  ; own letters (in ROM at 57344). A
  ; character is 8 bytes, so 520 =
  ; 65*8 and 880 = 110*8: our pictures
  ; go in as characters 65 to 97 and
  ; 110 to 122.
  MoveBlock(chb,57344,1024)
  MoveBlock(chb+520,mset,264)
  MoveBlock(chb+880,mset+264,104)
  ; the ghoul's two steps are
  ; characters 86-91 and 92-97 (688 =
  ; 86*8). Mirror them into 98-103 and
  ; 104-109.
  FlipG(chb+688,chb+784) ; walking left
  FlipG(chb+736,chb+832)
  ; and him: 156 bytes is his two
  ; walking poses, 78 bytes each
  Flip(hero,herol,156)
RETURN

; the game screen, as in screen 2:
; GRAPHICS 0 with its first 22 lines
; changed to ANTIC mode 4
PROC Screen()
  CARD dl
  BYTE i
  Graphics(0)
  ; no cursor, left margin 0
  Poke(752,1) Poke(82,0)
  ; scrn: where screen memory is. dl:
  ; where the display list is, the
  ; table that tells the Atari what
  ; kind of line to draw at each place
  ; down the screen.
  scrn=PeekC(88) dl=PeekC(560)
  ; Byte 3 of the list is the first
  ; text line ($40 = its address
  ; follows, in bytes 4 and 5). Bytes
  ; 6 to 26 are the next 21 lines.
  ; Make all 22 mode 4 (four-colour
  ; characters). $80 on the last one
  ; calls Dli. The two lines after it
  ; stay as text.
  Poke(dl+3,$44)
  FOR i=6 TO 25 DO Poke(dl+i,4) OD
  Poke(dl+26,$84)    ; ...and the DLI
  ; use our character set
  Poke(756,chb RSH 8)
  ; players and missiles: where their
  ; memory is, switch them on
  Poke(54279,ramtop) ; PMBASE
  Poke(53277,3)      ; GRACTL
  Poke(623,49)       ; players in front,
                     ; missiles = 5th,
                     ; P0 over P1 = both
                     ; colours ORed: his
                     ; white socks
  Poke(559,62)       ; one-line players
  ; colours: 704-707 the four players,
  ; 708-711 the playfield, 712 the
  ; background
  Poke(704,$34) ; his red suit
  Poke(705,$28) ; his skin
  Poke(706,$02) ; his hair and shoes
  Poke(707,$86) ; her dress
  Poke(708,$C6) ; ghoul flesh, books
  Poke(709,$06) ; the walls
  Poke(710,$24) ; wood, ghoul rags
  Poke(711,$26) ; her skin;
                ; eyes in the dark
  Poke(712,0)
  ; tell the Atari where Dli is, and
  ; switch display list interrupts on
  PokeC(512,Dli) Poke(54286,192)
RETURN

; ---------- the two text lines ----------

; Show a message at the right of the
; first text line (22 columns, wiped
; first).
PROC Msg(BYTE ARRAY s)
  Position(17,22)
  Print("                      ")
  Position(17,22) Print(s)
RETURN

; the score, on the second text line
PROC ShowScore()
  Position(7,23) PrintC(score)
RETURN

; how loud her last call was
PROC ShowVoice()
  BYTE i
  ; eight cells: a solid block for
  ; each step of loudness, a dot for
  ; the rest
  Position(7,22)
  FOR i=1 TO 8 DO
    IF i<=vlev THEN Put(160)
    ELSE Put('.) FI
  OD
RETURN

; how many rooms the ghouls are from her
PROC ShowPack()
  Position(21,23)
  PrintB(dist(proom)) Put(32)
RETURN

; the bottles he carries (character
; 121 is a bottle)
PROC ShowBot()
  BYTE i
  Position(15,22)
  FOR i=1 TO 2 DO
    IF i<=bot THEN Put(121)
    ELSE Put(32) FI
  OD
RETURN

; his strength, just left of the
; lives (character 122 is a spark)
PROC ShowHp()
  BYTE i
  ; hpm cells, ending just before
  ; column 34: the hp he has left are
  ; the sparks at the right
  Position(34-hpm,23)
  FOR i=1 TO hpm DO
    IF i>hpm-hp THEN Put(122)
    ELSE Put(32) FI
  OD
RETURN

; Both text lines, drawn afresh at the
; start of a round: the labels, the
; night, a heart for each life
; (character 0), and all the meters.
PROC ShowAll()
  BYTE i
  Position(1,22) Print("VOICE")
  Position(1,23) Print("SCORE")
  Position(14,23) Print("GHOULS")
  Position(24,23) Print("NIGHT ")
  PrintB(level) Print("   ")
  Position(34,23) Print("     ")
  Position(34,23)
  FOR i=1 TO lives DO Put(0) OD
  ShowScore() ShowVoice() ShowPack()
  ShowBot() ShowHp()
RETURN

; -------------- the mansion --------------

; is there a room that way at all?
; (0 north, 1 east, 2 south, 3 west)
; r&3 is the room's column (0 to 3).
; Rooms 0-3 are the top row, 8-11 the
; bottom.
BYTE FUNC Valid(BYTE r,d)
  IF d=0 AND r>3 THEN RETURN (1) FI
  IF d=1 AND (r&3)<3 THEN RETURN (1) FI
  IF d=2 AND r<8 THEN RETURN (1) FI
  IF d=3 AND (r&3)>0 THEN RETURN (1) FI
RETURN (0)

; the number of the room next door in
; direction d: 4 less north, 1 more
; east, 4 more south, 1 less west
BYTE FUNC Nbr(BYTE r,d)
  IF d=0 THEN RETURN (r-4) FI
  IF d=1 THEN RETURN (r+1) FI
  IF d=2 THEN RETURN (r+4) FI
RETURN (r-1)

; Is there a door in wall d of room r?
; Only east and south doors are
; stored, so a door north is the south
; door of the room above (r-4), and a
; door west is the east door of the
; room to the left (r-1).
BYTE FUNC HasDoor(BYTE r,d)
  IF Valid(r,d)=0 THEN RETURN (0) FI
  IF d=0 THEN RETURN (ds(r-4)) FI
  IF d=1 THEN RETURN (de(r)) FI
  IF d=2 THEN RETURN (ds(r)) FI
RETURN (de(r-1))

; A new set of doors. Wander from room
; to room at random, knocking a door
; through whenever the next room has
; not been visited, until all have:
; then every room can be reached.
PROC Maze()
  BYTE cur,nb,d,n,i
  ; no doors, no room visited
  FOR i=0 TO 11 DO
    de(i)=0 ds(i)=0 seen(i)=0
  OD
  ; start anywhere. n counts the rooms
  ; visited.
  cur=Rand(12) seen(cur)=1 n=1
  WHILE n<12 DO
    ; pick a direction. If there is a
    ; room that way, go there, and if
    ; it is new, open the door between
    ; the two
    d=Rand(4)
    IF Valid(cur,d)=1 THEN
      nb=Nbr(cur,d)
      IF seen(nb)=0 THEN
        seen(nb)=1 n==+1
        IF d=0 THEN ds(nb)=1
        ELSEIF d=1 THEN de(cur)=1
        ELSEIF d=2 THEN ds(cur)=1
        ELSE de(nb)=1 FI
      FI
      cur=nb
    FI
  OD
  ; up to three more east doors and
  ; three more south doors, so there
  ; is often more than one way round
  FOR i=1 TO 3 DO    ; and a few more
    cur=Rand(12)
    IF (cur&3)<3 THEN de(cur)=1 FI
    ds(Rand(8))=1
  OD
RETURN

; how many doors is each room from hers?
; (Called once a round, after Maze and
; after her room is chosen.)
PROC Dist()
  BYTE r,d,k,nb
  ; 255 = not known yet. Her own room
  ; is 0.
  FOR r=0 TO 11 DO dist(r)=255 OD
  dist(groom)=0
  ; Twelve passes over every room and
  ; every door. A room with a door to
  ; a nearer room becomes that room's
  ; count plus 1. (The first test
  ; stops a room that is still at 255
  ; being used: 255+1 would not fit in
  ; a byte.)
  FOR k=1 TO 12 DO
    FOR r=0 TO 11 DO
      FOR d=0 TO 3 DO
        IF HasDoor(r,d)=1 THEN
          nb=Nbr(r,d)
          IF dist(nb)<dist(r)
             AND dist(nb)+1<dist(r)
          THEN dist(r)=dist(nb)+1 FI
        FI
      OD
    OD
  OD
RETURN

; Three pieces of furniture a room,
; each in its own corner, clear of the
; doors and of the way between them.
; Also says how many ghouls live in
; each room, and hides the two
; bottles. Called once a round.
PROC Furnish()
  BYTE r,i,q,k
  FOR r=0 TO 11 DO
    ; q: the corner for the first
    ; piece; the next two pieces take
    ; the next two corners
    q=Rand(4)
    FOR i=0 TO 2 DO
      k=r*3+i
      ; k: this piece's place in the
      ; lists. A kind at random, not
      ; yet found.
      ft(k)=Rand(4) ff(k)=0
      ; bit 0 of q: left or right
      ; side. Bit 1: top or bottom.
      IF (q&1)=0 THEN fx(k)=3+Rand(9)
      ELSE fx(k)=25+Rand(8) FI
      IF (q&2)=0 THEN fy(k)=4+Rand(3)
      ELSE fy(k)=15+Rand(3) FI
      q=(q+1)&3
    OD
    ; ghouls living here: 0 or 1, one
    ; more after night 4, another
    ; after night 7
    rgh(r)=Rand(2)
    IF level>4 THEN rgh(r)==+1 FI
    IF level>7 THEN rgh(r)==+1 FI
  OD
  ; (the two pieces that hide a bottle
  ; are picked in Round, once it is
  ; known which pieces stay)
RETURN

; which doorway leads toward her?
; Sets gl to its direction (0 to 3),
; or 255 if no door leads nearer.
PROC Way()
  BYTE i
  gl=255
  FOR i=0 TO 3 DO
    IF HasDoor(hroom,i)=1 THEN
      IF dist(Nbr(hroom,i))<dist(hroom)
      THEN gl=i FI
    FI
  OD
RETURN

; make doorway gl glow
; (in the array room; GLOW is a bright
; doorway character)
PROC GlowDoor()
  BYTE y
  CARD p
  ; a west or east doorway is rows 10
  ; to 13 of column 0 or column 39
  FOR y=10 TO 13 DO
    p=y*40
    IF gl=3 THEN room(p)=GLOW FI
    IF gl=1 THEN room(p+39)=GLOW FI
  OD
  ; a north doorway is columns 18 to
  ; 21 of the top three rows
  IF gl=0 THEN SetBlock(room+18,4,GLOW)
    SetBlock(room+58,4,GLOW)
    SetBlock(room+98,4,GLOW)
  FI
  ; a south doorway is the same
  ; columns of row 21 (858 = 21*40+18)
  IF gl=2 THEN
    SetBlock(room+858,4,GLOW) FI
RETURN

; draw room hroom into the array room,
; as it would look with the lights on
; room is 22 rows of 40 cells, one
; byte each, so cell x,y is
; room(y*40+x).
PROC BuildRoom()
  BYTE y,i,k,t
  CARD p
  ; rows 0 and 1 wall, row 2 the foot
  ; of the wall, row 21 the bottom
  ; wall
  Zero(room,880)
  SetBlock(room,80,WALL)
  SetBlock(room+80,40,BASE)
  SetBlock(room+840,40,WALL)
  ; rows 3 to 20: a wall cell at each
  ; side, floor between
  FOR y=3 TO 20 DO
    p=y*40
    room(p)=WALL room(p+39)=WALL
    SetBlock(room+p+1,38,FLOOR)
  OD
  ; two portraits on the wall
  FOR i=0 TO 1 DO
    p=6+i*26
    room(p)=PORT room(p+1)=PORT+1
    room(p+40)=PORT+2
    room(p+41)=PORT+3
  OD
  ; cut the doorways: 0 is an empty
  ; cell
  IF HasDoor(hroom,0)=1 THEN
    Zero(room+18,4) Zero(room+58,4)
    Zero(room+98,4)
  FI
  IF HasDoor(hroom,2)=1 THEN
    Zero(room+858,4) FI
  FOR y=10 TO 13 DO
    p=y*40
    IF HasDoor(hroom,3)=1 THEN
      room(p)=0 FI
    IF HasDoor(hroom,1)=1 THEN
      room(p+39)=0 FI
  OD
  ; the doorway toward her, if it is
  ; to glow
  GlowDoor()
  ; this room's three pieces of
  ; furniture. p is the address of the
  ; piece's top left cell; +1 is the
  ; cell to its right, +40 the cell
  ; below.
  FOR i=0 TO 2 DO
    k=hroom*3+i t=ft(k)
    p=room+fy(k)*40+fx(k)
    IF t=0 THEN        ; a table
      Poke(p,69) Poke(p+1,70)
      Poke(p+2,71) Poke(p+40,72)
      Poke(p+41,73) Poke(p+42,74)
    ELSEIF t=1 THEN    ; a bookcase
      Poke(p,75) Poke(p+1,76)
      Poke(p+40,77) Poke(p+41,78)
      Poke(p+80,79) Poke(p+81,80)
    ELSEIF t=2 THEN    ; an armchair
      Poke(p,81) Poke(p+1,82)
      Poke(p+40,83) Poke(p+41,84)
    ELSEIF t=3 THEN    ; a clock
      Poke(p,CLOCK) Poke(p+1,CLOCK+1)
      Poke(p+40,CLOCK+2)
      Poke(p+41,CLOCK+3)
      Poke(p+80,CLOCK+4)
      Poke(p+81,CLOCK+5)
    FI
  OD
RETURN

; Is character c something he and the
; ghouls cannot walk through? Walls
; and furniture are (characters 65 to
; 84, all but the floor), and so are
; the clocks. Empty doorways are not.
BYTE FUNC Solid(BYTE c)
  IF c>=WALL AND c<=84 AND c<>FLOOR
  THEN RETURN (1) FI
  IF c>=CLOCK AND c<=CLOCK+5 THEN
    RETURN (1) FI
RETURN (0)

; what is on the floor under his feet,
; if he stood at x,y?
; x,y are player coordinates. A cell
; is 4 across and 8 down, and the
; room's top left corner is at 48,32.
; Taking off 44 and 9, not 48 and 32,
; gives the cell 4 to the right of x
; and 23 lines below y: the middle of
; his feet, as he is 8 wide and 26
; tall.
BYTE FUNC At(BYTE x,y)
  CARD p
  p=((y-9) RSH 3)*40+((x-44) RSH 2)
RETURN (room(p))

; --------------- the lamp ---------------
;
; lcx,lcy: the cell at the middle of
; the lamplight, which reaches four
; rows up and four down. lcx=255 means
; no lamplight is drawn.

; is the cell in column x, row y
; inside the lamplight?
BYTE FUNC InLight(BYTE x,y)
  BYTE d,w
  ; a bottle lights the whole room
  IF lit>0 THEN RETURN (1) FI
  ; d: which of the lamp's nine rows
  ; this is (0 to 8). For a row above
  ; the light the byte goes below 0
  ; and wraps round to a big number,
  ; so one test does for both.
  d=y+4-lcy
  IF d>8 THEN RETURN (0) FI
  w=lw(d)
  ; within w columns of the middle?
  IF x+w>=lcx AND lcx+w>=x THEN
    RETURN (1) FI
RETURN (0)

; put out the light where the lamp was
; (y is the row plus 4 here, so 7 to
; 24 means rows 3 to 20, the ones
; inside the walls. Columns 1 to 38 of
; each row are blanked.)
PROC Dark()
  BYTE d,y
  FOR d=0 TO 8 DO
    y=lcy+d
    IF y>=7 AND y<=24 THEN
      y==-4 Zero(scrn+y*40+1,38)
    FI
  OD
RETURN

; show the room where the lamp is now
; (by copying the lit cells from the
; array room to the screen)
PROC Light()
  BYTE d,y,w,x0,x1
  CARD p
  FOR d=0 TO 8 DO
    y=lcy+d
    IF y>=7 AND y<=24 THEN
      y==-4 w=lw(d)
      ; x0 to x1: the lit columns of
      ; this row, kept inside the
      ; walls
      IF lcx>w THEN x0=lcx-w
      ELSE x0=1 FI
      x1=lcx+w
      IF x1>38 THEN x1=38 FI
      IF x1>=x0 THEN
        p=y*40+x0
        MoveBlock(scrn+p,room+p,x1-x0+1)
      FI
    FI
  OD
RETURN

; Draw ghoul i: whatever part of it is
; in the lamplight. If its head is in
; the dark, all he sees is its eyes.
PROC DrawG(BYTE i)
  BYTE c,r,k,x,y
  ; c: its first character. 86 if it
  ; faces right, 98 if left, and 6
  ; more for its other step.
  IF gdir(i)=0 THEN c=86
  ELSE c=98 FI
  c==+gfr(i)*6
  x=gx(i) y=gy(i)
  ; neither of its two top cells is
  ; lit: draw the eyes
  IF InLight(x,y)=0
     AND InLight(x+1,y)=0
  THEN Poke(scrn+y*40+x,EYES) FI
  ; its six cells, the lit ones only
  FOR r=0 TO 2 DO
    FOR k=0 TO 1 DO
      IF InLight(x+k,y+r)=1 THEN
        Poke(scrn+(y+r)*40+x+k,c)
      FI
      c==+1
    OD
  OD
RETURN

; put back what was under ghoul i
PROC EraseG(BYTE i)
  BYTE r,k
  CARD p
  FOR r=0 TO 2 DO
    FOR k=0 TO 1 DO
      p=(gy(i)+r)*40+gx(i)+k
      IF InLight(gx(i)+k,gy(i)+r)=1
      THEN Poke(scrn+p,room(p))
      ELSE Poke(scrn+p,0) FI
    OD
  OD
RETURN

; she can only be seen in the lamplight
; She is player 3 and the four
; missiles, side by side. Position 0
; is off the screen, which hides her.
PROC ShowGirl()
  BYTE x
  x=0
  IF hroom=groom THEN
    ; her cell, worked out as his is
    ; in PROC Lamp
    IF InLight((lx-44) RSH 2,
               (ly-19) RSH 3)=1
    THEN x=lx FI
  FI
  hposp3=x hposm3=x hposm2=x+2
  hposm1=x+4 hposm0=x+6
RETURN

; move the lamplight when he has moved
; to another cell
; Called every frame. Also where
; points are scored for finding
; furniture.
PROC Lamp()
  BYTE i,k
  ; the cell at his middle: 4 across
  ; and 13 lines down from hx,hy (the
  ; room starts at 48,32)
  hcx=(hx-44) RSH 2 hcy=(hy-19) RSH 3
  ; lightning or a bottle is lighting
  ; the whole room: leave it
  IF flash>0 OR lit>0 THEN RETURN FI
  ; still in the same cell: no change
  IF hcx=lcx AND hcy=lcy THEN RETURN FI
  IF lcx<255 THEN Dark() FI
  lcx=hcx lcy=hcy
  Light()
  ; the ghouls, as the light now falls
  ; on them
  FOR i=0 TO MAXG-1 DO
    IF gst(i)>0 THEN DrawG(i) FI
  OD
  ShowGirl()
  ; the lamp falls on a piece of
  ; furniture for the first time:
  ; a chime, and 25 points
  FOR i=0 TO 2 DO
    k=hroom*3+i
    IF ff(k)=0 AND
       InLight(fx(k)+1,fy(k)+1)=1
    THEN
      ff(k)=1 score==+25 ShowScore()
      Sfx(SFIND,12)
      ; a bottle was hidden here? He
      ; can carry two.
      IF k=bk1 OR k=bk2 THEN
        IF bot<2 THEN bot==+1 FI
        ShowBot() Sfx(SFIND,24)
        Msg("LIGHTNING IN A BOTTLE!")
      ELSEIF ft(k)=0 THEN
        Msg("A TABLE! +25")
      ELSEIF ft(k)=1 THEN
        Msg("OLD BOOKS! +25")
      ELSEIF ft(k)=2 THEN
        Msg("AN ARMCHAIR! +25")
      ELSE
        Msg("A CLOCK! TICK... +25")
      FI
    FI
  OD
RETURN

; Now and then lightning shows the
; whole room for a moment: her, if she
; is here, and where the ghouls stand.
PROC Storm()
  BYTE y,i
  ; not while a bottle is burning
  IF lit>0 THEN RETURN FI
  ; a flash is on: count it down. The
  ; background is white for its first
  ; three frames; after nine the room
  ; goes dark and lcx=255 makes Lamp
  ; draw the lamplight again.
  IF flash>0 THEN
    flash==-1
    IF flash=6 THEN Poke(712,0) FI
    IF flash=0 THEN  ; dark again
      FOR y=3 TO 20 DO
        Zero(scrn+y*40+1,38)
      OD
      lcx=255
    FI
    RETURN
  FI
  ; no flash: count down to the next
  ftm==-1
  IF ftm>0 THEN RETURN FI
  ; Flash! The next will come in 4 to
  ; 8 seconds. Background white. Rows
  ; 3 to 20 of the room (720 cells,
  ; starting at cell 120) are all
  ; shown.
  ftm=250+Rand(250)
  flash=9 Poke(712,$0E)
  MoveBlock(scrn+120,room+120,720)
  ; a pair of eyes where each ghoul's
  ; head is
  FOR i=0 TO MAXG-1 DO
    IF gst(i)>0 THEN
      Poke(scrn+gy(i)*40+gx(i),EYES)
    FI
  OD
  ; and her, if this is her room
  IF hroom=groom THEN
    hposp3=lx hposm3=lx hposm2=lx+2
    hposm1=lx+4 hposm0=lx+6
  FI
  Sfx(STHUN,40)
  Msg("LIGHTNING!")
RETURN

; --------------- the ghouls ---------------

; could ghoul i stand at nx,ny?
BYTE FUNC Free(BYTE i,nx,ny)
  BYTE j
  CARD p
  ; inside the walls?
  IF nx<1 OR nx>37 OR ny<3 OR ny>18
  THEN RETURN (0) FI
  ; the two cells under its bottom row
  ; must not be solid
  p=(ny+2)*40+nx     ; its feet
  IF Solid(room(p))=1
     OR Solid(room(p+1))=1
  THEN RETURN (0) FI
  ; and it must not overlap another
  ; ghoul (each is 2 cells wide and 3
  ; high)
  FOR j=0 TO MAXG-1 DO
    IF j<>i AND gst(j)>0 THEN
      IF nx+2>gx(j) AND gx(j)+2>nx
         AND ny+3>gy(j) AND gy(j)+3>ny
      THEN RETURN (0) FI
    FI
  OD
RETURN (1)

; is x,y a fair way from him? A ghoul
; never rises within reach.
BYTE FUNC Far(BYTE x,y)
  BYTE ty,dx,dy
  ; ty: the row of his head. dx,dy:
  ; columns and rows between x,y and
  ; him.
  ty=(hy-32) RSH 3
  IF x<hcx THEN dx=hcx-x ELSE dx=x-hcx FI
  IF y<ty THEN dy=ty-y ELSE dy=y-ty FI
  IF dx>8 OR dy>6 THEN RETURN (1) FI
RETURN (0)

; n more ghouls, on the far side of
; the room from him. Called when he
; comes into a room, and when the pack
; arrives in the room he is in.
PROC SpawnG(BYTE n)
  BYTE k,i,t,x,y
  FOR k=1 TO n DO
    ; find a ghoul that is not in use
    ; (state 0); if all four are, give
    ; up
    i=0
    WHILE i<MAXG AND gst(i)>0 DO
      i==+1 OD
    IF i=MAXG THEN RETURN FI
    ; up to eight tries at a place in
    ; the half of the room he is not
    ; in
    FOR t=1 TO 8 DO
      IF hcx<20 THEN x=22+Rand(14)
      ELSE x=3+Rand(14) FI
      y=4+Rand(13)
      IF Free(i,x,y)=1 AND Far(x,y)=1
      THEN
        ; a walking ghoul, facing him,
        ; that waits half a second or
        ; more before it moves
        gx(i)=x gy(i)=y gfr(i)=0
        gst(i)=1 gtm(i)=30+Rand(30)
        IF x>hcx THEN gdir(i)=1
        ELSE gdir(i)=0 FI
        DrawG(i)
        EXIT
      FI
    OD
  OD
RETURN

; one shambling step toward him
PROC StepG(BYTE i)
  BYTE x,y,nx,ny,tx,ty,dx,dy,j
  x=gx(i) y=gy(i) nx=x ny=y
  ; tx,ty: where it is heading, the
  ; cell of his head (a column to the
  ; left, as a ghoul is two wide).
  ; dx,dy: how far it is from there.
  IF hcx>0 THEN tx=hcx-1 ELSE tx=0 FI
  ty=(hy-32) RSH 3
  IF x<tx THEN dx=tx-x ELSE dx=x-tx FI
  IF y<ty THEN dy=ty-y ELSE dy=y-ty FI
  ; one time in six, a step in any
  ; direction
  IF Rand(6)=0 THEN  ; a lurch off course
    j=Rand(4)
    IF j=0 THEN nx=x+1
    ELSEIF j=1 THEN nx=x-1
    ELSEIF j=2 THEN ny=y+1
    ELSE ny=y-1 FI
  ; otherwise a step along whichever
  ; is further, across or up and down
  ELSEIF dx>dy THEN
    IF x<tx THEN nx=x+1 ELSE nx=x-1 FI
  ELSEIF dy>0 THEN
    IF y<ty THEN ny=y+1 ELSE ny=y-1 FI
  FI
  ; blocked? Then a step the other way
  ; of the two, if that gets it
  ; nearer.
  IF Free(i,nx,ny)=0 THEN
    nx=x ny=y        ; try the other way
    IF dx>dy THEN
      IF y<ty THEN ny=y+1
      ELSEIF y>ty THEN ny=y-1 FI
    ELSE
      IF x<tx THEN nx=x+1
      ELSEIF x>tx THEN nx=x-1 FI
    FI
  FI
  ; face the way it is going
  IF nx>x THEN gdir(i)=0
  ELSEIF nx<x THEN gdir(i)=1 FI
  ; rub it out, move it if it can go
  ; there, change to its other step,
  ; draw it again
  EraseG(i)
  IF Free(i,nx,ny)=1 THEN
    gx(i)=nx gy(i)=ny
  FI
  gfr(i)==!1
  DrawG(i)
  ; frames until its next step
  gtm(i)=gdel+Rand(6)
RETURN

; A ghoul has grabbed him, and he has
; a bottle: it bursts. For four
; seconds the whole room is lit and
; every ghoul in it is dazed.
PROC Burst()
  BYTE i
  ; 240 frames is four seconds.
  ; Background white, rows 3 to 20 of
  ; the room all shown.
  bot==-1 ShowBot()
  lit=240 flash=0 Poke(712,$0E)
  MoveBlock(scrn+120,room+120,720)
  ; state 2 is dazed, for 255 frames
  FOR i=0 TO MAXG-1 DO
    IF gst(i)>0 THEN
      gst(i)=2 gtm(i)=255 DrawG(i)
    FI
  OD
  ShowGirl()
  Sfx(STHUN,40)
  Msg("THE BOTTLE BURSTS!")
RETURN

; the bottle's light runs down: it
; flickers for the last second, then
; the room is dark again
PROC Glare()
  BYTE y
  IF lit=0 THEN RETURN FI
  lit==-1
  ; white for the first four frames
  ; only
  IF lit=236 THEN Poke(712,0) FI
  ; under 48 frames left: the
  ; background flickers
  IF lit<48 THEN
    IF (lit&8)=8 THEN Poke(712,2)
    ELSE Poke(712,0) FI
  FI
  ; out: blank the room. lcx=255 makes
  ; Lamp draw the lamplight again.
  IF lit=0 THEN
    FOR y=3 TO 20 DO
      Zero(scrn+y*40+1,38)
    OD
    lcx=255 ShowGirl()
  FI
RETURN

; every ghoul, every frame
; (moves them, and sees whether one is
; touching him)
PROC Ghouls()
  BYTE i,px,py
  FOR i=0 TO MAXG-1 DO
    IF gst(i)>0 THEN
      ; its timer runs down. At 0 a
      ; dazed ghoul wakes, and a
      ; walking one takes a step.
      IF gtm(i)>0 THEN gtm(i)==-1
      ELSEIF gst(i)=2 THEN
        gst(i)=1 gtm(i)=gdel
      ELSE StepG(i) FI
      ; px,py: its top left corner in
      ; player coordinates (a cell is
      ; 4 by 8, the room starts at
      ; 48,32). Is it touching him?
      ; Not counted while hfree is
      ; running.
      px=48+gx(i)*4 py=32+gy(i)*8
      IF hfree=0
         AND hx+6>px AND px+6>hx
         AND hy+22>py AND py+22>hy
      THEN
        ; a walking ghoul (state 1)
        IF gst(i)=1 THEN
          IF bot>0 THEN Burst()
          ELSE       ; it has hold of him
            ; he is held for 45
            ; frames, and cannot be
            ; grabbed for 110. The
            ; ghoul is dazed for 90.
            ; He has one grab fewer
            ; left; at none, PROC
            ; Caught takes a life.
            stun=45 hfree=110
            gst(i)=2 gtm(i)=90
            Sfx(SGRAB,10)
            hp==-1 ShowHp()
            IF hp=0 THEN caught=1 cg=i
            ELSE
              Msg("A GHOUL GRABS YOU!")
            FI
          FI
        ELSEIF lit>0 THEN
          ; dazed, in the bottle's
          ; light: touch it and it goes
          ; back underground for good
          ; 50 points for it, and one
          ; ghoul fewer lives in this
          ; room
          EraseG(i) gst(i)=0
          IF rgh(hroom)>0 THEN
            rgh(hroom)==-1 FI
          score==+50 ShowScore()
          Sfx(SDOWN,20)
          Msg("BACK UNDERGROUND! +50")
        FI
      FI
    FI
  OD
RETURN

; ------------------ him ------------------

; he has just come into room hroom
; Draws the room dark, and puts the
; ghouls and (if it is her room) her
; into it.
PROC Enter()
  BYTE y,i
  CARD p
  ; On the first night, the first time
  ; he comes into a room the doorway
  ; that leads toward her glows. After
  ; that night he has to bump into a
  ; clock to see it (PROC Bump).
  gl=255 lit=0
  IF seen(hroom)=0 THEN
    seen(hroom)=1
    IF level=1 THEN Way() FI
  FI
  ; draw the room into the array.
  ; Clear the missiles and all four
  ; players (1280 bytes from +768 to
  ; the end of player memory).
  BuildRoom()
  Zero(pmb+768,1280)
  ; on the screen: the top three rows
  ; (120 cells) and the bottom row as
  ; they are
  MoveBlock(scrn,room,120)
  MoveBlock(scrn+840,room+840,40)
  FOR y=3 TO 20 DO   ; only the walls
    p=y*40
    Zero(scrn+p,40)
    Poke(scrn+p,room(p))
    Poke(scrn+p+39,room(p+39))
  OD
  ; no ghouls yet, his cell, no
  ; lamplight drawn, no lightning
  FOR i=0 TO MAXG-1 DO gst(i)=0 OD
  hcx=(hx-44) RSH 2 hcy=(hy-19) RSH 3
  lcx=255 flash=0 Poke(712,0)
  ; the ghouls who live here, and two
  ; more if the pack is in this room
  SpawnG(rgh(hroom))
  IF hroom=proom THEN SpawnG(2) FI
  ; Her room: put her shape into
  ; player memory. 26 bytes for player
  ; 3 (at +1792) and 26 for the
  ; missiles (at +768), each starting
  ; ly TV lines down.
  IF hroom=groom THEN
    MoveBlock(pmb+1792+ly,girl,26)
    MoveBlock(pmb+768+ly,girl+26,26)
  FI
  Sfx(SDOOR,8)
  ; she hears the door, and calls
  IF vtm>20 THEN vtm=20 FI
RETURN

; Draw him, every frame. A pose is 78
; bytes: 26 rows for each of players
; 0, 1 and 2, which lie one on top of
; another to give him his colours.
PROC DrawHero()
  CARD s
  BYTE x
  ; hstep picks one of his two walking
  ; poses, changing every eight frames
  ; he walks
  IF hdir=0 THEN
    s=hero+((hstep RSH 3)&1)*78
  ELSE
    s=herol+((hstep RSH 3)&1)*78
  FI
  ; With one-line players each player
  ; has 256 bytes of memory, one for
  ; every TV line down the screen:
  ; player 0 at +1024, player 1 at
  ; +1280, player 2 at +1536. Starting
  ; at +hy puts him hy lines down.
  MoveBlock(pmb+1024+hy,s,26)
  MoveBlock(pmb+1280+hy,s+26,26)
  MoveBlock(pmb+1536+hy,s+52,26)
  ; while a ghoul holds him he
  ; flickers: position 0 is off the
  ; screen
  x=hx
  IF (stun&2)=2 THEN x=0 FI ; held
  hposp0=x hposp1=x hposp2=x
RETURN

; He has walked into something. If it
; is a clock, it chimes, and (after
; the first night) the doorway toward
; her glows: once for each room.
PROC Bump(BYTE c)
  BYTE y
  CARD p
  ; not a clock: nothing happens
  IF c<CLOCK OR c>CLOCK+5 THEN
    RETURN FI
  ; not on night 1, only once in each
  ; room (seen goes to 2), and not in
  ; her room
  IF level=1 OR seen(hroom)<>1
     OR hroom=groom THEN RETURN FI
  seen(hroom)=2
  Way() GlowDoor()
  ; show the top and bottom rows and
  ; the side doorways again, where the
  ; glow may now be
  MoveBlock(scrn,room,120)
  MoveBlock(scrn+840,room+840,40)
  FOR y=10 TO 13 DO
    p=y*40
    Poke(scrn+p,room(p))
    Poke(scrn+p+39,room(p+39))
  OD
  Sfx(SFIND,24)
  Msg("THE CLOCK CHIMES...")
RETURN

; Move him, once a frame. The stick
; reads 15 at rest, and one bit goes
; to 0 for each way it is pushed: 1
; up, 2 down, 4 left, 8 right. He goes
; 1 across or 2 up or down in a frame,
; if what is there is not solid. Then
; see if he has gone through a
; doorway.
PROC Hero()
  BYTE s,c
  ; hfree: the time he cannot be
  ; grabbed runs down. stun: held by a
  ; ghoul, he cannot move.
  IF hfree>0 THEN hfree==-1 FI
  IF stun>0 THEN stun==-1
  ELSE
    s=Stick(0)
    ; stick pushed: on with his walk
    IF s<>15 THEN hstep==+1 FI
    ; right: look at the cell 4 ahead
    IF (s&8)=0 THEN
      hdir=0
      c=At(hx+4,hy)
      IF Solid(c)=0 THEN hx==+1
      ELSE Bump(c) FI
    FI
    ; left
    IF (s&4)=0 THEN
      hdir=1
      c=At(hx-4,hy)
      IF Solid(c)=0 THEN hx==-1
      ELSE Bump(c) FI
    FI
    ; up
    IF (s&1)=0 THEN
      c=At(hx,hy-2)
      IF Solid(c)=0 THEN hy==-2
      ELSE Bump(c) FI
    FI
    ; down
    IF (s&2)=0 THEN
      c=At(hx,hy+2)
      IF Solid(c)=0 THEN hy==+2
      ELSE Bump(c) FI
    FI
    ; through a door?
    ; Walls are solid, so he can only
    ; get this far out in a doorway.
    ; West is the room 1 less, east 1
    ; more, north 4 less, south 4
    ; more. He comes in at the
    ; opposite side.
    IF hx<=47 THEN
      hroom==-1 hx=194 Enter()
    ELSEIF hx>=197 THEN
      hroom==+1 hx=50 Enter()
    ELSEIF hy<=32 THEN
      hroom==-4 hy=166 Enter()
    ELSEIF hy>=178 THEN
      hroom==+4 hy=36 Enter()
    FI
  FI
  DrawHero()
RETURN

; has he reached her?
; (within 7 across and 14 down of her,
; in her room). found=1 ends the
; round: see PROC Main.
PROC Meet()
  IF hroom=groom
     AND hx+7>lx AND lx+7>hx
     AND hy+14>ly AND ly+14>hy
  THEN found=1 FI
RETURN

; She calls every few seconds, and at
; once if he calls first (the button).
; But then the ghouls hear him too.
PROC Voice()
  BYTE d
  ; both timers run down
  IF vcool>0 THEN vcool==-1 FI
  IF vtm>0 THEN vtm==-1 FI
  ; time for her call, or the button
  ; is down and has had its second of
  ; rest
  IF vtm=0 OR (Strig(0)=0 AND vcool=0)
  THEN
    ; vtm>0 means it was his call. The
    ; pack's next move comes 90 frames
    ; (a second and a half) sooner.
    IF vtm>0 THEN
      IF ptm>90 THEN ptm==-90
      ELSE ptm=1 FI
    FI
    ; her next call in 200 frames; the
    ; button rests for 60
    vtm=200 vcool=60
    ; d: doors between him and her.
    ; The meter shows 8 in her room
    ; and one less for each door, but
    ; never less than 1.
    d=dist(hroom)
    IF d<7 THEN vlev=8-d
    ELSE vlev=1 FI
    ShowVoice() Sfx(SCALL,24)
    IF d=0 THEN
      Msg("SHE IS IN THIS ROOM!")
    ELSE
      Msg("SHE CALLS: HELP ME!")
    FI
  FI
RETURN

; the ghouls go one room nearer to her
; Called every frame, but it only acts
; when its timer ptm runs out.
PROC Pack()
  BYTE d,nb
  ptm==-1
  IF ptm>0 THEN RETURN FI
  ptm=ptime
  ; take the first door that leads to
  ; a room nearer to her
  FOR d=0 TO 3 DO
    IF HasDoor(proom,d)=1 THEN
      nb=Nbr(proom,d)
      IF dist(nb)<dist(proom) THEN
        proom=nb
        EXIT
      FI
    FI
  OD
  ShowPack() Sfx(SGROAN,30)
  ; In her room: the round is lost. In
  ; his room: two of them appear.
  IF proom=groom THEN taken=1
  ELSEIF proom=hroom THEN
    SpawnG(2)
    Msg("THE GHOULS ARE HERE!")
  ELSE
    Msg("THEY ARE CLOSER NOW...")
  FI
RETURN

; ------------- sound -------------

; One frame of the sound effect that
; is playing, on voice 3. sfxt counts
; down the frames it has left, and
; most effects use it to slide the
; pitch or the loudness.
PROC Effect()
  sfxt==-1
  IF sfx=SCALL THEN   ; "he-elp!"
    IF sfxt>12 THEN
      Sound(3,40,10,vlev+4)
    ELSE Sound(3,50,10,vlev+4) FI
  ELSEIF sfx=SGRAB THEN ; a thump
    Sound(3,250-sfxt*12,10,10)
  ELSEIF sfx=SGROAN THEN ; a groan
    Sound(3,225-sfxt,12,8)
  ELSEIF sfx=SDOWN THEN ; underground
    Sound(3,220-sfxt*8,10,8)
  ELSEIF sfx=SFIND THEN ; a find
    IF sfxt>6 THEN Sound(3,60,10,8)
    ELSE Sound(3,40,10,8) FI
  ELSEIF sfx=STHUN THEN ; thunder
    Sound(3,200+Rand(50),8,sfxt RSH 2)
  ELSE                ; a door
    Sound(3,100+sfxt*4,10,4)
  FI
  IF sfxt=0 THEN sfx=0 FI
RETURN

; the band, called once a frame: bass
; on voice 0, lead on 1, its echo on 2,
; a heartbeat or a sound effect on 3
PROC Music()
  BYTE b,l,e,d
  ; in the middle of a step: let the
  ; bass and the lead die away a
  ; little
  IF mtm>0 THEN
    mtm==-1
    IF bvol>3 THEN bvol==-1 FI
    IF lvol>4 AND (mtm&1)=0 THEN
      lvol==-1 FI
  ELSE
    ; The next step of the tune. In
    ; the tables 0 is silence, 1 lets
    ; the note ring on, and anything
    ; else is a new note. The echo is
    ; the lead from three steps back.
    ; The drum table is 16 steps, used
    ; over and over.
    mtm=tempo-1
    b=bass(mpos) l=lead(mpos)
    e=lead((mpos-3)&127)
    d=drum(mpos&15)
    IF b=0 THEN bvol=0
    ELSEIF b>1 THEN bnote=b bvol=8 FI
    IF l=0 THEN lvol=0
    ELSEIF l>1 THEN lnote=l lvol=7 FI
    IF e=0 THEN evol=0
    ELSEIF e>1 THEN enote=e evol=3 FI
    IF d=1 THEN dtyp=1 dvol=12 dpit=100
    ELSEIF d=2 THEN dtyp=2 dvol=9
    ELSEIF d=3 THEN dtyp=3 dvol=4 FI
    ; the tune is 128 steps long
    mpos=(mpos+1)&127
  FI
  ; set the three voices
  Sound(0,bnote,12,bvol)
  Sound(1,lnote,10,lvol)
  Sound(2,enote,10,evol)
  ; voice 3: a sound effect if one is
  ; playing, else the drum
  IF sfx>0 THEN Effect()
  ELSEIF dvol>0 THEN
    IF dtyp=1 THEN    ; kick
      Sound(3,dpit,10,dvol)
      dpit==+45 dvol==-4
    ELSEIF dtyp=2 THEN ; snare
      Sound(3,14,8,dvol) dvol==-3
    ELSE              ; hat
      Sound(3,1,8,dvol) dvol=0
    FI
  ELSE Sound(3,0,0,0) FI
RETURN

; start the tune from the top, with
; every voice silent and no sound
; effect
PROC MusicOn()
  mpos=0 mtm=0 bvol=0 lvol=0 evol=0
  dvol=0 sfx=0
RETURN

; The digitized laugh: see screen 2.
; Each byte of laugh holds two samples
; of four bits. A sample is played by
; putting $10 + its level into the two
; volume registers; the empty FOR
; loops wait between samples. Nothing
; else can happen meanwhile. Called
; when a life is lost.
PROC Laugh(BYTE n)
  CARD i,c,fs,st
  BYTE b,v,d,k,o,p
  BYTE ARRAY ft(128)
  ; ft: the 16 levels at 1/8 to 8/8
  ; of full strength, 16 to a row,
  ; shrunk toward the middle level 8
  FOR k=1 TO 8 DO
    FOR v=0 TO 15 DO
      IF v<8 THEN d=8-((8-v)*k+4)/8
      ELSE d=8+((v-8)*k+4)/8 FI
      ft((k-1)*16+v)=$10%d
    OD
  OD
  ; n=1: once, fading over its last
  ; quarter. n=2: once at full strength
  ; (row 112), then again, fading all
  ; the way through. st is where the
  ; fade starts; it drops a row every
  ; fs bytes.
  IF n=1 THEN fs=LAUGHLEN/28
  ELSE fs=LAUGHLEN/7 FI
  ; o: the row of ft in use (112 is
  ; full strength)
  o=112
  FOR p=1 TO n DO
    st=LAUGHLEN c=fs-1
    IF p=n THEN st=LAUGHLEN-fs*7 FI
    FOR i=0 TO LAUGHLEN-1 DO
      ; the high half of the byte,
      ; then the low half
      b=laugh(i)
      v=ft(o+(b RSH 4)) audc1=v audc2=v
      FOR d=1 TO 9 DO OD
      v=ft(o+(b&15)) audc1=v audc2=v
      FOR d=1 TO 9 DO OD
      ; fading: a row quieter every fs
      ; bytes
      IF i>=st THEN
        c==+1
        IF c=fs THEN
          c=0
          IF o>0 THEN o==-16 FI
        FI
      FI
    OD
  OD
  audc1=0 audc2=0
RETURN

; ------------- the game -------------

; how hard is this level?
; Sets the numbers that depend on the
; night (level), and on ease.
PROC NewLevel()
  ; gdel: frames between a ghoul's
  ; steps, 15 on night 1 down to 8
  IF level<8 THEN gdel=16-level
  ELSE gdel=8 FI
  ; the grabs he can take
  IF level<3 THEN hpm=4-level
  ELSE hpm=1 FI
  ; frames the ghouls allow for each
  ; door between you and her: it goes
  ; on shrinking, night after night
  pace=150+2400/(level+2)
  ; and 150 more for each try already
  ; lost here
  pace==+ease*150
  ; slack: frames added to the pack's
  ; whole march (see PROC Round)
  IF level<6 THEN slack=1200-level*150
  ELSE slack=300 FI
  ; the tune goes faster after night 3
  tempo=10
  IF level>3 THEN tempo=9 FI
RETURN

; a new mansion, and everybody in it
; Called at the start of every try.
PROC Round()
  BYTE i,r
  ; doors, her room, the count of
  ; doors from it, furniture
  Maze() groom=Rand(12)
  Dist() Furnish()
  FOR i=0 TO 11 DO seen(i)=0 OD
  ; the ghouls start as far from her
  ; as they can be
  proom=0
  FOR i=1 TO 11 DO
    IF dist(i)>dist(proom) THEN
      proom=i FI
  OD
  ; and he starts a good way off too
  hroom=proom
  ; forty tries at a room more than
  ; two doors from her
  FOR i=1 TO 40 DO
    r=Rand(12)
    IF dist(r)>2 AND r<>proom THEN
      hroom=r
      EXIT
    FI
  OD
  ; no luck: any room but hers and the
  ; ghouls'
  IF hroom=proom THEN
    FOR r=0 TO 11 DO
      IF dist(r)>0 AND r<>proom THEN
        hroom=r FI
    OD
  FI
  ; ptime: frames between the pack's
  ; moves. Their whole march takes as
  ; long as he is allowed (pace for
  ; each door he has to go through)
  ; and slack frames more: about 17
  ; seconds on night 1, less on later
  ; nights.
  ptime=(dist(hroom)*pace+slack)
        /dist(proom)
  ptm=ptime
  ; she hides in one of the four
  ; corners of her room (lx,ly are
  ; player coordinates)
  IF Rand(2)=0 THEN lx=64
  ELSE lx=180 FI
  IF Rand(2)=0 THEN ly=44
  ELSE ly=150 FI
  ; nothing stands in her corner of
  ; her room, so he can always reach
  ; her
  FOR i=0 TO 2 DO
    r=groom*3+i
    IF (fx(r)<20 AND lx<124)
       OR (fx(r)>19 AND lx>123)
    THEN
      IF (fy(r)<10 AND ly<100)
         OR (fy(r)>9 AND ly>99)
      THEN ft(r)=255 ff(r)=1 FI
    FI
  OD
  ; two pieces hide lightning in a
  ; bottle: two different ones, and
  ; neither a piece just taken away,
  ; so every mansion has two to find
  DO
    bk1=Rand(36)
  UNTIL ft(bk1)<>255 OD
  DO
    bk2=Rand(36)
  UNTIL ft(bk2)<>255 AND bk2<>bk1 OD
  ; he starts in the middle of his
  ; room, with nothing in hand and
  ; full strength
  hx=124 hy=104 hdir=0 hstep=0
  stun=0 hfree=0 found=0 taken=0
  bot=0 lit=0 hp=hpm caught=0
  ; her first call comes after a
  ; second, the first lightning after
  ; 2.5 to 4
  vtm=60 vcool=0 vlev=0
  flash=0 ftm=150+Rand(100)
  ; draw the text lines and the room
  ShowAll() Enter()
  Msg("FIND HER! LISTEN...")
  MusicOn()
RETURN

; he has found her: the lights come on
PROC Found()
  SndRst()
  ; show the whole room, and her
  MoveBlock(scrn,room,880)
  hposp3=lx hposm3=lx hposm2=lx+2
  hposm1=lx+4 hposm0=lx+6
  ; she holds out her arms to him
  IF hx>=lx THEN
    MoveBlock(pmb+1792+ly,girl+156,26)
    MoveBlock(pmb+768+ly,girl+182,26)
  FI
  Msg("YOU FOUND HER! SAFE!")
  ; HAPPOKEY.ACT, 3.2 seconds of music
  ; on the sound chip's four voices,
  ; once through: a table of 193 rows,
  ; one for each sixtieth of a second.
  ; HpTick() pokes the next row into
  ; the sound registers and clears
  ; hpon after the last.
  ; Nothing moves meanwhile.
  HpStart()
  WHILE hpon<>0 DO
    Frame()
    HpTick()
  OD
  SndRst()
  ; points for finding her: 200, and
  ; 100 more for every room the ghouls
  ; still had to go
  score==+200+dist(proom)*100
  ShowScore()
RETURN

; the ghouls got to her room first
PROC Taken()
  BYTE i
  SndRst()
  Msg("THEY FOUND HER FIRST!")
  ; a falling note, then the laugh
  FOR i=0 TO 40 DO
    Frame()
    Sound(0,230-i,12,8)
  OD
  SndRst()
  ; twice for his last life
  IF lives=1 THEN Laugh(2)
  ELSE Laugh(1) FI
  ; a life goes, and its heart is
  ; rubbed out
  lives==-1
  Position(34+lives,23) Put(32)
RETURN

; a grab too many: it costs a life.
; If he has one left he gets up where
; he fell, in the same mansion; that
; ghoul has gone back underground,
; and for a while nothing can grab
; him. The pack loses no time by it.
PROC Caught()
  BYTE i
  SndRst()
  Msg("THE GHOULS GOT YOU!")
  ; a falling note, then the laugh
  FOR i=0 TO 40 DO
    Frame()
    Sound(0,230-i,12,8)
  OD
  SndRst()
  IF lives=1 THEN Laugh(2)
  ELSE Laugh(1) FI
  ; a life goes, and its heart is
  ; rubbed out
  lives==-1
  Position(34+lives,23) Put(32)
  caught=0
  IF lives=0 THEN RETURN FI
  ; the ghoul that got him is taken
  ; away, he has all his grabs back,
  ; and for 150 frames nothing can
  ; grab him
  EraseG(cg) gst(cg)=0
  IF rgh(hroom)>0 THEN
    rgh(hroom)==-1 FI
  hp=hpm ShowHp()
  stun=0 hfree=150
  Msg("UP YOU GET! FIND HER!")
RETURN

; Back to the title program, which
; shows the card for the next screen
; and loads it. (The loading is not
; written: under DOS 2.0S it is a
; binary load of D:THRILLER.OBJ and a
; jump to its start.)
PROC Next()
  SndRst()
  ; interrupt off, players and
  ; missiles off and moved off the
  ; screen
  Poke(54286,64) Poke(53277,0)
  hposp0=0 hposp1=0 hposp2=0 hposp3=0
  hposm0=0 hposm1=0 hposm2=0 hposm3=0
RETURN

; No lives left. Run by the title
; program: show GAME OVER for three
; seconds, set result to 0 and go
; back. Run by itself: wait for START.
PROC GameOver()
  BYTE i
  IF chain=1 THEN
    Msg("GAME OVER")
    FOR i=0 TO 180 DO Frame() OD
    result=0 Next()
  ELSE
    Msg("GAME OVER: PRESS START")
    DO Frame() UNTIL consol=6 OD
  FI
RETURN

; This screen's own title page, in big
; letters (GRAPHICS 18), with the
; tune. Only shown when the screen is
; run by itself. Waits for START.
PROC Title()
  ; interrupt and players off
  Poke(54286,64) Poke(53277,0)
  hposp0=0 hposp1=0 hposp2=0 hposp3=0
  hposm0=0 hposm1=0 hposm2=0 hposm3=0
  ; the Atari's own letters (756), and
  ; four colours
  Graphics(18) Poke(756,224)
  Poke(708,$36) Poke(709,$0E)
  Poke(710,$C8) Poke(711,$1A)
  Position(6,1) PrintD(6,"THRILLER")
  Position(4,3)
  PrintD(6,"screen three")
  Position(1,5)
  PrintD(6,"{inv}FIND YOUR GIRL IN{/inv}")
  Position(1,6)
  PrintD(6,"{inv}THE MANSION BEFORE{/inv}")
  Position(6,7)
  PrintD(6,"{inv}THEY DO!{/inv}")
  Position(4,9) PrintD(6,"press start")
  Position(0,11)
  PrintD(6,"{inv}designed 1984 d.lutz{/inv}")
  MusicOn() tempo=10
  DO
    Frame() Music()
    IF (frm&3)=0 THEN ; the title drips
      Poke(708,$32+Rand(4)*2) FI
  UNTIL consol=6 OD
  SndRst()
RETURN

; The whole program. The outer loop is
; one game, the middle loop one try at
; a mansion, the inner loop one TV
; frame.
PROC Main()
  Init()
  DO
    ; run by itself: title page, new
    ; game
    IF chain=0 THEN
      Title() score=0 lives=3 wins=0
    FI
    Screen() ease=0
    DO
      ; three screens won make a night
      level=wins/3+1 NewLevel()
      Round()
      DO
        ; one frame: him, his lamp,
        ; lightning, the bottle's
        ; light, the ghouls in this
        ; room, a life lost to them,
        ; her voice, the ghoul pack,
        ; has he reached her, the
        ; music
        Frame()
        Hero() Lamp() Storm() Glare()
        Ghouls()
        IF caught=1 THEN Caught() FI
        Voice() Pack() Meet() Music()
      UNTIL found=1 OR taken=1
            OR lives=0 OD
      IF found=1 THEN
        Found()
        ; in the full game: count the
        ; win, set result to 1 and go
        ; back to the title program
        IF chain=1 THEN
          wins==+1 result=1 Next()
          RETURN
        FI
        ; by itself: add 3 wins, which
        ; makes the next try a night
        ; later
        wins==+3 ease=0
      ELSEIF taken=1 THEN
        ; the ghouls won: a new
        ; mansion, a little easier
        ; (ease is 2 at most)
        Taken()
        IF ease<2 THEN ease==+1 FI
      FI
    UNTIL lives=0 OD
    GameOver()
    IF chain=1 THEN RETURN FI
  OD
RETURN
