# Testing if a edible thing is near a npc

``````		If hunger of selected-space-alien is greater than max-hunger of selected-space-alien:
Try selected-space-alien eating a random edible thing;``````

This seem to work, but I would prefer to have the IF condition check if there is a edible thing near the NPC. Any way to do that?

Here’s some working code:

[code]Section - Persons

A person has a number called hunger. The hunger of a person is usually 0.
A person has a number called max-hunger. The max-hunger of a person is usually 4.

A space-alien is a kind of man.

After an actor eating something:
Now the hunger of actor is 0;
Continue the action.

Section - Every Turn

An every turn rule:
Repeat with selected-space-alien running through space-aliens:
Increase hunger of selected-space-alien by a random number from 0 to 1;
If hunger of selected-space-alien is greater than max-hunger of selected-space-alien:
Try selected-space-alien eating a random edible thing;

Section - The World

Woods is a room.
The description of Woods is “There are trees here. The garden is to the north.”
Jack is a space-alien in Woods.
The player is jack.

John is a space-alien in Woods.
John carries a book.

Albert is a space-alien in Woods.
Ken is a space-alien in Woods.
Pete is a man in Woods.

The apple is a thing in Woods. The apple is edible.
The cow is a thing in Woods. The cow is edible.
The moon is a thing in Woods. The moon is edible.
The rat is a thing in Woods. The rat is edible.

Garden is a room. Garden is north of Woods.
The description of garden is “There is no trees here. The woods is to the south.”
[/code]

Yeah, you can test that with “can touch”:

``` An every turn rule: Repeat with selected-space-alien running through space-aliens: Increase hunger of selected-space-alien by a random number from 0 to 1; If hunger of selected-space-alien is greater than max-hunger of selected-space-alien: if selected-space-alien can touch an edible thing: Try selected-space-alien eating a random edible thing that can be touched by selected-space-alien;```

(We could’ve said “If selected-space alien can touch an edible thing (called food): [line break] try selected-space-alien eating food.” But that would mean that they alien always tries to eat the first edible thing it can touch that’s defined in the source code order, which isn’t what you want.)

You can stick it in a rule heading, like so:

[code]Section - Persons

Hunger is a kind of value. The hungers are sated, peckish, hankering, starving and ravenous.
A space-alien is a kind of man. A space-alien has a hunger.

After an actor eating something:
Now the actor is sated;
Continue the action.

Section - Every Turn

Every turn:
repeat with alien running through not ravenous space-aliens:
if a random chance of 1 in 2 succeeds:
now the hunger of the alien is the hunger after the hunger of the alien.

Every turn when a ravenous space-alien (called Kenneth) can see an edible thing (called meal):
if Kenneth is not the player, say “[Kenneth] reaches toward [the meal].”;
try kenneth eating the meal.[/code]

This won’t check to see whether the thing being eaten is something the alien can take, by the way; if you have the apple and the moon is lying on the ground, another alien might try and fail to eat the apple rather than eating the moon.

Also, unsuccessful eatings by other aliens might not get reported, because the “can’t eat other people’s food” rule doesn’t print anything when the actor isn’t the player. I added this to make my testing livelier:

[code]Check an actor eating (this is the new can’t eat other people’s food rule):
if the noun is enclosed by a person (called the owner) who is not the actor:
if the actor is the player:
say “[The owner] [might not appreciate] that.” (A);
otherwise if the player can see the actor:
say “[The actor] gazes hungrily at [the noun].” (B);
stop the action.

The new can’t eat other people’s food rule is listed instead of the can’t eat other people’s food rule in the check eating rulebook.[/code]

I noticed that, but I thought it was too funny not to keep that behavior. By the same token, this code has aliens taking turns eating things, which reads a lot more natural than if you suddenly have three aliens embark on a feeding frenzy.

Oh, I think I get it now: Since Inform 7 is based on natural language, it is more compatible with language-values, like “sated, peckish, hankering, starving and ravenous”.

… still, I don’t think I’m quite ready to abandon numbers, even if they don’t fit quite so well with Inform.

Thank for good advice! This is how it looks now: (not sure if the ‘and’ in the if-condition is okay, but it seem to work.)

[code]Definition: A person is starving if his hunger is greater than his max-hunger.

An every turn rule:
Repeat with selected-space-alien running through space-aliens:
Increase hunger of selected-space-alien by a random number from 0 to 1;
If selected-space-alien is starving and selected-space-alien can touch an edible thing:
Try selected-space-alien eating a random edible thing that can be touched by selected-space-alien;[/code]