# rail.cpp

Go to the documentation of this file.

    00001 /* $Id: rail.cpp 19665 2010-04-17 22:27:49Z 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
    00012 #include "stdafx.h"
    00013 #include "station_map.h"
    00014 #include "tunnelbridge_map.h"
    00015 #include "date_func.h"
    00016 #include "company_func.h"
    00017 #include "company_base.h"
    00018 #include "engine_base.h"
    00019
    00020 /* XXX: Below 3 tables store duplicate data. Maybe remove some? */
    00021 /* Maps a trackdir to the bit that stores its status in the map arrays, in the
    00022  * direction along with the trackdir */
    00023 extern const byte _signal_along_trackdir[TRACKDIR_END] = {
    00024   0x8, 0x8, 0x8, 0x2, 0x4, 0x1, 0, 0,
    00025   0x4, 0x4, 0x4, 0x1, 0x8, 0x2
    00026 };
    00027
    00028 /* Maps a trackdir to the bit that stores its status in the map arrays, in the
    00029  * direction against the trackdir */
    00030 extern const byte _signal_against_trackdir[TRACKDIR_END] = {
    00031   0x4, 0x4, 0x4, 0x1, 0x8, 0x2, 0, 0,
    00032   0x8, 0x8, 0x8, 0x2, 0x4, 0x1
    00033 };
    00034
    00035 /* Maps a Track to the bits that store the status of the two signals that can
    00036  * be present on the given track */
    00037 extern const byte _signal_on_track[] = {
    00038   0xC, 0xC, 0xC, 0x3, 0xC, 0x3
    00039 };
    00040
    00041 /* Maps a diagonal direction to the all trackdirs that are connected to any
    00042  * track entering in this direction (including those making 90 degree turns)
    00043  */
    00044 extern const TrackdirBits _exitdir_reaches_trackdirs[] = {
    00045   TRACKDIR_BIT_X_NE | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_LEFT_N,  // DIAGDIR_NE
    00046   TRACKDIR_BIT_Y_SE | TRACKDIR_BIT_LEFT_S  | TRACKDIR_BIT_UPPER_E, // DIAGDIR_SE
    00047   TRACKDIR_BIT_X_SW | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_RIGHT_S, // DIAGDIR_SW
    00048   TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_W  // DIAGDIR_NW
    00049 };
    00050
    00051 extern const Trackdir _next_trackdir[TRACKDIR_END] = {
    00052   TRACKDIR_X_NE,  TRACKDIR_Y_SE,  TRACKDIR_LOWER_E, TRACKDIR_UPPER_E, TRACKDIR_RIGHT_S, TRACKDIR_LEFT_S, INVALID_TRACKDIR, INVALID_TRACKDIR,
    00053   TRACKDIR_X_SW,  TRACKDIR_Y_NW,  TRACKDIR_LOWER_W, TRACKDIR_UPPER_W, TRACKDIR_RIGHT_N, TRACKDIR_LEFT_N
    00054 };
    00055
    00056 /* Maps a trackdir to all trackdirs that make 90 deg turns with it. */
    00057 extern const TrackdirBits _track_crosses_trackdirs[TRACKDIR_END] = {
    00058   TRACKDIR_BIT_Y_SE     | TRACKDIR_BIT_Y_NW,                                                   // TRACK_X
    00059   TRACKDIR_BIT_X_NE     | TRACKDIR_BIT_X_SW,                                                   // TRACK_Y
    00060   TRACKDIR_BIT_RIGHT_N  | TRACKDIR_BIT_RIGHT_S  | TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LEFT_S,  // TRACK_UPPER
    00061   TRACKDIR_BIT_RIGHT_N  | TRACKDIR_BIT_RIGHT_S  | TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LEFT_S,  // TRACK_LOWER
    00062   TRACKDIR_BIT_UPPER_W  | TRACKDIR_BIT_UPPER_E  | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_LOWER_E, // TRACK_LEFT
    00063   TRACKDIR_BIT_UPPER_W  | TRACKDIR_BIT_UPPER_E  | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_LOWER_E  // TRACK_RIGHT
    00064 };
    00065
    00066 /* Maps a track to all tracks that make 90 deg turns with it. */
    00067 extern const TrackBits _track_crosses_tracks[] = {
    00068   TRACK_BIT_Y,    // TRACK_X
    00069   TRACK_BIT_X,    // TRACK_Y
    00070   TRACK_BIT_VERT, // TRACK_UPPER
    00071   TRACK_BIT_VERT, // TRACK_LOWER
    00072   TRACK_BIT_HORZ, // TRACK_LEFT
    00073   TRACK_BIT_HORZ  // TRACK_RIGHT
    00074 };
    00075
    00076 /* Maps a trackdir to the (4-way) direction the tile is exited when following
    00077  * that trackdir */
    00078 extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END] = {
    00079   DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_NE,
    00080   DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE,
    00081 };
    00082
    00083 extern const Trackdir _track_exitdir_to_trackdir[][DIAGDIR_END] = {
    00084   {TRACKDIR_X_NE,     INVALID_TRACKDIR,  TRACKDIR_X_SW,     INVALID_TRACKDIR},
    00085   {INVALID_TRACKDIR,  TRACKDIR_Y_SE,     INVALID_TRACKDIR,  TRACKDIR_Y_NW},
    00086   {TRACKDIR_UPPER_E,  INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_UPPER_W},
    00087   {INVALID_TRACKDIR,  TRACKDIR_LOWER_E,  TRACKDIR_LOWER_W,  INVALID_TRACKDIR},
    00088   {INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_LEFT_S,   TRACKDIR_LEFT_N},
    00089   {TRACKDIR_RIGHT_N,  TRACKDIR_RIGHT_S,  INVALID_TRACKDIR,  INVALID_TRACKDIR}
    00090 };
    00091
    00092 extern const Trackdir _track_enterdir_to_trackdir[][DIAGDIR_END] = {
    00093   {TRACKDIR_X_NE,     INVALID_TRACKDIR,  TRACKDIR_X_SW,     INVALID_TRACKDIR},
    00094   {INVALID_TRACKDIR,  TRACKDIR_Y_SE,     INVALID_TRACKDIR,  TRACKDIR_Y_NW},
    00095   {INVALID_TRACKDIR,  TRACKDIR_UPPER_E,  TRACKDIR_UPPER_W,  INVALID_TRACKDIR},
    00096   {TRACKDIR_LOWER_E,  INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_LOWER_W},
    00097   {TRACKDIR_LEFT_N,   TRACKDIR_LEFT_S,   INVALID_TRACKDIR,  INVALID_TRACKDIR},
    00098   {INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_RIGHT_S,  TRACKDIR_RIGHT_N}
    00099 };
    00100
    00101 extern const Trackdir _track_direction_to_trackdir[][DIR_END] = {
    00102   {INVALID_TRACKDIR, TRACKDIR_X_NE,     INVALID_TRACKDIR, INVALID_TRACKDIR,  INVALID_TRACKDIR, TRACKDIR_X_SW,     INVALID_TRACKDIR, INVALID_TRACKDIR},
    00103   {INVALID_TRACKDIR, INVALID_TRACKDIR,  INVALID_TRACKDIR, TRACKDIR_Y_SE,     INVALID_TRACKDIR, INVALID_TRACKDIR,  INVALID_TRACKDIR, TRACKDIR_Y_NW},
    00104   {INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_UPPER_E, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_UPPER_W, INVALID_TRACKDIR},
    00105   {INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_LOWER_E, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_LOWER_W, INVALID_TRACKDIR},
    00106   {TRACKDIR_LEFT_N,  INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_LEFT_S,  INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR},
    00107   {TRACKDIR_RIGHT_N, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_RIGHT_S, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR}
    00108 };
    00109
    00110 extern const Trackdir _dir_to_diag_trackdir[] = {
    00111   TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW,
    00112 };
    00113
    00114 extern const TrackBits _corner_to_trackbits[] = {
    00115   TRACK_BIT_LEFT, TRACK_BIT_LOWER, TRACK_BIT_RIGHT, TRACK_BIT_UPPER,
    00116 };
    00117
    00118 extern const TrackdirBits _uphill_trackdirs[] = {
    00119   TRACKDIR_BIT_NONE                    ,
    00120   TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_NW,
    00121   TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_SE,
    00122   TRACKDIR_BIT_X_SW                    ,
    00123   TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_SE,
    00124   TRACKDIR_BIT_NONE                    ,
    00125   TRACKDIR_BIT_Y_SE                    ,
    00126   TRACKDIR_BIT_NONE                    ,
    00127   TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_NW,
    00128   TRACKDIR_BIT_Y_NW                    ,
    00129   TRACKDIR_BIT_NONE                    ,
    00130   TRACKDIR_BIT_NONE                    ,
    00131   TRACKDIR_BIT_X_NE                    ,
    00132   TRACKDIR_BIT_NONE                    ,
    00133   TRACKDIR_BIT_NONE                    ,
    00134   TRACKDIR_BIT_NONE                    ,
    00135   TRACKDIR_BIT_NONE                    ,
    00136   TRACKDIR_BIT_NONE                    ,
    00137   TRACKDIR_BIT_NONE                    ,
    00138   TRACKDIR_BIT_NONE                    ,
    00139   TRACKDIR_BIT_NONE                    ,
    00140   TRACKDIR_BIT_NONE                    ,
    00141   TRACKDIR_BIT_NONE                    ,
    00142   TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_SE,
    00143   TRACKDIR_BIT_NONE                    ,
    00144   TRACKDIR_BIT_NONE                    ,
    00145   TRACKDIR_BIT_NONE                    ,
    00146   TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_NW,
    00147   TRACKDIR_BIT_NONE                    ,
    00148   TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_NW,
    00149   TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_SE,
    00150 };
    00151
    00152 RailType GetTileRailType(TileIndex tile)
    00153 {
    00154   switch (GetTileType(tile)) {
    00155     case MP_RAILWAY:
    00156       return GetRailType(tile);
    00157
    00158     case MP_ROAD:
    00159       /* rail/road crossing */
    00160       if (IsLevelCrossing(tile)) return GetRailType(tile);
    00161       break;
    00162
    00163     case MP_STATION:
    00164       if (HasStationRail(tile)) return GetRailType(tile);
    00165       break;
    00166
    00167     case MP_TUNNELBRIDGE:
    00168       if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) return GetRailType(tile);
    00169       break;
    00170
    00171     default:
    00172       break;
    00173   }
    00174   return INVALID_RAILTYPE;
    00175 }
    00176
    00177 bool HasRailtypeAvail(const CompanyID company, const RailType railtype)
    00178 {
    00179   return HasBit(Company::Get(company)->avail_railtypes, railtype);
    00180 }
    00181
    00182 bool ValParamRailtype(const RailType rail)
    00183 {
    00184   return rail < RAILTYPE_END && HasRailtypeAvail(_current_company, rail);
    00185 }
    00186
    00187 RailType GetBestRailtype(const CompanyID company)
    00188 {
    00189   if (HasRailtypeAvail(company, RAILTYPE_MAGLEV)) return RAILTYPE_MAGLEV;
    00190   if (HasRailtypeAvail(company, RAILTYPE_MONO)) return RAILTYPE_MONO;
    00191   if (HasRailtypeAvail(company, RAILTYPE_ELECTRIC)) return RAILTYPE_ELECTRIC;
    00192   return RAILTYPE_RAIL;
    00193 }
    00194
    00195 RailTypes GetCompanyRailtypes(CompanyID company)
    00196 {
    00197   RailTypes rt = RAILTYPES_NONE;
    00198
    00199   Engine *e;
    00200   FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
    00201     const EngineInfo *ei = &e->info;
    00202
    00203     if (HasBit(ei->climates, _settings_game.game_creation.landscape) &&
    00204         (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
    00205       const RailVehicleInfo *rvi = &e->u.rail;
    00206
    00207       if (rvi->railveh_type != RAILVEH_WAGON) {
    00208         assert(rvi->railtype < RAILTYPE_END);
    00209         SetBit(rt, rvi->railtype);
    00210       }
    00211     }
    00212   }
    00213
    00214   return rt;
    00215 }
    00216
    00217 RailType GetRailTypeByLabel(RailTypeLabel label)
    00218 {
    00219   /* Loop through each rail type until the label is found */
    00220   for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
    00221     const RailtypeInfo *rti = GetRailTypeInfo(r);
    00222     if (rti->label == label) return r;
    00223   }
    00224
    00225   /* No matching label was found, so it is invalid */
    00226   return INVALID_RAILTYPE;
    00227 }