MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / DetermineCapacity

Method DetermineCapacity

src/engine.cpp:200–274  ·  view source on GitHub ↗

* Determines capacity of a given vehicle from scratch. * For aircraft the main capacity is determined. Mail might be present as well. * @param v Vehicle of interest; nullptr in purchase list * @param mail_capacity returns secondary cargo (mail) capacity of aircraft * @return Capacity */

Source from the content-addressed store, hash-verified

198 * @return Capacity
199 */
200uint Engine::DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity) const
201{
202 assert(v == nullptr || this->index == v->engine_type);
203 if (mail_capacity != nullptr) *mail_capacity = 0;
204
205 if (!this->CanCarryCargo()) return 0;
206
207 bool new_multipliers = this->info.misc_flags.Test(EngineMiscFlag::NoDefaultCargoMultiplier);
208 CargoType default_cargo = this->GetDefaultCargoType();
209 CargoType cargo_type = (v != nullptr) ? v->cargo_type : default_cargo;
210
211 if (mail_capacity != nullptr && this->type == VEH_AIRCRAFT && IsCargoInClass(cargo_type, CargoClass::Passengers)) {
212 *mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->VehInfo<AircraftVehicleInfo>().mail_capacity, v);
213 }
214
215 /* Check the refit capacity callback if we are not in the default configuration, or if we are using the new multiplier algorithm. */
216 if (this->info.callback_mask.Test(VehicleCallbackMask::RefitCapacity) &&
217 (new_multipliers || default_cargo != cargo_type || (v != nullptr && v->cargo_subtype != 0))) {
218 uint16_t callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, this->index, v);
219 if (callback != CALLBACK_FAILED) return callback;
220 }
221
222 /* Get capacity according to property resp. CB */
223 uint capacity;
224 uint extra_mail_cap = 0;
225 switch (this->type) {
226 case VEH_TRAIN:
227 capacity = GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->VehInfo<RailVehicleInfo>().capacity, v);
228
229 /* In purchase list add the capacity of the second head. Always use the plain property for this. */
230 if (v == nullptr && this->VehInfo<RailVehicleInfo>().railveh_type == RAILVEH_MULTIHEAD) capacity += this->VehInfo<RailVehicleInfo>().capacity;
231 break;
232
233 case VEH_ROAD:
234 capacity = GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->VehInfo<RoadVehicleInfo>().capacity, v);
235 break;
236
237 case VEH_SHIP:
238 capacity = GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->VehInfo<ShipVehicleInfo>().capacity, v);
239 break;
240
241 case VEH_AIRCRAFT:
242 capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->VehInfo<AircraftVehicleInfo>().passenger_capacity, v);
243 if (!IsCargoInClass(cargo_type, CargoClass::Passengers)) {
244 extra_mail_cap = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->VehInfo<AircraftVehicleInfo>().mail_capacity, v);
245 }
246 if (IsValidCargoType(GetCargoTypeByLabel(CT_MAIL))) {
247 if (!new_multipliers && cargo_type == GetCargoTypeByLabel(CT_MAIL)) return capacity + extra_mail_cap;
248 }
249 default_cargo = GetCargoTypeByLabel(CT_PASSENGERS); // Always use 'passengers' wrt. cargo multipliers
250 break;
251
252 default: NOT_REACHED();
253 }
254
255 if (!new_multipliers) {
256 /* Use the passenger multiplier for mail as well */
257 capacity += extra_mail_cap;

Callers 9

CmdBuildShipFunction · 0.80
ConsistChangedMethod · 0.80
GetMaxWeightMethod · 0.80
RefitVehicleFunction · 0.80
CmdBuildRoadVehicleFunction · 0.80
GetMaxWeightMethod · 0.80
CmdBuildAircraftFunction · 0.80
HandleRefitMethod · 0.80

Calls 9

CanCarryCargoMethod · 0.95
GetDefaultCargoTypeMethod · 0.95
IsCargoInClassFunction · 0.85
GetEnginePropertyFunction · 0.85
GetVehicleCallbackFunction · 0.85
IsValidCargoTypeFunction · 0.85
GetCargoTypeByLabelFunction · 0.85
NOT_REACHEDFunction · 0.85
TestMethod · 0.80

Tested by

no test coverage detected