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

Function CmdChangeTimetable

src/timetable_cmd.cpp:133–217  ·  view source on GitHub ↗

* Change timetable data of an order. * @param flags Operation to perform. * @param veh Vehicle with the orders to change. * @param order_number Order index to modify. * @param mtf Timetable data to change (@see ModifyTimetableFlags) * @param data The data to modify as specified by \c mtf. * 0 to clear times, UINT16_MAX to clear speed limit. * @return the cost of this operation o

Source from the content-addressed store, hash-verified

131 * @return the cost of this operation or an error
132 */
133CommandCost CmdChangeTimetable(DoCommandFlags flags, VehicleID veh, VehicleOrderID order_number, ModifyTimetableFlags mtf, uint16_t data)
134{
135 Vehicle *v = Vehicle::GetIfValid(veh);
136 if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
137
138 CommandCost ret = CheckOwnership(v->owner);
139 if (ret.Failed()) return ret;
140
141 Order *order = v->GetOrder(order_number);
142 if (order == nullptr || order->IsType(OT_IMPLICIT)) return CMD_ERROR;
143
144 if (mtf >= MTF_END) return CMD_ERROR;
145
146 int wait_time = order->GetWaitTime();
147 int travel_time = order->GetTravelTime();
148 int max_speed = order->GetMaxSpeed();
149 switch (mtf) {
150 case MTF_WAIT_TIME:
151 wait_time = data;
152 break;
153
154 case MTF_TRAVEL_TIME:
155 travel_time = data;
156 break;
157
158 case MTF_TRAVEL_SPEED:
159 max_speed = data;
160 if (max_speed == 0) max_speed = UINT16_MAX; // Disable speed limit.
161 break;
162
163 default:
164 NOT_REACHED();
165 }
166
167 if (wait_time != order->GetWaitTime()) {
168 switch (order->GetType()) {
169 case OT_GOTO_STATION:
170 if (order->GetNonStopType().Test(OrderNonStopFlag::NoDestination)) return CommandCost(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
171 break;
172
173 case OT_CONDITIONAL:
174 break;
175
176 default: return CommandCost(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
177 }
178 }
179
180 if (travel_time != order->GetTravelTime() && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
181 if (max_speed != order->GetMaxSpeed() && (order->IsType(OT_CONDITIONAL) || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
182
183 if (flags.Test(DoCommandFlag::Execute)) {
184 switch (mtf) {
185 case MTF_WAIT_TIME:
186 /* Set time if changing the value or confirming an estimated time as timetabled. */
187 if (wait_time != order->GetWaitTime() || (wait_time > 0 && !order->IsWaitTimetabled())) {
188 ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME, wait_time > 0);
189 }
190 break;

Callers

nothing calls this directly

Calls 15

CheckOwnershipFunction · 0.85
NOT_REACHEDFunction · 0.85
CommandCostClass · 0.85
ChangeTimetableFunction · 0.85
FailedMethod · 0.80
GetOrderMethod · 0.80
IsTypeMethod · 0.80
GetWaitTimeMethod · 0.80
GetTravelTimeMethod · 0.80
TestMethod · 0.80
GetNonStopTypeMethod · 0.80
IsWaitTimetabledMethod · 0.80

Tested by

no test coverage detected