Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / elrail.cpp
blob6be4d07b58b3897725145afc54b2fb8091262c65
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][i];
413 /* Tracks inciding from the neighbour tile */
414 for (uint k = 0; k < NUM_TRACKS_PER_SIDE; k++) {
415 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
416 if (IsRailBridgeTile(neighbour) && GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
417 continue;
420 /* We check whether the track in question (k) is present in the tile */
421 Track track = TracksAtTileSide[ReverseDiagDir(i)][k];
422 DiagDirection PCPpos = i;
423 if (HasBit(nbconfig.wires, track)) {
424 /* track found, adjust the number
425 * of the PCP for preferred/allowed determination*/
426 PCPpos = ReverseDiagDir(i);
427 SetBit(PCPstatus, i); // This PCP is in use
428 PPPpreferred[i] &= PreferredPPPofTrackAtPCP[track][PCPpos];
431 if (HasBit(nbconfig.tracks, track)) {
432 PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[track][PCPpos];
436 /* Deactivate all PPPs if PCP is not used */
437 if (!HasBit(PCPstatus, i)) {
438 PPPpreferred[i] = 0;
439 PPPallowed[i] = 0;
442 Foundation foundation = FOUNDATION_NONE;
444 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
445 if (IsStationTile(neighbour) || IsLevelCrossingTile(neighbour)) nbconfig.tileh = SLOPE_FLAT;
447 /* Read the foundations if they are present, and adjust the tileh */
448 if (nbconfig.tracks != TRACK_BIT_NONE && (IsNormalRailTile(neighbour) || IsRailDepotTile(neighbour))) {
449 foundation = GetRailFoundation(nbconfig.tileh, nbconfig.tracks);
451 if (IsRailBridgeTile(neighbour)) {
452 foundation = GetBridgeFoundation(nbconfig.tileh, DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
455 ApplyFoundationToSlope(foundation, &nbconfig.tileh);
457 /* Half tile slopes coincide only with horizontal/vertical track.
458 * Faking a flat slope results in the correct sprites on positions. */
459 if (IsHalftileSlope(nbconfig.tileh)) nbconfig.tileh = SLOPE_FLAT;
461 AdjustTileh(neighbour, &nbconfig.tileh);
463 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
464 * Delete the PCP if this is the case.
465 * Level means that the slope is the same, or the track is flat */
466 if (home.tileh == nbconfig.tileh || (home.isflat && nbconfig.isflat)) {
467 for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
468 if (PPPpreferred[i] == IgnoredPCP[tlg][i][k]) ClrBit(PCPstatus, i);
472 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
473 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
474 * anything. Note that the preferred PPPs still contain the end-of-line markers.
475 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
476 if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
478 if (HasBridgeAbove(ti->tile)) {
479 Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
480 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
482 if ((height <= GetTileMaxZ(ti->tile) + 1) &&
483 (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
484 SetBit(OverridePCP, i);
488 if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i) &&
489 (!IsRailStationTile(ti->tile) || CanStationTileHavePylons(ti->tile))) {
490 for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
491 byte temp = PPPorder[i][GetTLG(ti->tile)][k];
493 if (HasBit(PPPallowed[i], temp)) {
494 uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
495 uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
497 /* Don't build the pylon if it would be outside the tile */
498 if (!HasBit(OwnedPPPonPCP[i], temp)) {
499 /* We have a neighbour that will draw it, bail out */
500 if (nbconfig.tracks != TRACK_BIT_NONE) break;
501 continue; // No neighbour, go looking for a better position
504 AddSortableSpriteToDraw(pylon_base + pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
505 elevation, IsTransparencySet(TO_CATENARY), -1, -1);
507 break; // We already have drawn a pylon, bail out
513 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawCatenaryOnTunnel()) */
514 if (IsTunnelTile(ti->tile)) return;
516 /* Don't draw a wire under a low bridge */
517 if (HasBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
518 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
520 if (height <= GetTileMaxZ(ti->tile) + 1) return;
523 /* Don't draw a wire if the station tile does not want any */
524 if (IsRailStationTile(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
526 if (halftile_track == INVALID_TRACK) {
527 sprite_normal = GetWireBase(ti->tile, TCX_NORMAL);
528 } else {
529 sprite_halftile = GetWireBase(ti->tile, halftile_context, halftile_track);
530 sprite_normal = GetWireBase(ti->tile, TCX_NORMAL,
531 HasBit(home.tracks, TrackToOppositeTrack(halftile_track)) ? TrackToOppositeTrack(halftile_track) : halftile_track);
534 /* Drawing of pylons is finished, now draw the wires */
535 Track t;
536 FOR_EACH_SET_TRACK(t, home.wires) {
537 byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
538 (HasBit(PCPstatus, PCPpositions[t][1]) << 1);
540 assert(PCPconfig != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
541 assert(!IsSteepSlope(home.tileh));
543 const SortableSpriteStructM *sss;
544 switch (home.tileh) {
545 case SLOPE_SW: sss = &CatenarySpriteDataSW; break;
546 case SLOPE_SE: sss = &CatenarySpriteDataSE; break;
547 case SLOPE_NW: sss = &CatenarySpriteDataNW; break;
548 case SLOPE_NE: sss = &CatenarySpriteDataNE; break;
549 default: sss = &CatenarySpriteData[t]; break;
553 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
554 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
555 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
557 SpriteID wire_base = (t == halftile_track) ? sprite_halftile : sprite_normal;
558 AddSortableSpriteToDraw(wire_base + sss->image_offset[PCPconfig], PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
559 sss->x_size, sss->y_size, sss->z_size, GetSlopePixelZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
560 IsTransparencySet(TO_CATENARY));
565 * Draws wires on a tunnel tile
567 * DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
569 * @param ti The Tileinfo to draw the tile for
571 void DrawCatenaryOnBridge(const TileInfo *ti)
573 TileIndex start = GetNorthernBridgeEnd(ti->tile);
574 TileIndex end = GetSouthernBridgeEnd(ti->tile);
576 uint length = GetTunnelBridgeLength(start, end);
577 uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
579 Axis axis = GetBridgeAxis(ti->tile);
581 const SortableSpriteStructM *sss = &CatenarySpriteData[AxisToTrack(axis)];
583 uint config;
584 if ((length % 2) && num == length) {
585 /* Draw the "short" wire on the southern end of the bridge
586 * only needed if the length of the bridge is odd */
587 config = 3;
588 } else {
589 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
590 config = 2 - (num % 2);
593 uint height = GetBridgePixelHeight(end);
595 SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
597 AddSortableSpriteToDraw(wire_base + sss->image_offset[config], PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
598 sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
599 IsTransparencySet(TO_CATENARY)
602 /* Finished with wires, draw pylons */
603 if ((num % 2) == 0 && num != length) return; /* no pylons to draw */
605 DiagDirection PCPpos;
606 Direction PPPpos;
607 if (axis == AXIS_X) {
608 PCPpos = DIAGDIR_NE;
609 PPPpos = HasBit(GetTLG(ti->tile), 0) ? DIR_SE : DIR_NW;
610 } else {
611 PCPpos = DIAGDIR_NW;
612 PPPpos = HasBit(GetTLG(ti->tile), 1) ? DIR_SW : DIR_NE;
615 SpriteID pylon = GetPylonBase(end, TCX_ON_BRIDGE) + pylon_sprites[PPPpos];
616 uint x = ti->x + x_ppp_offsets[PPPpos];
617 uint y = ti->y + y_ppp_offsets[PPPpos];
619 /* every other tile needs a pylon on the northern end */
620 if (num % 2) {
621 AddSortableSpriteToDraw(pylon, PAL_NONE, x + x_pcp_offsets[PCPpos], y + y_pcp_offsets[PCPpos],
622 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
625 /* need a pylon on the southern end of the bridge */
626 if (num == length) {
627 PCPpos = ReverseDiagDir(PCPpos);
628 AddSortableSpriteToDraw(pylon, PAL_NONE, x + x_pcp_offsets[PCPpos], y + y_pcp_offsets[PCPpos],
629 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
634 * Draws overhead wires and pylons for electric railways.
635 * @param ti The TileInfo struct of the tile being drawn
636 * @see DrawCatenaryRailway
638 void DrawCatenary(const TileInfo *ti)
640 switch (GetTileType(ti->tile)) {
641 case TT_MISC:
642 switch (GetTileSubtype(ti->tile)) {
643 default: return;
645 case TT_MISC_DEPOT: {
646 if (!IsRailDepot(ti->tile)) return;
647 const SortableSpriteStruct *sss = &CatenarySpriteData_TunnelDepot[GetGroundDepotDirection(ti->tile)];
648 SpriteID wire_base = GetWireBase(ti->tile);
650 /* This wire is not visible with the default depot sprites */
651 AddSortableSpriteToDraw(
652 wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
653 sss->x_size, sss->y_size, sss->z_size,
654 GetTileMaxPixelZ(ti->tile) + sss->z_offset,
655 IsTransparencySet(TO_CATENARY)
657 return;
660 case TT_MISC_CROSSING:
661 case TT_MISC_TUNNEL:
662 break;
664 break;
666 case TT_RAILWAY:
667 case TT_STATION:
668 break;
670 default: return;
672 DrawCatenaryRailway(ti);
675 bool SettingsDisableElrail(int32 p1)
677 Company *c;
678 Train *t;
679 bool disable = (p1 != 0);
681 /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
682 const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
683 const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
685 /* walk through all train engines */
686 Engine *e;
687 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
688 RailVehicleInfo *rv_info = &e->u.rail;
689 /* if it is an electric rail engine and its railtype is the wrong one */
690 if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
691 /* change it to the proper one */
692 rv_info->railtype = new_railtype;
696 /* when disabling elrails, make sure that all existing trains can run on
697 * normal rail too */
698 if (disable) {
699 FOR_ALL_TRAINS(t) {
700 if (t->railtype == RAILTYPE_ELECTRIC) {
701 /* this railroad vehicle is now compatible only with elrail,
702 * so add there also normal rail compatibility */
703 t->compatible_railtypes |= RAILTYPES_RAIL;
704 t->railtype = RAILTYPE_RAIL;
705 SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
710 /* Fix the total power and acceleration for trains */
711 FOR_ALL_TRAINS(t) {
712 /* power and acceleration is cached only for front engines */
713 if (t->IsFrontEngine()) {
714 t->ConsistChanged(true);
718 FOR_ALL_COMPANIES(c) c->avail_railtypes = GetCompanyRailtypes(c->index);
720 /* This resets the _last_built_railtype, which will be invalid for electric
721 * rails. It may have unintended consequences if that function is ever
722 * extended, though. */
723 ReinitGuiAfterToggleElrail(disable);
724 return true;