# road_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: road_cmd.cpp 19128 2010-02-14 15:30:08Z alberth $ */
    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, 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     switch (GetTileType(tile)) {
    00636       case MP_ROAD: {
    00637         RoadTileType rtt = GetRoadTileType(tile);
    00638         if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
    00639           SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00640           SetRoadOwner(tile, rt, _current_company);
    00641           if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
    00642         }
    00643         if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
    00644       } break;
    00645
    00646       case MP_TUNNELBRIDGE: {
    00647         TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
    00648
    00649         SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
    00650         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00651         SetRoadOwner(other_end, rt, _current_company);
    00652         SetRoadOwner(tile, rt, _current_company);
    00653
    00654         /* Mark tiles diry that have been repaved */
    00655         MarkTileDirtyByTile(other_end);
    00656         MarkTileDirtyByTile(tile);
    00657         if (IsBridge(tile)) {
    00658           TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
    00659
    00660           for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
    00661         }
    00662       } break;
    00663
    00664       case MP_STATION:
    00665         assert(IsDriveThroughStopTile(tile));
    00666         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00667         SetRoadOwner(tile, rt, _current_company);
    00668         break;
    00669
    00670       default:
    00671         MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
    00672         break;
    00673     }
    00674
    00675     if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
    00676       existing |= pieces;
    00677       SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
    00678           GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
    00679     }
    00680
    00681     MarkTileDirtyByTile(tile);
    00682   }
    00683   return cost;
    00684 }
    00685
    00700 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00701 {
    00702   CommandCost cost(EXPENSES_CONSTRUCTION);
    00703   bool had_bridge = false;
    00704   bool had_tunnel = false;
    00705   bool had_success = false;
    00706   DisallowedRoadDirections drd = DRD_NORTHBOUND;
    00707
    00708   _error_message = INVALID_STRING_ID;
    00709
    00710   if (p1 >= MapSize()) return CMD_ERROR;
    00711
    00712   TileIndex end_tile = p1;
    00713   RoadType rt = (RoadType)GB(p2, 3, 2);
    00714   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00715
    00716   Axis axis = Extract<Axis, 2>(p2);
    00717   /* Only drag in X or Y direction dictated by the direction variable */
    00718   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00719   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00720
    00721   DiagDirection dir = AxisToDiagDir(axis);
    00722
    00723   /* Swap direction, also the half-tile drag var (bit 0 and 1) */
    00724   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00725     dir = ReverseDiagDir(dir);
    00726     p2 ^= 3;
    00727     drd = DRD_SOUTHBOUND;
    00728   }
    00729
    00730   /* On the X-axis, we have to swap the initial bits, so they
    00731    * will be interpreted correctly in the GTTS. Futhermore
    00732    * when you just 'click' on one tile to build them. */
    00733   if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
    00734   /* No disallowed direction bits have to be toggled */
    00735   if (!HasBit(p2, 5)) drd = DRD_NONE;
    00736
    00737   TileIndex tile = start_tile;
    00738   /* Start tile is the first tile clicked by the user. */
    00739   for (;;) {
    00740     RoadBits bits = AxisToRoadBits(axis);
    00741
    00742     /* Road parts only have to be built at the start tile or at the end tile. */
    00743     if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
    00744     if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
    00745
    00746     _error_message = INVALID_STRING_ID;
    00747     CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
    00748     if (ret.Failed()) {
    00749       if (_error_message != STR_ERROR_ALREADY_BUILT) {
    00750         if (HasBit(p2, 6)) return CMD_ERROR;
    00751         break;
    00752       }
    00753     } else {
    00754       had_success = true;
    00755       /* Only pay for the upgrade on one side of the bridges and tunnels */
    00756       if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00757         if (IsBridge(tile)) {
    00758           if ((!had_bridge || GetTunnelBridgeDirection(tile) == dir)) {
    00759             cost.AddCost(ret);
    00760           }
    00761           had_bridge = true;
    00762         } else { // IsTunnel(tile)
    00763           if ((!had_tunnel || GetTunnelBridgeDirection(tile) == dir)) {
    00764             cost.AddCost(ret);
    00765           }
    00766           had_tunnel = true;
    00767         }
    00768       } else {
    00769         cost.AddCost(ret);
    00770       }
    00771     }
    00772
    00773     if (tile == end_tile) break;
    00774
    00775     tile += TileOffsByDiagDir(dir);
    00776   }
    00777
    00778   return !had_success ? CMD_ERROR : cost;
    00779 }
    00780
    00793 CommandCost CmdRemoveLongRoad(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00794 {
    00795   CommandCost cost(EXPENSES_CONSTRUCTION);
    00796
    00797   if (p1 >= MapSize()) return CMD_ERROR;
    00798
    00799   TileIndex start_tile = p1;
    00800   RoadType rt = (RoadType)GB(p2, 3, 2);
    00801   if (!IsValidRoadType(rt)) return CMD_ERROR;
    00802
    00803   Axis axis = Extract<Axis, 2>(p2);
    00804   /* Only drag in X or Y direction dictated by the direction variable */
    00805   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00806   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00807
    00808   /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
    00809   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00810     TileIndex t = start_tile;
    00811     start_tile = end_tile;
    00812     end_tile = t;
    00813     p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
    00814   }
    00815
    00816   Money money = GetAvailableMoneyForCommand();
    00817   TileIndex tile = start_tile;
    00818   /* Start tile is the small number. */
    00819   for (;;) {
    00820     RoadBits bits = AxisToRoadBits(axis);
    00821
    00822     if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
    00823     if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
    00824
    00825     /* try to remove the halves. */
    00826     if (bits != 0) {
    00827       CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
    00828       if (ret.Succeeded()) {
    00829         if (flags & DC_EXEC) {
    00830           money -= ret.GetCost();
    00831           if (money < 0) {
    00832             _additional_cash_required = DoCommand(end_tile, start_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
    00833             return cost;
    00834           }
    00835           RemoveRoad(tile, flags, bits, rt, true, false);
    00836         }
    00837         cost.AddCost(ret);
    00838       }
    00839     }
    00840
    00841     if (tile == end_tile) break;
    00842
    00843     tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
    00844   }
    00845
    00846   return (cost.GetCost() == 0) ? CMD_ERROR : cost;
    00847 }
    00848
    00861 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00862 {
    00863   DiagDirection dir = Extract<DiagDirection, 0>(p1);
    00864   RoadType rt = (RoadType)GB(p1, 2, 2);
    00865
    00866   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00867
    00868   Slope tileh = GetTileSlope(tile, NULL);
    00869   if (tileh != SLOPE_FLAT && (
    00870         !_settings_game.construction.build_on_slopes ||
    00871         IsSteepSlope(tileh) ||
    00872         !CanBuildDepotByTileh(dir, tileh)
    00873       )) {
    00874     return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
    00875   }
    00876
    00877   CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00878   if (cost.Failed()) return CMD_ERROR;
    00879
    00880   if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00881
    00882   if (!Depot::CanAllocateItem()) return CMD_ERROR;
    00883
    00884   if (flags & DC_EXEC) {
    00885     Depot *dep = new Depot(tile);
    00886     dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
    00887
    00888     MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
    00889     MarkTileDirtyByTile(tile);
    00890   }
    00891   cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
    00892   return cost;
    00893 }
    00894
    00895 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
    00896 {
    00897   if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
    00898
    00899   if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00900
    00901   if (flags & DC_EXEC) {
    00902     delete Depot::GetByTile(tile);
    00903     DoClearSquare(tile);
    00904   }
    00905
    00906   return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
    00907 }
    00908
    00909 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
    00910 {
    00911   switch (GetRoadTileType(tile)) {
    00912     case ROAD_TILE_NORMAL: {
    00913       RoadBits b = GetAllRoadBits(tile);
    00914
    00915       /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
    00916       if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
    00917         RoadTypes rts = GetRoadTypes(tile);
    00918         CommandCost ret(EXPENSES_CONSTRUCTION);
    00919         for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    00920           if (HasBit(rts, rt)) {
    00921             CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
    00922             if (tmp_ret.Failed()) return tmp_ret;
    00923             ret.AddCost(tmp_ret);
    00924           }
    00925         }
    00926         return ret;
    00927       }
    00928       return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
    00929     }
    00930
    00931     case ROAD_TILE_CROSSING: {
    00932       RoadTypes rts = GetRoadTypes(tile);
    00933       CommandCost ret(EXPENSES_CONSTRUCTION);
    00934
    00935       if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
    00936
    00937       /* Must iterate over the roadtypes in a reverse manner because
    00938        * tram tracks must be removed before the road bits. */
    00939       RoadType rt = ROADTYPE_TRAM;
    00940       do {
    00941         if (HasBit(rts, rt)) {
    00942           CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
    00943           if (tmp_ret.Failed()) return tmp_ret;
    00944           ret.AddCost(tmp_ret);
    00945         }
    00946       } while (rt-- != ROADTYPE_ROAD);
    00947
    00948       if (flags & DC_EXEC) {
    00949         DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00950       }
    00951       return ret;
    00952     }
    00953
    00954     default:
    00955     case ROAD_TILE_DEPOT:
    00956       if (flags & DC_AUTO) {
    00957         return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
    00958       }
    00959       return RemoveRoadDepot(tile, flags);
    00960   }
    00961 }
    00962
    00963
    00964 struct DrawRoadTileStruct {
    00965   uint16 image;
    00966   byte subcoord_x;
    00967   byte subcoord_y;
    00968 };
    00969
    00970 #include "table/road_land.h"
    00971
    00979 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
    00980 {
    00981   /* Flat land and land without a road doesn't require a foundation */
    00982   if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
    00983
    00984   if (!IsSteepSlope(tileh)) {
    00985     /* Leveled RoadBits on a slope */
    00986     if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
    00987
    00988     /* Straight roads without foundation on a slope */
    00989     if (!IsSlopeWithOneCornerRaised(tileh) &&
    00990         (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
    00991       return FOUNDATION_NONE;
    00992   }
    00993
    00994   /* Roads on steep Slopes or on Slopes with one corner raised */
    00995   return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
    00996 }
    00997
    00998 const byte _road_sloped_sprites[14] = {
    00999   0,  0,  2,  0,
    01000   0,  1,  0,  0,
    01001   3,  0,  0,  0,
    01002   0,  0
    01003 };
    01004
    01015 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
    01016 {
    01017   return (IsOnSnow(tile) &&
    01018       !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
    01019         roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
    01020 }
    01021
    01027 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
    01028 {
    01029   /* Do not draw catenary if it is invisible */
    01030   if (IsInvisibilitySet(TO_CATENARY)) return;
    01031
    01032   /* Don't draw the catenary under a low bridge */
    01033   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
    01034     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01035
    01036     if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
    01037   }
    01038
    01039   SpriteID front;
    01040   SpriteID back;
    01041
    01042   if (ti->tileh != SLOPE_FLAT) {
    01043     back  = SPR_TRAMWAY_BACK_WIRES_SLOPED  + _road_sloped_sprites[ti->tileh - 1];
    01044     front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
    01045   } else {
    01046     back  = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
    01047     front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
    01048   }
    01049
    01050   AddSortableSpriteToDraw(back,  PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01051   AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01052 }
    01053
    01062 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
    01063 {
    01064   int x = ti->x | dx;
    01065   int y = ti->y | dy;
    01066   byte z = ti->z;
    01067   if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
    01068   AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
    01069 }
    01070
    01075 static void DrawRoadBits(TileInfo *ti)
    01076 {
    01077   RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
    01078   RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
    01079
    01080   SpriteID image = 0;
    01081   PaletteID pal = PAL_NONE;
    01082
    01083   if (ti->tileh != SLOPE_FLAT) {
    01084     DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
    01085
    01086     /* DrawFoundation() modifies ti.
    01087      * Default sloped sprites.. */
    01088     if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
    01089   }
    01090
    01091   if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
    01092
    01093   Roadside roadside = GetRoadside(ti->tile);
    01094
    01095   if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01096     image += 19;
    01097   } else {
    01098     switch (roadside) {
    01099       case ROADSIDE_BARREN:           pal = PALETTE_TO_BARE_LAND; break;
    01100       case ROADSIDE_GRASS:            break;
    01101       case ROADSIDE_GRASS_ROAD_WORKS: break;
    01102       default:                        image -= 19; break; // Paved
    01103     }
    01104   }
    01105
    01106   DrawGroundSprite(image, pal);
    01107
    01108   /* For tram we overlay the road graphics with either tram tracks only
    01109    * (when there is actual road beneath the trams) or with tram tracks
    01110    * and some dirts which hides the road graphics */
    01111   if (tram != ROAD_NONE) {
    01112     if (ti->tileh != SLOPE_FLAT) {
    01113       image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
    01114     } else {
    01115       image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
    01116     }
    01117     image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
    01118     DrawGroundSprite(image, pal);
    01119   }
    01120
    01121   if (road != ROAD_NONE) {
    01122     DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
    01123     if (drd != DRD_NONE) {
    01124       DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialZ(8, 8, ti->tileh));
    01125     }
    01126   }
    01127
    01128   if (HasRoadWorks(ti->tile)) {
    01129     /* Road works */
    01130     DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
    01131     return;
    01132   }
    01133
    01134   if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
    01135
    01136   /* Return if full detail is disabled, or we are zoomed fully out. */
    01137   if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
    01138
    01139   /* Do not draw details (street lights, trees) under low bridge */
    01140   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
    01141     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01142     uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
    01143
    01144     if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
    01145
    01146     if (height < minz) return;
    01147   }
    01148
    01149   /* If there are no road bits, return, as there is nothing left to do */
    01150   if (HasAtMostOneBit(road)) return;
    01151
    01152   /* Draw extra details. */
    01153   for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
    01154     DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
    01155   }
    01156 }
    01157
    01159 static void DrawTile_Road(TileInfo *ti)
    01160 {
    01161   switch (GetRoadTileType(ti->tile)) {
    01162     case ROAD_TILE_NORMAL:
    01163       DrawRoadBits(ti);
    01164       break;
    01165
    01166     case ROAD_TILE_CROSSING: {
    01167       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    01168
    01169       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
    01170
    01171       if (rti->UsesOverlay()) {
    01172         Axis axis = GetCrossingRailAxis(ti->tile);
    01173         SpriteID road = SPR_ROAD_Y + axis;
    01174         PaletteID pal = PAL_NONE;
    01175
    01176         Roadside roadside = GetRoadside(ti->tile);
    01177
    01178         if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01179           road += 19;
    01180         } else {
    01181           switch (roadside) {
    01182             case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
    01183             case ROADSIDE_GRASS:  break;
    01184             default:              road -= 19; break; // Paved
    01185           }
    01186         }
    01187
    01188         DrawGroundSprite(road, pal);
    01189
    01190         SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
    01191         DrawGroundSprite(rail, PAL_NONE);
    01192         DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
    01193
    01194         if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
    01195         break;
    01196       }
    01197
    01198       SpriteID image = rti->base_sprites.crossing;
    01199       PaletteID pal = PAL_NONE;
    01200
    01201       if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
    01202       if (IsCrossingBarred(ti->tile)) image += 2;
    01203
    01204       Roadside roadside = GetRoadside(ti->tile);
    01205
    01206       if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01207         image += 8;
    01208       } else {
    01209         switch (roadside) {
    01210           case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
    01211           case ROADSIDE_GRASS:  break;
    01212           default:              image += 4; break; // Paved
    01213         }
    01214       }
    01215
    01216       DrawGroundSprite(image, pal);
    01217
    01218       /* PBS debugging, draw reserved tracks darker */
    01219       if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
    01220         DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y, PALETTE_CRASH);
    01221       }
    01222
    01223       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
    01224         DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
    01225         DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
    01226       }
    01227       if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
    01228       break;
    01229     }
    01230
    01231     default:
    01232     case ROAD_TILE_DEPOT: {
    01233       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    01234
    01235       PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
    01236
    01237       const DrawTileSprites *dts;
    01238       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
    01239         dts =  &_tram_depot[GetRoadDepotDirection(ti->tile)];
    01240       } else {
    01241         dts =  &_road_depot[GetRoadDepotDirection(ti->tile)];
    01242       }
    01243
    01244       DrawGroundSprite(dts->ground.sprite, PAL_NONE);
    01245       DrawOrigTileSeq(ti, dts, TO_BUILDINGS, palette);
    01246       break;
    01247     }
    01248   }
    01249   DrawBridgeMiddle(ti);
    01250 }
    01251
    01252 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
    01253 {
    01254   PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
    01255   const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
    01256
    01257   x += 33;
    01258   y += 17;
    01259
    01260   DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
    01261   DrawOrigTileSeqInGUI(x, y, dts, palette);
    01262 }
    01263
    01269 void UpdateNearestTownForRoadTiles(bool invalidate)
    01270 {
    01271   assert(!invalidate || _generating_world);
    01272
    01273   for (TileIndex t = 0; t < MapSize(); t++) {
    01274     if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) {
    01275       TownID tid = (TownID)INVALID_TOWN;
    01276       if (!invalidate) {
    01277         const Town *town = CalcClosestTownFromTile(t);
    01278         if (town != NULL) tid = town->index;
    01279       }
    01280       SetTownIndex(t, tid);
    01281     }
    01282   }
    01283 }
    01284
    01285 static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
    01286 {
    01287   uint z;
    01288   Slope tileh = GetTileSlope(tile, &z);
    01289
    01290   if (tileh == SLOPE_FLAT) return z;
    01291   if (IsNormalRoad(tile)) {
    01292     Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
    01293     z += ApplyFoundationToSlope(f, &tileh);
    01294     return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
    01295   } else {
    01296     return z + TILE_HEIGHT;
    01297   }
    01298 }
    01299
    01300 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
    01301 {
    01302   if (IsNormalRoad(tile)) {
    01303     return GetRoadFoundation(tileh, GetAllRoadBits(tile));
    01304   } else {
    01305     return FlatteningFoundation(tileh);
    01306   }
    01307 }
    01308
    01309 static const Roadside _town_road_types[][2] = {
    01310   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
    01311   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01312   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01313   { ROADSIDE_TREES,         ROADSIDE_TREES },
    01314   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
    01315 };
    01316
    01317 static const Roadside _town_road_types_2[][2] = {
    01318   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
    01319   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01320   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
    01321   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
    01322   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
    01323 };
    01324
    01325
    01326 static void TileLoop_Road(TileIndex tile)
    01327 {
    01328   switch (_settings_game.game_creation.landscape) {
    01329     case LT_ARCTIC:
    01330       if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
    01331         ToggleSnow(tile);
    01332         MarkTileDirtyByTile(tile);
    01333       }
    01334       break;
    01335
    01336     case LT_TROPIC:
    01337       if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
    01338         ToggleDesert(tile);
    01339         MarkTileDirtyByTile(tile);
    01340       }
    01341       break;
    01342   }
    01343
    01344   if (IsRoadDepot(tile)) return;
    01345
    01346   const Town *t = ClosestTownFromTile(tile, UINT_MAX);
    01347   if (!HasRoadWorks(tile)) {
    01348     HouseZonesBits grp = HZB_TOWN_EDGE;
    01349
    01350     if (t != NULL) {
    01351       grp = GetTownRadiusGroup(t, tile);
    01352
    01353       /* Show an animation to indicate road work */
    01354       if (t->road_build_months != 0 &&
    01355           (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
    01356           IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
    01357         if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
    01358           StartRoadWorks(tile);
    01359
    01360           SndPlayTileFx(SND_21_JACKHAMMER, tile);
    01361           CreateEffectVehicleAbove(
    01362             TileX(tile) * TILE_SIZE + 7,
    01363             TileY(tile) * TILE_SIZE + 7,
    01364             0,
    01365             EV_BULLDOZER);
    01366           MarkTileDirtyByTile(tile);
    01367           return;
    01368         }
    01369       }
    01370     }
    01371
    01372     {
    01373       /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
    01374       const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
    01375       Roadside cur_rs = GetRoadside(tile);
    01376
    01377       /* We have our desired type, do nothing */
    01378       if (cur_rs == new_rs[0]) return;
    01379
    01380       /* We have the pre-type of the desired type, switch to the desired type */
    01381       if (cur_rs == new_rs[1]) {
    01382         cur_rs = new_rs[0];
    01383       /* We have barren land, install the pre-type */
    01384       } else if (cur_rs == ROADSIDE_BARREN) {
    01385         cur_rs = new_rs[1];
    01386       /* We're totally off limits, remove any installation and make barren land */
    01387       } else {
    01388         cur_rs = ROADSIDE_BARREN;
    01389       }
    01390       SetRoadside(tile, cur_rs);
    01391       MarkTileDirtyByTile(tile);
    01392     }
    01393   } else if (IncreaseRoadWorksCounter(tile)) {
    01394     TerminateRoadWorks(tile);
    01395
    01396     if (_settings_game.economy.mod_road_rebuild) {
    01397       /* Generate a nicer town surface */
    01398       const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
    01399       const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
    01400
    01401       if (old_rb != new_rb) {
    01402         RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
    01403       }
    01404     }
    01405
    01406     MarkTileDirtyByTile(tile);
    01407   }
    01408 }
    01409
    01410 static bool ClickTile_Road(TileIndex tile)
    01411 {
    01412   if (!IsRoadDepot(tile)) return false;
    01413
    01414   ShowDepotWindow(tile, VEH_ROAD);
    01415   return true;
    01416 }
    01417
    01418 /* Converts RoadBits to TrackBits */
    01419 static const byte _road_trackbits[16] = {
    01420   0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
    01421 };
    01422
    01423 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    01424 {
    01425   TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
    01426   TrackdirBits red_signals = TRACKDIR_BIT_NONE; // crossing barred
    01427   switch (mode) {
    01428     case TRANSPORT_RAIL:
    01429       if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
    01430       break;
    01431
    01432     case TRANSPORT_ROAD:
    01433       if ((GetRoadTypes(tile) & sub_mode) == 0) break;
    01434       switch (GetRoadTileType(tile)) {
    01435         case ROAD_TILE_NORMAL: {
    01436           const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
    01437           RoadType rt = (RoadType)FindFirstBit(sub_mode);
    01438           RoadBits bits = GetRoadBits(tile, rt);
    01439
    01440           /* no roadbit at this side of tile, return 0 */
    01441           if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
    01442
    01443           uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
    01444           if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
    01445           break;
    01446         }
    01447
    01448         case ROAD_TILE_CROSSING: {
    01449           Axis axis = GetCrossingRoadAxis(tile);
    01450
    01451           if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
    01452
    01453           trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
    01454           if (IsCrossingBarred(tile)) red_signals = trackdirbits;
    01455           break;
    01456         }
    01457
    01458         default:
    01459         case ROAD_TILE_DEPOT: {
    01460           DiagDirection dir = GetRoadDepotDirection(tile);
    01461
    01462           if (side != INVALID_DIAGDIR && side != dir) break;
    01463
    01464           trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
    01465           break;
    01466         }
    01467       }
    01468       break;
    01469
    01470     default: break;
    01471   }
    01472   return CombineTrackStatus(trackdirbits, red_signals);
    01473 }
    01474
    01475 static const StringID _road_tile_strings[] = {
    01476   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01477   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01478   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01479   STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS,
    01480   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01481   STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD,
    01482   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01483   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01484 };
    01485
    01486 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
    01487 {
    01488   Owner rail_owner = INVALID_OWNER;
    01489   Owner road_owner = INVALID_OWNER;
    01490   Owner tram_owner = INVALID_OWNER;
    01491
    01492   switch (GetRoadTileType(tile)) {
    01493     case ROAD_TILE_CROSSING: {
    01494       td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING;
    01495       RoadTypes rts = GetRoadTypes(tile);
    01496       rail_owner = GetTileOwner(tile);
    01497       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01498       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01499       break;
    01500     }
    01501
    01502     case ROAD_TILE_DEPOT:
    01503       td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
    01504       road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter
    01505       break;
    01506
    01507     default: {
    01508       RoadTypes rts = GetRoadTypes(tile);
    01509       td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
    01510       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01511       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01512       break;
    01513     }
    01514   }
    01515
    01516   /* Now we have to discover, if the tile has only one owner or many:
    01517    *   - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
    01518    *   - Compare the found owner with the other owners, and test if they differ.
    01519    * Note: If road exists it will be the first_owner.
    01520    */
    01521   Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
    01522   bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
    01523
    01524   if (mixed_owners) {
    01525     /* Multiple owners */
    01526     td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
    01527     td->owner[0] = rail_owner;
    01528     td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
    01529     td->owner[1] = road_owner;
    01530     td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
    01531     td->owner[2] = tram_owner;
    01532   } else {
    01533     /* One to rule them all */
    01534     td->owner[0] = first_owner;
    01535   }
    01536 }
    01537
    01542 static const byte _roadveh_enter_depot_dir[4] = {
    01543   TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
    01544 };
    01545
    01546 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
    01547 {
    01548   switch (GetRoadTileType(tile)) {
    01549     case ROAD_TILE_DEPOT: {
    01550       if (v->type != VEH_ROAD) break;
    01551
    01552       RoadVehicle *rv = RoadVehicle::From(v);
    01553       if (rv->frame == RVC_DEPOT_STOP_FRAME &&
    01554           _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
    01555         rv->state = RVSB_IN_DEPOT;
    01556         rv->vehstatus |= VS_HIDDEN;
    01557         rv->direction = ReverseDir(rv->direction);
    01558         if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
    01559         rv->tile = tile;
    01560
    01561         InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
    01562         return VETSB_ENTERED_WORMHOLE;
    01563       }
    01564     } break;
    01565
    01566     default: break;
    01567   }
    01568   return VETSB_CONTINUE;
    01569 }
    01570
    01571
    01572 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
    01573 {
    01574   if (IsRoadDepot(tile)) {
    01575     if (GetTileOwner(tile) == old_owner) {
    01576       if (new_owner == INVALID_OWNER) {
    01577         DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
    01578       } else {
    01579         SetTileOwner(tile, new_owner);
    01580       }
    01581     }
    01582     return;
    01583   }
    01584
    01585   for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    01586     /* Update all roadtypes, no matter if they are present */
    01587     if (GetRoadOwner(tile, rt) == old_owner) {
    01588       SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
    01589     }
    01590   }
    01591
    01592   if (IsLevelCrossing(tile)) {
    01593     if (GetTileOwner(tile) == old_owner) {
    01594       if (new_owner == INVALID_OWNER) {
    01595         DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
    01596       } else {
    01597         SetTileOwner(tile, new_owner);
    01598       }
    01599     }
    01600   }
    01601 }
    01602
    01603 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    01604 {
    01605   if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
    01606     switch (GetRoadTileType(tile)) {
    01607       case ROAD_TILE_CROSSING:
    01608         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]);
    01609         break;
    01610
    01611       case ROAD_TILE_DEPOT:
    01612         if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01613         break;
    01614
    01615       case ROAD_TILE_NORMAL: {
    01616         RoadBits bits = GetAllRoadBits(tile);
    01617         RoadBits bits_copy = bits;
    01618         /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
    01619         if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
    01620           /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
    01621           if (bits == bits_copy) {
    01622             uint z_old;
    01623             Slope tileh_old = GetTileSlope(tile, &z_old);
    01624
    01625             /* Get the slope on top of the foundation */
    01626             z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
    01627             z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
    01628
    01629             /* The surface slope must not be changed */
    01630             if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01631           }
    01632         }
    01633         break;
    01634       }
    01635
    01636       default: NOT_REACHED();
    01637     }
    01638   }
    01639
    01640   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    01641 }
    01642
    01644 extern const TileTypeProcs _tile_type_road_procs = {
    01645   DrawTile_Road,           // draw_tile_proc
    01646   GetSlopeZ_Road,          // get_slope_z_proc
    01647   ClearTile_Road,          // clear_tile_proc
    01648   NULL,                    // add_accepted_cargo_proc
    01649   GetTileDesc_Road,        // get_tile_desc_proc
    01650   GetTileTrackStatus_Road, // get_tile_track_status_proc
    01651   ClickTile_Road,          // click_tile_proc
    01652   NULL,                    // animate_tile_proc
    01653   TileLoop_Road,           // tile_loop_clear
    01654   ChangeTileOwner_Road,    // change_tile_owner_clear
    01655   NULL,                    // add_produced_cargo_proc
    01656   VehicleEnter_Road,       // vehicle_enter_tile_proc
    01657   GetFoundation_Road,      // get_foundation_proc
    01658   TerraformTile_Road,      // terraform_tile_proc
    01659 };