Files
@ r11188:143f3c64befd
Branch filter:
Location: cpp/openttd-patchpack/source/src/ai/api/ai_engine.cpp - annotation
r11188:143f3c64befd
7.0 KiB
text/x-c
(svn r15541) -Revert (r15399): 'v->cargo_type' is also used in other places, which cannot accept CT_INVALID.
-Codechange: Add Engine::GetDefaultCargoType() and Engine::CanCarryCargo() and use them.
-Fix [FS#2617]: When articulated parts have no available default cargo, use the cargo type of the first part for livery selection.
-Change: To decide whether a vehicle is refittable do not test its current capacity for being zero, but always use the 'capacity property'.
Note: The property is used unmodifed without calling CB 15/36. By setting it to a non-zero value and returning zero in the callback vehicles can be refitted to/from zero capacity for e.g. livery effects.
Note: It is intentional that you cannot control refittability by CB 36.
-Codechange: Add Engine::GetDefaultCargoType() and Engine::CanCarryCargo() and use them.
-Fix [FS#2617]: When articulated parts have no available default cargo, use the cargo type of the first part for livery selection.
-Change: To decide whether a vehicle is refittable do not test its current capacity for being zero, but always use the 'capacity property'.
Note: The property is used unmodifed without calling CB 15/36. By setting it to a non-zero value and returning zero in the callback vehicles can be refitted to/from zero capacity for e.g. livery effects.
Note: It is intentional that you cannot control refittability by CB 36.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10790:a63d33c87344 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r11188:143f3c64befd r11188:143f3c64befd r10696:8dfe83e30d01 r11188:143f3c64befd r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r11173:db2748dc0959 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10967:2c98940d6c64 r10969:8b8a9653d684 r10967:2c98940d6c64 r10967:2c98940d6c64 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10927:eb69eaf0227d r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r11173:db2748dc0959 r10696:8dfe83e30d01 r10744:a93a9430d707 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10925:e45ad06a0739 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r11173:db2748dc0959 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10768:5cefaf608914 r10768:5cefaf608914 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10768:5cefaf608914 r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10696:8dfe83e30d01 | /* $Id$ */
/** @file ai_engine.cpp Implementation of AIEngine. */
#include "ai_engine.hpp"
#include "ai_cargo.hpp"
#include "../../company_func.h"
#include "../../strings_func.h"
#include "../../aircraft.h"
#include "../../vehicle_func.h"
#include "../../settings_type.h"
#include "../../articulated_vehicles.h"
#include "table/strings.h"
/* static */ bool AIEngine::IsValidEngine(EngineID engine_id)
{
return ::IsEngineIndex(engine_id) && HasBit(::GetEngine(engine_id)->company_avail, _current_company);
}
/* static */ char *AIEngine::GetName(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return NULL;
static const int len = 64;
char *engine_name = MallocT<char>(len);
::SetDParam(0, engine_id);
::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
return engine_name;
}
/* static */ CargoID AIEngine::GetCargoType(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return CT_INVALID;
const Engine *e = ::GetEngine(engine_id);
if (!e->CanCarryCargo()) return CT_INVALID;
return e->GetDefaultCargoType();
}
/* static */ bool AIEngine::CanRefitCargo(EngineID engine_id, CargoID cargo_id)
{
if (!IsValidEngine(engine_id)) return false;
if (!AICargo::IsValidCargo(cargo_id)) return false;
if (GetCargoType(engine_id) == cargo_id) return true;
if (cargo_id == CT_MAIL && ::GetEngine(engine_id)->type == VEH_AIRCRAFT) return true;
if (::GetEngine(engine_id)->type == VEH_SHIP && !ShipVehInfo(engine_id)->refittable) return false;
return ::CanRefitTo(engine_id, cargo_id);
}
/* static */ bool AIEngine::CanPullCargo(EngineID engine_id, CargoID cargo_id)
{
if (!IsValidEngine(engine_id)) return false;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
if (!AICargo::IsValidCargo(cargo_id)) return false;
return (::RailVehInfo(engine_id)->ai_passenger_only != 1) || AICargo::HasCargoClass(cargo_id, AICargo::CC_PASSENGERS);
}
/* static */ int32 AIEngine::GetCapacity(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
switch (::GetEngine(engine_id)->type) {
case VEH_ROAD:
case VEH_TRAIN: {
uint16 *capacities = GetCapacityOfArticulatedParts(engine_id, ::GetEngine(engine_id)->type);
for (CargoID c = 0; c < NUM_CARGO; c++) {
if (capacities[c] == 0) continue;
return capacities[c];
}
return -1;
} break;
case VEH_SHIP: {
const ShipVehicleInfo *vi = ::ShipVehInfo(engine_id);
return vi->capacity;
} break;
case VEH_AIRCRAFT: {
const AircraftVehicleInfo *vi = ::AircraftVehInfo(engine_id);
return vi->passenger_capacity;
} break;
default: NOT_REACHED();
}
}
/* static */ int32 AIEngine::GetReliability(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
return (::GetEngine(engine_id)->reliability * 100 >> 16);
}
/* static */ int32 AIEngine::GetMaxSpeed(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
const Engine *e = ::GetEngine(engine_id);
int32 max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
return max_speed;
}
/* static */ Money AIEngine::GetPrice(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
return ::GetEngine(engine_id)->GetCost();
}
/* static */ int32 AIEngine::GetMaxAge(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
return ::GetEngine(engine_id)->lifelength * DAYS_IN_LEAP_YEAR;
}
/* static */ Money AIEngine::GetRunningCost(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
return ::GetEngine(engine_id)->GetRunningCost();
}
/* static */ int32 AIEngine::GetPower(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
if (IsWagon(engine_id)) return -1;
return ::GetEngine(engine_id)->GetPower();
}
/* static */ int32 AIEngine::GetWeight(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
return ::GetEngine(engine_id)->GetDisplayWeight();
}
/* static */ int32 AIEngine::GetMaxTractiveEffort(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return -1;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
if (IsWagon(engine_id)) return -1;
return ::GetEngine(engine_id)->GetDisplayMaxTractiveEffort();
}
/* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return AIVehicle::VT_INVALID;
switch (::GetEngine(engine_id)->type) {
case VEH_ROAD: return AIVehicle::VT_ROAD;
case VEH_TRAIN: return AIVehicle::VT_RAIL;
case VEH_SHIP: return AIVehicle::VT_WATER;
case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
default: NOT_REACHED();
}
}
/* static */ bool AIEngine::IsWagon(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return false;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
return ::RailVehInfo(engine_id)->power == 0;
}
/* static */ bool AIEngine::CanRunOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
{
if (!IsValidEngine(engine_id)) return false;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
return ::IsCompatibleRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
}
/* static */ bool AIEngine::HasPowerOnRail(EngineID engine_id, AIRail::RailType track_rail_type)
{
if (!IsValidEngine(engine_id)) return false;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
return ::HasPowerOnRail((::RailType)::RailVehInfo(engine_id)->railtype, (::RailType)track_rail_type);
}
/* static */ AIRoad::RoadType AIEngine::GetRoadType(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return AIRoad::ROADTYPE_INVALID;
if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD) return AIRoad::ROADTYPE_INVALID;
return HasBit(::EngInfo(engine_id)->misc_flags, EF_ROAD_TRAM) ? AIRoad::ROADTYPE_TRAM : AIRoad::ROADTYPE_ROAD;
}
/* static */ AIRail::RailType AIEngine::GetRailType(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return AIRail::RAILTYPE_INVALID;
if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return AIRail::RAILTYPE_INVALID;
return (AIRail::RailType)(uint)::RailVehInfo(engine_id)->railtype;
}
/* static */ bool AIEngine::IsArticulated(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return false;
if (GetVehicleType(engine_id) != AIVehicle::VT_ROAD && GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return false;
return CountArticulatedParts(engine_id, true) != 0;
}
/* static */ AIAirport::PlaneType AIEngine::GetPlaneType(EngineID engine_id)
{
if (!IsValidEngine(engine_id)) return AIAirport::PT_INVALID;
if (GetVehicleType(engine_id) != AIVehicle::VT_AIR) return AIAirport::PT_INVALID;
return (AIAirport::PlaneType)::AircraftVehInfo(engine_id)->subtype;
}
|