road.cpp

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

Generated on Fri Jul 31 22:33:17 2009 for OpenTTD by  doxygen 1.5.6