elrail.cpp

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

Generated on Sun May 8 07:30:11 2011 for OpenTTD by  doxygen 1.6.1