Files @ r28396:ef195b601c72
Branch filter:

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

Peter Nelson
Fix: Pass townnameparts as parameter when testing townname based companyname. (#11685)

Length of returned string could vary and pass/fail the length check with a different
string than the final selection.
  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
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r15610:623a23fb6560
r18845:66bf168f1100
r18845:66bf168f1100
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r18294:67a72ead8996
r6849:520d3766abf2
r13176:254c27d58bd6
r13176:254c27d58bd6
r18294:67a72ead8996
r15466:2b6ac974541f
r8118:2e26e814db44
r8459:1ef090778f87
r15466:2b6ac974541f
r15950:217ba3ee892a
r15741:b6af3c5d971b
r17728:6a6e98f25cac
r17850:7726e086e93b
r18023:2bce2aecd4c8
r18617:524f2e0f54dc
r18294:67a72ead8996
r27412:e80c11e22792
r17850:7726e086e93b
r17850:7726e086e93b
r6629:10368ea45a15
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r15610:623a23fb6560
r15610:623a23fb6560
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r27737:728d55b97775
r6629:10368ea45a15
r26806:aeaf4cdeef08
r26807:8c1f4aace1b4
r26807:8c1f4aace1b4
r6629:10368ea45a15
r26807:8c1f4aace1b4
r26805:f8660101f2c2
r26807:8c1f4aace1b4
r26805:f8660101f2c2
r6629:10368ea45a15
r6629:10368ea45a15
r15610:623a23fb6560
r15610:623a23fb6560
r6629:10368ea45a15
r6629:10368ea45a15
r7873:778564304bdd
r7873:778564304bdd
r6629:10368ea45a15
r6629:10368ea45a15
r27737:728d55b97775
r6629:10368ea45a15
r26806:aeaf4cdeef08
r7874:fcd5dd06188c
r26807:8c1f4aace1b4
r26806:aeaf4cdeef08
r26806:aeaf4cdeef08
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r26807:8c1f4aace1b4
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r26807:8c1f4aace1b4
r27737:728d55b97775
r6629:10368ea45a15
r6629:10368ea45a15
r15610:623a23fb6560
r15610:623a23fb6560
r19944:25a78576fb5e
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r27737:728d55b97775
r6629:10368ea45a15
r27737:728d55b97775
r26807:8c1f4aace1b4
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r7875:5a00edb118cc
r26807:8c1f4aace1b4
r6629:10368ea45a15
r6629:10368ea45a15
r15610:623a23fb6560
r15610:623a23fb6560
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r27737:728d55b97775
r6629:10368ea45a15
r27737:728d55b97775
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r26807:8c1f4aace1b4
r6629:10368ea45a15
r6629:10368ea45a15
r26807:8c1f4aace1b4
r26807:8c1f4aace1b4
r6629:10368ea45a15
r7632:40eddc1d4853
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r26807:8c1f4aace1b4
r6629:10368ea45a15
r6629:10368ea45a15
r15610:623a23fb6560
r17771:ad36e3e3a831
r17771:ad36e3e3a831
r17771:ad36e3e3a831
r17771:ad36e3e3a831
r27737:728d55b97775
r17771:ad36e3e3a831
r26807:8c1f4aace1b4
r17771:ad36e3e3a831
r17771:ad36e3e3a831
r17771:ad36e3e3a831
r15610:623a23fb6560
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r27737:728d55b97775
r6629:10368ea45a15
r26807:8c1f4aace1b4
r6629:10368ea45a15
r6629:10368ea45a15
r15610:623a23fb6560
r15610:623a23fb6560
r19944:25a78576fb5e
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r15724:6a590b922d6c
r6629:10368ea45a15
r26807:8c1f4aace1b4
r27119:18b3e09ea8be
r6629:10368ea45a15
r6629:10368ea45a15
r6629:10368ea45a15
r27135:53b98380657f
r6629:10368ea45a15
r6629:10368ea45a15
r26806:aeaf4cdeef08
r12241:f31db06a63d4
r6629:10368ea45a15
r26806:aeaf4cdeef08
r6629:10368ea45a15
r15724:6a590b922d6c
r26807:8c1f4aace1b4
r26806:aeaf4cdeef08
r6629:10368ea45a15
r6629:10368ea45a15
r6801:3c6b56e07296
r15610:623a23fb6560
r15610:623a23fb6560
r19944:25a78576fb5e
r8511:74e4574fb8bc
r8511:74e4574fb8bc
r8511:74e4574fb8bc
r27737:728d55b97775
r8511:74e4574fb8bc
r27737:728d55b97775
r26807:8c1f4aace1b4
r8511:74e4574fb8bc
r8511:74e4574fb8bc
r26806:aeaf4cdeef08
r26806:aeaf4cdeef08
r8511:74e4574fb8bc
r8511:74e4574fb8bc
r26807:8c1f4aace1b4
r8511:74e4574fb8bc
r8511:74e4574fb8bc
r15610:623a23fb6560
r15610:623a23fb6560
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r27737:728d55b97775
r6835:9051f7024392
r6835:9051f7024392
r27737:728d55b97775
r19944:25a78576fb5e
r26807:8c1f4aace1b4
r7875:5a00edb118cc
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r23607:36c15679007d
r19944:25a78576fb5e
r23607:36c15679007d
r26807:8c1f4aace1b4
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r26807:8c1f4aace1b4
r6835:9051f7024392
r6835:9051f7024392
r15610:623a23fb6560
r19944:25a78576fb5e
r19944:25a78576fb5e
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7667:3c17bebdab13
r6835:9051f7024392
r15802:c264c6cc5624
r6835:9051f7024392
r6835:9051f7024392
r26807:8c1f4aace1b4
r7875:5a00edb118cc
r25484:c5af4712cb7f
r7875:5a00edb118cc
r7875:5a00edb118cc
r7875:5a00edb118cc
r26807:8c1f4aace1b4
r6835:9051f7024392
r6835:9051f7024392
r26807:8c1f4aace1b4
r27119:18b3e09ea8be
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r15802:c264c6cc5624
r23867:be2be375b64c
r15802:c264c6cc5624
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r26807:8c1f4aace1b4
r27119:18b3e09ea8be
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r27135:53b98380657f
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r26806:aeaf4cdeef08
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r26806:aeaf4cdeef08
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r26807:8c1f4aace1b4
r26806:aeaf4cdeef08
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r7000:2cb5bda647b1
r15610:623a23fb6560
r15950:217ba3ee892a
r19944:25a78576fb5e
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r26807:8c1f4aace1b4
r15950:217ba3ee892a
r25484:c5af4712cb7f
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r26807:8c1f4aace1b4
r27119:18b3e09ea8be
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r27036:80cf2fa850d3
r15950:217ba3ee892a
r15950:217ba3ee892a
r27036:80cf2fa850d3
r27036:80cf2fa850d3
r15950:217ba3ee892a
r15950:217ba3ee892a
r15950:217ba3ee892a
r15610:623a23fb6560
r6801:3c6b56e07296
r6801:3c6b56e07296
r17629:21e9dfd343cd
r6801:3c6b56e07296
r15613:193c12018337
r15613:193c12018337
r27737:728d55b97775
r6801:3c6b56e07296
r9413:fcf267325763
r8394:437109215fdd
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15741:b6af3c5d971b
r15741:b6af3c5d971b
r15491:843ee05eb57e
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15741:b6af3c5d971b
r15741:b6af3c5d971b
r15466:2b6ac974541f
r15747:606d1ec00369
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15741:b6af3c5d971b
r15741:b6af3c5d971b
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15741:b6af3c5d971b
r15741:b6af3c5d971b
r15466:2b6ac974541f
r15491:843ee05eb57e
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15747:606d1ec00369
r18250:596efadc4b6d
r15746:21a9eef562ba
r15746:21a9eef562ba
r15746:21a9eef562ba
r15746:21a9eef562ba
r15746:21a9eef562ba
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15734:ae8e2e185874
r15466:2b6ac974541f
r18250:596efadc4b6d
r15466:2b6ac974541f
r15466:2b6ac974541f
r15528:0c5622cb2cfa
r15466:2b6ac974541f
r15741:b6af3c5d971b
r18250:596efadc4b6d
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r15466:2b6ac974541f
r6801:3c6b56e07296
r6801:3c6b56e07296
r6801:3c6b56e07296
r6801:3c6b56e07296
r17629:21e9dfd343cd
r17629:21e9dfd343cd
r17629:21e9dfd343cd
r17629:21e9dfd343cd
r17629:21e9dfd343cd
r17853:b905f907c347
r17629:21e9dfd343cd
r17629:21e9dfd343cd
r17853:b905f907c347
r6827:5f08bab6696e
r27737:728d55b97775
r27737:728d55b97775
r6827:5f08bab6696e
r16279:c9054b28cdd6
r16279:c9054b28cdd6
r6827:5f08bab6696e
r8118:2e26e814db44
r17853:b905f907c347
r17853:b905f907c347
r8118:2e26e814db44
r11229:0d2d21cc618d
r26788:0785302ccc5b
r6827:5f08bab6696e
r8458:f7e90b7c0926
r8458:f7e90b7c0926
r13165:024ce8aa50e8
r8458:f7e90b7c0926
r8458:f7e90b7c0926
r18308:05de10e00a85
r8458:f7e90b7c0926
r8458:f7e90b7c0926
r27737:728d55b97775
r8458:f7e90b7c0926
r8458:f7e90b7c0926
r8458:f7e90b7c0926
r8459:1ef090778f87
r8459:1ef090778f87
r8459:1ef090778f87
r18260:9616113792ef
r18245:fb370bf5a3c0
r17873:62c738837ee0
r17873:62c738837ee0
r18308:05de10e00a85
r27272:bb139906001a
r8458:f7e90b7c0926
r17671:6321f2c5c4f2
r17850:7726e086e93b
r18023:2bce2aecd4c8
r18023:2bce2aecd4c8
r23607:36c15679007d
r18023:2bce2aecd4c8
r18023:2bce2aecd4c8
r27737:728d55b97775
r18023:2bce2aecd4c8
r23607:36c15679007d
r23607:36c15679007d
r18023:2bce2aecd4c8
r18023:2bce2aecd4c8
r18023:2bce2aecd4c8
r17850:7726e086e93b
r17850:7726e086e93b
r21154:68958e5391ae
r17850:7726e086e93b
r17850:7726e086e93b
r17850:7726e086e93b
r27737:728d55b97775
r17850:7726e086e93b
r17850:7726e086e93b
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r21154:68958e5391ae
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r18296:89d34cbdf3a3
r17850:7726e086e93b
r17850:7726e086e93b
r17850:7726e086e93b
r21155:4f83de14e882
r17850:7726e086e93b
r17850:7726e086e93b
r17850:7726e086e93b
r17850:7726e086e93b
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r27737:728d55b97775
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r27412:e80c11e22792
r18294:67a72ead8996
r18294:67a72ead8996
r18294:67a72ead8996
r27412:e80c11e22792
r18294:67a72ead8996
r18294:67a72ead8996
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r27737:728d55b97775
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r27737:728d55b97775
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r18301:0c1dfbaba57a
r23538:8df50944b27a
r17728:6a6e98f25cac
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r23607:36c15679007d
r23607:36c15679007d
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r23607:36c15679007d
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17671:6321f2c5c4f2
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23838:bfeaabaa7b1d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r18093:bb567a02767f
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r27737:728d55b97775
r17728:6a6e98f25cac
r23517:1a32c3c14728
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r24434:1f13cf59de8a
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23532:dc91fcd293f5
r17728:6a6e98f25cac
r24434:1f13cf59de8a
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23536:ce42deb0b32d
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23536:ce42deb0b32d
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17906:b068a0f5f28a
r17906:b068a0f5f28a
r18093:bb567a02767f
r27737:728d55b97775
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r17728:6a6e98f25cac
r17906:b068a0f5f28a
r17906:b068a0f5f28a
r27737:728d55b97775
r27737:728d55b97775
r17906:b068a0f5f28a
r27737:728d55b97775
r17906:b068a0f5f28a
r27737:728d55b97775
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17906:b068a0f5f28a
r17728:6a6e98f25cac
r27737:728d55b97775
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r18093:bb567a02767f
r27737:728d55b97775
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r18093:bb567a02767f
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
/*
 * 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 newgrf_commons.cpp Implementation of the class %OverrideManagerBase
 * and its descendance, present and future.
 */

#include "stdafx.h"
#include "debug.h"
#include "landscape.h"
#include "house.h"
#include "industrytype.h"
#include "newgrf_config.h"
#include "clear_map.h"
#include "station_map.h"
#include "tree_map.h"
#include "tunnelbridge_map.h"
#include "newgrf_object.h"
#include "genworld.h"
#include "newgrf_spritegroup.h"
#include "newgrf_text.h"
#include "company_base.h"
#include "error.h"
#include "strings_func.h"
#include "string_func.h"

#include "table/strings.h"

#include "safeguards.h"

/**
 * Constructor of generic class
 * @param offset end of original data for this entity. i.e: houses = 110
 * @param maximum of entities this manager can deal with. i.e: houses = 512
 * @param invalid is the ID used to identify an invalid entity id
 */
OverrideManagerBase::OverrideManagerBase(uint16_t offset, uint16_t maximum, uint16_t invalid)
{
	this->max_offset = offset;
	this->max_entities = maximum;
	this->invalid_id = invalid;

	this->mappings.resize(this->max_entities);
	this->entity_overrides.resize(this->max_offset);
	std::fill(this->entity_overrides.begin(), this->entity_overrides.end(), this->invalid_id);
	this->grfid_overrides.resize(this->max_offset);
}

/**
 * Since the entity IDs defined by the GRF file does not necessarily correlate
 * to those used by the game, the IDs used for overriding old entities must be
 * translated when the entity spec is set.
 * @param local_id ID in grf file
 * @param grfid  ID of the grf file
 * @param entity_type original entity type
 */
void OverrideManagerBase::Add(uint16_t local_id, uint32_t grfid, uint entity_type)
{
	assert(entity_type < this->max_offset);
	/* An override can be set only once */
	if (this->entity_overrides[entity_type] != this->invalid_id) return;
	this->entity_overrides[entity_type] = local_id;
	this->grfid_overrides[entity_type] = grfid;
}

/** Resets the mapping, which is used while initializing game */
void OverrideManagerBase::ResetMapping()
{
	std::fill(this->mappings.begin(), this->mappings.end(), EntityIDMapping{});
}

/** Resets the override, which is used while initializing game */
void OverrideManagerBase::ResetOverride()
{
	std::fill(this->entity_overrides.begin(), this->entity_overrides.end(), this->invalid_id);
	std::fill(this->grfid_overrides.begin(), this->grfid_overrides.end(), uint32_t());
}

/**
 * Return the ID (if ever available) of a previously inserted entity.
 * @param grf_local_id ID of this entity within the grfID
 * @param grfid ID of the grf file
 * @return the ID of the candidate, of the Invalid flag item ID
 */
uint16_t OverrideManagerBase::GetID(uint16_t grf_local_id, uint32_t grfid) const
{
	for (uint16_t id = 0; id < this->max_entities; id++) {
		const EntityIDMapping *map = &this->mappings[id];
		if (map->entity_id == grf_local_id && map->grfid == grfid) {
			return id;
		}
	}

	return this->invalid_id;
}

/**
 * Reserves a place in the mapping array for an entity to be installed
 * @param grf_local_id is an arbitrary id given by the grf's author.  Also known as setid
 * @param grfid is the id of the grf file itself
 * @param substitute_id is the original entity from which data is copied for the new one
 * @return the proper usable slot id, or invalid marker if none is found
 */
uint16_t OverrideManagerBase::AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id)
{
	uint16_t id = this->GetID(grf_local_id, grfid);

	/* Look to see if this entity has already been added. This is done
	 * separately from the loop below in case a GRF has been deleted, and there
	 * are any gaps in the array.
	 */
	if (id != this->invalid_id) return id;

	/* This entity hasn't been defined before, so give it an ID now. */
	for (id = this->max_offset; id < this->max_entities; id++) {
		EntityIDMapping *map = &this->mappings[id];

		if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
			map->entity_id     = grf_local_id;
			map->grfid         = grfid;
			map->substitute_id = substitute_id;
			return id;
		}
	}

	return this->invalid_id;
}

/**
 * Gives the GRFID of the file the entity belongs to.
 * @param entity_id ID of the entity being queried.
 * @return GRFID.
 */
uint32_t OverrideManagerBase::GetGRFID(uint16_t entity_id) const
{
	return this->mappings[entity_id].grfid;
}

/**
 * Gives the substitute of the entity, as specified by the grf file
 * @param entity_id of the entity being queried
 * @return mapped id
 */
uint16_t OverrideManagerBase::GetSubstituteID(uint16_t entity_id) const
{
	return this->mappings[entity_id].substitute_id;
}

/**
 * Install the specs into the HouseSpecs array
 * It will find itself the proper slot on which it will go
 * @param hs HouseSpec read from the grf file, ready for inclusion
 */
void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
{
	HouseID house_id = this->AddEntityID(hs->grf_prop.local_id, hs->grf_prop.grffile->grfid, hs->grf_prop.subst_id);

	if (house_id == this->invalid_id) {
		GrfMsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
		return;
	}

	*HouseSpec::Get(house_id) = *hs;

	/* Now add the overrides. */
	for (int i = 0; i < this->max_offset; i++) {
		HouseSpec *overridden_hs = HouseSpec::Get(i);

		if (this->entity_overrides[i] != hs->grf_prop.local_id || this->grfid_overrides[i] != hs->grf_prop.grffile->grfid) continue;

		overridden_hs->grf_prop.override = house_id;
		this->entity_overrides[i] = this->invalid_id;
		this->grfid_overrides[i] = 0;
	}
}

/**
 * Return the ID (if ever available) of a previously inserted entity.
 * @param grf_local_id ID of this entity within the grfID
 * @param grfid ID of the grf file
 * @return the ID of the candidate, of the Invalid flag item ID
 */
uint16_t IndustryOverrideManager::GetID(uint16_t grf_local_id, uint32_t grfid) const
{
	uint16_t id = OverrideManagerBase::GetID(grf_local_id, grfid);
	if (id != this->invalid_id) return id;

	/* No mapping found, try the overrides */
	for (id = 0; id < this->max_offset; id++) {
		if (this->entity_overrides[id] == grf_local_id && this->grfid_overrides[id] == grfid) return id;
	}

	return this->invalid_id;
}

/**
 * Method to find an entity ID and to mark it as reserved for the Industry to be included.
 * @param grf_local_id ID used by the grf file for pre-installation work (equivalent of TTDPatch's setid
 * @param grfid ID of the current grf file
 * @param substitute_id industry from which data has been copied
 * @return a free entity id (slotid) if ever one has been found, or Invalid_ID marker otherwise
 */
uint16_t IndustryOverrideManager::AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id)
{
	/* This entity hasn't been defined before, so give it an ID now. */
	for (uint16_t id = 0; id < this->max_entities; id++) {
		/* Skip overridden industries */
		if (id < this->max_offset && this->entity_overrides[id] != this->invalid_id) continue;

		/* Get the real live industry */
		const IndustrySpec *inds = GetIndustrySpec(id);

		/* This industry must be one that is not available(enabled), mostly because of climate.
		 * And it must not already be used by a grf (grffile == nullptr).
		 * So reserve this slot here, as it is the chosen one */
		if (!inds->enabled && inds->grf_prop.grffile == nullptr) {
			EntityIDMapping *map = &this->mappings[id];

			if (map->entity_id == 0 && map->grfid == 0) {
				/* winning slot, mark it as been used */
				map->entity_id     = grf_local_id;
				map->grfid         = grfid;
				map->substitute_id = substitute_id;
				return id;
			}
		}
	}

	return this->invalid_id;
}

/**
 * Method to install the new industry data in its proper slot
 * The slot assignment is internal of this method, since it requires
 * checking what is available
 * @param inds Industryspec that comes from the grf decoding process
 */
void IndustryOverrideManager::SetEntitySpec(IndustrySpec *inds)
{
	/* First step : We need to find if this industry is already specified in the savegame data. */
	IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);

	if (ind_id == this->invalid_id) {
		/* Not found.
		 * Or it has already been overridden, so you've lost your place.
		 * Or it is a simple substitute.
		 * We need to find a free available slot */
		ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
		inds->grf_prop.override = this->invalid_id;  // make sure it will not be detected as overridden
	}

	if (ind_id == this->invalid_id) {
		GrfMsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
		return;
	}

	/* Now that we know we can use the given id, copy the spec to its final destination... */
	_industry_specs[ind_id] = *inds;
	/* ... and mark it as usable*/
	_industry_specs[ind_id].enabled = true;
}

void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
{
	IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);

	if (indt_id == this->invalid_id) {
		GrfMsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
		return;
	}

	_industry_tile_specs[indt_id] = *its;

	/* Now add the overrides. */
	for (int i = 0; i < this->max_offset; i++) {
		IndustryTileSpec *overridden_its = &_industry_tile_specs[i];

		if (this->entity_overrides[i] != its->grf_prop.local_id || this->grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;

		overridden_its->grf_prop.override = indt_id;
		overridden_its->enabled = false;
		this->entity_overrides[i] = this->invalid_id;
		this->grfid_overrides[i] = 0;
	}
}

/**
 * Method to install the new object data in its proper slot
 * The slot assignment is internal of this method, since it requires
 * checking what is available
 * @param spec ObjectSpec that comes from the grf decoding process
 */
void ObjectOverrideManager::SetEntitySpec(ObjectSpec *spec)
{
	/* First step : We need to find if this object is already specified in the savegame data. */
	ObjectType type = this->GetID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid);

	if (type == this->invalid_id) {
		/* Not found.
		 * Or it has already been overridden, so you've lost your place.
		 * Or it is a simple substitute.
		 * We need to find a free available slot */
		type = this->AddEntityID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid, OBJECT_TRANSMITTER);
	}

	if (type == this->invalid_id) {
		GrfMsg(1, "Object.SetEntitySpec: Too many objects allocated. Ignoring.");
		return;
	}

	extern std::vector<ObjectSpec> _object_specs;

	/* Now that we know we can use the given id, copy the spec to its final destination. */
	if (type >= _object_specs.size()) _object_specs.resize(type + 1);
	_object_specs[type] = *spec;
}

/**
 * Function used by houses (and soon industries) to get information
 * on type of "terrain" the tile it is queries sits on.
 * @param tile TileIndex of the tile been queried
 * @param context The context of the tile.
 * @return value corresponding to the grf expected format:
 *         Terrain type: 0 normal, 1 desert, 2 rainforest, 4 on or above snowline
 */
uint32_t GetTerrainType(TileIndex tile, TileContext context)
{
	switch (_settings_game.game_creation.landscape) {
		case LT_TROPIC: return GetTropicZone(tile);
		case LT_ARCTIC: {
			bool has_snow;
			switch (GetTileType(tile)) {
				case MP_CLEAR:
					/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
					if (_generating_world) goto genworld;
					has_snow = IsSnowTile(tile) && GetClearDensity(tile) >= 2;
					break;

				case MP_RAILWAY: {
					/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
					if (_generating_world) goto genworld; // we do not care about foundations here
					RailGroundType ground = GetRailGroundType(tile);
					has_snow = (ground == RAIL_GROUND_ICE_DESERT || (context == TCX_UPPER_HALFTILE && ground == RAIL_GROUND_HALF_SNOW));
					break;
				}

				case MP_ROAD:
					/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
					if (_generating_world) goto genworld; // we do not care about foundations here
					has_snow = IsOnSnow(tile);
					break;

				case MP_TREES: {
					/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
					if (_generating_world) goto genworld;
					TreeGround ground = GetTreeGround(tile);
					has_snow = (ground == TREE_GROUND_SNOW_DESERT || ground == TREE_GROUND_ROUGH_SNOW) && GetTreeDensity(tile) >= 2;
					break;
				}

				case MP_TUNNELBRIDGE:
					if (context == TCX_ON_BRIDGE) {
						has_snow = (GetBridgeHeight(tile) > GetSnowLine());
					} else {
						/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
						if (_generating_world) goto genworld; // we do not care about foundations here
						has_snow = HasTunnelBridgeSnowOrDesert(tile);
					}
					break;

				case MP_STATION:
				case MP_HOUSE:
				case MP_INDUSTRY:
				case MP_OBJECT:
					/* These tiles usually have a levelling foundation. So use max Z */
					has_snow = (GetTileMaxZ(tile) > GetSnowLine());
					break;

				case MP_VOID:
				case MP_WATER:
				genworld:
					has_snow = (GetTileZ(tile) > GetSnowLine());
					break;

				default: NOT_REACHED();
			}
			return has_snow ? 4 : 0;
		}
		default:        return 0;
	}
}

/**
 * Get the tile at the given offset.
 * @param parameter The NewGRF "encoded" offset.
 * @param tile The tile to base the offset from.
 * @param signed_offsets Whether the offsets are to be interpreted as signed or not.
 * @param axis Axis of a railways station.
 * @return The tile at the offset.
 */
TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets, Axis axis)
{
	int8_t x = GB(parameter, 0, 4);
	int8_t y = GB(parameter, 4, 4);

	if (signed_offsets && x >= 8) x -= 16;
	if (signed_offsets && y >= 8) y -= 16;

	/* Swap width and height depending on axis for railway stations */
	if (axis == INVALID_AXIS && HasStationTileRail(tile)) axis = GetRailStationAxis(tile);
	if (axis == AXIS_Y) Swap(x, y);

	/* Make sure we never roam outside of the map, better wrap in that case */
	return Map::WrapToMap(tile + TileDiffXY(x, y));
}

/**
 * Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
 *
 * @param tile the tile of interest.
 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
 * @return 0czzbbss: c = TileType; zz = TileZ; bb: 7-3 zero, 4-2 TerrainType, 1 water/shore, 0 zero; ss = TileSlope
 */
uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8)
{
	TileType tile_type = GetTileType(tile);

	/* Fake tile type for trees on shore */
	if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;

	int z;
	Slope tileh = GetTilePixelSlope(tile, &z);
	/* Return 0 if the tile is a land tile */
	byte terrain_type = (HasTileWaterClass(tile) ? (GetWaterClass(tile) + 1) & 3 : 0) << 5 | GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
	if (grf_version8) z /= TILE_HEIGHT;
	return tile_type << 24 | ClampTo<uint8_t>(z) << 16 | terrain_type << 8 | tileh;
}

/**
 * Returns company information like in vehicle var 43 or station var 43.
 * @param owner Owner of the object.
 * @param l Livery of the object; nullptr to use default.
 * @return NewGRF company information.
 */
uint32_t GetCompanyInfo(CompanyID owner, const Livery *l)
{
	if (l == nullptr && Company::IsValidID(owner)) l = &Company::Get(owner)->livery[LS_DEFAULT];
	return owner | (Company::IsValidAiID(owner) ? 0x10000 : 0) | (l != nullptr ? (l->colour1 << 24) | (l->colour2 << 28) : 0);
}

/**
 * Get the error message from a shape/location/slope check callback result.
 * @param cb_res Callback result to translate. If bit 10 is set this is a standard error message, otherwise a NewGRF provided string.
 * @param grffile NewGRF to use to resolve a custom error message.
 * @param default_error Error message to use for the generic error.
 * @return CommandCost indicating success or the error message.
 */
CommandCost GetErrorMessageFromLocationCallbackResult(uint16_t cb_res, const GRFFile *grffile, StringID default_error)
{
	CommandCost res;

	if (cb_res < 0x400) {
		res = CommandCost(GetGRFStringID(grffile->grfid, 0xD000 + cb_res));
	} else {
		switch (cb_res) {
			case 0x400: return res; // No error.

			default:    // unknown reason -> default error
			case 0x401: res = CommandCost(default_error); break;

			case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
			case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
			case 0x404: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_ABOVE_SNOW_LINE); break;
			case 0x405: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_BELOW_SNOW_LINE); break;
			case 0x406: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_SEA); break;
			case 0x407: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_CANAL); break;
			case 0x408: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_RIVER); break;
		}
	}

	/* Copy some parameters from the registers to the error message text ref. stack */
	res.UseTextRefStack(grffile, 4);

	return res;
}

/**
 * Record that a NewGRF returned an unknown/invalid callback result.
 * Also show an error to the user.
 * @param grfid ID of the NewGRF causing the problem.
 * @param cbid Callback causing the problem.
 * @param cb_res Invalid result returned by the callback.
 */
void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res)
{
	GRFConfig *grfconfig = GetGRFConfig(grfid);

	if (!HasBit(grfconfig->grf_bugs, GBUG_UNKNOWN_CB_RESULT)) {
		SetBit(grfconfig->grf_bugs, GBUG_UNKNOWN_CB_RESULT);
		SetDParamStr(0, grfconfig->GetName());
		SetDParam(1, cbid);
		SetDParam(2, cb_res);
		ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT, WL_CRITICAL);
	}

	/* debug output */
	SetDParamStr(0, grfconfig->GetName());
	Debug(grf, 0, "{}", StrMakeValid(GetString(STR_NEWGRF_BUGGY)));

	SetDParam(1, cbid);
	SetDParam(2, cb_res);
	Debug(grf, 0, "{}", StrMakeValid(GetString(STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT)));
}

/**
 * Converts a callback result into a boolean.
 * For grf version < 8 the result is checked for zero or non-zero.
 * For grf version >= 8 the callback result must be 0 or 1.
 * @param grffile NewGRF returning the value.
 * @param cbid Callback returning the value.
 * @param cb_res Callback result.
 * @return Boolean value. True if cb_res != 0.
 */
bool ConvertBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t cb_res)
{
	assert(cb_res != CALLBACK_FAILED); // We do not know what to return

	if (grffile->grf_version < 8) return cb_res != 0;

	if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
	return cb_res != 0;
}

/**
 * Converts a callback result into a boolean.
 * For grf version < 8 the first 8 bit of the result are checked for zero or non-zero.
 * For grf version >= 8 the callback result must be 0 or 1.
 * @param grffile NewGRF returning the value.
 * @param cbid Callback returning the value.
 * @param cb_res Callback result.
 * @return Boolean value. True if cb_res != 0.
 */
bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t cb_res)
{
	assert(cb_res != CALLBACK_FAILED); // We do not know what to return

	if (grffile->grf_version < 8) return GB(cb_res, 0, 8) != 0;

	if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
	return cb_res != 0;
}


/* static */ std::vector<DrawTileSeqStruct> NewGRFSpriteLayout::result_seq;

/**
 * Clone the building sprites of a spritelayout.
 * @param source The building sprites to copy.
 */
void NewGRFSpriteLayout::Clone(const DrawTileSeqStruct *source)
{
	assert(this->seq == nullptr);
	assert(source != nullptr);

	size_t count = 1; // 1 for the terminator
	const DrawTileSeqStruct *element;
	foreach_draw_tile_seq(element, source) count++;

	DrawTileSeqStruct *sprites = MallocT<DrawTileSeqStruct>(count);
	MemCpyT(sprites, source, count);
	this->seq = sprites;
}

/**
 * Clone a spritelayout.
 * @param source The spritelayout to copy.
 */
void NewGRFSpriteLayout::Clone(const NewGRFSpriteLayout *source)
{
	this->Clone((const DrawTileSprites*)source);

	if (source->registers != nullptr) {
		size_t count = 1; // 1 for the ground sprite
		const DrawTileSeqStruct *element;
		foreach_draw_tile_seq(element, source->seq) count++;

		TileLayoutRegisters *regs = MallocT<TileLayoutRegisters>(count);
		MemCpyT(regs, source->registers, count);
		this->registers = regs;
	}
}


/**
 * Allocate a spritelayout for \a num_sprites building sprites.
 * @param num_sprites Number of building sprites to allocate memory for. (not counting the terminator)
 */
void NewGRFSpriteLayout::Allocate(uint num_sprites)
{
	assert(this->seq == nullptr);

	DrawTileSeqStruct *sprites = CallocT<DrawTileSeqStruct>(num_sprites + 1);
	sprites[num_sprites].MakeTerminator();
	this->seq = sprites;
}

/**
 * Allocate memory for register modifiers.
 */
void NewGRFSpriteLayout::AllocateRegisters()
{
	assert(this->seq != nullptr);
	assert(this->registers == nullptr);

	size_t count = 1; // 1 for the ground sprite
	const DrawTileSeqStruct *element;
	foreach_draw_tile_seq(element, this->seq) count++;

	this->registers = CallocT<TileLayoutRegisters>(count);
}

/**
 * Prepares a sprite layout before resolving action-1-2-3 chains.
 * Integrates offsets into the layout and determines which chains to resolve.
 * @note The function uses statically allocated temporary storage, which is reused every time when calling the function.
 *       That means, you have to use the sprite layout before calling #PrepareLayout() the next time.
 * @param orig_offset          Offset to apply to non-action-1 sprites.
 * @param newgrf_ground_offset Offset to apply to action-1 ground sprites.
 * @param newgrf_offset        Offset to apply to action-1 non-ground sprites.
 * @param constr_stage         Construction stage (0-3) to apply to all action-1 sprites.
 * @param separate_ground      Whether the ground sprite shall be resolved by a separate action-1-2-3 chain by default.
 * @return Bitmask of values for variable 10 to resolve action-1-2-3 chains for.
 */
uint32_t NewGRFSpriteLayout::PrepareLayout(uint32_t orig_offset, uint32_t newgrf_ground_offset, uint32_t newgrf_offset, uint constr_stage, bool separate_ground) const
{
	result_seq.clear();
	uint32_t var10_values = 0;

	/* Create a copy of the spritelayout, so we can modify some values.
	 * Also include the groundsprite into the sequence for easier processing. */
	DrawTileSeqStruct *result = &result_seq.emplace_back();
	result->image = ground;
	result->delta_x = 0;
	result->delta_y = 0;
	result->delta_z = (int8_t)0x80;

	const DrawTileSeqStruct *dtss;
	foreach_draw_tile_seq(dtss, this->seq) {
		result_seq.push_back(*dtss);
	}
	result_seq.emplace_back().MakeTerminator();
	/* Determine the var10 values the action-1-2-3 chains needs to be resolved for,
	 * and apply the default sprite offsets (unless disabled). */
	const TileLayoutRegisters *regs = this->registers;
	bool ground = true;
	foreach_draw_tile_seq(result, result_seq.data()) {
		TileLayoutFlags flags = TLF_NOTHING;
		if (regs != nullptr) flags = regs->flags;

		/* Record var10 value for the sprite */
		if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_SPRITE_REG_FLAGS)) {
			uint8_t var10 = (flags & TLF_SPRITE_VAR10) ? regs->sprite_var10 : (ground && separate_ground ? 1 : 0);
			SetBit(var10_values, var10);
		}

		/* Add default sprite offset, unless there is a custom one */
		if (!(flags & TLF_SPRITE)) {
			if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
				result->image.sprite += ground ? newgrf_ground_offset : newgrf_offset;
				if (constr_stage > 0 && regs != nullptr) result->image.sprite += GetConstructionStageOffset(constr_stage, regs->max_sprite_offset);
			} else {
				result->image.sprite += orig_offset;
			}
		}

		/* Record var10 value for the palette */
		if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_PALETTE_REG_FLAGS)) {
			uint8_t var10 = (flags & TLF_PALETTE_VAR10) ? regs->palette_var10 : (ground && separate_ground ? 1 : 0);
			SetBit(var10_values, var10);
		}

		/* Add default palette offset, unless there is a custom one */
		if (!(flags & TLF_PALETTE)) {
			if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
				result->image.sprite += ground ? newgrf_ground_offset : newgrf_offset;
				if (constr_stage > 0 && regs != nullptr) result->image.sprite += GetConstructionStageOffset(constr_stage, regs->max_palette_offset);
			}
		}

		ground = false;
		if (regs != nullptr) regs++;
	}

	return var10_values;
}

/**
 * Evaluates the register modifiers and integrates them into the preprocessed sprite layout.
 * @pre #PrepareLayout() needs calling first.
 * @param resolved_var10  The value of var10 the action-1-2-3 chain was evaluated for.
 * @param resolved_sprite Result sprite of the action-1-2-3 chain.
 * @param separate_ground Whether the ground sprite is resolved by a separate action-1-2-3 chain.
 * @return Resulting spritelayout after processing the registers.
 */
void NewGRFSpriteLayout::ProcessRegisters(uint8_t resolved_var10, uint32_t resolved_sprite, bool separate_ground) const
{
	DrawTileSeqStruct *result;
	const TileLayoutRegisters *regs = this->registers;
	bool ground = true;
	foreach_draw_tile_seq(result, result_seq.data()) {
		TileLayoutFlags flags = TLF_NOTHING;
		if (regs != nullptr) flags = regs->flags;

		/* Is the sprite or bounding box affected by an action-1-2-3 chain? */
		if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_SPRITE_REG_FLAGS)) {
			/* Does the var10 value apply to this sprite? */
			uint8_t var10 = (flags & TLF_SPRITE_VAR10) ? regs->sprite_var10 : (ground && separate_ground ? 1 : 0);
			if (var10 == resolved_var10) {
				/* Apply registers */
				if ((flags & TLF_DODRAW) && GetRegister(regs->dodraw) == 0) {
					result->image.sprite = 0;
				} else {
					if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) result->image.sprite += resolved_sprite;
					if (flags & TLF_SPRITE) {
						int16_t offset = (int16_t)GetRegister(regs->sprite); // mask to 16 bits to avoid trouble
						if (!HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (offset >= 0 && offset < regs->max_sprite_offset)) {
							result->image.sprite += offset;
						} else {
							result->image.sprite = SPR_IMG_QUERY;
						}
					}

					if (result->IsParentSprite()) {
						if (flags & TLF_BB_XY_OFFSET) {
							result->delta_x += (int32_t)GetRegister(regs->delta.parent[0]);
							result->delta_y += (int32_t)GetRegister(regs->delta.parent[1]);
						}
						if (flags & TLF_BB_Z_OFFSET)    result->delta_z += (int32_t)GetRegister(regs->delta.parent[2]);
					} else {
						if (flags & TLF_CHILD_X_OFFSET) result->delta_x += (int32_t)GetRegister(regs->delta.child[0]);
						if (flags & TLF_CHILD_Y_OFFSET) result->delta_y += (int32_t)GetRegister(regs->delta.child[1]);
					}
				}
			}
		}

		/* Is the palette affected by an action-1-2-3 chain? */
		if (result->image.sprite != 0 && (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_PALETTE_REG_FLAGS))) {
			/* Does the var10 value apply to this sprite? */
			uint8_t var10 = (flags & TLF_PALETTE_VAR10) ? regs->palette_var10 : (ground && separate_ground ? 1 : 0);
			if (var10 == resolved_var10) {
				/* Apply registers */
				if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) result->image.pal += resolved_sprite;
				if (flags & TLF_PALETTE) {
					int16_t offset = (int16_t)GetRegister(regs->palette); // mask to 16 bits to avoid trouble
					if (!HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (offset >= 0 && offset < regs->max_palette_offset)) {
						result->image.pal += offset;
					} else {
						result->image.sprite = SPR_IMG_QUERY;
						result->image.pal = PAL_NONE;
					}
				}
			}
		}

		ground = false;
		if (regs != nullptr) regs++;
	}
}