Daily Nugget # 8: Selecting things according to some criteria

Hi All,

Still trucking on the rocky Inform7 tracks… I had a discussion with @Zed on how to best let an NPC randomly select things according to some criteria (In my WIP I will have an NPC following the player and examining random things not already examined in the location to add some “flavor” to the game.). My original approach was basically get a list of all items in the location, and then use a loop looking for a thing fulfilling my criteria. But Inform7 can do this better :stuck_out_tongue: hence this post.

Defining unexamined things

The simplest definition is this:

definition: a thing is unexamined if we have not examined it.

Here we can be the player or any NPC, Inform does not track who did the examining, just that someone did. But in my game the NPC wants to examine everything himself. So we need to add our own tracking of state:

A thing can be npc-examined.

After an actor examining something:
	if the actor is not the player:
		now the noun is npc-examined; [ remember the NPC examined it ]
	continue the action. [ We need this otherwise the Report stage is skipped ]

definition: a thing is unexamined if it is not npc-examined;

Defining examinable things

Here are my criteria for examinable things:

  1. they must be visible
  2. they must be unexamined (the NPC will look at each thing only once)
  3. they must not be part of another thing (we expect the player to examine parts of things)
  4. they must not be an actor (do not look at anyone)
  5. they must not be anything an actor wears or carries (do not look at anyone’s belongings)
  6. I want the NPC to examine containers/supporters before examining their contents

These criteria map to the following conditions:

  1. visible (the NPC follows the player, so visible to the player implies visible to the NPC)
  2. unexamined (the definition given above)
  3. incorporated by nothing (thx @Zed, see also 13.4. To carry, to wear, to have (ganelson.github.io)
  4. not a person
  5. not enclosed by a person
  6. not enclosed by an unexamined thing

We can now build our definition:

definition: a thing is examinable if it is visible
and it is unexamined and it is incorporated by nothing
and it is not a person and it is not enclosed by a person
and it is not enclosed by an unexamined thing.

Putting it all together I can then build a simple Every Turn rule for the NPC:

Laboratory is a room. "Many devious experiments are conducted here."

[ add any testing items in here ... ]

The npc is a man in Laboratory.

every turn:
	let item be a random examinable thing;
	if item is not nothing, try the npc examining item;

So now I have some NPC agency in that the NPC will examine everything in sight, one thing at a time until everthing has been examined. If the player opens any opaque container in the location, then the NPC will be able to examine its contents.

This is only a very small part of the NPC agency I am creating, but I hope this post may be useful to others.