Another idea for NoAI APi improvement

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
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Another idea for NoAI APi improvement

Post by Bilbo »

As humans can watch certain vehicles and stations to improve their performance, I think some functions could be added to NoAI API to make that a little bit easier for noAI too.

Now noAI can poll the vehicles, but to see what vehicles are around certain area, AI have to query for all vehicles and then filter them by position.

Adding AIVehicleList_Area(Tile, radius) that will return all vehicles in certain area (the maximal value of radius could be limited to some "reasonable value") would help AI's to monitor performance of vehicles in certain areas and detect queues or another unwanted situations (simply seeing too much vehicles in one area mean usually trouble)
Humans can look at place in map and see there is a problem. This will help AI's to do the same with less CPU time consumed.

Another usable addition would be adding event like AIEventStationFirstVehicle, but that will be triggered for every arrival, not just first. As that could generate lots of event, it could be disabled by default, and AI would have to enable it for certain vehicles or stations of interest - those the AI wish to monitor closely (similar to humans opening another viewports or windows with desired station or vehicle)
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Another idea for NoAI APi improvement

Post by Yexo »

Bilbo wrote:As humans can watch certain vehicles and stations to improve their performance, I think some functions could be added to NoAI API to make that a little bit easier for noAI too.

Now noAI can poll the vehicles, but to see what vehicles are around certain area, AI have to query for all vehicles and then filter them by position.

Adding AIVehicleList_Area(Tile, radius) that will return all vehicles in certain area (the maximal value of radius could be limited to some "reasonable value") would help AI's to monitor performance of vehicles in certain areas and detect queues or another unwanted situations (simply seeing too much vehicles in one area mean usually trouble)
Humans can look at place in map and see there is a problem. This will help AI's to do the same with less CPU time consumed.
The cpu win compared to the following is very small:

Code: Select all

function VehicleDistanceToTile(vehicle, tile) {
  return AIMap.DistanceManhattan(AIVehicle.GetLocation(vehicle), tile);
}
local list = AIVehicleList();
list.Valuate(VehicleDistanceToTile, some_tile);
list.KeepBelowValue(5);
/* list now contains all vehicles in a 5-tile radius around some_tile. */
Another usable addition would be adding event like AIEventStationFirstVehicle, but that will be triggered for every arrival, not just first. As that could generate lots of event, it could be disabled by default, and AI would have to enable it for certain vehicles or stations of interest - those the AI wish to monitor closely (similar to humans opening another viewports or windows with desired station or vehicle)
Personally I don't see a need for this. If you want to follow a vehicle closely, just query it a lot.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Another idea for NoAI APi improvement

Post by Zuu »

I'd say that AIs written with NoAI API already has the capability to be much better than human on monitoring large areas that a human. Although it will only monitor things you have programmed it to monitor.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Re: Another idea for NoAI APi improvement

Post by Bilbo »

Yexo wrote:The cpu win compared to the following is very small:

Code: Select all

function VehicleDistanceToTile(vehicle, tile) {
  return AIMap.DistanceManhattan(AIVehicle.GetLocation(vehicle), tile);
}
local list = AIVehicleList();
list.Valuate(VehicleDistanceToTile, some_tile);
list.KeepBelowValue(5);
/* list now contains all vehicles in a 5-tile radius around some_tile. */
Hmm .... this could probably work, though for large maps (2048 * 2048) and lots of vehicles (hundreds/thousands) it may start to be a little ineffective. I wonder if the code used to display vehicles on screen does have any optimization to select only vehicles visible in the shown area or if it simply uses similar iteration over all vehicles in each frame to determine if they lie withing the displayed boundaries - ineffective if only small fraction of many vehicles (for such large maps, thousands of vehicles is nothing uncommon) is actually visible ...
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
Pat
Engineer
Engineer
Posts: 9
Joined: 03 Sep 2006 12:06
Contact:

Re: Another idea for NoAI APi improvement

Post by Pat »

I think main problem is that there isn't any way how to see competitor's vehicles (or is it possible?). It makes it really hard to detect traffic jams. Same problem is with stations.
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Another idea for NoAI APi improvement

Post by planetmaker »

Bilbo wrote:Hmm .... this could probably work, though for large maps (2048 * 2048) and lots of vehicles (hundreds/thousands) it may start to be a little ineffective. I wonder if the code used to display vehicles on screen does have any optimization to select only vehicles visible in the shown area or if it simply uses similar iteration over all vehicles in each frame to determine if they lie withing the displayed boundaries - ineffective if only small fraction of many vehicles (for such large maps, thousands of vehicles is nothing uncommon) is actually visible ...
How do you determine whether something is within a given area except checking every vehicle? Even a sorting algorithm needs to do that and wouldn't gain speed over checking x and y directly...
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Another idea for NoAI APi improvement

Post by Zutty »

Pat wrote:I think main problem is that there isn't any way how to see competitor's vehicles (or is it possible?). It makes it really hard to detect traffic jams. Same problem is with stations.
I've not tested it, but something like this should work...

Code: Select all

function HasTraffic(tile) {
  local _ = AITestMode();
  return  (!AITile.DemolishTile(tile) && AIError.GetLastError() == AIError.ERR_VEHICLE_IN_THE_WAY);
}
ERR_VEHICLE_IN_THE_WAY takes precedence over other errors, at least it does in the GUI, and so you should get this error even if theres a vehicle on a road you don't own.

I'm still working on my traffic handling, but I maintain a list of tiles that are known to have traffic on then. e.g. if construction on a tile failed because of ERR_VEHICLE_IN_THE_WAY I remember it. If it happens enough times then the tile is black-listed.
PathZilla - A networking AI - Now with tram support.
User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Re: Another idea for NoAI APi improvement

Post by Bilbo »

planetmaker wrote:
Bilbo wrote:Hmm .... this could probably work, though for large maps (2048 * 2048) and lots of vehicles (hundreds/thousands) it may start to be a little ineffective. I wonder if the code used to display vehicles on screen does have any optimization to select only vehicles visible in the shown area or if it simply uses similar iteration over all vehicles in each frame to determine if they lie withing the displayed boundaries - ineffective if only small fraction of many vehicles (for such large maps, thousands of vehicles is nothing uncommon) is actually visible ...
How do you determine whether something is within a given area except checking every vehicle? Even a sorting algorithm needs to do that and wouldn't gain speed over checking x and y directly...
Well, there are data structures (like quad trees for example) that can help with that, so if you query for vehicles within certain rectangle, you do not need to go through all vehicles.

There are some hash tables for collision detection and for updating viewport, so probably there is faster way (in terms of CPU cycles) to retrieve all vehicles within given rectangle than going through all vehicles in the game. Now just export such fiunction so it is available to AI's :)
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
Pat
Engineer
Engineer
Posts: 9
Joined: 03 Sep 2006 12:06
Contact:

Re: Another idea for NoAI APi improvement

Post by Pat »

Zutty wrote: I've not tested it, but something like this should work...

Code: Select all

function HasTraffic(tile) {
  local _ = AITestMode();
  return  (!AITile.DemolishTile(tile) && AIError.GetLastError() == AIError.ERR_VEHICLE_IN_THE_WAY);
}
ERR_VEHICLE_IN_THE_WAY takes precedence over other errors, at least it does in the GUI, and so you should get this error even if theres a vehicle on a road you don't own.
I'm still working on my traffic handling, but I maintain a list of tiles that are known to have traffic on then. e.g. if construction on a tile failed because of ERR_VEHICLE_IN_THE_WAY I remember it. If it happens enough times then the tile is black-listed.
wow, it's really smart idea.
But i still think than for new versions we need posibility to get more info about competitors staions (rating) and vehicles. In this area AI has less informations than player.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests