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/>.
10 /** @file newgrf_station.cpp Functions for dealing with station classes and custom stations. */
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"
23 #include "newgrf_town.h"
24 #include "company_func.h"
25 #include "map/slope.h"
26 #include "newgrf_animation_base.h"
27 #include "newgrf_class_func.h"
29 #include "station_func.h"
33 /* static */ void NewGRFClass
<StationSpec
, StationClassID
, STAT_CLASS_MAX
>::InsertDefaults()
35 /* Set up initial data */
36 classes
[0].global_id
= 'DFLT';
37 classes
[0].name
= STR_STATION_CLASS_DFLT
;
38 classes
[0].Insert(NULL
);
40 classes
[1].global_id
= 'WAYP';
41 classes
[1].name
= STR_STATION_CLASS_WAYP
;
42 classes
[1].Insert(NULL
);
46 bool NewGRFClass
<StationSpec
, StationClassID
, STAT_CLASS_MAX
>::IsUIAvailable (uint index
) const
51 INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass
)
53 static const uint NUM_STATIONSSPECS_PER_STATION
= 255; ///< Maximum number of parts per station.
55 static void MakePlatformArea (TileArea
*area
, TileIndex tile
)
57 Axis axis
= GetRailStationAxis (tile
);
58 TileIndexDiff delta
= TileOffsByDiagDir (AxisToDiagDir (axis
));
61 for (uint i
= 0; i
< 2; i
++, delta
= -delta
) {
63 while (IsCompatibleTrainStationTile (t
+ delta
, tile
)) {
70 area
->w
= TileX(ends
[0]) - TileX(ends
[1]) + 1;
71 area
->h
= TileY(ends
[0]) - TileY(ends
[1]) + 1;
76 * Evaluate a tile's position within a station, and return the result in a bit-stuffed format.
77 * if not centered: .TNLcCpP, if centered: .TNL..CP
78 * - T = Tile layout number (#GetStationGfx)
79 * - N = Number of platforms
80 * - L = Length of platforms
81 * - C = Current platform number from start, c = from end
82 * - P = Position along platform from start, p = from end
84 * if centered, C/P start from the centre and c/p are not available.
85 * @return Platform information in bit-stuffed format.
87 uint32
GetPlatformInfo (byte tile
, int platforms
, int length
, int x
, int y
, bool centred
)
96 SB(retval
, 0, 4, y
& 0xF);
97 SB(retval
, 4, 4, x
& 0xF);
99 SB(retval
, 0, 4, min(15, y
));
100 SB(retval
, 4, 4, min(15, length
- y
- 1));
101 SB(retval
, 8, 4, min(15, x
));
102 SB(retval
, 12, 4, min(15, platforms
- x
- 1));
104 SB(retval
, 16, 4, min(15, length
));
105 SB(retval
, 20, 4, min(15, platforms
));
106 SB(retval
, 24, 4, tile
);
113 * Find the end of a railway station, from the \a tile, in the direction of \a delta.
114 * @param tile Start tile.
115 * @param delta Movement direction.
116 * @param check_type Stop when the custom station type changes.
117 * @param check_axis Stop when the station direction changes.
118 * @return Found end of the railway station.
120 static TileIndex
FindRailStationEnd(TileIndex tile
, TileIndexDiff delta
, bool check_type
, bool check_axis
)
123 Axis orig_axis
= AXIS_X
;
124 StationID sid
= GetStationIndex(tile
);
126 if (check_type
) orig_type
= GetCustomStationSpecIndex(tile
);
127 if (check_axis
) orig_axis
= GetRailStationAxis(tile
);
130 TileIndex new_tile
= TILE_ADD(tile
, delta
);
132 if (!IsStationTile(new_tile
) || GetStationIndex(new_tile
) != sid
) break;
133 if (!HasStationRail(new_tile
)) break;
134 if (check_type
&& GetCustomStationSpecIndex(new_tile
) != orig_type
) break;
135 if (check_axis
&& GetRailStationAxis(new_tile
) != orig_axis
) break;
143 static uint32
GetPlatformInfoHelper(TileIndex tile
, bool check_type
, bool check_axis
, bool centred
)
145 int tx
= TileX(tile
);
146 int ty
= TileY(tile
);
147 int sx
= TileX(FindRailStationEnd(tile
, TileDiffXY(-1, 0), check_type
, check_axis
));
148 int sy
= TileY(FindRailStationEnd(tile
, TileDiffXY( 0, -1), check_type
, check_axis
));
149 int ex
= TileX(FindRailStationEnd(tile
, TileDiffXY( 1, 0), check_type
, check_axis
)) + 1;
150 int ey
= TileY(FindRailStationEnd(tile
, TileDiffXY( 0, 1), check_type
, check_axis
)) + 1;
155 if (GetRailStationAxis (tile
) == AXIS_X
) {
160 return GetPlatformInfo (GetStationGfx(tile
), ex
, ey
, tx
, ty
, centred
);
164 static uint32
GetRailContinuationInfo(TileIndex tile
)
171 /* Tile offsets and exit dirs for X axis */
172 static const DataPair data_x
[8] = {
173 { DIR_SW
, DIAGDIR_SW
},
174 { DIR_NE
, DIAGDIR_NE
},
175 { DIR_SE
, DIAGDIR_SE
},
176 { DIR_NW
, DIAGDIR_NW
},
177 { DIR_S
, DIAGDIR_SW
},
178 { DIR_E
, DIAGDIR_NE
},
179 { DIR_W
, DIAGDIR_SW
},
180 { DIR_N
, DIAGDIR_NE
},
183 /* Tile offsets and exit dirs for Y axis */
184 static const DataPair data_y
[8] = {
185 { DIR_SE
, DIAGDIR_SE
},
186 { DIR_NW
, DIAGDIR_NW
},
187 { DIR_SW
, DIAGDIR_SW
},
188 { DIR_NE
, DIAGDIR_NE
},
189 { DIR_S
, DIAGDIR_SE
},
190 { DIR_W
, DIAGDIR_NW
},
191 { DIR_E
, DIAGDIR_SE
},
192 { DIR_N
, DIAGDIR_NW
},
195 Axis axis
= GetRailStationAxis(tile
);
197 /* Choose appropriate lookup table to use */
198 const DataPair
*data
= (axis
== AXIS_X
) ? data_x
: data_y
;
201 for (uint i
= 0; i
< lengthof(data_x
); i
++, data
++) {
202 TileIndex neighbour_tile
= tile
+ TileOffsByDir (data
->dir
);
203 TrackBits trackbits
= TrackStatusToTrackBits(GetTileRailwayStatus(neighbour_tile
));
204 if (trackbits
!= TRACK_BIT_NONE
) {
205 /* If there is any track on the tile, set the bit in the second byte */
208 /* With tunnels and bridges the tile has tracks, but they are not necessarily connected
209 * with the next tile because the ramp is not going in the right direction. */
210 if (IsTunnelTile (neighbour_tile
)) {
211 if (GetTunnelBridgeDirection (neighbour_tile
) != data
->exit
) {
214 } else if (IsBridgeHeadTile (neighbour_tile
)) {
215 if (GetTunnelBridgeDirection (neighbour_tile
) == ReverseDiagDir (data
->exit
)) {
220 /* If any track reaches our exit direction, set the bit in the lower byte */
221 if (trackbits
& DiagdirReachesTracks(data
->exit
)) SetBit(res
, i
);
229 /* Station Resolver Functions */
230 /* virtual */ uint32
StationScopeResolver::GetRandomBits() const
232 return this->st
->random_bits
| (this->tile
== INVALID_TILE
? 0 : GetStationTileRandomBits (this->tile
) << 16);
236 /* virtual */ uint32
StationScopeResolver::GetTriggers() const
238 return this->st
->waiting_triggers
;
242 /* virtual */ void StationScopeResolver::SetTriggers(int triggers
) const
244 BaseStation
*st
= const_cast<BaseStation
*>(this->st
);
246 st
->waiting_triggers
= triggers
;
250 * Station variable cache
251 * This caches 'expensive' station variable lookups which iterate over
252 * several tiles that may be called multiple times per Resolve().
261 uint8 valid
; ///< Bits indicating what variable is valid (for each bit, \c 0 is invalid, \c 1 is valid).
265 * Get the town scope associated with a station, if it exists.
266 * On the first call, the town scope is created (if possible).
267 * @return Town scope, if available.
269 TownScopeResolver
*StationResolverObject::GetTown()
271 if (this->town_scope
== NULL
) {
272 Town
*t
= this->station_scope
.st
->town
;
273 if (t
== NULL
) return NULL
;
274 this->town_scope
= new TownScopeResolver (this->grffile
, t
, false);
276 return this->town_scope
;
279 /* virtual */ uint32
StationScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
282 /* Calculated station variables */
284 if (!HasBit(_svc
.valid
, 0)) { _svc
.v40
= GetPlatformInfoHelper(this->tile
, false, false, false); SetBit(_svc
.valid
, 0); }
288 if (!HasBit(_svc
.valid
, 1)) { _svc
.v41
= GetPlatformInfoHelper(this->tile
, true, false, false); SetBit(_svc
.valid
, 1); }
291 case 0x42: return GetTerrainType(this->tile
) | (GetReverseRailTypeTranslation(GetRailType(this->tile
), this->statspec
->grf_prop
.grffile
) << 8);
292 case 0x43: return GetCompanyInfo(this->st
->owner
); // Station owner
293 case 0x44: return HasStationReservation(this->tile
) ? 7 : 4; // PBS status
295 if (!HasBit(_svc
.valid
, 2)) { _svc
.v45
= GetRailContinuationInfo(this->tile
); SetBit(_svc
.valid
, 2); }
299 if (!HasBit(_svc
.valid
, 3)) { _svc
.v46
= GetPlatformInfoHelper(this->tile
, false, false, true); SetBit(_svc
.valid
, 3); }
303 if (!HasBit(_svc
.valid
, 4)) { _svc
.v47
= GetPlatformInfoHelper(this->tile
, true, false, true); SetBit(_svc
.valid
, 4); }
307 if (!HasBit(_svc
.valid
, 5)) { _svc
.v49
= GetPlatformInfoHelper(this->tile
, false, true, false); SetBit(_svc
.valid
, 5); }
310 case 0x4A: // Animation frame of tile
311 return GetAnimationFrame(this->tile
);
313 /* Variables which use the parameter */
314 /* Variables 0x60 to 0x65 and 0x69 are handled separately below */
315 case 0x66: { // Animation frame of nearby tile
316 TileIndex tile
= this->tile
;
317 if (parameter
!= 0) tile
= GetNearbyTile(parameter
, tile
);
318 return this->st
->TileBelongsToRailStation(tile
) ? GetAnimationFrame(tile
) : UINT_MAX
;
321 case 0x67: { // Land info of nearby tile
322 Axis axis
= GetRailStationAxis(this->tile
);
323 TileIndex tile
= this->tile
;
324 if (parameter
!= 0) tile
= GetNearbyTile(parameter
, tile
); // only perform if it is required
326 Slope tileh
= GetTileSlope(tile
);
327 bool swap
= (axis
== AXIS_Y
&& HasBit(tileh
, CORNER_W
) != HasBit(tileh
, CORNER_E
));
329 return GetNearbyTileInformation (tile
, this->grffile
->grf_version
>= 8) ^ (swap
? SLOPE_EW
: 0);
332 case 0x68: { // Station info of nearby tiles
333 TileIndex nearby_tile
= GetNearbyTile(parameter
, this->tile
);
335 if (!HasStationTileRail(nearby_tile
)) return 0xFFFFFFFF;
337 uint32 grfid
= this->st
->speclist
[GetCustomStationSpecIndex(this->tile
)].grfid
;
338 bool perpendicular
= GetRailStationAxis(this->tile
) != GetRailStationAxis(nearby_tile
);
339 bool same_station
= this->st
->TileBelongsToRailStation(nearby_tile
);
340 uint32 res
= GB(GetStationGfx(nearby_tile
), 1, 2) << 12 | !!perpendicular
<< 11 | !!same_station
<< 10;
342 uint spec_index
= GetCustomStationSpecIndex (nearby_tile
);
343 if (spec_index
!= 0) {
344 const StationSpecList ssl
= BaseStation::GetByTile(nearby_tile
)->speclist
[spec_index
];
345 res
|= 1 << (ssl
.grfid
!= grfid
? 9 : 8) | ssl
.localidx
;
350 /* General station variables */
351 case 0x82: return 50;
352 case 0x84: return this->st
->string_id
;
354 case 0xF0: return this->st
->facilities
;
355 case 0xFA: return Clamp(this->st
->build_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535);
358 return this->st
->GetNewGRFVariable (this->grffile
, variable
, parameter
, available
);
361 uint32
Station::GetNewGRFVariable (const GRFFile
*grffile
, byte variable
, byte parameter
, bool *available
) const
364 case 0x48: { // Accepted cargo types
368 for (cargo_type
= 0; cargo_type
< NUM_CARGO
; cargo_type
++) {
369 if (HasBit(this->goods
[cargo_type
].status
, GoodsEntry::GES_ACCEPTANCE
)) SetBit(value
, cargo_type
);
374 case 0x8A: return this->had_vehicle_of_type
;
375 case 0xF1: return (this->airport
.tile
!= INVALID_TILE
) ? this->airport
.GetSpec()->ttd_airport_type
: ATP_TTDP_LARGE
;
376 case 0xF2: return (this->truck_stops
!= NULL
) ? this->truck_stops
->status
: 0;
377 case 0xF3: return (this->bus_stops
!= NULL
) ? this->bus_stops
->status
: 0;
378 case 0xF6: return this->airport
.flags
;
379 case 0xF7: return GB(this->airport
.flags
, 8, 8);
382 /* Handle cargo variables with parameter, 0x60 to 0x65 and 0x69 */
383 if ((variable
>= 0x60 && variable
<= 0x65) || variable
== 0x69) {
384 CargoID c
= GetCargoTranslation (parameter
, grffile
);
386 if (c
== CT_INVALID
) {
388 case 0x62: return 0xFFFFFFFF;
389 case 0x64: return 0xFF00;
393 const GoodsEntry
*ge
= &this->goods
[c
];
396 case 0x60: return min(ge
->cargo
.TotalCount(), 4095);
397 case 0x61: return ge
->HasVehicleEverTriedLoading() ? ge
->time_since_pickup
: 0;
398 case 0x62: return ge
->HasRating() ? ge
->rating
: 0xFFFFFFFF;
399 case 0x63: return ge
->cargo
.DaysInTransit();
400 case 0x64: return ge
->HasVehicleEverTriedLoading() ? ge
->last_speed
| (ge
->last_age
<< 8) : 0xFF00;
401 case 0x65: return GB(ge
->status
, GoodsEntry::GES_ACCEPTANCE
, 1) << 3;
403 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED
+ 1 == (int)GoodsEntry::GES_LAST_MONTH
);
404 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED
+ 2 == (int)GoodsEntry::GES_CURRENT_MONTH
);
405 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED
+ 3 == (int)GoodsEntry::GES_ACCEPTED_BIGTICK
);
406 return GB(ge
->status
, GoodsEntry::GES_EVER_ACCEPTED
, 4);
411 /* Handle cargo variables (deprecated) */
412 if (variable
>= 0x8C && variable
<= 0xEC) {
413 const GoodsEntry
*g
= &this->goods
[GB(variable
- 0x8C, 3, 4)];
414 switch (GB(variable
- 0x8C, 0, 3)) {
415 case 0: return g
->cargo
.TotalCount();
416 case 1: return GB(min(g
->cargo
.TotalCount(), 4095), 0, 4) | (GB(g
->status
, GoodsEntry::GES_ACCEPTANCE
, 1) << 7);
417 case 2: return g
->time_since_pickup
;
418 case 3: return g
->rating
;
419 case 4: return g
->cargo
.Source();
420 case 5: return g
->cargo
.DaysInTransit();
421 case 6: return g
->last_speed
;
422 case 7: return g
->last_age
;
426 DEBUG(grf
, 1, "Unhandled station variable 0x%X", variable
);
432 uint32
Waypoint::GetNewGRFVariable (const GRFFile
*grffile
, byte variable
, byte parameter
, bool *available
) const
435 case 0x48: return 0; // Accepted cargo types
436 case 0x8A: return HVOT_WAYPOINT
;
437 case 0xF1: return 0; // airport type
438 case 0xF2: return 0; // truck stop status
439 case 0xF3: return 0; // bus stop status
440 case 0xF6: return 0; // airport flags
441 case 0xF7: return 0; // airport flags cont.
444 /* Handle cargo variables with parameter, 0x60 to 0x65 */
445 if (variable
>= 0x60 && variable
<= 0x65) {
449 /* Handle cargo variables (deprecated) */
450 if (variable
>= 0x8C && variable
<= 0xEC) {
451 switch (GB(variable
- 0x8C, 0, 3)) {
452 case 3: return INITIAL_STATION_RATING
;
453 case 4: return INVALID_STATION
;
458 DEBUG(grf
, 1, "Unhandled station variable 0x%X", variable
);
464 /* virtual */ const SpriteGroup
*StationResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
466 if (this->station_scope
.statspec
->cls_id
== STAT_CLASS_WAYP
) {
467 return group
->get_first (true);
471 const Station
*st
= Station::From(this->station_scope
.st
);
473 switch (this->station_scope
.cargo_type
) {
481 for (CargoID cargo_type
= 0; cargo_type
< NUM_CARGO
; cargo_type
++) {
482 cargo
+= st
->goods
[cargo_type
].cargo
.TotalCount();
487 cargo
= st
->goods
[this->station_scope
.cargo_type
].cargo
.TotalCount();
491 if (HasBit(this->station_scope
.statspec
->flags
, SSF_DIV_BY_STATION_SIZE
)) cargo
/= (st
->train_station
.w
+ st
->train_station
.h
);
492 cargo
= min(0xfff, cargo
);
494 uint threshold
= this->station_scope
.statspec
->cargo_threshold
;
495 if (cargo
> threshold
) {
496 uint count
= group
->get_count (true);
498 uint set
= ((cargo
- threshold
) * count
) / (4096 - threshold
);
499 return group
->get_group (true, set
);
502 uint count
= group
->get_count (false);
504 uint set
= (cargo
* count
) / (threshold
+ 1);
505 return group
->get_group (false, set
);
509 return group
->get_first (false);
513 * Resolver for stations.
514 * @param statspec Station (type) specification.
515 * @param st Instance of the station.
516 * @param tile %Tile of the station.
517 * @param callback Callback ID.
518 * @param callback_param1 First parameter (var 10) of the callback.
519 * @param callback_param2 Second parameter (var 18) of the callback.
521 StationResolverObject::StationResolverObject(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
,
522 CallbackID callback
, uint32 callback_param1
, uint32 callback_param2
)
523 : ResolverObject(statspec
->grf_prop
.grffile
, callback
, callback_param1
, callback_param2
),
524 station_scope (this->grffile
, statspec
, st
, tile
), town_scope(NULL
)
528 /* Invalidate all cached vars */
531 CargoID ctype
= CT_DEFAULT_NA
;
533 if (!this->station_scope
.st
->IsWaypoint()) {
534 const Station
*st
= Station::From(this->station_scope
.st
);
535 /* Pick the first cargo that we have waiting */
537 FOR_ALL_CARGOSPECS(cs
) {
538 if (this->station_scope
.statspec
->spritegroup
[cs
->Index()] != NULL
&&
539 st
->goods
[cs
->Index()].cargo
.TotalCount() > 0) {
546 if (this->station_scope
.statspec
->spritegroup
[ctype
] == NULL
) {
550 /* Remember the cargo type we've picked */
551 this->station_scope
.cargo_type
= ctype
;
552 this->root_spritegroup
= this->station_scope
.statspec
->spritegroup
[this->station_scope
.cargo_type
];
555 StationResolverObject::~StationResolverObject()
557 delete this->town_scope
;
561 * Constructor for station scopes.
562 * @param grffile GRFFile the resolved SpriteGroup belongs to.
563 * @param statspec Station (type) specification.
564 * @param st Instance of the station.
565 * @param tile %Tile of the station.
567 StationScopeResolver::StationScopeResolver (const GRFFile
*grffile
, const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
)
568 : ScopeResolver(), grffile(grffile
)
574 this->statspec
= statspec
;
575 this->cargo_type
= CT_INVALID
;
579 /** Scope resolver for stations not yet built. */
580 struct FakeStationScopeResolver
: public ScopeResolver
{
581 const GRFFile
*const grffile
; ///< GRFFile the resolved SpriteGroup belongs to.
582 TileIndex tile
; ///< %Tile of the station.
583 const struct StationSpec
*statspec
; ///< Station (type) specification.
584 RailType railtype
; ///< Rail type.
585 Axis axis
; ///< Station axis, used only for the slope check callback.
587 FakeStationScopeResolver (const GRFFile
*grffile
, const StationSpec
*statspec
,
588 TileIndex tile
, RailType rt
, Axis axis
= INVALID_AXIS
)
589 : ScopeResolver(), grffile(grffile
), tile(tile
),
590 statspec(statspec
), railtype(rt
), axis(axis
)
594 uint32
GetVariable (byte variable
, uint32 parameter
, bool *available
) const OVERRIDE
;
597 uint32
FakeStationScopeResolver::GetVariable (byte variable
, uint32 parameter
, bool *available
) const
599 /* Station does not exist, so we're in a purchase list or the land slope check callback. */
605 case 0x49: return 0x2110000; // Platforms, tracks & position
606 case 0x42: return GetReverseRailTypeTranslation (this->railtype
, this->statspec
->grf_prop
.grffile
) << 8;
607 case 0x43: return GetCompanyInfo (_current_company
); // Station owner
608 case 0x44: return 2; // PBS status
609 case 0x67: // Land info of nearby tile
610 if (this->axis
!= INVALID_AXIS
&& this->tile
!= INVALID_TILE
) {
611 TileIndex tile
= this->tile
;
612 if (parameter
!= 0) tile
= GetNearbyTile (parameter
, tile
, true, this->axis
); // only perform if it is required
614 Slope tileh
= GetTileSlope(tile
);
615 bool swap
= (this->axis
== AXIS_Y
&& HasBit(tileh
, CORNER_W
) != HasBit(tileh
, CORNER_E
));
617 return GetNearbyTileInformation (tile
, this->grffile
->grf_version
>= 8) ^ (swap
? SLOPE_EW
: 0);
621 case 0xFA: return Clamp (_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535); // Build date, clamped to a 16 bit value
628 /** Resolver for stations not yet built. */
629 struct FakeStationResolverObject
: public ResolverObject
{
630 FakeStationScopeResolver station_scope
; ///< The station scope resolver.
631 TownScopeResolver
*town_scope
; ///< The town scope resolver (created on the first call).
633 const SpriteGroup
*root_spritegroup
; ///< Root SpriteGroup to use for resolving
636 * Resolver for stations not yet built.
637 * @param statspec Station (type) specification.
638 * @param tile %Tile of the station.
639 * @param rt Rail type.
640 * @param callback Callback ID.
641 * @param callback_param1 First parameter (var 10) of the callback.
642 * @param callback_param2 Second parameter (var 18) of the callback.
643 * @param axis Axis of the station tile to build, if any.
645 FakeStationResolverObject (const StationSpec
*statspec
, TileIndex tile
,
646 RailType rt
, CallbackID callback
= CBID_NO_CALLBACK
,
647 uint32 callback_param1
= 0, uint32 callback_param2
= 0,
648 Axis axis
= INVALID_AXIS
)
649 : ResolverObject (statspec
->grf_prop
.grffile
,
650 callback
, callback_param1
, callback_param2
),
651 station_scope (this->grffile
, statspec
, tile
, rt
, axis
),
654 /* Invalidate all cached vars */
657 /* No station, so we are in a purchase list */
658 const SpriteGroup
*const *groups
= statspec
->spritegroup
;
659 const SpriteGroup
*root
= groups
[CT_PURCHASE
];
662 root
= groups
[CT_DEFAULT
];
665 this->root_spritegroup
= root
;
668 ~FakeStationResolverObject();
670 TownScopeResolver
*GetTown (void);
672 ScopeResolver
*GetScope (VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0) OVERRIDE
676 return &this->station_scope
;
678 case VSG_SCOPE_PARENT
: {
679 TownScopeResolver
*tsr
= this->GetTown();
680 if (tsr
!= NULL
) return tsr
;
685 return ResolverObject::GetScope (scope
, relative
);
689 const SpriteGroup
*ResolveReal (const RealSpriteGroup
*group
) const OVERRIDE
691 return group
->get_first (true);
695 * Resolve SpriteGroup.
696 * @return Result spritegroup.
698 const SpriteGroup
*Resolve (void)
700 return SpriteGroup::Resolve (this->root_spritegroup
, *this);
704 FakeStationResolverObject::~FakeStationResolverObject()
706 delete this->town_scope
;
710 * Get the town scope associated with a station, if it exists.
711 * On the first call, the town scope is created (if possible).
712 * @return Town scope, if available.
714 TownScopeResolver
*FakeStationResolverObject::GetTown (void)
716 if (this->town_scope
== NULL
) {
718 if (this->station_scope
.tile
!= INVALID_TILE
) {
719 t
= ClosestTownFromTile(this->station_scope
.tile
);
721 if (t
== NULL
) return NULL
;
722 this->town_scope
= new TownScopeResolver (this->grffile
, t
, true);
724 return this->town_scope
;
729 * Resolve sprites for drawing a station tile.
730 * @param statspec Station spec
732 * @param tile Station tile being drawn
733 * @param var10 Value to put in variable 10; normally 0; 1 when resolving the groundsprite and SSF_SEPARATE_GROUND is set.
734 * @return First sprite of the Action 1 spriteset to use, minus an offset of 0x42D to accommodate for weird NewGRF specs.
736 SpriteID
GetCustomStationRelocation(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
, uint32 var10
)
739 StationResolverObject
object(statspec
, st
, tile
, CBID_NO_CALLBACK
, var10
);
740 const SpriteGroup
*group
= object
.Resolve();
741 if (group
== NULL
|| !group
->IsType (SGT_RESULT
)) return 0;
742 return group
->GetResult() - 0x42D;
746 * Resolve sprites for drawing a station tile in the GUI.
747 * @param statspec Station spec
748 * @param rt Rail type.
749 * @param var10 Value to put in variable 10; normally 0; 1 when resolving the groundsprite and SSF_SEPARATE_GROUND is set.
750 * @return First sprite of the Action 1 spriteset to use, minus an offset of 0x42D to accommodate for weird NewGRF specs.
752 static SpriteID
GetCustomStationRelocation (const StationSpec
*statspec
,
753 RailType rt
, uint32 var10
)
755 FakeStationResolverObject
object (statspec
, INVALID_TILE
, rt
, CBID_NO_CALLBACK
, var10
);
756 const SpriteGroup
*group
= object
.Resolve();
757 if (group
== NULL
|| !group
->IsType (SGT_RESULT
)) return 0;
758 return group
->GetResult() - 0x42D;
762 * Resolve the sprites for custom station foundations.
763 * @param statspec Station spec
765 * @param tile Station tile being drawn
766 * @param layout Spritelayout as returned by previous callback
767 * @param edge_info Information about northern tile edges; whether they need foundations or merge into adjacent tile's foundations.
768 * @return First sprite of a set of foundation sprites for various slopes, or 0 if default foundations shall be drawn.
770 SpriteID
GetCustomStationFoundationRelocation(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
, uint layout
, uint edge_info
)
772 /* callback_param1 == 2 means we are resolving the foundation sprites. */
773 StationResolverObject
object(statspec
, st
, tile
, CBID_NO_CALLBACK
, 2, layout
| (edge_info
<< 16));
775 const SpriteGroup
*group
= object
.Resolve();
776 if (group
== NULL
|| !group
->IsType (SGT_RESULT
)) return 0;
778 /* Note: SpriteGroup::Resolve zeroes all registers, so register 0x100 is initialised to 0. (compatibility) */
779 return group
->GetResult() + GetRegister(0x100);
783 uint16
GetStationCallback(CallbackID callback
, uint32 param1
, uint32 param2
, const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
)
786 StationResolverObject
object(statspec
, st
, tile
, callback
, param1
, param2
);
787 return SpriteGroup::CallbackResult (object
.Resolve());
790 uint16
GetStationCallback (CallbackID callback
, uint32 param1
, uint32 param2
,
791 const StationSpec
*statspec
, RailType rt
, TileIndex tile
)
793 FakeStationResolverObject
object (statspec
, tile
, rt
, callback
, param1
, param2
);
794 return SpriteGroup::CallbackResult (object
.Resolve());
798 * Check the slope of a tile of a new station.
799 * @param north_tile Norther tile of the station rect.
800 * @param cur_tile Tile to check.
801 * @param statspec Station spec.
802 * @param rt Rail type.
803 * @param axis Axis of the new station.
804 * @param plat_len Platform length.
805 * @param numtracks Number of platforms.
806 * @return Succeeded or failed command.
808 CommandCost
PerformStationTileSlopeCheck (TileIndex north_tile
,
809 TileIndex cur_tile
, const StationSpec
*statspec
, RailType rt
,
810 Axis axis
, byte plat_len
, byte numtracks
)
812 TileIndexDiff diff
= cur_tile
- north_tile
;
813 Slope slope
= GetTileSlope(cur_tile
);
815 FakeStationResolverObject
object (statspec
, cur_tile
, rt
, CBID_STATION_LAND_SLOPE_CHECK
,
816 (slope
<< 4) | (slope
^ (axis
== AXIS_Y
&& HasBit(slope
, CORNER_W
) != HasBit(slope
, CORNER_E
) ? SLOPE_EW
: 0)),
817 (numtracks
<< 24) | (plat_len
<< 16) | (axis
== AXIS_Y
? TileX(diff
) << 8 | TileY(diff
) : TileY(diff
) << 8 | TileX(diff
)),
820 uint16 cb_res
= SpriteGroup::CallbackResult (object
.Resolve());
822 /* Failed callback means success. */
823 if (cb_res
== CALLBACK_FAILED
) return CommandCost();
825 /* The meaning of bit 10 is inverted for a grf version < 8. */
826 if (statspec
->grf_prop
.grffile
->grf_version
< 8) ToggleBit(cb_res
, 10);
827 return GetErrorMessageFromLocationCallbackResult(cb_res
, statspec
->grf_prop
.grffile
, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
832 * Allocate a StationSpec to a Station. This is called once per build operation.
833 * @param statspec StationSpec to allocate.
834 * @param st Station to allocate it to.
835 * @param exec Whether to actually allocate the spec.
836 * @return Index within the Station's spec list, or -1 if the allocation failed.
838 int AllocateSpecToStation(const StationSpec
*statspec
, BaseStation
*st
, bool exec
)
842 if (statspec
== NULL
|| st
== NULL
) return 0;
844 for (i
= 1; i
< st
->num_specs
&& i
< NUM_STATIONSSPECS_PER_STATION
; i
++) {
845 if (st
->speclist
[i
].spec
== NULL
&& st
->speclist
[i
].grfid
== 0) break;
848 if (i
== NUM_STATIONSSPECS_PER_STATION
) {
849 /* As final effort when the spec list is already full...
850 * try to find the same spec and return that one. This might
851 * result in slightly "wrong" (as per specs) looking stations,
852 * but it's fairly unlikely that one reaches the limit anyways.
854 for (i
= 1; i
< st
->num_specs
&& i
< NUM_STATIONSSPECS_PER_STATION
; i
++) {
855 if (st
->speclist
[i
].spec
== statspec
) return i
;
862 if (i
>= st
->num_specs
) {
863 st
->num_specs
= i
+ 1;
864 st
->speclist
= xrealloct (st
->speclist
, st
->num_specs
);
866 if (st
->num_specs
== 2) {
867 /* Initial allocation */
868 st
->speclist
[0].spec
= NULL
;
869 st
->speclist
[0].grfid
= 0;
870 st
->speclist
[0].localidx
= 0;
874 st
->speclist
[i
].spec
= statspec
;
875 st
->speclist
[i
].grfid
= statspec
->grf_prop
.grffile
->grfid
;
876 st
->speclist
[i
].localidx
= statspec
->grf_prop
.local_id
;
878 StationUpdateCachedTriggers(st
);
886 * Deallocate a StationSpec from a Station. Called when removing a single station tile.
887 * @param st Station to work with.
888 * @param specindex Index of the custom station within the Station's spec list.
889 * @return Indicates whether the StationSpec was deallocated.
891 void DeallocateSpecFromStation(BaseStation
*st
, byte specindex
)
893 /* specindex of 0 (default) is never freeable */
894 if (specindex
== 0) return;
896 /* Check all tiles over the station to check if the specindex is still in use */
897 TILE_AREA_LOOP(tile
, st
->train_station
) {
898 if (st
->TileBelongsToRailStation(tile
) && GetCustomStationSpecIndex(tile
) == specindex
) {
903 /* This specindex is no longer in use, so deallocate it */
904 st
->speclist
[specindex
].spec
= NULL
;
905 st
->speclist
[specindex
].grfid
= 0;
906 st
->speclist
[specindex
].localidx
= 0;
908 /* If this was the highest spec index, reallocate */
909 if (specindex
== st
->num_specs
- 1) {
910 for (; st
->speclist
[st
->num_specs
- 1].grfid
== 0 && st
->num_specs
> 1; st
->num_specs
--) {}
912 if (st
->num_specs
> 1) {
913 st
->speclist
= xrealloct (st
->speclist
, st
->num_specs
);
918 st
->cached_anim_triggers
= 0;
919 st
->cached_cargo_triggers
= 0;
924 StationUpdateCachedTriggers(st
);
928 * Draw representation of a station tile for GUI purposes.
929 * @param dpi The area to draw on.
930 * @param x Position x of image.
931 * @param y Position y of image.
933 * @param railtype Rail type.
934 * @param sclass, station Type of station.
935 * @param station station ID
936 * @return True if the tile was drawn (allows for fallback to default graphic)
938 bool DrawStationTile (BlitArea
*dpi
, int x
, int y
, RailType railtype
,
939 Axis axis
, StationClassID sclass
, uint station
)
941 const DrawTileSprites
*sprites
= NULL
;
942 const RailtypeInfo
*rti
= GetRailTypeInfo(railtype
);
943 PaletteID palette
= COMPANY_SPRITE_COLOUR(_local_company
);
946 const StationSpec
*statspec
= StationClass::Get(sclass
)->GetSpec(station
);
947 if (statspec
== NULL
) return false;
949 if (HasBit(statspec
->callback_mask
, CBM_STATION_SPRITE_LAYOUT
)) {
950 uint16 callback
= GetStationCallback (CBID_STATION_SPRITE_LAYOUT
, 0x2110000, 0, statspec
, railtype
);
951 if (callback
!= CALLBACK_FAILED
) tile
= callback
;
954 uint32 total_offset
= rti
->GetRailtypeSpriteOffset();
955 uint32 relocation
= 0;
956 uint32 ground_relocation
= 0;
957 const NewGRFSpriteLayout
*layout
= NULL
;
959 if (statspec
->renderdata
.empty()) {
960 sprites
= GetDefaultStationTileLayout() + (tile
+ axis
);
962 uint t
= (tile
< statspec
->renderdata
.size()) ? tile
: 0;
963 layout
= statspec
->renderdata
[t
+ axis
].get();
964 if (!layout
->NeedsPreprocessing()) {
970 NewGRFSpriteLayout::Result result
;
972 const DrawTileSeqStruct
*seq
;
973 if (layout
!= NULL
) {
974 /* Sprite layout which needs preprocessing */
975 bool separate_ground
= HasBit(statspec
->flags
, SSF_SEPARATE_GROUND
);
976 uint32 var10_values
= result
.prepare (layout
, 0, total_offset
, rti
->fallback_railtype
, separate_ground
);
978 FOR_EACH_SET_BIT(var10
, var10_values
) {
979 uint32 var10_relocation
= GetCustomStationRelocation (statspec
, railtype
, var10
);
980 result
.process (layout
, var10
, var10_relocation
, separate_ground
);
982 ground
= result
.get_ground();
983 seq
= result
.get_seq();
986 /* Simple sprite layout */
987 ground
= sprites
->ground
;
989 ground_relocation
= relocation
= GetCustomStationRelocation (statspec
, railtype
, 0);
990 if (HasBit(ground
.sprite
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) {
991 ground_relocation
= GetCustomStationRelocation (statspec
, railtype
, 1);
993 ground_relocation
+= rti
->fallback_railtype
;
996 SpriteID image
= ground
.sprite
;
997 PaletteID pal
= ground
.pal
;
998 RailTrackOffset overlay_offset
;
999 if (rti
->UsesOverlay() && SplitGroundSpriteForOverlay (&image
, &overlay_offset
)) {
1000 SpriteID ground
= GetCustomRailSprite(rti
, INVALID_TILE
, RTSG_GROUND
);
1001 DrawSprite (dpi
, image
, PAL_NONE
, x
, y
);
1002 DrawSprite (dpi
, ground
+ overlay_offset
, PAL_NONE
, x
, y
);
1004 image
+= HasBit(image
, SPRITE_MODIFIER_CUSTOM_SPRITE
) ? ground_relocation
: total_offset
;
1005 if (HasBit(pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) pal
+= ground_relocation
;
1006 DrawSprite (dpi
, image
, GroundSpritePaletteTransform (image
, pal
, palette
), x
, y
);
1009 DrawRailTileSeqInGUI (dpi
, x
, y
, seq
, total_offset
, relocation
, palette
);
1015 const StationSpec
*GetStationSpec(TileIndex t
)
1017 uint specindex
= GetCustomStationSpecIndex(t
);
1018 if (specindex
== 0) return NULL
;
1020 const BaseStation
*st
= BaseStation::GetByTile(t
);
1021 return specindex
< st
->num_specs
? st
->speclist
[specindex
].spec
: NULL
;
1026 * Check whether a rail station tile is NOT traversable.
1027 * @param tile %Tile to test.
1028 * @return Station tile is blocked.
1029 * @note This could be cached (during build) in the map array to save on all the dereferencing.
1031 bool IsStationTileBlocked(TileIndex tile
)
1033 const StationSpec
*statspec
= GetStationSpec(tile
);
1035 return statspec
!= NULL
&& HasBit(statspec
->blocked
, GetStationGfx(tile
));
1039 * Check if a rail station tile shall have pylons when electrified.
1040 * @param tile %Tile to test.
1041 * @return Tile shall have pylons.
1042 * @note This could be cached (during build) in the map array to save on all the dereferencing.
1044 bool CanStationTileHavePylons(TileIndex tile
)
1046 const StationSpec
*statspec
= GetStationSpec(tile
);
1047 uint gfx
= GetStationGfx(tile
);
1048 /* Default stations do not draw pylons under roofs (gfx >= 4) */
1049 return statspec
!= NULL
? HasBit(statspec
->pylons
, gfx
) : gfx
< 4;
1053 * Check if a rail station tile shall have wires when electrified.
1054 * @param tile %Tile to test.
1055 * @return Tile shall have wires.
1056 * @note This could be cached (during build) in the map array to save on all the dereferencing.
1058 bool CanStationTileHaveWires(TileIndex tile
)
1060 const StationSpec
*statspec
= GetStationSpec(tile
);
1061 return statspec
== NULL
|| !HasBit(statspec
->wires
, GetStationGfx(tile
));
1064 /** Helper class for animation control. */
1065 struct StationAnimationBase
{
1066 static const CallbackID cb_animation_speed
= CBID_STATION_ANIMATION_SPEED
;
1067 static const CallbackID cb_animation_next_frame
= CBID_STATION_ANIM_NEXT_FRAME
;
1069 static const StationCallbackMask cbm_animation_speed
= CBM_STATION_ANIMATION_SPEED
;
1070 static const StationCallbackMask cbm_animation_next_frame
= CBM_STATION_ANIMATION_NEXT_FRAME
;
1072 /** Callback wrapper for animation control. */
1073 static uint16
get_callback (CallbackID callback
, uint32 param1
, uint32 param2
, const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
)
1075 return GetStationCallback (callback
, param1
, param2
, statspec
, st
, tile
);
1079 void AnimateStationTile(TileIndex tile
)
1081 const StationSpec
*ss
= GetStationSpec(tile
);
1082 if (ss
== NULL
) return;
1084 AnimationBase::AnimateTile
<StationAnimationBase
> (ss
, BaseStation::GetByTile(tile
), tile
, HasBit(ss
->flags
, SSF_CB141_RANDOM_BITS
));
1087 void TriggerStationAnimation(BaseStation
*st
, TileIndex tile
, StationAnimationTrigger trigger
, CargoID cargo_type
)
1089 /* Bitmask of animation triggers that affect the whole station. */
1090 static const uint whole
= (1 << SAT_NEW_CARGO
)
1091 | (1 << SAT_CARGO_TAKEN
)
1092 | (1 << SAT_250_TICKS
);
1094 /* Get Station if it wasn't supplied */
1095 if (st
== NULL
) st
= BaseStation::GetByTile(tile
);
1097 /* Check the cached animation trigger bitmask to see if we need
1098 * to bother with any further processing. */
1099 if (!HasBit(st
->cached_anim_triggers
, trigger
)) return;
1101 uint16 random_bits
= Random();
1103 if (trigger
== SAT_BUILT
) {
1107 } else if (HasBit(whole
, trigger
)) {
1108 area
= st
->train_station
;
1110 MakePlatformArea (&area
, tile
);
1113 /* Check all tiles over the station to check if the specindex is still in use */
1114 TILE_AREA_LOOP(tile
, area
) {
1115 if (st
->TileBelongsToRailStation(tile
)) {
1116 const StationSpec
*ss
= GetStationSpec(tile
);
1117 if (ss
!= NULL
&& HasBit(ss
->animation
.triggers
, trigger
)) {
1119 if (cargo_type
== CT_INVALID
) {
1122 cargo
= ss
->grf_prop
.grffile
->cargo_map
[cargo_type
];
1124 uint16 callback
= GetStationCallback (CBID_STATION_ANIM_START_STOP
,
1125 (random_bits
<< 16) | Random(),
1126 (uint8
)trigger
| (cargo
<< 8),
1128 AnimationBase::ChangeAnimationFrame (ss
, tile
, callback
);
1135 * Trigger station randomisation
1136 * @param st station being triggered
1137 * @param tile specific tile of platform to trigger
1138 * @param trigger trigger type
1139 * @param cargo_type cargo type causing trigger
1141 void TriggerStationRandomisation(Station
*st
, TileIndex tile
, StationRandomTrigger trigger
, CargoID cargo_type
)
1143 /* Bitmask of randomisation triggers that affect the whole station. */
1144 static const uint whole
= (1 << SRT_NEW_CARGO
)
1145 | (1 << SRT_CARGO_TAKEN
);
1147 /* Get Station if it wasn't supplied */
1148 if (st
== NULL
) st
= Station::GetByTile(tile
);
1150 /* Check the cached cargo trigger bitmask to see if we need
1151 * to bother with any further processing. */
1152 if (st
->cached_cargo_triggers
== 0) return;
1153 if (cargo_type
!= CT_INVALID
&& !HasBit(st
->cached_cargo_triggers
, cargo_type
)) return;
1155 uint32 whole_reseed
= 0;
1157 uint32 cargo_mask
= 0;
1158 if (trigger
== SRT_CARGO_TAKEN
) {
1159 /* Create a bitmask of completely empty cargo types to be matched */
1160 uint32 empty_mask
= 0;
1161 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
1162 if (st
->goods
[i
].cargo
.TotalCount() == 0) {
1163 SetBit(empty_mask
, i
);
1166 cargo_mask
= ~empty_mask
;
1169 /* Convert trigger to bit */
1170 uint8 trigger_bit
= 1 << trigger
;
1173 if ((whole
& trigger_bit
) != 0) {
1174 area
= st
->train_station
;
1176 MakePlatformArea (&area
, tile
);
1179 /* Check all tiles over the station to check if the specindex is still in use */
1180 TILE_AREA_LOOP(tile
, area
) {
1181 if (st
->TileBelongsToRailStation(tile
)) {
1182 const StationSpec
*ss
= GetStationSpec(tile
);
1183 if (ss
== NULL
) continue;
1185 /* Cargo taken "will only be triggered if all of those
1186 * cargo types have no more cargo waiting." */
1187 if ((ss
->cargo_triggers
& cargo_mask
) != 0) continue;
1189 if (cargo_type
== CT_INVALID
|| HasBit(ss
->cargo_triggers
, cargo_type
)) {
1190 StationResolverObject
object(ss
, st
, tile
, CBID_RANDOM_TRIGGER
, 0);
1191 object
.trigger
= trigger_bit
;
1193 const SpriteGroup
*group
= object
.Resolve();
1194 if (group
== NULL
) continue;
1196 uint32 reseed
= object
.GetReseedSum();
1198 whole_reseed
|= reseed
;
1201 /* Set individual tile random bits */
1202 uint8 random_bits
= GetStationTileRandomBits(tile
);
1203 random_bits
&= ~reseed
;
1204 random_bits
|= Random() & reseed
;
1205 SetStationTileRandomBits(tile
, random_bits
);
1207 MarkTileDirtyByTile(tile
);
1213 /* Update whole station random bits */
1214 if ((whole_reseed
& 0xFFFF) != 0) {
1215 st
->random_bits
&= ~whole_reseed
;
1216 st
->random_bits
|= Random() & whole_reseed
;
1221 * Update the cached animation trigger bitmask for a station.
1222 * @param st Station to update.
1224 void StationUpdateCachedTriggers(BaseStation
*st
)
1226 st
->cached_anim_triggers
= 0;
1227 st
->cached_cargo_triggers
= 0;
1229 /* Combine animation trigger bitmask for all station specs
1230 * of this station. */
1231 for (uint i
= 0; i
< st
->num_specs
; i
++) {
1232 const StationSpec
*ss
= st
->speclist
[i
].spec
;
1234 st
->cached_anim_triggers
|= ss
->animation
.triggers
;
1235 st
->cached_cargo_triggers
|= ss
->cargo_triggers
;