Fix 03cc0d6: Mark level crossings dirty when removing road from them, not from bridge...
[openttd-github.git] / src / elrail.cpp
blob03adb0c8c945e692c83b53dc169071fb16a3690b
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /**
9 * @file elrail.cpp
10 * This file deals with displaying wires and pylons for electric railways.
11 * <h2>Basics</h2>
13 * <h3>Tile Types</h3>
15 * We have two different types of tiles in the drawing code:
16 * Normal Railway Tiles (NRTs) which can have more than one track on it, and
17 * Special Railways tiles (SRTs) which have only one track (like crossings, depots
18 * stations, etc).
20 * <h3>Location Categories</h3>
22 * All tiles are categorized into three location groups (TLG):
23 * Group 0: Tiles with both an even X coordinate and an even Y coordinate
24 * Group 1: Tiles with an even X and an odd Y coordinate
25 * Group 2: Tiles with an odd X and an even Y coordinate
26 * Group 3: Tiles with both an odd X and Y coordinate.
28 * <h3>Pylon Points</h3>
29 * <h4>Control Points</h4>
30 * A Pylon Control Point (PCP) is a position where a wire (or rather two)
31 * is mounted onto a pylon.
32 * Each NRT does contain 4 PCPs which are bitmapped to a byte
33 * variable and are represented by the DiagDirection enum
35 * Each track ends on two PCPs and thus requires one pylon on each end. However,
36 * there is one exception: Straight-and-level tracks only have one pylon every
37 * other tile.
39 * Now on each edge there are two PCPs: One from each adjacent tile. Both PCPs
40 * are merged using an OR operation (i. e. if one tile needs a PCP at the position
41 * in question, both tiles get it).
43 * <h4>Position Points</h4>
44 * A Pylon Position Point (PPP) is a position where a pylon is located on the
45 * ground. Each PCP owns 8 in (45 degree steps) PPPs that are located around
46 * it. PPPs are represented using the Direction enum. Each track bit has PPPs
47 * that are impossible (because the pylon would be situated on the track) and
48 * some that are preferred (because the pylon would be rectangular to the track).
50 * @image html elrail_tile.png
51 * @image html elrail_track.png
55 #include "stdafx.h"
56 #include "station_map.h"
57 #include "viewport_func.h"
58 #include "train.h"
59 #include "rail_gui.h"
60 #include "tunnelbridge_map.h"
61 #include "tunnelbridge.h"
62 #include "elrail_func.h"
63 #include "company_base.h"
64 #include "newgrf_railtype.h"
66 #include "table/elrail_data.h"
68 #include "safeguards.h"
70 /**
71 * Get the tile location group of a tile.
72 * @param t The tile to get the tile location group of.
73 * @return The tile location group.
75 static inline TLG GetTLG(TileIndex t)
77 return (TLG)((HasBit(TileX(t), 0) << 1) + HasBit(TileY(t), 0));
80 /**
81 * Finds which Electrified Rail Bits are present on a given tile.
82 * @param t tile to check
83 * @param override pointer to PCP override, can be nullptr
84 * @return trackbits of tile if it is electrified
86 static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
88 switch (GetTileType(t)) {
89 case MP_RAILWAY:
90 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
91 switch (GetRailTileType(t)) {
92 case RAIL_TILE_NORMAL: case RAIL_TILE_SIGNALS:
93 return GetTrackBits(t);
94 default:
95 return TRACK_BIT_NONE;
97 break;
99 case MP_TUNNELBRIDGE:
100 if (GetTunnelBridgeTransportType(t) != TRANSPORT_RAIL) return TRACK_BIT_NONE;
101 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
102 if (override != nullptr && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
103 *override = 1 << GetTunnelBridgeDirection(t);
105 return DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t));
107 case MP_ROAD:
108 if (!IsLevelCrossing(t)) return TRACK_BIT_NONE;
109 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
110 return GetCrossingRailBits(t);
112 case MP_STATION:
113 if (!HasStationRail(t)) return TRACK_BIT_NONE;
114 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
115 return TrackToTrackBits(GetRailStationTrack(t));
117 default:
118 return TRACK_BIT_NONE;
123 * Masks out track bits when neighbouring tiles are unelectrified.
125 static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
127 /* Single track bits are never masked out. */
128 if (likely(HasAtMostOneBit(tracks))) return tracks;
130 if (!IsPlainRailTile(t)) return tracks;
132 TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
133 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
134 /* If the neighbour tile is either not electrified or has no tracks that can be reached
135 * from this tile, mark all trackdirs that can be reached from the neighbour tile
136 * as needing no catenary. We make an exception for blocked station tiles with a matching
137 * axis that still display wires to preserve visual continuity. */
138 TileIndex next_tile = TileAddByDiagDir(t, d);
139 RailType rt = GetTileRailType(next_tile);
140 if (rt == INVALID_RAILTYPE || !HasRailCatenary(rt) ||
141 ((TrackStatusToTrackBits(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTracks(d)) == TRACK_BIT_NONE &&
142 (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
143 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
147 /* If the tracks from either a diagonal crossing or don't overlap, both
148 * trackdirs have to be marked to mask the corresponding track bit. Else
149 * one marked trackdir is enough the mask the track bit. */
150 TrackBits mask;
151 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
152 /* If the tracks form either a diagonal crossing or don't overlap, both
153 * trackdirs have to be marked to mask the corresponding track bit. */
154 mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
155 /* If that results in no masked tracks and it is not a diagonal crossing,
156 * require only one marked trackdir to mask. */
157 if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
158 } else {
159 /* Require only one marked trackdir to mask the track. */
160 mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
161 /* If that results in an empty set, require both trackdirs for diagonal track. */
162 if ((tracks & mask) == TRACK_BIT_NONE) {
163 if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
164 if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
165 /* If that still is not enough, require both trackdirs for any track. */
166 if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
170 /* Mask the tracks only if at least one track bit would remain. */
171 return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
175 * Get the base wire sprite to use.
177 static inline SpriteID GetWireBase(TileIndex tile, TileContext context = TCX_NORMAL)
179 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
180 SpriteID wires = GetCustomRailSprite(rti, tile, RTSG_WIRES, context);
181 return wires == 0 ? SPR_WIRE_BASE : wires;
185 * Get the base pylon sprite to use.
187 static inline SpriteID GetPylonBase(TileIndex tile, TileContext context = TCX_NORMAL)
189 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
190 SpriteID pylons = GetCustomRailSprite(rti, tile, RTSG_PYLONS, context);
191 return pylons == 0 ? SPR_PYLON_BASE : pylons;
195 * Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
196 * @param tile The tile to analyse
197 * @param *tileh the tileh
199 static void AdjustTileh(TileIndex tile, Slope *tileh)
201 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
202 if (IsTunnel(tile)) {
203 *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
204 } else if (*tileh != SLOPE_FLAT) {
205 *tileh = SLOPE_FLAT;
206 } else {
207 *tileh = InclinedSlope(GetTunnelBridgeDirection(tile));
213 * Returns the Z position of a Pylon Control Point.
215 * @param tile The tile the pylon should stand on.
216 * @param PCPpos The PCP of the tile.
217 * @return The Z position of the PCP.
219 static int GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
221 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
222 * PCPs are always on a tile edge.
224 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
225 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
226 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
228 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
229 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
230 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
232 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
235 int z = GetSlopePixelZ(TileX(tile) * TILE_SIZE + std::min<int8>(x_pcp_offsets[PCPpos], TILE_SIZE - 1),
236 TileY(tile) * TILE_SIZE + std::min<int8>(y_pcp_offsets[PCPpos], TILE_SIZE - 1));
237 return (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
241 * Draws wires on a tunnel tile
243 * DrawTile_TunnelBridge() calls this function to draw the wires as SpriteCombine with the tunnel roof.
245 * @param ti The Tileinfo to draw the tile for
247 void DrawRailCatenaryOnTunnel(const TileInfo *ti)
249 /* xmin, ymin, xmax + 1, ymax + 1 of BB */
250 static const int _tunnel_wire_BB[4][4] = {
251 { 0, 1, 16, 15 }, // NE
252 { 1, 0, 15, 16 }, // SE
253 { 0, 1, 16, 15 }, // SW
254 { 1, 0, 15, 16 }, // NW
257 DiagDirection dir = GetTunnelBridgeDirection(ti->tile);
259 SpriteID wire_base = GetWireBase(ti->tile);
261 const SortableSpriteStruct *sss = &RailCatenarySpriteData_Tunnel[dir];
262 const int *BB_data = _tunnel_wire_BB[dir];
263 AddSortableSpriteToDraw(
264 wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
265 BB_data[2] - sss->x_offset, BB_data[3] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset + 1,
266 GetTilePixelZ(ti->tile) + sss->z_offset,
267 IsTransparencySet(TO_CATENARY),
268 BB_data[0] - sss->x_offset, BB_data[1] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset
273 * Draws wires and, if required, pylons on a given tile
274 * @param ti The Tileinfo to draw the tile for
276 static void DrawRailCatenaryRailway(const TileInfo *ti)
278 /* Pylons are placed on a tile edge, so we need to take into account
279 * the track configuration of 2 adjacent tiles. trackconfig[0] stores the
280 * current tile (home tile) while [1] holds the neighbour */
281 TrackBits trackconfig[TS_END];
282 TrackBits wireconfig[TS_END];
283 bool isflat[TS_END];
284 /* Note that ti->tileh has already been adjusted for Foundations */
285 Slope tileh[TS_END] = { ti->tileh, SLOPE_FLAT };
287 /* Half tile slopes coincide only with horizontal/vertical track.
288 * Faking a flat slope results in the correct sprites on positions. */
289 Corner halftile_corner = CORNER_INVALID;
290 if (IsHalftileSlope(tileh[TS_HOME])) {
291 halftile_corner = GetHalftileSlopeCorner(tileh[TS_HOME]);
292 tileh[TS_HOME] = SLOPE_FLAT;
295 TLG tlg = GetTLG(ti->tile);
296 byte PCPstatus = 0;
297 byte OverridePCP = 0;
298 byte PPPpreferred[DIAGDIR_END];
299 byte PPPallowed[DIAGDIR_END];
301 /* Find which rail bits are present, and select the override points.
302 * We don't draw a pylon:
303 * 1) INSIDE a tunnel (we wouldn't see it anyway)
304 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
305 * because that one is drawn on the bridge. Exception is for length 0 bridges
306 * which have no middle tiles */
307 trackconfig[TS_HOME] = GetRailTrackBitsUniversal(ti->tile, &OverridePCP);
308 wireconfig[TS_HOME] = MaskWireBits(ti->tile, trackconfig[TS_HOME]);
309 /* If a track bit is present that is not in the main direction, the track is level */
310 isflat[TS_HOME] = ((trackconfig[TS_HOME] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
312 AdjustTileh(ti->tile, &tileh[TS_HOME]);
314 SpriteID pylon_normal = GetPylonBase(ti->tile);
315 SpriteID pylon_halftile = (halftile_corner != CORNER_INVALID) ? GetPylonBase(ti->tile, TCX_UPPER_HALFTILE) : pylon_normal;
317 for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
318 static const uint edge_corners[] = {
319 1 << CORNER_N | 1 << CORNER_E, // DIAGDIR_NE
320 1 << CORNER_S | 1 << CORNER_E, // DIAGDIR_SE
321 1 << CORNER_S | 1 << CORNER_W, // DIAGDIR_SW
322 1 << CORNER_N | 1 << CORNER_W, // DIAGDIR_NW
324 SpriteID pylon_base = (halftile_corner != CORNER_INVALID && HasBit(edge_corners[i], halftile_corner)) ? pylon_halftile : pylon_normal;
325 TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
326 int elevation = GetPCPElevation(ti->tile, i);
328 /* Here's one of the main headaches. GetTileSlope does not correct for possibly
329 * existing foundataions, so we do have to do that manually later on.*/
330 tileh[TS_NEIGHBOUR] = GetTileSlope(neighbour);
331 trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, nullptr);
332 wireconfig[TS_NEIGHBOUR] = MaskWireBits(neighbour, trackconfig[TS_NEIGHBOUR]);
333 if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
335 /* Ignore station tiles that allow neither wires nor pylons. */
336 if (IsRailStationTile(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
338 /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
339 * we have to draw all pillars on the current tile. */
340 if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
342 isflat[TS_NEIGHBOUR] = ((trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
344 PPPpreferred[i] = 0xFF; // We start with preferring everything (end-of-line in any direction)
345 PPPallowed[i] = AllowedPPPonPCP[i];
347 /* We cycle through all the existing tracks at a PCP and see what
348 * PPPs we want to have, or may not have at all */
349 for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
350 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
351 if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
352 IsBridgeTile(neighbour) &&
353 GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
354 continue;
357 /* We check whether the track in question (k) is present in the tile
358 * (TrackSourceTile) */
359 DiagDirection PCPpos = i;
360 if (HasBit(wireconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
361 /* track found, if track is in the neighbour tile, adjust the number
362 * of the PCP for preferred/allowed determination*/
363 PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
364 SetBit(PCPstatus, i); // This PCP is in use
365 PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
368 if (HasBit(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
369 PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
373 /* Deactivate all PPPs if PCP is not used */
374 if (!HasBit(PCPstatus, i)) {
375 PPPpreferred[i] = 0;
376 PPPallowed[i] = 0;
379 Foundation foundation = FOUNDATION_NONE;
381 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
382 if (IsTileType(neighbour, MP_STATION) || IsTileType(neighbour, MP_ROAD)) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
384 /* Read the foundations if they are present, and adjust the tileh */
385 if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE && IsTileType(neighbour, MP_RAILWAY) && HasRailCatenary(GetRailType(neighbour))) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
386 if (IsBridgeTile(neighbour)) {
387 foundation = GetBridgeFoundation(tileh[TS_NEIGHBOUR], DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
390 ApplyFoundationToSlope(foundation, &tileh[TS_NEIGHBOUR]);
392 /* Half tile slopes coincide only with horizontal/vertical track.
393 * Faking a flat slope results in the correct sprites on positions. */
394 if (IsHalftileSlope(tileh[TS_NEIGHBOUR])) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
396 AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
398 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
399 * Delete the PCP if this is the case.
400 * Level means that the slope is the same, or the track is flat */
401 if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
402 for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
403 if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) ClrBit(PCPstatus, i);
407 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
408 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
409 * anything. Note that the preferred PPPs still contain the end-of-line markers.
410 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
411 if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
413 if (IsBridgeAbove(ti->tile)) {
414 Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
415 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
417 if ((height <= GetTileMaxZ(ti->tile) + 1) &&
418 (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
419 SetBit(OverridePCP, i);
423 if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i) &&
424 (!IsRailStationTile(ti->tile) || CanStationTileHavePylons(ti->tile))) {
425 for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
426 byte temp = PPPorder[i][GetTLG(ti->tile)][k];
428 if (HasBit(PPPallowed[i], temp)) {
429 uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
430 uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
432 /* Don't build the pylon if it would be outside the tile */
433 if (!HasBit(OwnedPPPonPCP[i], temp)) {
434 /* We have a neighbour that will draw it, bail out */
435 if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE) break;
436 continue; // No neighbour, go looking for a better position
439 AddSortableSpriteToDraw(pylon_base + pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
440 elevation, IsTransparencySet(TO_CATENARY), -1, -1);
442 break; // We already have drawn a pylon, bail out
448 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawRailCatenaryOnTunnel()) */
449 if (IsTunnelTile(ti->tile)) return;
451 /* Don't draw a wire under a low bridge */
452 if (IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
453 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
455 if (height <= GetTileMaxZ(ti->tile) + 1) return;
458 /* Don't draw a wire if the station tile does not want any */
459 if (IsRailStationTile(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
461 SpriteID wire_normal = GetWireBase(ti->tile);
462 SpriteID wire_halftile = (halftile_corner != CORNER_INVALID) ? GetWireBase(ti->tile, TCX_UPPER_HALFTILE) : wire_normal;
463 Track halftile_track;
464 switch (halftile_corner) {
465 case CORNER_W: halftile_track = TRACK_LEFT; break;
466 case CORNER_S: halftile_track = TRACK_LOWER; break;
467 case CORNER_E: halftile_track = TRACK_RIGHT; break;
468 case CORNER_N: halftile_track = TRACK_UPPER; break;
469 default: halftile_track = INVALID_TRACK; break;
472 /* Drawing of pylons is finished, now draw the wires */
473 for (Track t : SetTrackBitIterator(wireconfig[TS_HOME])) {
474 SpriteID wire_base = (t == halftile_track) ? wire_halftile : wire_normal;
475 byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
476 (HasBit(PCPstatus, PCPpositions[t][1]) << 1);
478 const SortableSpriteStruct *sss;
479 int tileh_selector = !(tileh[TS_HOME] % 3) * tileh[TS_HOME] / 3; // tileh for the slopes, 0 otherwise
481 assert(PCPconfig != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
482 assert(!IsSteepSlope(tileh[TS_HOME]));
483 sss = &RailCatenarySpriteData[Wires[tileh_selector][t][PCPconfig]];
486 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
487 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
488 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
490 AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
491 sss->x_size, sss->y_size, sss->z_size, GetSlopePixelZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
492 IsTransparencySet(TO_CATENARY));
497 * Draws wires on a tunnel tile
499 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
501 * @param ti The Tileinfo to draw the tile for
503 void DrawRailCatenaryOnBridge(const TileInfo *ti)
505 TileIndex end = GetSouthernBridgeEnd(ti->tile);
506 TileIndex start = GetOtherBridgeEnd(end);
508 uint length = GetTunnelBridgeLength(start, end);
509 uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
510 uint height;
512 const SortableSpriteStruct *sss;
513 Axis axis = GetBridgeAxis(ti->tile);
514 TLG tlg = GetTLG(ti->tile);
516 RailCatenarySprite offset = (RailCatenarySprite)(axis == AXIS_X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
518 if ((length % 2) && num == length) {
519 /* Draw the "short" wire on the southern end of the bridge
520 * only needed if the length of the bridge is odd */
521 sss = &RailCatenarySpriteData[WIRE_X_FLAT_BOTH + offset];
522 } else {
523 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
524 sss = &RailCatenarySpriteData[WIRE_X_FLAT_SW + (num % 2) + offset];
527 height = GetBridgePixelHeight(end);
529 SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
531 AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
532 sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
533 IsTransparencySet(TO_CATENARY)
536 SpriteID pylon_base = GetPylonBase(end, TCX_ON_BRIDGE);
538 /* Finished with wires, draw pylons
539 * every other tile needs a pylon on the northern end */
540 if (num % 2) {
541 DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_NE : DIAGDIR_NW);
542 Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
543 if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
544 uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
545 uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
546 AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
549 /* need a pylon on the southern end of the bridge */
550 if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
551 DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE);
552 Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
553 if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
554 uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
555 uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
556 AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
561 * Draws overhead wires and pylons for electric railways.
562 * @param ti The TileInfo struct of the tile being drawn
563 * @see DrawRailCatenaryRailway
565 void DrawRailCatenary(const TileInfo *ti)
567 switch (GetTileType(ti->tile)) {
568 case MP_RAILWAY:
569 if (IsRailDepot(ti->tile)) {
570 const SortableSpriteStruct *sss = &RailCatenarySpriteData_Depot[GetRailDepotDirection(ti->tile)];
572 SpriteID wire_base = GetWireBase(ti->tile);
574 /* This wire is not visible with the default depot sprites */
575 AddSortableSpriteToDraw(
576 wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
577 sss->x_size, sss->y_size, sss->z_size,
578 GetTileMaxPixelZ(ti->tile) + sss->z_offset,
579 IsTransparencySet(TO_CATENARY)
581 return;
583 break;
585 case MP_TUNNELBRIDGE:
586 case MP_ROAD:
587 case MP_STATION:
588 break;
590 default: return;
592 DrawRailCatenaryRailway(ti);
595 void SettingsDisableElrail(int32 new_value)
597 bool disable = (new_value != 0);
599 /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
600 const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
601 const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
603 /* walk through all train engines */
604 for (Engine *e : Engine::IterateType(VEH_TRAIN)) {
605 RailVehicleInfo *rv_info = &e->u.rail;
606 /* if it is an electric rail engine and its railtype is the wrong one */
607 if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
608 /* change it to the proper one */
609 rv_info->railtype = new_railtype;
613 /* when disabling elrails, make sure that all existing trains can run on
614 * normal rail too */
615 if (disable) {
616 for (Train *t : Train::Iterate()) {
617 if (t->railtype == RAILTYPE_ELECTRIC) {
618 /* this railroad vehicle is now compatible only with elrail,
619 * so add there also normal rail compatibility */
620 t->compatible_railtypes |= RAILTYPES_RAIL;
621 t->railtype = RAILTYPE_RAIL;
622 SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
627 /* Fix the total power and acceleration for trains */
628 for (Train *t : Train::Iterate()) {
629 /* power and acceleration is cached only for front engines */
630 if (t->IsFrontEngine()) {
631 t->ConsistChanged(CCF_TRACK);
635 for (Company *c : Company::Iterate()) c->avail_railtypes = GetCompanyRailtypes(c->index);
637 /* This resets the _last_built_railtype, which will be invalid for electric
638 * rails. It may have unintended consequences if that function is ever
639 * extended, though. */
640 ReinitGuiAfterToggleElrail(disable);