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

Function CopyHeadSpecificThings

src/autoreplace_cmd.cpp:416–449  ·  view source on GitHub ↗

* Copy head specific things to the new vehicle chain after it was successfully constructed * @param old_head The old front vehicle (no wagons attached anymore) * @param new_head The new head of the completely replaced vehicle chain * @param flags the command flags to use */

Source from the content-addressed store, hash-verified

414 * @param flags the command flags to use
415 */
416static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, DoCommandFlags flags)
417{
418 CommandCost cost = CommandCost();
419
420 /* Share orders */
421 if (cost.Succeeded() && old_head != new_head) cost.AddCost(Command<CMD_CLONE_ORDER>::Do(DoCommandFlag::Execute, CO_SHARE, new_head->index, old_head->index));
422
423 /* Copy group membership */
424 if (cost.Succeeded() && old_head != new_head) cost.AddCost(std::get<0>(Command<CMD_ADD_VEHICLE_GROUP>::Do(DoCommandFlag::Execute, old_head->group_id, new_head->index, false, VehicleListIdentifier{})));
425
426 /* Perform start/stop check whether the new vehicle suits newgrf restrictions etc. */
427 if (cost.Succeeded()) {
428 /* Start the vehicle, might be denied by certain things */
429 assert(new_head->vehstatus.Test(VehState::Stopped));
430 cost.AddCost(DoCmdStartStopVehicle(new_head, true));
431
432 /* Stop the vehicle again, but do not care about evil newgrfs allowing starting but not stopping :p */
433 if (cost.Succeeded()) cost.AddCost(DoCmdStartStopVehicle(new_head, false));
434 }
435
436 /* Last do those things which do never fail (resp. we do not care about), but which are not undo-able */
437 if (cost.Succeeded() && old_head != new_head && flags.Test(DoCommandFlag::Execute)) {
438 /* Copy other things which cannot be copied by a command and which shall not stay reset from the build vehicle command */
439 new_head->CopyVehicleConfigAndStatistics(old_head);
440 GroupStatistics::AddProfitLastYear(new_head);
441
442 /* Switch vehicle windows/news to the new vehicle, so they are not closed/deleted when the old vehicle is sold */
443 ChangeVehicleViewports(old_head->index, new_head->index);
444 ChangeVehicleViewWindow(old_head->index, new_head->index);
445 ChangeVehicleNews(old_head->index, new_head->index);
446 }
447
448 return cost;
449}
450
451/**
452 * Replace a single unit in a free wagon chain

Callers 1

ReplaceChainFunction · 0.85

Calls 9

CommandCostClass · 0.85
DoCmdStartStopVehicleFunction · 0.85
ChangeVehicleViewportsFunction · 0.85
ChangeVehicleViewWindowFunction · 0.85
ChangeVehicleNewsFunction · 0.85
SucceededMethod · 0.80
TestMethod · 0.80
AddCostMethod · 0.45

Tested by

no test coverage detected