Files @ r17505:4d7bb35255a6
Branch filter:

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

translators
(svn r22276) -Update from WebTranslator v3.0:
catalan - 1 changes by arnau
frisian - 61 changes by gjannema
german - 1 changes by MG
russian - 2 changes by Lone_Wolf
  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
r6835:9051f7024392
r6835:9051f7024392
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r12816:33099b7286d4
r6835:9051f7024392
r6835:9051f7024392
r7207:0be528a70131
r7207:0be528a70131
r15719:e9838b5d4aed
r8131:7a50db7be0ff
r8213:466402e95092
r10208:ef8fcc3dc4ca
r8275:5932486ddd21
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r14248:a9050881acd7
r6835:9051f7024392
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r8689:a464b41f4a24
r8436:ffb72b242e57
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7000:2cb5bda647b1
r6835:9051f7024392
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r7928:a80e7e05d6c5
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r14315:8b21eb31a313
r14315:8b21eb31a313
r6868:aab228767f55
r7622:69c44ae2c325
r7622:69c44ae2c325
r14315:8b21eb31a313
r14315:8b21eb31a313
r14315:8b21eb31a313
r14315:8b21eb31a313
r6868:aab228767f55
r7706:6322b044f683
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r6868:aab228767f55
r7622:69c44ae2c325
r7622:69c44ae2c325
r6868:aab228767f55
r16202:ca4a67f4e6a4
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r14315:8b21eb31a313
r7622:69c44ae2c325
r7622:69c44ae2c325
r14315:8b21eb31a313
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r6868:aab228767f55
r6868:aab228767f55
r7622:69c44ae2c325
r16331:77622ea88835
r14315:8b21eb31a313
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r7622:69c44ae2c325
r6868:aab228767f55
r6868:aab228767f55
r7233:d578434a488b
r7233:d578434a488b
r9605:6debaf923fba
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r14159:6cb75763eb03
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r7233:d578434a488b
r15610:623a23fb6560
r15610:623a23fb6560
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r9605:6debaf923fba
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7668:4f958c3ba983
r15569:7f2810d85ccf
r7451:e5b11cf98a73
r11363:6906c490a00e
r7931:4c17a74c399e
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r15077:f52f97d194e2
r15077:f52f97d194e2
r15077:f52f97d194e2
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r14159:6cb75763eb03
r14875:c6c6704d8c04
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r14159:6cb75763eb03
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r15610:623a23fb6560
r15610:623a23fb6560
r13060:e57594b0ca84
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r15613:193c12018337
r15613:193c12018337
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7207:0be528a70131
r8233:74750327ae8e
r8233:74750327ae8e
r8233:74750327ae8e
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r9736:0730920103f1
r8233:74750327ae8e
r16464:ae3f342df07b
r8233:74750327ae8e
r8233:74750327ae8e
r8233:74750327ae8e
r8233:74750327ae8e
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r13026:0c3f00ec0a74
r7928:a80e7e05d6c5
r7958:7816b05b15ac
r15110:4b0fbedb7d1d
r7958:7816b05b15ac
r7958:7816b05b15ac
r7958:7816b05b15ac
r7958:7816b05b15ac
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r6835:9051f7024392
r7445:0ebcc38f1de8
r7445:0ebcc38f1de8
r7445:0ebcc38f1de8
r10207:a1fc2f2a33db
r7671:a84de606c64a
r14949:c4d08730382c
r7671:a84de606c64a
r7685:99fa1f8cb376
r11944:3b8f9f948e8d
r11944:3b8f9f948e8d
r10207:a1fc2f2a33db
r7685:99fa1f8cb376
r10207:a1fc2f2a33db
r7685:99fa1f8cb376
r7671:a84de606c64a
r7685:99fa1f8cb376
r7685:99fa1f8cb376
r7671:a84de606c64a
r7671:a84de606c64a
r9487:ae7979d51839
r9487:ae7979d51839
r6868:aab228767f55
r16279:c9054b28cdd6
r6835:9051f7024392
r7621:16fbec405049
r7621:16fbec405049
r16279:c9054b28cdd6
r12816:33099b7286d4
r6835:9051f7024392
r7234:89807e0412b9
r16279:c9054b28cdd6
r7234:89807e0412b9
r7234:89807e0412b9
r7621:16fbec405049
r16279:c9054b28cdd6
r12816:33099b7286d4
r15924:f5bba173626e
r7234:89807e0412b9
r7234:89807e0412b9
r7234:89807e0412b9
r7234:89807e0412b9
r7314:c4f3b380b639
r6835:9051f7024392
r8970:a7dc16677ffd
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r7451:e5b11cf98a73
r6835:9051f7024392
r7610:6031d09c03ef
r7610:6031d09c03ef
r7610:6031d09c03ef
r6835:9051f7024392
r14159:6cb75763eb03
r14159:6cb75763eb03
r6835:9051f7024392
r7191:b6964312d678
r6835:9051f7024392
r6835:9051f7024392
r7186:dea62b7d11ec
r14159:6cb75763eb03
r14159:6cb75763eb03
r11363:6906c490a00e
r6835:9051f7024392
r7645:e35d73ba358a
r7165:b30ba7925dc4
r7165:b30ba7925dc4
r7165:b30ba7925dc4
r7165:b30ba7925dc4
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7645:e35d73ba358a
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r14421:c68379cfffcb
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r14421:c68379cfffcb
r6835:9051f7024392
r6835:9051f7024392
r7186:dea62b7d11ec
r11085:c87a330fb4c2
r8634:1ca26c0d3e1e
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7186:dea62b7d11ec
r8634:1ca26c0d3e1e
r7186:dea62b7d11ec
r8634:1ca26c0d3e1e
r6835:9051f7024392
r6835:9051f7024392
r16464:ae3f342df07b
r6835:9051f7024392
r6835:9051f7024392
r11163:407814b6138f
r6835:9051f7024392
r6835:9051f7024392
r11985:d8202dea2203
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7621:16fbec405049
r7621:16fbec405049
r15104:1752997daf93
r15109:6f957cc68271
r7621:16fbec405049
r7621:16fbec405049
r7621:16fbec405049
r7621:16fbec405049
r15104:1752997daf93
r15109:6f957cc68271
r7621:16fbec405049
r7621:16fbec405049
r7621:16fbec405049
r7621:16fbec405049
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r7621:16fbec405049
r7621:16fbec405049
r8233:74750327ae8e
r6835:9051f7024392
r7621:16fbec405049
r7621:16fbec405049
r7621:16fbec405049
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r7610:6031d09c03ef
r6835:9051f7024392
r6835:9051f7024392
r6884:06c993c360e4
r8233:74750327ae8e
r6835:9051f7024392
r7327:28855024ff6c
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r8756:3b4554f1d3d4
r9750:1e2535e15958
r9750:1e2535e15958
r9750:1e2535e15958
r6835:9051f7024392
r6835:9051f7024392
r7327:28855024ff6c
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r8233:74750327ae8e
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r6835:9051f7024392
r15751:f4fe02ef4406
r11985:d8202dea2203
r6835:9051f7024392
r11985:d8202dea2203
r6835:9051f7024392
r7168:156fd244b919
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7234:89807e0412b9
r7207:0be528a70131
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r8415:53530e0c5548
r8436:ffb72b242e57
r8689:a464b41f4a24
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7668:4f958c3ba983
r7207:0be528a70131
r7207:0be528a70131
r15610:623a23fb6560
r15610:623a23fb6560
r14598:744c3c3ec434
r14598:744c3c3ec434
r14598:744c3c3ec434
r14598:744c3c3ec434
r15105:18f362fd63e2
r15256:e22f3389fd3a
r16231:76e02ca8f4e5
r14598:744c3c3ec434
r14598:744c3c3ec434
r16231:76e02ca8f4e5
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7207:0be528a70131
r7668:4f958c3ba983
r7668:4f958c3ba983
r14159:6cb75763eb03
r14875:c6c6704d8c04
r7668:4f958c3ba983
r14598:744c3c3ec434
r10236:993bcc06ffa8
r15105:18f362fd63e2
r15256:e22f3389fd3a
r7668:4f958c3ba983
r8233:74750327ae8e
r7207:0be528a70131
r7207:0be528a70131
r16231:76e02ca8f4e5
r8689:a464b41f4a24
r7207:0be528a70131
r15751:f4fe02ef4406
r7207:0be528a70131
r7658:dc7d1afc14ba
r7658:dc7d1afc14ba
r14598:744c3c3ec434
r11985:d8202dea2203
r14598:744c3c3ec434
r7207:0be528a70131
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r11985:d8202dea2203
r14598:744c3c3ec434
r14598:744c3c3ec434
r14598:744c3c3ec434
r14598:744c3c3ec434
r7207:0be528a70131
r14598:744c3c3ec434
r7207:0be528a70131
r7207:0be528a70131
r16534:df4e8f1343d2
r16534:df4e8f1343d2
r16534:df4e8f1343d2
r16534:df4e8f1343d2
r16534:df4e8f1343d2
r16534:df4e8f1343d2
r7260:348163217738
r7260:348163217738
r7260:348163217738
r7260:348163217738
r13026:0c3f00ec0a74
r7260:348163217738
r7260:348163217738
r7260:348163217738
r7260:348163217738
r7260:348163217738
r7260:348163217738
r7260:348163217738
r7260:348163217738
r13338:391f5d181322
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7614:4da580d88972
r7168:156fd244b919
r14159:6cb75763eb03
r7614:4da580d88972
r7958:7816b05b15ac
r7958:7816b05b15ac
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r14635:a75264d517b7
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r11582:3a0dfd7221e5
r7168:156fd244b919
r15751:f4fe02ef4406
r11985:d8202dea2203
r11985:d8202dea2203
r7168:156fd244b919
r11985:d8202dea2203
r7168:156fd244b919
r7168:156fd244b919
r13338:391f5d181322
r7168:156fd244b919
r7168:156fd244b919
r11985:d8202dea2203
r7168:156fd244b919
r7168:156fd244b919
r11985:d8202dea2203
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7195:78237834f989
r13024:48c81d0b078a
r7168:156fd244b919
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
r15719:e9838b5d4aed
/* $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 newgrf_industries.cpp Handling of NewGRF industries. */

#include "stdafx.h"
#include "debug.h"
#include "industry.h"
#include "newgrf.h"
#include "newgrf_industries.h"
#include "newgrf_text.h"
#include "newgrf_town.h"
#include "newgrf_cargo.h"
#include "window_func.h"
#include "town.h"
#include "company_base.h"
#include "command_func.h"
#include "gui.h"
#include "strings_func.h"
#include "core/random_func.hpp"

#include "table/strings.h"

static uint32 _industry_creation_random_bits;

/* Since the industry IDs defined by the GRF file don't necessarily correlate
 * to those used by the game, the IDs used for overriding old industries must be
 * translated when the idustry spec is set. */
IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);

IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
{
	if (grf_type == IT_INVALID) return IT_INVALID;
	if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 6);

	return _industry_mngr.GetID(GB(grf_type, 0, 6), grf_id);
}

/**
 * Make an analysis of a tile and check for its belonging to the same
 * industry, and/or the same grf file
 * @param tile TileIndex of the tile to query
 * @param i Industry to which to compare the tile to
 * @param cur_grfid GRFID of the current callback chain
 * @return value encoded as per NFO specs
 */
uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid)
{
	if (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != i->index) {
		/* No industry and/or the tile does not have the same industry as the one we match it with */
		return 0xFFFF;
	}

	IndustryGfx gfx = GetCleanIndustryGfx(tile);
	const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);

	if (gfx < NEW_INDUSTRYTILEOFFSET) { // Does it belongs to an old type?
		/* It is an old tile.  We have to see if it's been overriden */
		if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) { // has it been overridden?
			return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
		}
		/* Overriden */
		const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);

		if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
			return tile_ovr->grf_prop.local_id; // same grf file
		} else {
			return 0xFFFE; // not the same grf file
		}
	}
	/* Not an 'old type' tile */
	if (indtsp->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
		if (indtsp->grf_prop.grffile->grfid == cur_grfid) { // same industry, same grf ?
			return indtsp->grf_prop.local_id;
		} else {
			return 0xFFFE; // Defined in another grf file
		}
	}
	/* The tile has no spritegroup */
	return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
}

static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
{
	uint32 best_dist = UINT32_MAX;
	const Industry *i;
	FOR_ALL_INDUSTRIES(i) {
		if (i->type != type || i == current) continue;

		best_dist = min(best_dist, DistanceManhattan(tile, i->location.tile));
	}

	return best_dist;
}

/**
 * Implementation of both var 67 and 68
 * since the mechanism is almost the same, it is easier to regroup them on the same
 * function.
 * @param param_setID parameter given to the callback, which is the set id, or the local id, in our terminology
 * @param layout_filter on what layout do we filter?
 * @param current Industry for which the inquiry is made
 * @return the formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
 */
static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, const Industry *current)
{
	uint32 GrfID = GetRegister(0x100);  ///< Get the GRFID of the definition to look for in register 100h
	IndustryType ind_index;
	uint32 closest_dist = UINT32_MAX;
	byte count = 0;

	/* Determine what will be the industry type to look for */
	switch (GrfID) {
		case 0:  // this is a default industry type
			ind_index = param_setID;
			break;

		case 0xFFFFFFFF: // current grf
			GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
			/* FALL THROUGH */

		default: // use the grfid specified in register 100h
			SetBit(param_setID, 7); // bit 7 means it is not an old type
			ind_index = MapNewGRFIndustryType(param_setID, GrfID);
			break;
	}

	/* If the industry type is invalid, there is none and the closest is far away. */
	if (ind_index >= NUM_INDUSTRYTYPES) return 0 | 0xFFFF;

	if (layout_filter == 0) {
		/* If the filter is 0, it could be because none was specified as well as being really a 0.
		 * In either case, just do the regular var67 */
		closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
		count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit
	} else {
		/* Count only those who match the same industry type and layout filter
		 * Unfortunately, we have to do it manually */
		const Industry *i;
		FOR_ALL_INDUSTRIES(i) {
			if (i->type == ind_index && i != current && i->selected_layout == layout_filter) {
				closest_dist = min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
				count++;
			}
		}
	}

	return count << 16 | GB(closest_dist, 0, 16);
}

/**
 * This function implements the industries variables that newGRF defines.
 * @param object the object that we want to query
 * @param variable that is queried
 * @param parameter unused
 * @param available will return false if ever the variable asked for does not exist
 * @return the value stored in the corresponding variable
 */
uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
{
	const Industry *industry = object->u.industry.ind;
	TileIndex tile = object->u.industry.tile;
	IndustryType type = object->u.industry.type;
	const IndustrySpec *indspec = GetIndustrySpec(type);

	/* Shall the variable get resolved in parent scope and are we not yet in parent scope? */
	if (object->u.industry.gfx == INVALID_INDUSTRYTILE && object->scope == VSG_SCOPE_PARENT) {
		/* Pass the request on to the town of the industry */
		const Town *t;

		if (industry != NULL) {
			t = industry->town;
		} else if (tile != INVALID_TILE) {
			t = ClosestTownFromTile(tile, UINT_MAX);
		} else {
			*available = false;
			return UINT_MAX;
		}

		return TownGetVariable(variable, parameter, available, t);
	}

	if (industry == NULL) {
		DEBUG(grf, 1, "Unhandled variable 0x%X (no available industry) in callback 0x%x", variable, object->callback);

		*available = false;
		return UINT_MAX;
	}

	switch (variable) {
		case 0x40:
		case 0x41:
		case 0x42: { // waiting cargo, but only if those two callback flags are set
			uint16 callback = indspec->callback_mask;
			if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
				if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
					if (industry->prod_level == 0) return 0;
					return min(industry->incoming_cargo_waiting[variable - 0x40] / industry->prod_level, (uint16)0xFFFF);
				} else {
					return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
				}
			} else {
				return 0;
			}
		}

		/* Manhattan distance of closes dry/water tile */
		case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);

		/* Layout number */
		case 0x44: return industry->selected_layout;

		/* Company info */
		case 0x45: {
			byte colours = 0;
			bool is_ai = false;

			const Company *c = Company::GetIfValid(industry->founder);
			if (c != NULL) {
				const Livery *l = &c->livery[LS_DEFAULT];

				is_ai = c->is_ai;
				colours = l->colour1 + l->colour2 * 16;
			}

			return industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
		}

		case 0x46: return industry->construction_date; // Date when built - long format - (in days)

		/* Get industry ID at offset param */
		case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->location.tile, false), industry, object->grffile->grfid);

		/* Get random tile bits at offset param */
		case 0x61:
			tile = GetNearbyTile(parameter, tile, false);
			return (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == industry) ? GetIndustryRandomBits(tile) : 0;

		/* Land info of nearby tiles */
		case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY, false);

		/* Animation stage of nearby tiles */
		case 0x63:
			tile = GetNearbyTile(parameter, tile, false);
			if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == industry) {
				return GetAnimationFrame(tile);
			}
			return 0xFFFFFFFF;

		/* Distance of nearest industry of given type */
		case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry);
		/* Get town zone and Manhattan distance of closest town */
		case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
		/* Get square of Euclidian distance of closes town */
		case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);

		/* Count of industry, distance of closest instance
		 * 68 is the same as 67, but with a filtering on selected layout */
		case 0x67:
		case 0x68: return GetCountAndDistanceOfClosestInstance(parameter, variable == 0x68 ? GB(GetRegister(0x101), 0, 8) : 0, industry);

		/* Get a variable from the persistent storage */
		case 0x7C: return industry->psa.Get(parameter);

		/* Industry structure access*/
		case 0x80: return industry->location.tile;
		case 0x81: return GB(industry->location.tile, 8, 8);
		/* Pointer to the town the industry is associated with */
		case 0x82: return industry->town->index;
		case 0x83:
		case 0x84:
		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
		case 0x86: return industry->location.w;
		case 0x87: return industry->location.h;// xy dimensions

		case 0x88:
		case 0x89: return industry->produced_cargo[variable - 0x88];
		case 0x8A: return industry->produced_cargo_waiting[0];
		case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
		case 0x8C: return industry->produced_cargo_waiting[1];
		case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
		case 0x8E:
		case 0x8F: return industry->production_rate[variable - 0x8E];
		case 0x90:
		case 0x91:
		case 0x92: return industry->accepts_cargo[variable - 0x90];
		case 0x93: return industry->prod_level;
		/* amount of cargo produced so far THIS month. */
		case 0x94: return industry->this_month_production[0];
		case 0x95: return GB(industry->this_month_production[0], 8, 8);
		case 0x96: return industry->this_month_production[1];
		case 0x97: return GB(industry->this_month_production[1], 8, 8);
		/* amount of cargo transported so far THIS month. */
		case 0x98: return industry->this_month_transported[0];
		case 0x99: return GB(industry->this_month_transported[0], 8, 8);
		case 0x9A: return industry->this_month_transported[1];
		case 0x9B: return GB(industry->this_month_transported[1], 8, 8);
		/* fraction of cargo transported LAST month. */
		case 0x9C:
		case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
		/* amount of cargo produced LAST month. */
		case 0x9E: return industry->last_month_production[0];
		case 0x9F: return GB(industry->last_month_production[0], 8, 8);
		case 0xA0: return industry->last_month_production[1];
		case 0xA1: return GB(industry->last_month_production[1], 8, 8);
		/* amount of cargo transported last month. */
		case 0xA2: return industry->last_month_transported[0];
		case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
		case 0xA4: return industry->last_month_transported[1];
		case 0xA5: return GB(industry->last_month_transported[1], 8, 8);

		case 0xA6: return industry->type;
		case 0xA7: return industry->founder;
		case 0xA8: return industry->random_colour;
		case 0xA9: return Clamp(industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
		case 0xAA: return industry->counter;
		case 0xAB: return GB(industry->counter, 8, 8);
		case 0xAC: return industry->was_cargo_delivered;

		case 0xB0: return Clamp(industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
		case 0xB3: return industry->construction_type; // Construction type
		case 0xB4: return Clamp(industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date last cargo accepted since 1920 (in days)
	}

	DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);

	*available = false;
	return UINT_MAX;
}

static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
{
	/* IndustryTile do not have 'real' groups */
	return NULL;
}

static uint32 IndustryGetRandomBits(const ResolverObject *object)
{
	const Industry *ind = object->u.industry.ind;
	return ind != NULL ? ind->random: 0;
}

static uint32 IndustryGetTriggers(const ResolverObject *object)
{
	const Industry *ind = object->u.industry.ind;
	return ind != NULL ? ind->random_triggers : 0;
}

static void IndustrySetTriggers(const ResolverObject *object, int triggers)
{
	Industry *ind = object->u.industry.ind;
	assert(ind != NULL && ind->index != INVALID_INDUSTRY);
	ind->random_triggers = triggers;
}

static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus, IndustryType type)
{
	res->GetRandomBits = IndustryGetRandomBits;
	res->GetTriggers   = IndustryGetTriggers;
	res->SetTriggers   = IndustrySetTriggers;
	res->GetVariable   = IndustryGetVariable;
	res->ResolveReal   = IndustryResolveReal;

	res->psa             = &indus->psa;
	res->u.industry.tile = tile;
	res->u.industry.ind  = indus;
	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
	res->u.industry.type = type;

	res->callback        = CBID_NO_CALLBACK;
	res->callback_param1 = 0;
	res->callback_param2 = 0;
	res->last_value      = 0;
	res->trigger         = 0;
	res->reseed          = 0;
	res->count           = 0;

	const IndustrySpec *indspec = GetIndustrySpec(type);
	res->grffile         = (indspec != NULL ? indspec->grf_prop.grffile : NULL);
}

uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
{
	ResolverObject object;
	const SpriteGroup *group;

	NewIndustryResolver(&object, tile, industry, type);
	object.callback = callback;
	object.callback_param1 = param1;
	object.callback_param2 = param2;

	group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
	if (group == NULL) return CALLBACK_FAILED;

	return group->GetCallbackResult();
}

uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
{
	const Industry *industry = object->u.industry.ind;
	TileIndex tile = object->u.industry.tile;

	if (object->scope == VSG_SCOPE_PARENT) {
		return TownGetVariable(variable, parameter, available, industry->town);
	}

	switch (variable) {
		case 0x80: return tile;
		case 0x81: return GB(tile, 8, 8);

		/* Pointer to the town the industry is associated with */
		case 0x82: return industry->town->index;
		case 0x83:
		case 0x84:
		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported

		/* Number of the layout */
		case 0x86: return industry->selected_layout;

		/* Ground type */
		case 0x87: return GetTerrainType(tile);

		/* Town zone */
		case 0x88: return GetTownRadiusGroup(industry->town, tile);

		/* Manhattan distance of the closest town */
		case 0x89: return min(DistanceManhattan(industry->town->xy, tile), 255);

		/* Lowest height of the tile */
		case 0x8A: return GetTileZ(tile);

		/* Distance to the nearest water/land tile */
		case 0x8B: return GetClosestWaterDistance(tile, (GetIndustrySpec(industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);

		/* Square of Euclidian distance from town */
		case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);

		/* 32 random bits */
		case 0x8F: return _industry_creation_random_bits;
	}

	/* None of the special ones, so try the general ones */
	return IndustryGetVariable(object, variable, parameter, available);
}

/**
 * Check that the industry callback allows creation of the industry.
 * @param tile %Tile to build the industry.
 * @param type Type of industry to build.
 * @param layout Layout number.
 * @param seed Seed for the random generator.
 * @param initial_random_bits The random bits the industry is going to have after construction.
 * @param founder Industry founder
 * @param creation_type The circumstances the industry is created under.
 * @return Succeeded or failed command.
 */
CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
{
	const IndustrySpec *indspec = GetIndustrySpec(type);

	ResolverObject object;
	const SpriteGroup *group;

	Industry ind;
	ind.index = INVALID_INDUSTRY;
	ind.location.tile = tile;
	ind.location.w = 0; // important to mark the industry invalid
	ind.type = type;
	ind.selected_layout = layout;
	ind.town = ClosestTownFromTile(tile, UINT_MAX);
	ind.random = initial_random_bits;
	ind.founder = founder;

	NewIndustryResolver(&object, tile, &ind, type);
	object.GetVariable = IndustryLocationGetVariable;
	object.callback = CBID_INDUSTRY_LOCATION;
	object.callback_param2 = creation_type;
	_industry_creation_random_bits = seed;

	group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);

	/* Unlike the "normal" cases, not having a valid result means we allow
	 * the building of the industry, as that's how it's done in TTDP. */
	if (group == NULL) return CommandCost();
	uint16 result = group->GetCallbackResult();
	if (result == 0x400 || result == CALLBACK_FAILED) return CommandCost();

	/* Copy some parameters from the registers to the error message text ref. stack */
	SwitchToErrorRefStack();
	PrepareTextRefStackUsage(4);
	SwitchToNormalRefStack();

	switch (result) {
		case 0x401: return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
		case 0x402: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
		case 0x403: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
		default:    return_cmd_error(GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + result));
	}
	NOT_REACHED();
}

/**
 * Check with callback #CBM_IND_AVAILABLE whether the industry can be built.
 * @param type Industry type to check.
 * @param creation_type Reason to construct a new industry.
 * @return If the industry has no callback or allows building, \c true is returned. Otherwise, \c false is returned.
 */
bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type)
{
	const IndustrySpec *indspec = GetIndustrySpec(type);

	if (HasBit(indspec->callback_mask, CBM_IND_AVAILABLE)) {
		uint16 res = GetIndustryCallback(CBID_INDUSTRY_AVAILABLE, 0, creation_type, NULL, type, INVALID_TILE);
		if (res != CALLBACK_FAILED) {
			return (res == 0);
		}
	}
	return true;
}

static int32 DerefIndProd(int field, bool use_register)
{
	return use_register ? (int32)GetRegister(field) : field;
}

/**
 * Get the industry production callback and apply it to the industry.
 * @param ind    the industry this callback has to be called for
 * @param reason the reason it is called (0 = incoming cargo, 1 = periodic tick callback)
 */
void IndustryProductionCallback(Industry *ind, int reason)
{
	const IndustrySpec *spec = GetIndustrySpec(ind->type);
	ResolverObject object;
	NewIndustryResolver(&object, ind->location.tile, ind, ind->type);
	if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
	int multiplier = 1;
	if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
	object.callback_param2 = reason;

	for (uint loop = 0;; loop++) {
		/* limit the number of calls to break infinite loops.
		 * 'loop' is provided as 16 bits to the newgrf, so abort when those are exceeded. */
		if (loop >= 0x10000) {
			/* display error message */
			SetDParamStr(0, spec->grf_prop.grffile->filename);
			SetDParam(1, spec->name);
			ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);

			/* abort the function early, this error isn't critical and will allow the game to continue to run */
			break;
		}

		SB(object.callback_param2, 8, 16, loop);
		const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup[0], &object);
		if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
		const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;

		bool deref = (group->version == 1);

		for (uint i = 0; i < 3; i++) {
			ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
		}
		for (uint i = 0; i < 2; i++) {
			ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
		}

		int32 again = DerefIndProd(group->again, deref);
		if (again == 0) break;

		SB(object.callback_param2, 24, 8, again);
	}

	SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
}

/**
 * Resolve a industry's spec and such so we can get a variable.
 * @param ro    The resolver object to fill.
 * @param index The industry ID to get the data from.
 */
void GetIndustryResolver(ResolverObject *ro, uint index)
{
	Industry *i = Industry::Get(index);
	NewIndustryResolver(ro, i->location.tile, i, i->type);
}

/**
 * Check whether an industry temporarily refuses to accept a certain cargo.
 * @param ind The industry to query.
 * @param cargo_type The cargo to get information about.
 * @pre cargo_type is in ind->accepts_cargo.
 * @return Whether the given industry refuses to accept this cargo type.
 */
bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
{
	assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]);

	const IndustrySpec *indspec = GetIndustrySpec(ind->type);
	if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
		uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
				0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile),
				ind, ind->type, ind->location.tile);
		return res == 0;
	}
	return false;
}