#====================================================================
#
proc npc.stuff
  local obj.ptr
  apport dwarf, limbo
  set dwarf 0,
#
  ifinrange here, road, pit   # Player outside the cave -- NPCs wait
    or
  iflt daggro, 0              # No NPCs in the end game
     proceed
  fin
#
# Player's deep enough in the cave. Unfreeze NPCs
#
  ifeq daggro, 0
    set daggro, 1             # Permit dwarf encounter
    proceed
  fin
#
# Check for 1st dwarf encounter.
#
  ifeq daggro, 1
    and
  chance 5
    iterate obj.ptr, dwarf0, pirate
      ifhere obj.ptr
        apport obj.ptr, nugget.room
      fin
    fin
#
# Commence hostilities!
#
    call meet.first.dwarf
    proceed
  fin
  ifge daggro, 2
    call move.npcs
    ifgt dattack, 0
      ifeq daggro, 2
        set daggro, 3           # Dwarves now allowed to kill!
      fin
      say knives.thrown, dattack
      ifeq dstick, 0
        say knives.miss, dattack
        proceed
      fin
      ifgt dstick, 0
        say knives.got.you, dstick
        call coroner
      fin
    fin
  fin
#
#=====================================================================
#
proc meet.first.dwarf
  local obj.ptr, dc
  set daggro,2              # Activate dwarfs
  apport axe, here
  say first.dwarf
#  say axe                  # The original game does not!
  flag axe, seen
  flag dwarf, seen
  iterate obj.ptr, dwarf0, dwarf4
    ifhere obj.ptr
      apport obj.ptr, nugget.room
    fin
  fin
#
#=====================================================================
#
proc move.npcs
  local dnew, atck, dfnc, dt
  local dcur, dloc, dold
  set dattack, 0
  set dstick, 0
  lda dt, dthere           # Point at NPC last locations array
  iterate dcur, dwarf0, pirate   # Loop through NPCs
    locate dloc, dcur       # NPC's current location
    eval dold, dt           # NPC's previous location
    deposit dt, dloc        # Remember it for the next command loop!
    add dt, 1
    ifis dloc, ylem
      continue              # Dead ones do not do anything
    fin
    ifeq dloc, there        # Is NPC THERE?
      and
    ifflag status, moved    # And has the player just moved from THERE?
      set dnew, here        # Then the NPC is following
    otherwise
    ifeq dloc, here         # Is the NPC already HERE?
      and
      not
    ifflag status, moved    # The player has not moved,
      set dnew, here        # so the NPC does not move either
    else
      call new.dloc, dcur, dold   # Choose a random move for this NPC
      set dnew, result
      ifis dcur, pirate
        and
      ifflag dnew, no.pirate  # The pirate does not spontaneously go here,
        set dnew, dloc
      fin
    fin
#
    ifne dloc, dnew           # NPC has moved
      apport dcur, dnew       # Move the NPC (maybe)
    fin
#
# Pirate actions are special.
#
    ifis dcur, pirate
      and 
    ifhere pirate             # Moved to HERE?
      call pirate.stuff, dloc
      continue
    fin
#
# Check for dwarf attack.
#
    ifeq dnew, here       # There is a little dwarf here!
      add dwarf, 1
      ifeq dnew, dloc     # Has the dwarf moved?
        add dattack, 1    # Non-moving dwarves attack
        set atck, daggro
        sub atck, 2
        multiply atck, 95 # That's how good the attack is
        random dfnc, 1000 # And this is how good player's defence is
        iflt dfnc, atck
          add dstick, 1   # Another hit!
        fin
      fin
    fin
  fin
  ifgt dwarf, 0
    apport dwarf, here
    say dwarf
  fin
#
#==================================================================
#
proc move.blocked?
  local npc, dl, dx, dt
  lda dt, dthere
# say "f:Player: $ => ", there
# say "$", here
  iterate npc, dwarf0, dwarf4
    locate dl, npc
    eval dx, dt
    add dt, 1
# set entname, npc
# say "f:{entname}: $ => ", dx
# say "$", dl
    ifeq dl, there
      and
    ifeq dx, here
      set here, there
      unflag status, moved
      say dwarf.blocks
    fin
  fin
#
#==================================================================
#
proc treasure.report
  local obj.ptr
  set still.to.find, 0
  set treasures.gone, 0
  itobj obj.ptr, valued
    ifflag obj.ptr, seen
      or
    ifloc obj.ptr, limbo     # Chest starts at limbo!
    else
      add still.to.find, 1   # Counts treasures not yet seen
    fin
    ifloc obj.ptr, ylem
      add treasures.gone, 1
      continue
    fin
  fin
#
#==================================================================
#
proc pirate.stuff, dloc
  local obj.ptr
  local swag
  ifflag chest, seen
    apport pirate, ylem   # No more pirate!
  fin
  itobj obj.ptr, valued      # Consider treasures
    ifloc obj.ptr, here, inhand
      ifis obj.ptr pyramid   # Don't solve the pyramid problem for the player!
        or
      ifis obj.ptr emerald   # Ditto for the emerald
        and
      ifat plover, dark
      else
        add swag, 1
        apport obj.ptr, mazea.114
      fin
    fin
  fin
  ifhere pirate
    ifgt swag, 0
      or
    ifeq still.to.find, 0
      apport pirate, mazea.114
      ifloc chest, limbo
        apport chest, mazea.114
        apport message, mazed.140
      fin
      ifgt swag, 0
        say pirate.robs
      else
        say pirate.runs
        apport pirate, ylem
      fin
    otherwise
    ifeq swag, 0
      and
    ifne here, dloc
      and
    chance 20
      say rustling
    fin
  fin
#
#=====================================================================
#
proc new.dloc, dcur, dold
  local count
  local tmp, tmp.ptr
  local dloc
#
  locate dloc, dcur
  set result, dloc
#
  ifis dloc, nugget.room
    lda result, mists
  otherwise
  ifis dloc, east.pit
    lda result, east.pit.end
  otherwise
  ifis dloc, east.window
    lda result, y2
  otherwise
  ifis dloc, streampit
    lda result, brink
  otherwise
  ifis dloc, mazea.46
    lda result, mazea.45
  otherwise
  ifis dloc, mazea.47
    lda result, mazea.45
  otherwise
  ifis dloc, mazea.48
    lda result, mazea.44
  otherwise
  ifis dloc, mazea.54
    lda result, mazea.53
  otherwise
  ifis dloc, mazea.56
    lda result, mazea.55
  otherwise
  ifis dloc, mazea.58
    lda result, mazea.by.pit
  otherwise
  ifis dloc, dead.end.1
    lda result, crossover
  otherwise
  ifis dloc, dead.end.2
    lda result, low
  otherwise
  ifis dloc, tight.dead.end
    lda result, wide.n/s.canyon
  otherwise
  ifis dloc, dead.end.3
    lda result, tall.e/w.canyon
  otherwise
  ifis dloc, mazea.81
    lda result, mazea.80
  otherwise
  ifis dloc, mazea.82
    lda result, mazea.44
  otherwise
  ifis dloc, mazea.85
    lda result, mazea.83
  otherwise
  ifis dloc, mazea.86
    lda result, mazea.52
  otherwise
  ifis dloc, mazea.87
    lda result, mazea.45
  otherwise
  ifis dloc, dark
    lda result, plover
  otherwise
  ifis dloc, arched
    lda result, shell
  otherwise
  ifis dloc, culdesac
    lda result, ragged.corridor
  otherwise
  ifis dloc, witts.end
    lda result, anteroom
  otherwise
  ifis dloc, west.window
    lda result, secret.junction
  otherwise
  ifis dloc, mazea.114
    lda result, mazea.84
  otherwise
  ifis dloc, soft
    lda result, swiss
  otherwise
  ifis dloc, reservoir
    lda result, mirror.canyon
  otherwise
  ifis dloc south.side.chamber
    lda result, mtking
  else
    iterate count, 1, 10
#
# First, selections which include transits disregarding obstacls
# E.g. the absence of the fissure bridge.
#
      ifis dloc, west.pit
        randsel result, west.pit.end, narrow.corridor # Added narrow.corridor
      otherwise
      ifis dloc, east.of.fissure
        randsel result, mists, west.of.fissure # Added west.of.fissure
      otherwise
      ifis dloc, west.of.fissure
        randsel result, west.end.of.mists, east.of.fissure # Added east.of.fissure
      otherwise
      ifis dloc, sw.of.chasm
        randsel result, sloping, ne.of.chasm # Added ne.of.chasm
      otherwise
#
# Then, selections which include travel options
#
      ifis dloc, y2
        randsel result, low.n/s.passage, jumble, east.window, plover
      otherwise
      ifis dloc, dirty
        randsel result, brink, low.n/s.passage, dusty, bedquilt
      otherwise
      ifis dloc, dusty
        randsel result, dirty, complex, bedquilt
      otherwise
      ifis dloc, bedquilt
        randsel result, complex, swiss, secret.n/s.canyon.2, dusty, low, secret.junction, anteroom, slab
      otherwise
      ifis dloc, swiss
        randsel result, bedquilt, east.pit.end, soft, tall.e/w.canyon, oriental
      otherwise
      ifis dloc, low
        randsel result, sloping, dead.end.2, oriental, bedquilt
      otherwise
      ifis dloc, plover
        randsel result, alcove, dark, y2
      otherwise
      ifis dloc, anteroom
        randsel result, complex, witts.end, bedquilt
      otherwise
#
      ifis dloc, mists
        randsel result, nugget.room, east.of.fissure, mtking
      otherwise
      ifis dloc, mtking
        randsel result, mists, low.n/s.passage, south.side.chamber, west.side.chamber, secret.e/w.canyon
      otherwise
      ifis dloc, west.pit.end
        randsel result, east.pit.end, slab, west.pit
      otherwise
      ifis dloc, low.n/s.passage
        randsel result, mtking, y2, dirty
      otherwise
      ifis dloc, west.side.chamber
        randsel result, mtking, crossover
      otherwise
      ifis dloc, jumble
        randsel result, y2, mists
      otherwise
      ifis dloc, brink
        randsel result, dirty, streampit
      otherwise
      ifis dloc, west.end.of.mists
        randsel result, mazea.42, west.of.fissure, long.hall.east
      otherwise
      ifis dloc, mazea.42
        randsel result, west.end.of.mists, mazea.43, mazea.45, mazea.80
      otherwise
      ifis dloc, mazea.43
        randsel result, mazea.42, mazea.44, mazea.45
      otherwise
      ifis dloc, mazea.44
        randsel result, mazea.43, mazea.48, mazea.50, mazea.82
      otherwise
      ifis dloc, mazea.45
        randsel result, mazea.42, mazea.43, mazea.46, mazea.47, mazea.87
      otherwise
      ifis dloc, mazea.49
        randsel result, mazea.50, mazea.51
      otherwise
      ifis dloc, mazea.50
        randsel result, mazea.44, mazea.49, mazea.51, mazea.52
      otherwise
      ifis dloc, mazea.51
        randsel result, mazea.49, mazea.50, mazea.52, mazea.53
      otherwise
      ifis dloc, mazea.52
        randsel result, mazea.50, mazea.51, mazea.53, mazea.55, mazea.86
      otherwise
      ifis dloc, mazea.53
        randsel result, mazea.51, mazea.52, mazea.54
      otherwise
      ifis dloc, mazea.55
        randsel result, mazea.52, mazea.55, mazea.56, mazea.by.pit
      otherwise
      ifis dloc, mazea.by.pit
        randsel result, mazea.55, mazea.58, mazea.83, mazea.84
      otherwise
      ifis dloc, long.hall.east
        randsel result, west.end.of.mists, long.hall.west, crossover
      otherwise
      ifis dloc, long.hall.west
        randsel result, long.hall.east, crossover
      otherwise
      ifis dloc, crossover
        randsel result, long.hall.east, dead.end.1, west.side.chamber, long.hall.west
      otherwise
      ifis dloc, complex
        randsel result, dusty, bedquilt, shell, anteroom
      otherwise
      ifis dloc, east.pit.end
        randsel result, swiss, west.pit.end, east.pit
      otherwise
      ifis dloc, slab
        randsel result, west.pit.end, bedquilt, secret.n/s.canyon.1
      otherwise
      ifis dloc, secret.n/s.canyon.1
        randsel result, slab, secret.n/e.canyon, mirror.canyon, reservoir
      otherwise
      ifis dloc, secret.n/s.canyon.2
        randsel result, secret.junction, bedquilt, top.of.stalactite
      otherwise
      ifis dloc, secret.junction
        randsel result, bedquilt, secret.n/s.canyon.2, west.window
      otherwise
      ifis dloc, secret.n/e.canyon
        randsel result, secret.e/w.canyon, secret.n/s.canyon.1
      otherwise
      ifis dloc, secret.e/w.canyon
        randsel result, mtking, secret.n/e.canyon, wide.n/s.canyon
      otherwise
      ifis dloc, wide.n/s.canyon
        randsel result, tight.dead.end, tall.e/w.canyon
      otherwise
      ifis dloc, tall.e/w.canyon
        randsel result, wide.n/s.canyon, dead.end.3, swiss
      otherwise
      ifis dloc, mazea.80
        randsel result, mazea.42, mazea.81
      otherwise
      ifis dloc, mazea.83
        randsel result, mazea.by.pit, mazea.84, mazea.85
      otherwise
      ifis dloc, mazea.84
        randsel result, mazea.by.pit, mazea.83, mazea.114
      otherwise
      ifis dloc, oriental
        randsel result, swiss, low, misty
      otherwise
      ifis dloc, misty
        randsel result, oriental, alcove
      otherwise
      ifis dloc, alcove
        randsel result, misty, plover
      otherwise
      ifis dloc, shell
        randsel result, arched, ragged.corridor, complex
      otherwise
      ifis dloc, ragged.corridor
        randsel result, shell, culdesac
      otherwise
      ifis dloc, mirror.canyon
        randsel result, secret.n/s.canyon.1, reservoir
      otherwise
      ifis dloc, top.of.stalactite
        randsel result, secret.n/s.canyon.2, mazea.50, mazea.53, mazea.45
      otherwise
      ifis dloc, sloping
        randsel result, low, sw.of.chasm
      otherwise
      ifis dloc, warmjunctn
        randsel result, fork, breathtaker, boulders
      otherwise
#
# The remainder is locations only accessible to NPCs if they ignore
# travel restrictions (such as he lack of the fissure bridge).
#
      ifis dloc, breathtaker
        randsel result, warmjunctn, fork
      otherwise
      ifis dloc, limestone
        randsel result, fork, barren, breathtaker
      otherwise
      ifis dloc, cavern
        randsel result, immense.n/s.passages, giant.room, incline
      otherwise
      ifis dloc, narrow.corridor
        randsel result, west.pit, giant.room
      otherwise
      ifis dloc, bear.room
        randsel result, barren, fork, breathtaker
      otherwise
      ifis dloc, boulders
        randsel result, warmjunctn, fork, breathtaker
      otherwise
      ifis dloc, immense.n/s.passages
        randsel result, giant.room, cavern
      otherwise
      ifis dloc, fork
        randsel result, corridor, warmjunctn, limestone, barren, breathtaker
      otherwise
      ifis dloc, ne.of.chasm
        randsel result, corridor, sw.of.chasm, fork, breathtaker, barren
      otherwise
      ifis dloc, incline
        randsel result, cavern, low
      otherwise
      ifis dloc, giant.room
        randsel result, narrow.corridor, immense.n/s.passages
      otherwise
      ifis dloc, barren
        randsel result, fork, bear.room, breathtaker
      otherwise
      ifis dloc, corridor
        randsel result, ne.of.chasm, fork, breathtaker, barren
      fin
#
      ifis dcur, pirate
        and
      ifflag result, no.pirate
        set result, dloc
      otherwise
      ifne result, dloc
        and
      ifne result, dold
        break
      fin
    fin
  fin
#        
#####################################################################
