Files @ r28293:13ab39ecefa6
Branch filter:

Location: cpp/openttd-patchpack/source/src/timetable_cmd.cpp - annotation

Loïc Guilloux
Fix: [Script] Properly store the previous AsyncMode state (#11587)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r8116:df67d3c5e4fd
r17263:a75c87f7e037
r27883:ddbd33508a8a
r27148:4e041ae27b9d
r8131:7a50db7be0ff
r8144:1432edd15267
r26094:7572e88decb3
r27125:3b26bfcc07bb
r6981:8e1c715f9e05
r8264:d493cb51fe8a
r6981:8e1c715f9e05
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r17624:f2c5f47dceaa
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r17624:f2c5f47dceaa
r17624:f2c5f47dceaa
r17624:f2c5f47dceaa
r19078:1939682c5ebf
r19078:1939682c5ebf
r21441:3cecc5d58a3f
r17624:f2c5f47dceaa
r27737:728d55b97775
r7066:7459a71fc283
r11986:6596046384a5
r26753:ca3f298cd85a
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r19078:1939682c5ebf
r19078:1939682c5ebf
r19078:1939682c5ebf
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21440:ea95dda89940
r21441:3cecc5d58a3f
r19078:1939682c5ebf
r7066:7459a71fc283
r19078:1939682c5ebf
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21440:ea95dda89940
r21441:3cecc5d58a3f
r19078:1939682c5ebf
r19078:1939682c5ebf
r19079:a1a4921bfd39
r21440:ea95dda89940
r19079:a1a4921bfd39
r19079:a1a4921bfd39
r19078:1939682c5ebf
r19078:1939682c5ebf
r7066:7459a71fc283
r26178:733038a553ce
r26178:733038a553ce
r7066:7459a71fc283
r23607:36c15679007d
r17184:a6dd7165a9b8
r19078:1939682c5ebf
r19078:1939682c5ebf
r21440:ea95dda89940
r21441:3cecc5d58a3f
r19078:1939682c5ebf
r19078:1939682c5ebf
r19078:1939682c5ebf
r21440:ea95dda89940
r21441:3cecc5d58a3f
r19078:1939682c5ebf
r19078:1939682c5ebf
r19079:a1a4921bfd39
r21440:ea95dda89940
r19079:a1a4921bfd39
r19079:a1a4921bfd39
r19078:1939682c5ebf
r19078:1939682c5ebf
r10033:424b14d0d013
r7066:7459a71fc283
r13024:48c81d0b078a
r7809:a8c0806e350f
r7066:7459a71fc283
r7066:7459a71fc283
r6981:8e1c715f9e05
r19078:1939682c5ebf
r26095:00c14d52e378
r26110:91f9ab113048
r26110:91f9ab113048
r26110:91f9ab113048
r26110:91f9ab113048
r26110:91f9ab113048
r13057:58af81fcdcf8
r6981:8e1c715f9e05
r27737:728d55b97775
r6981:8e1c715f9e05
r11944:3b8f9f948e8d
r23607:36c15679007d
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r6981:8e1c715f9e05
r11986:6596046384a5
r23607:36c15679007d
r6981:8e1c715f9e05
r19078:1939682c5ebf
r9665:f312055347cc
r21444:d2dfe2fa0fff
r21444:d2dfe2fa0fff
r21440:ea95dda89940
r19078:1939682c5ebf
r19078:1939682c5ebf
r26110:91f9ab113048
r19078:1939682c5ebf
r19078:1939682c5ebf
r19078:1939682c5ebf
r26110:91f9ab113048
r19078:1939682c5ebf
r19078:1939682c5ebf
r19079:a1a4921bfd39
r26110:91f9ab113048
r19165:1353a268dd5f
r19079:a1a4921bfd39
r19079:a1a4921bfd39
r19078:1939682c5ebf
r19078:1939682c5ebf
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r21444:d2dfe2fa0fff
r9665:f312055347cc
r9665:f312055347cc
r12622:202e83a6cee7
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r12622:202e83a6cee7
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r21444:d2dfe2fa0fff
r21440:ea95dda89940
r9665:f312055347cc
r6981:8e1c715f9e05
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r22128:eabf10d5eb01
r21444:d2dfe2fa0fff
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r27737:728d55b97775
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r26688:8dd6241f354e
r6981:8e1c715f9e05
r26095:00c14d52e378
r26110:91f9ab113048
r26871:14e8578f092a
r13057:58af81fcdcf8
r6981:8e1c715f9e05
r26871:14e8578f092a
r6981:8e1c715f9e05
r11944:3b8f9f948e8d
r26178:733038a553ce
r14805:8c416234ced2
r28064:25d3484657a5
r28064:25d3484657a5
r28064:25d3484657a5
r28064:25d3484657a5
r14805:8c416234ced2
r14805:8c416234ced2
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r26871:14e8578f092a
r28278:1fc682037ca2
r26871:14e8578f092a
r28064:25d3484657a5
r28064:25d3484657a5
r28064:25d3484657a5
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r28064:25d3484657a5
r28064:25d3484657a5
r28064:25d3484657a5
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r26871:14e8578f092a
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r7066:7459a71fc283
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r23616:e1fb1b190a7f
r23616:e1fb1b190a7f
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r23616:e1fb1b190a7f
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r23616:e1fb1b190a7f
r23616:e1fb1b190a7f
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r23616:e1fb1b190a7f
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r23616:e1fb1b190a7f
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r13757:cb9ca7946c57
r26095:00c14d52e378
r26110:91f9ab113048
r26110:91f9ab113048
r28176:bf82dbbfec4e
r15858:d75fb1dcda49
r13757:cb9ca7946c57
r28176:bf82dbbfec4e
r13757:cb9ca7946c57
r26110:91f9ab113048
r26178:733038a553ce
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r13757:cb9ca7946c57
r28135:e7f3044a8685
r26320:5d6742b81c27
r28176:bf82dbbfec4e
r28176:bf82dbbfec4e
r28135:e7f3044a8685
r27884:803962be0328
r28135:e7f3044a8685
r28135:e7f3044a8685
r28034:a74ad494b973
r28135:e7f3044a8685
r27884:803962be0328
r28135:e7f3044a8685
r28135:e7f3044a8685
r27129:34481b1f4b53
r28135:e7f3044a8685
r28135:e7f3044a8685
r28135:e7f3044a8685
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r23538:8df50944b27a
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r26178:733038a553ce
r23532:dc91fcd293f5
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r23532:dc91fcd293f5
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r23553:9f01781fc43d
r13757:cb9ca7946c57
r20364:4c24a05d03f8
r23616:e1fb1b190a7f
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r26320:5d6742b81c27
r20364:4c24a05d03f8
r23536:ce42deb0b32d
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r28176:bf82dbbfec4e
r20364:4c24a05d03f8
r23534:ced2648be38e
r20364:4c24a05d03f8
r20364:4c24a05d03f8
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r7066:7459a71fc283
r7066:7459a71fc283
r7066:7459a71fc283
r26095:00c14d52e378
r26110:91f9ab113048
r26110:91f9ab113048
r26110:91f9ab113048
r13057:58af81fcdcf8
r7066:7459a71fc283
r26110:91f9ab113048
r7066:7459a71fc283
r11944:3b8f9f948e8d
r26178:733038a553ce
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r7066:7459a71fc283
r7066:7459a71fc283
r26110:91f9ab113048
r10677:1a6f3c7876cf
r10677:1a6f3c7876cf
r10677:1a6f3c7876cf
r7931:4c17a74c399e
r7929:67846e70abf8
r7066:7459a71fc283
r10677:1a6f3c7876cf
r26110:91f9ab113048
r7066:7459a71fc283
r13757:cb9ca7946c57
r10341:c21fa463ba20
r7066:7459a71fc283
r7929:67846e70abf8
r10341:c21fa463ba20
r7066:7459a71fc283
r7066:7459a71fc283
r23607:36c15679007d
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r11042:9a00dc9e0e93
r7809:a8c0806e350f
r7809:a8c0806e350f
r7066:7459a71fc283
r7066:7459a71fc283
r6981:8e1c715f9e05
r17624:f2c5f47dceaa
r17624:f2c5f47dceaa
r17624:f2c5f47dceaa
r17624:f2c5f47dceaa
r17624:f2c5f47dceaa
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r28278:1fc682037ca2
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r17683:ed148d83dec5
r16897:d3b3d1abd708
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r26753:ca3f298cd85a
r22129:8e5c5b5cd408
r16897:d3b3d1abd708
r23607:36c15679007d
r16897:d3b3d1abd708
r16897:d3b3d1abd708
r7066:7459a71fc283
r10341:c21fa463ba20
r10341:c21fa463ba20
r13757:cb9ca7946c57
r17184:a6dd7165a9b8
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r28176:bf82dbbfec4e
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r8970:a7dc16677ffd
r13757:cb9ca7946c57
r8970:a7dc16677ffd
r7066:7459a71fc283
r7928:a80e7e05d6c5
r7066:7459a71fc283
r21441:3cecc5d58a3f
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r21441:3cecc5d58a3f
r8970:a7dc16677ffd
r7066:7459a71fc283
r10341:c21fa463ba20
r10341:c21fa463ba20
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r22129:8e5c5b5cd408
r28279:0fa1023cc51e
r28279:0fa1023cc51e
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r28279:0fa1023cc51e
r21441:3cecc5d58a3f
r22129:8e5c5b5cd408
r21441:3cecc5d58a3f
r22129:8e5c5b5cd408
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r21441:3cecc5d58a3f
r28278:1fc682037ca2
r22129:8e5c5b5cd408
r21441:3cecc5d58a3f
r7062:6a1de70754de
r7062:6a1de70754de
r7062:6a1de70754de
r7066:7459a71fc283
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r27884:803962be0328
r28278:1fc682037ca2
r27883:ddbd33508a8a
r27883:ddbd33508a8a
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r23607:36c15679007d
r13024:48c81d0b078a
r7809:a8c0806e350f
r6981:8e1c715f9e05
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file timetable_cmd.cpp Commands related to time tabling. */

#include "stdafx.h"
#include "command_func.h"
#include "company_func.h"
#include "timer/timer_game_tick.h"
#include "timer/timer_game_calendar.h"
#include "window_func.h"
#include "vehicle_base.h"
#include "timetable_cmd.h"
#include "timetable.h"

#include "table/strings.h"

#include "safeguards.h"

/**
 * Get the TimerGameTick::TickCounter tick of a given date.
 * @param start_date The date when the timetable starts.
 * @return The first tick of this date.
 */
TimerGameTick::TickCounter GetStartTickFromDate(TimerGameCalendar::Date start_date)
{
	/* Calculate the offset in ticks from the current date. */
	TimerGameTick::Ticks tick_offset = (start_date - TimerGameCalendar::date).base() * Ticks::DAY_TICKS;

	/* Compensate for the current date_fract. */
	tick_offset -= TimerGameCalendar::date_fract;

	/* Return the current tick plus the offset. */
	return TimerGameTick::counter + tick_offset;
}

/**
 * Get a date from a given start tick of timetable.
 * @param start_tick The TimerGameTick::TickCounter when the timetable starts.
 * @return The date when we reach this tick.
 */
TimerGameCalendar::Date GetDateFromStartTick(TimerGameTick::TickCounter start_tick)
{
	/* Calculate the offset in ticks from the current counter tick. */
	TimerGameTick::Ticks tick_offset = start_tick - TimerGameTick::counter;

	/* Compensate for the current date_fract. */
	tick_offset += TimerGameCalendar::date_fract;

	/* Return the current date plus the offset in days. */
	return TimerGameCalendar::date + (tick_offset / Ticks::DAY_TICKS);
}

/**
 * Change/update a particular timetable entry.
 * @param v            The vehicle to change the timetable of.
 * @param order_number The index of the timetable in the order list.
 * @param val          The new data of the timetable entry.
 * @param mtf          Which part of the timetable entry to change.
 * @param timetabled   If the new value is explicitly timetabled.
 */
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16_t val, ModifyTimetableFlags mtf, bool timetabled)
{
	Order *order = v->GetOrder(order_number);
	assert(order != nullptr);
	int total_delta = 0;
	int timetable_delta = 0;

	switch (mtf) {
		case MTF_WAIT_TIME:
			total_delta = val - order->GetWaitTime();
			timetable_delta = (timetabled ? val : 0) - order->GetTimetabledWait();
			order->SetWaitTime(val);
			order->SetWaitTimetabled(timetabled);
			break;

		case MTF_TRAVEL_TIME:
			total_delta = val - order->GetTravelTime();
			timetable_delta = (timetabled ? val : 0) - order->GetTimetabledTravel();
			order->SetTravelTime(val);
			order->SetTravelTimetabled(timetabled);
			break;

		case MTF_TRAVEL_SPEED:
			order->SetMaxSpeed(val);
			break;

		default:
			NOT_REACHED();
	}
	v->orders->UpdateTotalDuration(total_delta);
	v->orders->UpdateTimetableDuration(timetable_delta);

	for (v = v->FirstShared(); v != nullptr; v = v->NextShared()) {
		if (v->cur_real_order_index == order_number && v->current_order.Equals(*order)) {
			switch (mtf) {
				case MTF_WAIT_TIME:
					v->current_order.SetWaitTime(val);
					v->current_order.SetWaitTimetabled(timetabled);
					break;

				case MTF_TRAVEL_TIME:
					v->current_order.SetTravelTime(val);
					v->current_order.SetTravelTimetabled(timetabled);
					break;

				case MTF_TRAVEL_SPEED:
					v->current_order.SetMaxSpeed(val);
					break;

				default:
					NOT_REACHED();
			}
		}
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
	}
}

/**
 * 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 or an error
 */
CommandCost CmdChangeTimetable(DoCommandFlag flags, VehicleID veh, VehicleOrderID order_number, ModifyTimetableFlags mtf, uint16_t data)
{
	Vehicle *v = Vehicle::GetIfValid(veh);
	if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;

	CommandCost ret = CheckOwnership(v->owner);
	if (ret.Failed()) return ret;

	Order *order = v->GetOrder(order_number);
	if (order == nullptr || order->IsType(OT_IMPLICIT)) return CMD_ERROR;

	if (mtf >= MTF_END) return CMD_ERROR;

	int wait_time   = order->GetWaitTime();
	int travel_time = order->GetTravelTime();
	int max_speed   = order->GetMaxSpeed();
	switch (mtf) {
		case MTF_WAIT_TIME:
			wait_time = data;
			break;

		case MTF_TRAVEL_TIME:
			travel_time = data;
			break;

		case MTF_TRAVEL_SPEED:
			max_speed = data;
			if (max_speed == 0) max_speed = UINT16_MAX; // Disable speed limit.
			break;

		default:
			NOT_REACHED();
	}

	if (wait_time != order->GetWaitTime()) {
		switch (order->GetType()) {
			case OT_GOTO_STATION:
				if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
				break;

			case OT_CONDITIONAL:
				break;

			default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
		}
	}

	if (travel_time != order->GetTravelTime() && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
	if (max_speed != order->GetMaxSpeed() && (order->IsType(OT_CONDITIONAL) || v->type == VEH_AIRCRAFT)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		switch (mtf) {
			case MTF_WAIT_TIME:
				/* Set time if changing the value or confirming an estimated time as timetabled. */
				if (wait_time != order->GetWaitTime() || (wait_time > 0 && !order->IsWaitTimetabled())) {
					ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME, wait_time > 0);
				}
				break;

			case MTF_TRAVEL_TIME:
				/* Set time if changing the value or confirming an estimated time as timetabled. */
				if (travel_time != order->GetTravelTime() || (travel_time > 0 && !order->IsTravelTimetabled())) {
					ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME, travel_time > 0);
				}
				break;

			case MTF_TRAVEL_SPEED:
				if (max_speed != order->GetMaxSpeed()) {
					ChangeTimetable(v, order_number, max_speed, MTF_TRAVEL_SPEED, max_speed != UINT16_MAX);
				}
				break;

			default:
				break;
		}
	}

	return CommandCost();
}

/**
 * Change timetable data of all orders of a vehicle.
 * @param flags Operation to perform.
 * @param veh Vehicle with the orders to change.
 * @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 or an error
 */
CommandCost CmdBulkChangeTimetable(DoCommandFlag flags, VehicleID veh, ModifyTimetableFlags mtf, uint16_t data)
{
	Vehicle *v = Vehicle::GetIfValid(veh);
	if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;

	CommandCost ret = CheckOwnership(v->owner);
	if (ret.Failed()) return ret;

	if (mtf >= MTF_END) return CMD_ERROR;

	if (v->GetNumOrders() == 0) return CMD_ERROR;

	if (flags & DC_EXEC) {
		for (VehicleOrderID order_number = 0; order_number < v->GetNumOrders(); order_number++) {
			Order *order = v->GetOrder(order_number);
			if (order == nullptr || order->IsType(OT_IMPLICIT)) continue;

			Command<CMD_CHANGE_TIMETABLE>::Do(DC_EXEC, v->index, order_number, mtf, data);
		}
	}

	return CommandCost();
}

/**
 * Clear the lateness counter to make the vehicle on time.
 * @param flags Operation to perform.
 * @param veh Vehicle with the orders to change.
 * @param apply_to_group Set to reset the late counter for all vehicles sharing the orders.
 * @return the cost of this operation or an error
 */
CommandCost CmdSetVehicleOnTime(DoCommandFlag flags, VehicleID veh, bool apply_to_group)
{
	Vehicle *v = Vehicle::GetIfValid(veh);
	if (v == nullptr || !v->IsPrimaryVehicle() || v->orders == nullptr) return CMD_ERROR;

	/* A vehicle can't be late if its timetable hasn't started.
	 * If we're setting all vehicles in the group, we handle that below. */
	if (!apply_to_group && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return CommandCost(STR_ERROR_TIMETABLE_NOT_STARTED);

	CommandCost ret = CheckOwnership(v->owner);
	if (ret.Failed()) return ret;

	if (flags & DC_EXEC) {
		if (apply_to_group) {
			TimerGameTick::Ticks most_late = 0;
			for (Vehicle *u = v->FirstShared(); u != nullptr; u = u->NextShared()) {
				/* A vehicle can't be late if its timetable hasn't started. */
				if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) continue;

				if (u->lateness_counter > most_late) {
					most_late = u->lateness_counter;
				}
			}
			if (most_late > 0) {
				for (Vehicle *u = v->FirstShared(); u != nullptr; u = u->NextShared()) {
					/* A vehicle can't be late if its timetable hasn't started. */
					if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) continue;

					u->lateness_counter -= most_late;
					SetWindowDirty(WC_VEHICLE_TIMETABLE, u->index);
				}
			}
		} else {
			v->lateness_counter = 0;
			SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
		}
	}

	return CommandCost();
}

/**
 * Order vehicles based on their timetable. The vehicles will be sorted in order
 * they would reach the first station.
 *
 * @param a First Vehicle pointer.
 * @param b Second Vehicle pointer.
 * @return Comparison value.
 */
static bool VehicleTimetableSorter(Vehicle * const &a, Vehicle * const &b)
{
	VehicleOrderID a_order = a->cur_real_order_index;
	VehicleOrderID b_order = b->cur_real_order_index;
	int j = (int)b_order - (int)a_order;

	/* Are we currently at an ordered station (un)loading? */
	bool a_load = a->current_order.IsType(OT_LOADING) && a->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
	bool b_load = b->current_order.IsType(OT_LOADING) && b->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;

	/* If the current order is not loading at the ordered station, decrease the order index by one since we have
	 * not yet arrived at the station (and thus the timetable entry; still in the travelling of the previous one).
	 * Since the ?_order variables are unsigned the -1 will flow under and place the vehicles going to order #0 at
	 * the begin of the list with vehicles arriving at #0. */
	if (!a_load) a_order--;
	if (!b_load) b_order--;

	/* First check the order index that accounted for loading, then just the raw one. */
	int i = (int)b_order - (int)a_order;
	if (i != 0) return i < 0;
	if (j != 0) return j < 0;

	/* Look at the time we spent in this order; the higher, the closer to its destination. */
	i = b->current_order_time - a->current_order_time;
	if (i != 0) return i < 0;

	/* If all else is equal, use some unique index to sort it the same way. */
	return b->unitnumber < a->unitnumber;
}

/**
 * Set the start date of the timetable.
 * @param flags Operation to perform.
 * @param veh_id Vehicle ID.
 * @param timetable_all Set to set timetable start for all vehicles sharing this order
 * @param start_tick The TimerGameTick::counter tick when the timetable starts.
 * @return The error or cost of the operation.
 */
CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool timetable_all, TimerGameTick::TickCounter start_tick)
{
	Vehicle *v = Vehicle::GetIfValid(veh_id);
	if (v == nullptr || !v->IsPrimaryVehicle() || v->orders == nullptr) return CMD_ERROR;

	CommandCost ret = CheckOwnership(v->owner);
	if (ret.Failed()) return ret;

	TimerGameTick::Ticks total_duration = v->orders->GetTimetableTotalDuration();

	TimerGameCalendar::Date start_date = GetDateFromStartTick(start_tick);

	/* Don't let a timetable start at an invalid date. */
	if (start_date < 0 || start_date > CalendarTime::MAX_DATE) return CMD_ERROR;

	/* Don't let a timetable start more than 15 years into the future... */
	if (start_date - TimerGameCalendar::date > TimerGameCalendar::DateAtStartOfYear(MAX_TIMETABLE_START_YEARS)) return CMD_ERROR;
	/* ...or 1 year in the past. */
	if (TimerGameCalendar::date - start_date > CalendarTime::DAYS_IN_LEAP_YEAR) return CMD_ERROR;

	/* If trying to distribute start dates over a shared order group, we need to know the total duration. */
	if (timetable_all && !v->orders->IsCompleteTimetable()) return CommandCost(STR_ERROR_TIMETABLE_INCOMPLETE);

	/* Don't allow invalid start dates for other vehicles in the shared order group. */
	if (timetable_all && start_date + (total_duration / Ticks::DAY_TICKS) > CalendarTime::MAX_DATE) return CMD_ERROR;

	if (flags & DC_EXEC) {
		std::vector<Vehicle *> vehs;

		if (timetable_all) {
			for (Vehicle *w = v->orders->GetFirstSharedVehicle(); w != nullptr; w = w->NextShared()) {
				vehs.push_back(w);
			}
		} else {
			vehs.push_back(v);
		}

		int num_vehs = (uint)vehs.size();

		if (num_vehs >= 2) {
			std::sort(vehs.begin(), vehs.end(), &VehicleTimetableSorter);
		}

		int idx = 0;

		for (Vehicle *w : vehs) {

			w->lateness_counter = 0;
			ClrBit(w->vehicle_flags, VF_TIMETABLE_STARTED);
			/* Do multiplication, then division to reduce rounding errors. */
			w->timetable_start = start_tick + (idx * total_duration / num_vehs);
			SetWindowDirty(WC_VEHICLE_TIMETABLE, w->index);
			++idx;
		}

	}

	return CommandCost();
}


/**
 * Start or stop filling the timetable automatically from the time the vehicle
 * actually takes to complete it. When starting to autofill the current times
 * are cleared and the timetable will start again from scratch.
 * @param flags Operation to perform.
 * @param veh Vehicle index.
 * @param autofill Enable or disable autofill
 * @param preserve_wait_time Set to preserve waiting times in non-destructive mode
 * @return the cost of this operation or an error
 */
CommandCost CmdAutofillTimetable(DoCommandFlag flags, VehicleID veh, bool autofill, bool preserve_wait_time)
{
	Vehicle *v = Vehicle::GetIfValid(veh);
	if (v == nullptr || !v->IsPrimaryVehicle() || v->orders == nullptr) return CMD_ERROR;

	CommandCost ret = CheckOwnership(v->owner);
	if (ret.Failed()) return ret;

	if (flags & DC_EXEC) {
		if (autofill) {
			/* Start autofilling the timetable, which clears the
			 * "timetable has started" bit. Times are not cleared anymore, but are
			 * overwritten when the order is reached now. */
			SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
			ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);

			/* Overwrite waiting times only if they got longer */
			if (preserve_wait_time) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);

			v->timetable_start = 0;
			v->lateness_counter = 0;
		} else {
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
			ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
		}

		for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
			if (v2 != v) {
				/* Stop autofilling; only one vehicle at a time can perform autofill */
				ClrBit(v2->vehicle_flags, VF_AUTOFILL_TIMETABLE);
				ClrBit(v2->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
			}
			SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
		}
	}

	return CommandCost();
}

/**
 * Update the timetable for the vehicle.
 * @param v The vehicle to update the timetable for.
 * @param travelling Whether we just travelled or waited at a station.
 */
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
{
	TimerGameTick::Ticks time_taken = v->current_order_time;

	v->current_order_time = 0;

	if (v->current_order.IsType(OT_IMPLICIT)) return; // no timetabling of auto orders

	if (v->cur_real_order_index >= v->GetNumOrders()) return;
	Order *real_current_order = v->GetOrder(v->cur_real_order_index);
	assert(real_current_order != nullptr);

	VehicleOrderID first_manual_order = 0;
	for (Order *o = v->GetFirstOrder(); o != nullptr && o->IsType(OT_IMPLICIT); o = o->next) {
		++first_manual_order;
	}

	bool just_started = false;

	/* This vehicle is arriving at the first destination in the timetable. */
	if (v->cur_real_order_index == first_manual_order && travelling) {
		/* If the start date hasn't been set, or it was set automatically when
		 * the vehicle last arrived at the first destination, update it to the
		 * current time. Otherwise set the late counter appropriately to when
		 * the vehicle should have arrived. */
		just_started = !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED);

		if (v->timetable_start != 0) {
			v->lateness_counter = TimerGameTick::counter - v->timetable_start;
			v->timetable_start = 0;
		}

		SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
	}

	if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;

	bool autofilling = HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
	bool remeasure_wait_time = !real_current_order->IsWaitTimetabled() ||
			(autofilling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME));

	if (travelling && remeasure_wait_time) {
		/* We just finished travelling and want to remeasure the loading time,
		 * so do not apply any restrictions for the loading to finish. */
		v->current_order.SetWaitTime(0);
	}

	if (just_started) return;

	/* Before modifying waiting times, check whether we want to preserve bigger ones. */
	if (!real_current_order->IsType(OT_CONDITIONAL) &&
			(travelling || time_taken > real_current_order->GetWaitTime() || remeasure_wait_time)) {
		/* Round up to the smallest unit of time commonly shown in the GUI (seconds) to avoid confusion.
		 * Players timetabling in Ticks can adjust later.
		 * For trains/aircraft multiple movement cycles are done in one
		 * tick. This makes it possible to leave the station and process
		 * e.g. a depot order in the same tick, causing it to not fill
		 * the timetable entry like is done for road vehicles/ships.
		 * Thus always make sure at least one tick is used between the
		 * processing of different orders when filling the timetable. */
		uint time_to_set = CeilDiv(std::max(time_taken, 1), Ticks::TICKS_PER_SECOND) * Ticks::TICKS_PER_SECOND;

		if (travelling && (autofilling || !real_current_order->IsTravelTimetabled())) {
			ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_TRAVEL_TIME, autofilling);
		} else if (!travelling && (autofilling || !real_current_order->IsWaitTimetabled())) {
			ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_WAIT_TIME, autofilling);
		}
	}

	if (v->cur_real_order_index == first_manual_order && travelling) {
		/* If we just started we would have returned earlier and have not reached
		 * this code. So obviously, we have completed our round: So turn autofill
		 * off again. */
		ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
		ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
	}

	if (autofilling) return;

	TimerGameTick::Ticks timetabled = travelling ? real_current_order->GetTimetabledTravel() :
			real_current_order->GetTimetabledWait();

	/* Vehicles will wait at stations if they arrive early even if they are not
	 * timetabled to wait there, so make sure the lateness counter is updated
	 * when this happens. */
	if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;

	v->lateness_counter -= (timetabled - time_taken);

	/* When we are more late than this timetabled bit takes we (somewhat expensively)
	 * check how many ticks the (fully filled) timetable has. If a timetable cycle is
	 * shorter than the amount of ticks we are late we reduce the lateness by the
	 * length of a full cycle till lateness is less than the length of a timetable
	 * cycle. When the timetable isn't fully filled the cycle will be Ticks::INVALID_TICKS. */
	if (v->lateness_counter > timetabled) {
		TimerGameTick::Ticks cycle = v->orders->GetTimetableTotalDuration();
		if (cycle != Ticks::INVALID_TICKS && v->lateness_counter > cycle) {
			v->lateness_counter %= cycle;
		}
	}

	for (v = v->FirstShared(); v != nullptr; v = v->NextShared()) {
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
	}
}