# road_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: road_cmd.cpp 19439 2010-03-16 20:54:26Z 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>(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 = (RoadType)GB(p1, 4, 2);
    00464   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00465
    00466   DisallowedRoadDirections toggle_drd = (DisallowedRoadDirections)GB(p1, 6, 2);
    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       Axis roaddir;
    00536       switch (GetTrackBits(tile)) {
    00537         case TRACK_BIT_X:
    00538           if (pieces & ROAD_X) goto do_clear;
    00539           roaddir = AXIS_Y;
    00540           break;
    00541
    00542         case TRACK_BIT_Y:
    00543           if (pieces & ROAD_Y) goto do_clear;
    00544           roaddir = AXIS_X;
    00545           break;
    00546
    00547         default: goto do_clear;
    00548       }
    00549
    00550       if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00551
    00552       if (flags & DC_EXEC) {
    00553         Track railtrack = AxisToTrack(OtherAxis(roaddir));
    00554         YapfNotifyTrackLayoutChange(tile, railtrack);
    00555         /* Always add road to the roadtypes (can't draw without it) */
    00556         bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack);
    00557         MakeRoadCrossing(tile, _current_company, _current_company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
    00558         SetCrossingReservation(tile, reserved);
    00559         UpdateLevelCrossing(tile, false);
    00560         MarkTileDirtyByTile(tile);
    00561       }
    00562       return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_ROAD] * (rt == ROADTYPE_ROAD ? 2 : 4));
    00563     }
    00564
    00565     case MP_STATION: {
    00566       if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00567       if (!IsDriveThroughStopTile(tile)) goto do_clear;
    00568
    00569       RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
    00570       if (pieces & ~curbits) goto do_clear;
    00571       pieces = curbits; // we need to pay for both roadbits
    00572
    00573       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00574     } break;
    00575
    00576     case MP_TUNNELBRIDGE:
    00577       if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) goto do_clear;
    00578       if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) goto do_clear;
    00579       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00580       /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
    00581       if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
    00582       break;
    00583
    00584     default: {
    00585 do_clear:;
    00586       CommandCost ret = DoCommand(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
    00587       if (ret.Failed()) return ret;
    00588       cost.AddCost(ret);
    00589       tile_cleared = true;
    00590     } break;
    00591   }
    00592
    00593   if (other_bits != pieces) {
    00594     /* Check the foundation/slopes when adding road/tram bits */
    00595     CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
    00596     /* Return an error if we need to build a foundation (ret != 0) but the
    00597      * current setting is turned off */
    00598     if (ret.Failed() || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
    00599       return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00600     }
    00601     cost.AddCost(ret);
    00602   }
    00603
    00604   if (!tile_cleared && IsTileType(tile, MP_ROAD)) {
    00605     /* Don't put the pieces that already exist */
    00606     pieces &= ComplementRoadBits(existing);
    00607
    00608     /* Check if new road bits will have the same foundation as other existing road types */
    00609     if (IsNormalRoad(tile)) {
    00610       Slope slope = GetTileSlope(tile, NULL);
    00611       Foundation found_new = GetRoadFoundation(slope, pieces | existing);
    00612
    00613       /* Test if all other roadtypes can be built at that foundation */
    00614       for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
    00615         if (rtest != rt) { // check only other road types
    00616           RoadBits bits = GetRoadBits(tile, rtest);
    00617           /* do not check if there are not road bits of given type */
    00618           if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
    00619             return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00620           }
    00621         }
    00622       }
    00623     }
    00624   }
    00625
    00626   if (!tile_cleared && !EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00627
    00628   cost.AddCost(CountBits(pieces) * _price[PR_BUILD_ROAD]);
    00629   if (!tile_cleared && IsTileType(tile, MP_TUNNELBRIDGE)) {
    00630     /* Pay for *every* tile of the bridge or tunnel */
    00631     cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
    00632   }
    00633
    00634   if (flags & DC_EXEC) {
    00635     /* CmdBuildLongRoad calls us directly with DC_EXEC, so we may only clear the tile after all
    00636      * fail/success tests have been done. */
    00637     if (tile_cleared) DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00638
    00639     switch (GetTileType(tile)) {
    00640       case MP_ROAD: {
    00641         RoadTileType rtt = GetRoadTileType(tile);
    00642         if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
    00643           SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00644           SetRoadOwner(tile, rt, _current_company);
    00645           if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
    00646         }
    00647         if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
    00648       } break;
    00649
    00650       case MP_TUNNELBRIDGE: {
    00651         TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
    00652
    00653         SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
    00654         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00655         SetRoadOwner(other_end, rt, _current_company);
    00656         SetRoadOwner(tile, rt, _current_company);
    00657
    00658         /* Mark tiles diry that have been repaved */
    00659         MarkTileDirtyByTile(other_end);
    00660         MarkTileDirtyByTile(tile);
    00661         if (IsBridge(tile)) {
    00662           TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
    00663
    00664           for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
    00665         }
    00666       } break;
    00667
    00668       case MP_STATION:
    00669         assert(IsDriveThroughStopTile(tile));
    00670         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00671         SetRoadOwner(tile, rt, _current_company);
    00672         break;
    00673
    00674       default:
    00675         MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
    00676         break;
    00677     }
    00678
    00679     if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
    00680       existing |= pieces;
    00681       SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
    00682           GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
    00683     }
    00684
    00685     MarkTileDirtyByTile(tile);
    00686   }
    00687   return cost;
    00688 }
    00689
    00704 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00705 {
    00706   CommandCost cost(EXPENSES_CONSTRUCTION);
    00707   bool had_bridge = false;
    00708   bool had_tunnel = false;
    00709   bool had_success = false;
    00710   DisallowedRoadDirections drd = DRD_NORTHBOUND;
    00711
    00712   _error_message = INVALID_STRING_ID;
    00713
    00714   if (p1 >= MapSize()) return CMD_ERROR;
    00715
    00716   TileIndex end_tile = p1;
    00717   RoadType rt = (RoadType)GB(p2, 3, 2);
    00718   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00719
    00720   Axis axis = Extract<Axis, 2>(p2);
    00721   /* Only drag in X or Y direction dictated by the direction variable */
    00722   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00723   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00724
    00725   DiagDirection dir = AxisToDiagDir(axis);
    00726
    00727   /* Swap direction, also the half-tile drag var (bit 0 and 1) */
    00728   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00729     dir = ReverseDiagDir(dir);
    00730     p2 ^= 3;
    00731     drd = DRD_SOUTHBOUND;
    00732   }
    00733
    00734   /* On the X-axis, we have to swap the initial bits, so they
    00735    * will be interpreted correctly in the GTTS. Futhermore
    00736    * when you just 'click' on one tile to build them. */
    00737   if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
    00738   /* No disallowed direction bits have to be toggled */
    00739   if (!HasBit(p2, 5)) drd = DRD_NONE;
    00740
    00741   TileIndex tile = start_tile;
    00742   /* Start tile is the first tile clicked by the user. */
    00743   for (;;) {
    00744     RoadBits bits = AxisToRoadBits(axis);
    00745
    00746     /* Road parts only have to be built at the start tile or at the end tile. */
    00747     if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
    00748     if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
    00749
    00750     _error_message = INVALID_STRING_ID;
    00751     CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
    00752     if (ret.Failed()) {
    00753       if (_error_message != STR_ERROR_ALREADY_BUILT) {
    00754         if (HasBit(p2, 6)) return CMD_ERROR;
    00755         break;
    00756       }
    00757     } else {
    00758       had_success = true;
    00759       /* Only pay for the upgrade on one side of the bridges and tunnels */
    00760       if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00761         if (IsBridge(tile)) {
    00762           if ((!had_bridge || GetTunnelBridgeDirection(tile) == dir)) {
    00763             cost.AddCost(ret);
    00764           }
    00765           had_bridge = true;
    00766         } else { // IsTunnel(tile)
    00767           if ((!had_tunnel || GetTunnelBridgeDirection(tile) == dir)) {
    00768             cost.AddCost(ret);
    00769           }
    00770           had_tunnel = true;
    00771         }
    00772       } else {
    00773         cost.AddCost(ret);
    00774       }
    00775     }
    00776
    00777     if (tile == end_tile) break;
    00778
    00779     tile += TileOffsByDiagDir(dir);
    00780   }
    00781
    00782   return !had_success ? CMD_ERROR : cost;
    00783 }
    00784
    00797 CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00798 {
    00799   CommandCost cost(EXPENSES_CONSTRUCTION);
    00800
    00801   if (p1 >= MapSize()) return CMD_ERROR;
    00802
    00803   TileIndex end_tile = p1;
    00804   RoadType rt = (RoadType)GB(p2, 3, 2);
    00805   if (!IsValidRoadType(rt)) return CMD_ERROR;
    00806
    00807   Axis axis = Extract<Axis, 2>(p2);
    00808   /* Only drag in X or Y direction dictated by the direction variable */
    00809   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00810   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00811
    00812   /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
    00813   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00814     TileIndex t = start_tile;
    00815     start_tile = end_tile;
    00816     end_tile = t;
    00817     p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
    00818   }
    00819
    00820   Money money = GetAvailableMoneyForCommand();
    00821   TileIndex tile = start_tile;
    00822   /* Start tile is the small number. */
    00823   for (;;) {
    00824     RoadBits bits = AxisToRoadBits(axis);
    00825
    00826     if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
    00827     if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
    00828
    00829     /* try to remove the halves. */
    00830     if (bits != 0) {
    00831       CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
    00832       if (ret.Succeeded()) {
    00833         if (flags & DC_EXEC) {
    00834           money -= ret.GetCost();
    00835           if (money < 0) {
    00836             _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
    00837             return cost;
    00838           }
    00839           RemoveRoad(tile, flags, bits, rt, true, false);
    00840         }
    00841         cost.AddCost(ret);
    00842       }
    00843     }
    00844
    00845     if (tile == end_tile) break;
    00846
    00847     tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
    00848   }
    00849
    00850   return (cost.GetCost() == 0) ? CMD_ERROR : cost;
    00851 }
    00852
    00865 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00866 {
    00867   DiagDirection dir = Extract<DiagDirection, 0>(p1);
    00868   RoadType rt = (RoadType)GB(p1, 2, 2);
    00869
    00870   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00871
    00872   Slope tileh = GetTileSlope(tile, NULL);
    00873   if (tileh != SLOPE_FLAT && (
    00874         !_settings_game.construction.build_on_slopes ||
    00875         IsSteepSlope(tileh) ||
    00876         !CanBuildDepotByTileh(dir, tileh)
    00877       )) {
    00878     return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
    00879   }
    00880
    00881   CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00882   if (cost.Failed()) return CMD_ERROR;
    00883
    00884   if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00885
    00886   if (!Depot::CanAllocateItem()) return CMD_ERROR;
    00887
    00888   if (flags & DC_EXEC) {
    00889     Depot *dep = new Depot(tile);
    00890     dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
    00891
    00892     MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
    00893     MarkTileDirtyByTile(tile);
    00894   }
    00895   cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
    00896   return cost;
    00897 }
    00898
    00899 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
    00900 {
    00901   if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
    00902
    00903   if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00904
    00905   if (flags & DC_EXEC) {
    00906     delete Depot::GetByTile(tile);
    00907     DoClearSquare(tile);
    00908   }
    00909
    00910   return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
    00911 }
    00912
    00913 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
    00914 {
    00915   switch (GetRoadTileType(tile)) {
    00916     case ROAD_TILE_NORMAL: {
    00917       RoadBits b = GetAllRoadBits(tile);
    00918
    00919       /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
    00920       if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
    00921         RoadTypes rts = GetRoadTypes(tile);
    00922         CommandCost ret(EXPENSES_CONSTRUCTION);
    00923         for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    00924           if (HasBit(rts, rt)) {
    00925             CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
    00926             if (tmp_ret.Failed()) return tmp_ret;
    00927             ret.AddCost(tmp_ret);
    00928           }
    00929         }
    00930         return ret;
    00931       }
    00932       return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
    00933     }
    00934
    00935     case ROAD_TILE_CROSSING: {
    00936       RoadTypes rts = GetRoadTypes(tile);
    00937       CommandCost ret(EXPENSES_CONSTRUCTION);
    00938
    00939       if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
    00940
    00941       /* Must iterate over the roadtypes in a reverse manner because
    00942        * tram tracks must be removed before the road bits. */
    00943       RoadType rt = ROADTYPE_TRAM;
    00944       do {
    00945         if (HasBit(rts, rt)) {
    00946           CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
    00947           if (tmp_ret.Failed()) return tmp_ret;
    00948           ret.AddCost(tmp_ret);
    00949         }
    00950       } while (rt-- != ROADTYPE_ROAD);
    00951
    00952       if (flags & DC_EXEC) {
    00953         DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00954       }
    00955       return ret;
    00956     }
    00957
    00958     default:
    00959     case ROAD_TILE_DEPOT:
    00960       if (flags & DC_AUTO) {
    00961         return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
    00962       }
    00963       return RemoveRoadDepot(tile, flags);
    00964   }
    00965 }
    00966
    00967
    00968 struct DrawRoadTileStruct {
    00969   uint16 image;
    00970   byte subcoord_x;
    00971   byte subcoord_y;
    00972 };
    00973
    00974 #include "table/road_land.h"
    00975
    00983 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
    00984 {
    00985   /* Flat land and land without a road doesn't require a foundation */
    00986   if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
    00987
    00988   if (!IsSteepSlope(tileh)) {
    00989     /* Leveled RoadBits on a slope */
    00990     if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
    00991
    00992     /* Straight roads without foundation on a slope */
    00993     if (!IsSlopeWithOneCornerRaised(tileh) &&
    00994         (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
    00995       return FOUNDATION_NONE;
    00996   }
    00997
    00998   /* Roads on steep Slopes or on Slopes with one corner raised */
    00999   return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
    01000 }
    01001
    01002 const byte _road_sloped_sprites[14] = {
    01003   0,  0,  2,  0,
    01004   0,  1,  0,  0,
    01005   3,  0,  0,  0,
    01006   0,  0
    01007 };
    01008
    01019 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
    01020 {
    01021   return (IsOnSnow(tile) &&
    01022       !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
    01023         roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
    01024 }
    01025
    01031 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
    01032 {
    01033   /* Do not draw catenary if it is invisible */
    01034   if (IsInvisibilitySet(TO_CATENARY)) return;
    01035
    01036   /* Don't draw the catenary under a low bridge */
    01037   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
    01038     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01039
    01040     if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
    01041   }
    01042
    01043   SpriteID front;
    01044   SpriteID back;
    01045
    01046   if (ti->tileh != SLOPE_FLAT) {
    01047     back  = SPR_TRAMWAY_BACK_WIRES_SLOPED  + _road_sloped_sprites[ti->tileh - 1];
    01048     front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
    01049   } else {
    01050     back  = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
    01051     front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
    01052   }
    01053
    01054   AddSortableSpriteToDraw(back,  PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01055   AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01056 }
    01057
    01066 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
    01067 {
    01068   int x = ti->x | dx;
    01069   int y = ti->y | dy;
    01070   byte z = ti->z;
    01071   if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
    01072   AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
    01073 }
    01074
    01079 static void DrawRoadBits(TileInfo *ti)
    01080 {
    01081   RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
    01082   RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
    01083
    01084   SpriteID image = 0;
    01085   PaletteID pal = PAL_NONE;
    01086
    01087   if (ti->tileh != SLOPE_FLAT) {
    01088     DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
    01089
    01090     /* DrawFoundation() modifies ti.
    01091      * Default sloped sprites.. */
    01092     if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
    01093   }
    01094
    01095   if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
    01096
    01097   Roadside roadside = GetRoadside(ti->tile);
    01098
    01099   if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01100     image += 19;
    01101   } else {
    01102     switch (roadside) {
    01103       case ROADSIDE_BARREN:           pal = PALETTE_TO_BARE_LAND; break;
    01104       case ROADSIDE_GRASS:            break;
    01105       case ROADSIDE_GRASS_ROAD_WORKS: break;
    01106       default:                        image -= 19; break; // Paved
    01107     }
    01108   }
    01109
    01110   DrawGroundSprite(image, pal);
    01111
    01112   /* For tram we overlay the road graphics with either tram tracks only
    01113    * (when there is actual road beneath the trams) or with tram tracks
    01114    * and some dirts which hides the road graphics */
    01115   if (tram != ROAD_NONE) {
    01116     if (ti->tileh != SLOPE_FLAT) {
    01117       image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
    01118     } else {
    01119       image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
    01120     }
    01121     image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
    01122     DrawGroundSprite(image, pal);
    01123   }
    01124
    01125   if (road != ROAD_NONE) {
    01126     DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
    01127     if (drd != DRD_NONE) {
    01128       DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialZ(8, 8, ti->tileh));
    01129     }
    01130   }
    01131
    01132   if (HasRoadWorks(ti->tile)) {
    01133     /* Road works */
    01134     DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
    01135     return;
    01136   }
    01137
    01138   if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
    01139
    01140   /* Return if full detail is disabled, or we are zoomed fully out. */
    01141   if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
    01142
    01143   /* Do not draw details (street lights, trees) under low bridge */
    01144   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
    01145     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01146     uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
    01147
    01148     if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
    01149
    01150     if (height < minz) return;
    01151   }
    01152
    01153   /* If there are no road bits, return, as there is nothing left to do */
    01154   if (HasAtMostOneBit(road)) return;
    01155
    01156   /* Draw extra details. */
    01157   for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
    01158     DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
    01159   }
    01160 }
    01161
    01163 static void DrawTile_Road(TileInfo *ti)
    01164 {
    01165   switch (GetRoadTileType(ti->tile)) {
    01166     case ROAD_TILE_NORMAL:
    01167       DrawRoadBits(ti);
    01168       break;
    01169
    01170     case ROAD_TILE_CROSSING: {
    01171       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    01172
    01173       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
    01174
    01175       if (rti->UsesOverlay()) {
    01176         Axis axis = GetCrossingRailAxis(ti->tile);
    01177         SpriteID road = SPR_ROAD_Y + axis;
    01178         PaletteID pal = PAL_NONE;
    01179
    01180         Roadside roadside = GetRoadside(ti->tile);
    01181
    01182         if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01183           road += 19;
    01184         } else {
    01185           switch (roadside) {
    01186             case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
    01187             case ROADSIDE_GRASS:  break;
    01188             default:              road -= 19; break; // Paved
    01189           }
    01190         }
    01191
    01192         DrawGroundSprite(road, pal);
    01193
    01194         SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
    01195         DrawGroundSprite(rail, PAL_NONE);
    01196         DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
    01197
    01198         if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
    01199         break;
    01200       }
    01201
    01202       SpriteID image = rti->base_sprites.crossing;
    01203       PaletteID pal = PAL_NONE;
    01204
    01205       if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
    01206       if (IsCrossingBarred(ti->tile)) image += 2;
    01207
    01208       Roadside roadside = GetRoadside(ti->tile);
    01209
    01210       if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01211         image += 8;
    01212       } else {
    01213         switch (roadside) {
    01214           case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
    01215           case ROADSIDE_GRASS:  break;
    01216           default:              image += 4; break; // Paved
    01217         }
    01218       }
    01219
    01220       DrawGroundSprite(image, pal);
    01221
    01222       /* PBS debugging, draw reserved tracks darker */
    01223       if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
    01224         DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y, PALETTE_CRASH);
    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       break;
    01504     }
    01505
    01506     case ROAD_TILE_DEPOT:
    01507       td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
    01508       road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter
    01509       break;
    01510
    01511     default: {
    01512       RoadTypes rts = GetRoadTypes(tile);
    01513       td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
    01514       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01515       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01516       break;
    01517     }
    01518   }
    01519
    01520   /* Now we have to discover, if the tile has only one owner or many:
    01521    *   - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
    01522    *   - Compare the found owner with the other owners, and test if they differ.
    01523    * Note: If road exists it will be the first_owner.
    01524    */
    01525   Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
    01526   bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
    01527
    01528   if (mixed_owners) {
    01529     /* Multiple owners */
    01530     td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
    01531     td->owner[0] = rail_owner;
    01532     td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
    01533     td->owner[1] = road_owner;
    01534     td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
    01535     td->owner[2] = tram_owner;
    01536   } else {
    01537     /* One to rule them all */
    01538     td->owner[0] = first_owner;
    01539   }
    01540 }
    01541
    01546 static const byte _roadveh_enter_depot_dir[4] = {
    01547   TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
    01548 };
    01549
    01550 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
    01551 {
    01552   switch (GetRoadTileType(tile)) {
    01553     case ROAD_TILE_DEPOT: {
    01554       if (v->type != VEH_ROAD) break;
    01555
    01556       RoadVehicle *rv = RoadVehicle::From(v);
    01557       if (rv->frame == RVC_DEPOT_STOP_FRAME &&
    01558           _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
    01559         rv->state = RVSB_IN_DEPOT;
    01560         rv->vehstatus |= VS_HIDDEN;
    01561         rv->direction = ReverseDir(rv->direction);
    01562         if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
    01563         rv->tile = tile;
    01564
    01565         InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
    01566         return VETSB_ENTERED_WORMHOLE;
    01567       }
    01568     } break;
    01569
    01570     default: break;
    01571   }
    01572   return VETSB_CONTINUE;
    01573 }
    01574
    01575
    01576 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
    01577 {
    01578   if (IsRoadDepot(tile)) {
    01579     if (GetTileOwner(tile) == old_owner) {
    01580       if (new_owner == INVALID_OWNER) {
    01581         DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
    01582       } else {
    01583         SetTileOwner(tile, new_owner);
    01584       }
    01585     }
    01586     return;
    01587   }
    01588
    01589   for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    01590     /* Update all roadtypes, no matter if they are present */
    01591     if (GetRoadOwner(tile, rt) == old_owner) {
    01592       SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
    01593     }
    01594   }
    01595
    01596   if (IsLevelCrossing(tile)) {
    01597     if (GetTileOwner(tile) == old_owner) {
    01598       if (new_owner == INVALID_OWNER) {
    01599         DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
    01600       } else {
    01601         SetTileOwner(tile, new_owner);
    01602       }
    01603     }
    01604   }
    01605 }
    01606
    01607 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    01608 {
    01609   if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
    01610     switch (GetRoadTileType(tile)) {
    01611       case ROAD_TILE_CROSSING:
    01612         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]);
    01613         break;
    01614
    01615       case ROAD_TILE_DEPOT:
    01616         if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01617         break;
    01618
    01619       case ROAD_TILE_NORMAL: {
    01620         RoadBits bits = GetAllRoadBits(tile);
    01621         RoadBits bits_copy = bits;
    01622         /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
    01623         if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
    01624           /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
    01625           if (bits == bits_copy) {
    01626             uint z_old;
    01627             Slope tileh_old = GetTileSlope(tile, &z_old);
    01628
    01629             /* Get the slope on top of the foundation */
    01630             z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
    01631             z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
    01632
    01633             /* The surface slope must not be changed */
    01634             if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01635           }
    01636         }
    01637         break;
    01638       }
    01639
    01640       default: NOT_REACHED();
    01641     }
    01642   }
    01643
    01644   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    01645 }
    01646
    01648 extern const TileTypeProcs _tile_type_road_procs = {
    01649   DrawTile_Road,           // draw_tile_proc
    01650   GetSlopeZ_Road,          // get_slope_z_proc
    01651   ClearTile_Road,          // clear_tile_proc
    01652   NULL,                    // add_accepted_cargo_proc
    01653   GetTileDesc_Road,        // get_tile_desc_proc
    01654   GetTileTrackStatus_Road, // get_tile_track_status_proc
    01655   ClickTile_Road,          // click_tile_proc
    01656   NULL,                    // animate_tile_proc
    01657   TileLoop_Road,           // tile_loop_clear
    01658   ChangeTileOwner_Road,    // change_tile_owner_clear
    01659   NULL,                    // add_produced_cargo_proc
    01660   VehicleEnter_Road,       // vehicle_enter_tile_proc
    01661   GetFoundation_Road,      // get_foundation_proc
    01662   TerraformTile_Road,      // terraform_tile_proc
    01663 };