# road_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "cmd_helper.h"
    00014 #include "road_internal.h"
    00015 #include "viewport_func.h"
    00016 #include "command_func.h"
    00017 #include "pathfinder/yapf/yapf_cache.h"
    00018 #include "depot_base.h"
    00019 #include "newgrf.h"
    00020 #include "autoslope.h"
    00021 #include "tunnelbridge_map.h"
    00022 #include "window_func.h"
    00023 #include "strings_func.h"
    00024 #include "vehicle_func.h"
    00025 #include "sound_func.h"
    00026 #include "tunnelbridge.h"
    00027 #include "cheat_type.h"
    00028 #include "effectvehicle_func.h"
    00029 #include "effectvehicle_base.h"
    00030 #include "elrail_func.h"
    00031 #include "roadveh.h"
    00032 #include "town.h"
    00033 #include "company_base.h"
    00034 #include "core/random_func.hpp"
    00035 #include "newgrf_railtype.h"
    00036 #include "date_func.h"
    00037 #include "genworld.h"
    00038 #include "trafficlight_func.h"
    00039
    00040 #include "table/strings.h"
    00041
    00046 bool RoadVehiclesAreBuilt()
    00047 {
    00048   const RoadVehicle *rv;
    00049   FOR_ALL_ROADVEHICLES(rv) return true;
    00050
    00051   return false;
    00052 }
    00053
    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 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits);
    00104
    00115 CommandCost 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 CommandCost();
    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 && !Company::IsValidID(_current_company))) return CommandCost();
    00124
    00125   /* Only do the special processing if the road is owned
    00126    * by a town */
    00127   if (owner != OWNER_TOWN) {
    00128     if (owner == OWNER_NONE) return CommandCost();
    00129     CommandCost ret = CheckOwnership(owner);
    00130     return ret;
    00131   }
    00132
    00133   if (!town_check) return CommandCost();
    00134
    00135   if (_cheats.magic_bulldozer.value) return CommandCost();
    00136
    00137   Town *t = ClosestTownFromTile(tile, UINT_MAX);
    00138   if (t == NULL) return CommandCost();
    00139
    00140   /* check if you're allowed to remove the street owned by a town
    00141    * removal allowance depends on difficulty setting */
    00142   CommandCost ret = CheckforTownRating(flags, t, ROAD_REMOVE);
    00143   if (ret.Failed()) return ret;
    00144
    00145   /* Get a bitmask of which neighbouring roads has a tile */
    00146   RoadBits n = ROAD_NONE;
    00147   RoadBits present = GetAnyRoadBits(tile, rt);
    00148   if ((present & ROAD_NE) && (GetAnyRoadBits(TILE_ADDXY(tile, -1,  0), rt) & ROAD_SW)) n |= ROAD_NE;
    00149   if ((present & ROAD_SE) && (GetAnyRoadBits(TILE_ADDXY(tile,  0,  1), rt) & ROAD_NW)) n |= ROAD_SE;
    00150   if ((present & ROAD_SW) && (GetAnyRoadBits(TILE_ADDXY(tile,  1,  0), rt) & ROAD_NE)) n |= ROAD_SW;
    00151   if ((present & ROAD_NW) && (GetAnyRoadBits(TILE_ADDXY(tile,  0, -1), rt) & ROAD_SE)) n |= ROAD_NW;
    00152
    00153   int rating_decrease = RATING_ROAD_DOWN_STEP_EDGE;
    00154   /* If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
    00155    * then allow it */
    00156   if (KillFirstBit(n) != ROAD_NONE && (n & remove) != ROAD_NONE) {
    00157     /* you can remove all kind of roads with extra dynamite */
    00158     if (!_settings_game.construction.extra_dynamite) {
    00159       SetDParam(0, t->index);
    00160       return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
    00161     }
    00162     rating_decrease = RATING_ROAD_DOWN_STEP_INNER;
    00163   }
    00164   ChangeTownRating(t, rating_decrease, RATING_ROAD_MINIMUM, flags);
    00165
    00166   return CommandCost();
    00167 }
    00168
    00169
    00180 static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits pieces, RoadType rt, bool crossing_check, bool trafficlights_check, bool town_check = true)
    00181 {
    00182   RoadTypes rts = GetRoadTypes(tile);
    00183   /* The tile doesn't have the given road type */
    00184   if (!HasBit(rts, rt)) return_cmd_error(rt == ROADTYPE_TRAM ? STR_ERROR_THERE_IS_NO_TRAMWAY : STR_ERROR_THERE_IS_NO_ROAD);
    00185
    00186   switch (GetTileType(tile)) {
    00187     case MP_ROAD: {
    00188       CommandCost ret = EnsureNoVehicleOnGround(tile);
    00189       if (ret.Failed()) return ret;
    00190       break;
    00191     }
    00192
    00193     case MP_STATION: {
    00194       if (!IsDriveThroughStopTile(tile)) return CMD_ERROR;
    00195
    00196       CommandCost ret = EnsureNoVehicleOnGround(tile);
    00197       if (ret.Failed()) return ret;
    00198       break;
    00199     }
    00200
    00201     case MP_TUNNELBRIDGE: {
    00202       if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR;
    00203       CommandCost ret = TunnelBridgeIsFree(tile, GetOtherTunnelBridgeEnd(tile));
    00204       if (ret.Failed()) return ret;
    00205       break;
    00206     }
    00207
    00208     default:
    00209       return CMD_ERROR;
    00210   }
    00211
    00212   CommandCost ret = CheckAllowRemoveRoad(tile, pieces, GetRoadOwner(tile, rt), rt, flags, town_check);
    00213   if (ret.Failed()) return ret;
    00214
    00215   if (!IsTileType(tile, MP_ROAD)) {
    00216     /* If it's the last roadtype, just clear the whole tile */
    00217     if (rts == RoadTypeToRoadTypes(rt)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00218
    00219     CommandCost cost(EXPENSES_CONSTRUCTION);
    00220     if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00221       TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
    00222       /* Pay for *every* tile of the bridge or tunnel */
    00223       cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price[PR_CLEAR_ROAD]);
    00224       if (flags & DC_EXEC) {
    00225         SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt));
    00226         SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
    00227
    00228         /* If the owner of the bridge sells all its road, also move the ownership
    00229          * to the owner of the other roadtype. */
    00230         RoadType other_rt = (rt == ROADTYPE_ROAD) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
    00231         Owner other_owner = GetRoadOwner(tile, other_rt);
    00232         if (other_owner != GetTileOwner(tile)) {
    00233           SetTileOwner(tile, other_owner);
    00234           SetTileOwner(other_end, other_owner);
    00235         }
    00236
    00237         /* Mark tiles dirty that have been repaved */
    00238         MarkTileDirtyByTile(tile);
    00239         MarkTileDirtyByTile(other_end);
    00240         if (IsBridge(tile)) {
    00241           TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
    00242
    00243           for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
    00244         }
    00245       }
    00246     } else {
    00247       assert(IsDriveThroughStopTile(tile));
    00248       cost.AddCost(_price[PR_CLEAR_ROAD] * 2);
    00249       if (flags & DC_EXEC) {
    00250         SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
    00251         MarkTileDirtyByTile(tile);
    00252       }
    00253     }
    00254     return cost;
    00255   }
    00256
    00257   switch (GetRoadTileType(tile)) {
    00258     case ROAD_TILE_NORMAL: {
    00259       const Slope tileh = GetTileSlope(tile, NULL);
    00260       RoadBits present = GetRoadBits(tile, rt);
    00261       const RoadBits other = GetOtherRoadBits(tile, rt);
    00262       const Foundation f = GetRoadFoundation(tileh, present);
    00263
    00264       if (HasRoadWorks(tile) && _current_company != OWNER_WATER) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
    00265
    00266       /* Autocomplete to a straight road
    00267        * @li on steep slopes
    00268        * @li if the bits of the other roadtypes result in another foundation
    00269        * @li if build on slopes is disabled */
    00270       if (IsSteepSlope(tileh) || (IsStraightRoad(other) &&
    00271           (other & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) != ROAD_NONE) ||
    00272           (tileh != SLOPE_FLAT && !_settings_game.construction.build_on_slopes)) {
    00273         pieces |= MirrorRoadBits(pieces);
    00274       }
    00275
    00276       /* limit the bits to delete to the existing bits. */
    00277       pieces &= present;
    00278       if (pieces == ROAD_NONE) return_cmd_error(rt == ROADTYPE_TRAM ? STR_ERROR_THERE_IS_NO_TRAMWAY : STR_ERROR_THERE_IS_NO_ROAD);
    00279
    00280       /* Now set present what it will be after the remove */
    00281       present ^= pieces;
    00282
    00283       /* Check if traffic lights are present and if removing them would cause the tile to have less than three roadbits (of any kind!). */
    00284       if (trafficlights_check && HasTrafficLights(tile) && (CountBits(present | GetOtherRoadBits(tile, rt)) < 3)) {
    00285         return_cmd_error(STR_ERROR_MUST_REMOVE_TRAFFIC_LIGHTS_FIRST);
    00286       }
    00287
    00288       /* Check for invalid RoadBit combinations on slopes */
    00289       if (tileh != SLOPE_FLAT && present != ROAD_NONE &&
    00290           (present & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) == present) {
    00291         return CMD_ERROR;
    00292       }
    00293
    00294       if (flags & DC_EXEC) {
    00295         if (HasRoadWorks(tile)) {
    00296           /* flooding tile with road works, don't forget to remove the effect vehicle too */
    00297           assert(_current_company == OWNER_WATER);
    00298           EffectVehicle *v;
    00299           FOR_ALL_EFFECTVEHICLES(v) {
    00300             if (TileVirtXY(v->x_pos, v->y_pos) == tile) {
    00301               delete v;
    00302             }
    00303           }
    00304         }
    00305         if (present == ROAD_NONE) {
    00306           RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
    00307           if (rts == ROADTYPES_NONE) {
    00308             /* Includes MarkTileDirtyByTile() */
    00309             DoClearSquare(tile);
    00310           } else {
    00311             if (rt == ROADTYPE_ROAD && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) {
    00312               /* Update nearest-town index */
    00313               const Town *town = CalcClosestTownFromTile(tile);
    00314               SetTownIndex(tile, town == NULL ? (TownID)INVALID_TOWN : town->index);
    00315             }
    00316             SetRoadBits(tile, ROAD_NONE, rt);
    00317             SetRoadTypes(tile, rts);
    00318             MarkTileDirtyByTile(tile);
    00319           }
    00320         } else {
    00321           /* When bits are removed, you *always* end up with something that
    00322            * is not a complete straight road tile. However, trams do not have
    00323            * onewayness, so they cannot remove it either. */
    00324           if (rt != ROADTYPE_TRAM) SetDisallowedRoadDirections(tile, DRD_NONE);
    00325           SetRoadBits(tile, present, rt);
    00326           MarkTileDirtyByTile(tile);
    00327         }
    00328       }
    00329
    00330       CommandCost cost(EXPENSES_CONSTRUCTION, CountBits(pieces) * _price[PR_CLEAR_ROAD]);
    00331       /* If we build a foundation we have to pay for it. */
    00332       if (f == FOUNDATION_NONE && GetRoadFoundation(tileh, present) != FOUNDATION_NONE) cost.AddCost(_price[PR_BUILD_FOUNDATION]);
    00333
    00334       return cost;
    00335     }
    00336
    00337     case ROAD_TILE_CROSSING: {
    00338       if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) {
    00339         return CMD_ERROR;
    00340       }
    00341
    00342       /* Don't allow road to be removed from the crossing when there is tram;
    00343        * we can't draw the crossing without roadbits ;) */
    00344       if (rt == ROADTYPE_ROAD && HasTileRoadType(tile, ROADTYPE_TRAM) && (flags & DC_EXEC || crossing_check)) return CMD_ERROR;
    00345
    00346       if (flags & DC_EXEC) {
    00347         Track railtrack = GetCrossingRailTrack(tile);
    00348         RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
    00349         if (rts == ROADTYPES_NONE) {
    00350           TrackBits tracks = GetCrossingRailBits(tile);
    00351           bool reserved = HasCrossingReservation(tile);
    00352           MakeRailNormal(tile, GetTileOwner(tile), tracks, GetRailType(tile));
    00353           if (reserved) SetTrackReservation(tile, tracks);
    00354         } else {
    00355           SetRoadTypes(tile, rts);
    00356           /* If we ever get HWAY and it is possible without road then we will need to promote ownership and invalidate town index here, too */
    00357         }
    00358         MarkTileDirtyByTile(tile);
    00359         YapfNotifyTrackLayoutChange(tile, railtrack);
    00360       }
    00361       return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_ROAD] * 2);
    00362     }
    00363
    00364     default:
    00365     case ROAD_TILE_DEPOT:
    00366       return CMD_ERROR;
    00367   }
    00368 }
    00369
    00370
    00382 static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
    00383 {
    00384   /* Remove already build pieces */
    00385   CLRBITS(*pieces, existing);
    00386
    00387   /* If we can't build anything stop here */
    00388   if (*pieces == ROAD_NONE) return CMD_ERROR;
    00389
    00390   /* All RoadBit combos are valid on flat land */
    00391   if (tileh == SLOPE_FLAT) return CommandCost();
    00392
    00393   /* Proceed steep Slopes first to reduce lookup table size */
    00394   if (IsSteepSlope(tileh)) {
    00395     /* Force straight roads. */
    00396     *pieces |= MirrorRoadBits(*pieces);
    00397
    00398     /* Use existing as all existing since only straight
    00399      * roads are allowed here. */
    00400     existing |= other;
    00401
    00402     if ((existing == ROAD_NONE || existing == *pieces) && IsStraightRoad(*pieces)) {
    00403       return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00404     }
    00405     return CMD_ERROR;
    00406   }
    00407
    00408   /* Save the merge of all bits of the current type */
    00409   RoadBits type_bits = existing | *pieces;
    00410
    00411   /* Roads on slopes */
    00412   if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) {
    00413
    00414     /* If we add leveling we've got to pay for it */
    00415     if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00416
    00417     return CommandCost();
    00418   }
    00419
    00420   /* Autocomplete uphill roads */
    00421   *pieces |= MirrorRoadBits(*pieces);
    00422   type_bits = existing | *pieces;
    00423
    00424   /* Uphill roads */
    00425   if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) &&
    00426       (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) {
    00427
    00428     /* Slopes with foundation ? */
    00429     if (IsSlopeWithOneCornerRaised(tileh)) {
    00430
    00431       /* Prevent build on slopes if it isn't allowed */
    00432       if (_settings_game.construction.build_on_slopes) {
    00433
    00434         /* If we add foundation we've got to pay for it */
    00435         if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00436
    00437         return CommandCost();
    00438       }
    00439     } else {
    00440       if (HasExactlyOneBit(existing) && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00441       return CommandCost();
    00442     }
    00443   }
    00444   return CMD_ERROR;
    00445 }
    00446
    00458 CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00459 {
    00460   CommandCost cost(EXPENSES_CONSTRUCTION);
    00461
    00462   RoadBits existing = ROAD_NONE;
    00463   RoadBits other_bits = ROAD_NONE;
    00464
    00465   /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
    00466    * if a non-company is building the road */
    00467   if ((Company::IsValidID(_current_company) && p2 != 0) || (_current_company == OWNER_TOWN && !Town::IsValidID(p2))) return CMD_ERROR;
    00468   if (_current_company != OWNER_TOWN) {
    00469     const Town *town = CalcClosestTownFromTile(tile);
    00470     p2 = (town != NULL) ? town->index : (TownID)INVALID_TOWN;
    00471   }
    00472
    00473   RoadBits pieces = Extract<RoadBits, 0, 4>(p1);
    00474
    00475   /* do not allow building 'zero' road bits, code wouldn't handle it */
    00476   if (pieces == ROAD_NONE) return CMD_ERROR;
    00477
    00478   RoadType rt = Extract<RoadType, 4, 2>(p1);
    00479   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00480
    00481   DisallowedRoadDirections toggle_drd = Extract<DisallowedRoadDirections, 6, 2>(p1);
    00482
    00483   Slope tileh = GetTileSlope(tile, NULL);
    00484
    00485   bool need_to_clear = false;
    00486   switch (GetTileType(tile)) {
    00487     case MP_ROAD:
    00488       switch (GetRoadTileType(tile)) {
    00489         case ROAD_TILE_NORMAL: {
    00490           if (HasRoadWorks(tile)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
    00491
    00492           other_bits = GetOtherRoadBits(tile, rt);
    00493           if (!HasTileRoadType(tile, rt)) break;
    00494
    00495           existing = GetRoadBits(tile, rt);
    00496           bool crossing = !IsStraightRoad(existing | pieces);
    00497           if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
    00498             /* Junctions cannot be one-way */
    00499             return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
    00500           }
    00501           if ((existing & pieces) == pieces) {
    00502             /* We only want to set the (dis)allowed road directions */
    00503             if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM) {
    00504               if (crossing) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
    00505
    00506               Owner owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    00507               if (owner != OWNER_NONE) {
    00508                 CommandCost ret = CheckOwnership(owner, tile);
    00509                 if (ret.Failed()) return ret;
    00510               }
    00511
    00512               DisallowedRoadDirections dis_existing = GetDisallowedRoadDirections(tile);
    00513               DisallowedRoadDirections dis_new      = dis_existing ^ toggle_drd;
    00514
    00515               /* We allow removing disallowed directions to break up
    00516                * deadlocks, but adding them can break articulated
    00517                * vehicles. As such, only when less is disallowed,
    00518                * i.e. bits are removed, we skip the vehicle check. */
    00519               if (CountBits(dis_existing) <= CountBits(dis_new)) {
    00520                 CommandCost ret = EnsureNoVehicleOnGround(tile);
    00521                 if (ret.Failed()) return ret;
    00522               }
    00523
    00524               /* Ignore half built tiles */
    00525               if ((flags & DC_EXEC) && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
    00526                 SetDisallowedRoadDirections(tile, dis_new);
    00527                 MarkTileDirtyByTile(tile);
    00528               }
    00529               return CommandCost();
    00530             }
    00531             return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00532           }
    00533           break;
    00534         }
    00535
    00536         case ROAD_TILE_CROSSING:
    00537           other_bits = GetCrossingRoadBits(tile);
    00538           if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
    00539           pieces = other_bits; // we need to pay for both roadbits
    00540
    00541           if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00542           break;
    00543
    00544         case ROAD_TILE_DEPOT:
    00545           if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00546           goto do_clear;
    00547
    00548         default: NOT_REACHED();
    00549       }
    00550       break;
    00551
    00552     case MP_RAILWAY: {
    00553       if (IsSteepSlope(tileh)) {
    00554         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00555       }
    00556
    00557       /* Level crossings may only be built on these slopes */
    00558       if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
    00559         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00560       }
    00561
    00562       if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
    00563
    00564       if (RailNoLevelCrossings(GetRailType(tile))) {
    00565         return_cmd_error(STR_ERROR_CROSSING_DISALLOWED);
    00566       }
    00567
    00568       Axis roaddir;
    00569       switch (GetTrackBits(tile)) {
    00570         case TRACK_BIT_X:
    00571           if (pieces & ROAD_X) goto do_clear;
    00572           roaddir = AXIS_Y;
    00573           break;
    00574
    00575         case TRACK_BIT_Y:
    00576           if (pieces & ROAD_Y) goto do_clear;
    00577           roaddir = AXIS_X;
    00578           break;
    00579
    00580         default: goto do_clear;
    00581       }
    00582
    00583       CommandCost ret = EnsureNoVehicleOnGround(tile);
    00584       if (ret.Failed()) return ret;
    00585
    00586       if (flags & DC_EXEC) {
    00587         Track railtrack = AxisToTrack(OtherAxis(roaddir));
    00588         YapfNotifyTrackLayoutChange(tile, railtrack);
    00589         /* Always add road to the roadtypes (can't draw without it) */
    00590         bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack);
    00591         MakeRoadCrossing(tile, _current_company, _current_company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
    00592         SetCrossingReservation(tile, reserved);
    00593         UpdateLevelCrossing(tile, false);
    00594         MarkTileDirtyByTile(tile);
    00595       }
    00596       return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_ROAD] * (rt == ROADTYPE_ROAD ? 2 : 4));
    00597     }
    00598
    00599     case MP_STATION: {
    00600       if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00601       if (!IsDriveThroughStopTile(tile)) goto do_clear;
    00602
    00603       RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
    00604       if (pieces & ~curbits) goto do_clear;
    00605       pieces = curbits; // we need to pay for both roadbits
    00606
    00607       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00608       break;
    00609     }
    00610
    00611     case MP_TUNNELBRIDGE: {
    00612       if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) goto do_clear;
    00613       if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) goto do_clear;
    00614       if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00615       /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
    00616       CommandCost ret = TunnelBridgeIsFree(tile, GetOtherTunnelBridgeEnd(tile));
    00617       if (ret.Failed()) return ret;
    00618       break;
    00619     }
    00620
    00621     default: {
    00622 do_clear:;
    00623       need_to_clear = true;
    00624       break;
    00625     }
    00626   }
    00627
    00628   if (need_to_clear) {
    00629     CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00630     if (ret.Failed()) return ret;
    00631     cost.AddCost(ret);
    00632   }
    00633
    00634   if (other_bits != pieces) {
    00635     /* Check the foundation/slopes when adding road/tram bits */
    00636     CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
    00637     /* Return an error if we need to build a foundation (ret != 0) but the
    00638      * current setting is turned off */
    00639     if (ret.Failed() || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
    00640       return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00641     }
    00642     cost.AddCost(ret);
    00643   }
    00644
    00645   if (!need_to_clear) {
    00646     if (IsTileType(tile, MP_ROAD)) {
    00647       /* Don't put the pieces that already exist */
    00648       pieces &= ComplementRoadBits(existing);
    00649
    00650       /* Check if new road bits will have the same foundation as other existing road types */
    00651       if (IsNormalRoad(tile)) {
    00652         Slope slope = GetTileSlope(tile, NULL);
    00653         Foundation found_new = GetRoadFoundation(slope, pieces | existing);
    00654
    00655         /* Test if all other roadtypes can be built at that foundation */
    00656         for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
    00657           if (rtest != rt) { // check only other road types
    00658             RoadBits bits = GetRoadBits(tile, rtest);
    00659             /* do not check if there are not road bits of given type */
    00660             if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
    00661               return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00662             }
    00663           }
    00664         }
    00665       }
    00666     }
    00667
    00668     CommandCost ret = EnsureNoVehicleOnGround(tile);
    00669     if (ret.Failed()) return ret;
    00670
    00671   }
    00672   cost.AddCost(CountBits(pieces) * _price[PR_BUILD_ROAD]);
    00673
    00674   if (!need_to_clear && IsTileType(tile, MP_TUNNELBRIDGE)) {
    00675     /* Pay for *every* tile of the bridge or tunnel */
    00676     cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
    00677   }
    00678
    00679   if (flags & DC_EXEC) {
    00680     switch (GetTileType(tile)) {
    00681       case MP_ROAD: {
    00682         RoadTileType rtt = GetRoadTileType(tile);
    00683         if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
    00684           SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00685           SetRoadOwner(tile, rt, _current_company);
    00686           if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
    00687         }
    00688         if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
    00689         break;
    00690       }
    00691
    00692       case MP_TUNNELBRIDGE: {
    00693         TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
    00694
    00695         SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
    00696         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00697         SetRoadOwner(other_end, rt, _current_company);
    00698         SetRoadOwner(tile, rt, _current_company);
    00699
    00700         /* Mark tiles dirty that have been repaved */
    00701         MarkTileDirtyByTile(other_end);
    00702         MarkTileDirtyByTile(tile);
    00703         if (IsBridge(tile)) {
    00704           TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
    00705
    00706           for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
    00707         }
    00708         break;
    00709       }
    00710
    00711       case MP_STATION:
    00712         assert(IsDriveThroughStopTile(tile));
    00713         SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
    00714         SetRoadOwner(tile, rt, _current_company);
    00715         break;
    00716
    00717       default:
    00718         MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
    00719         break;
    00720     }
    00721
    00722     if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
    00723       existing |= pieces;
    00724       SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
    00725           GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
    00726     }
    00727
    00728     MarkTileDirtyByTile(tile);
    00729   }
    00730   return cost;
    00731 }
    00732
    00748 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00749 {
    00750   DisallowedRoadDirections drd = DRD_NORTHBOUND;
    00751
    00752   if (p1 >= MapSize()) return CMD_ERROR;
    00753
    00754   TileIndex end_tile = p1;
    00755   RoadType rt = Extract<RoadType, 3, 2>(p2);
    00756   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00757
    00758   Axis axis = Extract<Axis, 2, 1>(p2);
    00759   /* Only drag in X or Y direction dictated by the direction variable */
    00760   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00761   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00762
    00763   DiagDirection dir = AxisToDiagDir(axis);
    00764
    00765   /* Swap direction, also the half-tile drag var (bit 0 and 1) */
    00766   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00767     dir = ReverseDiagDir(dir);
    00768     p2 ^= 3;
    00769     drd = DRD_SOUTHBOUND;
    00770   }
    00771
    00772   /* On the X-axis, we have to swap the initial bits, so they
    00773    * will be interpreted correctly in the GTTS. Furthermore
    00774    * when you just 'click' on one tile to build them. */
    00775   if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
    00776   /* No disallowed direction bits have to be toggled */
    00777   if (!HasBit(p2, 5)) drd = DRD_NONE;
    00778
    00779   CommandCost cost(EXPENSES_CONSTRUCTION);
    00780   CommandCost last_error = CMD_ERROR;
    00781   TileIndex tile = start_tile;
    00782   bool had_bridge = false;
    00783   bool had_tunnel = false;
    00784   bool had_success = false;
    00785   /* Start tile is the first tile clicked by the user. */
    00786   for (;;) {
    00787     RoadBits bits = AxisToRoadBits(axis);
    00788
    00789     /* Road parts only have to be built at the start tile or at the end tile. */
    00790     if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
    00791     if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
    00792
    00793     CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
    00794     if (ret.Failed()) {
    00795       last_error = ret;
    00796       if (last_error.GetErrorMessage() != STR_ERROR_ALREADY_BUILT) {
    00797         if (HasBit(p2, 6)) return last_error;
    00798         break;
    00799       }
    00800     } else {
    00801       had_success = true;
    00802       /* Only pay for the upgrade on one side of the bridges and tunnels */
    00803       if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00804         if (IsBridge(tile)) {
    00805           if (!had_bridge || GetTunnelBridgeDirection(tile) == dir) {
    00806             cost.AddCost(ret);
    00807           }
    00808           had_bridge = true;
    00809         } else { // IsTunnel(tile)
    00810           if (!had_tunnel || GetTunnelBridgeDirection(tile) == dir) {
    00811             cost.AddCost(ret);
    00812           }
    00813           had_tunnel = true;
    00814         }
    00815       } else {
    00816         cost.AddCost(ret);
    00817       }
    00818     }
    00819
    00820     if (tile == end_tile) break;
    00821
    00822     tile += TileOffsByDiagDir(dir);
    00823   }
    00824
    00825   return had_success ? cost : last_error;
    00826 }
    00827
    00841 CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00842 {
    00843   CommandCost cost(EXPENSES_CONSTRUCTION);
    00844
    00845   if (p1 >= MapSize()) return CMD_ERROR;
    00846
    00847   TileIndex end_tile = p1;
    00848   RoadType rt = Extract<RoadType, 3, 2>(p2);
    00849   if (!IsValidRoadType(rt)) return CMD_ERROR;
    00850
    00851   Axis axis = Extract<Axis, 2, 1>(p2);
    00852   /* Only drag in X or Y direction dictated by the direction variable */
    00853   if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
    00854   if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
    00855
    00856   /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
    00857   if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
    00858     TileIndex t = start_tile;
    00859     start_tile = end_tile;
    00860     end_tile = t;
    00861     p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
    00862   }
    00863
    00864   Money money = GetAvailableMoneyForCommand();
    00865   TileIndex tile = start_tile;
    00866   CommandCost last_error = CMD_ERROR;
    00867   bool had_success = false;
    00868   /* Start tile is the small number. */
    00869   for (;;) {
    00870     RoadBits bits = AxisToRoadBits(axis);
    00871
    00872     if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
    00873     if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
    00874
    00875     /* try to remove the halves. */
    00876     if (bits != 0) {
    00877       CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true, true);
    00878       if (ret.Succeeded()) {
    00879         if (flags & DC_EXEC) {
    00880           money -= ret.GetCost();
    00881           if (money < 0) {
    00882             _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
    00883             return cost;
    00884           }
    00885           RemoveRoad(tile, flags, bits, rt, true, true, false);
    00886         }
    00887         cost.AddCost(ret);
    00888         had_success = true;
    00889       } else {
    00890         /* Ownership errors are more important. */
    00891         if (last_error.GetErrorMessage() != STR_ERROR_OWNED_BY) last_error = ret;
    00892       }
    00893     }
    00894
    00895     if (tile == end_tile) break;
    00896
    00897     tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
    00898   }
    00899
    00900   return had_success ? cost : last_error;
    00901 }
    00902
    00916 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00917 {
    00918   DiagDirection dir = Extract<DiagDirection, 0, 2>(p1);
    00919   RoadType rt = Extract<RoadType, 2, 2>(p1);
    00920
    00921   if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
    00922
    00923   Slope tileh = GetTileSlope(tile, NULL);
    00924   if (tileh != SLOPE_FLAT && (
    00925         !_settings_game.construction.build_on_slopes ||
    00926         IsSteepSlope(tileh) ||
    00927         !CanBuildDepotByTileh(dir, tileh)
    00928       )) {
    00929     return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
    00930   }
    00931
    00932   CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00933   if (cost.Failed()) return cost;
    00934
    00935   if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00936
    00937   if (!Depot::CanAllocateItem()) return CMD_ERROR;
    00938
    00939   if (flags & DC_EXEC) {
    00940     Depot *dep = new Depot(tile);
    00941     dep->build_date = _date;
    00942
    00943     MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
    00944     MarkTileDirtyByTile(tile);
    00945     MakeDefaultName(dep);
    00946   }
    00947   cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
    00948   return cost;
    00949 }
    00950
    00951 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
    00952 {
    00953   if (_current_company != OWNER_WATER) {
    00954     CommandCost ret = CheckTileOwnership(tile);
    00955     if (ret.Failed()) return ret;
    00956   }
    00957
    00958   CommandCost ret = EnsureNoVehicleOnGround(tile);
    00959   if (ret.Failed()) return ret;
    00960
    00961   if (flags & DC_EXEC) {
    00962     delete Depot::GetByTile(tile);
    00963     DoClearSquare(tile);
    00964   }
    00965
    00966   return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
    00967 }
    00968
    00969 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
    00970 {
    00971   switch (GetRoadTileType(tile)) {
    00972     case ROAD_TILE_NORMAL: {
    00973       RoadBits b = GetAllRoadBits(tile);
    00974
    00975       /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
    00976       if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
    00977         CommandCost ret(EXPENSES_CONSTRUCTION);
    00978
    00979         /* Remove traffic light if necessary. */
    00980         if (HasTrafficLights(tile)) {
    00981           CommandCost tl_ret = CmdRemoveTrafficLights(tile, flags, 0, 0, 0);
    00982           if (tl_ret.Failed()) return tl_ret;
    00983           ret.AddCost(tl_ret);
    00984         }
    00985
    00986         /* Remove road bits. */
    00987         RoadType rt;
    00988         FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
    00989           CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true, false);
    00990           if (tmp_ret.Failed()) return tmp_ret;
    00991           ret.AddCost(tmp_ret);
    00992         }
    00993         return ret;
    00994       }
    00995       return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
    00996     }
    00997
    00998     case ROAD_TILE_CROSSING: {
    00999       RoadTypes rts = GetRoadTypes(tile);
    01000       CommandCost ret(EXPENSES_CONSTRUCTION);
    01001
    01002       if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
    01003
    01004       /* Must iterate over the roadtypes in a reverse manner because
    01005        * tram tracks must be removed before the road bits. */
    01006       RoadType rt = ROADTYPE_TRAM;
    01007       do {
    01008         if (HasBit(rts, rt)) {
    01009           CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false, false);
    01010           if (tmp_ret.Failed()) return tmp_ret;
    01011           ret.AddCost(tmp_ret);
    01012         }
    01013       } while (rt-- != ROADTYPE_ROAD);
    01014
    01015       if (flags & DC_EXEC) {
    01016         DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    01017       }
    01018       return ret;
    01019     }
    01020
    01021     default:
    01022     case ROAD_TILE_DEPOT:
    01023       if (flags & DC_AUTO) {
    01024         return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
    01025       }
    01026       return RemoveRoadDepot(tile, flags);
    01027   }
    01028 }
    01029
    01030
    01031 struct DrawRoadTileStruct {
    01032   uint16 image;
    01033   byte subcoord_x;
    01034   byte subcoord_y;
    01035 };
    01036
    01037 #include "table/road_land.h"
    01038
    01046 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
    01047 {
    01048   /* Flat land and land without a road doesn't require a foundation */
    01049   if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
    01050
    01051   if (!IsSteepSlope(tileh)) {
    01052     /* Leveled RoadBits on a slope */
    01053     if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
    01054
    01055     /* Straight roads without foundation on a slope */
    01056     if (!IsSlopeWithOneCornerRaised(tileh) &&
    01057         (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
    01058       return FOUNDATION_NONE;
    01059   }
    01060
    01061   /* Roads on steep Slopes or on Slopes with one corner raised */
    01062   return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
    01063 }
    01064
    01065 const byte _road_sloped_sprites[14] = {
    01066   0,  0,  2,  0,
    01067   0,  1,  0,  0,
    01068   3,  0,  0,  0,
    01069   0,  0
    01070 };
    01071
    01082 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
    01083 {
    01084   return (IsOnSnow(tile) &&
    01085       !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
    01086         roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
    01087 }
    01088
    01094 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
    01095 {
    01096   /* Do not draw catenary if it is invisible */
    01097   if (IsInvisibilitySet(TO_CATENARY)) return;
    01098
    01099   /* Don't draw the catenary under a low bridge */
    01100   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
    01101     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01102
    01103     if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
    01104   }
    01105
    01106   SpriteID front;
    01107   SpriteID back;
    01108
    01109   if (ti->tileh != SLOPE_FLAT) {
    01110     back  = SPR_TRAMWAY_BACK_WIRES_SLOPED  + _road_sloped_sprites[ti->tileh - 1];
    01111     front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
    01112   } else {
    01113     back  = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
    01114     front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
    01115   }
    01116
    01117   AddSortableSpriteToDraw(back,  PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01118   AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
    01119 }
    01120
    01129 void DrawRoadDetail(SpriteID img, TileInfo *ti, int dx, int dy, int h)
    01130 {
    01131   int x = ti->x | dx;
    01132   int y = ti->y | dy;
    01133   int32 z = ti->z;
    01134   if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
    01135   AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
    01136 }
    01137
    01142 static void DrawRoadBits(TileInfo *ti)
    01143 {
    01144   RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
    01145   RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
    01146
    01147   SpriteID image = 0;
    01148   PaletteID pal = PAL_NONE;
    01149
    01150   if (ti->tileh != SLOPE_FLAT) {
    01151     DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
    01152
    01153     /* DrawFoundation() modifies ti.
    01154      * Default sloped sprites.. */
    01155     if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
    01156   }
    01157
    01158   if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
    01159
    01160   Roadside roadside = GetRoadside(ti->tile);
    01161
    01162   if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01163     image += 19;
    01164   } else {
    01165     switch (roadside) {
    01166       case ROADSIDE_BARREN:           pal = PALETTE_TO_BARE_LAND; break;
    01167       case ROADSIDE_GRASS:            break;
    01168       case ROADSIDE_GRASS_ROAD_WORKS: break;
    01169       default:                        image -= 19; break; // Paved
    01170     }
    01171   }
    01172
    01173   DrawGroundSprite(image, pal);
    01174
    01175   /* For tram we overlay the road graphics with either tram tracks only
    01176    * (when there is actual road beneath the trams) or with tram tracks
    01177    * and some dirts which hides the road graphics */
    01178   if (tram != ROAD_NONE) {
    01179     if (ti->tileh != SLOPE_FLAT) {
    01180       image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
    01181     } else {
    01182       image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
    01183     }
    01184     image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
    01185     DrawGroundSprite(image, pal);
    01186   }
    01187
    01188   if (road != ROAD_NONE) {
    01189     DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
    01190     if (drd != DRD_NONE) {
    01191       DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialZ(8, 8, ti->tileh));
    01192     }
    01193   }
    01194
    01195   if (_settings_game.construction.traffic_lights && HasTrafficLights(ti->tile) && _cur_dpi->zoom <= ZOOM_LVL_DETAIL) DrawTrafficLights(ti);
    01196
    01197   if (HasRoadWorks(ti->tile)) {
    01198     /* Road works */
    01199     DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
    01200     return;
    01201   }
    01202
    01203   if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
    01204
    01205   /* Return if full detail is disabled, or we are zoomed fully out. */
    01206   if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
    01207
    01208   /* Do not draw details (street lights, trees) under low bridge */
    01209   if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
    01210     uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
    01211     uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
    01212
    01213     if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
    01214
    01215     if (height < minz) return;
    01216   }
    01217
    01218   /* If there are no road bits, return, as there is nothing left to do */
    01219   if (HasAtMostOneBit(road)) return;
    01220
    01221   /* Draw extra details. */
    01222   for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
    01223     DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
    01224   }
    01225 }
    01226
    01228 static void DrawTile_Road(TileInfo *ti)
    01229 {
    01230   switch (GetRoadTileType(ti->tile)) {
    01231     case ROAD_TILE_NORMAL:
    01232       DrawRoadBits(ti);
    01233       break;
    01234
    01235     case ROAD_TILE_CROSSING: {
    01236       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    01237
    01238       PaletteID pal = PAL_NONE;
    01239       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
    01240
    01241       if (rti->UsesOverlay()) {
    01242         Axis axis = GetCrossingRailAxis(ti->tile);
    01243         SpriteID road = SPR_ROAD_Y + axis;
    01244
    01245         Roadside roadside = GetRoadside(ti->tile);
    01246
    01247         if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01248           road += 19;
    01249         } else {
    01250           switch (roadside) {
    01251             case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
    01252             case ROADSIDE_GRASS:  break;
    01253             default:              road -= 19; break; // Paved
    01254           }
    01255         }
    01256
    01257         DrawGroundSprite(road, pal);
    01258
    01259         SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
    01260         /* Draw tracks, but draw PBS reserved tracks darker. */
    01261         pal = (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) ? PALETTE_CRASH : PAL_NONE;
    01262         DrawGroundSprite(rail, pal);
    01263
    01264         DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
    01265       } else {
    01266         SpriteID image = rti->base_sprites.crossing;
    01267
    01268         if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
    01269         if (IsCrossingBarred(ti->tile)) image += 2;
    01270
    01271         Roadside roadside = GetRoadside(ti->tile);
    01272
    01273         if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
    01274           image += 8;
    01275         } else {
    01276           switch (roadside) {
    01277             case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
    01278             case ROADSIDE_GRASS:  break;
    01279             default:              image += 4; break; // Paved
    01280           }
    01281         }
    01282
    01283         DrawGroundSprite(image, pal);
    01284
    01285         /* PBS debugging, draw reserved tracks darker */
    01286         if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
    01287           DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y, PALETTE_CRASH);
    01288         }
    01289       }
    01290
    01291       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
    01292         DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
    01293         DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
    01294       }
    01295       if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
    01296       break;
    01297     }
    01298
    01299     default:
    01300     case ROAD_TILE_DEPOT: {
    01301       if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    01302
    01303       PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
    01304
    01305       const DrawTileSprites *dts;
    01306       if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
    01307         dts =  &_tram_depot[GetRoadDepotDirection(ti->tile)];
    01308       } else {
    01309         dts =  &_road_depot[GetRoadDepotDirection(ti->tile)];
    01310       }
    01311
    01312       DrawGroundSprite(dts->ground.sprite, PAL_NONE);
    01313       DrawOrigTileSeq(ti, dts, TO_BUILDINGS, palette);
    01314       break;
    01315     }
    01316   }
    01317   DrawBridgeMiddle(ti);
    01318 }
    01319
    01327 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
    01328 {
    01329   PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
    01330   const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
    01331
    01332   x += 33;
    01333   y += 17;
    01334
    01335   DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
    01336   DrawOrigTileSeqInGUI(x, y, dts, palette);
    01337 }
    01338
    01344 void UpdateNearestTownForRoadTiles(bool invalidate)
    01345 {
    01346   assert(!invalidate || _generating_world);
    01347
    01348   for (TileIndex t = 0; t < MapSize(); t++) {
    01349     if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) {
    01350       TownID tid = (TownID)INVALID_TOWN;
    01351       if (!invalidate) {
    01352         const Town *town = CalcClosestTownFromTile(t);
    01353         if (town != NULL) tid = town->index;
    01354       }
    01355       SetTownIndex(t, tid);
    01356     }
    01357   }
    01358 }
    01359
    01360 static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
    01361 {
    01362   uint z;
    01363   Slope tileh = GetTileSlope(tile, &z);
    01364
    01365   if (tileh == SLOPE_FLAT) return z;
    01366   if (IsNormalRoad(tile)) {
    01367     Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
    01368     z += ApplyFoundationToSlope(f, &tileh);
    01369     return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
    01370   } else {
    01371     return z + TILE_HEIGHT;
    01372   }
    01373 }
    01374
    01375 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
    01376 {
    01377   if (IsNormalRoad(tile)) {
    01378     return GetRoadFoundation(tileh, GetAllRoadBits(tile));
    01379   } else {
    01380     return FlatteningFoundation(tileh);
    01381   }
    01382 }
    01383
    01390 static void AnimateTile_Road(TileIndex tile)
    01391 {
    01392   if (_settings_game.construction.traffic_lights && HasTrafficLights(tile)) {
    01393     if (_tick_counter % 16 == 0) MarkTileDirtyByTile(tile);
    01394   }
    01395 }
    01396
    01397 static const Roadside _town_road_types[][2] = {
    01398   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
    01399   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01400   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01401   { ROADSIDE_TREES,         ROADSIDE_TREES },
    01402   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
    01403 };
    01404
    01405 static const Roadside _town_road_types_2[][2] = {
    01406   { ROADSIDE_GRASS,         ROADSIDE_GRASS },
    01407   { ROADSIDE_PAVED,         ROADSIDE_PAVED },
    01408   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
    01409   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
    01410   { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
    01411 };
    01412
    01413
    01414 static void TileLoop_Road(TileIndex tile)
    01415 {
    01416   switch (_settings_game.game_creation.landscape) {
    01417     case LT_ARCTIC:
    01418       if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
    01419         ToggleSnow(tile);
    01420         MarkTileDirtyByTile(tile);
    01421       }
    01422       break;
    01423
    01424     case LT_TROPIC:
    01425       if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
    01426         ToggleDesert(tile);
    01427         MarkTileDirtyByTile(tile);
    01428       }
    01429       break;
    01430   }
    01431
    01432   if (IsRoadDepot(tile)) return;
    01433
    01434   const Town *t = ClosestTownFromTile(tile, UINT_MAX);
    01435   if (!HasRoadWorks(tile)) {
    01436     HouseZonesBits grp = HZB_TOWN_EDGE;
    01437
    01438     if (t != NULL) {
    01439       grp = GetTownRadiusGroup(t, tile);
    01440
    01441       /* Show an animation to indicate road work */
    01442       if (t->road_build_months != 0 &&
    01443           (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
    01444           IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
    01445         if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile).Succeeded() && Chance16(1, 40)) {
    01446           StartRoadWorks(tile);
    01447
    01448           SndPlayTileFx(SND_21_JACKHAMMER, tile);
    01449           CreateEffectVehicleAbove(
    01450             TileX(tile) * TILE_SIZE + 7,
    01451             TileY(tile) * TILE_SIZE + 7,
    01452             0,
    01453             EV_BULLDOZER);
    01454           MarkTileDirtyByTile(tile);
    01455           return;
    01456         }
    01457       }
    01458     }
    01459
    01460     {
    01461       /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
    01462       const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
    01463       Roadside cur_rs = GetRoadside(tile);
    01464
    01465       /* We have our desired type, do nothing */
    01466       if (cur_rs == new_rs[0]) return;
    01467
    01468       /* We have the pre-type of the desired type, switch to the desired type */
    01469       if (cur_rs == new_rs[1]) {
    01470         cur_rs = new_rs[0];
    01471       /* We have barren land, install the pre-type */
    01472       } else if (cur_rs == ROADSIDE_BARREN) {
    01473         cur_rs = new_rs[1];
    01474       /* We're totally off limits, remove any installation and make barren land */
    01475       } else {
    01476         cur_rs = ROADSIDE_BARREN;
    01477       }
    01478       SetRoadside(tile, cur_rs);
    01479       MarkTileDirtyByTile(tile);
    01480     }
    01481   } else {
    01482     /* In the first half of roadworks, generate traffic lights with a certain chance. */
    01483     if (_settings_game.construction.traffic_lights && _settings_game.construction.towns_build_traffic_lights &&
    01484         (GetRoadWorksCounter(tile) < 8) && (CountBits(GetRoadBits(tile, ROADTYPE_ROAD)) >= 3) &&
    01485         !HasTrafficLights(tile) && Chance16(1, 20)) {
    01486       CmdBuildTrafficLights(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, 0, 0, 0);
    01487       MarkTileDirtyByTile(tile);
    01488     }
    01489     if (IncreaseRoadWorksCounter(tile)) {
    01490       TerminateRoadWorks(tile);
    01491
    01492       if (_settings_game.economy.mod_road_rebuild) {
    01493         /* Generate a nicer town surface. */
    01494         const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
    01495         const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
    01496
    01497         if (old_rb != new_rb) {
    01498           RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true, true);
    01499         }
    01500       }
    01501
    01502       MarkTileDirtyByTile(tile);
    01503     }
    01504   }
    01505 }
    01506
    01507 static bool ClickTile_Road(TileIndex tile)
    01508 {
    01509   if (!IsRoadDepot(tile)) return false;
    01510
    01511   ShowDepotWindow(tile, VEH_ROAD);
    01512   return true;
    01513 }
    01514
    01515 /* Converts RoadBits to TrackBits */
    01516 static const TrackBits _road_trackbits[16] = {
    01517   TRACK_BIT_NONE,                                  // ROAD_NONE
    01518   TRACK_BIT_NONE,                                  // ROAD_NW
    01519   TRACK_BIT_NONE,                                  // ROAD_SW
    01520   TRACK_BIT_LEFT,                                  // ROAD_W
    01521   TRACK_BIT_NONE,                                  // ROAD_SE
    01522   TRACK_BIT_Y,                                     // ROAD_Y
    01523   TRACK_BIT_LOWER,                                 // ROAD_S
    01524   TRACK_BIT_LEFT | TRACK_BIT_LOWER | TRACK_BIT_Y,  // ROAD_Y | ROAD_SW
    01525   TRACK_BIT_NONE,                                  // ROAD_NE
    01526   TRACK_BIT_UPPER,                                 // ROAD_N
    01527   TRACK_BIT_X,                                     // ROAD_X
    01528   TRACK_BIT_LEFT | TRACK_BIT_UPPER | TRACK_BIT_X,  // ROAD_X | ROAD_NW
    01529   TRACK_BIT_RIGHT,                                 // ROAD_E
    01530   TRACK_BIT_RIGHT | TRACK_BIT_UPPER | TRACK_BIT_Y, // ROAD_Y | ROAD_NE
    01531   TRACK_BIT_RIGHT | TRACK_BIT_LOWER | TRACK_BIT_X, // ROAD_X | ROAD_SE
    01532   TRACK_BIT_ALL,                                   // ROAD_ALL
    01533 };
    01534
    01535 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    01536 {
    01537   TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
    01538   TrackdirBits red_signals = TRACKDIR_BIT_NONE; // Crossing barred or red traffic light.
    01539   switch (mode) {
    01540     case TRANSPORT_RAIL:
    01541       if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
    01542       break;
    01543
    01544     case TRANSPORT_ROAD:
    01545       if ((GetRoadTypes(tile) & sub_mode) == 0) break;
    01546       switch (GetRoadTileType(tile)) {
    01547         case ROAD_TILE_NORMAL: {
    01548           const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
    01549           RoadType rt = (RoadType)FindFirstBit(sub_mode);
    01550           RoadBits bits = GetRoadBits(tile, rt);
    01551
    01552           /* no roadbit at this side of tile, return 0 */
    01553           if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
    01554
    01555           uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
    01556           if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
    01557           if (_settings_game.construction.traffic_lights && HasTrafficLights(tile))
    01558               red_signals = trackdirbits & GetTrafficLightDisallowedDirections(tile);
    01559           break;
    01560         }
    01561
    01562         case ROAD_TILE_CROSSING: {
    01563           Axis axis = GetCrossingRoadAxis(tile);
    01564
    01565           if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
    01566
    01567           trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
    01568           if (IsCrossingBarred(tile)) red_signals = trackdirbits;
    01569           break;
    01570         }
    01571
    01572         default:
    01573         case ROAD_TILE_DEPOT: {
    01574           DiagDirection dir = GetRoadDepotDirection(tile);
    01575
    01576           if (side != INVALID_DIAGDIR && side != dir) break;
    01577
    01578           trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
    01579           break;
    01580         }
    01581       }
    01582       break;
    01583
    01584     default: break;
    01585   }
    01586   return CombineTrackStatus(trackdirbits, red_signals);
    01587 }
    01588
    01589 static const StringID _road_tile_strings[] = {
    01590   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01591   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01592   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01593   STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS,
    01594   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01595   STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD,
    01596   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01597   STR_LAI_ROAD_DESCRIPTION_ROAD,
    01598 };
    01599
    01600 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
    01601 {
    01602   Owner rail_owner = INVALID_OWNER;
    01603   Owner road_owner = INVALID_OWNER;
    01604   Owner tram_owner = INVALID_OWNER;
    01605
    01606   switch (GetRoadTileType(tile)) {
    01607     case ROAD_TILE_CROSSING: {
    01608       td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING;
    01609       RoadTypes rts = GetRoadTypes(tile);
    01610       rail_owner = GetTileOwner(tile);
    01611       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01612       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01613
    01614       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
    01615       td->rail_speed = rti->max_speed;
    01616
    01617       break;
    01618     }
    01619
    01620     case ROAD_TILE_DEPOT:
    01621       td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
    01622       road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter
    01623       td->build_date = Depot::GetByTile(tile)->build_date;
    01624       break;
    01625
    01626     default: {
    01627       RoadTypes rts = GetRoadTypes(tile);
    01628       if (HasTrafficLights(tile)) {
    01629         td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_TRAFFIC_LIGHTS;
    01630       } else {
    01631         td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
    01632       }
    01633       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01634       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01635       break;
    01636     }
    01637   }
    01638
    01639   /* Now we have to discover, if the tile has only one owner or many:
    01640    *   - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
    01641    *   - Compare the found owner with the other owners, and test if they differ.
    01642    * Note: If road exists it will be the first_owner.
    01643    */
    01644   Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
    01645   bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
    01646
    01647   if (mixed_owners) {
    01648     /* Multiple owners */
    01649     td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
    01650     td->owner[0] = rail_owner;
    01651     td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
    01652     td->owner[1] = road_owner;
    01653     td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
    01654     td->owner[2] = tram_owner;
    01655   } else {
    01656     /* One to rule them all */
    01657     td->owner[0] = first_owner;
    01658   }
    01659 }
    01660
    01665 static const byte _roadveh_enter_depot_dir[4] = {
    01666   TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
    01667 };
    01668
    01669 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
    01670 {
    01671   switch (GetRoadTileType(tile)) {
    01672     case ROAD_TILE_DEPOT: {
    01673       if (v->type != VEH_ROAD) break;
    01674
    01675       RoadVehicle *rv = RoadVehicle::From(v);
    01676       if (rv->frame == RVC_DEPOT_STOP_FRAME &&
    01677           _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
    01678         rv->state = RVSB_IN_DEPOT;
    01679         rv->vehstatus |= VS_HIDDEN;
    01680         rv->direction = ReverseDir(rv->direction);
    01681         if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
    01682         rv->tile = tile;
    01683
    01684         InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
    01685         return VETSB_ENTERED_WORMHOLE;
    01686       }
    01687       break;
    01688     }
    01689
    01690     default: break;
    01691   }
    01692   return VETSB_CONTINUE;
    01693 }
    01694
    01695
    01696 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
    01697 {
    01698   if (IsRoadDepot(tile)) {
    01699     if (GetTileOwner(tile) == old_owner) {
    01700       if (new_owner == INVALID_OWNER) {
    01701         DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
    01702       } else {
    01703         SetTileOwner(tile, new_owner);
    01704       }
    01705     }
    01706     return;
    01707   }
    01708
    01709   for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    01710     /* Update all roadtypes, no matter if they are present */
    01711     if (GetRoadOwner(tile, rt) == old_owner) {
    01712       SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
    01713     }
    01714   }
    01715
    01716   if (IsLevelCrossing(tile)) {
    01717     if (GetTileOwner(tile) == old_owner) {
    01718       if (new_owner == INVALID_OWNER) {
    01719         DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
    01720       } else {
    01721         SetTileOwner(tile, new_owner);
    01722       }
    01723     }
    01724   }
    01725 }
    01726
    01727 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    01728 {
    01729   if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
    01730     switch (GetRoadTileType(tile)) {
    01731       case ROAD_TILE_CROSSING:
    01732         if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new)) && HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01733         break;
    01734
    01735       case ROAD_TILE_DEPOT:
    01736         if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01737         break;
    01738
    01739       case ROAD_TILE_NORMAL: {
    01740         RoadBits bits = GetAllRoadBits(tile);
    01741         RoadBits bits_copy = bits;
    01742         /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
    01743         if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
    01744           /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
    01745           if (bits == bits_copy) {
    01746             uint z_old;
    01747             Slope tileh_old = GetTileSlope(tile, &z_old);
    01748
    01749             /* Get the slope on top of the foundation */
    01750             z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
    01751             z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
    01752
    01753             /* The surface slope must not be changed */
    01754             if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01755           }
    01756         }
    01757         break;
    01758       }
    01759
    01760       default: NOT_REACHED();
    01761     }
    01762   }
    01763
    01764   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    01765 }
    01766
    01768 extern const TileTypeProcs _tile_type_road_procs = {
    01769   DrawTile_Road,           // draw_tile_proc
    01770   GetSlopeZ_Road,          // get_slope_z_proc
    01771   ClearTile_Road,          // clear_tile_proc
    01772   NULL,                    // add_accepted_cargo_proc
    01773   GetTileDesc_Road,        // get_tile_desc_proc
    01774   GetTileTrackStatus_Road, // get_tile_track_status_proc
    01775   ClickTile_Road,          // click_tile_proc
    01776   AnimateTile_Road,        // animate_tile_proc
    01777   TileLoop_Road,           // tile_loop_clear
    01778   ChangeTileOwner_Road,    // change_tile_owner_clear
    01779   NULL,                    // add_produced_cargo_proc
    01780   VehicleEnter_Road,       // vehicle_enter_tile_proc
    01781   GetFoundation_Road,      // get_foundation_proc
    01782   TerraformTile_Road,      // terraform_tile_proc
    01783 };