OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_station.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_station.cpp 26580 2014-05-11 18:02:11Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "station_base.h"
15 #include "waypoint_base.h"
16 #include "roadstop_base.h"
17 #include "newgrf_cargo.h"
18 #include "newgrf_station.h"
19 #include "newgrf_spritegroup.h"
20 #include "newgrf_sound.h"
21 #include "newgrf_railtype.h"
22 #include "town.h"
23 #include "newgrf_town.h"
24 #include "company_func.h"
25 #include "tunnelbridge_map.h"
26 #include "newgrf_animation_base.h"
27 #include "newgrf_class_func.h"
28
29 #include "safeguards.h"
30
31
32 template <typename Tspec, typename Tid, Tid Tmax>
33 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
34 {
35 /* Set up initial data */
36 classes[0].global_id = 'DFLT';
37 classes[0].name = STR_STATION_CLASS_DFLT;
38 classes[0].Insert(NULL);
39
40 classes[1].global_id = 'WAYP';
41 classes[1].name = STR_STATION_CLASS_WAYP;
42 classes[1].Insert(NULL);
43 }
44
45 template <typename Tspec, typename Tid, Tid Tmax>
46 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
47 {
48 return true;
49 }
50
51 INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX)
52
53 static const uint NUM_STATIONSSPECS_PER_STATION = 255;
54
55 enum TriggerArea {
56 TA_TILE,
57 TA_PLATFORM,
58 TA_WHOLE,
59 };
60
61 struct ETileArea : TileArea {
62 ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta)
63 {
64 switch (ta) {
65 default: NOT_REACHED();
66
67 case TA_TILE:
68 this->tile = tile;
69 this->w = 1;
70 this->h = 1;
71 break;
72
73 case TA_PLATFORM: {
74 TileIndex start, end;
75 Axis axis = GetRailStationAxis(tile);
76 TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis));
77
78 for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(end + delta, tile); end += delta) { /* Nothing */ }
79 for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(start - delta, tile); start -= delta) { /* Nothing */ }
80
81 this->tile = start;
82 this->w = TileX(end) - TileX(start) + 1;
83 this->h = TileY(end) - TileY(start) + 1;
84 break;
85 }
86
87 case TA_WHOLE:
88 st->GetTileArea(this, Station::IsExpected(st) ? STATION_RAIL : STATION_WAYPOINT);
89 break;
90 }
91 }
92 };
93
94
107 uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
108 {
109 uint32 retval = 0;
110
111 if (axis == AXIS_X) {
112 Swap(platforms, length);
113 Swap(x, y);
114 }
115
116 if (centred) {
117 x -= platforms / 2;
118 y -= length / 2;
119 x = Clamp(x, -8, 7);
120 y = Clamp(y, -8, 7);
121 SB(retval, 0, 4, y & 0xF);
122 SB(retval, 4, 4, x & 0xF);
123 } else {
124 SB(retval, 0, 4, min(15, y));
125 SB(retval, 4, 4, min(15, length - y - 1));
126 SB(retval, 8, 4, min(15, x));
127 SB(retval, 12, 4, min(15, platforms - x - 1));
128 }
129 SB(retval, 16, 4, min(15, length));
130 SB(retval, 20, 4, min(15, platforms));
131 SB(retval, 24, 4, tile);
132
133 return retval;
134 }
135
136
145 static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
146 {
147 byte orig_type = 0;
148 Axis orig_axis = AXIS_X;
149 StationID sid = GetStationIndex(tile);
150
151 if (check_type) orig_type = GetCustomStationSpecIndex(tile);
152 if (check_axis) orig_axis = GetRailStationAxis(tile);
153
154 for (;;) {
155 TileIndex new_tile = TILE_ADD(tile, delta);
156
157 if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break;
158 if (!HasStationRail(new_tile)) break;
159 if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
160 if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
161
162 tile = new_tile;
163 }
164 return tile;
165 }
166
167
168 static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
169 {
170 int tx = TileX(tile);
171 int ty = TileY(tile);
172 int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1, 0), check_type, check_axis));
173 int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
174 int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1, 0), check_type, check_axis)) + 1;
175 int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0, 1), check_type, check_axis)) + 1;
176
177 tx -= sx; ex -= sx;
178 ty -= sy; ey -= sy;
179
180 return GetPlatformInfo(GetRailStationAxis(tile), GetStationGfx(tile), ex, ey, tx, ty, centred);
181 }
182
183
184 static uint32 GetRailContinuationInfo(TileIndex tile)
185 {
186 /* Tile offsets and exit dirs for X axis */
187 static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
188 static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
189
190 /* Tile offsets and exit dirs for Y axis */
191 static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
192 static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
193
194 Axis axis = GetRailStationAxis(tile);
195
196 /* Choose appropriate lookup table to use */
197 const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
198 const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
199
200 uint32 res = 0;
201 uint i;
202
203 for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
204 TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
205 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
206 if (trackbits != TRACK_BIT_NONE) {
207 /* If there is any track on the tile, set the bit in the second byte */
208 SetBit(res, i + 8);
209
210 /* With tunnels and bridges the tile has tracks, but they are not necessarily connected
211 * with the next tile because the ramp is not going in the right direction. */
212 if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
213 continue;
214 }
215
216 /* If any track reaches our exit direction, set the bit in the lower byte */
217 if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
218 }
219 }
220
221 return res;
222 }
223
224
225 /* Station Resolver Functions */
226 /* virtual */ uint32 StationScopeResolver::GetRandomBits() const
227 {
228 return (this->st == NULL ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
229 }
230
231
232 /* virtual */ uint32 StationScopeResolver::GetTriggers() const
233 {
234 return this->st == NULL ? 0 : this->st->waiting_triggers;
235 }
236
237
238 /* virtual */ void StationScopeResolver::SetTriggers(int triggers) const
239 {
240 BaseStation *st = const_cast<BaseStation *>(this->st);
241 assert(st != NULL);
242 st->waiting_triggers = triggers;
243 }
244
250 static struct {
251 uint32 v40;
252 uint32 v41;
253 uint32 v45;
254 uint32 v46;
255 uint32 v47;
256 uint32 v49;
257 uint8 valid;
258 } _svc;
259
265 TownScopeResolver *StationResolverObject::GetTown()
266 {
267 if (this->town_scope == NULL) {
268 Town *t = NULL;
269 if (this->station_scope.st != NULL) {
270 t = this->station_scope.st->town;
271 } else if (this->station_scope.tile != INVALID_TILE) {
272 t = ClosestTownFromTile(this->station_scope.tile, UINT_MAX);
273 }
274 if (t == NULL) return NULL;
275 this->town_scope = new TownScopeResolver(*this, t, this->station_scope.st == NULL);
276 }
277 return this->town_scope;
278 }
279
280 /* virtual */ uint32 StationScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
281 {
282 if (this->st == NULL) {
283 /* Station does not exist, so we're in a purchase list or the land slope check callback. */
284 switch (variable) {
285 case 0x40:
286 case 0x41:
287 case 0x46:
288 case 0x47:
289 case 0x49: return 0x2110000; // Platforms, tracks & position
290 case 0x42: return 0; // Rail type (XXX Get current type from GUI?)
291 case 0x43: return GetCompanyInfo(_current_company); // Station owner
292 case 0x44: return 2; // PBS status
293 case 0x67: // Land info of nearby tile
294 if (this->axis != INVALID_AXIS && this->tile != INVALID_TILE) {
295 TileIndex tile = this->tile;
296 if (parameter != 0) tile = GetNearbyTile(parameter, tile, true, this->axis); // only perform if it is required
297
298 Slope tileh = GetTileSlope(tile);
299 bool swap = (this->axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
300
301 return GetNearbyTileInformation(tile, this->ro.grffile->grf_version >= 8) ^ (swap ? SLOPE_EW : 0);
302 }
303 break;
304
305 case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
306 }
307
308 *available = false;
309 return UINT_MAX;
310 }
311
312 switch (variable) {
313 /* Calculated station variables */
314 case 0x40:
315 if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(this->tile, false, false, false); SetBit(_svc.valid, 0); }
316 return _svc.v40;
317
318 case 0x41:
319 if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(this->tile, true, false, false); SetBit(_svc.valid, 1); }
320 return _svc.v41;
321
322 case 0x42: return GetTerrainType(this->tile) | (GetReverseRailTypeTranslation(GetRailType(this->tile), this->statspec->grf_prop.grffile) << 8);
323 case 0x43: return GetCompanyInfo(this->st->owner); // Station owner
324 case 0x44: return HasStationReservation(this->tile) ? 7 : 4; // PBS status
325 case 0x45:
326 if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(this->tile); SetBit(_svc.valid, 2); }
327 return _svc.v45;
328
329 case 0x46:
330 if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(this->tile, false, false, true); SetBit(_svc.valid, 3); }
331 return _svc.v46;
332
333 case 0x47:
334 if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(this->tile, true, false, true); SetBit(_svc.valid, 4); }
335 return _svc.v47;
336
337 case 0x49:
338 if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(this->tile, false, true, false); SetBit(_svc.valid, 5); }
339 return _svc.v49;
340
341 case 0x4A: // Animation frame of tile
342 return GetAnimationFrame(this->tile);
343
344 /* Variables which use the parameter */
345 /* Variables 0x60 to 0x65 and 0x69 are handled separately below */
346 case 0x66: { // Animation frame of nearby tile
347 TileIndex tile = this->tile;
348 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
349 return this->st->TileBelongsToRailStation(tile) ? GetAnimationFrame(tile) : UINT_MAX;
350 }
351
352 case 0x67: { // Land info of nearby tile
353 Axis axis = GetRailStationAxis(this->tile);
354 TileIndex tile = this->tile;
355 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
356
357 Slope tileh = GetTileSlope(tile);
358 bool swap = (axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
359
360 return GetNearbyTileInformation(tile, this->ro.grffile->grf_version >= 8) ^ (swap ? SLOPE_EW : 0);
361 }
362
363 case 0x68: { // Station info of nearby tiles
364 TileIndex nearby_tile = GetNearbyTile(parameter, this->tile);
365
366 if (!HasStationTileRail(nearby_tile)) return 0xFFFFFFFF;
367
368 uint32 grfid = this->st->speclist[GetCustomStationSpecIndex(this->tile)].grfid;
369 bool perpendicular = GetRailStationAxis(this->tile) != GetRailStationAxis(nearby_tile);
370 bool same_station = this->st->TileBelongsToRailStation(nearby_tile);
371 uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10;
372
373 if (IsCustomStationSpecIndex(nearby_tile)) {
374 const StationSpecList ssl = BaseStation::GetByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)];
375 res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx;
376 }
377 return res;
378 }
379
380 /* General station variables */
381 case 0x82: return 50;
382 case 0x84: return this->st->string_id;
383 case 0x86: return 0;
384 case 0xF0: return this->st->facilities;
385 case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
386 }
387
388 return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
389 }
390
391 uint32 Station::GetNewGRFVariable(const ResolverObject &object, byte variable, byte parameter, bool *available) const
392 {
393 switch (variable) {
394 case 0x48: { // Accepted cargo types
395 CargoID cargo_type;
396 uint32 value = 0;
397
398 for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
399 if (HasBit(this->goods[cargo_type].status, GoodsEntry::GES_ACCEPTANCE)) SetBit(value, cargo_type);
400 }
401 return value;
402 }
403
404 case 0x8A: return this->had_vehicle_of_type;
405 case 0xF1: return (this->airport.tile != INVALID_TILE) ? this->airport.GetSpec()->ttd_airport_type : ATP_TTDP_LARGE;
406 case 0xF2: return (this->truck_stops != NULL) ? this->truck_stops->status : 0;
407 case 0xF3: return (this->bus_stops != NULL) ? this->bus_stops->status : 0;
408 case 0xF6: return this->airport.flags;
409 case 0xF7: return GB(this->airport.flags, 8, 8);
410 }
411
412 /* Handle cargo variables with parameter, 0x60 to 0x65 and 0x69 */
413 if ((variable >= 0x60 && variable <= 0x65) || variable == 0x69) {
414 CargoID c = GetCargoTranslation(parameter, object.grffile);
415
416 if (c == CT_INVALID) {
417 switch (variable) {
418 case 0x62: return 0xFFFFFFFF;
419 case 0x64: return 0xFF00;
420 default: return 0;
421 }
422 }
423 const GoodsEntry *ge = &this->goods[c];
424
425 switch (variable) {
426 case 0x60: return min(ge->cargo.TotalCount(), 4095);
427 case 0x61: return ge->HasVehicleEverTriedLoading() ? ge->time_since_pickup : 0;
428 case 0x62: return ge->HasRating() ? ge->rating : 0xFFFFFFFF;
429 case 0x63: return ge->cargo.DaysInTransit();
430 case 0x64: return ge->HasVehicleEverTriedLoading() ? ge->last_speed | (ge->last_age << 8) : 0xFF00;
431 case 0x65: return GB(ge->status, GoodsEntry::GES_ACCEPTANCE, 1) << 3;
432 case 0x69: {
433 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED + 1 == (int)GoodsEntry::GES_LAST_MONTH);
434 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED + 2 == (int)GoodsEntry::GES_CURRENT_MONTH);
435 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED + 3 == (int)GoodsEntry::GES_ACCEPTED_BIGTICK);
436 return GB(ge->status, GoodsEntry::GES_EVER_ACCEPTED, 4);
437 }
438 }
439 }
440
441 /* Handle cargo variables (deprecated) */
442 if (variable >= 0x8C && variable <= 0xEC) {
443 const GoodsEntry *g = &this->goods[GB(variable - 0x8C, 3, 4)];
444 switch (GB(variable - 0x8C, 0, 3)) {
445 case 0: return g->cargo.TotalCount();
446 case 1: return GB(min(g->cargo.TotalCount(), 4095), 0, 4) | (GB(g->status, GoodsEntry::GES_ACCEPTANCE, 1) << 7);
447 case 2: return g->time_since_pickup;
448 case 3: return g->rating;
449 case 4: return g->cargo.Source();
450 case 5: return g->cargo.DaysInTransit();
451 case 6: return g->last_speed;
452 case 7: return g->last_age;
453 }
454 }
455
456 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
457
458 *available = false;
459 return UINT_MAX;
460 }
461
462 uint32 Waypoint::GetNewGRFVariable(const ResolverObject &object, byte variable, byte parameter, bool *available) const
463 {
464 switch (variable) {
465 case 0x48: return 0; // Accepted cargo types
466 case 0x8A: return HVOT_WAYPOINT;
467 case 0xF1: return 0; // airport type
468 case 0xF2: return 0; // truck stop status
469 case 0xF3: return 0; // bus stop status
470 case 0xF6: return 0; // airport flags
471 case 0xF7: return 0; // airport flags cont.
472 }
473
474 /* Handle cargo variables with parameter, 0x60 to 0x65 */
475 if (variable >= 0x60 && variable <= 0x65) {
476 return 0;
477 }
478
479 /* Handle cargo variables (deprecated) */
480 if (variable >= 0x8C && variable <= 0xEC) {
481 switch (GB(variable - 0x8C, 0, 3)) {
482 case 3: return INITIAL_STATION_RATING;
483 case 4: return INVALID_STATION;
484 default: return 0;
485 }
486 }
487
488 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
489
490 *available = false;
491 return UINT_MAX;
492 }
493
494 /* virtual */ const SpriteGroup *StationResolverObject::ResolveReal(const RealSpriteGroup *group) const
495 {
496 if (this->station_scope.st == NULL || this->station_scope.statspec->cls_id == STAT_CLASS_WAYP) {
497 return group->loading[0];
498 }
499
500 uint cargo = 0;
501 const Station *st = Station::From(this->station_scope.st);
502
503 switch (this->station_scope.cargo_type) {
504 case CT_INVALID:
505 case CT_DEFAULT_NA:
506 case CT_PURCHASE:
507 cargo = 0;
508 break;
509
510 case CT_DEFAULT:
511 for (CargoID cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
512 cargo += st->goods[cargo_type].cargo.TotalCount();
513 }
514 break;
515
516 default:
517 cargo = st->goods[this->station_scope.cargo_type].cargo.TotalCount();
518 break;
519 }
520
521 if (HasBit(this->station_scope.statspec->flags, SSF_DIV_BY_STATION_SIZE)) cargo /= (st->train_station.w + st->train_station.h);
522 cargo = min(0xfff, cargo);
523
524 if (cargo > this->station_scope.statspec->cargo_threshold) {
525 if (group->num_loading > 0) {
526 uint set = ((cargo - this->station_scope.statspec->cargo_threshold) * group->num_loading) / (4096 - this->station_scope.statspec->cargo_threshold);
527 return group->loading[set];
528 }
529 } else {
530 if (group->num_loaded > 0) {
531 uint set = (cargo * group->num_loaded) / (this->station_scope.statspec->cargo_threshold + 1);
532 return group->loaded[set];
533 }
534 }
535
536 return group->loading[0];
537 }
538
548 StationResolverObject::StationResolverObject(const StationSpec *statspec, BaseStation *st, TileIndex tile,
549 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
550 : ResolverObject(statspec->grf_prop.grffile, callback, callback_param1, callback_param2),
551 station_scope(*this, statspec, st, tile), town_scope(NULL)
552 {
553 /* Invalidate all cached vars */
554 _svc.valid = 0;
555
556 CargoID ctype = CT_DEFAULT_NA;
557
558 if (this->station_scope.st == NULL) {
559 /* No station, so we are in a purchase list */
560 ctype = CT_PURCHASE;
561 } else if (Station::IsExpected(this->station_scope.st)) {
562 const Station *st = Station::From(this->station_scope.st);
563 /* Pick the first cargo that we have waiting */
564 const CargoSpec *cs;
565 FOR_ALL_CARGOSPECS(cs) {
566 if (this->station_scope.statspec->grf_prop.spritegroup[cs->Index()] != NULL &&
567 st->goods[cs->Index()].cargo.TotalCount() > 0) {
568 ctype = cs->Index();
569 break;
570 }
571 }
572 }
573
574 if (this->station_scope.statspec->grf_prop.spritegroup[ctype] == NULL) {
575 ctype = CT_DEFAULT;
576 }
577
578 /* Remember the cargo type we've picked */
579 this->station_scope.cargo_type = ctype;
580 this->root_spritegroup = this->station_scope.statspec->grf_prop.spritegroup[this->station_scope.cargo_type];
581 }
582
583 StationResolverObject::~StationResolverObject()
584 {
585 delete this->town_scope;
586 }
587
595 StationScopeResolver::StationScopeResolver(ResolverObject &ro, const StationSpec *statspec, BaseStation *st, TileIndex tile)
596 : ScopeResolver(ro)
597 {
598 this->tile = tile;
599 this->st = st;
600 this->statspec = statspec;
601 this->cargo_type = CT_INVALID;
602 this->axis = INVALID_AXIS;
603 }
604
613 SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32 var10)
614 {
615 StationResolverObject object(statspec, st, tile, CBID_NO_CALLBACK, var10);
616 const SpriteGroup *group = object.Resolve();
617 if (group == NULL || group->type != SGT_RESULT) return 0;
618 return group->GetResult() - 0x42D;
619 }
620
630 SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info)
631 {
632 /* callback_param1 == 2 means we are resolving the foundation sprites. */
633 StationResolverObject object(statspec, st, tile, CBID_NO_CALLBACK, 2, layout | (edge_info << 16));
634
635 const SpriteGroup *group = object.Resolve();
636 if (group == NULL || group->type != SGT_RESULT) return 0;
637
638 /* Note: SpriteGroup::Resolve zeroes all registers, so register 0x100 is initialised to 0. (compatibility) */
639 return group->GetResult() + GetRegister(0x100);
640 }
641
642
643 uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, BaseStation *st, TileIndex tile)
644 {
645 StationResolverObject object(statspec, st, tile, callback, param1, param2);
646 return object.ResolveCallback();
647 }
648
659 CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, byte plat_len, byte numtracks)
660 {
661 TileIndexDiff diff = cur_tile - north_tile;
662 Slope slope = GetTileSlope(cur_tile);
663
664 StationResolverObject object(statspec, NULL, cur_tile, CBID_STATION_LAND_SLOPE_CHECK,
665 (slope << 4) | (slope ^ (axis == AXIS_Y && HasBit(slope, CORNER_W) != HasBit(slope, CORNER_E) ? SLOPE_EW : 0)),
666 (numtracks << 24) | (plat_len << 16) | (axis == AXIS_Y ? TileX(diff) << 8 | TileY(diff) : TileY(diff) << 8 | TileX(diff)));
667 object.station_scope.axis = axis;
668
669 uint16 cb_res = object.ResolveCallback();
670
671 /* Failed callback means success. */
672 if (cb_res == CALLBACK_FAILED) return CommandCost();
673
674 /* The meaning of bit 10 is inverted for a grf version < 8. */
675 if (statspec->grf_prop.grffile->grf_version < 8) ToggleBit(cb_res, 10);
676 return GetErrorMessageFromLocationCallbackResult(cb_res, statspec->grf_prop.grffile, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
677 }
678
679
687 int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
688 {
689 uint i;
690
691 if (statspec == NULL || st == NULL) return 0;
692
693 for (i = 1; i < st->num_specs && i < NUM_STATIONSSPECS_PER_STATION; i++) {
694 if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
695 }
696
697 if (i == NUM_STATIONSSPECS_PER_STATION) {
698 /* As final effort when the spec list is already full...
699 * try to find the same spec and return that one. This might
700 * result in slightly "wrong" (as per specs) looking stations,
701 * but it's fairly unlikely that one reaches the limit anyways.
702 */
703 for (i = 1; i < st->num_specs && i < NUM_STATIONSSPECS_PER_STATION; i++) {
704 if (st->speclist[i].spec == statspec) return i;
705 }
706
707 return -1;
708 }
709
710 if (exec) {
711 if (i >= st->num_specs) {
712 st->num_specs = i + 1;
713 st->speclist = ReallocT(st->speclist, st->num_specs);
714
715 if (st->num_specs == 2) {
716 /* Initial allocation */
717 st->speclist[0].spec = NULL;
718 st->speclist[0].grfid = 0;
719 st->speclist[0].localidx = 0;
720 }
721 }
722
723 st->speclist[i].spec = statspec;
724 st->speclist[i].grfid = statspec->grf_prop.grffile->grfid;
725 st->speclist[i].localidx = statspec->grf_prop.local_id;
726
727 StationUpdateCachedTriggers(st);
728 }
729
730 return i;
731 }
732
733
740 void DeallocateSpecFromStation(BaseStation *st, byte specindex)
741 {
742 /* specindex of 0 (default) is never freeable */
743 if (specindex == 0) return;
744
745 ETileArea area = ETileArea(st, INVALID_TILE, TA_WHOLE);
746 /* Check all tiles over the station to check if the specindex is still in use */
747 TILE_AREA_LOOP(tile, area) {
748 if (st->TileBelongsToRailStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
749 return;
750 }
751 }
752
753 /* This specindex is no longer in use, so deallocate it */
754 st->speclist[specindex].spec = NULL;
755 st->speclist[specindex].grfid = 0;
756 st->speclist[specindex].localidx = 0;
757
758 /* If this was the highest spec index, reallocate */
759 if (specindex == st->num_specs - 1) {
760 for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--) {}
761
762 if (st->num_specs > 1) {
763 st->speclist = ReallocT(st->speclist, st->num_specs);
764 } else {
765 free(st->speclist);
766 st->num_specs = 0;
767 st->speclist = NULL;
768 st->cached_anim_triggers = 0;
769 st->cached_cargo_triggers = 0;
770 return;
771 }
772 }
773
774 StationUpdateCachedTriggers(st);
775 }
776
787 bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
788 {
789 const DrawTileSprites *sprites = NULL;
790 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
791 PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
792 uint tile = 2;
793
794 const StationSpec *statspec = StationClass::Get(sclass)->GetSpec(station);
795 if (statspec == NULL) return false;
796
797 if (HasBit(statspec->callback_mask, CBM_STATION_SPRITE_LAYOUT)) {
798 uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE);
799 if (callback != CALLBACK_FAILED) tile = callback;
800 }
801
802 uint32 total_offset = rti->GetRailtypeSpriteOffset();
803 uint32 relocation = 0;
804 uint32 ground_relocation = 0;
805 const NewGRFSpriteLayout *layout = NULL;
806 DrawTileSprites tmp_rail_layout;
807
808 if (statspec->renderdata == NULL) {
809 sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
810 } else {
811 layout = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis];
812 if (!layout->NeedsPreprocessing()) {
813 sprites = layout;
814 layout = NULL;
815 }
816 }
817
818 if (layout != NULL) {
819 /* Sprite layout which needs preprocessing */
820 bool separate_ground = HasBit(statspec->flags, SSF_SEPARATE_GROUND);
821 uint32 var10_values = layout->PrepareLayout(total_offset, rti->fallback_railtype, 0, 0, separate_ground);
822 uint8 var10;
823 FOR_EACH_SET_BIT(var10, var10_values) {
824 uint32 var10_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, var10);
825 layout->ProcessRegisters(var10, var10_relocation, separate_ground);
826 }
827
828 tmp_rail_layout.seq = layout->GetLayout(&tmp_rail_layout.ground);
829 sprites = &tmp_rail_layout;
830 total_offset = 0;
831 } else {
832 /* Simple sprite layout */
833 ground_relocation = relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 0);
834 if (HasBit(sprites->ground.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
835 ground_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 1);
836 }
837 ground_relocation += rti->fallback_railtype;
838 }
839
840 SpriteID image = sprites->ground.sprite;
841 PaletteID pal = sprites->ground.pal;
842 RailTrackOffset overlay_offset;
843 if (rti->UsesOverlay() && SplitGroundSpriteForOverlay(NULL, &image, &overlay_offset)) {
844 SpriteID ground = GetCustomRailSprite(rti, INVALID_TILE, RTSG_GROUND);
845 DrawSprite(image, PAL_NONE, x, y);
846 DrawSprite(ground + overlay_offset, PAL_NONE, x, y);
847 } else {
848 image += HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? ground_relocation : total_offset;
849 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += ground_relocation;
850 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
851 }
852
853 DrawRailTileSeqInGUI(x, y, sprites, total_offset, relocation, palette);
854
855 return true;
856 }
857
858
859 const StationSpec *GetStationSpec(TileIndex t)
860 {
861 if (!IsCustomStationSpecIndex(t)) return NULL;
862
863 const BaseStation *st = BaseStation::GetByTile(t);
864 uint specindex = GetCustomStationSpecIndex(t);
865 return specindex < st->num_specs ? st->speclist[specindex].spec : NULL;
866 }
867
868
875 bool IsStationTileBlocked(TileIndex tile)
876 {
877 const StationSpec *statspec = GetStationSpec(tile);
878
879 return statspec != NULL && HasBit(statspec->blocked, GetStationGfx(tile));
880 }
881
888 bool CanStationTileHavePylons(TileIndex tile)
889 {
890 const StationSpec *statspec = GetStationSpec(tile);
891 uint gfx = GetStationGfx(tile);
892 /* Default stations do not draw pylons under roofs (gfx >= 4) */
893 return statspec != NULL ? HasBit(statspec->pylons, gfx) : gfx < 4;
894 }
895
902 bool CanStationTileHaveWires(TileIndex tile)
903 {
904 const StationSpec *statspec = GetStationSpec(tile);
905 return statspec == NULL || !HasBit(statspec->wires, GetStationGfx(tile));
906 }
907
909 uint16 GetAnimStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, BaseStation *st, TileIndex tile, int extra_data)
910 {
911 return GetStationCallback(callback, param1, param2, statspec, st, tile);
912 }
913
915 struct StationAnimationBase : public AnimationBase<StationAnimationBase, StationSpec, BaseStation, int, GetAnimStationCallback> {
916 static const CallbackID cb_animation_speed = CBID_STATION_ANIMATION_SPEED;
917 static const CallbackID cb_animation_next_frame = CBID_STATION_ANIM_NEXT_FRAME;
918
919 static const StationCallbackMask cbm_animation_speed = CBM_STATION_ANIMATION_SPEED;
920 static const StationCallbackMask cbm_animation_next_frame = CBM_STATION_ANIMATION_NEXT_FRAME;
921 };
922
923 void AnimateStationTile(TileIndex tile)
924 {
925 const StationSpec *ss = GetStationSpec(tile);
926 if (ss == NULL) return;
927
928 StationAnimationBase::AnimateTile(ss, BaseStation::GetByTile(tile), tile, HasBit(ss->flags, SSF_CB141_RANDOM_BITS));
929 }
930
931 void TriggerStationAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoID cargo_type)
932 {
933 /* List of coverage areas for each animation trigger */
934 static const TriggerArea tas[] = {
935 TA_TILE, TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_WHOLE
936 };
937
938 /* Get Station if it wasn't supplied */
939 if (st == NULL) st = BaseStation::GetByTile(tile);
940
941 /* Check the cached animation trigger bitmask to see if we need
942 * to bother with any further processing. */
943 if (!HasBit(st->cached_anim_triggers, trigger)) return;
944
945 uint16 random_bits = Random();
946 ETileArea area = ETileArea(st, tile, tas[trigger]);
947
948 /* Check all tiles over the station to check if the specindex is still in use */
949 TILE_AREA_LOOP(tile, area) {
950 if (st->TileBelongsToRailStation(tile)) {
951 const StationSpec *ss = GetStationSpec(tile);
952 if (ss != NULL && HasBit(ss->animation.triggers, trigger)) {
953 CargoID cargo;
954 if (cargo_type == CT_INVALID) {
955 cargo = CT_INVALID;
956 } else {
957 cargo = ss->grf_prop.grffile->cargo_map[cargo_type];
958 }
959 StationAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP, ss, st, tile, (random_bits << 16) | Random(), (uint8)trigger | (cargo << 8));
960 }
961 }
962 }
963 }
964
972 void TriggerStationRandomisation(Station *st, TileIndex tile, StationRandomTrigger trigger, CargoID cargo_type)
973 {
974 /* List of coverage areas for each animation trigger */
975 static const TriggerArea tas[] = {
976 TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM
977 };
978
979 /* Get Station if it wasn't supplied */
980 if (st == NULL) st = Station::GetByTile(tile);
981
982 /* Check the cached cargo trigger bitmask to see if we need
983 * to bother with any further processing. */
984 if (st->cached_cargo_triggers == 0) return;
985 if (cargo_type != CT_INVALID && !HasBit(st->cached_cargo_triggers, cargo_type)) return;
986
987 uint32 whole_reseed = 0;
988 ETileArea area = ETileArea(st, tile, tas[trigger]);
989
990 uint32 empty_mask = 0;
991 if (trigger == SRT_CARGO_TAKEN) {
992 /* Create a bitmask of completely empty cargo types to be matched */
993 for (CargoID i = 0; i < NUM_CARGO; i++) {
994 if (st->goods[i].cargo.TotalCount() == 0) {
995 SetBit(empty_mask, i);
996 }
997 }
998 }
999
1000 /* Convert trigger to bit */
1001 uint8 trigger_bit = 1 << trigger;
1002
1003 /* Check all tiles over the station to check if the specindex is still in use */
1004 TILE_AREA_LOOP(tile, area) {
1005 if (st->TileBelongsToRailStation(tile)) {
1006 const StationSpec *ss = GetStationSpec(tile);
1007 if (ss == NULL) continue;
1008
1009 /* Cargo taken "will only be triggered if all of those
1010 * cargo types have no more cargo waiting." */
1011 if (trigger == SRT_CARGO_TAKEN) {
1012 if ((ss->cargo_triggers & ~empty_mask) != 0) continue;
1013 }
1014
1015 if (cargo_type == CT_INVALID || HasBit(ss->cargo_triggers, cargo_type)) {
1016 StationResolverObject object(ss, st, tile, CBID_RANDOM_TRIGGER, 0);
1017 object.trigger = trigger_bit;
1018
1019 const SpriteGroup *group = object.Resolve();
1020 if (group == NULL) continue;
1021
1022 uint32 reseed = object.GetReseedSum();
1023 if (reseed != 0) {
1024 whole_reseed |= reseed;
1025 reseed >>= 16;
1026
1027 /* Set individual tile random bits */
1028 uint8 random_bits = GetStationTileRandomBits(tile);
1029 random_bits &= ~reseed;
1030 random_bits |= Random() & reseed;
1031 SetStationTileRandomBits(tile, random_bits);
1032
1033 MarkTileDirtyByTile(tile);
1034 }
1035 }
1036 }
1037 }
1038
1039 /* Update whole station random bits */
1040 if ((whole_reseed & 0xFFFF) != 0) {
1041 st->random_bits &= ~whole_reseed;
1042 st->random_bits |= Random() & whole_reseed;
1043 }
1044 }
1045
1050 void StationUpdateCachedTriggers(BaseStation *st)
1051 {
1052 st->cached_anim_triggers = 0;
1053 st->cached_cargo_triggers = 0;
1054
1055 /* Combine animation trigger bitmask for all station specs
1056 * of this station. */
1057 for (uint i = 0; i < st->num_specs; i++) {
1058 const StationSpec *ss = st->speclist[i].spec;
1059 if (ss != NULL) {
1060 st->cached_anim_triggers |= ss->animation.triggers;
1061 st->cached_cargo_triggers |= ss->cargo_triggers;
1062 }
1063 }
1064 }
1065