# tunnelbridge_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
    00015 #include "stdafx.h"
    00016 #include "openttd.h"
    00017 #include "rail_map.h"
    00018 #include "landscape.h"
    00019 #include "unmovable_map.h"
    00020 #include "viewport_func.h"
    00021 #include "command_func.h"
    00022 #include "town.h"
    00023 #include "variables.h"
    00024 #include "train.h"
    00025 #include "ship.h"
    00026 #include "roadveh.h"
    00027 #include "water_map.h"
    00028 #include "pathfinder/yapf/yapf_cache.h"
    00029 #include "newgrf_sound.h"
    00030 #include "autoslope.h"
    00031 #include "tunnelbridge_map.h"
    00032 #include "strings_func.h"
    00033 #include "date_func.h"
    00034 #include "functions.h"
    00035 #include "vehicle_func.h"
    00036 #include "sound_func.h"
    00037 #include "tunnelbridge.h"
    00038 #include "cheat_type.h"
    00039 #include "elrail_func.h"
    00040 #include "landscape_type.h"
    00041 #include "pbs.h"
    00042
    00043 #include "table/sprites.h"
    00044 #include "table/strings.h"
    00045 #include "table/bridge_land.h"
    00046
    00047 BridgeSpec _bridge[MAX_BRIDGES];
    00048 TileIndex _build_tunnel_endtile;
    00049
    00051 void ResetBridges()
    00052 {
    00053   /* First, free sprite table data */
    00054   for (BridgeType i = 0; i < MAX_BRIDGES; i++) {
    00055     if (_bridge[i].sprite_table != NULL) {
    00056       for (BridgePieces j = BRIDGE_PIECE_NORTH; j < BRIDGE_PIECE_INVALID; j++) free(_bridge[i].sprite_table[j]);
    00057       free(_bridge[i].sprite_table);
    00058     }
    00059   }
    00060
    00061   /* Then, wipe out current bidges */
    00062   memset(&_bridge, 0, sizeof(_bridge));
    00063   /* And finally, reinstall default data */
    00064   memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge));
    00065 }
    00066
    00070 int CalcBridgeLenCostFactor(int x)
    00071 {
    00072   int n;
    00073   int r;
    00074
    00075   if (x < 2) return x;
    00076   x -= 2;
    00077   for (n = 0, r = 2;; n++) {
    00078     if (x <= n) return r + x * n;
    00079     r += n * n;
    00080     x -= n;
    00081   }
    00082 }
    00083
    00084 Foundation GetBridgeFoundation(Slope tileh, Axis axis)
    00085 {
    00086   if ((tileh == SLOPE_FLAT) ||
    00087       (((tileh == SLOPE_NE) || (tileh == SLOPE_SW)) && (axis == AXIS_X)) ||
    00088       (((tileh == SLOPE_NW) || (tileh == SLOPE_SE)) && (axis == AXIS_Y))) return FOUNDATION_NONE;
    00089
    00090   return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh));
    00091 }
    00092
    00100 bool HasBridgeFlatRamp(Slope tileh, Axis axis)
    00101 {
    00102   ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh);
    00103   /* If the foundation slope is flat the bridge has a non-flat ramp and vice versa. */
    00104   return (tileh != SLOPE_FLAT);
    00105 }
    00106
    00107 static inline const PalSpriteID *GetBridgeSpriteTable(int index, BridgePieces table)
    00108 {
    00109   const BridgeSpec *bridge = GetBridgeSpec(index);
    00110   assert(table < BRIDGE_PIECE_INVALID);
    00111   if (bridge->sprite_table == NULL || bridge->sprite_table[table] == NULL) {
    00112     return _bridge_sprite_table[index][table];
    00113   } else {
    00114     return bridge->sprite_table[table];
    00115   }
    00116 }
    00117
    00118
    00127 static CommandCost CheckBridgeSlopeNorth(Axis axis, Slope *tileh, uint *z)
    00128 {
    00129   Foundation f = GetBridgeFoundation(*tileh, axis);
    00130   *z += ApplyFoundationToSlope(f, tileh);
    00131
    00132   Slope valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW);
    00133   if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
    00134
    00135   if (f == FOUNDATION_NONE) return CommandCost();
    00136
    00137   return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00138 }
    00139
    00148 static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, uint *z)
    00149 {
    00150   Foundation f = GetBridgeFoundation(*tileh, axis);
    00151   *z += ApplyFoundationToSlope(f, tileh);
    00152
    00153   Slope valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE);
    00154   if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
    00155
    00156   if (f == FOUNDATION_NONE) return CommandCost();
    00157
    00158   return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00159 }
    00160
    00161 bool CheckBridge_Stuff(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
    00162 {
    00163   if (flags & DC_QUERY_COST) {
    00164     return bridge_len <= (_settings_game.construction.longbridges ? 100U : 16U);
    00165   }
    00166
    00167   if (bridge_type >= MAX_BRIDGES) return false;
    00168
    00169   const BridgeSpec *b = GetBridgeSpec(bridge_type);
    00170   if (b->avail_year > _cur_year) return false;
    00171
    00172   uint max = b->max_length;
    00173   if (max >= 16 && _settings_game.construction.longbridges) max = 100;
    00174
    00175   return b->min_length <= bridge_len && bridge_len <= max;
    00176 }
    00177
    00189 CommandCost CmdBuildBridge(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00190 {
    00191   RailType railtype = INVALID_RAILTYPE;
    00192   RoadTypes roadtypes = ROADTYPES_NONE;
    00193   CommandCost cost(EXPENSES_CONSTRUCTION);
    00194   Owner owner;
    00195
    00196   /* unpack parameters */
    00197   BridgeType bridge_type = GB(p2, 0, 8);
    00198
    00199   if (p1 >= MapSize()) return CMD_ERROR;
    00200
    00201   TransportType transport_type = (TransportType)GB(p2, 15, 2);
    00202
    00203   /* type of bridge */
    00204   switch (transport_type) {
    00205     case TRANSPORT_ROAD:
    00206       roadtypes = (RoadTypes)GB(p2, 8, 2);
    00207       if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_company, roadtypes)) return CMD_ERROR;
    00208       break;
    00209
    00210     case TRANSPORT_RAIL:
    00211       railtype = (RailType)GB(p2, 8, 7);
    00212       if (!ValParamRailtype(railtype)) return CMD_ERROR;
    00213       break;
    00214
    00215     case TRANSPORT_WATER:
    00216       break;
    00217
    00218     default:
    00219       /* Airports don't have bridges. */
    00220       return CMD_ERROR;
    00221   }
    00222   TileIndex tile_start = p1;
    00223   TileIndex tile_end = end_tile;
    00224
    00225   if (tile_start == tile_end) {
    00226     return_cmd_error(STR_ERROR_CAN_T_START_AND_END_ON);
    00227   }
    00228
    00229   Axis direction;
    00230   if (TileX(tile_start) == TileX(tile_end)) {
    00231     direction = AXIS_Y;
    00232   } else if (TileY(tile_start) == TileY(tile_end)) {
    00233     direction = AXIS_X;
    00234   } else {
    00235     return_cmd_error(STR_ERROR_START_AND_END_MUST_BE_IN);
    00236   }
    00237
    00238   if (tile_end < tile_start) Swap(tile_start, tile_end);
    00239
    00240   uint bridge_len = GetTunnelBridgeLength(tile_start, tile_end);
    00241   if (transport_type != TRANSPORT_WATER) {
    00242     /* set and test bridge length, availability */
    00243     if (!CheckBridge_Stuff(bridge_type, bridge_len, flags)) return_cmd_error(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE);
    00244   } else {
    00245     if (bridge_len > (_settings_game.construction.longbridges ? 100U : 16U)) return_cmd_error(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE);
    00246   }
    00247
    00248   /* retrieve landscape height and ensure it's on land */
    00249   if (IsWaterTile(tile_start) || IsWaterTile(tile_end)) {
    00250     return_cmd_error(STR_ERROR_ENDS_OF_BRIDGE_MUST_BOTH);
    00251   }
    00252
    00253   uint z_start;
    00254   uint z_end;
    00255   Slope tileh_start = GetTileSlope(tile_start, &z_start);
    00256   Slope tileh_end = GetTileSlope(tile_end, &z_end);
    00257
    00258   CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start);
    00259   CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end,   &z_end);
    00260
    00261   if (z_start != z_end) return_cmd_error(STR_ERROR_BRIDGEHEADS_NOT_SAME_HEIGHT);
    00262
    00263   if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) &&
    00264       GetOtherBridgeEnd(tile_start) == tile_end &&
    00265       GetTunnelBridgeTransportType(tile_start) == transport_type) {
    00266     /* Replace a current bridge. */
    00267
    00268     /* If this is a railway bridge, make sure the railtypes match. */
    00269     if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) {
    00270       return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00271     }
    00272
    00273     /* Do not replace town bridges with lower speed bridges. */
    00274     if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) &&
    00275         GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed) {
    00276       Town *t = ClosestTownFromTile(tile_start, UINT_MAX);
    00277
    00278       if (t == NULL) {
    00279         return CMD_ERROR;
    00280       } else {
    00281         SetDParam(0, t->index);
    00282         return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
    00283       }
    00284     }
    00285
    00286     /* Do not replace the bridge with the same bridge type. */
    00287     if (!(flags & DC_QUERY_COST) && bridge_type == GetBridgeType(tile_start)) {
    00288       return_cmd_error(STR_ERROR_ALREADY_BUILT);
    00289     }
    00290
    00291     /* Do not allow replacing another company's bridges. */
    00292     if (!IsTileOwner(tile_start, _current_company) && !IsTileOwner(tile_start, OWNER_TOWN)) {
    00293       return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
    00294     }
    00295
    00296     cost.AddCost((bridge_len + 1) * _price[PR_CLEAR_BRIDGE]); // The cost of clearing the current bridge.
    00297     owner = GetTileOwner(tile_start);
    00298
    00299     /* Do not remove road types when upgrading a bridge */
    00300     roadtypes |= GetRoadTypes(tile_start);
    00301   } else {
    00302     /* Build a new bridge. */
    00303
    00304     bool allow_on_slopes = (_settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER);
    00305
    00306     /* Try and clear the start landscape */
    00307     CommandCost ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00308     if (CmdFailed(ret)) return ret;
    00309     cost = ret;
    00310
    00311     if (CmdFailed(terraform_cost_north) || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes))
    00312       return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00313     cost.AddCost(terraform_cost_north);
    00314
    00315     /* Try and clear the end landscape */
    00316     ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00317     if (CmdFailed(ret)) return ret;
    00318     cost.AddCost(ret);
    00319
    00320     /* false - end tile slope check */
    00321     if (CmdFailed(terraform_cost_south) || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes))
    00322       return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00323     cost.AddCost(terraform_cost_south);
    00324
    00325     if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00326
    00327     const TileIndex heads[] = {tile_start, tile_end};
    00328     for (int i = 0; i < 2; i++) {
    00329       if (MayHaveBridgeAbove(heads[i])) {
    00330         if (IsBridgeAbove(heads[i])) {
    00331           TileIndex north_head = GetNorthernBridgeEnd(heads[i]);
    00332
    00333           if (direction == GetBridgeAxis(heads[i])) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00334
    00335           if (z_start + TILE_HEIGHT == GetBridgeHeight(north_head)) {
    00336             return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00337           }
    00338         }
    00339       }
    00340     }
    00341
    00342     TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
    00343     for (TileIndex tile = tile_start + delta; tile != tile_end; tile += delta) {
    00344       if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_ERROR_BRIDGE_TOO_LOW_FOR_TERRAIN);
    00345
    00346       if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) {
    00347         /* Disallow crossing bridges for the time being */
    00348         return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00349       }
    00350
    00351       switch (GetTileType(tile)) {
    00352         case MP_WATER:
    00353           if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below;
    00354           break;
    00355
    00356         case MP_RAILWAY:
    00357           if (!IsPlainRail(tile)) goto not_valid_below;
    00358           break;
    00359
    00360         case MP_ROAD:
    00361           if (IsRoadDepot(tile)) goto not_valid_below;
    00362           break;
    00363
    00364         case MP_TUNNELBRIDGE:
    00365           if (IsTunnel(tile)) break;
    00366           if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below;
    00367           if (z_start < GetBridgeHeight(tile)) goto not_valid_below;
    00368           break;
    00369
    00370         case MP_UNMOVABLE:
    00371           if (!IsOwnedLand(tile)) goto not_valid_below;
    00372           break;
    00373
    00374         case MP_CLEAR:
    00375           break;
    00376
    00377         default:
    00378   not_valid_below:;
    00379           /* try and clear the middle landscape */
    00380           ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00381           if (CmdFailed(ret)) return ret;
    00382           cost.AddCost(ret);
    00383           break;
    00384       }
    00385
    00386       if (flags & DC_EXEC) {
    00387         /* We do this here because when replacing a bridge with another
    00388          * type calling SetBridgeMiddle isn't needed. After all, the
    00389          * tile alread has the has_bridge_above bits set. */
    00390         SetBridgeMiddle(tile, direction);
    00391       }
    00392     }
    00393
    00394     owner = _current_company;
    00395   }
    00396
    00397   /* do the drill? */
    00398   if (flags & DC_EXEC) {
    00399     DiagDirection dir = AxisToDiagDir(direction);
    00400
    00401     switch (transport_type) {
    00402       case TRANSPORT_RAIL:
    00403         MakeRailBridgeRamp(tile_start, owner, bridge_type, dir,                 railtype);
    00404         MakeRailBridgeRamp(tile_end,   owner, bridge_type, ReverseDiagDir(dir), railtype);
    00405         break;
    00406
    00407       case TRANSPORT_ROAD:
    00408         MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir,                 roadtypes);
    00409         MakeRoadBridgeRamp(tile_end,   owner, bridge_type, ReverseDiagDir(dir), roadtypes);
    00410         break;
    00411
    00412       case TRANSPORT_WATER:
    00413         MakeAqueductBridgeRamp(tile_start, owner, dir);
    00414         MakeAqueductBridgeRamp(tile_end,   owner, ReverseDiagDir(dir));
    00415         break;
    00416
    00417       default:
    00418         NOT_REACHED();
    00419     }
    00420
    00421     /* Mark all tiles dirty */
    00422     TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
    00423     for (TileIndex tile = tile_start; tile <= tile_end; tile += delta) {
    00424       MarkTileDirtyByTile(tile);
    00425     }
    00426   }
    00427
    00428   if ((flags & DC_EXEC) && transport_type == TRANSPORT_RAIL) {
    00429     Track track = AxisToTrack(direction);
    00430     AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, _current_company);
    00431     YapfNotifyTrackLayoutChange(tile_start, track);
    00432   }
    00433
    00434   /* for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST)
    00435    * It's unnecessary to execute this command every time for every bridge. So it is done only
    00436    * and cost is computed in "bridge_gui.c". For AI, Towns this has to be of course calculated
    00437    */
    00438   Company *c = Company::GetIfValid(_current_company);
    00439   if (!(flags & DC_QUERY_COST) || (c != NULL && c->is_ai)) {
    00440     bridge_len += 2; // begin and end tiles/ramps
    00441
    00442     if (c != NULL) bridge_len = CalcBridgeLenCostFactor(bridge_len);
    00443
    00444     cost.AddCost((int64)bridge_len * _price[PR_BUILD_BRIDGE] * GetBridgeSpec(bridge_type)->price >> 8);
    00445
    00446     /* Aqueducts are a little more expensive. */
    00447     if (transport_type == TRANSPORT_WATER) cost.AddCost((int64)bridge_len * _price[PR_CLEAR_WATER]);
    00448   }
    00449
    00450   return cost;
    00451 }
    00452
    00453
    00462 CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00463 {
    00464   TransportType transport_type = (TransportType)GB(p1, 9, 1);
    00465   CommandCost cost(EXPENSES_CONSTRUCTION);
    00466
    00467   _build_tunnel_endtile = 0;
    00468   if (transport_type == TRANSPORT_RAIL) {
    00469     if (!ValParamRailtype((RailType)p1)) return CMD_ERROR;
    00470   } else {
    00471     const RoadTypes rts = (RoadTypes)GB(p1, 0, 2);
    00472     if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_company, rts)) return CMD_ERROR;
    00473   }
    00474
    00475   uint start_z;
    00476   uint end_z;
    00477   Slope start_tileh = GetTileSlope(start_tile, &start_z);
    00478   DiagDirection direction = GetInclinedSlopeDirection(start_tileh);
    00479   if (direction == INVALID_DIAGDIR) return_cmd_error(STR_ERROR_SITE_UNSUITABLE_FOR_TUNNEL);
    00480
    00481   if (IsWaterTile(start_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
    00482
    00483   CommandCost ret = DoCommand(start_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00484   if (CmdFailed(ret)) return ret;
    00485
    00486   /* XXX - do NOT change 'ret' in the loop, as it is used as the price
    00487    * for the clearing of the entrance of the tunnel. Assigning it to
    00488    * cost before the loop will yield different costs depending on start-
    00489    * position, because of increased-cost-by-length: 'cost += cost >> 3' */
    00490
    00491   TileIndexDiff delta = TileOffsByDiagDir(direction);
    00492   DiagDirection tunnel_in_way_dir;
    00493   if (DiagDirToAxis(direction) == AXIS_Y) {
    00494     tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE;
    00495   } else {
    00496     tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW;
    00497   }
    00498
    00499   TileIndex end_tile = start_tile;
    00500
    00501   /* Tile shift coeficient. Will decrease for very long tunnels to avoid exponential growth of price*/
    00502   int tiles_coef = 3;
    00503   /* Number of tiles from start of tunnel */
    00504   int tiles = 0;
    00505   /* Number of tiles at which the cost increase coefficient per tile is halved */
    00506   int tiles_bump = 25;
    00507
    00508   Slope end_tileh;
    00509   for (;;) {
    00510     end_tile += delta;
    00511     if (!IsValidTile(end_tile)) return_cmd_error(STR_ERROR_TUNNEL_THROUGH_MAP_BORDER);
    00512     end_tileh = GetTileSlope(end_tile, &end_z);
    00513
    00514     if (start_z == end_z) break;
    00515
    00516     if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
    00517       return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY);
    00518     }
    00519
    00520     tiles++;
    00521     if (tiles == tiles_bump) {
    00522       tiles_coef++;
    00523       tiles_bump *= 2;
    00524     }
    00525
    00526     cost.AddCost(_price[PR_BUILD_TUNNEL]);
    00527     cost.AddCost(cost.GetCost() >> tiles_coef); // add a multiplier for longer tunnels
    00528   }
    00529
    00530   /* Add the cost of the entrance */
    00531   cost.AddCost(_price[PR_BUILD_TUNNEL]);
    00532   cost.AddCost(ret);
    00533
    00534   /* if the command fails from here on we want the end tile to be highlighted */
    00535   _build_tunnel_endtile = end_tile;
    00536
    00537   if (IsWaterTile(end_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
    00538
    00539   /* slope of end tile must be complementary to the slope of the start tile */
    00540   if (end_tileh != ComplementSlope(start_tileh)) {
    00541     /* Check if there is a structure on the terraformed tile. Do not add the cost, that will be done by the terraforming
    00542      * Note: Currently the town rating is also affected by this clearing-test. So effectivly the player is punished twice for clearing
    00543      *       the tree on end_tile.
    00544      */
    00545     ret = DoCommand(end_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR);
    00546     if (CmdFailed(ret)) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
    00547
    00548     ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND);
    00549     if (CmdFailed(ret)) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
    00550   } else {
    00551     ret = DoCommand(end_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00552     if (CmdFailed(ret)) return ret;
    00553   }
    00554   cost.AddCost(_price[PR_BUILD_TUNNEL]);
    00555   cost.AddCost(ret);
    00556
    00557   if (flags & DC_EXEC) {
    00558     if (transport_type == TRANSPORT_RAIL) {
    00559       MakeRailTunnel(start_tile, _current_company, direction,                 (RailType)GB(p1, 0, 4));
    00560       MakeRailTunnel(end_tile,   _current_company, ReverseDiagDir(direction), (RailType)GB(p1, 0, 4));
    00561       AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_company);
    00562       YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction));
    00563     } else {
    00564       MakeRoadTunnel(start_tile, _current_company, direction,                 (RoadTypes)GB(p1, 0, 2));
    00565       MakeRoadTunnel(end_tile,   _current_company, ReverseDiagDir(direction), (RoadTypes)GB(p1, 0, 2));
    00566     }
    00567   }
    00568
    00569   return cost;
    00570 }
    00571
    00572
    00573 static inline bool CheckAllowRemoveTunnelBridge(TileIndex tile)
    00574 {
    00575   /* Floods can remove anything as well as the scenario editor */
    00576   if (_current_company == OWNER_WATER || _game_mode == GM_EDITOR) return true;
    00577
    00578   switch (GetTunnelBridgeTransportType(tile)) {
    00579     case TRANSPORT_ROAD: {
    00580       RoadTypes rts = GetRoadTypes(tile);
    00581       Owner road_owner = _current_company;
    00582       Owner tram_owner = _current_company;
    00583
    00584       if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    00585       if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    00586
    00587       /* We can remove unowned road and if the town allows it */
    00588       if (road_owner == OWNER_TOWN && !(_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) return CheckTileOwnership(tile);
    00589       if (road_owner == OWNER_NONE || road_owner == OWNER_TOWN) road_owner = _current_company;
    00590       if (tram_owner == OWNER_NONE) tram_owner = _current_company;
    00591
    00592       return CheckOwnership(road_owner) && CheckOwnership(tram_owner);
    00593     }
    00594
    00595     case TRANSPORT_RAIL:
    00596     case TRANSPORT_WATER:
    00597       return CheckOwnership(GetTileOwner(tile));
    00598
    00599     default: NOT_REACHED();
    00600   }
    00601 }
    00602
    00603 static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags)
    00604 {
    00605   Town *t = NULL;
    00606   TileIndex endtile;
    00607
    00608   if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
    00609
    00610   endtile = GetOtherTunnelEnd(tile);
    00611
    00612   if (HasVehicleOnTunnelBridge(tile, endtile)) return CMD_ERROR;
    00613
    00614   _build_tunnel_endtile = endtile;
    00615
    00616   if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
    00617     t = ClosestTownFromTile(tile, UINT_MAX); // town penalty rating
    00618
    00619     /* Check if you are allowed to remove the tunnel owned by a town
    00620      * Removal depends on difficulty settings */
    00621     if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) {
    00622       SetDParam(0, t->index);
    00623       return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
    00624     }
    00625   }
    00626
    00627   /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
    00628    * you have a "Poor" (0) town rating */
    00629   if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
    00630     ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
    00631   }
    00632
    00633   if (flags & DC_EXEC) {
    00634     if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
    00635       /* We first need to request values before calling DoClearSquare */
    00636       DiagDirection dir = GetTunnelBridgeDirection(tile);
    00637       Track track = DiagDirToDiagTrack(dir);
    00638       Owner owner = GetTileOwner(tile);
    00639
    00640       Train *v = NULL;
    00641       if (HasTunnelBridgeReservation(tile)) {
    00642         v = GetTrainForReservation(tile, track);
    00643         if (v != NULL) FreeTrainTrackReservation(v);
    00644       }
    00645
    00646       DoClearSquare(tile);
    00647       DoClearSquare(endtile);
    00648
    00649       /* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */
    00650       AddSideToSignalBuffer(tile,    ReverseDiagDir(dir), owner);
    00651       AddSideToSignalBuffer(endtile, dir,                 owner);
    00652
    00653       YapfNotifyTrackLayoutChange(tile,    track);
    00654       YapfNotifyTrackLayoutChange(endtile, track);
    00655
    00656       if (v != NULL) TryPathReserve(v);
    00657     } else {
    00658       DoClearSquare(tile);
    00659       DoClearSquare(endtile);
    00660     }
    00661   }
    00662   return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_TUNNEL] * (GetTunnelBridgeLength(tile, endtile) + 2));
    00663 }
    00664
    00665
    00666 static CommandCost DoClearBridge(TileIndex tile, DoCommandFlag flags)
    00667 {
    00668   DiagDirection direction;
    00669   TileIndexDiff delta;
    00670   TileIndex endtile;
    00671   Town *t = NULL;
    00672
    00673   if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
    00674
    00675   endtile = GetOtherBridgeEnd(tile);
    00676
    00677   if (HasVehicleOnTunnelBridge(tile, endtile)) return CMD_ERROR;
    00678
    00679   direction = GetTunnelBridgeDirection(tile);
    00680   delta = TileOffsByDiagDir(direction);
    00681
    00682   if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
    00683     t = ClosestTownFromTile(tile, UINT_MAX); // town penalty rating
    00684
    00685     /* Check if you are allowed to remove the bridge owned by a town
    00686      * Removal depends on difficulty settings */
    00687     if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) {
    00688       SetDParam(0, t->index);
    00689       return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
    00690     }
    00691   }
    00692
    00693   /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
    00694    * you have a "Poor" (0) town rating */
    00695   if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
    00696     ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
    00697   }
    00698
    00699   if (flags & DC_EXEC) {
    00700     /* read this value before actual removal of bridge */
    00701     bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
    00702     Owner owner = GetTileOwner(tile);
    00703     uint height = GetBridgeHeight(tile);
    00704     Train *v = NULL;
    00705
    00706     if (rail && HasTunnelBridgeReservation(tile)) {
    00707       v = GetTrainForReservation(tile, DiagDirToDiagTrack(direction));
    00708       if (v != NULL) FreeTrainTrackReservation(v);
    00709     }
    00710
    00711     DoClearSquare(tile);
    00712     DoClearSquare(endtile);
    00713     for (TileIndex c = tile + delta; c != endtile; c += delta) {
    00714       /* do not let trees appear from 'nowhere' after removing bridge */
    00715       if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) {
    00716         uint minz = GetTileMaxZ(c) + 3 * TILE_HEIGHT;
    00717         if (height < minz) SetRoadside(c, ROADSIDE_PAVED);
    00718       }
    00719       ClearBridgeMiddle(c);
    00720       MarkTileDirtyByTile(c);
    00721     }
    00722
    00723     if (rail) {
    00724       /* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */
    00725       AddSideToSignalBuffer(tile,    ReverseDiagDir(direction), owner);
    00726       AddSideToSignalBuffer(endtile, direction,                 owner);
    00727
    00728       Track track = DiagDirToDiagTrack(direction);
    00729       YapfNotifyTrackLayoutChange(tile,    track);
    00730       YapfNotifyTrackLayoutChange(endtile, track);
    00731
    00732       if (v != NULL) TryPathReserve(v, true);
    00733     }
    00734   }
    00735
    00736   return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * _price[PR_CLEAR_BRIDGE]);
    00737 }
    00738
    00739 static CommandCost ClearTile_TunnelBridge(TileIndex tile, DoCommandFlag flags)
    00740 {
    00741   if (IsTunnel(tile)) {
    00742     if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST);
    00743     return DoClearTunnel(tile, flags);
    00744   } else { // IsBridge(tile)
    00745     if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00746     return DoClearBridge(tile, flags);
    00747   }
    00748
    00749   return CMD_ERROR;
    00750 }
    00751
    00763 static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
    00764 {
    00765   /* Do not draw bridge pillars if they are invisible */
    00766   if (IsInvisibilitySet(TO_BRIDGES)) return;
    00767
    00768   SpriteID image = psid->sprite;
    00769
    00770   if (image != 0) {
    00771     /* "side" specifies the side the pillars stand on.
    00772      * The length of the pillars is then set to the height of the bridge over the corners of this edge.
    00773      *
    00774      *                axis==AXIS_X  axis==AXIS_Y
    00775      *   side==false      SW            NW
    00776      *   side==true       NE            SE
    00777      *
    00778      * I have no clue, why this was done this way.
    00779      */
    00780     bool side = HasBit(image, 0);
    00781
    00782     /* "dir" means the edge the pillars stand on */
    00783     DiagDirection dir = AxisToDiagDir(axis);
    00784     if (side != (axis == AXIS_Y)) dir = ReverseDiagDir(dir);
    00785
    00786     /* Determine ground height under pillars */
    00787     int front_height = ti->z;
    00788     int back_height = ti->z;
    00789     GetSlopeZOnEdge(ti->tileh, dir, &front_height, &back_height);
    00790
    00791     /* x and y size of bounding-box of pillars */
    00792     int w = (axis == AXIS_X ? 16 : 2);
    00793     int h = (axis == AXIS_X ? 2 : 16);
    00794     /* sprite position of back facing pillar */
    00795     int x_back = x - (axis == AXIS_X ? 0 : 9);
    00796     int y_back = y - (axis == AXIS_X ? 9 : 0);
    00797
    00798     for (int cur_z = z_bridge; cur_z >= front_height || cur_z >= back_height; cur_z -= TILE_HEIGHT) {
    00799       /* Draw front facing pillar */
    00800       if (cur_z >= front_height) {
    00801         AddSortableSpriteToDraw(image, psid->pal, x, y, w, h, BB_HEIGHT_UNDER_BRIDGE - 5, cur_z, IsTransparencySet(TO_BRIDGES), 0, 0, -5);
    00802       }
    00803
    00804       /* Draw back facing pillar, but not the highest part directly under the bridge-floor */
    00805       if (drawfarpillar && cur_z >= back_height && cur_z < z_bridge - TILE_HEIGHT) {
    00806         AddSortableSpriteToDraw(image, psid->pal, x_back, y_back, w, h, BB_HEIGHT_UNDER_BRIDGE - 5, cur_z, IsTransparencySet(TO_BRIDGES), 0, 0, -5);
    00807       }
    00808     }
    00809   }
    00810 }
    00811
    00821 static void DrawBridgeTramBits(int x, int y, byte z, int offset, bool overlay, bool head)
    00822 {
    00823   static const SpriteID tram_offsets[2][6] = { { 107, 108, 109, 110, 111, 112 }, { 4, 5, 15, 16, 17, 18 } };
    00824   static const SpriteID back_offsets[6]    =   {  95,  96,  99, 102, 100, 101 };
    00825   static const SpriteID front_offsets[6]   =   {  97,  98, 103, 106, 104, 105 };
    00826
    00827   static const uint size_x[6] = {  1, 16, 16,  1, 16,  1 };
    00828   static const uint size_y[6] = { 16,  1,  1, 16,  1, 16 };
    00829   static const uint front_bb_offset_x[6] = { 15,  0,  0, 15,  0, 15 };
    00830   static const uint front_bb_offset_y[6] = {  0, 15, 15,  0, 15,  0 };
    00831
    00832   /* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called
    00833    * The bounding boxes here are the same as for bridge front/roof */
    00834   if (head || !IsInvisibilitySet(TO_BRIDGES)) {
    00835     AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE,
    00836       x, y, size_x[offset], size_y[offset], 0x28, z,
    00837       !head && IsTransparencySet(TO_BRIDGES));
    00838   }
    00839
    00840   /* Do not draw catenary if it is set invisible */
    00841   if (!IsInvisibilitySet(TO_CATENARY)) {
    00842     AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset], PAL_NONE,
    00843       x, y, size_x[offset], size_y[offset], 0x28, z,
    00844       IsTransparencySet(TO_CATENARY));
    00845   }
    00846
    00847   /* Start a new SpriteCombine for the front part */
    00848   EndSpriteCombine();
    00849   StartSpriteCombine();
    00850
    00851   /* For sloped sprites the bounding box needs to be higher, as the pylons stop on a higher point */
    00852   if (!IsInvisibilitySet(TO_CATENARY)) {
    00853     AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE,
    00854       x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z,
    00855       IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]);
    00856   }
    00857 }
    00858
    00872 static void DrawTile_TunnelBridge(TileInfo *ti)
    00873 {
    00874   SpriteID image;
    00875   TransportType transport_type = GetTunnelBridgeTransportType(ti->tile);
    00876   DiagDirection tunnelbridge_direction = GetTunnelBridgeDirection(ti->tile);
    00877
    00878   if (IsTunnel(ti->tile)) {
    00879     /* Front view of tunnel bounding boxes:
    00880      *
    00881      *   122223  <- BB_Z_SEPARATOR
    00882      *   1    3
    00883      *   1    3                1,3 = empty helper BB
    00884      *   1    3                  2 = SpriteCombine of tunnel-roof and catenary (tram & elrail)
    00885      *
    00886      */
    00887
    00888     static const int _tunnel_BB[4][12] = {
    00889       /*  tunnnel-roof  |  Z-separator  | tram-catenary
    00890        * w  h  bb_x bb_y| x   y   w   h |bb_x bb_y w h */
    00891       {  1,  0, -15, -14,  0, 15, 16,  1, 0, 1, 16, 15 }, // NE
    00892       {  0,  1, -14, -15, 15,  0,  1, 16, 1, 0, 15, 16 }, // SE
    00893       {  1,  0, -15, -14,  0, 15, 16,  1, 0, 1, 16, 15 }, // SW
    00894       {  0,  1, -14, -15, 15,  0,  1, 16, 1, 0, 15, 16 }, // NW
    00895     };
    00896     const int *BB_data = _tunnel_BB[tunnelbridge_direction];
    00897
    00898     bool catenary = false;
    00899
    00900     if (transport_type == TRANSPORT_RAIL) {
    00901       image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
    00902     } else {
    00903       image = SPR_TUNNEL_ENTRY_REAR_ROAD;
    00904     }
    00905
    00906     if (HasTunnelBridgeSnowOrDesert(ti->tile)) image += 32;
    00907
    00908     image += tunnelbridge_direction * 2;
    00909     DrawGroundSprite(image, PAL_NONE);
    00910
    00911     /* PBS debugging, draw reserved tracks darker */
    00912     if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && (transport_type == TRANSPORT_RAIL && HasTunnelBridgeReservation(ti->tile))) {
    00913       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
    00914       DrawGroundSprite(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH);
    00915     }
    00916
    00917     if (transport_type == TRANSPORT_ROAD) {
    00918       RoadTypes rts = GetRoadTypes(ti->tile);
    00919
    00920       if (HasBit(rts, ROADTYPE_TRAM)) {
    00921         static const SpriteID tunnel_sprites[2][4] = { { 28, 78, 79, 27 }, {  5, 76, 77,  4 } };
    00922
    00923         DrawGroundSprite(SPR_TRAMWAY_BASE + tunnel_sprites[rts - ROADTYPES_TRAM][tunnelbridge_direction], PAL_NONE);
    00924
    00925         /* Do not draw wires if they are invisible */
    00926         if (!IsInvisibilitySet(TO_CATENARY)) {
    00927           catenary = true;
    00928           StartSpriteCombine();
    00929           AddSortableSpriteToDraw(SPR_TRAMWAY_TUNNEL_WIRES + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, BB_data[10], BB_data[11], TILE_HEIGHT, ti->z, IsTransparencySet(TO_CATENARY), BB_data[8], BB_data[9], BB_Z_SEPARATOR);
    00930         }
    00931       }
    00932     } else if (HasCatenaryDrawn(GetRailType(ti->tile))) {
    00933       /* Maybe draw pylons on the entry side */
    00934       DrawCatenary(ti);
    00935
    00936       catenary = true;
    00937       StartSpriteCombine();
    00938       /* Draw wire above the ramp */
    00939       DrawCatenaryOnTunnel(ti);
    00940     }
    00941
    00942     AddSortableSpriteToDraw(image + 1, PAL_NONE, ti->x + TILE_SIZE - 1, ti->y + TILE_SIZE - 1, BB_data[0], BB_data[1], TILE_HEIGHT, ti->z, false, BB_data[2], BB_data[3], BB_Z_SEPARATOR);
    00943
    00944     if (catenary) EndSpriteCombine();
    00945
    00946     /* Add helper BB for sprite sorting, that separate the tunnel from things beside of it */
    00947     AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x,              ti->y,              BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
    00948     AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x + BB_data[4], ti->y + BB_data[5], BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
    00949
    00950     DrawBridgeMiddle(ti);
    00951   } else { // IsBridge(ti->tile)
    00952     const PalSpriteID *psid;
    00953     int base_offset;
    00954     bool ice = HasTunnelBridgeSnowOrDesert(ti->tile);
    00955
    00956     if (transport_type == TRANSPORT_RAIL) {
    00957       base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset;
    00958       assert(base_offset != 8); // This one is used for roads
    00959     } else {
    00960       base_offset = 8;
    00961     }
    00962
    00963     /* as the lower 3 bits are used for other stuff, make sure they are clear */
    00964     assert( (base_offset & 0x07) == 0x00);
    00965
    00966     DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(tunnelbridge_direction)));
    00967
    00968     /* HACK Wizardry to convert the bridge ramp direction into a sprite offset */
    00969     base_offset += (6 - tunnelbridge_direction) % 4;
    00970
    00971     if (ti->tileh == SLOPE_FLAT) base_offset += 4; // sloped bridge head
    00972
    00973     /* Table number BRIDGE_PIECE_HEAD always refers to the bridge heads for any bridge type */
    00974     if (transport_type != TRANSPORT_WATER) {
    00975       psid = &GetBridgeSpriteTable(GetBridgeType(ti->tile), BRIDGE_PIECE_HEAD)[base_offset];
    00976     } else {
    00977       psid = _aqueduct_sprites + base_offset;
    00978     }
    00979
    00980     if (!ice) {
    00981       DrawClearLandTile(ti, 3);
    00982     } else {
    00983       DrawGroundSprite(SPR_FLAT_SNOW_DESERT_TILE + _tileh_to_sprite[ti->tileh], PAL_NONE);
    00984     }
    00985
    00986     /* draw ramp */
    00987
    00988     /* Draw Trambits and PBS Reservation as SpriteCombine */
    00989     if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
    00990
    00991     /* HACK set the height of the BB of a sloped ramp to 1 so a vehicle on
    00992      * it doesn't disappear behind it
    00993      */
    00994     /* Bridge heads are drawn solid no matter how invisibility/transparency is set */
    00995     AddSortableSpriteToDraw(psid->sprite, psid->pal, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 0 : 8, ti->z);
    00996
    00997     if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && transport_type == TRANSPORT_RAIL && HasTunnelBridgeReservation(ti->tile)) {
    00998       const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
    00999       if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
    01000         AddSortableSpriteToDraw(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, ti->z + 8);
    01001       } else {
    01002         AddSortableSpriteToDraw(rti->base_sprites.single_sloped + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
    01003       }
    01004     }
    01005
    01006     if (transport_type == TRANSPORT_ROAD) {
    01007       RoadTypes rts = GetRoadTypes(ti->tile);
    01008
    01009       if (HasBit(rts, ROADTYPE_TRAM)) {
    01010         uint offset = tunnelbridge_direction;
    01011         uint z = ti->z;
    01012         if (ti->tileh != SLOPE_FLAT) {
    01013           offset = (offset + 1) & 1;
    01014           z += TILE_HEIGHT;
    01015         } else {
    01016           offset += 2;
    01017         }
    01018         /* DrawBridgeTramBits() calls EndSpriteCombine() and StartSpriteCombine() */
    01019         DrawBridgeTramBits(ti->x, ti->y, z, offset, HasBit(rts, ROADTYPE_ROAD), true);
    01020       }
    01021       EndSpriteCombine();
    01022     } else if (transport_type == TRANSPORT_RAIL) {
    01023       EndSpriteCombine();
    01024       if (HasCatenaryDrawn(GetRailType(ti->tile))) {
    01025         DrawCatenary(ti);
    01026       }
    01027     }
    01028
    01029     DrawBridgeMiddle(ti);
    01030   }
    01031 }
    01032
    01033
    01051 static BridgePieces CalcBridgePiece(uint north, uint south)
    01052 {
    01053   if (north == 1) {
    01054     return BRIDGE_PIECE_NORTH;
    01055   } else if (south == 1) {
    01056     return BRIDGE_PIECE_SOUTH;
    01057   } else if (north < south) {
    01058     return north & 1 ? BRIDGE_PIECE_INNER_SOUTH : BRIDGE_PIECE_INNER_NORTH;
    01059   } else if (north > south) {
    01060     return south & 1 ? BRIDGE_PIECE_INNER_NORTH : BRIDGE_PIECE_INNER_SOUTH;
    01061   } else {
    01062     return north & 1 ? BRIDGE_PIECE_MIDDLE_EVEN : BRIDGE_PIECE_MIDDLE_ODD;
    01063   }
    01064 }
    01065
    01066
    01067 void DrawBridgeMiddle(const TileInfo *ti)
    01068 {
    01069   /* Sectional view of bridge bounding boxes:
    01070    *
    01071    *  1           2                                1,2 = SpriteCombine of Bridge front/(back&floor) and TramCatenary
    01072    *  1           2                                  3 = empty helper BB
    01073    *  1     7     2                                4,5 = pillars under higher bridges
    01074    *  1 6 88888 6 2                                  6 = elrail-pylons
    01075    *  1 6 88888 6 2                                  7 = elrail-wire
    01076    *  1 6 88888 6 2  <- TILE_HEIGHT                  8 = rail-vehicle on bridge
    01077    *  3333333333333  <- BB_Z_SEPARATOR
    01078    *                 <- unused
    01079    *    4       5    <- BB_HEIGHT_UNDER_BRIDGE
    01080    *    4       5
    01081    *    4       5
    01082    *
    01083    */
    01084
    01085   /* Z position of the bridge sprites relative to bridge height (downwards) */
    01086   static const int BRIDGE_Z_START = 3;
    01087
    01088   if (!IsBridgeAbove(ti->tile)) return;
    01089
    01090   TileIndex rampnorth = GetNorthernBridgeEnd(ti->tile);
    01091   TileIndex rampsouth = GetSouthernBridgeEnd(ti->tile);
    01092   TransportType transport_type = GetTunnelBridgeTransportType(rampsouth);
    01093
    01094   Axis axis = GetBridgeAxis(ti->tile);
    01095   BridgePieces piece = CalcBridgePiece(
    01096     GetTunnelBridgeLength(ti->tile, rampnorth) + 1,
    01097     GetTunnelBridgeLength(ti->tile, rampsouth) + 1
    01098   );
    01099
    01100   const PalSpriteID *psid;
    01101   bool drawfarpillar;
    01102   if (transport_type != TRANSPORT_WATER) {
    01103     BridgeType type =  GetBridgeType(rampsouth);
    01104     drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0);
    01105
    01106     uint base_offset;
    01107     if (transport_type == TRANSPORT_RAIL) {
    01108       base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset;
    01109     } else {
    01110       base_offset = 8;
    01111     }
    01112
    01113     psid = base_offset + GetBridgeSpriteTable(type, piece);
    01114   } else {
    01115     drawfarpillar = true;
    01116     psid = _aqueduct_sprites;
    01117   }
    01118
    01119   if (axis != AXIS_X) psid += 4;
    01120
    01121   int x = ti->x;
    01122   int y = ti->y;
    01123   uint bridge_z = GetBridgeHeight(rampsouth);
    01124   uint z = bridge_z - BRIDGE_Z_START;
    01125
    01126   /* Add a bounding box, that separates the bridge from things below it. */
    01127   AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, x, y, 16, 16, 1, bridge_z - TILE_HEIGHT + BB_Z_SEPARATOR);
    01128
    01129   /* Draw Trambits as SpriteCombine */
    01130   if (transport_type == TRANSPORT_ROAD) StartSpriteCombine();
    01131
    01132   /* Draw floor and far part of bridge*/
    01133   if (!IsInvisibilitySet(TO_BRIDGES)) {
    01134     if (axis == AXIS_X) {
    01135       AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 1, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
    01136     } else {
    01137       AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 1, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
    01138     }
    01139   }
    01140
    01141   psid++;
    01142
    01143   if (transport_type == TRANSPORT_ROAD) {
    01144     RoadTypes rts = GetRoadTypes(rampsouth);
    01145
    01146     if (HasBit(rts, ROADTYPE_TRAM)) {
    01147       /* DrawBridgeTramBits() calls EndSpriteCombine() and StartSpriteCombine() */
    01148       DrawBridgeTramBits(x, y, bridge_z, axis ^ 1, HasBit(rts, ROADTYPE_ROAD), false);
    01149     } else {
    01150       EndSpriteCombine();
    01151       StartSpriteCombine();
    01152     }
    01153   } else if (transport_type == TRANSPORT_RAIL) {
    01154     if (HasCatenaryDrawn(GetRailType(rampsouth))) {
    01155       DrawCatenaryOnBridge(ti);
    01156     }
    01157   }
    01158
    01159   /* draw roof, the component of the bridge which is logically between the vehicle and the camera */
    01160   if (!IsInvisibilitySet(TO_BRIDGES)) {
    01161     if (axis == AXIS_X) {
    01162       y += 12;
    01163       if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 4, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 3, BRIDGE_Z_START);
    01164     } else {
    01165       x += 12;
    01166       if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 4, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 3, 0, BRIDGE_Z_START);
    01167     }
    01168   }
    01169
    01170   /* Draw TramFront as SpriteCombine */
    01171   if (transport_type == TRANSPORT_ROAD) EndSpriteCombine();
    01172
    01173   /* Do not draw anything more if bridges are invisible */
    01174   if (IsInvisibilitySet(TO_BRIDGES)) return;
    01175
    01176   psid++;
    01177   if (ti->z + 5 == z) {
    01178     /* draw poles below for small bridges */
    01179     if (psid->sprite != 0) {
    01180       SpriteID image = psid->sprite;
    01181       SpriteID pal   = psid->pal;
    01182       if (IsTransparencySet(TO_BRIDGES)) {
    01183         SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
    01184         pal = PALETTE_TO_TRANSPARENT;
    01185       }
    01186
    01187       DrawGroundSpriteAt(image, pal, x, y, z);
    01188     }
    01189   } else if (_settings_client.gui.bridge_pillars) {
    01190     /* draw pillars below for high bridges */
    01191     DrawBridgePillars(psid, ti, axis, drawfarpillar, x, y, z);
    01192   }
    01193 }
    01194
    01195
    01196 static uint GetSlopeZ_TunnelBridge(TileIndex tile, uint x, uint y)
    01197 {
    01198   uint z;
    01199   Slope tileh = GetTileSlope(tile, &z);
    01200
    01201   x &= 0xF;
    01202   y &= 0xF;
    01203
    01204   if (IsTunnel(tile)) {
    01205     uint pos = (DiagDirToAxis(GetTunnelBridgeDirection(tile)) == AXIS_X ? y : x);
    01206
    01207     /* In the tunnel entrance? */
    01208     if (5 <= pos && pos <= 10) return z;
    01209   } else { // IsBridge(tile)
    01210     DiagDirection dir = GetTunnelBridgeDirection(tile);
    01211     uint pos = (DiagDirToAxis(dir) == AXIS_X ? y : x);
    01212
    01213     z += ApplyFoundationToSlope(GetBridgeFoundation(tileh, DiagDirToAxis(dir)), &tileh);
    01214
    01215     /* On the bridge ramp? */
    01216     if (5 <= pos && pos <= 10) {
    01217       uint delta;
    01218
    01219       if (tileh != SLOPE_FLAT) return z + TILE_HEIGHT;
    01220
    01221       switch (dir) {
    01222         default: NOT_REACHED();
    01223         case DIAGDIR_NE: delta = (TILE_SIZE - 1 - x) / 2; break;
    01224         case DIAGDIR_SE: delta = y / 2; break;
    01225         case DIAGDIR_SW: delta = x / 2; break;
    01226         case DIAGDIR_NW: delta = (TILE_SIZE - 1 - y) / 2; break;
    01227       }
    01228       return z + 1 + delta;
    01229     }
    01230   }
    01231
    01232   return z + GetPartialZ(x, y, tileh);
    01233 }
    01234
    01235 static Foundation GetFoundation_TunnelBridge(TileIndex tile, Slope tileh)
    01236 {
    01237   return IsTunnel(tile) ? FOUNDATION_NONE : GetBridgeFoundation(tileh, DiagDirToAxis(GetTunnelBridgeDirection(tile)));
    01238 }
    01239
    01240 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
    01241 {
    01242   TransportType tt = GetTunnelBridgeTransportType(tile);
    01243
    01244   if (IsTunnel(tile)) {
    01245     td->str = (tt == TRANSPORT_RAIL) ? STR_LAI_TUNNEL_DESCRIPTION_RAILROAD : STR_LAI_TUNNEL_DESCRIPTION_ROAD;
    01246   } else { // IsBridge(tile)
    01247     td->str = (tt == TRANSPORT_WATER) ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : GetBridgeSpec(GetBridgeType(tile))->transport_name[tt];
    01248   }
    01249   td->owner[0] = GetTileOwner(tile);
    01250
    01251   Owner road_owner = INVALID_OWNER;
    01252   Owner tram_owner = INVALID_OWNER;
    01253   RoadTypes rts = GetRoadTypes(tile);
    01254   if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
    01255   if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
    01256
    01257   /* Is there a mix of owners? */
    01258   if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
    01259       (road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
    01260     uint i = 1;
    01261     if (road_owner != INVALID_OWNER) {
    01262       td->owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
    01263       td->owner[i] = road_owner;
    01264       i++;
    01265     }
    01266     if (tram_owner != INVALID_OWNER) {
    01267       td->owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
    01268       td->owner[i] = tram_owner;
    01269     }
    01270   }
    01271 }
    01272
    01273
    01274 static void TileLoop_TunnelBridge(TileIndex tile)
    01275 {
    01276   bool snow_or_desert = HasTunnelBridgeSnowOrDesert(tile);
    01277   switch (_settings_game.game_creation.landscape) {
    01278     case LT_ARCTIC:
    01279       if (snow_or_desert != (GetTileZ(tile) > GetSnowLine())) {
    01280         SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert);
    01281         MarkTileDirtyByTile(tile);
    01282       }
    01283       break;
    01284
    01285     case LT_TROPIC:
    01286       if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
    01287         SetTunnelBridgeSnowOrDesert(tile, true);
    01288         MarkTileDirtyByTile(tile);
    01289       }
    01290       break;
    01291
    01292     default:
    01293       break;
    01294   }
    01295 }
    01296
    01297 static TrackStatus GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    01298 {
    01299   TransportType transport_type = GetTunnelBridgeTransportType(tile);
    01300   if (transport_type != mode || (transport_type == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0)) return 0;
    01301
    01302   DiagDirection dir = GetTunnelBridgeDirection(tile);
    01303   if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0;
    01304   return CombineTrackStatus(TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)), TRACKDIR_BIT_NONE);
    01305 }
    01306
    01307 static void ChangeTileOwner_TunnelBridge(TileIndex tile, Owner old_owner, Owner new_owner)
    01308 {
    01309   for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    01310     /* Update all roadtypes, no matter if they are present */
    01311     if (GetRoadOwner(tile, rt) == old_owner) {
    01312       SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
    01313     }
    01314   }
    01315
    01316   if (!IsTileOwner(tile, old_owner)) return;
    01317
    01318   if (new_owner != INVALID_OWNER) {
    01319     SetTileOwner(tile, new_owner);
    01320   } else {
    01321     if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR))) {
    01322       /* When clearing the bridge/tunnel failed there are still vehicles on/in
    01323        * the bridge/tunnel. As all *our* vehicles are already removed, they
    01324        * must be of another owner. Therefore this can't be rail tunnel/bridge.
    01325        * In that case we can safely reassign the ownership to OWNER_NONE. */
    01326       assert(GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL);
    01327       SetTileOwner(tile, OWNER_NONE);
    01328     }
    01329   }
    01330 }
    01331
    01332
    01333 static const byte _tunnel_fractcoord_1[4]    = {0x8E, 0x18, 0x81, 0xE8};
    01334 static const byte _tunnel_fractcoord_2[4]    = {0x81, 0x98, 0x87, 0x38};
    01335 static const byte _tunnel_fractcoord_3[4]    = {0x82, 0x88, 0x86, 0x48};
    01336 static const byte _exit_tunnel_track[4]      = {1, 2, 1, 2};
    01337
    01339 static const Trackdir _road_exit_tunnel_state[DIAGDIR_END] = {
    01340   TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
    01341 };
    01342 static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4};
    01343
    01344 static const byte _tunnel_fractcoord_4[4]    = {0x52, 0x85, 0x98, 0x29};
    01345 static const byte _tunnel_fractcoord_5[4]    = {0x92, 0x89, 0x58, 0x25};
    01346 static const byte _tunnel_fractcoord_6[4]    = {0x92, 0x89, 0x56, 0x45};
    01347 static const byte _tunnel_fractcoord_7[4]    = {0x52, 0x85, 0x96, 0x49};
    01348
    01349 static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
    01350 {
    01351   int z = GetSlopeZ(x, y) - v->z_pos;
    01352
    01353   if (abs(z) > 2) return VETSB_CANNOT_ENTER;
    01354   const DiagDirection dir = GetTunnelBridgeDirection(tile);
    01355
    01356   if (IsTunnel(tile)) {
    01357     byte fc;
    01358     DiagDirection vdir;
    01359
    01360     if (v->type == VEH_TRAIN) {
    01361       Train *t = Train::From(v);
    01362       fc = (x & 0xF) + (y << 4);
    01363
    01364       vdir = DirToDiagDir(t->direction);
    01365
    01366       if (t->track != TRACK_BIT_WORMHOLE && dir == vdir) {
    01367         if (t->IsFrontEngine() && fc == _tunnel_fractcoord_1[dir]) {
    01368           if (!PlayVehicleSound(t, VSE_TUNNEL) && RailVehInfo(t->engine_type)->engclass == 0) {
    01369             SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
    01370           }
    01371           return VETSB_CONTINUE;
    01372         }
    01373         if (fc == _tunnel_fractcoord_2[dir]) {
    01374           t->tile = tile;
    01375           t->track = TRACK_BIT_WORMHOLE;
    01376           t->vehstatus |= VS_HIDDEN;
    01377           return VETSB_ENTERED_WORMHOLE;
    01378         }
    01379       }
    01380
    01381       if (dir == ReverseDiagDir(vdir) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
    01382         /* We're at the tunnel exit ?? */
    01383         t->tile = tile;
    01384         t->track = (TrackBits)_exit_tunnel_track[dir];
    01385         assert(t->track);
    01386         t->vehstatus &= ~VS_HIDDEN;
    01387         return VETSB_ENTERED_WORMHOLE;
    01388       }
    01389     } else if (v->type == VEH_ROAD) {
    01390       RoadVehicle *rv = RoadVehicle::From(v);
    01391       fc = (x & 0xF) + (y << 4);
    01392       vdir = DirToDiagDir(v->direction);
    01393
    01394       /* Enter tunnel? */
    01395       if (rv->state != RVSB_WORMHOLE && dir == vdir) {
    01396         if (fc == _tunnel_fractcoord_4[dir] ||
    01397             fc == _tunnel_fractcoord_5[dir]) {
    01398           rv->tile = tile;
    01399           rv->state = RVSB_WORMHOLE;
    01400           rv->vehstatus |= VS_HIDDEN;
    01401           return VETSB_ENTERED_WORMHOLE;
    01402         } else {
    01403           return VETSB_CONTINUE;
    01404         }
    01405       }
    01406
    01407       if (dir == ReverseDiagDir(vdir) && (
    01408             /* We're at the tunnel exit ?? */
    01409             fc == _tunnel_fractcoord_6[dir] ||
    01410             fc == _tunnel_fractcoord_7[dir]
    01411           ) &&
    01412           z == 0) {
    01413         rv->tile = tile;
    01414         rv->state = _road_exit_tunnel_state[dir];
    01415         rv->frame = _road_exit_tunnel_frame[dir];
    01416         rv->vehstatus &= ~VS_HIDDEN;
    01417         return VETSB_ENTERED_WORMHOLE;
    01418       }
    01419     }
    01420   } else { // IsBridge(tile)
    01421
    01422     if (v->IsPrimaryVehicle() && v->type != VEH_SHIP) {
    01423       /* modify speed of vehicle */
    01424       uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
    01425
    01426       if (v->type == VEH_ROAD) spd *= 2;
    01427       if (v->cur_speed > spd) v->cur_speed = spd;
    01428     }
    01429
    01430     if (DirToDiagDir(v->direction) == dir) {
    01431       switch (dir) {
    01432         default: NOT_REACHED();
    01433         case DIAGDIR_NE: if ((x & 0xF) != 0)             return VETSB_CONTINUE; break;
    01434         case DIAGDIR_SE: if ((y & 0xF) != TILE_SIZE - 1) return VETSB_CONTINUE; break;
    01435         case DIAGDIR_SW: if ((x & 0xF) != TILE_SIZE - 1) return VETSB_CONTINUE; break;
    01436         case DIAGDIR_NW: if ((y & 0xF) != 0)             return VETSB_CONTINUE; break;
    01437       }
    01438       switch (v->type) {
    01439         case VEH_TRAIN: {
    01440           Train *t = Train::From(v);
    01441           t->track = TRACK_BIT_WORMHOLE;
    01442           ClrBit(t->flags, VRF_GOINGUP);
    01443           ClrBit(t->flags, VRF_GOINGDOWN);
    01444         } break;
    01445
    01446         case VEH_ROAD:
    01447           RoadVehicle::From(v)->state = RVSB_WORMHOLE;
    01448           break;
    01449
    01450         case VEH_SHIP:
    01451           Ship::From(v)->state = TRACK_BIT_WORMHOLE;
    01452           break;
    01453
    01454         default: NOT_REACHED();
    01455       }
    01456       return VETSB_ENTERED_WORMHOLE;
    01457     } else if (DirToDiagDir(v->direction) == ReverseDiagDir(dir)) {
    01458       v->tile = tile;
    01459       switch (v->type) {
    01460         case VEH_TRAIN: {
    01461           Train *t = Train::From(v);
    01462           if (t->track == TRACK_BIT_WORMHOLE) {
    01463             t->track = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
    01464             return VETSB_ENTERED_WORMHOLE;
    01465           }
    01466         } break;
    01467
    01468         case VEH_ROAD: {
    01469           RoadVehicle *rv = RoadVehicle::From(v);
    01470           if (rv->state == RVSB_WORMHOLE) {
    01471             rv->state = _road_exit_tunnel_state[dir];
    01472             rv->frame = 0;
    01473             return VETSB_ENTERED_WORMHOLE;
    01474           }
    01475         } break;
    01476
    01477         case VEH_SHIP: {
    01478           Ship *ship = Ship::From(v);
    01479           if (ship->state == TRACK_BIT_WORMHOLE) {
    01480             ship->state = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
    01481             return VETSB_ENTERED_WORMHOLE;
    01482           }
    01483         } break;
    01484
    01485         default: NOT_REACHED();
    01486       }
    01487     }
    01488   }
    01489   return VETSB_CONTINUE;
    01490 }
    01491
    01492 static CommandCost TerraformTile_TunnelBridge(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    01493 {
    01494   if (_settings_game.construction.build_on_slopes && AutoslopeEnabled() && IsBridge(tile) && GetTunnelBridgeTransportType(tile) != TRANSPORT_WATER) {
    01495     DiagDirection direction = GetTunnelBridgeDirection(tile);
    01496     Axis axis = DiagDirToAxis(direction);
    01497     CommandCost res;
    01498     uint z_old;
    01499     Slope tileh_old = GetTileSlope(tile, &z_old);
    01500
    01501     /* Check if new slope is valid for bridges in general (so we can safely call GetBridgeFoundation()) */
    01502     if ((direction == DIAGDIR_NW) || (direction == DIAGDIR_NE)) {
    01503       CheckBridgeSlopeSouth(axis, &tileh_old, &z_old);
    01504       res = CheckBridgeSlopeSouth(axis, &tileh_new, &z_new);
    01505     } else {
    01506       CheckBridgeSlopeNorth(axis, &tileh_old, &z_old);
    01507       res = CheckBridgeSlopeNorth(axis, &tileh_new, &z_new);
    01508     }
    01509
    01510     /* Surface slope is valid and remains unchanged? */
    01511     if (!CmdFailed(res) && (z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    01512   }
    01513
    01514   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    01515 }
    01516
    01517 extern const TileTypeProcs _tile_type_tunnelbridge_procs = {
    01518   DrawTile_TunnelBridge,           // draw_tile_proc
    01519   GetSlopeZ_TunnelBridge,          // get_slope_z_proc
    01520   ClearTile_TunnelBridge,          // clear_tile_proc
    01521   NULL,                            // add_accepted_cargo_proc
    01522   GetTileDesc_TunnelBridge,        // get_tile_desc_proc
    01523   GetTileTrackStatus_TunnelBridge, // get_tile_track_status_proc
    01524   NULL,                            // click_tile_proc
    01525   NULL,                            // animate_tile_proc
    01526   TileLoop_TunnelBridge,           // tile_loop_clear
    01527   ChangeTileOwner_TunnelBridge,    // change_tile_owner_clear
    01528   NULL,                            // add_produced_cargo_proc
    01529   VehicleEnter_TunnelBridge,       // vehicle_enter_tile_proc
    01530   GetFoundation_TunnelBridge,      // get_foundation_proc
    01531   TerraformTile_TunnelBridge,      // terraform_tile_proc
    01532 };