elrail.cpp

Go to the documentation of this file.
00001 /* $Id: elrail.cpp 26317 2014-02-07 23:48:56Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00057 #include "stdafx.h"
00058 #include "station_map.h"
00059 #include "viewport_func.h"
00060 #include "train.h"
00061 #include "rail_gui.h"
00062 #include "tunnelbridge_map.h"
00063 #include "tunnelbridge.h"
00064 #include "elrail_func.h"
00065 #include "company_base.h"
00066 #include "newgrf_railtype.h"
00067 
00068 #include "table/elrail_data.h"
00069 
00075 static inline TLG GetTLG(TileIndex t)
00076 {
00077   return (TLG)((HasBit(TileX(t), 0) << 1) + HasBit(TileY(t), 0));
00078 }
00079 
00086 static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
00087 {
00088   switch (GetTileType(t)) {
00089     case MP_RAILWAY:
00090       if (!HasCatenary(GetRailType(t))) return TRACK_BIT_NONE;
00091       switch (GetRailTileType(t)) {
00092         case RAIL_TILE_NORMAL: case RAIL_TILE_SIGNALS:
00093           return GetTrackBits(t);
00094         default:
00095           return TRACK_BIT_NONE;
00096       }
00097       break;
00098 
00099     case MP_TUNNELBRIDGE:
00100       if (!HasCatenary(GetRailType(t))) return TRACK_BIT_NONE;
00101       if (override != NULL && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
00102         *override = 1 << GetTunnelBridgeDirection(t);
00103       }
00104       return DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t));
00105 
00106     case MP_ROAD:
00107       if (!IsLevelCrossing(t)) return TRACK_BIT_NONE;
00108       if (!HasCatenary(GetRailType(t))) return TRACK_BIT_NONE;
00109       return GetCrossingRailBits(t);
00110 
00111     case MP_STATION:
00112       if (!HasStationRail(t)) return TRACK_BIT_NONE;
00113       if (!HasCatenary(GetRailType(t))) return TRACK_BIT_NONE;
00114       return TrackToTrackBits(GetRailStationTrack(t));
00115 
00116     default:
00117       return TRACK_BIT_NONE;
00118   }
00119 }
00120 
00124 static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
00125 {
00126   if (!IsPlainRailTile(t)) return tracks;
00127 
00128   TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
00129   for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
00130     /* If the neighbour tile is either not electrified or has no tracks that can be reached
00131      * from this tile, mark all trackdirs that can be reached from the neighbour tile
00132      * as needing no catenary. We make an exception for blocked station tiles with a matching
00133      * axis that still display wires to preserve visual continuity. */
00134     TileIndex next_tile = TileAddByDiagDir(t, d);
00135     RailType rt = GetTileRailType(next_tile);
00136     if (rt == INVALID_RAILTYPE || !HasCatenary(rt) ||
00137         ((TrackStatusToTrackBits(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTracks(d)) == TRACK_BIT_NONE &&
00138         (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
00139       neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
00140     }
00141   }
00142 
00143   /* If the tracks from either a diagonal crossing or don't overlap, both
00144    * trackdirs have to be marked to mask the corresponding track bit. Else
00145    * one marked trackdir is enough the mask the track bit. */
00146   TrackBits mask;
00147   if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
00148     /* If the tracks form either a diagonal crossing or don't overlap, both
00149      * trackdirs have to be marked to mask the corresponding track bit. */
00150     mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
00151     /* If that results in no masked tracks and it is not a diagonal crossing,
00152      * require only one marked trackdir to mask. */
00153     if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
00154   } else {
00155     /* Require only one marked trackdir to mask the track. */
00156     mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
00157     /* If that results in an empty set, require both trackdirs for diagonal track. */
00158     if ((tracks & mask) == TRACK_BIT_NONE) {
00159       if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
00160       if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
00161       /* If that still is not enough, require both trackdirs for any track. */
00162       if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
00163     }
00164   }
00165 
00166   /* Mask the tracks only if at least one track bit would remain. */
00167   return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
00168 }
00169 
00173 static inline SpriteID GetWireBase(TileIndex tile, TileContext context = TCX_NORMAL)
00174 {
00175   const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
00176   SpriteID wires = GetCustomRailSprite(rti, tile, RTSG_WIRES, context);
00177   return wires == 0 ? SPR_WIRE_BASE : wires;
00178 }
00179 
00183 static inline SpriteID GetPylonBase(TileIndex tile, TileContext context = TCX_NORMAL)
00184 {
00185   const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
00186   SpriteID pylons = GetCustomRailSprite(rti, tile, RTSG_PYLONS, context);
00187   return pylons == 0 ? SPR_PYLON_BASE : pylons;
00188 }
00189 
00195 static void AdjustTileh(TileIndex tile, Slope *tileh)
00196 {
00197   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00198     if (IsTunnel(tile)) {
00199       *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
00200     } else if (*tileh != SLOPE_FLAT) {
00201       *tileh = SLOPE_FLAT;
00202     } else {
00203       *tileh = InclinedSlope(GetTunnelBridgeDirection(tile));
00204     }
00205   }
00206 }
00207 
00215 static int GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
00216 {
00217   /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
00218    * PCPs are always on a tile edge.
00219    *
00220    * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
00221    * So we have to move it inside the tile, because if the neighboured tile has a foundation,
00222    * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
00223    *
00224    * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
00225    * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
00226    * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
00227    *
00228    * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
00229    */
00230 
00231   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));
00232   return (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
00233 }
00234 
00242 void DrawCatenaryOnTunnel(const TileInfo *ti)
00243 {
00244   /* xmin, ymin, xmax + 1, ymax + 1 of BB */
00245   static const int _tunnel_wire_BB[4][4] = {
00246     { 0, 1, 16, 15 }, // NE
00247     { 1, 0, 15, 16 }, // SE
00248     { 0, 1, 16, 15 }, // SW
00249     { 1, 0, 15, 16 }, // NW
00250   };
00251 
00252   DiagDirection dir = GetTunnelBridgeDirection(ti->tile);
00253 
00254   SpriteID wire_base = GetWireBase(ti->tile);
00255 
00256   const SortableSpriteStruct *sss = &CatenarySpriteData_Tunnel[dir];
00257   const int *BB_data = _tunnel_wire_BB[dir];
00258   AddSortableSpriteToDraw(
00259     wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
00260     BB_data[2] - sss->x_offset, BB_data[3] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset + 1,
00261     GetTilePixelZ(ti->tile) + sss->z_offset,
00262     IsTransparencySet(TO_CATENARY),
00263     BB_data[0] - sss->x_offset, BB_data[1] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset
00264   );
00265 }
00266 
00271 static void DrawCatenaryRailway(const TileInfo *ti)
00272 {
00273   /* Pylons are placed on a tile edge, so we need to take into account
00274    * the track configuration of 2 adjacent tiles. trackconfig[0] stores the
00275    * current tile (home tile) while [1] holds the neighbour */
00276   TrackBits trackconfig[TS_END];
00277   TrackBits wireconfig[TS_END];
00278   bool isflat[TS_END];
00279   /* Note that ti->tileh has already been adjusted for Foundations */
00280   Slope tileh[TS_END] = { ti->tileh, SLOPE_FLAT };
00281 
00282   /* Half tile slopes coincide only with horizontal/vertical track.
00283    * Faking a flat slope results in the correct sprites on positions. */
00284   Corner halftile_corner = CORNER_INVALID;
00285   if (IsHalftileSlope(tileh[TS_HOME])) {
00286     halftile_corner = GetHalftileSlopeCorner(tileh[TS_HOME]);
00287     tileh[TS_HOME] = SLOPE_FLAT;
00288   }
00289 
00290   TLG tlg = GetTLG(ti->tile);
00291   byte PCPstatus = 0;
00292   byte OverridePCP = 0;
00293   byte PPPpreferred[DIAGDIR_END];
00294   byte PPPallowed[DIAGDIR_END];
00295 
00296   /* Find which rail bits are present, and select the override points.
00297    * We don't draw a pylon:
00298    * 1) INSIDE a tunnel (we wouldn't see it anyway)
00299    * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
00300    *    because that one is drawn on the bridge. Exception is for length 0 bridges
00301    *    which have no middle tiles */
00302   trackconfig[TS_HOME] = GetRailTrackBitsUniversal(ti->tile, &OverridePCP);
00303   wireconfig[TS_HOME] = MaskWireBits(ti->tile, trackconfig[TS_HOME]);
00304   /* If a track bit is present that is not in the main direction, the track is level */
00305   isflat[TS_HOME] = ((trackconfig[TS_HOME] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
00306 
00307   AdjustTileh(ti->tile, &tileh[TS_HOME]);
00308 
00309   SpriteID pylon_normal = GetPylonBase(ti->tile);
00310   SpriteID pylon_halftile = (halftile_corner != CORNER_INVALID) ? GetPylonBase(ti->tile, TCX_UPPER_HALFTILE) : pylon_normal;
00311 
00312   for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
00313     static const uint edge_corners[] = {
00314       1 << CORNER_N | 1 << CORNER_E, // DIAGDIR_NE
00315       1 << CORNER_S | 1 << CORNER_E, // DIAGDIR_SE
00316       1 << CORNER_S | 1 << CORNER_W, // DIAGDIR_SW
00317       1 << CORNER_N | 1 << CORNER_W, // DIAGDIR_NW
00318     };
00319     SpriteID pylon_base = (halftile_corner != CORNER_INVALID && HasBit(edge_corners[i], halftile_corner)) ? pylon_halftile : pylon_normal;
00320     TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
00321     int elevation = GetPCPElevation(ti->tile, i);
00322 
00323     /* Here's one of the main headaches. GetTileSlope does not correct for possibly
00324      * existing foundataions, so we do have to do that manually later on.*/
00325     tileh[TS_NEIGHBOUR] = GetTileSlope(neighbour);
00326     trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, NULL);
00327     wireconfig[TS_NEIGHBOUR] = MaskWireBits(neighbour, trackconfig[TS_NEIGHBOUR]);
00328     if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
00329 
00330     /* Ignore station tiles that allow neither wires nor pylons. */
00331     if (IsRailStationTile(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
00332 
00333     /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
00334      * we have to draw all pillars on the current tile. */
00335     if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
00336 
00337     isflat[TS_NEIGHBOUR] = ((trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
00338 
00339     PPPpreferred[i] = 0xFF; // We start with preferring everything (end-of-line in any direction)
00340     PPPallowed[i] = AllowedPPPonPCP[i];
00341 
00342     /* We cycle through all the existing tracks at a PCP and see what
00343      * PPPs we want to have, or may not have at all */
00344     for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
00345       /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
00346       if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
00347           IsBridgeTile(neighbour) &&
00348           GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
00349         continue;
00350       }
00351 
00352       /* We check whether the track in question (k) is present in the tile
00353        * (TrackSourceTile) */
00354       DiagDirection PCPpos = i;
00355       if (HasBit(wireconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
00356         /* track found, if track is in the neighbour tile, adjust the number
00357          * of the PCP for preferred/allowed determination*/
00358         PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
00359         SetBit(PCPstatus, i); // This PCP is in use
00360         PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
00361       }
00362 
00363       if (HasBit(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
00364         PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
00365       }
00366     }
00367 
00368     /* Deactivate all PPPs if PCP is not used */
00369     if (!HasBit(PCPstatus, i)) {
00370       PPPpreferred[i] = 0;
00371       PPPallowed[i] = 0;
00372     }
00373 
00374     Foundation foundation = FOUNDATION_NONE;
00375 
00376     /* Station and road crossings are always "flat", so adjust the tileh accordingly */
00377     if (IsTileType(neighbour, MP_STATION) || IsTileType(neighbour, MP_ROAD)) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
00378 
00379     /* Read the foundations if they are present, and adjust the tileh */
00380     if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE && IsTileType(neighbour, MP_RAILWAY) && HasCatenary(GetRailType(neighbour))) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
00381     if (IsBridgeTile(neighbour)) {
00382       foundation = GetBridgeFoundation(tileh[TS_NEIGHBOUR], DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
00383     }
00384 
00385     ApplyFoundationToSlope(foundation, &tileh[TS_NEIGHBOUR]);
00386 
00387     /* Half tile slopes coincide only with horizontal/vertical track.
00388      * Faking a flat slope results in the correct sprites on positions. */
00389     if (IsHalftileSlope(tileh[TS_NEIGHBOUR])) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
00390 
00391     AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
00392 
00393     /* If we have a straight (and level) track, we want a pylon only every 2 tiles
00394      * Delete the PCP if this is the case.
00395      * Level means that the slope is the same, or the track is flat */
00396     if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
00397       for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
00398         if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) ClrBit(PCPstatus, i);
00399       }
00400     }
00401 
00402     /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
00403      * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
00404      * anything. Note that the preferred PPPs still contain the end-of-line markers.
00405      * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
00406     if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
00407 
00408     if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile)) {
00409       Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
00410       int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
00411 
00412       if ((height <= GetTileMaxZ(ti->tile) + 1) &&
00413           (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
00414         SetBit(OverridePCP, i);
00415       }
00416     }
00417 
00418     if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i) &&
00419         (!IsRailStationTile(ti->tile) || CanStationTileHavePylons(ti->tile))) {
00420       for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
00421         byte temp = PPPorder[i][GetTLG(ti->tile)][k];
00422 
00423         if (HasBit(PPPallowed[i], temp)) {
00424           uint x  = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
00425           uint y  = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
00426 
00427           /* Don't build the pylon if it would be outside the tile */
00428           if (!HasBit(OwnedPPPonPCP[i], temp)) {
00429             /* We have a neighbour that will draw it, bail out */
00430             if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE) break;
00431             continue; // No neighbour, go looking for a better position
00432           }
00433 
00434           AddSortableSpriteToDraw(pylon_base + pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
00435             elevation, IsTransparencySet(TO_CATENARY), -1, -1);
00436 
00437           break; // We already have drawn a pylon, bail out
00438         }
00439       }
00440     }
00441   }
00442 
00443   /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawCatenaryOnTunnel()) */
00444   if (IsTunnelTile(ti->tile)) return;
00445 
00446   /* Don't draw a wire under a low bridge */
00447   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
00448     int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
00449 
00450     if (height <= GetTileMaxZ(ti->tile) + 1) return;
00451   }
00452 
00453   /* Don't draw a wire if the station tile does not want any */
00454   if (IsRailStationTile(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
00455 
00456   SpriteID wire_normal = GetWireBase(ti->tile);
00457   SpriteID wire_halftile = (halftile_corner != CORNER_INVALID) ? GetWireBase(ti->tile, TCX_UPPER_HALFTILE) : wire_normal;
00458   Track halftile_track;
00459   switch (halftile_corner) {
00460     case CORNER_W: halftile_track = TRACK_LEFT; break;
00461     case CORNER_S: halftile_track = TRACK_LOWER; break;
00462     case CORNER_E: halftile_track = TRACK_RIGHT; break;
00463     case CORNER_N: halftile_track = TRACK_UPPER; break;
00464     default:       halftile_track = INVALID_TRACK; break;
00465   }
00466 
00467   /* Drawing of pylons is finished, now draw the wires */
00468   Track t;
00469   FOR_EACH_SET_TRACK(t, wireconfig[TS_HOME]) {
00470     SpriteID wire_base = (t == halftile_track) ? wire_halftile : wire_normal;
00471     byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
00472       (HasBit(PCPstatus, PCPpositions[t][1]) << 1);
00473 
00474     const SortableSpriteStruct *sss;
00475     int tileh_selector = !(tileh[TS_HOME] % 3) * tileh[TS_HOME] / 3; // tileh for the slopes, 0 otherwise
00476 
00477     assert(PCPconfig != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
00478     assert(!IsSteepSlope(tileh[TS_HOME]));
00479     sss = &CatenarySpriteData[Wires[tileh_selector][t][PCPconfig]];
00480 
00481     /*
00482      * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
00483      * Therefore it is safe to use GetSlopePixelZ() for the elevation.
00484      * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
00485      */
00486     AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
00487       sss->x_size, sss->y_size, sss->z_size, GetSlopePixelZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
00488       IsTransparencySet(TO_CATENARY));
00489   }
00490 }
00491 
00499 void DrawCatenaryOnBridge(const TileInfo *ti)
00500 {
00501   TileIndex end = GetSouthernBridgeEnd(ti->tile);
00502   TileIndex start = GetOtherBridgeEnd(end);
00503 
00504   uint length = GetTunnelBridgeLength(start, end);
00505   uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
00506   uint height;
00507 
00508   const SortableSpriteStruct *sss;
00509   Axis axis = GetBridgeAxis(ti->tile);
00510   TLG tlg = GetTLG(ti->tile);
00511 
00512   CatenarySprite offset = (CatenarySprite)(axis == AXIS_X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
00513 
00514   if ((length % 2) && num == length) {
00515     /* Draw the "short" wire on the southern end of the bridge
00516      * only needed if the length of the bridge is odd */
00517     sss = &CatenarySpriteData[WIRE_X_FLAT_BOTH + offset];
00518   } else {
00519     /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
00520     sss = &CatenarySpriteData[WIRE_X_FLAT_SW + (num % 2) + offset];
00521   }
00522 
00523   height = GetBridgePixelHeight(end);
00524 
00525   SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
00526 
00527   AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
00528     sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
00529     IsTransparencySet(TO_CATENARY)
00530   );
00531 
00532   SpriteID pylon_base = GetPylonBase(end, TCX_ON_BRIDGE);
00533 
00534   /* Finished with wires, draw pylons
00535    * every other tile needs a pylon on the northern end */
00536   if (num % 2) {
00537     DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_NE : DIAGDIR_NW);
00538     Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
00539     if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
00540     uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
00541     uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
00542     AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
00543   }
00544 
00545   /* need a pylon on the southern end of the bridge */
00546   if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
00547     DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE);
00548     Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
00549     if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
00550     uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
00551     uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
00552     AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
00553   }
00554 }
00555 
00561 void DrawCatenary(const TileInfo *ti)
00562 {
00563   switch (GetTileType(ti->tile)) {
00564     case MP_RAILWAY:
00565       if (IsRailDepot(ti->tile)) {
00566         const SortableSpriteStruct *sss = &CatenarySpriteData_Depot[GetRailDepotDirection(ti->tile)];
00567 
00568         SpriteID wire_base = GetWireBase(ti->tile);
00569 
00570         /* This wire is not visible with the default depot sprites */
00571         AddSortableSpriteToDraw(
00572           wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
00573           sss->x_size, sss->y_size, sss->z_size,
00574           GetTileMaxPixelZ(ti->tile) + sss->z_offset,
00575           IsTransparencySet(TO_CATENARY)
00576         );
00577         return;
00578       }
00579       break;
00580 
00581     case MP_TUNNELBRIDGE:
00582     case MP_ROAD:
00583     case MP_STATION:
00584       break;
00585 
00586     default: return;
00587   }
00588   DrawCatenaryRailway(ti);
00589 }
00590 
00591 bool SettingsDisableElrail(int32 p1)
00592 {
00593   Company *c;
00594   Train *t;
00595   bool disable = (p1 != 0);
00596 
00597   /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
00598   const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
00599   const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
00600 
00601   /* walk through all train engines */
00602   Engine *e;
00603   FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
00604     RailVehicleInfo *rv_info = &e->u.rail;
00605     /* if it is an electric rail engine and its railtype is the wrong one */
00606     if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
00607       /* change it to the proper one */
00608       rv_info->railtype = new_railtype;
00609     }
00610   }
00611 
00612   /* when disabling elrails, make sure that all existing trains can run on
00613    *  normal rail too */
00614   if (disable) {
00615     FOR_ALL_TRAINS(t) {
00616       if (t->railtype == RAILTYPE_ELECTRIC) {
00617         /* this railroad vehicle is now compatible only with elrail,
00618          *  so add there also normal rail compatibility */
00619         t->compatible_railtypes |= RAILTYPES_RAIL;
00620         t->railtype = RAILTYPE_RAIL;
00621         SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
00622       }
00623     }
00624   }
00625 
00626   /* Fix the total power and acceleration for trains */
00627   FOR_ALL_TRAINS(t) {
00628     /* power and acceleration is cached only for front engines */
00629     if (t->IsFrontEngine()) {
00630       t->ConsistChanged(CCF_TRACK);
00631     }
00632   }
00633 
00634   FOR_ALL_COMPANIES(c) c->avail_railtypes = GetCompanyRailtypes(c->index);
00635 
00636   /* This resets the _last_built_railtype, which will be invalid for electric
00637    * rails. It may have unintended consequences if that function is ever
00638    * extended, though. */
00639   ReinitGuiAfterToggleElrail(disable);
00640   return true;
00641 }