AIs and Cargodist

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
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

AIs and Cargodist

Post by Zuu »

Starting from tonight's nightly there will now be three new API methods that should help AIs to better work with Cargodist.

First, cargo distribution is enabled per set of cargo types, globally for all companies. Eg. it is in the control of the human person playing with your AI. There is a new method AICargo.GetDistributionType(cargo_type) which you can use to check if a given cargo type uses non-manual distribution. This is probably easier to use than reading the settings by hand in most cases.

When cargo distribution is non-manual (asymmetric or symmetric), and you give cargo the option to make choices in where to be transported, you get the problem that you will want to analyze where cargo goes. This is where the new APIs come in. (It is also possible to stick your head into the sand and always force cargo to only have one choice (by use of order modifiers) and ignore cargo distribution even if it has been enabled)

New APIs
  • AICargo.GetDistributionType(cargo_type)
  • AIStation.GetCargoWaitingFrom(station_id, from_station_id, cargo_id)
  • AIStation.GetCargoWaitingVia(station_id, via_station_id, cargo_id)
In the API there already exist GetCargoWaiting(station_id, cargo_id). The two new AIStation APIs will allow you to get the portion of waiting cargo that originate from some station id (GetCargoWaitingFrom), or that have some station as next hop (GetCargoWaitingVia).

Both also accept STATION_INVALID as from/via station id. This give you the red "any station" numbers displayed in GUI.

The reason why there is no GetCargoWaitingTo is that a such value would only be an estimate based on planned values from CargoDist. You can find further API design discussion on the IRC log of #openttd.dev: 9 March and 10 March. (note that my link to the work-in-progress patch now shows the final version of the patch)

Use case - PAXLink
I have updated my AI PAXLink (not yet released), so that when cargodist is enabled, it omit the order modifers (transfer, leave empty etc.) and instead let cargo find their way. Using GetCargoWaitingVia, I can filter the passengers waiting at my airports for passengers heading to the other town which is what I want to use as input to decide if I need more/fewer aircraft. Passengers waiting to travel locally is used as input to the decision making if more/fewer local buses are wanted.

Game scripts
All the three new API methods are also available to Game Scripts if anyone find use for them in that context.
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: No registered users and 15 guests