Files @ r21640:6ebf6839752c
Branch filter:

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

rubidium
(svn r26772) -Cleanup [Squirrel]: remove traces to SQUNICODE define
  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
r5665:f732864744b6
r5665:f732864744b6
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r5665:f732864744b6
r5665:f732864744b6
r10208:ef8fcc3dc4ca
r15305:1b8d2fb07238
r7469:b4e494b560de
r17267:4f82ca9bd495
r8140:9424f012f6a2
r8275:5932486ddd21
r10123:310b4a36ab6c
r10155:4ffdd10e735b
r16069:5049a8060a4d
r11967:df0600d2c7e7
r12218:9b04ff1ad183
r12215:7e5ec1b6625d
r12816:33099b7286d4
r14248:a9050881acd7
r20252:76a6f1c78ce7
r20346:401f2bc98df5
r5665:f732864744b6
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r17625:366001a31c88
r11967:df0600d2c7e7
r11967:df0600d2c7e7
r11967:df0600d2c7e7
r20846:9aff10202f3d
r21255:d6aa9ab2e6d4
r20846:9aff10202f3d
r12417:860684b5a219
r12417:860684b5a219
r12417:860684b5a219
r12417:860684b5a219
r15306:1e95a9ffc6fb
r15306:1e95a9ffc6fb
r15306:1e95a9ffc6fb
r15306:1e95a9ffc6fb
r15306:1e95a9ffc6fb
r16071:70ab58c585e0
r16069:5049a8060a4d
r16069:5049a8060a4d
r16069:5049a8060a4d
r16069:5049a8060a4d
r15307:f844b2fa748c
r15307:f844b2fa748c
r12417:860684b5a219
r12417:860684b5a219
r12212:e749da7519b4
r12432:c48499a7baa0
r13838:8fec527b40c1
r13838:8fec527b40c1
r12212:e749da7519b4
r12212:e749da7519b4
r12212:e749da7519b4
r12212:e749da7519b4
r12212:e749da7519b4
r5665:f732864744b6
r12212:e749da7519b4
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r20252:76a6f1c78ce7
r20252:76a6f1c78ce7
r6420:01087f989fd1
r6420:01087f989fd1
r6420:01087f989fd1
r6420:01087f989fd1
r5665:f732864744b6
r5665:f732864744b6
r17355:e67faa335e9c
r17355:e67faa335e9c
r17355:e67faa335e9c
r17355:e67faa335e9c
r17355:e67faa335e9c
r17355:e67faa335e9c
r7413:3ccdde9800e0
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r7453:707d7f97be45
r7453:707d7f97be45
r12029:5b077ec055c0
r12029:5b077ec055c0
r12374:50d157b51c2c
r11977:bc3933e2c9d0
r10155:4ffdd10e735b
r10155:4ffdd10e735b
r20252:76a6f1c78ce7
r20252:76a6f1c78ce7
r20420:0d08d63e1ea5
r20420:0d08d63e1ea5
r20420:0d08d63e1ea5
r21199:0d94a08beeae
r21199:0d94a08beeae
r20420:0d08d63e1ea5
r20420:0d08d63e1ea5
r20420:0d08d63e1ea5
r20252:76a6f1c78ce7
r20252:76a6f1c78ce7
r20420:0d08d63e1ea5
r20420:0d08d63e1ea5
r20420:0d08d63e1ea5
r20420:0d08d63e1ea5
r20333:a5be8ec53733
r20252:76a6f1c78ce7
r20252:76a6f1c78ce7
r12063:19a5d1ea1b70
r12063:19a5d1ea1b70
r12063:19a5d1ea1b70
r12064:dab8b4af4fd5
r12063:19a5d1ea1b70
r12063:19a5d1ea1b70
r20252:76a6f1c78ce7
r20252:76a6f1c78ce7
r20252:76a6f1c78ce7
r12063:19a5d1ea1b70
r12063:19a5d1ea1b70
r17772:ce3cc7016cdc
r17772:ce3cc7016cdc
r17772:ce3cc7016cdc
r18353:55e21e815cc4
r18353:55e21e815cc4
r18353:55e21e815cc4
r18353:55e21e815cc4
r18353:55e21e815cc4
r18353:55e21e815cc4
r5665:f732864744b6
r5665:f732864744b6
r15305:1b8d2fb07238
r5665:f732864744b6
r12642:9f166415b666
r5665:f732864744b6
r10123:310b4a36ab6c
r10123:310b4a36ab6c
r10123:310b4a36ab6c
r7010:fd8e3ec6f727
r20050:e220620362e7
r7010:fd8e3ec6f727
r11996:57a5615c0893
r13285:765ad409980c
r7376:c436274a8ff0
r7376:c436274a8ff0
r7469:b4e494b560de
r7469:b4e494b560de
r12315:655b1bd7e9be
r12315:655b1bd7e9be
r12315:655b1bd7e9be
r12315:655b1bd7e9be
r13004:7de4c6068404
r12315:655b1bd7e9be
r12315:655b1bd7e9be
r12315:655b1bd7e9be
r12315:655b1bd7e9be
r12315:655b1bd7e9be
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r11978:b70e888fac6f
r7469:b4e494b560de
r13842:5f8f28d5f86b
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r11979:a450390d0f16
r19821:021f5f9159cc
r12295:bb9023f54628
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r15610:623a23fb6560
r15610:623a23fb6560
r15613:193c12018337
r15613:193c12018337
r12309:02ac22c76734
r5721:2780cdf77488
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r5721:2780cdf77488
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r5721:2780cdf77488
r5721:2780cdf77488
r17099:f140f6165f35
r17099:f140f6165f35
r17099:f140f6165f35
r17099:f140f6165f35
r17099:f140f6165f35
r6823:c5b9c08647ba
r5665:f732864744b6
r12500:5e5dd00d1e93
r5665:f732864744b6
r5665:f732864744b6
r10488:cbe4b3cd8549
r5665:f732864744b6
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r12500:5e5dd00d1e93
r12500:5e5dd00d1e93
r12329:a23a488e4b7e
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r12493:6da69ce1ff42
r5998:69c1cce26321
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r12493:6da69ce1ff42
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r12492:6a787b05475e
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r12493:6da69ce1ff42
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r15610:623a23fb6560
r15610:623a23fb6560
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r12500:5e5dd00d1e93
r12500:5e5dd00d1e93
r12500:5e5dd00d1e93
r12500:5e5dd00d1e93
r14849:27386629fac1
r10290:f54b39ef78a7
r14611:79a9ea376bde
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r10290:f54b39ef78a7
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12641:561071636083
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r17625:366001a31c88
r17625:366001a31c88
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12329:a23a488e4b7e
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12816:33099b7286d4
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12640:28892ff3883e
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12641:561071636083
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r13165:024ce8aa50e8
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r12229:0a7acf0ff382
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5676:e4a5055c9100
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r19944:25a78576fb5e
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5676:e4a5055c9100
r16537:b136370bd3c2
r16537:b136370bd3c2
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5676:e4a5055c9100
r14737:40252806a75b
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r14736:e919fd200318
r5678:ea7f4cfadfcd
r10241:1669d9ba852b
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r10241:1669d9ba852b
r14736:e919fd200318
r11363:6906c490a00e
r11363:6906c490a00e
r12329:a23a488e4b7e
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r9413:fcf267325763
r5676:e4a5055c9100
r14737:40252806a75b
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r14737:40252806a75b
r5676:e4a5055c9100
r5676:e4a5055c9100
r14752:ebcf936bf2ae
r5676:e4a5055c9100
r14737:40252806a75b
r14737:40252806a75b
r14737:40252806a75b
r14737:40252806a75b
r14752:ebcf936bf2ae
r14737:40252806a75b
r14752:ebcf936bf2ae
r5676:e4a5055c9100
r5676:e4a5055c9100
r10260:88d82a40120c
r10260:88d82a40120c
r10260:88d82a40120c
r10260:88d82a40120c
r10260:88d82a40120c
r10260:88d82a40120c
r10260:88d82a40120c
r10260:88d82a40120c
r10260:88d82a40120c
r11363:6906c490a00e
r5676:e4a5055c9100
r16740:27be434a443c
r16740:27be434a443c
r5676:e4a5055c9100
r12519:e52fcaadf579
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r12515:abd5af2c2638
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r14753:66a6d88d47ea
r5676:e4a5055c9100
r14753:66a6d88d47ea
r14753:66a6d88d47ea
r5678:ea7f4cfadfcd
r14753:66a6d88d47ea
r14753:66a6d88d47ea
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r14736:e919fd200318
r5676:e4a5055c9100
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r12329:a23a488e4b7e
r5676:e4a5055c9100
r5676:e4a5055c9100
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
/* $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 station.cpp Implementation of the station base class. */

#include "stdafx.h"
#include "company_func.h"
#include "company_base.h"
#include "roadveh.h"
#include "viewport_func.h"
#include "date_func.h"
#include "command_func.h"
#include "news_func.h"
#include "aircraft.h"
#include "vehiclelist.h"
#include "core/pool_func.hpp"
#include "station_base.h"
#include "roadstop_base.h"
#include "industry.h"
#include "core/random_func.hpp"
#include "linkgraph/linkgraph.h"
#include "linkgraph/linkgraphschedule.h"

#include "table/strings.h"

#include "safeguards.h"

/** The pool of stations. */
StationPool _station_pool("Station");
INSTANTIATE_POOL_METHODS(Station)

typedef StationIDStack::SmallStackPool StationIDStackPool;
template<> StationIDStackPool StationIDStack::_pool = StationIDStackPool();

BaseStation::~BaseStation()
{
	free(this->name);
	free(this->speclist);

	if (CleaningPool()) return;

	Owner owner = this->owner;
	if (!Company::IsValidID(owner)) owner = _local_company;
	if (!Company::IsValidID(owner)) return; // Spectators
	DeleteWindowById(WC_TRAINS_LIST,   VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN,    owner, this->index).Pack());
	DeleteWindowById(WC_ROADVEH_LIST,  VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD,     owner, this->index).Pack());
	DeleteWindowById(WC_SHIPS_LIST,    VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP,     owner, this->index).Pack());
	DeleteWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, owner, this->index).Pack());

	this->sign.MarkDirty();
}

Station::Station(TileIndex tile) :
	SpecializedStation<Station, false>(tile),
	bus_station(INVALID_TILE, 0, 0),
	truck_station(INVALID_TILE, 0, 0),
	dock_tile(INVALID_TILE),
	indtype(IT_INVALID),
	time_since_load(255),
	time_since_unload(255),
	last_vehicle_type(VEH_INVALID)
{
	/* this->random_bits is set in Station::AddFacility() */
}

/**
 * Clean up a station by clearing vehicle orders, invalidating windows and
 * removing link stats.
 * Aircraft-Hangar orders need special treatment here, as the hangars are
 * actually part of a station (tiletype is STATION), but the order type
 * is OT_GOTO_DEPOT.
 */
Station::~Station()
{
	if (CleaningPool()) {
		for (CargoID c = 0; c < NUM_CARGO; c++) {
			this->goods[c].cargo.OnCleanPool();
		}
		return;
	}

	while (!this->loading_vehicles.empty()) {
		this->loading_vehicles.front()->LeaveStation();
	}

	Aircraft *a;
	FOR_ALL_AIRCRAFT(a) {
		if (!a->IsNormalAircraft()) continue;
		if (a->targetairport == this->index) a->targetairport = INVALID_STATION;
	}

	for (CargoID c = 0; c < NUM_CARGO; ++c) {
		LinkGraph *lg = LinkGraph::GetIfValid(this->goods[c].link_graph);
		if (lg == NULL) continue;

		for (NodeID node = 0; node < lg->Size(); ++node) {
			Station *st = Station::Get((*lg)[node].Station());
			st->goods[c].flows.erase(this->index);
			if ((*lg)[node][this->goods[c].node].LastUpdate() != INVALID_DATE) {
				st->goods[c].flows.DeleteFlows(this->index);
				RerouteCargo(st, c, this->index, st->index);
			}
		}
		lg->RemoveNode(this->goods[c].node);
		if (lg->Size() == 0) {
			LinkGraphSchedule::Instance()->Unqueue(lg);
			delete lg;
		}
	}

	Vehicle *v;
	FOR_ALL_VEHICLES(v) {
		/* Forget about this station if this station is removed */
		if (v->last_station_visited == this->index) {
			v->last_station_visited = INVALID_STATION;
		}
		if (v->last_loading_station == this->index) {
			v->last_loading_station = INVALID_STATION;
		}
	}

	/* Clear the persistent storage. */
	delete this->airport.psa;

	if (this->owner == OWNER_NONE) {
		/* Invalidate all in case of oil rigs. */
		InvalidateWindowClassesData(WC_STATION_LIST, 0);
	} else {
		InvalidateWindowData(WC_STATION_LIST, this->owner, 0);
	}

	DeleteWindowById(WC_STATION_VIEW, index);

	/* Now delete all orders that go to the station */
	RemoveOrderFromAllVehicles(OT_GOTO_STATION, this->index);

	/* Remove all news items */
	DeleteStationNews(this->index);

	for (CargoID c = 0; c < NUM_CARGO; c++) {
		this->goods[c].cargo.Truncate();
	}

	CargoPacket::InvalidateAllFrom(this->index);
}


/**
 * Invalidating of the JoinStation window has to be done
 * after removing item from the pool.
 * @param index index of deleted item
 */
void BaseStation::PostDestructor(size_t index)
{
	InvalidateWindowData(WC_SELECT_STATION, 0, 0);
}

/**
 * Get the primary road stop (the first road stop) that the given vehicle can load/unload.
 * @param v the vehicle to get the first road stop for
 * @return the first roadstop that this vehicle can load at
 */
RoadStop *Station::GetPrimaryRoadStop(const RoadVehicle *v) const
{
	RoadStop *rs = this->GetPrimaryRoadStop(v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK);

	for (; rs != NULL; rs = rs->next) {
		/* The vehicle cannot go to this roadstop (different roadtype) */
		if ((GetRoadTypes(rs->xy) & v->compatible_roadtypes) == ROADTYPES_NONE) continue;
		/* The vehicle is articulated and can therefore not go to a standard road stop. */
		if (IsStandardRoadStopTile(rs->xy) && v->HasArticulatedPart()) continue;

		/* The vehicle can actually go to this road stop. So, return it! */
		break;
	}

	return rs;
}

/**
 * Called when new facility is built on the station. If it is the first facility
 * it initializes also 'xy' and 'random_bits' members
 */
void Station::AddFacility(StationFacility new_facility_bit, TileIndex facil_xy)
{
	if (this->facilities == FACIL_NONE) {
		this->xy = facil_xy;
		this->random_bits = Random();
	}
	this->facilities |= new_facility_bit;
	this->owner = _current_company;
	this->build_date = _date;
}

/**
 * Marks the tiles of the station as dirty.
 *
 * @ingroup dirty
 */
void Station::MarkTilesDirty(bool cargo_change) const
{
	TileIndex tile = this->train_station.tile;
	int w, h;

	if (tile == INVALID_TILE) return;

	/* cargo_change is set if we're refreshing the tiles due to cargo moving
	 * around. */
	if (cargo_change) {
		/* Don't waste time updating if there are no custom station graphics
		 * that might change. Even if there are custom graphics, they might
		 * not change. Unfortunately we have no way of telling. */
		if (this->num_specs == 0) return;
	}

	for (h = 0; h < train_station.h; h++) {
		for (w = 0; w < train_station.w; w++) {
			if (this->TileBelongsToRailStation(tile)) {
				MarkTileDirtyByTile(tile);
			}
			tile += TileDiffXY(1, 0);
		}
		tile += TileDiffXY(-w, 1);
	}
}

/* virtual */ uint Station::GetPlatformLength(TileIndex tile) const
{
	assert(this->TileBelongsToRailStation(tile));

	TileIndexDiff delta = (GetRailStationAxis(tile) == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));

	TileIndex t = tile;
	uint len = 0;
	do {
		t -= delta;
		len++;
	} while (IsCompatibleTrainStationTile(t, tile));

	t = tile;
	do {
		t += delta;
		len++;
	} while (IsCompatibleTrainStationTile(t, tile));

	return len - 1;
}

/* virtual */ uint Station::GetPlatformLength(TileIndex tile, DiagDirection dir) const
{
	TileIndex start_tile = tile;
	uint length = 0;
	assert(IsRailStationTile(tile));
	assert(dir < DIAGDIR_END);

	do {
		length++;
		tile += TileOffsByDiagDir(dir);
	} while (IsCompatibleTrainStationTile(tile, start_tile));

	return length;
}

/**
 * Determines the catchment radius of the station
 * @return The radius
 */
uint Station::GetCatchmentRadius() const
{
	uint ret = CA_NONE;

	if (_settings_game.station.modified_catchment) {
		if (this->bus_stops          != NULL)         ret = max<uint>(ret, CA_BUS);
		if (this->truck_stops        != NULL)         ret = max<uint>(ret, CA_TRUCK);
		if (this->train_station.tile != INVALID_TILE) ret = max<uint>(ret, CA_TRAIN);
		if (this->dock_tile          != INVALID_TILE) ret = max<uint>(ret, CA_DOCK);
		if (this->airport.tile       != INVALID_TILE) ret = max<uint>(ret, this->airport.GetSpec()->catchment);
	} else {
		if (this->bus_stops != NULL || this->truck_stops != NULL || this->train_station.tile != INVALID_TILE || this->dock_tile != INVALID_TILE || this->airport.tile != INVALID_TILE) {
			ret = CA_UNMODIFIED;
		}
	}

	return ret;
}

/**
 * Determines catchment rectangle of this station
 * @return clamped catchment rectangle
 */
Rect Station::GetCatchmentRect() const
{
	assert(!this->rect.IsEmpty());

	/* Compute acceptance rectangle */
	int catchment_radius = this->GetCatchmentRadius();

	Rect ret = {
		max<int>(this->rect.left   - catchment_radius, 0),
		max<int>(this->rect.top    - catchment_radius, 0),
		min<int>(this->rect.right  + catchment_radius, MapMaxX()),
		min<int>(this->rect.bottom + catchment_radius, MapMaxY())
	};

	return ret;
}

/** Rect and pointer to IndustryVector */
struct RectAndIndustryVector {
	Rect rect;                       ///< The rectangle to search the industries in.
	IndustryVector *industries_near; ///< The nearby industries.
};

/**
 * Callback function for Station::RecomputeIndustriesNear()
 * Tests whether tile is an industry and possibly adds
 * the industry to station's industries_near list.
 * @param ind_tile tile to check
 * @param user_data pointer to RectAndIndustryVector
 * @return always false, we want to search all tiles
 */
static bool FindIndustryToDeliver(TileIndex ind_tile, void *user_data)
{
	/* Only process industry tiles */
	if (!IsTileType(ind_tile, MP_INDUSTRY)) return false;

	RectAndIndustryVector *riv = (RectAndIndustryVector *)user_data;
	Industry *ind = Industry::GetByTile(ind_tile);

	/* Don't check further if this industry is already in the list */
	if (riv->industries_near->Contains(ind)) return false;

	/* Only process tiles in the station acceptance rectangle */
	int x = TileX(ind_tile);
	int y = TileY(ind_tile);
	if (x < riv->rect.left || x > riv->rect.right || y < riv->rect.top || y > riv->rect.bottom) return false;

	/* Include only industries that can accept cargo */
	uint cargo_index;
	for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
		if (ind->accepts_cargo[cargo_index] != CT_INVALID) break;
	}
	if (cargo_index >= lengthof(ind->accepts_cargo)) return false;

	*riv->industries_near->Append() = ind;

	return false;
}

/**
 * Recomputes Station::industries_near, list of industries possibly
 * accepting cargo in station's catchment radius
 */
void Station::RecomputeIndustriesNear()
{
	this->industries_near.Clear();
	if (this->rect.IsEmpty()) return;

	RectAndIndustryVector riv = {
		this->GetCatchmentRect(),
		&this->industries_near
	};

	/* Compute maximum extent of acceptance rectangle wrt. station sign */
	TileIndex start_tile = this->xy;
	uint max_radius = max(
		max(DistanceManhattan(start_tile, TileXY(riv.rect.left,  riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.left,  riv.rect.bottom))),
		max(DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.bottom)))
	);

	CircularTileSearch(&start_tile, 2 * max_radius + 1, &FindIndustryToDeliver, &riv);
}

/**
 * Recomputes Station::industries_near for all stations
 */
/* static */ void Station::RecomputeIndustriesNearForAll()
{
	Station *st;
	FOR_ALL_STATIONS(st) st->RecomputeIndustriesNear();
}

/************************************************************************/
/*                     StationRect implementation                       */
/************************************************************************/

StationRect::StationRect()
{
	this->MakeEmpty();
}

void StationRect::MakeEmpty()
{
	this->left = this->top = this->right = this->bottom = 0;
}

/**
 * Determines whether a given point (x, y) is within a certain distance of
 * the station rectangle.
 * @note x and y are in Tile coordinates
 * @param x X coordinate
 * @param y Y coordinate
 * @param distance The maximum distance a point may have (L1 norm)
 * @return true if the point is within distance tiles of the station rectangle
 */
bool StationRect::PtInExtendedRect(int x, int y, int distance) const
{
	return this->left - distance <= x && x <= this->right + distance &&
			this->top - distance <= y && y <= this->bottom + distance;
}

bool StationRect::IsEmpty() const
{
	return this->left == 0 || this->left > this->right || this->top > this->bottom;
}

CommandCost StationRect::BeforeAddTile(TileIndex tile, StationRectMode mode)
{
	int x = TileX(tile);
	int y = TileY(tile);
	if (this->IsEmpty()) {
		/* we are adding the first station tile */
		if (mode != ADD_TEST) {
			this->left = this->right = x;
			this->top = this->bottom = y;
		}
	} else if (!this->PtInExtendedRect(x, y)) {
		/* current rect is not empty and new point is outside this rect
		 * make new spread-out rectangle */
		Rect new_rect = {min(x, this->left), min(y, this->top), max(x, this->right), max(y, this->bottom)};

		/* check new rect dimensions against preset max */
		int w = new_rect.right - new_rect.left + 1;
		int h = new_rect.bottom - new_rect.top + 1;
		if (mode != ADD_FORCE && (w > _settings_game.station.station_spread || h > _settings_game.station.station_spread)) {
			assert(mode != ADD_TRY);
			return_cmd_error(STR_ERROR_STATION_TOO_SPREAD_OUT);
		}

		/* spread-out ok, return true */
		if (mode != ADD_TEST) {
			/* we should update the station rect */
			*this = new_rect;
		}
	} else {
		; // new point is inside the rect, we don't need to do anything
	}
	return CommandCost();
}

CommandCost StationRect::BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode)
{
	if (mode == ADD_FORCE || (w <= _settings_game.station.station_spread && h <= _settings_game.station.station_spread)) {
		/* Important when the old rect is completely inside the new rect, resp. the old one was empty. */
		CommandCost ret = this->BeforeAddTile(tile, mode);
		if (ret.Succeeded()) ret = this->BeforeAddTile(TILE_ADDXY(tile, w - 1, h - 1), mode);
		return ret;
	}
	return CommandCost();
}

/**
 * Check whether station tiles of the given station id exist in the given rectangle
 * @param st_id    Station ID to look for in the rectangle
 * @param left_a   Minimal tile X edge of the rectangle
 * @param top_a    Minimal tile Y edge of the rectangle
 * @param right_a  Maximal tile X edge of the rectangle (inclusive)
 * @param bottom_a Maximal tile Y edge of the rectangle (inclusive)
 * @return \c true if a station tile with the given \a st_id exists in the rectangle, \c false otherwise
 */
/* static */ bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a)
{
	TileArea ta(TileXY(left_a, top_a), TileXY(right_a, bottom_a));
	TILE_AREA_LOOP(tile, ta) {
		if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true;
	}

	return false;
}

bool StationRect::AfterRemoveTile(BaseStation *st, TileIndex tile)
{
	int x = TileX(tile);
	int y = TileY(tile);

	/* look if removed tile was on the bounding rect edge
	 * and try to reduce the rect by this edge
	 * do it until we have empty rect or nothing to do */
	for (;;) {
		/* check if removed tile is on rect edge */
		bool left_edge = (x == this->left);
		bool right_edge = (x == this->right);
		bool top_edge = (y == this->top);
		bool bottom_edge = (y == this->bottom);

		/* can we reduce the rect in either direction? */
		bool reduce_x = ((left_edge || right_edge) && !ScanForStationTiles(st->index, x, this->top, x, this->bottom));
		bool reduce_y = ((top_edge || bottom_edge) && !ScanForStationTiles(st->index, this->left, y, this->right, y));
		if (!(reduce_x || reduce_y)) break; // nothing to do (can't reduce)

		if (reduce_x) {
			/* reduce horizontally */
			if (left_edge) {
				/* move left edge right */
				this->left = x = x + 1;
			} else {
				/* move right edge left */
				this->right = x = x - 1;
			}
		}
		if (reduce_y) {
			/* reduce vertically */
			if (top_edge) {
				/* move top edge down */
				this->top = y = y + 1;
			} else {
				/* move bottom edge up */
				this->bottom = y = y - 1;
			}
		}

		if (left > right || top > bottom) {
			/* can't continue, if the remaining rectangle is empty */
			this->MakeEmpty();
			return true; // empty remaining rect
		}
	}
	return false; // non-empty remaining rect
}

bool StationRect::AfterRemoveRect(BaseStation *st, TileArea ta)
{
	assert(this->PtInExtendedRect(TileX(ta.tile), TileY(ta.tile)));
	assert(this->PtInExtendedRect(TileX(ta.tile) + ta.w - 1, TileY(ta.tile) + ta.h - 1));

	bool empty = this->AfterRemoveTile(st, ta.tile);
	if (ta.w != 1 || ta.h != 1) empty = empty || this->AfterRemoveTile(st, TILE_ADDXY(ta.tile, ta.w - 1, ta.h - 1));
	return empty;
}

StationRect& StationRect::operator = (const Rect &src)
{
	this->left = src.left;
	this->top = src.top;
	this->right = src.right;
	this->bottom = src.bottom;
	return *this;
}

/**
 * Calculates the maintenance cost of all airports of a company.
 * @param owner Company.
 * @return Total cost.
 */
Money AirportMaintenanceCost(Owner owner)
{
	Money total_cost = 0;

	const Station *st;
	FOR_ALL_STATIONS(st) {
		if (st->owner == owner && (st->facilities & FACIL_AIRPORT)) {
			total_cost += _price[PR_INFRASTRUCTURE_AIRPORT] * st->airport.GetSpec()->maintenance_cost;
		}
	}
	/* 3 bits fraction for the maintenance cost factor. */
	return total_cost >> 3;
}