Inform 7- Check Undo

I’m trying to write a game in which the “undo” option is like an in-game spell.

There is a puzzle involving paper-rock-scissors where the opponent is utilizing the undo option, so you can cast a spell to prevent the undo option, thus causing him to lose. Another puzzle will involve you using undo “in game” to guess a random number between 1 and 65535.

I know inform comes with a “use undo prevention” option, but I’d like that option to be able to turn on and off in-game, perhaps with an alternate response if undo is deactivated (besides the default one- so players realize it can be turned back on).

How can I have the game utilize some kind of “check undo” or “before undo”?
It seems like the game already has this for save and restart.

Will you be compiling to Glulx?

Yes. I’m also using some windows manipulation for an inventory window. I wanted “Undo” to be a spell you learn rather than a normal part of the game.

Undoing is handled entirely on the Inform 6 level, so we need to add a hook there:

"To undo, or not to undo?"

Place is a room. The spellbook is in the place.

Check undoing is a rulebook with default success.

Check undoing when the player does not have the spellbook:
	say "You can't do that yet.";
	rule fails.

Include (-
[ Perform_Undo;
	if (IterationsOfTurnSequence == 0) { IMMEDIATELY_UNDO_RM('B'); new_line; return; }
	if (undo_flag == 0) { IMMEDIATELY_UNDO_RM('C'); new_line; return; }
	if (undo_flag == 1) { IMMEDIATELY_UNDO_RM('D'); new_line; return; }
	if (FollowRulebook((+ the check undoing rules +)) && RulebookFailed()) return;
	if (VM_Undo() == 0) { IMMEDIATELY_UNDO_RM('F'); new_line; }
];
-) instead of "Perform Undo" in "OutOfWorld.i6t".

You can then add more “check undoing” rules as you see fit.

Keep in mind, though, that undo is not guaranteed to be available across all interpreters and in all situations (although as far as I know it’s fairly common for Glulx interpreters to support it).

The Undo Output Control extension may be helpful: extensions/Undo Output Control.i7x at master · i7/extensions · GitHub

Thank you, that extension did it!

I knew I could “cheat” with the NPC using the undo spell by just making it an event, but wanted it to seem real “in world”.