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