Files @ r28520:f9aebe299cae
Branch filter:

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

Patric Stout
Codechange: MacOS already has MIN/MAX macros defined

This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
  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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6420:01087f989fd1
r5584:545d748cc681
r5584:545d748cc681
r6453:b0b56773284a
r6980:3b43790d6e21
r8763:c2c776621d56
r10208:ef8fcc3dc4ca
r13828:41995609971a
r8962:cfbdc736c8db
r13825:5de0a5d39b9e
r5584:545d748cc681
r13825:5de0a5d39b9e
r5584:545d748cc681
r5972:0afe141fca29
r8114:866ed489ed98
r8131:7a50db7be0ff
r27148:4e041ae27b9d
r8144:1432edd15267
r8157:cf41aa22cd09
r10696:8dfe83e30d01
r22645:184bf99e642e
r14248:a9050881acd7
r14248:a9050881acd7
r17856:71a25bec0cc6
r18462:a1ba75a7f741
r22959:8c769e762c2f
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r26094:7572e88decb3
r8211:7dc9fca2785c
r8264:d493cb51fe8a
r5584:545d748cc681
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17982:320ae85f0acc
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r17856:71a25bec0cc6
r27737:728d55b97775
r5584:545d748cc681
r21050:ac153d7f94aa
r27737:728d55b97775
r21050:ac153d7f94aa
r21050:ac153d7f94aa
r21050:ac153d7f94aa
r21050:ac153d7f94aa
r8616:656db5986c9e
r5584:545d748cc681
r8616:656db5986c9e
r5584:545d748cc681
r5584:545d748cc681
r22459:c1921e75ce89
r5584:545d748cc681
r13208:40382e8abf19
r27737:728d55b97775
r5584:545d748cc681
r5584:545d748cc681
r22459:c1921e75ce89
r22459:c1921e75ce89
r5584:545d748cc681
r13208:40382e8abf19
r5584:545d748cc681
r9022:5be8b7703ae9
r21050:ac153d7f94aa
r22459:c1921e75ce89
r9022:5be8b7703ae9
r9022:5be8b7703ae9
r18234:0d83ef81cba4
r9022:5be8b7703ae9
r22459:c1921e75ce89
r22459:c1921e75ce89
r22459:c1921e75ce89
r22460:68e4922ff4a3
r22460:68e4922ff4a3
r21979:8f441a1deb2a
r22460:68e4922ff4a3
r22460:68e4922ff4a3
r22460:68e4922ff4a3
r22460:68e4922ff4a3
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r19884:a1593074c6b8
r19884:a1593074c6b8
r19884:a1593074c6b8
r19884:a1593074c6b8
r19884:a1593074c6b8
r19884:a1593074c6b8
r19884:a1593074c6b8
r5972:0afe141fca29
r19884:a1593074c6b8
r5972:0afe141fca29
r22459:c1921e75ce89
r22459:c1921e75ce89
r22459:c1921e75ce89
r22460:68e4922ff4a3
r22460:68e4922ff4a3
r5972:0afe141fca29
r26546:de367f04dbcc
r26546:de367f04dbcc
r22460:68e4922ff4a3
r22460:68e4922ff4a3
r5972:0afe141fca29
r5972:0afe141fca29
r22459:c1921e75ce89
r5584:545d748cc681
r27737:728d55b97775
r5584:545d748cc681
r23203:0db8e65bec82
r23203:0db8e65bec82
r5584:545d748cc681
r22459:c1921e75ce89
r22459:c1921e75ce89
r5584:545d748cc681
r18228:6cd8bae37157
r5584:545d748cc681
r9022:5be8b7703ae9
r21050:ac153d7f94aa
r22459:c1921e75ce89
r5584:545d748cc681
r5584:545d748cc681
r14021:1aae6a0ffa3b
r5584:545d748cc681
r13828:41995609971a
r23607:36c15679007d
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r5584:545d748cc681
r23957:a76e956cb358
r8554:399ea60cfafc
r8961:7da2b7436c69
r8554:399ea60cfafc
r8554:399ea60cfafc
r8554:399ea60cfafc
r8554:399ea60cfafc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8554:399ea60cfafc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12027:83216fa52f8f
r19467:47fa1346a132
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r14021:1aae6a0ffa3b
r5584:545d748cc681
r23607:36c15679007d
r8836:911846a8cfa6
r8836:911846a8cfa6
r18682:0cc315a3a76a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8853:c56f6dec859b
r23178:e613ba71d020
r18682:0cc315a3a76a
r5584:545d748cc681
r5584:545d748cc681
r17625:366001a31c88
r17625:366001a31c88
r17625:366001a31c88
r16372:76fe3abb2725
r16372:76fe3abb2725
r17838:9b7a4cc981f2
r17838:9b7a4cc981f2
r17838:9b7a4cc981f2
r18024:9fef0937e079
r18024:9fef0937e079
r18024:9fef0937e079
r16502:5d6ae9da514c
r17899:92aaab03ad72
r17899:92aaab03ad72
r17899:92aaab03ad72
r16502:5d6ae9da514c
r16372:76fe3abb2725
r16372:76fe3abb2725
r10875:1cfbd5e0cc73
r10875:1cfbd5e0cc73
r18228:6cd8bae37157
r13731:60d669a7b9e1
r18229:a35410711978
r10875:1cfbd5e0cc73
r10875:1cfbd5e0cc73
r8467:0ea88f22d4aa
r5584:545d748cc681
r15542:f6c6d0f8689e
r8467:0ea88f22d4aa
r15542:f6c6d0f8689e
r5584:545d748cc681
r8467:0ea88f22d4aa
r8467:0ea88f22d4aa
r8467:0ea88f22d4aa
r5584:545d748cc681
r8467:0ea88f22d4aa
r5584:545d748cc681
r8556:c5860d29500d
r5584:545d748cc681
r27884:803962be0328
r8556:c5860d29500d
r8556:c5860d29500d
r8556:c5860d29500d
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r13024:48c81d0b078a
r6420:01087f989fd1
r13024:48c81d0b078a
r5584:545d748cc681
r5584:545d748cc681
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r25484:c5af4712cb7f
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r6553:04028e73a0f7
r5584:545d748cc681
r20610:4d2570719606
r20610:4d2570719606
r16422:45fe03d3bf62
r5584:545d748cc681
r5584:545d748cc681
r26531:0c30eaa59bc4
r5584:545d748cc681
r26531:0c30eaa59bc4
r26531:0c30eaa59bc4
r6593:469af92ae569
r6593:469af92ae569
r8827:6ee5217ef94b
r5584:545d748cc681
r8830:3bad3e96d573
r8830:3bad3e96d573
r11917:612c11f7ab47
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r8827:6ee5217ef94b
r17184:a6dd7165a9b8
r8827:6ee5217ef94b
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r22868:6eb08946de7f
r5584:545d748cc681
r27737:728d55b97775
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r5584:545d748cc681
r6558:469828caa298
r27737:728d55b97775
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r19411:0e2bff2f890e
r8793:779ed14d9cc0
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r23276:c458bd8c3ba3
r5584:545d748cc681
r5584:545d748cc681
r23156:a3b45a3c1810
r24809:7c4e0873f12c
r23156:a3b45a3c1810
r27942:f7389062d120
r23156:a3b45a3c1810
r24809:7c4e0873f12c
r23156:a3b45a3c1810
r23156:a3b45a3c1810
r26015:62da647d3454
r26014:71f8ae89abb6
r26014:71f8ae89abb6
r26014:71f8ae89abb6
r26014:71f8ae89abb6
r26015:62da647d3454
r26015:62da647d3454
r26014:71f8ae89abb6
r26014:71f8ae89abb6
r26014:71f8ae89abb6
r26014:71f8ae89abb6
r26014:71f8ae89abb6
r17535:d01e1a26daa5
r5584:545d748cc681
r19467:47fa1346a132
r17535:d01e1a26daa5
r17535:d01e1a26daa5
r17535:d01e1a26daa5
r17535:d01e1a26daa5
r17535:d01e1a26daa5
r17535:d01e1a26daa5
r17535:d01e1a26daa5
r5584:545d748cc681
r23154:e57cef82d7ee
r23154:e57cef82d7ee
r23154:e57cef82d7ee
r23156:a3b45a3c1810
r23156:a3b45a3c1810
r24809:7c4e0873f12c
r23156:a3b45a3c1810
r9045:56afeadb0fbc
r9045:56afeadb0fbc
r5584:545d748cc681
r19550:f624172dec6d
r22596:d66173d9fdc5
r19550:f624172dec6d
r22596:d66173d9fdc5
r19550:f624172dec6d
r19550:f624172dec6d
r19550:f624172dec6d
r19550:f624172dec6d
r26014:71f8ae89abb6
r19550:f624172dec6d
r19550:f624172dec6d
r19550:f624172dec6d
r19550:f624172dec6d
r23203:0db8e65bec82
r19550:f624172dec6d
r19550:f624172dec6d
r23203:0db8e65bec82
r5584:545d748cc681
r19550:f624172dec6d
r17535:d01e1a26daa5
r5584:545d748cc681
r9045:56afeadb0fbc
r11971:ac7eb21a00e4
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18855:53b988159599
r15484:b055edb27a71
r5584:545d748cc681
r26531:0c30eaa59bc4
r5584:545d748cc681
r5584:545d748cc681
r13024:48c81d0b078a
r17535:d01e1a26daa5
r17535:d01e1a26daa5
r5584:545d748cc681
r5584:545d748cc681
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r27190:da3564d50942
r5584:545d748cc681
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r11363:6906c490a00e
r27190:da3564d50942
r27190:da3564d50942
r18682:0cc315a3a76a
r5584:545d748cc681
r5584:545d748cc681
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r5584:545d748cc681
r16362:9d8d927a8276
r16362:9d8d927a8276
r16362:9d8d927a8276
r16362:9d8d927a8276
r16362:9d8d927a8276
r10647:62911ec68e89
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12005:1096fe8178fd
r11725:57bc99fdc1bc
r19369:05b3aefe07b5
r5584:545d748cc681
r10123:310b4a36ab6c
r10123:310b4a36ab6c
r18501:8e3d905ea4bc
r22645:184bf99e642e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r19549:dd9cbeaec418
r19549:dd9cbeaec418
r19549:dd9cbeaec418
r19549:dd9cbeaec418
r19549:dd9cbeaec418
r19549:dd9cbeaec418
r19549:dd9cbeaec418
r15613:193c12018337
r16768:069c18958074
r5584:545d748cc681
r8554:399ea60cfafc
r8554:399ea60cfafc
r16768:069c18958074
r16768:069c18958074
r23155:9478b05634f7
r23573:9e11ee1779ef
r23573:9e11ee1779ef
r23158:cb904715ae99
r23214:c04c9a31fcbc
r23398:38ccf1798c05
r23155:9478b05634f7
r23155:9478b05634f7
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23180:e97c0042b5fd
r23178:e613ba71d020
r23178:e613ba71d020
r23155:9478b05634f7
r23248:ca7f30396636
r23178:e613ba71d020
r23155:9478b05634f7
r23155:9478b05634f7
r5584:545d748cc681
r16768:069c18958074
r16768:069c18958074
r16768:069c18958074
r5584:545d748cc681
r5584:545d748cc681
r23329:f0d3b7025198
r23329:f0d3b7025198
r23329:f0d3b7025198
r23329:f0d3b7025198
r23329:f0d3b7025198
r23329:f0d3b7025198
r24031:2e70ebd83a00
r5584:545d748cc681
r23329:f0d3b7025198
r23329:f0d3b7025198
r23329:f0d3b7025198
r5584:545d748cc681
r5584:545d748cc681
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r5584:545d748cc681
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r5584:545d748cc681
r26245:d7f5ec275f43
r5584:545d748cc681
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r5584:545d748cc681
r26245:d7f5ec275f43
r5584:545d748cc681
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r5584:545d748cc681
r26245:d7f5ec275f43
r5584:545d748cc681
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r26245:d7f5ec275f43
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23278:5df09db4235a
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r27945:eecaf9c32524
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r24135:db14c768b909
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r23760:58e1a032cdaf
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r11971:ac7eb21a00e4
r5584:545d748cc681
r5584:545d748cc681
r6980:3b43790d6e21
r5584:545d748cc681
r15947:6f1c6a44a807
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r27190:da3564d50942
r26014:71f8ae89abb6
r6594:cd6701cee0ee
r5584:545d748cc681
r8836:911846a8cfa6
r5584:545d748cc681
r17535:d01e1a26daa5
r5584:545d748cc681
r16502:5d6ae9da514c
r16502:5d6ae9da514c
r23203:0db8e65bec82
r23203:0db8e65bec82
r23203:0db8e65bec82
r23203:0db8e65bec82
r23203:0db8e65bec82
r24672:610552ba854b
r24672:610552ba854b
r23203:0db8e65bec82
r23203:0db8e65bec82
r23203:0db8e65bec82
r23203:0db8e65bec82
r23203:0db8e65bec82
r23278:5df09db4235a
r23278:5df09db4235a
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r9490:5cf190d39c21
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r9490:5cf190d39c21
r9490:5cf190d39c21
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5994:2d36164769d3
r23416:88996730d74a
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r27190:da3564d50942
r27190:da3564d50942
r27190:da3564d50942
r5584:545d748cc681
r5584:545d748cc681
r11965:2b94ac4aa35a
r5584:545d748cc681
r22959:8c769e762c2f
r22959:8c769e762c2f
r8556:c5860d29500d
r8556:c5860d29500d
r7135:3964060426dc
r11965:2b94ac4aa35a
r11965:2b94ac4aa35a
r5584:545d748cc681
r5584:545d748cc681
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r23178:e613ba71d020
r15610:623a23fb6560
r15610:623a23fb6560
r26095:00c14d52e378
r15839:2b7528d029e3
r15839:2b7528d029e3
r23023:7b8669afd1db
r15839:2b7528d029e3
r5584:545d748cc681
r26107:59139eb8d51a
r5584:545d748cc681
r15867:ecad3e587c06
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r13208:40382e8abf19
r5584:545d748cc681
r11971:ac7eb21a00e4
r15839:2b7528d029e3
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18245:fb370bf5a3c0
r5584:545d748cc681
r22868:6eb08946de7f
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11188:143f3c64befd
r5584:545d748cc681
r20252:76a6f1c78ce7
r5584:545d748cc681
r5584:545d748cc681
r20252:76a6f1c78ce7
r15839:2b7528d029e3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12160:b49804528c72
r5584:545d748cc681
r11971:ac7eb21a00e4
r5584:545d748cc681
r20037:2761c8d9b8cc
r27148:4e041ae27b9d
r27769:ebc238ecee04
r27148:4e041ae27b9d
r24570:4fa1d23595bf
r27231:22301bb798c3
r5584:545d748cc681
r16372:76fe3abb2725
r16372:76fe3abb2725
r7931:4c17a74c399e
r20041:fec94c55536d
r6176:e229e61b4324
r12068:a96f28ffc328
r12068:a96f28ffc328
r18327:3f476c73b5cb
r6608:f2d13a751f0b
r12068:a96f28ffc328
r12068:a96f28ffc328
r21720:231348c28655
r5584:545d748cc681
r5584:545d748cc681
r15839:2b7528d029e3
r5584:545d748cc681
r5584:545d748cc681
r26715:ffe313a8ffbc
r8890:02179c54681e
r14021:1aae6a0ffa3b
r26715:ffe313a8ffbc
r8890:02179c54681e
r26715:ffe313a8ffbc
r8890:02179c54681e
/*
 * 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 ship_cmd.cpp Handling of ships. */

#include "stdafx.h"
#include "ship.h"
#include "landscape.h"
#include "timetable.h"
#include "news_func.h"
#include "company_func.h"
#include "pathfinder/npf/npf_func.h"
#include "depot_base.h"
#include "station_base.h"
#include "newgrf_engine.h"
#include "pathfinder/yapf/yapf.h"
#include "newgrf_sound.h"
#include "spritecache.h"
#include "strings_func.h"
#include "window_func.h"
#include "timer/timer_game_calendar.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "engine_base.h"
#include "company_base.h"
#include "tunnelbridge_map.h"
#include "zoom_func.h"
#include "framerate_type.h"
#include "industry.h"
#include "industry_map.h"
#include "ship_cmd.h"

#include "table/strings.h"

#include "safeguards.h"

/**
 * Determine the effective #WaterClass for a ship travelling on a tile.
 * @param tile Tile of interest
 * @return the waterclass to be used by the ship.
 */
WaterClass GetEffectiveWaterClass(TileIndex tile)
{
	if (HasTileWaterClass(tile)) return GetWaterClass(tile);
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
		assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER);
		return WATER_CLASS_CANAL;
	}
	if (IsTileType(tile, MP_RAILWAY)) {
		assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
		return WATER_CLASS_SEA;
	}
	NOT_REACHED();
}

static const uint16_t _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};

template <>
bool IsValidImageIndex<VEH_SHIP>(uint8_t image_index)
{
	return image_index < lengthof(_ship_sprites);
}

static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
{
	return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
}

static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
{
	const Engine *e = Engine::Get(engine);
	uint8_t spritenum = e->u.ship.image_index;

	if (is_custom_sprite(spritenum)) {
		GetCustomVehicleIcon(engine, DIR_W, image_type, result);
		if (result->IsValid()) return;

		spritenum = e->original_image_index;
	}

	assert(IsValidImageIndex<VEH_SHIP>(spritenum));
	result->Set(DIR_W + _ship_sprites[spritenum]);
}

void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
{
	VehicleSpriteSeq seq;
	GetShipIcon(engine, image_type, &seq);

	Rect rect;
	seq.GetBounds(&rect);
	preferred_x = Clamp(preferred_x,
			left - UnScaleGUI(rect.left),
			right - UnScaleGUI(rect.right));

	seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
}

/**
 * Get the size of the sprite of a ship sprite heading west (used for lists).
 * @param engine The engine to get the sprite from.
 * @param[out] width The width of the sprite.
 * @param[out] height The height of the sprite.
 * @param[out] xoffs Number of pixels to shift the sprite to the right.
 * @param[out] yoffs Number of pixels to shift the sprite downwards.
 * @param image_type Context the sprite is used in.
 */
void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
{
	VehicleSpriteSeq seq;
	GetShipIcon(engine, image_type, &seq);

	Rect rect;
	seq.GetBounds(&rect);

	width  = UnScaleGUI(rect.Width());
	height = UnScaleGUI(rect.Height());
	xoffs  = UnScaleGUI(rect.left);
	yoffs  = UnScaleGUI(rect.top);
}

void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
{
	uint8_t spritenum = this->spritenum;

	if (image_type == EIT_ON_MAP) direction = this->rotation;

	if (is_custom_sprite(spritenum)) {
		GetCustomVehicleSprite(this, direction, image_type, result);
		if (result->IsValid()) return;

		spritenum = this->GetEngine()->original_image_index;
	}

	assert(IsValidImageIndex<VEH_SHIP>(spritenum));
	result->Set(_ship_sprites[spritenum] + direction);
}

static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
{
	/* Find the closest depot */
	const Depot *best_depot = nullptr;
	/* If we don't have a maximum distance, i.e. distance = 0,
	 * we want to find any depot so the best distance of no
	 * depot must be more than any correct distance. On the
	 * other hand if we have set a maximum distance, any depot
	 * further away than max_distance can safely be ignored. */
	uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;

	for (const Depot *depot : Depot::Iterate()) {
		TileIndex tile = depot->xy;
		if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
			uint dist = DistanceManhattan(tile, v->tile);
			if (dist < best_dist) {
				best_dist = dist;
				best_depot = depot;
			}
		}
	}

	return best_depot;
}

static void CheckIfShipNeedsService(Vehicle *v)
{
	if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
	if (v->IsChainInDepot()) {
		VehicleServiceInDepot(v);
		return;
	}

	uint max_distance;
	switch (_settings_game.pf.pathfinder_for_ships) {
		case VPF_NPF:  max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty  / NPF_TILE_LENGTH;  break;
		case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
		default: NOT_REACHED();
	}

	const Depot *depot = FindClosestShipDepot(v, max_distance);

	if (depot == nullptr) {
		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
			v->current_order.MakeDummy();
			SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
		}
		return;
	}

	v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
	v->SetDestTile(depot->xy);
	SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
}

/**
 * Update the caches of this ship.
 */
void Ship::UpdateCache()
{
	const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);

	/* Get speed fraction for the current water type. Aqueducts are always canals. */
	bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
	uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
	this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);

	/* Update cargo aging period. */
	this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);

	this->UpdateVisualEffect();
}

Money Ship::GetRunningCost() const
{
	const Engine *e = this->GetEngine();
	uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
	return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
}

void Ship::OnNewDay()
{
	if ((++this->day_counter & 7) == 0) {
		DecreaseVehicleValue(this);
	}

	CheckVehicleBreakdown(this);
	AgeVehicle(this);
	CheckIfShipNeedsService(this);

	CheckOrders(this);

	if (this->running_ticks == 0) return;

	CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (CalendarTime::DAYS_IN_YEAR * Ticks::DAY_TICKS));

	this->profit_this_year -= cost.GetCost();
	this->running_ticks = 0;

	SubtractMoneyFromCompanyFract(this->owner, cost);

	SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
	/* we need this for the profit */
	SetWindowClassesDirty(WC_SHIPS_LIST);
}

Trackdir Ship::GetVehicleTrackdir() const
{
	if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;

	if (this->IsInDepot()) {
		/* We'll assume the ship is facing outwards */
		return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
	}

	if (this->state == TRACK_BIT_WORMHOLE) {
		/* ship on aqueduct, so just use its direction and assume a diagonal track */
		return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
	}

	return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
}

void Ship::MarkDirty()
{
	this->colourmap = PAL_NONE;
	this->UpdateViewport(true, false);
	this->UpdateCache();
}

void Ship::PlayLeaveStationSound(bool force) const
{
	if (PlayVehicleSound(this, VSE_START, force)) return;
	SndPlayVehicleFx(ShipVehInfo(this->engine_type)->sfx, this);
}

TileIndex Ship::GetOrderStationLocation(StationID station)
{
	if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;

	const Station *st = Station::Get(station);
	if (CanVehicleUseStation(this, st)) {
		return st->xy;
	} else {
		this->IncrementRealOrderIndex();
		return 0;
	}
}

void Ship::UpdateDeltaXY()
{
	static const int8_t _delta_xy_table[8][4] = {
		/* y_extent, x_extent, y_offs, x_offs */
		{ 6,  6,  -3,  -3}, // N
		{ 6, 32,  -3, -16}, // NE
		{ 6,  6,  -3,  -3}, // E
		{32,  6, -16,  -3}, // SE
		{ 6,  6,  -3,  -3}, // S
		{ 6, 32,  -3, -16}, // SW
		{ 6,  6,  -3,  -3}, // W
		{32,  6, -16,  -3}, // NW
	};

	const int8_t *bb = _delta_xy_table[this->rotation];
	this->x_offs        = bb[3];
	this->y_offs        = bb[2];
	this->x_extent      = bb[1];
	this->y_extent      = bb[0];
	this->z_extent      = 6;

	if (this->direction != this->rotation) {
		/* If we are rotating, then it is possible the ship was moved to its next position. In that
		 * case, because we are still showing the old direction, the ship will appear to glitch sideways
		 * slightly. We can work around this by applying an additional offset to make the ship appear
		 * where it was before it moved. */
		this->x_offs -= this->x_pos - this->rotation_x_pos;
		this->y_offs -= this->y_pos - this->rotation_y_pos;
	}
}

/**
 * Test-procedure for HasVehicleOnPos to check for any ships which are visible and not stopped by the player.
 */
static Vehicle *EnsureNoMovingShipProc(Vehicle *v, void *)
{
	return v->type == VEH_SHIP && (v->vehstatus & (VS_HIDDEN | VS_STOPPED)) == 0 ? v : nullptr;
}

static bool CheckReverseShip(const Ship *v, Trackdir *trackdir = nullptr)
{
	/* Ask pathfinder for best direction */
	bool reverse = false;
	switch (_settings_game.pf.pathfinder_for_ships) {
		case VPF_NPF: reverse = NPFShipCheckReverse(v, trackdir); break;
		case VPF_YAPF: reverse = YapfShipCheckReverse(v, trackdir); break;
		default: NOT_REACHED();
	}
	return reverse;
}

static bool CheckShipLeaveDepot(Ship *v)
{
	if (!v->IsChainInDepot()) return false;

	/* We are leaving a depot, but have to go to the exact same one; re-enter */
	if (v->current_order.IsType(OT_GOTO_DEPOT) &&
			IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
		VehicleEnterDepot(v);
		return true;
	}

	/* Don't leave depot if no destination set */
	if (v->dest_tile == 0) return true;

	/* Don't leave depot if another vehicle is already entering/leaving */
	/* This helps avoid CPU load if many ships are set to start at the same time */
	if (HasVehicleOnPos(v->tile, nullptr, &EnsureNoMovingShipProc)) return true;

	TileIndex tile = v->tile;
	Axis axis = GetShipDepotAxis(tile);

	DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
	TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir));
	DiagDirection south_dir = AxisToDiagDir(axis);
	TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir));

	TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
	TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
	if (north_tracks && south_tracks) {
		if (CheckReverseShip(v)) north_tracks = TRACK_BIT_NONE;
	}

	if (north_tracks) {
		/* Leave towards north */
		v->rotation = v->direction = DiagDirToDir(north_dir);
	} else if (south_tracks) {
		/* Leave towards south */
		v->rotation = v->direction = DiagDirToDir(south_dir);
	} else {
		/* Both ways blocked */
		return false;
	}

	v->state = AxisToTrackBits(axis);
	v->vehstatus &= ~VS_HIDDEN;

	v->cur_speed = 0;
	v->UpdateViewport(true, true);
	SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);

	v->PlayLeaveStationSound();
	VehicleServiceInDepot(v);
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
	SetWindowClassesDirty(WC_SHIPS_LIST);

	return false;
}

/**
 * Accelerates the ship towards its target speed.
 * @param v Ship to accelerate.
 * @return Number of steps to move the ship.
 */
static uint ShipAccelerate(Vehicle *v)
{
	uint speed;

	speed = std::min<uint>(v->cur_speed + 1, v->vcache.cached_max_speed);
	speed = std::min<uint>(speed, v->current_order.GetMaxSpeed() * 2);

	/* updates statusbar only if speed have changed to save CPU time */
	if (speed != v->cur_speed) {
		v->cur_speed = speed;
		SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
	}

	const uint advance_speed = v->GetAdvanceSpeed(speed);
	const uint number_of_steps = (advance_speed + v->progress) / v->GetAdvanceDistance();
	const uint remainder = (advance_speed + v->progress) % v->GetAdvanceDistance();
	assert(remainder <= std::numeric_limits<byte>::max());
	v->progress = static_cast<byte>(remainder);
	return number_of_steps;
}

/**
 * Ship arrives at a dock. If it is the first time, send out a news item.
 * @param v  Ship that arrived.
 * @param st Station being visited.
 */
static void ShipArrivesAt(const Vehicle *v, Station *st)
{
	/* Check if station was ever visited before */
	if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
		st->had_vehicle_of_type |= HVOT_SHIP;

		SetDParam(0, st->index);
		AddVehicleNewsItem(
			STR_NEWS_FIRST_SHIP_ARRIVAL,
			(v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
			v->index,
			st->index
		);
		AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
		Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
	}
}


/**
 * Runs the pathfinder to choose a track to continue along.
 *
 * @param v Ship to navigate
 * @param tile Tile, the ship is about to enter
 * @param enterdir Direction of entering
 * @param tracks Available track choices on \a tile
 * @return Track to choose, or INVALID_TRACK when to reverse.
 */
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
{
	assert(IsValidDiagDirection(enterdir));

	bool path_found = true;
	Track track;

	if (v->dest_tile == 0) {
		/* No destination, don't invoke pathfinder. */
		track = TrackBitsToTrack(v->state);
		if (!IsDiagonalTrack(track)) track = TrackToOppositeTrack(track);
		if (!HasBit(tracks, track)) track = FindFirstTrack(tracks);
		path_found = false;
	} else {
		/* Attempt to follow cached path. */
		if (!v->path.empty()) {
			track = TrackdirToTrack(v->path.front());

			if (HasBit(tracks, track)) {
				v->path.pop_front();
				/* HandlePathfindResult() is not called here because this is not a new pathfinder result. */
				return track;
			}

			/* Cached path is invalid so continue with pathfinder. */
			v->path.clear();
		}

		switch (_settings_game.pf.pathfinder_for_ships) {
			case VPF_NPF: track = NPFShipChooseTrack(v, path_found); break;
			case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found, v->path); break;
			default: NOT_REACHED();
		}
	}

	v->HandlePathfindingResult(path_found);
	return track;
}

/**
 * Get the available water tracks on a tile for a ship entering a tile.
 * @param tile The tile about to enter.
 * @param dir The entry direction.
 * @return The available trackbits on the next tile.
 */
static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
{
	TrackBits tracks = GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);

	return tracks;
}

/** Structure for ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track. */
struct ShipSubcoordData {
	byte x_subcoord; ///< New X sub-coordinate on the new tile
	byte y_subcoord; ///< New Y sub-coordinate on the new tile
	Direction dir;   ///< New Direction to move in on the new track
};
/** Ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track.
 * Array indexes are Diagdir, Track.
 * There will always be three possible tracks going into an adjacent tile via a Diagdir,
 * so each Diagdir sub-array will have three valid and three invalid structures per Track.
 */
static const ShipSubcoordData _ship_subcoord[DIAGDIR_END][TRACK_END] = {
	// DIAGDIR_NE
	{
		{15,  8, DIR_NE},      // TRACK_X
		{ 0,  0, INVALID_DIR}, // TRACK_Y
		{ 0,  0, INVALID_DIR}, // TRACK_UPPER
		{15,  8, DIR_E},       // TRACK_LOWER
		{15,  7, DIR_N},       // TRACK_LEFT
		{ 0,  0, INVALID_DIR}, // TRACK_RIGHT
	},
	// DIAGDIR_SE
	{
		{ 0,  0, INVALID_DIR}, // TRACK_X
		{ 8,  0, DIR_SE},      // TRACK_Y
		{ 7,  0, DIR_E},       // TRACK_UPPER
		{ 0,  0, INVALID_DIR}, // TRACK_LOWER
		{ 8,  0, DIR_S},       // TRACK_LEFT
		{ 0,  0, INVALID_DIR}, // TRACK_RIGHT
	},
	// DIAGDIR_SW
	{
		{ 0,  8, DIR_SW},      // TRACK_X
		{ 0,  0, INVALID_DIR}, // TRACK_Y
		{ 0,  7, DIR_W},       // TRACK_UPPER
		{ 0,  0, INVALID_DIR}, // TRACK_LOWER
		{ 0,  0, INVALID_DIR}, // TRACK_LEFT
		{ 0,  8, DIR_S},       // TRACK_RIGHT
	},
	// DIAGDIR_NW
	{
		{ 0,  0, INVALID_DIR}, // TRACK_X
		{ 8, 15, DIR_NW},      // TRACK_Y
		{ 0,  0, INVALID_DIR}, // TRACK_UPPER
		{ 8, 15, DIR_W},       // TRACK_LOWER
		{ 0,  0, INVALID_DIR}, // TRACK_LEFT
		{ 7, 15, DIR_N},       // TRACK_RIGHT
	}
};

/**
 * Test if a ship is in the centre of a lock and should move up or down.
 * @param v Ship being tested.
 * @return 0 if ship is not moving in lock, or -1 to move down, 1 to move up.
 */
static int ShipTestUpDownOnLock(const Ship *v)
{
	/* Suitable tile? */
	if (!IsTileType(v->tile, MP_WATER) || !IsLock(v->tile) || GetLockPart(v->tile) != LOCK_PART_MIDDLE) return 0;

	/* Must be at the centre of the lock */
	if ((v->x_pos & 0xF) != 8 || (v->y_pos & 0xF) != 8) return 0;

	DiagDirection diagdir = GetInclinedSlopeDirection(GetTileSlope(v->tile));
	assert(IsValidDiagDirection(diagdir));

	if (DirToDiagDir(v->direction) == diagdir) {
		/* Move up */
		return (v->z_pos < GetTileMaxZ(v->tile) * (int)TILE_HEIGHT) ? 1 : 0;
	} else {
		/* Move down */
		return (v->z_pos > GetTileZ(v->tile) * (int)TILE_HEIGHT) ? -1 : 0;
	}
}

/**
 * Test and move a ship up or down in a lock.
 * @param v Ship to move.
 * @return true iff ship is moving up or down in a lock.
 */
static bool ShipMoveUpDownOnLock(Ship *v)
{
	/* Moving up/down through lock */
	int dz = ShipTestUpDownOnLock(v);
	if (dz == 0) return false;

	if (v->cur_speed != 0) {
		v->cur_speed = 0;
		SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
	}

	if ((v->tick_counter & 7) == 0) {
		v->z_pos += dz;
		v->UpdatePosition();
		v->UpdateViewport(true, true);
	}

	return true;
}

/**
 * Test if a tile is a docking tile for the given station.
 * @param tile Docking tile to test.
 * @param station Destination station.
 * @return true iff docking tile is next to station.
 */
bool IsShipDestinationTile(TileIndex tile, StationID station)
{
	assert(IsDockingTile(tile));
	/* Check each tile adjacent to docking tile. */
	for (DiagDirection d = DIAGDIR_BEGIN; d != DIAGDIR_END; d++) {
		TileIndex t = tile + TileOffsByDiagDir(d);
		if (!IsValidTile(t)) continue;
		if (IsDockTile(t) && GetStationIndex(t) == station && IsDockWaterPart(t)) return true;
		if (IsTileType(t, MP_INDUSTRY)) {
			const Industry *i = Industry::GetByTile(t);
			if (i->neutral_station != nullptr && i->neutral_station->index == station) return true;
		}
		if (IsTileType(t, MP_STATION) && IsOilRig(t) && GetStationIndex(t) == station) return true;
	}
	return false;
}

static void ReverseShipIntoTrackdir(Ship *v, Trackdir trackdir)
{
	static constexpr Direction _trackdir_to_direction[] = {
		DIR_NE, DIR_SE, DIR_E, DIR_E, DIR_S, DIR_S, INVALID_DIR, INVALID_DIR,
		DIR_SW, DIR_NW, DIR_W, DIR_W, DIR_N, DIR_N, INVALID_DIR, INVALID_DIR,
	};

	v->direction = _trackdir_to_direction[trackdir];
	assert(v->direction != INVALID_DIR);
	v->state = TrackdirBitsToTrackBits(TrackdirToTrackdirBits(trackdir));

	/* Remember our current location to avoid movement glitch */
	v->rotation_x_pos = v->x_pos;
	v->rotation_y_pos = v->y_pos;
	v->cur_speed = 0;
	v->path.clear();

	v->UpdatePosition();
	v->UpdateViewport(true, true);
}

static void ReverseShip(Ship *v)
{
	v->direction = ReverseDir(v->direction);

	/* Remember our current location to avoid movement glitch */
	v->rotation_x_pos = v->x_pos;
	v->rotation_y_pos = v->y_pos;
	v->cur_speed = 0;
	v->path.clear();

	v->UpdatePosition();
	v->UpdateViewport(true, true);
}

static void ShipController(Ship *v)
{
	v->tick_counter++;
	v->current_order_time++;

	if (v->HandleBreakdown()) return;

	if (v->vehstatus & VS_STOPPED) return;

	if (ProcessOrders(v) && CheckReverseShip(v)) return ReverseShip(v);

	v->HandleLoading();

	if (v->current_order.IsType(OT_LOADING)) return;

	if (CheckShipLeaveDepot(v)) return;

	v->ShowVisualEffect();

	/* Rotating on spot */
	if (v->direction != v->rotation) {
		if ((v->tick_counter & 7) == 0) {
			DirDiff diff = DirDifference(v->direction, v->rotation);
			v->rotation = ChangeDir(v->rotation, diff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
			/* Invalidate the sprite cache direction to force recalculation of viewport */
			v->sprite_cache.last_direction = INVALID_DIR;
			v->UpdateViewport(true, true);
		}
		return;
	}

	if (ShipMoveUpDownOnLock(v)) return;

	const uint number_of_steps = ShipAccelerate(v);
	for (uint i = 0; i < number_of_steps; ++i) {
		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
		if (v->state != TRACK_BIT_WORMHOLE) {
			/* Not on a bridge */
			if (gp.old_tile == gp.new_tile) {
				/* Staying in tile */
				if (v->IsInDepot()) {
					gp.x = v->x_pos;
					gp.y = v->y_pos;
				} else {
					/* Not inside depot */
					const VehicleEnterTileStatus r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
					if (HasBit(r, VETS_CANNOT_ENTER)) return ReverseShip(v);

					/* A leave station order only needs one tick to get processed, so we can
					 * always skip ahead. */
					if (v->current_order.IsType(OT_LEAVESTATION)) {
						v->current_order.Free();
						SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
						/* Test if continuing forward would lead to a dead-end, moving into the dock. */
						const DiagDirection exitdir = VehicleExitDir(v->direction, v->state);
						const TileIndex tile = TileAddByDiagDir(v->tile, exitdir);
						if (TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0, exitdir)) == TRACK_BIT_NONE) return ReverseShip(v);
					} else if (v->dest_tile != 0) {
						/* We have a target, let's see if we reached it... */
						if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
								DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
							/* We got within 3 tiles of our target buoy, so let's skip to our
							 * next order */
							UpdateVehicleTimetable(v, true);
							v->IncrementRealOrderIndex();
							v->current_order.MakeDummy();
						} else if (v->current_order.IsType(OT_GOTO_DEPOT) &&
							v->dest_tile == gp.new_tile) {
							/* Depot orders really need to reach the tile */
							if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
								VehicleEnterDepot(v);
								return;
							}
						} else if (v->current_order.IsType(OT_GOTO_STATION) && IsDockingTile(gp.new_tile)) {
							/* Process station in the orderlist. */
							Station *st = Station::Get(v->current_order.GetDestination());
							if (st->docking_station.Contains(gp.new_tile) && IsShipDestinationTile(gp.new_tile, st->index)) {
								v->last_station_visited = st->index;
								if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
									ShipArrivesAt(v, st);
									v->BeginLoading();
								} else { // leave stations without docks right away
									v->current_order.MakeLeaveStation();
									v->IncrementRealOrderIndex();
								}
							}
						}
					}
				}
			} else {
				/* New tile */
				if (!IsValidTile(gp.new_tile)) return ReverseShip(v);

				const DiagDirection diagdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
				assert(diagdir != INVALID_DIAGDIR);
				const TrackBits tracks = GetAvailShipTracks(gp.new_tile, diagdir);
				if (tracks == TRACK_BIT_NONE) {
					Trackdir trackdir = INVALID_TRACKDIR;
					CheckReverseShip(v, &trackdir);
					if (trackdir == INVALID_TRACKDIR) return ReverseShip(v);
					return ReverseShipIntoTrackdir(v, trackdir);
				}

				/* Choose a direction, and continue if we find one */
				const Track track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
				if (track == INVALID_TRACK) return ReverseShip(v);

				const ShipSubcoordData &b = _ship_subcoord[diagdir][track];

				gp.x = (gp.x & ~0xF) | b.x_subcoord;
				gp.y = (gp.y & ~0xF) | b.y_subcoord;

				/* Call the landscape function and tell it that the vehicle entered the tile */
				const VehicleEnterTileStatus r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
				if (HasBit(r, VETS_CANNOT_ENTER)) return ReverseShip(v);

				if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
					v->tile = gp.new_tile;
					v->state = TrackToTrackBits(track);

					/* Update ship cache when the water class changes. Aqueducts are always canals. */
					if (GetEffectiveWaterClass(gp.old_tile) != GetEffectiveWaterClass(gp.new_tile)) v->UpdateCache();
				}

				const Direction new_direction = b.dir;
				const DirDiff diff = DirDifference(new_direction, v->direction);
				switch (diff) {
					case DIRDIFF_SAME:
					case DIRDIFF_45RIGHT:
					case DIRDIFF_45LEFT:
						/* Continue at speed */
						v->rotation = v->direction = new_direction;
						break;

					default:
						/* Stop for rotation */
						v->cur_speed = 0;
						v->direction = new_direction;
						/* Remember our current location to avoid movement glitch */
						v->rotation_x_pos = v->x_pos;
						v->rotation_y_pos = v->y_pos;
						break;
				}
			}
		} else {
			/* On a bridge */
			if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
				v->x_pos = gp.x;
				v->y_pos = gp.y;
				v->UpdatePosition();
				if ((v->vehstatus & VS_HIDDEN) == 0) v->Vehicle::UpdateViewport(true);
				return;
			}

			/* Ship is back on the bridge head, we need to consume its path
			 * cache entry here as we didn't have to choose a ship track. */
			if (!v->path.empty()) v->path.pop_front();
		}

		/* update image of ship, as well as delta XY */
		v->x_pos = gp.x;
		v->y_pos = gp.y;

		v->UpdatePosition();
		v->UpdateViewport(true, true);
	}
}

bool Ship::Tick()
{
	PerformanceAccumulator framerate(PFE_GL_SHIPS);

	if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;

	ShipController(this);

	return true;
}

void Ship::SetDestTile(TileIndex tile)
{
	if (tile == this->dest_tile) return;
	this->path.clear();
	this->dest_tile = tile;
}

/**
 * Build a ship.
 * @param flags    type of operation.
 * @param tile     tile of the depot where ship is built.
 * @param e        the engine to build.
 * @param[out] ret the vehicle that has been built.
 * @return the cost of this operation or an error.
 */
CommandCost CmdBuildShip(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret)
{
	tile = GetShipDepotNorthTile(tile);
	if (flags & DC_EXEC) {
		int x;
		int y;

		const ShipVehicleInfo *svi = &e->u.ship;

		Ship *v = new Ship();
		*ret = v;

		v->owner = _current_company;
		v->tile = tile;
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
		v->x_pos = x;
		v->y_pos = y;
		v->z_pos = GetSlopePixelZ(x, y);

		v->UpdateDeltaXY();
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;

		v->spritenum = svi->image_index;
		v->cargo_type = e->GetDefaultCargoType();
		v->cargo_cap = svi->capacity;
		v->refit_cap = 0;

		v->last_station_visited = INVALID_STATION;
		v->last_loading_station = INVALID_STATION;
		v->engine_type = e->index;

		v->reliability = e->reliability;
		v->reliability_spd_dec = e->reliability_spd_dec;
		v->max_age = e->GetLifeLengthInDays();

		v->state = TRACK_BIT_DEPOT;

		v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
		v->date_of_last_service = TimerGameCalendar::date;
		v->date_of_last_service_newgrf = TimerGameCalendar::date;
		v->build_year = TimerGameCalendar::year;
		v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
		v->random_bits = Random();

		v->UpdateCache();

		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
		v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);

		v->InvalidateNewGRFCacheOfChain();

		v->cargo_cap = e->DetermineCapacity(v);

		v->InvalidateNewGRFCacheOfChain();

		v->UpdatePosition();
	}

	return CommandCost();
}

ClosestDepot Ship::FindClosestDepot()
{
	const Depot *depot = FindClosestShipDepot(this, 0);
	if (depot == nullptr) return ClosestDepot();

	return ClosestDepot(depot->xy, depot->index);
}