Translations update
[openttd/fttd.git] / src / elrail.cpp
blobe49300fbd2cd875301d99fbf26dd73212065eeb2
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"
76 /** Which PPPs are possible at all on a given PCP */
77 static const byte AllowedPPPonPCP[DIAGDIR_END] = {
78 1 << DIR_N | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_W | 1 << DIR_NW,
79 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W,
80 1 << DIR_N | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_W | 1 << DIR_NW,
81 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W,
85 /* Geometric placement of the PCP relative to the tile origin */
86 static const int8 x_pcp_offsets[DIAGDIR_END] = {0, 8, 16, 8};
87 static const int8 y_pcp_offsets[DIAGDIR_END] = {8, 16, 8, 0};
88 /* Geometric placement of the PPP relative to the PCP*/
89 static const int8 x_ppp_offsets[DIR_END] = {-2, -4, -2, 0, 2, 4, 2, 0};
90 static const int8 y_ppp_offsets[DIR_END] = {-2, 0, 2, 4, 2, 0, -2, -4};
92 /* The type of pylon to draw at each PPP */
93 static const uint8 pylon_sprites[DIR_END] = { 4, 0, 7, 3, 5, 1, 6, 2, };
95 /**
96 * Offset for wire sprites from the base wire sprite.
98 enum WireSpriteOffset {
99 WSO_X_SHORT,
100 WSO_Y_SHORT,
101 WSO_EW_SHORT,
102 WSO_NS_SHORT,
103 WSO_X_SHORT_DOWN,
104 WSO_Y_SHORT_UP,
105 WSO_X_SHORT_UP,
106 WSO_Y_SHORT_DOWN,
108 WSO_X_SW,
109 WSO_Y_SE,
110 WSO_EW_E,
111 WSO_NS_S,
112 WSO_X_SW_DOWN,
113 WSO_Y_SE_UP,
114 WSO_X_SW_UP,
115 WSO_Y_SE_DOWN,
117 WSO_X_NE,
118 WSO_Y_NW,
119 WSO_EW_W,
120 WSO_NS_N,
121 WSO_X_NE_DOWN,
122 WSO_Y_NW_UP,
123 WSO_X_NE_UP,
124 WSO_Y_NW_DOWN,
126 WSO_ENTRANCE_NE,
127 WSO_ENTRANCE_SE,
128 WSO_ENTRANCE_SW,
129 WSO_ENTRANCE_NW,
132 struct SortableSpriteStructM {
133 int8 x_offset;
134 int8 y_offset;
135 int8 x_size;
136 int8 y_size;
137 int8 z_offset;
138 uint8 image_offset[3];
141 /** Distance between wire and rail */
142 static const uint ELRAIL_ELEVATION = 10;
143 /** Wires that a draw one level higher than the north corner. */
144 static const uint ELRAIL_ELEVRAISE = ELRAIL_ELEVATION + TILE_HEIGHT;
146 static const SortableSpriteStructM CatenarySpriteData[TRACK_END] = {
147 { 0, 7, 15, 1, ELRAIL_ELEVATION, { WSO_X_NE, WSO_X_SW, WSO_X_SHORT } }, // X flat
148 { 7, 0, 1, 15, ELRAIL_ELEVATION, { WSO_Y_SE, WSO_Y_NW, WSO_Y_SHORT } }, // Y flat
149 { 7, 0, 1, 1, ELRAIL_ELEVATION, { WSO_EW_W, WSO_EW_E, WSO_EW_SHORT } }, // UPPER
150 { 15, 8, 3, 3, ELRAIL_ELEVATION, { WSO_EW_E, WSO_EW_W, WSO_EW_SHORT } }, // LOWER
151 { 8, 0, 8, 8, ELRAIL_ELEVATION, { WSO_NS_S, WSO_NS_N, WSO_NS_SHORT } }, // LEFT
152 { 0, 8, 8, 8, ELRAIL_ELEVATION, { WSO_NS_N, WSO_NS_S, WSO_NS_SHORT } }, // RIGHT
155 static const SortableSpriteStructM CatenarySpriteDataSW =
156 { 0, 7, 15, 8, ELRAIL_ELEVRAISE, { WSO_X_NE_UP, WSO_X_SW_UP, WSO_X_SHORT_UP } }; // X up
158 static const SortableSpriteStructM CatenarySpriteDataSE =
159 { 7, 0, 8, 15, ELRAIL_ELEVRAISE, { WSO_Y_SE_UP, WSO_Y_NW_UP, WSO_Y_SHORT_UP } }; // Y up
161 static const SortableSpriteStructM CatenarySpriteDataNW =
162 { 7, 0, 8, 15, ELRAIL_ELEVATION, { WSO_Y_SE_DOWN, WSO_Y_NW_DOWN, WSO_Y_SHORT_DOWN } }; // Y down
164 static const SortableSpriteStructM CatenarySpriteDataNE =
165 { 0, 7, 15, 8, ELRAIL_ELEVATION, { WSO_X_NE_DOWN, WSO_X_SW_DOWN, WSO_X_SHORT_DOWN } }; // X down
169 * Check if a tile is on an odd X coordinate.
170 * @param t The tile to check
171 * @return Whether the tile is on an odd X coordinate
173 static inline bool IsOddX (TileIndex t)
175 return HasBit (TileX(t), 0);
179 * Check if a tile is on an odd Y coordinate.
180 * @param t The tile to check
181 * @return Whether the tile is on an odd Y coordinate
183 static inline bool IsOddY (TileIndex t)
185 return HasBit (TileY(t), 0);
189 * Test if a rail type has catenary
190 * @param rt Rail type to test
192 static inline bool HasRailCatenary (RailType rt)
194 return HasRailCatenary (GetRailTypeInfo (rt));
197 /** Get the electrified track bits on a railway tile. */
198 static TrackBits GetElectrifiedTrackBits (TileIndex t)
200 TrackBits present = GetTrackBits(t);
201 TrackBits result = TRACK_BIT_NONE;
202 if (HasRailCatenary (GetRailType (t, TRACK_UPPER))) result |= present & (TRACK_BIT_CROSS | TRACK_BIT_UPPER | TRACK_BIT_LEFT);
203 if (HasRailCatenary (GetRailType (t, TRACK_LOWER))) result |= present & (TRACK_BIT_LOWER | TRACK_BIT_RIGHT);
204 return result;
208 * Masks out track bits when neighbouring tiles are unelectrified.
210 static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
212 if (!IsNormalRailTile(t)) return tracks;
214 TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
215 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
216 /* If the neighbour tile is either not electrified or has no tracks that can be reached
217 * from this tile, mark all trackdirs that can be reached from the neighbour tile
218 * as needing no catenary. We make an exception for blocked station tiles with a matching
219 * axis that still display wires to preserve visual continuity. */
220 TileIndex next_tile = TileAddByDiagDir(t, d);
221 TrackBits reachable = TrackStatusToTrackBits(GetTileRailwayStatus(next_tile)) & DiagdirReachesTracks(d);
222 RailType rt;
223 if ((reachable != TRACK_BIT_NONE) ?
224 ((rt = GetRailType(next_tile, FindFirstTrack(reachable))) == INVALID_RAILTYPE || !HasRailCatenary(rt)) :
225 (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile))) {
226 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
230 /* If the tracks from either a diagonal crossing or don't overlap, both
231 * trackdirs have to be marked to mask the corresponding track bit. Else
232 * one marked trackdir is enough the mask the track bit. */
233 TrackBits mask;
234 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
235 /* If the tracks form either a diagonal crossing or don't overlap, both
236 * trackdirs have to be marked to mask the corresponding track bit. */
237 mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
238 /* If that results in no masked tracks and it is not a diagonal crossing,
239 * require only one marked trackdir to mask. */
240 if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
241 } else {
242 /* Require only one marked trackdir to mask the track. */
243 mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
244 /* If that results in an empty set, require both trackdirs for diagonal track. */
245 if ((tracks & mask) == TRACK_BIT_NONE) {
246 if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
247 if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
248 /* If that still is not enough, require both trackdirs for any track. */
249 if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
253 /* Mask the tracks only if at least one track bit would remain. */
254 return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
257 /** Get the base wire sprite to use. */
258 static inline SpriteID GetWireBase (const RailtypeInfo *rti, TileIndex tile,
259 TileContext context = TCX_NORMAL)
261 SpriteID wires = GetCustomRailSprite (rti, tile, RTSG_WIRES, context);
262 return wires == 0 ? SPR_WIRE_BASE : wires;
265 /** Get the base pylon sprite to use. */
266 static inline SpriteID GetPylonBase (const RailtypeInfo *rti, TileIndex tile,
267 TileContext context = TCX_NORMAL)
269 SpriteID pylons = GetCustomRailSprite (rti, tile, RTSG_PYLONS, context);
270 return pylons == 0 ? SPR_PYLON_BASE : pylons;
274 * Draws wires on a rail tunnel or depot tile.
275 * @param ti The TileInfo to draw the tile for.
276 * @param rti The rail type information of the rail.
277 * @param depot The tile is a depot, else a tunnel.
278 * @param dir The direction of the tunnel or depot.
280 void DrawRailTunnelDepotCatenary (const TileInfo *ti, const RailtypeInfo *rti,
281 bool depot, DiagDirection dir)
283 struct SortableSpriteStruct {
284 struct { int8 x, y, w, h; } bb[2];
285 int8 x_offset;
286 int8 y_offset;
289 static const SortableSpriteStruct data[2] = {
290 { { { 0, -6, 16, 8 }, { 0, 0, 15, 1 } }, 0, 7 }, //! Wire along X axis
291 { { { -6, 0, 8, 16 }, { 0, 0, 1, 15 } }, 7, 0 }, //! Wire along Y axis
294 assert_compile (WSO_ENTRANCE_NE == WSO_ENTRANCE_NE + DIAGDIR_NE);
295 assert_compile (WSO_ENTRANCE_SE == WSO_ENTRANCE_NE + DIAGDIR_SE);
296 assert_compile (WSO_ENTRANCE_SW == WSO_ENTRANCE_NE + DIAGDIR_SW);
297 assert_compile (WSO_ENTRANCE_NW == WSO_ENTRANCE_NE + DIAGDIR_NW);
299 const SortableSpriteStruct *sss = &data[DiagDirToAxis(dir)];
300 int dz = depot ? 0 : BB_Z_SEPARATOR - ELRAIL_ELEVATION;
301 int z = depot ? GetTileMaxPixelZ (ti->tile) : GetTilePixelZ (ti->tile);
302 /* This wire is not visible with the default depot sprites. */
303 AddSortableSpriteToDraw (ti->vd,
304 GetWireBase (rti, ti->tile) + WSO_ENTRANCE_NE + dir, PAL_NONE,
305 ti->x + sss->x_offset, ti->y + sss->y_offset,
306 sss->bb[depot].w, sss->bb[depot].h, dz + 1,
307 z + ELRAIL_ELEVATION, IsTransparencySet (TO_CATENARY),
308 sss->bb[depot].x, sss->bb[depot].y, dz);
312 struct SideTrackData {
313 byte track; ///< a track that incides at this side
314 byte preferred; ///< preferred pylon position points for it
317 static const uint NUM_TRACKS_PER_SIDE = 3;
319 /* Side track data, 3 tracks per side. */
320 static const SideTrackData side_tracks[DIAGDIR_END][NUM_TRACKS_PER_SIDE] = {
321 { // NE
322 { TRACK_X, 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_NW },
323 { TRACK_UPPER, 1 << DIR_E | 1 << DIR_N | 1 << DIR_S },
324 { TRACK_RIGHT, 1 << DIR_N | 1 << DIR_E | 1 << DIR_W },
325 }, { // SE
326 { TRACK_Y, 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_SW },
327 { TRACK_LOWER, 1 << DIR_E | 1 << DIR_N | 1 << DIR_S },
328 { TRACK_RIGHT, 1 << DIR_S | 1 << DIR_E | 1 << DIR_W },
329 }, { // SW
330 { TRACK_X, 1 << DIR_SE | 1 << DIR_SW | 1 << DIR_NW },
331 { TRACK_LOWER, 1 << DIR_W | 1 << DIR_N | 1 << DIR_S },
332 { TRACK_LEFT, 1 << DIR_S | 1 << DIR_E | 1 << DIR_W },
333 }, { // NW
334 { TRACK_Y, 1 << DIR_SW | 1 << DIR_NW | 1 << DIR_NE },
335 { TRACK_UPPER, 1 << DIR_W | 1 << DIR_N | 1 << DIR_S },
336 { TRACK_LEFT, 1 << DIR_N | 1 << DIR_E | 1 << DIR_W },
340 /* Mask of positions at which pylons can be built per track. */
341 static const byte allowed_ppp[TRACK_END] = {
342 1 << DIR_N | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_W | 1 << DIR_NW, // X
343 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W, // Y
344 1 << DIR_N | 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_NW, // UPPER
345 1 << DIR_N | 1 << DIR_NE | 1 << DIR_SE | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_NW, // LOWER
346 1 << DIR_NE | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_SW | 1 << DIR_W | 1 << DIR_NW, // LEFT
347 1 << DIR_NE | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_SW | 1 << DIR_W | 1 << DIR_NW, // RIGHT
351 * Mask preferred and allowed pylon position points on a tile side.
352 * @param tracks Tracks present on the tile.
353 * @param wires Electrified tracks present on the tile.
354 * @param side Tile side to check.
355 * @param preferred Pointer to preferred positions to mask.
356 * @param allowed Pointer to allowed positions to mask.
357 * @return Whether the pylon control point is in use from this tile.
359 static bool CheckCatenarySide (TrackBits tracks, TrackBits wires,
360 DiagDirection side, byte *preferred, byte *allowed)
362 bool pcp_in_use = false;
363 byte pmask = 0xFF;
364 byte amask = 0xFF;
366 for (uint k = 0; k < NUM_TRACKS_PER_SIDE; k++) {
367 /* We check whether the track in question is present. */
368 const SideTrackData *data = &side_tracks[side][k];
369 byte track = data->track;
370 if (HasBit(wires, track)) {
371 /* track found */
372 pcp_in_use = true;
373 pmask &= data->preferred;
375 if (HasBit(tracks, track)) {
376 amask &= allowed_ppp[track];
380 *preferred &= pmask;
381 *allowed &= amask;
382 return pcp_in_use;
386 * Mask preferred and allowed pylon position points on a tile side,
387 * when there is a single track along an axis on the tile.
388 * @param axis Axis of the track.
389 * @param side Tile side to check.
390 * @param preferred Pointer to preferred positions to mask.
391 * @return Whether the pylon control point is in use from this tile.
393 static inline bool CheckCatenarySideAxis (Axis axis, DiagDirection side,
394 byte *preferred)
396 /* We check whether the track in question is present. */
397 if (DiagDirToAxis (side) != axis) return false;
399 /* track found */
400 *preferred &= side_tracks[side][0].preferred;
401 return true;
405 * Check if the pylon on a tile side should be elided on long track runs.
406 * @param side Tile side to check.
407 * @param preferred Preferred pylon positions.
408 * @param odd Array of tile coordinate parity per axis.
409 * @param level Whether the land is level (for tracks running along an axis).
410 * @return Whether the pylon should be elided.
412 static bool CheckPylonElision (DiagDirection side, byte preferred,
413 const bool *odd, bool level)
415 Axis axis = DiagDirToAxis (side);
416 bool ignore;
417 switch (preferred) {
418 case 1 << DIR_NW | 1 << DIR_SE:
419 if (!level) return false;
420 ignore = false; // must be X axis
421 break;
423 case 1 << DIR_NE | 1 << DIR_SW:
424 if (!level) return false;
425 ignore = true; // must be Y axis
426 break;
428 case 1 << DIR_E | 1 << DIR_W:
429 /* Non-orthogonal tracks must always be level. */
430 ignore = (axis == AXIS_X) ? !odd[AXIS_Y] : odd[AXIS_X];
431 break;
433 case 1 << DIR_N | 1 << DIR_S:
434 /* Non-orthogonal tracks must always be level. */
435 ignore = !odd[OtherAxis(axis)];
436 break;
438 default:
439 return false;
442 /* This configuration may be subject to pylon elision. */
443 /* Toggle ignore if we are in an odd row, or heading the other way. */
444 return (ignore ^ odd[axis] ^ HasBit(side, 1));
447 /** Possible return values for CheckNeighbourPCP below. */
448 enum {
449 PCP_NB_NONE, ///< PCP not in use from the neighbour tile
450 PCP_NB_TUNNEL, ///< PCP in use by a tunnel from the neighbour tile
451 PCP_NB_TRY_ELIDE, ///< PCP is in use and may be subject to elision
455 * Check whether a pylon is also in use from a railway tile at the other side.
456 * @param tile The neighbour railway tile to check.
457 * @param side The tile side to check from this tile.
458 * @param preferred Pointer to preferred positions to mask.
459 * @param allowed Pointer to allowed positions to mask.
460 * @param slope Pointer to store the tile slope if pylon elision is possible.
461 * @return A value representing the PCP state at the given side.
463 static uint CheckRailNeighbourPCP (TileIndex tile, DiagDirection side,
464 byte *preferred, byte *allowed, Slope *slope)
466 assert (IsRailwayTile (tile));
468 bool is_bridge = IsTileSubtype (tile, TT_BRIDGE);
469 if (is_bridge && GetTunnelBridgeDirection (tile) == side) {
470 return PCP_NB_NONE;
473 TrackBits nb_tracks = GetElectrifiedTrackBits (tile);
474 if (nb_tracks == TRACK_BIT_NONE) return PCP_NB_NONE;
475 TrackBits nb_wires = MaskWireBits (tile, nb_tracks);
477 /* Tracks inciding from the neighbour tile */
478 if (!CheckCatenarySide (nb_tracks, nb_wires, side, preferred, allowed)) {
479 return PCP_NB_NONE;
482 /* Read the foundations if they are present, and adjust the tileh */
483 assert_compile (TRACK_BIT_X == 1);
484 assert_compile (TRACK_BIT_Y == 2);
486 Slope nb_slope;
487 if (nb_tracks > 2) {
488 /* Anything having more than a single X or Y track must be
489 * flat (or a half tile slope, but we treat those as flat). */
490 nb_slope = SLOPE_FLAT;
491 } else if (!is_bridge) {
492 nb_slope = GetTileSlope (tile);
493 Foundation f = GetRailFoundation (nb_slope, nb_tracks);
494 ApplyFoundationToSlope (f, &nb_slope);
495 } else {
496 nb_slope = GetTileSlope (tile);
497 /* With a single X or Y track, bridge must
498 * head away from our side. */
499 nb_slope = HasBridgeFlatRamp (nb_slope, DiagDirToAxis (side)) ?
500 SLOPE_FLAT :
501 InclinedSlope (ReverseDiagDir (side));
504 *slope = nb_slope;
505 return PCP_NB_TRY_ELIDE;
509 * Check whether a pylon is also in use from the other side.
510 * @param tile The neighbour tile to check.
511 * @param side The tile side to check from this tile.
512 * @param preferred Pointer to preferred positions to mask.
513 * @param allowed Pointer to allowed positions to mask.
514 * @param slope Pointer to store the tile slope if pylon elision is possible.
515 * @return A value representing the PCP state at the given side.
517 static uint CheckNeighbourPCP (TileIndex tile, DiagDirection side,
518 byte *preferred, byte *allowed, Slope *slope)
520 Axis axis;
521 switch (GetTileType (tile)) {
522 case TT_RAILWAY:
523 return CheckRailNeighbourPCP (tile, side,
524 preferred, allowed, slope);
526 case TT_MISC:
527 switch (GetTileSubtype (tile)) {
528 default: return PCP_NB_NONE;
530 case TT_MISC_CROSSING:
531 if (!HasRailCatenary (GetRailType (tile))) return PCP_NB_NONE;
532 axis = GetCrossingRailAxis (tile);
533 break;
535 case TT_MISC_TUNNEL:
536 if (GetTunnelTransportType (tile) != TRANSPORT_RAIL) return PCP_NB_NONE;
537 if (!HasRailCatenary (GetRailType (tile))) return PCP_NB_NONE;
538 /* ignore tunnels facing the wrong way for neighbouring tiles */
539 if (GetTunnelBridgeDirection (tile) != ReverseDiagDir (side)) return PCP_NB_NONE;
540 /* force tunnels to always have a pylon (no elision) */
541 *preferred = 0;
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 if (!CanStationTileHavePylons (tile) && !CanStationTileHaveWires (tile)) return PCP_NB_NONE;
551 axis = GetRailStationAxis (tile);
552 break;
554 default:
555 return PCP_NB_NONE;
558 /* Crossing or station tile, so just one flat track along an axis. */
559 if (!CheckCatenarySideAxis (axis, side, preferred)) {
560 return PCP_NB_NONE;
563 *slope = SLOPE_FLAT;
564 return PCP_NB_TRY_ELIDE;
567 /** Possible return values for CheckSidePCP below. */
568 enum {
569 PCP_NONE, ///< PCP is not in use
570 PCP_IN_USE, ///< PCP is in use from this tile
571 PCP_IN_USE_BOTH, ///< PCP is in use also from the neighbour tile
575 * Check whether there should be a pylon at a tile side.
576 * @param tile The tile to check.
577 * @param home_tracks Tracks present on the home tile.
578 * @param home_wires Electrified tracks present on the home tile.
579 * @param home_slope Slope of the home tile, adjusted for foundations.
580 * @param side The side to check.
581 * @param odd Array of tile coordinate parity per axis.
582 * @return A value representing the PCP state at the given side, plus
583 * a bitmask of allowed directions for the pylon, if any.
585 static std::pair <uint, byte> CheckSidePCP (TileIndex tile,
586 TrackBits home_tracks, TrackBits home_wires, Slope home_slope,
587 DiagDirection side, const bool *odd)
589 /* We cycle through all the existing tracks at a PCP and see what
590 * PPPs we want to have, or may not have at all */
591 byte PPPpreferred = 0xFF; // We start with preferring everything (end-of-line in any direction)
592 byte PPPallowed = AllowedPPPonPCP[side];
594 /* Tracks inciding from the home tile */
595 if (!CheckCatenarySide (home_tracks, home_wires, side, &PPPpreferred, &PPPallowed)) {
596 /* PCP not used at all from this tile. */
597 return std::make_pair (PCP_NONE, 0);
600 bool pcp_neighbour;
601 Slope nb_slope;
602 switch (CheckNeighbourPCP (tile + TileOffsByDiagDir (side),
603 ReverseDiagDir (side),
604 &PPPpreferred, &PPPallowed, &nb_slope)) {
605 default: // PCP_NB_TRY_ELIDE
606 if (CheckPylonElision (side, PPPpreferred, odd, home_slope == nb_slope)) {
607 return std::make_pair (PCP_NONE, 0);
609 /* fall through */
610 case PCP_NB_TUNNEL:
611 pcp_neighbour = true;
612 break;
614 case PCP_NB_NONE:
615 pcp_neighbour = false;
616 break;
619 /* Now decide where we draw our pylons. First try the preferred PPPs,
620 * but they may not exist. In that case, we try the any of the allowed
621 * ones. if they don't exist either, don't draw anything. Note that
622 * the preferred PPPs still contain the end-of-line markers. Remove
623 * those (simply by ANDing with allowed, since these markers are never
624 * allowed) */
625 if (PPPallowed == 0) return std::make_pair (PCP_NONE, 0);
627 if ((PPPallowed & PPPpreferred) != 0) PPPallowed &= PPPpreferred;
628 return std::make_pair (pcp_neighbour ? PCP_IN_USE_BOTH : PCP_IN_USE, PPPallowed);
632 * Choose the pylon position point to use for a pylon.
633 * @param side Tile side where the pylon will be drawn.
634 * @param allowed Mask of allowed pylon position points.
635 * @param order Possible pylon positions arranged by preference.
636 * @param nb Whether there is a neighbour tile that could draw this pylon.
637 * @return The pylon position point to use, or -1 for none.
638 * @note Use the overloaded variant below.
640 static int ChoosePylonPosition (DiagDirection side, byte allowed,
641 const Direction *order, bool nb)
643 /* Which of the PPPs are inside the tile. For the two PPPs on the tile
644 * border the following system is used: if you rotate the PCP so that
645 * it is in the north, the eastern PPP belongs to the tile. */
646 static const byte owned[DIAGDIR_END] = {
647 1 << DIR_SE | 1 << DIR_S | 1 << DIR_SW | 1 << DIR_W,
648 1 << DIR_N | 1 << DIR_SW | 1 << DIR_W | 1 << DIR_NW,
649 1 << DIR_N | 1 << DIR_NE | 1 << DIR_E | 1 << DIR_NW,
650 1 << DIR_NE | 1 << DIR_E | 1 << DIR_SE | 1 << DIR_S,
653 assert (allowed != 0);
655 for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
656 byte pos = order[k];
658 if (!HasBit(allowed, pos)) continue;
660 /* Don't build the pylon if it would be outside the tile */
661 if (HasBit(owned[side], pos)) return pos;
663 /* We have a neighbour that will draw it, bail out */
664 if (nb) return -1;
667 NOT_REACHED();
671 * Choose the pylon position point to use for a pylon.
672 * @param side Tile side where the pylon will be drawn.
673 * @param allowed Mask of allowed pylon position points.
674 * @param odd_x Whether the tile is on an odd X coordinate.
675 * @param odd_y Whether the tile is on an odd Y coordinate.
676 * @param nb Whether there is a neighbour tile that could draw this pylon.
677 * @return The pylon position point to use.
679 static inline int ChoosePylonPosition (DiagDirection side, byte allowed,
680 bool odd_x, bool odd_y, bool nb)
682 /* Several PPPs maybe exist, here they are sorted in order of preference. */
683 static const Direction order[2][2][DIAGDIR_END][DIR_END] = {
684 { // X even
685 { // Y even
686 {DIR_NE, DIR_NW, DIR_SE, DIR_SW, DIR_N, DIR_E, DIR_S, DIR_W}, // NE
687 {DIR_NE, DIR_NW, DIR_SE, DIR_SW, DIR_S, DIR_E, DIR_N, DIR_W}, // SE
688 {DIR_NE, DIR_NW, DIR_SE, DIR_SW, DIR_S, DIR_W, DIR_N, DIR_E}, // SW
689 {DIR_NE, DIR_NW, DIR_SE, DIR_SW, DIR_N, DIR_W, DIR_S, DIR_E}, // NW
690 }, { // Y odd
691 {DIR_NE, DIR_SE, DIR_SW, DIR_NW, DIR_S, DIR_W, DIR_N, DIR_E}, // NE
692 {DIR_NE, DIR_SE, DIR_SW, DIR_NW, DIR_N, DIR_W, DIR_S, DIR_E}, // SE
693 {DIR_NE, DIR_SE, DIR_SW, DIR_NW, DIR_N, DIR_E, DIR_S, DIR_W}, // SW
694 {DIR_NE, DIR_SE, DIR_SW, DIR_NW, DIR_S, DIR_E, DIR_N, DIR_W}, // NW
696 }, { // X odd
697 { // Y even
698 {DIR_SW, DIR_NW, DIR_NE, DIR_SE, DIR_S, DIR_W, DIR_N, DIR_E}, // NE
699 {DIR_SW, DIR_NW, DIR_NE, DIR_SE, DIR_N, DIR_W, DIR_S, DIR_E}, // SE
700 {DIR_SW, DIR_NW, DIR_NE, DIR_SE, DIR_N, DIR_E, DIR_S, DIR_W}, // SW
701 {DIR_SW, DIR_NW, DIR_NE, DIR_SE, DIR_S, DIR_E, DIR_N, DIR_W}, // NW
702 }, { // Y odd
703 {DIR_SW, DIR_SE, DIR_NE, DIR_NW, DIR_N, DIR_E, DIR_S, DIR_W}, // NE
704 {DIR_SW, DIR_SE, DIR_NE, DIR_NW, DIR_S, DIR_E, DIR_N, DIR_W}, // SE
705 {DIR_SW, DIR_SE, DIR_NE, DIR_NW, DIR_S, DIR_W, DIR_N, DIR_E}, // SW
706 {DIR_SW, DIR_SE, DIR_NE, DIR_NW, DIR_N, DIR_W, DIR_S, DIR_E}, // NW
711 return ChoosePylonPosition (side, allowed, order[odd_x][odd_y][side], nb);
715 * Add a pylon sprite for a tile.
716 * @param ti The TileInfo struct of the tile being drawn.
717 * @param pylon The sprite to draw.
718 * @param x X position of the sprite.
719 * @param y Y position of the sprite.
720 * @param z Z position of the sprite.
722 static void AddPylonSprite (const TileInfo *ti, SpriteID pylon,
723 int x, int y, int z)
725 AddSortableSpriteToDraw (ti->vd, pylon, PAL_NONE, x, y, 1, 1,
726 BB_HEIGHT_UNDER_BRIDGE, z,
727 IsTransparencySet (TO_CATENARY), -1, -1);
731 * Draw a pylon at a tile side.
732 * @param ti The TileInfo struct of the tile being drawn.
733 * @param side Side where to draw the pylon.
734 * @param dir Pylon position point.
735 * @param pylon_base Pylon sprite base.
737 static void DrawPylon (const TileInfo *ti, DiagDirection side, Direction dir,
738 SpriteID pylon_base)
740 uint x = ti->x + x_pcp_offsets[side] + x_ppp_offsets[dir];
741 uint y = ti->y + y_pcp_offsets[side] + y_ppp_offsets[dir];
743 /* The elevation of the "pylon"-sprite should be the elevation
744 * at the PCP. PCPs are always on a tile edge.
746 * This position can be outside of the tile, i.e.
747 * ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
748 * So we have to move it inside the tile, because if the neighboured
749 * tile has a foundation, that does not smoothly connect to the
750 * current tile, we will get a wrong elevation from GetSlopePixelZ().
752 * When we move the position inside the tile, we will get a wrong
753 * elevation if we have a slope. To catch all cases we round the Z
754 * position to the next (TILE_HEIGHT / 2). This will return the
755 * correct elevation for slopes and will also detect non-continuous
756 * elevation on edges.
758 * Also note that the result of GetSlopePixelZ() is very special on
759 * bridge-ramps.
762 TileIndex tile = ti->tile;
763 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));
764 int elevation = (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
766 AddPylonSprite (ti, pylon_base + pylon_sprites[dir], x, y, elevation);
770 * Add a wire sprite for a tile.
771 * @param ti The TileInfo struct of the tile being drawn.
772 * @param wire_base The base of the wire sprite to draw.
773 * @param sss The sprite data for the wire.
774 * @param config The configuration to use for the wire.
775 * @param z Base Z position of the sprite.
777 static inline void AddWireSprite (const TileInfo *ti, SpriteID wire_base,
778 const SortableSpriteStructM *sss, uint config, int z)
780 AddSortableSpriteToDraw (ti->vd,
781 wire_base + sss->image_offset[config - 1], PAL_NONE,
782 ti->x + sss->x_offset, ti->y + sss->y_offset,
783 sss->x_size, sss->y_size, 1,
784 z + sss->z_offset, IsTransparencySet (TO_CATENARY));
788 * Draws overhead wires and pylons for electric railways.
789 * @param ti The TileInfo struct of the tile being drawn.
790 * @param rti The rail type information of the rail.
791 * @param tracks Tracks on which to draw.
792 * @param wires Wires to draw.
793 * @param slope Slope of the track surface for pylon elision.
794 * @param draw_pylons Whether to draw pylons (some stations disable this).
795 * @param draw_wires Whether to draw wires (some stations disable this).
796 * @param context Tile context for GetWireBase and GetPylonBase.
797 * @param bridge Bridge direction, if any.
799 static void DrawRailCatenary (const TileInfo *ti, const RailtypeInfo *rti,
800 TrackBits tracks, TrackBits wires, Slope slope,
801 bool draw_pylons, bool draw_wires, TileContext context = TCX_NORMAL,
802 DiagDirection bridge = INVALID_DIAGDIR)
804 bool odd[AXIS_END];
805 odd[AXIS_X] = IsOddX(ti->tile);
806 odd[AXIS_Y] = IsOddY(ti->tile);
808 byte pcp_status = 0;
810 SpriteID pylon_base = GetPylonBase (rti, ti->tile, context);
812 for (DiagDirection side = DIAGDIR_BEGIN; side < DIAGDIR_END; side++) {
813 bool pcp_neighbour;
814 byte ppp_allowed;
815 if (side != bridge) {
816 std::pair <uint, byte> pcp_state = CheckSidePCP (ti->tile,
817 tracks, wires, slope, side, odd);
818 if (pcp_state.first == PCP_NONE) continue;
819 pcp_neighbour = (pcp_state.first == PCP_IN_USE_BOTH);
820 ppp_allowed = pcp_state.second;
821 SetBit(pcp_status, side);
822 } else {
823 /* Bridge tile. */
824 TrackBits bridge_tracks = DiagdirReachesTracks (ReverseDiagDir (side));
825 if ((tracks & bridge_tracks) == TRACK_BIT_NONE) continue;
826 SetBit(pcp_status, side);
827 /* Pylon is drawn by the middle part if there is any. */
828 if (GetTunnelBridgeLength (ti->tile, GetOtherBridgeEnd (ti->tile)) > 0) continue;
829 pcp_neighbour = true;
830 ppp_allowed = AllowedPPPonPCP[side];
833 if (!draw_pylons) continue;
835 if (HasBridgeAbove(ti->tile)) {
836 if (GetBridgeAxis (ti->tile) == DiagDirToAxis (side)) {
837 int height = GetBridgeHeight (GetNorthernBridgeEnd (ti->tile));
838 if (height <= GetTileMaxZ (ti->tile) + 1) {
839 continue;
844 int pos = ChoosePylonPosition (side, ppp_allowed,
845 odd[AXIS_X], odd[AXIS_Y], pcp_neighbour);
846 if (pos >= 0) {
847 DrawPylon (ti, side, (Direction)pos, pylon_base);
851 /* Don't draw a wire if the station tile does not want any */
852 if (!draw_wires) return;
854 /* Don't draw a wire under a low bridge */
855 if (HasBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
856 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
858 if (height <= GetTileMaxZ(ti->tile) + 1) return;
861 /* Drawing of pylons is finished, now draw the wires */
862 SpriteID wire_base = GetWireBase (rti, ti->tile, context);
864 Track t;
865 FOR_EACH_SET_TRACK(t, wires) {
866 /* Map a track bit onto its two tile sides. */
867 static const byte track_sides[TRACK_END][2] = {
868 {DIAGDIR_NE, DIAGDIR_SW}, // X
869 {DIAGDIR_SE, DIAGDIR_NW}, // Y
870 {DIAGDIR_NW, DIAGDIR_NE}, // UPPER
871 {DIAGDIR_SE, DIAGDIR_SW}, // LOWER
872 {DIAGDIR_SW, DIAGDIR_NW}, // LEFT
873 {DIAGDIR_NE, DIAGDIR_SE}, // RIGHT
876 byte pcp_config = HasBit(pcp_status, track_sides[t][0]) +
877 (HasBit(pcp_status, track_sides[t][1]) << 1);
879 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)
880 assert(!IsSteepSlope(slope));
882 const SortableSpriteStructM *sss;
883 switch (slope) {
884 case SLOPE_SW: sss = &CatenarySpriteDataSW; break;
885 case SLOPE_SE: sss = &CatenarySpriteDataSE; break;
886 case SLOPE_NW: sss = &CatenarySpriteDataNW; break;
887 case SLOPE_NE: sss = &CatenarySpriteDataNE; break;
888 default: sss = &CatenarySpriteData[t]; break;
892 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
893 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
894 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
896 AddWireSprite (ti, wire_base, sss, pcp_config,
897 GetSlopePixelZ (ti->x + sss->x_offset, ti->y + sss->y_offset));
902 * Draws overhead wires and pylons for electric railways.
903 * @param ti The TileInfo struct of the tile being drawn
905 void DrawRailwayCatenary (const TileInfo *ti)
907 assert (IsRailwayTile (ti->tile));
909 /* Find which rail bits are present, and select the override points. */
910 DiagDirection overridePCP = IsTileSubtype (ti->tile, TT_BRIDGE) ? GetTunnelBridgeDirection (ti->tile) : INVALID_DIAGDIR;
911 TrackBits tracks = GetElectrifiedTrackBits (ti->tile);
912 TrackBits wires = MaskWireBits (ti->tile, tracks);
914 /* Note that ti->tileh has already been adjusted for Foundations */
915 Slope slope = ti->tileh;
917 const RailtypeInfo *rti, *halftile_rti;
918 Track halftile_track;
919 TileContext halftile_context;
920 if (IsHalftileSlope (slope)) {
921 switch (GetHalftileSlopeCorner (slope)) {
922 default: NOT_REACHED();
923 case CORNER_W: halftile_track = TRACK_LEFT; break;
924 case CORNER_S: halftile_track = TRACK_LOWER; break;
925 case CORNER_E: halftile_track = TRACK_RIGHT; break;
926 case CORNER_N: halftile_track = TRACK_UPPER; break;
928 halftile_rti = GetRailTypeInfo (GetRailType (ti->tile, halftile_track));
929 halftile_context = TCX_UPPER_HALFTILE;
930 Track opposite = TrackToOppositeTrack (halftile_track);
931 rti = !HasBit(tracks, opposite) ? NULL :
932 GetRailTypeInfo (GetRailType (ti->tile, opposite));
933 slope = SLOPE_FLAT;
934 } else {
935 RailType rt1 = GetRailType (ti->tile, TRACK_UPPER);
936 RailType rt2 = GetRailType (ti->tile, TRACK_LOWER);
937 rti = GetRailTypeInfo (rt1);
938 if (rt1 == rt2) {
939 halftile_track = INVALID_TRACK;
940 } else {
941 const RailtypeInfo *rti2 = GetRailTypeInfo (rt2);
942 switch (tracks) {
943 case TRACK_BIT_HORZ:
944 halftile_rti = rti2;
945 halftile_track = TRACK_LOWER;
946 break;
947 case TRACK_BIT_VERT:
948 halftile_rti = rti2;
949 halftile_track = TRACK_RIGHT;
950 break;
951 case TRACK_BIT_LOWER:
952 case TRACK_BIT_RIGHT:
953 rti = rti2;
954 /* fall through */
955 default: // TRACK_BIT_UPPER or TRACK_BIT_LEFT
956 halftile_track = INVALID_TRACK;
957 break;
960 halftile_context = TCX_NORMAL;
963 if (halftile_track != INVALID_TRACK) {
964 TrackBits tracks = TrackToTrackBits (halftile_track);
965 if (HasRailCatenary (halftile_rti)) {
966 DrawRailCatenary (ti, halftile_rti, tracks, tracks,
967 SLOPE_FLAT, true, true,
968 halftile_context);
970 if (rti == NULL) return;
971 tracks &= ~tracks;
972 wires = tracks;
975 if (HasRailCatenary (rti)) {
976 DrawRailCatenary (ti, rti, tracks, wires, slope, true, true,
977 TCX_NORMAL, overridePCP);
982 * Draws overhead wires and pylons on a normal (non-custom) bridge head.
983 * @param ti The TileInfo struct of the tile being drawn.
984 * @param rti The rail type information of the rail.
985 * @param dir The direction of the bridge.
987 void DrawRailBridgeHeadCatenary (const TileInfo *ti, const RailtypeInfo *rti,
988 DiagDirection dir)
990 TrackBits tracks = DiagDirToDiagTrackBits (dir);
992 DrawRailCatenary (ti, rti, tracks, tracks, (ti->tileh != SLOPE_FLAT) ?
993 SLOPE_FLAT : InclinedSlope (dir),
994 true, true, TCX_NORMAL, dir);
998 * Draws overhead wires and pylons for electric railways along an axis
999 * (for crossings and station tiles).
1000 * @param ti The TileInfo struct of the tile being drawn.
1001 * @param rti The rail type information of the rail.
1002 * @param axis The axis along which to draw the wire.
1003 * @param draw_pylons Whether to draw pylons (some stations disable this).
1004 * @param draw_wire Whether to draw the wire (some stations disable this).
1006 void DrawRailAxisCatenary (const TileInfo *ti, const RailtypeInfo *rti,
1007 Axis axis, bool draw_pylons, bool draw_wire)
1009 /* Note that ti->tileh has already been adjusted for Foundations */
1010 assert (ti->tileh == SLOPE_FLAT);
1012 TrackBits tracks = AxisToTrackBits (axis);
1013 DrawRailCatenary (ti, rti, tracks, tracks, SLOPE_FLAT, draw_pylons, draw_wire);
1017 * Draws overhead wires and pylons at a tunnel entrance.
1018 * @param ti The TileInfo struct of the tile being drawn.
1019 * @param dir The direction of the tunnel.
1021 void DrawRailTunnelCatenary (const TileInfo *ti, DiagDirection dir)
1023 /* Draw pylon. */
1024 TileIndex tile = ti->tile;
1025 DiagDirection rev = ReverseDiagDir (dir);
1027 byte dummy_preferred, dummy_allowed;
1028 Slope dummy_slope;
1029 bool pcp_neighbour = CheckNeighbourPCP (tile + TileOffsByDiagDir (rev),
1030 dir, &dummy_preferred, &dummy_allowed, &dummy_slope);
1032 int pos = ChoosePylonPosition (rev, AllowedPPPonPCP[rev],
1033 IsOddX(tile), IsOddY(tile), pcp_neighbour);
1035 const RailtypeInfo *rti = GetRailTypeInfo (GetRailType (tile));
1036 if (pos >= 0) {
1037 DrawPylon (ti, rev, (Direction)pos,
1038 GetPylonBase (rti, tile, TCX_NORMAL));
1041 /* Draw wire. */
1042 StartSpriteCombine (ti->vd);
1043 DrawRailTunnelDepotCatenary (ti, rti, false, dir);
1047 * Draws wires on a tunnel tile
1049 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
1051 * @param ti The Tileinfo to draw the tile for
1053 void DrawRailCatenaryOnBridge(const TileInfo *ti)
1055 TileIndex start = GetNorthernBridgeEnd(ti->tile);
1056 bool odd = ((GetTunnelBridgeLength (ti->tile, start) + 1) % 2) != 0;
1058 TileIndex end = GetSouthernBridgeEnd(ti->tile);
1059 bool last = (GetTunnelBridgeLength (ti->tile, end) == 0);
1061 const RailtypeInfo *rti = GetRailTypeInfo (GetBridgeRailType (end));
1063 Axis axis = GetBridgeAxis(ti->tile);
1065 uint config;
1066 if (odd && last) {
1067 /* Draw the "short" wire on the southern end of the bridge
1068 * only needed if the length of the bridge is odd */
1069 config = 3;
1070 } else {
1071 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
1072 config = 2 - odd;
1075 uint height = GetBridgePixelHeight(end);
1077 AddWireSprite (ti, GetWireBase (rti, end, TCX_ON_BRIDGE),
1078 &CatenarySpriteData[AxisToTrack(axis)], config, height);
1080 /* Finished with wires, draw pylons */
1081 if (!odd && !last) return; /* no pylons to draw */
1083 DiagDirection PCPpos;
1084 Direction PPPpos;
1085 if (axis == AXIS_X) {
1086 PCPpos = DIAGDIR_NE;
1087 PPPpos = IsOddY(ti->tile) ? DIR_SE : DIR_NW;
1088 } else {
1089 PCPpos = DIAGDIR_NW;
1090 PPPpos = IsOddX(ti->tile) ? DIR_SW : DIR_NE;
1093 SpriteID pylon = GetPylonBase (rti, end, TCX_ON_BRIDGE) + pylon_sprites[PPPpos];
1094 uint x = ti->x + x_ppp_offsets[PPPpos];
1095 uint y = ti->y + y_ppp_offsets[PPPpos];
1097 /* every other tile needs a pylon on the northern end */
1098 if (odd) {
1099 AddPylonSprite (ti, pylon, x + x_pcp_offsets[PCPpos],
1100 y + y_pcp_offsets[PCPpos], height);
1103 /* need a pylon on the southern end of the bridge */
1104 if (last) {
1105 PCPpos = ReverseDiagDir(PCPpos);
1106 AddPylonSprite (ti, pylon, x + x_pcp_offsets[PCPpos],
1107 y + y_pcp_offsets[PCPpos], height);
1111 bool SettingsDisableElrail(int32 p1)
1113 Company *c;
1114 Train *t;
1115 bool disable = (p1 != 0);
1117 /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
1118 const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
1119 const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
1121 /* walk through all train engines */
1122 Engine *e;
1123 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
1124 RailVehicleInfo *rv_info = &e->u.rail;
1125 /* if it is an electric rail engine and its railtype is the wrong one */
1126 if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
1127 /* change it to the proper one */
1128 rv_info->railtype = new_railtype;
1132 /* when disabling elrails, make sure that all existing trains can run on
1133 * normal rail too */
1134 if (disable) {
1135 FOR_ALL_TRAINS(t) {
1136 if (t->railtype == RAILTYPE_ELECTRIC) {
1137 /* this railroad vehicle is now compatible only with elrail,
1138 * so add there also normal rail compatibility */
1139 t->compatible_railtypes |= RAILTYPES_RAIL;
1140 t->railtype = RAILTYPE_RAIL;
1141 SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
1146 /* Fix the total power and acceleration for trains */
1147 FOR_ALL_TRAINS(t) {
1148 /* power and acceleration is cached only for front engines */
1149 if (t->IsFrontEngine()) {
1150 t->ConsistChanged(CCF_TRACK);
1154 FOR_ALL_COMPANIES(c) c->avail_railtypes = GetCompanyRailtypes(c->index);
1156 /* This resets the _last_built_railtype, which will be invalid for electric
1157 * rails. It may have unintended consequences if that function is ever
1158 * extended, though. */
1159 ReinitGuiAfterToggleElrail(disable);
1160 return true;