road.cpp

Go to the documentation of this file.
00001 /* $Id: road.cpp 20281 2010-08-01 18:53:30Z 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 conditionally 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               HasExactlyOneBit(neighbor_rb); // Neighbor has got only one Roadbit
00068 
00069           break;
00070         }
00071 
00072         case MP_RAILWAY:
00073           connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
00074           break;
00075 
00076         case MP_WATER:
00077           /* Check for real water tile */
00078           connective = !IsWater(neighbor_tile);
00079           break;
00080 
00081         /* The defentetly not connective ones */
00082         default: break;
00083       }
00084 
00085       /* If the neighbor tile is inconnective remove the planed road connection to it */
00086       if (!connective) org_rb ^= target_rb;
00087 
00088     }
00089   }
00090 
00091   return org_rb;
00092 }
00093 
00094 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
00095 {
00096   RoadTypes avail_roadtypes;
00097 
00098   if (company == OWNER_TOWN || _game_mode == GM_EDITOR || IsGeneratingWorld()) {
00099     avail_roadtypes = ROADTYPES_ROAD;
00100   } else {
00101     Company *c = Company::GetIfValid(company);
00102     if (c == NULL) return false;
00103     avail_roadtypes = (RoadTypes)c->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody
00104   }
00105   return (rts & ~avail_roadtypes) == 0;
00106 }
00107 
00108 bool ValParamRoadType(const RoadType rt)
00109 {
00110   return HasRoadTypesAvail(_current_company, RoadTypeToRoadTypes(rt));
00111 }
00112 
00113 RoadTypes GetCompanyRoadtypes(CompanyID company)
00114 {
00115   RoadTypes rt = ROADTYPES_NONE;
00116 
00117   Engine *e;
00118   FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
00119     const EngineInfo *ei = &e->info;
00120 
00121     if (HasBit(ei->climates, _settings_game.game_creation.landscape) &&
00122         (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
00123       SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00124     }
00125   }
00126 
00127   return rt;
00128 }

Generated on Fri Dec 31 17:15:37 2010 for OpenTTD by  doxygen 1.6.1