Translations update
[openttd/fttd.git] / src / elrail.cpp
blob2442c41e02a06ed8df9bd8bfa9ba73d43458602c
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /**
11 * @file elrail.cpp
12 * This file deals with displaying wires and pylons for electric railways.
13 * <h2>Basics</h2>
15 * <h3>Tile Types</h3>
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
20 * stations, etc).
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
39 * other tile.
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
57 #include "stdafx.h"
59 #include <utility>
61 #include "map/rail.h"
62 #include "map/road.h"
63 #include "map/slope.h"
64 #include "map/bridge.h"
65 #include "map/tunnelbridge.h"
66 #include "viewport_func.h"
67 #include "train.h"
68 #include "rail_gui.h"
69 #include "bridge.h"
70 #include "elrail_func.h"
71 #include "company_base.h"
72 #include "newgrf_railtype.h"
73 #include "station_func.h"
74 #include "newgrf_station.h"
77 /** Which PPPs are possible at all on a given PCP */
78 static const byte AllowedPPPonPCP[DIAGDIR_END] = {
79 1 << DIR_N | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_W | 1 << DIR_NW,
80 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W,
81 1 << DIR_N | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_W | 1 << DIR_NW,
82 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W,
86 /* Geometric placement of the PCP relative to the tile origin */
87 static const int8 x_pcp_offsets[DIAGDIR_END] = {0, 8, 16, 8};
88 static const int8 y_pcp_offsets[DIAGDIR_END] = {8, 16, 8, 0};
89 /* Geometric placement of the PPP relative to the PCP*/
90 static const int8 x_ppp_offsets[DIR_END] = {-2, -4, -2, 0, 2, 4, 2, 0};
91 static const int8 y_ppp_offsets[DIR_END] = {-2, 0, 2, 4, 2, 0, -2, -4};
93 /* The type of pylon to draw at each PPP */
94 static const uint8 pylon_sprites[DIR_END] = { 4, 0, 7, 3, 5, 1, 6, 2, };
96 /**
97 * Offset for wire sprites from the base wire sprite.
99 enum WireSpriteOffset {
100 WSO_X_SHORT,
101 WSO_Y_SHORT,
102 WSO_EW_SHORT,
103 WSO_NS_SHORT,
104 WSO_X_SHORT_DOWN,
105 WSO_Y_SHORT_UP,
106 WSO_X_SHORT_UP,
107 WSO_Y_SHORT_DOWN,
109 WSO_X_SW,
110 WSO_Y_SE,
111 WSO_EW_E,
112 WSO_NS_S,
113 WSO_X_SW_DOWN,
114 WSO_Y_SE_UP,
115 WSO_X_SW_UP,
116 WSO_Y_SE_DOWN,
118 WSO_X_NE,
119 WSO_Y_NW,
120 WSO_EW_W,
121 WSO_NS_N,
122 WSO_X_NE_DOWN,
123 WSO_Y_NW_UP,
124 WSO_X_NE_UP,
125 WSO_Y_NW_DOWN,
127 WSO_ENTRANCE_NE,
128 WSO_ENTRANCE_SE,
129 WSO_ENTRANCE_SW,
130 WSO_ENTRANCE_NW,
133 struct SortableSpriteStructM {
134 int8 x_offset;
135 int8 y_offset;
136 int8 x_size;
137 int8 y_size;
138 int8 z_offset;
139 uint8 image_offset[3];
142 /** Distance between wire and rail */
143 static const uint ELRAIL_ELEVATION = 10;
144 /** Wires that a draw one level higher than the north corner. */
145 static const uint ELRAIL_ELEVRAISE = ELRAIL_ELEVATION + TILE_HEIGHT;
147 static const SortableSpriteStructM CatenarySpriteData[TRACK_END] = {
148 { 0, 7, 15, 1, ELRAIL_ELEVATION, { WSO_X_NE, WSO_X_SW, WSO_X_SHORT } }, // X flat
149 { 7, 0, 1, 15, ELRAIL_ELEVATION, { WSO_Y_SE, WSO_Y_NW, WSO_Y_SHORT } }, // Y flat
150 { 7, 0, 1, 1, ELRAIL_ELEVATION, { WSO_EW_W, WSO_EW_E, WSO_EW_SHORT } }, // UPPER
151 { 15, 8, 3, 3, ELRAIL_ELEVATION, { WSO_EW_E, WSO_EW_W, WSO_EW_SHORT } }, // LOWER
152 { 8, 0, 8, 8, ELRAIL_ELEVATION, { WSO_NS_S, WSO_NS_N, WSO_NS_SHORT } }, // LEFT
153 { 0, 8, 8, 8, ELRAIL_ELEVATION, { WSO_NS_N, WSO_NS_S, WSO_NS_SHORT } }, // RIGHT
156 static const SortableSpriteStructM CatenarySpriteDataSW =
157 { 0, 7, 15, 8, ELRAIL_ELEVRAISE, { WSO_X_NE_UP, WSO_X_SW_UP, WSO_X_SHORT_UP } }; // X up
159 static const SortableSpriteStructM CatenarySpriteDataSE =
160 { 7, 0, 8, 15, ELRAIL_ELEVRAISE, { WSO_Y_SE_UP, WSO_Y_NW_UP, WSO_Y_SHORT_UP } }; // Y up
162 static const SortableSpriteStructM CatenarySpriteDataNW =
163 { 7, 0, 8, 15, ELRAIL_ELEVATION, { WSO_Y_SE_DOWN, WSO_Y_NW_DOWN, WSO_Y_SHORT_DOWN } }; // Y down
165 static const SortableSpriteStructM CatenarySpriteDataNE =
166 { 0, 7, 15, 8, ELRAIL_ELEVATION, { WSO_X_NE_DOWN, WSO_X_SW_DOWN, WSO_X_SHORT_DOWN } }; // X down
170 * Check if a tile is on an odd X coordinate.
171 * @param t The tile to check
172 * @return Whether the tile is on an odd X coordinate
174 static inline bool IsOddX (TileIndex t)
176 return HasBit (TileX(t), 0);
180 * Check if a tile is on an odd Y coordinate.
181 * @param t The tile to check
182 * @return Whether the tile is on an odd Y coordinate
184 static inline bool IsOddY (TileIndex t)
186 return HasBit (TileY(t), 0);
190 * Test if a rail type has catenary
191 * @param rt Rail type to test
193 static inline bool HasRailCatenary (RailType rt)
195 return HasRailCatenary (GetRailTypeInfo (rt));
198 /** Get the electrified track bits on a railway tile. */
199 static TrackBits GetElectrifiedTrackBits (TileIndex t)
201 TrackBits present = GetTrackBits(t);
202 TrackBits result = TRACK_BIT_NONE;
203 if (HasRailCatenary (GetRailType (t, TRACK_UPPER))) result |= present & (TRACK_BIT_CROSS | TRACK_BIT_UPPER | TRACK_BIT_LEFT);
204 if (HasRailCatenary (GetRailType (t, TRACK_LOWER))) result |= present & (TRACK_BIT_LOWER | TRACK_BIT_RIGHT);
205 return result;
209 * Masks out track bits when neighbouring tiles are unelectrified.
211 static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
213 if (!IsNormalRailTile(t)) return tracks;
215 TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
216 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
217 /* If the neighbour tile is either not electrified or has no tracks that can be reached
218 * from this tile, mark all trackdirs that can be reached from the neighbour tile
219 * as needing no catenary. We make an exception for blocked station tiles with a matching
220 * axis that still display wires to preserve visual continuity. */
221 TileIndex next_tile = TileAddByDiagDir(t, d);
222 TrackBits reachable = TrackStatusToTrackBits(GetTileRailwayStatus(next_tile)) & DiagdirReachesTracks(d);
223 bool add;
224 if (reachable != TRACK_BIT_NONE) {
225 RailType rt = GetRailType (next_tile, FindFirstTrack (reachable));
226 add = (rt == INVALID_RAILTYPE || !HasRailCatenary(rt));
227 } else if (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d)) {
228 add = true;
229 } else {
230 const StationSpec *statspec = GetStationSpec (next_tile);
231 add = (statspec != NULL) && HasBit(statspec->wires, GetStationGfx (next_tile));
233 if (add) {
234 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
238 /* If the tracks from either a diagonal crossing or don't overlap, both
239 * trackdirs have to be marked to mask the corresponding track bit. Else
240 * one marked trackdir is enough the mask the track bit. */
241 TrackBits mask;
242 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
243 /* If the tracks form either a diagonal crossing or don't overlap, both
244 * trackdirs have to be marked to mask the corresponding track bit. */
245 mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
246 /* If that results in no masked tracks and it is not a diagonal crossing,
247 * require only one marked trackdir to mask. */
248 if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
249 } else {
250 /* Require only one marked trackdir to mask the track. */
251 mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
252 /* If that results in an empty set, require both trackdirs for diagonal track. */
253 if ((tracks & mask) == TRACK_BIT_NONE) {
254 if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
255 if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
256 /* If that still is not enough, require both trackdirs for any track. */
257 if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
261 /* Mask the tracks only if at least one track bit would remain. */
262 return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
265 /** Get the base wire sprite to use. */
266 static inline SpriteID GetWireBase (const RailtypeInfo *rti, TileIndex tile,
267 TileContext context = TCX_NORMAL)
269 SpriteID wires = GetCustomRailSprite (rti, tile, RTSG_WIRES, context);
270 return wires == 0 ? SPR_WIRE_BASE : wires;
273 /** Get the base pylon sprite to use. */
274 static inline SpriteID GetPylonBase (const RailtypeInfo *rti, TileIndex tile,
275 TileContext context = TCX_NORMAL)
277 SpriteID pylons = GetCustomRailSprite (rti, tile, RTSG_PYLONS, context);
278 return pylons == 0 ? SPR_PYLON_BASE : pylons;
282 * Draws wires on a rail tunnel or depot tile.
283 * @param ti The TileInfo to draw the tile for.
284 * @param rti The rail type information of the rail.
285 * @param depot The tile is a depot, else a tunnel.
286 * @param dir The direction of the tunnel or depot.
288 void DrawRailTunnelDepotCatenary (const TileInfo *ti, const RailtypeInfo *rti,
289 bool depot, DiagDirection dir)
291 struct SortableSpriteStruct {
292 struct { int8 x, y, w, h; } bb[2];
293 int8 x_offset;
294 int8 y_offset;
297 static const SortableSpriteStruct data[2] = {
298 { { { 0, -6, 16, 8 }, { 0, 0, 15, 1 } }, 0, 7 }, //! Wire along X axis
299 { { { -6, 0, 8, 16 }, { 0, 0, 1, 15 } }, 7, 0 }, //! Wire along Y axis
302 assert_compile (WSO_ENTRANCE_NE == WSO_ENTRANCE_NE + DIAGDIR_NE);
303 assert_compile (WSO_ENTRANCE_SE == WSO_ENTRANCE_NE + DIAGDIR_SE);
304 assert_compile (WSO_ENTRANCE_SW == WSO_ENTRANCE_NE + DIAGDIR_SW);
305 assert_compile (WSO_ENTRANCE_NW == WSO_ENTRANCE_NE + DIAGDIR_NW);
307 const SortableSpriteStruct *sss = &data[DiagDirToAxis(dir)];
308 int dz = depot ? 0 : BB_Z_SEPARATOR - ELRAIL_ELEVATION;
309 int z = depot ? GetTileMaxPixelZ (ti->tile) : GetTilePixelZ (ti->tile);
310 /* This wire is not visible with the default depot sprites. */
311 AddSortableSpriteToDraw (ti->vd,
312 GetWireBase (rti, ti->tile) + WSO_ENTRANCE_NE + dir, PAL_NONE,
313 ti->x + sss->x_offset, ti->y + sss->y_offset,
314 sss->bb[depot].w, sss->bb[depot].h, dz + 1,
315 z + ELRAIL_ELEVATION, IsTransparencySet (TO_CATENARY),
316 sss->bb[depot].x, sss->bb[depot].y, dz);
320 struct SideTrackData {
321 byte track; ///< a track that incides at this side
322 byte preferred; ///< preferred pylon position points for it
325 static const uint NUM_TRACKS_PER_SIDE = 3;
327 /* Side track data, 3 tracks per side. */
328 static const SideTrackData side_tracks[DIAGDIR_END][NUM_TRACKS_PER_SIDE] = {
329 { // NE
330 { TRACK_X, 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_NW },
331 { TRACK_UPPER, 1 << DIR_E | 1 << DIR_N | 1 << DIR_S },
332 { TRACK_RIGHT, 1 << DIR_N | 1 << DIR_E | 1 << DIR_W },
333 }, { // SE
334 { TRACK_Y, 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_SW },
335 { TRACK_LOWER, 1 << DIR_E | 1 << DIR_N | 1 << DIR_S },
336 { TRACK_RIGHT, 1 << DIR_S | 1 << DIR_E | 1 << DIR_W },
337 }, { // SW
338 { TRACK_X, 1 << DIR_SE | 1 << DIR_SW | 1 << DIR_NW },
339 { TRACK_LOWER, 1 << DIR_W | 1 << DIR_N | 1 << DIR_S },
340 { TRACK_LEFT, 1 << DIR_S | 1 << DIR_E | 1 << DIR_W },
341 }, { // NW
342 { TRACK_Y, 1 << DIR_SW | 1 << DIR_NW | 1 << DIR_NE },
343 { TRACK_UPPER, 1 << DIR_W | 1 << DIR_N | 1 << DIR_S },
344 { TRACK_LEFT, 1 << DIR_N | 1 << DIR_E | 1 << DIR_W },
348 /* Mask of positions at which pylons can be built per track. */
349 static const byte allowed_ppp[TRACK_END] = {
350 1 << DIR_N | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_W | 1 << DIR_NW, // X
351 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W, // Y
352 1 << DIR_N | 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_NW, // UPPER
353 1 << DIR_N | 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_NW, // LOWER
354 1 << DIR_NE | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_SW | 1 << DIR_W | 1 << DIR_NW, // LEFT
355 1 << DIR_NE | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_SW | 1 << DIR_W | 1 << DIR_NW, // RIGHT
359 * Mask preferred and allowed pylon position points on a tile side.
360 * @param tracks Tracks present on the tile.
361 * @param wires Electrified tracks present on the tile.
362 * @param side Tile side to check.
363 * @param preferred Pointer to preferred positions to mask.
364 * @param allowed Pointer to allowed positions to mask.
365 * @return The number of wires inciding on the given side.
367 static uint CheckCatenarySide (TrackBits tracks, TrackBits wires,
368 DiagDirection side, byte *preferred, byte *allowed)
370 uint pcp_in_use = 0;
371 byte pmask = 0xFF;
372 byte amask = 0xFF;
374 for (uint k = 0; k < NUM_TRACKS_PER_SIDE; k++) {
375 /* We check whether the track in question is present. */
376 const SideTrackData *data = &side_tracks[side][k];
377 byte track = data->track;
378 if (HasBit(wires, track)) {
379 /* track found */
380 pcp_in_use++;
381 pmask &= data->preferred;
383 if (HasBit(tracks, track)) {
384 amask &= allowed_ppp[track];
388 /* At least the PPPs along the tile side must be in the allowed set. */
389 byte test = (DiagDirToAxis(side) == AXIS_X) ?
390 (1 << DIR_SE) | (1 << DIR_NW) :
391 (1 << DIR_NE) | (1 << DIR_SW);
392 assert ((amask & test) == test);
394 *preferred &= pmask;
395 *allowed &= amask;
396 return pcp_in_use;
400 * Check if the pylon on a tile side should be elided on long track runs.
401 * @param side Tile side to check.
402 * @param preferred Preferred pylon positions.
403 * @param odd Bitmask of tile coordinate parity per axis.
404 * @param level Whether the land is level (for tracks running along an axis).
405 * @return Whether the pylon should be elided.
407 static bool CheckPylonElision (DiagDirection side, byte preferred,
408 byte odd, bool level)
410 /* Preferred pylon positions must be a pair of opposite directions. */
411 if (((preferred ^ (preferred >> 4)) & 0xF) != 0) return false;
412 assert (HasAtMostOneBit (preferred & 0xF));
414 /* Direction bits to test for pylon elision. */
415 static const byte masks[4][DIAGDIR_END] = {
416 { // Y even, X even
417 (1 << DIR_N) | (1 << DIR_E), // NE
418 (1 << DIR_N) | (1 << DIR_NE), // SE
419 (1 << DIR_SE), // SW
420 (1 << DIR_E), // NW
421 }, { // Y even, X odd
422 (1 << DIR_SE), // NE
423 (1 << DIR_E) | (1 << DIR_NE), // SE
424 (1 << DIR_N) | (1 << DIR_E), // SW
425 (1 << DIR_N), // NW
426 }, { // Y odd, X even
427 0, // NE
428 (1 << DIR_E), // SE
429 (1 << DIR_N) | (1 << DIR_E) | (1 << DIR_SE), // SW
430 (1 << DIR_N) | (1 << DIR_NE), // NW
431 }, { // Y odd, X odd
432 (1 << DIR_N) | (1 << DIR_E) | (1 << DIR_SE), // NE
433 (1 << DIR_N), // SE
434 0, // SW
435 (1 << DIR_E) | (1 << DIR_NE), // NW
439 byte test = masks[odd][side];
440 if (!level) test &= (1 << DIR_N) | (1 << DIR_E);
441 return (preferred & test) != 0;
444 /** Possible return values for CheckNeighbourPCP below. */
445 enum {
446 PCP_NB_NONE, ///< PCP not in use from the neighbour tile
447 PCP_NB_TUNNEL, ///< PCP in use by a tunnel from the neighbour tile
448 PCP_NB_IN_USE, ///< PCP is in use and may not be elided
449 PCP_NB_TRY_ELIDE, ///< PCP is in use and may be subject to elision
453 * Check whether a pylon is also in use from a railway tile at the other side.
454 * @param tile The neighbour railway tile to check.
455 * @param side The tile side to check from this tile.
456 * @param preferred Pointer to preferred positions to mask.
457 * @param allowed Pointer to allowed positions to mask.
458 * @param slope Pointer to store the tile slope if pylon elision is possible.
459 * @return A value representing the PCP state at the given side.
461 static uint CheckRailNeighbourPCP (TileIndex tile, DiagDirection side,
462 byte *preferred, byte *allowed, Slope *slope)
464 assert (IsRailwayTile (tile));
466 bool is_bridge = IsTileSubtype (tile, TT_BRIDGE);
467 if (is_bridge && GetTunnelBridgeDirection (tile) == side) {
468 return PCP_NB_NONE;
471 TrackBits nb_tracks = GetElectrifiedTrackBits (tile);
472 if (nb_tracks == TRACK_BIT_NONE) return PCP_NB_NONE;
473 TrackBits nb_wires = MaskWireBits (tile, nb_tracks);
475 /* Tracks inciding from the neighbour tile */
476 switch (CheckCatenarySide (nb_tracks, nb_wires, side,
477 preferred, allowed)) {
478 case 0: return PCP_NB_NONE;
479 case 1: break;
480 default: /* more than one wire, so we need the pylon */
481 return PCP_NB_IN_USE;
484 /* Read the foundations if they are present, and adjust the tileh */
485 assert_compile (TRACK_BIT_X == 1);
486 assert_compile (TRACK_BIT_Y == 2);
488 Slope nb_slope;
489 if (nb_tracks > 2) {
490 /* Anything having more than a single X or Y track must be
491 * flat (or a half tile slope, but we treat those as flat). */
492 nb_slope = SLOPE_FLAT;
493 } else if (!is_bridge) {
494 nb_slope = GetTileSlope (tile);
495 Foundation f = GetRailFoundation (nb_slope, nb_tracks);
496 ApplyFoundationToSlope (f, &nb_slope);
497 } else {
498 nb_slope = GetTileSlope (tile);
499 /* With a single X or Y track, bridge must
500 * head away from our side. */
501 nb_slope = HasBridgeFlatRamp (nb_slope, DiagDirToAxis (side)) ?
502 SLOPE_FLAT :
503 InclinedSlope (ReverseDiagDir (side));
506 *slope = nb_slope;
507 return PCP_NB_TRY_ELIDE;
511 * Check whether a pylon is also in use from the other side.
512 * @param tile The neighbour tile to check.
513 * @param side The tile side to check from this tile.
514 * @param preferred Pointer to preferred positions to mask.
515 * @param allowed Pointer to allowed positions to mask.
516 * @param slope Pointer to store the tile slope if pylon elision is possible.
517 * @return A value representing the PCP state at the given side.
519 static uint CheckNeighbourPCP (TileIndex tile, DiagDirection side,
520 byte *preferred, byte *allowed, Slope *slope)
522 Axis axis;
523 switch (GetTileType (tile)) {
524 case TT_RAILWAY:
525 return CheckRailNeighbourPCP (tile, side,
526 preferred, allowed, slope);
528 case TT_MISC:
529 switch (GetTileSubtype (tile)) {
530 default: return PCP_NB_NONE;
532 case TT_MISC_CROSSING:
533 if (!HasRailCatenary (GetRailType (tile))) return PCP_NB_NONE;
534 axis = GetCrossingRailAxis (tile);
535 break;
537 case TT_MISC_TUNNEL:
538 if (GetTunnelTransportType (tile) != TRANSPORT_RAIL) return PCP_NB_NONE;
539 if (!HasRailCatenary (GetRailType (tile))) return PCP_NB_NONE;
540 /* ignore tunnels facing the wrong way for neighbouring tiles */
541 if (GetTunnelBridgeDirection (tile) != ReverseDiagDir (side)) return PCP_NB_NONE;
542 return PCP_NB_TUNNEL;
544 break;
546 case TT_STATION: {
547 if (!HasStationRail (tile)) return PCP_NB_NONE;
548 if (!HasRailCatenary (GetRailType (tile))) return PCP_NB_NONE;
549 /* Ignore neighbouring station tiles that allow neither wires nor pylons. */
550 const StationSpec *statspec = GetStationSpec (tile);
551 if (statspec != NULL) {
552 byte mask = statspec->wires & ~statspec->pylons;
553 uint gfx = GetStationGfx (tile);
554 if (HasBit(mask, gfx)) return PCP_NB_NONE;
556 axis = GetRailStationAxis (tile);
557 break;
560 default:
561 return PCP_NB_NONE;
564 /* Crossing or station tile, so just one flat track along an axis. */
566 /* We check whether the track in question is present. */
567 if (DiagDirToAxis (side) != axis) return PCP_NB_NONE;
569 /* Track found. */
570 *preferred &= side_tracks[side][0].preferred;
571 *slope = SLOPE_FLAT;
572 return PCP_NB_TRY_ELIDE;
575 /** Possible return values for CheckSidePCP below. */
576 enum {
577 PCP_NONE, ///< PCP is not in use
578 PCP_IN_USE, ///< PCP is in use from this tile
579 PCP_IN_USE_BOTH, ///< PCP is in use also from the neighbour tile
583 * Check whether there should be a pylon at a tile side.
584 * @param tile The tile to check.
585 * @param home_tracks Tracks present on the home tile.
586 * @param home_wires Electrified tracks present on the home tile.
587 * @param home_slope Slope of the home tile, adjusted for foundations.
588 * @param side The side to check.
589 * @param odd Bitmask of tile coordinate parity per axis.
590 * @return A value representing the PCP state at the given side, plus
591 * a bitmask of preferred directions for the pylon, if any.
593 static std::pair <uint, byte> CheckSidePCP (TileIndex tile,
594 TrackBits home_tracks, TrackBits home_wires, Slope home_slope,
595 DiagDirection side, byte odd)
597 /* We cycle through all the existing tracks at a PCP and see what
598 * PPPs we want to have, or may not have at all */
599 byte PPPpreferred = 0xFF; // We start with preferring everything (end-of-line in any direction)
600 byte PPPallowed = AllowedPPPonPCP[side];
602 /* Tracks inciding from the home tile */
603 if (CheckCatenarySide (home_tracks, home_wires, side, &PPPpreferred,
604 &PPPallowed) == 0) {
605 /* PCP not used at all from this tile. */
606 return std::make_pair (PCP_NONE, 0);
609 bool pcp_neighbour;
610 Slope nb_slope;
611 switch (CheckNeighbourPCP (tile + TileOffsByDiagDir (side),
612 ReverseDiagDir (side),
613 &PPPpreferred, &PPPallowed, &nb_slope)) {
614 default: // PCP_NB_TRY_ELIDE
615 if (CheckPylonElision (side, PPPpreferred, odd, home_slope == nb_slope)) {
616 return std::make_pair (PCP_NONE, 0);
618 FALLTHROUGH;
619 case PCP_NB_IN_USE:
620 pcp_neighbour = true;
621 break;
623 case PCP_NB_TUNNEL:
624 /* force tunnels to always have a pylon (no elision) */
625 return std::make_pair (PCP_IN_USE_BOTH, 0);
627 case PCP_NB_NONE:
628 pcp_neighbour = false;
629 break;
632 /* At least the PPPs along the tile side must be in the allowed set. */
633 byte test = (DiagDirToAxis(side) == AXIS_X) ?
634 (1 << DIR_SE) | (1 << DIR_NW) :
635 (1 << DIR_NE) | (1 << DIR_SW);
636 assert ((PPPallowed & test) == test);
638 /* Now decide where we draw our pylons. First try the preferred PPPs,
639 * but they may not exist. In that case, we try the any of the allowed
640 * ones. Note that the preferred PPPs still contain the end-of-line
641 * markers. Remove those (simply by ANDing with allowed, since these
642 * markers are never allowed). */
643 return std::make_pair (pcp_neighbour ? PCP_IN_USE_BOTH : PCP_IN_USE,
644 PPPpreferred & PPPallowed);
648 * Choose the pylon position point to use for a pylon.
649 * @param side Tile side where the pylon will be drawn.
650 * @param allowed Mask of allowed pylon position points.
651 * @param odd Bitmask of tile coordinate parity per axis.
652 * @param nb Whether there is a neighbour tile that could draw this pylon.
653 * @return The pylon position point to use.
655 static inline int ChoosePylonPosition (DiagDirection side, byte allowed,
656 byte odd, bool nb)
658 assert_compile (DIR_END == 8);
660 /* Several PPPs maybe exist, here they are sorted in order of preference. */
661 #define D(x,n) (DIR_##x << (4 * n))
662 #define M(a,b,c,d,e,f) D(a,0) | D(b,1) | D(c,2) | D(d,3) | D(e,4) | D(f,5)
663 static const uint32 order[4][DIAGDIR_END] = {
664 { // Y even, X even
665 M (NW, SE, N, E, S, W), // NE
666 M (NE, SW, S, E, N, W), // SE
667 M (NW, SE, S, W, N, E), // SW
668 M (NE, SW, N, W, S, E), // NW
669 }, { // Y even, X odd
670 M (NW, SE, S, W, N, E), // NE
671 M (SW, NE, N, W, S, E), // SE
672 M (NW, SE, N, E, S, W), // SW
673 M (SW, NE, S, E, N, W), // NW
674 }, { // Y odd, X even
675 M (SE, NW, S, W, N, E), // NE
676 M (NE, SW, N, W, S, E), // SE
677 M (SE, NW, N, E, S, W), // SW
678 M (NE, SW, S, E, N, W), // NW
679 }, { // Y odd, X odd
680 M (SE, NW, N, E, S, W), // NE
681 M (SW, NE, S, E, N, W), // SE
682 M (SE, NW, S, W, N, E), // SW
683 M (SW, NE, N, W, S, E), // NW
686 #undef M
687 #undef D
689 /* Which of the PPPs are inside the tile. For the two PPPs on the tile
690 * border the following system is used: if you rotate the PCP so that
691 * it is in the north, the eastern PPP belongs to the tile. */
692 static const byte owned[DIAGDIR_END] = {
693 1 << DIR_SE | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W,
694 1 << DIR_N | 1 << DIR_SW | 1 << DIR_W | 1 << DIR_NW,
695 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_NW,
696 1 << DIR_NE | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S,
699 assert (allowed != 0);
701 uint32 x = order[odd][side];
703 byte own = owned[side] & allowed;
704 byte mask = nb ? allowed : own;
706 for (;;) {
707 byte pos = x & 0xF;
709 /* Don't build the pylon if it would be outside the tile */
710 if (HasBit(mask, pos)) {
711 /* We have a neighbour that will draw it, bail out */
712 return HasBit(own, pos) ? pos : -1;
715 assert (x != 0);
716 x >>= 4;
721 * Choose the pylon position point to use for a pylon.
722 * @param side Tile side where the pylon will be drawn.
723 * @param odd_x Whether the tile is on an odd X coordinate.
724 * @param odd_y Whether the tile is on an odd Y coordinate.
725 * @param nb Whether there is a neighbour tile that could draw this pylon.
726 * @return The pylon position point to use.
728 static int ChoosePylonPosition (DiagDirection side, bool odd_x, bool odd_y,
729 bool nb)
731 /* This is a clone of the previous function for the particular case
732 * where all pylon position points are allowed. */
734 if (nb) {
735 /* We have a neighbour that will draw it, bail out. */
736 bool owned = (DiagDirToAxis(side) == AXIS_X) ? odd_y : odd_x;
737 if (!owned ^ HasBit(side, 1)) return -1;
740 return DiagDirToDir (ChangeDiagDir (side, DIAGDIRDIFF_90RIGHT));
744 * Add a pylon sprite for a tile.
745 * @param ti The TileInfo struct of the tile being drawn.
746 * @param pylon The sprite to draw.
747 * @param x X position of the sprite.
748 * @param y Y position of the sprite.
749 * @param z Z position of the sprite.
751 static void AddPylonSprite (const TileInfo *ti, SpriteID pylon,
752 int x, int y, int z)
754 AddSortableSpriteToDraw (ti->vd, pylon, PAL_NONE, x, y, 1, 1,
755 BB_HEIGHT_UNDER_BRIDGE, z,
756 IsTransparencySet (TO_CATENARY), -1, -1);
760 * Draw a pylon at a tile side.
761 * @param ti The TileInfo struct of the tile being drawn.
762 * @param side Side where to draw the pylon.
763 * @param dir Pylon position point.
764 * @param pylon_base Pylon sprite base.
766 static void DrawPylon (const TileInfo *ti, DiagDirection side, Direction dir,
767 SpriteID pylon_base)
769 uint x = ti->x + x_pcp_offsets[side] + x_ppp_offsets[dir];
770 uint y = ti->y + y_pcp_offsets[side] + y_ppp_offsets[dir];
772 /* The elevation of the "pylon"-sprite should be the elevation
773 * at the PCP. PCPs are always on a tile edge.
775 * This position can be outside of the tile, i.e.
776 * ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
777 * So we have to move it inside the tile, because if the neighboured
778 * tile has a foundation, that does not smoothly connect to the
779 * current tile, we will get a wrong elevation from GetSlopePixelZ().
781 * When we move the position inside the tile, we will get a wrong
782 * elevation if we have a slope. To catch all cases we round the Z
783 * position to the next (TILE_HEIGHT / 2). This will return the
784 * correct elevation for slopes and will also detect non-continuous
785 * elevation on edges.
787 * Also note that the result of GetSlopePixelZ() is very special on
788 * bridge-ramps.
791 TileIndex tile = ti->tile;
792 int z = GetSlopePixelZ (TileX(tile) * TILE_SIZE + min(x_pcp_offsets[side], TILE_SIZE - 1), TileY(tile) * TILE_SIZE + min(y_pcp_offsets[side], TILE_SIZE - 1));
793 int elevation = (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
795 AddPylonSprite (ti, pylon_base + pylon_sprites[dir], x, y, elevation);
799 * Add a wire sprite for a tile.
800 * @param ti The TileInfo struct of the tile being drawn.
801 * @param wire_base The base of the wire sprite to draw.
802 * @param sss The sprite data for the wire.
803 * @param config The configuration to use for the wire.
804 * @param z Base Z position of the sprite.
806 static inline void AddWireSprite (const TileInfo *ti, SpriteID wire_base,
807 const SortableSpriteStructM *sss, uint config, int z)
809 AddSortableSpriteToDraw (ti->vd,
810 wire_base + sss->image_offset[config - 1], PAL_NONE,
811 ti->x + sss->x_offset, ti->y + sss->y_offset,
812 sss->x_size, sss->y_size, 1,
813 z + sss->z_offset, IsTransparencySet (TO_CATENARY));
817 * Draws overhead wires and pylons for electric railways.
818 * @param ti The TileInfo struct of the tile being drawn.
819 * @param rti The rail type information of the rail.
820 * @param tracks Tracks on which to draw.
821 * @param wires Wires to draw.
822 * @param slope Slope of the track surface for pylon elision.
823 * @param draw_pylons Whether to draw pylons (some stations disable this).
824 * @param draw_wires Whether to draw wires (some stations disable this).
825 * @param context Tile context for GetWireBase and GetPylonBase.
826 * @param bridge Bridge direction, if any.
828 static void DrawRailCatenary (const TileInfo *ti, const RailtypeInfo *rti,
829 TrackBits tracks, TrackBits wires, Slope slope,
830 bool draw_pylons, bool draw_wires, TileContext context = TCX_NORMAL,
831 DiagDirection bridge = INVALID_DIAGDIR)
833 byte odd = 0;
834 if (IsOddX(ti->tile)) SetBit(odd, AXIS_X);
835 if (IsOddY(ti->tile)) SetBit(odd, AXIS_Y);
837 byte pcp_status = 0;
839 SpriteID pylon_base = draw_pylons ? GetPylonBase (rti, ti->tile, context) : 0;
841 for (DiagDirection side = DIAGDIR_BEGIN; side < DIAGDIR_END; side++) {
842 bool pcp_neighbour;
843 byte ppp_allowed;
844 if (side != bridge) {
845 std::pair <uint, byte> pcp_state = CheckSidePCP (ti->tile,
846 tracks, wires, slope, side, odd);
847 if (pcp_state.first == PCP_NONE) continue;
848 pcp_neighbour = (pcp_state.first == PCP_IN_USE_BOTH);
849 ppp_allowed = pcp_state.second;
850 SetBit(pcp_status, side);
851 } else {
852 /* Bridge tile. */
853 TrackBits bridge_tracks = DiagdirReachesTracks (ReverseDiagDir (side));
854 if ((tracks & bridge_tracks) == TRACK_BIT_NONE) continue;
855 SetBit(pcp_status, side);
856 /* Pylon is drawn by the middle part if there is any. */
857 if (GetTunnelBridgeLength (ti->tile, GetOtherBridgeEnd (ti->tile)) > 0) continue;
858 pcp_neighbour = true;
859 ppp_allowed = 0;
862 if (pylon_base == 0) continue;
864 if (HasBridgeAbove(ti->tile)) {
865 if (GetBridgeAxis (ti->tile) == DiagDirToAxis (side)) {
866 int height = GetBridgeHeight (GetNorthernBridgeEnd (ti->tile));
867 if (height <= GetTileMaxZ (ti->tile) + 1) {
868 continue;
873 int pos = (ppp_allowed != 0) ? ChoosePylonPosition (side,
874 ppp_allowed, odd, pcp_neighbour) :
875 ChoosePylonPosition (side, HasBit(odd, AXIS_X),
876 HasBit(odd, AXIS_Y), pcp_neighbour);
877 if (pos >= 0) {
878 DrawPylon (ti, side, (Direction)pos, pylon_base);
882 /* Don't draw a wire if the station tile does not want any */
883 if (!draw_wires) return;
885 /* Don't draw a wire under a low bridge */
886 if (HasBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
887 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
889 if (height <= GetTileMaxZ(ti->tile) + 1) return;
892 /* Drawing of pylons is finished, now draw the wires */
893 SpriteID wire_base = GetWireBase (rti, ti->tile, context);
895 Track t;
896 FOR_EACH_SET_TRACK(t, wires) {
897 /* Map a track bit onto its two tile sides. */
898 static const byte track_sides[TRACK_END][2] = {
899 {DIAGDIR_NE, DIAGDIR_SW}, // X
900 {DIAGDIR_SE, DIAGDIR_NW}, // Y
901 {DIAGDIR_NW, DIAGDIR_NE}, // UPPER
902 {DIAGDIR_SE, DIAGDIR_SW}, // LOWER
903 {DIAGDIR_SW, DIAGDIR_NW}, // LEFT
904 {DIAGDIR_NE, DIAGDIR_SE}, // RIGHT
907 byte pcp_config = HasBit(pcp_status, track_sides[t][0]) +
908 (HasBit(pcp_status, track_sides[t][1]) << 1);
910 assert(pcp_config != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
911 assert(!IsSteepSlope(slope));
913 const SortableSpriteStructM *sss;
914 switch (slope) {
915 case SLOPE_SW: sss = &CatenarySpriteDataSW; break;
916 case SLOPE_SE: sss = &CatenarySpriteDataSE; break;
917 case SLOPE_NW: sss = &CatenarySpriteDataNW; break;
918 case SLOPE_NE: sss = &CatenarySpriteDataNE; break;
919 default: sss = &CatenarySpriteData[t]; break;
923 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
924 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
925 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
927 AddWireSprite (ti, wire_base, sss, pcp_config,
928 GetSlopePixelZ (ti->x + sss->x_offset, ti->y + sss->y_offset));
933 * Draws overhead wires and pylons for electric railways.
934 * @param ti The TileInfo struct of the tile being drawn
936 void DrawRailwayCatenary (const TileInfo *ti)
938 assert (IsRailwayTile (ti->tile));
940 /* Find which rail bits are present, and select the override points. */
941 DiagDirection overridePCP = IsTileSubtype (ti->tile, TT_BRIDGE) ? GetTunnelBridgeDirection (ti->tile) : INVALID_DIAGDIR;
942 TrackBits tracks = GetElectrifiedTrackBits (ti->tile);
943 TrackBits wires = MaskWireBits (ti->tile, tracks);
945 /* Note that ti->tileh has already been adjusted for Foundations */
946 Slope slope = ti->tileh;
948 const RailtypeInfo *rti, *halftile_rti;
949 Track halftile_track;
950 TileContext halftile_context;
951 if (IsHalftileSlope (slope)) {
952 switch (GetHalftileSlopeCorner (slope)) {
953 default: NOT_REACHED();
954 case CORNER_W: halftile_track = TRACK_LEFT; break;
955 case CORNER_S: halftile_track = TRACK_LOWER; break;
956 case CORNER_E: halftile_track = TRACK_RIGHT; break;
957 case CORNER_N: halftile_track = TRACK_UPPER; break;
959 halftile_rti = GetRailTypeInfo (GetRailType (ti->tile, halftile_track));
960 halftile_context = TCX_UPPER_HALFTILE;
961 Track opposite = TrackToOppositeTrack (halftile_track);
962 rti = !HasBit(tracks, opposite) ? NULL :
963 GetRailTypeInfo (GetRailType (ti->tile, opposite));
964 slope = SLOPE_FLAT;
965 } else {
966 RailType rt1 = GetRailType (ti->tile, TRACK_UPPER);
967 RailType rt2 = GetRailType (ti->tile, TRACK_LOWER);
968 rti = GetRailTypeInfo (rt1);
969 if (rt1 == rt2) {
970 halftile_track = INVALID_TRACK;
971 } else {
972 const RailtypeInfo *rti2 = GetRailTypeInfo (rt2);
973 switch (tracks) {
974 case TRACK_BIT_HORZ:
975 halftile_rti = rti2;
976 halftile_track = TRACK_LOWER;
977 break;
978 case TRACK_BIT_VERT:
979 halftile_rti = rti2;
980 halftile_track = TRACK_RIGHT;
981 break;
982 case TRACK_BIT_LOWER:
983 case TRACK_BIT_RIGHT:
984 rti = rti2;
985 /* fall through */
986 default: // TRACK_BIT_UPPER or TRACK_BIT_LEFT
987 halftile_track = INVALID_TRACK;
988 break;
991 halftile_context = TCX_NORMAL;
994 if (halftile_track != INVALID_TRACK) {
995 TrackBits tracks = TrackToTrackBits (halftile_track);
996 if (HasRailCatenary (halftile_rti)) {
997 DrawRailCatenary (ti, halftile_rti, tracks, tracks,
998 SLOPE_FLAT, true, true,
999 halftile_context);
1001 if (rti == NULL) return;
1002 tracks &= ~tracks;
1003 wires = tracks;
1006 if (HasRailCatenary (rti)) {
1007 DrawRailCatenary (ti, rti, tracks, wires, slope, true, true,
1008 TCX_NORMAL, overridePCP);
1013 * Draws overhead wires and pylons on a normal (non-custom) bridge head.
1014 * @param ti The TileInfo struct of the tile being drawn.
1015 * @param rti The rail type information of the rail.
1016 * @param dir The direction of the bridge.
1018 void DrawRailBridgeHeadCatenary (const TileInfo *ti, const RailtypeInfo *rti,
1019 DiagDirection dir)
1021 TrackBits tracks = DiagDirToDiagTrackBits (dir);
1023 DrawRailCatenary (ti, rti, tracks, tracks, (ti->tileh != SLOPE_FLAT) ?
1024 SLOPE_FLAT : InclinedSlope (dir),
1025 true, true, TCX_NORMAL, dir);
1029 * Draws overhead wires and pylons for electric railways along an axis
1030 * (for crossings and station tiles).
1031 * @param ti The TileInfo struct of the tile being drawn.
1032 * @param rti The rail type information of the rail.
1033 * @param axis The axis along which to draw the wire.
1034 * @param draw_pylons Whether to draw pylons (some stations disable this).
1035 * @param draw_wire Whether to draw the wire (some stations disable this).
1037 void DrawRailAxisCatenary (const TileInfo *ti, const RailtypeInfo *rti,
1038 Axis axis, bool draw_pylons, bool draw_wire)
1040 /* Note that ti->tileh has already been adjusted for Foundations */
1041 assert (ti->tileh == SLOPE_FLAT);
1043 TrackBits tracks = AxisToTrackBits (axis);
1044 DrawRailCatenary (ti, rti, tracks, tracks, SLOPE_FLAT, draw_pylons, draw_wire);
1048 * Draws overhead wires and pylons at a tunnel entrance.
1049 * @param ti The TileInfo struct of the tile being drawn.
1050 * @param dir The direction of the tunnel.
1052 void DrawRailTunnelCatenary (const TileInfo *ti, DiagDirection dir)
1054 /* Draw pylon. */
1055 TileIndex tile = ti->tile;
1056 DiagDirection rev = ReverseDiagDir (dir);
1058 byte dummy_preferred, dummy_allowed;
1059 Slope dummy_slope;
1060 bool pcp_neighbour = CheckNeighbourPCP (tile + TileOffsByDiagDir (rev),
1061 dir, &dummy_preferred, &dummy_allowed, &dummy_slope)
1062 != PCP_NB_NONE;
1064 int pos = ChoosePylonPosition (rev,
1065 IsOddX(tile), IsOddY(tile), pcp_neighbour);
1067 const RailtypeInfo *rti = GetRailTypeInfo (GetRailType (tile));
1068 if (pos >= 0) {
1069 DrawPylon (ti, rev, (Direction)pos,
1070 GetPylonBase (rti, tile, TCX_NORMAL));
1073 /* Draw wire. */
1074 StartSpriteCombine (ti->vd);
1075 DrawRailTunnelDepotCatenary (ti, rti, false, dir);
1079 * Draws wires on a tunnel tile
1081 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
1083 * @param ti The Tileinfo to draw the tile for
1085 void DrawRailCatenaryOnBridge(const TileInfo *ti)
1087 TileIndex start = GetNorthernBridgeEnd(ti->tile);
1088 bool odd = ((GetTunnelBridgeLength (ti->tile, start) + 1) % 2) != 0;
1090 TileIndex end = GetSouthernBridgeEnd(ti->tile);
1091 bool last = (GetTunnelBridgeLength (ti->tile, end) == 0);
1093 const RailtypeInfo *rti = GetRailTypeInfo (GetBridgeRailType (end));
1095 Axis axis = GetBridgeAxis(ti->tile);
1097 uint config;
1098 if (odd && last) {
1099 /* Draw the "short" wire on the southern end of the bridge
1100 * only needed if the length of the bridge is odd */
1101 config = 3;
1102 } else {
1103 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
1104 config = 2 - odd;
1107 uint height = GetBridgePixelHeight(end);
1109 AddWireSprite (ti, GetWireBase (rti, end, TCX_ON_BRIDGE),
1110 &CatenarySpriteData[AxisToTrack(axis)], config, height);
1112 /* Finished with wires, draw pylons */
1113 if (!odd && !last) return; /* no pylons to draw */
1115 DiagDirection PCPpos;
1116 Direction PPPpos;
1117 if (axis == AXIS_X) {
1118 PCPpos = DIAGDIR_NE;
1119 PPPpos = IsOddY(ti->tile) ? DIR_SE : DIR_NW;
1120 } else {
1121 PCPpos = DIAGDIR_NW;
1122 PPPpos = IsOddX(ti->tile) ? DIR_SW : DIR_NE;
1125 SpriteID pylon = GetPylonBase (rti, end, TCX_ON_BRIDGE) + pylon_sprites[PPPpos];
1126 uint x = ti->x + x_ppp_offsets[PPPpos];
1127 uint y = ti->y + y_ppp_offsets[PPPpos];
1129 /* every other tile needs a pylon on the northern end */
1130 if (odd) {
1131 AddPylonSprite (ti, pylon, x + x_pcp_offsets[PCPpos],
1132 y + y_pcp_offsets[PCPpos], height);
1135 /* need a pylon on the southern end of the bridge */
1136 if (last) {
1137 PCPpos = ReverseDiagDir(PCPpos);
1138 AddPylonSprite (ti, pylon, x + x_pcp_offsets[PCPpos],
1139 y + y_pcp_offsets[PCPpos], height);
1143 bool SettingsDisableElrail(int32 p1)
1145 Company *c;
1146 Train *t;
1147 bool disable = (p1 != 0);
1149 /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
1150 const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
1151 const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
1153 /* walk through all train engines */
1154 Engine *e;
1155 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
1156 RailVehicleInfo *rv_info = &e->u.rail;
1157 /* if it is an electric rail engine and its railtype is the wrong one */
1158 if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
1159 /* change it to the proper one */
1160 rv_info->railtype = new_railtype;
1164 /* when disabling elrails, make sure that all existing trains can run on
1165 * normal rail too */
1166 if (disable) {
1167 FOR_ALL_TRAINS(t) {
1168 if (t->railtype == RAILTYPE_ELECTRIC) {
1169 /* this railroad vehicle is now compatible only with elrail,
1170 * so add there also normal rail compatibility */
1171 t->compatible_railtypes |= RAILTYPES_RAIL;
1172 t->railtype = RAILTYPE_RAIL;
1173 SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
1178 /* Fix the total power and acceleration for trains */
1179 FOR_ALL_TRAINS(t) {
1180 /* power and acceleration is cached only for front engines */
1181 if (t->IsFrontEngine()) {
1182 t->ConsistChanged(CCF_TRACK);
1186 FOR_ALL_COMPANIES(c) c->avail_railtypes = GetCompanyRailtypes(c->index);
1188 /* This resets the _last_built_railtype, which will be invalid for electric
1189 * rails. It may have unintended consequences if that function is ever
1190 * extended, though. */
1191 ReinitGuiAfterToggleElrail(disable);
1192 return true;