# road.cpp

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