# elrail.cpp

Go to the documentation of this file.

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