Doing something after every turn?

I have a theoretical situation that I can’t quite get Inform to work with, and the documentation isn’t entirely clear on this. Consider the following code:

Every turn while in darkness: if a random chance of 1 in 3 succeeds: end the game saying "You have wandered into the slavering fangs of a lurking grue![line break]*** YOU ARE DEAD."

The code technically works fine, but say I enter darkness, THEN light a lantern. Even if I use that first turn in darkness to light the lantern, that 1 in 3 chance can still succeed. How could I add to that rule to say something like, “Unless the player lights a lantern, if a random chance of 1 in 3 succeeds…” etc.? I don’t want the player to light a lantern, then still have the chance of being eaten by the grue on that turn. The idea would be to process that random chance only if the player does not light the lantern.

Every turn rules run after action rules, so you should be able to check if an action has lit a lantern by then.
Like so:

Every turn while in darkness: if the lantern is not lit: if a random chance of 1 in 3 succeeds: end the game saying "You have wandered into the slavering fangs of a lurking grue! ***[line break]*** YOU ARE DEAD."

Thanks a ton, that worked perfectly. I could have sworn I tried stacking “if” statements before and got some kind of syntax error, but doing what you said gave me no problems at all. Really appreciate it!

That’s an interesting circumstance. I’m assuming that “darkness” here is interpreted as the darkness room, and not the state of having no light. Is there a phrase that can be used to invoke the checking-for-light routine?

This seems to work, although I feel like there should be an even simpler phrase:

Every turn when in darkness and no visible thing is lit:

Actualy, that’s not quite right. The “visible” adjective only applies to things that were visible at the start of the turn, i.e. only the players possessions when it’s dark. So this code will not prevent grues:

[code]
The Cave is a dark room. “You are in a brightly lit cave.” The Plain is west of the Cave. “You are in broad daylight.”

The remote control is a device. Understand “light” as the remote control. It is carried by the player.
The fixture is scenery in the Cave.

Carry out switching on the remote control: now the fixture is lit.
Carry out switching off the remote control: now the fixture is dark.

Every turn while in darkness:
say “You are in darkness.”

Every turn when in darkness and no visible thing is lit:
say “There is no light in here.”

test me with “z/turn light on/z/turn light off/z/turn light on/w”[/code]

While I was checking this out, I noticed that the “printing the announcement of light” activity happens after the every turn rules.

Felix’s example is almost right. You should check that the lantern is lit and in the location. Right now, the lantern will suppress darkness messages even if it’s lit somewhere else in the game.

Capmikee’s example works too.

That’s a good idea. Since the lantern would have to be on the player or in the room, would there be a phrase to check whether or not an item is at the player’s location, regardless of visibility? Alternately, I could rig it so that the lantern becomes unlit if put down, but I’d like to have the player be able to set it down and still light the room.

Or, possibly even better would be to just prohibit the player from dropping the lantern, and instead say that the lantern is too important to leave behind. That way, I could just add a qualifier to check if the player carries the lantern AND if it’s lit.

e.g.

Every turn while in darkness:
	if the player carries the lantern:
		if the lantern is not lit:
			if a random chance of 1 in 3 succeeds: 
				end the game saying "You have wandered into the slavering fangs of a lurking grue![line break]*** YOU ARE DEAD."
				
Instead of dropping the lantern, say "You realize you shouldn't do that, as the lantern is your only source of light. It could be dangerous in here!"
Instead of inserting the lantern into a container, say "You realize you shouldn't do that, as the lantern is your only source of light. It could be dangerous in here!"

It would be possible to prohibit the player from lighting the lantern unless he/she picks it up first as well, right?

Sure. Pseudocode-wise (meaning I haven’t tested it, but it should look like this):

Check lighting the brass lantern when the lantern is not held by the player (this is the too light not to carry rule): say "You had best carry the lantern before attempting that." instead.

although in that situation I’d much prefer to have Inform take the lantern before lighting it. As a player, any time I’m being told to perform an essentially mindless task, I feel like I’m jumping through hoops. If the author had the time to snicker at me for not dotting the i:s and crossing the t:s, then the author clearly had the time to automate such pointless makework, but chose not to.

EDIT: Changed around a few phrases.

I did something similar to that actually, using a check statement:

Check switching on the battery powered lantern: If the player does not carry the battery powered lantern, instead say "You must first pick up the lantern."

This seems to work perfectly, and in conjunction with the code discussed earlier, the whole thing works just as it should. Thanks for the help everyone!

I don’t know why this wouldn’t work.

To decide if a player is darkened:
  if he can see a lit thing, decide no;
  if he is not in a dark room, decide no;
  decide yes.

Every turn while the player is darkened:
  if a 1 in 3 chance succeeds:
   say "[grue death]".

Of course this doesn’t stop you from wandering around in the dark through careful use of undo and luck. You might want a turn counter during darkness, or something that prevents you from moving to a non-lit area.