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

Function CmdOrderRefit

src/order_cmd.cpp:1634–1673  ·  view source on GitHub ↗

* Add/remove refit orders from an order * @param flags operation to perform * @param veh VehicleIndex of the vehicle having the order * @param order_number number of order to modify * @param cargo CargoType * @return the cost of this operation or an error */

Source from the content-addressed store, hash-verified

1632 * @return the cost of this operation or an error
1633 */
1634CommandCost CmdOrderRefit(DoCommandFlags flags, VehicleID veh, VehicleOrderID order_number, CargoType cargo)
1635{
1636 if (cargo >= NUM_CARGO && cargo != CARGO_NO_REFIT && cargo != CARGO_AUTO_REFIT) return CMD_ERROR;
1637
1638 const Vehicle *v = Vehicle::GetIfValid(veh);
1639 if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
1640
1641 CommandCost ret = CheckOwnership(v->owner);
1642 if (ret.Failed()) return ret;
1643
1644 Order *order = v->GetOrder(order_number);
1645 if (order == nullptr) return CMD_ERROR;
1646
1647 /* Automatic refit cargo is only supported for goto station orders. */
1648 if (cargo == CARGO_AUTO_REFIT && !order->IsType(OT_GOTO_STATION)) return CMD_ERROR;
1649
1650 if (order->GetLoadType() == OrderLoadType::NoLoad) return CMD_ERROR;
1651
1652 if (flags.Test(DoCommandFlag::Execute)) {
1653 order->SetRefit(cargo);
1654
1655 /* Make the depot order an 'always go' order. */
1656 if (cargo != CARGO_NO_REFIT && order->IsType(OT_GOTO_DEPOT)) {
1657 order->SetDepotOrderType(order->GetDepotOrderType().Reset(OrderDepotTypeFlag::Service));
1658 order->SetDepotActionType(order->GetDepotActionType().Reset(OrderDepotActionFlag::Halt));
1659 }
1660
1661 for (Vehicle *u = v->FirstShared(); u != nullptr; u = u->NextShared()) {
1662 /* Update any possible open window of the vehicle */
1663 InvalidateVehicleOrder(u, VIWD_MODIFY_ORDERS);
1664
1665 /* If the vehicle already got the current depot set as current order, then update current order as well */
1666 if (u->cur_real_order_index == order_number && u->current_order.GetDepotOrderType().Test(OrderDepotTypeFlag::PartOfOrders)) {
1667 u->current_order.SetRefit(cargo);
1668 }
1669 }
1670 }
1671
1672 return CommandCost();
1673}
1674
1675
1676/**

Callers

nothing calls this directly

Calls 15

CheckOwnershipFunction · 0.85
InvalidateVehicleOrderFunction · 0.85
CommandCostClass · 0.85
FailedMethod · 0.80
GetOrderMethod · 0.80
IsTypeMethod · 0.80
GetLoadTypeMethod · 0.80
TestMethod · 0.80
SetRefitMethod · 0.80
SetDepotOrderTypeMethod · 0.80
GetDepotOrderTypeMethod · 0.80
SetDepotActionTypeMethod · 0.80

Tested by

no test coverage detected