# ai_engine.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_engine.cpp 15308 2009-02-01 17:14:39Z 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   switch (::GetEngine(engine_id)->type) {
    00037     case VEH_ROAD: {
    00038       const RoadVehicleInfo *vi = ::RoadVehInfo(engine_id);
    00039       return vi->cargo_type;
    00040     } break;
    00041
    00042     case VEH_TRAIN: {
    00043       const RailVehicleInfo *vi = ::RailVehInfo(engine_id);
    00044       return vi->cargo_type;
    00045     } break;
    00046
    00047     case VEH_SHIP: {
    00048       const ShipVehicleInfo *vi = ::ShipVehInfo(engine_id);
    00049       return vi->cargo_type;
    00050     } break;
    00051
    00052     case VEH_AIRCRAFT: {
    00053       return CT_PASSENGERS;
    00054     } break;
    00055
    00056     default: NOT_REACHED();
    00057   }
    00058 }
    00059
    00060 /* static */ bool AIEngine::CanRefitCargo(EngineID engine_id, CargoID cargo_id)
    00061 {
    00062   if (!IsValidEngine(engine_id)) return false;
    00063   if (!AICargo::IsValidCargo(cargo_id)) return false;
    00064
    00065   if (GetCargoType(engine_id) == cargo_id) return true;
    00066   if (cargo_id == CT_MAIL && ::GetEngine(engine_id)->type == VEH_AIRCRAFT) return true;
    00067   if (::GetEngine(engine_id)->type == VEH_SHIP && !ShipVehInfo(engine_id)->refittable) return false;
    00068   return ::CanRefitTo(engine_id, cargo_id);
    00069 }
    00070
    00071 /* static */ bool AIEngine::CanPullCargo(EngineID engine_id, CargoID cargo_id)
    00072 {
    00073   if (!IsValidEngine(engine_id)) return false;
    00074   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00075   if (!AICargo::IsValidCargo(cargo_id)) return false;
    00076
    00077   return (::RailVehInfo(engine_id)->ai_passenger_only != 1) || AICargo::HasCargoClass(cargo_id, AICargo::CC_PASSENGERS);
    00078 }
    00079
    00080
    00081 /* static */ int32 AIEngine::GetCapacity(EngineID engine_id)
    00082 {
    00083   if (!IsValidEngine(engine_id)) return -1;
    00084
    00085   switch (::GetEngine(engine_id)->type) {
    00086     case VEH_ROAD:
    00087     case VEH_TRAIN: {
    00088       uint16 *capacities = GetCapacityOfArticulatedParts(engine_id, ::GetEngine(engine_id)->type);
    00089       for (CargoID c = 0; c < NUM_CARGO; c++) {
    00090         if (capacities[c] == 0) continue;
    00091         return capacities[c];
    00092       }
    00093       return -1;
    00094     } break;
    00095
    00096     case VEH_SHIP: {
    00097       const ShipVehicleInfo *vi = ::ShipVehInfo(engine_id);
    00098       return vi->capacity;
    00099     } break;
    00100
    00101     case VEH_AIRCRAFT: {
    00102       const AircraftVehicleInfo *vi = ::AircraftVehInfo(engine_id);
    00103       return vi->passenger_capacity;
    00104     } break;
    00105
    00106     default: NOT_REACHED();
    00107   }
    00108 }
    00109
    00110 /* static */ int32 AIEngine::GetReliability(EngineID engine_id)
    00111 {
    00112   if (!IsValidEngine(engine_id)) return -1;
    00113
    00114   return (::GetEngine(engine_id)->reliability * 100 >> 16);
    00115 }
    00116
    00117 /* static */ int32 AIEngine::GetMaxSpeed(EngineID engine_id)
    00118 {
    00119   if (!IsValidEngine(engine_id)) return -1;
    00120
    00121   const Engine *e = ::GetEngine(engine_id);
    00122   int32 max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
    00123   if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
    00124   return max_speed;
    00125 }
    00126
    00127 /* static */ Money AIEngine::GetPrice(EngineID engine_id)
    00128 {
    00129   if (!IsValidEngine(engine_id)) return -1;
    00130
    00131   return ::GetEngine(engine_id)->GetCost();
    00132 }
    00133
    00134 /* static */ int32 AIEngine::GetMaxAge(EngineID engine_id)
    00135 {
    00136   if (!IsValidEngine(engine_id)) return -1;
    00137
    00138   return ::GetEngine(engine_id)->lifelength * DAYS_IN_LEAP_YEAR;
    00139 }
    00140
    00141 /* static */ Money AIEngine::GetRunningCost(EngineID engine_id)
    00142 {
    00143   if (!IsValidEngine(engine_id)) return -1;
    00144
    00145   return ::GetEngine(engine_id)->GetRunningCost();
    00146 }
    00147
    00148 /* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
    00149 {
    00150   if (!IsValidEngine(engine_id)) return AIVehicle::VT_INVALID;
    00151
    00152   switch (::GetEngine(engine_id)->type) {
    00153     case VEH_ROAD:     return AIVehicle::VT_ROAD;
    00154     case VEH_TRAIN:    return AIVehicle::VT_RAIL;
    00155     case VEH_SHIP:     return AIVehicle::VT_WATER;
    00156     case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
    00157     default: NOT_REACHED();
    00158   }
    00159 }
    00160
    00161 /* static */ bool AIEngine::IsWagon(EngineID engine_id)
    00162 {
    00163   if (!IsValidEngine(engine_id)) return false;
    00164   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00165
    00166   return ::RailVehInfo(engine_id)->power == 0;
    00167 }
    00168
    00169 /* static */ bool AIEngine::CanRunOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00170 {
    00171   if (!IsValidEngine(engine_id)) return false;
    00172   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00173   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00174
    00175   return ::IsCompatibleRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00176 }
    00177
    00178 /* static */ bool AIEngine::HasPowerOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00179 {
    00180   if (!IsValidEngine(engine_id)) return false;
    00181   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00182   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00183
    00184   return ::HasPowerOnRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00185 }
    00186
    00187 /* static */ AIRoad::RoadType AIEngine::GetRoadType(EngineID engine_id)
    00188 {
    00189   if (!IsValidEngine(engine_id)) return AIRoad::ROADTYPE_INVALID;
    00190   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD) return AIRoad::ROADTYPE_INVALID;
    00191
    00192   return HasBit(::EngInfo(engine_id)->misc_flags, EF_ROAD_TRAM) ? AIRoad::ROADTYPE_TRAM : AIRoad::ROADTYPE_ROAD;
    00193 }
    00194
    00195 /* static */ AIRail::RailType AIEngine::GetRailType(EngineID engine_id)
    00196 {
    00197   if (!IsValidEngine(engine_id)) return AIRail::RAILTYPE_INVALID;
    00198   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return AIRail::RAILTYPE_INVALID;
    00199
    00200   return (AIRail::RailType)(uint)::RailVehInfo(engine_id)->railtype;
    00201 }
    00202
    00203 /* static */ bool AIEngine::IsArticulated(EngineID engine_id)
    00204 {
    00205   if (!IsValidEngine(engine_id)) return false;
    00206   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD && GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00207
    00208   return CountArticulatedParts(engine_id, true) != 0;
    00209 }
    00210
    00211 /* static */ AIAirport::PlaneType AIEngine::GetPlaneType(EngineID engine_id)
    00212 {
    00213   if (!IsValidEngine(engine_id)) return AIAirport::PT_INVALID;
    00214   if (GetVehicleType(engine_id) != AIVehicle::VT_AIR) return AIAirport::PT_INVALID;
    00215
    00216   return (AIAirport::PlaneType)::AircraftVehInfo(engine_id)->subtype;
    00217 }