Simplify DisallowedPPPofTrackAtPCP
[openttd/fttd.git] / src / elrail.cpp
blob6f9e5f6961eee343417e4ef49c28805a4c5d728d
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"
58 #include "map/rail.h"
59 #include "map/road.h"
60 #include "map/slope.h"
61 #include "map/bridge.h"
62 #include "map/tunnelbridge.h"
63 #include "viewport_func.h"
64 #include "train.h"
65 #include "rail_gui.h"
66 #include "bridge.h"
67 #include "elrail_func.h"
68 #include "company_base.h"
69 #include "newgrf_railtype.h"
70 #include "station_func.h"
72 #include "table/elrail_data.h"
74 /**
75 * Get the tile location group of a tile.
76 * @param t The tile to get the tile location group of.
77 * @return The tile location group.
79 static inline TLG GetTLG(TileIndex t)
81 return (TLG)((HasBit(TileX(t), 0) << 1) + HasBit(TileY(t), 0));
84 /**
85 * Finds which Electrified Rail Bits are present on a given tile.
86 * @param t tile to check
87 * @param dir direction this tile is from the home tile, or INVALID_TILE for the home tile itself
88 * @param override pointer to PCP override, can be NULL
89 * @return trackbits of tile if it is electrified
91 static TrackBits GetRailTrackBitsUniversal(TileIndex t, DiagDirection dir, byte *override = NULL)
93 switch (GetTileType(t)) {
94 case TT_RAILWAY: {
95 TrackBits present = GetTrackBits(t);
96 TrackBits result = TRACK_BIT_NONE;
97 if (HasCatenary(GetRailType(t, TRACK_UPPER))) result |= present & (TRACK_BIT_CROSS | TRACK_BIT_UPPER | TRACK_BIT_LEFT);
98 if (HasCatenary(GetRailType(t, TRACK_LOWER))) result |= present & (TRACK_BIT_LOWER | TRACK_BIT_RIGHT);
100 if (IsTileSubtype(t, TT_BRIDGE) && (override != NULL) && GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0) {
101 *override = 1 << GetTunnelBridgeDirection(t);
104 return result;
107 case TT_MISC:
108 switch (GetTileSubtype(t)) {
109 default: return TRACK_BIT_NONE;
111 case TT_MISC_CROSSING:
112 if (!HasCatenary(GetRailType(t))) return TRACK_BIT_NONE;
113 return GetCrossingRailBits(t);
115 case TT_MISC_TUNNEL:
116 if (GetTunnelTransportType(t) != TRANSPORT_RAIL) return TRACK_BIT_NONE;
117 if (!HasCatenary(GetRailType(t))) return TRACK_BIT_NONE;
118 /* ignore tunnels facing the wrong way for neighbouring tiles */
119 if (dir != INVALID_DIAGDIR && dir != GetTunnelBridgeDirection(t)) return TRACK_BIT_NONE;
120 if (override != NULL) {
121 *override = 1 << GetTunnelBridgeDirection(t);
123 return DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t));
126 case TT_STATION:
127 if (!HasStationRail(t)) return TRACK_BIT_NONE;
128 if (!HasCatenary(GetRailType(t))) return TRACK_BIT_NONE;
129 /* Ignore neighbouring station tiles that allow neither wires nor pylons. */
130 if (dir != INVALID_DIAGDIR && !CanStationTileHavePylons(t) && !CanStationTileHaveWires(t)) return TRACK_BIT_NONE;
131 return TrackToTrackBits(GetRailStationTrack(t));
133 default:
134 return TRACK_BIT_NONE;
139 * Masks out track bits when neighbouring tiles are unelectrified.
141 static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
143 if (!IsNormalRailTile(t)) return tracks;
145 TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
146 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
147 /* If the neighbour tile is either not electrified or has no tracks that can be reached
148 * from this tile, mark all trackdirs that can be reached from the neighbour tile
149 * as needing no catenary. We make an exception for blocked station tiles with a matching
150 * axis that still display wires to preserve visual continuity. */
151 TileIndex next_tile = TileAddByDiagDir(t, d);
152 TrackBits reachable = TrackStatusToTrackBits(GetTileRailwayStatus(next_tile)) & DiagdirReachesTracks(d);
153 RailType rt;
154 if ((reachable != TRACK_BIT_NONE) ?
155 ((rt = GetRailType(next_tile, FindFirstTrack(reachable))) == INVALID_RAILTYPE || !HasCatenary(rt)) :
156 (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile))) {
157 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
161 /* If the tracks from either a diagonal crossing or don't overlap, both
162 * trackdirs have to be marked to mask the corresponding track bit. Else
163 * one marked trackdir is enough the mask the track bit. */
164 TrackBits mask;
165 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
166 /* If the tracks form either a diagonal crossing or don't overlap, both
167 * trackdirs have to be marked to mask the corresponding track bit. */
168 mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
169 /* If that results in no masked tracks and it is not a diagonal crossing,
170 * require only one marked trackdir to mask. */
171 if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
172 } else {
173 /* Require only one marked trackdir to mask the track. */
174 mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
175 /* If that results in an empty set, require both trackdirs for diagonal track. */
176 if ((tracks & mask) == TRACK_BIT_NONE) {
177 if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
178 if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
179 /* If that still is not enough, require both trackdirs for any track. */
180 if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
184 /* Mask the tracks only if at least one track bit would remain. */
185 return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
189 * Get the base wire sprite to use.
191 static inline SpriteID GetWireBase(TileIndex tile, TileContext context = TCX_NORMAL, Track track = INVALID_TRACK)
193 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile, track));
194 SpriteID wires = GetCustomRailSprite(rti, tile, RTSG_WIRES, context);
195 return wires == 0 ? SPR_WIRE_BASE : wires;
199 * Get the base pylon sprite to use.
201 static inline SpriteID GetPylonBase(TileIndex tile, TileContext context = TCX_NORMAL, Track track = INVALID_TRACK)
203 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile, track));
204 SpriteID pylons = GetCustomRailSprite(rti, tile, RTSG_PYLONS, context);
205 return pylons == 0 ? SPR_PYLON_BASE : pylons;
209 * Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
210 * @param tile The tile to analyse
211 * @param *tileh the tileh
213 static void AdjustTileh(TileIndex tile, Slope *tileh)
215 if (IsTunnelTile(tile)) {
216 *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
217 } else if (IsRailBridgeTile(tile) && !IsExtendedRailBridge(tile)) {
218 if (*tileh != SLOPE_FLAT) {
219 *tileh = SLOPE_FLAT;
220 } else {
221 *tileh = InclinedSlope(GetTunnelBridgeDirection(tile));
227 * Returns the Z position of a Pylon Control Point.
229 * @param tile The tile the pylon should stand on.
230 * @param PCPpos The PCP of the tile.
231 * @return The Z position of the PCP.
233 static int GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
235 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
236 * PCPs are always on a tile edge.
238 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
239 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
240 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
242 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
243 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
244 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
246 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
249 int z = GetSlopePixelZ(TileX(tile) * TILE_SIZE + min(x_pcp_offsets[PCPpos], TILE_SIZE - 1), TileY(tile) * TILE_SIZE + min(y_pcp_offsets[PCPpos], TILE_SIZE - 1));
250 return (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
254 * Draws wires on a tunnel tile
256 * DrawTile_TunnelBridge() calls this function to draw the wires as SpriteCombine with the tunnel roof.
258 * @param ti The Tileinfo to draw the tile for
260 void DrawCatenaryOnTunnel(const TileInfo *ti)
262 /* xmin, ymin, xmax + 1, ymax + 1 of BB */
263 static const int _tunnel_wire_BB[4][4] = {
264 { 0, 1, 16, 15 }, // NE
265 { 1, 0, 15, 16 }, // SE
266 { 0, 1, 16, 15 }, // SW
267 { 1, 0, 15, 16 }, // NW
270 DiagDirection dir = GetTunnelBridgeDirection(ti->tile);
272 SpriteID wire_base = GetWireBase(ti->tile);
274 const SortableSpriteStruct *sss = &CatenarySpriteData_TunnelDepot[dir];
275 const int *BB_data = _tunnel_wire_BB[dir];
276 AddSortableSpriteToDraw(
277 wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
278 BB_data[2] - sss->x_offset, BB_data[3] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset + 1,
279 GetTilePixelZ(ti->tile) + sss->z_offset,
280 IsTransparencySet(TO_CATENARY),
281 BB_data[0] - sss->x_offset, BB_data[1] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset
285 struct CatenaryConfig {
286 TrackBits tracks;
287 TrackBits wires;
288 bool isflat;
289 Slope tileh;
293 * Draws wires and, if required, pylons on a given tile
294 * @param ti The Tileinfo to draw the tile for
296 static void DrawCatenaryRailway(const TileInfo *ti)
298 /* Pylons are placed on a tile edge, so we need to take into account
299 * the track configuration of 2 adjacent tiles. home stores the
300 * current tile */
301 CatenaryConfig home;
302 /* Note that ti->tileh has already been adjusted for Foundations */
303 home.tileh = ti->tileh;
305 TLG tlg = GetTLG(ti->tile);
306 byte PCPstatus = 0;
307 byte OverridePCP = 0;
308 byte PPPpreferred[DIAGDIR_END];
309 byte PPPallowed[DIAGDIR_END];
311 /* Find which rail bits are present, and select the override points.
312 * We don't draw a pylon:
313 * 1) INSIDE a tunnel (we wouldn't see it anyway)
314 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
315 * because that one is drawn on the bridge. Exception is for length 0 bridges
316 * which have no middle tiles */
317 home.tracks = GetRailTrackBitsUniversal(ti->tile, INVALID_DIAGDIR, &OverridePCP);
318 home.wires = MaskWireBits(ti->tile, home.tracks);
319 /* If a track bit is present that is not in the main direction, the track is level */
320 home.isflat = ((home.tracks & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
322 /* Half tile slopes coincide only with horizontal/vertical track.
323 * Faking a flat slope results in the correct sprites on positions. */
324 Track halftile_track;
325 TileContext halftile_context;
326 if (IsHalftileSlope(home.tileh)) {
327 switch (GetHalftileSlopeCorner(home.tileh)) {
328 default: NOT_REACHED();
329 case CORNER_W: halftile_track = TRACK_LEFT; break;
330 case CORNER_S: halftile_track = TRACK_LOWER; break;
331 case CORNER_E: halftile_track = TRACK_RIGHT; break;
332 case CORNER_N: halftile_track = TRACK_UPPER; break;
334 halftile_context = TCX_UPPER_HALFTILE;
335 home.tileh = SLOPE_FLAT;
336 } else {
337 switch (home.tracks) {
338 case TRACK_BIT_LOWER:
339 case TRACK_BIT_HORZ:
340 halftile_track = GetRailType(ti->tile, TRACK_UPPER) == GetRailType(ti->tile, TRACK_LOWER) ? INVALID_TRACK : TRACK_LOWER;
341 break;
342 case TRACK_BIT_RIGHT:
343 case TRACK_BIT_VERT:
344 halftile_track = GetRailType(ti->tile, TRACK_LEFT) == GetRailType(ti->tile, TRACK_RIGHT) ? INVALID_TRACK : TRACK_RIGHT;
345 break;
346 default:
347 halftile_track = INVALID_TRACK;
348 break;
350 halftile_context = TCX_NORMAL;
353 AdjustTileh(ti->tile, &home.tileh);
355 SpriteID sprite_normal, sprite_halftile;
357 if (halftile_track == INVALID_TRACK) {
358 sprite_normal = GetPylonBase(ti->tile, TCX_NORMAL);
359 } else {
360 sprite_halftile = GetPylonBase(ti->tile, halftile_context, halftile_track);
361 sprite_normal = GetPylonBase(ti->tile, TCX_NORMAL,
362 HasBit(home.tracks, TrackToOppositeTrack(halftile_track)) ? TrackToOppositeTrack(halftile_track) : halftile_track);
365 for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
366 static const TrackBits edge_tracks[] = {
367 TRACK_BIT_UPPER | TRACK_BIT_RIGHT, // DIAGDIR_NE
368 TRACK_BIT_LOWER | TRACK_BIT_RIGHT, // DIAGDIR_SE
369 TRACK_BIT_LOWER | TRACK_BIT_LEFT, // DIAGDIR_SW
370 TRACK_BIT_UPPER | TRACK_BIT_LEFT, // DIAGDIR_NW
372 SpriteID pylon_base = (halftile_track != INVALID_TRACK && HasBit(edge_tracks[i], halftile_track)) ? sprite_halftile : sprite_normal;
373 TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
374 int elevation = GetPCPElevation(ti->tile, i);
375 CatenaryConfig nbconfig;
377 /* Here's one of the main headaches. GetTileSlope does not correct for possibly
378 * existing foundataions, so we do have to do that manually later on.*/
379 nbconfig.tileh = GetTileSlope(neighbour);
380 nbconfig.tracks = GetRailTrackBitsUniversal(neighbour, i);
382 /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
383 * we have to draw all pillars on the current tile. */
384 if (nbconfig.tracks == TRACK_BIT_NONE || elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) {
385 nbconfig.tracks = TRACK_BIT_NONE;
386 nbconfig.wires = TRACK_BIT_NONE;
387 nbconfig.isflat = false;
388 } else {
389 nbconfig.wires = MaskWireBits(neighbour, nbconfig.tracks);
390 nbconfig.isflat = ((nbconfig.tracks & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
393 PPPpreferred[i] = 0xFF; // We start with preferring everything (end-of-line in any direction)
394 PPPallowed[i] = AllowedPPPonPCP[i];
396 /* We cycle through all the existing tracks at a PCP and see what
397 * PPPs we want to have, or may not have at all */
399 /* Tracks inciding from the home tile */
400 for (uint k = 0; k < NUM_TRACKS_PER_SIDE; k++) {
401 /* We check whether the track in question (k) is present in the tile */
402 Track track = TracksAtTileSide[i][k];
403 if (HasBit(home.wires, track)) {
404 /* track found */
405 SetBit(PCPstatus, i); // This PCP is in use
406 PPPpreferred[i] &= PreferredPPPofTrackAtPCP[track][i];
408 if (HasBit(home.tracks, track)) {
409 PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[track];
413 /* Tracks inciding from the neighbour tile */
414 for (uint k = 0; k < NUM_TRACKS_PER_SIDE; k++) {
415 DiagDirection PCPpos = ReverseDiagDir(i);
416 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
417 if (IsRailBridgeTile(neighbour) && GetTunnelBridgeDirection(neighbour) == PCPpos) {
418 continue;
421 /* We check whether the track in question (k) is present in the tile */
422 Track track = TracksAtTileSide[PCPpos][k];
423 if (HasBit(nbconfig.wires, track)) {
424 /* track found, adjust the number
425 * of the PCP for preferred/allowed determination*/
426 SetBit(PCPstatus, i); // This PCP is in use
427 PPPpreferred[i] &= PreferredPPPofTrackAtPCP[track][PCPpos];
430 if (HasBit(nbconfig.tracks, track)) {
431 PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[track];
435 /* Deactivate all PPPs if PCP is not used */
436 if (!HasBit(PCPstatus, i)) {
437 PPPpreferred[i] = 0;
438 PPPallowed[i] = 0;
441 Foundation foundation = FOUNDATION_NONE;
443 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
444 if (IsStationTile(neighbour) || IsLevelCrossingTile(neighbour)) nbconfig.tileh = SLOPE_FLAT;
446 /* Read the foundations if they are present, and adjust the tileh */
447 if (nbconfig.tracks != TRACK_BIT_NONE && (IsNormalRailTile(neighbour) || IsRailDepotTile(neighbour))) {
448 foundation = GetRailFoundation(nbconfig.tileh, nbconfig.tracks);
450 if (IsRailBridgeTile(neighbour)) {
451 foundation = GetBridgeFoundation(nbconfig.tileh, DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
454 ApplyFoundationToSlope(foundation, &nbconfig.tileh);
456 /* Half tile slopes coincide only with horizontal/vertical track.
457 * Faking a flat slope results in the correct sprites on positions. */
458 if (IsHalftileSlope(nbconfig.tileh)) nbconfig.tileh = SLOPE_FLAT;
460 AdjustTileh(neighbour, &nbconfig.tileh);
462 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
463 * Delete the PCP if this is the case.
464 * Level means that the slope is the same, or the track is flat */
465 if (home.tileh == nbconfig.tileh || (home.isflat && nbconfig.isflat)) {
466 for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
467 if (PPPpreferred[i] == IgnoredPCP[tlg][i][k]) ClrBit(PCPstatus, i);
471 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
472 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
473 * anything. Note that the preferred PPPs still contain the end-of-line markers.
474 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
475 if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
477 if (HasBridgeAbove(ti->tile)) {
478 Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
479 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
481 if ((height <= GetTileMaxZ(ti->tile) + 1) &&
482 (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
483 SetBit(OverridePCP, i);
487 if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i) &&
488 (!IsRailStationTile(ti->tile) || CanStationTileHavePylons(ti->tile))) {
489 for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
490 byte temp = PPPorder[i][GetTLG(ti->tile)][k];
492 if (HasBit(PPPallowed[i], temp)) {
493 uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
494 uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
496 /* Don't build the pylon if it would be outside the tile */
497 if (!HasBit(OwnedPPPonPCP[i], temp)) {
498 /* We have a neighbour that will draw it, bail out */
499 if (nbconfig.tracks != TRACK_BIT_NONE) break;
500 continue; // No neighbour, go looking for a better position
503 AddSortableSpriteToDraw(pylon_base + pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
504 elevation, IsTransparencySet(TO_CATENARY), -1, -1);
506 break; // We already have drawn a pylon, bail out
512 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawCatenaryOnTunnel()) */
513 if (IsTunnelTile(ti->tile)) return;
515 /* Don't draw a wire under a low bridge */
516 if (HasBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
517 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
519 if (height <= GetTileMaxZ(ti->tile) + 1) return;
522 /* Don't draw a wire if the station tile does not want any */
523 if (IsRailStationTile(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
525 if (halftile_track == INVALID_TRACK) {
526 sprite_normal = GetWireBase(ti->tile, TCX_NORMAL);
527 } else {
528 sprite_halftile = GetWireBase(ti->tile, halftile_context, halftile_track);
529 sprite_normal = GetWireBase(ti->tile, TCX_NORMAL,
530 HasBit(home.tracks, TrackToOppositeTrack(halftile_track)) ? TrackToOppositeTrack(halftile_track) : halftile_track);
533 /* Drawing of pylons is finished, now draw the wires */
534 Track t;
535 FOR_EACH_SET_TRACK(t, home.wires) {
536 byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
537 (HasBit(PCPstatus, PCPpositions[t][1]) << 1);
539 assert(PCPconfig != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
540 assert(!IsSteepSlope(home.tileh));
542 const SortableSpriteStructM *sss;
543 switch (home.tileh) {
544 case SLOPE_SW: sss = &CatenarySpriteDataSW; break;
545 case SLOPE_SE: sss = &CatenarySpriteDataSE; break;
546 case SLOPE_NW: sss = &CatenarySpriteDataNW; break;
547 case SLOPE_NE: sss = &CatenarySpriteDataNE; break;
548 default: sss = &CatenarySpriteData[t]; break;
552 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
553 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
554 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
556 SpriteID wire_base = (t == halftile_track) ? sprite_halftile : sprite_normal;
557 AddSortableSpriteToDraw(wire_base + sss->image_offset[PCPconfig], PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
558 sss->x_size, sss->y_size, sss->z_size, GetSlopePixelZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
559 IsTransparencySet(TO_CATENARY));
564 * Draws wires on a tunnel tile
566 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
568 * @param ti The Tileinfo to draw the tile for
570 void DrawCatenaryOnBridge(const TileInfo *ti)
572 TileIndex start = GetNorthernBridgeEnd(ti->tile);
573 TileIndex end = GetSouthernBridgeEnd(ti->tile);
575 uint length = GetTunnelBridgeLength(start, end);
576 uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
578 Axis axis = GetBridgeAxis(ti->tile);
580 const SortableSpriteStructM *sss = &CatenarySpriteData[AxisToTrack(axis)];
582 uint config;
583 if ((length % 2) && num == length) {
584 /* Draw the "short" wire on the southern end of the bridge
585 * only needed if the length of the bridge is odd */
586 config = 3;
587 } else {
588 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
589 config = 2 - (num % 2);
592 uint height = GetBridgePixelHeight(end);
594 SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
596 AddSortableSpriteToDraw(wire_base + sss->image_offset[config], PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
597 sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
598 IsTransparencySet(TO_CATENARY)
601 /* Finished with wires, draw pylons */
602 if ((num % 2) == 0 && num != length) return; /* no pylons to draw */
604 DiagDirection PCPpos;
605 Direction PPPpos;
606 if (axis == AXIS_X) {
607 PCPpos = DIAGDIR_NE;
608 PPPpos = HasBit(GetTLG(ti->tile), 0) ? DIR_SE : DIR_NW;
609 } else {
610 PCPpos = DIAGDIR_NW;
611 PPPpos = HasBit(GetTLG(ti->tile), 1) ? DIR_SW : DIR_NE;
614 SpriteID pylon = GetPylonBase(end, TCX_ON_BRIDGE) + pylon_sprites[PPPpos];
615 uint x = ti->x + x_ppp_offsets[PPPpos];
616 uint y = ti->y + y_ppp_offsets[PPPpos];
618 /* every other tile needs a pylon on the northern end */
619 if (num % 2) {
620 AddSortableSpriteToDraw(pylon, PAL_NONE, x + x_pcp_offsets[PCPpos], y + y_pcp_offsets[PCPpos],
621 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
624 /* need a pylon on the southern end of the bridge */
625 if (num == length) {
626 PCPpos = ReverseDiagDir(PCPpos);
627 AddSortableSpriteToDraw(pylon, PAL_NONE, x + x_pcp_offsets[PCPpos], y + y_pcp_offsets[PCPpos],
628 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
633 * Draws overhead wires and pylons for electric railways.
634 * @param ti The TileInfo struct of the tile being drawn
635 * @see DrawCatenaryRailway
637 void DrawCatenary(const TileInfo *ti)
639 switch (GetTileType(ti->tile)) {
640 case TT_MISC:
641 switch (GetTileSubtype(ti->tile)) {
642 default: return;
644 case TT_MISC_DEPOT: {
645 if (!IsRailDepot(ti->tile)) return;
646 const SortableSpriteStruct *sss = &CatenarySpriteData_TunnelDepot[GetGroundDepotDirection(ti->tile)];
647 SpriteID wire_base = GetWireBase(ti->tile);
649 /* This wire is not visible with the default depot sprites */
650 AddSortableSpriteToDraw(
651 wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
652 sss->x_size, sss->y_size, sss->z_size,
653 GetTileMaxPixelZ(ti->tile) + sss->z_offset,
654 IsTransparencySet(TO_CATENARY)
656 return;
659 case TT_MISC_CROSSING:
660 case TT_MISC_TUNNEL:
661 break;
663 break;
665 case TT_RAILWAY:
666 case TT_STATION:
667 break;
669 default: return;
671 DrawCatenaryRailway(ti);
674 bool SettingsDisableElrail(int32 p1)
676 Company *c;
677 Train *t;
678 bool disable = (p1 != 0);
680 /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
681 const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
682 const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
684 /* walk through all train engines */
685 Engine *e;
686 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
687 RailVehicleInfo *rv_info = &e->u.rail;
688 /* if it is an electric rail engine and its railtype is the wrong one */
689 if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
690 /* change it to the proper one */
691 rv_info->railtype = new_railtype;
695 /* when disabling elrails, make sure that all existing trains can run on
696 * normal rail too */
697 if (disable) {
698 FOR_ALL_TRAINS(t) {
699 if (t->railtype == RAILTYPE_ELECTRIC) {
700 /* this railroad vehicle is now compatible only with elrail,
701 * so add there also normal rail compatibility */
702 t->compatible_railtypes |= RAILTYPES_RAIL;
703 t->railtype = RAILTYPE_RAIL;
704 SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
709 /* Fix the total power and acceleration for trains */
710 FOR_ALL_TRAINS(t) {
711 /* power and acceleration is cached only for front engines */
712 if (t->IsFrontEngine()) {
713 t->ConsistChanged(true);
717 FOR_ALL_COMPANIES(c) c->avail_railtypes = GetCompanyRailtypes(c->index);
719 /* This resets the _last_built_railtype, which will be invalid for electric
720 * rails. It may have unintended consequences if that function is ever
721 * extended, though. */
722 ReinitGuiAfterToggleElrail(disable);
723 return true;