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

Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo »

Kogut wrote:Another idea:
there is BuildNewGRFRailStation
is it possible to have BuildNewGRFTileWithWaitingCargo
or even BuildNewGRFTileWithBuilding?

use: extending station coverage using rail station in looking-like-no-cheat-and-stupid-exploit way.
Not possible with the current newgrfs specs.
Kogut wrote:Something more important:
There is already AIIndustry.GetStockpiledCargo() but for ECS it is useless without AIIndustry.GetMaxStockpileSize().
There is no such thing in the OpenTTD code as a "stockpile limit", it's completely coded in the newgrf logic and thus can't be exposed to AIs.
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 »

Yexo wrote:
Kogut wrote:Something more important:
There is already AIIndustry.GetStockpiledCargo() but for ECS it is useless without AIIndustry.GetMaxStockpileSize().
There is no such thing in the OpenTTD code as a "stockpile limit", it's completely coded in the newgrf logic and thus can't be exposed to AIs.
Well... I think one could possibly check for the presence of callback flags action0, prop 22, bit4 (CB for opting out of accepting cargo) and action0, prop 21, bit 2 (incoming cargo callback), i.e. whether those callbacks are used. That might at least give a hint to the AI to be aware.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

Yexo wrote:
Kogut wrote:Something more important:
There is already AIIndustry.GetStockpiledCargo() but for ECS it is useless without AIIndustry.GetMaxStockpileSize().
There is no such thing in the OpenTTD code as a "stockpile limit", it's completely coded in the newgrf logic and thus can't be exposed to AIs.
Why GetStockpiledCargo is possible and GetMaxStockpileSize is impossible?
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo »

Kogut wrote:Why GetStockpiledCargo is possible and GetMaxStockpileSize is impossible?
Because "stockpiles" are a concept that is coded in OpenTTD. There is no such concept as a "maximum stockpile size" in OpenTTD code (apart from 0xFFFF as hard limit, but it's hardly useful to return that).
Blustuff
Engineer
Engineer
Posts: 112
Joined: 21 Aug 2008 09:37
Location: France

Re: Some API extension ideas

Post by Blustuff »

Set operations on AIList
  • Intersection : KeepList already exists
  • Union : Add to the list items of the second list whenever their index don't exist in the first one
  • Difference : Remove from the first lists index of the second one
I'm currently using a workaround for industry lists.
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 »

For union, you can use AIAbstractList.AddList:

Code: Select all

void 	AddList  (AIAbstractList  *list)
 	Add one list to another one. 
There is also RemoveList and KeepList.

So I believe you can already do all three operations with AI*List.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Blustuff
Engineer
Engineer
Posts: 112
Joined: 21 Aug 2008 09:37
Location: France

Re: Some API extension ideas

Post by Blustuff »

You're absolutely right and I don't understand how I missed these functions...
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

AIStation.GetAgeOfStation(station)
AICompany.ChangeCompanyColourTo(colour)
AICompany.GetCompanyColour()
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 »

Suggestion has been withdrawn since you can use AITileList__StationType instead

A suggestion:

AIRoad.GetRoadVehType(station_tile) - so that you can check if an existing road stop is a bus stop or truck stop. I'm amazed this is not yet supported.

The only way I can see to detect the type of a road stop is to have enough money that you can afford to try to remove a bus stop and see if it fails because of wrong stop type. Hopefully that fails before vehicle in the way otherwise it could also take quite long time to determine the type. An other way would be to check the type of vehicles that are scheduled for the road stop station and see what type they have.

Edit: There is only a AIRoad.RemoveRoadStation that can remove both bus stops and truck stops, so you can't do the remove stop workaround. This leaves you with the only possibility to check which vehicles that are scheduled for the station. If there are vehicles of say type "bus", you'll be sure that there is at least one bus stop, but you can never see if there are no bus/truck stops for sure and it will be very hard to track the usage ratio of a station consisting of both types.

I guess for now the best solution is to use drive-through for trucks and terminating stops for buses or the other way around if you want to be able to determine the type. This of course will not work if you get to take over after another AI.


Edit2: I'm withdrawing this suggestion since there is a AITileList__StationType and station types has the distinction between bus and truck stops, so you can write an utility function to do what I wanted to do.
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: Some API extension ideas

Post by Zuu »

For industries:
  • AITile.IsIndustry(tile)
  • AITileList_Industry(industry_id) and/or AIIndustry.GetIndustryId(tile)
Motivation: You sometimes want to know the id of the industries that your possible future station will cover. Or if you look in towns for cargo, you want to make sure that if you find out that a cargo is accepted/produced by a town, really is accepted/produced by the town and not by an industry.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Some API extension ideas

Post by fanioz »

Zuu wrote:Suggestion has been withdrawn since you can use AITileList__StationType instead
or using
AIStation::HasStationType ( StationID station_id, StationType station_type) :D
Zuu wrote:AIIndustry.GetIndustryId(tile)
Agree, right now I have to write this with an ugly code :D .
(look at the several first line code on /Trans/ext/xindustry.nut or /Trans/ext/xtown.nut how I could get ID from a tile for both town and industry)

Another request is :
AIBaseStation::GetConstructionDate (tile_ID) instead of Station_ID. Or I have to loop over TileList to get the correct station. (imagine that the station was adjacent with another station type)
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
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 »

  • AIVehicle.GetDirection(vehicle_id)
  • Would be nice if AIVehicle.GetCurrentSpeed() reported zero when vehicles are stuck in queues.
  • AIStation.IsCargoAccepted
An AI could get the direction of vehicles by storing their current location and then sleep for a few days and check again. However that is a quite expansive operation even if you store the position for all vehicles, sleep and then compare. It will still only work for vehicles that are not stuck in a queue etc.

My guess is that it is non-trival to make AIVehicle.GetCurrentSpeed() to return zero when vehicles don't move. Is there a constant for the maximum speed a vehicle that stands still can have? I've for now used 20% of the max speed as threshold for when vehicles stand still.

I've also found the need for AIStation.IsCargoAccepted but if FS#3799 gets implemented, then AIStation.IsCargoAccepted is not needed. The pro of having this in the API is that if the acceptance pattern changes, then the AIs do not need to re-implement this function. On the other hand, as I wrote in a comment to that FS task, both my main AIs will probably break anyways if acceptance pattern changes as I'm sure I've hardcoded the 3,4,5 acceptance radius of stations at other places.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Some API extension ideas

Post by frosch »

Zuu wrote:# Would be nice if AIVehicle.GetCurrentSpeed() reported zero when vehicles are stuck in queues.
Done in r19755 :)
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
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 »

Thanks a lot. That should make it much easier to detect stuck vehicles. (even if there is room for more improvements in that area)
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Some API extension ideas

Post by Lord Aro »

i notice that AIAbstractList.HasNext() is missing from the trunk version
is this meant to be like this? if so what do you use instead?
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
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo »

Lord Aro wrote:i notice that AIAbstractList.HasNext() is missing from the trunk version
is this meant to be like this?
Yes
if so what do you use instead?
IsEnd()
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 »

Today Yexo did:
  • -Add: [NoAI] AIIndustry::GetIndustryID(TileIndex)
  • -Fix [FS#3818]: the break-on-log-message feature could trigger an assert (Zuu)
It is now possible to do AIIndustry.IsValidIndustry(AIIndustry::GetIndustryID(tile)) to check if a tile is industry or not.


A new issue: (edit: see my edit below, this is not a problem)
Unless I've missed something, AI's can't upgrade town bridges. When I try to upgrade a bridge using AIBridge::BuildBridge it tells that the bridge is owned by another company even if it is owned by the town (Owner: N/A in land info dialog). Perhaps either introduce AIBridge::UpgradeBridge or change BuildBridge so it can upgrade town bridges just as players can do.

Edit: Sorry, in my test scenario I created the bridge in the scenario editor and assumed it would become town owned, but apparently you need to use the expand town feature to build bridges that actually become owned by the town. Doing the scenario right, it works and the AI upgrades the bridge.
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 »

- AIVehicle.GetWeight(vehicle_id)
- IsItPossibleToBuildNewGrfBuffer()
- BuildNewGrfBuffer(tile, direction, acces_track_tile)
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo »

Kogut wrote:- AIVehicle.GetWeight(vehicle_id)
Seems logical to include this, I'll have another look at it when I find the time.
- IsItPossibleToBuildNewGrfBuffer()
- BuildNewGrfBuffer(tile, direction, acces_track_tile)
Not possible, OpenTTD doesn't know anything about "buffer" tiles. There are only 2 types of station tiles: station tiles with rails and station tiles without rails. Of course you're free to open up a suggestion in the graphics development forum for an extension of the grf spec for this, but I don't have much hope it'd be included anytime soon.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Some API extension ideas

Post by Kogut »

Yexo wrote:
Lord Aro wrote:i notice that AIAbstractList.HasNext() is missing from the trunk version
is this meant to be like this?
Yes
if so what do you use instead?
IsEnd()
Warning: In recent trunk (20027) HasNext is unsupported and following AIs failed immediately after starting new game: AIAI (fixed in dev version), ChooChoo, Chopper, Convoy, Otviai, Trans, PAXlink, RoadRunner, Rondje, NoCab and Cluelessplus.
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests