# road_cmd.cpp

Go to the documentation of this file.

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