Loading

Paste #pnrowcvo8

  1. /* static */ VehicleID ScriptVehicle::GetVehicleID(UnitID unit_number, ScriptVehicle::VehicleType vehicle_type)
  2. {
  3.     EnforcePrecondition(INVALID_VEHICLE, vehicle_type == ScriptVehicle::VT_AIR || vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER);
  4.     EnforcePrecondition(INVALID_VEHICLE, unit_number >= 0 && unit_number <= UINT16_MAX);
  5.  
  6.     switch (vehicle_type) {
  7.         case ScriptVehicle::VT_AIR: {
  8.             Aircraft *v;
  9.             FOR_ALL_AIRCRAFT(v) {
  10.                 if (!v->IsPrimaryVehicle()) continue;
  11.  
  12.                 if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
  13.                     return v->index;
  14.                 }
  15.             }
  16.             return INVALID_VEHICLE;
  17.         }
  18.  
  19.         case ScriptVehicle::VT_ROAD: {
  20.             RoadVehicle *v;
  21.             FOR_ALL_ROADVEHICLES(v) {
  22.                 if (!v->IsPrimaryVehicle()) continue;
  23.  
  24.                 if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
  25.                     return v->index;
  26.                 }
  27.             }
  28.             return INVALID_VEHICLE;
  29.         }
  30.  
  31.         case ScriptVehicle::VT_RAIL: {
  32.             Train *v;
  33.             FOR_ALL_TRAINS(v) {
  34.                 if (!v->IsPrimaryVehicle()) continue;
  35.  
  36.                 if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
  37.                     return v->index;
  38.                 }
  39.             }
  40.             return INVALID_VEHICLE;
  41.         }
  42.  
  43.         case ScriptVehicle::VT_WATER: {
  44.             Ship *v;
  45.             FOR_ALL_SHIPS(v) {
  46.                 if (!v->IsPrimaryVehicle()) continue;
  47.  
  48.                 if (v->unitnumber == unit_number && v->owner == ScriptObject::GetCompany()) {
  49.                     return v->index;
  50.                 }
  51.             }
  52.             return INVALID_VEHICLE;
  53.         }
  54.  
  55.         default: return INVALID_VEHICLE;
  56.     }
  57. }

Comments