* 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
| 279 | * @return Error if the engine to build is not available |
| 280 | */ |
| 281 | static 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 |
no test coverage detected