Help me fix this Counterfeit Monkey bug

This is an old regression introduced on 3 October 2016 in commit a390bad, but wasn’t noticed until now.

If you play Counterfeit Monkey on an interpreter that supports GLK style hints, such as Gargoyle or Windows Glulxe, and set default interpreter text color and background to anything other than black and white, the game will revert to black text on white after a restart.

Basically it seems as if stylehint_TextColor 0x00000000 (black) and stylehint_BackColor 0x00FFFFFF (white) somehow get set for style_Normal in the main window during restart, but only on restart. When starting the game for the first time, there are no style hints set for style_Normal, so the interpreter defaults are used. Other windows and styles seem unaffected.

After this has happened it is difficult to do anything about, because once a window is open and running, commands like glk_stylehint_clear() only affect newly created windows. I guess this also means that the main window is somehow destroyed and recreated on restart.

Is it caused by the new GGRecoverObjects routine?

I hope somebody can help me understand what is going on here.

This should not be the case. Open windows stay open across restart/restore/undo. The job of GGRecoverObjects() is to get those window references back into the game’s global variables, so that the game can continue printing to them.

Thanks for clarifying that. I am now comparing the calls to glk_stylehint_set() between the previous working version and the broken one. The previous code never tries to set the text color of style_Normal, but it seems that the new code tries to set it to black on white (twice, for some reason) on both first start up and restart. It only seems to have effect after restart, however.

You could try disabling various rules from Flexible Windows. The Gargoyle cursor color rule and the Gargoyle window padding rule both set colours after a window has been opened.

Thanks, I’ll try that!

I see that the new version, the one with the bug, actually deletes the main text window on restart, sets the text color and then creates a new one. Now let’s see if I can work out why and what to do about it.

EDIT: Note that this bug does not seem to have anything to do with any particular Gargoyle quirks, as it manifests in the same way on Gargoyle, Win Glulxe and Spatterlight.

Right, those rules don’t detect Gargoyle, they just do stuff to counter some of its quirks.

Maybe it would be possible to add extra code to them to only turn them on if the author has actually specified windows with colours.

I don’t know what would be deleting the main window.

If anyone is curious, here is debug output from Spatterlight listing all functions calls from the game. It does not quite overlap GLK function calls, but I think it is close enough for our purposes.

This is the output from typing YES, ANDRA, RESTART.

Output from the functioning version.
Output from the buggy version.

The interesting part is the handleStyleHintOnWindowType lines.

The game is restarted on line 157 in the “good” version and line 135 in the “bad”

The extra style hints in the “bad” version after restart begin on line 189.

Note that the main window and the status window are deleted on lines 180 and 184.

Hooray, disabling the Gargoyle-countering rules in Flexible Windows seems to have fixed it! Thanks a lot!

It seems that those style_Normal hints are meant to set the cursor color on Gargoyle.

EDIT: I guess the windows that are destroyed on restart are the split window map-text, and then a new non-split text window is created for the initial questions. Now I just need to work out why the cursor is invisible on Gargoyle on restart until you press a button.

Hmm. Actually I think restarting with FW is making the windows be deleted. The issue is that when you restart the windows are not g-required. GGRecoverObjects will correctly identify and set the gg_mainwin variables etc, but the recalibrate windows rule will then deconstruct them because they’re not yet required.

I think FW should in the recalibrate windows rule set the main and status window to required if they exist then. That way they won’t be deleted, but they also won’t be created earlier than expected when you’re not restarting.

Hmm… but what if you’re loading a game that has disabled the main window in favour of another window??? This is complicated! Maybe we can make it only do so during the starting the virtual machine activity.

Try this:

A first glulx object-updating rule (this is the recalibrate windows rule):
	if the starting the virtual machine activity is going on:
		if the main window is g-present:
			now the main window is g-required;
		if the status window is g-present and the no status line option is not active:
			now the status window is g-required;
	calibrate windows;
	focus the current focus window;

That’s it! Now everything is fixed. Thanks again! I could never have done it without your help.

Note that to make Gargoyle colors work as expected, The Gargoyle cursor color rule and the Gargoyle window padding rule in Flexible Windows still have to be commented out.