tron@2186: /* $Id$ */ tron@2186: belugas@6443: /** @file tunnelbridge_cmd.cpp celestar@2262: * This file deals with tunnels and bridges (non-gui stuff) celestar@2262: * @todo seperate this file into two celestar@2262: */ celestar@2262: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3189: #include "bridge_map.h" tron@3187: #include "rail_map.h" tron@3144: #include "road_map.h" rubidium@6872: #include "tile_cmd.h" maedhros@6669: #include "landscape.h" tron@3154: #include "tunnel_map.h" celestar@5573: #include "unmovable_map.h" rubidium@6872: #include "viewport_func.h" rubidium@6872: #include "command_func.h" truelight@0: #include "town.h" tron@2159: #include "variables.h" celestar@2262: #include "bridge.h" bjarni@2676: #include "train.h" tron@3187: #include "water_map.h" KUDr@3900: #include "yapf/yapf.h" peter1138@4656: #include "newgrf_sound.h" rubidium@6870: #include "autoslope.h" rubidium@6871: #include "transparency.h" rubidium@6872: #include "tunnelbridge_map.h" rubidium@6872: #include "strings_func.h" rubidium@6872: #include "date_func.h" rubidium@6872: #include "functions.h" rubidium@6872: #include "vehicle_func.h" rubidium@6872: #include "sound_func.h" rubidium@6872: #include "signal_func.h" rubidium@6877: #include "tunnelbridge.h" richk@6878: #include "player_base.h" richk@10184: #include "engine_func.h" richk@10724: #include "engine_base.h" richk@10242: #include "economy_func.h" richk@10242: #include "rail.h" richk@10242: #include "cheat_func.h" richk@10724: #include "elrail_func.h" richk@10724: #include "landscape_type.h" truelight@0: rubidium@6872: #include "table/sprites.h" rubidium@6872: #include "table/strings.h" ludde@2261: #include "table/bridge_land.h" ludde@2261: richk@6878: BridgeSpec _bridge[MAX_BRIDGES]; richk@10242: TileIndex _build_tunnel_endtile; rubidium@6877: richk@6878: /** Reset the data been eventually changed by the grf loaded. */ richk@6878: void ResetBridges() richk@6878: { richk@6878: /* First, free sprite table data */ richk@6878: for (BridgeType i = 0; i < MAX_BRIDGES; i++) { richk@6878: if (_bridge[i].sprite_table != NULL) { richk@6878: for (uint j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]); richk@6878: free(_bridge[i].sprite_table); richk@6878: } richk@6878: } rubidium@6877: richk@6878: /* Then, wipe out current bidges */ richk@6878: memset(&_bridge, 0, sizeof(_bridge)); richk@6878: /* And finally, reinstall default data */ richk@6878: memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge)); richk@6878: } tron@2763: richk@6719: /** calculate the price factor for building a long bridge. richk@6719: * basically the cost delta is 1,1, 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5, 6,6,6,6,6,6, 7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8, richk@6719: */ truelight@0: int CalcBridgeLenCostFactor(int x) truelight@0: { tron@2639: int n; tron@2639: int r; tron@2639: truelight@0: if (x < 2) return x; truelight@0: x -= 2; tron@2639: for (n = 0, r = 2;; n++) { truelight@0: if (x <= n) return r + x * n; truelight@0: r += n * n; truelight@0: x -= n; truelight@0: } truelight@0: } truelight@0: rubidium@6872: Foundation GetBridgeFoundation(Slope tileh, Axis axis) rubidium@6872: { rubidium@6872: if ((tileh == SLOPE_FLAT) || rubidium@6872: (((tileh == SLOPE_NE) || (tileh == SLOPE_SW)) && (axis == AXIS_X)) || rubidium@6872: (((tileh == SLOPE_NW) || (tileh == SLOPE_SE)) && (axis == AXIS_Y))) return FOUNDATION_NONE; rubidium@6872: rubidium@6872: return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh)); rubidium@6872: } rubidium@6872: rubidium@6872: /** rubidium@6872: * Determines if the track on a bridge ramp is flat or goes up/down. rubidium@6872: * rubidium@6872: * @param tileh Slope of the tile under the bridge head rubidium@6872: * @param axis Orientation of bridge rubidium@6872: * @return true iff the track is flat. rubidium@6872: */ rubidium@6872: bool HasBridgeFlatRamp(Slope tileh, Axis axis) rubidium@6872: { rubidium@6872: ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh); rubidium@6872: /* If the foundation slope is flat the bridge has a non-flat ramp and vice versa. */ rubidium@6872: return (tileh != SLOPE_FLAT); rubidium@6872: } truelight@0: peter1138@2478: static inline const PalSpriteID *GetBridgeSpriteTable(int index, byte table) peter1138@2478: { richk@6878: const BridgeSpec *bridge = GetBridgeSpec(index); peter1138@2478: assert(table < 7); peter1138@2478: if (bridge->sprite_table == NULL || bridge->sprite_table[table] == NULL) { peter1138@2478: return _bridge_sprite_table[index][table]; peter1138@2478: } else { peter1138@2478: return bridge->sprite_table[table]; peter1138@2478: } peter1138@2478: } peter1138@2478: truelight@0: rubidium@6872: /** rubidium@6872: * Determines the foundation for the north bridge head, and tests if the resulting slope is valid. rubidium@6872: * rubidium@6872: * @param axis Axis of the bridge rubidium@6872: * @param tileh Slope of the tile under the north bridge head; returns slope on top of foundation rubidium@6872: * @param z TileZ corresponding to tileh, gets modified as well rubidium@6872: * @return Error or cost for bridge foundation tron@4239: */ rubidium@6872: static CommandCost CheckBridgeSlopeNorth(Axis axis, Slope *tileh, uint *z) tron@4239: { rubidium@6872: Foundation f = GetBridgeFoundation(*tileh, axis); rubidium@6872: *z += ApplyFoundationToSlope(f, tileh); truelight@0: rubidium@6872: Slope valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW); rubidium@6872: if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR; tron@2639: rubidium@6872: if (f == FOUNDATION_NONE) return CommandCost(); rubidium@6872: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); truelight@0: } truelight@0: rubidium@6872: /** rubidium@6872: * Determines the foundation for the south bridge head, and tests if the resulting slope is valid. rubidium@6872: * rubidium@6872: * @param axis Axis of the bridge rubidium@6872: * @param tileh Slope of the tile under the south bridge head; returns slope on top of foundation rubidium@6872: * @param z TileZ corresponding to tileh, gets modified as well rubidium@6872: * @return Error or cost for bridge foundation rubidium@6872: */ rubidium@6872: static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, uint *z) tron@4239: { rubidium@6872: Foundation f = GetBridgeFoundation(*tileh, axis); rubidium@6872: *z += ApplyFoundationToSlope(f, tileh); tron@4239: rubidium@6872: Slope valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE); rubidium@6872: if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR; tron@4239: rubidium@6872: if (f == FOUNDATION_NONE) return CommandCost(); rubidium@6872: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); tron@4239: } tron@4239: richk@6878: bool CheckBridge_Stuff(BridgeType bridge_type, uint bridge_len) truelight@0: { richk@6878: const BridgeSpec *b = GetBridgeSpec(bridge_type); tron@2639: uint max; // max possible length of a bridge (with patch 100) truelight@0: Darkvater@1781: if (bridge_type >= MAX_BRIDGES) return false; celestar@2262: if (b->avail_year > _cur_year) return false; truelight@0: celestar@2262: max = b->max_length; richk@10991: if (max >= 16 && _settings_game.construction.longbridges) max = 100; truelight@0: tron@2639: return b->min_length <= bridge_len && bridge_len <= max; truelight@0: } truelight@0: Darkvater@1775: /** Build a Bridge tron@3491: * @param end_tile end tile richk@6719: * @param flags type of operation Darkvater@1775: * @param p1 packed start tile coords (~ dx) Darkvater@1775: * @param p2 various bitstuffed elements Darkvater@1775: * - p2 = (bit 0- 7) - bridge type (hi bh) richk@6719: * - p2 = (bit 8-..) - rail type or road types. richk@6719: * - p2 = (bit 15 ) - set means road bridge. truelight@0: */ richk@6720: CommandCost CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { richk@6878: BridgeType bridge_type; richk@6878: RailType railtype = INVALID_RAILTYPE; richk@6878: RoadTypes roadtypes = ROADTYPES_NONE; celestar@3853: uint x; celestar@3853: uint y; celestar@3853: uint sx; celestar@3853: uint sy; tron@3301: TileIndex tile_start; tron@3301: TileIndex tile_end; tron@3636: Slope tileh_start; tron@3636: Slope tileh_end; tron@3301: uint z_start; tron@3301: uint z_end; tron@3178: TileIndex tile; tron@3178: TileIndexDiff delta; tron@2639: uint bridge_len; tron@3157: Axis direction; rubidium@6872: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@6872: CommandCost ret; maedhros@6181: bool replace_bridge = false; richk@6878: BridgeType replaced_bridge_type; richk@6878: TransportType transport_type; truelight@0: truelight@0: /* unpack parameters */ tron@2140: bridge_type = GB(p2, 0, 8); truelight@193: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1775: richk@6878: transport_type = (TransportType)GB(p2, 15, 2); richk@6878: richk@6719: /* type of bridge */ richk@6878: switch (transport_type) { richk@6878: case TRANSPORT_ROAD: richk@6878: roadtypes = (RoadTypes)GB(p2, 8, 3); richk@6878: if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_player, roadtypes)) return CMD_ERROR; richk@6878: break; richk@6878: richk@6878: case TRANSPORT_RAIL: richk@6878: railtype = (RailType)GB(p2, 8, 8); richk@6878: if (!ValParamRailtype(railtype)) return CMD_ERROR; richk@6878: break; richk@6878: richk@10994: case TRANSPORT_WATER: richk@10994: break; richk@10994: richk@6878: default: richk@10994: /* Airports don't have tunnels. */ richk@6878: return CMD_ERROR; truelight@0: } truelight@0: tron@3493: x = TileX(end_tile); tron@3493: y = TileY(end_tile); tron@3493: sx = TileX(p1); tron@3493: sy = TileY(p1); truelight@0: truelight@0: /* check if valid, and make sure that (x,y) are smaller than (sx,sy) */ truelight@0: if (x == sx) { tron@2639: if (y == sy) return_cmd_error(STR_5008_CANNOT_START_AND_END_ON); tron@3157: direction = AXIS_Y; tron@6432: if (y > sy) Swap(y, sy); truelight@0: } else if (y == sy) { tron@3157: direction = AXIS_X; tron@6432: if (x > sx) Swap(x, sx); tron@2639: } else { truelight@0: return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN); tron@2639: } truelight@0: tron@4000: bridge_len = sx + sy - x - y - 1; richk@10994: if (transport_type != TRANSPORT_WATER) { richk@10994: /* set and test bridge length, availability */ richk@10994: if (!CheckBridge_Stuff(bridge_type, bridge_len)) return_cmd_error(STR_5015_CAN_T_BUILD_BRIDGE_HERE); richk@10994: } Darkvater@1781: truelight@0: /* retrieve landscape height and ensure it's on land */ tron@3493: tile_start = TileXY(x, y); tron@3493: tile_end = TileXY(sx, sy); rubidium@6871: if (IsWaterTile(tile_start) || IsWaterTile(tile_end)) { tron@4000: return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH); tron@4000: } truelight@0: tron@3301: tileh_start = GetTileSlope(tile_start, &z_start); tron@3301: tileh_end = GetTileSlope(tile_end, &z_end); tron@3301: rubidium@6872: CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start); richk@6878: CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end, &z_end); truelight@0: rubidium@6870: if (z_start != z_end) return_cmd_error(STR_BRIDGEHEADS_NOT_SAME_HEIGHT); pasky@1585: maedhros@6181: if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) && maedhros@6181: GetOtherBridgeEnd(tile_start) == tile_end && rubidium@6872: GetTunnelBridgeTransportType(tile_start) == transport_type) { maedhros@6181: /* Replace a current bridge. */ truelight@193: maedhros@6181: /* If this is a railway bridge, make sure the railtypes match. */ maedhros@6181: if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) { maedhros@6181: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); maedhros@6181: } truelight@0: maedhros@6181: /* Do not replace town bridges with lower speed bridges. */ maedhros@6181: if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) && richk@6878: GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed) { maedhros@6181: Town *t = ClosestTownFromTile(tile_start, UINT_MAX); maedhros@6181: maedhros@6181: if (t == NULL) { maedhros@6181: return CMD_ERROR; maedhros@6181: } else { maedhros@6181: SetDParam(0, t->index); maedhros@6181: return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES); maedhros@6181: } maedhros@6181: } maedhros@6181: maedhros@6181: /* Do not replace the bridge with the same bridge type. */ maedhros@6181: if (!(flags & DC_QUERY_COST) && bridge_type == GetBridgeType(tile_start)) { maedhros@6181: return_cmd_error(STR_1007_ALREADY_BUILT); maedhros@6181: } maedhros@6181: maedhros@6181: /* Do not allow replacing another player's bridges. */ maedhros@6181: if (!IsTileOwner(tile_start, _current_player) && !IsTileOwner(tile_start, OWNER_TOWN)) { maedhros@6181: return_cmd_error(STR_1024_AREA_IS_OWNED_BY_ANOTHER); maedhros@6181: } maedhros@6181: richk@6720: cost.AddCost((bridge_len + 1) * _price.clear_bridge); // The cost of clearing the current bridge. maedhros@6181: replace_bridge = true; maedhros@6181: replaced_bridge_type = GetBridgeType(tile_start); richk@6719: richk@6719: /* Do not remove road types when upgrading a bridge */ richk@6719: roadtypes |= GetRoadTypes(tile_start); maedhros@6181: } else { maedhros@6181: /* Build a new bridge. */ maedhros@6181: richk@10994: bool allow_on_slopes = (!_is_old_ai_player && _settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER); rubidium@6872: maedhros@6181: /* Try and clear the start landscape */ maedhros@6181: ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR); maedhros@6181: if (CmdFailed(ret)) return ret; maedhros@6181: cost = ret; maedhros@6181: rubidium@6872: if (CmdFailed(terraform_cost_north) || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes)) maedhros@6181: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); rubidium@6872: cost.AddCost(terraform_cost_north); maedhros@6181: maedhros@6181: /* Try and clear the end landscape */ maedhros@6181: ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR); maedhros@6181: if (CmdFailed(ret)) return ret; richk@6720: cost.AddCost(ret); maedhros@6181: richk@6719: /* false - end tile slope check */ rubidium@6872: if (CmdFailed(terraform_cost_south) || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes)) maedhros@6181: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); rubidium@6872: cost.AddCost(terraform_cost_south); richk@10994: richk@10994: if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); maedhros@6181: } maedhros@6181: maedhros@6181: if (!replace_bridge) { celestar@5573: TileIndex Heads[] = {tile_start, tile_end}; celestar@5573: int i; celestar@5573: celestar@5573: for (i = 0; i < 2; i++) { celestar@5573: if (MayHaveBridgeAbove(Heads[i])) { celestar@5573: if (IsBridgeAbove(Heads[i])) { celestar@5573: TileIndex north_head = GetNorthernBridgeEnd(Heads[i]); celestar@5573: celestar@5573: if (direction == GetBridgeAxis(Heads[i])) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5573: celestar@5573: if (z_start + TILE_HEIGHT == GetBridgeHeight(north_head)) { celestar@5573: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5573: } celestar@5573: } celestar@5573: } celestar@5573: } celestar@5573: } truelight@0: truelight@0: /* do the drill? */ truelight@0: if (flags & DC_EXEC) { tron@3209: DiagDirection dir = AxisToDiagDir(direction); maedhros@6181: Owner owner = (replace_bridge && IsTileOwner(tile_start, OWNER_TOWN)) ? OWNER_TOWN : _current_player; truelight@0: richk@6878: switch (transport_type) { richk@6878: case TRANSPORT_RAIL: richk@6878: MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype); richk@6878: MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype); richk@6878: break; richk@6878: richk@6878: case TRANSPORT_ROAD: richk@6878: MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir, roadtypes); richk@6878: MakeRoadBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), roadtypes); richk@6878: break; richk@6878: richk@10994: case TRANSPORT_WATER: richk@10994: MakeAqueductBridgeRamp(tile_start, owner, dir); richk@10994: MakeAqueductBridgeRamp(tile_end, owner, ReverseDiagDir(dir)); richk@10994: break; richk@10994: richk@6878: default: richk@6878: NOT_REACHED(); richk@6878: break; tron@3209: } tron@3301: MarkTileDirtyByTile(tile_start); tron@3301: MarkTileDirtyByTile(tile_end); truelight@0: } truelight@0: tron@3178: delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); celestar@5573: for (tile = tile_start + delta; tile != tile_end; tile += delta) { rubidium@6872: if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_BRIDGE_TOO_LOW_FOR_TERRAIN); truelight@0: maedhros@6181: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) && !replace_bridge) { celestar@5573: /* Disallow crossing bridges for the time being */ celestar@5573: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); tron@3183: } truelight@0: tron@3178: switch (GetTileType(tile)) { tron@3065: case MP_WATER: rubidium@6871: if (!EnsureNoVehicleOnGround(tile)) return_cmd_error(STR_980E_SHIP_IN_THE_WAY); rubidium@6877: if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below; tron@3065: break; truelight@0: tron@3065: case MP_RAILWAY: celestar@5573: if (!IsPlainRailTile(tile)) goto not_valid_below; tron@3065: break; tron@3065: richk@6743: case MP_ROAD: richk@6878: if (IsRoadDepot(tile)) goto not_valid_below; celestar@5573: break; celestar@5573: celestar@5573: case MP_TUNNELBRIDGE: celestar@5573: if (IsTunnel(tile)) break; maedhros@6181: if (replace_bridge) break; rubidium@6872: if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below; celestar@5573: if (z_start < GetBridgeHeight(tile)) goto not_valid_below; celestar@5573: break; celestar@5573: celestar@5573: case MP_UNMOVABLE: celestar@5573: if (!IsOwnedLand(tile)) goto not_valid_below; celestar@5573: break; celestar@5573: celestar@5573: case MP_CLEAR: tron@3065: break; tron@3065: tron@3065: default: truelight@0: not_valid_below:; tron@3065: /* try and clear the middle landscape */ tron@3491: ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3183: if (CmdFailed(ret)) return ret; richk@6720: cost.AddCost(ret); tron@3065: break; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@5573: SetBridgeMiddle(tile, direction); tron@3178: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: richk@6878: if (flags & DC_EXEC && transport_type == TRANSPORT_RAIL) { Darkvater@5812: Track track = AxisToTrack(direction); rubidium@6872: AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, _current_player); Darkvater@5812: YapfNotifyTrackLayoutChange(tile_start, track); Darkvater@5805: } truelight@0: rubidium@4434: /* for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST) rubidium@4434: * It's unnecessary to execute this command every time for every bridge. So it is done only rubidium@4434: * and cost is computed in "bridge_gui.c". For AI, Towns this has to be of course calculated rubidium@4434: */ richk@6878: if (!(flags & DC_QUERY_COST) || (IsValidPlayer(_current_player) && GetPlayer(_current_player)->is_ai)) { rubidium@4434: bridge_len += 2; // begin and end tiles/ramps truelight@0: Darkvater@4850: if (IsValidPlayer(_current_player) && !_is_old_ai_player) truelight@0: bridge_len = CalcBridgeLenCostFactor(bridge_len); truelight@0: richk@6878: cost.AddCost((int64)bridge_len * _price.build_bridge * GetBridgeSpec(bridge_type)->price >> 8); richk@10994: richk@10994: /* Aqueducts are a little more expensive. */ richk@10994: if (transport_type == TRANSPORT_WATER) cost.AddCost((int64)bridge_len * _price.clear_water); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: Darkvater@1782: /** Build Tunnel. richk@6719: * @param start_tile start tile of tunnel richk@6719: * @param flags type of operation richk@6719: * @param p1 railtype or roadtypes. bit 9 set means road tunnel tron@2639: * @param p2 unused truelight@0: */ richk@6720: CommandCost CmdBuildTunnel(TileIndex start_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@3063: TileIndexDiff delta; tron@3063: TileIndex end_tile; tron@3063: DiagDirection direction; tron@3636: Slope start_tileh; tron@3636: Slope end_tileh; richk@6878: TransportType transport_type = (TransportType)GB(p1, 9, 1); tron@3063: uint start_z; tron@3063: uint end_z; rubidium@6872: CommandCost cost(EXPENSES_CONSTRUCTION); richk@6720: CommandCost ret; truelight@0: tron@3063: _build_tunnel_endtile = 0; richk@6878: if (transport_type == TRANSPORT_RAIL) { rubidium@6872: if (!ValParamRailtype((RailType)p1)) return CMD_ERROR; rubidium@6871: } else { rubidium@6871: const RoadTypes rts = (RoadTypes)GB(p1, 0, 3); rubidium@6871: if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_player, rts)) return CMD_ERROR; richk@6719: } Darkvater@1782: tron@3063: start_tileh = GetTileSlope(start_tile, &start_z); rubidium@6877: direction = GetInclinedSlopeDirection(start_tileh); rubidium@6877: if (direction == INVALID_DIAGDIR) return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL); truelight@0: rubidium@6877: if (IsWaterTile(start_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); truelight@0: tron@3491: ret = DoCommand(start_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3063: if (CmdFailed(ret)) return ret; truelight@0: Darkvater@4273: /* XXX - do NOT change 'ret' in the loop, as it is used as the price Darkvater@4273: * for the clearing of the entrance of the tunnel. Assigning it to Darkvater@4273: * cost before the loop will yield different costs depending on start- Darkvater@4273: * position, because of increased-cost-by-length: 'cost += cost >> 3' */ richk@6720: Darkvater@4559: delta = TileOffsByDiagDir(direction); richk@6720: DiagDirection tunnel_in_way_dir; richk@10724: if (DiagDirToAxis(direction) == AXIS_Y) { richk@6720: tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE; richk@6720: } else { richk@6720: tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW; richk@6720: } richk@6720: tron@3063: end_tile = start_tile; richk@6720: richk@6720: /** Tile shift coeficient. Will decrease for very long tunnels to avoid exponential growth of price*/ richk@6720: int tiles_coef = 3; richk@6720: /** Number of tiles from start of tunnel */ richk@6720: int tiles = 0; richk@6743: /** Number of tiles at which the cost increase coefficient per tile is halved */ richk@6743: int tiles_bump = 25; richk@6720: tron@3063: for (;;) { tron@3063: end_tile += delta; tron@3063: end_tileh = GetTileSlope(end_tile, &end_z); truelight@0: tron@3063: if (start_z == end_z) break; tron@3063: richk@6720: if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) { tron@3156: return_cmd_error(STR_5003_ANOTHER_TUNNEL_IN_THE_WAY); tron@3063: } tron@3063: richk@6720: tiles++; richk@6743: if (tiles == tiles_bump) { richk@6743: tiles_coef++; richk@6743: tiles_bump *= 2; richk@6743: } richk@6720: richk@6720: cost.AddCost(_price.build_tunnel); richk@6720: cost.AddCost(cost.GetCost() >> tiles_coef); // add a multiplier for longer tunnels truelight@0: } truelight@0: Darkvater@4273: /* Add the cost of the entrance */ richk@6720: cost.AddCost(_price.build_tunnel); richk@6720: cost.AddCost(ret); Darkvater@4273: richk@6719: /* if the command fails from here on we want the end tile to be highlighted */ tron@3063: _build_tunnel_endtile = end_tile; tron@3063: rubidium@6877: if (IsWaterTile(end_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); rubidium@6877: richk@6719: /* slope of end tile must be complementary to the slope of the start tile */ tron@3636: if (end_tileh != ComplementSlope(start_tileh)) { rubidium@6870: /* Check if there is a structure on the terraformed tile. Do not add the cost, that will be done by the terraforming */ rubidium@6870: ret = DoCommand(end_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR); rubidium@6870: if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND); richk@6720: tron@3491: ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND); tron@3063: if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND); tron@3063: } else { tron@3491: ret = DoCommand(end_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3063: if (CmdFailed(ret)) return ret; tron@3063: } richk@6720: cost.AddCost(_price.build_tunnel); richk@6720: cost.AddCost(ret); tron@3063: tron@3063: if (flags & DC_EXEC) { richk@6878: if (transport_type == TRANSPORT_RAIL) { rubidium@5838: MakeRailTunnel(start_tile, _current_player, direction, (RailType)GB(p1, 0, 4)); rubidium@5838: MakeRailTunnel(end_tile, _current_player, ReverseDiagDir(direction), (RailType)GB(p1, 0, 4)); rubidium@6872: AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_player); richk@10724: YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction)); tron@3154: } else { richk@6719: MakeRoadTunnel(start_tile, _current_player, direction, (RoadTypes)GB(p1, 0, 3)); richk@6719: MakeRoadTunnel(end_tile, _current_player, ReverseDiagDir(direction), (RoadTypes)GB(p1, 0, 3)); tron@3154: } tron@3063: } tron@3063: tron@3063: return cost; truelight@0: } truelight@0: truelight@0: Darkvater@5009: static inline bool CheckAllowRemoveTunnelBridge(TileIndex tile) Darkvater@5009: { Darkvater@5009: /* Floods can remove anything as well as the scenario editor */ Darkvater@5009: if (_current_player == OWNER_WATER || _game_mode == GM_EDITOR) return true; Darkvater@5009: /* Obviously if the bridge/tunnel belongs to us, or no-one, we can remove it */ Darkvater@5009: if (CheckTileOwnership(tile) || IsTileOwner(tile, OWNER_NONE)) return true; Darkvater@5009: /* Otherwise we can only remove town-owned stuff with extra patch-settings, or cheat */ richk@10991: if (IsTileOwner(tile, OWNER_TOWN) && (_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) return true; Darkvater@5009: return false; Darkvater@5009: } Darkvater@5009: richk@6720: static CommandCost DoClearTunnel(TileIndex tile, uint32 flags) truelight@0: { Darkvater@5009: Town *t = NULL; Darkvater@1782: TileIndex endtile; truelight@0: Darkvater@5009: if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR; truelight@0: rubidium@6872: endtile = GetOtherTunnelEnd(tile); rubidium@6872: rubidium@6872: if (GetVehicleTunnelBridge(tile, endtile) != NULL) return CMD_ERROR; truelight@0: truelight@0: _build_tunnel_endtile = endtile; truelight@193: tron@1901: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { Darkvater@5009: t = ClosestTownFromTile(tile, (uint)-1); // town penalty rating Darkvater@5009: Darkvater@5009: /* Check if you are allowed to remove the tunnel owned by a town Darkvater@5009: * Removal depends on difficulty settings */ tron@2958: if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) { tron@534: SetDParam(0, t->index); truelight@0: return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES); truelight@0: } truelight@0: } truelight@0: rubidium@6872: /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until rubidium@6872: * you have a "Poor" (0) town rating */ rubidium@6872: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { rubidium@6872: ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM); rubidium@6872: } peter1138@2870: rubidium@6872: if (flags & DC_EXEC) { rubidium@6872: if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { rubidium@6872: /* We first need to request values before calling DoClearSquare */ rubidium@6872: DiagDirection dir = GetTunnelBridgeDirection(tile); rubidium@6872: Owner owner = GetTileOwner(tile); peter1138@2870: rubidium@6872: DoClearSquare(tile); rubidium@6872: DoClearSquare(endtile); rubidium@6872: rubidium@6872: /* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */ richk@6878: AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner); richk@6878: AddSideToSignalBuffer(endtile, dir, owner); rubidium@6872: richk@10724: Track track = DiagDirToDiagTrack(dir); richk@6878: YapfNotifyTrackLayoutChange(tile, track); rubidium@6872: YapfNotifyTrackLayoutChange(endtile, track); rubidium@6872: } else { rubidium@6872: DoClearSquare(tile); rubidium@6872: DoClearSquare(endtile); rubidium@6872: } truelight@0: } rubidium@6877: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (GetTunnelBridgeLength(tile, endtile) + 2)); truelight@0: } truelight@0: tron@3977: richk@6720: static CommandCost DoClearBridge(TileIndex tile, uint32 flags) truelight@0: { tron@3227: DiagDirection direction; tron@3227: TileIndexDiff delta; tron@1977: TileIndex endtile; Darkvater@5009: Town *t = NULL; truelight@193: Darkvater@5009: if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR; truelight@0: tron@3228: endtile = GetOtherBridgeEnd(tile); tron@3228: rubidium@6872: if (GetVehicleTunnelBridge(tile, endtile) != NULL) return CMD_ERROR; truelight@0: rubidium@6872: direction = GetTunnelBridgeDirection(tile); Darkvater@4559: delta = TileOffsByDiagDir(direction); tron@3227: tron@1901: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { Darkvater@5009: t = ClosestTownFromTile(tile, (uint)-1); // town penalty rating Darkvater@5009: Darkvater@5009: /* Check if you are allowed to remove the bridge owned by a town Darkvater@5009: * Removal depends on difficulty settings */ Darkvater@5009: if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) { Darkvater@5009: SetDParam(0, t->index); Darkvater@5009: return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES); Darkvater@5009: } truelight@0: } truelight@0: rubidium@6872: /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until rubidium@6872: * you have a "Poor" (0) town rating */ rubidium@6872: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { rubidium@6872: ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM); rubidium@6872: } truelight@0: rubidium@6872: if (flags & DC_EXEC) { rubidium@6872: /* read this value before actual removal of bridge */ rubidium@6872: bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL; rubidium@6872: Owner owner = GetTileOwner(tile); richk@6878: uint height = GetBridgeHeight(tile); truelight@0: tron@3217: DoClearSquare(tile); tron@3217: DoClearSquare(endtile); rubidium@6872: for (TileIndex c = tile + delta; c != endtile; c += delta) { richk@6878: /* do not let trees appear from 'nowhere' after removing bridge */ richk@6878: if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) { richk@6878: uint minz = GetTileMaxZ(c) + 3 * TILE_HEIGHT; richk@6878: if (height < minz) SetRoadside(c, ROADSIDE_PAVED); richk@6878: } rubidium@6872: ClearBridgeMiddle(c); celestar@5573: MarkTileDirtyByTile(c); tron@3217: } truelight@0: rubidium@6872: if (rail) { rubidium@6872: /* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */ richk@6878: AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner); richk@6878: AddSideToSignalBuffer(endtile, direction, owner); rubidium@6872: richk@10724: Track track = DiagDirToDiagTrack(direction); richk@6878: YapfNotifyTrackLayoutChange(tile, track); rubidium@6872: YapfNotifyTrackLayoutChange(endtile, track); rubidium@6872: } truelight@0: } truelight@0: rubidium@6877: return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * _price.clear_bridge); truelight@0: } truelight@0: richk@6720: static CommandCost ClearTile_TunnelBridge(TileIndex tile, byte flags) tron@1977: { tron@3184: if (IsTunnel(tile)) { tron@2639: if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST); truelight@0: return DoClearTunnel(tile, flags); rubidium@6877: } else { // IsBridge(tile) tron@2639: if (flags & DC_AUTO) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); darkvater@1082: return DoClearBridge(tile, flags); tron@1109: } tron@1109: darkvater@1082: return CMD_ERROR; truelight@0: } truelight@0: celestar@6694: /** rubidium@6871: * Draws the pillars under high bridges. rubidium@6871: * rubidium@6871: * @param psid Image and palette of a bridge pillar. rubidium@6871: * @param ti #TileInfo of current bridge-middle-tile. rubidium@6871: * @param axis Orientation of bridge. rubidium@6871: * @param type Bridge type. rubidium@6871: * @param x Sprite X position of front pillar. rubidium@6871: * @param y Sprite Y position of front pillar. rubidium@6871: * @param z_bridge Absolute height of bridge bottom. rubidium@6871: */ richk@10994: static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo* ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge) truelight@0: { richk@10184: /* Do not draw bridge pillars if they are invisible */ richk@10184: if (IsInvisibilitySet(TO_BRIDGES)) return; richk@10184: peter1138@5919: SpriteID image = psid->sprite; richk@10184: truelight@0: if (image != 0) { rubidium@6871: /* "side" specifies the side the pillars stand on. rubidium@6871: * The length of the pillars is then set to the height of the bridge over the corners of this edge. rubidium@6871: * rubidium@6871: * axis==AXIS_X axis==AXIS_Y rubidium@6871: * side==false SW NW rubidium@6871: * side==true NE SE rubidium@6871: * rubidium@6871: * I have no clue, why this was done this way. rubidium@6871: */ rubidium@6871: bool side = HasBit(image, 0); celestar@2262: rubidium@6871: /* "dir" means the edge the pillars stand on */ rubidium@6871: DiagDirection dir = AxisToDiagDir(axis); rubidium@6871: if (side != (axis == AXIS_Y)) dir = ReverseDiagDir(dir); truelight@0: rubidium@6871: /* Determine ground height under pillars */ rubidium@6871: int front_height = ti->z; rubidium@6871: int back_height = ti->z; rubidium@6871: GetSlopeZOnEdge(ti->tileh, dir, &front_height, &back_height); rubidium@6871: rubidium@6871: /* x and y size of bounding-box of pillars */ rubidium@6871: int w = (axis == AXIS_X ? 16 : 2); rubidium@6871: int h = (axis == AXIS_X ? 2 : 16); rubidium@6871: /* sprite position of back facing pillar */ rubidium@6871: int x_back = x - (axis == AXIS_X ? 0 : 9); rubidium@6871: int y_back = y - (axis == AXIS_X ? 9 : 0); rubidium@6871: rubidium@6871: for (int cur_z = z_bridge; cur_z >= front_height || cur_z >= back_height; cur_z -= TILE_HEIGHT) { rubidium@6871: /* Draw front facing pillar */ rubidium@6871: if (cur_z >= front_height) { rubidium@6871: AddSortableSpriteToDraw(image, psid->pal, x, y, w, h, BB_HEIGHT_UNDER_BRIDGE - 5, cur_z, IsTransparencySet(TO_BRIDGES), 0, 0, -5); tron@2952: } Darkvater@3556: rubidium@6871: /* Draw back facing pillar, but not the highest part directly under the bridge-floor */ rubidium@6871: if (drawfarpillar && cur_z >= back_height && cur_z < z_bridge - TILE_HEIGHT) { rubidium@6871: AddSortableSpriteToDraw(image, psid->pal, x_back, y_back, w, h, BB_HEIGHT_UNDER_BRIDGE - 5, cur_z, IsTransparencySet(TO_BRIDGES), 0, 0, -5); tron@2952: } truelight@0: } truelight@0: } truelight@0: } truelight@0: celestar@2536: /** richk@6719: * Draws the trambits over an already drawn (lower end) of a bridge. richk@6719: * @param x the x of the bridge richk@6719: * @param y the y of the bridge richk@6719: * @param z the z of the bridge richk@6719: * @param offset number representing whether to level or sloped and the direction richk@6719: * @param overlay do we want to still see the road? richk@10184: * @param head are we drawing bridge head? richk@6719: */ richk@10184: static void DrawBridgeTramBits(int x, int y, byte z, int offset, bool overlay, bool head) richk@6719: { richk@6719: static const SpriteID tram_offsets[2][6] = { { 107, 108, 109, 110, 111, 112 }, { 4, 5, 15, 16, 17, 18 } }; richk@6720: static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 }; richk@6719: static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 }; richk@6719: rubidium@6870: static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 }; rubidium@6870: static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 }; rubidium@6870: static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 }; rubidium@6870: static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 }; richk@6719: rubidium@6870: /* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called rubidium@6870: * The bounding boxes here are the same as for bridge front/roof */ richk@10184: if (head || !IsInvisibilitySet(TO_BRIDGES)) { richk@10184: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE, richk@10184: x, y, size_x[offset], size_y[offset], 0x28, z, richk@10184: !head && IsTransparencySet(TO_BRIDGES)); richk@10184: } richk@6719: richk@10184: /* Do not draw catenary if it is set invisible */ richk@10184: if (!IsInvisibilitySet(TO_CATENARY)) { richk@10184: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset], PAL_NONE, richk@10184: x, y, size_x[offset], size_y[offset], 0x28, z, richk@10184: IsTransparencySet(TO_CATENARY)); richk@10184: } rubidium@6870: rubidium@6870: /* Start a new SpriteCombine for the front part */ rubidium@6870: EndSpriteCombine(); rubidium@6870: StartSpriteCombine(); rubidium@6870: richk@6719: /* For sloped sprites the bounding box needs to be higher, as the pylons stop on a higher point */ richk@10184: if (!IsInvisibilitySet(TO_CATENARY)) { richk@10184: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE, richk@10184: x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z, richk@10184: IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]); richk@10184: } richk@6719: } richk@6719: richk@6719: /** rubidium@4549: * Draws a tunnel of bridge tile. rubidium@4549: * For tunnels, this is rather simple, as you only needa draw the entrance. rubidium@4549: * Bridges are a bit more complex. base_offset is where the sprite selection comes into play rubidium@4549: * and it works a bit like a bitmask.
For bridge heads: richk@6719: * @param ti TileInfo of the structure to draw rubidium@4549: *