Author Topic: GAL (KQ1) extractor and decompiler  (Read 14856 times)

0 Members and 1 Guest are viewing this topic.

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #60 on: November 09, 2023, 10:30:02 AM »
Yeah, I have looked at the missing opcodes. Many of them are related to movers/cyclers and aren't easy to decode. But I have worked out some, and I'm updating SYSDEFS (and GAMEDEFS) as I go. They don't map neatly to the AGI set (for example there's a specific opcode that I have called ego.falls; you can guess what it does). There are also a few mistakes in the ones that NewRisingSun identified (current.room.f takes two var parameters, not a number and a var).

You have inspired me to look into the disassembly of the GAL KQ as well, but my focus is going to be on the picture drawing logic, so there should be no danger of us doubling up. I've already identified the entry points for each of the picture commands. I'll report back in a day or two with what I've discovered. There is clearly some magic going on with some of the picture actions that scummvm hasn't captured yet, as the Troll's Tale probably isn't making use of it. For example, the 0xF0 picture code toggles something, and other picture codes set certain state that gets cleared by the use of other picture codes. I just need to work out now what exactly those bits of state are controlling and try to replicate it in my PICEDIT GAL import prototype.

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #61 on: November 10, 2023, 06:19:05 AM »
So, it looks like the 0xF0 picture code toggles between the visual and priority screens with regards to which of those two screens the 0xF8/0xF9/0xFA/0xFB commands draw to.

0xF8 = The same as 0xF4, except that it uses the current setting of the 0xF0 toggle to determine if visual or priority is drawn to.
0xF9 = The same as 0xF5, except that it uses the current setting of the 0xF0 toggle to determine if visual or priority is drawn to.
0xFA = The same as 0xF6, except that it uses the current setting of the 0xF0 toggle to determine if visual or priority is drawn to.
0xFB = The same as 0xF7, except that it uses the current setting of the 0xF0 toggle to determine if visual or priority is drawn to.

This is why I was getting some strange behaviour in my prototype. I initially thought that this second set of line drawing commands were for visual only, given that Troll's Tale uses them, but it turns out that they support both visual only and priority only. The control of what screen is being drawn to is not as straight forward as it is in the AGIV2 picture format, where we instead have explicit commands for turning visual and priority on and off.

I haven't incorporated this discovery into my prototype yet but I'll do it soon. There is also something strange going on with the three variants of the fill command (0xFC, 0xFD and 0xFE) that I'll check out first.

The goal is not to build a GAL picture editor at this stage, but rather to see if those pictures can be cleanly imported. It looks like the pictures went through some sort of conversion in between the original KQ booter releases and the later AGIV2 release of KQ1, since the equivalent drawing actions are all in the same order and the lines appear to match. It will be interesting to see if the AGIv2 codes generated by my import align with what the AGIV2 KQ1 uses. I suspect that the pictures probably went through some manual touch ups, but regardless, it will be interesting to discover that either way. I'm just crossing my fingers that the line drawing algorithm remained consistent. A pixel placed differently here or there would mess things up.

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #62 on: November 10, 2023, 01:02:27 PM »
Interestingly, the background for the priority screen appears to be black, rather than red

It turns out that I was wrong about the above. The KQ/GAL interpreter code is clearly setting the priority screen to be all blue. So blue is the default priority background. Black appears to be used a lot to fill in areas of water. I need to check a few more pictures to see if this is consistent, but so far for the few that I've checked, it does seem like water is priority black.

For the three fill modes, one fills in both visual and priority, the second visual only and the third is priority only. These fill modes not only behave like that, but they also include the colours to fill in with. So, for example, 0xFC includes a visual colour that it sets the visual colour to, then includes a priority colour that it sets the priority colour to, and then fills at the given points, in both the visual and priority screens. 0xFD includes a priority colour that it sets the priority colour to, then it fills in only the priority screen. And finally, 0xFE includes a visual colour that it sets the visual colour to, then it fills in only the visual screen. - So if there happen to be no points included to fill at, then these picture actions would only change the colour.

There are also separate codes for changing the colours. 0xF1 sets the visual colour. 0xF2 sets the priority colour. 0xF3 sets visual and priority colours. I think if 0xFC did not include any fill points, then it would behave the same as 0xF3, and it would be similar for 0xFD and 0xFE with regards to 0xF2 and 0xF1.

Now, back to 0xFF: the default mode for this one is that 0xF8/0xF9/0xFA/0xFB will draw only in the priority mode. The 0xFF action has to be used once to make these instead draw only in the visual mode. And now that I've had a look at a Troll's Tale picture, I can see that it uses 0xFF right at the start to toggle this mode, so that all subsequent drawing actions that it uses are drawing only in the visual screen. KQ instead uses 0xFF a few times during each picture, whenever it wants to draw lines only to visual or only to priority. It usually starts with using the 0xF4/0xF5/0xF6/0xF7 actions though, which draw to both screens, and it isn't until a bit into the picture that it switches to using the single screen modes.

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #63 on: November 12, 2023, 05:19:55 AM »
There is one mystery with regards to the Troll's Tale format though. It appears that it isn't an exact match with the KQ/GAL picture format. The 0xFB command doesn't behave the same. For the KQ/GAL format, 0xFB is the same as 0xF7 (i.e. the shorter relative lines) except that 0xF0 controls what screen 0xFB is operating in. In the Troll's Tale format, 0xFB appears to be absolute lines rather than relative lines, so appears to be identical to the 0xFA command. I had to implement it as such in my prototype in order for the Troll's Tale pictures to render properly. Other than that, the KQ/GAL and Troll's Tale picture codes (at least in relation to the ones that Troll's Tale uses) are equivalent.

I hadn't noticed until now, but the scummvm code clearly points this out, and has a TODO message asking if this really is the case, i.e. the duplication (see below):

https://github.com/scummvm/scummvm/blob/master/engines/agi/picture.cpp#L473

Code: [Select]
case 0xfa:
// TODO: is this really correct?
draw_LineAbsolute();
break;
case 0xfb:
// TODO: is this really correct?
draw_LineAbsolute();
break;

Offline lskovlun

Re: GAL (KQ1) extractor and decompiler
« Reply #64 on: November 23, 2023, 03:10:05 PM »
I discovered a bug in GALextract affecting views. Besides generating files that weren't quite what the interpreter sees, some view numbers were unused and generated garbled files. This version of GALextract should fix both and also prints the view numbers that are unused.

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #65 on: November 24, 2023, 09:58:18 AM »
Thanks. I'll give it a go tonight.

I've been meaning to post an update on where I got to with the GAL picture investigation. I'll hopefully find some time to do that tonight as well. There are a number of interesting differences when comparing the original KQ pictures from the Booter versions with the later AGI versions of the same pictures.

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #66 on: November 25, 2023, 12:21:43 PM »
I have attached some comparison pictures between a GAL imported picture (i.e. imported into the PICEDIT branch I was working on) and the corresponding AGI picture from the AGI version of the game. I have done this for PICTURE 6 and PICTURE 23. The GAL picture in each case is not exactly as it would have appeared in the original KQ booter versions but rather what it looks like when converted to the equivalent AGI picture codes.

The visual screens look almost the same. In the case of PICTURE 6 at the top, there are only a few pixels different, where there is a white pixel in the GAL picture that shouldn't be white. Now this obviously isn't what it looked like in the original booter version of the game, so it suggests that the GAL line or fill algorithm is slightly different. When comparing the picture commands for the GAL vs AGI versions, the AGI version usually has additional fill commands to fill in these white pixels that shouldn't be there. This suggests that the pictures were converted to the AGI format and then touched up afterwards. You'll notice that they missed one pixel in the upper middle part of the picture though, as it is still white in the AGI version.

The visual screen for PICTURE 23 really does look identical. The GAL picture on the right is how it appears in my PICEDIT app after being imported, so the end result, at least as far as the visual screen goes, appears to be identical in this case.

Where things get very different though is in the priority screens. For starters, the most distant priority colour, i.e. the one at the back, is blue in the case of GAL but red in the case of AGI. The red, cyan and green priority colours in GAL are just normal priority band colours and don't appear to have special meaning.

You'll also notice that black is used for water in the GAL picture but cyan is used in AGI.

Another thing that is fairly obvious is that the priority band division must be different, since the same objects, e.g. the trees, log, rock, are using different priority colours despite being placed in exactly the same part of the screen. This isn't surprising, given that red, cyan and green are used as normal priority colours, so there are more colours to cover the screen.

The other big difference is that the GAL picture doesn't have any "block" control lines drawn. I have seen in some of the GAL pictures that they use the white colour as a block line, in the places where an AGI picture might use the black control line. This seems to be used quite rarely though, and it makes me think that the block control lines must be placed into the picture using a different mechanism, and not generally part of the picture itself. But, as I say, some pictures do use white for a block line, meaning that white must have that meaning in GAL, i.e. the same meaning as blank in AGI.

I think that covers all the obvious differences. It is due to these priority screen differences that a tool like PICEDIT or WinAGI can't really support importing a GAL picture. It could import the visual screen and get very close, but the imported priority screen would not be compatible with AGI v1/2/3. I wondered whether the priority colours could be changed on import, but I think that would get very tricky to get right, since the tool would be making guesses about whether the base of the object is at ground level or not. You'll notice from the PICTURE 23 example that in one case, there is an object that is light green in the GAL picture but split between two different colours in the AGI picture (light cyan and light red). This shows that when they were touching up the converted image for the AGI version of KQ1, they must have decided that they needed to change the priority colour partway up that object. Not sure why yet, but it does suggest a manual conversion of the priority screen rather than automated. Maybe they automated it to some degree, but as I say, it would be difficult to get right.

And the absence of the control lines would certainly be an issue as well.
« Last Edit: November 25, 2023, 12:28:55 PM by lance.ewing »

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #67 on: November 25, 2023, 12:56:16 PM »
I also played around with importing pictures from the ADL games, the importing involving converting the ADL picture codes to the relevant AGI ones. The first issue there was that the picture resolution is different. Those games were originally for the Apple II, so the width of the picture was 140 rather than 160. The height of the pictures appear to be 160 rather than 168. So after importing the picture into PICEDIT, it is slightly smaller than the available AGI picture area.

The second issue is that the fill tool works differently. ADL pictures support 22 "colours", where a lot of those colours are actually made up from alternating two of the real colours. This is a bit like some of the fills that the early 16 colour SCI games had. The available ADL colours are listed on page 18 and 19 of the following manual for the Tablet Graphics tool that Ken Williams wrote in 1980:

https://www.mocagh.org/sierra/tabletgraphics.pdf

And the same list of colours appears within one of the screens of "The Artist" tool that I mentioned earlier in this topic. "The Artist" appears to be a continuation of Ken's tool, with Warren Schwader taking over the programming.

So my import had to pick an AGI colour, one of the 16, that was closest to each of the 22. It doesn't really work, because some of the ADL pictures use different shades of certain colours that end up being the same colour in the import. It looks ugly to be honest, so I'm not certain it will be useful.

Unless someone wanted to get a head start on a project to port one of the ADL games to AGI. Don't think it would be Time Zone though, as that game allegedly has around 1400 pictures!! Not sure how we'd support that in an AGI game that is limited to 256.

Offline lskovlun

Re: GAL (KQ1) extractor and decompiler
« Reply #68 on: November 25, 2023, 06:16:55 PM »
So I might as well post my newest SYSDEFS and GAMEDEFS here...

Offline lskovlun

Re: GAL (KQ1) extractor and decompiler
« Reply #69 on: December 02, 2023, 02:22:13 AM »
Hmm. Donald Trivette is very explicit about control lines existing (which he calls skeleton lines):
Quote
The scenes are more than just static back grounds. For instance, if Sir Grahame staggers a bit and runs into a castle wall, he stops. How does the program know when the character hits something? There's a skeleton, an invisible structure, behind each picture. If you could see this skeleton, you'd notice lots of lines running all over the screen defining where Sir Grahame can and cannot walk. The lines were drawn into each room with the graphics tablet.
He then goes on to describe priorities.

Offline Collector

Re: GAL (KQ1) extractor and decompiler
« Reply #70 on: December 02, 2023, 11:39:47 AM »
Was that from the "Official Book of King's Quest"?
KQII Remake Pic

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #71 on: December 02, 2023, 01:17:29 PM »
No, it's from the February 1985 COMPUTE! Magazine article that we discussed a few years back:

https://www.atarimagazines.com/compute/issue57/kings_quest.html

Interesting. Yeah, there obviously has to be control lines in there but I'm not sure where they are drawn. I haven't had much of a go looking for them yet.

Offline lskovlun

Re: GAL (KQ1) extractor and decompiler
« Reply #72 on: December 10, 2023, 05:21:13 AM »
I've had the chance to run GALdecompile on some Apple II images. It seems to be an odd mixture of old and new code. Load.pic and overlay.pic don't take any parameters (I'm guessing this is older than the PC version). There have been slight changes in the text, and numerous small code changes. The words.bin file has no sign of Greg, Hal or Arthur; all the word numbers are different. This means no 'greg' Easter egg, but the flamethrower Easter egg is present (which only got added to the AGI version for PC). The variable numbers will need to be redone.

Oh, Lance already did this. Never mind.
« Last Edit: December 10, 2023, 06:18:07 PM by lskovlun »

Offline lance.ewing

Re: GAL (KQ1) extractor and decompiler
« Reply #73 on: December 11, 2023, 03:14:11 PM »
No worries  ;D. To be honest, I didn't explore much more with the Apple II KQ1 other than using GAL decompiler to decompile some of the scripts. I did do quite a bit with the Apple II KQ2 though, including a whole feature branch of the jagi project that could view the various resources from it. That one is an AGI V1 game, so the PICTUREs are the same as AGI V2, but the logic scripts are different. The format of the LOGIC script is the same, but the opcode values for the commands are defined differently. This would be the same for the early PC versions of KQ2 as well, I would imagine. I think there are some threads in the forums where NewRisingSun also attached some tools for viewing AGIV1.

Offline Collector

KQII Remake Pic


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.05 seconds with 23 queries.