# ai_engine.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_engine.cpp 19927 2010-06-04 21:10:18Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "ai_engine.hpp"
    00013 #include "ai_cargo.hpp"
    00014 #include "../../company_func.h"
    00015 #include "../../company_base.h"
    00016 #include "../../strings_func.h"
    00017 #include "../../rail.h"
    00018 #include "../../engine_base.h"
    00019 #include "../../engine_func.h"
    00020 #include "../../articulated_vehicles.h"
    00021 #include "table/strings.h"
    00022
    00023 /* static */ bool AIEngine::IsValidEngine(EngineID engine_id)
    00024 {
    00025   const Engine *e = ::Engine::GetIfValid(engine_id);
    00026   return e != NULL && (::IsEngineBuildable(engine_id, e->type, _current_company) || ::Company::Get(_current_company)->num_engines[engine_id] > 0);
    00027
    00028 }
    00029
    00030 /* static */ bool AIEngine::IsBuildable(EngineID engine_id)
    00031 {
    00032   const Engine *e = ::Engine::GetIfValid(engine_id);
    00033   return e != NULL && ::IsEngineBuildable(engine_id, e->type, _current_company);
    00034 }
    00035
    00036 /* static */ char *AIEngine::GetName(EngineID engine_id)
    00037 {
    00038   if (!IsValidEngine(engine_id)) return NULL;
    00039
    00040   static const int len = 64;
    00041   char *engine_name = MallocT<char>(len);
    00042
    00043 	::SetDParam(0, engine_id);
    00044   ::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
    00045   return engine_name;
    00046 }
    00047
    00048 /* static */ CargoID AIEngine::GetCargoType(EngineID engine_id)
    00049 {
    00050   if (!IsValidEngine(engine_id)) return CT_INVALID;
    00051
    00052   CargoArray cap = ::GetCapacityOfArticulatedParts(engine_id);
    00053
    00054   CargoID most_cargo = CT_INVALID;
    00055   uint amount = 0;
    00056   for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
    00057     if (cap[cid] > amount) {
    00058       amount = cap[cid];
    00059       most_cargo = cid;
    00060     }
    00061   }
    00062
    00063   return most_cargo;
    00064 }
    00065
    00066 /* static */ bool AIEngine::CanRefitCargo(EngineID engine_id, CargoID cargo_id)
    00067 {
    00068   if (!IsValidEngine(engine_id)) return false;
    00069   if (!AICargo::IsValidCargo(cargo_id)) return false;
    00070
    00071   return HasBit(::GetUnionOfArticulatedRefitMasks(engine_id, true), cargo_id);
    00072 }
    00073
    00074 /* static */ bool AIEngine::CanPullCargo(EngineID engine_id, CargoID cargo_id)
    00075 {
    00076   if (!IsValidEngine(engine_id)) return false;
    00077   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00078   if (!AICargo::IsValidCargo(cargo_id)) return false;
    00079
    00080   return (::RailVehInfo(engine_id)->ai_passenger_only != 1) || AICargo::HasCargoClass(cargo_id, AICargo::CC_PASSENGERS);
    00081 }
    00082
    00083
    00084 /* static */ int32 AIEngine::GetCapacity(EngineID engine_id)
    00085 {
    00086   if (!IsValidEngine(engine_id)) return -1;
    00087
    00088   const Engine *e = ::Engine::Get(engine_id);
    00089   switch (e->type) {
    00090     case VEH_ROAD:
    00091     case VEH_TRAIN: {
    00092       CargoArray capacities = GetCapacityOfArticulatedParts(engine_id);
    00093       for (CargoID c = 0; c < NUM_CARGO; c++) {
    00094         if (capacities[c] == 0) continue;
    00095         return capacities[c];
    00096       }
    00097       return -1;
    00098     } break;
    00099
    00100     case VEH_SHIP:
    00101     case VEH_AIRCRAFT:
    00102       return e->GetDisplayDefaultCapacity();
    00103       break;
    00104
    00105     default: NOT_REACHED();
    00106   }
    00107 }
    00108
    00109 /* static */ int32 AIEngine::GetReliability(EngineID engine_id)
    00110 {
    00111   if (!IsValidEngine(engine_id)) return -1;
    00112   if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
    00113
    00114   return ::ToPercent16(::Engine::Get(engine_id)->reliability);
    00115 }
    00116
    00117 /* static */ int32 AIEngine::GetMaxSpeed(EngineID engine_id)
    00118 {
    00119   if (!IsValidEngine(engine_id)) return -1;
    00120
    00121   const Engine *e = ::Engine::Get(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 ::Engine::Get(engine_id)->GetCost();
    00132 }
    00133
    00134 /* static */ int32 AIEngine::GetMaxAge(EngineID engine_id)
    00135 {
    00136   if (!IsValidEngine(engine_id)) return -1;
    00137   if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
    00138
    00139   return ::Engine::Get(engine_id)->GetLifeLengthInDays();
    00140 }
    00141
    00142 /* static */ Money AIEngine::GetRunningCost(EngineID engine_id)
    00143 {
    00144   if (!IsValidEngine(engine_id)) return -1;
    00145
    00146   return ::Engine::Get(engine_id)->GetRunningCost();
    00147 }
    00148
    00149 /* static */ int32 AIEngine::GetPower(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 ::Engine::Get(engine_id)->GetPower();
    00156 }
    00157
    00158 /* static */ int32 AIEngine::GetWeight(EngineID engine_id)
    00159 {
    00160   if (!IsValidEngine(engine_id)) return -1;
    00161   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00162
    00163   return ::Engine::Get(engine_id)->GetDisplayWeight();
    00164 }
    00165
    00166 /* static */ int32 AIEngine::GetMaxTractiveEffort(EngineID engine_id)
    00167 {
    00168   if (!IsValidEngine(engine_id)) return -1;
    00169   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
    00170   if (IsWagon(engine_id)) return -1;
    00171
    00172   return ::Engine::Get(engine_id)->GetDisplayMaxTractiveEffort();
    00173 }
    00174
    00175 /* static */ int32 AIEngine::GetDesignDate(EngineID engine_id)
    00176 {
    00177   if (!IsValidEngine(engine_id)) return -1;
    00178
    00179   return ::Engine::Get(engine_id)->intro_date;
    00180 }
    00181
    00182 /* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
    00183 {
    00184   if (!IsValidEngine(engine_id)) return AIVehicle::VT_INVALID;
    00185
    00186   switch (::Engine::Get(engine_id)->type) {
    00187     case VEH_ROAD:     return AIVehicle::VT_ROAD;
    00188     case VEH_TRAIN:    return AIVehicle::VT_RAIL;
    00189     case VEH_SHIP:     return AIVehicle::VT_WATER;
    00190     case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
    00191     default: NOT_REACHED();
    00192   }
    00193 }
    00194
    00195 /* static */ bool AIEngine::IsWagon(EngineID engine_id)
    00196 {
    00197   if (!IsValidEngine(engine_id)) return false;
    00198   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00199
    00200   return ::RailVehInfo(engine_id)->power == 0;
    00201 }
    00202
    00203 /* static */ bool AIEngine::CanRunOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00204 {
    00205   if (!IsValidEngine(engine_id)) return false;
    00206   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00207   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00208
    00209   return ::IsCompatibleRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00210 }
    00211
    00212 /* static */ bool AIEngine::HasPowerOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
    00213 {
    00214   if (!IsValidEngine(engine_id)) return false;
    00215   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00216   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00217
    00218   return ::HasPowerOnRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
    00219 }
    00220
    00221 /* static */ AIRoad::RoadType AIEngine::GetRoadType(EngineID engine_id)
    00222 {
    00223   if (!IsValidEngine(engine_id)) return AIRoad::ROADTYPE_INVALID;
    00224   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD) return AIRoad::ROADTYPE_INVALID;
    00225
    00226   return HasBit(::EngInfo(engine_id)->misc_flags, EF_ROAD_TRAM) ? AIRoad::ROADTYPE_TRAM : AIRoad::ROADTYPE_ROAD;
    00227 }
    00228
    00229 /* static */ AIRail::RailType AIEngine::GetRailType(EngineID engine_id)
    00230 {
    00231   if (!IsValidEngine(engine_id)) return AIRail::RAILTYPE_INVALID;
    00232   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return AIRail::RAILTYPE_INVALID;
    00233
    00234   return (AIRail::RailType)(uint)::RailVehInfo(engine_id)->railtype;
    00235 }
    00236
    00237 /* static */ bool AIEngine::IsArticulated(EngineID engine_id)
    00238 {
    00239   if (!IsValidEngine(engine_id)) return false;
    00240   if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD && GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
    00241
    00242   return CountArticulatedParts(engine_id, true) != 0;
    00243 }
    00244
    00245 /* static */ AIAirport::PlaneType AIEngine::GetPlaneType(EngineID engine_id)
    00246 {
    00247   if (!IsValidEngine(engine_id)) return AIAirport::PT_INVALID;
    00248   if (GetVehicleType(engine_id) != AIVehicle::VT_AIR) return AIAirport::PT_INVALID;
    00249
    00250   return (AIAirport::PlaneType)::AircraftVehInfo(engine_id)->subtype;
    00251 }