Files @ r17055:2989afdad450
Branch filter:

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

smatz
(svn r21802) -Cleanup (r2600): remove unused _vehicle_id_ctr_day
  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
r5584:545d748cc681
r5584:545d748cc681
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
r8116:df67d3c5e4fd
r8763:c2c776621d56
r10208:ef8fcc3dc4ca
r13828:41995609971a
r8962:cfbdc736c8db
r13825:5de0a5d39b9e
r5584:545d748cc681
r5584:545d748cc681
r13825:5de0a5d39b9e
r5584:545d748cc681
r5972:0afe141fca29
r8114:866ed489ed98
r8131:7a50db7be0ff
r8131:7a50db7be0ff
r8140:9424f012f6a2
r8144:1432edd15267
r8157:cf41aa22cd09
r10696:8dfe83e30d01
r13825:5de0a5d39b9e
r12222:eb1f720072c3
r14248:a9050881acd7
r14248:a9050881acd7
r8211:7dc9fca2785c
r8264:d493cb51fe8a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8616:656db5986c9e
r5584:545d748cc681
r8616:656db5986c9e
r5584:545d748cc681
r5584:545d748cc681
r9022:5be8b7703ae9
r5584:545d748cc681
r13208:40382e8abf19
r13208:40382e8abf19
r5584:545d748cc681
r5584:545d748cc681
r9022:5be8b7703ae9
r9022:5be8b7703ae9
r5584:545d748cc681
r13208:40382e8abf19
r5584:545d748cc681
r9022:5be8b7703ae9
r12015:489eee6f86b7
r9022:5be8b7703ae9
r9022:5be8b7703ae9
r14309:f445232b17f6
r9022:5be8b7703ae9
r13589:d06fa116bfaa
r13589:d06fa116bfaa
r13589:d06fa116bfaa
r13589:d06fa116bfaa
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r15610:623a23fb6560
r5972:0afe141fca29
r6420:01087f989fd1
r6420:01087f989fd1
r5972:0afe141fca29
r5972:0afe141fca29
r5972:0afe141fca29
r10056:7a18efcd8edb
r5972:0afe141fca29
r5972:0afe141fca29
r5972:0afe141fca29
r5972:0afe141fca29
r5972:0afe141fca29
r9022:5be8b7703ae9
r5584:545d748cc681
r9022:5be8b7703ae9
r5584:545d748cc681
r5584:545d748cc681
r9022:5be8b7703ae9
r9022:5be8b7703ae9
r5584:545d748cc681
r12159:d9511457307a
r5584:545d748cc681
r9022:5be8b7703ae9
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14021:1aae6a0ffa3b
r5584:545d748cc681
r13828:41995609971a
r10647:62911ec68e89
r10647:62911ec68e89
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r14021:1aae6a0ffa3b
r5584:545d748cc681
r8554:399ea60cfafc
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
r7490:4e86e893fa7f
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r13935:e0b7841f69a5
r14021:1aae6a0ffa3b
r5584:545d748cc681
r14021:1aae6a0ffa3b
r8836:911846a8cfa6
r8836:911846a8cfa6
r13024:48c81d0b078a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8853:c56f6dec859b
r5584:545d748cc681
r13024:48c81d0b078a
r5584:545d748cc681
r5584:545d748cc681
r16372:76fe3abb2725
r16372:76fe3abb2725
r16397:a0320c7457c2
r16502:5d6ae9da514c
r16502:5d6ae9da514c
r16372:76fe3abb2725
r16372:76fe3abb2725
r10875:1cfbd5e0cc73
r10875:1cfbd5e0cc73
r13731:60d669a7b9e1
r13731:60d669a7b9e1
r15753:7a5fae91288a
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
r10875:1cfbd5e0cc73
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
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r11971:ac7eb21a00e4
r6553:04028e73a0f7
r5584:545d748cc681
r12656:6db5e45ad71c
r16422:45fe03d3bf62
r5584:545d748cc681
r5584:545d748cc681
r6593:469af92ae569
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6593:469af92ae569
r6593:469af92ae569
r6593:469af92ae569
r6593:469af92ae569
r6593:469af92ae569
r8827:6ee5217ef94b
r5584:545d748cc681
r8830:3bad3e96d573
r8830:3bad3e96d573
r11917:612c11f7ab47
r10488:cbe4b3cd8549
r8827:6ee5217ef94b
r8827:6ee5217ef94b
r11861:e9322118cf50
r8827:6ee5217ef94b
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6558:469828caa298
r5584:545d748cc681
r6558:469828caa298
r5584:545d748cc681
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r5584:545d748cc681
r5584:545d748cc681
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r6558:469828caa298
r8793:779ed14d9cc0
r8793:779ed14d9cc0
r8793:779ed14d9cc0
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11971:ac7eb21a00e4
r5584:545d748cc681
r7490:4e86e893fa7f
r5584:545d748cc681
r9045:56afeadb0fbc
r9045:56afeadb0fbc
r5584:545d748cc681
r9045:56afeadb0fbc
r13826:2077419a88c9
r9045:56afeadb0fbc
r9045:56afeadb0fbc
r13826:2077419a88c9
r9045:56afeadb0fbc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9045:56afeadb0fbc
r11971:ac7eb21a00e4
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15484:b055edb27a71
r15484:b055edb27a71
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r13024:48c81d0b078a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r16372:76fe3abb2725
r5584:545d748cc681
r11363:6906c490a00e
r5584:545d748cc681
r5584:545d748cc681
r15542:f6c6d0f8689e
r13024:48c81d0b078a
r15542:f6c6d0f8689e
r5584:545d748cc681
r5584:545d748cc681
r15426:38ea83bb4f51
r15426:38ea83bb4f51
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
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
r10207:a1fc2f2a33db
r5584:545d748cc681
r10123:310b4a36ab6c
r10123:310b4a36ab6c
r10696:8dfe83e30d01
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r15610:623a23fb6560
r5584:545d748cc681
r15613:193c12018337
r15613:193c12018337
r16768:069c18958074
r5584:545d748cc681
r8554:399ea60cfafc
r8554:399ea60cfafc
r16768:069c18958074
r16768:069c18958074
r9413:fcf267325763
r16768:069c18958074
r16768:069c18958074
r16768:069c18958074
r13827:a88b85af5397
r5584:545d748cc681
r16768:069c18958074
r16768:069c18958074
r16768:069c18958074
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5587:034e5e185dc2
r5587:034e5e185dc2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5587:034e5e185dc2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5587:034e5e185dc2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r13826:2077419a88c9
r5584:545d748cc681
r13826:2077419a88c9
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6491:6b6c19f090e1
r5584:545d748cc681
r6491:6b6c19f090e1
r5584:545d748cc681
r6491:6b6c19f090e1
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11971:ac7eb21a00e4
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5587:034e5e185dc2
r5587:034e5e185dc2
r5584:545d748cc681
r5584:545d748cc681
r6980:3b43790d6e21
r5584:545d748cc681
r15947:6f1c6a44a807
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8827:6ee5217ef94b
r6594:cd6701cee0ee
r5584:545d748cc681
r8836:911846a8cfa6
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r16502:5d6ae9da514c
r16502:5d6ae9da514c
r5584:545d748cc681
r5584:545d748cc681
r6153:c45b80eecb6a
r11971:ac7eb21a00e4
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r5584:545d748cc681
r9490:5cf190d39c21
r11356:ae9277304002
r9490:5cf190d39c21
r9490:5cf190d39c21
r13024:48c81d0b078a
r9490:5cf190d39c21
r9490:5cf190d39c21
r12351:edd77a5b715f
r9490:5cf190d39c21
r9490:5cf190d39c21
r11356:ae9277304002
r9490:5cf190d39c21
r11861:e9322118cf50
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r5584:545d748cc681
r9490:5cf190d39c21
r11917:612c11f7ab47
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r11861:e9322118cf50
r9490:5cf190d39c21
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r11971:ac7eb21a00e4
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r5584:545d748cc681
r5584:545d748cc681
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r9490:5cf190d39c21
r11321:15f691941b01
r9490:5cf190d39c21
r5994:2d36164769d3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12656:6db5e45ad71c
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11965:2b94ac4aa35a
r5584:545d748cc681
r8556:c5860d29500d
r8556:c5860d29500d
r7135:3964060426dc
r11965:2b94ac4aa35a
r11965:2b94ac4aa35a
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r15610:623a23fb6560
r15839:2b7528d029e3
r15839:2b7528d029e3
r15839:2b7528d029e3
r15839:2b7528d029e3
r15839:2b7528d029e3
r15839:2b7528d029e3
r5584:545d748cc681
r15839:2b7528d029e3
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
r6491:6b6c19f090e1
r5584:545d748cc681
r6558:469828caa298
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11188:143f3c64befd
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15839:2b7528d029e3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12160:b49804528c72
r5584:545d748cc681
r5584:545d748cc681
r11971:ac7eb21a00e4
r5584:545d748cc681
r12027:83216fa52f8f
r5584:545d748cc681
r5584:545d748cc681
r11898:9a83f7c7f3a1
r5584:545d748cc681
r5584:545d748cc681
r16372:76fe3abb2725
r16372:76fe3abb2725
r7931:4c17a74c399e
r6176:e229e61b4324
r12068:a96f28ffc328
r12068:a96f28ffc328
r13378:d77630912fbb
r6608:f2d13a751f0b
r12068:a96f28ffc328
r12068:a96f28ffc328
r11321:15f691941b01
r5584:545d748cc681
r5584:545d748cc681
r15839:2b7528d029e3
r5584:545d748cc681
r5584:545d748cc681
r8890:02179c54681e
r8890:02179c54681e
r14021:1aae6a0ffa3b
r8890:02179c54681e
r8890:02179c54681e
r8890:02179c54681e
r8890:02179c54681e
r8890:02179c54681e
r8890:02179c54681e
r8890:02179c54681e
r8890:02179c54681e
/* $Id$ */

/*
 * 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 "command_func.h"
#include "news_func.h"
#include "company_func.h"
#include "pathfinder/npf/npf_func.h"
#include "depot_base.h"
#include "station_base.h"
#include "vehicle_gui.h"
#include "newgrf_engine.h"
#include "pathfinder/yapf/yapf.h"
#include "newgrf_sound.h"
#include "spritecache.h"
#include "strings_func.h"
#include "functions.h"
#include "window_func.h"
#include "date_func.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "pathfinder/opf/opf_ship.h"
#include "landscape_type.h"
#include "engine_base.h"
#include "company_base.h"

#include "table/strings.h"

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

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

static SpriteID GetShipIcon(EngineID engine)
{
	const Engine *e = Engine::Get(engine);
	uint8 spritenum = e->u.ship.image_index;

	if (is_custom_sprite(spritenum)) {
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
		if (sprite != 0) return sprite;

		spritenum = e->original_image_index;
	}

	return DIR_W + _ship_sprites[spritenum];
}

void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
{
	SpriteID sprite = GetShipIcon(engine);
	const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
	preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
	DrawSprite(sprite, pal, preferred_x, y);
}

/**
 * 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 width The width of the sprite
 * @param height The height of the sprite
 */
void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
{
	const Sprite *spr = GetSprite(GetShipIcon(engine), ST_NORMAL);

	width  = spr->width;
	height = spr->height;
}

SpriteID Ship::GetImage(Direction direction) const
{
	uint8 spritenum = this->spritenum;

	if (is_custom_sprite(spritenum)) {
		SpriteID sprite = GetCustomVehicleSprite(this, direction);
		if (sprite != 0) return sprite;

		spritenum = Engine::Get(this->engine_type)->original_image_index;
	}

	return _ship_sprites[spritenum] + direction;
}

static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
{
	/* Find the closest depot */
	const Depot *depot;
	const Depot *best_depot = NULL;
	/* 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_ALL_DEPOTS(depot) {
		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->IsInDepot()) {
		VehicleServiceInDepot(v);
		return;
	}

	uint max_distance;
	switch (_settings_game.pf.pathfinder_for_ships) {
		case VPF_OPF:  max_distance = 12; break;
		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 == NULL) {
		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
			v->current_order.MakeDummy();
			SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
		}
		return;
	}

	v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
	v->dest_tile = depot->xy;
	SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
}

void Ship::UpdateCache()
{
	this->vcache.cached_max_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, ShipVehInfo(this->engine_type)->max_speed);

	this->UpdateVisualEffect();
}

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

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 / (DAYS_IN_YEAR * 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 his direction and assume a diagonal track */
		return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
	}

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

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

static void PlayShipSound(const Vehicle *v)
{
	if (!PlayVehicleSound(v, VSE_START)) {
		SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
	}
}

void Ship::PlayLeaveStationSound() const
{
	PlayShipSound(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 (st->dock_tile != INVALID_TILE) {
		return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
	} else {
		this->IncrementOrderIndex();
		return 0;
	}
}

void Ship::UpdateDeltaXY(Direction direction)
{
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
	static const uint32 _delta_xy_table[8] = {
		MKIT( 6,  6,  -3,  -3),
		MKIT( 6, 32,  -3, -16),
		MKIT( 6,  6,  -3,  -3),
		MKIT(32,  6, -16,  -3),
		MKIT( 6,  6,  -3,  -3),
		MKIT( 6, 32,  -3, -16),
		MKIT( 6,  6,  -3,  -3),
		MKIT(32,  6, -16,  -3),
	};
#undef MKIT

	uint32 x = _delta_xy_table[direction];
	this->x_offs        = GB(x,  0, 8);
	this->y_offs        = GB(x,  8, 8);
	this->x_extent      = GB(x, 16, 8);
	this->y_extent      = GB(x, 24, 8);
	this->z_extent      = 6;
}

static const TileIndexDiffC _ship_leave_depot_offs[] = {
	{-1,  0},
	{ 0, -1}
};

static void CheckShipLeaveDepot(Ship *v)
{
	if (!v->IsInDepot()) return;

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

	/* Check first (north) side */
	if (DiagdirReachesTracks((DiagDirection)axis) & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
		v->direction = ReverseDir(AxisToDirection(axis));
	/* Check second (south) side */
	} else if (DiagdirReachesTracks((DiagDirection)(axis + 2)) & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
		v->direction = AxisToDirection(axis);
	} else {
		return;
	}

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

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

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

static bool ShipAccelerate(Vehicle *v)
{
	uint spd;
	byte t;

	spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);

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

	/* Convert direction-indepenent speed into direction-dependent speed. (old movement method) */
	spd = v->GetOldAdvanceSpeed(spd);

	if (spd == 0) return false;
	if ((byte)++spd == 0) return true;

	v->progress = (t = v->progress) - (byte)spd;

	return (t < v->progress);
}

/**
 * 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) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
			v->index,
			st->index
		);
		AI::NewEvent(v->owner, new AIEventStationFirstVehicle(st->index, v->index));
	}
}


/**
 * returns the track to choose on the next tile, or -1 when it's better to
 * reverse. The tile given is the tile we are about to enter, enterdir is the
 * direction in which we are entering the tile
 */
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
{
	assert(IsValidDiagDirection(enterdir));

	bool path_found = true;
	Track track;
	switch (_settings_game.pf.pathfinder_for_ships) {
		case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
		case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
		case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
		default: NOT_REACHED();
	}

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

static const Direction _new_vehicle_direction_table[] = {
	DIR_N , DIR_NW, DIR_W , INVALID_DIR,
	DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
	DIR_E , DIR_SE, DIR_S
};

static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
{
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
							TileX(new_tile) - TileX(old_tile) + 1;
	assert(offs < 11 && offs != 3 && offs != 7);
	return _new_vehicle_direction_table[offs];
}

static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
{
	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
	assert(offs < 11 && offs != 3 && offs != 7);
	return _new_vehicle_direction_table[offs];
}

static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
{
	return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
}

static const byte _ship_subcoord[4][6][3] = {
	{
		{15, 8, 1},
		{ 0, 0, 0},
		{ 0, 0, 0},
		{15, 8, 2},
		{15, 7, 0},
		{ 0, 0, 0},
	},
	{
		{ 0, 0, 0},
		{ 8, 0, 3},
		{ 7, 0, 2},
		{ 0, 0, 0},
		{ 8, 0, 4},
		{ 0, 0, 0},
	},
	{
		{ 0, 8, 5},
		{ 0, 0, 0},
		{ 0, 7, 6},
		{ 0, 0, 0},
		{ 0, 0, 0},
		{ 0, 8, 4},
	},
	{
		{ 0, 0, 0},
		{ 8, 15, 7},
		{ 0, 0, 0},
		{ 8, 15, 6},
		{ 0, 0, 0},
		{ 7, 15, 0},
	}
};

static void ShipController(Ship *v)
{
	uint32 r;
	const byte *b;
	Direction dir;
	Track track;
	TrackBits tracks;

	v->tick_counter++;
	v->current_order_time++;

	if (v->HandleBreakdown()) return;

	if (v->vehstatus & VS_STOPPED) return;

	ProcessOrders(v);
	v->HandleLoading();

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

	CheckShipLeaveDepot(v);

	v->ShowVisualEffect();

	if (!ShipAccelerate(v)) return;

	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 */
				r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
				if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;

				/* 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, VVW_WIDGET_START_STOP_VEH);
				} 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->IncrementOrderIndex();
						v->current_order.MakeDummy();
					} else {
						/* Non-buoy orders really need to reach the tile */
						if (v->dest_tile == gp.new_tile) {
							if (v->current_order.IsType(OT_GOTO_DEPOT)) {
								if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
									VehicleEnterDepot(v);
									return;
								}
							} else if (v->current_order.IsType(OT_GOTO_STATION)) {
								v->last_station_visited = v->current_order.GetDestination();

								/* Process station in the orderlist. */
								Station *st = Station::Get(v->current_order.GetDestination());
								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 aways
									v->current_order.MakeLeaveStation();
									v->IncrementOrderIndex();
								}
							}
						}
					}
				}
			}
		} else {
			DiagDirection diagdir;
			/* New tile */
			if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
				goto reverse_direction;
			}

			dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
			assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
			diagdir = DirToDiagDir(dir);
			tracks = GetAvailShipTracks(gp.new_tile, diagdir);
			if (tracks == TRACK_BIT_NONE) goto reverse_direction;

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

			b = _ship_subcoord[diagdir][track];

			gp.x = (gp.x & ~0xF) | b[0];
			gp.y = (gp.y & ~0xF) | b[1];

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

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

			v->direction = (Direction)b[2];
		}
	} 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;
			VehicleMove(v, !(v->vehstatus & VS_HIDDEN));
			return;
		}
	}

	/* update image of ship, as well as delta XY */
	dir = ShipGetNewDirection(v, gp.x, gp.y);
	v->x_pos = gp.x;
	v->y_pos = gp.y;
	v->z_pos = GetSlopeZ(gp.x, gp.y);

getout:
	v->UpdateViewport(true, true);
	return;

reverse_direction:
	dir = ReverseDir(v->direction);
	v->direction = dir;
	goto getout;
}

bool Ship::Tick()
{
	if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;

	ShipController(this);

	return true;
}

/**
 * Build a ship.
 * @param tile     tile of the depot where ship is built.
 * @param flags    type of operation.
 * @param e        the engine to build.
 * @param data     unused.
 * @param ret[out] the vehicle that has been built.
 * @return the cost of this operation or an error.
 */
CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, 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 = GetSlopeZ(x, y);

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

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

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

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

		v->state = TRACK_BIT_DEPOT;

		v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_ships;
		v->date_of_last_service = _date;
		v->build_year = _cur_year;
		v->cur_image = SPR_IMG_QUERY;
		v->random_bits = VehicleRandomBits();

		v->UpdateCache();

		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);

		v->InvalidateNewGRFCacheOfChain();

		v->cargo_cap = GetVehicleCapacity(v);

		v->InvalidateNewGRFCacheOfChain();

		VehicleMove(v, false);
	}

	return CommandCost();
}

bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
{
	const Depot *depot = FindClosestShipDepot(this, 0);

	if (depot == NULL) return false;

	if (location    != NULL) *location    = depot->xy;
	if (destination != NULL) *destination = depot->index;

	return true;
}