#==============================================================
#
repeat
  local tmp, obj.ptr
#
# Deal with any pending queries -- this aviods using the QUERY directive,
# in order to make the game compatible with single turn build modes.
#
  ifne context, none
    call handle.query   # This call may return instead of quitting
    set context, none   # If it does, avoid looping and ensure...
    quit                # ...the persistent state image gets updated
  fin
#
# Now do whatever pre-input housekeeping required.
#
# Does he need a hint? These are offered spontaneously -- cannot be asked for.
#
  call hint.logic
#
# Fiddle portable objects in repository. A picked up object stays real.
#
  ifeq stage, in.repository
    itobj obj.ptr, portable
      ifloc obj.ptr, inhand
        unflag obj.ptr, faked # Has been picked up -- no longer faked
      otherwise
      ifhere obj.ptr
        and
      ifflag obj.ptr, faked
        apport obj.ptr, limbo # Still a fake -- get rid of it
      fin
    fin
  fin
#
# Is player's move blocked by a dwarf?
#
  ifflag status, moved
    call move.blocked?
  fin
#
# Fiddle the steps going down or up, as appropriate.
#
  ifat pit, mists
    ifhave gold
      apport steps, limbo
    else
      apport steps, pit   # Steps are schizoid, so will be at mists too
    fin
  fin
#
# Can we see anything?
#
  call is.it.dark?       # Sets the LIGHT.HERE bit of te STATUS variable
  ifflag status, moved
    call describe.here
  fin
#
# Make sure oil and water are actually where they are supposed to be!
#
  apport water, limbo
  apport oil, limbo
  ifflag here, h2o.here
    apport water, here
  otherwise
  ifflag here, oil.here
    apport oil, here
  fin
  ifhave bottle
    ifeq bottle, full.of.water
      get water
    otherwise
    ifeq bottle, full.of.oil
      get oil
    fin
  fin
#
# How is that lamp doing?
#
  ifeq lamp, switched.on
    ifeq lamplife, 0
      ifeq lamp, switched.on
        set lamp, switched.off
      fin
    fin
    iflt lamplife, 0
      and
    ifinrange here, road, depression  # Need to check for game closing??
      say call.it.a.day
      call finis
    fin
    iflt lamplife, 30                 # Swap batteries
      ifnear batteries, charged
        set batteries, discharged
        add lamplife, 2500
        say lamp.refuel
      otherwise
      ifeq batteries discharged
        say lamp.getting.dim
      otherwise
      ifloc batteries, limbo
        say lamp.is.dim
      else
        say lamp.batteries
      fin
    fin
  fin
#
# Imply that *all* directions can be abbreviated.
#
  iftyped west
    add w.is.west, 1
    ifeq w.is.west, 10
      say w.will.do
    fin
  fin
#
# Has he found all that can be found?
#
  ifeq still.to.find, 0
    ifflag clock, moved # So, OK, I am using a nominally STATUS flag here
    else
      set clock, 30
      flag clock, moved         # Closure clock now running
      ifgt treasures.gone, 0
        and
      ifgt lamplife, 35
        set lamplife, 35
      fin
    fin
  else
    call treasure.report        # How's that treasure finding going?
  fin
#
# Is the closure clock running? It only runs inside the cave!
#
  ifgt clock, 0
    and
    not
  ifinrange here, road, depression
    sub clock, 1
    ifeq clock, 0
      call cave.closure
    fin
  fin
#
# Attend to the NPC stuff
#
  call npc.stuff
#
#----------------------------------------------------------
#
# Main input section.
#
repeat
  call wiz.reports      # Debug mode reports
  unflag status, moved
  input
  add turns, 1
  ifeq lamp, switched.on
    sub lamplife, 1
  fin
#
#----------------------------------------------------------
#
# Do any required command fiddling before actually processing the command.
#
repeat 
  ifeq arg1, badword
    or
  ifeq arg1, ambigword
    or
  ifeq arg1, ambigtypo
    quip nocomprende
  fin
  ifgt status, 1
    ifeq arg2, badword
      or
    ifeq arg2, ambigword
      or
    ifeq arg2, ambigtypo
      and
      not
    ifany say, save, restore, delete
      quip nocomprende
      flush
    fin
  fin
#
# Dwarf knives don't actually exist
#
  ifkey knife
    ifgt dattack, 0
      and
      not
    ifflag hints, knives.noted
      flag hints, knives.noted
      quip knives.go.poof
    fin
    quip i.dont.see, knife
  fin
#
# Ignore redundant GO.
#
  ifeq status, 2
    and
  ifkey go
    exec 29 # Swaps ARG1 and ARG2
    set status, 1
  fin
#
# Pretend the troll is not here when he is not described as here.
#  
  ifkey troll
    and
  ifnear troll
    and
  ifne, troll, wants.treasure
    quip i.dont.see, arg1
  fin
#
# Check for an interrupted FEE, FIE... series.
#
  ifany fee, fie foe foo, fum
  else
    set foobar, 0
  fin
#
#
#----------------------------------------------------------
#
# Now actually process the pleayer's command.
#
repeat
  local the.it
#
# Handle uses of IT
#
  ifis arg1, it
    or
  ifis arg2, it
    and
  ifgt it, 0
    set the.it, it
    fakecom it, the.it
  otherwise
  ifeq status, 2
    and
  ifflag arg2, portable
    and
    not
  ifdoall
    set it, arg2
  fin
#
# Some of these responses ought to be location specific, but they are not
# in the original game.
#
  respond help, help.spiel
  respond information, info.spiel
  respond abracadabra, oldfashioned.magic
  respond fuck, no.fucks
  respond dig, need.shovel
  respond trees, in.forest
  respond lost, i.am.confused.too
  respond mist, this.is.mist
  respond stop, use.quit
  respond swim, dunno.hao
  ifkey enter
    and
  ifany stream, water
    ifflag here, h2o.here
      quip wet.feet
    else
      quip where?
    fin
  fin
#
# Finally, it's time to look at the player's command more seriously.
#
  call here      # Things that happen at the current locations
  fakecom plover, emerald
  call arg1      # Things that happen in response to the player's command
  call bailout   # And if all else fails...
# 
######################################################################
