Beginning AI Development

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
SMurf
Engineer
Engineer
Posts: 7
Joined: 23 Jun 2011 20:33

Beginning AI Development

Post by SMurf »

Hello,

Long time reader here, just managed to finally get my account activated. I've been playing TTD since it came out and when OpenTTD first appeared I was pleased to see some progression in my favourite game.
As the game no longer has an intrinsic AI however, I often find my games are a bit... one-sided.

To be fair, I have installed AdmiralAI and found it's very good at developing its bottom line, but I wouldn't say that it competes directly with you.

So I took the brave step of branching into AI development last weekend. I'm quite good at C, so writing Squirrel doesn't represent a challenge, learning the concepts however takes more time.
At this point, I have an AI that:-
  • Upon startup, scans for the most populous town above 500 and designates it "home", setting its name to <town> + " Transport".
  • Attempts to build a HQ there that satisfies some rules (flat land, adjacent to a road that is within the local authority's influence).
What I want now is for the AI to attempt to leech off roads that you build, connecting nearby industries and towns (but not delivering passengers, only cargo).
My main motivation for this is that I find that most AIs build terrible roads and railways, not taking the lie of the land into account.
It also highlights to human players how they are not exploiting their own infrastructure properly.

I think that I need to get a list of all industries, and each tick scan for one falling within range of a road and building a lorry station there.

However, I have a couple of Q's:-
  • Is there away for an AI to cleanly "abort" itself if it doesn't think that it'll be much good on a particular map? (I would like my AI to refuse to start at a time when there are no towns above 500 pop.)
  • If more than one instance of my AI is generated in game, its company usually takes on the name of the manager, for some reason. Why?
  • Is there a standard company name generator available? I'm honestly not bothered what it calls itself, only how it plays.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Beginning AI Development

Post by Yexo »

SMurf wrote:[*]Is there away for an AI to cleanly "abort" itself if it doesn't think that it'll be much good on a particular map? (I would like my AI to refuse to start at a time when there are no towns above 500 pop.)
Basically no. I'd say returning from the main function is a clean way, however that still results in a crash report. Alternatively you could raise an exception with a nice message, like "No towns with population > 500, aborting".
[*]If more than one instance of my AI is generated in game, its company usually takes on the name of the manager, for some reason. Why?
Company names have to be unique. If you try to set a company name that's already in use it'll fail (and therefor use a default name).
[*]Is there a standard company name generator available? I'm honestly not bothered what it calls itself, only how it plays.
Just don't set any company name at all and you'll get some default.
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: Beginning AI Development

Post by krinn »

SMurf wrote:What I want now is for the AI to attempt to leech off roads that you build, connecting nearby industries and towns (but not delivering passengers, only cargo).
My main motivation for this is that I find that most AIs build terrible roads and railways, not taking the lie of the land into account.
It also highlights to human players how they are not exploiting their own infrastructure properly.
Just use the "standard" road pathfinding to check a path from your industry to its destination just disallow any constructions, alternativly you can build your own search for valid route routine or reuse one (i have one in my AI).
If route is found, then it's because someone else build it, goal reach, you've found a ready made route for your vehicles.
SMurf wrote: [*]Is there away for an AI to cleanly "abort" itself if it doesn't think that it'll be much good on a particular map? (I would like my AI to refuse to start at a time when there are no towns above 500 pop.)
You shouldn't do that imo, before the why comes, here's why :
- Abording your AI won't let (well with my version) openttd reload another random AI, openttd will just keep your dead/cleanly end AI there. So from user point of view, your AI is a bugger, it doesn't work and stop working for no real reason (seriously not founding a town with 500 people isn't really a good condition to stop your AI)
- What if you don't stop your AI ? Well, let it just wait for that condition to be met, and except (not even sure towns doesn't grow themselves) your AI is alone on the map, someone will make towns grow bigger (a player or even another AI).
So just output a message saying you didn't find your town with 500 people and just loop until your AI find one, because this will happen soon or later, and your AI will then could work as it should.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 7 guests