Multiple instance issue with PlannerAI P.S. Hello Forums!

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
jimbob
Engineer
Engineer
Posts: 87
Joined: 24 Nov 2014 21:13
Location: At a desk
Contact:

Multiple instance issue with PlannerAI P.S. Hello Forums!

Post by jimbob »

Hi all,

First of all let me introduce myself, I have been a long time player of TT (I have been playing Transport Tycoon since It came out when I was 2 years old!) and I have been occasionally looking through these forums since around ~2008 . OTTD is a fantastic improvement to what is already probably one of the top games of all time. So hats off to the developers and all you content creators. I have recently started working as transport planner which has inspired me to get back into OTTD and try and write an AI.

So please welcome PlannerAI (very early alpha, please ignore the debug signs...):
main.nut
(16.26 KiB) Downloaded 89 times
Currently it is just a humble bus only AI with new GRF support which does point to point connections out of large cities. I have written a custom valuator that gets the highest profit vehicle option taking into account distance, speed and running cost (including purchase cost spread over a vehicles lifetime). Although I say it myself it does seem to pick good options from eGRVTS. While this AI is far from complete one issue that has me totally stumped is I can't seem to start a second instance of the AI even though I have taken the code from the wiki that gives new instances a different name. Help would be appreciated thanks.
Image
Real life transport planner
My projects:Link to my UK Scenario|Scenario Builder GS
Do check out my 3D unity transport game: transporter
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by frosch »

You don't need a different name to start multiple instances.
For testing purposes just open the console and use "startai <ainame>" to start another instance, with <ainame> being the name from your info.nut.
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
User avatar
jimbob
Engineer
Engineer
Posts: 87
Joined: 24 Nov 2014 21:13
Location: At a desk
Contact:

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by jimbob »

frosch wrote:You don't need a different name to start multiple instances.
For testing purposes just open the console and use "startai <ainame>" to start another instance, with <ainame> being the name from your info.nut.
Sorry perhaps I should elaborate. When I use the console to start a second instance as you suggest, the company forms but the script does not run, no debug log appears, the company name does not change (the first thing the script does). it basically appears that while the company exists the ai is totally inactive.

I'm guessing the fault lies with my ai rather than OTTD
Image
Real life transport planner
My projects:Link to my UK Scenario|Scenario Builder GS
Do check out my 3D unity transport game: transporter
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by Zuu »

If you guess that it has problems related to naming, insert some AILog.Info calls in that code so you can track what is happening. Insert a very early log in Start() to verify for yourself that your AI starts.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by krinn »

while (!AICompany.SetName("MyNewAI #" + i));
User avatar
jimbob
Engineer
Engineer
Posts: 87
Joined: 24 Nov 2014 21:13
Location: At a desk
Contact:

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by jimbob »

Zuu wrote:If you guess that it has problems related to naming, insert some AILog.Info calls in that code so you can track what is happening. Insert a very early log in Start() to verify for yourself that your AI starts.
I threw in a ton of log messages and found the script worked up to the to the first company setname function before freezing, its perfectly happy as long as you don't attempt to set the name as the same as the existing one. I am now using the companyID to ensure that all instances have a unique company name, and everything works happily :)

Code: Select all

AICompany.SetName("Planner Co"+ AICompany.ResolveCompanyID( AICompany.COMPANY_SELF ));
I have no idea why the failed set function freezes the script. If this an issue the example script given here on the wiki should be updated: https://wiki.openttd.org/AI:Basics
Image
Real life transport planner
My projects:Link to my UK Scenario|Scenario Builder GS
Do check out my 3D unity transport game: transporter
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by Zuu »

Krinn did point out the problem in his post above though he didn't explain why it is a problem.

Basically, stick to the rule to not put a semicolon after if, while and for statements and you will be good. (Yes there are a few cases when a semicolon after while may be valid, but you will not miss anything by sticking to the rule to not place it there until you know the language better)

The while statement will loop the next statement until the condition is met. With a semicolon you run an empty statement instead of the block below that you think it will run.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
jimbob
Engineer
Engineer
Posts: 87
Joined: 24 Nov 2014 21:13
Location: At a desk
Contact:

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by jimbob »

Zuu wrote:Krinn did point out the problem in his post above though he didn't explain why it is a problem.

Basically, stick to the rule to not put a semicolon after if, while and for statements and you will be good. (Yes there are a few cases when a semicolon after while may be valid, but you will not miss anything by sticking to the rule to not place it there until you know the language better)

The while statement will loop the next statement until the condition is met. With a semicolon you run an empty statement instead of the block below that you think it will run.

derp thanks for the explanation.

Another issue I've come across is the GetProduction function for returning passenger production doesn't seem to correlate at all with how many passengers are actually generated, I found stations on a tile with 11 production with hundreds of passengers a month, then a production 14 tile in another town which only produces ~30 passengers. What value does production actually correspond to?
Image
Real life transport planner
My projects:Link to my UK Scenario|Scenario Builder GS
Do check out my 3D unity transport game: transporter
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by krinn »

If you want info on some API usage, it would help if you actually gives us the real function name.
User avatar
jimbob
Engineer
Engineer
Posts: 87
Joined: 24 Nov 2014 21:13
Location: At a desk
Contact:

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by jimbob »

krinn wrote:If you want info on some API usage, it would help if you actually gives us the real function name.
Sorry its how the AITile.GetProduction function works in the context of:

Code: Select all

AITile.GetCargoProduction( choice , passengerID , 1 , 1 , AIStation.GetCoverageRadius( AIStation.STATION_BUS_STOP ))
I also use this function to evaluate the valid tile list and picks the best spot for production

I print this value onto a sign, but have spotted that production doesn't always seem to correlate with the passengers produced at the station, this seems to be the case where the station is build in-between smaller houses. Is it the case that small houses have the same production value as a tower but produces less passengers?
Image
Real life transport planner
My projects:Link to my UK Scenario|Scenario Builder GS
Do check out my 3D unity transport game: transporter
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by frosch »

The returned value of GetCargoProduction means about nothing. It is the number of tiles that could *potentially* produce *some* amount. Huge houses and small houses count the same, just like industry tiles count the same, even when the industry is producing nothing due to missing supplies.

Anyway, scripts have the same information as human players. A human player cannot know how much a station will receive, nor does OTTD itself know. For industries all you can do is finding the nearby Industry instances and ask them about the production last month (just like humans). For houses I am not aware of a useful method, other than counting tiles and then making some assumption from experience.
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
User avatar
jimbob
Engineer
Engineer
Posts: 87
Joined: 24 Nov 2014 21:13
Location: At a desk
Contact:

Re: Multiple instance issue with PlannerAI P.S. Hello Forums

Post by jimbob »

Thanks Frosch that's useful info I guess I can apply some factor that rewards being closer to the centre of town over number of houses.

I'm glad that the api gives the ai the same powers as a human in that it's both not cheaty and easier to write script. However a human can not only see house size but also query tiles to get building names. Does the ai have a function to get that?

Ps just had a brain wave the larger buildings tend to have higher goods acceptance could use that to try and build a clearer picture of a town for the ai.
Image
Real life transport planner
My projects:Link to my UK Scenario|Scenario Builder GS
Do check out my 3D unity transport game: transporter
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: Google [Bot] and 30 guests