; **************************************
; *  T H R I L L E R    -   screen 1   *
; *    "Save your girl from the        *
; *         haunted house!"            *
; **************************************
; 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.
; (The 1984 sketch called this screen
; "Catch the girl as you are a
; werewolf!" We made it a rescue.)
;
; The full moon has made a werewolf of
; you. Your girl has run from the wolf,
; straight toward the old house at the
; far side of the woods, and she does
; not know what is waiting inside it.
; Reach her before she reaches the
; door: one touch breaks the spell, you
; are yourself again, and she is safe.
; Trees stop you, logs trip you and
; brambles slow you down. The button
; makes the wolf leap: fast, straight,
; and clear over logs and brambles.
; When a cloud hides the moon you are
; only a man again, and slow. Then the
; button calls to her: she knows your
; voice, and stops to look back.
;
; In this listing {inv}...{/inv} marks
; text typed in inverse video.
;
; HOW THIS PROGRAM WORKS
; The woods are made of characters: a
; map 256 columns wide and 19 rows
; high, in a four-colour character
; mode. The TV shows 40 columns of it,
; and new columns are planted just off
; the right as the picture slides.
; Above are two rows of sky and a
; skyline; below are two lines of
; ordinary text for the score.
; He and she are players and missiles:
; small pictures laid over the screen
; that move a dot at a time. He is
; three players, one for each colour.
; She is the fourth player plus the
; four missiles.
; Main runs one loop for each TV
; frame, a sixtieth of a second: move
; him, move her, slide the woods,
; place the players, see if he has
; touched her, drift the clouds, play
; one step of the music.
; Touch her and the night is won. If
; she reaches the door first a life is
; lost. No lives left ends the game.

INCLUDE "D:SHAPES1.ACT" ; wset,wolf
INCLUDE "D:SHAPES.ACT"  ; hero,girl
INCLUDE "D:MUSIC1.ACT"  ; bass,lead,drum
INCLUDE "D:LAUGH.ACT"   ; laugh,LAUGHLEN

; names for the characters that make
; the woods, two addresses in page 6,
; two colours for player 1 (FUR and
; SKIN) and the sound effects
DEFINE STAR="65",GRASS="66",
  TRUNK="87",LOG="88",BRAMBLE="90",
  STUMP="92",GLOVE="111", ; characters
  DLIST="$640",      ; our display list
  LMS="$64B",        ; its first map row
  FUR="$18",SKIN="$28",
  SLEAP="1",STRIP="2",SHOWL="3",
  SWHIM="4",SCALL="5",
  SGLOVE="6"         ; 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's own memory:
; the frame clock, the attract timer,
; the top of memory, the console
; keys, two sound volume registers
; and the horizontal positions of the
; four players and four missiles
BYTE rtclok=20,atract=77,ramtop=106,
  consol=53279,
  gfine=$CB,  ; HSCROL for the woods
  ffine=$CC,  ; and for the skyline
  audc1=$D201,audc2=$D203,
  hposp0=$D000,hposp1=$D001,
  hposp2=$D002,hposp3=$D003,
  hposm0=$D004,hposm1=$D005,
  hposm2=$D006,hposm3=$D007
; addresses: screen memory, player
; memory, our character set, the map
CARD scrn,pmb,chb,mapb

; Where things are, in dots from the
; left end of the map (which is 256
; characters wide and slides along)
CARD cam,   ; the left edge of the TV
  hbx,gbx,  ; him and her
  togo,     ; how far she is from home
  gleft,    ; columns still to plant
  glen      ; how long this chase is

; lcol: the map column at the left of
;   the TV. gencol: the next column
;   to plant
; quiet: columns to leave bare at the
;   start. dens: things planted at a
;   time. fpos: where the skyline is
; hy: his top line. hpy: where he was
;   last drawn. hacc: the part of a
;   dot left over from his speed
; hstep: counts his steps, to pick
;   the picture. wolfm: 1 wolf, 0 man
; leap: frames of leap left. lcool:
;   frames until he can leap again
; stun: frames he is knocked still
; hfree: frames he cannot trip again
; called: 1 once he has called her
; flash: frames his suit flickers
; gy,gpy,gacc,gstep: the same for her
; gstop: frames she stands looking
;   back. gstuck: frames a tree has
;   held her up. gdodge: which way
;   she is going round it (1 up,
;   2 down)
; gspd: her speed. pan: her speed
;   when the wolf is close. fal: how
;   near the house she falters
; ctm: frames until the clouds move.
;   cdel: frames between moves. ncl:
;   how many clouds. cmax: sets how
;   long they can be. covered: 1 if
;   a cloud hides the moon
; chase: which night. got: 1 when he
;   has reached her. frm: counts
;   frames. hshown, bshown: what the
;   text lines last showed, so they
;   are printed only on a change
BYTE lcol,gencol,quiet,dens,fpos,
  hy,hpy,hacc,hstep,wolfm,leap,lcool,
  stun,hfree,called,flash,
  boost,  ; frames of extra speed left
  gy,gpy,gacc,gstep,gstop,gstuck,
  gdodge,gspd,pan,fal,
  ctm,cdel,ncl,cmax,covered,
  chase,got,frm,hshown,bshown

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

; ccx, clen: where each cloud is and
;   how long. hop: how many lines the
;   wolf is lifted at each stage of a
;   leap. fan is not used here.
BYTE ARRAY ccx(3),clen(3), ; clouds
  skyb(80),          ; the empty sky
  girll(208),        ; her, looking back
  hop=[2 4 6 6 6 4 2],
  fan=[121 96 81 60 81 60 47 40]

; Two display list interrupts, copied
; to page 6. The first runs under the
; skyline: it sets the fine scroll for
; the woods and changes the colours.
;  PHA; LDA $CB; STA WSYNC; STA HSCROL
;  LDA #$C4; STA COLPF0
;  LDA #$24; STA COLPF2
;  LDA #$00; STA COLBK
;  LDA #$1F; STA VDSLST; PLA; RTI
; The second runs under the woods. It
; colours the two text lines and sets
; the fine scroll for the skyline:
;  PHA; LDA #$34; STA WSYNC; STA COLPF2
;  LDA #$0E; STA COLPF1
;  LDA #0; STA COLBK
;  LDA $CC; STA HSCROL
;  LDA #0; STA VDSLST; PLA; RTI
; the leaves, night by night: summer,
; autumn, winter (snow), spring. A
; salute to Eastern Front (1941).
BYTE ARRAY leaf=[$C4 $36 $0A $B8]

; the machine code of the two
; interrupts, 31 bytes each
BYTE ARRAY dlicode=[
 $48 $A5 $CB $8D $0A $D4 $8D $04 $D4
 $A9 $C4 $8D $16 $D0 $A9 $24 $8D $18
 $D0 $A9 $00 $8D $1A $D0 $A9 $1F $8D
 $00 $02 $68 $40
 $48 $A9 $34 $8D $0A $D4 $8D $18 $D0
 $A9 $0E $8D $17 $D0 $A9 $00 $8D $1A
 $D0 $A5 $CC $8D $04 $D4 $A9 $00 $8D
 $00 $02 $68 $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"

; wait for the next sixtieth of a second
; It also holds off the Atari's
; attract mode (the colour cycling
; when no key is pressed) and counts
; the frames in frm.
PROC Frame()
  BYTE f
  f=rtclok
  DO UNTIL rtclok<>f OD
  atract=0 frm==+1
RETURN

; start sound effect t, to last n
; frames. 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 turned end for end, so
; the picture faces the other way)
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

; done once: memory and shapes. From
; the top down: 19 pages of map (one
; for each row of the woods), 1K of
; characters, 2K of players.
PROC Init()
  ; take 32 pages (8K) off the top of
  ; memory for ourselves
  ramtop==-32
  Graphics(0)
  ; pmb: players. chb: characters, 2K
  ; above. mapb: the map, 1K above
  ; that.
  pmb=ramtop*256 chb=pmb+2048
  mapb=pmb+3072
  Zero(pmb,2048)
  ; copy the Atari's own characters
  ; (at 57344), then put ours over
  ; them from character 65 on (65 x
  ; 8 bytes = 520)
  MoveBlock(chb,57344,1024)
  MoveBlock(chb+520,wset,384)
  ; the interrupts go to page 6
  MoveBlock($600,dlicode,62)
  ; her, mirrored (looking back)
  Flip(girl,girll,208)
RETURN

; The game screen. Our own display
; list: two rows of sky, the skyline,
; 19 rows of woods (each with its own
; address, so the woods can slide) and
; two lines of ordinary text.
PROC Screen()
  CARD p
  BYTE i
  Graphics(0)
  ; no cursor; left margin at 0;
  ; scrn = where screen memory is
  Poke(752,1) Poke(82,0)
  scrn=PeekC(88)
  ; The display list is a table that
  ; says what kind each line of the
  ; TV is. Three blank strips, then
  ; two rows of sky from scrn ($44:
  ; four-colour characters, with an
  ; address to follow).
  Poke(DLIST,$70) Poke(DLIST+1,$70)
  Poke(DLIST+2,$70)
  Poke(DLIST+3,$44) PokeC(DLIST+4,scrn)
  Poke(DLIST+6,4)
  Poke(DLIST+7,$D4)  ; skyline: scrolls,
  PokeC(DLIST+8,scrn+80) ; then a DLI
  ; 19 rows of woods ($54: the same,
  ; but able to slide sideways). Row
  ; i is page i of the map; the low
  ; byte of its address is the map
  ; column at the left of the TV.
  p=DLIST+10
  FOR i=0 TO 18 DO
    Poke(p,$54) Poke(p+1,0)
    Poke(p+2,(mapb RSH 8)+i)
    p==+3
  OD
  Poke(p-3,$D4)      ; the second DLI
  ; two text lines, from row 22 of
  ; the ordinary screen (22 x 40 =
  ; 880), then back to the top
  Poke(p,$42) PokeC(p+1,scrn+880)
  Poke(p+3,2)
  Poke(p+4,$41) PokeC(p+5,DLIST)
  ; tell the Atari to use our list,
  ; our characters and our players
  PokeC(560,DLIST)
  Poke(756,chb RSH 8)
  Poke(54279,ramtop) ; PMBASE
  Poke(53277,3)      ; GRACTL
  Poke(623,49)       ; players in front,
                     ; missiles = 5th,
                     ; P0 over P1 = both
                     ; colours ORed: his
                     ; socks, the wolf's
                     ; eye and teeth
  Poke(559,62)       ; one-line players
  Poke(704,$34) ; his red suit
  Poke(705,FUR) ; fur, or skin
  Poke(706,$02) ; claws; hair and shoes
  Poke(707,$86) ; her dress
  Poke(708,$06) ; clouds (then leaves)
  Poke(709,$0C) ; the moon; pale things
  Poke(710,$92) ; skyline (then wood)
  Poke(711,$26) ; her skin;
                ; the lit windows
  Poke(712,0)
  ; point the interrupt vector at
  ; page 6 and switch interrupts on
  PokeC(512,$600) Poke(54286,192)
RETURN

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

; show a message at the right of the
; upper text line, wiping the old one
PROC Msg(BYTE ARRAY s)
  Position(17,22)
  Print("                      ")
  Position(17,22) Print(s)
RETURN

; print the score on the bottom line
PROC ShowScore()
  Position(7,23) PrintC(score)
RETURN

; show whether he is WOLF or MAN
PROC ShowForm()
  Position(1,22)
  IF wolfm=1 THEN Print("{inv}WOLF{/inv}")
  ELSE Print("MAN ") FI
RETURN

; what the button will do just now
PROC ShowButton()
  BYTE b
  b=0
  IF wolfm=1 THEN
    IF lcool=0 AND leap=0 THEN b=1 FI
  ELSEIF called=0 THEN b=2 FI
  IF b<>bshown THEN
    bshown=b Position(6,22)
    IF b=1 THEN Print("LEAP")
    ELSEIF b=2 THEN Print("CALL")
    ELSE Print("    ") FI
  FI
RETURN

; how far she is from the house, in
; steps of 16 dots (four characters).
; Printed only when it changes.
PROC ShowHome()
  BYTE n
  n=togo RSH 4
  IF n<>hshown THEN
    hshown=n Position(29,23)
    PrintB(n) Put(32)
  FI
RETURN

; print both text lines afresh: the
; labels, the night, a heart for each
; life and all the numbers
PROC ShowAll()
  BYTE i
  Position(1,23) Print("SCORE")
  Position(14,23) Print("NIGHT ")
  PrintB(chase)
  Position(23,23) Print("HOUSE")
  Position(34,23) Print("     ")
  Position(34,23)
  ; character 0 is a heart
  FOR i=1 TO lives DO Put(0) OD
  ShowScore() ShowForm()
  hshown=255 ShowHome()
  bshown=255 ShowButton()
RETURN

; --------------- the woods ---------------

; the house, with its door four columns
; back from the newest column
PROC House()
  BYTE r,i
  CARD p
  ; clear a space 8 wide, rows 3-10
  FOR r=3 TO 10 DO
    Zero(mapb+r*256+gencol-7,8)
  OD
  ; p: the left end of the house in
  ; a map row. A row is 256 bytes,
  ; so p+256 is the cell below and
  ; p-252 is one row up, four along.
  p=mapb+1280+gencol-7   ; row 5
  Poke(p-252,104)        ; the chimney
  ; the roof: narrow, then wide
  Poke(p+2,93) Poke(p+3,94)
  Poke(p+4,94) Poke(p+5,95)
  p==+256
  Poke(p,93) Poke(p+7,95)
  FOR i=1 TO 6 DO Poke(p+i,94) OD
  ; three rows of wall (rows 7-9)
  FOR r=7 TO 9 DO
    p==+256
    FOR i=0 TO 7 DO Poke(p+i,96) OD
    IF r=7 THEN        ; lit windows:
      Poke(p+1,230) Poke(p+2,231)
      Poke(p+5,230) Poke(p+6,231)
    ELSEIF r=9 THEN    ; wide upstairs,
      Poke(p+1,225) Poke(p+6,225)
    FI                 ; narrow below
  OD
  ; the crooked door, two wide
  ; (p is in row 9; p-253 is row 8)
  Poke(p-253,98) Poke(p+3,99)
  Poke(p-252,100) Poke(p+4,101)
RETURN

; a tree, a log, a bramble patch or a
; stump, in the three newest columns
PROC Thing()
  BYTE t,k
  CARD p
  ; pick a row from 2 to 18 in the
  ; newest column; give up if the
  ; cell to its left is taken
  p=mapb+(2+Rand(17))*256+gencol
  IF Peek(p-1)>GRASS THEN RETURN FI
  ; 5 times in 10 a tree, 2 a log,
  ; 2 brambles, 1 a stump
  t=Rand(10)
  IF t<5 THEN          ; a tree
    ; it needs room above: the crown
    ; is 3 wide and 2 high, over the
    ; trunk (p-256 is one row up,
    ; p-512 two)
    IF Peek(p-257)>GRASS
       OR Peek(p-513)>GRASS
    THEN RETURN FI
    k=81               ; in leaf, or
    IF Rand(4)=0 THEN k=105 FI ; bare
    Poke(p-514,k) Poke(p-513,k+1)
    Poke(p-512,k+2) Poke(p-258,k+3)
    Poke(p-257,k+4) Poke(p-256,k+5)
    Poke(p-1,TRUNK)
  ELSEIF t<7 THEN
    Poke(p-1,LOG) Poke(p,LOG+1)
  ELSEIF t<9 THEN
    Poke(p-1,BRAMBLE) Poke(p,BRAMBLE+1)
  ELSE Poke(p-1,STUMP) FI
RETURN

; Once in a while, a white glove lies
; in the woods. Run over it for a
; burst of speed.
PROC Glove()
  CARD p
  ; only on bare ground or grass
  p=mapb+(2+Rand(16))*256+gencol
  IF Peek(p-1)<=GRASS AND Peek(p)<=GRASS
  THEN
    Poke(p-1,GLOVE) Poke(p,GLOVE+1)
  FI
RETURN

; plant one more column of the woods,
; just off the right of the TV
PROC GenCol()
  BYTE r,k
  CARD p
  ; bare ground, with a tuft of grass
  ; now and then
  p=mapb+gencol
  FOR r=0 TO 18 DO
    IF Rand(14)=0 THEN Poke(p,GRASS)
    ELSE Poke(p,0) FI
    p==+256
  OD
  ; gleft counts down to the house.
  ; Until then every fourth column
  ; gets dens things, but not the
  ; first columns (quiet) or the
  ; last 12 before the house.
  IF gleft>0 THEN
    gleft==-1
    IF gleft=0 THEN House()
    ELSEIF gleft>12 AND quiet=0
       AND (gencol&3)=3
    THEN
      FOR k=1 TO dens DO Thing() OD
    FI
    ; one column in 32 has an even
    ; chance of a glove
    IF gleft>12 AND quiet=0
       AND (gencol&31)=17
    THEN
      IF Rand(2)=0 THEN Glove() FI
    FI
  FI
  IF quiet>0 THEN quiet==-1 FI
  gencol==+1
RETURN

; The map is only 256 columns wide.
; When the TV gets near the end of it,
; move what is in view back to the
; start, and everybody with it.
PROC Wrap()
  BYTE r
  CARD p
  ; copy columns 200-255 to 0-55.
  ; 200 columns are 800 dots.
  p=mapb
  FOR r=0 TO 18 DO
    MoveBlock(p,p+200,56) p==+256
  OD
  cam==-800 hbx==-800 gbx==-800
  gencol==-200 lcol==-200
RETURN

; The TV follows her, so she is always
; in sight, 88 dots from its left
; edge. He has to keep up: the left
; edge of the TV carries him along.
PROC Scroll()
  BYTE i,d
  CARD p
  ; has she run ahead? move the TV
  IF gbx>cam+88 THEN
    d=gbx-cam-88 cam==+d fpos==+d
  FI
  ; keep him on the TV
  IF hbx<cam+4 THEN hbx=cam+4 FI
  IF hbx>cam+150 THEN hbx=cam+150 FI
  ; Four dots to a character: the
  ; whole columns go into the display
  ; list (lcol) and the odd dots into
  ; the fine scroll. The -4 is there
  ; because a scrolling row is 48
  ; wide and starts four characters
  ; to the left of the TV picture.
  lcol=((cam+3) RSH 2)-4
  IF lcol>=200 THEN Wrap() FI
  ; plant ahead: 52 columns from lcol
  WHILE gencol<lcol+52 DO GenCol() OD
  ; the same column for all 19 rows
  p=LMS
  FOR i=0 TO 18 DO
    Poke(p,lcol) p==+3
  OD
  gfine=(4-(cam&3))&3
  ; the skyline goes by at half speed
  ; (it is the same four characters
  ; over and over, so 16 dots of
  ; fine scroll go round for ever)
  ffine=(16-((fpos RSH 1)&15))&15
RETURN

; what is on the ground under a figure
; whose picture starts at line y?
BYTE FUNC Cell(CARD x BYTE y)
  BYTE r
  ; x is in dots, four to a column
  x=x RSH 2
  IF x>=gencol THEN RETURN (0) FI
  ; Players count lines from 32 at
  ; the top of the TV, and the woods
  ; start 24 lines down. So y-33 is
  ; a line near the feet of a figure
  ; 26 lines high. 8 lines to a row.
  r=(y-33) RSH 3
RETURN (Peek(mapb+r*256+x))

; 1 if character c blocks the way: a
; tree trunk or a stump
BYTE FUNC Solid(BYTE c)
  IF c=TRUNK OR c=STUMP THEN
    RETURN (1) FI
RETURN (0)

; ---------------- the sky ----------------

; Build the sky: 80 bytes, two rows of
; 40, with stars and the moon (four
; characters). skyb keeps a copy with
; no clouds. Then the skyline.
PROC Sky()
  BYTE i
  Zero(skyb,80)
  FOR i=1 TO 12 DO
    skyb(Rand(80))=STAR
  OD
  skyb(6)=67 skyb(7)=68  ; the moon
  skyb(46)=69 skyb(47)=70
  MoveBlock(scrn,skyb,80)
  ; 48 characters: a scrolling row
  ; is wider than the TV
  FOR i=0 TO 47 DO       ; the skyline
    Poke(scrn+80+i,77+(i&3))
  OD
RETURN

; the moon comes out (w=1) or goes in
; Called by Clouds. Sets his colour,
; a sound and a message. flash makes
; his suit flicker for 16 frames.
PROC Change(BYTE w)
  wolfm=w leap=0 flash=16
  IF w=1 THEN
    Poke(705,FUR) Sfx(SHOWL,40)
    Msg("THE MOON! RUN, WOLF!")
  ELSE
    Poke(705,SKIN) Sfx(SWHIM,20)
    called=0
    Msg("CLOUDS... JUST A MAN")
  FI
  ShowForm()
RETURN

; The clouds drift to the left, a
; column at a time. ccx is a cloud's
; left end plus 16, so that it can be
; partly off the left of the TV.
PROC Clouds()
  BYTE i,k,x,n,cov
  ; only every cdel+1 frames
  IF ctm>0 THEN ctm==-1 RETURN FI
  ctm=cdel
  ; clean sky, then draw them again
  MoveBlock(scrn,skyb,80)
  cov=0
  FOR i=0 TO ncl-1 DO
    ccx(i)==-1 n=clen(i)
    IF ccx(i)+n<=16 THEN ; round again
      ccx(i)=56+Rand(40)
      n=5+Rand(cmax) clen(i)=n
    FI
    ; a cloud is two rows high: a
    ; left end, middles and a right
    ; end. Draw what is on the TV.
    FOR k=0 TO n-1 DO
      x=ccx(i)+k
      IF x>=16 AND x<56 THEN
        x==-16
        IF k=0 THEN
          Poke(scrn+x,71)
          Poke(scrn+40+x,74)
        ELSEIF k=n-1 THEN
          Poke(scrn+x,73)
          Poke(scrn+40+x,76)
        ELSE
          Poke(scrn+x,72)
          Poke(scrn+40+x,75)
        FI
      FI
    OD
    ; is it right over the moon?
    ; (the moon is columns 6 and 7:
    ; 22 and 23 with the 16 added)
    IF ccx(i)<=22 AND ccx(i)+n>=24
    THEN cov=1 FI
  OD
  ; moon just hidden, or just out?
  IF cov<>covered THEN
    covered=cov Change(1-cov)
  FI
RETURN

; ------------ him and her ------------

; draw him: pick the right picture and
; copy it into the three players that
; make him up
PROC DrawHero()
  CARD s
  BYTE y
  ; s: which picture. Each is 78
  ; bytes: 26 lines for each of the
  ; three players. Wolf: two running
  ; and one leaping (lifted by hop).
  ; Man: two walking.
  y=hy
  IF wolfm=1 THEN
    IF leap>0 THEN
      s=wolf+156 y=hy-hop(leap RSH 2)
    ELSE
      s=wolf+((hstep RSH 2)&1)*78
    FI
  ELSE
    s=hero+((hstep RSH 3)&1)*78
  FI
  ; A player has 256 bytes, one for
  ; each line of the TV. Player 0 is
  ; at pmb+1024, 1 at +1280, 2 at
  ; +1536. Putting 26 bytes at +y
  ; draws them from line y down.
  IF y<>hpy THEN   ; wipe the old one
    Zero(pmb+1024+hpy,26)
    Zero(pmb+1280+hpy,26)
    Zero(pmb+1536+hpy,26)
    hpy=y
  FI
  MoveBlock(pmb+1024+y,s,26)
  MoveBlock(pmb+1280+y,s+26,26)
  MoveBlock(pmb+1536+y,s+52,26)
RETURN

; as a man he can call to her
; (once each time he is a man). She
; stops for 45 frames.
PROC Call()
  called=1 Sfx(SCALL,16)
  gstop=45 Msg("SHE KNOWS YOUR VOICE")
RETURN

; the wolf has run into a log: he is
; stunned for 25 frames. hfree gives
; him 60 frames in which he cannot
; trip again.
PROC Trip()
  stun=25 hfree=60 Sfx(STRIP,10)
  Msg("TRIPPED ON A LOG!")
RETURN

; he picks up the glove under his feet
PROC Take()
  BYTE r
  CARD p
  ; find the map cell under him
  r=(hy-33) RSH 3
  p=mapb+r*256+((hbx+4) RSH 2)
  ; a glove is two characters: wipe
  ; both
  IF Peek(p)=GLOVE+1 THEN p==-1 FI
  Poke(p,0) Poke(p+1,0)
  ; 200 frames of speed, and 50
  ; points for picking it up
  boost=200 score==+50 ShowScore()
  Sfx(SGLOVE,24)
  Msg("THE GLOVE! FASTER!")
RETURN

; Move him, once a frame: read the
; stick and button, work out his speed
; from what he is and what is
; underfoot, then move if no tree is
; in the way.
PROC Hero()
  BYTE s,c,spd,d
  CARD nx
  ; s is the stick. Stunned: no
  ; stick. Leaping: the stick is
  ; forced right (7) for 24 frames,
  ; then 45 frames before the next.
  ; Otherwise the button leaps (wolf)
  ; or calls (man, once).
  s=Stick(0)
  IF stun>0 THEN stun==-1 s=15
  ELSEIF leap>0 THEN
    leap==-1 s=7   ; straight ahead
    IF leap=0 THEN lcool=45 FI
  ELSE
    IF lcool>0 THEN lcool==-1 FI
    IF Strig(0)=0 THEN
      IF wolfm=1 THEN
        IF lcool=0 THEN
          leap=24 s=7 Sfx(SLEAP,12)
        FI
      ELSEIF called=0 THEN Call() FI
    FI
  FI
  ; a glove under him?
  c=Cell(hbx+4,hy)
  IF c=GLOVE OR c=GLOVE+1 THEN
    Take() FI
  ; speed: wolf 32, man 16, a leap
  ; 44. On the ground, brambles
  ; halve it; a log slows the man to
  ; 6 and trips the wolf.
  IF wolfm=1 THEN spd=32 ELSE spd=16 FI
  IF leap>0 THEN spd=44
  ELSE             ; what is underfoot?
    c=Cell(hbx+4,hy)
    IF c=BRAMBLE OR c=BRAMBLE+1 THEN
      spd=spd RSH 1
    ELSEIF c=LOG OR c=LOG+1 THEN
      IF wolfm=0 THEN spd=6
      ELSEIF hfree=0 AND stun=0 THEN
        Trip() s=15
      FI
    FI
  FI
  IF hfree>0 THEN hfree==-1 FI
  ; the glove: half as fast again
  IF boost>0 THEN
    spd=spd+(spd RSH 1) FI
  ; speeds are sixteenths of a dot
  ; hacc adds them up; d is the whole
  ; dots to move this frame
  hacc==+spd d=hacc RSH 4 hacc==&15
  ; hstep picks the walking picture
  IF s<>15 THEN hstep==+1 FI
  ; right (stick bit 8 is clear):
  ; look at the cell at his front
  IF (s&8)=0 THEN
    nx=hbx+d
    IF Solid(Cell(nx+6,hy))=1 THEN
      IF leap>0 THEN ; into a tree!
        leap=0 lcool=45 stun=30
        Sfx(STRIP,10)
      FI
    ELSE hbx=nx FI
  ; left, but not off the TV
  ELSEIF (s&4)=0 AND hbx>cam+4+d THEN
    nx=hbx-d
    IF Solid(Cell(nx+1,hy))=0 THEN
      hbx=nx FI
  FI
  ; up and down, two lines a frame,
  ; inside the woods (lines 50-180)
  IF (s&1)=0 AND hy>50 THEN
    IF Solid(Cell(hbx+4,hy-2))=0 THEN
      hy==-2 FI
  FI
  IF (s&2)=0 AND hy<180 THEN
    IF Solid(Cell(hbx+4,hy+2))=0 THEN
      hy==+2 FI
  FI
  DrawHero()
RETURN

; She runs for the house. She goes
; round trees, shies away from the
; wolf when he is close, and stumbles
; a little in logs and brambles. The
; wolf is only a shade faster than a
; frightened girl: he has to leap.
; Near the house she falters.
PROC Girl()
  BYTE c,d,w
  CARD nx,s
  ; standing still after his call?
  IF gstop>0 THEN gstop==-1
  ELSE
    ; w: 1 = step up, 2 = step down.
    ; A tree 8 or 12 dots ahead: pick
    ; a way round and keep to it.
    w=0
    IF Solid(Cell(gbx+8,gy))=1
       OR Solid(Cell(gbx+12,gy))=1
    THEN               ; a tree ahead
      IF gdodge=0 THEN
        IF gy>112 THEN gdodge=1
        ELSE gdodge=2 FI
      FI
      w=gdodge
    ELSE
      ; clear ahead. Every other
      ; frame: near the house, make
      ; for the door's level (110).
      ; If the wolf is close behind
      ; and level, edge away.
      gdodge=0 gstuck=0
      IF (frm&1)=0 THEN
        IF togo<240 THEN ; the door!
          IF gy>110 THEN w=1
          ELSEIF gy<110 THEN w=2 FI
        ELSEIF wolfm=1 AND hbx+56>gbx
           AND hy+12>gy AND gy+12>hy
        THEN
          IF hy<gy THEN w=2
          ELSE w=1 FI
        ELSEIF (frm&7)=0 THEN
          ; back toward the middle
          IF gy>110 THEN w=1
          ELSEIF gy<110 THEN w=2 FI
        FI
      FI
    FI
    ; take the step, unless a tree is
    ; there. At the edge of the
    ; woods, go round the other way.
    IF w=1 THEN
      IF gy<=50 THEN gdodge=2
      ELSEIF Solid(Cell(gbx+4,gy-2))=0
      THEN gy==-2 FI
    ELSEIF w=2 THEN
      IF gy>=180 THEN gdodge=1
      ELSEIF Solid(Cell(gbx+4,gy+2))=0
      THEN gy==+2 FI
    FI
    ; her speed this frame
    c=Cell(gbx+4,gy)
    d=gspd
    ; with the wolf at her heels she
    ; finds a little more speed
    IF wolfm=1 AND hbx+48>gbx THEN
      d=pan FI
    ; logs and brambles halve it; so
    ; does faltering near the house
    IF c>=LOG AND c<=BRAMBLE+1 THEN
      d=d RSH 1 FI
    IF togo<fal THEN d=d RSH 1 FI
    ; (sixteenths of a dot, as his)
    gacc==+d d=gacc RSH 4 gacc==&15
    nx=gbx+d
    ; blocked by a tree: wait, but
    ; after 20 frames go through.
    ; togo counts down to the door.
    IF Solid(Cell(nx+6,gy))=1
       AND gstuck<20
    THEN gstuck==+1
    ELSE
      gbx=nx gstep==+1
      IF togo>d THEN togo==-d
      ELSE togo=0 FI
    FI
  FI
  ; her picture: looking back, or one
  ; of two running. 26 bytes go to
  ; player 3 (pmb+1792) and 26 to
  ; the missiles (pmb+768).
  IF gstop>0 THEN s=girll
  ELSE s=girl+((gstep RSH 2)&1)*52 FI
  IF gy<>gpy THEN
    Zero(pmb+1792+gpy,26)
    Zero(pmb+768+gpy,26)
    gpy=gy
  FI
  MoveBlock(pmb+1792+gy,s,26)
  MoveBlock(pmb+768+gy,s+26,26)
RETURN

; put the players where the TV now is
; 48 is the players' number for the
; left edge of the TV picture.
PROC Place()
  BYTE x
  x=hbx-cam+48
  ; (position 0 is off the TV, so he
  ; blinks while stunned)
  IF (stun&2)=2 THEN x=0 FI ; dazed
  hposp0=x hposp1=x hposp2=x
  ; her: player 3, and the four
  ; missiles side by side, each two
  ; dots wide
  x=gbx-cam+48
  hposp3=x hposm3=x hposm2=x+2
  hposm1=x+4 hposm0=x+6
  IF flash>0 THEN  ; he is changing
    flash==-1
    IF (flash&2)=2 THEN Poke(704,$3E)
    ELSE Poke(704,$34) FI
  ELSEIF boost>0 THEN ; the glove: his
    boost==-1         ; suit shimmers
    IF (boost&4)=4 THEN Poke(704,$3E)
    ELSE Poke(704,$34) FI
  FI
RETURN

; reaching her is enough, as a wolf or
; as a man
; (within 7 dots and 14 lines). Sets
; got, which ends the main loop.
PROC Touch()
  IF stun=0
     AND hbx+7>gbx AND gbx+7>hbx
     AND hy+14>gy AND gy+14>hy
  THEN got=1 FI
RETURN

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

; play one frame of the sound effect
; on voice 3. sfxt counts down, and
; most effects slide in pitch with it.
PROC Effect()
  BYTE t
  sfxt==-1
  IF sfx=SLEAP THEN   ; a rush of air
    Sound(3,20+sfxt*4,8,6)
  ELSEIF sfx=STRIP THEN ; a thump
    Sound(3,250-sfxt*12,10,10)
  ELSEIF sfx=SHOWL THEN ; the howl
    t=40-sfxt
    IF t<12 THEN Sound(3,120-t*6,10,10)
    ELSE Sound(3,36+t,10,10) FI
  ELSEIF sfx=SWHIM THEN ; a whimper
    Sound(3,160-sfxt*5,10,8)
  ELSEIF sfx=SGLOVE THEN ; a sparkle
    Sound(3,30+sfxt*3,10,8)
  ELSEIF sfxt>8 THEN  ; "hey!"
    Sound(3,60,10,10)
  ELSE Sound(3,45,10,10) 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,
; drums or a sound effect on 3. The
; lead only plays for the wolf.
PROC Music()
  BYTE b,l,e,d
  ; between steps, let the notes fade
  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
    ; a new step of the tune. In the
    ; tables 0 is silence, 1 holds the
    ; note, anything else is a new
    ; note. The echo is the lead,
    ; three steps late.
    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=8 FI
    IF e=0 THEN evol=0
    ELSEIF e>1 THEN enote=e evol=3 FI
    ; drums: 1 kick, 2 snare, 3 hat
    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
    mpos=(mpos+1)&127
  FI
  ; send it all to the sound chip
  Sound(0,bnote,12,bvol)
  IF wolfm=1 THEN
    Sound(1,lnote,10,lvol)
    Sound(2,enote,10,evol)
  ELSE
    Sound(1,0,0,0) Sound(2,0,0,0)
  FI
  ; voice 3: an effect if there is
  ; one, else the drum dying away
  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
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 holds two samples of four
; bits, poked into two channels as
; plain volume levels, about 4,000 a
; second.
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=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
      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
      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 tonight?
; Called before each try. chase is the
; night; ease softens it.
PROC NewChase()
  ; she is quicker every night; on
  ; night 7 she is as fast as the wolf,
  ; after that a shade faster, and only
  ; a leap will close the gap
  gspd=25+chase
  IF gspd>33 THEN gspd=33 FI
  ; (slower after each lost try)
  gspd==-ease
  ; her speed with the wolf at her heels
  pan=gspd+3
  IF pan<30 THEN pan=30 FI
  ; more trees and logs
  dens=2
  IF chase>2 THEN dens=3 FI
  IF chase>4 THEN dens=4 FI
  IF chase>6 THEN dens=5 FI
  ; less far to the house
  IF chase<9 THEN glen=900-chase*50
  ELSE glen=450 FI
  ; and she falters later, near the door
  IF chase<8 THEN fal=130-chase*10
  ELSE fal=50 FI
  ; clouds: faster, more, longer
  cdel=24-chase*2
  IF chase>5 THEN cdel=14 FI
  ncl=2
  IF chase>3 THEN ncl=3 FI
  cmax=3+chase
  IF cmax>8 THEN cmax=8 FI
  ; and the music speeds up
  tempo=6
  IF chase>4 THEN tempo=5 FI
  ; the colour the first interrupt
  ; gives the leaves and the grass
  ; ($60A is a byte inside its code)
  Poke($60A,leaf((chase-1)&3))
RETURN

; set everybody out for another try
PROC Round()
  ; wipe the missiles and all four
  ; players (1280 bytes) and the map
  ; (19 rows of 256)
  Zero(pmb+768,1280)
  Zero(mapb,4864)
  ; the TV at the start of the map;
  ; 46 bare columns; glen to plant
  cam=16 lcol=0 gencol=0 fpos=0
  quiet=46 gleft=glen
  ; him at dot 24, a wolf
  hbx=24 hy=110 hpy=110 hacc=0 hstep=0
  wolfm=1 leap=0 lcool=0 stun=0
  hfree=0 called=0 flash=0 boost=0
  ; her 80 dots ahead
  gbx=104 gy=110 gpy=110 gacc=0
  gstep=0 gstop=0 gstuck=0 gdodge=0
  ; the door is 5 columns back from
  ; the last one planted; togo is
  ; her distance from it in dots
  togo=(glen-5)*4-gbx
  got=0 covered=0 ctm=cdel
  ; three clouds, right of the moon
  ccx(0)=42 clen(0)=7
  ccx(1)=76 clen(1)=6
  ccx(2)=110 clen(2)=8
  Poke(705,FUR)
  Sky() Scroll() ShowAll()
  Msg("THE HOUSE IS HAUNTED!")
  MusicOn()
RETURN

; He has reached her. The spell breaks:
; he is himself again, and she is safe.
PROC Saved()
  BYTE i,pounce
  SndRst()
  ; was he in mid leap?
  pounce=leap
  ; a man again
  wolfm=0 leap=0 Poke(705,SKIN)
  DrawHero() ShowForm()
  ; she turns, and holds out her arms
  MoveBlock(pmb+1792+gy,girll+156,26)
  MoveBlock(pmb+768+gy,girll+182,26)
  IF pounce>0 THEN
    Msg("A FLYING TACKLE! +50")
  ELSE
    Msg("IT'S YOU! SHE IS SAFE")
  FI
  ; 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. i counts the
  ; frames.
  i=0
  HpStart()
  WHILE hpon<>0 DO
    Frame()
    HpTick()
    i==+1
    Poke(704,$30+(i&14)) ; the change
  OD
  SndRst()
  Poke(704,$34)
  ; 100 points for saving her, 2
  ; more for every 16 dots she was
  ; still short of the door (the
  ; HOUSE number), and 50 for
  ; catching her with a leap
  score==+100+(togo RSH 4)*2
  IF pounce>0 THEN score==+50 FI
  ShowScore()
RETURN

; she is through the door, and whoever
; lives there is delighted
PROC Taken()
  BYTE i
  SndRst()
  Msg("THE HOUSE HAS HER!")
  ; she is gone from the picture
  Zero(pmb+1792,256) Zero(pmb+768,256)
  FOR i=0 TO 30 DO     ; it slams
    Frame()
    IF i<10 THEN Sound(0,200,8,14-i)
    ELSE SndRst() FI
  OD
  ; twice for his last life
  IF lives=1 THEN Laugh(2)
  ELSE Laugh(1) FI
  ; a life lost: rub out one heart
  lives==-1
  Position(34+lives,23) Put(32)
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()
  ; interrupts and players off, and
  ; all eight moved off the TV
  SndRst()
  Poke(54286,64) Poke(53277,0)
  hposp0=0 hposp1=0 hposp2=0 hposp3=0
  hposm0=0 hposm1=0 hposm2=0 hposm3=0
RETURN

; All lives are gone. If the title
; program ran us, wait three seconds
; and hand back with result=0. If
; not, wait for the START key.
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

; The title page, shown only when this
; screen is run by itself: big text,
; the tune, and a wait for START.
PROC Title()
  ; interrupts 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
  ; big text (GRAPHICS 2, no window)
  ; in the Atari's own characters
  Graphics(18) Poke(756,224)
  Poke(708,$36) Poke(709,$0E)
  Poke(710,$C8) Poke(711,$1A)
  Position(6,1) PrintD(6,"THRILLER")
  Position(5,3) PrintD(6,"screen one")
  Position(3,5)
  PrintD(6,"{inv}SAVE YOUR GIRL{/inv}")
  Position(6,6)
  PrintD(6,"{inv}FROM THE{/inv}")
  Position(3,7)
  PrintD(6,"{inv}HAUNTED HOUSE!{/inv}")
  Position(4,9) PrintD(6,"press start")
  Position(0,11)
  PrintD(6,"{inv}designed 1984 d.lutz{/inv}")
  ; play the tune until START (wolfm
  ; is set so that the lead plays)
  MusicOn() tempo=6 wolfm=1
  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 game. One pass of the
; inner loop is one TV frame; the
; loop round it is one try at a
; night; the outer one is one game.
PROC Main()
  Init()
  DO
    ; run by itself: title, new game
    IF chain=0 THEN
      Title() score=0 lives=3 wins=0
    FI
    Screen() ease=0
    DO
      ; wins/3+1: the night number
      chase=wins/3+1 NewChase()
      Round()
      DO
        ; one TV frame
        Frame()
        Hero() Girl() Scroll() Place()
        Touch() Clouds() Music()
        ShowHome() ShowButton()
      UNTIL got=1 OR togo=0 OD
      ; he touched her: a win
      IF got=1 THEN
        Saved()
        ; run from the title program:
        ; one more screen won; go back
        IF chain=1 THEN
          wins==+1 result=1 Next()
          RETURN
        FI
        ; by itself: a harder night
        ; (wins+3 is one more night)
        wins==+3 ease=0
      ELSE
        ; she got home: a life lost,
        ; and the next try is easier
        Taken()
        IF ease<2 THEN ease==+1 FI
      FI
    UNTIL lives=0 OD
    GameOver()
    IF chain=1 THEN RETURN FI
  OD
RETURN
