SimpleAI v14 - trying to remake the old AI

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

User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: SimpleAI v4 - trying to remake the old AI

Post by SirkoZ »

Lord Aro wrote:Again, the old ai would have made huge long wooden bridges, and certainly never upgrade them
Coulda, woulda, shoulda... I'll let Brumi decide. ;-)
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: SimpleAI v4 - trying to remake the old AI

Post by Brumi »

I think separating roads (or rather just using the existing one) can be done by modifying some pathfinder penalties. I cannot do serious modifications to the pathfinder simply because I don't understand the mechanics of it (I'm really bad at code reading :( ). So, feel free to do some experiments with those penalties around line 545 in builder.nut. I think _cost_no_existing_road is the key here.
There are some cases when multiple instances of SimpleAI try to build roads at the same place (it occurs quite often when they're racing for a subsidy), then they find their routes independent of each other before one road is built. So, the second company to finish the pathfinding doesn't consider the road built by the first one, because it was built while the second company was already pathfinding. This may result in close parallel roads.

Upgrading bridges could be quite hard as tracking roads is not very easy, especially in towns. Tracking rails is possible, but so far the most conveninent solution would be to scan the whole map for bridges owned by the company and then upgrade them if possible. But this would take a really big hit on the AI's performance CPU-wise. Any better ideas?

Anyway, I have an easy job when talking about these little inconveniences because 'the old AI also did it like that' argument is always there :P
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: SimpleAI v4 - trying to remake the old AI

Post by SirkoZ »

Brumi wrote:...Any better ideas?

Anyway, I have an easy job when talking about these little inconveniences because 'the old AI also did it like that' argument is always there :P
:lol:

Indeed. I'll go through the builder.nut to see if I can make something useful.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: SimpleAI v4 - trying to remake the old AI

Post by Kogut »

Upgrading bridges could be quite hard as tracking roads is not very easy, especially in towns. Tracking rails is possible, but so far the most conveninent solution would be to scan the whole map for bridges owned by the company and then upgrade them if possible. But this would take a really big hit on the AI's performance CPU-wise. Any better ideas?
AIBridgeList?
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: SimpleAI v4 - trying to remake the old AI

Post by Brumi »

As far as I know it lists the available bridges, not the already built ones. Or maybe I'm confusing it with AIBridgeList_length... :?
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: SimpleAI v4 - trying to remake the old AI

Post by Kogut »

Brumi wrote:As far as I know it lists the available bridges, not the already built ones. Or maybe I'm confusing it with AIBridgeList_length... :?
Sorry. So it may be necessary to create array of all bridges, updated during construction.
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
ABCRic
Engineer
Engineer
Posts: 27
Joined: 10 Aug 2010 17:32

Re: SimpleAI v4 - trying to remake the old AI

Post by ABCRic »

SimpleAI crashed right after loading savegame.
Attachments
SimpleAI_Crash.PNG
SimpleAI_Crash.PNG (27.9 KiB) Viewed 4436 times
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: SimpleAI v4 - trying to remake the old AI

Post by Brumi »

Thank you for the report. :)
Here the game was saved right after it was loaded, wasn't it? The AI hadn't finished its loading routines when the game was saved again.
Two consecutive save/loads in a very short time may cause problems when there is an unfinished route, as the AI may not be able to remove it properly. But a crash is too much here - I will look into it.


And another thing: can any of you recommend me an AI which has easily understandable terraforming routines? Placing bigger airports without terraforming is quite ineffective.
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

Re: SimpleAI v4 - trying to remake the old AI

Post by Michiel »

ChooChoo has a simple "flatten this area" routine. It's in builder_misc.nut (LevelTerrain).
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: SimpleAI v4 - trying to remake the old AI

Post by Lord Aro »

Don't know about effective, but Chopper terraforms for its helipad things
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: SimpleAI v4 - trying to remake the old AI

Post by Zuu »

PAXLink do some landscaping when placing airports. I don't remember how good it is implemented though. It is probably quite simple with 4-5 retries.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
ABCRic
Engineer
Engineer
Posts: 27
Joined: 10 Aug 2010 17:32

Re: SimpleAI v4 - trying to remake the old AI

Post by ABCRic »

Brumi wrote:Thank you for the report. :)
Here the game was saved right after it was loaded, wasn't it? The AI hadn't finished its loading routines when the game was saved again.
Two consecutive save/loads in a very short time may cause problems when there is an unfinished route, as the AI may not be able to remove it properly. But a crash is too much here - I will look into it.
No, I loaded the game and a couple of seconds later the AI crashed. I didn't have to do anything.

Edit: Never mind that. The savegame autosaves right after I open it.
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: SimpleAI v4 - trying to remake the old AI

Post by Brumi »

Thanks for the answers, I will have a look at those terraforming routines.

@ABCRic: Anyway, if you still have the last save before the crash, you can recover it and continue playing if you disable autosave for a while (I mean, make sure it doesn't save the game right after loading). Of course I'm trying to fix this problem for the next version, but reading and understanding my own code after so much time is giving me a hard time :?
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: SimpleAI v4 - trying to remake the old AI

Post by Brumi »

Plane support is here! At least to some extent... :D
I'd like to show you what I've been working on recently, plane support is coming for SimpleAI v5. The alpha version is downloadable from the first post. (it's not there on Bananas)

Of course there is a bunch of things missing, for example, terraforming for airports is still not there. So the AI will only be able to build airports on flat, smooth terrain. The integration of planes into the existing system is also not the best, currently it will try to build airports in roughly 10% of the cases if the other two means of transport are also enabled. Tell me if this should be higher/lower/configurable.

Currently the AI tries to build only 1 airport per city. It uses four types of airports: small, commuter, large and metropolitan. I also tried to set a maximum value of planes for each type of airport, so planes won't be added if this value is reached. So, the current airport capacities:
Small: 4
Commuter: 6
Large: 6
Metropolitan: 8
I set these values without any experimenting, so tell me if you think that these airports can handle more/less aircraft.
If you see large planes landing on small airports, or that there are more planes using an airport than the given value above, then it's a bug, please report it to me.

Also, any other bug reports are welcome. Happy testing! :D
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: SimpleAI v4 - trying to remake the old AI

Post by Kogut »

I think that number of allowed planes should depend on distance between airports (like in AIAI) and their speed (slower/faster planes, speed multiplier).
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: SimpleAI v4 - trying to remake the old AI

Post by Brumi »

You're right, but as only one airport is built per town, one airport may handle multiple routes. So, if the AI wants to build an air connection from A to B, and A already has an airport, then it will use the existing airport. Then wtih mutiple routes it would be hard to determine the capacity dynamically.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: SimpleAI v4 - trying to remake the old AI

Post by Kogut »

It is not very hard (from AIAI, free to reuse and modify (I would like to hear about improvements)):

Code: Select all

function AirBuilder::IsItPossibleToAddBurden(stacja, tile, engine, ile=1)
{
local maksimum;
local total = this.GetBurden(stacja);
local airport_type = AIAirport.GetAirportType(AIStation.GetLocation(stacja));
if(airport_type==AIAirport.AT_LARGE) maksimum = 1500; 
if(airport_type==AIAirport.AT_METROPOLITAN ) maksimum = 2000;
if(airport_type==AIAirport.AT_COMMUTER) maksimum = 500; 
if(airport_type==AIAirport.AT_SMALL) maksimum = 600; 
 
if(AIAI.GetSetting("debug_signs_for_airports_load")) AISign.BuildSign(AIStation.GetLocation(stacja), total + " (" + maksimum + ")");

total+=ile*this.Burden(AIStation.GetLocation(stacja), tile, engine);

return total <= maksimum;
}

function AirBuilder::GetBurden(stacja)
{
local total;
local total = 0;
local airlist=AIVehicleList_Station(stacja);
for (local plane = airlist.Begin(); airlist.HasNext(); plane = airlist.Next())
   {
   total += this.Burden(AIOrder.GetOrderDestination (plane, 0), AIOrder.GetOrderDestination (plane, 1), AIVehicle.GetEngineType(plane));
   }
   
return total;
}

function AirBuilder::Burden(tile_1, tile_2, engine)
{
return AIEngine.GetMaxSpeed(engine)*200/(this.GetEffectiveDistanceBetweenAirports(tile_1, tile_2)+50);
}

function AirBuilder::GetEffectiveDistanceBetweenAirports(tile_1, tile_2)
{
local x1 = AIMap.GetTileX(tile_1);
local y1 = AIMap.GetTileY(tile_1);

local x2 = AIMap.GetTileX(tile_2);
local y2 = AIMap.GetTileY(tile_2);

local x_delta = abs(x1 - x2);
local y_delta = abs(y1 - y2);

local longer = max(x_delta, y_delta);
local shorter = min(x_delta, y_delta);

return shorter*99/70 + longer - shorter;
}
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: SimpleAI v4 - trying to remake the old AI

Post by SirkoZ »

Brumi wrote:Plane support is here! At least to some extent... :D
I'd like to show you what I've been working on recently, plane support is coming for SimpleAI v5. The alpha version is downloadable from the first post. (it's not there on Bananas)

Of course there is a bunch of things missing, for example, terraforming for airports is still not there. So the AI will only be able to build airports on flat, smooth terrain. The integration of planes into the existing system is also not the best, currently it will try to build airports in roughly 10% of the cases if the other two means of transport are also enabled. Tell me if this should be higher/lower/configurable.

Currently the AI tries to build only 1 airport per city. It uses four types of airports: small, commuter, large and metropolitan. I also tried to set a maximum value of planes for each type of airport, so planes won't be added if this value is reached. So, the current airport capacities:
Small: 4
Commuter: 6
Large: 6
Metropolitan: 8
I set these values without any experimenting, so tell me if you think that these airports can handle more/less aircraft.
If you see large planes landing on small airports, or that there are more planes using an airport than the given value above, then it's a bug, please report it to me.

Also, any other bug reports are welcome. Happy testing! :D
Thank you for the nice surprise. Those numbers of aircraft per airport seem right to me as I use similar numbers myself. Chance of building airports should be as it was with the old AI - that is if it fails building other two transport types, as you suggested, Brumi.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: SimpleAI v4 - trying to remake the old AI

Post by Zuu »

Another approach is to set a limit of the allowed queue length of the airport. PAXLink has the same limit for all airports (I think it is 8 ). Airports with a higher throughput will be allowed to take more aircrafts without tailoring a specific limit to each airport. That said, the queue length code requires some airport-specific definitions of the holding pattern since the API do not include any function to query the number of airplanes in holding (queue).
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: SimpleAI v4 - trying to remake the old AI

Post by Brumi »

A new beta version for v5 is available, this time with terraforming included 8)
I've used CostToFlattern() from PAXLink (oops, I forgot to credit it in the code, but it will be there in the next release :) ), and the whole Terraform.nut from NoCAB. So the AI will be able to build airports even on hilly terrain.
Another feature I added is that existing bridges are upgraded if there are newer bridges available.

The bad news is that I rarely write code these days, so some features I was planning to add for v5 (like dynamic airport capacities on the advice of Kogut, and maybe longer trains and stations) will only be there in v6 or later. I'll upload this version to Bananas if there are no bug reports. If I have a free day or two, I may start working on it again, but now it's a bit hard for me.

And I have a question:
In what cases does this code fail?

Code: Select all

group = AIGroup.CreateGroup(AIVehicle.VT_AIR);
local groupname = AICargo.GetCargoLabel(crg) + " - " + AIStation.GetName(stasrc);
if (groupname.len() > 29) groupname = groupname.slice(0, 28);
AIGroup.SetName(group, groupname);
I know that group names must be unique, but I'm getting names like 'group 2' just too frequently.

Keep me informed if there is a bug in the new version :)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 0 guests