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