/* static */ VehicleID ScriptVehicle::GetVehicleID(UnitID unit_number, ScriptVehicle::VehicleType vehicle_type)
{
EnforcePrecondition(INVALID_VEHICLE, vehicle_type == ScriptVehicle::VT_AIR || vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER);
EnforcePrecondition(INVALID_VEHICLE, unit_number >= 0 && unit_number <= UINT16_MAX);
switch (vehicle_type) {
case ScriptVehicle::VT_AIR: {
Aircraft *v;
FOR_ALL_AIRCRAFT(v) {
if (!v->IsPrimaryVehicle()) continue;
if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
return v->index;
}
}
return INVALID_VEHICLE;
}
case ScriptVehicle::VT_ROAD: {
RoadVehicle *v;
FOR_ALL_ROADVEHICLES(v) {
if (!v->IsPrimaryVehicle()) continue;
if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
return v->index;
}
}
return INVALID_VEHICLE;
}
case ScriptVehicle::VT_RAIL: {
Train *v;
FOR_ALL_TRAINS(v) {
if (!v->IsPrimaryVehicle()) continue;
if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
return v->index;
}
}
return INVALID_VEHICLE;
}
case ScriptVehicle::VT_WATER: {
Ship *v;
FOR_ALL_SHIPS(v) {
if (!v->IsPrimaryVehicle()) continue;
if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
return v->index;
}
}
return INVALID_VEHICLE;
}
default: return INVALID_VEHICLE;
}
}