2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file newgrf_station.cpp Functions for dealing with station classes and custom stations. */
12 #include "station_base.h"
13 #include "waypoint_base.h"
14 #include "roadstop_base.h"
15 #include "newgrf_cargo.h"
16 #include "newgrf_station.h"
17 #include "newgrf_spritegroup.h"
18 #include "newgrf_sound.h"
19 #include "newgrf_railtype.h"
21 #include "newgrf_town.h"
22 #include "company_func.h"
23 #include "tunnelbridge_map.h"
24 #include "newgrf_animation_base.h"
25 #include "newgrf_class_func.h"
27 #include "safeguards.h"
30 template <typename Tspec
, typename Tid
, Tid Tmax
>
31 /* static */ void NewGRFClass
<Tspec
, Tid
, Tmax
>::InsertDefaults()
33 /* Set up initial data */
34 classes
[0].global_id
= 'DFLT';
35 classes
[0].name
= STR_STATION_CLASS_DFLT
;
36 classes
[0].Insert(nullptr);
38 classes
[1].global_id
= 'WAYP';
39 classes
[1].name
= STR_STATION_CLASS_WAYP
;
40 classes
[1].Insert(nullptr);
43 template <typename Tspec
, typename Tid
, Tid Tmax
>
44 bool NewGRFClass
<Tspec
, Tid
, Tmax
>::IsUIAvailable(uint index
) const
49 INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass
, StationSpec
, StationClassID
, STAT_CLASS_MAX
)
51 static const uint NUM_STATIONSSPECS_PER_STATION
= 255; ///< Maximum number of parts per station.
59 struct ETileArea
: TileArea
{
60 ETileArea(const BaseStation
*st
, TileIndex tile
, TriggerArea ta
)
63 default: NOT_REACHED();
73 Axis axis
= GetRailStationAxis(tile
);
74 TileIndexDiff delta
= TileOffsByDiagDir(AxisToDiagDir(axis
));
76 for (end
= tile
; IsRailStationTile(end
+ delta
) && IsCompatibleTrainStationTile(end
+ delta
, tile
); end
+= delta
) { /* Nothing */ }
77 for (start
= tile
; IsRailStationTile(start
- delta
) && IsCompatibleTrainStationTile(start
- delta
, tile
); start
-= delta
) { /* Nothing */ }
80 this->w
= TileX(end
) - TileX(start
) + 1;
81 this->h
= TileY(end
) - TileY(start
) + 1;
86 st
->GetTileArea(this, Station::IsExpected(st
) ? STATION_RAIL
: STATION_WAYPOINT
);
94 * Evaluate a tile's position within a station, and return the result in a bit-stuffed format.
95 * if not centered: .TNLcCpP, if centered: .TNL..CP
96 * - T = Tile layout number (#GetStationGfx)
97 * - N = Number of platforms
98 * - L = Length of platforms
99 * - C = Current platform number from start, c = from end
100 * - P = Position along platform from start, p = from end
102 * if centered, C/P start from the centre and c/p are not available.
103 * @return Platform information in bit-stuffed format.
105 uint32
GetPlatformInfo(Axis axis
, byte tile
, int platforms
, int length
, int x
, int y
, bool centred
)
109 if (axis
== AXIS_X
) {
110 Swap(platforms
, length
);
119 SB(retval
, 0, 4, y
& 0xF);
120 SB(retval
, 4, 4, x
& 0xF);
122 SB(retval
, 0, 4, min(15, y
));
123 SB(retval
, 4, 4, min(15, length
- y
- 1));
124 SB(retval
, 8, 4, min(15, x
));
125 SB(retval
, 12, 4, min(15, platforms
- x
- 1));
127 SB(retval
, 16, 4, min(15, length
));
128 SB(retval
, 20, 4, min(15, platforms
));
129 SB(retval
, 24, 4, tile
);
136 * Find the end of a railway station, from the \a tile, in the direction of \a delta.
137 * @param tile Start tile.
138 * @param delta Movement direction.
139 * @param check_type Stop when the custom station type changes.
140 * @param check_axis Stop when the station direction changes.
141 * @return Found end of the railway station.
143 static TileIndex
FindRailStationEnd(TileIndex tile
, TileIndexDiff delta
, bool check_type
, bool check_axis
)
146 Axis orig_axis
= AXIS_X
;
147 StationID sid
= GetStationIndex(tile
);
149 if (check_type
) orig_type
= GetCustomStationSpecIndex(tile
);
150 if (check_axis
) orig_axis
= GetRailStationAxis(tile
);
153 TileIndex new_tile
= TILE_ADD(tile
, delta
);
155 if (!IsTileType(new_tile
, MP_STATION
) || GetStationIndex(new_tile
) != sid
) break;
156 if (!HasStationRail(new_tile
)) break;
157 if (check_type
&& GetCustomStationSpecIndex(new_tile
) != orig_type
) break;
158 if (check_axis
&& GetRailStationAxis(new_tile
) != orig_axis
) break;
166 static uint32
GetPlatformInfoHelper(TileIndex tile
, bool check_type
, bool check_axis
, bool centred
)
168 int tx
= TileX(tile
);
169 int ty
= TileY(tile
);
170 int sx
= TileX(FindRailStationEnd(tile
, TileDiffXY(-1, 0), check_type
, check_axis
));
171 int sy
= TileY(FindRailStationEnd(tile
, TileDiffXY( 0, -1), check_type
, check_axis
));
172 int ex
= TileX(FindRailStationEnd(tile
, TileDiffXY( 1, 0), check_type
, check_axis
)) + 1;
173 int ey
= TileY(FindRailStationEnd(tile
, TileDiffXY( 0, 1), check_type
, check_axis
)) + 1;
178 return GetPlatformInfo(GetRailStationAxis(tile
), GetStationGfx(tile
), ex
, ey
, tx
, ty
, centred
);
182 static uint32
GetRailContinuationInfo(TileIndex tile
)
184 /* Tile offsets and exit dirs for X axis */
185 static const Direction x_dir
[8] = { DIR_SW
, DIR_NE
, DIR_SE
, DIR_NW
, DIR_S
, DIR_E
, DIR_W
, DIR_N
};
186 static const DiagDirection x_exits
[8] = { DIAGDIR_SW
, DIAGDIR_NE
, DIAGDIR_SE
, DIAGDIR_NW
, DIAGDIR_SW
, DIAGDIR_NE
, DIAGDIR_SW
, DIAGDIR_NE
};
188 /* Tile offsets and exit dirs for Y axis */
189 static const Direction y_dir
[8] = { DIR_SE
, DIR_NW
, DIR_SW
, DIR_NE
, DIR_S
, DIR_W
, DIR_E
, DIR_N
};
190 static const DiagDirection y_exits
[8] = { DIAGDIR_SE
, DIAGDIR_NW
, DIAGDIR_SW
, DIAGDIR_NE
, DIAGDIR_SE
, DIAGDIR_NW
, DIAGDIR_SE
, DIAGDIR_NW
};
192 Axis axis
= GetRailStationAxis(tile
);
194 /* Choose appropriate lookup table to use */
195 const Direction
*dir
= axis
== AXIS_X
? x_dir
: y_dir
;
196 const DiagDirection
*diagdir
= axis
== AXIS_X
? x_exits
: y_exits
;
201 for (i
= 0; i
< lengthof(x_dir
); i
++, dir
++, diagdir
++) {
202 TileIndex neighbour_tile
= tile
+ TileOffsByDir(*dir
);
203 TrackBits trackbits
= TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile
, TRANSPORT_RAIL
, 0));
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 (IsTileType(neighbour_tile
, MP_TUNNELBRIDGE
) && GetTunnelBridgeDirection(neighbour_tile
) != *diagdir
) {
214 /* If any track reaches our exit direction, set the bit in the lower byte */
215 if (trackbits
& DiagdirReachesTracks(*diagdir
)) SetBit(res
, i
);
223 /* Station Resolver Functions */
224 /* virtual */ uint32
StationScopeResolver::GetRandomBits() const
226 return (this->st
== nullptr ? 0 : this->st
->random_bits
) | (this->tile
== INVALID_TILE
? 0 : GetStationTileRandomBits(this->tile
) << 16);
230 /* virtual */ uint32
StationScopeResolver::GetTriggers() const
232 return this->st
== nullptr ? 0 : this->st
->waiting_triggers
;
237 * Station variable cache
238 * This caches 'expensive' station variable lookups which iterate over
239 * several tiles that may be called multiple times per Resolve().
248 uint8 valid
; ///< Bits indicating what variable is valid (for each bit, \c 0 is invalid, \c 1 is valid).
252 * Get the town scope associated with a station, if it exists.
253 * On the first call, the town scope is created (if possible).
254 * @return Town scope, if available.
256 TownScopeResolver
*StationResolverObject::GetTown()
258 if (this->town_scope
== nullptr) {
260 if (this->station_scope
.st
!= nullptr) {
261 t
= this->station_scope
.st
->town
;
262 } else if (this->station_scope
.tile
!= INVALID_TILE
) {
263 t
= ClosestTownFromTile(this->station_scope
.tile
, UINT_MAX
);
265 if (t
== nullptr) return nullptr;
266 this->town_scope
= new TownScopeResolver(*this, t
, this->station_scope
.st
== nullptr);
268 return this->town_scope
;
271 /* virtual */ uint32
StationScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
273 if (this->st
== nullptr) {
274 /* Station does not exist, so we're in a purchase list or the land slope check callback. */
280 case 0x49: return 0x2110000; // Platforms, tracks & position
281 case 0x42: return 0; // Rail type (XXX Get current type from GUI?)
282 case 0x43: return GetCompanyInfo(_current_company
); // Station owner
283 case 0x44: return 2; // PBS status
284 case 0x67: // Land info of nearby tile
285 if (this->axis
!= INVALID_AXIS
&& this->tile
!= INVALID_TILE
) {
286 TileIndex tile
= this->tile
;
287 if (parameter
!= 0) tile
= GetNearbyTile(parameter
, tile
, true, this->axis
); // only perform if it is required
289 Slope tileh
= GetTileSlope(tile
);
290 bool swap
= (this->axis
== AXIS_Y
&& HasBit(tileh
, CORNER_W
) != HasBit(tileh
, CORNER_E
));
292 return GetNearbyTileInformation(tile
, this->ro
.grffile
->grf_version
>= 8) ^ (swap
? SLOPE_EW
: 0);
296 case 0xFA: return Clamp(_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535); // Build date, clamped to a 16 bit value
304 /* Calculated station variables */
306 if (!HasBit(_svc
.valid
, 0)) { _svc
.v40
= GetPlatformInfoHelper(this->tile
, false, false, false); SetBit(_svc
.valid
, 0); }
310 if (!HasBit(_svc
.valid
, 1)) { _svc
.v41
= GetPlatformInfoHelper(this->tile
, true, false, false); SetBit(_svc
.valid
, 1); }
313 case 0x42: return GetTerrainType(this->tile
) | (GetReverseRailTypeTranslation(GetRailType(this->tile
), this->statspec
->grf_prop
.grffile
) << 8);
314 case 0x43: return GetCompanyInfo(this->st
->owner
); // Station owner
315 case 0x44: return HasStationReservation(this->tile
) ? 7 : 4; // PBS status
317 if (!HasBit(_svc
.valid
, 2)) { _svc
.v45
= GetRailContinuationInfo(this->tile
); SetBit(_svc
.valid
, 2); }
321 if (!HasBit(_svc
.valid
, 3)) { _svc
.v46
= GetPlatformInfoHelper(this->tile
, false, false, true); SetBit(_svc
.valid
, 3); }
325 if (!HasBit(_svc
.valid
, 4)) { _svc
.v47
= GetPlatformInfoHelper(this->tile
, true, false, true); SetBit(_svc
.valid
, 4); }
329 if (!HasBit(_svc
.valid
, 5)) { _svc
.v49
= GetPlatformInfoHelper(this->tile
, false, true, false); SetBit(_svc
.valid
, 5); }
332 case 0x4A: // Animation frame of tile
333 return GetAnimationFrame(this->tile
);
335 /* Variables which use the parameter */
336 /* Variables 0x60 to 0x65 and 0x69 are handled separately below */
337 case 0x66: { // Animation frame of nearby tile
338 TileIndex tile
= this->tile
;
339 if (parameter
!= 0) tile
= GetNearbyTile(parameter
, tile
);
340 return this->st
->TileBelongsToRailStation(tile
) ? GetAnimationFrame(tile
) : UINT_MAX
;
343 case 0x67: { // Land info of nearby tile
344 Axis axis
= GetRailStationAxis(this->tile
);
345 TileIndex tile
= this->tile
;
346 if (parameter
!= 0) tile
= GetNearbyTile(parameter
, tile
); // only perform if it is required
348 Slope tileh
= GetTileSlope(tile
);
349 bool swap
= (axis
== AXIS_Y
&& HasBit(tileh
, CORNER_W
) != HasBit(tileh
, CORNER_E
));
351 return GetNearbyTileInformation(tile
, this->ro
.grffile
->grf_version
>= 8) ^ (swap
? SLOPE_EW
: 0);
354 case 0x68: { // Station info of nearby tiles
355 TileIndex nearby_tile
= GetNearbyTile(parameter
, this->tile
);
357 if (!HasStationTileRail(nearby_tile
)) return 0xFFFFFFFF;
359 uint32 grfid
= this->st
->speclist
[GetCustomStationSpecIndex(this->tile
)].grfid
;
360 bool perpendicular
= GetRailStationAxis(this->tile
) != GetRailStationAxis(nearby_tile
);
361 bool same_station
= this->st
->TileBelongsToRailStation(nearby_tile
);
362 uint32 res
= GB(GetStationGfx(nearby_tile
), 1, 2) << 12 | !!perpendicular
<< 11 | !!same_station
<< 10;
364 if (IsCustomStationSpecIndex(nearby_tile
)) {
365 const StationSpecList ssl
= BaseStation::GetByTile(nearby_tile
)->speclist
[GetCustomStationSpecIndex(nearby_tile
)];
366 res
|= 1 << (ssl
.grfid
!= grfid
? 9 : 8) | ssl
.localidx
;
371 /* General station variables */
372 case 0x82: return 50;
373 case 0x84: return this->st
->string_id
;
375 case 0xF0: return this->st
->facilities
;
376 case 0xFA: return Clamp(this->st
->build_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535);
379 return this->st
->GetNewGRFVariable(this->ro
, variable
, parameter
, available
);
382 uint32
Station::GetNewGRFVariable(const ResolverObject
&object
, byte variable
, byte parameter
, bool *available
) const
385 case 0x48: { // Accepted cargo types
389 for (cargo_type
= 0; cargo_type
< NUM_CARGO
; cargo_type
++) {
390 if (HasBit(this->goods
[cargo_type
].status
, GoodsEntry::GES_ACCEPTANCE
)) SetBit(value
, cargo_type
);
395 case 0x8A: return this->had_vehicle_of_type
;
396 case 0xF1: return (this->airport
.tile
!= INVALID_TILE
) ? this->airport
.GetSpec()->ttd_airport_type
: ATP_TTDP_LARGE
;
397 case 0xF2: return (this->truck_stops
!= nullptr) ? this->truck_stops
->status
: 0;
398 case 0xF3: return (this->bus_stops
!= nullptr) ? this->bus_stops
->status
: 0;
399 case 0xF6: return this->airport
.flags
;
400 case 0xF7: return GB(this->airport
.flags
, 8, 8);
403 /* Handle cargo variables with parameter, 0x60 to 0x65 and 0x69 */
404 if ((variable
>= 0x60 && variable
<= 0x65) || variable
== 0x69) {
405 CargoID c
= GetCargoTranslation(parameter
, object
.grffile
);
407 if (c
== CT_INVALID
) {
409 case 0x62: return 0xFFFFFFFF;
410 case 0x64: return 0xFF00;
414 const GoodsEntry
*ge
= &this->goods
[c
];
417 case 0x60: return min(ge
->cargo
.TotalCount(), 4095);
418 case 0x61: return ge
->HasVehicleEverTriedLoading() ? ge
->time_since_pickup
: 0;
419 case 0x62: return ge
->HasRating() ? ge
->rating
: 0xFFFFFFFF;
420 case 0x63: return ge
->cargo
.DaysInTransit();
421 case 0x64: return ge
->HasVehicleEverTriedLoading() ? ge
->last_speed
| (ge
->last_age
<< 8) : 0xFF00;
422 case 0x65: return GB(ge
->status
, GoodsEntry::GES_ACCEPTANCE
, 1) << 3;
424 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED
+ 1 == (int)GoodsEntry::GES_LAST_MONTH
);
425 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED
+ 2 == (int)GoodsEntry::GES_CURRENT_MONTH
);
426 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED
+ 3 == (int)GoodsEntry::GES_ACCEPTED_BIGTICK
);
427 return GB(ge
->status
, GoodsEntry::GES_EVER_ACCEPTED
, 4);
432 /* Handle cargo variables (deprecated) */
433 if (variable
>= 0x8C && variable
<= 0xEC) {
434 const GoodsEntry
*g
= &this->goods
[GB(variable
- 0x8C, 3, 4)];
435 switch (GB(variable
- 0x8C, 0, 3)) {
436 case 0: return g
->cargo
.TotalCount();
437 case 1: return GB(min(g
->cargo
.TotalCount(), 4095), 0, 4) | (GB(g
->status
, GoodsEntry::GES_ACCEPTANCE
, 1) << 7);
438 case 2: return g
->time_since_pickup
;
439 case 3: return g
->rating
;
440 case 4: return g
->cargo
.Source();
441 case 5: return g
->cargo
.DaysInTransit();
442 case 6: return g
->last_speed
;
443 case 7: return g
->last_age
;
447 DEBUG(grf
, 1, "Unhandled station variable 0x%X", variable
);
453 uint32
Waypoint::GetNewGRFVariable(const ResolverObject
&object
, byte variable
, byte parameter
, bool *available
) const
456 case 0x48: return 0; // Accepted cargo types
457 case 0x8A: return HVOT_WAYPOINT
;
458 case 0xF1: return 0; // airport type
459 case 0xF2: return 0; // truck stop status
460 case 0xF3: return 0; // bus stop status
461 case 0xF6: return 0; // airport flags
462 case 0xF7: return 0; // airport flags cont.
465 /* Handle cargo variables with parameter, 0x60 to 0x65 */
466 if (variable
>= 0x60 && variable
<= 0x65) {
470 /* Handle cargo variables (deprecated) */
471 if (variable
>= 0x8C && variable
<= 0xEC) {
472 switch (GB(variable
- 0x8C, 0, 3)) {
473 case 3: return INITIAL_STATION_RATING
;
474 case 4: return INVALID_STATION
;
479 DEBUG(grf
, 1, "Unhandled station variable 0x%X", variable
);
485 /* virtual */ const SpriteGroup
*StationResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
487 if (this->station_scope
.st
== nullptr || this->station_scope
.statspec
->cls_id
== STAT_CLASS_WAYP
) {
488 return group
->loading
[0];
492 const Station
*st
= Station::From(this->station_scope
.st
);
494 switch (this->station_scope
.cargo_type
) {
502 for (CargoID cargo_type
= 0; cargo_type
< NUM_CARGO
; cargo_type
++) {
503 cargo
+= st
->goods
[cargo_type
].cargo
.TotalCount();
508 cargo
= st
->goods
[this->station_scope
.cargo_type
].cargo
.TotalCount();
512 if (HasBit(this->station_scope
.statspec
->flags
, SSF_DIV_BY_STATION_SIZE
)) cargo
/= (st
->train_station
.w
+ st
->train_station
.h
);
513 cargo
= min(0xfff, cargo
);
515 if (cargo
> this->station_scope
.statspec
->cargo_threshold
) {
516 if (group
->num_loading
> 0) {
517 uint set
= ((cargo
- this->station_scope
.statspec
->cargo_threshold
) * group
->num_loading
) / (4096 - this->station_scope
.statspec
->cargo_threshold
);
518 return group
->loading
[set
];
521 if (group
->num_loaded
> 0) {
522 uint set
= (cargo
* group
->num_loaded
) / (this->station_scope
.statspec
->cargo_threshold
+ 1);
523 return group
->loaded
[set
];
527 return group
->loading
[0];
530 GrfSpecFeature
StationResolverObject::GetFeature() const
535 uint32
StationResolverObject::GetDebugID() const
537 return this->station_scope
.statspec
->grf_prop
.local_id
;
541 * Resolver for stations.
542 * @param statspec Station (type) specification.
543 * @param st Instance of the station.
544 * @param tile %Tile of the station.
545 * @param callback Callback ID.
546 * @param callback_param1 First parameter (var 10) of the callback.
547 * @param callback_param2 Second parameter (var 18) of the callback.
549 StationResolverObject::StationResolverObject(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
,
550 CallbackID callback
, uint32 callback_param1
, uint32 callback_param2
)
551 : ResolverObject(statspec
->grf_prop
.grffile
, callback
, callback_param1
, callback_param2
),
552 station_scope(*this, statspec
, st
, tile
), town_scope(nullptr)
554 /* Invalidate all cached vars */
557 CargoID ctype
= CT_DEFAULT_NA
;
559 if (this->station_scope
.st
== nullptr) {
560 /* No station, so we are in a purchase list */
562 } else if (Station::IsExpected(this->station_scope
.st
)) {
563 const Station
*st
= Station::From(this->station_scope
.st
);
564 /* Pick the first cargo that we have waiting */
566 FOR_ALL_CARGOSPECS(cs
) {
567 if (this->station_scope
.statspec
->grf_prop
.spritegroup
[cs
->Index()] != nullptr &&
568 st
->goods
[cs
->Index()].cargo
.TotalCount() > 0) {
575 if (this->station_scope
.statspec
->grf_prop
.spritegroup
[ctype
] == nullptr) {
579 /* Remember the cargo type we've picked */
580 this->station_scope
.cargo_type
= ctype
;
581 this->root_spritegroup
= this->station_scope
.statspec
->grf_prop
.spritegroup
[this->station_scope
.cargo_type
];
584 StationResolverObject::~StationResolverObject()
586 delete this->town_scope
;
590 * Resolve sprites for drawing a station tile.
591 * @param statspec Station spec
592 * @param st Station (nullptr in GUI)
593 * @param tile Station tile being drawn (INVALID_TILE in GUI)
594 * @param var10 Value to put in variable 10; normally 0; 1 when resolving the groundsprite and SSF_SEPARATE_GROUND is set.
595 * @return First sprite of the Action 1 spriteset to use, minus an offset of 0x42D to accommodate for weird NewGRF specs.
597 SpriteID
GetCustomStationRelocation(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
, uint32 var10
)
599 StationResolverObject
object(statspec
, st
, tile
, CBID_NO_CALLBACK
, var10
);
600 const SpriteGroup
*group
= object
.Resolve();
601 if (group
== nullptr || group
->type
!= SGT_RESULT
) return 0;
602 return group
->GetResult() - 0x42D;
606 * Resolve the sprites for custom station foundations.
607 * @param statspec Station spec
609 * @param tile Station tile being drawn
610 * @param layout Spritelayout as returned by previous callback
611 * @param edge_info Information about northern tile edges; whether they need foundations or merge into adjacent tile's foundations.
612 * @return First sprite of a set of foundation sprites for various slopes, or 0 if default foundations shall be drawn.
614 SpriteID
GetCustomStationFoundationRelocation(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
, uint layout
, uint edge_info
)
616 /* callback_param1 == 2 means we are resolving the foundation sprites. */
617 StationResolverObject
object(statspec
, st
, tile
, CBID_NO_CALLBACK
, 2, layout
| (edge_info
<< 16));
619 const SpriteGroup
*group
= object
.Resolve();
620 if (group
== nullptr || group
->type
!= SGT_RESULT
) return 0;
622 /* Note: SpriteGroup::Resolve zeroes all registers, so register 0x100 is initialised to 0. (compatibility) */
623 return group
->GetResult() + GetRegister(0x100);
627 uint16
GetStationCallback(CallbackID callback
, uint32 param1
, uint32 param2
, const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
)
629 StationResolverObject
object(statspec
, st
, tile
, callback
, param1
, param2
);
630 return object
.ResolveCallback();
634 * Check the slope of a tile of a new station.
635 * @param north_tile Norther tile of the station rect.
636 * @param cur_tile Tile to check.
637 * @param statspec Station spec.
638 * @param axis Axis of the new station.
639 * @param plat_len Platform length.
640 * @param numtracks Number of platforms.
641 * @return Succeeded or failed command.
643 CommandCost
PerformStationTileSlopeCheck(TileIndex north_tile
, TileIndex cur_tile
, const StationSpec
*statspec
, Axis axis
, byte plat_len
, byte numtracks
)
645 TileIndexDiff diff
= cur_tile
- north_tile
;
646 Slope slope
= GetTileSlope(cur_tile
);
648 StationResolverObject
object(statspec
, nullptr, cur_tile
, CBID_STATION_LAND_SLOPE_CHECK
,
649 (slope
<< 4) | (slope
^ (axis
== AXIS_Y
&& HasBit(slope
, CORNER_W
) != HasBit(slope
, CORNER_E
) ? SLOPE_EW
: 0)),
650 (numtracks
<< 24) | (plat_len
<< 16) | (axis
== AXIS_Y
? TileX(diff
) << 8 | TileY(diff
) : TileY(diff
) << 8 | TileX(diff
)));
651 object
.station_scope
.axis
= axis
;
653 uint16 cb_res
= object
.ResolveCallback();
655 /* Failed callback means success. */
656 if (cb_res
== CALLBACK_FAILED
) return CommandCost();
658 /* The meaning of bit 10 is inverted for a grf version < 8. */
659 if (statspec
->grf_prop
.grffile
->grf_version
< 8) ToggleBit(cb_res
, 10);
660 return GetErrorMessageFromLocationCallbackResult(cb_res
, statspec
->grf_prop
.grffile
, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
665 * Allocate a StationSpec to a Station. This is called once per build operation.
666 * @param statspec StationSpec to allocate.
667 * @param st Station to allocate it to.
668 * @param exec Whether to actually allocate the spec.
669 * @return Index within the Station's spec list, or -1 if the allocation failed.
671 int AllocateSpecToStation(const StationSpec
*statspec
, BaseStation
*st
, bool exec
)
675 if (statspec
== nullptr || st
== nullptr) return 0;
677 for (i
= 1; i
< st
->num_specs
&& i
< NUM_STATIONSSPECS_PER_STATION
; i
++) {
678 if (st
->speclist
[i
].spec
== nullptr && st
->speclist
[i
].grfid
== 0) break;
681 if (i
== NUM_STATIONSSPECS_PER_STATION
) {
682 /* As final effort when the spec list is already full...
683 * try to find the same spec and return that one. This might
684 * result in slightly "wrong" (as per specs) looking stations,
685 * but it's fairly unlikely that one reaches the limit anyways.
687 for (i
= 1; i
< st
->num_specs
&& i
< NUM_STATIONSSPECS_PER_STATION
; i
++) {
688 if (st
->speclist
[i
].spec
== statspec
) return i
;
695 if (i
>= st
->num_specs
) {
696 st
->num_specs
= i
+ 1;
697 st
->speclist
= ReallocT(st
->speclist
, st
->num_specs
);
699 if (st
->num_specs
== 2) {
700 /* Initial allocation */
701 st
->speclist
[0].spec
= nullptr;
702 st
->speclist
[0].grfid
= 0;
703 st
->speclist
[0].localidx
= 0;
707 st
->speclist
[i
].spec
= statspec
;
708 st
->speclist
[i
].grfid
= statspec
->grf_prop
.grffile
->grfid
;
709 st
->speclist
[i
].localidx
= statspec
->grf_prop
.local_id
;
711 StationUpdateCachedTriggers(st
);
719 * Deallocate a StationSpec from a Station. Called when removing a single station tile.
720 * @param st Station to work with.
721 * @param specindex Index of the custom station within the Station's spec list.
722 * @return Indicates whether the StationSpec was deallocated.
724 void DeallocateSpecFromStation(BaseStation
*st
, byte specindex
)
726 /* specindex of 0 (default) is never freeable */
727 if (specindex
== 0) return;
729 ETileArea area
= ETileArea(st
, INVALID_TILE
, TA_WHOLE
);
730 /* Check all tiles over the station to check if the specindex is still in use */
731 TILE_AREA_LOOP(tile
, area
) {
732 if (st
->TileBelongsToRailStation(tile
) && GetCustomStationSpecIndex(tile
) == specindex
) {
737 /* This specindex is no longer in use, so deallocate it */
738 st
->speclist
[specindex
].spec
= nullptr;
739 st
->speclist
[specindex
].grfid
= 0;
740 st
->speclist
[specindex
].localidx
= 0;
742 /* If this was the highest spec index, reallocate */
743 if (specindex
== st
->num_specs
- 1) {
744 for (; st
->speclist
[st
->num_specs
- 1].grfid
== 0 && st
->num_specs
> 1; st
->num_specs
--) {}
746 if (st
->num_specs
> 1) {
747 st
->speclist
= ReallocT(st
->speclist
, st
->num_specs
);
751 st
->speclist
= nullptr;
752 st
->cached_anim_triggers
= 0;
753 st
->cached_cargo_triggers
= 0;
758 StationUpdateCachedTriggers(st
);
762 * Draw representation of a station tile for GUI purposes.
763 * @param x Position x of image.
764 * @param y Position y of image.
766 * @param railtype Rail type.
767 * @param sclass, station Type of station.
768 * @param station station ID
769 * @return True if the tile was drawn (allows for fallback to default graphic)
771 bool DrawStationTile(int x
, int y
, RailType railtype
, Axis axis
, StationClassID sclass
, uint station
)
773 const DrawTileSprites
*sprites
= nullptr;
774 const RailtypeInfo
*rti
= GetRailTypeInfo(railtype
);
775 PaletteID palette
= COMPANY_SPRITE_COLOUR(_local_company
);
778 const StationSpec
*statspec
= StationClass::Get(sclass
)->GetSpec(station
);
779 if (statspec
== nullptr) return false;
781 if (HasBit(statspec
->callback_mask
, CBM_STATION_SPRITE_LAYOUT
)) {
782 uint16 callback
= GetStationCallback(CBID_STATION_SPRITE_LAYOUT
, 0x2110000, 0, statspec
, nullptr, INVALID_TILE
);
783 if (callback
!= CALLBACK_FAILED
) tile
= callback
;
786 uint32 total_offset
= rti
->GetRailtypeSpriteOffset();
787 uint32 relocation
= 0;
788 uint32 ground_relocation
= 0;
789 const NewGRFSpriteLayout
*layout
= nullptr;
790 DrawTileSprites tmp_rail_layout
;
792 if (statspec
->renderdata
== nullptr) {
793 sprites
= GetStationTileLayout(STATION_RAIL
, tile
+ axis
);
795 layout
= &statspec
->renderdata
[(tile
< statspec
->tiles
) ? tile
+ axis
: (uint
)axis
];
796 if (!layout
->NeedsPreprocessing()) {
802 if (layout
!= nullptr) {
803 /* Sprite layout which needs preprocessing */
804 bool separate_ground
= HasBit(statspec
->flags
, SSF_SEPARATE_GROUND
);
805 uint32 var10_values
= layout
->PrepareLayout(total_offset
, rti
->fallback_railtype
, 0, 0, separate_ground
);
807 FOR_EACH_SET_BIT(var10
, var10_values
) {
808 uint32 var10_relocation
= GetCustomStationRelocation(statspec
, nullptr, INVALID_TILE
, var10
);
809 layout
->ProcessRegisters(var10
, var10_relocation
, separate_ground
);
812 tmp_rail_layout
.seq
= layout
->GetLayout(&tmp_rail_layout
.ground
);
813 sprites
= &tmp_rail_layout
;
816 /* Simple sprite layout */
817 ground_relocation
= relocation
= GetCustomStationRelocation(statspec
, nullptr, INVALID_TILE
, 0);
818 if (HasBit(sprites
->ground
.sprite
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) {
819 ground_relocation
= GetCustomStationRelocation(statspec
, nullptr, INVALID_TILE
, 1);
821 ground_relocation
+= rti
->fallback_railtype
;
824 SpriteID image
= sprites
->ground
.sprite
;
825 PaletteID pal
= sprites
->ground
.pal
;
826 RailTrackOffset overlay_offset
;
827 if (rti
->UsesOverlay() && SplitGroundSpriteForOverlay(nullptr, &image
, &overlay_offset
)) {
828 SpriteID ground
= GetCustomRailSprite(rti
, INVALID_TILE
, RTSG_GROUND
);
829 DrawSprite(image
, PAL_NONE
, x
, y
);
830 DrawSprite(ground
+ overlay_offset
, PAL_NONE
, x
, y
);
832 image
+= HasBit(image
, SPRITE_MODIFIER_CUSTOM_SPRITE
) ? ground_relocation
: total_offset
;
833 if (HasBit(pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) pal
+= ground_relocation
;
834 DrawSprite(image
, GroundSpritePaletteTransform(image
, pal
, palette
), x
, y
);
837 DrawRailTileSeqInGUI(x
, y
, sprites
, total_offset
, relocation
, palette
);
843 const StationSpec
*GetStationSpec(TileIndex t
)
845 if (!IsCustomStationSpecIndex(t
)) return nullptr;
847 const BaseStation
*st
= BaseStation::GetByTile(t
);
848 uint specindex
= GetCustomStationSpecIndex(t
);
849 return specindex
< st
->num_specs
? st
->speclist
[specindex
].spec
: nullptr;
854 * Check whether a rail station tile is NOT traversable.
855 * @param tile %Tile to test.
856 * @return Station tile is blocked.
857 * @note This could be cached (during build) in the map array to save on all the dereferencing.
859 bool IsStationTileBlocked(TileIndex tile
)
861 const StationSpec
*statspec
= GetStationSpec(tile
);
863 return statspec
!= nullptr && HasBit(statspec
->blocked
, GetStationGfx(tile
));
867 * Check if a rail station tile shall have pylons when electrified.
868 * @param tile %Tile to test.
869 * @return Tile shall have pylons.
870 * @note This could be cached (during build) in the map array to save on all the dereferencing.
872 bool CanStationTileHavePylons(TileIndex tile
)
874 const StationSpec
*statspec
= GetStationSpec(tile
);
875 uint gfx
= GetStationGfx(tile
);
876 /* Default stations do not draw pylons under roofs (gfx >= 4) */
877 return statspec
!= nullptr ? HasBit(statspec
->pylons
, gfx
) : gfx
< 4;
881 * Check if a rail station tile shall have wires when electrified.
882 * @param tile %Tile to test.
883 * @return Tile shall have wires.
884 * @note This could be cached (during build) in the map array to save on all the dereferencing.
886 bool CanStationTileHaveWires(TileIndex tile
)
888 const StationSpec
*statspec
= GetStationSpec(tile
);
889 return statspec
== nullptr || !HasBit(statspec
->wires
, GetStationGfx(tile
));
892 /** Wrapper for animation control, see GetStationCallback. */
893 uint16
GetAnimStationCallback(CallbackID callback
, uint32 param1
, uint32 param2
, const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
, int extra_data
)
895 return GetStationCallback(callback
, param1
, param2
, statspec
, st
, tile
);
898 /** Helper class for animation control. */
899 struct StationAnimationBase
: public AnimationBase
<StationAnimationBase
, StationSpec
, BaseStation
, int, GetAnimStationCallback
> {
900 static const CallbackID cb_animation_speed
= CBID_STATION_ANIMATION_SPEED
;
901 static const CallbackID cb_animation_next_frame
= CBID_STATION_ANIM_NEXT_FRAME
;
903 static const StationCallbackMask cbm_animation_speed
= CBM_STATION_ANIMATION_SPEED
;
904 static const StationCallbackMask cbm_animation_next_frame
= CBM_STATION_ANIMATION_NEXT_FRAME
;
907 void AnimateStationTile(TileIndex tile
)
909 const StationSpec
*ss
= GetStationSpec(tile
);
910 if (ss
== nullptr) return;
912 StationAnimationBase::AnimateTile(ss
, BaseStation::GetByTile(tile
), tile
, HasBit(ss
->flags
, SSF_CB141_RANDOM_BITS
));
915 void TriggerStationAnimation(BaseStation
*st
, TileIndex tile
, StationAnimationTrigger trigger
, CargoID cargo_type
)
917 /* List of coverage areas for each animation trigger */
918 static const TriggerArea tas
[] = {
919 TA_TILE
, TA_WHOLE
, TA_WHOLE
, TA_PLATFORM
, TA_PLATFORM
, TA_PLATFORM
, TA_WHOLE
922 /* Get Station if it wasn't supplied */
923 if (st
== nullptr) st
= BaseStation::GetByTile(tile
);
925 /* Check the cached animation trigger bitmask to see if we need
926 * to bother with any further processing. */
927 if (!HasBit(st
->cached_anim_triggers
, trigger
)) return;
929 uint16 random_bits
= Random();
930 ETileArea area
= ETileArea(st
, tile
, tas
[trigger
]);
932 /* Check all tiles over the station to check if the specindex is still in use */
933 TILE_AREA_LOOP(tile
, area
) {
934 if (st
->TileBelongsToRailStation(tile
)) {
935 const StationSpec
*ss
= GetStationSpec(tile
);
936 if (ss
!= nullptr && HasBit(ss
->animation
.triggers
, trigger
)) {
938 if (cargo_type
== CT_INVALID
) {
941 cargo
= ss
->grf_prop
.grffile
->cargo_map
[cargo_type
];
943 StationAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP
, ss
, st
, tile
, (random_bits
<< 16) | Random(), (uint8
)trigger
| (cargo
<< 8));
950 * Trigger station randomisation
951 * @param st station being triggered
952 * @param tile specific tile of platform to trigger
953 * @param trigger trigger type
954 * @param cargo_type cargo type causing trigger
956 void TriggerStationRandomisation(Station
*st
, TileIndex tile
, StationRandomTrigger trigger
, CargoID cargo_type
)
958 /* List of coverage areas for each animation trigger */
959 static const TriggerArea tas
[] = {
960 TA_WHOLE
, TA_WHOLE
, TA_PLATFORM
, TA_PLATFORM
, TA_PLATFORM
, TA_PLATFORM
963 /* Get Station if it wasn't supplied */
964 if (st
== nullptr) st
= Station::GetByTile(tile
);
966 /* Check the cached cargo trigger bitmask to see if we need
967 * to bother with any further processing. */
968 if (st
->cached_cargo_triggers
== 0) return;
969 if (cargo_type
!= CT_INVALID
&& !HasBit(st
->cached_cargo_triggers
, cargo_type
)) return;
971 uint32 whole_reseed
= 0;
972 ETileArea area
= ETileArea(st
, tile
, tas
[trigger
]);
974 CargoTypes empty_mask
= 0;
975 if (trigger
== SRT_CARGO_TAKEN
) {
976 /* Create a bitmask of completely empty cargo types to be matched */
977 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
978 if (st
->goods
[i
].cargo
.TotalCount() == 0) {
979 SetBit(empty_mask
, i
);
984 /* Store triggers now for var 5F */
985 SetBit(st
->waiting_triggers
, trigger
);
986 uint32 used_triggers
= 0;
988 /* Check all tiles over the station to check if the specindex is still in use */
989 TILE_AREA_LOOP(tile
, area
) {
990 if (st
->TileBelongsToRailStation(tile
)) {
991 const StationSpec
*ss
= GetStationSpec(tile
);
992 if (ss
== nullptr) continue;
994 /* Cargo taken "will only be triggered if all of those
995 * cargo types have no more cargo waiting." */
996 if (trigger
== SRT_CARGO_TAKEN
) {
997 if ((ss
->cargo_triggers
& ~empty_mask
) != 0) continue;
1000 if (cargo_type
== CT_INVALID
|| HasBit(ss
->cargo_triggers
, cargo_type
)) {
1001 StationResolverObject
object(ss
, st
, tile
, CBID_RANDOM_TRIGGER
, 0);
1002 object
.waiting_triggers
= st
->waiting_triggers
;
1004 const SpriteGroup
*group
= object
.Resolve();
1005 if (group
== nullptr) continue;
1007 used_triggers
|= object
.used_triggers
;
1009 uint32 reseed
= object
.GetReseedSum();
1011 whole_reseed
|= reseed
;
1014 /* Set individual tile random bits */
1015 uint8 random_bits
= GetStationTileRandomBits(tile
);
1016 random_bits
&= ~reseed
;
1017 random_bits
|= Random() & reseed
;
1018 SetStationTileRandomBits(tile
, random_bits
);
1020 MarkTileDirtyByTile(tile
);
1026 /* Update whole station random bits */
1027 st
->waiting_triggers
&= ~used_triggers
;
1028 if ((whole_reseed
& 0xFFFF) != 0) {
1029 st
->random_bits
&= ~whole_reseed
;
1030 st
->random_bits
|= Random() & whole_reseed
;
1035 * Update the cached animation trigger bitmask for a station.
1036 * @param st Station to update.
1038 void StationUpdateCachedTriggers(BaseStation
*st
)
1040 st
->cached_anim_triggers
= 0;
1041 st
->cached_cargo_triggers
= 0;
1043 /* Combine animation trigger bitmask for all station specs
1044 * of this station. */
1045 for (uint i
= 0; i
< st
->num_specs
; i
++) {
1046 const StationSpec
*ss
= st
->speclist
[i
].spec
;
1047 if (ss
!= nullptr) {
1048 st
->cached_anim_triggers
|= ss
->animation
.triggers
;
1049 st
->cached_cargo_triggers
|= ss
->cargo_triggers
;