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
60 #include "map/slope.h"
61 #include "map/bridge.h"
62 #include "map/tunnelbridge.h"
63 #include "viewport_func.h"
67 #include "elrail_func.h"
68 #include "company_base.h"
69 #include "newgrf_railtype.h"
70 #include "station_func.h"
72 #include "table/elrail_data.h"
75 * Check if a tile is on an odd X coordinate.
76 * @param t The tile to check
77 * @return Whether the tile is on an odd X coordinate
79 static inline bool IsOddX (TileIndex t
)
81 return HasBit (TileX(t
), 0);
85 * Check if a tile is on an odd Y coordinate.
86 * @param t The tile to check
87 * @return Whether the tile is on an odd Y coordinate
89 static inline bool IsOddY (TileIndex t
)
91 return HasBit (TileY(t
), 0);
95 * Finds which Electrified Rail Bits are present on a given tile.
96 * @param t tile to check
97 * @param dir direction this tile is from the home tile, or INVALID_TILE for the home tile itself
98 * @param override pointer to PCP override, can be NULL
99 * @return trackbits of tile if it is electrified
101 static TrackBits
GetRailTrackBitsUniversal(TileIndex t
, DiagDirection dir
, DiagDirection
*override
= NULL
)
103 switch (GetTileType(t
)) {
105 TrackBits present
= GetTrackBits(t
);
106 TrackBits result
= TRACK_BIT_NONE
;
107 if (HasCatenary(GetRailType(t
, TRACK_UPPER
))) result
|= present
& (TRACK_BIT_CROSS
| TRACK_BIT_UPPER
| TRACK_BIT_LEFT
);
108 if (HasCatenary(GetRailType(t
, TRACK_LOWER
))) result
|= present
& (TRACK_BIT_LOWER
| TRACK_BIT_RIGHT
);
110 if (IsTileSubtype(t
, TT_BRIDGE
) && (override
!= NULL
) && GetTunnelBridgeLength(t
, GetOtherBridgeEnd(t
)) > 0) {
111 *override
= GetTunnelBridgeDirection(t
);
118 switch (GetTileSubtype(t
)) {
119 default: return TRACK_BIT_NONE
;
121 case TT_MISC_CROSSING
:
122 if (!HasCatenary(GetRailType(t
))) return TRACK_BIT_NONE
;
123 return GetCrossingRailBits(t
);
126 if (GetTunnelTransportType(t
) != TRANSPORT_RAIL
) return TRACK_BIT_NONE
;
127 if (!HasCatenary(GetRailType(t
))) return TRACK_BIT_NONE
;
128 /* ignore tunnels facing the wrong way for neighbouring tiles */
129 if (dir
!= INVALID_DIAGDIR
&& dir
!= GetTunnelBridgeDirection(t
)) return TRACK_BIT_NONE
;
130 if (override
!= NULL
) {
131 *override
= GetTunnelBridgeDirection(t
);
133 return DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t
));
137 if (!HasStationRail(t
)) return TRACK_BIT_NONE
;
138 if (!HasCatenary(GetRailType(t
))) return TRACK_BIT_NONE
;
139 /* Ignore neighbouring station tiles that allow neither wires nor pylons. */
140 if (dir
!= INVALID_DIAGDIR
&& !CanStationTileHavePylons(t
) && !CanStationTileHaveWires(t
)) return TRACK_BIT_NONE
;
141 return TrackToTrackBits(GetRailStationTrack(t
));
144 return TRACK_BIT_NONE
;
149 * Masks out track bits when neighbouring tiles are unelectrified.
151 static TrackBits
MaskWireBits(TileIndex t
, TrackBits tracks
)
153 if (!IsNormalRailTile(t
)) return tracks
;
155 TrackdirBits neighbour_tdb
= TRACKDIR_BIT_NONE
;
156 for (DiagDirection d
= DIAGDIR_BEGIN
; d
< DIAGDIR_END
; d
++) {
157 /* If the neighbour tile is either not electrified or has no tracks that can be reached
158 * from this tile, mark all trackdirs that can be reached from the neighbour tile
159 * as needing no catenary. We make an exception for blocked station tiles with a matching
160 * axis that still display wires to preserve visual continuity. */
161 TileIndex next_tile
= TileAddByDiagDir(t
, d
);
162 TrackBits reachable
= TrackStatusToTrackBits(GetTileRailwayStatus(next_tile
)) & DiagdirReachesTracks(d
);
164 if ((reachable
!= TRACK_BIT_NONE
) ?
165 ((rt
= GetRailType(next_tile
, FindFirstTrack(reachable
))) == INVALID_RAILTYPE
|| !HasCatenary(rt
)) :
166 (!HasStationTileRail(next_tile
) || GetRailStationAxis(next_tile
) != DiagDirToAxis(d
) || !CanStationTileHaveWires(next_tile
))) {
167 neighbour_tdb
|= DiagdirReachesTrackdirs(ReverseDiagDir(d
));
171 /* If the tracks from either a diagonal crossing or don't overlap, both
172 * trackdirs have to be marked to mask the corresponding track bit. Else
173 * one marked trackdir is enough the mask the track bit. */
175 if (tracks
== TRACK_BIT_CROSS
|| !TracksOverlap(tracks
)) {
176 /* If the tracks form either a diagonal crossing or don't overlap, both
177 * trackdirs have to be marked to mask the corresponding track bit. */
178 mask
= ~(TrackBits
)((neighbour_tdb
& (neighbour_tdb
>> 8)) & TRACK_BIT_MASK
);
179 /* If that results in no masked tracks and it is not a diagonal crossing,
180 * require only one marked trackdir to mask. */
181 if (tracks
!= TRACK_BIT_CROSS
&& (mask
& TRACK_BIT_MASK
) == TRACK_BIT_MASK
) mask
= ~TrackdirBitsToTrackBits(neighbour_tdb
);
183 /* Require only one marked trackdir to mask the track. */
184 mask
= ~TrackdirBitsToTrackBits(neighbour_tdb
);
185 /* If that results in an empty set, require both trackdirs for diagonal track. */
186 if ((tracks
& mask
) == TRACK_BIT_NONE
) {
187 if ((neighbour_tdb
& TRACKDIR_BIT_X_NE
) == 0 || (neighbour_tdb
& TRACKDIR_BIT_X_SW
) == 0) mask
|= TRACK_BIT_X
;
188 if ((neighbour_tdb
& TRACKDIR_BIT_Y_NW
) == 0 || (neighbour_tdb
& TRACKDIR_BIT_Y_SE
) == 0) mask
|= TRACK_BIT_Y
;
189 /* If that still is not enough, require both trackdirs for any track. */
190 if ((tracks
& mask
) == TRACK_BIT_NONE
) mask
= ~(TrackBits
)((neighbour_tdb
& (neighbour_tdb
>> 8)) & TRACK_BIT_MASK
);
194 /* Mask the tracks only if at least one track bit would remain. */
195 return (tracks
& mask
) != TRACK_BIT_NONE
? tracks
& mask
: tracks
;
199 * Get the base wire sprite to use.
201 static inline SpriteID
GetWireBase(TileIndex tile
, TileContext context
= TCX_NORMAL
, Track track
= INVALID_TRACK
)
203 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
, track
));
204 SpriteID wires
= GetCustomRailSprite(rti
, tile
, RTSG_WIRES
, context
);
205 return wires
== 0 ? SPR_WIRE_BASE
: wires
;
209 * Get the base pylon sprite to use.
211 static inline SpriteID
GetPylonBase(TileIndex tile
, TileContext context
= TCX_NORMAL
, Track track
= INVALID_TRACK
)
213 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
, track
));
214 SpriteID pylons
= GetCustomRailSprite(rti
, tile
, RTSG_PYLONS
, context
);
215 return pylons
== 0 ? SPR_PYLON_BASE
: pylons
;
219 * Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
220 * @param tile The tile to analyse
221 * @param *tileh the tileh
223 static void AdjustTileh(TileIndex tile
, Slope
*tileh
)
225 if (IsTunnelTile(tile
)) {
226 *tileh
= SLOPE_STEEP
; // XXX - Hack to make tunnel entrances to always have a pylon
227 } else if (IsRailBridgeTile(tile
) && !IsExtendedRailBridge(tile
)) {
228 if (*tileh
!= SLOPE_FLAT
) {
231 *tileh
= InclinedSlope(GetTunnelBridgeDirection(tile
));
237 * Returns the Z position of a Pylon Control Point.
239 * @param tile The tile the pylon should stand on.
240 * @param PCPpos The PCP of the tile.
241 * @return The Z position of the PCP.
243 static int GetPCPElevation(TileIndex tile
, DiagDirection PCPpos
)
245 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
246 * PCPs are always on a tile edge.
248 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
249 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
250 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
252 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
253 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
254 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
256 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
259 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));
260 return (z
+ 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
264 * Draws wires on a tunnel tile
266 * DrawTile_TunnelBridge() calls this function to draw the wires as SpriteCombine with the tunnel roof.
268 * @param ti The Tileinfo to draw the tile for
270 void DrawCatenaryOnTunnel(const TileInfo
*ti
)
272 /* xmin, ymin, xmax + 1, ymax + 1 of BB */
273 static const int _tunnel_wire_BB
[4][4] = {
274 { 0, 1, 16, 15 }, // NE
275 { 1, 0, 15, 16 }, // SE
276 { 0, 1, 16, 15 }, // SW
277 { 1, 0, 15, 16 }, // NW
280 DiagDirection dir
= GetTunnelBridgeDirection(ti
->tile
);
282 SpriteID wire_base
= GetWireBase(ti
->tile
);
284 const SortableSpriteStruct
*sss
= &CatenarySpriteData_TunnelDepot
[dir
];
285 const int *BB_data
= _tunnel_wire_BB
[dir
];
286 AddSortableSpriteToDraw(
287 wire_base
+ sss
->image_offset
, PAL_NONE
, ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
,
288 BB_data
[2] - sss
->x_offset
, BB_data
[3] - sss
->y_offset
, BB_Z_SEPARATOR
- sss
->z_offset
+ 1,
289 GetTilePixelZ(ti
->tile
) + sss
->z_offset
,
290 IsTransparencySet(TO_CATENARY
),
291 BB_data
[0] - sss
->x_offset
, BB_data
[1] - sss
->y_offset
, BB_Z_SEPARATOR
- sss
->z_offset
295 struct CatenaryConfig
{
303 * Draws wires and, if required, pylons on a given tile
304 * @param ti The Tileinfo to draw the tile for
306 static void DrawCatenaryRailway(const TileInfo
*ti
)
308 /* Pylons are placed on a tile edge, so we need to take into account
309 * the track configuration of 2 adjacent tiles. home stores the
312 /* Note that ti->tileh has already been adjusted for Foundations */
313 home
.tileh
= ti
->tileh
;
316 odd
[AXIS_X
] = IsOddX(ti
->tile
);
317 odd
[AXIS_Y
] = IsOddY(ti
->tile
);
319 DiagDirection overridePCP
= INVALID_DIAGDIR
;
321 /* Find which rail bits are present, and select the override points.
322 * We don't draw a pylon:
323 * 1) INSIDE a tunnel (we wouldn't see it anyway)
324 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
325 * because that one is drawn on the bridge. Exception is for length 0 bridges
326 * which have no middle tiles */
327 home
.tracks
= GetRailTrackBitsUniversal(ti
->tile
, INVALID_DIAGDIR
, &overridePCP
);
328 home
.wires
= MaskWireBits(ti
->tile
, home
.tracks
);
329 /* If a track bit is present that is not in the main direction, the track is level */
330 home
.isflat
= ((home
.tracks
& (TRACK_BIT_HORZ
| TRACK_BIT_VERT
)) != 0);
332 /* Half tile slopes coincide only with horizontal/vertical track.
333 * Faking a flat slope results in the correct sprites on positions. */
334 Track halftile_track
;
335 TileContext halftile_context
;
336 if (IsHalftileSlope(home
.tileh
)) {
337 switch (GetHalftileSlopeCorner(home
.tileh
)) {
338 default: NOT_REACHED();
339 case CORNER_W
: halftile_track
= TRACK_LEFT
; break;
340 case CORNER_S
: halftile_track
= TRACK_LOWER
; break;
341 case CORNER_E
: halftile_track
= TRACK_RIGHT
; break;
342 case CORNER_N
: halftile_track
= TRACK_UPPER
; break;
344 halftile_context
= TCX_UPPER_HALFTILE
;
345 home
.tileh
= SLOPE_FLAT
;
347 switch (home
.tracks
) {
348 case TRACK_BIT_LOWER
:
350 halftile_track
= GetRailType(ti
->tile
, TRACK_UPPER
) == GetRailType(ti
->tile
, TRACK_LOWER
) ? INVALID_TRACK
: TRACK_LOWER
;
352 case TRACK_BIT_RIGHT
:
354 halftile_track
= GetRailType(ti
->tile
, TRACK_LEFT
) == GetRailType(ti
->tile
, TRACK_RIGHT
) ? INVALID_TRACK
: TRACK_RIGHT
;
357 halftile_track
= INVALID_TRACK
;
360 halftile_context
= TCX_NORMAL
;
363 AdjustTileh(ti
->tile
, &home
.tileh
);
365 SpriteID sprite_normal
, sprite_halftile
;
367 if (halftile_track
== INVALID_TRACK
) {
368 sprite_normal
= GetPylonBase(ti
->tile
, TCX_NORMAL
);
370 sprite_halftile
= GetPylonBase(ti
->tile
, halftile_context
, halftile_track
);
371 sprite_normal
= GetPylonBase(ti
->tile
, TCX_NORMAL
,
372 HasBit(home
.tracks
, TrackToOppositeTrack(halftile_track
)) ? TrackToOppositeTrack(halftile_track
) : halftile_track
);
375 for (DiagDirection i
= DIAGDIR_BEGIN
; i
< DIAGDIR_END
; i
++) {
376 static const TrackBits edge_tracks
[] = {
377 TRACK_BIT_UPPER
| TRACK_BIT_RIGHT
, // DIAGDIR_NE
378 TRACK_BIT_LOWER
| TRACK_BIT_RIGHT
, // DIAGDIR_SE
379 TRACK_BIT_LOWER
| TRACK_BIT_LEFT
, // DIAGDIR_SW
380 TRACK_BIT_UPPER
| TRACK_BIT_LEFT
, // DIAGDIR_NW
382 SpriteID pylon_base
= (halftile_track
!= INVALID_TRACK
&& HasBit(edge_tracks
[i
], halftile_track
)) ? sprite_halftile
: sprite_normal
;
383 TileIndex neighbour
= ti
->tile
+ TileOffsByDiagDir(i
);
384 int elevation
= GetPCPElevation(ti
->tile
, i
);
385 CatenaryConfig nbconfig
;
387 /* Here's one of the main headaches. GetTileSlope does not correct for possibly
388 * existing foundataions, so we do have to do that manually later on.*/
389 nbconfig
.tileh
= GetTileSlope(neighbour
);
390 nbconfig
.tracks
= GetRailTrackBitsUniversal(neighbour
, i
);
392 /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
393 * we have to draw all pillars on the current tile. */
394 if (nbconfig
.tracks
== TRACK_BIT_NONE
|| elevation
!= GetPCPElevation(neighbour
, ReverseDiagDir(i
))) {
395 nbconfig
.tracks
= TRACK_BIT_NONE
;
396 nbconfig
.wires
= TRACK_BIT_NONE
;
397 nbconfig
.isflat
= false;
399 nbconfig
.wires
= MaskWireBits(neighbour
, nbconfig
.tracks
);
400 nbconfig
.isflat
= ((nbconfig
.tracks
& (TRACK_BIT_HORZ
| TRACK_BIT_VERT
)) != 0);
403 byte PPPpreferred
= 0xFF; // We start with preferring everything (end-of-line in any direction)
404 byte PPPallowed
= AllowedPPPonPCP
[i
];
406 /* We cycle through all the existing tracks at a PCP and see what
407 * PPPs we want to have, or may not have at all */
409 /* Tracks inciding from the home tile */
410 for (uint k
= 0; k
< NUM_TRACKS_PER_SIDE
; k
++) {
411 /* We check whether the track in question (k) is present in the tile */
412 Track track
= TracksAtTileSide
[i
][k
];
413 if (HasBit(home
.wires
, track
)) {
415 SetBit(PCPstatus
, i
); // This PCP is in use
416 PPPpreferred
&= PreferredPPPofTrackAtPCP
[track
][i
];
418 if (HasBit(home
.tracks
, track
)) {
419 PPPallowed
&= AllowedPPPofTrackAtPCP
[track
];
423 /* Tracks inciding from the neighbour tile */
424 for (uint k
= 0; k
< NUM_TRACKS_PER_SIDE
; k
++) {
425 DiagDirection PCPpos
= ReverseDiagDir(i
);
426 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
427 if (IsRailBridgeTile(neighbour
) && GetTunnelBridgeDirection(neighbour
) == PCPpos
) {
431 /* We check whether the track in question (k) is present in the tile */
432 Track track
= TracksAtTileSide
[PCPpos
][k
];
433 if (HasBit(nbconfig
.wires
, track
)) {
434 /* track found, adjust the number
435 * of the PCP for preferred/allowed determination*/
436 SetBit(PCPstatus
, i
); // This PCP is in use
437 PPPpreferred
&= PreferredPPPofTrackAtPCP
[track
][PCPpos
];
440 if (HasBit(nbconfig
.tracks
, track
)) {
441 PPPallowed
&= AllowedPPPofTrackAtPCP
[track
];
445 /* Deactivate all PPPs if PCP is not used */
446 if (!HasBit(PCPstatus
, i
)) continue;
448 Foundation foundation
= FOUNDATION_NONE
;
450 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
451 if (IsStationTile(neighbour
) || IsLevelCrossingTile(neighbour
)) nbconfig
.tileh
= SLOPE_FLAT
;
453 /* Read the foundations if they are present, and adjust the tileh */
454 if (nbconfig
.tracks
!= TRACK_BIT_NONE
&& (IsNormalRailTile(neighbour
) || IsRailDepotTile(neighbour
))) {
455 foundation
= GetRailFoundation(nbconfig
.tileh
, nbconfig
.tracks
);
457 if (IsRailBridgeTile(neighbour
)) {
458 foundation
= GetBridgeFoundation(nbconfig
.tileh
, DiagDirToAxis(GetTunnelBridgeDirection(neighbour
)));
461 ApplyFoundationToSlope(foundation
, &nbconfig
.tileh
);
463 /* Half tile slopes coincide only with horizontal/vertical track.
464 * Faking a flat slope results in the correct sprites on positions. */
465 if (IsHalftileSlope(nbconfig
.tileh
)) nbconfig
.tileh
= SLOPE_FLAT
;
467 AdjustTileh(neighbour
, &nbconfig
.tileh
);
469 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
470 * Delete the PCP if this is the case.
471 * Level means that the slope is the same, or the track is flat */
472 if (home
.tileh
== nbconfig
.tileh
|| (home
.isflat
&& nbconfig
.isflat
)) {
473 Axis axis
= DiagDirToAxis(i
);
474 for (uint k
= 0; k
< NUM_IGNORE_GROUPS
; k
++) {
475 if (PPPpreferred
== IgnoredPCPconfigs
[axis
][k
] ) {
476 /* This configuration may be subject to pylon elision. */
477 bool ignore
= HasBit (IgnoredPCP
[axis
][odd
[OtherAxis(axis
)]], k
);
478 /* Toggle ignore if we are in an odd row, or heading the other way. */
479 if (ignore
^ odd
[axis
] ^ HasBit(i
, 1)) ClrBit(PCPstatus
, i
);
483 if (!HasBit(PCPstatus
, i
)) continue;
486 if (overridePCP
== i
) continue;
488 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
489 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
490 * anything. Note that the preferred PPPs still contain the end-of-line markers.
491 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
492 if (PPPallowed
== 0) continue;
493 if ((PPPallowed
& PPPpreferred
) != 0) PPPallowed
&= PPPpreferred
;
495 if (IsRailStationTile(ti
->tile
) && !CanStationTileHavePylons(ti
->tile
)) continue;
497 if (HasBridgeAbove(ti
->tile
)) {
498 Track bridgetrack
= GetBridgeAxis(ti
->tile
) == AXIS_X
? TRACK_X
: TRACK_Y
;
499 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
501 if ((height
<= GetTileMaxZ(ti
->tile
) + 1) &&
502 (i
== PCPpositions
[bridgetrack
][0] || i
== PCPpositions
[bridgetrack
][1])) {
507 for (Direction k
= DIR_BEGIN
; k
< DIR_END
; k
++) {
508 byte temp
= PPPorder
[odd
[AXIS_X
]][odd
[AXIS_Y
]][i
][k
];
510 if (!HasBit(PPPallowed
, temp
)) continue;
512 /* Don't build the pylon if it would be outside the tile */
513 if (HasBit(OwnedPPPonPCP
[i
], temp
)) {
514 uint x
= ti
->x
+ x_pcp_offsets
[i
] + x_ppp_offsets
[temp
];
515 uint y
= ti
->y
+ y_pcp_offsets
[i
] + y_ppp_offsets
[temp
];
517 AddSortableSpriteToDraw(pylon_base
+ pylon_sprites
[temp
], PAL_NONE
, x
, y
, 1, 1, BB_HEIGHT_UNDER_BRIDGE
,
518 elevation
, IsTransparencySet(TO_CATENARY
), -1, -1);
519 break; // We already have drawn a pylon, bail out
522 /* We have a neighbour that will draw it, bail out */
523 if (nbconfig
.tracks
!= TRACK_BIT_NONE
) break;
527 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawCatenaryOnTunnel()) */
528 if (IsTunnelTile(ti
->tile
)) return;
530 /* Don't draw a wire under a low bridge */
531 if (HasBridgeAbove(ti
->tile
) && !IsTransparencySet(TO_BRIDGES
)) {
532 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
534 if (height
<= GetTileMaxZ(ti
->tile
) + 1) return;
537 /* Don't draw a wire if the station tile does not want any */
538 if (IsRailStationTile(ti
->tile
) && !CanStationTileHaveWires(ti
->tile
)) return;
540 if (halftile_track
== INVALID_TRACK
) {
541 sprite_normal
= GetWireBase(ti
->tile
, TCX_NORMAL
);
543 sprite_halftile
= GetWireBase(ti
->tile
, halftile_context
, halftile_track
);
544 sprite_normal
= GetWireBase(ti
->tile
, TCX_NORMAL
,
545 HasBit(home
.tracks
, TrackToOppositeTrack(halftile_track
)) ? TrackToOppositeTrack(halftile_track
) : halftile_track
);
548 /* Drawing of pylons is finished, now draw the wires */
550 FOR_EACH_SET_TRACK(t
, home
.wires
) {
551 byte PCPconfig
= HasBit(PCPstatus
, PCPpositions
[t
][0]) +
552 (HasBit(PCPstatus
, PCPpositions
[t
][1]) << 1);
554 assert(PCPconfig
!= 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
555 assert(!IsSteepSlope(home
.tileh
));
557 const SortableSpriteStructM
*sss
;
558 switch (home
.tileh
) {
559 case SLOPE_SW
: sss
= &CatenarySpriteDataSW
; break;
560 case SLOPE_SE
: sss
= &CatenarySpriteDataSE
; break;
561 case SLOPE_NW
: sss
= &CatenarySpriteDataNW
; break;
562 case SLOPE_NE
: sss
= &CatenarySpriteDataNE
; break;
563 default: sss
= &CatenarySpriteData
[t
]; break;
567 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
568 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
569 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
571 SpriteID wire_base
= (t
== halftile_track
) ? sprite_halftile
: sprite_normal
;
572 AddSortableSpriteToDraw(wire_base
+ sss
->image_offset
[PCPconfig
], PAL_NONE
, ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
,
573 sss
->x_size
, sss
->y_size
, sss
->z_size
, GetSlopePixelZ(ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
) + sss
->z_offset
,
574 IsTransparencySet(TO_CATENARY
));
579 * Draws wires on a tunnel tile
581 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
583 * @param ti The Tileinfo to draw the tile for
585 void DrawCatenaryOnBridge(const TileInfo
*ti
)
587 TileIndex start
= GetNorthernBridgeEnd(ti
->tile
);
588 TileIndex end
= GetSouthernBridgeEnd(ti
->tile
);
590 uint length
= GetTunnelBridgeLength(start
, end
);
591 uint num
= GetTunnelBridgeLength(ti
->tile
, start
) + 1;
593 Axis axis
= GetBridgeAxis(ti
->tile
);
595 const SortableSpriteStructM
*sss
= &CatenarySpriteData
[AxisToTrack(axis
)];
598 if ((length
% 2) && num
== length
) {
599 /* Draw the "short" wire on the southern end of the bridge
600 * only needed if the length of the bridge is odd */
603 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
604 config
= 2 - (num
% 2);
607 uint height
= GetBridgePixelHeight(end
);
609 SpriteID wire_base
= GetWireBase(end
, TCX_ON_BRIDGE
);
611 AddSortableSpriteToDraw(wire_base
+ sss
->image_offset
[config
], PAL_NONE
, ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
,
612 sss
->x_size
, sss
->y_size
, sss
->z_size
, height
+ sss
->z_offset
,
613 IsTransparencySet(TO_CATENARY
)
616 /* Finished with wires, draw pylons */
617 if ((num
% 2) == 0 && num
!= length
) return; /* no pylons to draw */
619 DiagDirection PCPpos
;
621 if (axis
== AXIS_X
) {
623 PPPpos
= IsOddY(ti
->tile
) ? DIR_SE
: DIR_NW
;
626 PPPpos
= IsOddX(ti
->tile
) ? DIR_SW
: DIR_NE
;
629 SpriteID pylon
= GetPylonBase(end
, TCX_ON_BRIDGE
) + pylon_sprites
[PPPpos
];
630 uint x
= ti
->x
+ x_ppp_offsets
[PPPpos
];
631 uint y
= ti
->y
+ y_ppp_offsets
[PPPpos
];
633 /* every other tile needs a pylon on the northern end */
635 AddSortableSpriteToDraw(pylon
, PAL_NONE
, x
+ x_pcp_offsets
[PCPpos
], y
+ y_pcp_offsets
[PCPpos
],
636 1, 1, BB_HEIGHT_UNDER_BRIDGE
, height
, IsTransparencySet(TO_CATENARY
), -1, -1);
639 /* need a pylon on the southern end of the bridge */
641 PCPpos
= ReverseDiagDir(PCPpos
);
642 AddSortableSpriteToDraw(pylon
, PAL_NONE
, x
+ x_pcp_offsets
[PCPpos
], y
+ y_pcp_offsets
[PCPpos
],
643 1, 1, BB_HEIGHT_UNDER_BRIDGE
, height
, IsTransparencySet(TO_CATENARY
), -1, -1);
648 * Draws overhead wires and pylons for electric railways.
649 * @param ti The TileInfo struct of the tile being drawn
650 * @see DrawCatenaryRailway
652 void DrawCatenary(const TileInfo
*ti
)
654 switch (GetTileType(ti
->tile
)) {
656 switch (GetTileSubtype(ti
->tile
)) {
659 case TT_MISC_DEPOT
: {
660 if (!IsRailDepot(ti
->tile
)) return;
661 const SortableSpriteStruct
*sss
= &CatenarySpriteData_TunnelDepot
[GetGroundDepotDirection(ti
->tile
)];
662 SpriteID wire_base
= GetWireBase(ti
->tile
);
664 /* This wire is not visible with the default depot sprites */
665 AddSortableSpriteToDraw(
666 wire_base
+ sss
->image_offset
, PAL_NONE
, ti
->x
+ sss
->x_offset
, ti
->y
+ sss
->y_offset
,
667 sss
->x_size
, sss
->y_size
, sss
->z_size
,
668 GetTileMaxPixelZ(ti
->tile
) + sss
->z_offset
,
669 IsTransparencySet(TO_CATENARY
)
674 case TT_MISC_CROSSING
:
686 DrawCatenaryRailway(ti
);
689 bool SettingsDisableElrail(int32 p1
)
693 bool disable
= (p1
!= 0);
695 /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
696 const RailType old_railtype
= disable
? RAILTYPE_ELECTRIC
: RAILTYPE_RAIL
;
697 const RailType new_railtype
= disable
? RAILTYPE_RAIL
: RAILTYPE_ELECTRIC
;
699 /* walk through all train engines */
701 FOR_ALL_ENGINES_OF_TYPE(e
, VEH_TRAIN
) {
702 RailVehicleInfo
*rv_info
= &e
->u
.rail
;
703 /* if it is an electric rail engine and its railtype is the wrong one */
704 if (rv_info
->engclass
== 2 && rv_info
->railtype
== old_railtype
) {
705 /* change it to the proper one */
706 rv_info
->railtype
= new_railtype
;
710 /* when disabling elrails, make sure that all existing trains can run on
714 if (t
->railtype
== RAILTYPE_ELECTRIC
) {
715 /* this railroad vehicle is now compatible only with elrail,
716 * so add there also normal rail compatibility */
717 t
->compatible_railtypes
|= RAILTYPES_RAIL
;
718 t
->railtype
= RAILTYPE_RAIL
;
719 SetBit(t
->flags
, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL
);
724 /* Fix the total power and acceleration for trains */
726 /* power and acceleration is cached only for front engines */
727 if (t
->IsFrontEngine()) {
728 t
->ConsistChanged(true);
732 FOR_ALL_COMPANIES(c
) c
->avail_railtypes
= GetCompanyRailtypes(c
->index
);
734 /* This resets the _last_built_railtype, which will be invalid for electric
735 * rails. It may have unintended consequences if that function is ever
736 * extended, though. */
737 ReinitGuiAfterToggleElrail(disable
);