Transport Tycoon Forums

The place to talk about Transport Tycoon
It is currently Fri Oct 20, 2017 12:18 pm

All times are UTC




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Dec 16, 2016 2:06 pm 
Offline
Engineer
Engineer
User avatar

Joined: Fri May 07, 2010 11:46 am
Posts: 7
Skype: munty92
Location: Bucharest
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.


Top
   
PostPosted: Sat Dec 17, 2016 12:48 pm 
Offline
OpenTTD Developer
OpenTTD Developer

Joined: Wed Dec 20, 2006 1:31 pm
Posts: 972
Location: Aschaffenburg
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.

_________________
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁


Top
   
PostPosted: Sat Dec 17, 2016 1:01 pm 
Offline
Engineer
Engineer
User avatar

Joined: Fri May 07, 2010 11:46 am
Posts: 7
Skype: munty92
Location: Bucharest
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).


Top
   
PostPosted: Fri Dec 23, 2016 7:47 am 
Offline
Engineer
Engineer

Joined: Sun May 12, 2013 10:28 pm
Posts: 86
Location: Argentina
Hi, have you checked CityConnecter AI?


Top
   
PostPosted: Sun Dec 25, 2016 2:02 pm 
Offline
Engineer
Engineer
User avatar

Joined: Fri May 07, 2010 11:46 am
Posts: 7
Skype: munty92
Location: Bucharest
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.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000-2017 phpBB Limited

Copyright © Owen Rudge/The Transport Tycoon Forums 2001-2017.
Hosted by Zernebok Hosting.