Saying hello without specifying the NPC

I’m trying to address the issue that a player may simply type “say hello”, rather than address it directly to an NPC.
If there is only one NPC, I’d like the program to determine the name and use it in the greeting.
If there is more than one, I’d like to have the program ask which one to greet and use that name.
Here is the basic code I tried for the unspecified NPC case. It seems to work erratically.
I’d like suggestions, if anyone has any.

[code]Instead of answering someone that “Hello”:
say “You say, ‘Hello, [noun].’[paragraph break]”;
[say “You say, ‘Hello, [noun].’[paragraph break]”;]
say “The [noun] replies, ‘Nice to meet you. You’re expected. I’m here to answer your questions.’”.

Instead of answering someone that “Hello” more than one time:
say “You say, ‘Hello, [noun].’[paragraph break]”;
say “The [noun] replies, ‘Didn’t we already do this?’”.

Instead of answering someone that “Goodbye”:
say “You say, ‘Goodbye, [noun].’[paragraph break]”;
say “The [noun] replies, ‘Nice to have met you. It’s been a pleasure. I’m here for a while, in case you have any further questions.’”.

Instead of answering someone that “Goodbye” more than one time:
say “You say, ‘Goodbye, [noun].’[paragraph break]”;
say “The [noun] replies, ‘Didn’t we already do this?’”.
[/code]

It seems to me that the game is already going to take care of the initial problems that you mentioned; when you type “say hello” it tries to fill in the missing noun. If the player types “say hello” it will try to fill it out to make the “answering it that” action (which would be what “say hello to Alice” triggers). When one NPC is in the location, it fills in the NPC; when two NPCs are in the location, it asks you to disambiguate. At least in my testing; I’m not sure how this works internally.

The problem is that when no NPCs are in the location, it fills in the only person who is in the location: yourself. So that’s what it chooses. You could take care of this with a rule that blocks answering yourself that something:

First instead of answering yourself that something: say "No need to talk to yourself."

(“First” means that it runs before any other instead rules; otherwise the rules for answering someone that “hello” would take precedence. You could also fix that by making it a “Before” rule.)

Another issue you might be having is that “Instead of answering someone that ‘Hello’ more than one time:” applies whenever you answer anyone that “Hello” for the second or later time. So if you say “Hello” to one NPC and then to another, the second NPC will give you the dismissive response. You might have to give NPCs a property that flags whether you’ve said hello/goodbye to them.

Also, you should write “[The noun]” rather than “The [noun],” or you’ll get stuff like “The Alice says…”

Thanks for your response.
I’ll give it a try.