Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / timetable_cmd.cpp
bloba6cb8fc6bac0b65096f5c141bac28cd39db46393
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file timetable_cmd.cpp Commands related to time tabling. */
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "company_func.h"
15 #include "date_func.h"
16 #include "window_func.h"
17 #include "vehicle_base.h"
18 #include "cmd_helper.h"
19 #include "settings_type.h"
20 #include "core/sort_func.hpp"
22 #include "table/strings.h"
24 #include "safeguards.h"
26 /**
27 * Change/update a particular timetable entry.
28 * @param v The vehicle to change the timetable of.
29 * @param order_number The index of the timetable in the order list.
30 * @param val The new data of the timetable entry.
31 * @param mtf Which part of the timetable entry to change.
32 * @param timetabled If the new value is explicitly timetabled.
34 static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf, bool timetabled)
36 Order *order = v->GetOrder(order_number);
37 int total_delta = 0;
38 int timetable_delta = 0;
40 switch (mtf) {
41 case MTF_WAIT_TIME:
42 total_delta = val - order->GetWaitTime();
43 timetable_delta = (timetabled ? val : 0) - order->GetTimetabledWait();
44 order->SetWaitTime(val);
45 order->SetWaitTimetabled(timetabled);
46 break;
48 case MTF_TRAVEL_TIME:
49 total_delta = val - order->GetTravelTime();
50 timetable_delta = (timetabled ? val : 0) - order->GetTimetabledTravel();
51 order->SetTravelTime(val);
52 order->SetTravelTimetabled(timetabled);
53 break;
55 case MTF_TRAVEL_SPEED:
56 order->SetMaxSpeed(val);
57 break;
59 default:
60 NOT_REACHED();
62 v->UpdateTotalDuration(total_delta);
63 v->UpdateTimetableDuration(timetable_delta);
65 for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
66 if (v->cur_real_order_index == order_number && v->current_order.Equals(*order)) {
67 switch (mtf) {
68 case MTF_WAIT_TIME:
69 v->current_order.SetWaitTime(val);
70 v->current_order.SetWaitTimetabled(timetabled);
71 break;
73 case MTF_TRAVEL_TIME:
74 v->current_order.SetTravelTime(val);
75 v->current_order.SetTravelTimetabled(timetabled);
76 break;
78 case MTF_TRAVEL_SPEED:
79 v->current_order.SetMaxSpeed(val);
80 break;
82 default:
83 NOT_REACHED();
86 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
90 /**
91 * Change timetable data of an order.
92 * @param tile Not used.
93 * @param flags Operation to perform.
94 * @param p1 Various bitstuffed elements
95 * - p1 = (bit 0-19) - Vehicle with the orders to change.
96 * - p1 = (bit 20-27) - Order index to modify.
97 * - p1 = (bit 28-29) - Timetable data to change (@see ModifyTimetableFlags)
98 * @param p2 The amount of time to wait.
99 * - p2 = (bit 0-15) - The data to modify as specified by p1 bits 28-29.
100 * 0 to clear times, UINT16_MAX to clear speed limit.
101 * @param text unused
102 * @return the cost of this operation or an error
104 CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
106 VehicleID veh = GB(p1, 0, 20);
108 Vehicle *v = Vehicle::GetIfValid(veh);
109 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
111 CommandCost ret = CheckOwnership(v->owner);
112 if (ret.Failed()) return ret;
114 VehicleOrderID order_number = GB(p1, 20, 8);
115 Order *order = v->GetOrder(order_number);
116 if (order == NULL || order->IsType(OT_IMPLICIT)) return CMD_ERROR;
118 ModifyTimetableFlags mtf = Extract<ModifyTimetableFlags, 28, 2>(p1);
119 if (mtf >= MTF_END) return CMD_ERROR;
121 int wait_time = order->GetWaitTime();
122 int travel_time = order->GetTravelTime();
123 int max_speed = order->GetMaxSpeed();
124 switch (mtf) {
125 case MTF_WAIT_TIME:
126 wait_time = GB(p2, 0, 16);
127 break;
129 case MTF_TRAVEL_TIME:
130 travel_time = GB(p2, 0, 16);
131 break;
133 case MTF_TRAVEL_SPEED:
134 max_speed = GB(p2, 0, 16);
135 if (max_speed == 0) max_speed = UINT16_MAX; // Disable speed limit.
136 break;
138 default:
139 NOT_REACHED();
142 if (wait_time != order->GetWaitTime()) {
143 switch (order->GetType()) {
144 case OT_GOTO_STATION:
145 if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
146 break;
148 case OT_GOTO_DEPOT:
149 break;
151 case OT_CONDITIONAL:
152 break;
154 default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
158 if (travel_time != order->GetTravelTime() && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
159 if (max_speed != order->GetMaxSpeed() && (order->IsType(OT_CONDITIONAL) || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
161 if (flags & DC_EXEC) {
162 switch (mtf) {
163 case MTF_WAIT_TIME:
164 /* Set time if changing the value or confirming an estimated time as timetabled. */
165 if (wait_time != order->GetWaitTime() || (wait_time > 0 && !order->IsWaitTimetabled())) {
166 ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME, wait_time > 0);
168 break;
170 case MTF_TRAVEL_TIME:
171 /* Set time if changing the value or confirming an estimated time as timetabled. */
172 if (travel_time != order->GetTravelTime() || (travel_time > 0 && !order->IsTravelTimetabled())) {
173 ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME, travel_time > 0);
175 break;
177 case MTF_TRAVEL_SPEED:
178 if (max_speed != order->GetMaxSpeed()) {
179 ChangeTimetable(v, order_number, max_speed, MTF_TRAVEL_SPEED, max_speed != UINT16_MAX);
181 break;
183 default:
184 break;
188 return CommandCost();
192 * Change timetable data of all orders of a vehicle.
193 * @param tile Not used.
194 * @param flags Operation to perform.
195 * @param p1 Various bitstuffed elements
196 * - p1 = (bit 0-19) - Vehicle with the orders to change.
197 * - p1 = (bit 20-27) - unused
198 * - p1 = (bit 28-29) - Timetable data to change (@see ModifyTimetableFlags)
199 * - p1 = (bit 30) - 0 to set timetable wait/travel time, 1 to clear it
200 * @param p2 The amount of time to wait.
201 * - p2 = (bit 0-15) - The data to modify as specified by p1 bits 28-29.
202 * 0 to clear times, UINT16_MAX to clear speed limit.
203 * @param text unused
204 * @return the cost of this operation or an error
206 CommandCost CmdBulkChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
208 VehicleID veh = GB(p1, 0, 20);
210 Vehicle *v = Vehicle::GetIfValid(veh);
211 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
213 CommandCost ret = CheckOwnership(v->owner);
214 if (ret.Failed()) return ret;
216 ModifyTimetableFlags mtf = Extract<ModifyTimetableFlags, 28, 2>(p1);
217 if (mtf >= MTF_END) return CMD_ERROR;
219 if (v->GetNumOrders() == 0) return CMD_ERROR;
221 if (flags & DC_EXEC) {
222 for (VehicleOrderID order_number = 0; order_number < v->GetNumOrders(); order_number++) {
223 Order *order = v->GetOrder(order_number);
224 if (order == NULL || order->IsType(OT_IMPLICIT)) continue;
226 uint32 new_p1 = p1;
227 SB(new_p1, 20, 8, order_number);
228 DoCommand(tile, new_p1, p2, flags, CMD_CHANGE_TIMETABLE);
232 return CommandCost();
236 * Clear the lateness counter to make the vehicle on time.
237 * @param tile Not used.
238 * @param flags Operation to perform.
239 * @param p1 Various bitstuffed elements
240 * - p1 = (bit 0-19) - Vehicle with the orders to change.
241 * @param p2 unused
242 * @param text unused
243 * @return the cost of this operation or an error
245 CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
247 VehicleID veh = GB(p1, 0, 20);
249 Vehicle *v = Vehicle::GetIfValid(veh);
250 if (v == NULL || !v->IsPrimaryVehicle() || !v->HasOrdersList()) return CMD_ERROR;
252 CommandCost ret = CheckOwnership(v->owner);
253 if (ret.Failed()) return ret;
255 if (flags & DC_EXEC) {
256 v->lateness_counter = 0;
257 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
260 return CommandCost();
264 * Order vehicles based on their timetable. The vehicles will be sorted in order
265 * they would reach the first station.
267 * @param ap First Vehicle pointer.
268 * @param bp Second Vehicle pointer.
269 * @return Comparison value.
271 static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp)
273 const Vehicle *a = *ap;
274 const Vehicle *b = *bp;
276 VehicleOrderID a_order = a->cur_real_order_index;
277 VehicleOrderID b_order = b->cur_real_order_index;
278 int j = (int)b_order - (int)a_order;
280 /* Are we currently at an ordered station (un)loading? */
281 bool a_load = a->current_order.IsType(OT_LOADING) && a->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
282 bool b_load = b->current_order.IsType(OT_LOADING) && b->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
284 /* If the current order is not loading at the ordered station, decrease the order index by one since we have
285 * not yet arrived at the station (and thus the timetable entry; still in the travelling of the previous one).
286 * Since the ?_order variables are unsigned the -1 will flow under and place the vehicles going to order #0 at
287 * the begin of the list with vehicles arriving at #0. */
288 if (!a_load) a_order--;
289 if (!b_load) b_order--;
291 /* First check the order index that accounted for loading, then just the raw one. */
292 int i = (int)b_order - (int)a_order;
293 if (i != 0) return i;
294 if (j != 0) return j;
296 /* Look at the time we spent in this order; the higher, the closer to its destination. */
297 i = b->current_order_time - a->current_order_time;
298 if (i != 0) return i;
300 /* If all else is equal, use some unique index to sort it the same way. */
301 return b->unitnumber - a->unitnumber;
305 * Set the start date of the timetable.
306 * @param tile Not used.
307 * @param flags Operation to perform.
308 * @param p2 Various bitstuffed elements
309 * - p2 = (bit 0-19) - Vehicle ID.
310 * - p2 = (bit 20) - Set to 1 to set timetable start for all vehicles sharing this order
311 * @param p2 The timetable start date.
312 * @param text Not used.
313 * @return The error or cost of the operation.
315 CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
317 bool timetable_all = HasBit(p1, 20);
318 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
319 if (v == NULL || !v->IsPrimaryVehicle() || !v->HasOrdersList()) return CMD_ERROR;
321 CommandCost ret = CheckOwnership(v->owner);
322 if (ret.Failed()) return ret;
324 /* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
325 Date start_date = (Date)p2;
326 if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR;
327 if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
328 if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR;
329 if (timetable_all && !v->HasCompleteTimetable()) return CMD_ERROR;
331 if (flags & DC_EXEC) {
332 SmallVector<Vehicle *, 8> vehs;
334 if (timetable_all) {
335 for (Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
336 *vehs.Append() = w;
338 } else {
339 *vehs.Append() = v;
342 int total_duration = v->GetTimetableTotalDuration();
343 int num_vehs = vehs.Length();
345 if (num_vehs >= 2) {
346 QSortT(vehs.Begin(), vehs.Length(), &VehicleTimetableSorter);
349 int base = vehs.FindIndex(v);
351 for (Vehicle **viter = vehs.Begin(); viter != vehs.End(); viter++) {
352 int idx = (viter - vehs.Begin()) - base;
353 Vehicle *w = *viter;
355 w->lateness_counter = 0;
356 ClrBit(w->vehicle_flags, VF_TIMETABLE_STARTED);
357 /* Do multiplication, then division to reduce rounding errors. */
358 w->timetable_start = start_date + idx * total_duration / num_vehs / DAY_TICKS;
359 SetWindowDirty(WC_VEHICLE_TIMETABLE, w->index);
364 return CommandCost();
368 * Start or stop automatic management of timetables.
369 * @param tile Not used.
370 * @param flags Operation to perform.
371 * @param p1 Vehicle index.
372 * @param p2 Various bitstuffed elements
373 * - p2 = (bit 0) - Set to 1 to enable, 0 to disable automation.
374 * - p2 = (bit 1) - Ctrl was pressed. Used to keep wait times.
375 * @param text unused
376 * @return the cost of this operation or an error
378 CommandCost CmdAutomateTimetable(TileIndex index, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
380 VehicleID veh = GB(p1, 0, 16);
382 Vehicle *v = Vehicle::GetIfValid(veh);
383 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
385 CommandCost ret = CheckOwnership(v->owner);
386 if (ret.Failed()) return ret;
388 if (flags & DC_EXEC) {
389 for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
390 if (HasBit(p2, 0)) {
391 /* Automated timetable. */
392 SetBit(v2->vehicle_flags, VF_AUTOMATE_TIMETABLE);
394 if (HasBit(p2, 1)) {
395 SetBit(v2->vehicle_flags, VF_AUTOMATE_PRES_WAIT_TIME);
398 ClrBit(v2->vehicle_flags, VF_TIMETABLE_STARTED);
399 v2->timetable_start = 0;
400 v2->lateness_counter = 0;
401 v2->current_loading_time = 0;
403 else {
404 /* De-automate timetable. */
405 ClrBit(v2->vehicle_flags, VF_AUTOMATE_TIMETABLE);
406 ClrBit(v2->vehicle_flags, VF_AUTOMATE_PRES_WAIT_TIME);
408 SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
412 return CommandCost();
416 * Confirm all estimated wait and travel times as timetabled.
417 * @param tile Not used.
418 * @param flags Operation to perform.
419 * @param p1 Vehicle index.
420 * @param p2 Unused
421 * @param text unused
422 * @return the cost of this operation or an error
424 CommandCost CmdConfirmAll(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
426 VehicleID veh = GB(p1, 0, 20);
428 Vehicle *v = Vehicle::GetIfValid(veh);
429 if (v == nullptr || !v->IsPrimaryVehicle() || !v->HasOrdersList()) return CMD_ERROR;
431 CommandCost ret = CheckOwnership(v->owner);
432 if (ret.Failed()) return ret;
434 if (flags & DC_EXEC) {
435 int num_orders = v->GetNumOrders();
436 int timetable_delta = 0;
438 for (int i = 0; i < num_orders; ++i) {
439 Order* order = v->GetOrderAt(i);
441 assert(order != nullptr);
443 if (!order->IsType(OT_IMPLICIT)) {
444 if (order->GetWaitTime() != 0 && !order->IsWaitTimetabled()) {
445 timetable_delta += order->GetWaitTime() - order->GetTimetabledWait();
446 order->SetWaitTimetabled(true);
449 if (order->GetTravelTime() != 0 && !order->IsTravelTimetabled()) {
450 timetable_delta += order->GetTravelTime() - order->GetTimetabledTravel();
451 order->SetTravelTimetabled(true);
456 v->UpdateTimetableDuration(timetable_delta);
458 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
461 return CommandCost();
465 * Set new separation parameters
466 * @param tile Not used.
467 * @param flags Operation to perform.
468 * @param p1 Vehicle id.
469 * @param p2
470 * - p2 = (bit 0-2) - Separation mode (@see TTSepMode)
471 * - p2 = (bit 3-31) - Separation parameter (Unused if #TTS_MODE_OFF | #TTS_MODE_AUTO,
472 * Number of vehicles if #TTS_MODE_MAN_N, separation delay in ticks if #TTS_MODE_MAN_T).
473 * @param text Not used.
474 * @return The error or cost of the operation.
476 CommandCost CmdReinitSeparation(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
478 Vehicle *v = Vehicle::GetIfValid(p1);
479 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
481 CommandCost ret = CheckOwnership(v->owner);
482 if (ret.Failed()) return ret;
484 if (flags & DC_EXEC) {
485 v->SetSepSettings((TTSepMode)GB(p2, 0, 3), GB(p2, 3, 29));
488 return CommandCost();
492 * Update the timetable for the vehicle.
493 * @param v The vehicle to update the timetable for.
494 * @param traveling Whether we just traveled or waited at a station.
496 void UpdateVehicleTimetable(Vehicle *v, bool travelling)
498 if (!travelling) v->current_loading_time++; // +1 because this time is one tick behind
499 uint time_taken = v->current_order_time;
500 uint time_loading = v->current_loading_time;
502 // We are on our way so vehicle separation has finished.
503 if (travelling) ClrBit(v->vehicle_flags, VF_SEPARATION_IN_PROGRESS);
505 v->current_order_time = 0;
506 v->current_loading_time = 0;
508 if (v->current_order.IsType(OT_IMPLICIT)) return; // no timetabling of auto orders
510 if (v->cur_real_order_index >= v->GetNumOrders()) return;
511 Order *real_current_order = v->GetOrder(v->cur_real_order_index);
513 VehicleOrderID first_manual_order = 0;
514 for (Order *o = v->GetFirstOrder(); o != NULL && o->IsType(OT_IMPLICIT); o = o->next) {
515 ++first_manual_order;
518 bool just_started = false;
520 /* Start automated timetables at first opportunity */
521 if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED) && HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) {
522 SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
524 v->lateness_counter = 0;
526 for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
527 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
529 return;
532 /* This vehicle is arriving at the first destination in the timetable. */
533 if (v->cur_real_order_index == first_manual_order && travelling) {
534 v->trip_history.NewRound();
535 /* If the start date hasn't been set, or it was set automatically when
536 * the vehicle last arrived at the first destination, update it to the
537 * current time. Otherwise set the late counter appropriately to when
538 * the vehicle should have arrived. */
539 just_started = !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
541 if (v->timetable_start != 0) {
542 v->lateness_counter = (_date - v->timetable_start) * DAY_TICKS + _date_fract;
543 v->timetable_start = 0;
546 SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
547 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
550 if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
552 bool remeasure_wait_time = !real_current_order->IsWaitTimetabled() ||
553 (HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE) && !HasBit(v->vehicle_flags, VF_AUTOMATE_PRES_WAIT_TIME));
555 if (travelling && remeasure_wait_time) {
556 /* We just finished traveling and want to remeasure the loading time,
557 * so do not apply any restrictions for the loading to finish. */
558 v->current_order.SetWaitTime(0);
561 if (just_started) return;
563 /* Before modifying waiting times, check whether we want to preserve bigger ones. */
564 if (!real_current_order->IsType(OT_CONDITIONAL) &&
565 (travelling || time_loading > real_current_order->GetWaitTime() || remeasure_wait_time)) {
566 /* For trains/aircraft multiple movement cycles are done in one
567 * tick. This makes it possible to leave the station and process
568 * e.g. a depot order in the same tick, causing it to not fill
569 * the timetable entry like is done for road vehicles/ships.
570 * Thus always make sure at least one tick is used between the
571 * processing of different orders when filling the timetable. */
572 if (travelling && !real_current_order->IsTravelTimetabled()) {
573 ChangeTimetable(v, v->cur_real_order_index, max(time_taken, 1U), MTF_TRAVEL_TIME, false);
574 } else if (!travelling && !real_current_order->IsWaitTimetabled()) {
575 ChangeTimetable(v, v->cur_real_order_index, max(time_loading, 1U), MTF_WAIT_TIME, false);
579 uint timetabled = travelling ? real_current_order->GetTimetabledTravel() :
580 real_current_order->GetTimetabledWait();
582 /* Update the timetable to gradually shift order times towards the actual travel times. */
583 if (timetabled != 0 && HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE) && (travelling || !HasBit(v->vehicle_flags, VF_AUTOMATE_PRES_WAIT_TIME))) {
584 int32 new_time;
585 if (travelling) {
586 new_time = time_taken;
588 else {
589 new_time = time_loading;
592 if (new_time > (int32)timetabled * 4 && travelling) {
593 /* Possible jam, clear time and restart timetable for all vehicles.
594 * Otherwise we risk trains blocking 1-lane stations for long times. */
595 ChangeTimetable(v, v->cur_real_order_index, 0, (travelling ? MTF_TRAVEL_TIME : MTF_WAIT_TIME), true);
596 for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
597 ClrBit(v2->vehicle_flags, VF_TIMETABLE_STARTED);
598 SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
600 return;
602 else if (new_time >= (int32)timetabled / 2) {
603 /* Compute running average, with sign conversion to avoid negative overflow. */
604 if (new_time < (int32)timetabled) {
605 new_time = ((int32)timetabled * 3 + new_time * 2 + 2) / 5;
607 else {
608 new_time = ((int32)timetabled * 9 + new_time + 5) / 10;
611 else {
612 /* new time is less than half old time, set value directly */
615 if (new_time < 1) new_time = 1;
616 if (new_time != (int32)timetabled) {
617 ChangeTimetable(v, v->cur_real_order_index, new_time, travelling ? MTF_TRAVEL_TIME : MTF_WAIT_TIME, true);
620 else if (timetabled == 0 && HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) {
621 /* Add times for orders that are not yet timetabled, even while not autofilling */
622 const int32 new_time = travelling ? time_taken : time_loading;
624 ChangeTimetable(v, v->cur_real_order_index, new_time, travelling ? MTF_TRAVEL_TIME : MTF_WAIT_TIME, true);
627 /* Vehicles will wait at stations if they arrive early even if they are not
628 * timetabled to wait there, so make sure the lateness counter is updated
629 * when this happens. */
630 if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
632 v->lateness_counter -= (timetabled - time_taken);
634 /* When we are more late than this timetabled bit takes we (somewhat expensively)
635 * check how many ticks the (fully filled) timetable has. If a timetable cycle is
636 * shorter than the amount of ticks we are late we reduce the lateness by the
637 * length of a full cycle till lateness is less than the length of a timetable
638 * cycle. When the timetable isn't fully filled the cycle will be INVALID_TICKS. */
639 if (v->lateness_counter > (int)timetabled) {
640 Ticks cycle = v->GetTimetableTotalDuration();
641 if (cycle != INVALID_TICKS && v->lateness_counter > cycle) {
642 v->lateness_counter %= cycle;
646 for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
647 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);