ai_engine.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "../../stdafx.h"
00013 #include "ai_engine.hpp"
00014 #include "ai_cargo.hpp"
00015 #include "../../company_func.h"
00016 #include "../../company_base.h"
00017 #include "../../strings_func.h"
00018 #include "../../rail.h"
00019 #include "../../engine_base.h"
00020 #include "../../engine_func.h"
00021 #include "../../articulated_vehicles.h"
00022 #include "table/strings.h"
00023 
00024 /* static */ bool AIEngine::IsValidEngine(EngineID engine_id)
00025 {
00026   const Engine *e = ::Engine::GetIfValid(engine_id);
00027   return e != NULL && (::IsEngineBuildable(engine_id, e->type, _current_company) || ::Company::Get(_current_company)->num_engines[engine_id] > 0);
00028 
00029 }
00030 
00031 /* static */ bool AIEngine::IsBuildable(EngineID engine_id)
00032 {
00033   const Engine *e = ::Engine::GetIfValid(engine_id);
00034   return e != NULL && ::IsEngineBuildable(engine_id, e->type, _current_company);
00035 }
00036 
00037 /* static */ char *AIEngine::GetName(EngineID engine_id)
00038 {
00039   if (!IsValidEngine(engine_id)) return NULL;
00040 
00041   static const int len = 64;
00042   char *engine_name = MallocT<char>(len);
00043 
00044 	::SetDParam(0, engine_id);
00045   ::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
00046   return engine_name;
00047 }
00048 
00049 /* static */ CargoID AIEngine::GetCargoType(EngineID engine_id)
00050 {
00051   if (!IsValidEngine(engine_id)) return CT_INVALID;
00052 
00053   CargoArray cap = ::GetCapacityOfArticulatedParts(engine_id);
00054 
00055   CargoID most_cargo = CT_INVALID;
00056   uint amount = 0;
00057   for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00058     if (cap[cid] > amount) {
00059       amount = cap[cid];
00060       most_cargo = cid;
00061     }
00062   }
00063 
00064   return most_cargo;
00065 }
00066 
00067 /* static */ bool AIEngine::CanRefitCargo(EngineID engine_id, CargoID cargo_id)
00068 {
00069   if (!IsValidEngine(engine_id)) return false;
00070   if (!AICargo::IsValidCargo(cargo_id)) return false;
00071 
00072   return HasBit(::GetUnionOfArticulatedRefitMasks(engine_id, true), cargo_id);
00073 }
00074 
00075 /* static */ bool AIEngine::CanPullCargo(EngineID engine_id, CargoID cargo_id)
00076 {
00077   if (!IsValidEngine(engine_id)) return false;
00078   if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
00079   if (!AICargo::IsValidCargo(cargo_id)) return false;
00080 
00081   return (::RailVehInfo(engine_id)->ai_passenger_only != 1) || AICargo::HasCargoClass(cargo_id, AICargo::CC_PASSENGERS);
00082 }
00083 
00084 
00085 /* static */ int32 AIEngine::GetCapacity(EngineID engine_id)
00086 {
00087   if (!IsValidEngine(engine_id)) return -1;
00088 
00089   const Engine *e = ::Engine::Get(engine_id);
00090   switch (e->type) {
00091     case VEH_ROAD:
00092     case VEH_TRAIN: {
00093       CargoArray capacities = GetCapacityOfArticulatedParts(engine_id);
00094       for (CargoID c = 0; c < NUM_CARGO; c++) {
00095         if (capacities[c] == 0) continue;
00096         return capacities[c];
00097       }
00098       return -1;
00099     }
00100 
00101     case VEH_SHIP:
00102     case VEH_AIRCRAFT:
00103       return e->GetDisplayDefaultCapacity();
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 && GetVehicleType(engine_id) != AIVehicle::VT_ROAD) 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 && GetVehicleType(engine_id) != AIVehicle::VT_ROAD) 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 && GetVehicleType(engine_id) != AIVehicle::VT_ROAD) 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 }

Generated on Thu Apr 14 00:48:10 2011 for OpenTTD by  doxygen 1.6.1