cargo_ID for mail

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
Posts: 1
Joined: 03 Oct 2019 03:54

cargo_ID for mail

Post by superpumpie »


I am patching together my first AI, focusing on passengers and mail.

I saw that "num_passengers = AIStation.GetCargoWaiting(station, 0);" grabs the number of passengers of a station. But I cannot find the respective ID for mail. What am I missing here? ?( ?(


Transport Coordinator
Transport Coordinator
Posts: 291
Joined: 26 Feb 2015 00:52

Re: cargo_ID for mail

Post by xarick »

I do like this for my AI:

Code: Select all

/// getCargoId - Returns either mail cargo id, or passenger cargo id.
/// @param cargoClass - either AICargo.CC_MAIL, or AICargo.CC_PASSENGERS
/// @return - Cargo list.
function Utils::getCargoId(cargoClass) {
	local cargoList = AICargoList();

	local cargoId = null;
	for (cargoId = cargoList.Begin(); !cargoList.IsEnd(); cargoId = cargoList.Next()) {
		if (AICargo.HasCargoClass(cargoId, cargoClass)) {

	/* both AICargo.CC_MAIL and AICargo.CC_PASSENGERS should return the first available cargo */
	return cargoId;
Cargo_ID being 0 usually happens to be passengers by default, but it may not always be the case with some NewGRFs.

Code: Select all

num_passengers = AIStation.GetCargoWaiting(station, Utils.getCargoId(AICargo.CC_PASSENGERS));
num_mail = AIStation.GetCargoWaiting(station, Utils.getCargoId(AICargo.CC_MAIL));
Formerly known as Samu

User avatar
Graphics Moderator
Graphics Moderator
Posts: 5472
Joined: 13 Sep 2004 13:21
Location: The Moon

Re: cargo_ID for mail

Post by PikkaBird »

I have a similar approach to xarick, although my function searches by cargo label so I can find goods, food, or whatever (and I don't assume that MAIL is the only cargo in the mail class. ;)).

Code: Select all

function CivilAI::FindCargo(label) {

local clist = AICargoList();

 foreach (cargo, z in clist) {
 if (AICargo.GetCargoLabel(cargo) == label) {
 return cargo;
return null;
Then I can just FindCargo("MAIL"), or whichever cargo I'm looking for.

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests