tetracorp / k240

Exploring K240, a project to disassemble the 1994 Commodore Amiga game, K240
https://tetracorp.github.io/k240/
5 stars 1 forks source link

K240 - undocumented bug? #11

Open drDragonSmoke opened 1 year ago

drDragonSmoke commented 1 year ago

I recently ran into the following bug/exploit while playing K240: A Repair Facility will increase the hull of any ship in the hangar to above the ship maximum. It will increase by 1 point for every Repair Facility on the asteroid, every 8 days. I have tested this with up to 20 Repair Facilities. The ship hull will keep increasing until the hull armor reaches 250, at which point the ship will disappear from the hangar! At least, I believe it is 250. The maximum I observed on a ship was 248, before it disappeared on the next 8-day tick.

This is potentially a very efficient exploit, provided you remember to release the over-repaired ships from the hangar before they disappear. I also wonder if this affects buildings in the same way, ie. does the Repair Facility supercharge the building armor as well?

I would be very interested to know if there are any versions of the game with this bug fixed, or if it is even possible to fix.

tetracorp commented 1 year ago

You're right!

I'm able to reproduce the bug. Looking at the game code, the Repair Facility makes two checks before increasing the Armour value of a ship:

The result is that a small ship with any shield can repair above the maximum. This can raise a ship as high as 255 HP. Once you reach 256 HP, the byte loops around to zero, in which case it counts as zero. That ship will never repair above zero, and the game will treat it as destroyed.

It appears in all versions of the game. I think this bug may actually be visible in a screenshot on the back of the box, where several small ships have impossibly high Armour values.

I believe you could fix it by changing an instruction from BEQ to BGE, although it would mean ships only repair to their unshielded value. At 0x15e6e (89,710) bytes in playk240, hex edit 6702 to 6c02. Accounting correctly for shields would require writing more code.

The bug doesn't affect buildings, because the Building Armour blueprint is correctly accounted for. However, this does mean that Building Armour is retroactively applied to existing buildings if you have a Repair Facility.

drDragonSmoke commented 1 year ago

Thanks for the quick reply! (and thank you for this awesome site)

"this bug may actually be visible in a screenshot on the back of the box" Haha, that is absolutely amazing!

I don't know enough about coding (yet!) to attempt a fix, or even if it possible to try, but I do feel inspired :) I took a look at your annotated .asm (interesting stuff) and even tried a re-assembly with vasm. Got some 'redefined' errors (and some errors about 'trailing garbage'), and no output file. Guess I got a lot of reading to do :)

Update: I was able to use IRA to make my own .cnf and .asm, and vasm also worked to reassemble it. I could not quite find the place you were referring to in the .asm, so I used a hex editor (flexhex) directly on the playk240 game file instead. I used Go to - Offset - and typed in the position you mentioned (0x15e6e). There I edited the last segment from 6702 to 6c02. I then tested the game, but unfortunately this did not change the bug.

Update 2: It's fixed! I changed the instruction from BEQ to BLE (hex-edited 6702 to 6f02), and the repair function now works as intended! Ships at default or over default hp get no increase, and ships under default gain hp until default it reached. Fantastic! Thanks for your help!

To explain (in case you want to include in your annotated .asm): I believe the original code checks to see if ship damage is equal to 0, so it doesn't repair ships at default hp. It will repair ships with a damage value, but it will also repair ships above default hp (with shield) because they are also counted as having a damage value, albeit a negative one.