the-infocom-files / amfv

A Mind Forever Voyaging
5 stars 2 forks source link

Library mode can run out of stack space #5

Open eriktorbjorn opened 5 years ago

eriktorbjorn commented 5 years ago

If you spend too long in Library Mode, the game will run out of stack space. When I try it in Frotz, if I start the game, immediately type "GO TO LIBRARY MODE" and then hold down "N" the crash will happen at around 11:00pm.

I originally discovered this back when I played it on my old Mac Plus. I thought I had found a clever way to pass time quickly without having to type "WAIT UNTIL ..." or entering Sleep Mode (which doesn't always work). From what I remember, Infocom's interpreter had a smaller stack than Frotz so the crash happened quicker.

Once you leave Library Mode, the stack space is released.

The problem is in LIBRARY-ACTION. It turns out that the Z-Machine will leave any unhandled return values on the stack, and that routine potentially loops forever.

My first idea was to assign the unhandled return values to a temporary variable, catching them all is prone to errors.

My second idea was the move everything inside the REPEAT to a separate routine. This works, because any stack space that routine leaks is released immediately.

Then I was informed that ZIL has a built-in feature to handle this. All you have to do is to flag the routine by adding <ROUTINE-FLAGS CLEAN-STACK?> right before <ROUTINE LIBRARY-ACTION ("AUX" X). This feature can be seen in hints.zil in several games, and in many places in Bureaucracy.

Unless space is really tight (which it's not, since ZILF apparently generates smaller files than Infocom's compiler did), the CLEAN-STACK? solution should be the least intrusive one.