Ensure for NPCs

Hi everyone!

When trying to write some code for NPCs I discovered that there isn’t much in the way of nice checking and ensuring predicates like there are for the player. So I tried writing an ensure predicate for an NPC, modeled after the corresponding predicate for the player:

(ensure $object is held by $actor)
 (if)
  ($object is #heldby $actor)
 (then)
  %% do nothing
 (elseif)
  ~($actor can not remove $object)
 (then)
  (let $actor remove $object)
 (elseif)
  ~($actor can not take $object)
 (then)
  (let $actor take $object)
 (else)
  (fail)
 (endif)

I then wrote several “can not X” predicates; no guarantee that these cover all cases:

($actor can not take $object)
 ($actor is in room $room)
 {
  ~($object is in room $room)
 (or)
  ($object is hidden)
 (or)
  ($object is #in $container)
  ($container is $closed)
 (or)
  ($object is #heldby $)
 (or)
  ($object is #wornby $)
 (or)
  (fine where it is $object)
 (or)
  ($object is #partof $)
 (or)
  ~(item $object)
 } 
($actor can not wear $object)
 {
  ~($object is #heldby $actor)
 (or)
  ~(wearable $object)
 }
($actor can not drop $object)
 ~($object is #heldby $actor)
($actor can not remove $object)
 ~($object is #wornby $actor)
($actor can not put $object $relation $parent)
 ($actor is in room $room)
 {
  ~($object is in room $room)
 (or)
  ~($parent is in room $room)
 (or)
  ~($object is #heldby $actor)
 (or)
  ($object = $parent)
 (or)
  ($relation = #in)
  ~(container $parent)
 (or)
  ($relation = #on)
  ~(supporter $parent)
 (or)
  ($relation = #in)
  ($parent is closed)
 (or)
  ~($relation is one of [#in #on])
 }
($actor can not open $object)
 ($actor is in room $room)
 {
  ~($object is in room $room)
 (or)
  ~(openable $object)
 (or)
  ~($object is closed)
 (or)
  ($object is locked)
 }
($actor can not close $object)
 ($actor is in room $room)
 {
  ~($object is in room $room)
 (or)
  ~(openble $object)
 (or)
  ~($object is open)
 }

I think something like these in the standard library would make doing non-trivial things with NPCs a lot easier and less error prone. Thoughts?

1 Like