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

cargo_ID for mail

Post by superpumpie » 03 Oct 2019 03:57

Hi,

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? ?( ?(

Thanks!
-sp

xarick
Transport Coordinator
Transport Coordinator
Posts: 269
Joined: 26 Feb 2015 00:52

Re: cargo_ID for mail

Post by xarick » 03 Oct 2019 09:48

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();
	cargoList.Sort(AIList.SORT_BY_ITEM, AIList.SORT_ASCENDING);

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

	/* 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
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5390
Joined: 13 Sep 2004 13:21
Location: The Moon
Contact:

Re: cargo_ID for mail

Post by PikkaBird » 04 Oct 2019 07:58

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