ai_engine.cpp

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

Generated on Wed Mar 31 22:43:20 2010 for OpenTTD by  doxygen 1.6.1