(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / elrail.cpp
blobac3616161727e45dbf5aa6d6d506e0fa34fa0933
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 "station_map.h"
59 #include "viewport_func.h"
60 #include "train.h"
61 #include "rail_gui.h"
62 #include "tunnelbridge_map.h"
63 #include "tunnelbridge.h"
64 #include "elrail_func.h"
65 #include "company_base.h"
66 #include "newgrf_railtype.h"
68 #include "table/elrail_data.h"
70 #include "safeguards.h"
72 /**
73 * Get the tile location group of a tile.
74 * @param t The tile to get the tile location group of.
75 * @return The tile location group.
77 static inline TLG GetTLG(TileIndex t)
79 return (TLG)((HasBit(TileX(t), 0) << 1) + HasBit(TileY(t), 0));
82 /**
83 * Finds which Electrified Rail Bits are present on a given tile.
84 * @param t tile to check
85 * @param override pointer to PCP override, can be NULL
86 * @return trackbits of tile if it is electrified
88 static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
90 switch (GetTileType(t)) {
91 case MP_RAILWAY:
92 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
93 switch (GetRailTileType(t)) {
94 case RAIL_TILE_NORMAL: case RAIL_TILE_SIGNALS:
95 return GetTrackBits(t);
96 default:
97 return TRACK_BIT_NONE;
99 break;
101 case MP_TUNNELBRIDGE:
102 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
103 if (override != NULL && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
104 *override = 1 << GetTunnelBridgeDirection(t);
106 return DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t));
108 case MP_ROAD:
109 if (!IsLevelCrossing(t)) return TRACK_BIT_NONE;
110 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
111 return GetCrossingRailBits(t);
113 case MP_STATION:
114 if (!HasStationRail(t)) return TRACK_BIT_NONE;
115 if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
116 return TrackToTrackBits(GetRailStationTrack(t));
118 default:
119 return TRACK_BIT_NONE;
124 * Masks out track bits when neighbouring tiles are unelectrified.
126 static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
128 if (!IsPlainRailTile(t)) return tracks;
130 TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
131 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
132 /* If the neighbour tile is either not electrified or has no tracks that can be reached
133 * from this tile, mark all trackdirs that can be reached from the neighbour tile
134 * as needing no catenary. We make an exception for blocked station tiles with a matching
135 * axis that still display wires to preserve visual continuity. */
136 TileIndex next_tile = TileAddByDiagDir(t, d);
137 RailType rt = GetTileRailType(next_tile);
138 if (rt == INVALID_RAILTYPE || !HasRailCatenary(rt) ||
139 ((TrackStatusToTrackBits(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTracks(d)) == TRACK_BIT_NONE &&
140 (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
141 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
145 /* If the tracks from either a diagonal crossing or don't overlap, both
146 * trackdirs have to be marked to mask the corresponding track bit. Else
147 * one marked trackdir is enough the mask the track bit. */
148 TrackBits mask;
149 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
150 /* If the tracks form either a diagonal crossing or don't overlap, both
151 * trackdirs have to be marked to mask the corresponding track bit. */
152 mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
153 /* If that results in no masked tracks and it is not a diagonal crossing,
154 * require only one marked trackdir to mask. */
155 if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
156 } else {
157 /* Require only one marked trackdir to mask the track. */
158 mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
159 /* If that results in an empty set, require both trackdirs for diagonal track. */
160 if ((tracks & mask) == TRACK_BIT_NONE) {
161 if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
162 if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
163 /* If that still is not enough, require both trackdirs for any track. */
164 if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
168 /* Mask the tracks only if at least one track bit would remain. */
169 return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
173 * Get the base wire sprite to use.
175 static inline SpriteID GetWireBase(TileIndex tile, TileContext context = TCX_NORMAL)
177 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
178 SpriteID wires = GetCustomRailSprite(rti, tile, RTSG_WIRES, context);
179 return wires == 0 ? SPR_WIRE_BASE : wires;
183 * Get the base pylon sprite to use.
185 static inline SpriteID GetPylonBase(TileIndex tile, TileContext context = TCX_NORMAL)
187 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
188 SpriteID pylons = GetCustomRailSprite(rti, tile, RTSG_PYLONS, context);
189 return pylons == 0 ? SPR_PYLON_BASE : pylons;
193 * Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
194 * @param tile The tile to analyse
195 * @param *tileh the tileh
197 static void AdjustTileh(TileIndex tile, Slope *tileh)
199 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
200 if (IsTunnel(tile)) {
201 *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
202 } else if (*tileh != SLOPE_FLAT) {
203 *tileh = SLOPE_FLAT;
204 } else {
205 *tileh = InclinedSlope(GetTunnelBridgeDirection(tile));
211 * Returns the Z position of a Pylon Control Point.
213 * @param tile The tile the pylon should stand on.
214 * @param PCPpos The PCP of the tile.
215 * @return The Z position of the PCP.
217 static int GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
219 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
220 * PCPs are always on a tile edge.
222 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
223 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
224 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
226 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
227 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
228 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
230 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
233 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));
234 return (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
238 * Draws wires on a tunnel tile
240 * DrawTile_TunnelBridge() calls this function to draw the wires as SpriteCombine with the tunnel roof.
242 * @param ti The Tileinfo to draw the tile for
244 void DrawRailCatenaryOnTunnel(const TileInfo *ti)
246 /* xmin, ymin, xmax + 1, ymax + 1 of BB */
247 static const int _tunnel_wire_BB[4][4] = {
248 { 0, 1, 16, 15 }, // NE
249 { 1, 0, 15, 16 }, // SE
250 { 0, 1, 16, 15 }, // SW
251 { 1, 0, 15, 16 }, // NW
254 DiagDirection dir = GetTunnelBridgeDirection(ti->tile);
256 SpriteID wire_base = GetWireBase(ti->tile);
258 const SortableSpriteStruct *sss = &RailCatenarySpriteData_Tunnel[dir];
259 const int *BB_data = _tunnel_wire_BB[dir];
260 AddSortableSpriteToDraw(
261 wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
262 BB_data[2] - sss->x_offset, BB_data[3] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset + 1,
263 GetTilePixelZ(ti->tile) + sss->z_offset,
264 IsTransparencySet(TO_CATENARY),
265 BB_data[0] - sss->x_offset, BB_data[1] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset
270 * Draws wires and, if required, pylons on a given tile
271 * @param ti The Tileinfo to draw the tile for
273 static void DrawRailCatenaryRailway(const TileInfo *ti)
275 /* Pylons are placed on a tile edge, so we need to take into account
276 * the track configuration of 2 adjacent tiles. trackconfig[0] stores the
277 * current tile (home tile) while [1] holds the neighbour */
278 TrackBits trackconfig[TS_END];
279 TrackBits wireconfig[TS_END];
280 bool isflat[TS_END];
281 /* Note that ti->tileh has already been adjusted for Foundations */
282 Slope tileh[TS_END] = { ti->tileh, SLOPE_FLAT };
284 /* Half tile slopes coincide only with horizontal/vertical track.
285 * Faking a flat slope results in the correct sprites on positions. */
286 Corner halftile_corner = CORNER_INVALID;
287 if (IsHalftileSlope(tileh[TS_HOME])) {
288 halftile_corner = GetHalftileSlopeCorner(tileh[TS_HOME]);
289 tileh[TS_HOME] = SLOPE_FLAT;
292 TLG tlg = GetTLG(ti->tile);
293 byte PCPstatus = 0;
294 byte OverridePCP = 0;
295 byte PPPpreferred[DIAGDIR_END];
296 byte PPPallowed[DIAGDIR_END];
298 /* Find which rail bits are present, and select the override points.
299 * We don't draw a pylon:
300 * 1) INSIDE a tunnel (we wouldn't see it anyway)
301 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
302 * because that one is drawn on the bridge. Exception is for length 0 bridges
303 * which have no middle tiles */
304 trackconfig[TS_HOME] = GetRailTrackBitsUniversal(ti->tile, &OverridePCP);
305 wireconfig[TS_HOME] = MaskWireBits(ti->tile, trackconfig[TS_HOME]);
306 /* If a track bit is present that is not in the main direction, the track is level */
307 isflat[TS_HOME] = ((trackconfig[TS_HOME] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
309 AdjustTileh(ti->tile, &tileh[TS_HOME]);
311 SpriteID pylon_normal = GetPylonBase(ti->tile);
312 SpriteID pylon_halftile = (halftile_corner != CORNER_INVALID) ? GetPylonBase(ti->tile, TCX_UPPER_HALFTILE) : pylon_normal;
314 for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
315 static const uint edge_corners[] = {
316 1 << CORNER_N | 1 << CORNER_E, // DIAGDIR_NE
317 1 << CORNER_S | 1 << CORNER_E, // DIAGDIR_SE
318 1 << CORNER_S | 1 << CORNER_W, // DIAGDIR_SW
319 1 << CORNER_N | 1 << CORNER_W, // DIAGDIR_NW
321 SpriteID pylon_base = (halftile_corner != CORNER_INVALID && HasBit(edge_corners[i], halftile_corner)) ? pylon_halftile : pylon_normal;
322 TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
323 int elevation = GetPCPElevation(ti->tile, i);
325 /* Here's one of the main headaches. GetTileSlope does not correct for possibly
326 * existing foundataions, so we do have to do that manually later on.*/
327 tileh[TS_NEIGHBOUR] = GetTileSlope(neighbour);
328 trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, NULL);
329 wireconfig[TS_NEIGHBOUR] = MaskWireBits(neighbour, trackconfig[TS_NEIGHBOUR]);
330 if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
332 /* Ignore station tiles that allow neither wires nor pylons. */
333 if (IsRailStationTile(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
335 /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
336 * we have to draw all pillars on the current tile. */
337 if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
339 isflat[TS_NEIGHBOUR] = ((trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
341 PPPpreferred[i] = 0xFF; // We start with preferring everything (end-of-line in any direction)
342 PPPallowed[i] = AllowedPPPonPCP[i];
344 /* We cycle through all the existing tracks at a PCP and see what
345 * PPPs we want to have, or may not have at all */
346 for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
347 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
348 if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
349 IsBridgeTile(neighbour) &&
350 GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
351 continue;
354 /* We check whether the track in question (k) is present in the tile
355 * (TrackSourceTile) */
356 DiagDirection PCPpos = i;
357 if (HasBit(wireconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
358 /* track found, if track is in the neighbour tile, adjust the number
359 * of the PCP for preferred/allowed determination*/
360 PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
361 SetBit(PCPstatus, i); // This PCP is in use
362 PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
365 if (HasBit(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
366 PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
370 /* Deactivate all PPPs if PCP is not used */
371 if (!HasBit(PCPstatus, i)) {
372 PPPpreferred[i] = 0;
373 PPPallowed[i] = 0;
376 Foundation foundation = FOUNDATION_NONE;
378 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
379 if (IsTileType(neighbour, MP_STATION) || IsTileType(neighbour, MP_ROAD)) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
381 /* Read the foundations if they are present, and adjust the tileh */
382 if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE && IsTileType(neighbour, MP_RAILWAY) && HasRailCatenary(GetRailType(neighbour))) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
383 if (IsBridgeTile(neighbour)) {
384 foundation = GetBridgeFoundation(tileh[TS_NEIGHBOUR], DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
387 ApplyFoundationToSlope(foundation, &tileh[TS_NEIGHBOUR]);
389 /* Half tile slopes coincide only with horizontal/vertical track.
390 * Faking a flat slope results in the correct sprites on positions. */
391 if (IsHalftileSlope(tileh[TS_NEIGHBOUR])) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
393 AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
395 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
396 * Delete the PCP if this is the case.
397 * Level means that the slope is the same, or the track is flat */
398 if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
399 for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
400 if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) ClrBit(PCPstatus, i);
404 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
405 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
406 * anything. Note that the preferred PPPs still contain the end-of-line markers.
407 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
408 if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
410 if (IsBridgeAbove(ti->tile)) {
411 Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
412 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
414 if ((height <= GetTileMaxZ(ti->tile) + 1) &&
415 (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
416 SetBit(OverridePCP, i);
420 if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i) &&
421 (!IsRailStationTile(ti->tile) || CanStationTileHavePylons(ti->tile))) {
422 for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
423 byte temp = PPPorder[i][GetTLG(ti->tile)][k];
425 if (HasBit(PPPallowed[i], temp)) {
426 uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
427 uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
429 /* Don't build the pylon if it would be outside the tile */
430 if (!HasBit(OwnedPPPonPCP[i], temp)) {
431 /* We have a neighbour that will draw it, bail out */
432 if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE) break;
433 continue; // No neighbour, go looking for a better position
436 AddSortableSpriteToDraw(pylon_base + pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
437 elevation, IsTransparencySet(TO_CATENARY), -1, -1);
439 break; // We already have drawn a pylon, bail out
445 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawRailCatenaryOnTunnel()) */
446 if (IsTunnelTile(ti->tile)) return;
448 /* Don't draw a wire under a low bridge */
449 if (IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
450 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
452 if (height <= GetTileMaxZ(ti->tile) + 1) return;
455 /* Don't draw a wire if the station tile does not want any */
456 if (IsRailStationTile(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
458 SpriteID wire_normal = GetWireBase(ti->tile);
459 SpriteID wire_halftile = (halftile_corner != CORNER_INVALID) ? GetWireBase(ti->tile, TCX_UPPER_HALFTILE) : wire_normal;
460 Track halftile_track;
461 switch (halftile_corner) {
462 case CORNER_W: halftile_track = TRACK_LEFT; break;
463 case CORNER_S: halftile_track = TRACK_LOWER; break;
464 case CORNER_E: halftile_track = TRACK_RIGHT; break;
465 case CORNER_N: halftile_track = TRACK_UPPER; break;
466 default: halftile_track = INVALID_TRACK; break;
469 /* Drawing of pylons is finished, now draw the wires */
470 Track t;
471 FOR_EACH_SET_TRACK(t, wireconfig[TS_HOME]) {
472 SpriteID wire_base = (t == halftile_track) ? wire_halftile : wire_normal;
473 byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
474 (HasBit(PCPstatus, PCPpositions[t][1]) << 1);
476 const SortableSpriteStruct *sss;
477 int tileh_selector = !(tileh[TS_HOME] % 3) * tileh[TS_HOME] / 3; // tileh for the slopes, 0 otherwise
479 assert(PCPconfig != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
480 assert(!IsSteepSlope(tileh[TS_HOME]));
481 sss = &RailCatenarySpriteData[Wires[tileh_selector][t][PCPconfig]];
484 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
485 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
486 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
488 AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
489 sss->x_size, sss->y_size, sss->z_size, GetSlopePixelZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
490 IsTransparencySet(TO_CATENARY));
495 * Draws wires on a tunnel tile
497 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
499 * @param ti The Tileinfo to draw the tile for
501 void DrawRailCatenaryOnBridge(const TileInfo *ti)
503 TileIndex end = GetSouthernBridgeEnd(ti->tile);
504 TileIndex start = GetOtherBridgeEnd(end);
506 uint length = GetTunnelBridgeLength(start, end);
507 uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
508 uint height;
510 const SortableSpriteStruct *sss;
511 Axis axis = GetBridgeAxis(ti->tile);
512 TLG tlg = GetTLG(ti->tile);
514 RailCatenarySprite offset = (RailCatenarySprite)(axis == AXIS_X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
516 if ((length % 2) && num == length) {
517 /* Draw the "short" wire on the southern end of the bridge
518 * only needed if the length of the bridge is odd */
519 sss = &RailCatenarySpriteData[WIRE_X_FLAT_BOTH + offset];
520 } else {
521 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
522 sss = &RailCatenarySpriteData[WIRE_X_FLAT_SW + (num % 2) + offset];
525 height = GetBridgePixelHeight(end);
527 SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
529 AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
530 sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
531 IsTransparencySet(TO_CATENARY)
534 SpriteID pylon_base = GetPylonBase(end, TCX_ON_BRIDGE);
536 /* Finished with wires, draw pylons
537 * every other tile needs a pylon on the northern end */
538 if (num % 2) {
539 DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_NE : DIAGDIR_NW);
540 Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
541 if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
542 uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
543 uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
544 AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
547 /* need a pylon on the southern end of the bridge */
548 if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
549 DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE);
550 Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
551 if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
552 uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
553 uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
554 AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
559 * Draws overhead wires and pylons for electric railways.
560 * @param ti The TileInfo struct of the tile being drawn
561 * @see DrawRailCatenaryRailway
563 void DrawRailCatenary(const TileInfo *ti)
565 switch (GetTileType(ti->tile)) {
566 case MP_RAILWAY:
567 if (IsRailDepot(ti->tile)) {
568 const SortableSpriteStruct *sss = &RailCatenarySpriteData_Depot[GetRailDepotDirection(ti->tile)];
570 SpriteID wire_base = GetWireBase(ti->tile);
572 /* This wire is not visible with the default depot sprites */
573 AddSortableSpriteToDraw(
574 wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
575 sss->x_size, sss->y_size, sss->z_size,
576 GetTileMaxPixelZ(ti->tile) + sss->z_offset,
577 IsTransparencySet(TO_CATENARY)
579 return;
581 break;
583 case MP_TUNNELBRIDGE:
584 case MP_ROAD:
585 case MP_STATION:
586 break;
588 default: return;
590 DrawRailCatenaryRailway(ti);
593 bool SettingsDisableElrail(int32 p1)
595 Company *c;
596 Train *t;
597 bool disable = (p1 != 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 Engine *e;
605 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
606 RailVehicleInfo *rv_info = &e->u.rail;
607 /* if it is an electric rail engine and its railtype is the wrong one */
608 if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
609 /* change it to the proper one */
610 rv_info->railtype = new_railtype;
614 /* when disabling elrails, make sure that all existing trains can run on
615 * normal rail too */
616 if (disable) {
617 FOR_ALL_TRAINS(t) {
618 if (t->railtype == RAILTYPE_ELECTRIC) {
619 /* this railroad vehicle is now compatible only with elrail,
620 * so add there also normal rail compatibility */
621 t->compatible_railtypes |= RAILTYPES_RAIL;
622 t->railtype = RAILTYPE_RAIL;
623 SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
628 /* Fix the total power and acceleration for trains */
629 FOR_ALL_TRAINS(t) {
630 /* power and acceleration is cached only for front engines */
631 if (t->IsFrontEngine()) {
632 t->ConsistChanged(CCF_TRACK);
636 FOR_ALL_COMPANIES(c) c->avail_railtypes = GetCompanyRailtypes(c->index);
638 /* This resets the _last_built_railtype, which will be invalid for electric
639 * rails. It may have unintended consequences if that function is ever
640 * extended, though. */
641 ReinitGuiAfterToggleElrail(disable);
642 return true;