Anuken / Mindustry

The automation tower defense RTS
https://mindustrygame.github.io
GNU General Public License v3.0
22.55k stars 2.95k forks source link

Saving and loading can sometimes change a Logic block's outcome. #4888

Closed NoExit4U closed 3 years ago

NoExit4U commented 3 years ago

Platform: Windows

Build: Steam build 126.1

Issue: When saving a game and then loading it, the outcome of a logic block can be different. In this case it causes disabled Factories to sometimes become permanently active after loading the game.

Here you see that the 3 factories are disabled, even when I wait 5+ minutes. The Factory in the top left is crossed because it never showed the bug even though it have very similar code. 20210307165205_1

After saving and loading twice, one of the factories immediately became active 20210307165238_1

After Saving and Loading about 10 times, I managed to get all 3 of the factories to become active. 20210307165359_1

Steps to reproduce:

It may require a couple of tries before the bug works. This is my record of how many factories started to produce while testing multiple times: 2 - 2 - 0 - 0 - 0 - 0 - 1 - 1 - 1 - 1 - 0 - 1 - 0 - 0 - 0 - 1 - 0 - 0 - 1 - 1 - 0 - 1

Link(s) to mod(s) used: None (the save file may have been with the Cliff mod active, but I was able to reproduce the bug even when I disabled it)

Additional Info The logic scripts are in a chain as follows:

Logic Block1 writes 1 into the Memory Cell. Logic Block1 detects whether or not their linked blocks died. If so, it writes 0 into the Memory Cell.

Logic Block2 loops to wait until Memory Cell becomes 1 Logic Block2 loops to wait until Memory Cell becomes 0 again (while disabling the Factory). Logic Block2 then goes into a loop where it enables the factory forever.

This setup is not guaranteed for it to go bad. Out of the 4 setups, I have only seen 3 of them show the bug. The logic code is not identical for all Logic blocks. For example for the 2 factories on the right, their "Logic Block2" will also check if nearby blocks died to activate.

Other logic blocks on the map appear to have similar issues but are harder to observe and have more complex logic blocks.

Other observations

Map File: map_91.zip Save file: A Save for when all 3 factories have been activated: 411.zip


Place an X (no spaces) between the brackets to confirm that you have read the line below.

Anuken commented 3 years ago

It sounds like you're relying on update order for your logic blocks; when the map reloads, update order will change unpredictably. This is not a bug. Logic should be designed with this in mind.

NoExit4U commented 3 years ago

It can't be an order update issue:

1) The loop in Logic Block 2 waits for Memory Cell 1 to become 1 first.

2) If it was an "update order" issue then I would have seen one of these factories become active when I start the map, which didn't happen in more than 25 times. Only after saving and loading does it happen.

3) I changed the code of the lower left logic block to: 20210307192414_1 and (cellIndex = 0 didn't fit on the screenshot) 20210307192434_1

Now the order no longer matters and I still managed to replicate the bug after loading a couple of times.

Anuken commented 3 years ago

There are so many variables and operations here that it's impossible to tell whether it's a bug in your code or the game. Historically, every "bug" related to large logic programs has turned out to be an issue with the program, not Mindustry. Unless you can produce a minimal example that clearly demonstrates incorrect behavior (and specified what exactly goes wrong), I won't be looking into this.

NoExit4U commented 3 years ago

Ok, I'll try and isolate the bug. I'll open a new issue if I find it.

Before I go can I know which of these are supposed to be saved in a map?

Anuken commented 3 years ago

All of those are saved.