# road.cpp

Go to the documentation of this file.

    00001 /* $Id: road.cpp 18028 2009-11-09 10:40:33Z 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 "openttd.h"
    00014 #include "rail_map.h"
    00015 #include "road_map.h"
    00016 #include "water_map.h"
    00017 #include "genworld.h"
    00018 #include "company_func.h"
    00019 #include "company_base.h"
    00020 #include "engine_base.h"
    00021 #include "date_func.h"
    00022 #include "landscape.h"
    00023
    00031 static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
    00032 {
    00033   return (IsTileType(tile, MP_RAILWAY) &&
    00034     GetRailTileType(tile) == RAIL_TILE_NORMAL &&
    00035     GetTrackBits(tile) == (ax == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) &&
    00036     GetFoundationSlope(tile, NULL) == SLOPE_FLAT);
    00037 }
    00038
    00039 RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
    00040 {
    00041   if (!IsValidTile(tile)) return ROAD_NONE;
    00042   for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
    00043     const TileIndex neighbor_tile = TileAddByDiagDir(tile, dir);
    00044
    00045     /* Get the Roadbit pointing to the neighbor_tile */
    00046     const RoadBits target_rb = DiagDirToRoadBits(dir);
    00047
    00048     /* If the roadbit is in the current plan */
    00049     if (org_rb & target_rb) {
    00050       bool connective = false;
    00051       const RoadBits mirrored_rb = MirrorRoadBits(target_rb);
    00052
    00053       switch (GetTileType(neighbor_tile)) {
    00054         /* Allways connective ones */
    00055         case MP_CLEAR: case MP_TREES:
    00056           connective = true;
    00057           break;
    00058
    00059         /* The conditionaly connective ones */
    00060         case MP_TUNNELBRIDGE:
    00061         case MP_STATION:
    00062         case MP_ROAD: {
    00063           const RoadBits neighbor_rb = GetAnyRoadBits(neighbor_tile, ROADTYPE_ROAD) | GetAnyRoadBits(neighbor_tile, ROADTYPE_TRAM);
    00064
    00065           /* Accept only connective tiles */
    00066           connective = (neighbor_rb & mirrored_rb) || // Neighbor has got the fitting RoadBit
    00067               CountBits(neighbor_rb) == 1; // Neighbor has got only one Roadbit
    00068
    00069         } break;
    00070
    00071         case MP_RAILWAY:
    00072           connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
    00073           break;
    00074
    00075         case MP_WATER:
    00076           /* Check for real water tile */
    00077           connective = !IsWater(neighbor_tile);
    00078           break;
    00079
    00080         /* The defentetly not connective ones */
    00081         default: break;
    00082       }
    00083
    00084       /* If the neighbor tile is inconnective remove the planed road connection to it */
    00085       if (!connective) org_rb ^= target_rb;
    00086
    00087     }
    00088   }
    00089
    00090   return org_rb;
    00091 }
    00092
    00093 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
    00094 {
    00095   RoadTypes avail_roadtypes;
    00096
    00097   if (company == OWNER_TOWN || _game_mode == GM_EDITOR || IsGeneratingWorld()) {
    00098     avail_roadtypes = ROADTYPES_ROAD;
    00099   } else {
    00100     Company *c = Company::GetIfValid(company);
    00101     if (c == NULL) return false;
    00102     avail_roadtypes = (RoadTypes)c->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody
    00103   }
    00104   return (rts & ~avail_roadtypes) == 0;
    00105 }
    00106
    00107 bool ValParamRoadType(const RoadType rt)
    00108 {
    00109   return HasRoadTypesAvail(_current_company, RoadTypeToRoadTypes(rt));
    00110 }
    00111
    00112 RoadTypes GetCompanyRoadtypes(CompanyID company)
    00113 {
    00114   RoadTypes rt = ROADTYPES_NONE;
    00115
    00116   Engine *e;
    00117   FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
    00118     const EngineInfo *ei = &e->info;
    00119
    00120     if (HasBit(ei->climates, _settings_game.game_creation.landscape) &&
    00121         (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
    00122       SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
    00123     }
    00124   }
    00125
    00126   return rt;
    00127 }