Files @ r10208:ef8fcc3dc4ca
Branch filter:

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

rubidium
(svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
  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
r5584:545d748cc681
r5584:545d748cc681
r9111:983de9c5a848
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10208:ef8fcc3dc4ca
r10208:ef8fcc3dc4ca
r8116:df67d3c5e4fd
r8763:c2c776621d56
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5854:d116ce6001d7
r5584:545d748cc681
r9070:e059c65164f3
r6643:fa000ce7441e
r8114:866ed489ed98
r8225:491804378826
r8131:7a50db7be0ff
r8131:7a50db7be0ff
r8140:9424f012f6a2
r8212:13ec7a6b1407
r8214:9a3935f9ef4e
r8270:c2ec53375c9d
r8847:d0a2cc468a55
r9070:e059c65164f3
r9211:ed60710b275b
r9417:af83f94fd11e
r5584:545d748cc681
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r9070:e059c65164f3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r10207:a1fc2f2a33db
r9220:b7dc98e0e246
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r10207:a1fc2f2a33db
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r9211:ed60710b275b
r8221:3d4e594201cd
r8221:3d4e594201cd
r9070:e059c65164f3
r9070:e059c65164f3
r8221:3d4e594201cd
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r8221:3d4e594201cd
r8221:3d4e594201cd
r8221:3d4e594201cd
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r8258:08100da56269
r8258:08100da56269
r8258:08100da56269
r8258:08100da56269
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9468:a092e2eb1209
r9468:a092e2eb1209
r9468:a092e2eb1209
r9468:a092e2eb1209
r9468:a092e2eb1209
r9468:a092e2eb1209
r9468:a092e2eb1209
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6606:113b1820d4cb
r10207:a1fc2f2a33db
r9070:e059c65164f3
r8509:7aa0def85795
r8509:7aa0def85795
r6606:113b1820d4cb
r10207:a1fc2f2a33db
r6606:113b1820d4cb
r6606:113b1820d4cb
r6606:113b1820d4cb
r6606:113b1820d4cb
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9413:fcf267325763
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5944:2c68b22d94bf
r5944:2c68b22d94bf
r5584:545d748cc681
r5584:545d748cc681
r5944:2c68b22d94bf
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r6125:eb40461cb765
r6125:eb40461cb765
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
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
r9468:a092e2eb1209
r5584:545d748cc681
r9468:a092e2eb1209
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9413:fcf267325763
r5584:545d748cc681
r6125:eb40461cb765
r9413:fcf267325763
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r10023:75762313594d
r10023:75762313594d
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10023:75762313594d
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5823:af534354d69d
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r6259:e2dba394134b
r5823:af534354d69d
r5823:af534354d69d
r5823:af534354d69d
r10207:a1fc2f2a33db
r6685:d870a5ede2e2
r10207:a1fc2f2a33db
r5823:af534354d69d
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5944:2c68b22d94bf
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r8572:dcba9a2313b3
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r8633:b5abb1b3589c
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r6174:4c8d6f811300
r6174:4c8d6f811300
r10207:a1fc2f2a33db
r6174:4c8d6f811300
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r6174:4c8d6f811300
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r6481:534c9f0317ec
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6943:a727ca1f1062
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r6950:1a54b1afb12a
r5584:545d748cc681
r5584:545d748cc681
r9199:7d9724de3af0
r9199:7d9724de3af0
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r9070:e059c65164f3
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r6174:4c8d6f811300
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r6259:e2dba394134b
r6259:e2dba394134b
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6174:4c8d6f811300
r5944:2c68b22d94bf
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r8220:d02d1c33cbc9
r6125:eb40461cb765
r9070:e059c65164f3
r5823:af534354d69d
r10207:a1fc2f2a33db
r8220:d02d1c33cbc9
r6686:950533a758c0
r10207:a1fc2f2a33db
r6686:950533a758c0
r9199:7d9724de3af0
r9199:7d9724de3af0
r9199:7d9724de3af0
r9234:c3f7730d9a9b
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r8401:6c59045c24ab
r8401:6c59045c24ab
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r6174:4c8d6f811300
r10207:a1fc2f2a33db
r6174:4c8d6f811300
r5584:545d748cc681
r6125:eb40461cb765
r9070:e059c65164f3
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7097:1aa026536399
r7097:1aa026536399
r7097:1aa026536399
r7097:1aa026536399
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r7097:1aa026536399
r7097:1aa026536399
r7097:1aa026536399
r7097:1aa026536399
r7097:1aa026536399
r7097:1aa026536399
r7097:1aa026536399
r5584:545d748cc681
r5584:545d748cc681
r6481:534c9f0317ec
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6943:a727ca1f1062
r5584:545d748cc681
r9914:213bce2115cb
r10148:ec65442a5187
r10148:ec65442a5187
r5584:545d748cc681
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r7097:1aa026536399
r5584:545d748cc681
r8258:08100da56269
r8258:08100da56269
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r10148:ec65442a5187
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6950:1a54b1afb12a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8479:2768e655b413
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r8479:2768e655b413
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r8479:2768e655b413
r5584:545d748cc681
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r6125:eb40461cb765
r10207:a1fc2f2a33db
r5584:545d748cc681
r6530:7940d97af96d
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r6530:7940d97af96d
r6530:7940d97af96d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
r6505:a9483158c698
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
r10207:a1fc2f2a33db
r5584:545d748cc681
r5823:af534354d69d
r10207:a1fc2f2a33db
r8258:08100da56269
r5584:545d748cc681
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r6247:96e840dbefcc
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r5584:545d748cc681
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r10207:a1fc2f2a33db
r9070:e059c65164f3
r10207:a1fc2f2a33db
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r8258:08100da56269
r5584:545d748cc681
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r8258:08100da56269
r8258:08100da56269
r8258:08100da56269
r9070:e059c65164f3
r8258:08100da56269
r9070:e059c65164f3
r8258:08100da56269
r8258:08100da56269
r5584:545d748cc681
r5584:545d748cc681
r5587:034e5e185dc2
r5584:545d748cc681
r10127:f89df6b20b16
r5584:545d748cc681
/* $Id$ */

/** @file engine.cpp Base for all engine handling. */

#include "stdafx.h"
#include "openttd.h"
#include "debug.h"
#include "company_base.h"
#include "company_func.h"
#include "command_func.h"
#include "news_func.h"
#include "saveload.h"
#include "variables.h"
#include "train.h"
#include "aircraft.h"
#include "newgrf_cargo.h"
#include "newgrf_engine.h"
#include "group.h"
#include "strings_func.h"
#include "gfx_func.h"
#include "functions.h"
#include "window_func.h"
#include "date_func.h"
#include "autoreplace_gui.h"
#include "string_func.h"
#include "settings_type.h"
#include "oldpool_func.h"
#include "core/alloc_func.hpp"
#include "vehicle_func.h"
#include <map>

#include "table/strings.h"
#include "table/engines.h"

DEFINE_OLD_POOL_GENERIC(Engine, Engine)

enum {
	YEAR_ENGINE_AGING_STOPS = 2050,
};


/** Number of engines of each vehicle type in original engine data */
const uint8 _engine_counts[4] = {
	lengthof(_orig_rail_vehicle_info),
	lengthof(_orig_road_vehicle_info),
	lengthof(_orig_ship_vehicle_info),
	lengthof(_orig_aircraft_vehicle_info),
};

/** Offset of the first engine of each vehicle type in original engine data */
const uint8 _engine_offsets[4] = {
	0,
	lengthof(_orig_rail_vehicle_info),
	lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
	lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
};

Engine::Engine() :
	name(NULL),
	overrides_count(0),
	overrides(NULL)
{
}

Engine::Engine(VehicleType type, EngineID base)
{
	this->type = type;
	this->internal_id = base;
	this->list_position = base;

	/* Check if this base engine is within the original engine data range */
	if (base >= _engine_counts[type]) {
		/* Mark engine as valid anyway */
		this->info.climates = 0x80;
		return;
	}

	/* Copy the original engine info for this slot */
	this->info = _orig_engine_info[_engine_offsets[type] + base];

	/* Copy the original engine data for this slot */
	switch (type) {
		default: NOT_REACHED();

		case VEH_TRAIN:
			this->u.rail = _orig_rail_vehicle_info[base];
			this->image_index = this->u.rail.image_index;
			this->info.string_id = STR_8000_KIRBY_PAUL_TANK_STEAM + base;
			break;

		case VEH_ROAD:
			this->u.road = _orig_road_vehicle_info[base];
			this->image_index = this->u.road.image_index;
			this->info.string_id = STR_8074_MPS_REGAL_BUS + base;
			break;

		case VEH_SHIP:
			this->u.ship = _orig_ship_vehicle_info[base];
			this->image_index = this->u.ship.image_index;
			this->info.string_id = STR_80CC_MPS_OIL_TANKER + base;
			break;

		case VEH_AIRCRAFT:
			this->u.air = _orig_aircraft_vehicle_info[base];
			this->image_index = this->u.air.image_index;
			this->info.string_id = STR_80D7_SAMPSON_U52 + base;
			break;
	}
}

Engine::~Engine()
{
	UnloadWagonOverrides(this);
	free(this->name);
}

/** Sets cached values in Company::num_vehicles and Group::num_vehicles
 */
void SetCachedEngineCounts()
{
	uint engines = GetEnginePoolSize();

	/* Set up the engine count for all companies */
	Company *c;
	FOR_ALL_COMPANIES(c) {
		free(c->num_engines);
		c->num_engines = CallocT<EngineID>(engines);
	}

	/* Recalculate */
	Group *g;
	FOR_ALL_GROUPS(g) {
		free(g->num_engines);
		g->num_engines = CallocT<EngineID>(engines);
	}

	const Vehicle *v;
	FOR_ALL_VEHICLES(v) {
		if (!IsEngineCountable(v)) continue;

		assert(v->engine_type < engines);

		GetCompany(v->owner)->num_engines[v->engine_type]++;

		if (v->group_id == DEFAULT_GROUP) continue;

		g = GetGroup(v->group_id);
		assert(v->type == g->vehicle_type);
		assert(v->owner == g->owner);

		g->num_engines[v->engine_type]++;
	}
}

void SetupEngines()
{
	_Engine_pool.CleanPool();
	_Engine_pool.AddBlockToPool();

	for (uint i = 0; i < lengthof(_orig_rail_vehicle_info); i++) new Engine(VEH_TRAIN, i);
	for (uint i = 0; i < lengthof(_orig_road_vehicle_info); i++) new Engine(VEH_ROAD, i);
	for (uint i = 0; i < lengthof(_orig_ship_vehicle_info); i++) new Engine(VEH_SHIP, i);
	for (uint i = 0; i < lengthof(_orig_aircraft_vehicle_info); i++) new Engine(VEH_AIRCRAFT, i);
}


void ShowEnginePreviewWindow(EngineID engine);

void DeleteCustomEngineNames()
{
	Engine *e;
	FOR_ALL_ENGINES(e) {
		free(e->name);
		e->name = NULL;
	}

	_vehicle_design_names &= ~1;
}

void LoadCustomEngineNames()
{
	/* XXX: not done */
	DEBUG(misc, 1, "LoadCustomEngineNames: not done");
}

/* Determine if an engine type is a wagon (and not a loco) */
static bool IsWagon(EngineID index)
{
	const Engine *e = GetEngine(index);
	return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
}

static void CalcEngineReliability(Engine *e)
{
	uint age = e->age;

	/* Check for early retirement */
	if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles) {
		int retire_early = e->info.retire_early;
		uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
		if (retire_early != 0 && age >= retire_early_max_age) {
			/* Early retirement is enabled and we're past the date... */
			e->company_avail = 0;
			AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
		}
	}

	if (age < e->duration_phase_1) {
		uint start = e->reliability_start;
		e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
	} else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles) {
		/* We are at the peak of this engines life. It will have max reliability.
		 * This is also true if the engines never expire. They will not go bad over time */
		e->reliability = e->reliability_max;
	} else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
		uint max = e->reliability_max;
		e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
	} else {
		/* time's up for this engine.
		 * We will now completely retire this design */
		e->company_avail = 0;
		e->reliability = e->reliability_final;
		/* Kick this engine out of the lists */
		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
	}
	InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
}

void StartupEngines()
{
	Engine *e;
	/* Aging of vehicles stops, so account for that when starting late */
	const Date aging_date = min(_date, ConvertYMDToDate(YEAR_ENGINE_AGING_STOPS, 0, 1));

	FOR_ALL_ENGINES(e) {
		const EngineInfo *ei = &e->info;
		uint32 r;

		e->age = 0;
		e->flags = 0;
		e->company_avail = 0;

		/* The magic value of 729 days below comes from the NewGRF spec. If the
		 * base intro date is before 1922 then the random number of days is not
		 * added. */
		r = Random();
		e->intro_date = ei->base_intro <= ConvertYMDToDate(1922, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
		if (e->intro_date <= _date) {
			e->age = (aging_date - e->intro_date) >> 5;
			e->company_avail = (CompanyMask)-1;
			e->flags |= ENGINE_AVAILABLE;
		}

		e->reliability_start = GB(r, 16, 14) + 0x7AE0;
		r = Random();
		e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
		e->reliability_final = GB(r, 16, 14) + 0x3FFF;

		r = Random();
		e->duration_phase_1 = GB(r, 0, 5) + 7;
		e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
		e->duration_phase_3 = GB(r, 9, 7) + 120;

		e->reliability_spd_dec = ei->decay_speed << 2;

		if (IsWagon(e->index)) {
			e->age = 0xFFFF;
		} else {
			CalcEngineReliability(e);
		}

		e->lifelength = ei->lifelength + _settings_game.vehicle.extend_vehicle_life;

		/* prevent certain engines from ever appearing. */
		if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
			e->flags |= ENGINE_AVAILABLE;
			e->company_avail = 0;
		}
	}

	/* Update the bitmasks for the vehicle lists */
	Company *c;
	FOR_ALL_COMPANIES(c) {
		c->avail_railtypes = GetCompanyRailtypes(c->index);
		c->avail_roadtypes = GetCompanyRoadtypes(c->index);
	}
}

static void AcceptEnginePreview(EngineID eid, CompanyID company)
{
	Engine *e = GetEngine(eid);
	Company *c = GetCompany(company);

	SetBit(e->company_avail, company);
	if (e->type == VEH_TRAIN) {
		const RailVehicleInfo *rvi = RailVehInfo(eid);

		assert(rvi->railtype < RAILTYPE_END);
		SetBit(c->avail_railtypes, rvi->railtype);
	} else if (e->type == VEH_ROAD) {
		SetBit(c->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
	}

	e->preview_company_rank = 0xFF;
	if (company == _local_company) {
		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
	}
}

static CompanyID GetBestCompany(uint8 pp)
{
	const Company *c;
	int32 best_hist;
	CompanyID best_company;
	uint mask = 0;

	do {
		best_hist = -1;
		best_company = INVALID_COMPANY;
		FOR_ALL_COMPANIES(c) {
			if (c->block_preview == 0 && !HasBit(mask, c->index) &&
					c->old_economy[0].performance_history > best_hist) {
				best_hist = c->old_economy[0].performance_history;
				best_company = c->index;
			}
		}

		if (best_company == INVALID_COMPANY) return INVALID_COMPANY;

		SetBit(mask, best_company);
	} while (--pp != 0);

	return best_company;
}

void EnginesDailyLoop()
{
	if (_cur_year >= YEAR_ENGINE_AGING_STOPS) return;

	Engine *e;
	FOR_ALL_ENGINES(e) {
		EngineID i = e->index;
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
			if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
				if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
					e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
					DeleteWindowById(WC_ENGINE_PREVIEW, i);
					e->preview_company_rank++;
				}
			} else if (e->preview_company_rank != 0xFF) {
				CompanyID best_company = GetBestCompany(e->preview_company_rank);

				if (best_company == INVALID_COMPANY) {
					e->preview_company_rank = 0xFF;
					continue;
				}

				if (!IsHumanCompany(best_company)) {
					/* XXX - TTDBUG: TTD has a bug here ???? */
					AcceptEnginePreview(i, best_company);
				} else {
					e->flags |= ENGINE_OFFER_WINDOW_OPEN;
					e->preview_wait = 20;
					if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
				}
			}
		}
	}
}

/** Accept an engine prototype. XXX - it is possible that the top-company
 * changes while you are waiting to accept the offer? Then it becomes invalid
 * @param tile unused
 * @param flags operation to perfom
 * @param p1 engine-prototype offered
 * @param p2 unused
 */
CommandCost CmdWantEnginePreview(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
	Engine *e;

	if (!IsEngineIndex(p1)) return CMD_ERROR;
	e = GetEngine(p1);
	if (GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;

	if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);

	return CommandCost();
}

StringID GetEngineCategoryName(EngineID engine);

static void NewVehicleAvailable(Engine *e)
{
	Vehicle *v;
	Company *c;
	EngineID index = e->index;

	/* In case the company didn't build the vehicle during the intro period,
	 * prevent that company from getting future intro periods for a while. */
	if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
		FOR_ALL_COMPANIES(c) {
			uint block_preview = c->block_preview;

			if (!HasBit(e->company_avail, c->index)) continue;

			/* We assume the user did NOT build it.. prove me wrong ;) */
			c->block_preview = 20;

			FOR_ALL_VEHICLES(v) {
				if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
						(v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) {
					if (v->owner == c->index && v->engine_type == index) {
						/* The user did prove me wrong, so restore old value */
						c->block_preview = block_preview;
						break;
					}
				}
			}
		}
	}

	e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
	AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);

	/* Now available for all companies */
	e->company_avail = (CompanyMask)-1;

	/* Do not introduce new rail wagons */
	if (IsWagon(index)) return;

	if (e->type == VEH_TRAIN) {
		/* maybe make another rail type available */
		RailType railtype = e->u.rail.railtype;
		assert(railtype < RAILTYPE_END);
		FOR_ALL_COMPANIES(c) SetBit(c->avail_railtypes, railtype);
	} else if (e->type == VEH_ROAD) {
		/* maybe make another road type available */
		FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
	}

	SetDParam(0, GetEngineCategoryName(index));
	SetDParam(1, index);
	AddNewsItem(STR_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, index, 0);
}

void EnginesMonthlyLoop()
{
	if (_cur_year < YEAR_ENGINE_AGING_STOPS) {
		Engine *e;
		FOR_ALL_ENGINES(e) {
			/* Age the vehicle */
			if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
				e->age++;
				CalcEngineReliability(e);
			}

			if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + 365)) {
				/* Introduce it to all companies */
				NewVehicleAvailable(e);
			} else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
				/* Introduction date has passed.. show introducing dialog to one companies. */
				e->flags |= ENGINE_EXCLUSIVE_PREVIEW;

				/* Do not introduce new rail wagons */
				if (!IsWagon(e->index))
					e->preview_company_rank = 1; // Give to the company with the highest rating.
			}
		}
	}
}

static bool IsUniqueEngineName(const char *name)
{
	char buf[512];

	const Engine *e;
	FOR_ALL_ENGINES(e) {
		SetDParam(0, e->index);
		GetString(buf, STR_ENGINE_NAME, lastof(buf));
		if (strcmp(buf, name) == 0) return false;
	}

	return true;
}

/** Rename an engine.
 * @param tile unused
 * @param flags operation to perfom
 * @param p1 engine ID to rename
 * @param p2 unused
 */
CommandCost CmdRenameEngine(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
	if (!IsEngineIndex(p1)) return CMD_ERROR;

	bool reset = StrEmpty(_cmd_text);

	if (!reset) {
		if (strlen(_cmd_text) >= MAX_LENGTH_ENGINE_NAME_BYTES) return CMD_ERROR;
		if (!IsUniqueEngineName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
	}

	if (flags & DC_EXEC) {
		Engine *e = GetEngine(p1);
		free(e->name);

		if (reset) {
			e->name = NULL;
			/* if we removed the last custom name, disable the 'Save custom names' button */
			_vehicle_design_names &= ~1;
			FOR_ALL_ENGINES(e) {
				if (e->name != NULL) {
					_vehicle_design_names |= 1;
					break;
				}
			}
		} else {
			e->name = strdup(_cmd_text);
			_vehicle_design_names |= 3;
		}

		MarkWholeScreenDirty();
	}

	return CommandCost();
}


/** Check if an engine is buildable.
 * @param engine  index of the engine to check.
 * @param type    the type the engine should be.
 * @param company index of the company.
 * @return True if an engine is valid, of the specified type, and buildable by
 *              the given company.
 */
bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
{
	/* check if it's an engine that is in the engine array */
	if (!IsEngineIndex(engine)) return false;

	const Engine *e = GetEngine(engine);

	/* check if it's an engine of specified type */
	if (e->type != type) return false;

	/* check if it's available */
	if (!HasBit(e->company_avail, company)) return false;

	if (type == VEH_TRAIN) {
		/* Check if the rail type is available to this company */
		const Company *c = GetCompany(company);
		if (!HasBit(c->avail_railtypes, RailVehInfo(engine)->railtype)) return false;
	}

	return true;
}

/** Get the default cargo type for a certain engine type
 * @param engine The ID to get the cargo for
 * @return The cargo type. CT_INVALID means no cargo capacity
 */
CargoID GetEngineCargoType(EngineID engine)
{
	assert(IsEngineIndex(engine));

	switch (GetEngine(engine)->type) {
		case VEH_TRAIN:
			if (RailVehInfo(engine)->capacity == 0) return CT_INVALID;
			return RailVehInfo(engine)->cargo_type;

		case VEH_ROAD:
			if (RoadVehInfo(engine)->capacity == 0) return CT_INVALID;
			return RoadVehInfo(engine)->cargo_type;

		case VEH_SHIP:
			if (ShipVehInfo(engine)->capacity == 0) return CT_INVALID;
			return ShipVehInfo(engine)->cargo_type;

		case VEH_AIRCRAFT:
			/* all aircraft starts as passenger planes with cargo capacity */
			return CT_PASSENGERS;

		default: NOT_REACHED(); return CT_INVALID;
	}
}

static const SaveLoad _engine_desc[] = {
	SLE_CONDVAR(Engine, intro_date,          SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
	SLE_CONDVAR(Engine, intro_date,          SLE_INT32,                  31, SL_MAX_VERSION),
	SLE_CONDVAR(Engine, age,                 SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
	SLE_CONDVAR(Engine, age,                 SLE_INT32,                  31, SL_MAX_VERSION),
	    SLE_VAR(Engine, reliability,         SLE_UINT16),
	    SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
	    SLE_VAR(Engine, reliability_start,   SLE_UINT16),
	    SLE_VAR(Engine, reliability_max,     SLE_UINT16),
	    SLE_VAR(Engine, reliability_final,   SLE_UINT16),
	    SLE_VAR(Engine, duration_phase_1,    SLE_UINT16),
	    SLE_VAR(Engine, duration_phase_2,    SLE_UINT16),
	    SLE_VAR(Engine, duration_phase_3,    SLE_UINT16),

	    SLE_VAR(Engine, lifelength,          SLE_UINT8),
	    SLE_VAR(Engine, flags,               SLE_UINT8),
	    SLE_VAR(Engine, preview_company_rank,SLE_UINT8),
	    SLE_VAR(Engine, preview_wait,        SLE_UINT8),
	SLE_CONDNULL(1, 0, 44),
	    SLE_VAR(Engine, company_avail,       SLE_UINT8),
	SLE_CONDSTR(Engine, name,                SLE_STR, 0,                 84, SL_MAX_VERSION),

	/* reserve extra space in savegame here. (currently 16 bytes) */
	SLE_CONDNULL(16, 2, SL_MAX_VERSION),

	SLE_END()
};

static std::map<EngineID, Engine> _temp_engine;

Engine *GetTempDataEngine(EngineID index)
{
	return &_temp_engine[index];
}

static void Save_ENGN()
{
	Engine *e;
	FOR_ALL_ENGINES(e) {
		SlSetArrayIndex(e->index);
		SlObject(e, _engine_desc);
	}
}

static void Load_ENGN()
{
	/* As engine data is loaded before engines are initialized we need to load
	 * this information into a temporary array. This is then copied into the
	 * engine pool after processing NewGRFs by CopyTempEngineData(). */
	int index;
	while ((index = SlIterateArray()) != -1) {
		Engine *e = GetTempDataEngine(index);
		SlObject(e, _engine_desc);
	}
}

/**
 * Copy data from temporary engine array into the real engine pool.
 */
void CopyTempEngineData()
{
	Engine *e;
	FOR_ALL_ENGINES(e) {
		if (e->index >= _temp_engine.size()) break;

		const Engine *se = GetTempDataEngine(e->index);
		e->intro_date          = se->intro_date;
		e->age                 = se->age;
		e->reliability         = se->reliability;
		e->reliability_spd_dec = se->reliability_spd_dec;
		e->reliability_start   = se->reliability_start;
		e->reliability_max     = se->reliability_max;
		e->reliability_final   = se->reliability_final;
		e->duration_phase_1    = se->duration_phase_1;
		e->duration_phase_2    = se->duration_phase_2;
		e->duration_phase_3    = se->duration_phase_3;
		e->lifelength          = se->lifelength;
		e->flags               = se->flags;
		e->preview_company_rank= se->preview_company_rank;
		e->preview_wait        = se->preview_wait;
		e->company_avail       = se->company_avail;
		if (se->name != NULL) e->name = strdup(se->name);
	}

	/* Get rid of temporary data */
	_temp_engine.clear();
}

static void Load_ENGS()
{
	/* Load old separate String ID list into a temporary array. This
	 * was always 256 entries. */
	StringID names[256];

	SlArray(names, lengthof(names), SLE_STRINGID);

	/* Copy each string into the temporary engine array. */
	for (EngineID engine = 0; engine < lengthof(names); engine++) {
		Engine *e = GetTempDataEngine(engine);
		e->name = CopyFromOldName(names[engine]);
	}
}

extern const ChunkHandler _engine_chunk_handlers[] = {
	{ 'ENGN', Save_ENGN,     Load_ENGN,     CH_ARRAY          },
	{ 'ENGS', NULL,          Load_ENGS,     CH_RIFF | CH_LAST },
};