# elrail.cpp

Go to the documentation of this file.

    00001 /* $Id: elrail.cpp 13685 2008-07-09 18:59:39Z rubidium $ */
    00048 #include "stdafx.h"
    00049 #include "openttd.h"
    00050 #include "station_map.h"
    00051 #include "viewport_func.h"
    00052 #include "settings_type.h"
    00053 #include "landscape.h"
    00054 #include "rail_type.h"
    00055 #include "debug.h"
    00056 #include "tunnel_map.h"
    00057 #include "road_map.h"
    00058 #include "bridge_map.h"
    00059 #include "bridge.h"
    00060 #include "rail_map.h"
    00061 #include "train.h"
    00062 #include "rail_gui.h"
    00063 #include "transparency.h"
    00064 #include "tunnelbridge_map.h"
    00065 #include "vehicle_func.h"
    00066 #include "player_base.h"
    00067 #include "tunnelbridge.h"
    00068
    00069 #include "table/sprites.h"
    00070 #include "table/elrail_data.h"
    00071
    00072 static inline TLG GetTLG(TileIndex t)
    00073 {
    00074   return (TLG)((HasBit(TileX(t), 0) << 1) + HasBit(TileY(t), 0));
    00075 }
    00076
    00083 static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
    00084 {
    00085   switch (GetTileType(t)) {
    00086     case MP_RAILWAY:
    00087       if (GetRailType(t) != RAILTYPE_ELECTRIC) return TRACK_BIT_NONE;
    00088       switch (GetRailTileType(t)) {
    00089         case RAIL_TILE_NORMAL: case RAIL_TILE_SIGNALS:
    00090           return GetTrackBits(t);
    00091         case RAIL_TILE_WAYPOINT:
    00092           return GetRailWaypointBits(t);
    00093         default:
    00094           return TRACK_BIT_NONE;
    00095       }
    00096       break;
    00097
    00098     case MP_TUNNELBRIDGE:
    00099       if (GetRailType(t) != RAILTYPE_ELECTRIC) return TRACK_BIT_NONE;
    00100       if (override != NULL && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
    00101         *override = 1 << GetTunnelBridgeDirection(t);
    00102       }
    00103       return AxisToTrackBits(DiagDirToAxis(GetTunnelBridgeDirection(t)));
    00104
    00105     case MP_ROAD:
    00106       if (!IsLevelCrossing(t)) return TRACK_BIT_NONE;
    00107       if (GetRailType(t) != RAILTYPE_ELECTRIC) return TRACK_BIT_NONE;
    00108       return GetCrossingRailBits(t);
    00109
    00110     case MP_STATION:
    00111       if (!IsRailwayStation(t)) return TRACK_BIT_NONE;
    00112       if (GetRailType(t) != RAILTYPE_ELECTRIC) return TRACK_BIT_NONE;
    00113       if (!IsStationTileElectrifiable(t)) return TRACK_BIT_NONE;
    00114       return TrackToTrackBits(GetRailStationTrack(t));
    00115
    00116     default:
    00117       return TRACK_BIT_NONE;
    00118   }
    00119 }
    00120
    00125 static void AdjustTileh(TileIndex tile, Slope *tileh)
    00126 {
    00127   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00128     if (IsTunnel(tile)) {
    00129       *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
    00130     } else if (*tileh != SLOPE_FLAT) {
    00131       *tileh = SLOPE_FLAT;
    00132     } else {
    00133       *tileh = InclinedSlope(GetTunnelBridgeDirection(tile));
    00134     }
    00135   }
    00136 }
    00137
    00145 static byte GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
    00146 {
    00147   /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
    00148    * PCPs are always on a tile edge.
    00149    *
    00150    * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
    00151    * So we have to move it inside the tile, because if the neighboured tile has a foundation,
    00152    * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopeZ().
    00153    *
    00154    * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
    00155    * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
    00156    * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
    00157    *
    00158    * Also note that the result of GetSlopeZ() is very special on bridge-ramps.
    00159    */
    00160
    00161   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));
    00162   return (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
    00163 }
    00164
    00172 void DrawCatenaryOnTunnel(const TileInfo *ti)
    00173 {
    00174   /* xmin, ymin, xmax + 1, ymax + 1 of BB */
    00175   static const int _tunnel_wire_BB[4][4] = {
    00176     { 0, 1, 16, 15 }, // NE
    00177     { 1, 0, 15, 16 }, // SE
    00178     { 0, 1, 16, 15 }, // SW
    00179     { 1, 0, 15, 16 }, // NW
    00180   };
    00181
    00182   if ((GetRailType(ti->tile) != RAILTYPE_ELECTRIC) || _patches.disable_elrails) return;
    00183
    00184   DiagDirection dir = GetTunnelBridgeDirection(ti->tile);
    00185
    00186   const SortableSpriteStruct *sss = &CatenarySpriteData_Tunnel[dir];
    00187   const int *BB_data = _tunnel_wire_BB[dir];
    00188   AddSortableSpriteToDraw(
    00189     sss->image, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
    00190     BB_data[2] - sss->x_offset, BB_data[3] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset + 1,
    00191     GetTileZ(ti->tile) + sss->z_offset,
    00192     IsTransparencySet(TO_CATENARY),
    00193     BB_data[0] - sss->x_offset, BB_data[1] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset
    00194   );
    00195 }
    00196
    00200 static void DrawCatenaryRailway(const TileInfo *ti)
    00201 {
    00202   /* Pylons are placed on a tile edge, so we need to take into account
    00203    * the track configuration of 2 adjacent tiles. trackconfig[0] stores the
    00204    * current tile (home tile) while [1] holds the neighbour */
    00205   TrackBits trackconfig[TS_END];
    00206   bool isflat[TS_END];
    00207   /* Note that ti->tileh has already been adjusted for Foundations */
    00208   Slope tileh[TS_END] = { ti->tileh, SLOPE_FLAT };
    00209
    00210   /* Half tile slopes coincide only with horizontal/vertical track.
    00211    * Faking a flat slope results in the correct sprites on positions. */
    00212   if (IsHalftileSlope(tileh[TS_HOME])) tileh[TS_HOME] = SLOPE_FLAT;
    00213
    00214   TLG tlg = GetTLG(ti->tile);
    00215   byte PCPstatus = 0;
    00216   byte OverridePCP = 0;
    00217   byte PPPpreferred[DIAGDIR_END];
    00218   byte PPPallowed[DIAGDIR_END];
    00219   DiagDirection i;
    00220   Track t;
    00221
    00222   /* Find which rail bits are present, and select the override points.
    00223    * We don't draw a pylon:
    00224    * 1) INSIDE a tunnel (we wouldn't see it anyway)
    00225    * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
    00226    *    because that one is drawn on the bridge. Exception is for length 0 bridges
    00227    *    which have no middle tiles */
    00228   trackconfig[TS_HOME] = GetRailTrackBitsUniversal(ti->tile, &OverridePCP);
    00229   /* If a track bit is present that is not in the main direction, the track is level */
    00230   isflat[TS_HOME] = ((trackconfig[TS_HOME] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
    00231
    00232   AdjustTileh(ti->tile, &tileh[TS_HOME]);
    00233
    00234   for (i = DIAGDIR_NE; i < DIAGDIR_END; i++) {
    00235     TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
    00236     Foundation foundation = FOUNDATION_NONE;
    00237     int k;
    00238
    00239     /* Here's one of the main headaches. GetTileSlope does not correct for possibly
    00240      * existing foundataions, so we do have to do that manually later on.*/
    00241     tileh[TS_NEIGHBOUR] = GetTileSlope(neighbour, NULL);
    00242     trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, NULL);
    00243     if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
    00244
    00245     /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
    00246      * we have to draw all pillars on the current tile. */
    00247     if (GetPCPElevation(ti->tile, i) != GetPCPElevation(neighbour, ReverseDiagDir(i))) trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
    00248
    00249     isflat[TS_NEIGHBOUR] = ((trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
    00250
    00251     PPPpreferred[i] = 0xFF; // We start with preferring everything (end-of-line in any direction)
    00252     PPPallowed[i] = AllowedPPPonPCP[i];
    00253
    00254     /* We cycle through all the existing tracks at a PCP and see what
    00255      * PPPs we want to have, or may not have at all */
    00256     for (k = 0; k < NUM_TRACKS_AT_PCP; k++) {
    00257       /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
    00258       if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
    00259           IsBridgeTile(neighbour) &&
    00260           GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
    00261         continue;
    00262       }
    00263
    00264       /* We check whether the track in question (k) is present in the tile
    00265        * (TrackSourceTile) */
    00266       if (HasBit(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
    00267         /* track found, if track is in the neighbour tile, adjust the number
    00268          * of the PCP for preferred/allowed determination*/
    00269         DiagDirection PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
    00270         SetBit(PCPstatus, i); // This PCP is in use
    00271
    00272         PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
    00273         PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
    00274       }
    00275     }
    00276
    00277     /* Deactivate all PPPs if PCP is not used */
    00278     PPPpreferred[i] *= HasBit(PCPstatus, i);
    00279     PPPallowed[i] *= HasBit(PCPstatus, i);
    00280
    00281     /* A station is always "flat", so adjust the tileh accordingly */
    00282     if (IsTileType(neighbour, MP_STATION)) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
    00283
    00284     /* Read the foundataions if they are present, and adjust the tileh */
    00285     if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE && IsTileType(neighbour, MP_RAILWAY) && GetRailType(neighbour) == RAILTYPE_ELECTRIC) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
    00286     if (IsBridgeTile(neighbour)) {
    00287       foundation = GetBridgeFoundation(tileh[TS_NEIGHBOUR], DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
    00288     }
    00289
    00290     ApplyFoundationToSlope(foundation, &tileh[TS_NEIGHBOUR]);
    00291
    00292   /* Half tile slopes coincide only with horizontal/vertical track.
    00293    * Faking a flat slope results in the correct sprites on positions. */
    00294     if (IsHalftileSlope(tileh[TS_NEIGHBOUR])) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
    00295
    00296     AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
    00297
    00298     /* If we have a straight (and level) track, we want a pylon only every 2 tiles
    00299      * Delete the PCP if this is the case. */
    00300     /* Level means that the slope is the same, or the track is flat */
    00301     if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
    00302       for (k = 0; k < NUM_IGNORE_GROUPS; k++)
    00303         if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) ClrBit(PCPstatus, i);
    00304     }
    00305
    00306     /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
    00307      * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
    00308      * anything. Note that the preferred PPPs still contain the end-of-line markers.
    00309      * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
    00310     if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
    00311
    00312     if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile)) {
    00313       Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
    00314       uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    00315
    00316       if ((height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) &&
    00317           (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
    00318         SetBit(OverridePCP, i);
    00319       }
    00320     }
    00321
    00322     if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i)) {
    00323       for (k = 0; k < DIR_END; k++) {
    00324         byte temp = PPPorder[i][GetTLG(ti->tile)][k];
    00325
    00326         if (HasBit(PPPallowed[i], temp)) {
    00327           uint x  = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
    00328           uint y  = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
    00329
    00330           /* Don't build the pylon if it would be outside the tile */
    00331           if (!HasBit(OwnedPPPonPCP[i], temp)) {
    00332             /* We have a neighour that will draw it, bail out */
    00333             if (trackconfig[TS_NEIGHBOUR] != 0) break;
    00334             continue; /* No neighbour, go looking for a better position */
    00335           }
    00336
    00337           AddSortableSpriteToDraw(pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
    00338               GetPCPElevation(ti->tile, i),
    00339               IsTransparencySet(TO_CATENARY), -1, -1);
    00340           break; /* We already have drawn a pylon, bail out */
    00341         }
    00342       }
    00343     }
    00344   }
    00345
    00346   /* Don't draw a wire under a low bridge */
    00347   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
    00348     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    00349
    00350     if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
    00351   }
    00352
    00353   /* Drawing of pylons is finished, now draw the wires */
    00354   for (t = TRACK_BEGIN; t < TRACK_END; t++) {
    00355     if (HasBit(trackconfig[TS_HOME], t)) {
    00356       if (IsTunnelTile(ti->tile)) break; // drawn together with tunnel-roof (see DrawCatenaryOnTunnel())
    00357       byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
    00358         (HasBit(PCPstatus, PCPpositions[t][1]) << 1);
    00359
    00360       const SortableSpriteStruct *sss;
    00361       int tileh_selector = !(tileh[TS_HOME] % 3) * tileh[TS_HOME] / 3; /* tileh for the slopes, 0 otherwise */
    00362
    00363       assert(PCPconfig != 0); /* We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that) */
    00364       assert(!IsSteepSlope(tileh[TS_HOME]));
    00365       sss = &CatenarySpriteData[Wires[tileh_selector][t][PCPconfig]];
    00366
    00367       /*
    00368        * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
    00369        * Therefore it is save to use GetSlopeZ() for the elevation.
    00370        * Also note, that the result of GetSlopeZ() is very special for bridge-ramps.
    00371        */
    00372       AddSortableSpriteToDraw(sss->image, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
    00373         sss->x_size, sss->y_size, sss->z_size, GetSlopeZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
    00374         IsTransparencySet(TO_CATENARY));
    00375     }
    00376   }
    00377 }
    00378
    00379 static void DrawCatenaryOnBridge(const TileInfo *ti)
    00380 {
    00381   TileIndex end = GetSouthernBridgeEnd(ti->tile);
    00382   TileIndex start = GetOtherBridgeEnd(end);
    00383
    00384   uint length = GetTunnelBridgeLength(start, end);
    00385   uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
    00386   uint height;
    00387
    00388   const SortableSpriteStruct *sss;
    00389   Axis axis = GetBridgeAxis(ti->tile);
    00390   TLG tlg = GetTLG(ti->tile);
    00391
    00392   CatenarySprite offset = (CatenarySprite)(axis == AXIS_X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
    00393
    00394   if ((length % 2) && num == length) {
    00395     /* Draw the "short" wire on the southern end of the bridge
    00396      * only needed if the length of the bridge is odd */
    00397     sss = &CatenarySpriteData[WIRE_X_FLAT_BOTH + offset];
    00398   } else {
    00399     /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
    00400     sss = &CatenarySpriteData[WIRE_X_FLAT_SW + (num % 2) + offset];
    00401   }
    00402
    00403   height = GetBridgeHeight(end);
    00404
    00405   AddSortableSpriteToDraw(sss->image, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
    00406     sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
    00407     IsTransparencySet(TO_CATENARY)
    00408   );
    00409
    00410   /* Finished with wires, draw pylons */
    00411   /* every other tile needs a pylon on the northern end */
    00412   if (num % 2) {
    00413     DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_NE : DIAGDIR_NW);
    00414     Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
    00415     if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
    00416     uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
    00417     uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
    00418     AddSortableSpriteToDraw(pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
    00419   }
    00420
    00421   /* need a pylon on the southern end of the bridge */
    00422   if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
    00423     DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE);
    00424     Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
    00425     if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
    00426     uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
    00427     uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
    00428     AddSortableSpriteToDraw(pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
    00429   }
    00430 }
    00431
    00432 void DrawCatenary(const TileInfo *ti)
    00433 {
    00434   if (_patches.disable_elrails) return;
    00435
    00436   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile)) {
    00437     TileIndex head = GetNorthernBridgeEnd(ti->tile);
    00438
    00439     if (GetTunnelBridgeTransportType(head) == TRANSPORT_RAIL && GetRailType(head) == RAILTYPE_ELECTRIC) {
    00440       DrawCatenaryOnBridge(ti);
    00441     }
    00442   }
    00443
    00444   switch (GetTileType(ti->tile)) {
    00445     case MP_RAILWAY:
    00446       if (IsRailDepot(ti->tile)) {
    00447         const SortableSpriteStruct *sss = &CatenarySpriteData_Depot[GetRailDepotDirection(ti->tile)];
    00448
    00449         /* This wire is not visible with the default depot sprites */
    00450         AddSortableSpriteToDraw(
    00451           sss->image, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
    00452           sss->x_size, sss->y_size, sss->z_size,
    00453           GetTileMaxZ(ti->tile) + sss->z_offset,
    00454           IsTransparencySet(TO_CATENARY)
    00455         );
    00456         return;
    00457       }
    00458       break;
    00459
    00460     case MP_TUNNELBRIDGE:
    00461     case MP_ROAD:
    00462     case MP_STATION:
    00463       break;
    00464
    00465     default: return;
    00466   }
    00467   DrawCatenaryRailway(ti);
    00468 }
    00469
    00470 int32 SettingsDisableElrail(int32 p1)
    00471 {
    00472   Vehicle *v;
    00473   Player *p;
    00474   bool disable = (p1 != 0);
    00475
    00476   /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
    00477   const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
    00478   const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
    00479
    00480   /* walk through all train engines */
    00481   EngineID eid;
    00482   FOR_ALL_ENGINEIDS_OF_TYPE(eid, VEH_TRAIN) {
    00483     RailVehicleInfo *rv_info = &_rail_vehicle_info[eid];
    00484     /* if it is an electric rail engine and its railtype is the wrong one */
    00485     if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
    00486       /* change it to the proper one */
    00487       rv_info->railtype = new_railtype;
    00488     }
    00489   }
    00490
    00491   /* when disabling elrails, make sure that all existing trains can run on
    00492   *  normal rail too */
    00493   if (disable) {
    00494     FOR_ALL_VEHICLES(v) {
    00495       if (v->type == VEH_TRAIN && v->u.rail.railtype == RAILTYPE_ELECTRIC) {
    00496         /* this railroad vehicle is now compatible only with elrail,
    00497         *  so add there also normal rail compatibility */
    00498         v->u.rail.compatible_railtypes |= RAILTYPES_RAIL;
    00499         v->u.rail.railtype = RAILTYPE_RAIL;
    00500         SetBit(v->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
    00501       }
    00502     }
    00503   }
    00504
    00505   /* Fix the total power and acceleration for trains */
    00506   FOR_ALL_VEHICLES(v) {
    00507     /* power and acceleration is cached only for front engines */
    00508     if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
    00509       TrainPowerChanged(v);
    00510       UpdateTrainAcceleration(v);
    00511     }
    00512   }
    00513
    00514   FOR_ALL_PLAYERS(p) p->avail_railtypes = GetPlayerRailtypes(p->index);
    00515
    00516   /* This resets the _last_built_railtype, which will be invalid for electric
    00517   * rails. It may have unintended consequences if that function is ever
    00518   * extended, though. */
    00519   ReinitGuiAfterToggleElrail(disable);
    00520   return 0;
    00521 }