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@8615: #include "tile_cmd.h" maedhros@6669: #include "landscape.h" tron@3154: #include "tunnel_map.h" celestar@5573: #include "unmovable_map.h" rubidium@8720: #include "viewport_func.h" rubidium@8612: #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@8078: #include "autoslope.h" belugas@8345: #include "transparency.h" smatz@8579: #include "tunnelbridge_map.h" rubidium@8610: #include "strings_func.h" rubidium@8636: #include "date_func.h" rubidium@8627: #include "functions.h" rubidium@8640: #include "vehicle_func.h" rubidium@8653: #include "sound_func.h" smatz@8734: #include "signal_func.h" smatz@8894: #include "tunnelbridge.h" smatz@9086: #include "player_base.h" rubidium@9282: #include "engine_func.h" truelight@0: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8760: #include "table/bridge_land.h" ludde@2261: belugas@9031: BridgeSpec _bridge[MAX_BRIDGES]; tron@2763: belugas@8985: /** Reset the data been eventually changed by the grf loaded. */ belugas@8985: void ResetBridges() belugas@8985: { belugas@8985: /* First, free sprite table data */ belugas@9028: for (BridgeType i = 0; i < MAX_BRIDGES; i++) { belugas@8985: if (_bridge[i].sprite_table != NULL) { belugas@8985: for (uint j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]); belugas@8985: free(_bridge[i].sprite_table); belugas@8985: } belugas@8985: } belugas@8985: belugas@8985: /* Then, wipe out current bidges */ belugas@8985: memset(&_bridge, 0, sizeof(_bridge)); belugas@8985: /* And finally, reinstall default data */ belugas@8985: memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge)); belugas@8985: } tron@2763: belugas@6918: /** calculate the price factor for building a long bridge. belugas@6918: * 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@6918: */ 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@8867: Foundation GetBridgeFoundation(Slope tileh, Axis axis) frosch@8867: { frosch@8867: if ((tileh == SLOPE_FLAT) || frosch@8867: (((tileh == SLOPE_NE) || (tileh == SLOPE_SW)) && (axis == AXIS_X)) || frosch@8867: (((tileh == SLOPE_NW) || (tileh == SLOPE_SE)) && (axis == AXIS_Y))) return FOUNDATION_NONE; frosch@8867: frosch@8867: return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh)); frosch@8867: } truelight@0: frosch@8875: /** frosch@8875: * Determines if the track on a bridge ramp is flat or goes up/down. frosch@8875: * frosch@8875: * @param tileh Slope of the tile under the bridge head frosch@8875: * @param axis Orientation of bridge frosch@8875: * @return true iff the track is flat. frosch@8875: */ frosch@8875: bool HasBridgeFlatRamp(Slope tileh, Axis axis) frosch@8875: { frosch@8875: ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh); frosch@8875: /* If the foundation slope is flat the bridge has a non-flat ramp and vice versa. */ frosch@8875: return (tileh != SLOPE_FLAT); frosch@8875: } frosch@8875: peter1138@2478: static inline const PalSpriteID *GetBridgeSpriteTable(int index, byte table) peter1138@2478: { belugas@9031: 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: frosch@8867: /** frosch@8867: * Determines the foundation for the north bridge head, and tests if the resulting slope is valid. frosch@8867: * frosch@8867: * @param axis Axis of the bridge frosch@8867: * @param tileh Slope of the tile under the north bridge head; returns slope on top of foundation frosch@8867: * @param z TileZ corresponding to tileh, gets modified as well frosch@8867: * @return Error or cost for bridge foundation tron@4239: */ frosch@8867: static CommandCost CheckBridgeSlopeNorth(Axis axis, Slope *tileh, uint *z) tron@4239: { frosch@8867: Foundation f = GetBridgeFoundation(*tileh, axis); frosch@8867: *z += ApplyFoundationToSlope(f, tileh); truelight@0: frosch@8867: Slope valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW); frosch@8867: if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR; tron@2639: frosch@8867: if (f == FOUNDATION_NONE) return CommandCost(); frosch@8867: frosch@8867: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); truelight@0: } truelight@0: frosch@8867: /** frosch@8867: * Determines the foundation for the south bridge head, and tests if the resulting slope is valid. frosch@8867: * frosch@8867: * @param axis Axis of the bridge frosch@8867: * @param tileh Slope of the tile under the south bridge head; returns slope on top of foundation frosch@8867: * @param z TileZ corresponding to tileh, gets modified as well frosch@8867: * @return Error or cost for bridge foundation frosch@8867: */ frosch@8867: static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, uint *z) tron@4239: { frosch@8867: Foundation f = GetBridgeFoundation(*tileh, axis); frosch@8867: *z += ApplyFoundationToSlope(f, tileh); tron@4239: frosch@8867: Slope valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE); frosch@8867: if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR; tron@4239: frosch@8867: if (f == FOUNDATION_NONE) return CommandCost(); frosch@8867: frosch@8867: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); tron@4239: } tron@4239: belugas@9028: bool CheckBridge_Stuff(BridgeType bridge_type, uint bridge_len) truelight@0: { belugas@9031: 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; tron@2639: if (max >= 16 && _patches.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@6918: * @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@7157: * - p2 = (bit 8-..) - rail type or road types. rubidium@7157: * - p2 = (bit 15 ) - set means road bridge. truelight@0: */ rubidium@7439: CommandCost CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { belugas@9028: BridgeType bridge_type; belugas@9026: RailType railtype = INVALID_RAILTYPE; belugas@9026: 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@8726: CommandCost cost(EXPENSES_CONSTRUCTION); frosch@8867: CommandCost ret; maedhros@6181: bool replace_bridge = false; belugas@9028: BridgeType replaced_bridge_type; belugas@9026: TransportType transport_type; truelight@0: truelight@0: /* unpack parameters */ tron@2140: bridge_type = GB(p2, 0, 8); truelight@193: belugas@9027: if (p1 >= MapSize()) return CMD_ERROR; belugas@9027: belugas@9026: transport_type = (TransportType)GB(p2, 15, 2); Darkvater@1775: belugas@6918: /* type of bridge */ belugas@9026: switch (transport_type) { belugas@9026: case TRANSPORT_ROAD: belugas@9026: roadtypes = (RoadTypes)GB(p2, 8, 3); belugas@9026: if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_player, roadtypes)) return CMD_ERROR; belugas@9026: break; belugas@9026: belugas@9026: case TRANSPORT_RAIL: belugas@9026: railtype = (RailType)GB(p2, 8, 8); belugas@9026: if (!ValParamRailtype(railtype)) return CMD_ERROR; belugas@9026: break; belugas@9027: belugas@9027: default: belugas@9027: /* For now, only TRANSPORT_RAIL and TRANSPORT_ROAD are allowed. belugas@9027: * But let not this stops us for preparing the future */ belugas@9027: 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: Darkvater@1781: /* set and test bridge length, availability */ tron@4000: bridge_len = sx + sy - x - y - 1; Darkvater@1781: if (!CheckBridge_Stuff(bridge_type, bridge_len)) return_cmd_error(STR_5015_CAN_T_BUILD_BRIDGE_HERE); 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@8967: 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@8867: CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start); belugas@9121: CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end, &z_end); truelight@0: rubidium@8178: 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 && smatz@8579: 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) && belugas@8987: 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: rubidium@7446: 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); rubidium@7204: rubidium@7204: /* Do not remove road types when upgrading a bridge */ rubidium@7204: roadtypes |= GetRoadTypes(tile_start); maedhros@6181: } else { maedhros@6181: /* Build a new bridge. */ maedhros@6181: frosch@8867: bool allow_on_slopes = (!_is_old_ai_player && _patches.build_on_slopes); frosch@8867: 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: frosch@8867: 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); frosch@8867: 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; rubidium@7446: cost.AddCost(ret); maedhros@6181: belugas@6918: /* false - end tile slope check */ frosch@8867: 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); frosch@8867: cost.AddCost(terraform_cost_south); 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: belugas@9026: switch (transport_type) { belugas@9026: case TRANSPORT_RAIL: belugas@9121: MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype); belugas@9026: MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype); belugas@9026: break; belugas@9026: belugas@9026: case TRANSPORT_ROAD: belugas@9121: MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir, roadtypes); belugas@9026: MakeRoadBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), roadtypes); belugas@9026: break; belugas@9027: belugas@9027: default: belugas@9027: NOT_REACHED(); belugas@9027: 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) { frosch@8866: 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@8254: if (!EnsureNoVehicleOnGround(tile)) return_cmd_error(STR_980E_SHIP_IN_THE_WAY); peter1138@8967: 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: rubidium@7866: case MP_ROAD: frosch@9059: 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; smatz@8579: 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; rubidium@7446: 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: belugas@9026: if (flags & DC_EXEC && transport_type == TRANSPORT_RAIL) { Darkvater@5812: Track track = AxisToTrack(direction); smatz@8802: 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: */ smatz@9086: 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: belugas@8987: cost.AddCost((int64)bridge_len * _price.build_bridge * GetBridgeSpec(bridge_type)->price >> 8); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: Darkvater@1782: /** Build Tunnel. belugas@6980: * @param start_tile start tile of tunnel belugas@6918: * @param flags type of operation rubidium@7157: * @param p1 railtype or roadtypes. bit 9 set means road tunnel tron@2639: * @param p2 unused truelight@0: */ rubidium@7439: 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@9045: TransportType transport_type = (TransportType)GB(p1, 9, 1); tron@3063: uint start_z; tron@3063: uint end_z; rubidium@8726: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@7439: CommandCost ret; truelight@0: tron@3063: _build_tunnel_endtile = 0; belugas@9045: if (transport_type == TRANSPORT_RAIL) { rubidium@8732: if (!ValParamRailtype((RailType)p1)) return CMD_ERROR; truelight@8353: } else { truelight@8353: const RoadTypes rts = (RoadTypes)GB(p1, 0, 3); truelight@8353: if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_player, rts)) return CMD_ERROR; rubidium@7157: } Darkvater@1782: tron@3063: start_tileh = GetTileSlope(start_tile, &start_z); frosch@8909: direction = GetInclinedSlopeDirection(start_tileh); frosch@8909: if (direction == INVALID_DIAGDIR) return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL); truelight@0: peter1138@8967: if (IsWaterTile(start_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); peter1138@8881: 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@7446: Darkvater@4559: delta = TileOffsByDiagDir(direction); rubidium@7498: DiagDirection tunnel_in_way_dir; rubidium@7498: if (OtherAxis(DiagDirToAxis(direction)) == AXIS_X) { rubidium@7498: tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE; rubidium@7498: } else { rubidium@7498: tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW; rubidium@7498: } rubidium@7498: tron@3063: end_tile = start_tile; rubidium@7591: rubidium@7591: /** Tile shift coeficient. Will decrease for very long tunnels to avoid exponential growth of price*/ rubidium@7591: int tiles_coef = 3; rubidium@7591: /** Number of tiles from start of tunnel */ rubidium@7591: int tiles = 0; truelight@7804: /** Number of tiles at which the cost increase coefficient per tile is halved */ truelight@7804: int tiles_bump = 25; rubidium@7591: 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@7498: 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@7591: tiles++; truelight@7804: if (tiles == tiles_bump) { truelight@7804: tiles_coef++; truelight@7804: tiles_bump *= 2; truelight@7804: } rubidium@7591: rubidium@7446: cost.AddCost(_price.build_tunnel); rubidium@7591: 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@7446: cost.AddCost(_price.build_tunnel); rubidium@7446: cost.AddCost(ret); Darkvater@4273: belugas@6918: /* 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@8967: if (IsWaterTile(end_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); peter1138@8881: belugas@6918: /* slope of end tile must be complementary to the slope of the start tile */ tron@3636: if (end_tileh != ComplementSlope(start_tileh)) { rubidium@8173: /* Check if there is a structure on the terraformed tile. Do not add the cost, that will be done by the terraforming */ rubidium@8173: ret = DoCommand(end_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR); rubidium@8173: if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND); rubidium@7538: rubidium@8173: ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND); rubidium@8173: 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@7446: cost.AddCost(_price.build_tunnel); rubidium@7446: cost.AddCost(ret); tron@3063: tron@3063: if (flags & DC_EXEC) { belugas@9045: 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)); smatz@8802: AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_player); tron@4158: YapfNotifyTrackLayoutChange(start_tile, AxisToTrack(DiagDirToAxis(direction))); tron@3154: } else { rubidium@7159: MakeRoadTunnel(start_tile, _current_player, direction, (RoadTypes)GB(p1, 0, 3)); rubidium@7159: 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 */ Darkvater@5009: if (IsTileOwner(tile, OWNER_TOWN) && (_patches.extra_dynamite || _cheats.magic_bulldozer.value)) return true; Darkvater@5009: return false; Darkvater@5009: } Darkvater@5009: rubidium@7439: 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@8568: endtile = GetOtherTunnelEnd(tile); smatz@8568: smatz@8568: 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@8728: /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until glx@8728: * you have a "Poor" (0) town rating */ glx@8728: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { glx@8728: ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM); glx@8728: } glx@8728: truelight@0: if (flags & DC_EXEC) { smatz@8796: if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { smatz@8796: /* We first need to request values before calling DoClearSquare */ smatz@8796: DiagDirection dir = GetTunnelBridgeDirection(tile); smatz@8796: Owner owner = GetTileOwner(tile); peter1138@2870: smatz@8796: DoClearSquare(tile); smatz@8796: DoClearSquare(endtile); smatz@8793: smatz@8796: /* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */ belugas@9121: AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner); belugas@9121: AddSideToSignalBuffer(endtile, dir, owner); smatz@8793: smatz@8793: Track track = AxisToTrack(DiagDirToAxis(dir)); belugas@9121: YapfNotifyTrackLayoutChange(tile, track); smatz@8793: YapfNotifyTrackLayoutChange(endtile, track); smatz@8796: } else { smatz@8796: DoClearSquare(tile); smatz@8796: DoClearSquare(endtile); smatz@8793: } truelight@0: } smatz@8894: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (GetTunnelBridgeLength(tile, endtile) + 2)); truelight@0: } truelight@0: tron@3977: rubidium@7439: 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@8568: if (GetVehicleTunnelBridge(tile, endtile) != NULL) return CMD_ERROR; truelight@0: smatz@8579: 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@8728: /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until glx@8728: * you have a "Poor" (0) town rating */ glx@8728: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) { glx@8728: ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM); glx@8728: } glx@8728: truelight@0: if (flags & DC_EXEC) { smatz@8793: /* read this value before actual removal of bridge */ smatz@8793: bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL; smatz@8796: Owner owner = GetTileOwner(tile); smatz@9085: uint height = GetBridgeHeight(tile); truelight@0: tron@3217: DoClearSquare(tile); tron@3217: DoClearSquare(endtile); smatz@8793: for (TileIndex c = tile + delta; c != endtile; c += delta) { smatz@9085: /* do not let trees appear from 'nowhere' after removing bridge */ smatz@9085: if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) { smatz@9085: uint minz = GetTileMaxZ(c) + 3 * TILE_HEIGHT; smatz@9085: if (height < minz) SetRoadside(c, ROADSIDE_PAVED); smatz@9085: } smatz@8793: ClearBridgeMiddle(c); celestar@5573: MarkTileDirtyByTile(c); tron@3217: } truelight@0: smatz@8793: if (rail) { smatz@8796: /* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */ belugas@9121: AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner); belugas@9121: AddSideToSignalBuffer(endtile, direction, owner); smatz@8793: smatz@8793: Track track = AxisToTrack(DiagDirToAxis(direction)); belugas@9121: YapfNotifyTrackLayoutChange(tile, track); smatz@8793: YapfNotifyTrackLayoutChange(endtile, track); smatz@8793: } truelight@0: } truelight@0: smatz@8894: return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * _price.clear_bridge); truelight@0: } truelight@0: rubidium@7439: 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@8886: } 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@8224: * Draws the pillars under high bridges. rubidium@8224: * rubidium@8224: * @param psid Image and palette of a bridge pillar. rubidium@8224: * @param ti #TileInfo of current bridge-middle-tile. rubidium@8224: * @param axis Orientation of bridge. rubidium@8224: * @param type Bridge type. rubidium@8224: * @param x Sprite X position of front pillar. rubidium@8224: * @param y Sprite Y position of front pillar. rubidium@8224: * @param z_bridge Absolute height of bridge bottom. rubidium@8224: */ belugas@9028: static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo* ti, Axis axis, BridgeType type, int x, int y, int z_bridge) truelight@0: { smatz@9302: /* Do not draw bridge pillars if they are invisible */ smatz@9302: if (IsInvisibilitySet(TO_BRIDGES)) return; smatz@9302: peter1138@5919: SpriteID image = psid->sprite; smatz@9302: truelight@0: if (image != 0) { belugas@8987: bool drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0); truelight@0: rubidium@8224: /* "side" specifies the side the pillars stand on. rubidium@8224: * The length of the pillars is then set to the height of the bridge over the corners of this edge. rubidium@8224: * rubidium@8224: * axis==AXIS_X axis==AXIS_Y rubidium@8224: * side==false SW NW rubidium@8224: * side==true NE SE rubidium@8224: * rubidium@8224: * I have no clue, why this was done this way. rubidium@8224: */ skidd13@8424: bool side = HasBit(image, 0); celestar@2262: rubidium@8224: /* "dir" means the edge the pillars stand on */ rubidium@8224: DiagDirection dir = AxisToDiagDir(axis); rubidium@8224: if (side != (axis == AXIS_Y)) dir = ReverseDiagDir(dir); truelight@0: rubidium@8224: /* Determine ground height under pillars */ rubidium@8224: int front_height = ti->z; rubidium@8224: int back_height = ti->z; rubidium@8224: GetSlopeZOnEdge(ti->tileh, dir, &front_height, &back_height); rubidium@8224: rubidium@8224: /* x and y size of bounding-box of pillars */ rubidium@8224: int w = (axis == AXIS_X ? 16 : 2); rubidium@8224: int h = (axis == AXIS_X ? 2 : 16); rubidium@8224: /* sprite position of back facing pillar */ rubidium@8224: int x_back = x - (axis == AXIS_X ? 0 : 9); rubidium@8224: int y_back = y - (axis == AXIS_X ? 9 : 0); rubidium@8224: rubidium@8224: for (int cur_z = z_bridge; cur_z >= front_height || cur_z >= back_height; cur_z -= TILE_HEIGHT) { rubidium@8224: /* Draw front facing pillar */ rubidium@8224: if (cur_z >= front_height) { belugas@8345: 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@8224: /* Draw back facing pillar, but not the highest part directly under the bridge-floor */ rubidium@8224: if (drawfarpillar && cur_z >= back_height && cur_z < z_bridge - TILE_HEIGHT) { belugas@8345: 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@7187: * Draws the trambits over an already drawn (lower end) of a bridge. rubidium@7187: * @param x the x of the bridge rubidium@7187: * @param y the y of the bridge rubidium@7187: * @param z the z of the bridge rubidium@7187: * @param offset number representing whether to level or sloped and the direction rubidium@7187: * @param overlay do we want to still see the road? smatz@9302: * @param head are we drawing bridge head? rubidium@7187: */ smatz@9302: static void DrawBridgeTramBits(int x, int y, byte z, int offset, bool overlay, bool head) rubidium@7187: { rubidium@7187: static const SpriteID tram_offsets[2][6] = { { 107, 108, 109, 110, 111, 112 }, { 4, 5, 15, 16, 17, 18 } }; rubidium@7745: static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 }; rubidium@7187: static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 }; rubidium@7187: rubidium@8097: static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 }; rubidium@8097: static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 }; rubidium@8097: static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 }; rubidium@8097: static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 }; rubidium@7195: rubidium@8097: /* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called rubidium@8097: * The bounding boxes here are the same as for bridge front/roof */ smatz@9302: if (head || !IsInvisibilitySet(TO_BRIDGES)) { smatz@9302: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE, x, y, size_x[offset], size_y[offset], 0x28, z, !head && IsTransparencySet(TO_BRIDGES)); smatz@9302: } smatz@9302: smatz@9302: /* Do not draw catenary if it is set invisible */ smatz@9302: if (IsInvisibilitySet(TO_CATENARY)) return; rubidium@7187: smatz@9024: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset], PAL_NONE, x, y, size_x[offset], size_y[offset], 0x28, z, IsTransparencySet(TO_CATENARY)); rubidium@8097: rubidium@8097: /* Start a new SpriteCombine for the front part */ rubidium@8097: EndSpriteCombine(); rubidium@8097: StartSpriteCombine(); rubidium@8097: rubidium@7187: /* For sloped sprites the bounding box needs to be higher, as the pylons stop on a higher point */ smatz@9024: AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE, x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z, IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]); rubidium@7187: } rubidium@7187: rubidium@7187: /** 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@6918: * @param ti TileInfo of the structure to draw rubidium@4549: *