# NPC.library

string npc_directions "north" "south" "east" "west"
string npc_directions "northeast" "northwest" "southeast" "southwest"
string npc_directions "up" "down" "in" "out"

string npc_opposite "south" "north" "west" "east"
string npc_opposite "southwest" "southeast" "northwest" "northeast"

string npc_leaves " heads off to the "
string npc_leaves " walks "
string npc_leaves " heads "
string npc_leaves " leaves to the "

string npc_arrives " enters from the "
string npc_arrives " arrives from the "
string npc_arrives " arrives here from the "
string npc_arrives " approaches from the "

integer npc_index

# THIS FUNCTION PUSHES THE SPECIFIED NPC TOWARDS THE SPECIFIED LOCATION

{+push_to #ARGS: <npc<destination
npc_to npc_index arg[0](parent) arg[1]
if npc_index != -1
   execute "+push_object<arg[0]<npc_index"
else
   return false
endif
}

# THIS FUNCTION MOVES THE SPECIFIED NPC IN A RANDOM DIRECTION

integer npc_loc

{+wander #ARGS: <npc
set npc_loc = arg(parent)

set npc_index = 0
set COUNTER = 0
repeat
   ifall npc_loc(COUNTER) > 0 : npc_loc(COUNTER) < objects
      set npc_index + 1
   endif
   set COUNTER + 1
until COUNTER = 10

if npc_index = 0
   return
endif

# WHICH EXIT SHALL WE CHOOSE...
set max_rand = npc_index
set npc_index = random

set COUNTER = 0
repeat
   ifall npc_loc(COUNTER) > 0 : npc_loc(COUNTER) < objects
      set npc_index - 1
      if npc_index = 0
         #...AH, THIS ONE!
         execute "+push_object<arg<COUNTER"
         return
      endif
   endif
   set COUNTER + 1
until COUNTER = 10
}

{+push_object #ARGS: <object<direction
if player(parent) = arg[0](parent)
   if here hasnt DARKNESS
      write "^" arg[0]{The}
      execute +to_direction<arg[1]
   else
      write "You hear the footsteps nearby.^"
   endif
endif
set UTILS_temp = arg[0](parent)
set UTILS_temp = UTILS_temp(arg[1])
if UTILS_temp < 1 : UTILS_temp > objects
   return false
endif
set arg[0](parent) = UTILS_temp
if player(parent) = arg[0](parent)
   if here hasnt DARKNESS
      write  "^" arg[0]{The}
      execute +from_direction<arg[1]
   else
      write "You hear the footsteps nearby.^"
   endif
endif
}

{+to_direction
set max_rand = 4
set npc_index = random
set npc_index - 1
if arg[0] = up
   write " heads up from here.^"
   return
endif
if arg[0] = down
   write " heads down from here.^"
   return
endif
write npc_leaves[npc_index] npc_directions[arg] ".^"
}

{+from_direction
set max_rand = 4
set npc_index = random
set npc_index - 1
if arg[0] = up
   write " enters from below.^"
   return
endif
if arg[0] = down
   write " enters from above.^"
   return
endif
write npc_arrives[npc_index] npc_opposite[arg] ".^"
}
