# ai_engine.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_engine.cpp 15705 2009-03-13 23:49:12Z 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   const Engine *e = ::GetEngine(engine_id);
    00068   switch (e->type) {
    00069     case VEH_ROAD:
    00070     case VEH_TRAIN: {
    00071       uint16 *capacities = GetCapacityOfArticulatedParts(engine_id, e->type);
    00072       for (CargoID c = 0; c < NUM_CARGO; c++) {
    00073         if (capacities[c] == 0) continue;
    00074         return capacities[c];
    00075       }
    00076       return -1;
    00077     } break;
    00078
    00079     case VEH_SHIP:
    00080       return e->u.ship.capacity;
    00081       break;
    00082
    00083     case VEH_AIRCRAFT:
    00084       return AircraftDefaultCargoCapacity(e->GetDefaultCargoType(), &e->u.air);
    00085       break;
    00086
    00087     default: NOT_REACHED();
    00088   }
    00089 }
    00090
    00091 /* static */ int32 AIEngine::GetReliability(EngineID engine_id)
    00092 {
    00093   if (!IsValidEngine(engine_id)) return -1;
    00094   if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
    00095
    00096   return (::GetEngine(engine_id)->reliability * 100 >> 16);
    00097 }
    00098
    00099 /* static */ int32 AIEngine::GetMaxSpeed(EngineID engine_id)
    00100 {
    00101   if (!IsValidEngine(engine_id)) return -1;
    00102
    00103   const Engine *e = ::GetEngine(engine_id);
    00104   int32 max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
    00105   if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
    00106   return max_speed;
    00107 }
    00108
    00109 /* static */ Money AIEngine::GetPrice(EngineID engine_id)
    00110 {
    00111   if (!IsValidEngine(engine_id)) return -1;
    00112
    00113   return ::GetEngine(engine_id)->GetCost();
    00114 }
    00115
    00116 /* static */ int32 AIEngine::GetMaxAge(EngineID engine_id)
    00117 {
    00118   if (!IsValidEngine(engine_id)) return -1;
    00119   if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
    00120
    00121   return ::GetEngine(engine_id)->lifelength * DAYS_IN_LEAP_YEAR;
    00122 }
    00123
    00124 /* static */ Money AIEngine::GetRunningCost(EngineID engine_id)
    00125 {
    00126   if (!IsValidEngine(engine_id)) return -1;
    00127
    00128   return ::GetEngine(engine_id)->GetRunningCost();
    00129 }
    00130
    00131 /* static */ int32 AIEngine::GetPower(EngineID engine_id)
    00132 {
    00133   if (!IsValidEngine(engine_id)) return -1;
    00134   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00135   if (IsWagon(engine_id)) return -1;
    00136
    00137   return ::GetEngine(engine_id)->GetPower();
    00138 }
    00139
    00140 /* static */ int32 AIEngine::GetWeight(EngineID engine_id)
    00141 {
    00142   if (!IsValidEngine(engine_id)) return -1;
    00143   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00144
    00145   return ::GetEngine(engine_id)->GetDisplayWeight();
    00146 }
    00147
    00148 /* static */ int32 AIEngine::GetMaxTractiveEffort(EngineID engine_id)
    00149 {
    00150   if (!IsValidEngine(engine_id)) return -1;
    00151   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00152   if (IsWagon(engine_id)) return -1;
    00153
    00154   return ::GetEngine(engine_id)->GetDisplayMaxTractiveEffort();
    00155 }
    00156
    00157 /* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
    00158 {
    00159   if (!IsValidEngine(engine_id)) return AIVehicle::VT_INVALID;
    00160
    00161   switch (::GetEngine(engine_id)->type) {
    00162     case VEH_ROAD:     return AIVehicle::VT_ROAD;
    00163     case VEH_TRAIN:    return AIVehicle::VT_RAIL;
    00164     case VEH_SHIP:     return AIVehicle::VT_WATER;
    00165     case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
    00166     default: NOT_REACHED();
    00167   }
    00168 }
    00169
    00170 /* static */ bool AIEngine::IsWagon(EngineID engine_id)
    00171 {
    00172   if (!IsValidEngine(engine_id)) return false;
    00173   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00174
    00175   return ::RailVehInfo(engine_id)->power == 0;
    00176 }
    00177
    00178 /* static */ bool AIEngine::CanRunOnRail(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 ::IsCompatibleRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00185 }
    00186
    00187 /* static */ bool AIEngine::HasPowerOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00188 {
    00189   if (!IsValidEngine(engine_id)) return false;
    00190   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00191   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00192
    00193   return ::HasPowerOnRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00194 }
    00195
    00196 /* static */ AIRoad::RoadType AIEngine::GetRoadType(EngineID engine_id)
    00197 {
    00198   if (!IsValidEngine(engine_id)) return AIRoad::ROADTYPE_INVALID;
    00199   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD) return AIRoad::ROADTYPE_INVALID;
    00200
    00201   return HasBit(::EngInfo(engine_id)->misc_flags, EF_ROAD_TRAM) ? AIRoad::ROADTYPE_TRAM : AIRoad::ROADTYPE_ROAD;
    00202 }
    00203
    00204 /* static */ AIRail::RailType AIEngine::GetRailType(EngineID engine_id)
    00205 {
    00206   if (!IsValidEngine(engine_id)) return AIRail::RAILTYPE_INVALID;
    00207   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return AIRail::RAILTYPE_INVALID;
    00208
    00209   return (AIRail::RailType)(uint)::RailVehInfo(engine_id)->railtype;
    00210 }
    00211
    00212 /* static */ bool AIEngine::IsArticulated(EngineID engine_id)
    00213 {
    00214   if (!IsValidEngine(engine_id)) return false;
    00215   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD && GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00216
    00217   return CountArticulatedParts(engine_id, true) != 0;
    00218 }
    00219
    00220 /* static */ AIAirport::PlaneType AIEngine::GetPlaneType(EngineID engine_id)
    00221 {
    00222   if (!IsValidEngine(engine_id)) return AIAirport::PT_INVALID;
    00223   if (GetVehicleType(engine_id) != AIVehicle::VT_AIR) return AIAirport::PT_INVALID;
    00224
    00225   return (AIAirport::PlaneType)::AircraftVehInfo(engine_id)->subtype;
    00226 }