How does AITestMode work internally?

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
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

How does AITestMode work internally?

Post by Zutty »

OK lets christen the new subforum with a tricky question...

How does AITestMode work?
  • Is construction made in test mode persistent, i.e. if I build a station on the same spot twice, will the seond attempt give an error (I assume it works like this, but I'm just checking to be certain)
  • If its persistent, does that only last until the AITestMode instance is destroyed or doe it persist into every subsequent AITestMode (i.e. is there only one test mode that you switch back and forth from)?
  • How is the test mode represented? Is it like a secondary map array or perhaps a diff against the map array?
  • Would it be even slightly possible to have multiple instances of AITestMode, each with different changes?
I ask because I'm trying to think of a way to allow terraforming while path-finding.

At present, my AI checks to see if any proposed construction (e.g. the next road tile) is valid before building, as I'm sure everyone else's does. The problem is that it can only check against the CURRENT state of the map and not against the map how it WILL BE when all the construction up to that point has been done.

For instance the AI might build a tunnel, then loop around and try to build another tunnel that crosses it. I know this is a very bad example and I wouldn't actually make it do this, but its just to illustrate a point. The first tunnel will be built fine, but since it wont actually build it until AFTER the whole path has been planned then when the AI check to see if the second tunnel is valid it will succeed, though when it tries to build it it will fail.

The problem is that the path-finder has no awareness of the construction that it is GOING to do. This isn't such a problem if its only building roads but what about if the pathfinder attempts terraforming? Terraforming affects more than tile, and so could affect part of a path that we have already found and perhaps parts of the path that we haven't reached yet. In order to avoid this problem the AI would need some way of keeping track of all the terraforming and construction it plans to do.

I thought of three ways of doing this...
  • Do all terraforming at any proposed node live in the real world
    • Would waste money and turn half the map brown!
  • Accumulate all the planned terraforming in each path node (an array?) and then each time and then perform it all in AITestMode for every node
    • Doesn't affect the real world until we need to, but wastes time and would slow down path-finding tremendously
  • Keep a persistent instance of AITestMode for each open node and accumulate all terraforming in that, branching from that instance to create new nodes
    • Faster as theres no need to keep re-doing the same terraforming over and over, but would use lots of memory and might not be possible!!
What do you think? Do you think I am going about this the wrong way?!

Thanks :)
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: How does AITestMode work internally?

Post by Yexo »

Zutty wrote:OK lets christen the new subforum with a tricky question...

How does AITestMode work?
  • Is construction made in test mode persistent, i.e. if I build a station on the same spot twice, will the seond attempt give an error (I assume it works like this, but I'm just checking to be certain)
No, it's not persistant. I think this makes it useless to answer your other questions.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: How does AITestMode work internally?

Post by Zutty »

Yexo wrote:No, it's not persistant. I think this makes it useless to answer your other questions.
ARGH! Shot down in flames!!! :lol:

Oh well... not only does that make my terraforming ideas useless, I'm going to have to re-think some other stuff as well!! C'est la vie!

Thanks Yexo :)
PathZilla - A networking AI - Now with tram support.
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: How does AITestMode work internally?

Post by TrueBrain »

In fact, it would be impossible to make it persistant.. sorry :)

AITestMode simply checks if one given thing can be build, and then forgets ALL about it. So even if you try it 1000 times, it will succeed :)

I completely agree that what you assume would be so much easier .. but it can't be done :(
The only thing necessary for the triumph of evil is for good men to do nothing.
libik
Engineer
Engineer
Posts: 43
Joined: 18 Nov 2006 11:39

Re: How does AITestMode work internally?

Post by libik »

Just googled this recently :). To also answer the questions I had, I tried to build station (it is buildable on that location).
The result of following code:

Code: Select all

    
    local x = AITestMode();
    local result = AIRail.BuildRailStation(location, AIRail.RAILTRACK_NE_SW, 1, 7, AIStation.STATION_NEW);
    AILog.Info(result);
    local result = AIRail.BuildRailStation(location, AIRail.RAILTRACK_NE_SW, 1, 7, AIStation.STATION_NEW);
    AILog.Info(result);
    x = null;
    local result = AIRail.BuildRailStation(location, AIRail.RAILTRACK_NE_SW, 1, 7, AIStation.STATION_NEW);
    AILog.Info(result);
    local result = AIRail.BuildRailStation(location, AIRail.RAILTRACK_NE_SW, 1, 7, AIStation.STATION_NEW);
    AILog.Info(result);
 
is

true, true, true, false

The first two "true" are in testing mode = proving that there is no history attached to it.
Then it builds the actual station (returning true), then we cannot create new station over existing one (returning false at the end).

Also the squirell seems not using any kind of complex "garbage collector" - once the instance is not reachable, it cease to exist = test mode exists immediatelly.
Hi all
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 23 guests