# road_cmd.cpp

Go to the documentation of this file.

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