Page 1 of 1

cargo_ID for mail

Posted: 03 Oct 2019 03:57
by superpumpie
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

Re: cargo_ID for mail

Posted: 03 Oct 2019 09:48
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();
	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));

Re: cargo_ID for mail

Posted: 04 Oct 2019 07:58
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.