The UNDO Command - ThePix/QuestJS GitHub Wiki

Adding an UNDO command is not trivial, and its current status is "experimental".

The system saves the game state to an array each turn, whilst removing the first entry. It uses the same mechanism as the save/load system, so anything that is saved when the game is saved will get restored.

Speed considerations

Before implementing, I was concerned that this would slow the game down. The unit test game has nearly 60 objects, and took 1.2 to 1.3 ms per test without saving the game state, and 1.5 to 1.6 ms when it was, which is hardly anything. Some of the tests will not trigger the game state save, but even an increase in 0.5 ms is not going to be noticeable.

Assuming it scales linearly, a game with 6000 objects will take around 50 ms to save the game state, which should still be acceptable.

When it saves

The game state is saved after a "proper" turn, this is when the turn count would increment. It does not save after the player does HELP or VERBOSE, as those do not count as turns. It may not save after the player does LOOK, depending of settings of the game. These commands do not change the game state, so there is no point saving it.

This means that if the player does EAST, then KICK CAT, the LOOK, then UNDO, the game state will revert back to how it was before the cat was kicked. The player can then go LOOK, realise she wants to go back another step, do UNDO, and will be back before she went east.

Save location

Game state is saved to an array, so will be lost if the browser closes if the player navigates to a new web page. It is not saved when the player saves the game, so when a game is loaded the player will only be able to undo back to that point.

Undo limit

By default, the player can go back a maximum of 10 UNDOs. Modify MAX_UNDO in setting.js to change this. Set it to zero to disable the UNDO feature.

Beta...

This feature works in theory and has had some limited testing, but may not be completely bug free.