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/>.
12 * This file deals with displaying wires and pylons for electric railways.
17 * We have two different types of tiles in the drawing code:
18 * Normal Railway Tiles (NRTs) which can have more than one track on it, and
19 * Special Railways tiles (SRTs) which have only one track (like crossings, depots
22 * <h3>Location Categories</h3>
24 * All tiles are categorized into three location groups (TLG):
25 * Group 0: Tiles with both an even X coordinate and an even Y coordinate
26 * Group 1: Tiles with an even X and an odd Y coordinate
27 * Group 2: Tiles with an odd X and an even Y coordinate
28 * Group 3: Tiles with both an odd X and Y coordinate.
30 * <h3>Pylon Points</h3>
31 * <h4>Control Points</h4>
32 * A Pylon Control Point (PCP) is a position where a wire (or rather two)
33 * is mounted onto a pylon.
34 * Each NRT does contain 4 PCPs which are bitmapped to a byte
35 * variable and are represented by the DiagDirection enum
37 * Each track ends on two PCPs and thus requires one pylon on each end. However,
38 * there is one exception: Straight-and-level tracks only have one pylon every
41 * Now on each edge there are two PCPs: One from each adjacent tile. Both PCPs
42 * are merged using an OR operation (i. e. if one tile needs a PCP at the position
43 * in question, both tiles get it).
45 * <h4>Position Points</h4>
46 * A Pylon Position Point (PPP) is a position where a pylon is located on the
47 * ground. Each PCP owns 8 in (45 degree steps) PPPs that are located around
48 * it. PPPs are represented using the Direction enum. Each track bit has PPPs
49 * that are impossible (because the pylon would be situated on the track) and
50 * some that are preferred (because the pylon would be rectangular to the track).
52 * @image html elrail_tile.png
53 * @image html elrail_track.png
63 #include "map/slope.h"
64 #include "map/bridge.h"
65 #include "map/tunnelbridge.h"
66 #include "viewport_func.h"
70 #include "elrail_func.h"
71 #include "company_base.h"
72 #include "newgrf_railtype.h"
73 #include "station_func.h"
75 #include "table/elrail_data.h"
78 * Check if a tile is on an odd X coordinate.
79 * @param t The tile to check
80 * @return Whether the tile is on an odd X coordinate
82 static inline bool IsOddX (TileIndex t
)
84 return HasBit (TileX(t
), 0);
88 * Check if a tile is on an odd Y coordinate.
89 * @param t The tile to check
90 * @return Whether the tile is on an odd Y coordinate
92 static inline bool IsOddY (TileIndex t
)
94 return HasBit (TileY(t
), 0);
97 /** Get the electrified track bits on a railway tile. */
98 static TrackBits
GetElectrifiedTrackBits (TileIndex t
)
100 TrackBits present
= GetTrackBits(t
);
101 TrackBits result
= TRACK_BIT_NONE
;
102 if (HasCatenary (GetRailType (t
, TRACK_UPPER
))) result
|= present
& (TRACK_BIT_CROSS
| TRACK_BIT_UPPER
| TRACK_BIT_LEFT
);
103 if (HasCatenary (GetRailType (t
, TRACK_LOWER
))) result
|= present
& (TRACK_BIT_LOWER
| TRACK_BIT_RIGHT
);
108 * Finds which Electrified Rail Bits are present on a given tile.
109 * @param t tile to check
110 * @param dir direction this tile is from the home tile, or INVALID_TILE for the home tile itself
111 * @param override pointer to PCP override, can be NULL
112 * @return trackbits of tile if it is electrified
114 static TrackBits
GetRailTrackBitsUniversal(TileIndex t
, DiagDirection dir
, DiagDirection
*override
= NULL
)
116 switch (GetTileType(t
)) {
118 if (IsTileSubtype(t
, TT_BRIDGE
) && (override
!= NULL
)) {
119 *override
= GetTunnelBridgeDirection(t
);
121 return GetElectrifiedTrackBits (t
);
124 switch (GetTileSubtype(t
)) {
125 default: return TRACK_BIT_NONE
;
127 case TT_MISC_CROSSING
:
128 if (!HasCatenary(GetRailType(t
))) return TRACK_BIT_NONE
;
129 return GetCrossingRailBits(t
);
132 if (GetTunnelTransportType(t
) != TRANSPORT_RAIL
) return TRACK_BIT_NONE
;
133 if (!HasCatenary(GetRailType(t
))) return TRACK_BIT_NONE
;
134 /* ignore tunnels facing the wrong way for neighbouring tiles */
135 if (dir
!= INVALID_DIAGDIR
&& dir
!= GetTunnelBridgeDirection(t
)) return TRACK_BIT_NONE
;
136 if (override
!= NULL
) {
137 *override
= GetTunnelBridgeDirection(t
);
139 return DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t
));
143 if (!HasStationRail(t
)) return TRACK_BIT_NONE
;
144 if (!HasCatenary(GetRailType(t
))) return TRACK_BIT_NONE
;
145 /* Ignore neighbouring station tiles that allow neither wires nor pylons. */
146 if (dir
!= INVALID_DIAGDIR
&& !CanStationTileHavePylons(t
) && !CanStationTileHaveWires(t
)) return TRACK_BIT_NONE
;
147 return TrackToTrackBits(GetRailStationTrack(t
));
150 return TRACK_BIT_NONE
;
155 * Masks out track bits when neighbouring tiles are unelectrified.
157 static TrackBits
MaskWireBits(TileIndex t
, TrackBits tracks
)
159 if (!IsNormalRailTile(t
)) return tracks
;
161 TrackdirBits neighbour_tdb
= TRACKDIR_BIT_NONE
;
162 for (DiagDirection d
= DIAGDIR_BEGIN
; d
< DIAGDIR_END
; d
++) {
163 /* If the neighbour tile is either not electrified or has no tracks that can be reached
164 * from this tile, mark all trackdirs that can be reached from the neighbour tile
165 * as needing no catenary. We make an exception for blocked station tiles with a matching
166 * axis that still display wires to preserve visual continuity. */
167 TileIndex next_tile
= TileAddByDiagDir(t
, d
);
168 TrackBits reachable
= TrackStatusToTrackBits(GetTileRailwayStatus(next_tile
)) & DiagdirReachesTracks(d
);
170 if ((reachable
!= TRACK_BIT_NONE
) ?
171 ((rt
= GetRailType(next_tile
, FindFirstTrack(reachable
))) == INVALID_RAILTYPE
|| !HasCatenary(rt
)) :
172 (!HasStationTileRail(next_tile
) || GetRailStationAxis(next_tile
) != DiagDirToAxis(d
) || !CanStationTileHaveWires(next_tile
))) {
173 neighbour_tdb
|= DiagdirReachesTrackdirs(ReverseDiagDir(d
));
177 /* If the tracks from either a diagonal crossing or don't overlap, both
178 * trackdirs have to be marked to mask the corresponding track bit. Else
179 * one marked trackdir is enough the mask the track bit. */
181 if (tracks
== TRACK_BIT_CROSS
|| !TracksOverlap(tracks
)) {
182 /* If the tracks form either a diagonal crossing or don't overlap, both
183 * trackdirs have to be marked to mask the corresponding track bit. */
184 mask
= ~(TrackBits
)((neighbour_tdb
& (neighbour_tdb
>> 8)) & TRACK_BIT_MASK
);
185 /* If that results in no masked tracks and it is not a diagonal crossing,
186 * require only one marked trackdir to mask. */
187 if (tracks
!= TRACK_BIT_CROSS
&& (mask
& TRACK_BIT_MASK
) == TRACK_BIT_MASK
) mask
= ~TrackdirBitsToTrackBits(neighbour_tdb
);
189 /* Require only one marked trackdir to mask the track. */
190 mask
= ~TrackdirBitsToTrackBits(neighbour_tdb
);
191 /* If that results in an empty set, require both trackdirs for diagonal track. */
192 if ((tracks
& mask
) == TRACK_BIT_NONE
) {
193 if ((neighbour_tdb
& TRACKDIR_BIT_X_NE
) == 0 || (neighbour_tdb
& TRACKDIR_BIT_X_SW
) == 0) mask
|= TRACK_BIT_X
;
194 if ((neighbour_tdb
& TRACKDIR_BIT_Y_NW
) == 0 || (neighbour_tdb
& TRACKDIR_BIT_Y_SE
) == 0) mask
|= TRACK_BIT_Y
;
195 /* If that still is not enough, require both trackdirs for any track. */
196 if ((tracks
& mask
) == TRACK_BIT_NONE
) mask
= ~(TrackBits
)((neighbour_tdb
& (neighbour_tdb
>> 8)) & TRACK_BIT_MASK
);
200 /* Mask the tracks only if at least one track bit would remain. */
201 return (tracks
& mask
) != TRACK_BIT_NONE
? tracks
& mask
: tracks
;
205 * Get the base wire sprite to use.
207 static inline SpriteID
GetWireBase(TileIndex tile
, TileContext context
= TCX_NORMAL
, Track track
= INVALID_TRACK
)
209 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
, track
));
210 SpriteID wires
= GetCustomRailSprite(rti
, tile
, RTSG_WIRES
, context
);
211 return wires
== 0 ? SPR_WIRE_BASE
: wires
;
215 * Get the base pylon sprite to use.
217 static inline SpriteID
GetPylonBase(TileIndex tile
, TileContext context
= TCX_NORMAL
, Track track
= INVALID_TRACK
)
219 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
, track
));
220 SpriteID pylons
= GetCustomRailSprite(rti
, tile
, RTSG_PYLONS
, context
);
221 return pylons
== 0 ? SPR_PYLON_BASE
: pylons
;
225 * Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
226 * @param tile The tile to analyse
227 * @param *tileh the tileh
229 static void AdjustTileh(TileIndex tile
, Slope
*tileh
)
231 if (IsRailBridgeTile (tile
) && !IsExtendedRailBridge (tile
)) {
232 if (*tileh
!= SLOPE_FLAT
) {
235 *tileh
= InclinedSlope(GetTunnelBridgeDirection(tile
));
241 * Returns the Z position of a Pylon Control Point.
243 * @param tile The tile the pylon should stand on.
244 * @param PCPpos The PCP of the tile.
245 * @return The Z position of the PCP.
247 static int GetPCPElevation(TileIndex tile
, DiagDirection PCPpos
)
249 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
250 * PCPs are always on a tile edge.
252 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
253 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
254 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
256 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
257 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
258 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
260 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
263 int z
= GetSlopePixelZ(TileX(tile
) * TILE_SIZE
+ min(x_pcp_offsets
[PCPpos
], TILE_SIZE
- 1), TileY(tile
) * TILE_SIZE
+ min(y_pcp_offsets
[PCPpos
], TILE_SIZE
- 1));
264 return (z
+ 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
268 * Draws wires on a rail tunnel or depot tile.
269 * @param ti The TileInfo to draw the tile for.
270 * @param depot The tile is a depot, else a tunnel.
271 * @param dir The direction of the tunnel or depot.
273 void DrawRailTunnelDepotCatenary (const TileInfo
*ti
, bool depot
,
276 struct SortableSpriteStruct
{
277 struct { int8 x
, y
, w
, h
; } bb
[2];
282 static const SortableSpriteStruct data
[2] = {
283 { { { 0, -6, 16, 8 }, { 0, 0, 15, 1 } }, 0, 7 }, //! Wire along X axis
284 { { { -6, 0, 8, 16 }, { 0, 0, 1, 15 } }, 7, 0 }, //! Wire along Y axis
287 assert_compile (WSO_ENTRANCE_NE
== WSO_ENTRANCE_NE
+ DIAGDIR_NE
);
288 assert_compile (WSO_ENTRANCE_SE
== WSO_ENTRANCE_NE
+ DIAGDIR_SE
);
289 assert_compile (WSO_ENTRANCE_SW
== WSO_ENTRANCE_NE
+ DIAGDIR_SW
);
290 assert_compile (WSO_ENTRANCE_NW
== WSO_ENTRANCE_NE
+ DIAGDIR_NW
);
292 const SortableSpriteStruct
*sss
= &data
[DiagDirToAxis(dir
)];
293 int dz
= depot
? 0 : BB_Z_SEPARATOR
- ELRAIL_ELEVATION
;
294 int z
= depot
? GetTileMaxPixelZ (ti
->tile
) : GetTilePixelZ (ti
->tile
);
295 /* This wire is not visible with the default depot sprites. */
296 AddSortableSpriteToDraw (ti
->vd
,
297 GetWireBase (ti
->tile
) + WSO_ENTRANCE_NE
+ dir
, PAL_NONE
,
298 ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
,
299 sss
->bb
[depot
].w
, sss
->bb
[depot
].h
, dz
+ 1,
300 z
+ ELRAIL_ELEVATION
, IsTransparencySet (TO_CATENARY
),
301 sss
->bb
[depot
].x
, sss
->bb
[depot
].y
, dz
);
305 struct SideTrackData
{
306 byte track
; ///< a track that incides at this side
307 byte preferred
; ///< preferred pylon position points for it
310 static const uint NUM_TRACKS_PER_SIDE
= 3;
312 /* Side track data, 3 tracks per side. */
313 static const SideTrackData side_tracks
[DIAGDIR_END
][NUM_TRACKS_PER_SIDE
] = {
315 { TRACK_X
, 1 << DIR_NE
| 1 << DIR_SE
| 1 << DIR_NW
},
316 { TRACK_UPPER
, 1 << DIR_E
| 1 << DIR_N
| 1 << DIR_S
},
317 { TRACK_RIGHT
, 1 << DIR_N
| 1 << DIR_E
| 1 << DIR_W
},
319 { TRACK_Y
, 1 << DIR_NE
| 1 << DIR_SE
| 1 << DIR_SW
},
320 { TRACK_LOWER
, 1 << DIR_E
| 1 << DIR_N
| 1 << DIR_S
},
321 { TRACK_RIGHT
, 1 << DIR_S
| 1 << DIR_E
| 1 << DIR_W
},
323 { TRACK_X
, 1 << DIR_SE
| 1 << DIR_SW
| 1 << DIR_NW
},
324 { TRACK_LOWER
, 1 << DIR_W
| 1 << DIR_N
| 1 << DIR_S
},
325 { TRACK_LEFT
, 1 << DIR_S
| 1 << DIR_E
| 1 << DIR_W
},
327 { TRACK_Y
, 1 << DIR_SW
| 1 << DIR_NW
| 1 << DIR_NE
},
328 { TRACK_UPPER
, 1 << DIR_W
| 1 << DIR_N
| 1 << DIR_S
},
329 { TRACK_LEFT
, 1 << DIR_N
| 1 << DIR_E
| 1 << DIR_W
},
333 /* Mask of positions at which pylons can be built per track. */
334 static const byte allowed_ppp
[TRACK_END
] = {
335 1 << DIR_N
| 1 << DIR_E
| 1 << DIR_SE
| 1 << DIR_S
| 1 << DIR_W
| 1 << DIR_NW
, // X
336 1 << DIR_N
| 1 << DIR_NE
| 1 << DIR_E
| 1 << DIR_S
| 1 << DIR_SW
| 1 << DIR_W
, // Y
337 1 << DIR_N
| 1 << DIR_NE
| 1 << DIR_SE
| 1 << DIR_S
| 1 << DIR_SW
| 1 << DIR_NW
, // UPPER
338 1 << DIR_N
| 1 << DIR_NE
| 1 << DIR_SE
| 1 << DIR_S
| 1 << DIR_SW
| 1 << DIR_NW
, // LOWER
339 1 << DIR_NE
| 1 << DIR_E
| 1 << DIR_SE
| 1 << DIR_SW
| 1 << DIR_W
| 1 << DIR_NW
, // LEFT
340 1 << DIR_NE
| 1 << DIR_E
| 1 << DIR_SE
| 1 << DIR_SW
| 1 << DIR_W
| 1 << DIR_NW
, // RIGHT
344 * Mask preferred and allowed pylon position points on a tile side.
345 * @param tracks Tracks present on the tile.
346 * @param wires Electrified tracks present on the tile.
347 * @param side Tile side to check.
348 * @param preferred Pointer to preferred positions to mask.
349 * @param allowed Pointer to allowed positions to mask.
350 * @return Whether the pylon control point is in use from this tile.
352 static bool CheckCatenarySide (TrackBits tracks
, TrackBits wires
,
353 DiagDirection side
, byte
*preferred
, byte
*allowed
)
355 bool pcp_in_use
= false;
359 for (uint k
= 0; k
< NUM_TRACKS_PER_SIDE
; k
++) {
360 /* We check whether the track in question is present. */
361 const SideTrackData
*data
= &side_tracks
[side
][k
];
362 byte track
= data
->track
;
363 if (HasBit(wires
, track
)) {
366 pmask
&= data
->preferred
;
368 if (HasBit(tracks
, track
)) {
369 amask
&= allowed_ppp
[track
];
379 * Mask preferred and allowed pylon position points on a tile side,
380 * when there is a single track along an axis on the tile.
381 * @param axis Axis of the track.
382 * @param side Tile side to check.
383 * @param preferred Pointer to preferred positions to mask.
384 * @return Whether the pylon control point is in use from this tile.
386 static inline bool CheckCatenarySideAxis (Axis axis
, DiagDirection side
,
389 /* We check whether the track in question is present. */
390 if (DiagDirToAxis (side
) != axis
) return false;
393 *preferred
&= side_tracks
[side
][0].preferred
;
398 * Check if the pylon on a tile side should be elided on long track runs.
399 * @param side Tile side to check.
400 * @param preferred Preferred pylon positions.
401 * @param odd Array of tile coordinate parity per axis.
402 * @param level Whether the land is level (for tracks running along an axis).
403 * @return Whether the pylon should be elided.
405 static bool CheckPylonElision (DiagDirection side
, byte preferred
,
406 const bool *odd
, bool level
)
408 Axis axis
= DiagDirToAxis (side
);
411 case 1 << DIR_NW
| 1 << DIR_SE
:
412 if (!level
) return false;
413 ignore
= false; // must be X axis
416 case 1 << DIR_NE
| 1 << DIR_SW
:
417 if (!level
) return false;
418 ignore
= true; // must be Y axis
421 case 1 << DIR_E
| 1 << DIR_W
:
422 /* Non-orthogonal tracks must always be level. */
423 ignore
= (axis
== AXIS_X
) ? !odd
[AXIS_Y
] : odd
[AXIS_X
];
426 case 1 << DIR_N
| 1 << DIR_S
:
427 /* Non-orthogonal tracks must always be level. */
428 ignore
= !odd
[OtherAxis(axis
)];
435 /* This configuration may be subject to pylon elision. */
436 /* Toggle ignore if we are in an odd row, or heading the other way. */
437 return (ignore
^ odd
[axis
] ^ HasBit(side
, 1));
440 /** Possible return values for CheckNeighbourPCP below. */
442 PCP_NB_NONE
, ///< PCP not in use from the neighbour tile
443 PCP_NB_TUNNEL
, ///< PCP in use by a tunnel from the neighbour tile
444 PCP_NB_TRY_ELIDE
, ///< PCP is in use and may be subject to elision
448 * Check whether a pylon is also in use from a railway tile at the other side.
449 * @param tile The neighbour railway tile to check.
450 * @param side The tile side to check from this tile.
451 * @param preferred Pointer to preferred positions to mask.
452 * @param allowed Pointer to allowed positions to mask.
453 * @param slope Pointer to store the tile slope if pylon elision is possible.
454 * @return A value representing the PCP state at the given side.
456 static uint
CheckRailNeighbourPCP (TileIndex tile
, DiagDirection side
,
457 byte
*preferred
, byte
*allowed
, Slope
*slope
)
459 assert (IsRailwayTile (tile
));
461 bool is_bridge
= IsTileSubtype (tile
, TT_BRIDGE
);
462 if (is_bridge
&& GetTunnelBridgeDirection (tile
) == side
) {
466 TrackBits nb_tracks
= GetElectrifiedTrackBits (tile
);
467 if (nb_tracks
== TRACK_BIT_NONE
) return PCP_NB_NONE
;
468 TrackBits nb_wires
= MaskWireBits (tile
, nb_tracks
);
470 /* Tracks inciding from the neighbour tile */
471 if (!CheckCatenarySide (nb_tracks
, nb_wires
, side
, preferred
, allowed
)) {
475 /* Read the foundations if they are present, and adjust the tileh */
476 assert_compile (TRACK_BIT_X
== 1);
477 assert_compile (TRACK_BIT_Y
== 2);
481 /* Anything having more than a single X or Y track must be
482 * flat (or a half tile slope, but we treat those as flat). */
483 nb_slope
= SLOPE_FLAT
;
484 } else if (!is_bridge
) {
485 nb_slope
= GetTileSlope (tile
);
486 Foundation f
= GetRailFoundation (nb_slope
, nb_tracks
);
487 ApplyFoundationToSlope (f
, &nb_slope
);
489 nb_slope
= GetTileSlope (tile
);
490 /* With a single X or Y track, bridge must
491 * head away from our side. */
492 nb_slope
= HasBridgeFlatRamp (nb_slope
, DiagDirToAxis (side
)) ?
494 InclinedSlope (ReverseDiagDir (side
));
498 return PCP_NB_TRY_ELIDE
;
502 * Check whether a pylon is also in use from the other side.
503 * @param tile The neighbour tile to check.
504 * @param side The tile side to check from this tile.
505 * @param preferred Pointer to preferred positions to mask.
506 * @param allowed Pointer to allowed positions to mask.
507 * @param slope Pointer to store the tile slope if pylon elision is possible.
508 * @return A value representing the PCP state at the given side.
510 static uint
CheckNeighbourPCP (TileIndex tile
, DiagDirection side
,
511 byte
*preferred
, byte
*allowed
, Slope
*slope
)
514 switch (GetTileType (tile
)) {
516 return CheckRailNeighbourPCP (tile
, side
,
517 preferred
, allowed
, slope
);
520 switch (GetTileSubtype (tile
)) {
521 default: return PCP_NB_NONE
;
523 case TT_MISC_CROSSING
:
524 if (!HasCatenary (GetRailType (tile
))) return PCP_NB_NONE
;
525 axis
= GetCrossingRailAxis (tile
);
529 if (GetTunnelTransportType (tile
) != TRANSPORT_RAIL
) return PCP_NB_NONE
;
530 if (!HasCatenary (GetRailType (tile
))) return PCP_NB_NONE
;
531 /* ignore tunnels facing the wrong way for neighbouring tiles */
532 if (GetTunnelBridgeDirection (tile
) != ReverseDiagDir (side
)) return PCP_NB_NONE
;
533 /* force tunnels to always have a pylon (no elision) */
535 return PCP_NB_TUNNEL
;
540 if (!HasStationRail (tile
)) return PCP_NB_NONE
;
541 if (!HasCatenary (GetRailType (tile
))) return PCP_NB_NONE
;
542 /* Ignore neighbouring station tiles that allow neither wires nor pylons. */
543 if (!CanStationTileHavePylons (tile
) && !CanStationTileHaveWires (tile
)) return PCP_NB_NONE
;
544 axis
= GetRailStationAxis (tile
);
551 /* Crossing or station tile, so just one flat track along an axis. */
552 if (!CheckCatenarySideAxis (axis
, side
, preferred
)) {
557 return PCP_NB_TRY_ELIDE
;
560 /** Possible return values for CheckSidePCP below. */
562 PCP_NONE
, ///< PCP is not in use
563 PCP_IN_USE
, ///< PCP is in use from this tile
564 PCP_IN_USE_BOTH
, ///< PCP is in use also from the neighbour tile
568 * Check whether there should be a pylon at a tile side.
569 * @param tile The tile to check.
570 * @param home_tracks Tracks present on the home tile.
571 * @param home_wires Electrified tracks present on the home tile.
572 * @param home_slope Slope of the home tile, adjusted for foundations.
573 * @param side The side to check.
574 * @param odd Array of tile coordinate parity per axis.
575 * @return A value representing the PCP state at the given side, plus
576 * a bitmask of allowed directions for the pylon, if any.
578 static std::pair
<uint
, byte
> CheckSidePCP (TileIndex tile
,
579 TrackBits home_tracks
, TrackBits home_wires
, Slope home_slope
,
580 DiagDirection side
, const bool *odd
)
582 /* We cycle through all the existing tracks at a PCP and see what
583 * PPPs we want to have, or may not have at all */
584 byte PPPpreferred
= 0xFF; // We start with preferring everything (end-of-line in any direction)
585 byte PPPallowed
= AllowedPPPonPCP
[side
];
587 /* Tracks inciding from the home tile */
588 if (!CheckCatenarySide (home_tracks
, home_wires
, side
, &PPPpreferred
, &PPPallowed
)) {
589 /* PCP not used at all from this tile. */
590 return std::make_pair (PCP_NONE
, 0);
595 switch (CheckNeighbourPCP (tile
+ TileOffsByDiagDir (side
),
596 ReverseDiagDir (side
),
597 &PPPpreferred
, &PPPallowed
, &nb_slope
)) {
598 default: // PCP_NB_TRY_ELIDE
599 if (CheckPylonElision (side
, PPPpreferred
, odd
, home_slope
== nb_slope
)) {
600 return std::make_pair (PCP_NONE
, 0);
604 pcp_neighbour
= true;
608 pcp_neighbour
= false;
612 /* Now decide where we draw our pylons. First try the preferred PPPs,
613 * but they may not exist. In that case, we try the any of the allowed
614 * ones. if they don't exist either, don't draw anything. Note that
615 * the preferred PPPs still contain the end-of-line markers. Remove
616 * those (simply by ANDing with allowed, since these markers are never
618 if (PPPallowed
== 0) return std::make_pair (PCP_NONE
, 0);
620 if ((PPPallowed
& PPPpreferred
) != 0) PPPallowed
&= PPPpreferred
;
621 return std::make_pair (pcp_neighbour
? PCP_IN_USE_BOTH
: PCP_IN_USE
, PPPallowed
);
625 * Draws overhead wires and pylons for electric railways.
626 * @param ti The TileInfo struct of the tile being drawn
628 void DrawCatenary (const TileInfo
*ti
)
630 /* Pylons are placed on a tile edge, so we need to take into account
631 * the track configuration of 2 adjacent tiles. home stores the
633 TrackBits home_tracks
, home_wires
;
634 /* Note that ti->tileh has already been adjusted for Foundations */
635 Slope home_slope
= ti
->tileh
;
638 odd
[AXIS_X
] = IsOddX(ti
->tile
);
639 odd
[AXIS_Y
] = IsOddY(ti
->tile
);
641 DiagDirection overridePCP
= INVALID_DIAGDIR
;
643 /* Find which rail bits are present, and select the override points.
644 * We don't draw a pylon:
645 * 1) INSIDE a tunnel (we wouldn't see it anyway)
646 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
647 * because that one is drawn on the bridge. Exception is for length 0 bridges
648 * which have no middle tiles */
649 home_tracks
= GetRailTrackBitsUniversal (ti
->tile
, INVALID_DIAGDIR
, &overridePCP
);
650 home_wires
= MaskWireBits (ti
->tile
, home_tracks
);
652 /* Half tile slopes coincide only with horizontal/vertical track.
653 * Faking a flat slope results in the correct sprites on positions. */
654 Track halftile_track
;
655 TileContext halftile_context
;
656 if (IsHalftileSlope (home_slope
)) {
657 switch (GetHalftileSlopeCorner (home_slope
)) {
658 default: NOT_REACHED();
659 case CORNER_W
: halftile_track
= TRACK_LEFT
; break;
660 case CORNER_S
: halftile_track
= TRACK_LOWER
; break;
661 case CORNER_E
: halftile_track
= TRACK_RIGHT
; break;
662 case CORNER_N
: halftile_track
= TRACK_UPPER
; break;
664 halftile_context
= TCX_UPPER_HALFTILE
;
665 home_slope
= SLOPE_FLAT
;
667 switch (home_tracks
) {
668 case TRACK_BIT_LOWER
:
670 halftile_track
= GetRailType(ti
->tile
, TRACK_UPPER
) == GetRailType(ti
->tile
, TRACK_LOWER
) ? INVALID_TRACK
: TRACK_LOWER
;
672 case TRACK_BIT_RIGHT
:
674 halftile_track
= GetRailType(ti
->tile
, TRACK_LEFT
) == GetRailType(ti
->tile
, TRACK_RIGHT
) ? INVALID_TRACK
: TRACK_RIGHT
;
677 halftile_track
= INVALID_TRACK
;
680 halftile_context
= TCX_NORMAL
;
683 if (IsTunnelTile (ti
->tile
)) {
684 home_slope
= SLOPE_STEEP
; // XXX - Hack to make tunnel entrances to always have a pylon
686 AdjustTileh (ti
->tile
, &home_slope
);
689 SpriteID sprite_normal
, sprite_halftile
;
691 if (halftile_track
== INVALID_TRACK
) {
692 sprite_normal
= GetPylonBase(ti
->tile
, TCX_NORMAL
);
694 sprite_halftile
= GetPylonBase(ti
->tile
, halftile_context
, halftile_track
);
695 sprite_normal
= GetPylonBase(ti
->tile
, TCX_NORMAL
,
696 HasBit(home_tracks
, TrackToOppositeTrack(halftile_track
)) ? TrackToOppositeTrack(halftile_track
) : halftile_track
);
699 for (DiagDirection i
= DIAGDIR_BEGIN
; i
< DIAGDIR_END
; i
++) {
700 static const TrackBits edge_tracks
[] = {
701 TRACK_BIT_UPPER
| TRACK_BIT_RIGHT
, // DIAGDIR_NE
702 TRACK_BIT_LOWER
| TRACK_BIT_RIGHT
, // DIAGDIR_SE
703 TRACK_BIT_LOWER
| TRACK_BIT_LEFT
, // DIAGDIR_SW
704 TRACK_BIT_UPPER
| TRACK_BIT_LEFT
, // DIAGDIR_NW
706 SpriteID pylon_base
= (halftile_track
!= INVALID_TRACK
&& HasBit(edge_tracks
[i
], halftile_track
)) ? sprite_halftile
: sprite_normal
;
710 if (overridePCP
!= i
) {
711 std::pair
<uint
, byte
> pcp_state
= CheckSidePCP (ti
->tile
,
712 home_tracks
, home_wires
, home_slope
,
714 if (pcp_state
.first
== PCP_NONE
) continue;
715 pcp_neighbour
= (pcp_state
.first
== PCP_IN_USE_BOTH
);
716 PPPallowed
= pcp_state
.second
;
717 SetBit(PCPstatus
, i
);
718 } else if (!IsRailwayTile (ti
->tile
)) {
720 SetBit(PCPstatus
, i
);
724 TrackBits bridge_tracks
= DiagdirReachesTracks (ReverseDiagDir (i
));
725 if ((home_tracks
& bridge_tracks
) == TRACK_BIT_NONE
) continue;
726 SetBit(PCPstatus
, i
);
727 /* Pylon is drawn by the middle part if there is any. */
728 if (GetTunnelBridgeLength (ti
->tile
, GetOtherBridgeEnd (ti
->tile
)) > 0) continue;
729 pcp_neighbour
= true;
730 PPPallowed
= AllowedPPPonPCP
[i
];
733 if (IsRailStationTile(ti
->tile
) && !CanStationTileHavePylons(ti
->tile
)) continue;
735 if (HasBridgeAbove(ti
->tile
)) {
736 if (GetBridgeAxis (ti
->tile
) == DiagDirToAxis (i
)) {
737 int height
= GetBridgeHeight (GetNorthernBridgeEnd (ti
->tile
));
738 if (height
<= GetTileMaxZ (ti
->tile
) + 1) {
744 for (Direction k
= DIR_BEGIN
; k
< DIR_END
; k
++) {
745 byte temp
= PPPorder
[odd
[AXIS_X
]][odd
[AXIS_Y
]][i
][k
];
747 if (!HasBit(PPPallowed
, temp
)) continue;
749 /* Don't build the pylon if it would be outside the tile */
750 if (HasBit(OwnedPPPonPCP
[i
], temp
)) {
751 uint x
= ti
->x
+ x_pcp_offsets
[i
] + x_ppp_offsets
[temp
];
752 uint y
= ti
->y
+ y_pcp_offsets
[i
] + y_ppp_offsets
[temp
];
754 int elevation
= GetPCPElevation (ti
->tile
, i
);
756 AddSortableSpriteToDraw (ti
->vd
, pylon_base
+ pylon_sprites
[temp
], PAL_NONE
, x
, y
, 1, 1, BB_HEIGHT_UNDER_BRIDGE
,
757 elevation
, IsTransparencySet(TO_CATENARY
), -1, -1);
758 break; // We already have drawn a pylon, bail out
761 /* We have a neighbour that will draw it, bail out */
762 if (pcp_neighbour
) break;
766 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawCatenaryOnTunnel()) */
767 if (IsTunnelTile(ti
->tile
)) return;
769 /* Don't draw a wire under a low bridge */
770 if (HasBridgeAbove(ti
->tile
) && !IsTransparencySet(TO_BRIDGES
)) {
771 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
773 if (height
<= GetTileMaxZ(ti
->tile
) + 1) return;
776 /* Don't draw a wire if the station tile does not want any */
777 if (IsRailStationTile(ti
->tile
) && !CanStationTileHaveWires(ti
->tile
)) return;
779 if (halftile_track
== INVALID_TRACK
) {
780 sprite_normal
= GetWireBase(ti
->tile
, TCX_NORMAL
);
782 sprite_halftile
= GetWireBase(ti
->tile
, halftile_context
, halftile_track
);
783 sprite_normal
= GetWireBase(ti
->tile
, TCX_NORMAL
,
784 HasBit(home_tracks
, TrackToOppositeTrack(halftile_track
)) ? TrackToOppositeTrack(halftile_track
) : halftile_track
);
787 /* Drawing of pylons is finished, now draw the wires */
789 FOR_EACH_SET_TRACK(t
, home_wires
) {
790 byte PCPconfig
= HasBit(PCPstatus
, PCPpositions
[t
][0]) +
791 (HasBit(PCPstatus
, PCPpositions
[t
][1]) << 1);
793 assert(PCPconfig
!= 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
794 assert(!IsSteepSlope(home_slope
));
796 const SortableSpriteStructM
*sss
;
797 switch (home_slope
) {
798 case SLOPE_SW
: sss
= &CatenarySpriteDataSW
; break;
799 case SLOPE_SE
: sss
= &CatenarySpriteDataSE
; break;
800 case SLOPE_NW
: sss
= &CatenarySpriteDataNW
; break;
801 case SLOPE_NE
: sss
= &CatenarySpriteDataNE
; break;
802 default: sss
= &CatenarySpriteData
[t
]; break;
806 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
807 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
808 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
810 SpriteID wire_base
= (t
== halftile_track
) ? sprite_halftile
: sprite_normal
;
811 AddSortableSpriteToDraw (ti
->vd
, wire_base
+ sss
->image_offset
[PCPconfig
], PAL_NONE
, ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
,
812 sss
->x_size
, sss
->y_size
, sss
->z_size
, GetSlopePixelZ(ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
) + sss
->z_offset
,
813 IsTransparencySet(TO_CATENARY
));
818 * Draws wires on a tunnel tile
820 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
822 * @param ti The Tileinfo to draw the tile for
824 void DrawCatenaryOnBridge(const TileInfo
*ti
)
826 TileIndex start
= GetNorthernBridgeEnd(ti
->tile
);
827 TileIndex end
= GetSouthernBridgeEnd(ti
->tile
);
829 uint length
= GetTunnelBridgeLength(start
, end
);
830 uint num
= GetTunnelBridgeLength(ti
->tile
, start
) + 1;
832 Axis axis
= GetBridgeAxis(ti
->tile
);
834 const SortableSpriteStructM
*sss
= &CatenarySpriteData
[AxisToTrack(axis
)];
837 if ((length
% 2) && num
== length
) {
838 /* Draw the "short" wire on the southern end of the bridge
839 * only needed if the length of the bridge is odd */
842 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
843 config
= 2 - (num
% 2);
846 uint height
= GetBridgePixelHeight(end
);
848 SpriteID wire_base
= GetWireBase(end
, TCX_ON_BRIDGE
);
850 AddSortableSpriteToDraw (ti
->vd
, wire_base
+ sss
->image_offset
[config
], PAL_NONE
, ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
,
851 sss
->x_size
, sss
->y_size
, sss
->z_size
, height
+ sss
->z_offset
,
852 IsTransparencySet(TO_CATENARY
)
855 /* Finished with wires, draw pylons */
856 if ((num
% 2) == 0 && num
!= length
) return; /* no pylons to draw */
858 DiagDirection PCPpos
;
860 if (axis
== AXIS_X
) {
862 PPPpos
= IsOddY(ti
->tile
) ? DIR_SE
: DIR_NW
;
865 PPPpos
= IsOddX(ti
->tile
) ? DIR_SW
: DIR_NE
;
868 SpriteID pylon
= GetPylonBase(end
, TCX_ON_BRIDGE
) + pylon_sprites
[PPPpos
];
869 uint x
= ti
->x
+ x_ppp_offsets
[PPPpos
];
870 uint y
= ti
->y
+ y_ppp_offsets
[PPPpos
];
872 /* every other tile needs a pylon on the northern end */
874 AddSortableSpriteToDraw (ti
->vd
, pylon
, PAL_NONE
, x
+ x_pcp_offsets
[PCPpos
], y
+ y_pcp_offsets
[PCPpos
],
875 1, 1, BB_HEIGHT_UNDER_BRIDGE
, height
, IsTransparencySet(TO_CATENARY
), -1, -1);
878 /* need a pylon on the southern end of the bridge */
880 PCPpos
= ReverseDiagDir(PCPpos
);
881 AddSortableSpriteToDraw (ti
->vd
, pylon
, PAL_NONE
, x
+ x_pcp_offsets
[PCPpos
], y
+ y_pcp_offsets
[PCPpos
],
882 1, 1, BB_HEIGHT_UNDER_BRIDGE
, height
, IsTransparencySet(TO_CATENARY
), -1, -1);
886 bool SettingsDisableElrail(int32 p1
)
890 bool disable
= (p1
!= 0);
892 /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
893 const RailType old_railtype
= disable
? RAILTYPE_ELECTRIC
: RAILTYPE_RAIL
;
894 const RailType new_railtype
= disable
? RAILTYPE_RAIL
: RAILTYPE_ELECTRIC
;
896 /* walk through all train engines */
898 FOR_ALL_ENGINES_OF_TYPE(e
, VEH_TRAIN
) {
899 RailVehicleInfo
*rv_info
= &e
->u
.rail
;
900 /* if it is an electric rail engine and its railtype is the wrong one */
901 if (rv_info
->engclass
== 2 && rv_info
->railtype
== old_railtype
) {
902 /* change it to the proper one */
903 rv_info
->railtype
= new_railtype
;
907 /* when disabling elrails, make sure that all existing trains can run on
911 if (t
->railtype
== RAILTYPE_ELECTRIC
) {
912 /* this railroad vehicle is now compatible only with elrail,
913 * so add there also normal rail compatibility */
914 t
->compatible_railtypes
|= RAILTYPES_RAIL
;
915 t
->railtype
= RAILTYPE_RAIL
;
916 SetBit(t
->flags
, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL
);
921 /* Fix the total power and acceleration for trains */
923 /* power and acceleration is cached only for front engines */
924 if (t
->IsFrontEngine()) {
925 t
->ConsistChanged(CCF_TRACK
);
929 FOR_ALL_COMPANIES(c
) c
->avail_railtypes
= GetCompanyRailtypes(c
->index
);
931 /* This resets the _last_built_railtype, which will be invalid for electric
932 * rails. It may have unintended consequences if that function is ever
933 * extended, though. */
934 ReinitGuiAfterToggleElrail(disable
);