tron@2186: /* $Id$ */ tron@2186: celestar@5595: /** @file bridge_cmd.c 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" tron@1363: #include "table/sprites.h" tron@507: #include "table/strings.h" tron@2163: #include "functions.h" tron@679: #include "map.h" tron@1209: #include "tile.h" tron@3154: #include "tunnel_map.h" celestar@5573: #include "unmovable_map.h" truelight@0: #include "vehicle.h" truelight@0: #include "viewport.h" truelight@0: #include "command.h" truelight@0: #include "player.h" truelight@0: #include "town.h" tron@337: #include "sound.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" rubidium@4261: #include "date.h" peter1138@4656: #include "newgrf_sound.h" truelight@0: ludde@2261: #include "table/bridge_land.h" ludde@2261: peter1138@2478: const Bridge orig_bridge[] = { celestar@2262: /* rubidium@4293: year of availablity rubidium@4293: | minimum length rubidium@4293: | | maximum length rubidium@4293: | | | price rubidium@4293: | | | | maximum speed rubidium@4293: | | | | | sprite to use in GUI string with description rubidium@4293: | | | | | | | */ rubidium@4293: { 0, 0, 16, 80, 32, 0xA24 , STR_5012_WOODEN , NULL, 0 }, rubidium@4293: { 0, 0, 2, 112, 48, 0xA26 | PALETTE_TO_STRUCT_RED , STR_5013_CONCRETE , NULL, 0 }, rubidium@4293: { 1930, 0, 5, 144, 64, 0xA25 , STR_500F_GIRDER_STEEL , NULL, 0 }, rubidium@4293: { 0, 2, 10, 168, 80, 0xA22 | PALETTE_TO_STRUCT_CONCRETE, STR_5011_SUSPENSION_CONCRETE, NULL, 0 }, rubidium@4293: { 1930, 3, 16, 185, 96, 0xA22 , STR_500E_SUSPENSION_STEEL , NULL, 0 }, rubidium@4293: { 1930, 3, 16, 192, 112, 0xA22 | PALETTE_TO_STRUCT_YELLOW , STR_500E_SUSPENSION_STEEL , NULL, 0 }, rubidium@4293: { 1930, 3, 7, 224, 160, 0xA23 , STR_5010_CANTILEVER_STEEL , NULL, 0 }, rubidium@4293: { 1930, 3, 8, 232, 208, 0xA23 | PALETTE_TO_STRUCT_BROWN , STR_5010_CANTILEVER_STEEL , NULL, 0 }, rubidium@4293: { 1930, 3, 9, 248, 240, 0xA23 | PALETTE_TO_STRUCT_RED , STR_5010_CANTILEVER_STEEL , NULL, 0 }, rubidium@4293: { 1930, 0, 2, 240, 256, 0xA27 , STR_500F_GIRDER_STEEL , NULL, 0 }, rubidium@4293: { 1995, 2, 16, 255, 320, 0xA28 , STR_5014_TUBULAR_STEEL , NULL, 0 }, rubidium@4293: { 2005, 2, 32, 380, 512, 0xA28 | PALETTE_TO_STRUCT_YELLOW , STR_5014_TUBULAR_STEEL , NULL, 0 }, rubidium@4293: { 2010, 2, 32, 510, 608, 0xA28 | PALETTE_TO_STRUCT_GREY , STR_BRIDGE_TUBULAR_SILICON , NULL, 0 } truelight@0: }; truelight@0: tron@2763: Bridge _bridge[MAX_BRIDGES]; tron@2763: tron@2763: truelight@0: // calculate the price factor for building a long bridge. truelight@0: // 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, 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: peter1138@2478: static inline const PalSpriteID *GetBridgeSpriteTable(int index, byte table) peter1138@2478: { peter1138@2478: const Bridge *bridge = &_bridge[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: Darkvater@3556: static inline byte GetBridgeFlags(int index) { return _bridge[index].flags;} celestar@2262: truelight@0: tron@4239: /** Check the slope at the bridge ramps in three easy steps: tron@4239: * - valid slopes without foundation tron@4239: * - valid slopes with foundation tron@4239: * - rest is invalid tron@4239: */ tron@4239: #define M(x) (1 << (x)) tron@4239: static int32 CheckBridgeSlopeNorth(Axis axis, Slope tileh) tron@4239: { tron@4239: uint32 valid; truelight@0: tron@4239: valid = M(SLOPE_FLAT) | (axis == AXIS_X ? M(SLOPE_NE) : M(SLOPE_NW)); tron@4239: if (HASBIT(valid, tileh)) return 0; truelight@0: tron@4239: valid = tron@4246: BRIDGE_FULL_LEVELED_FOUNDATION | M(SLOPE_N) | M(SLOPE_STEEP_N) | tron@4246: (axis == AXIS_X ? M(SLOPE_E) | M(SLOPE_STEEP_E) : M(SLOPE_W) | M(SLOPE_STEEP_W)); tron@4239: if (HASBIT(valid, tileh)) return _price.terraform; tron@2639: truelight@0: return CMD_ERROR; truelight@0: } truelight@0: tron@4239: static int32 CheckBridgeSlopeSouth(Axis axis, Slope tileh) tron@4239: { tron@4239: uint32 valid; tron@4239: tron@4239: valid = M(SLOPE_FLAT) | (axis == AXIS_X ? M(SLOPE_SW) : M(SLOPE_SE)); tron@4239: if (HASBIT(valid, tileh)) return 0; tron@4239: tron@4239: valid = tron@4246: BRIDGE_FULL_LEVELED_FOUNDATION | M(SLOPE_S) | M(SLOPE_STEEP_S) | tron@4246: (axis == AXIS_X ? M(SLOPE_W) | M(SLOPE_STEEP_W) : M(SLOPE_E) | M(SLOPE_STEEP_E)); tron@4239: if (HASBIT(valid, tileh)) return _price.terraform; tron@4239: tron@4239: return CMD_ERROR; tron@4239: } tron@4239: #undef M tron@4239: tron@4239: truelight@0: uint32 GetBridgeLength(TileIndex begin, TileIndex end) truelight@0: { tron@2639: int x1 = TileX(begin); tron@2639: int y1 = TileY(begin); tron@2639: int x2 = TileX(end); tron@2639: int y2 = TileY(end); truelight@193: tron@2639: return abs(x2 + y2 - x1 - y1) - 1; truelight@0: } truelight@0: tron@2639: bool CheckBridge_Stuff(byte bridge_type, uint bridge_len) truelight@0: { celestar@2262: const Bridge *b = &_bridge[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 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) Darkvater@1775: * - p2 = (bit 8-..) - rail type. bit15 ((x>>8)&0x80) means road bridge. truelight@0: */ tron@3491: int32 CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int bridge_type; tron@3180: TransportType transport; tron@3180: RailType railtype; 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; truelight@0: int32 cost, terraformcost, ret; pasky@1585: bool allow_on_slopes; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@193: truelight@0: /* unpack parameters */ tron@2140: bridge_type = GB(p2, 0, 8); truelight@193: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1775: truelight@0: // type of bridge tron@3180: if (HASBIT(p2, 15)) { celestar@5650: railtype = RAILTYPE_BEGIN; tron@3180: transport = TRANSPORT_ROAD; truelight@0: } else { tron@3180: if (!ValParamRailtype(GB(p2, 8, 8))) return CMD_ERROR; celestar@5650: railtype = (RailType)GB(p2, 8, 8); tron@3180: transport = TRANSPORT_RAIL; 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; celestar@3854: if (y > sy) uintswap(y,sy); truelight@0: } else if (y == sy) { tron@3157: direction = AXIS_X; celestar@3854: if (x > sx) uintswap(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); tron@4000: if (IsClearWaterTile(tile_start) || IsClearWaterTile(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: tron@4246: if (IsSteepSlope(tileh_start)) z_start += TILE_HEIGHT; tron@3636: if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh_start)) { tron@3645: z_start += TILE_HEIGHT; tron@3636: tileh_start = SLOPE_FLAT; truelight@0: } truelight@0: tron@4246: if (IsSteepSlope(tileh_end)) z_end += TILE_HEIGHT; tron@3636: if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh_end)) { tron@3645: z_end += TILE_HEIGHT; tron@3636: tileh_end = SLOPE_FLAT; truelight@0: } truelight@0: tron@3301: if (z_start != z_end) return_cmd_error(STR_5009_LEVEL_LAND_OR_WATER_REQUIRED); pasky@1585: pasky@1585: // Towns are not allowed to use bridges on slopes. truelight@2422: allow_on_slopes = (!_is_old_ai_player Darkvater@1781: && _current_player != OWNER_TOWN && _patches.build_on_slopes); pasky@1585: pasky@1585: /* Try and clear the start landscape */ pasky@1585: tron@3491: ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3183: if (CmdFailed(ret)) return ret; truelight@0: cost = ret; truelight@0: tron@4239: terraformcost = CheckBridgeSlopeNorth(direction, tileh_start); tron@4239: if (CmdFailed(terraformcost) || (terraformcost != 0 && !allow_on_slopes)) truelight@0: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); truelight@0: cost += terraformcost; truelight@0: pasky@1585: /* Try and clear the end landscape */ pasky@1585: tron@3491: ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3183: if (CmdFailed(ret)) return ret; truelight@0: cost += ret; truelight@193: pasky@1585: // false - end tile slope check tron@4239: terraformcost = CheckBridgeSlopeSouth(direction, tileh_end); tron@4239: if (CmdFailed(terraformcost) || (terraformcost != 0 && !allow_on_slopes)) pasky@1585: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); pasky@1585: cost += terraformcost; truelight@0: celestar@5573: { 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); truelight@0: tron@3209: if (transport == TRANSPORT_RAIL) { tron@3301: MakeRailBridgeRamp(tile_start, _current_player, bridge_type, dir, railtype); tron@3301: MakeRailBridgeRamp(tile_end, _current_player, bridge_type, ReverseDiagDir(dir), railtype); tron@3209: } else { tron@3301: MakeRoadBridgeRamp(tile_start, _current_player, bridge_type, dir); tron@3301: MakeRoadBridgeRamp(tile_end, _current_player, bridge_type, ReverseDiagDir(dir)); 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) { tron@3178: uint z; truelight@0: celestar@5573: if (GetTileSlope(tile, &z) != SLOPE_FLAT && z >= z_start) return_cmd_error(STR_5009_LEVEL_LAND_OR_WATER_REQUIRED); truelight@0: celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) { 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: tron@3183: if (!EnsureNoVehicle(tile)) return_cmd_error(STR_980E_SHIP_IN_THE_WAY); tron@4000: 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: tron@3065: case MP_STREET: celestar@5573: if (GetRoadTileType(tile) == ROAD_TILE_DEPOT) goto not_valid_below; celestar@5573: break; celestar@5573: celestar@5590: case MP_TUNNEL: celestar@5590: break; celestar@5590: celestar@5590: case MP_STREET_BRIDGE: celestar@5590: case MP_RAILWAY_BRIDGE: celestar@5573: if (direction == DiagDirToAxis(GetBridgeRampDirection(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: celestar@5573: if (IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); 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; tron@3065: cost += 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: celestar@5648: if (flags & DC_EXEC) { celestar@5648: Track track = AxisToTrack(direction); celestar@5648: SetSignalsOnBothDir(tile_start, track); celestar@5648: YapfNotifyTrackLayoutChange(tile_start, track); celestar@5648: } 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: */ truelight@193: if (!(flags & DC_QUERY_COST)) { celestar@2262: const Bridge *b = &_bridge[bridge_type]; celestar@2262: 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: tron@3033: cost += (int64)bridge_len * _price.build_bridge * b->price >> 8; truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: celestar@5595: static inline bool CheckAllowRemoveBridge(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: celestar@5573: static bool IsVehicleOnBridge(TileIndex starttile, TileIndex endtile, uint z) tron@3779: { celestar@5573: const Vehicle *v; celestar@5573: FOR_ALL_VEHICLES(v) { celestar@5573: if ((v->tile == starttile || v->tile == endtile) && v->z_pos == z) { celestar@5573: _error_message = VehicleInTheWayErrMsg(v); celestar@5573: return true; celestar@5573: } celestar@5573: } celestar@5573: return false; tron@3779: } tron@3779: tron@1977: static int32 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: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: celestar@5595: if (!CheckAllowRemoveBridge(tile)) return CMD_ERROR; truelight@0: tron@3228: endtile = GetOtherBridgeEnd(tile); tron@3228: celestar@5573: if (!EnsureNoVehicle(tile) || celestar@5573: !EnsureNoVehicle(endtile) || celestar@5573: IsVehicleOnBridge(tile, endtile, GetBridgeHeight(tile))) { celestar@5573: return CMD_ERROR; celestar@5573: } truelight@0: tron@3227: direction = GetBridgeRampDirection(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: truelight@0: if (flags & DC_EXEC) { tron@3217: TileIndex c; tron@4158: Track track; truelight@0: celestar@1005: //checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until truelight@0: // you have a "Poor" (0) town rating tron@1901: if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) celestar@1005: ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM); truelight@0: tron@3217: DoClearSquare(tile); tron@3217: DoClearSquare(endtile); tron@3217: for (c = tile + delta; c != endtile; c += delta) { celestar@5573: ClearBridgeMiddle(c); celestar@5573: MarkTileDirtyByTile(c); tron@3217: } truelight@0: tron@3227: UpdateSignalsOnSegment(tile, ReverseDiagDir(direction)); tron@3227: UpdateSignalsOnSegment(endtile, direction); tron@4158: track = AxisToTrack(DiagDirToAxis(direction)); tron@4158: YapfNotifyTrackLayoutChange(tile, track); tron@4158: YapfNotifyTrackLayoutChange(endtile, track); truelight@0: } truelight@0: tron@3227: return (DistanceManhattan(tile, endtile) + 1) * _price.clear_bridge; truelight@0: } truelight@0: tron@1109: celestar@5593: static int32 ClearTile_Bridge(TileIndex tile, byte flags) celestar@5593: { celestar@5593: assert(IsBridgeTile(tile)); celestar@5593: if (flags & DC_AUTO) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5593: return DoClearBridge(tile, flags); truelight@0: } truelight@0: celestar@5595: int32 DoConvertBridgeRail(TileIndex tile, RailType totype, bool exec) truelight@0: { Darkvater@1782: TileIndex endtile; truelight@0: celestar@5595: if (GetBridgeTransportType(tile) == TRANSPORT_RAIL) { truelight@0: if (!CheckTileOwnership(tile)) return CMD_ERROR; truelight@0: tron@3215: endtile = GetOtherBridgeEnd(tile); truelight@0: tron@4172: if (!EnsureNoVehicle(tile) || tron@4172: !EnsureNoVehicle(endtile) || celestar@5573: IsVehicleOnBridge(tile, endtile, GetBridgeHeight(tile))) { celestar@5573: return CMD_ERROR; celestar@1073: } celestar@1073: tron@3242: if (GetRailType(tile) == totype) return CMD_ERROR; tron@3213: tron@3218: if (exec) { celestar@5573: TileIndexDiff delta; tron@4158: Track track; celestar@5573: tron@3242: SetRailType(tile, totype); tron@3242: SetRailType(endtile, totype); tron@3218: MarkTileDirtyByTile(tile); tron@3218: MarkTileDirtyByTile(endtile); KUDr@3900: tron@4158: track = AxisToTrack(DiagDirToAxis(GetBridgeRampDirection(tile))); tron@3977: YapfNotifyTrackLayoutChange(tile, track); tron@4158: YapfNotifyTrackLayoutChange(endtile, track); celestar@5573: celestar@5573: delta = TileOffsByDiagDir(GetBridgeRampDirection(tile)); celestar@5573: for (tile += delta; tile != endtile; tile += delta) { celestar@5573: MarkTileDirtyByTile(tile); // TODO encapsulate this into a function truelight@0: } tron@3213: } truelight@0: celestar@5573: return (DistanceManhattan(tile, endtile) + 1) * (_price.build_rail >> 1); tron@4000: } else { truelight@0: return CMD_ERROR; tron@4000: } truelight@0: } truelight@0: truelight@0: celestar@5573: static void DrawBridgePillars(PalSpriteID image, const TileInfo* ti, Axis axis, uint type, int x, int y, int z) truelight@0: { truelight@0: if (image != 0) { celestar@5573: bool drawfarpillar = !HASBIT(GetBridgeFlags(type), 0); Darkvater@3556: int back_height, front_height; Darkvater@3556: int i = z; truelight@0: const byte *p; truelight@0: truelight@0: static const byte _tileh_bits[4][8] = { tron@4191: { 2, 1, 8, 4, 16, 2, 0, 9 }, tron@4191: { 1, 8, 4, 2, 2, 16, 9, 0 }, tron@4191: { 4, 8, 1, 2, 16, 2, 0, 9 }, tron@4191: { 2, 4, 8, 1, 2, 16, 9, 0 } truelight@0: }; truelight@0: celestar@2148: if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); tron@333: tron@3234: p = _tileh_bits[(image & 1) * 2 + (axis == AXIS_X ? 0 : 1)]; tron@3645: front_height = ti->z + (ti->tileh & p[0] ? TILE_HEIGHT : 0); tron@3645: back_height = ti->z + (ti->tileh & p[1] ? TILE_HEIGHT : 0); celestar@2262: tron@3636: if (IsSteepSlope(ti->tileh)) { tron@3645: if (!(ti->tileh & p[2])) front_height += TILE_HEIGHT; tron@3645: if (!(ti->tileh & p[3])) back_height += TILE_HEIGHT; truelight@0: } truelight@0: tron@3645: for (; z >= front_height || z >= back_height; z -= TILE_HEIGHT) { tron@4191: /* HACK set height of the BB of pillars to 1, because the origin of the tron@4191: * sprites is at the top tron@4191: */ Darkvater@3556: if (z >= front_height) { // front facing pillar tron@4191: AddSortableSpriteToDraw(image, x, y, p[4], p[5], 1, z); tron@2952: } Darkvater@3556: tron@3645: if (drawfarpillar && z >= back_height && z < i - TILE_HEIGHT) { // back facing pillar tron@4191: AddSortableSpriteToDraw(image, x - p[6], y - p[7], p[4], p[5], 1, z); tron@2952: } truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@3636: uint GetBridgeFoundation(Slope tileh, Axis axis) tron@2639: { tron@3878: uint i; tron@3878: tron@3636: if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh)) return tileh; truelight@0: truelight@0: // inclined sloped building tron@3878: switch (tileh) { tron@4246: case SLOPE_W: tron@4246: case SLOPE_STEEP_W: i = 0; break; tron@4246: case SLOPE_S: tron@4246: case SLOPE_STEEP_S: i = 2; break; tron@4246: case SLOPE_E: tron@4246: case SLOPE_STEEP_E: i = 4; break; tron@4246: case SLOPE_N: tron@4246: case SLOPE_STEEP_N: i = 6; break; tron@4246: default: return 0; tron@3017: } tron@3878: if (axis != AXIS_X) ++i; tron@3878: return i + 15; truelight@0: } truelight@0: celestar@5593: celestar@5593: /** celestar@5593: * Draws a bridge tile. celestar@5593: * base_offset is where the sprite selection comes into play rubidium@4549: * and it works a bit like a bitmask.

For bridge heads: rubidium@4549: *

rubidium@4549: * Please note that in this code, "roads" are treated as railtype 1, whilst the real railtypes are 0, 2 and 3 rubidium@4549: */ celestar@5593: static void DrawTile_Bridge(TileInfo *ti) truelight@0: { truelight@0: uint32 image; truelight@193: celestar@5593: int base_offset; celestar@5593: bool ice = HasBridgeSnowOrDesert(ti->tile); truelight@0: celestar@5593: if (GetBridgeTransportType(ti->tile) == TRANSPORT_RAIL) { celestar@5599: if (GetTrackBits(ti->tile) != TRACK_BIT_X && GetTrackBits(ti->tile) != TRACK_BIT_Y) { celestar@5599: DrawTile_Track(ti); celestar@5599: return; celestar@5599: } celestar@5593: base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset; celestar@5593: assert(base_offset != 8); /* This one is used for roads */ celestar@5593: } else { celestar@5593: base_offset = 8; celestar@5593: } tron@3977: celestar@5593: /* as the lower 3 bits are used for other stuff, make sure they are clear */ celestar@5593: assert( (base_offset & 0x07) == 0x00); celestar@5573: celestar@5593: if (!HASBIT(BRIDGE_NO_FOUNDATION, ti->tileh)) { celestar@5593: int f = GetBridgeFoundation(ti->tileh, DiagDirToAxis(GetBridgeRampDirection(ti->tile))); celestar@5593: if (f != 0) DrawFoundation(ti, f); celestar@5593: } celestar@5573: celestar@5593: // HACK Wizardry to convert the bridge ramp direction into a sprite offset celestar@5593: base_offset += (6 - GetBridgeRampDirection(ti->tile)) % 4; celestar@5593: celestar@5593: if (ti->tileh == SLOPE_FLAT) base_offset += 4; // sloped bridge head celestar@5593: celestar@5593: /* Table number 6 always refers to the bridge heads for any bridge type */ celestar@5593: image = GetBridgeSpriteTable(GetBridgeType(ti->tile), 6)[base_offset]; celestar@5593: celestar@5593: if (!ice) { celestar@5593: DrawClearLandTile(ti, 3); celestar@5593: } else { celestar@5593: DrawGroundSprite(SPR_FLAT_SNOWY_TILE + _tileh_to_sprite[ti->tileh]); celestar@3933: } celestar@5593: celestar@5593: if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenary(ti); celestar@5593: celestar@5593: // draw ramp celestar@5593: if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); celestar@5593: /* HACK set the height of the BB of a sloped ramp to 1 so a vehicle on celestar@5593: * it doesn't disappear behind it celestar@5593: */ celestar@5593: AddSortableSpriteToDraw( celestar@5593: image, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 1 : 8, ti->z celestar@5593: ); celestar@5593: celestar@5633: if (IsTileType(ti->tile, MP_RAILWAY_BRIDGE) && HasSignals(ti->tile)) DrawSignals(ti->tile, GetTrackBits(ti->tile)); celestar@5632: celestar@5593: DrawBridgeMiddle(ti); celestar@3933: } celestar@3933: celestar@5573: celestar@5573: /** Compute bridge piece. Computes the bridge piece to display depending on the position inside the bridge. celestar@5573: * bridges pieces sequence (middle parts) celestar@5573: * bridge len 1: 0 celestar@5573: * bridge len 2: 0 1 celestar@5573: * bridge len 3: 0 4 1 celestar@5573: * bridge len 4: 0 2 3 1 celestar@5573: * bridge len 5: 0 2 5 3 1 celestar@5573: * bridge len 6: 0 2 3 2 3 1 celestar@5573: * bridge len 7: 0 2 3 4 2 3 1 celestar@5573: * #0 - always as first, #1 - always as last (if len>1) celestar@5573: * #2,#3 are to pair in order celestar@5573: * for odd bridges: #5 is going in the bridge middle if on even position, #4 on odd (counting from 0) celestar@5573: * @param north Northernmost tile of bridge celestar@5573: * @param south Southernmost tile of bridge celestar@5573: * @return Index of bridge piece celestar@5573: */ celestar@5573: static uint CalcBridgePiece(uint north, uint south) celestar@5573: { celestar@5573: if (north == 1) { celestar@5573: return 0; celestar@5573: } else if (south == 1) { celestar@5573: return 1; celestar@5573: } else if (north < south) { celestar@5573: return north & 1 ? 3 : 2; celestar@5573: } else if (north > south) { celestar@5573: return south & 1 ? 2 : 3; celestar@5573: } else { celestar@5573: return north & 1 ? 5 : 4; celestar@5573: } celestar@5573: } celestar@5573: celestar@5573: celestar@5573: void DrawBridgeMiddle(const TileInfo* ti) celestar@5573: { celestar@5573: const PalSpriteID* b; celestar@5573: PalSpriteID image; celestar@5573: uint base_offset; celestar@5573: TileIndex rampnorth; celestar@5573: TileIndex rampsouth; celestar@5573: Axis axis; celestar@5573: uint piece; celestar@5573: uint type; celestar@5573: int x; celestar@5573: int y; celestar@5573: uint z; celestar@5573: celestar@5573: if (!IsBridgeAbove(ti->tile)) return; celestar@5573: celestar@5573: rampnorth = GetNorthernBridgeEnd(ti->tile); celestar@5573: rampsouth = GetSouthernBridgeEnd(ti->tile); celestar@5573: celestar@5573: axis = GetBridgeAxis(ti->tile); celestar@5573: piece = CalcBridgePiece( celestar@5573: DistanceManhattan(ti->tile, rampnorth), celestar@5573: DistanceManhattan(ti->tile, rampsouth) celestar@5573: ); celestar@5573: type = GetBridgeType(rampsouth); celestar@5573: celestar@5573: if (GetBridgeTransportType(rampsouth) == TRANSPORT_RAIL) { celestar@5573: base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset; celestar@5573: } else { celestar@5573: base_offset = 8; celestar@5573: } celestar@5573: celestar@5573: b = base_offset + GetBridgeSpriteTable(type, piece); celestar@5573: if (axis != AXIS_X) b += 4; celestar@5573: celestar@5573: x = ti->x; celestar@5573: y = ti->y; celestar@5573: z = GetBridgeHeight(rampsouth) - 3; celestar@5573: celestar@5573: image = b[0]; celestar@5573: if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); celestar@5573: if (axis == AXIS_X) { celestar@5573: AddSortableSpriteToDraw(image, x, y, 16, 11, 1, z); celestar@5573: } else { celestar@5573: AddSortableSpriteToDraw(image, x, y, 11, 16, 1, z); celestar@5573: } celestar@5573: celestar@5573: image = b[1]; celestar@5573: if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); celestar@5573: celestar@5573: // draw roof, the component of the bridge which is logically between the vehicle and the camera celestar@5573: if (axis == AXIS_X) { celestar@5573: y += 12; celestar@5573: if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x, y, 16, 1, 0x28, z); celestar@5573: } else { celestar@5573: x += 12; celestar@5573: if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x, y, 1, 16, 0x28, z); celestar@5573: } celestar@5573: celestar@5573: if (GetRailType(rampsouth) == RAILTYPE_ELECTRIC) DrawCatenary(ti); celestar@5573: celestar@5573: if (ti->z + 5 == z) { celestar@5573: // draw poles below for small bridges celestar@5573: image = b[2]; celestar@5573: if (image != 0) { celestar@5573: if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); celestar@5573: DrawGroundSpriteAt(image, x, y, z); celestar@5573: } celestar@5573: } else if (_patches.bridge_pillars) { celestar@5573: // draw pillars below for high bridges celestar@5573: DrawBridgePillars(b[2], ti, axis, type, x, y, z); celestar@5573: } celestar@5573: } celestar@5573: celestar@5573: celestar@5573: uint SetSpeedLimitOnBridge(Vehicle *v) celestar@5573: { celestar@5573: uint bridge_speed; celestar@5573: if (v->vehstatus & VS_HIDDEN) return v->max_speed; /* in tunnel */ celestar@5573: celestar@5573: bridge_speed = _bridge[GetBridgeType(v->tile)].speed; celestar@5573: celestar@5573: if (v->type == VEH_Road) bridge_speed *= 2; /* XXX give vehicles proper speeds */ celestar@5573: celestar@5573: if (v->cur_speed > bridge_speed) v->cur_speed = bridge_speed; celestar@5573: return bridge_speed; celestar@5573: } celestar@5573: celestar@5606: static bool IsCustomBridgeHead(TileIndex tile) celestar@5606: { celestar@5606: assert(IsBridgeTile(tile)); celestar@5606: celestar@5606: if (IsTileType(tile, MP_RAILWAY_BRIDGE)) { celestar@5606: return !(GetTrackBits(tile) == TRACK_BIT_X || GetTrackBits(tile) == TRACK_BIT_Y); celestar@5606: } celestar@5606: celestar@5606: return false; /* TODO - street bridges */ celestar@5606: } celestar@5606: celestar@5573: celestar@5593: /** Gets the absolute z coordinate of a point inside a bridge tile celestar@5593: * When we're on the track (that means between position 5 and 10) celestar@5593: * on the coordinate perpendicular to the track it returns the base celestar@5593: * height of the ramp celestar@5593: * Outside this range (from 0 to 4 and from 11 to 15) it returns the celestar@5593: * "true" Z coordinate of the tile by taking the slope into account celestar@5606: * For custom bridge heads the entire bridge head is flat and has the celestar@5606: * same z coordinate celestar@5593: * @param tile The index of the tile we are talking about celestar@5593: * @param x Absolute or relative x coordinate celestar@5593: * @param y Absolute or relative y coordinate celestar@5593: * @return Absolute z coordinate celestar@5593: */ celestar@5593: static uint GetSlopeZ_Bridge(TileIndex tile, uint x, uint y) tron@2639: { celestar@5593: uint z, pos; celestar@5593: Slope tileh = GetTileSlope(tile, &z); celestar@5593: DiagDirection dir = GetBridgeRampDirection(tile); celestar@5593: celestar@5593: x &= 0xF; celestar@5593: y &= 0xF; celestar@5593: celestar@5593: pos = (DiagDirToAxis(dir) == AXIS_X ? y : x); celestar@5593: celestar@5606: // On the bridge ramp or flat bridge head? celestar@5632: if ( (2 <= pos && pos <= 13) || IsCustomBridgeHead(tile)) { celestar@5593: uint delta; celestar@5593: celestar@5593: if (IsSteepSlope(tileh)) return z + TILE_HEIGHT * 2; celestar@5593: celestar@5593: if (HASBIT(BRIDGE_HORZ_RAMP, tileh)) return z + TILE_HEIGHT; celestar@5593: celestar@5593: if (HASBIT(BRIDGE_FULL_LEVELED_FOUNDATION, tileh)) z += TILE_HEIGHT; celestar@5593: switch (dir) { celestar@5593: default: NOT_REACHED(); celestar@5593: case DIAGDIR_NE: delta = (TILE_SIZE - 1 - x) / 2; break; celestar@5593: case DIAGDIR_SE: delta = y / 2; break; celestar@5593: case DIAGDIR_SW: delta = x / 2; break; celestar@5593: case DIAGDIR_NW: delta = (TILE_SIZE - 1 - y) / 2; break; celestar@5593: } celestar@5593: return z + 1 + delta; celestar@5593: } else { celestar@5593: uint f = GetBridgeFoundation(tileh, DiagDirToAxis(dir)); celestar@5593: celestar@5593: if (f != 0) { celestar@5593: if (IsSteepSlope(tileh)) { celestar@5593: z += TILE_HEIGHT; celestar@5593: } else if (f < 15) { celestar@5593: return z + TILE_HEIGHT; celestar@5593: } celestar@5650: tileh = (Slope)_inclined_tileh[f - 15]; celestar@5593: } celestar@5593: } celestar@5593: return z + GetPartialZ(x, y, tileh); celestar@5593: } celestar@5593: celestar@5593: static Slope GetSlopeTileh_Bridge(TileIndex tile, Slope tileh) celestar@5593: { celestar@5593: if (HASBIT(BRIDGE_NO_FOUNDATION, tileh)) { tron@4062: return tileh; tron@4062: } else { celestar@5593: uint f = GetBridgeFoundation(tileh, DiagDirToAxis(GetBridgeRampDirection(tile))); celestar@5573: celestar@5593: if (f == 0) return tileh; celestar@5593: if (f < 15) return SLOPE_FLAT; celestar@5650: return (Slope)_inclined_tileh[f - 15]; tron@4062: } dominik@39: } dominik@39: celestar@5593: static void GetAcceptedCargo_Bridge(TileIndex tile, AcceptedCargo ac) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: static const StringID _bridge_tile_str[(MAX_BRIDGES + 3) + (MAX_BRIDGES + 3)] = { truelight@0: STR_501F_WOODEN_RAIL_BRIDGE, truelight@0: STR_5020_CONCRETE_RAIL_BRIDGE, truelight@0: STR_501C_STEEL_GIRDER_RAIL_BRIDGE, truelight@0: STR_501E_REINFORCED_CONCRETE_SUSPENSION, truelight@0: STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE, truelight@0: STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE, truelight@0: STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE, truelight@0: STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE, truelight@0: STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE, truelight@0: STR_501C_STEEL_GIRDER_RAIL_BRIDGE, truelight@0: STR_5027_TUBULAR_RAIL_BRIDGE, truelight@0: STR_5027_TUBULAR_RAIL_BRIDGE, truelight@0: STR_5027_TUBULAR_RAIL_BRIDGE, rubidium@4344: 0, 0, 0, truelight@0: truelight@0: STR_5025_WOODEN_ROAD_BRIDGE, truelight@0: STR_5026_CONCRETE_ROAD_BRIDGE, truelight@0: STR_5022_STEEL_GIRDER_ROAD_BRIDGE, truelight@0: STR_5024_REINFORCED_CONCRETE_SUSPENSION, truelight@0: STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE, truelight@0: STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE, truelight@0: STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE, truelight@0: STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE, truelight@0: STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE, truelight@0: STR_5022_STEEL_GIRDER_ROAD_BRIDGE, truelight@0: STR_5028_TUBULAR_ROAD_BRIDGE, truelight@0: STR_5028_TUBULAR_ROAD_BRIDGE, truelight@0: STR_5028_TUBULAR_ROAD_BRIDGE, rubidium@4344: 0, 0, 0, truelight@0: }; truelight@0: celestar@5593: static void GetTileDesc_Bridge(TileIndex tile, TileDesc *td) celestar@5593: { celestar@5593: td->str = _bridge_tile_str[GetBridgeTransportType(tile) << 4 | GetBridgeType(tile)]; tron@1901: td->owner = GetTileOwner(tile); truelight@0: } truelight@0: truelight@0: celestar@5593: static void AnimateTile_Bridge(TileIndex tile) truelight@0: { celestar@5593: /* not used */ celestar@5593: } celestar@5593: truelight@0: celestar@5593: static void TileLoop_Bridge(TileIndex tile) celestar@5593: { celestar@5593: bool snow_or_desert = HasBridgeSnowOrDesert(tile); celestar@5593: switch (_opt.landscape) { celestar@5593: case LT_HILLY: celestar@5593: if (snow_or_desert != (GetTileZ(tile) > _opt.snow_line)) { celestar@5593: SetBridgeSnowOrDesert(tile, !snow_or_desert); celestar@5593: MarkTileDirtyByTile(tile); celestar@5593: } celestar@5593: break; celestar@5593: celestar@5593: case LT_DESERT: celestar@5593: if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) { celestar@5593: SetBridgeSnowOrDesert(tile, true); celestar@5593: MarkTileDirtyByTile(tile); celestar@5593: } celestar@5593: break; celestar@5593: } celestar@5593: } celestar@5593: celestar@5593: static void ClickTile_Bridge(TileIndex tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: celestar@5593: static uint32 GetTileTrackStatus_Bridge(TileIndex tile, TransportType mode) celestar@5593: { celestar@5593: if (GetBridgeTransportType(tile) != mode) return 0; celestar@5598: celestar@5598: if (IsTileType(tile, MP_RAILWAY_BRIDGE)) return GetTileTrackStatus_Track(tile, mode); celestar@5593: return AxisToTrackBits(DiagDirToAxis(GetBridgeRampDirection(tile))) * 0x101; celestar@5593: } celestar@5593: truelight@0: celestar@5593: static void ChangeTileOwner_Bridge(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { tron@1901: if (!IsTileOwner(tile, old_player)) return; truelight@193: Darkvater@4848: if (new_player != PLAYER_SPECTATOR) { tron@1902: SetTileOwner(tile, new_player); rubidium@4434: } else { celestar@5573: DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: } truelight@0: } truelight@0: truelight@0: celestar@5608: static uint32 VehicleEnter_Street_Bridge(Vehicle *v, TileIndex tile, int x, int y) celestar@5608: { celestar@5608: int z = GetSlopeZ(x, y) - v->z_pos; celestar@5608: celestar@5608: DiagDirection dir; celestar@5608: celestar@5608: if (myabs(z) > 2) return 8; celestar@5608: celestar@5608: if (v->type == VEH_Road) { celestar@5608: /* modify speed of vehicle */ celestar@5608: uint16 spd = _bridge[GetBridgeType(tile)].speed * 2; celestar@5608: if (v->cur_speed > spd) v->cur_speed = spd; celestar@5608: } celestar@5608: celestar@5608: dir = GetBridgeRampDirection(tile); celestar@5608: if (DirToDiagDir(v->direction) == dir) { celestar@5608: switch (dir) { celestar@5608: default: NOT_REACHED(); celestar@5608: case DIAGDIR_NE: if ((x & 0xF) != 0) return 0; break; celestar@5608: case DIAGDIR_SE: if ((y & 0xF) != TILE_SIZE - 1) return 0; break; celestar@5608: case DIAGDIR_SW: if ((x & 0xF) != TILE_SIZE - 1) return 0; break; celestar@5608: case DIAGDIR_NW: if ((y & 0xF) != 0) return 0; break; celestar@5608: } celestar@5608: v->u.road.state = 0xFF; celestar@5608: return 4; celestar@5608: } else if (DirToDiagDir(v->direction) == ReverseDiagDir(dir)) { celestar@5608: v->tile = tile; celestar@5608: if (v->u.road.state == 0xFF) { celestar@5608: static const byte road_exit_bridge_state[4] = {8, 9, 0, 1}; celestar@5608: v->u.road.state = road_exit_bridge_state[dir]; celestar@5608: v->u.road.frame = 0; celestar@5608: return 4; celestar@5608: } celestar@5608: return 0; celestar@5608: } celestar@5608: return 0; celestar@5608: } celestar@5608: celestar@5622: /** celestar@5622: * @retval 0 The vehicle can proceed celestar@5622: * @retval 4 The vehicle changed onto of off the bridge (depending on the celestar@5622: * status before, check with v->u.rail.track == 0x40) celestar@5622: * @retval 8 The vehicle cannot enter the tile celestar@5622: */ celestar@5613: static uint32 VehicleEnter_Railway_Bridge(Vehicle *v, TileIndex tile, int x, int y) celestar@5593: { celestar@5593: int z = GetSlopeZ(x, y) - v->z_pos; celestar@5593: celestar@5593: DiagDirection dir; celestar@5593: celestar@5606: if (IsCustomBridgeHead(tile)) { celestar@5606: uint h; celestar@5606: GetTileSlope(tile, &h); celestar@5606: celestar@5606: z = h + TILE_HEIGHT - v->z_pos; celestar@5606: } celestar@5606: celestar@5593: if (myabs(z) > 2) return 8; celestar@5593: celestar@5608: if (IsFrontEngine(v)) { celestar@5593: /* modify speed of vehicle */ celestar@5593: uint16 spd = _bridge[GetBridgeType(tile)].speed; celestar@5593: if (v->cur_speed > spd) v->cur_speed = spd; celestar@5593: } celestar@5593: celestar@5593: dir = GetBridgeRampDirection(tile); celestar@5622: celestar@5622: if (v->direction == DiagDirToDir(ReverseDiagDir(dir))) { KUDr@5624: /* We are entering the bridge head from the bridge itself */ celestar@5651: if (v->u.rail.track == TRACK_BIT_WORMHOLE && (tile == TileVirtXY(v->x_pos, v->y_pos))) { celestar@5622: /* Get the vehicle out of the wormhole, the track will be chosen later celestar@5622: by the pathfinder */ KUDr@5624: v->tile = tile; KUDr@5628: v->u.rail.track = TrackToTrackBits(TrackdirToTrack(DiagdirToDiagTrackdir(dir))); celestar@5622: return 4; celestar@5622: } celestar@5622: return 0; celestar@5622: celestar@5622: } else { KUDr@5628: TileIndex other_bridge_end_tile; KUDr@5628: uint32 bridge_length; KUDr@5624: /* We are on the bridge head itself, possibly entering the bridge */ KUDr@5624: /* Vehicle will enter the bridge wormhole when it reaches the tile edge in the KUDr@5624: * direction of the bridge. */ KUDr@5624: TileIndexDiffC diff = TileIndexDiffCByDiagDir(dir); KUDr@5624: /* If vehicle didn't reach the edge we can return and try it next time */ KUDr@5624: if (((diff.x != 0 ? x : y) & 0x0F) != (diff.x + diff.y > 0 ? TILE_SIZE - 1 : 0)) return 0; KUDr@5628: /* We will enter the bridge wormhole. */ celestar@5622: KUDr@5628: other_bridge_end_tile = GetOtherBridgeEnd(tile); KUDr@5628: bridge_length = GetBridgeLength(tile, other_bridge_end_tile); KUDr@5628: if (bridge_length > 0) { KUDr@5628: /* Non-zero bridge length. Adjust the other coordinate to the middle of tile KUDr@5628: * to allow train controller to select proper vehicle image */ KUDr@5628: if (diff.x != 0) v->y_pos = y; else v->x_pos = x; KUDr@5628: v->direction = DiagDirToDir(dir); KUDr@5628: /* We're about to enter the bridge body, clear all up/down flags just in case */ celestar@5650: v->u.rail.track = TRACK_BIT_WORMHOLE; KUDr@5628: v->direction = DiagDirToDir(dir); KUDr@5628: CLRBIT(v->u.rail.flags, VRF_GOINGUP); KUDr@5628: CLRBIT(v->u.rail.flags, VRF_GOINGDOWN); KUDr@5628: return 4; KUDr@5628: } else { KUDr@5628: /* Zero bridge length. Pretend that nothing extra happened. Custom bridge heads should act as normal tracks. */ KUDr@5628: ; KUDr@5628: } celestar@5593: } celestar@5593: return 0; celestar@5593: } celestar@5593: celestar@5608: static uint32 VehicleEnter_Bridge(Vehicle *v, TileIndex tile, int x, int y) celestar@5608: { celestar@5613: if (v->type == VEH_Train) return VehicleEnter_Railway_Bridge(v, tile, x, y); celestar@5608: celestar@5608: if (v->type == VEH_Road) return VehicleEnter_Street_Bridge(v, tile, x, y); celestar@5608: celestar@5608: NOT_REACHED(); celestar@5608: } celestar@5608: celestar@5650: extern const TileTypeProcs _tile_type_bridge_procs = { celestar@5593: DrawTile_Bridge, /* draw_tile_proc */ celestar@5593: GetSlopeZ_Bridge, /* get_slope_z_proc */ celestar@5593: ClearTile_Bridge, /* clear_tile_proc */ celestar@5593: GetAcceptedCargo_Bridge, /* get_accepted_cargo_proc */ celestar@5593: GetTileDesc_Bridge, /* get_tile_desc_proc */ celestar@5593: GetTileTrackStatus_Bridge, /* get_tile_track_status_proc */ celestar@5593: ClickTile_Bridge, /* click_tile_proc */ celestar@5593: AnimateTile_Bridge, /* animate_tile_proc */ celestar@5593: TileLoop_Bridge, /* tile_loop_clear */ celestar@5593: ChangeTileOwner_Bridge, /* change_tile_owner_clear */ celestar@5593: NULL, /* get_produced_cargo_proc */ celestar@5593: VehicleEnter_Bridge, /* vehicle_enter_tile_proc */ celestar@5593: GetSlopeTileh_Bridge, /* get_slope_tileh_proc */ celestar@5593: };