# elrail.cpp

Go to the documentation of this file.

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