Some API extension ideas

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
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

Re: Some API extension ideas

Post by MinchinWeb »

Well it seems that Zuu has figured out a way to tell an AI to build the ship, so the pressing need is gone.

I still like the idea of including Order commands. As it sits right now, a GameScript and build the station and depot, run the pathfinder, build a ship, start a ship moving, it just can't tell it where to go...
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

Re: Some API extension ideas

Post by MinchinWeb »

Is it possible to just make one tile a one-way road? It seems that to build one-way road, you need to supply two tiles and it you can't turn the first tile into a one-way road, neither is converted to be a one-way road.
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

AIStation::GetMonthlyCargoSupply(StationID station_id, CargoID cargo_id) - to access new information available to players (via station window, after selecting ratings)

AIStation::HasCargoAcceptance(StationID station_id, CargoID cargo_id) - to access information available to players since forever (via station window, after selecting ratings)

Both are missing and I just wanted to use it.
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Some API extension ideas

Post by Zuu »

Kogut wrote:AIStation::HasCargoAcceptance(StationID station_id, CargoID cargo_id) - to access information available to players since forever (via station window, after selecting ratings)
Using AICargoList.StationAccepting I think that you can create a Squirrel wrapper for the requested method.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

Zuu wrote:
Kogut wrote:AIStation::HasCargoAcceptance(StationID station_id, CargoID cargo_id) - to access information available to players since forever (via station window, after selecting ratings)
Using AICargoList.StationAccepting I think that you can create a Squirrel wrapper for the requested method.
Thanks, I missed this list!
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: Some API extension ideas

Post by krinn »

Kogut wrote:
Zuu wrote:
Kogut wrote:AIStation::HasCargoAcceptance(StationID station_id, CargoID cargo_id) - to access information available to players since forever (via station window, after selecting ratings)
Using AICargoList.StationAccepting I think that you can create a Squirrel wrapper for the requested method.
Thanks, I missed this list!
And for this functionnality, AIStation.HasRating == HasCargoAcceptance in newer API.

edit: the function is HasRating is Changelog, but the real one is there http://noai.openttd.org/docs/trunk/clas ... 2d4b3a545e
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

It would be useful to have AIEngine::GetCargoLoadingSpeed(engine_id, cargo_id).
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Some API extension ideas

Post by planetmaker »

Kogut wrote:It would be useful to have AIEngine::GetCargoLoadingSpeed(engine_id, cargo_id).
That's at least numerically not available to the user either. Granted, some NewGRFs give a hint on the order of speed in the extra text (and possibly it could be done so w/o the extra text even), thus it is IMHO a valid request.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

It is a change, not expansion but I think it fits here.

This is one of the most confusing things:

Code: Select all

enum AIAirport::AirportType

The types of airports available in the game.

Enumerator:
    AT_SMALL 	The small airport.
    AT_LARGE 	The large airport.
    AT_METROPOLITAN 	The metropolitan airport.
    AT_INTERNATIONAL 	The international airport.
    AT_COMMUTER 	The commuter airport.
    AT_INTERCON 	The intercontinental airport.
    AT_HELIPORT 	The heliport.
    AT_HELISTATION 	The helistation.
    AT_HELIDEPOT 	The helidepot.
    AT_INVALID 	Invalid airport. 

Code: Select all

    AT_SMALL 	The small airport.
    AT_LARGE 	The large airport.
Is it applied to the single airport or is it about two groups (whatever large planes can safely fly)?

It seems that it is about single airports - so why city airport is named large? And is it possible to order this in the same way as it is in interface (it is enough to change position of commuter airport)?

Code: Select all

enum AIAirport::AirportType

The types of airports available in the game.

Enumerator:
    AT_SMALL 	The small airport.
    AT_COMMUTER 	The commuter airport.
    AT_LARGE 	The large airport.
    AT_METROPOLITAN 	The metropolitan airport.
    AT_INTERNATIONAL 	The international airport.
    AT_INTERCON 	The intercontinental airport.
    AT_HELIPORT 	The heliport.
    AT_HELISTATION 	The helistation.
    AT_HELIDEPOT 	The helidepot.
    AT_INVALID 	Invalid airport. 
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: Some API extension ideas

Post by Rubidium »

Kogut wrote:It seems that it is about single airports - so why city airport is named large?
Historical reasons; in the beginning there was a heliport, and a small and large airport.
Kogut wrote:And is it possible to order this in the same way as it is in interface (it is enough to change position of commuter airport)?
No. That would change the API (read: old saved data from AIs) and break in case NewGRFs were to provide airports. I think there should be a CanLandAtStation(AirportType, PlaneType) function, which should return false for PT_BIG_PLANE at "small" airports (those where big planes crash more often). Though this slight misleading name should then be documented: "Big planes can land at all airports where small planes can land, but for certain airports the chance of crashing the big plane is significantly increased. Thus this function return false to try to prevent causing unneeded plane crashes"
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

CanSafelyUseStation?
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: Some API extension ideas

Post by krinn »

As long as airporttype are enum, nothing is critically need, except for an AI dev that have never played openttd. It "may" disturb an AI if a newGRF change a big airport to a small one (never saw that yet).
So, i would say an "AIAirport.GetAirportTypeList" should goes first. Until this exist, it means the list as an enum because wrote in stone rock, and so are the airport properties.


As this is suggestion, i would prefer getting the numerical value of current rating in town instead of "poor...". This will allow us to know before doing an action the cost to the town rating http://wiki.openttd.org/Game_mechanics# ... ity_rating

like removing a tree (cost 35) may put you from mediocre (200) down to mediocre (165) or from mediocre (10) down to poor (-25)
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

This one is completely hidden from player, so it may be intentionally obscured information.
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Some API extension ideas

Post by Zuu »

You may want to look for my AI airport API extension patch queue. I dont remember if it is just a forum thread or an fs task. It does split Airport and AirportType apart an address small/large planes etc in preparation for NewGRF airports. I remember that I had it difficult to implement a method to get number of aircraft in holding.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Wormnest
Engineer
Engineer
Posts: 117
Joined: 14 Jul 2013 12:33
Location: Netherlands

Re: Some API extension ideas

Post by Wormnest »

Since the inclusion of cargo distribution in trunk an AI should have a way to determine where the cargo wants to go. As far as I can see that's not available in AIStation.
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: Some API extension ideas

Post by R2dical »

GSTile::GetClosestIndustry()

Return the ID of the industry.

Otherwise, are there any functions or existing libraries that can do this? I had a look around but could find nothing (or nothing simple). I did find what I think are these same functions in the OpenTTD source docs so maybe its simple to implement?

I'm trying to use these features in a GS that builds industries based on some limitations, like max per town, or min distance from town/other industry :)

While I'm at it what would also be great is to get a grid of the town size, say:

GSTown::GetTownSize()

Returning a grid of the min and max x and y coords of town buildings.

Both these would be great for NoAI also, like for trains pathfinding.
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: Some API extension ideas

Post by krinn »

Code: Select all

GetClosestIndustry(Tile) {
local indu = AIIndustryList();
indu.Valuate(AIIndustry.GetDistanceManhattanToTile, Tile);
indu.Sort(AIList.SORT_BY_VALUE, AIList.SORT_ASCENDING);
return indu.Begin(); }
That is pretty simple to get, that's why none provide it.
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: Some API extension ideas

Post by R2dical »

Ahh of course, didn't understand the Valuate function so well so guess I overlooked it...Thanks! That flicked another lightbulb for me and the API :idea:

Also I did now find a .GetTownTiles() function in Superlib, but it uses an estimate:

Code: Select all

local radius = 20 + _SuperLib_Helper.Max(0, GSTown.GetPopulation(town_id) / 1000);

Maybe a loop function for each house might be too slow...
Steffl
Engineer
Engineer
Posts: 103
Joined: 23 Feb 2010 15:44

Re: Some API extension ideas

Post by Steffl »

Hi,
finding house tiles is tricky.
I wrote a function for you which returns all tiles which produce mail. In a game without newGRFs this are only house tiles. But don't use this function to often because it is really very slow.

Code: Select all

function HouseList(town){
local mailID=2;//CargoId for mail
local tiles=AITileList();
if (!AITown.IsValidTown(town))return tiles;
local location=AITown.GetLocation(town);
local townX=AIMap.GetTileX(location);
local townY=AIMap.GetTileY(location);
local maxSize=25+(AITown.GetHouseCount(town)/20).tointeger();//to limit the tiles we need to check
local from=AIMap.GetTileIndex(((townX<=maxSize)?1:townX-maxSize),((townY<=maxSize)?1:townY-maxSize));
local to=AIMap.GetTileIndex(((townX+maxSize>=AIMap.GetMapSizeX()-2)?AIMap.GetMapSizeX()-2:townX+maxSize),((townY+maxSize>=AIMap.GetMapSizeY()-2)?AIMap.GetMapSizeY()-2:townY+maxSize));
tiles.AddRectangle(from,to);
tiles.Valuate(AITile.GetClosestTown);
tiles.KeepValue(town);
tiles.Valuate(AITile.GetCargoProduction,mailID,1,1,0);
tiles.RemoveValue(0);
return tiles;
}

To get the max./ min. X and Y coordinate of a town you can do this for example:

Code: Select all

local houseList=AIList();
houseList.AddList(HouseList(town));
local xmax=-1;
local xmin=-1;
local ymax=-1;
local ymin=-1;
if(!houseList.IsEmpty()){
houseList.Valuate(AIMap.GetTileX);
houseList.Sort(AIAbstractList.SORT_BY_VALUE, false);
xmax=houseList.Begin();
houseList.Sort(AIAbstractList.SORT_BY_VALUE, true);
xmin=houseList.Begin();
houseList.Valuate(AIMap.GetTileY);
houseList.Sort(AIAbstractList.SORT_BY_VALUE, false);
ymax=houseList.Begin();
houseList.Sort(AIAbstractList.SORT_BY_VALUE, true);
ymin=houseList.Begin();
}
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: Some API extension ideas

Post by R2dical »

Many thanks for the code :o I've been using your great RoadrunnerAI as a reference to see how some squirrel things work, not many comments though :P

My current shot at this function uses the minchin.metalib spiral walker to start at town centre, then loop for 0 to AITown.GetHouseCount(Town), putting every tile with AITile.GetTownAuthority(Town) into a list. I got stuck though at sorting the lists...also not sure if AITile.GetTownAuthority(Tile) is only true for town buildings of if I need to skip for AITile.IsRoad(Tile). I am trying to keep NewGrf ability as I use TTRS and a few others.

I think this would be a quick and solid approach if there was AITile.IsHouse or AITile.IsIndustry in a similar function for industries.

But thanks again, I will give it a try :D
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 21 guests