# ai_engine.cpp

Go to the documentation of this file.

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