Germanunkol / trAInsported

Löve2D Game
http://germanunkol.de/trainsported/
Other
163 stars 40 forks source link

Line counting for ai.* callbacks fails #118

Open scotty007 opened 8 years ago

scotty007 commented 8 years ago

For some (line optimized) AIs calls to coroutine.resume() in ai.*() callbacks don't return and the game hangs. It seems that there's no "line" event triggered for some long running (or endless) loops in newLineCountHook(). I can reproduce this issue on different systems with the (original, line optimized) czAI12 AI. The "un-optimized" version gets stopped as expected.

Germanunkol commented 8 years ago

Hm, this seems to be a Lua-specific thing. I think I might have to block the usage of coroutines somehow...? I haven't used coroutines much, I'll have to look into this. Thanks for the detailed report!

scotty007 commented 8 years ago

"block the usage of coroutines" means rewriting the AI wrappers (Scripts/ai.lua) without using coroutines? (The AIs can't use coroutines inside the sandbox (Scripts/sandbox.lua line 198).)

I'm not sure if lua.debug's line counting behaves different inside coroutines. I'd guess, no.

Another possible way would be to count the instructions instead of the executed lines. I created a quick'n'dirty version here: https://github.com/scotty007/trAInsported/tree/experiments/instruction_count for testing. This works for both versions of the AI mentioned above. But it changes the performance of the AIs as well (regarding the AI callback timeouts). First point: "nicely formatted" and "line optimized" versions are counted the same, while optimized versions highly benefit from the current line counting. Second: There's no linear factor from lines to instructions to get the same timeouts.

I ran some tests with a single AI on 6 different maps (sizes between 10x8 and 20x16). For these specific conditions the factor ranges from 3.8 to 4 for the un-optimized version, and from 15 to 25 for the optimized one. For other AIs and maps this may vary.

Germanunkol commented 8 years ago

Ah, sorry, I misunderstood the issue. Yes, you're right, I don't allow coroutines - it's been too long since I had written that code. So that's a good thing. There was a reason why I didn't use instructions and stuck with lines instead. I don't remember it at the moment, though. Yes, changing it would be quite a big change to the game... Maybe I can figure out somehow, why the optimized version of czAI12 hangs and then prohibit this somehow...

scotty007 commented 8 years ago

While digging into this, I "de-optimized" czAI12. There seems to be nothing special in it, just a long running (nested) loop. If it would help you, I can provide the formatted version somehow. I also tried to reproduce this behavior in a simple AI, but with no success so far ...

ghost commented 8 years ago

In case it helps, I uploaded the original unoptimized version of this AI (its not pretty tho, was work in progress at the time):

https://raw.githubusercontent.com/ChrZae/czAi/master/czAi12.lua

I wasn't aware of an issue with this, certainly wasn't intentional.

scotty007 commented 8 years ago

Hey ChrZae, thanks! For work in progress, czAI12 performs quite well. Ranks at 9 ATM ;-)