# road_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: road_cmd.cpp 15704 2009-03-13 23:48:07Z smatz $ */
    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         RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
    00325         if (rts == ROADTYPES_NONE) {
    00326           TrackBits tracks = GetCrossingRailBits(tile);
    00327           bool reserved = GetCrossingReservation(tile);
    00328           MakeRailNormal(tile, GetTileOwner(tile), tracks, GetRailType(tile));
    00329           if (reserved) SetTrackReservation(tile, tracks);
    00330         } else {
    00331           SetRoadTypes(tile, rts);
    00332           /* If we ever get HWAY and it is possible without road then we will need to promote ownership and invalidate town index here, too */
    00333         }
    00334         MarkTileDirtyByTile(tile);
    00335         YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
    00336       }
    00337       return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road * 2);
    00338     }
    00339
    00340     default:
    00341     case ROAD_TILE_DEPOT:
    00342       return CMD_ERROR;
    00343   }
    00344 }
    00345
    00346
    00354 CommandCost CmdRemoveRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00355 {
    00356   RoadType rt = (RoadType)GB(p1, 4, 2);
    00357   if (!IsValidRoadType(rt)) return CMD_ERROR;
    00358
    00359   RoadBits pieces = Extract<RoadBits, 0>(p1);
    00360
    00361   return RemoveRoad(tile, flags, pieces, rt, true);
    00362 }
    00363
    00375 static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
    00376 {
    00377   /* Remove already build pieces */
    00378   CLRBITS(*pieces, existing);
    00379
    00380   /* If we can't build anything stop here */
    00381   if (*pieces == ROAD_NONE) return CMD_ERROR;
    00382
    00383   /* All RoadBit combos are valid on flat land */
    00384   if (tileh == SLOPE_FLAT) return CommandCost();
    00385
    00386   /* Proceed steep Slopes first to reduce lookup table size */
    00387   if (IsSteepSlope(tileh)) {
    00388     /* Force straight roads. */
    00389     *pieces |= MirrorRoadBits(*pieces);
    00390
    00391     /* Use existing as all existing since only straight
    00392      * roads are allowed here. */
    00393     existing |= other;
    00394
    00395     if ((existing == ROAD_NONE || existing == *pieces) && IsStraightRoad(*pieces)) {
    00396       return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    00397     }
    00398     return CMD_ERROR;
    00399   }
    00400
    00401   /* Save the merge of all bits of the current type */
    00402   RoadBits type_bits = existing | *pieces;
    00403
    00404   /* Roads on slopes */
    00405   if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) {
    00406
    00407     /* If we add leveling we've got to pay for it */
    00408     if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    00409
    00410     return CommandCost();
    00411   }
    00412
    00413   /* Autocomplete uphill roads */
    00414   *pieces |= MirrorRoadBits(*pieces);
    00415   type_bits = existing | *pieces;
    00416
    00417   /* Uphill roads */
    00418   if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) &&
    00419       (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) {
    00420
    00421     /* Slopes with foundation ? */
    00422     if (IsSlopeWithOneCornerRaised(tileh)) {
    00423
    00424       /* Prevent build on slopes if it isn't allowed */
    00425       if (_settings_game.construction.build_on_slopes) {
    00426
    00427         /* If we add foundation we've got to pay for it */
    00428         if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    00429
    00430         return CommandCost();
    00431       }
    00432     } else {
    00433       if (CountBits(existing) == 1) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    00434       return CommandCost();
    00435     }
    00436   }
    00437   return CMD_ERROR;
    00438 }
    00439
    00448 CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00449 {
    00450   CommandCost cost(EXPENSES_CONSTRUCTION);
    00451
    00452   RoadBits existing = ROAD_NONE;
    00453   RoadBits other_bits = ROAD_NONE;
    00454
    00455   /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
    00456    * if a non-company is building the road */
    00457   if ((IsValidCompanyID(_current_company) && p2 != 0) || (_current_company == OWNER_TOWN && !IsValidTownID(p2))) return CMD_ERROR;
    00458   if (_current_company != OWNER_TOWN) {
    00459     const Town *town = CalcClosestTownFromTile(tile);
    00460     p2 = (town != NULL) ? town->index : (TownID)INVALID_TOWN;
    00461   }
    00462
    00463   RoadBits pieces = Extract<RoadBits, 0>(p1);
    00464
    00465   /* do not allow building 'zero' road bits, code wouldn't handle it */
    00466   if (pieces == ROAD_NONE) return CMD_ERROR;
    00467
    00468   RoadType rt = (RoadType)GB(p1, 4, 2);
    00469   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00470
    00471   DisallowedRoadDirections toggle_drd = (DisallowedRoadDirections)GB(p1, 6, 2);
    00472
    00473   Slope tileh = GetTileSlope(tile, NULL);
    00474
    00475   switch (GetTileType(tile)) {
    00476     case MP_ROAD:
    00477       switch (GetRoadTileType(tile)) {
    00478         case ROAD_TILE_NORMAL: {
    00479           if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
    00480
    00481           other_bits = GetOtherRoadBits(tile, rt);
    00482           if (!HasTileRoadType(tile, rt)) break;
    00483
    00484           existing = GetRoadBits(tile, rt);
    00485           bool crossing = !IsStraightRoad(existing | pieces);
    00486           if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
    00487             /* Junctions cannot be one-way */
    00488             return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
    00489           }
    00490           if ((existing & pieces) == pieces) {
    00491             /* We only want to set the (dis)allowed road directions */
    00492             if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM && IsRoadOwner(tile, ROADTYPE_ROAD, _current_company)) {
    00493               if (crossing) return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
    00494
    00495               if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00496
    00497               /* Ignore half built tiles */
    00498               if (flags & DC_EXEC && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
    00499                 SetDisallowedRoadDirections(tile, GetDisallowedRoadDirections(tile) ^ toggle_drd);
    00500                 MarkTileDirtyByTile(tile);
    00501               }
    00502               return CommandCost();
    00503             }
    00504             return_cmd_error(STR_1007_ALREADY_BUILT);
    00505           }
    00506         } break;
    00507
    00508         case ROAD_TILE_CROSSING:
    00509           other_bits = GetCrossingRoadBits(tile);
    00510           if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
    00511           pieces = other_bits; // we need to pay for both roadbits
    00512
    00513           if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
    00514           break;
    00515
    00516         default:
    00517         case ROAD_TILE_DEPOT:
    00518           goto do_clear;
    00519       }
    00520       break;
    00521
    00522     case MP_RAILWAY: {
    00523       if (IsSteepSlope(tileh)) {
    00524         return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
    00525       }
    00526
    00527       /* Level crossings may only be built on these slopes */
    00528       if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
    00529         return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
    00530       }
    00531
    00532       if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
    00533
    00534       Axis roaddir;
    00535       switch (GetTrackBits(tile)) {
    00536         case TRACK_BIT_X:
    00537           if (pieces & ROAD_X) goto do_clear;
    00538           roaddir = AXIS_Y;
    00539           break;
    00540
    00541         case TRACK_BIT_Y:
    00542           if (pieces & ROAD_Y) goto do_clear;
    00543           roaddir = AXIS_X;
    00544           break;
    00545
    00546         default: goto do_clear;
    00547       }
    00548
    00549       if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00550
    00551       if (flags & DC_EXEC) {
    00552         YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
    00553         /* Always add road to the roadtypes (can't draw without it) */
    00554         bool reserved = HasBit(GetTrackReservation(tile), AxisToTrack(OtherAxis(roaddir)));
    00555         MakeRoadCrossing(tile, _current_company, _current_company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
    00556         SetCrossingReservation(tile, reserved);
    00557         UpdateLevelCrossing(tile, false);
    00558         MarkTileDirtyByTile(tile);
    00559       }
    00560       return CommandCost(EXPENSES_CONSTRUCTION, _price.build_road * (rt == ROADTYPE_ROAD ? 2 : 4));
    00561     }
    00562
    00563     case MP_STATION: {
    00564       if (!IsDriveThroughStopTile(tile)) goto do_clear;
    00565
    00566       RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
    00567       if (pieces & ~curbits) goto do_clear;
    00568       pieces = curbits; // we need to pay for both roadbits
    00569
    00570       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
    00571     } break;
    00572
    00573     case MP_TUNNELBRIDGE:
    00574       if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR;
    00575       if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) return CMD_ERROR;
    00576       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
    00577       /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
    00578       if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
    00579       break;
    00580
    00581     default: {
    00582 do_clear:;
    00583       CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00584       if (CmdFailed(ret)) return ret;
    00585       cost.AddCost(ret);
    00586     } break;
    00587   }
    00588
    00589   if (other_bits != pieces) {
    00590     /* Check the foundation/slopes when adding road/tram bits */
    00591     CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
    00592     /* Return an error if we need to build a foundation (ret != 0) but the
    00593      * current setting is turned off */
    00594     if (CmdFailed(ret) || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
    00595       return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
    00596     }
    00597     cost.AddCost(ret);
    00598   }
    00599
    00600   if (IsTileType(tile, MP_ROAD)) {
    00601     /* Don't put the pieces that already exist */
    00602     pieces &= ComplementRoadBits(existing);
    00603
    00604     /* Check if new road bits will have the same foundation as other existing road types */
    00605     if (IsNormalRoad(tile)) {
    00606       Slope slope = GetTileSlope(tile, NULL);
    00607       Foundation found_new = GetRoadFoundation(slope, pieces | existing);
    00608
    00609       /* Test if all other roadtypes can be built at that foundation */
    00610       for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
    00611         if (rtest != rt) { // check only other road types
    00612           RoadBits bits = GetRoadBits(tile, rtest);
    00613           /* do not check if there are not road bits of given type */
    00614           if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
    00615             return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
    00616           }
    00617         }
    00618       }
    00619     }
    00620   }
    00621
    00622   if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00623
    00624   cost.AddCost(CountBits(pieces) * _price.build_road);
    00625   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00626     /* Pay for *every* tile of the bridge or tunnel */
    00627     cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
    00628   }
    00629
    00630   if (flags & DC_EXEC) {
    00631     switch (GetTileType(tile)) {
    00632       case MP_ROAD: {
    00633         RoadTileType rtt = GetRoadTileType(tile);
    00634         if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
    00635           SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00636           SetRoadOwner(tile, rt, _current_company);
    00637           if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
    00638         }
    00639         if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
    00640       } break;
    00641
    00642       case MP_TUNNELBRIDGE: {
    00643         TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
    00644
    00645         SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
    00646         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00647         SetRoadOwner(other_end, rt, _current_company);
    00648         SetRoadOwner(tile, rt, _current_company);
    00649
    00650         /* Mark tiles diry that have been repaved */
    00651         MarkTileDirtyByTile(other_end);
    00652         MarkTileDirtyByTile(tile);
    00653         if (IsBridge(tile)) {
    00654           TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
    00655
    00656           for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
    00657         }
    00658       } break;
    00659
    00660       case MP_STATION:
    00661         assert(IsDriveThroughStopTile(tile));
    00662         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00663         SetRoadOwner(tile, rt, _current_company);
    00664         break;
    00665
    00666       default:
    00667         MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
    00668         break;
    00669     }
    00670
    00671     if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
    00672       existing |= pieces;
    00673       SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
    00674           GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
    00675     }
    00676
    00677     MarkTileDirtyByTile(tile);
    00678   }
    00679   return cost;
    00680 }
    00681
    00693 CommandCost CmdBuildLongRoad(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00694 {
    00695   CommandCost cost(EXPENSES_CONSTRUCTION);
    00696   bool had_bridge = false;
    00697   bool had_tunnel = false;
    00698   bool had_success = false;
    00699   DisallowedRoadDirections drd = DRD_NORTHBOUND;
    00700
    00701   _error_message = INVALID_STRING_ID;
    00702
    00703   if (p1 >= MapSize()) return CMD_ERROR;
    00704
    00705   TileIndex start_tile = p1;
    00706   RoadType rt = (RoadType)GB(p2, 3, 2);
    00707   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00708
    00709   /* Only drag in X or Y direction dictated by the direction variable */
    00710   if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00711   if (HasBit(p2, 2)  && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00712
    00713   /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
    00714   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00715     TileIndex t = start_tile;
    00716     start_tile = end_tile;
    00717     end_tile = t;
    00718     p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
    00719     drd = DRD_SOUTHBOUND;
    00720   }
    00721
    00722   /* On the X-axis, we have to swap the initial bits, so they
    00723    * will be interpreted correctly in the GTTS. Futhermore
    00724    * when you just 'click' on one tile to build them. */
    00725   if (HasBit(p2, 2) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
    00726   /* No disallowed direction bits have to be toggled */
    00727   if (!HasBit(p2, 5)) drd = DRD_NONE;
    00728
    00729   TileIndex tile = start_tile;
    00730   /* Start tile is the small number. */
    00731   for (;;) {
    00732     RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X;
    00733
    00734     if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
    00735     if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
    00736
    00737     _error_message = INVALID_STRING_ID;
    00738     CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
    00739     if (CmdFailed(ret)) {
    00740       if (_error_message != STR_1007_ALREADY_BUILT) return CMD_ERROR;
    00741     } else {
    00742       had_success = true;
    00743       /* Only pay for the upgrade on one side of the bridges and tunnels */
    00744       if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00745         if (IsBridge(tile)) {
    00746           if ((!had_bridge || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) {
    00747             cost.AddCost(ret);
    00748           }
    00749           had_bridge = true;
    00750         } else { // IsTunnel(tile)
    00751           if ((!had_tunnel || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) {
    00752             cost.AddCost(ret);
    00753           }
    00754           had_tunnel = true;
    00755         }
    00756       } else {
    00757         cost.AddCost(ret);
    00758       }
    00759     }
    00760
    00761     if (tile == end_tile) break;
    00762
    00763     tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
    00764   }
    00765
    00766   return !had_success ? CMD_ERROR : cost;
    00767 }
    00768
    00779 CommandCost CmdRemoveLongRoad(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00780 {
    00781   CommandCost cost(EXPENSES_CONSTRUCTION);
    00782
    00783   if (p1 >= MapSize()) return CMD_ERROR;
    00784
    00785   TileIndex start_tile = p1;
    00786   RoadType rt = (RoadType)GB(p2, 3, 2);
    00787   if (!IsValidRoadType(rt)) return CMD_ERROR;
    00788
    00789   /* Only drag in X or Y direction dictated by the direction variable */
    00790   if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00791   if (HasBit(p2, 2)  && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00792
    00793   /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
    00794   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00795     TileIndex t = start_tile;
    00796     start_tile = end_tile;
    00797     end_tile = t;
    00798     p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
    00799   }
    00800
    00801   Money money = GetAvailableMoneyForCommand();
    00802   TileIndex tile = start_tile;
    00803   /* Start tile is the small number. */
    00804   for (;;) {
    00805     RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X;
    00806
    00807     if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
    00808     if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
    00809
    00810     /* try to remove the halves. */
    00811     if (bits != 0) {
    00812       CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
    00813       if (CmdSucceeded(ret)) {
    00814         if (flags & DC_EXEC) {
    00815           money -= ret.GetCost();
    00816           if (money < 0) {
    00817             _additional_cash_required = DoCommand(end_tile, start_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
    00818             return cost;
    00819           }
    00820           RemoveRoad(tile, flags, bits, rt, true, false);
    00821         }
    00822         cost.AddCost(ret);
    00823       }
    00824     }
    00825
    00826     if (tile == end_tile) break;
    00827
    00828     tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
    00829   }
    00830
    00831   return (cost.GetCost() == 0) ? CMD_ERROR : cost;
    00832 }
    00833
    00844 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00845 {
    00846   DiagDirection dir = Extract<DiagDirection, 0>(p1);
    00847   RoadType rt = (RoadType)GB(p1, 2, 2);
    00848
    00849   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00850
    00851   Slope tileh = GetTileSlope(tile, NULL);
    00852   if (tileh != SLOPE_FLAT && (
    00853         !_settings_game.construction.build_on_slopes ||
    00854         IsSteepSlope(tileh) ||
    00855         !CanBuildDepotByTileh(dir, tileh)
    00856       )) {
    00857     return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
    00858   }
    00859
    00860   CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00861   if (CmdFailed(cost)) return CMD_ERROR;
    00862
    00863   if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
    00864
    00865   if (!Depot::CanAllocateItem()) return CMD_ERROR;
    00866
    00867   if (flags & DC_EXEC) {
    00868     Depot *dep = new Depot(tile);
    00869     dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
    00870
    00871     MakeRoadDepot(tile, _current_company, dir, rt, dep->town_index);
    00872     MarkTileDirtyByTile(tile);
    00873   }
    00874   return cost.AddCost(_price.build_road_depot);
    00875 }
    00876
    00877 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
    00878 {
    00879   if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
    00880
    00881   if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00882
    00883   if (flags & DC_EXEC) {
    00884     DoClearSquare(tile);
    00885     delete GetDepotByTile(tile);
    00886   }
    00887
    00888   return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road_depot);
    00889 }
    00890
    00891 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
    00892 {
    00893   switch (GetRoadTileType(tile)) {
    00894     case ROAD_TILE_NORMAL: {
    00895       RoadBits b = GetAllRoadBits(tile);
    00896
    00897       /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
    00898       if ((CountBits(b) == 1 && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
    00899         RoadTypes rts = GetRoadTypes(tile);
    00900         CommandCost ret(EXPENSES_CONSTRUCTION);
    00901         for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    00902           if (HasBit(rts, rt)) {
    00903             CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
    00904             if (CmdFailed(tmp_ret)) return tmp_ret;
    00905             ret.AddCost(tmp_ret);
    00906           }
    00907         }
    00908         return ret;
    00909       }
    00910       return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
    00911     }
    00912
    00913     case ROAD_TILE_CROSSING: {
    00914       RoadTypes rts = GetRoadTypes(tile);
    00915       CommandCost ret(EXPENSES_CONSTRUCTION);
    00916
    00917       if (flags & DC_AUTO) return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
    00918
    00919       /* Must iterate over the roadtypes in a reverse manner because
    00920        * tram tracks must be removed before the road bits. */
    00921       RoadType rt = ROADTYPE_TRAM;
    00922       do {
    00923         if (HasBit(rts, rt)) {
    00924           CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
    00925           if (CmdFailed(tmp_ret)) return tmp_ret;
    00926           ret.AddCost(tmp_ret);
    00927         }
    00928       } while (rt-- != ROADTYPE_ROAD);
    00929
    00930       if (flags & DC_EXEC) {
    00931         DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00932       }
    00933       return ret;
    00934     }
    00935
    00936     default:
    00937     case ROAD_TILE_DEPOT:
    00938       if (flags & DC_AUTO) {
    00939         return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
    00940       }
    00941       return RemoveRoadDepot(tile, flags);
    00942   }
    00943 }
    00944
    00945
    00946 struct DrawRoadTileStruct {
    00947   uint16 image;
    00948   byte subcoord_x;
    00949   byte subcoord_y;
    00950 };
    00951
    00952 #include "table/road_land.h"
    00953
    00961 Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
    00962 {
    00963   /* Flat land and land without a road doesn't require a foundation */
    00964   if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
    00965
    00966   if (!IsSteepSlope(tileh)) {
    00967     /* Leveled RoadBits on a slope */
    00968     if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
    00969
    00970     /* Straight roads without foundation on a slope */
    00971     if (!IsSlopeWithOneCornerRaised(tileh) &&
    00972         (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
    00973       return FOUNDATION_NONE;
    00974   }
    00975
    00976   /* Roads on steep Slopes or on Slopes with one corner raised */
    00977   return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
    00978 }
    00979
    00980 const byte _road_sloped_sprites[14] = {
    00981   0,  0,  2,  0,
    00982   0,  1,  0,  0,
    00983   3,  0,  0,  0,
    00984   0,  0
    00985 };
    00986
    00997 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
    00998 {
    00999   return (IsOnSnow(tile) &&
    01000       !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
    01001         roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
    01002 }
    01003
    01009 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
    01010 {
    01011   /* Do not draw catenary if it is invisible */
    01012   if (IsInvisibilitySet(TO_CATENARY)) return;
    01013
    01014   /* Don't draw the catenary under a low bridge */
    01015   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
    01016     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01017
    01018     if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
    01019   }
    01020
    01021   SpriteID front;
    01022   SpriteID back;
    01023
    01024   if (ti->tileh != SLOPE_FLAT) {
    01025     back  = SPR_TRAMWAY_BACK_WIRES_SLOPED  + _road_sloped_sprites[ti->tileh - 1];
    01026     front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
    01027   } else {
    01028     back  = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
    01029     front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
    01030   }
    01031
    01032   AddSortableSpriteToDraw(back,  PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01033   AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01034 }
    01035
    01044 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
    01045 {
    01046   int x = ti->x | dx;
    01047   int y = ti->y | dy;
    01048   byte z = ti->z;
    01049   if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
    01050   AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
    01051 }
    01052
    01057 static void DrawRoadBits(TileInfo *ti)
    01058 {
    01059   RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
    01060   RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
    01061
    01062   SpriteID image = 0;
    01063   SpriteID pal = PAL_NONE;
    01064
    01065   if (ti->tileh != SLOPE_FLAT) {
    01066     DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
    01067
    01068     /* DrawFoundation() modifies ti.
    01069      * Default sloped sprites.. */
    01070     if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
    01071   }
    01072
    01073   if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
    01074
    01075   Roadside roadside = GetRoadside(ti->tile);
    01076
    01077   if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01078     image += 19;
    01079   } else {
    01080     switch (roadside) {
    01081       case ROADSIDE_BARREN:           pal = PALETTE_TO_BARE_LAND; break;
    01082       case ROADSIDE_GRASS:            break;
    01083       case ROADSIDE_GRASS_ROAD_WORKS: break;
    01084       default:                        image -= 19; break; // Paved
    01085     }
    01086   }
    01087
    01088   DrawGroundSprite(image, pal);
    01089
    01090   /* For tram we overlay the road graphics with either tram tracks only
    01091    * (when there is actual road beneath the trams) or with tram tracks
    01092    * and some dirts which hides the road graphics */
    01093   if (tram != ROAD_NONE) {
    01094     if (ti->tileh != SLOPE_FLAT) {
    01095       image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
    01096     } else {
    01097       image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
    01098     }
    01099     image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
    01100     DrawGroundSprite(image, pal);
    01101   }
    01102
    01103   if (road != ROAD_NONE) {
    01104     DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
    01105     if (drd != DRD_NONE) {
    01106       DrawRoadDetail(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), ti, 8, 8, 0);
    01107     }
    01108   }
    01109
    01110   if (HasRoadWorks(ti->tile)) {
    01111     /* Road works */
    01112     DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
    01113     return;
    01114   }
    01115
    01116   if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
    01117
    01118   /* Return if full detail is disabled, or we are zoomed fully out. */
    01119   if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
    01120
    01121   /* Do not draw details (street lights, trees) under low bridge */
    01122   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
    01123     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01124     uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
    01125
    01126     if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
    01127
    01128     if (height < minz) return;
    01129   }
    01130
    01131   /* If there are no road bits, return, as there is nothing left to do */
    01132   if (CountBits(road) < 2) return;
    01133
    01134   /* Draw extra details. */
    01135   for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
    01136     DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
    01137   }
    01138 }
    01139
    01141 static void DrawTile_Road(TileInfo *ti)
    01142 {
    01143   switch (GetRoadTileType(ti->tile)) {
    01144     case ROAD_TILE_NORMAL:
    01145       DrawRoadBits(ti);
    01146       break;
    01147
    01148     case ROAD_TILE_CROSSING: {
    01149       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    01150
    01151       SpriteID image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.crossing;
    01152       SpriteID pal = PAL_NONE;
    01153
    01154       if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
    01155       if (IsCrossingBarred(ti->tile)) image += 2;
    01156
    01157       Roadside roadside = GetRoadside(ti->tile);
    01158
    01159       if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01160         image += 8;
    01161       } else {
    01162         switch (roadside) {
    01163           case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
    01164           case ROADSIDE_GRASS:  break;
    01165           default:              image += 4; break; // Paved
    01166         }
    01167       }
    01168
    01169       DrawGroundSprite(image, pal);
    01170
    01171       /* PBS debugging, draw reserved tracks darker */
    01172       if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && GetCrossingReservation(ti->tile)) {
    01173         DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x, PALETTE_CRASH);
    01174       }
    01175
    01176       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
    01177         DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
    01178         DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
    01179       }
    01180       if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
    01181       break;
    01182     }
    01183
    01184     default:
    01185     case ROAD_TILE_DEPOT: {
    01186       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    01187
    01188       SpriteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
    01189
    01190       const DrawTileSprites *dts;
    01191       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
    01192         dts =  &_tram_depot[GetRoadDepotDirection(ti->tile)];
    01193       } else {
    01194         dts =  &_road_depot[GetRoadDepotDirection(ti->tile)];
    01195       }
    01196
    01197       DrawGroundSprite(dts->ground.sprite, PAL_NONE);
    01198
    01199       /* End now if buildings are invisible */
    01200       if (IsInvisibilitySet(TO_BUILDINGS)) break;
    01201
    01202       for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
    01203         SpriteID image = dtss->image.sprite;
    01204         SpriteID pal;
    01205
    01206         if (!IsTransparencySet(TO_BUILDINGS) && HasBit(image, PALETTE_MODIFIER_COLOUR)) {
    01207           pal = palette;
    01208         } else {
    01209           pal = PAL_NONE;
    01210         }
    01211
    01212         AddSortableSpriteToDraw(
    01213           image, pal,
    01214           ti->x + dtss->delta_x, ti->y + dtss->delta_y,
    01215           dtss->size_x, dtss->size_y,
    01216           dtss->size_z, ti->z,
    01217           IsTransparencySet(TO_BUILDINGS)
    01218         );
    01219       }
    01220       break;
    01221     }
    01222   }
    01223   DrawBridgeMiddle(ti);
    01224 }
    01225
    01226 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
    01227 {
    01228   SpriteID palette = COMPANY_SPRITE_COLOUR(_local_company);
    01229   const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
    01230
    01231   x += 33;
    01232   y += 17;
    01233
    01234   DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
    01235
    01236   for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
    01237     Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
    01238     SpriteID image = dtss->image.sprite;
    01239
    01240     DrawSprite(image, HasBit(image, PALETTE_MODIFIER_COLOUR) ? palette : PAL_NONE, x + pt.x, y + pt.y);
    01241   }
    01242 }
    01243
    01248 void UpdateNearestTownForRoadTiles(bool invalidate)
    01249 {
    01250   assert(!invalidate || _generating_world);
    01251
    01252   for (TileIndex t = 0; t < MapSize(); t++) {
    01253     if (IsTileType(t, MP_ROAD) && !HasTownOwnedRoad(t)) {
    01254       TownID tid = (TownID)INVALID_TOWN;
    01255       if (!invalidate) {
    01256         const Town *town = CalcClosestTownFromTile(t);
    01257         if (town != NULL) tid = town->index;
    01258       }
    01259       SetTownIndex(t, tid);
    01260     }
    01261   }
    01262 }
    01263
    01264 static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
    01265 {
    01266   uint z;
    01267   Slope tileh = GetTileSlope(tile, &z);
    01268
    01269   if (tileh == SLOPE_FLAT) return z;
    01270   if (IsNormalRoad(tile)) {
    01271     Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
    01272     z += ApplyFoundationToSlope(f, &tileh);
    01273     return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
    01274   } else {
    01275     return z + TILE_HEIGHT;
    01276   }
    01277 }
    01278
    01279 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
    01280 {
    01281   if (IsNormalRoad(tile)) {
    01282     return GetRoadFoundation(tileh, GetAllRoadBits(tile));
    01283   } else {
    01284     return FlatteningFoundation(tileh);
    01285   }
    01286 }
    01287
    01288 static void GetAcceptedCargo_Road(TileIndex tile, AcceptedCargo ac)
    01289 {
    01290   /* not used */
    01291 }
    01292
    01293 static void AnimateTile_Road(TileIndex tile)
    01294 {
    01295   if (IsLevelCrossing(tile)) MarkTileDirtyByTile(tile);
    01296 }
    01297
    01298
    01299 static const Roadside _town_road_types[][2] = {
    01300   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
    01301   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01302   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01303   { ROADSIDE_TREES,         ROADSIDE_TREES },
    01304   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
    01305 };
    01306
    01307 static const Roadside _town_road_types_2[][2] = {
    01308   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
    01309   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01310   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
    01311   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
    01312   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
    01313 };
    01314
    01315
    01316 static void TileLoop_Road(TileIndex tile)
    01317 {
    01318   switch (_settings_game.game_creation.landscape) {
    01319     case LT_ARCTIC:
    01320       if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
    01321         ToggleSnow(tile);
    01322         MarkTileDirtyByTile(tile);
    01323       }
    01324       break;
    01325
    01326     case LT_TROPIC:
    01327       if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
    01328         ToggleDesert(tile);
    01329         MarkTileDirtyByTile(tile);
    01330       }
    01331       break;
    01332   }
    01333
    01334   if (IsRoadDepot(tile)) return;
    01335
    01336   const Town *t = ClosestTownFromTile(tile, UINT_MAX);
    01337   if (!HasRoadWorks(tile)) {
    01338     HouseZonesBits grp = HZB_TOWN_EDGE;
    01339
    01340     if (t != NULL) {
    01341       grp = GetTownRadiusGroup(t, tile);
    01342
    01343       /* Show an animation to indicate road work */
    01344       if (t->road_build_months != 0 &&
    01345           (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
    01346           IsNormalRoad(tile) && CountBits(GetAllRoadBits(tile)) > 1 ) {
    01347         if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
    01348           StartRoadWorks(tile);
    01349
    01350           SndPlayTileFx(SND_21_JACKHAMMER, tile);
    01351           CreateEffectVehicleAbove(
    01352             TileX(tile) * TILE_SIZE + 7,
    01353             TileY(tile) * TILE_SIZE + 7,
    01354             0,
    01355             EV_BULLDOZER);
    01356           MarkTileDirtyByTile(tile);
    01357           return;
    01358         }
    01359       }
    01360     }
    01361
    01362     {
    01363       /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
    01364       const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
    01365       Roadside cur_rs = GetRoadside(tile);
    01366
    01367       /* We have our desired type, do nothing */
    01368       if (cur_rs == new_rs[0]) return;
    01369
    01370       /* We have the pre-type of the desired type, switch to the desired type */
    01371       if (cur_rs == new_rs[1]) {
    01372         cur_rs = new_rs[0];
    01373       /* We have barren land, install the pre-type */
    01374       } else if (cur_rs == ROADSIDE_BARREN) {
    01375         cur_rs = new_rs[1];
    01376       /* We're totally off limits, remove any installation and make barren land */
    01377       } else {
    01378         cur_rs = ROADSIDE_BARREN;
    01379       }
    01380       SetRoadside(tile, cur_rs);
    01381       MarkTileDirtyByTile(tile);
    01382     }
    01383   } else if (IncreaseRoadWorksCounter(tile)) {
    01384     TerminateRoadWorks(tile);
    01385
    01386     if (_settings_game.economy.mod_road_rebuild) {
    01387       /* Generate a nicer town surface */
    01388       const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
    01389       const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
    01390
    01391       if (old_rb != new_rb) {
    01392         RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
    01393       }
    01394     }
    01395
    01396     MarkTileDirtyByTile(tile);
    01397   }
    01398 }
    01399
    01400 static bool ClickTile_Road(TileIndex tile)
    01401 {
    01402   if (!IsRoadDepot(tile)) return false;
    01403
    01404   ShowDepotWindow(tile, VEH_ROAD);
    01405   return true;
    01406 }
    01407
    01408 /* Converts RoadBits to TrackBits */
    01409 static const byte _road_trackbits[16] = {
    01410   0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
    01411 };
    01412
    01413 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    01414 {
    01415   TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
    01416   TrackdirBits red_signals = TRACKDIR_BIT_NONE; // crossing barred
    01417   switch (mode) {
    01418     case TRANSPORT_RAIL:
    01419       if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
    01420       break;
    01421
    01422     case TRANSPORT_ROAD:
    01423       if ((GetRoadTypes(tile) & sub_mode) == 0) break;
    01424       switch (GetRoadTileType(tile)) {
    01425         case ROAD_TILE_NORMAL: {
    01426           const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
    01427           RoadType rt = (RoadType)FindFirstBit(sub_mode);
    01428           RoadBits bits = GetRoadBits(tile, rt);
    01429
    01430           /* no roadbit at this side of tile, return 0 */
    01431           if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
    01432
    01433           uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
    01434           if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
    01435           break;
    01436         }
    01437
    01438         case ROAD_TILE_CROSSING: {
    01439           Axis axis = GetCrossingRoadAxis(tile);
    01440
    01441           if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
    01442
    01443           trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
    01444           if (IsCrossingBarred(tile)) red_signals = trackdirbits;
    01445           break;
    01446         }
    01447
    01448         default:
    01449         case ROAD_TILE_DEPOT: {
    01450           DiagDirection dir = GetRoadDepotDirection(tile);
    01451
    01452           if (side != INVALID_DIAGDIR && side != dir) break;
    01453
    01454           trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
    01455           break;
    01456         }
    01457       }
    01458       break;
    01459
    01460     default: break;
    01461   }
    01462   return CombineTrackStatus(trackdirbits, red_signals);
    01463 }
    01464
    01465 static const StringID _road_tile_strings[] = {
    01466   STR_1814_ROAD,
    01467   STR_1814_ROAD,
    01468   STR_1814_ROAD,
    01469   STR_1815_ROAD_WITH_STREETLIGHTS,
    01470   STR_1814_ROAD,
    01471   STR_1816_TREE_LINED_ROAD,
    01472   STR_1814_ROAD,
    01473   STR_1814_ROAD,
    01474 };
    01475
    01476 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
    01477 {
    01478   Owner rail_owner = INVALID_OWNER;
    01479   Owner road_owner = INVALID_OWNER;
    01480   Owner tram_owner = INVALID_OWNER;
    01481
    01482   switch (GetRoadTileType(tile)) {
    01483     case ROAD_TILE_CROSSING: {
    01484       td->str = STR_1818_ROAD_RAIL_LEVEL_CROSSING;
    01485       RoadTypes rts = GetRoadTypes(tile);
    01486       rail_owner = GetTileOwner(tile);
    01487       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01488       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01489       break;
    01490     }
    01491
    01492     case ROAD_TILE_DEPOT:
    01493       td->str = STR_1817_ROAD_VEHICLE_DEPOT;
    01494       road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter
    01495       break;
    01496
    01497     default: {
    01498       RoadTypes rts = GetRoadTypes(tile);
    01499       td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_TRAMWAY);
    01500       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01501       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01502       break;
    01503     }
    01504   }
    01505
    01506   /* Now we have to discover, if the tile has only one owner or many:
    01507    *   - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
    01508    *   - Compare the found owner with the other owners, and test if they differ.
    01509    * Note: If road exists it will be the first_owner.
    01510    */
    01511   Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
    01512   bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
    01513
    01514   if (mixed_owners) {
    01515     /* Multiple owners */
    01516     td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_RAIL_OWNER);
    01517     td->owner[0] = rail_owner;
    01518     td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_ROAD_OWNER);
    01519     td->owner[1] = road_owner;
    01520     td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_TRAM_OWNER);
    01521     td->owner[2] = tram_owner;
    01522   } else {
    01523     /* One to rule them all */
    01524     td->owner[0] = first_owner;
    01525   }
    01526 }
    01527
    01532 static const byte _roadveh_enter_depot_dir[4] = {
    01533   TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
    01534 };
    01535
    01536 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
    01537 {
    01538   switch (GetRoadTileType(tile)) {
    01539     case ROAD_TILE_CROSSING:
    01540       if (v->type == VEH_TRAIN) {
    01541         /* it should be barred */
    01542         assert(IsCrossingBarred(tile));
    01543       }
    01544       break;
    01545
    01546     case ROAD_TILE_DEPOT:
    01547       if (v->type == VEH_ROAD &&
    01548           v->u.road.frame == RVC_DEPOT_STOP_FRAME &&
    01549           _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == v->u.road.state) {
    01550         v->u.road.state = RVSB_IN_DEPOT;
    01551         v->vehstatus |= VS_HIDDEN;
    01552         v->direction = ReverseDir(v->direction);
    01553         if (v->Next() == NULL) VehicleEnterDepot(v);
    01554         v->tile = tile;
    01555
    01556         InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
    01557         return VETSB_ENTERED_WORMHOLE;
    01558       }
    01559       break;
    01560
    01561     default: break;
    01562   }
    01563   return VETSB_CONTINUE;
    01564 }
    01565
    01566
    01567 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
    01568 {
    01569   if (IsRoadDepot(tile)) {
    01570     if (GetTileOwner(tile) == old_owner) {
    01571       if (new_owner == INVALID_OWNER) {
    01572         DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
    01573       } else {
    01574         SetTileOwner(tile, new_owner);
    01575       }
    01576     }
    01577     return;
    01578   }
    01579
    01580   for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    01581     /* Update all roadtypes, no matter if they are present */
    01582     if (GetRoadOwner(tile, rt) == old_owner) {
    01583       SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
    01584     }
    01585   }
    01586
    01587   if (IsLevelCrossing(tile)) {
    01588     if (GetTileOwner(tile) == old_owner) {
    01589       if (new_owner == INVALID_OWNER) {
    01590         DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
    01591       } else {
    01592         SetTileOwner(tile, new_owner);
    01593       }
    01594     }
    01595   }
    01596 }
    01597
    01598 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    01599 {
    01600   if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
    01601     switch (GetRoadTileType(tile)) {
    01602       case ROAD_TILE_CROSSING:
    01603         if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new)) && HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    01604         break;
    01605
    01606       case ROAD_TILE_DEPOT:
    01607         if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    01608         break;
    01609
    01610       case ROAD_TILE_NORMAL: {
    01611         RoadBits bits = GetAllRoadBits(tile);
    01612         RoadBits bits_copy = bits;
    01613         /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
    01614         if (!CmdFailed(CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE))) {
    01615           /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
    01616           if (bits == bits_copy) {
    01617             uint z_old;
    01618             Slope tileh_old = GetTileSlope(tile, &z_old);
    01619
    01620             /* Get the slope on top of the foundation */
    01621             z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
    01622             z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
    01623
    01624             /* The surface slope must not be changed */
    01625             if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    01626           }
    01627         }
    01628         break;
    01629       }
    01630
    01631       default: NOT_REACHED();
    01632     }
    01633   }
    01634
    01635   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    01636 }
    01637
    01639 extern const TileTypeProcs _tile_type_road_procs = {
    01640   DrawTile_Road,           // draw_tile_proc
    01641   GetSlopeZ_Road,          // get_slope_z_proc
    01642   ClearTile_Road,          // clear_tile_proc
    01643   GetAcceptedCargo_Road,   // get_accepted_cargo_proc
    01644   GetTileDesc_Road,        // get_tile_desc_proc
    01645   GetTileTrackStatus_Road, // get_tile_track_status_proc
    01646   ClickTile_Road,          // click_tile_proc
    01647   AnimateTile_Road,        // animate_tile_proc
    01648   TileLoop_Road,           // tile_loop_clear
    01649   ChangeTileOwner_Road,    // change_tile_owner_clear
    01650   NULL,                    // get_produced_cargo_proc
    01651   VehicleEnter_Road,       // vehicle_enter_tile_proc
    01652   GetFoundation_Road,      // get_foundation_proc
    01653   TerraformTile_Road,      // terraform_tile_proc
    01654 };