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
cargo_ID for mail
Moderator: OpenTTD Developers
Re: cargo_ID for mail
I do like this for my AI:
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
/// 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;
}
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
Re: cargo_ID for mail
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. ).
Then I can just FindCargo("MAIL"), or whichever cargo I'm looking for.
Code: Select all
function CivilAI::FindCargo(label) {
local clist = AICargoList();
foreach (cargo, z in clist) {
if (AICargo.GetCargoLabel(cargo) == label) {
return cargo;
}
}
return null;
}
Who is online
Users browsing this forum: Baidu [Spider] and 33 guests