elrail.cpp

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

Generated on Fri Mar 18 23:17:35 2011 for OpenTTD by  doxygen 1.6.1