; **************************************
; *  T H R I L L E R    -   screen 2   *
; *   "Protect your girlfriend from    *
; *         grisly ghouls!"            *
; **************************************
; 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.
;
; You are the fellow in the red suit.
; Ghouls climb out of their graves and
; shamble toward your girl, who stays a
; step behind you. Walk into a ghoul to
; shove it back. Press the button to
; dance: the ghouls nearby can't help
; joining in, and a dancing ghoul that
; you touch goes back into the ground.
; Keep her safe until dawn.
;
; In this listing {inv}...{/inv} marks
; text typed in inverse video.
;
; HOW THIS PROGRAM WORKS
; The graveyard and the ghouls are
; characters, in a four-colour
; character mode: 22 rows of 40. A
; ghoul is six characters, two wide
; and three high, and moves by whole
; characters. bkg keeps a
; copy of the empty graveyard, to put
; back wherever a ghoul has been.
; 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. Two lines of ordinary
; text at the bottom show the score.
; Main runs one loop for each TV
; frame, a sixtieth of a second: move
; him, move her, move the ghouls,
; perhaps raise a new one, see who is
; touching whom, run the clock and
; play one step of the music.
; The night is won when the clock
; reaches dawn. A life is lost if a
; ghoul keeps hold of her too long.

INCLUDE "D:SHAPES.ACT" ; cset,hero,girl
INCLUDE "D:MUSIC.ACT"  ; bass,lead,drum
INCLUDE "D:LAUGH.ACT"  ; laugh,LAUGHLEN

; A ghoul's state is a number. 1 to 3
; are climbing out of the grave and 6
; to 8 are sinking back (GSINK is the
; first of those). The rest have
; names.
DEFINE MAXG="8",     ; ghouls at once
  GWALK="4",GDANCE="5",GSINK="6",
  GDAZE="9",GGRAB="10", ; ghoul states
  GMAX="160",        ; a full groove
  SBUMP="1",SRISE="2",SSINK="3",
  SDANCE="4"         ; 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, the background colour, two
; sound volume registers and the
; horizontal positions of the four
; players and four missiles
BYTE rtclok=20,atract=77,ramtop=106,
  consol=53279,colbk=712,
  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
CARD scrn,pmb,chb

; him, her, and the night
; hx,hy: his place, in player dots and
;   lines. hdir: 0 facing right, 1
;   left. hstep: counts his steps.
;   hdance: frames of dance left
; lx,ly,ldir,lstep: the same for her
; groove: the dance meter, 0 to GMAX.
;   gshown: what the meter last showed
; night: which night. tleft: seconds
;   to dawn. ttick: frames so far in
;   this second
; maxg: most ghouls allowed tonight.
;   ngh: how many are up. gdel: frames
;   between a ghoul's steps
; spn: frames until the next may rise.
;   spdel: the usual wait
; caught: 1 when she is lost. frm:
;   counts frames
BYTE hx,hy,hdir,hstep,hdance,
  lx,ly,ldir,lstep,
  groove,gshown,night,
  tleft,ttick,maxg,ngh,gdel,
  spn,spdel,caught,frm,
  early,  ; ghouls still to hurry up
  hpush,  ; frames his claws stay out
  hpdir,  ; and to which side
  held,   ; a ghoul has hold of her
  grabt,  ; frames left to free her
  gtime   ; frames you are given

; 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

; the ghouls: state, column and row
; (of the top left character), a timer,
; which step, which way, dance beats
; A state of 0 means no ghoul there;
; see Ghouls() for the others.
BYTE ARRAY gst(MAXG),gx(MAXG),
  gy(MAXG),gtm(MAXG),gfr(MAXG),
  gdir(MAXG),gdn(MAXG)

BYTE ARRAY bkg(880), ; the graveyard
  herol(858),        ; him, facing left
  girll(208)         ; her, facing left

; where the eight graves are
; (gvx: the column. gvy: the top row
; of the plot of earth)
BYTE ARRAY gvx=[3 13 25 35 2 36 8 30],
  gvy=[6 5 5 6 13 13 19 19],
; the sky in the last seconds of night
  sky=[$2A $28 $36 $34 $44 $52
       $62 $72 $80 $90 $90 $00],
; riff: the notes of the dance sound.
; fan is not used here.
  riff=[121 96 81 60 47 40],
  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 graveyard and gives the two
; text lines their own colours:
;  PHA; LDA #$34; STA WSYNC; STA COLPF2
;  LDA #$0E; STA COLPF1; PLA; RTI
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
; 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

; 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
PROC FlipG(BYTE ARRAY src,dst)
  BYTE r,k
  CARD p
  FOR r=0 TO 2 DO
    ; a character is 8 bytes and a
    ; row of the ghoul is two of
    ; them. Left and right swap.
    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()
  ramtop==-16  ; 2K of players and 1K
  Graphics(0)  ; of characters up top
  ; pmb: players. chb: characters
  pmb=ramtop*256 chb=pmb+2048
  Zero(pmb,2048)
  ; the Atari's own characters (at
  ; 57344), then ours over them from
  ; character 65 on (65 x 8 = 520)
  MoveBlock(chb,57344,1024)
  MoveBlock(chb+520,cset,312)
  ; the cross and the leaning stone
  ; go in after the mirrored ghouls
  MoveBlock(chb+928,cset+312,64)
  ; ghoul pictures start at
  ; character 80 (640 / 8): two
  ; walking steps, each mirrored
  FlipG(chb+640,chb+832) ; walking left
  FlipG(chb+688,chb+880)
  ; him and her, facing left
  Flip(hero,herol,858)
  Flip(girl,girll,208)
RETURN

; the game screen: GRAPHICS 0 with its
; first 22 lines changed to ANTIC mode
; 4 (four-colour characters) and two
; lines of ordinary text left below
PROC Screen()
  CARD dl
  BYTE i
  Graphics(0)
  ; no cursor; left margin at 0
  Poke(752,1) Poke(82,0)
  ; scrn: screen memory. dl: the
  ; display list, the Atari's table
  ; that says what kind each line is
  scrn=PeekC(88) dl=PeekC(560)
  ; (dl+3 is the first line; it has
  ; an address after it, hence $44)
  Poke(dl+3,$44)
  FOR i=6 TO 25 DO Poke(dl+i,4) OD
  Poke(dl+26,$84)    ; ...and the DLI
  ; use our characters, and turn the
  ; players on
  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
                     ; white socks
  Poke(559,62)       ; one-line players
  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, grass
  Poke(709,$08) ; stone, rags
  Poke(710,$24) ; ghoul trousers, earth
  Poke(711,$26) ; her skin; a dancing
                ; ghoul's trousers
  Poke(712,0)
  ; point the interrupt vector at Dli
  ; and switch interrupts on
  PokeC(512,Dli) 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

; print the seconds left until dawn
PROC ShowTime()
  Position(28,23) PrintB(tleft) Put(32)
RETURN

; draw the groove meter: eight blocks,
; one for every 20 of groove. Printed
; only when it changes.
PROC ShowGroove()
  BYTE i,n
  n=groove/20
  IF n<>gshown THEN
    gshown=n Position(8,22)
    FOR i=1 TO 8 DO
      IF i<=n THEN Put(160)
      ELSE Put('.) FI
    OD
  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,22) Print("GROOVE")
  Position(1,23) Print("SCORE")
  Position(14,23) Print("NIGHT ")
  PrintB(night)
  Position(23,23) Print("DAWN")
  Position(32,23) Print("     ")
  Position(32,23)
  ; character 0 is a heart
  FOR i=1 TO lives DO Put(0) OD
  ShowScore() ShowTime()
  gshown=255 ShowGroove()
RETURN

; ------------- the graveyard -------------

; Build the empty graveyard in bkg, 22
; rows of 40 (so row r, column c is at
; r*40+c), and copy it to the screen.
PROC Scene()
  BYTE i
  CARD p
  Zero(bkg,880)
  ; rows 0 and 1 are sky
  FOR i=1 TO 14 DO     ; stars
    bkg(Rand(80))=65
  OD
  bkg(33)=76 bkg(34)=77 ; the moon
  bkg(73)=78 bkg(74)=79
  FOR i=0 TO 39 DO     ; the fence
    bkg(80+i)=67
  OD
  ; (rows 3 to 21: 120 is row 3)
  FOR i=1 TO 24 DO     ; grass
    p=120+Rand(19)*40+Rand(40)
    bkg(p)=66
  OD
  ; a grave is 2 wide and 4 high: a
  ; stone over a plot of earth. p is
  ; the stone, two rows above gvy.
  FOR i=0 TO 7 DO      ; eight graves:
    p=(gvy(i)-2)*40+gvx(i) ; a stone
    bkg(p)=68 bkg(p+1)=69
    bkg(p+40)=70 bkg(p+41)=71
    IF (i&3)=1 THEN    ; a cross
      bkg(p)=116 bkg(p+1)=117
      bkg(p+40)=118 bkg(p+41)=119
    ELSEIF (i&3)=3 THEN ; one that leans
      bkg(p)=120 bkg(p+1)=121
      bkg(p+40)=122 bkg(p+41)=123
    FI
    bkg(p+80)=72 bkg(p+81)=73 ; and a
    bkg(p+120)=74 bkg(p+121)=75 ; plot
  OD
  MoveBlock(scrn,bkg,880)
RETURN

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

; draw the top n rows of ghoul i, with
; its feet (or what there is) on the
; bottom row of its three
PROC DrawG(BYTE i,n)
  BYTE c,r
  CARD p
  ; c: its first character. Each
  ; picture is six in a run. 80 on:
  ; walking right. 104 on: walking
  ; left. 92 on: dancing.
  IF gst(i)=GDANCE THEN
    c=220+gfr(i)*6     ; inverse: 92+128
  ELSEIF gdir(i)=0 THEN c=80+gfr(i)*6
  ELSE c=104+gfr(i)*6 FI
  ; start n rows up from its feet;
  ; 40 characters to a screen row
  p=scrn+(gy(i)+3-n)*40+gx(i)
  FOR r=1 TO n DO
    Poke(p,c) Poke(p+1,c+1)
    p==+40 c==+2
  OD
RETURN

; put back the graveyard under ghoul i
PROC EraseG(BYTE i)
  BYTE r
  CARD o
  ; o: its place, both on the screen
  ; and in the copy kept in bkg
  o=gy(i)*40+gx(i)
  FOR r=1 TO 3 DO
    MoveBlock(scrn+o,bkg+o,2)
    o==+40
  OD
RETURN

; would ghoul i, at nx,ny, be standing
; on another one?
; (a ghoul is 2 wide and 3 high)
BYTE FUNC Blocked(BYTE i,nx,ny)
  BYTE j
  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 (1) FI
    FI
  OD
RETURN (0)

; one shambling step toward the girl
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 she is, in
  ; characters. Players count from
  ; 48 across and 32 down, with 4
  ; dots and 8 lines to a character.
  ; Ghouls keep out of the top three
  ; rows (sky and fence).
  tx=(lx-48) RSH 2 ty=(ly-32) RSH 3
  IF ty<3 THEN ty=3 FI
  ; how far off she is, each way
  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 five, a step in any
  ; direction. Otherwise close the
  ; bigger gap.
  IF Rand(5)=0 THEN   ; a lurch off course
    j=Rand(4)
    IF j=0 AND x<38 THEN nx=x+1
    ELSEIF j=1 AND x>0 THEN nx=x-1
    ELSEIF j=2 AND y<19 THEN ny=y+1
    ELSEIF j=3 AND y>3 THEN ny=y-1 FI
  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
  ; face the way it is going
  IF nx>x THEN gdir(i)=0
  ELSEIF nx<x THEN gdir(i)=1 FI
  ; move, if no other ghoul is there
  IF Blocked(i,nx,ny)=0 THEN
    EraseG(i) gx(i)=nx gy(i)=ny
  FI
  ; the other leg, and a wait before
  ; the next step
  gfr(i)==!1
  DrawG(i,3)
  gtm(i)=gdel+Rand(6)
RETURN

; every ghoul, every frame
; States: 0 none; 1-3 climbing out;
; GWALK; GDANCE; 6-8 sinking; GDAZE
; shoved; GGRAB holding her. gtm
; counts frames until its next move.
PROC Ghouls()
  BYTE i,s
  FOR i=0 TO MAXG-1 DO
    s=gst(i)
    IF s>0 THEN
      IF gtm(i)>0 THEN gtm(i)==-1
      ELSEIF s<3 THEN ; climbing out
        gst(i)=s+1 gtm(i)=20
        DrawG(i,s+1) Sfx(SRISE,10)
      ; fully out, or over a shove:
      ; start walking
      ELSEIF s=3 OR s=GDAZE THEN
        gst(i)=GWALK gtm(i)=gdel
      ELSEIF s=GWALK THEN StepG(i)
      ; dancing: gdn beats of 7
      ; frames, then walk again
      ELSEIF s=GDANCE THEN
        gdn(i)==-1
        IF gdn(i)=0 THEN gst(i)=GWALK FI
        gfr(i)==!1 gtm(i)=7
        DrawG(i,3)
      ELSEIF s=GGRAB THEN ; tugging at her
        gfr(i)==!1 gtm(i)=6
        DrawG(i,3)
      ELSE            ; sinking: 6, 7, 8
        ; show one row fewer each
        ; time, then free the slot
        EraseG(i)
        IF s=8 THEN gst(i)=0 ngh==-1
        ELSE
          DrawG(i,8-s)
          gst(i)=s+1 gtm(i)=6
        FI
      FI
    FI
  OD
RETURN

; now and then another one wakes up
PROC Spawn()
  BYTE i,g,x,y,px,py
  ; spn counts down the frames
  IF spn>0 THEN spn==-1 RETURN FI
  ; (if we give up below, try again
  ; in 15 frames)
  spn=15
  IF ngh>=maxg THEN RETURN FI
  ; i: the first free slot
  i=0
  WHILE gst(i)>0 DO i==+1 OD
  ; a grave at random. A ghoul's top
  ; row is one above the plot.
  g=Rand(8) x=gvx(g) y=gvy(g)-1
  IF Blocked(i,x,y)=1 THEN RETURN FI
  ; not right under her feet
  ; (px,py: the grave in player dots
  ; and lines, as she is)
  px=48+x*4 py=32+y*8
  IF lx+14>px AND px+14>lx
     AND ly+34>py AND py+34>ly
  THEN RETURN FI
  ; up it comes, facing the middle
  gx(i)=x gy(i)=y gfr(i)=0
  IF x>19 THEN gdir(i)=1
  ELSE gdir(i)=0 FI
  gst(i)=1 gtm(i)=20 ngh==+1
  DrawG(i,1) Sfx(SRISE,10)
  ; the first two of a try come up
  ; quickly, to get things going
  IF early>0 THEN
    early==-1 spn=30+Rand(30)
  ELSE spn=spdel+Rand(60) 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 b
  ; a picture is 78 bytes: 26 lines
  ; for each of the three players
  IF hdance>0 THEN
    ; six beats: claws out to the
    ; right, an arm up, claws out to
    ; the left, the other arm up, the
    ; crouch, and a spin to finish
    b=hdance RSH 3
    IF b>4 THEN s=hero+312
    ELSEIF b=4 THEN s=hero+156
    ELSEIF b=3 THEN s=herol+312
    ELSEIF b=2 THEN s=hero+234
    ELSEIF b=1 THEN s=hero+390
    ELSE s=hero+468 FI
  ELSEIF hpush>0 THEN ; shoving a ghoul
    IF hpdir=0 THEN s=hero+312
    ELSE s=herol+312 FI
  ELSEIF hdir=0 THEN
    s=hero+((hstep RSH 3)&1)*78
  ELSE
    s=herol+((hstep RSH 3)&1)*78
  FI
  ; players 0, 1 and 2 are at
  ; pmb+1024, +1280 and +1536. Each
  ; has a byte for each TV line, so
  ; adding hy puts him at that line.
  MoveBlock(pmb+1024+hy,s,26)
  MoveBlock(pmb+1280+hy,s+26,26)
  MoveBlock(pmb+1536+hy,s+52,26)
  ; and all three at the same dot
  hposp0=hx hposp1=hx hposp2=hx
RETURN

; the dance: every ghoul close enough
; has to join in for a while
PROC Dance()
  BYTE i,cx,cy,n
  n=0
  ; 48 frames of dance; the meter is
  ; spent
  hdance=48 groove=0 Sfx(SDANCE,24)
  ; cx,cy: where he is, in characters
  cx=(hx-48) RSH 2 cy=(hy-32) RSH 3
  ; ghouls within 14 columns and 9
  ; rows dance for 22 beats
  FOR i=0 TO MAXG-1 DO
    IF gst(i)=GWALK OR gst(i)=GDAZE
       OR gst(i)=GGRAB THEN
      IF gx(i)+14>cx AND cx+14>gx(i)
         AND gy(i)+9>cy AND cy+9>gy(i)
      THEN
        gst(i)=GDANCE gdn(i)=22 gtm(i)=0
        n==+1
      FI
    FI
  OD
  ; four or more dancing at once
  ; score 200 points
  IF n=0 THEN
    Msg("NOBODY IS WATCHING...")
  ELSEIF n<4 THEN
    Msg("THEY CAN'T HELP IT!")
  ELSE  ; a full chorus line
    score==+200 ShowScore()
    Msg("WHAT A SHOW! +200")
  FI
RETURN

; Move him, once a frame. He cannot
; walk while he dances. The button
; starts a dance, but only when the
; groove meter is full.
PROC Hero()
  BYTE s
  IF hpush>0 THEN hpush==-1 FI
  IF hdance>0 THEN hdance==-1
  ELSE
    ; stick bits: 8 right, 4 left,
    ; 1 up, 2 down (0 = pushed). One
    ; dot or two lines a frame,
    ; inside the graveyard.
    s=Stick(0)
    IF s<>15 THEN hstep==+1 FI
    IF (s&8)=0 AND hx<200 THEN
      hx==+1 hdir=0 FI
    IF (s&4)=0 AND hx>48 THEN
      hx==-1 hdir=1 FI
    IF (s&1)=0 AND hy>54 THEN hy==-2 FI
    IF (s&2)=0 AND hy<182 THEN hy==+2 FI
    IF Strig(0)=0 AND groove=GMAX THEN
      Dance() FI
  FI
  DrawHero()
RETURN

; she keeps a step behind him, and she
; is only half as quick
PROC Girl()
  BYTE tx
  CARD s
  ; tx: 10 dots behind his back
  IF hdir=0 THEN tx=hx-10
  ELSE tx=hx+10 FI
  IF tx<48 THEN tx=48 FI
  IF tx>200 THEN tx=200 FI
  ; she can't follow while one has her
  IF (frm&1)=0 AND held=0 THEN
    IF lx<tx THEN
      lx==+1 ldir=0 lstep==+1
    ELSEIF lx>tx THEN
      lx==-1 ldir=1 lstep==+1 FI
    IF ly<hy THEN ly==+2 lstep==+1
    ELSEIF ly>hy THEN ly==-2 lstep==+1 FI
  FI
  ; her picture: 52 bytes each, 26
  ; for player 3 (pmb+1792) and 26
  ; for the missiles (pmb+768)
  IF ldir=0 THEN s=girl ELSE s=girll FI
  s==+((lstep RSH 2)&1)*52
  ; held: her hands fly to her cheeks
  IF held>0 THEN s=girl+104 FI
  MoveBlock(pmb+1792+ly,s,26)
  MoveBlock(pmb+768+ly,s+26,26)
  ; the four missiles side by side
  hposp3=lx hposm3=lx hposm2=lx+2
  hposm1=lx+4 hposm0=lx+6
RETURN

; shove ghoul i away from him
PROC Push(BYTE i)
  BYTE nx,ny,s
  ; stick straight up or down sends
  ; it 3 rows that way. Otherwise it
  ; goes 4 columns to the side it is
  ; on. Never off the graveyard.
  nx=gx(i) ny=gy(i) s=Stick(0)
  IF s=14 THEN         ; straight up
    IF ny>5 THEN ny==-3 ELSE ny=3 FI
  ELSEIF s=13 THEN     ; straight down
    IF ny<16 THEN ny==+3 ELSE ny=19 FI
  ELSEIF 48+nx*4>=hx THEN
    IF nx<34 THEN nx==+4 ELSE nx=38 FI
  ELSE
    IF nx>4 THEN nx==-4 ELSE nx=0 FI
  FI
  ; (it stays put if another ghoul is
  ; in the way)
  IF Blocked(i,nx,ny)=0 THEN
    EraseG(i) gx(i)=nx gy(i)=ny
    DrawG(i,3)
  FI
  ; dazed for 50 frames
  gst(i)=GDAZE gtm(i)=50
  ; his claws go out toward it
  hpush=12
  IF 48+gx(i)*4>=hx THEN hpdir=0
  ELSE hpdir=1 FI
  ; 5 points for every shove
  score==+5 ShowScore() Sfx(SBUMP,8)
RETURN

; who is touching whom
; Called every frame. Also sets held,
; if any ghoul has hold of her.
PROC Bumps()
  BYTE i,px,py
  held=0
  FOR i=0 TO MAXG-1 DO
    IF gst(i)=GWALK OR gst(i)=GDANCE
       OR gst(i)=GGRAB THEN
      ; px,py: the ghoul in player
      ; dots and lines
      px=48+gx(i)*4 py=32+gy(i)*8
      ; is he touching it?
      IF hx+6>px AND px+6>hx
         AND hy+22>py AND py+22>hy
      THEN
        ; a dancing ghoul goes back
        ; into the ground: 50 points
        IF gst(i)=GDANCE THEN
          gst(i)=GSINK gtm(i)=0
          score==+50 ShowScore()
          Sfx(SSINK,20)
        ELSE
          ; one that had hold of her:
          ; 20 points, then the shove
          ; (5 more)
          IF gst(i)=GGRAB THEN
            score==+20
            Msg("THAT WAS CLOSE!")
          FI
          Push(i)
        FI
      FI
      ; a ghoul that reaches her takes
      ; hold: shove it off, quickly!
      IF gst(i)=GWALK AND lx+5>px
         AND px+5>lx AND ly+18>py
         AND py+18>ly
      THEN
        gst(i)=GGRAB gtm(i)=0
        Msg("SHE NEEDS YOU! QUICK!")
      FI
      IF gst(i)=GGRAB THEN held=1 FI
    FI
  OD
RETURN

; the groove meter and the clock
PROC Clock()
  ; the groove fills by one every
  ; other frame, but not in a dance
  IF groove<GMAX AND (frm&1)=0
     AND hdance=0
  THEN groove==+1 FI
  ShowGroove()
  ; while a ghoul holds her, grabt
  ; runs down, her dress flashes and
  ; a sound repeats. At 0 she is
  ; lost. Let go: grabt starts again.
  IF held=1 THEN
    grabt==-1
    Poke(707,$86+(grabt&8))
    IF (grabt&7)=0 THEN Sfx(SRISE,4) FI
    IF grabt=0 THEN caught=1 FI
  ELSE
    grabt=gtime Poke(707,$86)
  FI
  ; 60 frames make a second. The last
  ; 12 seconds colour the sky.
  ttick==+1
  IF ttick=60 THEN
    ttick=0 tleft==-1 ShowTime()
    IF tleft<12 THEN colbk=sky(tleft) FI
  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()
  sfxt==-1
  IF sfx=SBUMP THEN   ; a thump
    Sound(3,250-sfxt*12,10,10)
  ELSEIF sfx=SRISE THEN ; earth moving
    Sound(3,200+Rand(40),0,8)
  ELSEIF sfx=SSINK THEN ; going down
    Sound(3,200-sfxt*8,10,8)
  ELSE                ; the dance
    Sound(3,riff(5-(sfxt RSH 2)),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
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)
  Sound(1,lnote,10,lvol)
  Sound(2,enote,10,evol)
  ; 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. Each byte holds
; two samples of four bits. Bit 4 of
; AUDC makes a channel put out its
; volume as a steady level, so poking
; one sample after another draws the
; sound wave. Two channels at once are
; twice as loud. The empty loops set
; the pace: 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 for each night, and again
; after each lost try (for ease).
PROC NewNight()
  ; more ghouls at once
  maxg=2+night
  IF maxg>MAXG THEN maxg=MAXG FI
  ; fewer frames between their steps
  IF night<10 THEN gdel=34-night*3
  ELSE gdel=5 FI
  gdel==+ease*4
  ; a shorter wait for the next one
  IF night<9 THEN spdel=150-night*14
  ELSE spdel=24 FI
  ; less time to free her
  IF night<6 THEN gtime=90-night*12
  ELSE gtime=20 FI
  gtime==+ease*20
  ; and faster music
  tempo=8
  IF night>3 THEN tempo=7 FI
  IF night>6 THEN tempo=6 FI
RETURN

; the nights get longer, too
; tleft: seconds until dawn. Not set
; again after a lost try: the clock
; carries on from where it was.
PROC Dusk()
  IF night<6 THEN tleft=35+night*5
  ELSE tleft=60 FI
RETURN

; set everybody out for another try
PROC Round()
  BYTE i
  ; no ghouls; a full groove meter
  FOR i=0 TO MAXG-1 DO gst(i)=0 OD
  ngh=0 spn=20 caught=0 ttick=0
  early=2
  held=0 grabt=gtime
  hdance=0 hpush=0 groove=GMAX
  ; wipe the missiles and all four
  ; players (1280 bytes)
  Zero(pmb+768,1280)
  ; him in the middle, her beside
  hx=128 hy=118 hdir=0 hstep=0
  lx=114 ly=118 ldir=0 lstep=0
  ; the sky, if dawn is near
  IF tleft<12 THEN colbk=sky(tleft)
  ELSE colbk=0 FI
  Scene() ShowAll()
  Msg("PROTECT YOUR GIRL!")
  MusicOn()
RETURN

; a ghoul has her
PROC Lost()
  BYTE i
  SndRst()
  Msg("THE GHOULS GOT HER!")
  FOR i=0 TO 50 DO     ; her scream
    Frame()
    Sound(0,70-i+(i&1)*2,10,12)
    Sound(1,72-i,10,6)
    IF (i&2)=0 THEN colbk=$34
    ELSE colbk=0 FI
  OD
  SndRst() colbk=0
  ; 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(32+lives,23) Put(32)
RETURN

; the sun is up: every ghoul goes home
PROC Dawn()
  BYTE i,md
  CARD s
  SndRst()
  Msg("DAWN! SHE IS SAFE")
  ; they sink one after another
  FOR i=0 TO MAXG-1 DO
    IF gst(i)>0 THEN
      gst(i)=GSINK gtm(i)=i*5 FI
  OD
  ; she holds out her arms to him...
  IF lx<hx THEN s=girl ELSE s=girll FI
  s==+156
  MoveBlock(pmb+1792+ly,s,26)
  MoveBlock(pmb+768+ly,s+26,26)
  ; ...and he moonwalks, toward the
  ; middle: facing one way, gliding
  ; the other
  IF hx>124 THEN md=0 ELSE md=1 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
    ; once a frame: sink the ghouls
    ; and move him. Pictures from
    ; 546 on are the moonwalk.
    Ghouls()
    IF i<160 THEN
      IF md=0 THEN s=hero ELSE s=herol FI
      s==+546+((i RSH 3)&3)*78
      IF (i&1)=0 THEN
        IF md=0 AND hx>52 THEN hx==-1 FI
        IF md=1 AND hx<196 THEN hx==+1 FI
      FI
    ELSE ; and a spin to finish
      s=hero+468
    FI
    MoveBlock(pmb+1024+hy,s,26)
    MoveBlock(pmb+1280+hy,s+26,26)
    MoveBlock(pmb+1536+hy,s+52,26)
    hposp0=hx hposp1=hx hposp2=hx
  OD
  SndRst()
  ; 100 points times the night number
  score==+night*100 ShowScore()
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 two")
  Position(4,5)
  PrintD(6,"{inv}PROTECT YOUR{/inv}")
  Position(2,6)
  PrintD(6,"{inv}GIRLFRIEND FROM{/inv}")
  Position(3,7)
  PrintD(6,"{inv}GRISLY GHOULS!{/inv}")
  Position(4,9) PrintD(6,"press start")
  Position(0,11)
  PrintD(6,"{inv}designed 1984 d.lutz{/inv}")
  ; play the tune until START
  MusicOn() tempo=8
  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
    ; wins/3+1: the night number
    night=wins/3+1 NewNight() Dusk()
    DO
      Round()
      DO
        ; one TV frame
        Frame()
        Hero() Girl() Ghouls() Spawn()
        Bumps() Clock() Music()
      UNTIL caught=1 OR tleft=0 OD
      IF caught=1 THEN
        ; a ghoul kept hold of her: a
        ; life lost. The next try is
        ; easier, on the same clock.
        Lost()
        IF ease<2 THEN ease==+1 FI
        NewNight()
      ELSE
        ; the clock ran out: a win
        Dawn()
        ; run from the title program:
        ; one more screen won; go back
        IF chain=1 THEN
          wins==+1 result=1 Next()
          RETURN
        FI
        ; by itself: on to a harder,
        ; longer night
        wins==+3 ease=0
        night=wins/3+1 NewNight() Dusk()
      FI
    UNTIL lives=0 OD
    GameOver()
    IF chain=1 THEN RETURN FI
  OD
RETURN
