# road_cmd.cpp

Go to the documentation of this file.

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