Files @ r9469:c669ca0efed3
Branch filter:

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

rubidium
(svn r13389) -Codechange: remove NM_CALLBACK because that information could be gathered from the fact that a callback has been specified. Patch by Cirdan.
  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
r5665:f732864744b6
r5665:f732864744b6
r9111:983de9c5a848
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r8785:8312063c5ee4
r5665:f732864744b6
r5665:f732864744b6
r8254:b96be9ebfc6e
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r7469:b4e494b560de
r7469:b4e494b560de
r9043:f68547db66fc
r8016:8a1d54caaa5f
r8123:dde0a9a84019
r8131:7a50db7be0ff
r8131:7a50db7be0ff
r8140:9424f012f6a2
r8211:7dc9fca2785c
r8270:c2ec53375c9d
r8275:5932486ddd21
r8784:f3d6ba8eb078
r5665:f732864744b6
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r6259:e2dba394134b
r5665:f732864744b6
r5721:2780cdf77488
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r6420:01087f989fd1
r6420:01087f989fd1
r6420:01087f989fd1
r6420:01087f989fd1
r6420:01087f989fd1
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r8258:08100da56269
r7413:3ccdde9800e0
r7413:3ccdde9800e0
r7413:3ccdde9800e0
r7413:3ccdde9800e0
r7453:707d7f97be45
r7453:707d7f97be45
r7453:707d7f97be45
r7453:707d7f97be45
r5665:f732864744b6
r9297:6cd3e74fc38e
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5678:ea7f4cfadfcd
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r7010:fd8e3ec6f727
r7010:fd8e3ec6f727
r7010:fd8e3ec6f727
r7010:fd8e3ec6f727
r7376:c436274a8ff0
r7376:c436274a8ff0
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r8785:8312063c5ee4
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r5721:2780cdf77488
r6420:01087f989fd1
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5721:2780cdf77488
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r8016:8a1d54caaa5f
r5665:f732864744b6
r6700:d421e53a3cc4
r6700:d421e53a3cc4
r6700:d421e53a3cc4
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r6700:d421e53a3cc4
r6700:d421e53a3cc4
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r6823:c5b9c08647ba
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5678:ea7f4cfadfcd
r5665:f732864744b6
r5665:f732864744b6
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r6823:c5b9c08647ba
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5665:f732864744b6
r5998:69c1cce26321
r5998:69c1cce26321
r5998:69c1cce26321
r6420:01087f989fd1
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
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
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
r5998:69c1cce26321
r5896:637e08761f0c
r5896:637e08761f0c
r5896:637e08761f0c
r5896:637e08761f0c
r5896:637e08761f0c
r5997:b5a74d8a12b8
r5896:637e08761f0c
r5896:637e08761f0c
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5910:eab744de5201
r5676:e4a5055c9100
r5910:eab744de5201
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5910:eab744de5201
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r9413:fcf267325763
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5910:eab744de5201
r5910:eab744de5201
r5678:ea7f4cfadfcd
r5678:ea7f4cfadfcd
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5676:e4a5055c9100
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5716:92051313c912
r5713:eab150192986
r5713:eab150192986
r5713:eab150192986
r5867:de68577800e6
r5708:7ae0ffd22f15
r5716:92051313c912
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r7420:9706ae234622
r7420:9706ae234622
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5713:eab150192986
r5713:eab150192986
r5708:7ae0ffd22f15
r6259:e2dba394134b
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5708:7ae0ffd22f15
r5716:92051313c912
r5708:7ae0ffd22f15
r7377:722cd70012ed
r5708:7ae0ffd22f15
r5835:b95936d13ee4
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r7928:a80e7e05d6c5
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r6005:4e2beebaf215
r6005:4e2beebaf215
r5990:d6898a57a2b5
r6005:4e2beebaf215
r7928:a80e7e05d6c5
r5990:d6898a57a2b5
r7929:67846e70abf8
r6005:4e2beebaf215
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r7929:67846e70abf8
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r6012:6160fb57dc8a
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r7931:4c17a74c399e
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r7928:a80e7e05d6c5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r5990:d6898a57a2b5
r6005:4e2beebaf215
r5990:d6898a57a2b5
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7471:86f7861fdba8
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
r7469:b4e494b560de
/* $Id$ */

/** @file station.cpp Implementation of the station base class. */

#include "stdafx.h"
#include "openttd.h"
#include "bridge_map.h"
#include "debug.h"
#include "station_map.h"
#include "station_base.h"
#include "town.h"
#include "saveload.h"
#include "player_func.h"
#include "airport.h"
#include "sprite.h"
#include "train.h"
#include "water_map.h"
#include "industry_map.h"
#include "newgrf_callbacks.h"
#include "newgrf_station.h"
#include "yapf/yapf.h"
#include "cargotype.h"
#include "roadveh.h"
#include "window_type.h"
#include "station_gui.h"
#include "zoom_func.h"
#include "functions.h"
#include "window_func.h"
#include "date_func.h"
#include "variables.h"
#include "settings_type.h"
#include "command_func.h"
#include "order_func.h"

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

Station::Station(TileIndex tile)
{
	DEBUG(station, cDebugCtorLevel, "I+%3d", index);

	xy = tile;
	airport_tile = dock_tile = train_tile = 0;
	bus_stops = truck_stops = NULL;
	had_vehicle_of_type = 0;
	time_since_load = 255;
	time_since_unload = 255;
	delete_ctr = 0;
	facilities = 0;

	last_vehicle_type = VEH_INVALID;

	random_bits = 0; // Random() must be called when station is really built (DC_EXEC)
	waiting_triggers = 0;
}

/**
 * Clean up a station by clearing vehicle orders and invalidating windows.
 * 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()
{
	DEBUG(station, cDebugCtorLevel, "I-%3d", index);

	free(this->name);
	free(this->speclist);

	if (CleaningPool()) return;

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

	MarkDirty();
	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, index);

	/* Subsidies need removal as well */
	DeleteSubsidyWithStation(index);

	xy = 0;

	for (CargoID c = 0; c < NUM_CARGO; c++) {
		goods[c].cargo.Truncate(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 Vehicle *v) const
{
	RoadStop *rs = this->GetPrimaryRoadStop(IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK);

	for (; rs != NULL; rs = rs->next) {
		/* The vehicle cannot go to this roadstop (different roadtype) */
		if ((GetRoadTypes(rs->xy) & v->u.road.compatible_roadtypes) == ROADTYPES_NONE) continue;
		/* The vehicle is articulated and can therefor not go the a standard road stop */
		if (IsStandardRoadStopTile(rs->xy) && RoadVehHasArticPart(v)) 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(byte new_facility_bit, TileIndex facil_xy)
{
	if (facilities == 0) {
		xy = facil_xy;
		random_bits = Random();
	}
	facilities |= new_facility_bit;
	owner = _current_player;
	build_date = _date;
}

void Station::MarkDirty() const
{
	if (sign.width_1 != 0) {
		InvalidateWindowWidget(WC_STATION_VIEW, index, SVW_CAPTION);

		/* We use ZOOM_LVL_MAX here, as every viewport can have an other zoom,
		 *  and there is no way for us to know which is the biggest. So make the
		 *  biggest area dirty, and we are safe for sure. */
		MarkAllViewportsDirty(
			sign.left - 6,
			sign.top,
			sign.left + ScaleByZoom(sign.width_1 + 12, ZOOM_LVL_MAX),
			sign.top + ScaleByZoom(12, ZOOM_LVL_MAX));
	}
}

void Station::MarkTilesDirty(bool cargo_change) const
{
	TileIndex tile = train_tile;
	int w, h;

	/* XXX No station is recorded as 0, not INVALID_TILE... */
	if (tile == 0) 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 < trainst_h; h++) {
		for (w = 0; w < trainst_w; w++) {
			if (TileBelongsToRailStation(tile)) {
				MarkTileDirtyByTile(tile);
			}
			tile += TileDiffXY(1, 0);
		}
		tile += TileDiffXY(-w, 1);
	}
}

bool Station::TileBelongsToRailStation(TileIndex tile) const
{
	return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == index && IsRailwayStation(tile);
}

/** Obtain the length of a platform
 * @pre tile must be a railway station tile
 * @param tile A tile that contains the platform in question
 * @return The length of the platform
 */
uint Station::GetPlatformLength(TileIndex tile) const
{
	TileIndex t;
	TileIndexDiff delta;
	uint len = 0;
	assert(TileBelongsToRailStation(tile));

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

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

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

	return len - 1;
}

/** Determines the REMAINING length of a platform, starting at (and including)
 * the given tile.
 * @param tile the tile from which to start searching. Must be a railway station tile
 * @param dir The direction in which to search.
 * @return The platform length
 */
uint Station::GetPlatformLength(TileIndex tile, DiagDirection dir) const
{
	TileIndex start_tile = tile;
	uint length = 0;
	assert(IsRailwayStationTile(tile));
	assert(dir < DIAGDIR_END);

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

	return length;
}

/** Determines whether a station is a buoy only.
 * @todo Ditch this encoding of buoys
 */
bool Station::IsBuoy() const
{
	return (had_vehicle_of_type & HVOT_BUOY) != 0;
}


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

StationRect::StationRect()
{
	MakeEmpty();
}

void StationRect::MakeEmpty()
{
	left = top = right = 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 maxmium 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 (left - distance <= x && x <= right + distance && top - distance <= y && y <= bottom + distance);
}

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

bool StationRect::BeforeAddTile(TileIndex tile, StationRectMode mode)
{
	int x = TileX(tile);
	int y = TileY(tile);
	if (IsEmpty()) {
		/* we are adding the first station tile */
		left = right = x;
		top = bottom = y;

	} else if (!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, left), min(y, top), max(x, right), max(y, 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);
			_error_message = STR_306C_STATION_TOO_SPREAD_OUT;
			return false;
		}

		/* 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 true;
}

bool StationRect::BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode)
{
	return BeforeAddTile(tile, mode) && BeforeAddTile(TILE_ADDXY(tile, w - 1, h - 1), mode);
}

/*static*/ bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a)
{
	TileIndex top_left = TileXY(left_a, top_a);
	int width = right_a - left_a + 1;
	int height = bottom_a - top_a + 1;

	BEGIN_TILE_LOOP(tile, width, height, top_left)
		if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true;
	END_TILE_LOOP(tile, width, height, top_left);

	return false;
}

bool StationRect::AfterRemoveTile(Station *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 == left);
		bool right_edge = (x == right);
		bool top_edge = (y == top);
		bool bottom_edge = (y == bottom);

		/* can we reduce the rect in either direction? */
		bool reduce_x = ((left_edge || right_edge) && !ScanForStationTiles(st->index, x, top, x, bottom));
		bool reduce_y = ((top_edge || bottom_edge) && !ScanForStationTiles(st->index, left, y, 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 */
				left = x = x + 1;
			} else {
				/* move right edge left */
				right = x = x - 1;
			}
		}
		if (reduce_y) {
			/* reduce vertically */
			if (top_edge) {
				/* move top edge down */
				top = y = y + 1;
			} else {
				/* move bottom edge up */
				bottom = y = y - 1;
			}
		}

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

bool StationRect::AfterRemoveRect(Station *st, TileIndex tile, int w, int h)
{
	assert(PtInExtendedRect(TileX(tile), TileY(tile)));
	assert(PtInExtendedRect(TileX(tile) + w - 1, TileY(tile) + h - 1));

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

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


/************************************************************************/
/*                     RoadStop implementation                          */
/************************************************************************/

/** Initializes a RoadStop */
RoadStop::RoadStop(TileIndex tile) :
	xy(tile),
	status(3), // stop is free
	num_vehicles(0),
	next(NULL)
{
	DEBUG(ms, cDebugCtorLevel,  "I+ at %d[0x%x]", tile, tile);
}

/** De-Initializes a RoadStops. This includes clearing all slots that vehicles might
  * have and unlinks it from the linked list of road stops at the given station
  */
RoadStop::~RoadStop()
{
	if (CleaningPool()) return;

	/* Clear the slot assignment of all vehicles heading for this road stop */
	if (num_vehicles != 0) {
		Vehicle *v;

		FOR_ALL_VEHICLES(v) {
			if (v->type == VEH_ROAD && v->u.road.slot == this) ClearSlot(v);
		}
	}
	assert(num_vehicles == 0);

	DEBUG(ms, cDebugCtorLevel , "I- at %d[0x%x]", xy, xy);

	xy = 0;
}

/** Checks whether there is a free bay in this road stop */
bool RoadStop::HasFreeBay() const
{
	return GB(status, 0, MAX_BAY_COUNT) != 0;
}

/** Checks whether the given bay is free in this road stop */
bool RoadStop::IsFreeBay(uint nr) const
{
	assert(nr < MAX_BAY_COUNT);
	return HasBit(status, nr);
}

/**
 * Allocates a bay
 * @return the allocated bay number
 * @pre this->HasFreeBay()
 */
uint RoadStop::AllocateBay()
{
	assert(HasFreeBay());

	/* Find the first free bay. If the bit is set, the bay is free. */
	uint bay_nr = 0;
	while (!HasBit(status, bay_nr)) bay_nr++;

	ClrBit(status, bay_nr);
	return bay_nr;
}

/**
 * Allocates a bay in a drive-through road stop
 * @param nr the number of the bay to allocate
 */
void RoadStop::AllocateDriveThroughBay(uint nr)
{
	assert(nr < MAX_BAY_COUNT);
	ClrBit(status, nr);
}

/**
 * Frees the given bay
 * @param nr the number of the bay to free
 */
void RoadStop::FreeBay(uint nr)
{
	assert(nr < MAX_BAY_COUNT);
	SetBit(status, nr);
}


/** Checks whether the entrance of the road stop is occupied by a vehicle */
bool RoadStop::IsEntranceBusy() const
{
	return HasBit(status, 7);
}

/** Makes an entrance occupied or free */
void RoadStop::SetEntranceBusy(bool busy)
{
	SB(status, 7, 1, busy);
}

/**
 * Get the next road stop accessible by this vehicle.
 * @param v the vehicle to get the next road stop for.
 * @return the next road stop accessible.
 */
RoadStop *RoadStop::GetNextRoadStop(const Vehicle *v) const
{
	for (RoadStop *rs = this->next; rs != NULL; rs = rs->next) {
		/* The vehicle cannot go to this roadstop (different roadtype) */
		if ((GetRoadTypes(rs->xy) & v->u.road.compatible_roadtypes) == ROADTYPES_NONE) continue;
		/* The vehicle is articulated and can therefor not go the a standard road stop */
		if (IsStandardRoadStopTile(rs->xy) && RoadVehHasArticPart(v)) continue;

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

	return NULL;
}