2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file timetable_cmd.cpp Commands related to time tabling. */
11 #include "command_func.h"
12 #include "company_func.h"
13 #include "date_func.h"
14 #include "window_func.h"
15 #include "vehicle_base.h"
16 #include "timetable_cmd.h"
18 #include "table/strings.h"
20 #include "safeguards.h"
23 * Change/update a particular timetable entry.
24 * @param v The vehicle to change the timetable of.
25 * @param order_number The index of the timetable in the order list.
26 * @param val The new data of the timetable entry.
27 * @param mtf Which part of the timetable entry to change.
28 * @param timetabled If the new value is explicitly timetabled.
30 static void ChangeTimetable(Vehicle
*v
, VehicleOrderID order_number
, uint16 val
, ModifyTimetableFlags mtf
, bool timetabled
)
32 Order
*order
= v
->GetOrder(order_number
);
34 int timetable_delta
= 0;
38 total_delta
= val
- order
->GetWaitTime();
39 timetable_delta
= (timetabled
? val
: 0) - order
->GetTimetabledWait();
40 order
->SetWaitTime(val
);
41 order
->SetWaitTimetabled(timetabled
);
45 total_delta
= val
- order
->GetTravelTime();
46 timetable_delta
= (timetabled
? val
: 0) - order
->GetTimetabledTravel();
47 order
->SetTravelTime(val
);
48 order
->SetTravelTimetabled(timetabled
);
51 case MTF_TRAVEL_SPEED
:
52 order
->SetMaxSpeed(val
);
58 v
->orders
->UpdateTotalDuration(total_delta
);
59 v
->orders
->UpdateTimetableDuration(timetable_delta
);
61 for (v
= v
->FirstShared(); v
!= nullptr; v
= v
->NextShared()) {
62 if (v
->cur_real_order_index
== order_number
&& v
->current_order
.Equals(*order
)) {
65 v
->current_order
.SetWaitTime(val
);
66 v
->current_order
.SetWaitTimetabled(timetabled
);
70 v
->current_order
.SetTravelTime(val
);
71 v
->current_order
.SetTravelTimetabled(timetabled
);
74 case MTF_TRAVEL_SPEED
:
75 v
->current_order
.SetMaxSpeed(val
);
82 SetWindowDirty(WC_VEHICLE_TIMETABLE
, v
->index
);
87 * Change timetable data of an order.
88 * @param flags Operation to perform.
89 * @param veh Vehicle with the orders to change.
90 * @param order_number Order index to modify.
91 * @param mtf Timetable data to change (@see ModifyTimetableFlags)
92 * @param data The data to modify as specified by \c mtf.
93 * 0 to clear times, UINT16_MAX to clear speed limit.
94 * @return the cost of this operation or an error
96 CommandCost
CmdChangeTimetable(DoCommandFlag flags
, VehicleID veh
, VehicleOrderID order_number
, ModifyTimetableFlags mtf
, uint16 data
)
98 Vehicle
*v
= Vehicle::GetIfValid(veh
);
99 if (v
== nullptr || !v
->IsPrimaryVehicle()) return CMD_ERROR
;
101 CommandCost ret
= CheckOwnership(v
->owner
);
102 if (ret
.Failed()) return ret
;
104 Order
*order
= v
->GetOrder(order_number
);
105 if (order
== nullptr || order
->IsType(OT_IMPLICIT
)) return CMD_ERROR
;
107 if (mtf
>= MTF_END
) return CMD_ERROR
;
109 int wait_time
= order
->GetWaitTime();
110 int travel_time
= order
->GetTravelTime();
111 int max_speed
= order
->GetMaxSpeed();
117 case MTF_TRAVEL_TIME
:
121 case MTF_TRAVEL_SPEED
:
123 if (max_speed
== 0) max_speed
= UINT16_MAX
; // Disable speed limit.
130 if (wait_time
!= order
->GetWaitTime()) {
131 switch (order
->GetType()) {
132 case OT_GOTO_STATION
:
133 if (order
->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE
);
139 default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS
);
143 if (travel_time
!= order
->GetTravelTime() && order
->IsType(OT_CONDITIONAL
)) return CMD_ERROR
;
144 if (max_speed
!= order
->GetMaxSpeed() && (order
->IsType(OT_CONDITIONAL
) || v
->type
== VEH_AIRCRAFT
)) return CMD_ERROR
;
146 if (flags
& DC_EXEC
) {
149 /* Set time if changing the value or confirming an estimated time as timetabled. */
150 if (wait_time
!= order
->GetWaitTime() || (wait_time
> 0 && !order
->IsWaitTimetabled())) {
151 ChangeTimetable(v
, order_number
, wait_time
, MTF_WAIT_TIME
, wait_time
> 0);
155 case MTF_TRAVEL_TIME
:
156 /* Set time if changing the value or confirming an estimated time as timetabled. */
157 if (travel_time
!= order
->GetTravelTime() || (travel_time
> 0 && !order
->IsTravelTimetabled())) {
158 ChangeTimetable(v
, order_number
, travel_time
, MTF_TRAVEL_TIME
, travel_time
> 0);
162 case MTF_TRAVEL_SPEED
:
163 if (max_speed
!= order
->GetMaxSpeed()) {
164 ChangeTimetable(v
, order_number
, max_speed
, MTF_TRAVEL_SPEED
, max_speed
!= UINT16_MAX
);
173 return CommandCost();
177 * Clear the lateness counter to make the vehicle on time.
178 * @param flags Operation to perform.
179 * @param veh Vehicle with the orders to change.
180 * @return the cost of this operation or an error
182 CommandCost
CmdSetVehicleOnTime(DoCommandFlag flags
, VehicleID veh
)
184 Vehicle
*v
= Vehicle::GetIfValid(veh
);
185 if (v
== nullptr || !v
->IsPrimaryVehicle() || v
->orders
== nullptr) return CMD_ERROR
;
187 CommandCost ret
= CheckOwnership(v
->owner
);
188 if (ret
.Failed()) return ret
;
190 if (flags
& DC_EXEC
) {
191 v
->lateness_counter
= 0;
192 SetWindowDirty(WC_VEHICLE_TIMETABLE
, v
->index
);
195 return CommandCost();
199 * Order vehicles based on their timetable. The vehicles will be sorted in order
200 * they would reach the first station.
202 * @param a First Vehicle pointer.
203 * @param b Second Vehicle pointer.
204 * @return Comparison value.
206 static bool VehicleTimetableSorter(Vehicle
* const &a
, Vehicle
* const &b
)
208 VehicleOrderID a_order
= a
->cur_real_order_index
;
209 VehicleOrderID b_order
= b
->cur_real_order_index
;
210 int j
= (int)b_order
- (int)a_order
;
212 /* Are we currently at an ordered station (un)loading? */
213 bool a_load
= a
->current_order
.IsType(OT_LOADING
) && a
->current_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
;
214 bool b_load
= b
->current_order
.IsType(OT_LOADING
) && b
->current_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
;
216 /* If the current order is not loading at the ordered station, decrease the order index by one since we have
217 * not yet arrived at the station (and thus the timetable entry; still in the travelling of the previous one).
218 * Since the ?_order variables are unsigned the -1 will flow under and place the vehicles going to order #0 at
219 * the begin of the list with vehicles arriving at #0. */
220 if (!a_load
) a_order
--;
221 if (!b_load
) b_order
--;
223 /* First check the order index that accounted for loading, then just the raw one. */
224 int i
= (int)b_order
- (int)a_order
;
225 if (i
!= 0) return i
< 0;
226 if (j
!= 0) return j
< 0;
228 /* Look at the time we spent in this order; the higher, the closer to its destination. */
229 i
= b
->current_order_time
- a
->current_order_time
;
230 if (i
!= 0) return i
< 0;
232 /* If all else is equal, use some unique index to sort it the same way. */
233 return b
->unitnumber
< a
->unitnumber
;
237 * Set the start date of the timetable.
238 * @param flags Operation to perform.
239 * @param veh_id Vehicle ID.
240 * @param timetable_all Set to set timetable start for all vehicles sharing this order
241 * @param start_date The timetable start date.
242 * @return The error or cost of the operation.
244 CommandCost
CmdSetTimetableStart(DoCommandFlag flags
, VehicleID veh_id
, bool timetable_all
, Date start_date
)
246 Vehicle
*v
= Vehicle::GetIfValid(veh_id
);
247 if (v
== nullptr || !v
->IsPrimaryVehicle() || v
->orders
== nullptr) return CMD_ERROR
;
249 CommandCost ret
= CheckOwnership(v
->owner
);
250 if (ret
.Failed()) return ret
;
252 int total_duration
= v
->orders
->GetTimetableTotalDuration();
254 /* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
255 if (start_date
< 0 || start_date
> MAX_DAY
) return CMD_ERROR
;
256 if (start_date
- _date
> 15 * DAYS_IN_LEAP_YEAR
) return CMD_ERROR
;
257 if (_date
- start_date
> DAYS_IN_LEAP_YEAR
) return CMD_ERROR
;
258 if (timetable_all
&& !v
->orders
->IsCompleteTimetable()) return CMD_ERROR
;
259 if (timetable_all
&& start_date
+ total_duration
/ DAY_TICKS
> MAX_DAY
) return CMD_ERROR
;
261 if (flags
& DC_EXEC
) {
262 std::vector
<Vehicle
*> vehs
;
265 for (Vehicle
*w
= v
->orders
->GetFirstSharedVehicle(); w
!= nullptr; w
= w
->NextShared()) {
272 int num_vehs
= (uint
)vehs
.size();
275 std::sort(vehs
.begin(), vehs
.end(), &VehicleTimetableSorter
);
280 for (Vehicle
*w
: vehs
) {
282 w
->lateness_counter
= 0;
283 ClrBit(w
->vehicle_flags
, VF_TIMETABLE_STARTED
);
284 /* Do multiplication, then division to reduce rounding errors. */
285 w
->timetable_start
= start_date
+ idx
* total_duration
/ num_vehs
/ DAY_TICKS
;
286 SetWindowDirty(WC_VEHICLE_TIMETABLE
, w
->index
);
292 return CommandCost();
297 * Start or stop filling the timetable automatically from the time the vehicle
298 * actually takes to complete it. When starting to autofill the current times
299 * are cleared and the timetable will start again from scratch.
300 * @param flags Operation to perform.
301 * @param veh Vehicle index.
302 * @param autofill Enable or disable autofill
303 * @param preserve_wait_time Set to preserve waiting times in non-destructive mode
304 * @return the cost of this operation or an error
306 CommandCost
CmdAutofillTimetable(DoCommandFlag flags
, VehicleID veh
, bool autofill
, bool preserve_wait_time
)
308 Vehicle
*v
= Vehicle::GetIfValid(veh
);
309 if (v
== nullptr || !v
->IsPrimaryVehicle() || v
->orders
== nullptr) return CMD_ERROR
;
311 CommandCost ret
= CheckOwnership(v
->owner
);
312 if (ret
.Failed()) return ret
;
314 if (flags
& DC_EXEC
) {
316 /* Start autofilling the timetable, which clears the
317 * "timetable has started" bit. Times are not cleared anymore, but are
318 * overwritten when the order is reached now. */
319 SetBit(v
->vehicle_flags
, VF_AUTOFILL_TIMETABLE
);
320 ClrBit(v
->vehicle_flags
, VF_TIMETABLE_STARTED
);
322 /* Overwrite waiting times only if they got longer */
323 if (preserve_wait_time
) SetBit(v
->vehicle_flags
, VF_AUTOFILL_PRES_WAIT_TIME
);
325 v
->timetable_start
= 0;
326 v
->lateness_counter
= 0;
328 ClrBit(v
->vehicle_flags
, VF_AUTOFILL_TIMETABLE
);
329 ClrBit(v
->vehicle_flags
, VF_AUTOFILL_PRES_WAIT_TIME
);
332 for (Vehicle
*v2
= v
->FirstShared(); v2
!= nullptr; v2
= v2
->NextShared()) {
334 /* Stop autofilling; only one vehicle at a time can perform autofill */
335 ClrBit(v2
->vehicle_flags
, VF_AUTOFILL_TIMETABLE
);
336 ClrBit(v2
->vehicle_flags
, VF_AUTOFILL_PRES_WAIT_TIME
);
338 SetWindowDirty(WC_VEHICLE_TIMETABLE
, v2
->index
);
342 return CommandCost();
346 * Update the timetable for the vehicle.
347 * @param v The vehicle to update the timetable for.
348 * @param travelling Whether we just travelled or waited at a station.
350 void UpdateVehicleTimetable(Vehicle
*v
, bool travelling
)
352 uint time_taken
= v
->current_order_time
;
354 v
->current_order_time
= 0;
356 if (v
->current_order
.IsType(OT_IMPLICIT
)) return; // no timetabling of auto orders
358 if (v
->cur_real_order_index
>= v
->GetNumOrders()) return;
359 Order
*real_current_order
= v
->GetOrder(v
->cur_real_order_index
);
361 VehicleOrderID first_manual_order
= 0;
362 for (Order
*o
= v
->GetFirstOrder(); o
!= nullptr && o
->IsType(OT_IMPLICIT
); o
= o
->next
) {
363 ++first_manual_order
;
366 bool just_started
= false;
368 /* This vehicle is arriving at the first destination in the timetable. */
369 if (v
->cur_real_order_index
== first_manual_order
&& travelling
) {
370 /* If the start date hasn't been set, or it was set automatically when
371 * the vehicle last arrived at the first destination, update it to the
372 * current time. Otherwise set the late counter appropriately to when
373 * the vehicle should have arrived. */
374 just_started
= !HasBit(v
->vehicle_flags
, VF_TIMETABLE_STARTED
);
376 if (v
->timetable_start
!= 0) {
377 v
->lateness_counter
= (_date
- v
->timetable_start
) * DAY_TICKS
+ _date_fract
;
378 v
->timetable_start
= 0;
381 SetBit(v
->vehicle_flags
, VF_TIMETABLE_STARTED
);
382 SetWindowDirty(WC_VEHICLE_TIMETABLE
, v
->index
);
385 if (!HasBit(v
->vehicle_flags
, VF_TIMETABLE_STARTED
)) return;
387 bool autofilling
= HasBit(v
->vehicle_flags
, VF_AUTOFILL_TIMETABLE
);
388 bool remeasure_wait_time
= !real_current_order
->IsWaitTimetabled() ||
389 (autofilling
&& !HasBit(v
->vehicle_flags
, VF_AUTOFILL_PRES_WAIT_TIME
));
391 if (travelling
&& remeasure_wait_time
) {
392 /* We just finished travelling and want to remeasure the loading time,
393 * so do not apply any restrictions for the loading to finish. */
394 v
->current_order
.SetWaitTime(0);
397 if (just_started
) return;
399 /* Before modifying waiting times, check whether we want to preserve bigger ones. */
400 if (!real_current_order
->IsType(OT_CONDITIONAL
) &&
401 (travelling
|| time_taken
> real_current_order
->GetWaitTime() || remeasure_wait_time
)) {
402 /* Round the time taken up to the nearest day, as this will avoid
403 * confusion for people who are timetabling in days, and can be
404 * adjusted later by people who aren't.
405 * For trains/aircraft multiple movement cycles are done in one
406 * tick. This makes it possible to leave the station and process
407 * e.g. a depot order in the same tick, causing it to not fill
408 * the timetable entry like is done for road vehicles/ships.
409 * Thus always make sure at least one tick is used between the
410 * processing of different orders when filling the timetable. */
411 uint time_to_set
= CeilDiv(std::max(time_taken
, 1U), DAY_TICKS
) * DAY_TICKS
;
413 if (travelling
&& (autofilling
|| !real_current_order
->IsTravelTimetabled())) {
414 ChangeTimetable(v
, v
->cur_real_order_index
, time_to_set
, MTF_TRAVEL_TIME
, autofilling
);
415 } else if (!travelling
&& (autofilling
|| !real_current_order
->IsWaitTimetabled())) {
416 ChangeTimetable(v
, v
->cur_real_order_index
, time_to_set
, MTF_WAIT_TIME
, autofilling
);
420 if (v
->cur_real_order_index
== first_manual_order
&& travelling
) {
421 /* If we just started we would have returned earlier and have not reached
422 * this code. So obviously, we have completed our round: So turn autofill
424 ClrBit(v
->vehicle_flags
, VF_AUTOFILL_TIMETABLE
);
425 ClrBit(v
->vehicle_flags
, VF_AUTOFILL_PRES_WAIT_TIME
);
428 if (autofilling
) return;
430 uint timetabled
= travelling
? real_current_order
->GetTimetabledTravel() :
431 real_current_order
->GetTimetabledWait();
433 /* Vehicles will wait at stations if they arrive early even if they are not
434 * timetabled to wait there, so make sure the lateness counter is updated
435 * when this happens. */
436 if (timetabled
== 0 && (travelling
|| v
->lateness_counter
>= 0)) return;
438 v
->lateness_counter
-= (timetabled
- time_taken
);
440 /* When we are more late than this timetabled bit takes we (somewhat expensively)
441 * check how many ticks the (fully filled) timetable has. If a timetable cycle is
442 * shorter than the amount of ticks we are late we reduce the lateness by the
443 * length of a full cycle till lateness is less than the length of a timetable
444 * cycle. When the timetable isn't fully filled the cycle will be INVALID_TICKS. */
445 if (v
->lateness_counter
> (int)timetabled
) {
446 Ticks cycle
= v
->orders
->GetTimetableTotalDuration();
447 if (cycle
!= INVALID_TICKS
&& v
->lateness_counter
> cycle
) {
448 v
->lateness_counter
%= cycle
;
452 for (v
= v
->FirstShared(); v
!= nullptr; v
= v
->NextShared()) {
453 SetWindowDirty(WC_VEHICLE_TIMETABLE
, v
->index
);