# ai_engine.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_engine.cpp 15765 2009-03-18 19:50:34Z 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 */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
    00156 {
    00157   if (!IsValidEngine(engine_id)) return AIVehicle::VT_INVALID;
    00158
    00159   switch (::GetEngine(engine_id)->type) {
    00160     case VEH_ROAD:     return AIVehicle::VT_ROAD;
    00161     case VEH_TRAIN:    return AIVehicle::VT_RAIL;
    00162     case VEH_SHIP:     return AIVehicle::VT_WATER;
    00163     case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
    00164     default: NOT_REACHED();
    00165   }
    00166 }
    00167
    00168 /* static */ bool AIEngine::IsWagon(EngineID engine_id)
    00169 {
    00170   if (!IsValidEngine(engine_id)) return false;
    00171   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00172
    00173   return ::RailVehInfo(engine_id)->power == 0;
    00174 }
    00175
    00176 /* static */ bool AIEngine::CanRunOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00177 {
    00178   if (!IsValidEngine(engine_id)) return false;
    00179   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00180   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00181
    00182   return ::IsCompatibleRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00183 }
    00184
    00185 /* static */ bool AIEngine::HasPowerOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00186 {
    00187   if (!IsValidEngine(engine_id)) return false;
    00188   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00189   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00190
    00191   return ::HasPowerOnRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00192 }
    00193
    00194 /* static */ AIRoad::RoadType AIEngine::GetRoadType(EngineID engine_id)
    00195 {
    00196   if (!IsValidEngine(engine_id)) return AIRoad::ROADTYPE_INVALID;
    00197   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD) return AIRoad::ROADTYPE_INVALID;
    00198
    00199   return HasBit(::EngInfo(engine_id)->misc_flags, EF_ROAD_TRAM) ? AIRoad::ROADTYPE_TRAM : AIRoad::ROADTYPE_ROAD;
    00200 }
    00201
    00202 /* static */ AIRail::RailType AIEngine::GetRailType(EngineID engine_id)
    00203 {
    00204   if (!IsValidEngine(engine_id)) return AIRail::RAILTYPE_INVALID;
    00205   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return AIRail::RAILTYPE_INVALID;
    00206
    00207   return (AIRail::RailType)(uint)::RailVehInfo(engine_id)->railtype;
    00208 }
    00209
    00210 /* static */ bool AIEngine::IsArticulated(EngineID engine_id)
    00211 {
    00212   if (!IsValidEngine(engine_id)) return false;
    00213   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD && GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00214
    00215   return CountArticulatedParts(engine_id, true) != 0;
    00216 }
    00217
    00218 /* static */ AIAirport::PlaneType AIEngine::GetPlaneType(EngineID engine_id)
    00219 {
    00220   if (!IsValidEngine(engine_id)) return AIAirport::PT_INVALID;
    00221   if (GetVehicleType(engine_id) != AIVehicle::VT_AIR) return AIAirport::PT_INVALID;
    00222
    00223   return (AIAirport::PlaneType)::AircraftVehInfo(engine_id)->subtype;
    00224 }