road_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: road_cmd.cpp 20095 2010-07-08 19:59:13Z 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 "cmd_helper.h"
00014 #include "road_internal.h"
00015 #include "landscape.h"
00016 #include "viewport_func.h"
00017 #include "command_func.h"
00018 #include "pathfinder/yapf/yapf_cache.h"
00019 #include "depot_base.h"
00020 #include "newgrf.h"
00021 #include "variables.h"
00022 #include "autoslope.h"
00023 #include "tunnelbridge_map.h"
00024 #include "window_func.h"
00025 #include "strings_func.h"
00026 #include "vehicle_func.h"
00027 #include "sound_func.h"
00028 #include "tunnelbridge.h"
00029 #include "cheat_type.h"
00030 #include "functions.h"
00031 #include "effectvehicle_func.h"
00032 #include "effectvehicle_base.h"
00033 #include "elrail_func.h"
00034 #include "roadveh.h"
00035 #include "town.h"
00036 #include "company_base.h"
00037 #include "core/random_func.hpp"
00038 #include "newgrf_railtype.h"
00039 
00040 #include "table/strings.h"
00041 
00046 bool RoadVehiclesAreBuilt()
00047 {
00048   const RoadVehicle *rv;
00049   FOR_ALL_ROADVEHICLES(rv) return true;
00050 
00051   return false;
00052 }
00053 
00054 #define M(x) (1 << (x))
00055 /* Level crossings may only be built on these slopes */
00056 static const uint32 VALID_LEVEL_CROSSING_SLOPES = (M(SLOPE_SEN) | M(SLOPE_ENW) | M(SLOPE_NWS) | M(SLOPE_NS) | M(SLOPE_WSE) | M(SLOPE_EW) | M(SLOPE_FLAT));
00057 #undef M
00058 
00059 /* Invalid RoadBits on slopes  */
00060 static const RoadBits _invalid_tileh_slopes_road[2][15] = {
00061   /* The inverse of the mixable RoadBits on a leveled slope */
00062   {
00063     ROAD_NONE,         // SLOPE_FLAT
00064     ROAD_NE | ROAD_SE, // SLOPE_W
00065     ROAD_NE | ROAD_NW, // SLOPE_S
00066 
00067     ROAD_NE,           // SLOPE_SW
00068     ROAD_NW | ROAD_SW, // SLOPE_E
00069     ROAD_NONE,         // SLOPE_EW
00070 
00071     ROAD_NW,           // SLOPE_SE
00072     ROAD_NONE,         // SLOPE_WSE
00073     ROAD_SE | ROAD_SW, // SLOPE_N
00074 
00075     ROAD_SE,           // SLOPE_NW
00076     ROAD_NONE,         // SLOPE_NS
00077     ROAD_NONE,         // SLOPE_ENW
00078 
00079     ROAD_SW,           // SLOPE_NE
00080     ROAD_NONE,         // SLOPE_SEN
00081     ROAD_NONE          // SLOPE_NWS
00082   },
00083   /* The inverse of the allowed straight roads on a slope
00084    * (with and without a foundation). */
00085   {
00086     ROAD_NONE, // SLOPE_FLAT
00087     ROAD_NONE, // SLOPE_W    Foundation
00088     ROAD_NONE, // SLOPE_S    Foundation
00089 
00090     ROAD_Y,    // SLOPE_SW
00091     ROAD_NONE, // SLOPE_E    Foundation
00092     ROAD_ALL,  // SLOPE_EW
00093 
00094     ROAD_X,    // SLOPE_SE
00095     ROAD_ALL,  // SLOPE_WSE
00096     ROAD_NONE, // SLOPE_N    Foundation
00097 
00098     ROAD_X,    // SLOPE_NW
00099     ROAD_ALL,  // SLOPE_NS
00100     ROAD_ALL,  // SLOPE_ENW
00101 
00102     ROAD_Y,    // SLOPE_NE
00103     ROAD_ALL,  // SLOPE_SEN
00104     ROAD_ALL   // SLOPE_NW
00105   }
00106 };
00107 
00108 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits);
00109 
00120 bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, RoadType rt, DoCommandFlag flags, bool town_check)
00121 {
00122   if (_game_mode == GM_EDITOR || remove == ROAD_NONE) return true;
00123 
00124   /* Water can always flood and towns can always remove "normal" road pieces.
00125    * Towns are not be allowed to remove non "normal" road pieces, like tram
00126    * tracks as that would result in trams that cannot turn. */
00127   if (_current_company == OWNER_WATER ||
00128       (rt == ROADTYPE_ROAD && !Company::IsValidID(_current_company))) return true;
00129 
00130   /* Only do the special processing if the road is owned
00131    * by a town */
00132   if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner);
00133 
00134   if (!town_check) return true;
00135 
00136   if (_cheats.magic_bulldozer.value) return true;
00137 
00138   Town *t = ClosestTownFromTile(tile, UINT_MAX);
00139   if (t == NULL) return true;
00140 
00141   /* check if you're allowed to remove the street owned by a town
00142    * removal allowance depends on difficulty setting */
00143   if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return false;
00144 
00145   /* Get a bitmask of which neighbouring roads has a tile */
00146   RoadBits n = ROAD_NONE;
00147   RoadBits present = GetAnyRoadBits(tile, rt);
00148   if ((present & ROAD_NE) && (GetAnyRoadBits(TILE_ADDXY(tile, -1,  0), rt) & ROAD_SW)) n |= ROAD_NE;
00149   if ((present & ROAD_SE) && (GetAnyRoadBits(TILE_ADDXY(tile,  0,  1), rt) & ROAD_NW)) n |= ROAD_SE;
00150   if ((present & ROAD_SW) && (GetAnyRoadBits(TILE_ADDXY(tile,  1,  0), rt) & ROAD_NE)) n |= ROAD_SW;
00151   if ((present & ROAD_NW) && (GetAnyRoadBits(TILE_ADDXY(tile,  0, -1), rt) & ROAD_SE)) n |= ROAD_NW;
00152 
00153   int rating_decrease = RATING_ROAD_DOWN_STEP_EDGE;
00154   /* If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
00155    * then allow it */
00156   if (KillFirstBit(n) != ROAD_NONE && (n & remove) != ROAD_NONE) {
00157     /* you can remove all kind of roads with extra dynamite */
00158     if (!_settings_game.construction.extra_dynamite) {
00159       SetDParam(0, t->index);
00160       _error_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS;
00161       return false;
00162     }
00163     rating_decrease = RATING_ROAD_DOWN_STEP_INNER;
00164   }
00165   ChangeTownRating(t, rating_decrease, RATING_ROAD_MINIMUM, flags);
00166 
00167   return true;
00168 }
00169 
00170 
00179 static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits pieces, RoadType rt, bool crossing_check, bool town_check = true)
00180 {
00181   RoadTypes rts = GetRoadTypes(tile);
00182   /* The tile doesn't have the given road type */
00183   if (!HasBit(rts, rt)) return CMD_ERROR;
00184 
00185   switch (GetTileType(tile)) {
00186     case MP_ROAD:
00187       if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00188       break;
00189 
00190     case MP_STATION:
00191       if (!IsDriveThroughStopTile(tile)) return CMD_ERROR;
00192       if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00193       break;
00194 
00195     case MP_TUNNELBRIDGE:
00196       if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR;
00197       if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
00198       break;
00199 
00200     default:
00201       return CMD_ERROR;
00202   }
00203 
00204   if (!CheckAllowRemoveRoad(tile, pieces, GetRoadOwner(tile, rt), rt, flags, town_check)) return CMD_ERROR;
00205 
00206   if (!IsTileType(tile, MP_ROAD)) {
00207     /* If it's the last roadtype, just clear the whole tile */
00208     if (rts == RoadTypeToRoadTypes(rt)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00209 
00210     CommandCost cost(EXPENSES_CONSTRUCTION);
00211     if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00212       TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
00213       /* Pay for *every* tile of the bridge or tunnel */
00214       cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price[PR_CLEAR_ROAD]);
00215       if (flags & DC_EXEC) {
00216         SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt));
00217         SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
00218 
00219         /* If the owner of the bridge sells all it's road, also move the ownership
00220          * to the owner of the other roadtype. */
00221         RoadType other_rt = (rt == ROADTYPE_ROAD) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
00222         Owner other_owner = GetRoadOwner(tile, other_rt);
00223         if (other_owner != GetTileOwner(tile)) {
00224           SetTileOwner(tile, other_owner);
00225           SetTileOwner(other_end, other_owner);
00226         }
00227 
00228         /* Mark tiles diry that have been repaved */
00229         MarkTileDirtyByTile(tile);
00230         MarkTileDirtyByTile(other_end);
00231         if (IsBridge(tile)) {
00232           TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
00233 
00234           for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
00235         }
00236       }
00237     } else {
00238       assert(IsDriveThroughStopTile(tile));
00239       cost.AddCost(_price[PR_CLEAR_ROAD] * 2);
00240       if (flags & DC_EXEC) {
00241         SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
00242         MarkTileDirtyByTile(tile);
00243       }
00244     }
00245     return cost;
00246   }
00247 
00248   switch (GetRoadTileType(tile)) {
00249     case ROAD_TILE_NORMAL: {
00250       const Slope tileh = GetTileSlope(tile, NULL);
00251       RoadBits present = GetRoadBits(tile, rt);
00252       const RoadBits other = GetOtherRoadBits(tile, rt);
00253       const Foundation f = GetRoadFoundation(tileh, present);
00254 
00255       if (HasRoadWorks(tile) && _current_company != OWNER_WATER) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
00256 
00257       /* Autocomplete to a straight road
00258        * @li on steep slopes
00259        * @li if the bits of the other roadtypes result in another foundation
00260        * @li if build on slopes is disabled */
00261       if (IsSteepSlope(tileh) || (IsStraightRoad(other) &&
00262           (other & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) != ROAD_NONE) ||
00263           (tileh != SLOPE_FLAT && !_settings_game.construction.build_on_slopes)) {
00264         pieces |= MirrorRoadBits(pieces);
00265       }
00266 
00267       /* limit the bits to delete to the existing bits. */
00268       pieces &= present;
00269       if (pieces == ROAD_NONE) return CMD_ERROR;
00270 
00271       /* Now set present what it will be after the remove */
00272       present ^= pieces;
00273 
00274       /* Check for invalid RoadBit combinations on slopes */
00275       if (tileh != SLOPE_FLAT && present != ROAD_NONE &&
00276           (present & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) == present) {
00277         return CMD_ERROR;
00278       }
00279 
00280       if (flags & DC_EXEC) {
00281         if (HasRoadWorks(tile)) {
00282           /* flooding tile with road works, don't forget to remove the effect vehicle too */
00283           assert(_current_company == OWNER_WATER);
00284           EffectVehicle *v;
00285           FOR_ALL_EFFECTVEHICLES(v) {
00286             if (TileVirtXY(v->x_pos, v->y_pos) == tile) {
00287               delete v;
00288             }
00289           }
00290         }
00291         if (present == ROAD_NONE) {
00292           RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
00293           if (rts == ROADTYPES_NONE) {
00294             /* Includes MarkTileDirtyByTile() */
00295             DoClearSquare(tile);
00296           } else {
00297             if (rt == ROADTYPE_ROAD && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) {
00298               /* Update nearest-town index */
00299               const Town *town = CalcClosestTownFromTile(tile);
00300               SetTownIndex(tile, town == NULL ? (TownID)INVALID_TOWN : town->index);
00301             }
00302             SetRoadBits(tile, ROAD_NONE, rt);
00303             SetRoadTypes(tile, rts);
00304             MarkTileDirtyByTile(tile);
00305           }
00306         } else {
00307           /* When bits are removed, you *always* end up with something that
00308            * is not a complete straight road tile. However, trams do not have
00309            * onewayness, so they cannot remove it either. */
00310           if (rt != ROADTYPE_TRAM) SetDisallowedRoadDirections(tile, DRD_NONE);
00311           SetRoadBits(tile, present, rt);
00312           MarkTileDirtyByTile(tile);
00313         }
00314       }
00315 
00316       CommandCost cost(EXPENSES_CONSTRUCTION, CountBits(pieces) * _price[PR_CLEAR_ROAD]);
00317       /* If we build a foundation we have to pay for it. */
00318       if (f == FOUNDATION_NONE && GetRoadFoundation(tileh, present) != FOUNDATION_NONE) cost.AddCost(_price[PR_BUILD_FOUNDATION]);
00319 
00320       return cost;
00321     }
00322 
00323     case ROAD_TILE_CROSSING: {
00324       if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) {
00325         return CMD_ERROR;
00326       }
00327 
00328       /* Don't allow road to be removed from the crossing when there is tram;
00329        * we can't draw the crossing without roadbits ;) */
00330       if (rt == ROADTYPE_ROAD && HasTileRoadType(tile, ROADTYPE_TRAM) && (flags & DC_EXEC || crossing_check)) return CMD_ERROR;
00331 
00332       if (flags & DC_EXEC) {
00333         Track railtrack = GetCrossingRailTrack(tile);
00334         RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
00335         if (rts == ROADTYPES_NONE) {
00336           TrackBits tracks = GetCrossingRailBits(tile);
00337           bool reserved = HasCrossingReservation(tile);
00338           MakeRailNormal(tile, GetTileOwner(tile), tracks, GetRailType(tile));
00339           if (reserved) SetTrackReservation(tile, tracks);
00340         } else {
00341           SetRoadTypes(tile, rts);
00342           /* If we ever get HWAY and it is possible without road then we will need to promote ownership and invalidate town index here, too */
00343         }
00344         MarkTileDirtyByTile(tile);
00345         YapfNotifyTrackLayoutChange(tile, railtrack);
00346       }
00347       return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_ROAD] * 2);
00348     }
00349 
00350     default:
00351     case ROAD_TILE_DEPOT:
00352       return CMD_ERROR;
00353   }
00354 }
00355 
00356 
00368 static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
00369 {
00370   /* Remove already build pieces */
00371   CLRBITS(*pieces, existing);
00372 
00373   /* If we can't build anything stop here */
00374   if (*pieces == ROAD_NONE) return CMD_ERROR;
00375 
00376   /* All RoadBit combos are valid on flat land */
00377   if (tileh == SLOPE_FLAT) return CommandCost();
00378 
00379   /* Proceed steep Slopes first to reduce lookup table size */
00380   if (IsSteepSlope(tileh)) {
00381     /* Force straight roads. */
00382     *pieces |= MirrorRoadBits(*pieces);
00383 
00384     /* Use existing as all existing since only straight
00385      * roads are allowed here. */
00386     existing |= other;
00387 
00388     if ((existing == ROAD_NONE || existing == *pieces) && IsStraightRoad(*pieces)) {
00389       return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00390     }
00391     return CMD_ERROR;
00392   }
00393 
00394   /* Save the merge of all bits of the current type */
00395   RoadBits type_bits = existing | *pieces;
00396 
00397   /* Roads on slopes */
00398   if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) {
00399 
00400     /* If we add leveling we've got to pay for it */
00401     if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00402 
00403     return CommandCost();
00404   }
00405 
00406   /* Autocomplete uphill roads */
00407   *pieces |= MirrorRoadBits(*pieces);
00408   type_bits = existing | *pieces;
00409 
00410   /* Uphill roads */
00411   if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) &&
00412       (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) {
00413 
00414     /* Slopes with foundation ? */
00415     if (IsSlopeWithOneCornerRaised(tileh)) {
00416 
00417       /* Prevent build on slopes if it isn't allowed */
00418       if (_settings_game.construction.build_on_slopes) {
00419 
00420         /* If we add foundation we've got to pay for it */
00421         if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00422 
00423         return CommandCost();
00424       }
00425     } else {
00426       if (HasExactlyOneBit(existing) && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00427       return CommandCost();
00428     }
00429   }
00430   return CMD_ERROR;
00431 }
00432 
00443 CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00444 {
00445   CommandCost cost(EXPENSES_CONSTRUCTION);
00446 
00447   RoadBits existing = ROAD_NONE;
00448   RoadBits other_bits = ROAD_NONE;
00449 
00450   /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
00451    * if a non-company is building the road */
00452   if ((Company::IsValidID(_current_company) && p2 != 0) || (_current_company == OWNER_TOWN && !Town::IsValidID(p2))) return CMD_ERROR;
00453   if (_current_company != OWNER_TOWN) {
00454     const Town *town = CalcClosestTownFromTile(tile);
00455     p2 = (town != NULL) ? town->index : (TownID)INVALID_TOWN;
00456   }
00457 
00458   RoadBits pieces = Extract<RoadBits, 0, 4>(p1);
00459 
00460   /* do not allow building 'zero' road bits, code wouldn't handle it */
00461   if (pieces == ROAD_NONE) return CMD_ERROR;
00462 
00463   RoadType rt = Extract<RoadType, 4, 2>(p1);
00464   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00465 
00466   DisallowedRoadDirections toggle_drd = Extract<DisallowedRoadDirections, 6, 2>(p1);
00467 
00468   Slope tileh = GetTileSlope(tile, NULL);
00469 
00470   bool tile_cleared = false; // Used to remember that the tile was cleared during test run
00471   switch (GetTileType(tile)) {
00472     case MP_ROAD:
00473       switch (GetRoadTileType(tile)) {
00474         case ROAD_TILE_NORMAL: {
00475           if (HasRoadWorks(tile)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
00476 
00477           other_bits = GetOtherRoadBits(tile, rt);
00478           if (!HasTileRoadType(tile, rt)) break;
00479 
00480           existing = GetRoadBits(tile, rt);
00481           bool crossing = !IsStraightRoad(existing | pieces);
00482           if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
00483             /* Junctions cannot be one-way */
00484             return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
00485           }
00486           if ((existing & pieces) == pieces) {
00487             /* We only want to set the (dis)allowed road directions */
00488             if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM) {
00489               if (crossing) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
00490 
00491               Owner owner = GetRoadOwner(tile, ROADTYPE_ROAD);
00492               if (owner != OWNER_NONE && !CheckOwnership(owner, tile)) return CMD_ERROR;
00493 
00494               if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00495 
00496               /* Ignore half built tiles */
00497               if ((flags & DC_EXEC) && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
00498                 SetDisallowedRoadDirections(tile, GetDisallowedRoadDirections(tile) ^ toggle_drd);
00499                 MarkTileDirtyByTile(tile);
00500               }
00501               return CommandCost();
00502             }
00503             return_cmd_error(STR_ERROR_ALREADY_BUILT);
00504           }
00505         } break;
00506 
00507         case ROAD_TILE_CROSSING:
00508           other_bits = GetCrossingRoadBits(tile);
00509           if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
00510           pieces = other_bits; // we need to pay for both roadbits
00511 
00512           if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00513           break;
00514 
00515         case ROAD_TILE_DEPOT:
00516           if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00517           goto do_clear;
00518 
00519         default: NOT_REACHED();
00520       }
00521       break;
00522 
00523     case MP_RAILWAY: {
00524       if (IsSteepSlope(tileh)) {
00525         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00526       }
00527 
00528       /* Level crossings may only be built on these slopes */
00529       if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
00530         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00531       }
00532 
00533       if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
00534 
00535       if (RailNoLevelCrossings(GetRailType(tile))) {
00536         return_cmd_error(STR_ERROR_CROSSING_DISALLOWED);
00537       }
00538 
00539       Axis roaddir;
00540       switch (GetTrackBits(tile)) {
00541         case TRACK_BIT_X:
00542           if (pieces & ROAD_X) goto do_clear;
00543           roaddir = AXIS_Y;
00544           break;
00545 
00546         case TRACK_BIT_Y:
00547           if (pieces & ROAD_Y) goto do_clear;
00548           roaddir = AXIS_X;
00549           break;
00550 
00551         default: goto do_clear;
00552       }
00553 
00554       if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00555 
00556       if (flags & DC_EXEC) {
00557         Track railtrack = AxisToTrack(OtherAxis(roaddir));
00558         YapfNotifyTrackLayoutChange(tile, railtrack);
00559         /* Always add road to the roadtypes (can't draw without it) */
00560         bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack);
00561         MakeRoadCrossing(tile, _current_company, _current_company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
00562         SetCrossingReservation(tile, reserved);
00563         UpdateLevelCrossing(tile, false);
00564         MarkTileDirtyByTile(tile);
00565       }
00566       return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_ROAD] * (rt == ROADTYPE_ROAD ? 2 : 4));
00567     }
00568 
00569     case MP_STATION: {
00570       if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00571       if (!IsDriveThroughStopTile(tile)) goto do_clear;
00572 
00573       RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
00574       if (pieces & ~curbits) goto do_clear;
00575       pieces = curbits; // we need to pay for both roadbits
00576 
00577       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00578     } break;
00579 
00580     case MP_TUNNELBRIDGE:
00581       if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) goto do_clear;
00582       if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) goto do_clear;
00583       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00584       /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
00585       if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
00586       break;
00587 
00588     default: {
00589 do_clear:;
00590       CommandCost ret = DoCommand(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
00591       if (ret.Failed()) return ret;
00592       cost.AddCost(ret);
00593       tile_cleared = true;
00594     } break;
00595   }
00596 
00597   if (other_bits != pieces) {
00598     /* Check the foundation/slopes when adding road/tram bits */
00599     CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
00600     /* Return an error if we need to build a foundation (ret != 0) but the
00601      * current setting is turned off */
00602     if (ret.Failed() || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
00603       return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00604     }
00605     cost.AddCost(ret);
00606   }
00607 
00608   if (!tile_cleared && IsTileType(tile, MP_ROAD)) {
00609     /* Don't put the pieces that already exist */
00610     pieces &= ComplementRoadBits(existing);
00611 
00612     /* Check if new road bits will have the same foundation as other existing road types */
00613     if (IsNormalRoad(tile)) {
00614       Slope slope = GetTileSlope(tile, NULL);
00615       Foundation found_new = GetRoadFoundation(slope, pieces | existing);
00616 
00617       /* Test if all other roadtypes can be built at that foundation */
00618       for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
00619         if (rtest != rt) { // check only other road types
00620           RoadBits bits = GetRoadBits(tile, rtest);
00621           /* do not check if there are not road bits of given type */
00622           if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
00623             return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00624           }
00625         }
00626       }
00627     }
00628   }
00629 
00630   if (!tile_cleared && !EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00631 
00632   cost.AddCost(CountBits(pieces) * _price[PR_BUILD_ROAD]);
00633   if (!tile_cleared && IsTileType(tile, MP_TUNNELBRIDGE)) {
00634     /* Pay for *every* tile of the bridge or tunnel */
00635     cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
00636   }
00637 
00638   if (flags & DC_EXEC) {
00639     /* CmdBuildLongRoad calls us directly with DC_EXEC, so we may only clear the tile after all
00640      * fail/success tests have been done. */
00641     if (tile_cleared) DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00642 
00643     switch (GetTileType(tile)) {
00644       case MP_ROAD: {
00645         RoadTileType rtt = GetRoadTileType(tile);
00646         if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
00647           SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00648           SetRoadOwner(tile, rt, _current_company);
00649           if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
00650         }
00651         if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
00652       } break;
00653 
00654       case MP_TUNNELBRIDGE: {
00655         TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
00656 
00657         SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
00658         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00659         SetRoadOwner(other_end, rt, _current_company);
00660         SetRoadOwner(tile, rt, _current_company);
00661 
00662         /* Mark tiles diry that have been repaved */
00663         MarkTileDirtyByTile(other_end);
00664         MarkTileDirtyByTile(tile);
00665         if (IsBridge(tile)) {
00666           TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
00667 
00668           for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
00669         }
00670       } break;
00671 
00672       case MP_STATION:
00673         assert(IsDriveThroughStopTile(tile));
00674         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00675         SetRoadOwner(tile, rt, _current_company);
00676         break;
00677 
00678       default:
00679         MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
00680         break;
00681     }
00682 
00683     if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
00684       existing |= pieces;
00685       SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
00686           GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
00687     }
00688 
00689     MarkTileDirtyByTile(tile);
00690   }
00691   return cost;
00692 }
00693 
00708 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00709 {
00710   CommandCost cost(EXPENSES_CONSTRUCTION);
00711   bool had_bridge = false;
00712   bool had_tunnel = false;
00713   bool had_success = false;
00714   DisallowedRoadDirections drd = DRD_NORTHBOUND;
00715 
00716   _error_message = INVALID_STRING_ID;
00717 
00718   if (p1 >= MapSize()) return CMD_ERROR;
00719 
00720   TileIndex end_tile = p1;
00721   RoadType rt = Extract<RoadType, 3, 2>(p2);
00722   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00723 
00724   Axis axis = Extract<Axis, 2, 1>(p2);
00725   /* Only drag in X or Y direction dictated by the direction variable */
00726   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
00727   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
00728 
00729   DiagDirection dir = AxisToDiagDir(axis);
00730 
00731   /* Swap direction, also the half-tile drag var (bit 0 and 1) */
00732   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00733     dir = ReverseDiagDir(dir);
00734     p2 ^= 3;
00735     drd = DRD_SOUTHBOUND;
00736   }
00737 
00738   /* On the X-axis, we have to swap the initial bits, so they
00739    * will be interpreted correctly in the GTTS. Futhermore
00740    * when you just 'click' on one tile to build them. */
00741   if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
00742   /* No disallowed direction bits have to be toggled */
00743   if (!HasBit(p2, 5)) drd = DRD_NONE;
00744 
00745   TileIndex tile = start_tile;
00746   /* Start tile is the first tile clicked by the user. */
00747   for (;;) {
00748     RoadBits bits = AxisToRoadBits(axis);
00749 
00750     /* Road parts only have to be built at the start tile or at the end tile. */
00751     if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
00752     if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
00753 
00754     _error_message = INVALID_STRING_ID;
00755     CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
00756     if (ret.Failed()) {
00757       if (_error_message != STR_ERROR_ALREADY_BUILT) {
00758         if (HasBit(p2, 6)) return CMD_ERROR;
00759         break;
00760       }
00761     } else {
00762       had_success = true;
00763       /* Only pay for the upgrade on one side of the bridges and tunnels */
00764       if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00765         if (IsBridge(tile)) {
00766           if ((!had_bridge || GetTunnelBridgeDirection(tile) == dir)) {
00767             cost.AddCost(ret);
00768           }
00769           had_bridge = true;
00770         } else { // IsTunnel(tile)
00771           if ((!had_tunnel || GetTunnelBridgeDirection(tile) == dir)) {
00772             cost.AddCost(ret);
00773           }
00774           had_tunnel = true;
00775         }
00776       } else {
00777         cost.AddCost(ret);
00778       }
00779     }
00780 
00781     if (tile == end_tile) break;
00782 
00783     tile += TileOffsByDiagDir(dir);
00784   }
00785 
00786   return !had_success ? CMD_ERROR : cost;
00787 }
00788 
00801 CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00802 {
00803   CommandCost cost(EXPENSES_CONSTRUCTION);
00804 
00805   if (p1 >= MapSize()) return CMD_ERROR;
00806 
00807   TileIndex end_tile = p1;
00808   RoadType rt = Extract<RoadType, 3, 2>(p2);
00809   if (!IsValidRoadType(rt)) return CMD_ERROR;
00810 
00811   Axis axis = Extract<Axis, 2, 1>(p2);
00812   /* Only drag in X or Y direction dictated by the direction variable */
00813   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
00814   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
00815 
00816   /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
00817   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00818     TileIndex t = start_tile;
00819     start_tile = end_tile;
00820     end_tile = t;
00821     p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
00822   }
00823 
00824   Money money = GetAvailableMoneyForCommand();
00825   TileIndex tile = start_tile;
00826   /* Start tile is the small number. */
00827   for (;;) {
00828     RoadBits bits = AxisToRoadBits(axis);
00829 
00830     if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
00831     if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
00832 
00833     /* try to remove the halves. */
00834     if (bits != 0) {
00835       CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
00836       if (ret.Succeeded()) {
00837         if (flags & DC_EXEC) {
00838           money -= ret.GetCost();
00839           if (money < 0) {
00840             _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
00841             return cost;
00842           }
00843           RemoveRoad(tile, flags, bits, rt, true, false);
00844         }
00845         cost.AddCost(ret);
00846       }
00847     }
00848 
00849     if (tile == end_tile) break;
00850 
00851     tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
00852   }
00853 
00854   return (cost.GetCost() == 0) ? CMD_ERROR : cost;
00855 }
00856 
00869 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00870 {
00871   DiagDirection dir = Extract<DiagDirection, 0, 2>(p1);
00872   RoadType rt = Extract<RoadType, 2, 2>(p1);
00873 
00874   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00875 
00876   Slope tileh = GetTileSlope(tile, NULL);
00877   if (tileh != SLOPE_FLAT && (
00878         !_settings_game.construction.build_on_slopes ||
00879         IsSteepSlope(tileh) ||
00880         !CanBuildDepotByTileh(dir, tileh)
00881       )) {
00882     return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
00883   }
00884 
00885   CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00886   if (cost.Failed()) return CMD_ERROR;
00887 
00888   if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00889 
00890   if (!Depot::CanAllocateItem()) return CMD_ERROR;
00891 
00892   if (flags & DC_EXEC) {
00893     Depot *dep = new Depot(tile);
00894     dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
00895 
00896     MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
00897     MarkTileDirtyByTile(tile);
00898   }
00899   cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
00900   return cost;
00901 }
00902 
00903 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
00904 {
00905   if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
00906 
00907   if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00908 
00909   if (flags & DC_EXEC) {
00910     delete Depot::GetByTile(tile);
00911     DoClearSquare(tile);
00912   }
00913 
00914   return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
00915 }
00916 
00917 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
00918 {
00919   switch (GetRoadTileType(tile)) {
00920     case ROAD_TILE_NORMAL: {
00921       RoadBits b = GetAllRoadBits(tile);
00922 
00923       /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
00924       if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
00925         RoadTypes rts = GetRoadTypes(tile);
00926         CommandCost ret(EXPENSES_CONSTRUCTION);
00927         for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
00928           if (HasBit(rts, rt)) {
00929             CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
00930             if (tmp_ret.Failed()) return tmp_ret;
00931             ret.AddCost(tmp_ret);
00932           }
00933         }
00934         return ret;
00935       }
00936       return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
00937     }
00938 
00939     case ROAD_TILE_CROSSING: {
00940       RoadTypes rts = GetRoadTypes(tile);
00941       CommandCost ret(EXPENSES_CONSTRUCTION);
00942 
00943       if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
00944 
00945       /* Must iterate over the roadtypes in a reverse manner because
00946        * tram tracks must be removed before the road bits. */
00947       RoadType rt = ROADTYPE_TRAM;
00948       do {
00949         if (HasBit(rts, rt)) {
00950           CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
00951           if (tmp_ret.Failed()) return tmp_ret;
00952           ret.AddCost(tmp_ret);
00953         }
00954       } while (rt-- != ROADTYPE_ROAD);
00955 
00956       if (flags & DC_EXEC) {
00957         DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00958       }
00959       return ret;
00960     }
00961 
00962     default:
00963     case ROAD_TILE_DEPOT:
00964       if (flags & DC_AUTO) {
00965         return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
00966       }
00967       return RemoveRoadDepot(tile, flags);
00968   }
00969 }
00970 
00971 
00972 struct DrawRoadTileStruct {
00973   uint16 image;
00974   byte subcoord_x;
00975   byte subcoord_y;
00976 };
00977 
00978 #include "table/road_land.h"
00979 
00987 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
00988 {
00989   /* Flat land and land without a road doesn't require a foundation */
00990   if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
00991 
00992   if (!IsSteepSlope(tileh)) {
00993     /* Leveled RoadBits on a slope */
00994     if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
00995 
00996     /* Straight roads without foundation on a slope */
00997     if (!IsSlopeWithOneCornerRaised(tileh) &&
00998         (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
00999       return FOUNDATION_NONE;
01000   }
01001 
01002   /* Roads on steep Slopes or on Slopes with one corner raised */
01003   return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
01004 }
01005 
01006 const byte _road_sloped_sprites[14] = {
01007   0,  0,  2,  0,
01008   0,  1,  0,  0,
01009   3,  0,  0,  0,
01010   0,  0
01011 };
01012 
01023 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
01024 {
01025   return (IsOnSnow(tile) &&
01026       !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
01027         roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
01028 }
01029 
01035 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
01036 {
01037   /* Do not draw catenary if it is invisible */
01038   if (IsInvisibilitySet(TO_CATENARY)) return;
01039 
01040   /* Don't draw the catenary under a low bridge */
01041   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
01042     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01043 
01044     if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
01045   }
01046 
01047   SpriteID front;
01048   SpriteID back;
01049 
01050   if (ti->tileh != SLOPE_FLAT) {
01051     back  = SPR_TRAMWAY_BACK_WIRES_SLOPED  + _road_sloped_sprites[ti->tileh - 1];
01052     front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
01053   } else {
01054     back  = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
01055     front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
01056   }
01057 
01058   AddSortableSpriteToDraw(back,  PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01059   AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01060 }
01061 
01070 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
01071 {
01072   int x = ti->x | dx;
01073   int y = ti->y | dy;
01074   byte z = ti->z;
01075   if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
01076   AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
01077 }
01078 
01083 static void DrawRoadBits(TileInfo *ti)
01084 {
01085   RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
01086   RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
01087 
01088   SpriteID image = 0;
01089   PaletteID pal = PAL_NONE;
01090 
01091   if (ti->tileh != SLOPE_FLAT) {
01092     DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
01093 
01094     /* DrawFoundation() modifies ti.
01095      * Default sloped sprites.. */
01096     if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
01097   }
01098 
01099   if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
01100 
01101   Roadside roadside = GetRoadside(ti->tile);
01102 
01103   if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01104     image += 19;
01105   } else {
01106     switch (roadside) {
01107       case ROADSIDE_BARREN:           pal = PALETTE_TO_BARE_LAND; break;
01108       case ROADSIDE_GRASS:            break;
01109       case ROADSIDE_GRASS_ROAD_WORKS: break;
01110       default:                        image -= 19; break; // Paved
01111     }
01112   }
01113 
01114   DrawGroundSprite(image, pal);
01115 
01116   /* For tram we overlay the road graphics with either tram tracks only
01117    * (when there is actual road beneath the trams) or with tram tracks
01118    * and some dirts which hides the road graphics */
01119   if (tram != ROAD_NONE) {
01120     if (ti->tileh != SLOPE_FLAT) {
01121       image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
01122     } else {
01123       image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
01124     }
01125     image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
01126     DrawGroundSprite(image, pal);
01127   }
01128 
01129   if (road != ROAD_NONE) {
01130     DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
01131     if (drd != DRD_NONE) {
01132       DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialZ(8, 8, ti->tileh));
01133     }
01134   }
01135 
01136   if (HasRoadWorks(ti->tile)) {
01137     /* Road works */
01138     DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
01139     return;
01140   }
01141 
01142   if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
01143 
01144   /* Return if full detail is disabled, or we are zoomed fully out. */
01145   if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
01146 
01147   /* Do not draw details (street lights, trees) under low bridge */
01148   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
01149     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01150     uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
01151 
01152     if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
01153 
01154     if (height < minz) return;
01155   }
01156 
01157   /* If there are no road bits, return, as there is nothing left to do */
01158   if (HasAtMostOneBit(road)) return;
01159 
01160   /* Draw extra details. */
01161   for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
01162     DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
01163   }
01164 }
01165 
01167 static void DrawTile_Road(TileInfo *ti)
01168 {
01169   switch (GetRoadTileType(ti->tile)) {
01170     case ROAD_TILE_NORMAL:
01171       DrawRoadBits(ti);
01172       break;
01173 
01174     case ROAD_TILE_CROSSING: {
01175       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01176 
01177       PaletteID pal = PAL_NONE;
01178       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01179 
01180       if (rti->UsesOverlay()) {
01181         Axis axis = GetCrossingRailAxis(ti->tile);
01182         SpriteID road = SPR_ROAD_Y + axis;
01183 
01184         Roadside roadside = GetRoadside(ti->tile);
01185 
01186         if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01187           road += 19;
01188         } else {
01189           switch (roadside) {
01190             case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01191             case ROADSIDE_GRASS:  break;
01192             default:              road -= 19; break; // Paved
01193           }
01194         }
01195 
01196         DrawGroundSprite(road, pal);
01197 
01198         SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
01199         DrawGroundSprite(rail, PAL_NONE);
01200         DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
01201       } else {
01202         SpriteID image = rti->base_sprites.crossing;
01203 
01204         if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
01205         if (IsCrossingBarred(ti->tile)) image += 2;
01206 
01207         Roadside roadside = GetRoadside(ti->tile);
01208 
01209         if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01210           image += 8;
01211         } else {
01212           switch (roadside) {
01213             case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01214             case ROADSIDE_GRASS:  break;
01215             default:              image += 4; break; // Paved
01216           }
01217         }
01218 
01219         DrawGroundSprite(image, pal);
01220 
01221         /* PBS debugging, draw reserved tracks darker */
01222         if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
01223           DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y, PALETTE_CRASH);
01224         }
01225       }
01226 
01227       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01228         DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
01229         DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
01230       }
01231       if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
01232       break;
01233     }
01234 
01235     default:
01236     case ROAD_TILE_DEPOT: {
01237       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01238 
01239       PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
01240 
01241       const DrawTileSprites *dts;
01242       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01243         dts =  &_tram_depot[GetRoadDepotDirection(ti->tile)];
01244       } else {
01245         dts =  &_road_depot[GetRoadDepotDirection(ti->tile)];
01246       }
01247 
01248       DrawGroundSprite(dts->ground.sprite, PAL_NONE);
01249       DrawOrigTileSeq(ti, dts, TO_BUILDINGS, palette);
01250       break;
01251     }
01252   }
01253   DrawBridgeMiddle(ti);
01254 }
01255 
01256 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
01257 {
01258   PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
01259   const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
01260 
01261   x += 33;
01262   y += 17;
01263 
01264   DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
01265   DrawOrigTileSeqInGUI(x, y, dts, palette);
01266 }
01267 
01273 void UpdateNearestTownForRoadTiles(bool invalidate)
01274 {
01275   assert(!invalidate || _generating_world);
01276 
01277   for (TileIndex t = 0; t < MapSize(); t++) {
01278     if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) {
01279       TownID tid = (TownID)INVALID_TOWN;
01280       if (!invalidate) {
01281         const Town *town = CalcClosestTownFromTile(t);
01282         if (town != NULL) tid = town->index;
01283       }
01284       SetTownIndex(t, tid);
01285     }
01286   }
01287 }
01288 
01289 static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
01290 {
01291   uint z;
01292   Slope tileh = GetTileSlope(tile, &z);
01293 
01294   if (tileh == SLOPE_FLAT) return z;
01295   if (IsNormalRoad(tile)) {
01296     Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
01297     z += ApplyFoundationToSlope(f, &tileh);
01298     return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
01299   } else {
01300     return z + TILE_HEIGHT;
01301   }
01302 }
01303 
01304 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
01305 {
01306   if (IsNormalRoad(tile)) {
01307     return GetRoadFoundation(tileh, GetAllRoadBits(tile));
01308   } else {
01309     return FlatteningFoundation(tileh);
01310   }
01311 }
01312 
01313 static const Roadside _town_road_types[][2] = {
01314   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
01315   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
01316   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
01317   { ROADSIDE_TREES,         ROADSIDE_TREES },
01318   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01319 };
01320 
01321 static const Roadside _town_road_types_2[][2] = {
01322   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
01323   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
01324   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01325   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01326   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01327 };
01328 
01329 
01330 static void TileLoop_Road(TileIndex tile)
01331 {
01332   switch (_settings_game.game_creation.landscape) {
01333     case LT_ARCTIC:
01334       if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
01335         ToggleSnow(tile);
01336         MarkTileDirtyByTile(tile);
01337       }
01338       break;
01339 
01340     case LT_TROPIC:
01341       if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
01342         ToggleDesert(tile);
01343         MarkTileDirtyByTile(tile);
01344       }
01345       break;
01346   }
01347 
01348   if (IsRoadDepot(tile)) return;
01349 
01350   const Town *t = ClosestTownFromTile(tile, UINT_MAX);
01351   if (!HasRoadWorks(tile)) {
01352     HouseZonesBits grp = HZB_TOWN_EDGE;
01353 
01354     if (t != NULL) {
01355       grp = GetTownRadiusGroup(t, tile);
01356 
01357       /* Show an animation to indicate road work */
01358       if (t->road_build_months != 0 &&
01359           (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
01360           IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
01361         if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
01362           StartRoadWorks(tile);
01363 
01364           SndPlayTileFx(SND_21_JACKHAMMER, tile);
01365           CreateEffectVehicleAbove(
01366             TileX(tile) * TILE_SIZE + 7,
01367             TileY(tile) * TILE_SIZE + 7,
01368             0,
01369             EV_BULLDOZER);
01370           MarkTileDirtyByTile(tile);
01371           return;
01372         }
01373       }
01374     }
01375 
01376     {
01377       /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
01378       const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
01379       Roadside cur_rs = GetRoadside(tile);
01380 
01381       /* We have our desired type, do nothing */
01382       if (cur_rs == new_rs[0]) return;
01383 
01384       /* We have the pre-type of the desired type, switch to the desired type */
01385       if (cur_rs == new_rs[1]) {
01386         cur_rs = new_rs[0];
01387       /* We have barren land, install the pre-type */
01388       } else if (cur_rs == ROADSIDE_BARREN) {
01389         cur_rs = new_rs[1];
01390       /* We're totally off limits, remove any installation and make barren land */
01391       } else {
01392         cur_rs = ROADSIDE_BARREN;
01393       }
01394       SetRoadside(tile, cur_rs);
01395       MarkTileDirtyByTile(tile);
01396     }
01397   } else if (IncreaseRoadWorksCounter(tile)) {
01398     TerminateRoadWorks(tile);
01399 
01400     if (_settings_game.economy.mod_road_rebuild) {
01401       /* Generate a nicer town surface */
01402       const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
01403       const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
01404 
01405       if (old_rb != new_rb) {
01406         RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
01407       }
01408     }
01409 
01410     MarkTileDirtyByTile(tile);
01411   }
01412 }
01413 
01414 static bool ClickTile_Road(TileIndex tile)
01415 {
01416   if (!IsRoadDepot(tile)) return false;
01417 
01418   ShowDepotWindow(tile, VEH_ROAD);
01419   return true;
01420 }
01421 
01422 /* Converts RoadBits to TrackBits */
01423 static const byte _road_trackbits[16] = {
01424   0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
01425 };
01426 
01427 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
01428 {
01429   TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
01430   TrackdirBits red_signals = TRACKDIR_BIT_NONE; // crossing barred
01431   switch (mode) {
01432     case TRANSPORT_RAIL:
01433       if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
01434       break;
01435 
01436     case TRANSPORT_ROAD:
01437       if ((GetRoadTypes(tile) & sub_mode) == 0) break;
01438       switch (GetRoadTileType(tile)) {
01439         case ROAD_TILE_NORMAL: {
01440           const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
01441           RoadType rt = (RoadType)FindFirstBit(sub_mode);
01442           RoadBits bits = GetRoadBits(tile, rt);
01443 
01444           /* no roadbit at this side of tile, return 0 */
01445           if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
01446 
01447           uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
01448           if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
01449           break;
01450         }
01451 
01452         case ROAD_TILE_CROSSING: {
01453           Axis axis = GetCrossingRoadAxis(tile);
01454 
01455           if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
01456 
01457           trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
01458           if (IsCrossingBarred(tile)) red_signals = trackdirbits;
01459           break;
01460         }
01461 
01462         default:
01463         case ROAD_TILE_DEPOT: {
01464           DiagDirection dir = GetRoadDepotDirection(tile);
01465 
01466           if (side != INVALID_DIAGDIR && side != dir) break;
01467 
01468           trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
01469           break;
01470         }
01471       }
01472       break;
01473 
01474     default: break;
01475   }
01476   return CombineTrackStatus(trackdirbits, red_signals);
01477 }
01478 
01479 static const StringID _road_tile_strings[] = {
01480   STR_LAI_ROAD_DESCRIPTION_ROAD,
01481   STR_LAI_ROAD_DESCRIPTION_ROAD,
01482   STR_LAI_ROAD_DESCRIPTION_ROAD,
01483   STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS,
01484   STR_LAI_ROAD_DESCRIPTION_ROAD,
01485   STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD,
01486   STR_LAI_ROAD_DESCRIPTION_ROAD,
01487   STR_LAI_ROAD_DESCRIPTION_ROAD,
01488 };
01489 
01490 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
01491 {
01492   Owner rail_owner = INVALID_OWNER;
01493   Owner road_owner = INVALID_OWNER;
01494   Owner tram_owner = INVALID_OWNER;
01495 
01496   switch (GetRoadTileType(tile)) {
01497     case ROAD_TILE_CROSSING: {
01498       td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING;
01499       RoadTypes rts = GetRoadTypes(tile);
01500       rail_owner = GetTileOwner(tile);
01501       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01502       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01503 
01504       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
01505       td->rail_speed = rti->max_speed;
01506 
01507       break;
01508     }
01509 
01510     case ROAD_TILE_DEPOT:
01511       td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
01512       road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter
01513       break;
01514 
01515     default: {
01516       RoadTypes rts = GetRoadTypes(tile);
01517       td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
01518       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01519       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01520       break;
01521     }
01522   }
01523 
01524   /* Now we have to discover, if the tile has only one owner or many:
01525    *   - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
01526    *   - Compare the found owner with the other owners, and test if they differ.
01527    * Note: If road exists it will be the first_owner.
01528    */
01529   Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
01530   bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
01531 
01532   if (mixed_owners) {
01533     /* Multiple owners */
01534     td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
01535     td->owner[0] = rail_owner;
01536     td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
01537     td->owner[1] = road_owner;
01538     td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
01539     td->owner[2] = tram_owner;
01540   } else {
01541     /* One to rule them all */
01542     td->owner[0] = first_owner;
01543   }
01544 }
01545 
01550 static const byte _roadveh_enter_depot_dir[4] = {
01551   TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
01552 };
01553 
01554 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
01555 {
01556   switch (GetRoadTileType(tile)) {
01557     case ROAD_TILE_DEPOT: {
01558       if (v->type != VEH_ROAD) break;
01559 
01560       RoadVehicle *rv = RoadVehicle::From(v);
01561       if (rv->frame == RVC_DEPOT_STOP_FRAME &&
01562           _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
01563         rv->state = RVSB_IN_DEPOT;
01564         rv->vehstatus |= VS_HIDDEN;
01565         rv->direction = ReverseDir(rv->direction);
01566         if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
01567         rv->tile = tile;
01568 
01569         InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
01570         return VETSB_ENTERED_WORMHOLE;
01571       }
01572     } break;
01573 
01574     default: break;
01575   }
01576   return VETSB_CONTINUE;
01577 }
01578 
01579 
01580 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
01581 {
01582   if (IsRoadDepot(tile)) {
01583     if (GetTileOwner(tile) == old_owner) {
01584       if (new_owner == INVALID_OWNER) {
01585         DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
01586       } else {
01587         SetTileOwner(tile, new_owner);
01588       }
01589     }
01590     return;
01591   }
01592 
01593   for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
01594     /* Update all roadtypes, no matter if they are present */
01595     if (GetRoadOwner(tile, rt) == old_owner) {
01596       SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
01597     }
01598   }
01599 
01600   if (IsLevelCrossing(tile)) {
01601     if (GetTileOwner(tile) == old_owner) {
01602       if (new_owner == INVALID_OWNER) {
01603         DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
01604       } else {
01605         SetTileOwner(tile, new_owner);
01606       }
01607     }
01608   }
01609 }
01610 
01611 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
01612 {
01613   if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
01614     switch (GetRoadTileType(tile)) {
01615       case ROAD_TILE_CROSSING:
01616         if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new)) && HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01617         break;
01618 
01619       case ROAD_TILE_DEPOT:
01620         if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01621         break;
01622 
01623       case ROAD_TILE_NORMAL: {
01624         RoadBits bits = GetAllRoadBits(tile);
01625         RoadBits bits_copy = bits;
01626         /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
01627         if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
01628           /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
01629           if (bits == bits_copy) {
01630             uint z_old;
01631             Slope tileh_old = GetTileSlope(tile, &z_old);
01632 
01633             /* Get the slope on top of the foundation */
01634             z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
01635             z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
01636 
01637             /* The surface slope must not be changed */
01638             if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01639           }
01640         }
01641         break;
01642       }
01643 
01644       default: NOT_REACHED();
01645     }
01646   }
01647 
01648   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
01649 }
01650 
01652 extern const TileTypeProcs _tile_type_road_procs = {
01653   DrawTile_Road,           // draw_tile_proc
01654   GetSlopeZ_Road,          // get_slope_z_proc
01655   ClearTile_Road,          // clear_tile_proc
01656   NULL,                    // add_accepted_cargo_proc
01657   GetTileDesc_Road,        // get_tile_desc_proc
01658   GetTileTrackStatus_Road, // get_tile_track_status_proc
01659   ClickTile_Road,          // click_tile_proc
01660   NULL,                    // animate_tile_proc
01661   TileLoop_Road,           // tile_loop_clear
01662   ChangeTileOwner_Road,    // change_tile_owner_clear
01663   NULL,                    // add_produced_cargo_proc
01664   VehicleEnter_Road,       // vehicle_enter_tile_proc
01665   GetFoundation_Road,      // get_foundation_proc
01666   TerraformTile_Road,      // terraform_tile_proc
01667 };

Generated on Sat Jul 31 21:37:51 2010 for OpenTTD by  doxygen 1.6.1