Game Script Performance OPCode

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
User avatar
adysnook
Engineer
Engineer
Posts: 7
Joined: 07 May 2010 11:46
Skype: munty92
Location: Bucharest
Contact:

Game Script Performance OPCode

Post by adysnook »

Hello,

I just started coding a game script that should do the following at the startup of the game, and the game should not begin before it finishes.
It's a basic idea that was suggested before: pick some pairs of towns and connect them using SuperLib RoadBuilder. The script works, but it seems to me that it's very very slow.
I have done all the function calls in MyController extending GSController::Start, so it should run everything just at the beginning, but the days are still counting.

There are 2 questions:
1. How do I make the game wait for the script to execute? Maybe pause the game until it's done? (GSGame Pause/Unpause) is there other way?

2. I see the my CPU usage is 1-2%, but the script takes some time to finish. How can I improve performance?
I have tried playing with script_max_opcode_till_suspend setting, but it seems to have no effect.

PS: It's my first time playing with gamescripts, but I have programming background. I would be able to implement this in OpenTTD's C++ code.

If needed, I can provide more information or the script.

Thanks.
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Game Script Performance OPCode

Post by frosch »

The main limiting factor here is the way game script execution is interleaved with the game state loop.

Essentially a "game tick" consists of:
1. Run game script for N operations, or until it issues a "command".
2. Run game state and execute scheduled commands.
3. Wait for 30ms to finish.

That means: A script can only execute one "command" every 30ms.
A "command" is something that alters the game state, unlike merely reading the game state or altering script-internal variables.

That means if you build a 100 tile long road by calling GSRoad::BuildRoad 100 times (once for each tile), this will take 3 seconds.
But if you run GSRoad::BuildRoad only once and pass it a stretch of a 100 tile long road, it will complete in 30 milliseconds.

About pausing:
Yes, game scripts can pause the game.
However, the commands it triggers are still subject to the "build while paused" setting. (game scripts can change settings btw.)
In fact this means that a game script can dead-lock itself and the whole game (FS#5283): If the game script pauses the game and then issues a command which is disallowed while paused, the game stops forever. No player can unpause a game, which was paused by a game script. The game script is suspended and waiting for the command to complete. And the command is waiting for the game to get unpaused.

About world generation:
Technicaly the game script gets a head start and is run for several thousand operations before the game starts. However, currently this is interrupted with the first command that is triggered by the game script. That is, the game always starts with the first command.
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
User avatar
adysnook
Engineer
Engineer
Posts: 7
Joined: 07 May 2010 11:46
Skype: munty92
Location: Bucharest
Contact:

Re: Game Script Performance OPCode

Post by adysnook »

Actually, what I have found is that the Script Takes A LOT to find a path using SuperLib.RoadBuilder::ConnectTiles(), that in turn calls the A* finder from AyStar library.

Is it a hardcoded upper limit of 250 000 opcodes ? that's what I see...

I see that even if the game is paused, the script takes long to find path (without building).
HGus
Engineer
Engineer
Posts: 121
Joined: 12 May 2013 22:28
Location: Argentina

Re: Game Script Performance OPCode

Post by HGus »

Hi, have you checked CityConnecter AI?
User avatar
adysnook
Engineer
Engineer
Posts: 7
Joined: 07 May 2010 11:46
Skype: munty92
Location: Bucharest
Contact:

Re: Game Script Performance OPCode

Post by adysnook »

I didn't try CityConnecter AI. I will take a look into the code. From what I understand, this will also take some time to do its job, so I think I need to implement the path finder into the C++ code.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 6 guests