tron@2186: /* $Id$ */ tron@2186: belugas@6117: /** @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@8119: #include "tile_cmd.h" maedhros@6343: #include "landscape.h" tron@3154: #include "tunnel_map.h" celestar@5385: #include "unmovable_map.h" rubidium@8224: #include "viewport_func.h" rubidium@8116: #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@7582: #include "autoslope.h" belugas@7849: #include "transparency.h" smatz@8083: #include "tunnelbridge_map.h" rubidium@8114: #include "strings_func.h" rubidium@8140: #include "date_func.h" rubidium@8131: #include "functions.h" rubidium@8144: #include "vehicle_func.h" rubidium@8157: #include "sound_func.h" smatz@8238: #include "signal_func.h" smatz@8398: #include "tunnelbridge.h" smatz@8590: #include "player_base.h" rubidium@8786: #include "engine_func.h" peter1138@9070: #include "engine_base.h" rubidium@8962: #include "economy_func.h" rubidium@8962: #include "rail.h" rubidium@8965: #include "cheat_func.h" smatz@9154: #include "elrail_func.h" rubidium@9126: #include "landscape_type.h" truelight@0: rubidium@8264: #include "table/sprites.h" rubidium@8264: #include "table/strings.h" rubidium@8264: #include "table/bridge_land.h" ludde@2261: belugas@8535: BridgeSpec _bridge[MAX_BRIDGES]; rubidium@8973: TileIndex _build_tunnel_endtile; tron@2763: belugas@8489: /** Reset the data been eventually changed by the grf loaded. */ belugas@8489: void ResetBridges() belugas@8489: { belugas@8489: /* First, free sprite table data */ belugas@8532: for (BridgeType i = 0; i < MAX_BRIDGES; i++) { belugas@8489: if (_bridge[i].sprite_table != NULL) { belugas@9642: for (BridgePieces j = BRIDGE_PIECE_NORTH; j < BRIDGE_PIECE_INVALID; j++) free(_bridge[i].sprite_table[j]); belugas@8489: free(_bridge[i].sprite_table); belugas@8489: } belugas@8489: } belugas@8489: belugas@8489: /* Then, wipe out current bidges */ belugas@8489: memset(&_bridge, 0, sizeof(_bridge)); belugas@8489: /* And finally, reinstall default data */ belugas@8489: memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge)); belugas@8489: } tron@2763: belugas@6422: /** calculate the price factor for building a long bridge. belugas@6422: * 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, belugas@6422: */ 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: frosch@8371: Foundation GetBridgeFoundation(Slope tileh, Axis axis) frosch@8371: { frosch@8371: if ((tileh == SLOPE_FLAT) || frosch@8371: (((tileh == SLOPE_NE) || (tileh == SLOPE_SW)) && (axis == AXIS_X)) || frosch@8371: (((tileh == SLOPE_NW) || (tileh == SLOPE_SE)) && (axis == AXIS_Y))) return FOUNDATION_NONE; frosch@8371: frosch@8371: return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh)); frosch@8371: } truelight@0: frosch@8379: /** frosch@8379: * Determines if the track on a bridge ramp is flat or goes up/down. frosch@8379: * frosch@8379: * @param tileh Slope of the tile under the bridge head frosch@8379: * @param axis Orientation of bridge frosch@8379: * @return true iff the track is flat. frosch@8379: */ frosch@8379: bool HasBridgeFlatRamp(Slope tileh, Axis axis) frosch@8379: { frosch@8379: ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh); frosch@8379: /* If the foundation slope is flat the bridge has a non-flat ramp and vice versa. */ frosch@8379: return (tileh != SLOPE_FLAT); frosch@8379: } frosch@8379: belugas@9642: static inline const PalSpriteID *GetBridgeSpriteTable(int index, BridgePieces table) peter1138@2478: { belugas@8535: const BridgeSpec *bridge = GetBridgeSpec(index); belugas@9642: assert(table < BRIDGE_PIECE_INVALID); 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: frosch@8371: /** frosch@8371: * Determines the foundation for the north bridge head, and tests if the resulting slope is valid. frosch@8371: * frosch@8371: * @param axis Axis of the bridge frosch@8371: * @param tileh Slope of the tile under the north bridge head; returns slope on top of foundation frosch@8371: * @param z TileZ corresponding to tileh, gets modified as well frosch@8371: * @return Error or cost for bridge foundation tron@4239: */ frosch@8371: static CommandCost CheckBridgeSlopeNorth(Axis axis, Slope *tileh, uint *z) tron@4239: { frosch@8371: Foundation f = GetBridgeFoundation(*tileh, axis); frosch@8371: *z += ApplyFoundationToSlope(f, tileh); truelight@0: frosch@8371: Slope valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW); frosch@8371: if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR; tron@2639: frosch@8371: if (f == FOUNDATION_NONE) return CommandCost(); frosch@8371: frosch@8371: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); truelight@0: } truelight@0: frosch@8371: /** frosch@8371: * Determines the foundation for the south bridge head, and tests if the resulting slope is valid. frosch@8371: * frosch@8371: * @param axis Axis of the bridge frosch@8371: * @param tileh Slope of the tile under the south bridge head; returns slope on top of foundation frosch@8371: * @param z TileZ corresponding to tileh, gets modified as well frosch@8371: * @return Error or cost for bridge foundation frosch@8371: */ frosch@8371: static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, uint *z) tron@4239: { frosch@8371: Foundation f = GetBridgeFoundation(*tileh, axis); frosch@8371: *z += ApplyFoundationToSlope(f, tileh); tron@4239: frosch@8371: Slope valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE); frosch@8371: if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR; tron@4239: frosch@8371: if (f == FOUNDATION_NONE) return CommandCost(); frosch@8371: frosch@8371: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); tron@4239: } tron@4239: rubidium@9578: bool CheckBridge_Stuff(BridgeType bridge_type, uint bridge_len, uint32 flags) truelight@0: { rubidium@9578: if (flags & DC_QUERY_COST) { smatz@9596: return bridge_len <= (_settings_game.construction.longbridges ? 100U : 16U); rubidium@9578: } truelight@0: Darkvater@1781: if (bridge_type >= MAX_BRIDGES) return false; rubidium@9578: rubidium@9578: const BridgeSpec *b = GetBridgeSpec(bridge_type); celestar@2262: if (b->avail_year > _cur_year) return false; truelight@0: rubidium@9578: uint max = b->max_length; rubidium@9413: 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 belugas@6422: * @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) rubidium@6661: * - p2 = (bit 8-..) - rail type or road types. rubidium@6661: * - p2 = (bit 15 ) - set means road bridge. truelight@0: */ rubidium@6943: CommandCost CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { belugas@8532: BridgeType bridge_type; belugas@8530: RailType railtype = INVALID_RAILTYPE; belugas@8530: 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@8230: CommandCost cost(EXPENSES_CONSTRUCTION); frosch@8371: CommandCost ret; maedhros@5930: bool replace_bridge = false; belugas@8532: BridgeType replaced_bridge_type; belugas@8530: TransportType transport_type; truelight@0: truelight@0: /* unpack parameters */ tron@2140: bridge_type = GB(p2, 0, 8); truelight@193: belugas@8531: if (p1 >= MapSize()) return CMD_ERROR; belugas@8531: belugas@8530: transport_type = (TransportType)GB(p2, 15, 2); Darkvater@1775: belugas@6422: /* type of bridge */ belugas@8530: switch (transport_type) { belugas@8530: case TRANSPORT_ROAD: belugas@8530: roadtypes = (RoadTypes)GB(p2, 8, 3); belugas@8530: if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_player, roadtypes)) return CMD_ERROR; belugas@8530: break; belugas@8530: belugas@8530: case TRANSPORT_RAIL: belugas@8530: railtype = (RailType)GB(p2, 8, 8); belugas@8530: if (!ValParamRailtype(railtype)) return CMD_ERROR; belugas@8530: break; belugas@8531: rubidium@9490: case TRANSPORT_WATER: rubidium@9490: break; rubidium@9490: belugas@8531: default: rubidium@9490: /* Airports don't have tunnels. */ belugas@8531: 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@6106: if (y > sy) Swap(y, sy); truelight@0: } else if (y == sy) { tron@3157: direction = AXIS_X; tron@6106: 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; rubidium@9490: if (transport_type != TRANSPORT_WATER) { rubidium@9490: /* set and test bridge length, availability */ rubidium@9578: if (!CheckBridge_Stuff(bridge_type, bridge_len, flags)) return_cmd_error(STR_5015_CAN_T_BUILD_BRIDGE_HERE); rubidium@9490: } 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); peter1138@8471: 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: frosch@8371: CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start); belugas@8625: CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end, &z_end); truelight@0: rubidium@7682: if (z_start != z_end) return_cmd_error(STR_BRIDGEHEADS_NOT_SAME_HEIGHT); pasky@1585: maedhros@5930: if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) && maedhros@5930: GetOtherBridgeEnd(tile_start) == tile_end && smatz@8083: GetTunnelBridgeTransportType(tile_start) == transport_type) { maedhros@5930: /* Replace a current bridge. */ truelight@193: maedhros@5930: /* If this is a railway bridge, make sure the railtypes match. */ maedhros@5930: if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) { maedhros@5930: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); maedhros@5930: } truelight@0: maedhros@5930: /* Do not replace town bridges with lower speed bridges. */ maedhros@5930: if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) && belugas@8491: GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed) { maedhros@5930: Town *t = ClosestTownFromTile(tile_start, UINT_MAX); maedhros@5930: maedhros@5930: if (t == NULL) { maedhros@5930: return CMD_ERROR; maedhros@5930: } else { maedhros@5930: SetDParam(0, t->index); maedhros@5930: return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES); maedhros@5930: } maedhros@5930: } maedhros@5930: maedhros@5930: /* Do not replace the bridge with the same bridge type. */ maedhros@5930: if (!(flags & DC_QUERY_COST) && bridge_type == GetBridgeType(tile_start)) { maedhros@5930: return_cmd_error(STR_1007_ALREADY_BUILT); maedhros@5930: } maedhros@5930: maedhros@5930: /* Do not allow replacing another player's bridges. */ maedhros@5930: if (!IsTileOwner(tile_start, _current_player) && !IsTileOwner(tile_start, OWNER_TOWN)) { maedhros@5930: return_cmd_error(STR_1024_AREA_IS_OWNED_BY_ANOTHER); maedhros@5930: } maedhros@5930: rubidium@6950: cost.AddCost((bridge_len + 1) * _price.clear_bridge); // The cost of clearing the current bridge. maedhros@5930: replace_bridge = true; maedhros@5930: replaced_bridge_type = GetBridgeType(tile_start); rubidium@6708: rubidium@6708: /* Do not remove road types when upgrading a bridge */ rubidium@6708: roadtypes |= GetRoadTypes(tile_start); maedhros@5930: } else { maedhros@5930: /* Build a new bridge. */ maedhros@5930: rubidium@9491: bool allow_on_slopes = (!_is_old_ai_player && _settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER); frosch@8371: maedhros@5930: /* Try and clear the start landscape */ maedhros@5930: ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR); maedhros@5930: if (CmdFailed(ret)) return ret; maedhros@5930: cost = ret; maedhros@5930: frosch@8371: if (CmdFailed(terraform_cost_north) || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes)) maedhros@5930: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); frosch@8371: cost.AddCost(terraform_cost_north); maedhros@5930: maedhros@5930: /* Try and clear the end landscape */ maedhros@5930: ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR); maedhros@5930: if (CmdFailed(ret)) return ret; rubidium@6950: cost.AddCost(ret); maedhros@5930: belugas@6422: /* false - end tile slope check */ frosch@8371: if (CmdFailed(terraform_cost_south) || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes)) maedhros@5930: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); frosch@8371: cost.AddCost(terraform_cost_south); rubidium@9491: rubidium@9491: if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); maedhros@5930: } maedhros@5930: maedhros@5930: if (!replace_bridge) { celestar@5385: TileIndex Heads[] = {tile_start, tile_end}; celestar@5385: int i; celestar@5385: celestar@5385: for (i = 0; i < 2; i++) { celestar@5385: if (MayHaveBridgeAbove(Heads[i])) { celestar@5385: if (IsBridgeAbove(Heads[i])) { celestar@5385: TileIndex north_head = GetNorthernBridgeEnd(Heads[i]); celestar@5385: celestar@5385: if (direction == GetBridgeAxis(Heads[i])) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5385: celestar@5385: if (z_start + TILE_HEIGHT == GetBridgeHeight(north_head)) { celestar@5385: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5385: } celestar@5385: } celestar@5385: } celestar@5385: } celestar@5385: } truelight@0: truelight@0: /* do the drill? */ truelight@0: if (flags & DC_EXEC) { tron@3209: DiagDirection dir = AxisToDiagDir(direction); maedhros@5930: Owner owner = (replace_bridge && IsTileOwner(tile_start, OWNER_TOWN)) ? OWNER_TOWN : _current_player; truelight@0: belugas@8530: switch (transport_type) { belugas@8530: case TRANSPORT_RAIL: belugas@8625: MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype); belugas@8530: MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype); belugas@8530: break; belugas@8530: belugas@8530: case TRANSPORT_ROAD: belugas@8625: MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir, roadtypes); belugas@8530: MakeRoadBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), roadtypes); belugas@8530: break; belugas@8531: rubidium@9490: case TRANSPORT_WATER: rubidium@9490: MakeAqueductBridgeRamp(tile_start, owner, dir); rubidium@9490: MakeAqueductBridgeRamp(tile_end, owner, ReverseDiagDir(dir)); rubidium@9490: break; rubidium@9490: belugas@8531: default: belugas@8531: NOT_REACHED(); belugas@8531: 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@5385: for (tile = tile_start + delta; tile != tile_end; tile += delta) { frosch@8370: if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_BRIDGE_TOO_LOW_FOR_TERRAIN); truelight@0: maedhros@5930: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) && !replace_bridge) { celestar@5385: /* Disallow crossing bridges for the time being */ celestar@5385: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); tron@3183: } truelight@0: tron@3178: switch (GetTileType(tile)) { tron@3065: case MP_WATER: peter1138@8471: if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below; tron@3065: break; truelight@0: tron@3065: case MP_RAILWAY: celestar@5385: if (!IsPlainRailTile(tile)) goto not_valid_below; tron@3065: break; tron@3065: rubidium@7370: case MP_ROAD: frosch@8563: if (IsRoadDepot(tile)) goto not_valid_below; celestar@5385: break; celestar@5385: celestar@5385: case MP_TUNNELBRIDGE: celestar@5385: if (IsTunnel(tile)) break; maedhros@5930: if (replace_bridge) break; smatz@8083: if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below; celestar@5385: if (z_start < GetBridgeHeight(tile)) goto not_valid_below; celestar@5385: break; celestar@5385: celestar@5385: case MP_UNMOVABLE: celestar@5385: if (!IsOwnedLand(tile)) goto not_valid_below; celestar@5385: break; celestar@5385: celestar@5385: 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; rubidium@6950: cost.AddCost(ret); tron@3065: break; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@5385: SetBridgeMiddle(tile, direction); tron@3178: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: belugas@8530: if (flags & DC_EXEC && transport_type == TRANSPORT_RAIL) { Darkvater@5561: Track track = AxisToTrack(direction); smatz@8306: AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, _current_player); Darkvater@5561: YapfNotifyTrackLayoutChange(tile_start, track); Darkvater@5554: } 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: */ rubidium@9652: if (!(flags & DC_QUERY_COST) || (IsValidPlayerID(_current_player) && GetPlayer(_current_player)->is_ai)) { rubidium@4434: bridge_len += 2; // begin and end tiles/ramps truelight@0: rubidium@9652: if (IsValidPlayerID(_current_player) && !_is_old_ai_player) truelight@0: bridge_len = CalcBridgeLenCostFactor(bridge_len); truelight@0: belugas@8491: cost.AddCost((int64)bridge_len * _price.build_bridge * GetBridgeSpec(bridge_type)->price >> 8); rubidium@9492: rubidium@9492: /* Aqueducts are a little more expensive. */ rubidium@9492: 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. belugas@6484: * @param start_tile start tile of tunnel belugas@6422: * @param flags type of operation rubidium@6661: * @param p1 railtype or roadtypes. bit 9 set means road tunnel tron@2639: * @param p2 unused truelight@0: */ rubidium@6943: 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; belugas@8549: TransportType transport_type = (TransportType)GB(p1, 9, 1); tron@3063: uint start_z; tron@3063: uint end_z; rubidium@8230: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@6943: CommandCost ret; truelight@0: tron@3063: _build_tunnel_endtile = 0; belugas@8549: if (transport_type == TRANSPORT_RAIL) { rubidium@8236: if (!ValParamRailtype((RailType)p1)) return CMD_ERROR; truelight@7857: } else { truelight@7857: const RoadTypes rts = (RoadTypes)GB(p1, 0, 3); truelight@7857: if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_player, rts)) return CMD_ERROR; rubidium@6661: } Darkvater@1782: tron@3063: start_tileh = GetTileSlope(start_tile, &start_z); frosch@8413: direction = GetInclinedSlopeDirection(start_tileh); frosch@8413: if (direction == INVALID_DIAGDIR) return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL); truelight@0: peter1138@8471: if (IsWaterTile(start_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); peter1138@8385: 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' */ rubidium@6950: Darkvater@4559: delta = TileOffsByDiagDir(direction); rubidium@7002: DiagDirection tunnel_in_way_dir; smatz@9224: if (DiagDirToAxis(direction) == AXIS_Y) { rubidium@7002: tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE; rubidium@7002: } else { rubidium@7002: tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW; rubidium@7002: } rubidium@7002: tron@3063: end_tile = start_tile; rubidium@7095: rubidium@7095: /** Tile shift coeficient. Will decrease for very long tunnels to avoid exponential growth of price*/ rubidium@7095: int tiles_coef = 3; rubidium@7095: /** Number of tiles from start of tunnel */ rubidium@7095: int tiles = 0; truelight@7308: /** Number of tiles at which the cost increase coefficient per tile is halved */ truelight@7308: int tiles_bump = 25; rubidium@7095: 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: rubidium@7002: 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: rubidium@7095: tiles++; truelight@7308: if (tiles == tiles_bump) { truelight@7308: tiles_coef++; truelight@7308: tiles_bump *= 2; truelight@7308: } rubidium@7095: rubidium@6950: cost.AddCost(_price.build_tunnel); rubidium@7095: cost.AddCost(cost.GetCost() >> tiles_coef); // add a multiplier for longer tunnels truelight@0: } truelight@0: Darkvater@4273: /* Add the cost of the entrance */ rubidium@6950: cost.AddCost(_price.build_tunnel); rubidium@6950: cost.AddCost(ret); Darkvater@4273: belugas@6422: /* if the command fails from here on we want the end tile to be highlighted */ tron@3063: _build_tunnel_endtile = end_tile; tron@3063: peter1138@8471: if (IsWaterTile(end_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); peter1138@8385: belugas@6422: /* slope of end tile must be complementary to the slope of the start tile */ tron@3636: if (end_tileh != ComplementSlope(start_tileh)) { rubidium@7677: /* Check if there is a structure on the terraformed tile. Do not add the cost, that will be done by the terraforming */ rubidium@7677: ret = DoCommand(end_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR); rubidium@7677: if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND); rubidium@7042: rubidium@7677: ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND); rubidium@7677: 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: } rubidium@6950: cost.AddCost(_price.build_tunnel); rubidium@6950: cost.AddCost(ret); tron@3063: tron@3063: if (flags & DC_EXEC) { belugas@8549: if (transport_type == TRANSPORT_RAIL) { rubidium@5587: MakeRailTunnel(start_tile, _current_player, direction, (RailType)GB(p1, 0, 4)); rubidium@5587: MakeRailTunnel(end_tile, _current_player, ReverseDiagDir(direction), (RailType)GB(p1, 0, 4)); smatz@8306: AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_player); smatz@9224: YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction)); tron@3154: } else { rubidium@6663: MakeRoadTunnel(start_tile, _current_player, direction, (RoadTypes)GB(p1, 0, 3)); rubidium@6663: 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 */ rubidium@9413: if (IsTileOwner(tile, OWNER_TOWN) && (_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) return true; Darkvater@5009: return false; Darkvater@5009: } Darkvater@5009: rubidium@6943: 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: smatz@8072: endtile = GetOtherTunnelEnd(tile); smatz@8072: smatz@8072: 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: glx@8232: /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until glx@8232: * you have a "Poor" (0) town rating */ glx@8232: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { glx@8232: ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM); glx@8232: } glx@8232: truelight@0: if (flags & DC_EXEC) { smatz@8300: if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { smatz@8300: /* We first need to request values before calling DoClearSquare */ smatz@8300: DiagDirection dir = GetTunnelBridgeDirection(tile); smatz@8300: Owner owner = GetTileOwner(tile); peter1138@2870: smatz@8300: DoClearSquare(tile); smatz@8300: DoClearSquare(endtile); smatz@8297: smatz@8300: /* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */ belugas@8625: AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner); belugas@8625: AddSideToSignalBuffer(endtile, dir, owner); smatz@8297: smatz@9224: Track track = DiagDirToDiagTrack(dir); belugas@8625: YapfNotifyTrackLayoutChange(tile, track); smatz@8297: YapfNotifyTrackLayoutChange(endtile, track); smatz@8300: } else { smatz@8300: DoClearSquare(tile); smatz@8300: DoClearSquare(endtile); smatz@8297: } truelight@0: } smatz@8398: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (GetTunnelBridgeLength(tile, endtile) + 2)); truelight@0: } truelight@0: tron@3977: rubidium@6943: 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: smatz@8072: if (GetVehicleTunnelBridge(tile, endtile) != NULL) return CMD_ERROR; truelight@0: smatz@8083: 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: glx@8232: /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until glx@8232: * you have a "Poor" (0) town rating */ glx@8232: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { glx@8232: ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM); glx@8232: } glx@8232: truelight@0: if (flags & DC_EXEC) { smatz@8297: /* read this value before actual removal of bridge */ smatz@8297: bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL; smatz@8300: Owner owner = GetTileOwner(tile); smatz@8589: uint height = GetBridgeHeight(tile); truelight@0: tron@3217: DoClearSquare(tile); tron@3217: DoClearSquare(endtile); smatz@8297: for (TileIndex c = tile + delta; c != endtile; c += delta) { smatz@8589: /* do not let trees appear from 'nowhere' after removing bridge */ smatz@8589: if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) { smatz@8589: uint minz = GetTileMaxZ(c) + 3 * TILE_HEIGHT; smatz@8589: if (height < minz) SetRoadside(c, ROADSIDE_PAVED); smatz@8589: } smatz@8297: ClearBridgeMiddle(c); celestar@5385: MarkTileDirtyByTile(c); tron@3217: } truelight@0: smatz@8297: if (rail) { smatz@8300: /* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */ belugas@8625: AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner); belugas@8625: AddSideToSignalBuffer(endtile, direction, owner); smatz@8297: smatz@9224: Track track = DiagDirToDiagTrack(direction); belugas@8625: YapfNotifyTrackLayoutChange(tile, track); smatz@8297: YapfNotifyTrackLayoutChange(endtile, track); smatz@8297: } truelight@0: } truelight@0: smatz@8398: return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * _price.clear_bridge); truelight@0: } truelight@0: rubidium@6943: 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); smatz@8390: } 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@6368: /** rubidium@7728: * Draws the pillars under high bridges. rubidium@7728: * rubidium@7728: * @param psid Image and palette of a bridge pillar. rubidium@7728: * @param ti #TileInfo of current bridge-middle-tile. rubidium@7728: * @param axis Orientation of bridge. rubidium@7728: * @param type Bridge type. rubidium@7728: * @param x Sprite X position of front pillar. rubidium@7728: * @param y Sprite Y position of front pillar. rubidium@7728: * @param z_bridge Absolute height of bridge bottom. rubidium@7728: */ smatz@9498: static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo* ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge) truelight@0: { smatz@8806: /* Do not draw bridge pillars if they are invisible */ smatz@8806: if (IsInvisibilitySet(TO_BRIDGES)) return; smatz@8806: peter1138@5668: SpriteID image = psid->sprite; smatz@8806: truelight@0: if (image != 0) { rubidium@7728: /* "side" specifies the side the pillars stand on. rubidium@7728: * The length of the pillars is then set to the height of the bridge over the corners of this edge. rubidium@7728: * rubidium@7728: * axis==AXIS_X axis==AXIS_Y rubidium@7728: * side==false SW NW rubidium@7728: * side==true NE SE rubidium@7728: * rubidium@7728: * I have no clue, why this was done this way. rubidium@7728: */ skidd13@7928: bool side = HasBit(image, 0); celestar@2262: rubidium@7728: /* "dir" means the edge the pillars stand on */ rubidium@7728: DiagDirection dir = AxisToDiagDir(axis); rubidium@7728: if (side != (axis == AXIS_Y)) dir = ReverseDiagDir(dir); truelight@0: rubidium@7728: /* Determine ground height under pillars */ rubidium@7728: int front_height = ti->z; rubidium@7728: int back_height = ti->z; rubidium@7728: GetSlopeZOnEdge(ti->tileh, dir, &front_height, &back_height); rubidium@7728: rubidium@7728: /* x and y size of bounding-box of pillars */ rubidium@7728: int w = (axis == AXIS_X ? 16 : 2); rubidium@7728: int h = (axis == AXIS_X ? 2 : 16); rubidium@7728: /* sprite position of back facing pillar */ rubidium@7728: int x_back = x - (axis == AXIS_X ? 0 : 9); rubidium@7728: int y_back = y - (axis == AXIS_X ? 9 : 0); rubidium@7728: rubidium@7728: for (int cur_z = z_bridge; cur_z >= front_height || cur_z >= back_height; cur_z -= TILE_HEIGHT) { rubidium@7728: /* Draw front facing pillar */ rubidium@7728: if (cur_z >= front_height) { belugas@7849: 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@7728: /* Draw back facing pillar, but not the highest part directly under the bridge-floor */ rubidium@7728: if (drawfarpillar && cur_z >= back_height && cur_z < z_bridge - TILE_HEIGHT) { belugas@7849: 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: /** rubidium@6691: * Draws the trambits over an already drawn (lower end) of a bridge. rubidium@6691: * @param x the x of the bridge rubidium@6691: * @param y the y of the bridge rubidium@6691: * @param z the z of the bridge rubidium@6691: * @param offset number representing whether to level or sloped and the direction rubidium@6691: * @param overlay do we want to still see the road? smatz@8806: * @param head are we drawing bridge head? rubidium@6691: */ smatz@8806: static void DrawBridgeTramBits(int x, int y, byte z, int offset, bool overlay, bool head) rubidium@6691: { rubidium@6691: static const SpriteID tram_offsets[2][6] = { { 107, 108, 109, 110, 111, 112 }, { 4, 5, 15, 16, 17, 18 } }; rubidium@7249: static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 }; rubidium@6691: static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 }; rubidium@6691: rubidium@7601: static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 }; rubidium@7601: static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 }; rubidium@7601: static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 }; rubidium@7601: static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 }; rubidium@6699: rubidium@7601: /* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called rubidium@7601: * The bounding boxes here are the same as for bridge front/roof */ smatz@8806: if (head || !IsInvisibilitySet(TO_BRIDGES)) { smatz@8865: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE, smatz@8865: x, y, size_x[offset], size_y[offset], 0x28, z, smatz@8865: !head && IsTransparencySet(TO_BRIDGES)); smatz@8806: } smatz@8806: smatz@8806: /* Do not draw catenary if it is set invisible */ smatz@8865: if (!IsInvisibilitySet(TO_CATENARY)) { smatz@8865: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset], PAL_NONE, smatz@8865: x, y, size_x[offset], size_y[offset], 0x28, z, smatz@8865: IsTransparencySet(TO_CATENARY)); smatz@8865: } rubidium@7601: rubidium@7601: /* Start a new SpriteCombine for the front part */ rubidium@7601: EndSpriteCombine(); rubidium@7601: StartSpriteCombine(); rubidium@7601: rubidium@6691: /* For sloped sprites the bounding box needs to be higher, as the pylons stop on a higher point */ smatz@8865: if (!IsInvisibilitySet(TO_CATENARY)) { smatz@8865: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE, smatz@8865: x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z, smatz@8865: IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]); smatz@8865: } rubidium@6691: } rubidium@6691: rubidium@6691: /** 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: belugas@6422: * @param ti TileInfo of the structure to draw rubidium@4549: *