# ai_engine.cpp

Go to the documentation of this file.

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