Files @ r28520:f9aebe299cae
Branch filter:

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

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

This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r12287:e460d5b47431
r12639:103f47c555cc
r12659:f65aabcdc037
r14248:a9050881acd7
r27400:742251af6ade
r18764:f6e8611401f3
r18770:fe0f5aab3963
r21410:ae5961f02724
r24345:d35de6ba4d03
r26094:7572e88decb3
r27090:1ea30951c0f3
r27090:1ea30951c0f3
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r18845:66bf168f1100
r12659:f65aabcdc037
r12287:e460d5b47431
r12287:e460d5b47431
r12639:103f47c555cc
r12639:103f47c555cc
r12639:103f47c555cc
r12648:24c912bd53f6
r12639:103f47c555cc
r12639:103f47c555cc
r12639:103f47c555cc
r12648:24c912bd53f6
r27884:803962be0328
r12648:24c912bd53f6
r16699:2a65db466640
r25743:0be6a18df69a
r12639:103f47c555cc
r12639:103f47c555cc
r25759:5af213b2a854
r12639:103f47c555cc
r25743:0be6a18df69a
r12639:103f47c555cc
r12639:103f47c555cc
r19369:05b3aefe07b5
r25207:880e11b8d85d
r25743:0be6a18df69a
r12639:103f47c555cc
r18501:8e3d905ea4bc
r18764:f6e8611401f3
r12639:103f47c555cc
r12854:adb7b59e6e99
r12639:103f47c555cc
r12639:103f47c555cc
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r25636:7c2fc0b1c9aa
r25759:5af213b2a854
r18845:66bf168f1100
r18845:66bf168f1100
r25759:5af213b2a854
r11992:e36dfa660636
r12005:1096fe8178fd
r12005:1096fe8178fd
r11992:e36dfa660636
r26025:e0dcf6dad103
r12405:ba094e765533
r26025:e0dcf6dad103
r11992:e36dfa660636
r12642:9f166415b666
r27106:2d95beb51290
r12642:9f166415b666
r25759:5af213b2a854
r12642:9f166415b666
r27106:2d95beb51290
r12642:9f166415b666
r25759:5af213b2a854
r12642:9f166415b666
r12642:9f166415b666
r12642:9f166415b666
r25759:5af213b2a854
r11992:e36dfa660636
r12642:9f166415b666
r27106:2d95beb51290
r12642:9f166415b666
r25759:5af213b2a854
r12642:9f166415b666
r27106:2d95beb51290
r12642:9f166415b666
r25759:5af213b2a854
r12642:9f166415b666
r12642:9f166415b666
r11992:e36dfa660636
r25759:5af213b2a854
r11992:e36dfa660636
r25636:7c2fc0b1c9aa
r25636:7c2fc0b1c9aa
r25759:5af213b2a854
r25636:7c2fc0b1c9aa
r25636:7c2fc0b1c9aa
r25207:880e11b8d85d
r11992:e36dfa660636
r11992:e36dfa660636
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r27106:2d95beb51290
r27106:2d95beb51290
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r18845:66bf168f1100
r12648:24c912bd53f6
r12648:24c912bd53f6
r23973:490b8210dd15
r12648:24c912bd53f6
r23962:50d7e69becd3
r12648:24c912bd53f6
r23972:7fe6caff6272
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r12642:9f166415b666
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r23972:7fe6caff6272
r12642:9f166415b666
r12659:f65aabcdc037
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r12659:f65aabcdc037
r12854:adb7b59e6e99
r12659:f65aabcdc037
r12659:f65aabcdc037
r11992:e36dfa660636
r11992:e36dfa660636
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r18845:66bf168f1100
r12663:7142663fe75d
r11992:e36dfa660636
r23972:7fe6caff6272
r12663:7142663fe75d
r12663:7142663fe75d
r12663:7142663fe75d
r12663:7142663fe75d
r12663:7142663fe75d
r12663:7142663fe75d
r12663:7142663fe75d
r12663:7142663fe75d
r11992:e36dfa660636
r18845:66bf168f1100
r18845:66bf168f1100
r19478:8cc1ec86658e
r18553:240b9a013aae
r19478:8cc1ec86658e
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r27106:2d95beb51290
r27106:2d95beb51290
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18845:66bf168f1100
r18845:66bf168f1100
r18553:240b9a013aae
r19478:8cc1ec86658e
r18553:240b9a013aae
r19478:8cc1ec86658e
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r12663:7142663fe75d
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r25636:7c2fc0b1c9aa
r25207:880e11b8d85d
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18764:f6e8611401f3
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18553:240b9a013aae
r18553:240b9a013aae
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r26095:00c14d52e378
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r26120:15c912177c88
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r27106:2d95beb51290
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r27106:2d95beb51290
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r27106:2d95beb51290
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r27106:2d95beb51290
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18770:fe0f5aab3963
r18553:240b9a013aae
r18845:66bf168f1100
r18845:66bf168f1100
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r12663:7142663fe75d
r12663:7142663fe75d
r19276:ad854f3053cd
r18444:c1e881f954d6
r18553:240b9a013aae
r12654:21eb6d3202f9
r11992:e36dfa660636
r12663:7142663fe75d
r19276:ad854f3053cd
r18553:240b9a013aae
r12663:7142663fe75d
r12663:7142663fe75d
r18553:240b9a013aae
r27106:2d95beb51290
r11992:e36dfa660636
r27106:2d95beb51290
r12663:7142663fe75d
r18553:240b9a013aae
r11992:e36dfa660636
r11992:e36dfa660636
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18845:66bf168f1100
r18845:66bf168f1100
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r11992:e36dfa660636
r18553:240b9a013aae
r18553:240b9a013aae
r27106:2d95beb51290
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r23608:a351d794c5b8
r18553:240b9a013aae
r24345:d35de6ba4d03
r27424:6a028979ce01
r24345:d35de6ba4d03
r25451:917146b539af
r24345:d35de6ba4d03
r24345:d35de6ba4d03
r24345:d35de6ba4d03
r24345:d35de6ba4d03
r18553:240b9a013aae
r18553:240b9a013aae
r24300:dc74d00736ca
r24300:dc74d00736ca
r27737:728d55b97775
r12663:7142663fe75d
r19255:5913e0fedb2e
r24300:dc74d00736ca
r19255:5913e0fedb2e
r18553:240b9a013aae
r27737:728d55b97775
r18553:240b9a013aae
r24300:dc74d00736ca
r24300:dc74d00736ca
r24300:dc74d00736ca
r24300:dc74d00736ca
r24300:dc74d00736ca
r18553:240b9a013aae
r18553:240b9a013aae
r18668:b656d614c4fd
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r12663:7142663fe75d
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18845:66bf168f1100
r18845:66bf168f1100
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r27106:2d95beb51290
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r23607:36c15679007d
r18553:240b9a013aae
r15173:a59afd6301a6
r11992:e36dfa660636
r18553:240b9a013aae
r18553:240b9a013aae
r11992:e36dfa660636
r27438:22e72ba90974
r23040:680908bb49de
r23040:680908bb49de
r27438:22e72ba90974
r27438:22e72ba90974
r27438:22e72ba90974
r27438:22e72ba90974
r23040:680908bb49de
r23040:680908bb49de
r27438:22e72ba90974
r27438:22e72ba90974
r27438:22e72ba90974
r27438:22e72ba90974
r27438:22e72ba90974
r11992:e36dfa660636
r11992:e36dfa660636
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r27421:e8c2cdc1e8e6
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r11992:e36dfa660636
r18553:240b9a013aae
r11992:e36dfa660636
r18553:240b9a013aae
r18553:240b9a013aae
r11992:e36dfa660636
r18845:66bf168f1100
r18845:66bf168f1100
r18553:240b9a013aae
r19478:8cc1ec86658e
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r24300:dc74d00736ca
r27106:2d95beb51290
r12663:7142663fe75d
r18553:240b9a013aae
r18553:240b9a013aae
r27106:2d95beb51290
r18553:240b9a013aae
r18553:240b9a013aae
r24345:d35de6ba4d03
r24345:d35de6ba4d03
r27424:6a028979ce01
r24345:d35de6ba4d03
r25451:917146b539af
r24345:d35de6ba4d03
r24345:d35de6ba4d03
r24345:d35de6ba4d03
r24345:d35de6ba4d03
r11992:e36dfa660636
r18553:240b9a013aae
r24301:7ff717a1243f
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r11992:e36dfa660636
r12663:7142663fe75d
r27106:2d95beb51290
r18553:240b9a013aae
r18553:240b9a013aae
r23607:36c15679007d
r12663:7142663fe75d
r18553:240b9a013aae
r27429:1d7757745f42
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r11992:e36dfa660636
r11992:e36dfa660636
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r12663:7142663fe75d
r18553:240b9a013aae
r18553:240b9a013aae
r12663:7142663fe75d
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r11992:e36dfa660636
r11992:e36dfa660636
r18845:66bf168f1100
r27090:1ea30951c0f3
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r23972:7fe6caff6272
r12648:24c912bd53f6
r12648:24c912bd53f6
r25636:7c2fc0b1c9aa
r25207:880e11b8d85d
r18501:8e3d905ea4bc
r18764:f6e8611401f3
r12648:24c912bd53f6
r12648:24c912bd53f6
r25636:7c2fc0b1c9aa
r25207:880e11b8d85d
r12648:24c912bd53f6
r18501:8e3d905ea4bc
r18764:f6e8611401f3
r12648:24c912bd53f6
r12659:f65aabcdc037
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r25636:7c2fc0b1c9aa
r25636:7c2fc0b1c9aa
r25636:7c2fc0b1c9aa
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r20838:c4bef7c0db02
r12659:f65aabcdc037
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r20838:c4bef7c0db02
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r11992:e36dfa660636
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r18553:240b9a013aae
r19010:67c7dc093316
r18553:240b9a013aae
r11992:e36dfa660636
r12659:f65aabcdc037
r18553:240b9a013aae
r18553:240b9a013aae
r12854:adb7b59e6e99
r27090:1ea30951c0f3
r11992:e36dfa660636
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r19478:8cc1ec86658e
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r11992:e36dfa660636
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r27106:2d95beb51290
r12648:24c912bd53f6
r12648:24c912bd53f6
r27106:2d95beb51290
r19276:ad854f3053cd
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r11992:e36dfa660636
r12648:24c912bd53f6
r12648:24c912bd53f6
r23538:8df50944b27a
r12648:24c912bd53f6
r23972:7fe6caff6272
r12648:24c912bd53f6
r27106:2d95beb51290
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r23431:60a46e3b7cd7
r23431:60a46e3b7cd7
r23431:60a46e3b7cd7
r23431:60a46e3b7cd7
r23535:9604c18808f0
r12648:24c912bd53f6
r12648:24c912bd53f6
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r12648:24c912bd53f6
r11992:e36dfa660636
r12648:24c912bd53f6
r12648:24c912bd53f6
r23972:7fe6caff6272
r12648:24c912bd53f6
r12648:24c912bd53f6
r27106:2d95beb51290
r26179:ad5479cbfaa1
r26179:ad5479cbfaa1
r26179:ad5479cbfaa1
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r11992:e36dfa660636
r27106:2d95beb51290
r23536:ce42deb0b32d
r23536:ce42deb0b32d
r23536:ce42deb0b32d
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r12648:24c912bd53f6
r11992:e36dfa660636
r12648:24c912bd53f6
r11992:e36dfa660636
r11992:e36dfa660636
r11992:e36dfa660636
r12648:24c912bd53f6
r12648:24c912bd53f6
r11992:e36dfa660636
/*
 * 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 subsidy.cpp Handling of subsidies. */

#include "stdafx.h"
#include "company_func.h"
#include "industry.h"
#include "town.h"
#include "news_func.h"
#include "ai/ai.hpp"
#include "station_base.h"
#include "strings_func.h"
#include "window_func.h"
#include "subsidy_base.h"
#include "subsidy_func.h"
#include "core/pool_func.hpp"
#include "core/random_func.hpp"
#include "core/container_func.hpp"
#include "game/game.hpp"
#include "command_func.h"
#include "string_func.h"
#include "tile_cmd.h"
#include "subsidy_cmd.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"

#include "table/strings.h"

#include "safeguards.h"

SubsidyPool _subsidy_pool("Subsidy"); ///< Pool for the subsidies.
INSTANTIATE_POOL_METHODS(Subsidy)

/**
 * Marks subsidy as awarded, creates news and AI event
 * @param company awarded company
 */
void Subsidy::AwardTo(CompanyID company)
{
	assert(!this->IsAwarded());

	this->awarded = company;
	this->remaining = _settings_game.difficulty.subsidy_duration * CalendarTime::MONTHS_IN_YEAR;

	SetDParam(0, company);
	NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));

	/* Add a news item */
	std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded, 1);

	SetDParamStr(0, company_name->string);
	AddNewsItem(
		STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
		NT_SUBSIDIES, NF_NORMAL,
		reftype.first, this->src, reftype.second, this->dst,
		company_name
	);
	AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
	Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));

	InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
}

/**
 * Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
 * @param s %Subsidy being printed.
 * @param mode Type of subsidy news message to decide on parameter format.
 * @param parameter_offset The location/index in the String DParams to start decoding the subsidy's parameters. Defaults to 0.
 * @return Reference of the subsidy in the news system.
 */
std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
{
	NewsReferenceType reftype1 = NR_NONE;
	NewsReferenceType reftype2 = NR_NONE;

	/* Always use the plural form of the cargo name - trying to decide between plural or singular causes issues for translations */
	const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
	SetDParam(parameter_offset, cs->name);

	switch (s->src_type) {
		case SourceType::Industry:
			reftype1 = NR_INDUSTRY;
			SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
			break;
		case SourceType::Town:
			reftype1 = NR_TOWN;
			SetDParam(parameter_offset + 1, STR_TOWN_NAME);
			break;
		default: NOT_REACHED();
	}
	SetDParam(parameter_offset + 2, s->src);

	switch (s->dst_type) {
		case SourceType::Industry:
			reftype2 = NR_INDUSTRY;
			SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
			break;
		case SourceType::Town:
			reftype2 = NR_TOWN;
			SetDParam(parameter_offset + 4, STR_TOWN_NAME);
			break;
		default: NOT_REACHED();
	}
	SetDParam(parameter_offset + 5, s->dst);

	/* If the subsidy is being offered or awarded, the news item mentions the subsidy duration. */
	if (mode == SubsidyDecodeParamType::NewsOffered || mode == SubsidyDecodeParamType::NewsAwarded) {
		SetDParam(parameter_offset + 7, _settings_game.difficulty.subsidy_duration);
	}

	return std::pair<NewsReferenceType, NewsReferenceType>(reftype1, reftype2);
}

/**
 * Sets a flag indicating that given town/industry is part of subsidised route.
 * @param type is it a town or an industry?
 * @param index index of town/industry
 * @param flag flag to set
 */
static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
{
	switch (type) {
		case SourceType::Industry: Industry::Get(index)->part_of_subsidy |= flag; return;
		case SourceType::Town:   Town::Get(index)->cache.part_of_subsidy |= flag; return;
		default: NOT_REACHED();
	}
}

/** Perform a full rebuild of the subsidies cache. */
void RebuildSubsidisedSourceAndDestinationCache()
{
	for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = POS_NONE;

	for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE;

	for (const Subsidy *s : Subsidy::Iterate()) {
		SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
		SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
	}
}

/**
 * Delete the subsidies associated with a given cargo source type and id.
 * @param type  Cargo source type of the id.
 * @param index Id to remove.
 */
void DeleteSubsidyWith(SourceType type, SourceID index)
{
	bool dirty = false;

	for (Subsidy *s : Subsidy::Iterate()) {
		if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
			delete s;
			dirty = true;
		}
	}

	if (dirty) {
		InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
		RebuildSubsidisedSourceAndDestinationCache();
	}
}

/**
 * Check whether a specific subsidy already exists.
 * @param cargo Cargo type.
 * @param src_type Type of source of the cargo, affects interpretation of \a src.
 * @param src Id of the source.
 * @param dst_type Type of the destination of the cargo, affects interpretation of \a dst.
 * @param dst Id of the destination.
 * @return \c true if the subsidy already exists, \c false if not.
 */
static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
{
	for (const Subsidy *s : Subsidy::Iterate()) {
		if (s->cargo_type == cargo &&
				s->src_type == src_type && s->src == src &&
				s->dst_type == dst_type && s->dst == dst) {
			return true;
		}
	}
	return false;
}

/**
 * Checks if the source and destination of a subsidy are inside the distance limit.
 * @param src_type Type of \a src.
 * @param src      Index of source.
 * @param dst_type Type of \a dst.
 * @param dst      Index of destination.
 * @return True if they are inside the distance limit.
 */
static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
{
	TileIndex tile_src = (src_type == SourceType::Town) ? Town::Get(src)->xy : Industry::Get(src)->location.tile;
	TileIndex tile_dst = (dst_type == SourceType::Town) ? Town::Get(dst)->xy : Industry::Get(dst)->location.tile;

	return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
}

/**
 * Creates a subsidy with the given parameters.
 * @param cid      Subsidised cargo.
 * @param src_type Type of \a src.
 * @param src      Index of source.
 * @param dst_type Type of \a dst.
 * @param dst      Index of destination.
 */
void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
{
	Subsidy *s = new Subsidy();
	s->cargo_type = cid;
	s->src_type = src_type;
	s->src = src;
	s->dst_type = dst_type;
	s->dst = dst;
	s->remaining = SUBSIDY_OFFER_MONTHS;
	s->awarded = INVALID_COMPANY;

	std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsOffered);
	AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
	SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
	SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
	AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
	Game::NewEvent(new ScriptEventSubsidyOffer(s->index));

	InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
}

/**
 * Create a new subsidy.
 * @param flags type of operation
 * @param cid CargoID of subsidy.
 * @param src_type SourceType of source.
 * @param src SourceID of source.
 * @param dst_type SourceType of destination.
 * @param dst SourceID of destination.
 * @return the cost of this operation or an error
 */
CommandCost CmdCreateSubsidy(DoCommandFlag flags, CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
{
	if (!Subsidy::CanAllocateItem()) return CMD_ERROR;

	if (_current_company != OWNER_DEITY) return CMD_ERROR;

	if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;

	switch (src_type) {
		case SourceType::Town:
			if (!Town::IsValidID(src)) return CMD_ERROR;
			break;
		case SourceType::Industry:
			if (!Industry::IsValidID(src)) return CMD_ERROR;
			break;
		default:
			return CMD_ERROR;
	}
	switch (dst_type) {
		case SourceType::Town:
			if (!Town::IsValidID(dst)) return CMD_ERROR;
			break;
		case SourceType::Industry:
			if (!Industry::IsValidID(dst)) return CMD_ERROR;
			break;
		default:
			return CMD_ERROR;
	}

	if (flags & DC_EXEC) {
		CreateSubsidy(cid, src_type, src, dst_type, dst);
	}

	return CommandCost();
}

/**
 * Tries to create a passenger subsidy between two towns.
 * @return True iff the subsidy was created.
 */
bool FindSubsidyPassengerRoute()
{
	if (!Subsidy::CanAllocateItem()) return false;

	const Town *src = Town::GetRandom();
	if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
			src->GetPercentTransported(CT_PASSENGERS) > SUBSIDY_MAX_PCT_TRANSPORTED) {
		return false;
	}

	const Town *dst = Town::GetRandom();
	if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
		return false;
	}

	if (DistanceManhattan(src->xy, dst->xy) > SUBSIDY_MAX_DISTANCE) return false;
	if (CheckSubsidyDuplicate(CT_PASSENGERS, SourceType::Town, src->index, SourceType::Town, dst->index)) return false;

	CreateSubsidy(CT_PASSENGERS, SourceType::Town, src->index, SourceType::Town, dst->index);

	return true;
}

bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src);


/**
 * Tries to create a cargo subsidy with a town as source.
 * @return True iff the subsidy was created.
 */
bool FindSubsidyTownCargoRoute()
{
	if (!Subsidy::CanAllocateItem()) return false;

	SourceType src_type = SourceType::Town;

	/* Select a random town. */
	const Town *src_town = Town::GetRandom();
	if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;

	/* Calculate the produced cargo of houses around town center. */
	CargoArray town_cargo_produced{};
	TileArea ta = TileArea(src_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
	for (TileIndex tile : ta) {
		if (IsTileType(tile, MP_HOUSE)) {
			AddProducedCargo(tile, town_cargo_produced);
		}
	}

	/* Passenger subsidies are not handled here. */
	town_cargo_produced[CT_PASSENGERS] = 0;

	uint8_t cargo_count = town_cargo_produced.GetCount();

	/* No cargo produced at all? */
	if (cargo_count == 0) return false;

	/* Choose a random cargo that is produced in the town. */
	uint8_t cargo_number = RandomRange(cargo_count);
	CargoID cid;
	for (cid = 0; cid < NUM_CARGO; cid++) {
		if (town_cargo_produced[cid] > 0) {
			if (cargo_number == 0) break;
			cargo_number--;
		}
	}

	/* Avoid using invalid NewGRF cargoes. */
	if (!CargoSpec::Get(cid)->IsValid() ||
			_settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
		return false;
	}

	/* Quit if the percentage transported is large enough. */
	if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;

	SourceID src = src_town->index;

	return FindSubsidyCargoDestination(cid, src_type, src);
}

/**
 * Tries to create a cargo subsidy with an industry as source.
 * @return True iff the subsidy was created.
 */
bool FindSubsidyIndustryCargoRoute()
{
	if (!Subsidy::CanAllocateItem()) return false;

	SourceType src_type = SourceType::Industry;

	/* Select a random industry. */
	const Industry *src_ind = Industry::GetRandom();
	if (src_ind == nullptr) return false;

	uint trans, total;

	CargoID cid;

	/* Randomize cargo type */
	int num_cargos = std::count_if(std::begin(src_ind->produced), std::end(src_ind->produced), [](const auto &p) { return IsValidCargoID(p.cargo); });
	if (num_cargos == 0) return false; // industry produces nothing
	int cargo_num = RandomRange(num_cargos) + 1;

	auto it = std::begin(src_ind->produced);
	for (/* nothing */; it != std::end(src_ind->produced); ++it) {
		if (IsValidCargoID(it->cargo)) cargo_num--;
		if (cargo_num == 0) break;
	}
	assert(it != std::end(src_ind->produced)); // indicates loop didn't end as intended

	cid = it->cargo;
	trans = it->history[LAST_MONTH].PctTransported();
	total = it->history[LAST_MONTH].production;

	/* Quit if no production in this industry
	 * or if the pct transported is already large enough
	 * or if the cargo is automatically distributed */
	if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
			!IsValidCargoID(cid) ||
			_settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
		return false;
	}

	SourceID src = src_ind->index;

	return FindSubsidyCargoDestination(cid, src_type, src);
}

/**
 * Tries to find a suitable destination for the given source and cargo.
 * @param cid      Subsidized cargo.
 * @param src_type Type of \a src.
 * @param src      Index of source.
 * @return True iff the subsidy was created.
 */
bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
{
	/* Choose a random destination. */
	SourceType dst_type = Chance16(1, 2) ? SourceType::Town : SourceType::Industry;

	SourceID dst;
	switch (dst_type) {
		case SourceType::Town: {
			/* Select a random town. */
			const Town *dst_town = Town::GetRandom();

			/* Calculate cargo acceptance of houses around town center. */
			CargoArray town_cargo_accepted{};
			TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
			for (TileIndex tile : ta) {
				if (IsTileType(tile, MP_HOUSE)) {
					AddAcceptedCargo(tile, town_cargo_accepted, nullptr);
				}
			}

			/* Check if the town can accept this cargo. */
			if (town_cargo_accepted[cid] < 8) return false;

			dst = dst_town->index;
			break;
		}

		case SourceType::Industry: {
			/* Select a random industry. */
			const Industry *dst_ind = Industry::GetRandom();
			if (dst_ind == nullptr) return false;

			/* The industry must accept the cargo */
			if (!dst_ind->IsCargoAccepted(cid)) return false;

			dst = dst_ind->index;
			break;
		}

		default: NOT_REACHED();
	}

	/* Check that the source and the destination are not the same. */
	if (src_type == dst_type && src == dst) return false;

	/* Check distance between source and destination. */
	if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;

	/* Avoid duplicate subsidies. */
	if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;

	CreateSubsidy(cid, src_type, src, dst_type, dst);

	return true;
}

/** Perform the monthly update of open subsidies, and try to create a new one. */
static IntervalTimer<TimerGameCalendar> _subsidies_monthly({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::SUBSIDY}, [](auto)
{
	bool modified = false;

	for (Subsidy *s : Subsidy::Iterate()) {
		if (--s->remaining == 0) {
			if (!s->IsAwarded()) {
				std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
				AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
				AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
				Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
			} else {
				if (s->awarded == _local_company) {
					std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
					AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
				}
				AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
				Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
			}
			delete s;
			modified = true;
		}
	}

	if (modified) {
		RebuildSubsidisedSourceAndDestinationCache();
	} else if (_settings_game.difficulty.subsidy_duration == 0) {
		/* If subsidy duration is set to 0, subsidies are disabled, so bail out. */
		return;
	} else if (_settings_game.linkgraph.distribution_pax != DT_MANUAL &&
			   _settings_game.linkgraph.distribution_mail != DT_MANUAL &&
			   _settings_game.linkgraph.distribution_armoured != DT_MANUAL &&
			   _settings_game.linkgraph.distribution_default != DT_MANUAL) {
		/* Return early if there are no manually distributed cargoes and if we
		 * don't need to invalidate the subsidies window. */
		return;
	}

	bool passenger_subsidy = false;
	bool town_subsidy = false;
	bool industry_subsidy = false;

	int random_chance = RandomRange(16);

	if (random_chance < 2 && _settings_game.linkgraph.distribution_pax == DT_MANUAL) {
		/* There is a 1/8 chance each month of generating a passenger subsidy. */
		int n = 1000;

		do {
			passenger_subsidy = FindSubsidyPassengerRoute();
		} while (!passenger_subsidy && n--);
	} else if (random_chance == 2) {
		/* Cargo subsidies with a town as a source have a 1/16 chance. */
		int n = 1000;

		do {
			town_subsidy = FindSubsidyTownCargoRoute();
		} while (!town_subsidy && n--);
	} else if (random_chance == 3) {
		/* Cargo subsidies with an industry as a source have a 1/16 chance. */
		int n = 1000;

		do {
			industry_subsidy = FindSubsidyIndustryCargoRoute();
		} while (!industry_subsidy && n--);
	}

	modified |= passenger_subsidy || town_subsidy || industry_subsidy;

	if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
});

/**
 * Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
 * @param cargo_type type of cargo
 * @param company company delivering the cargo
 * @param src_type type of \a src
 * @param src index of source
 * @param st station where the cargo is delivered to
 * @return is the delivery subsidised?
 */
bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
{
	/* If the source isn't subsidised, don't continue */
	if (src == INVALID_SOURCE) return false;
	switch (src_type) {
		case SourceType::Industry:
			if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
			break;
		case SourceType::Town:
			if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
			break;
		default: return false;
	}

	/* Remember all towns near this station (at least one house in its catchment radius)
	 * which are destination of subsidised path. Do that only if needed */
	std::vector<const Town *> towns_near;
	if (!st->rect.IsEmpty()) {
		for (const Subsidy *s : Subsidy::Iterate()) {
			/* Don't create the cache if there is no applicable subsidy with town as destination */
			if (s->dst_type != SourceType::Town) continue;
			if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
			if (s->IsAwarded() && s->awarded != company) continue;

			BitmapTileIterator it(st->catchment_tiles);
			for (TileIndex tile = it; tile != INVALID_TILE; tile = ++it) {
				if (!IsTileType(tile, MP_HOUSE)) continue;
				const Town *t = Town::GetByTile(tile);
				if (t->cache.part_of_subsidy & POS_DST) include(towns_near, t);
			}
			break;
		}
	}

	bool subsidised = false;

	/* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
	 * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
	for (Subsidy *s : Subsidy::Iterate()) {
		if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
			switch (s->dst_type) {
				case SourceType::Industry:
					for (const auto &i : st->industries_near) {
						if (s->dst == i.industry->index) {
							assert(i.industry->part_of_subsidy & POS_DST);
							subsidised = true;
							if (!s->IsAwarded()) s->AwardTo(company);
						}
					}
					break;
				case SourceType::Town:
					for (const Town *tp : towns_near) {
						if (s->dst == tp->index) {
							assert(tp->cache.part_of_subsidy & POS_DST);
							subsidised = true;
							if (!s->IsAwarded()) s->AwardTo(company);
						}
					}
					break;
				default:
					NOT_REACHED();
			}
		}
	}

	return subsidised;
}