# ai_engine.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_engine.cpp 15541 2009-02-21 12:52:41Z frosch $ */
    00002
    00005 #include "ai_engine.hpp"
    00006 #include "ai_cargo.hpp"
    00007 #include "../../company_func.h"
    00008 #include "../../strings_func.h"
    00009 #include "../../aircraft.h"
    00010 #include "../../vehicle_func.h"
    00011 #include "../../settings_type.h"
    00012 #include "../../articulated_vehicles.h"
    00013 #include "table/strings.h"
    00014
    00015 /* static */ bool AIEngine::IsValidEngine(EngineID engine_id)
    00016 {
    00017   return ::IsEngineIndex(engine_id) && HasBit(::GetEngine(engine_id)->company_avail, _current_company);
    00018 }
    00019
    00020 /* static */ char *AIEngine::GetName(EngineID engine_id)
    00021 {
    00022   if (!IsValidEngine(engine_id)) return NULL;
    00023
    00024   static const int len = 64;
    00025   char *engine_name = MallocT<char>(len);
    00026
    00027   ::SetDParam(0, engine_id);
    00028   ::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
    00029   return engine_name;
    00030 }
    00031
    00032 /* static */ CargoID AIEngine::GetCargoType(EngineID engine_id)
    00033 {
    00034   if (!IsValidEngine(engine_id)) return CT_INVALID;
    00035
    00036   const Engine *e = ::GetEngine(engine_id);
    00037   if (!e->CanCarryCargo()) return CT_INVALID;
    00038
    00039   return e->GetDefaultCargoType();
    00040 }
    00041
    00042 /* static */ bool AIEngine::CanRefitCargo(EngineID engine_id, CargoID cargo_id)
    00043 {
    00044   if (!IsValidEngine(engine_id)) return false;
    00045   if (!AICargo::IsValidCargo(cargo_id)) return false;
    00046
    00047   if (GetCargoType(engine_id) == cargo_id) return true;
    00048   if (cargo_id == CT_MAIL && ::GetEngine(engine_id)->type == VEH_AIRCRAFT) return true;
    00049   if (::GetEngine(engine_id)->type == VEH_SHIP && !ShipVehInfo(engine_id)->refittable) return false;
    00050   return ::CanRefitTo(engine_id, cargo_id);
    00051 }
    00052
    00053 /* static */ bool AIEngine::CanPullCargo(EngineID engine_id, CargoID cargo_id)
    00054 {
    00055   if (!IsValidEngine(engine_id)) return false;
    00056   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00057   if (!AICargo::IsValidCargo(cargo_id)) return false;
    00058
    00059   return (::RailVehInfo(engine_id)->ai_passenger_only != 1) || AICargo::HasCargoClass(cargo_id, AICargo::CC_PASSENGERS);
    00060 }
    00061
    00062
    00063 /* static */ int32 AIEngine::GetCapacity(EngineID engine_id)
    00064 {
    00065   if (!IsValidEngine(engine_id)) return -1;
    00066
    00067   switch (::GetEngine(engine_id)->type) {
    00068     case VEH_ROAD:
    00069     case VEH_TRAIN: {
    00070       uint16 *capacities = GetCapacityOfArticulatedParts(engine_id, ::GetEngine(engine_id)->type);
    00071       for (CargoID c = 0; c < NUM_CARGO; c++) {
    00072         if (capacities[c] == 0) continue;
    00073         return capacities[c];
    00074       }
    00075       return -1;
    00076     } break;
    00077
    00078     case VEH_SHIP: {
    00079       const ShipVehicleInfo *vi = ::ShipVehInfo(engine_id);
    00080       return vi->capacity;
    00081     } break;
    00082
    00083     case VEH_AIRCRAFT: {
    00084       const AircraftVehicleInfo *vi = ::AircraftVehInfo(engine_id);
    00085       return vi->passenger_capacity;
    00086     } break;
    00087
    00088     default: NOT_REACHED();
    00089   }
    00090 }
    00091
    00092 /* static */ int32 AIEngine::GetReliability(EngineID engine_id)
    00093 {
    00094   if (!IsValidEngine(engine_id)) return -1;
    00095   if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
    00096
    00097   return (::GetEngine(engine_id)->reliability * 100 >> 16);
    00098 }
    00099
    00100 /* static */ int32 AIEngine::GetMaxSpeed(EngineID engine_id)
    00101 {
    00102   if (!IsValidEngine(engine_id)) return -1;
    00103
    00104   const Engine *e = ::GetEngine(engine_id);
    00105   int32 max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
    00106   if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
    00107   return max_speed;
    00108 }
    00109
    00110 /* static */ Money AIEngine::GetPrice(EngineID engine_id)
    00111 {
    00112   if (!IsValidEngine(engine_id)) return -1;
    00113
    00114   return ::GetEngine(engine_id)->GetCost();
    00115 }
    00116
    00117 /* static */ int32 AIEngine::GetMaxAge(EngineID engine_id)
    00118 {
    00119   if (!IsValidEngine(engine_id)) return -1;
    00120   if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
    00121
    00122   return ::GetEngine(engine_id)->lifelength * DAYS_IN_LEAP_YEAR;
    00123 }
    00124
    00125 /* static */ Money AIEngine::GetRunningCost(EngineID engine_id)
    00126 {
    00127   if (!IsValidEngine(engine_id)) return -1;
    00128
    00129   return ::GetEngine(engine_id)->GetRunningCost();
    00130 }
    00131
    00132 /* static */ int32 AIEngine::GetPower(EngineID engine_id)
    00133 {
    00134   if (!IsValidEngine(engine_id)) return -1;
    00135   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00136   if (IsWagon(engine_id)) return -1;
    00137
    00138   return ::GetEngine(engine_id)->GetPower();
    00139 }
    00140
    00141 /* static */ int32 AIEngine::GetWeight(EngineID engine_id)
    00142 {
    00143   if (!IsValidEngine(engine_id)) return -1;
    00144   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00145
    00146   return ::GetEngine(engine_id)->GetDisplayWeight();
    00147 }
    00148
    00149 /* static */ int32 AIEngine::GetMaxTractiveEffort(EngineID engine_id)
    00150 {
    00151   if (!IsValidEngine(engine_id)) return -1;
    00152   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00153   if (IsWagon(engine_id)) return -1;
    00154
    00155   return ::GetEngine(engine_id)->GetDisplayMaxTractiveEffort();
    00156 }
    00157
    00158 /* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
    00159 {
    00160   if (!IsValidEngine(engine_id)) return AIVehicle::VT_INVALID;
    00161
    00162   switch (::GetEngine(engine_id)->type) {
    00163     case VEH_ROAD:     return AIVehicle::VT_ROAD;
    00164     case VEH_TRAIN:    return AIVehicle::VT_RAIL;
    00165     case VEH_SHIP:     return AIVehicle::VT_WATER;
    00166     case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
    00167     default: NOT_REACHED();
    00168   }
    00169 }
    00170
    00171 /* static */ bool AIEngine::IsWagon(EngineID engine_id)
    00172 {
    00173   if (!IsValidEngine(engine_id)) return false;
    00174   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00175
    00176   return ::RailVehInfo(engine_id)->power == 0;
    00177 }
    00178
    00179 /* static */ bool AIEngine::CanRunOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00180 {
    00181   if (!IsValidEngine(engine_id)) return false;
    00182   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00183   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00184
    00185   return ::IsCompatibleRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00186 }
    00187
    00188 /* static */ bool AIEngine::HasPowerOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00189 {
    00190   if (!IsValidEngine(engine_id)) return false;
    00191   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00192   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00193
    00194   return ::HasPowerOnRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00195 }
    00196
    00197 /* static */ AIRoad::RoadType AIEngine::GetRoadType(EngineID engine_id)
    00198 {
    00199   if (!IsValidEngine(engine_id)) return AIRoad::ROADTYPE_INVALID;
    00200   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD) return AIRoad::ROADTYPE_INVALID;
    00201
    00202   return HasBit(::EngInfo(engine_id)->misc_flags, EF_ROAD_TRAM) ? AIRoad::ROADTYPE_TRAM : AIRoad::ROADTYPE_ROAD;
    00203 }
    00204
    00205 /* static */ AIRail::RailType AIEngine::GetRailType(EngineID engine_id)
    00206 {
    00207   if (!IsValidEngine(engine_id)) return AIRail::RAILTYPE_INVALID;
    00208   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return AIRail::RAILTYPE_INVALID;
    00209
    00210   return (AIRail::RailType)(uint)::RailVehInfo(engine_id)->railtype;
    00211 }
    00212
    00213 /* static */ bool AIEngine::IsArticulated(EngineID engine_id)
    00214 {
    00215   if (!IsValidEngine(engine_id)) return false;
    00216   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD && GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00217
    00218   return CountArticulatedParts(engine_id, true) != 0;
    00219 }
    00220
    00221 /* static */ AIAirport::PlaneType AIEngine::GetPlaneType(EngineID engine_id)
    00222 {
    00223   if (!IsValidEngine(engine_id)) return AIAirport::PT_INVALID;
    00224   if (GetVehicleType(engine_id) != AIVehicle::VT_AIR) return AIAirport::PT_INVALID;
    00225
    00226   return (AIAirport::PlaneType)::AircraftVehInfo(engine_id)->subtype;
    00227 }