road.cpp

Go to the documentation of this file.
00001 /* $Id: road.cpp 21846 2011-01-18 23:09:43Z 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 
00045 RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
00046 {
00047   if (!IsValidTile(tile)) return ROAD_NONE;
00048   for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00049     const TileIndex neighbor_tile = TileAddByDiagDir(tile, dir);
00050 
00051     /* Get the Roadbit pointing to the neighbor_tile */
00052     const RoadBits target_rb = DiagDirToRoadBits(dir);
00053 
00054     /* If the roadbit is in the current plan */
00055     if (org_rb & target_rb) {
00056       bool connective = false;
00057       const RoadBits mirrored_rb = MirrorRoadBits(target_rb);
00058 
00059       switch (GetTileType(neighbor_tile)) {
00060         /* Allways connective ones */
00061         case MP_CLEAR: case MP_TREES:
00062           connective = true;
00063           break;
00064 
00065         /* The conditionally connective ones */
00066         case MP_TUNNELBRIDGE:
00067         case MP_STATION:
00068         case MP_ROAD: {
00069           const RoadBits neighbor_rb = GetAnyRoadBits(neighbor_tile, ROADTYPE_ROAD) | GetAnyRoadBits(neighbor_tile, ROADTYPE_TRAM);
00070 
00071           /* Accept only connective tiles */
00072           connective = (neighbor_rb & mirrored_rb) || // Neighbor has got the fitting RoadBit
00073               HasExactlyOneBit(neighbor_rb); // Neighbor has got only one Roadbit
00074 
00075           break;
00076         }
00077 
00078         case MP_RAILWAY:
00079           connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
00080           break;
00081 
00082         case MP_WATER:
00083           /* Check for real water tile */
00084           connective = !IsWater(neighbor_tile);
00085           break;
00086 
00087         /* The defentetly not connective ones */
00088         default: break;
00089       }
00090 
00091       /* If the neighbor tile is inconnective remove the planed road connection to it */
00092       if (!connective) org_rb ^= target_rb;
00093 
00094     }
00095   }
00096 
00097   return org_rb;
00098 }
00099 
00106 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
00107 {
00108   RoadTypes avail_roadtypes;
00109 
00110   if (company == OWNER_TOWN || _game_mode == GM_EDITOR || IsGeneratingWorld()) {
00111     avail_roadtypes = ROADTYPES_ROAD;
00112   } else {
00113     Company *c = Company::GetIfValid(company);
00114     if (c == NULL) return false;
00115     avail_roadtypes = (RoadTypes)c->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody
00116   }
00117   return (rts & ~avail_roadtypes) == 0;
00118 }
00119 
00125 bool ValParamRoadType(const RoadType rt)
00126 {
00127   return HasRoadTypesAvail(_current_company, RoadTypeToRoadTypes(rt));
00128 }
00129 
00135 RoadTypes GetCompanyRoadtypes(CompanyID company)
00136 {
00137   RoadTypes rt = ROADTYPES_NONE;
00138 
00139   Engine *e;
00140   FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
00141     const EngineInfo *ei = &e->info;
00142 
00143     if (HasBit(ei->climates, _settings_game.game_creation.landscape) &&
00144         (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
00145       SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00146     }
00147   }
00148 
00149   return rt;
00150 }

Generated on Thu Jan 20 22:57:39 2011 for OpenTTD by  doxygen 1.6.1