I’m trying to implement a room where you can guess a code without typing in any other text. Now, I could change this so it said “guess [number]” but I was wondering if there was a way to get this to work.
[code]“6m” by Andrew
r1 is a room. r2 is east of r1.
guessing is an action applying to one number
understand “[number]” as guessing when player is in r1.[/code]
Now technically this does work–it just short circuits the usual error messages in r1, if I type a nonsense command like BHZV or something. I’ve tried to find an elegant way around it, without poking into “after reading a command” with (pseudocode below)
if player is in r1 and the player's command matches the regular expression "^[0-9]+$": now the player's command is "guess [the player's command]";
I could also set a flag e.g.
[code]if player is in r1 and the player’s command matches the regular expression “^[0-9]+$”: now all-nums is true; else: now all-nums is false;
…
understand “[number]” as guessing when all-nums is true.[/code]
[/code]
Which would be acceptable hacks for what I want to do, I guess, but really inelegant. And I hate to add too much text to “after reading a command” because that can have all sorts of unintended stuff waiting to go wrong.
Is there a simple trick to work around this? Is this a potential parser bug? My assumption is that “[number]” should not also assume that any bad verb is a number guess.
"6m" by Andrew
r1 is a room. r2 is east of r1.
guessing is an action applying to one number.
understand "[number]" as guessing when the player is in r1.
report guessing:
say "You guess [the number understood]."
counting to is an action applying to one number.
understand "count to [number]" as counting to.
report counting to:
say "You count to [the number understood]."
understand "[text]" as a mistake ("I didn't understand that sentence.") when the player is in r1.
when play begins:
now the right hand status line is "[turn count]".
test me with "42 / count to 42 / asdf / count to asdf / e / 42 / count to 42 / asdf / count to asdf".
I set the error msg in my example to “I didn’t understand that sentence”, but something interesting:
r1 is a room.
xyzzying is an action applying to nothing.
understand "xyzzy" as xyzzying.
vs.
r1 is a room.
xyzzying is an action applying to nothing.
understand "xyzzy" as "[magic word]".
understand "[magic word]" as xyzzying.
The presence of an intermediate grammar token changes the parser error msg that the player sees.
Re: Zarf’s comment
If you want to go with “after reading a command” (and assuming you’ve defined a guess command), this doesn’t seem too hairy:
after reading a command when the player is in r1:
if the player's command matches "[number]":
replace the player's command with "guess [the number understood]".
This crashes the I7 6M62 interpreter after a restart:
r1 is a room.
after reading a command:
if the player's command matches "[number]":
replace the player's command with "x me".
test crash with "42 / restart".
“Fatal error: zcode_op_aread called with the memory buffer size set to 0 (PC=#11de0)”.
Okay, yeah, on second thought, I agree. The thing is–I’ve done so much of that that I’m worried about a patchwork quilt of code, which can get dangerous. But in this case, my “after reading a command” is far less messy than in other games, so yeah.
So the note above works. For some reason I thought it’d be more complex than it was. That’s what I get for posting when tired!
Interestingly, when testing vlaviano’s code, it seems regular numbers can be written out as words, which works in all cases other than 0: “zero”, “nought”, “nil” or “null” all fail to be recognized. Does anyone know if that’s by design?
The parser was designed around commands that manipulate the physical world (>TAKE THREE APPLES. GIVE FIVE COINS TO TROLL) so I assume zero wasn’t considered necessary.