road_cmd.cpp

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

Generated on Thu Jan 20 22:57:39 2011 for OpenTTD by  doxygen 1.6.1