AIAirport API improvements - towards better NewGRF support

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
1009
Engineer
Engineer
Posts: 81
Joined: 22 Mar 2009 07:24
Location: Haarlem, The Netherlands
Contact:

Re: AIAirport API improvements - towards better NewGRF suppo

Post by 1009 »

krinn suggested me to help on this project, I've been reading through it.
Zuu wrote:As for getting more information about airports via the API, now that you soon can't trust hard coded values anymore, I here just give an idea of what I have in my wish-list: (that I also think could be possible to implement) Some of these might not be possible with the current NewGRF Spec. In that case we AI authors will have to work with the NewGRF people to find a solution.

Code: Select all

AIAirport.GetHoldingQueueLength(airport_tile) // Get the number of aircrafts that are waiting to land on the airport
AIAirport.GetNumberOfRunways(airport_type) // Gets the number of runways that a given airport type have
AIAirport.GetNumberOfHelicopterLandingPlaces(airport_type) // Gets the number of places where helicopters can start and land
AIAirport.GetNumberOfLoadingBays(airport_type) // Gets the number of loading bays for aircrafts of a given airport type. 
Isn't it possible to rewrite AIAirport.GetNumberOfRunways(airport_type) to AIAirport.GetNumberOfRunways(airport_type, airplane_type), to take care for small, large and helis? I read about the problems with NewGRF airplanes unknown to the airport, but can't NewGRF airport programmers write AirplaneType_UNKNOWN (or whatever it is) to allow (or deny) 'other' aircrafts to land?

(You'll notice that I'm trying to get my head around the abstractions... so I might ask silly questions :))
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: AIAirport API improvements - towards better NewGRF suppo

Post by krinn »

Zuu wrote: Above in the same function this code makes sure that the plane_type is an expected type.
Miss it sorry, you are right :)
Keep up the good work
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: AIAirport API improvements - towards better NewGRF suppo

Post by Zuu »

1009: you want to combine GetNumberOfRunways(airport_type) with GetNumberOfHelicopterLandingPlaces(airport_type) and use a second parameter (plane_type) to decide if the number of runways or helicopter landing places should be returned?

A problem with this abstraction is that you then can't know if the X runways for small airplanes and Y runways for large aircrafts are the same or different runways. In the current OpenTTD you know for sure that large and small aircrafts use the same runways, but this abstraction implicitly suggests that that might not be the case in future.


Edit: However, we would need an API to tell if a plane-type is a helicopter or not in order to work with helicopters vs airplanes when the PlaneType enum get replaced with AIPlaneTypeList.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
1009
Engineer
Engineer
Posts: 81
Joined: 22 Mar 2009 07:24
Location: Haarlem, The Netherlands
Contact:

Re: AIAirport API improvements - towards better NewGRF suppo

Post by 1009 »

Zuu: that's right. I think the problem you see (not knowing for sure whether it's the same runway for small or large airplanes) is not a problem but rather a good thing: if NewGRF aiport coders decide to make an airport with 4 runways for both small and large aircraft and 2 express ones for smaller, then that would make it possible.
Same if they make something for a new kind of airplane (a spaceship, a UFO, a zeppelin) - although I'm not sure whether airplanes that aren't helis, large or small ones are supported by that library. Is that the PlaneType enum / AIPlaneTypeList?
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: AIAirport API improvements - towards better NewGRF suppo

Post by Zuu »

Update!

Screenshot
This Airport was built by my test AI
This Airport was built by my test AI
Hisköping Transport, 12th Oct 1950.png (71.37 KiB) Viewed 424 times
Patch queue: - Extends the API to allow usage of views.

Click on the links to read each patch, or view all changes:
all.patch
Patch with changes from r22893 to last patch in queue.
(44.3 KiB) Downloaded 230 times
Test AI
AirportAPITestAI.tar
My test AI for this API extension. (Note: reports same version number as the previously uploaded one - but differs in implementation)

Obviously, you need to apply all patches to use this AI.
(7.5 KiB) Downloaded 190 times
Known issues:
airport-api-split.patch introduces AIAirportType::IsValidPlaneType without using it. Only the patch after it will actually use the function. So perhaps introduction of the function should come in the patch after it.

AIAirportType::GetNearestTown and AIAirportType::GetNoiseLevelIncrease should be changed in airport-view-usage.patch to take views into account. However, there are issues that need to be solved in OpentTTD before that can happen.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: AIAirport API improvements - towards better NewGRF suppo

Post by Zuu »

I realize that I've missed out an important part in my last update, to explain what I've done. Something I will try to address here. I have also uploaded a win32 binary in case anyone want to try it out and find a windows binary useful.

Below follows a list of all airport related APIs as they exist when all patches in the patch queue has been applied. I have marked changes/additions as underlined text. The data types are the C++ data types where all types that are not a bool is a numeric value/index.

AIAirport:
  • bool AIAirport::IsHangarTile(TileIndex tile)
    bool AIAirport::IsAirportTile(TileIndex tile)
    bool AIAirport::BuildAirport(TileIndex tile, AirportType type, AirportView view, StationID station_id) // takes new argument
    bool AIAirport::RemoveAirport(TileIndex tile)
    int32 AIAirport::GetNumHangars(TileIndex tile)
    TileIndex AIAirport::GetHangarOfAirport(TileIndex tile)
    AirportType AIAirport::GetAirportType(TileIndex tile)
    AirportView AIAirport::GetAirportView(TileIndex tile) // new
AIAirportType:
  • bool AIAirportType::IsBuildableAirportType(AirportType type) // replaces AIAirport::IsValidAirportType
    bool AIAirportType::IsAirportTypeInformationAvailable(AirportType type)
    bool AIAirportType::IsValidAirportView(AirportView airport_view, AirportType airport_type)
    bool AIAirportType::IsValidPlaneType(PlaneType plane_type)
    Money AIAirportType::GetPrice(AirportType type)
    int32 AIAirportType::GetAirportWidth(AirportType type, AirportView view) // takes new argument
    int32 AIAirportType::GetAirportHeight(AirportType type, AirportView view) // takes new argument
    int32 AIAirportType::GetNumHangars(AirportType type) // new function
    int32 AIAirportType::GetNumHelipads(AirportType type) // new function
    int32 AIAirportType::GetNumTerminals(AirportType type) // new function
    bool AIAirportType::CanPlaneTypeLand(AirportType type, AIAirportType::PlaneType plane_type) // new function
    bool AIAirportType::IsLandingExtraDangerous(AirportType type, AIAirportType::PlaneType plane_type) // new function
    int32 AIAirportType::GetAirportCoverageRadius(AirportType type)
    int AIAirportType::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
    TownID AIAirportType::GetNearestTown(TileIndex tile, AirportType type)
AIAirportTypeList:
  • AIAirportTypeList::AIAirportTypeList() // new - lists all airport types
AIAirportViewList:
  • AIAirportViewList::AIAirportViewList(AIAirportType type) // new - lists all views of given airport type

For reference: Trunk AIAirport


Comment
I realize that the new AIAirport::BuildAirport function possible should be renamed as AIAirports::BuildAirportView while the old one is kept as it will still work, just that it will always build the view with index 0. One could argue if the old function should be deprecated and moved to the compatibility layers or kept as an ordinary API function.


Win32 binary
noai_airport_2011-09-07.zip
Win32 release binary. Includes the test AI in the ai directory.
(4.17 MiB) Downloaded 188 times
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: AIAirport API improvements - towards better NewGRF suppo

Post by krinn »

Code: Select all

bool AIAirport::BuildAirport(TileIndex tile, AirportType type, AirportView view, StationID station_id) // takes new argument
Would have been better with moving AirportView as 3rd arguement and default it if none pass to 0
Compatibility kept
And i would drop BuildAirportView as it mean nothing as building an airport would imply building it with a view now (even if the airport have only 1 view usable)
Note that just default AirportView everywhere in the new functions to 0 will kept old AI compatibility without need of a layer addition as long as the argument is the last one.

Nice work Zuu, will take a look at patch and if i have time build openttd with it to test
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: AIAirport API improvements - towards better NewGRF suppo

Post by Yexo »

krinn wrote:

Code: Select all

bool AIAirport::BuildAirport(TileIndex tile, AirportType type, AirportView view, StationID station_id) // takes new argument
Would have been better with moving AirportView as 3rd arguement and default it if none pass to 0
Compatibility kept
And i would drop BuildAirportView as it mean nothing as building an airport would imply building it with a view now (even if the airport have only 1 view usable)
Note that just default AirportView everywhere in the new functions to 0 will kept old AI compatibility without need of a layer addition as long as the argument is the last one.
Only squirrel doesn't support default argument values (or at least or C++/squirrel layer doesn't). Therefor we need to change the name of the function when adding/removing a parameter.
User avatar
1009
Engineer
Engineer
Posts: 81
Joined: 22 Mar 2009 07:24
Location: Haarlem, The Netherlands
Contact:

Re: AIAirport API improvements - towards better NewGRF suppo

Post by 1009 »

Is there still work to be done on this branch? Since I've finished the Stop AI Cheat, I'm looking for something else to help with.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: AIAirport API improvements - towards better NewGRF suppo

Post by Zuu »

The work is currently a bit on hold since the project is waiting for some decisions to be made by the development team. Thus, the last 4-6 days I've focused on other projects (mostly a non-OpenTTD program called Junctioneer)
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: CTarana45 and 23 guests