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

Function GetNewEngineType

src/autoreplace_cmd.cpp:281–309  ·  view source on GitHub ↗

* Get the EngineID of the replacement for a vehicle * @param v The vehicle to find a replacement for * @param c The vehicle's owner (it's faster to forward the pointer than refinding it) * @param always_replace Always replace, even if not old. * @param[out] e the EngineID of the replacement. EngineID::Invalid() if no replacement is found * @return Error if the engine to build is not available

Source from the content-addressed store, hash-verified

279 * @return Error if the engine to build is not available
280 */
281static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, bool always_replace, EngineID &e)
282{
283 assert(v->type != VEH_TRAIN || !v->IsArticulatedPart());
284
285 e = EngineID::Invalid();
286
287 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
288 /* we build the rear ends of multiheaded trains with the front ones */
289 return CommandCost();
290 }
291
292 bool replace_when_old;
293 e = EngineReplacementForCompany(c, v->engine_type, v->group_id, &replace_when_old);
294 if (!always_replace && replace_when_old && !v->NeedsAutorenewing(c, false)) e = EngineID::Invalid();
295
296 /* Autoreplace, if engine is available */
297 if (e != EngineID::Invalid() && IsEngineBuildable(e, v->type, _current_company)) {
298 return CommandCost();
299 }
300
301 /* Autorenew if needed */
302 if (v->NeedsAutorenewing(c)) e = v->engine_type;
303
304 /* Nothing to do or all is fine? */
305 if (e == EngineID::Invalid() || IsEngineBuildable(e, v->type, _current_company)) return CommandCost();
306
307 /* The engine we need is not available. Report error to user */
308 return CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + v->type);
309}
310
311/**
312 * Builds and refits a replacement vehicle

Callers 2

BuildReplacementVehicleFunction · 0.85
CmdAutoreplaceVehicleFunction · 0.85

Calls 7

InvalidFunction · 0.85
CommandCostClass · 0.85
IsEngineBuildableFunction · 0.85
IsArticulatedPartMethod · 0.80
IsRearDualheadedMethod · 0.80
NeedsAutorenewingMethod · 0.80

Tested by

no test coverage detected