truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" tron@507: #include "table/strings.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" truelight@0: truelight@0: extern void DrawCanalWater(uint tile); truelight@0: truelight@0: static const byte _bridge_available_year[MAX_BRIDGES] = { truelight@0: 0, 0, 10, 0, 10, 10, 10, 10, 10, 10, 75, 85, 90 truelight@0: }; truelight@0: truelight@0: static const byte _bridge_minlen[MAX_BRIDGES] = { truelight@0: 0, 0, 0, 2, 3, 3, 3, 3, 3, 0, 2, 2, 2 truelight@0: }; truelight@0: truelight@0: static const byte _bridge_maxlen[MAX_BRIDGES] = { truelight@193: 16, 2, 5, 10, 16, 16, 7, 8, 9, 2, 16, 32, 32, truelight@0: }; truelight@0: truelight@0: const uint16 _bridge_type_price_mod[MAX_BRIDGES] = { truelight@0: 80, 112, 144, 168, 185, 192, 224, 232, 248, 240, 255, 380, 510, truelight@0: }; truelight@0: truelight@0: const uint16 _bridge_speeds[MAX_BRIDGES] = { truelight@0: 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x0A0, 0x0D0, 0x0F0, 0x100, 0x140, 0x200, 0x260 truelight@0: }; truelight@0: truelight@0: const PalSpriteID _bridge_sprites[MAX_BRIDGES] = { truelight@0: 0x0A24, 0x31E8A26, 0x0A25, 0x3208A22, truelight@0: 0x0A22, 0x3218A22, 0x0A23, 0x31C8A23, truelight@193: 0x31E8A23, 0x0A27, 0x0A28, 0x3218A28, truelight@193: 0x3238A28, truelight@0: }; truelight@0: truelight@0: const StringID _bridge_material[MAX_BRIDGES] = { truelight@0: STR_5012_WOODEN, truelight@0: STR_5013_CONCRETE, truelight@0: STR_500F_GIRDER_STEEL, truelight@0: STR_5011_SUSPENSION_CONCRETE, truelight@0: STR_500E_SUSPENSION_STEEL, truelight@0: STR_500E_SUSPENSION_STEEL, truelight@0: STR_5010_CANTILEVER_STEEL, truelight@0: STR_5010_CANTILEVER_STEEL, truelight@0: STR_5010_CANTILEVER_STEEL, truelight@0: STR_500F_GIRDER_STEEL, truelight@0: STR_5014_TUBULAR_STEEL, truelight@0: STR_5014_TUBULAR_STEEL, truelight@0: STR_BRIDGE_TUBULAR_SILICON, truelight@0: }; truelight@0: 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: { truelight@0: int n,r; truelight@0: if (x < 2) return x; truelight@0: x -= 2; truelight@0: 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: truelight@0: enum { truelight@0: // foundation, whole tile is leveled up (tileh's 7, 11, 13, 14) --> 3 corners raised truelight@0: BRIDGE_FULL_LEVELED_FOUNDATION = 1 << 7 | 1 << 11 | 1 << 13 | 1 << 14, truelight@0: // foundation, tile is partly leveled up (tileh's 1, 2, 4, 8) --> 1 corner raised truelight@0: BRIDGE_PARTLY_LEVELED_FOUNDATION = 1 << 1 | 1 << 2 | 1 << 4 | 1 << 8, truelight@0: // no foundations (X,Y direction) (tileh's 0, 3, 6, 9, 12) truelight@0: BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12, truelight@0: }; truelight@0: truelight@0: /* check if bridge can be built on slope truelight@0: * direction 0 = X-axis, direction 1 = Y-axis truelight@0: * is_start_tile = false <-- end tile truelight@0: * is_start_tile = true <-- start tile truelight@0: */ truelight@0: static uint32 CheckBridgeSlope(uint direction, uint tileh, bool is_start_tile) truelight@0: { truelight@0: if (!(tileh & 0x10)) { // disable building on very steep slopes truelight@0: truelight@0: if (is_start_tile) { truelight@0: /* check slope at start tile truelight@0: - no extra cost truelight@0: - direction X: tiles 0,12 truelight@0: - direction Y: tiles 0, 9 truelight@193: */ truelight@0: if ((direction?0x201:0x1001) & (1 << tileh)) truelight@0: return 0; truelight@193: truelight@193: // disallow certain start tiles to avoid certain crooked bridges truelight@0: if (tileh == 2) truelight@0: return CMD_ERROR; truelight@0: truelight@0: } truelight@0: else { truelight@0: /* check slope at end tile truelight@0: - no extra cost truelight@0: - direction X: tiles 0, 3 truelight@0: - direction Y: tiles 0, 6 truelight@0: */ truelight@0: if ((direction?0x41:0x9) & (1 << tileh)) truelight@0: return 0; truelight@0: truelight@193: // disallow certain end tiles to avoid certain crooked bridges truelight@0: if (tileh == 8) truelight@0: return CMD_ERROR; truelight@0: truelight@0: } truelight@0: truelight@0: /* disallow common start/end tiles to avoid certain crooked bridges e.g. truelight@0: * start-tile: X 2,1 Y 2,4 (2 was disabled before) truelight@0: * end-tile: X 8,4 Y 8,1 (8 was disabled before) truelight@0: */ truelight@193: if ( (tileh == 1 && (is_start_tile != (bool)direction)) || truelight@0: (tileh == 4 && (is_start_tile == (bool)direction)) ) truelight@0: return CMD_ERROR; truelight@0: truelight@0: // slope foundations truelight@0: if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << tileh) || BRIDGE_PARTLY_LEVELED_FOUNDATION & (1 << tileh)) truelight@0: return _price.terraform; truelight@0: } truelight@0: truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: uint32 GetBridgeLength(TileIndex begin, TileIndex end) truelight@0: { truelight@0: int x1, y1, x2, y2; // coordinates of starting and end tiles truelight@0: x1 = GET_TILE_X(begin); truelight@0: y1 = GET_TILE_Y(begin); truelight@0: x2 = GET_TILE_X(end); truelight@0: y2 = GET_TILE_Y(end); truelight@193: truelight@0: return abs((x2 + y2 - x1 - y1)) - 1; truelight@0: } truelight@0: truelight@0: bool CheckBridge_Stuff(byte bridge_type, int bridge_len) truelight@0: { truelight@0: int max; // max possible length of a bridge (with patch 100) truelight@0: truelight@0: if (_bridge_available_year[bridge_type] > _cur_year) truelight@0: return false; truelight@0: truelight@0: max = _bridge_maxlen[bridge_type]; truelight@0: if (max >= 16 && _patches.longbridges) truelight@0: max = 100; truelight@0: truelight@0: if (bridge_len < _bridge_minlen[bridge_type] || bridge_len > max) truelight@0: return false; truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@193: /* Build a Bridge truelight@0: * x,y - end tile coord truelight@0: * p1 - packed start tile coords (~ dx) truelight@0: * p2&0xFF - bridge type (hi bh) truelight@0: * p2>>8 - rail type. &0x80 means road bridge. truelight@0: */ truelight@0: int32 CmdBuildBridge(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int bridge_type; truelight@0: byte rail_or_road, railtype, m5; truelight@0: int sx,sy; truelight@0: TileInfo ti_start, ti_end, ti; /* OPT: only 2 of those are ever used */ truelight@0: int bridge_len, odd_middle_part; truelight@0: uint direction; truelight@0: int i; truelight@0: int32 cost, terraformcost, ret; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@193: truelight@0: /* unpack parameters */ truelight@0: bridge_type = p2 & 0xFF; truelight@0: railtype = (byte)(p2 >> 8); truelight@193: truelight@0: // type of bridge truelight@0: if (railtype & 0x80) { truelight@0: railtype = 0; truelight@0: rail_or_road = 2; truelight@0: } else { truelight@0: rail_or_road = 0; truelight@0: } truelight@0: truelight@0: sx = GET_TILE_X(p1) * 16; truelight@0: sy = GET_TILE_Y(p1) * 16; truelight@0: truelight@0: direction = 0; truelight@0: truelight@0: /* check if valid, and make sure that (x,y) are smaller than (sx,sy) */ truelight@0: if (x == sx) { truelight@0: if (y == sy) truelight@0: return_cmd_error(STR_5008_CANNOT_START_AND_END_ON); truelight@0: direction = 1; truelight@0: if (y > sy) { truelight@0: intswap(y,sy); truelight@0: intswap(x,sx); truelight@0: } truelight@0: } else if (y == sy) { truelight@0: if (x > sx) { truelight@0: intswap(y,sy); truelight@0: intswap(x,sx); truelight@0: } truelight@0: } else truelight@0: return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN); truelight@0: truelight@0: /* retrieve landscape height and ensure it's on land */ truelight@0: if ( truelight@0: ((FindLandscapeHeight(&ti_end, sx, sy), truelight@0: ti_end.type == MP_WATER) && ti_end.map5 == 0) || truelight@0: ((FindLandscapeHeight(&ti_start, x, y), truelight@0: ti_start.type == MP_WATER) && ti_start.map5 == 0)) truelight@0: return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH); truelight@0: truelight@0: if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << ti_start.tileh)) { truelight@0: ti_start.z += 8; truelight@0: ti_start.tileh = 0; truelight@0: } truelight@0: truelight@0: if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << ti_end.tileh)) { truelight@0: ti_end.z += 8; truelight@0: ti_end.tileh = 0; truelight@0: } truelight@0: truelight@0: if (ti_start.z != ti_end.z) truelight@0: return_cmd_error(STR_5009_LEVEL_LAND_OR_WATER_REQUIRED); truelight@0: truelight@0: _error_message = STR_500C; truelight@0: truelight@0: /* try and clear the start landscape */ truelight@0: if ((ret=DoCommandByTile(ti_start.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR)) == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: cost = ret; truelight@0: truelight@0: terraformcost = CheckBridgeSlope(direction, ti_start.tileh, true); // true - bridge-start-tile, false - bridge-end-tile truelight@193: truelight@84: // towns are not allowed to use bridges on slopes. truelight@193: if (terraformcost == CMD_ERROR || truelight@84: (terraformcost && ((!_patches.ainew_active && _is_ai_player) || _current_player == OWNER_TOWN || !_patches.build_on_slopes))) truelight@0: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); truelight@0: truelight@0: cost += terraformcost; truelight@0: truelight@0: /* try and clear the end landscape */ truelight@0: if ((ret=DoCommandByTile(ti_end.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR)) == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: cost += ret; truelight@193: truelight@0: terraformcost = CheckBridgeSlope(direction, ti_end.tileh, false); // false - end tile slope check truelight@0: truelight@84: // towns are not allowed to use bridges on slopes. truelight@193: if (terraformcost == CMD_ERROR || truelight@84: (terraformcost && ((!_patches.ainew_active && _is_ai_player) || _current_player == OWNER_TOWN || !_patches.build_on_slopes))) truelight@0: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); truelight@0: truelight@0: cost += terraformcost; truelight@0: truelight@0: /* do the drill? */ truelight@0: if (flags & DC_EXEC) { truelight@0: /* build the start tile */ truelight@0: ModifyTile(ti_start.tile, truelight@0: MP_SETTYPE(MP_TUNNELBRIDGE) | truelight@0: MP_MAP2 | MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5, truelight@0: (bridge_type << 4), /* map2 */ truelight@0: railtype, /* map3_lo */ truelight@0: 0x80 | direction | rail_or_road /* map5 */ truelight@0: ); truelight@0: truelight@0: /* build the end tile */ truelight@0: ModifyTile(ti_end.tile, truelight@0: MP_SETTYPE(MP_TUNNELBRIDGE) | truelight@0: MP_MAP2 | MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5, truelight@0: (bridge_type << 4), /* map2 */ truelight@0: railtype, /* map3_lo */ truelight@0: 0x80 | 0x20 | direction | rail_or_road /* map5 */ truelight@0: ); truelight@0: } truelight@0: truelight@0: /* set various params */ truelight@0: bridge_len = ((sx + sy - x - y) >> 4) - 1; truelight@0: truelight@0: //position of middle part of the odd bridge (larger than MAX(i) otherwise) truelight@0: odd_middle_part=(bridge_len%2)?(bridge_len/2):bridge_len; truelight@0: truelight@0: for(i = 0; i != bridge_len; i++) { truelight@0: if (direction != 0) truelight@0: y+=16; truelight@0: else truelight@0: x+=16; truelight@0: truelight@0: FindLandscapeHeight(&ti, x, y); truelight@0: truelight@0: _error_message = STR_5009_LEVEL_LAND_OR_WATER_REQUIRED; truelight@0: if (ti.tileh != 0 && ti.z >= ti_start.z) truelight@0: return CMD_ERROR; truelight@0: truelight@193: // Find ship below truelight@0: if ( ti.type == MP_WATER && !EnsureNoVehicle(ti.tile) ) truelight@0: { truelight@0: _error_message = STR_980E_SHIP_IN_THE_WAY; truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: if (ti.type == MP_WATER) { truelight@0: if (ti.map5 != 0) goto not_valid_below; truelight@0: m5 = 0xC8; truelight@0: } else if (ti.type == MP_RAILWAY) { truelight@0: if (direction == 0) { truelight@0: if (ti.map5 != 2) goto not_valid_below; truelight@0: } else { truelight@0: if (ti.map5 != 1) goto not_valid_below; truelight@0: } truelight@0: m5 = 0xE0; truelight@0: } else if (ti.type == MP_STREET) { truelight@0: if (direction == 0) { truelight@0: if (ti.map5 != 5) goto not_valid_below; truelight@0: } else { truelight@0: if (ti.map5 != 10) goto not_valid_below; truelight@0: } truelight@0: m5 = 0xE8; truelight@0: } else { truelight@0: not_valid_below:; truelight@0: /* try and clear the middle landscape */ truelight@0: if ((ret=DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR)) == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: cost += ret; truelight@0: m5 = 0xC0; truelight@0: } truelight@0: truelight@0: /* do middle part of bridge */ truelight@0: if (flags & DC_EXEC) { truelight@0: _map5[ti.tile] = (byte)(m5 | direction | rail_or_road); truelight@0: _map_type_and_height[ti.tile] &= ~0xF0; truelight@0: _map_type_and_height[ti.tile] |= MP_TUNNELBRIDGE << 4; truelight@0: truelight@0: //bridges pieces sequence (middle parts) truelight@193: // bridge len 1: 0 truelight@0: // bridge len 2: 0 1 truelight@0: // bridge len 3: 0 4 1 truelight@0: // bridge len 4: 0 2 3 1 truelight@0: // bridge len 5: 0 2 5 3 1 truelight@0: // bridge len 6: 0 2 3 2 3 1 truelight@0: // bridge len 7: 0 2 3 4 2 3 1 truelight@193: // #0 - alwats as first, #1 - always as last (if len>1) truelight@0: // #2,#3 are to pair in order truelight@0: // for odd bridges: #5 is going in the bridge middle if on even position, #4 on odd (counting from 0) truelight@0: truelight@0: if(i==0) //first tile truelight@193: m5 = 0; truelight@0: else if (i==bridge_len-1) //last tile truelight@0: m5 = 1; truelight@0: else if(i==odd_middle_part) //we are on the middle of odd bridge: #5 on even pos, #4 on odd truelight@0: m5 = 5 - (i%2); truelight@0: else { truelight@0: // generate #2 and #3 in turns [i%2==0], after the middle of odd bridge truelight@193: // this sequence swaps [... XOR (i>odd_middle_part)], truelight@0: // for even bridges XOR does not apply as odd_middle_part==bridge_len truelight@0: m5 = 2 + ((i%2==0)^(i>odd_middle_part)); truelight@0: } truelight@0: truelight@0: _map2[ti.tile] = (bridge_type << 4) | m5; truelight@0: _map3_lo[ti.tile] &= 0xF; truelight@0: _map3_lo[ti.tile] |= (byte)(railtype << 4); truelight@0: truelight@0: MarkTileDirtyByTile(ti.tile); truelight@0: } truelight@0: } truelight@0: truelight@0: SetSignalsOnBothDir(ti_start.tile, (direction&1) ? 1 : 0); truelight@0: truelight@0: /* for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST) truelight@0: It's unnecessary to execute this command every time for every bridge. So it is done only truelight@0: and cost is computed in "bridge_gui.c". For AI, Towns this has to be of course calculated truelight@0: */ truelight@193: if (!(flags & DC_QUERY_COST)) { truelight@0: bridge_len += 2; // begin and end tiles/ramps truelight@0: truelight@84: if (_current_player < MAX_PLAYERS && !(_is_ai_player && !_patches.ainew_active)) truelight@0: bridge_len = CalcBridgeLenCostFactor(bridge_len); truelight@0: truelight@203: cost += ((int64)bridge_len * _price.build_bridge * _bridge_type_price_mod[bridge_type]) >> 8; truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static bool DoCheckTunnelInWay(uint tile, uint z, uint dir) truelight@0: { truelight@0: TileInfo ti; truelight@0: int delta; truelight@0: truelight@0: delta = _tileoffs_by_dir[dir]; truelight@0: truelight@0: do { truelight@0: tile -= delta; truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: } while (z < ti.z); truelight@0: truelight@0: if (z == ti.z && ti.type == MP_TUNNELBRIDGE && (ti.map5&0xF0) == 0 && (ti.map5&3) == dir) { truelight@0: _error_message = STR_5003_ANOTHER_TUNNEL_IN_THE_WAY; truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: bool CheckTunnelInWay(uint tile, int z) truelight@0: { truelight@0: return DoCheckTunnelInWay(tile,z,0) && truelight@0: DoCheckTunnelInWay(tile,z,1) && truelight@0: DoCheckTunnelInWay(tile,z,2) && truelight@0: DoCheckTunnelInWay(tile,z,3); truelight@0: } truelight@0: truelight@0: static byte _build_tunnel_bh; truelight@0: static byte _build_tunnel_railtype; truelight@0: truelight@0: static int32 DoBuildTunnel(int x, int y, int x2, int y2, uint32 flags, uint exc_tile) truelight@0: { truelight@0: uint end_tile; truelight@0: int direction; truelight@0: int32 cost, ret; truelight@0: TileInfo ti; truelight@0: uint z; truelight@0: truelight@0: if ( (uint) x > TILE_X_MAX * 16 - 1 || (uint) y > TILE_X_MAX * 16 - 1) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* check if valid, and make sure that (x,y) is smaller than (x2,y2) */ truelight@0: direction = 0; truelight@0: if (x == x2) { truelight@0: if (y == y2) truelight@0: return_cmd_error(STR_5008_CANNOT_START_AND_END_ON); truelight@0: direction++; truelight@0: if (y > y2) { truelight@0: intswap(y,y2); truelight@0: intswap(x,x2); truelight@0: exc_tile|=2; truelight@0: } truelight@0: } else if (y == y2) { truelight@0: if (x > x2) { truelight@0: intswap(y,y2); truelight@0: intswap(x,x2); truelight@0: exc_tile|=2; truelight@0: } truelight@0: } else truelight@0: return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN); truelight@0: truelight@0: cost = 0; truelight@0: truelight@0: FindLandscapeHeight(&ti, x2, y2); truelight@0: end_tile = ti.tile; truelight@0: z = ti.z; truelight@0: truelight@0: if (exc_tile != 3) { truelight@0: if ( (direction ? 9U : 12U) != ti.tileh) truelight@0: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); truelight@0: ret = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (ret == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: cost += ret; truelight@0: } truelight@0: cost += _price.build_tunnel; truelight@0: truelight@0: for(;;) { truelight@0: if (direction) y2-=16; else x2-=16; truelight@0: truelight@0: if (x2 == x && y2 == y) truelight@0: break; truelight@0: truelight@0: FindLandscapeHeight(&ti, x2, y2); truelight@0: if (ti.z <= z) truelight@0: return_cmd_error(STR_5002); truelight@0: truelight@0: if (!_cheats.crossing_tunnels.value && !CheckTunnelInWay(ti.tile, z)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: cost += _price.build_tunnel; truelight@0: cost += (cost >> 3); truelight@0: truelight@0: if (cost >= 400000000) truelight@0: cost = 400000000; truelight@0: } truelight@0: truelight@0: FindLandscapeHeight(&ti, x2, y2); truelight@0: if (ti.z != z) truelight@0: return_cmd_error(STR_5004); truelight@0: truelight@0: if (exc_tile != 1) { truelight@0: if ( (direction ? 6U : 3U) != ti.tileh) truelight@0: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); truelight@0: truelight@0: ret = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (ret == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: cost += ret; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: ModifyTile(ti.tile, truelight@0: MP_SETTYPE(MP_TUNNELBRIDGE) | truelight@0: MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5, truelight@0: _build_tunnel_railtype, /* map3lo */ truelight@0: ((_build_tunnel_bh << 1) | 2) - direction /* map5 */ truelight@0: ); truelight@0: truelight@0: ModifyTile(end_tile, truelight@0: MP_SETTYPE(MP_TUNNELBRIDGE) | truelight@0: MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5, truelight@0: _build_tunnel_railtype, /* map3lo */ truelight@0: (_build_tunnel_bh << 1) | (direction ? 3:0)/* map5 */ truelight@0: ); truelight@0: truelight@0: UpdateSignalsOnSegment(end_tile, direction?7:1); truelight@0: } truelight@0: truelight@0: return cost + _price.build_tunnel; truelight@0: } truelight@0: truelight@0: /* Build Tunnel truelight@0: * x,y - start tile coord truelight@0: * p1 - railtype truelight@0: * p2 - ptr to uint that recieves end tile truelight@0: */ truelight@0: int32 CmdBuildTunnel(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: TileInfo ti, tiorg; truelight@0: int direction; truelight@0: uint z; truelight@0: static const int8 _build_tunnel_coord_mod[4+1] = { -16, 0, 16, 0, -16 }; truelight@0: static const byte _build_tunnel_tileh[4] = {3, 9, 12, 6}; truelight@0: uint excavated_tile; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: _build_tunnel_railtype = (byte)(p1 & 0xFF); truelight@0: _build_tunnel_bh = (byte)(p1 >> 8); truelight@0: truelight@0: _build_tunnel_endtile = 0; truelight@0: excavated_tile = 0; truelight@0: truelight@0: FindLandscapeHeight(&tiorg, x, y); truelight@0: truelight@0: if (!EnsureNoVehicle(tiorg.tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (!(direction=0, tiorg.tileh==12) && truelight@0: !(direction++, tiorg.tileh==6) && truelight@0: !(direction++, tiorg.tileh==3) && truelight@0: !(direction++, tiorg.tileh==9) ) truelight@0: return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL); truelight@0: truelight@0: z = tiorg.z; truelight@0: do { truelight@0: x += _build_tunnel_coord_mod[direction]; truelight@0: y += _build_tunnel_coord_mod[direction+1]; truelight@0: FindLandscapeHeight(&ti, x, y); truelight@0: } while (z != ti.z); truelight@0: _build_tunnel_endtile = ti.tile; truelight@0: truelight@0: truelight@0: if (!EnsureNoVehicle(ti.tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (ti.tileh != _build_tunnel_tileh[direction]) { truelight@0: if (DoCommandByTile(ti.tile, ti.tileh & ~_build_tunnel_tileh[direction], 0, flags, CMD_TERRAFORM_LAND) == CMD_ERROR) truelight@0: return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND); truelight@0: excavated_tile = 1; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC && DoBuildTunnel(x,y,tiorg.x,tiorg.y,flags&~DC_EXEC,excavated_tile) == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@193: truelight@0: return DoBuildTunnel(x,y,tiorg.x, tiorg.y,flags,excavated_tile); truelight@0: } truelight@0: truelight@0: static const byte _updsignals_tunnel_dir[4] = { 5, 7, 1, 3}; truelight@0: dominik@98: uint CheckTunnelBusy(uint tile, int *length) truelight@0: { truelight@0: int z = GetTileZ(tile); truelight@0: byte m5 = _map5[tile]; truelight@0: int delta = _tileoffs_by_dir[m5 & 3], len = 0; truelight@0: uint starttile = tile; truelight@0: Vehicle *v; truelight@193: truelight@0: do { tile += delta; len++; } while (!IS_TILETYPE(tile, MP_TUNNELBRIDGE) || _map5[tile]&0xF0 || (byte)(_map5[tile] ^ 2) != m5 || GetTileZ(tile) != z); truelight@0: truelight@0: if ((v=FindVehicleBetween(starttile, tile, z)) != NULL) { truelight@0: _error_message = v->type == VEH_Train ? STR_5000_TRAIN_IN_TUNNEL : STR_5001_ROAD_VEHICLE_IN_TUNNEL; truelight@0: return (uint)-1; truelight@0: } truelight@193: truelight@0: if (length) *length = len; truelight@0: return tile; truelight@0: } truelight@0: truelight@0: static int32 DoClearTunnel(uint tile, uint32 flags) truelight@0: { truelight@0: Town *t; truelight@0: uint endtile; truelight@0: int length; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: // in scenario editor you can always destroy tunnels truelight@0: if (_game_mode != GM_EDITOR && !CheckTileOwnership(tile)) { truelight@0: if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || _map_owner[tile] != OWNER_TOWN) truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: endtile = CheckTunnelBusy(tile, &length); truelight@0: if (endtile == (uint)-1) return CMD_ERROR; truelight@0: truelight@0: _build_tunnel_endtile = endtile; truelight@193: truelight@0: t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty truelight@0: // check if you're allowed to remove the tunnel owned by a town truelight@0: // removal allowal depends on difficulty settings truelight@0: if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR ) { truelight@0: if (!CheckforTownRating(tile, 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: truelight@0: if (flags & DC_EXEC) { darkvater@38: // We first need to request the direction before calling DoClearSquare darkvater@38: // else the direction is always 0.. dah!! ;) darkvater@38: byte tile_dir = _map5[tile]&3; darkvater@38: byte endtile_dir = _map5[endtile]&3; truelight@0: DoClearSquare(tile); truelight@0: DoClearSquare(endtile); darkvater@38: UpdateSignalsOnSegment(tile, _updsignals_tunnel_dir[tile_dir]); darkvater@38: UpdateSignalsOnSegment(endtile, _updsignals_tunnel_dir[endtile_dir]); truelight@193: if (_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) truelight@0: ChangeTownRating(t, -250, 0); truelight@0: } truelight@0: return _price.clear_tunnel * (length + 1); truelight@0: } truelight@0: truelight@0: static uint FindEdgesOfBridge(uint tile, uint *endtile) truelight@0: { truelight@0: int direction = _map5[tile] & 1; truelight@0: uint start; truelight@0: truelight@0: // find start of bridge truelight@0: for(;;) { truelight@0: if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0x80) truelight@0: break; truelight@0: tile += direction ? TILE_XY(0,-1) : TILE_XY(-1,0); truelight@0: } truelight@0: truelight@0: start = tile; truelight@0: truelight@0: // find end of bridge truelight@0: for(;;) { truelight@0: if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0xA0) truelight@0: break; truelight@0: tile += direction ? TILE_XY(0,1) : TILE_XY(1,0); truelight@0: } truelight@0: truelight@0: *endtile = tile; truelight@0: truelight@0: return start; truelight@0: } truelight@0: truelight@0: static int32 DoClearBridge(uint tile, uint32 flags) truelight@0: { truelight@0: uint endtile; truelight@0: Vehicle *v; truelight@0: Town *t; truelight@0: int direction; truelight@193: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: direction = _map5[tile]&1; truelight@0: truelight@0: /* delete stuff under the middle part if there's a transport route there..? */ truelight@0: if ((_map5[tile] & 0xE0) == 0xE0) { truelight@0: int32 cost; truelight@0: truelight@0: // check if we own the tile below the bridge.. truelight@0: if (_current_player != OWNER_WATER && (!CheckTileOwnership(tile) || !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)) )) truelight@0: return CMD_ERROR; truelight@193: truelight@0: cost = (_map5[tile] & 8) ? _price.remove_road * 2 : _price.remove_rail; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: _map5[tile] = _map5[tile] & ~0x38; truelight@0: _map_owner[tile] = OWNER_NONE; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: return cost; truelight@193: truelight@0: /* delete canal under bridge */ truelight@0: } else if(_map5[tile]==0xC8 && GET_TILEHEIGHT(tile)!=0) { truelight@0: int32 cost; truelight@193: truelight@0: // check for vehicles under bridge truelight@0: if ( !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)) ) truelight@0: return CMD_ERROR; truelight@0: cost = _price.clear_water; truelight@0: if (flags & DC_EXEC) { truelight@0: _map5[tile] = _map5[tile] & ~0x38; truelight@0: _map_owner[tile] = OWNER_NONE; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: return cost; truelight@0: } truelight@0: truelight@0: tile = FindEdgesOfBridge(tile, &endtile); truelight@0: truelight@0: // floods, scenario editor can always destroy bridges truelight@0: if (_current_player != OWNER_WATER && _game_mode != GM_EDITOR && !CheckTileOwnership(tile)) { truelight@0: if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || _map_owner[tile] != OWNER_TOWN) truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: if (!EnsureNoVehicle(tile) || !EnsureNoVehicle(endtile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* Make sure there's no vehicle on the bridge truelight@0: Omit tile and endtile, since these are already checked, thus solving the problem truelight@0: of bridges over water, or higher bridges, where z is not increased, eg level bridge truelight@0: */ truelight@0: tile += direction ? TILE_XY(0, 1) : TILE_XY( 1,0); truelight@0: endtile -= direction ? TILE_XY(0, 1) : TILE_XY( 1,0); truelight@0: if ((v=FindVehicleBetween(tile, endtile, GET_TILEHEIGHT(tile) + 8)) != NULL) { truelight@193: VehicleInTheWayErrMsg(v); truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: /* Put the tiles back to start/end position */ truelight@0: tile -= direction ? TILE_XY(0, 1) : TILE_XY( 1,0); truelight@0: endtile += direction ? TILE_XY(0, 1) : TILE_XY( 1,0); truelight@0: truelight@193: truelight@0: t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty truelight@193: // check if you're allowed to remove the bridge owned by a town. truelight@0: // removal allowal depends on difficulty settings truelight@0: if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) { truelight@193: if (!CheckforTownRating(tile, flags, t, TUNNELBRIDGE_REMOVE)) truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: byte m5; truelight@0: uint c = tile; truelight@0: uint16 new_data; truelight@0: truelight@0: //checks if the owner is town then decrease town rating by 250 until truelight@0: // you have a "Poor" (0) town rating truelight@193: if (_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) truelight@193: ChangeTownRating(t, -250, 0); truelight@0: truelight@0: do { truelight@0: m5 = _map5[c]; truelight@0: truelight@0: if (m5 & 0x40) { truelight@0: if (m5 & 0x20) { truelight@0: static const uint16 _new_data_table[] = {0x1002, 0x1001, 0x2005, 0x200A, 0, 0, 0, 0}; truelight@0: new_data = _new_data_table[((m5 & 0x18) >> 2) | (m5&1)]; truelight@0: } else { truelight@0: if (!(m5 & 0x18)) goto clear_it; truelight@0: new_data = 0x6000; truelight@0: } truelight@0: truelight@0: _map_type_and_height[c] &= 0x0F; truelight@0: _map_type_and_height[c] |= new_data >> 8; truelight@0: _map5[c] = (byte)new_data; truelight@0: _map2[c] = 0; truelight@0: truelight@0: MarkTileDirtyByTile(c); truelight@0: truelight@0: } else { truelight@0: clear_it:; truelight@0: DoClearSquare(c); truelight@0: } truelight@0: c += direction ? TILE_XY(0,1) : TILE_XY(1,0); truelight@0: } while (c <= endtile); truelight@0: truelight@0: SetSignalsOnBothDir(tile, direction); truelight@0: SetSignalsOnBothDir(endtile, direction); truelight@193: truelight@0: } truelight@0: truelight@0: return ((((endtile - tile) >> (direction?8:0))&0xFF)+1) * _price.clear_bridge; truelight@0: } truelight@0: truelight@0: static int32 ClearTile_TunnelBridge(uint tile, byte flags) { truelight@0: int32 ret; truelight@0: byte m5 = _map5[tile]; truelight@0: truelight@0: if ((m5 & 0xF0) == 0) { truelight@0: if (flags & DC_AUTO) truelight@0: return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST); truelight@0: return DoClearTunnel(tile, flags); truelight@0: } else if (m5 & 0x80) { truelight@0: if (flags & DC_AUTO) truelight@0: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); truelight@0: truelight@0: ret = DoClearBridge(tile, flags); truelight@0: if (ret == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: truelight@0: return ret; truelight@0: } else { truelight@0: return CMD_ERROR; truelight@0: } truelight@0: } truelight@0: truelight@0: int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec) truelight@0: { truelight@0: uint endtile; truelight@0: int length; truelight@0: Vehicle *v; truelight@0: truelight@0: if ((_map5[tile] & 0xFC) == 0x00) { truelight@0: // railway tunnel truelight@0: if (!CheckTileOwnership(tile)) return CMD_ERROR; truelight@0: truelight@0: if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR; truelight@193: truelight@0: endtile = CheckTunnelBusy(tile, &length); truelight@0: if (endtile == (uint)-1) return CMD_ERROR; truelight@0: truelight@0: if (exec) { truelight@0: _map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; truelight@0: _map3_lo[endtile] = (_map3_lo[endtile] & 0xF0) + totype; truelight@0: MarkTileDirtyByTile(tile); truelight@0: MarkTileDirtyByTile(endtile); truelight@0: } truelight@0: return (length + 1) * (_price.build_rail >> 1); truelight@0: } else if ((_map5[tile] & 0xF8) == 0xE0) { truelight@0: // bridge middle part with rail below truelight@0: // only check for train under bridge truelight@0: if (!CheckTileOwnership(tile) || !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile))) truelight@0: return CMD_ERROR; truelight@0: truelight@0: // tile is already of requested type? truelight@0: if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR; truelight@0: // change type. truelight@0: if (exec) { truelight@0: _map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: return _price.build_rail >> 1; truelight@0: } else if ((_map5[tile]&0xC6) == 0x80) { truelight@0: uint starttile; truelight@0: int32 cost; truelight@0: truelight@0: if (!CheckTileOwnership(tile)) return CMD_ERROR; truelight@0: truelight@0: // railway bridge truelight@0: starttile = tile = FindEdgesOfBridge(tile, &endtile); truelight@0: // Make sure there's no vehicle on the bridge truelight@0: if ((v=FindVehicleBetween(tile, endtile, 0xff)) != NULL) { truelight@193: VehicleInTheWayErrMsg(v); truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR; truelight@0: cost = 0; truelight@0: do { truelight@0: if (exec) { truelight@0: if (tile == starttile || tile == endtile) { truelight@0: _map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; truelight@0: } else { truelight@0: _map3_lo[tile] = (_map3_lo[tile] & 0x0F) + (totype << 4); truelight@0: } truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: cost += (_price.build_rail>>1); truelight@0: tile += _map5[tile]&1 ? TILE_XY(0,1) : TILE_XY(1,0); truelight@0: } while (tile <= endtile); truelight@0: truelight@0: return cost; truelight@193: } else truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: truelight@0: // fast routine for getting the height of a middle bridge tile. 'tile' MUST be a middle bridge tile. truelight@84: uint GetBridgeHeight(const TileInfo *ti) truelight@0: { truelight@0: uint delta; truelight@0: TileInfo ti_end; truelight@0: uint tile = ti->tile; truelight@0: uint z_correction = 0; truelight@0: truelight@0: // find the end tile of the bridge. truelight@0: delta = (_map5[tile] & 1) ? TILE_XY(0,1) : TILE_XY(1,0); truelight@0: do { truelight@0: assert((_map5[tile] & 0xC0) == 0xC0); // bridge and middle part truelight@0: tile += delta; truelight@0: } while (_map5[tile] & 0x40); // while bridge middle parts truelight@0: truelight@0: // if the end of the bridge is on a tileh 7, the z coordinate is 1 tile too low truelight@0: // correct it. truelight@0: FindLandscapeHeightByTile(&ti_end, tile); truelight@0: if (HASBIT(1 << 7, ti_end.tileh)) truelight@0: z_correction += 8; truelight@193: truelight@0: // return the height there (the height of the NORTH CORNER) truelight@0: return GET_TILEHEIGHT(tile) + z_correction; truelight@0: } truelight@0: truelight@0: static const byte _bridge_foundations[2][16] = { truelight@0: // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 truelight@0: {1,16,18,3,20,5,0,7,22,0,10,11,12,13,14}, truelight@0: {1,15,17,0,19,5,6,7,21,9,10,11, 0,13,14}, truelight@0: }; truelight@0: truelight@0: extern const byte _track_sloped_sprites[14]; truelight@0: extern const byte _road_sloped_sprites[14]; truelight@0: tron@511: #include "table/bridge_land.h" truelight@0: #include "table/tunnel_land.h" truelight@0: truelight@0: static void DrawBridgePillars(TileInfo *ti, int x, int y, int z) truelight@0: { truelight@0: const uint32 *b; truelight@0: uint32 image; truelight@0: int piece; truelight@0: truelight@0: b = _bridge_poles_table[_map2[ti->tile]>>4]; truelight@0: truelight@0: // Draw first piece truelight@0: // (necessary for cantilever bridges) truelight@0: image = b[12 + (ti->map5&0x01)]; truelight@0: piece = _map2[ti->tile]&0xF; tron@333: if (image != 0 && piece != 0) { tron@497: if (_display_opt & DO_TRANS_BUILDINGS) image = (image & 0x3FFF) | 0x03224000; tron@333: DrawGroundSpriteAt(image, x, y, z); tron@333: } truelight@0: truelight@0: image = b[(ti->map5&0x01)*6 + piece]; truelight@0: truelight@0: if (image != 0) { truelight@0: int back_height, front_height, i=z; truelight@0: const byte *p; truelight@0: truelight@0: static const byte _tileh_bits[4][8] = { truelight@0: {2,1,8,4, 16,11,0,9}, truelight@0: {1,8,4,2, 11,16,9,0}, truelight@0: {4,8,1,2, 16,11,0,9}, truelight@0: {2,4,8,1, 11,16,9,0}, truelight@0: }; truelight@0: tron@497: if (_display_opt & DO_TRANS_BUILDINGS) image = (image & 0x3FFF) | 0x03224000; tron@333: truelight@0: p = _tileh_bits[(image & 1) * 2 + (ti->map5&0x01)]; truelight@0: front_height = ti->z + ((ti->tileh & p[0])?8:0); truelight@0: back_height = ti->z + ((ti->tileh & p[1])?8:0); truelight@0: if (ti->tileh & 0x10) { truelight@0: if (!(ti->tileh & p[2])) front_height += 8; truelight@0: if (!(ti->tileh & p[3])) back_height += 8; truelight@0: } truelight@0: truelight@0: for(; z>=front_height || z>=back_height; z=z-8) { truelight@0: if (z>=front_height) AddSortableSpriteToDraw(image, x,y, p[4], p[5], 0x28, z); // front facing pillar truelight@0: if (z>=back_height && ztile] & 0x80; truelight@193: truelight@193: // draw tunnel? truelight@0: if ( (byte)(ti->map5&0xF0) == 0) { truelight@0: /* railway type */ truelight@0: image = (_map3_lo[ti->tile] & 0xF) * 8; truelight@0: truelight@0: /* ice? */ truelight@0: if (ice) truelight@0: image += 32; truelight@0: truelight@0: image += _draw_tunnel_table_1[(ti->map5 >> 2) & 0x3]; truelight@0: image += (ti->map5 & 3) << 1; truelight@0: DrawGroundSprite(image); truelight@0: truelight@0: AddSortableSpriteToDraw(image+1, ti->x + 15, ti->y + 15, 1, 1, 8, (byte)ti->z); truelight@0: // draw bridge? truelight@0: } else if ((byte)ti->map5 & 0x80) { truelight@0: // get type of track on the bridge. truelight@0: tmp = _map3_lo[ti->tile]; truelight@0: if (ti->map5 & 0x40) tmp >>= 4; truelight@0: tmp &= 0xF; truelight@0: truelight@0: // 0 = rail bridge truelight@0: // 1 = road bridge truelight@0: // 2 = monorail bridge truelight@0: // 3 = maglev bridge truelight@0: truelight@0: // add direction and fix stuff. truelight@0: if (tmp != 0) tmp++; truelight@0: tmp = (ti->map5&3) + (tmp*2); truelight@0: truelight@0: if (!(ti->map5 & 0x40)) { // bridge ramps truelight@193: truelight@0: if (!(BRIDGE_NO_FOUNDATION & (1 << ti->tileh))) { // no foundations for 0, 3, 6, 9, 12 truelight@0: int f = GetBridgeFoundation(ti->tileh, ti->map5 & 0x1); // pass direction truelight@0: if (f) DrawFoundation(ti, f); truelight@193: truelight@0: // default sloped sprites.. truelight@0: if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3; truelight@0: } truelight@0: truelight@0: // bridge ending. truelight@0: b = _bridge_sprite_table[_map2[ti->tile]>>4][6]; truelight@0: b += (tmp&(3<<1))*4; /* actually ((tmp>>2)&3)*8 */ truelight@0: b += (tmp&1); // direction truelight@0: if (ti->tileh == 0) b += 4; // sloped "entrance" ? truelight@0: if (ti->map5 & 0x20) b += 2; // which side truelight@193: truelight@193: image = *b; truelight@0: truelight@0: if (!ice) { truelight@0: DrawClearLandTile(ti, 3); truelight@0: } else { truelight@0: DrawGroundSprite(0x11C6 + _tileh_to_sprite[ti->tileh]); truelight@0: } truelight@0: truelight@0: // draw ramp tron@497: if (_display_opt & DO_TRANS_BUILDINGS) image = (image & 0x3FFF) | 0x03224000; truelight@0: AddSortableSpriteToDraw(image, ti->x, ti->y, 16, 16, 7, ti->z); truelight@0: } else { truelight@0: // bridge middle part. truelight@0: uint z; truelight@0: int x,y; truelight@0: truelight@0: image = (ti->map5 >> 3) & 3; // type of stuff under bridge (only defined for 0,1) truelight@0: assert(image <= 1); truelight@0: truelight@0: if (!(ti->map5 & 0x20)) { truelight@0: // draw land under bridge truelight@0: if (ice) image += 2; // ice too? truelight@0: DrawGroundSprite(_bridge_land_below[image] + _tileh_to_sprite[ti->tileh]); truelight@0: truelight@0: // draw canal water? truelight@0: if (ti->map5 & 8 && ti->z != 0) DrawCanalWater(ti->tile); truelight@0: } else { truelight@0: // draw transport route under bridge truelight@193: truelight@0: // draw foundation? truelight@0: if (ti->tileh) { truelight@0: int f = _bridge_foundations[ti->map5&1][ti->tileh]; truelight@0: if (f) DrawFoundation(ti, f); truelight@0: } truelight@0: truelight@0: if (!(image&1)) { truelight@0: // railway truelight@0: image = 0x3F3 + (ti->map5 & 1); truelight@0: if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3; truelight@0: image += (_map3_lo[ti->tile] & 0xF) * TRACKTYPE_SPRITE_PITCH; truelight@0: if (ice) image += 26; // ice? truelight@0: } else { truelight@0: // road truelight@0: image = 1332 + (ti->map5 & 1); truelight@0: if (ti->tileh != 0) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F; truelight@0: if (ice) image += 19; // ice? truelight@0: } truelight@0: DrawGroundSprite(image); truelight@0: } truelight@0: // get bridge sprites truelight@0: b = _bridge_sprite_table[_map2[ti->tile]>>4][_map2[ti->tile]&0xF] + tmp * 4; truelight@0: truelight@0: z = GetBridgeHeight(ti) + 5; truelight@193: truelight@0: // draw rail truelight@0: image = b[0]; tron@497: if (_display_opt & DO_TRANS_BUILDINGS) image = (image & 0x3FFF) | 0x03224000; truelight@0: AddSortableSpriteToDraw(image, ti->x, ti->y, (ti->map5&1)?11:16, (ti->map5&1)?16:11, 1, z); truelight@0: truelight@0: x = ti->x; truelight@0: y = ti->y; truelight@0: image = b[1]; tron@497: if (_display_opt & DO_TRANS_BUILDINGS) image = (image & 0x3FFF) | 0x03224000; truelight@0: truelight@0: // draw roof truelight@0: if (ti->map5&1) { truelight@0: x += 12; truelight@0: if (image&0x3FFF) AddSortableSpriteToDraw(image, x,y, 1, 16, 0x28, z); truelight@0: } else { truelight@0: y += 12; truelight@0: if (image&0x3FFF) AddSortableSpriteToDraw(image, x,y, 16, 1, 0x28, z); truelight@0: } truelight@193: truelight@0: if (ti->z + 5 == z ) { truelight@0: // draw poles below for small bridges truelight@0: image = b[2]; truelight@0: if (image) { tron@497: if (_display_opt & DO_TRANS_BUILDINGS) image = (image & 0x3FFF) | 0x03224000; truelight@0: DrawGroundSpriteAt(image, x, y, z); truelight@0: } truelight@0: } else if (_patches.bridge_pillars) { truelight@0: // draw pillars below for high bridges truelight@0: DrawBridgePillars(ti, x, y, z); truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static uint GetSlopeZ_TunnelBridge(TileInfo *ti) { truelight@0: uint z = ti->z; truelight@0: uint x = ti->x & 0xF; truelight@0: uint y = ti->y & 0xF; truelight@0: truelight@0: // swap directions if Y tunnel/bridge to let the code handle the X case only. truelight@0: if (ti->map5 & 1) intswap(x,y); truelight@0: truelight@0: // to the side of the tunnel/bridge? truelight@0: if (IS_INT_INSIDE(y, 5, 10+1)) { truelight@0: // tunnel? truelight@0: if ( (ti->map5 & 0xF0) == 0) truelight@0: return z; truelight@0: truelight@193: // bridge? truelight@0: if ( ti->map5 & 0x80 ) { truelight@0: // bridge ending? truelight@0: if (!(ti->map5 & 0x40)) { truelight@0: if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << ti->tileh)) // 7, 11, 13, 14 truelight@0: z += 8; truelight@0: truelight@0: // no ramp for bridge ending truelight@0: if ((BRIDGE_PARTLY_LEVELED_FOUNDATION & (1 << ti->tileh) || BRIDGE_NO_FOUNDATION & (1 << ti->tileh)) && ti->tileh != 0) { truelight@0: return z + 8; truelight@0: truelight@0: } else if (!(ti->map5 & 0x20)) { // northern / southern ending truelight@0: // ramp truelight@0: return (z + (x>>1) + 1); truelight@0: } else { truelight@0: // ramp in opposite dir truelight@0: return (z + ((x^0xF)>>1)); truelight@0: } truelight@193: truelight@0: // bridge middle part truelight@0: } else { truelight@0: // build on slopes? truelight@0: if (ti->tileh) z+=8; truelight@0: truelight@0: // keep the same elevation because we're on the bridge? truelight@193: if (_get_z_hint >= z + 8) truelight@0: return _get_z_hint; truelight@193: truelight@0: // actually on the bridge, but not yet in the shared area. truelight@193: if (!IS_INT_INSIDE(x, 5, 10+1)) truelight@0: return GetBridgeHeight(ti) + 8; truelight@0: truelight@0: // in the shared area, assume that we're below the bridge, cause otherwise the hint would've caught it. truelight@0: // if rail or road below then it means it's possibly build on slope below the bridge. truelight@0: if (ti->map5 & 0x20) { truelight@0: uint f = _bridge_foundations[ti->map5&1][ti->tileh]; truelight@0: // make sure that the slope is not inclined foundation truelight@0: if (IS_BYTE_INSIDE(f, 1, 15)) return z; truelight@193: truelight@0: // change foundation type? truelight@0: if (f) ti->tileh = _inclined_tileh[f - 15]; truelight@0: } truelight@193: truelight@0: // no transport route, fallback to default truelight@0: } truelight@0: } truelight@0: } else { truelight@0: // if it's a bridge middle with transport route below, then we need to compensate for build on slopes truelight@0: if ( (ti->map5 & (0x80 + 0x40 + 0x20)) == (0x80 + 0x40 + 0x20)) { truelight@0: uint f; truelight@0: if (ti->tileh) z += 8; truelight@0: f = _bridge_foundations[ti->map5&1][ti->tileh]; truelight@0: if (IS_BYTE_INSIDE(f, 1, 15)) return z; truelight@0: if (f) ti->tileh = _inclined_tileh[f - 15]; truelight@0: } truelight@0: } truelight@0: truelight@0: // default case truelight@0: z = ti->z; truelight@0: return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + z; truelight@0: } truelight@0: dominik@39: static uint GetSlopeTileh_TunnelBridge(TileInfo *ti) { dominik@39: // not accurate, but good enough for slope graphics drawing dominik@39: return 0; dominik@39: } dominik@39: dominik@39: tron@473: static void GetAcceptedCargo_TunnelBridge(uint 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, truelight@0: 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, truelight@0: 0,0,0, truelight@0: }; truelight@0: truelight@0: static void GetTileDesc_TunnelBridge(uint tile, TileDesc *td) truelight@0: { truelight@0: int delta; truelight@0: truelight@0: if ((_map5[tile] & 0x80) == 0) { truelight@0: td->str = STR_5017_RAILROAD_TUNNEL + ((_map5[tile] >> 2) & 3); truelight@0: } else { truelight@0: td->str = _bridge_tile_str[ (_map2[tile] >> 4) + (((_map5[tile]>>1)&3)<<4) ]; truelight@0: truelight@0: /* scan to the end of the bridge, that's where the owner is stored */ truelight@0: if (_map5[tile] & 0x40) { truelight@0: delta = _map5[tile] & 1 ? TILE_XY(0,-1) : TILE_XY(-1,0); truelight@0: do tile += delta; while (_map5[tile] & 0x40); truelight@0: } truelight@0: } truelight@0: td->owner = _map_owner[tile]; truelight@0: } truelight@0: truelight@0: truelight@0: static void AnimateTile_TunnelBridge(uint tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: static void TileLoop_TunnelBridge(uint tile) truelight@0: { truelight@0: if (_opt.landscape == LT_HILLY) { truelight@0: if ( GetTileZ(tile) > _opt.snow_line) { truelight@0: if (!(_map3_hi[tile] & 0x80)) { truelight@0: _map3_hi[tile] |= 0x80; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } else { truelight@0: if (_map3_hi[tile] & 0x80) { truelight@0: _map3_hi[tile] &= ~0x80; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: } else if (_opt.landscape == LT_DESERT) { truelight@0: if (GetMapExtraBits(tile) == 1 && !(_map3_hi[tile]&0x80)) { truelight@0: _map3_hi[tile] |= 0x80; truelight@193: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: truelight@0: // if it's a bridge with water below, call tileloop_water on it. truelight@0: if ((_map5[tile] & 0xF8) == 0xC8) TileLoop_Water(tile); truelight@0: } truelight@0: truelight@0: static void ClickTile_TunnelBridge(uint tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: truelight@159: static uint32 GetTileTrackStatus_TunnelBridge(uint tile, TransportType mode) truelight@0: { truelight@0: uint32 result; truelight@159: byte m5 = _map5[tile]; truelight@0: truelight@0: if ((m5 & 0xF0) == 0) { truelight@159: /* This is a tunnel */ truelight@193: if (((m5 & 0xC) >> 2) == mode) { truelight@159: /* Tranport in the tunnel is compatible */ truelight@0: return m5&1 ? 0x202 : 0x101; truelight@0: } truelight@0: } else if (m5 & 0x80) { truelight@159: /* This is a bridge */ truelight@0: result = 0; truelight@159: if (((m5 & 0x6) >> 1) == mode) { truelight@159: /* Transport over the bridge is compatible */ truelight@0: result = m5&1 ? 0x202 : 0x101; truelight@0: } truelight@0: if (m5 & 0x40) { truelight@159: /* Bridge middle part */ truelight@159: if (!(m5 & 0x20)) { truelight@159: /* Clear ground or water underneath */ darkvater@241: if ((m5 & 0x18) != 8) truelight@159: /* Clear ground */ truelight@0: return result; truelight@193: else truelight@159: if (mode != TRANSPORT_WATER) truelight@159: return result; truelight@159: } else { truelight@159: /* Transport underneath */ truelight@159: if ((m5 & 0x18) >> 3 != mode) truelight@159: /* Incompatible transport underneath */ truelight@159: return result; truelight@0: } truelight@159: /* If we've not returned yet, there is a compatible truelight@159: * transport or water beneath, so we can add it to truelight@159: * result */ truelight@159: /* Why is this xor'd ? Can't it just be or'd? */ truelight@0: result ^= m5&1 ? 0x101 : 0x202; truelight@0: } truelight@0: return result; truelight@159: } else { truelight@159: assert(0); /* This should never occur */ truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: static void ChangeTileOwner_TunnelBridge(uint tile, byte old_player, byte new_player) truelight@0: { truelight@0: if (_map_owner[tile] != old_player) truelight@0: return; truelight@193: truelight@0: if (new_player != 255) { truelight@0: _map_owner[tile] = new_player; truelight@0: } else { truelight@0: if((_map5[tile] & 0xC0)==0xC0) { truelight@0: // the stuff BELOW the middle part is owned by the deleted player. truelight@0: if (!(_map5[tile] & (1 << 4 | 1 << 3))) { truelight@0: // convert railway into grass. truelight@0: _map5[tile] &= ~(1 << 5 | 1 << 4 | 1 << 3); // no transport route under bridge anymore.. truelight@0: } else { truelight@0: // for road, change the owner of the road to local authority truelight@0: _map_owner[tile] = OWNER_NONE; truelight@0: } truelight@0: } else { truelight@0: DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static const byte _tunnel_fractcoord_1[4] = {0x8E,0x18,0x81,0xE8}; truelight@0: static const byte _tunnel_fractcoord_2[4] = {0x81,0x98,0x87,0x38}; truelight@0: static const byte _tunnel_fractcoord_3[4] = {0x82,0x88,0x86,0x48}; truelight@0: static const byte _exit_tunnel_track[4] = {1,2,1,2}; truelight@0: truelight@0: static const byte _road_exit_tunnel_state[4] = {8, 9, 0, 1}; truelight@0: static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4}; truelight@0: truelight@0: static const byte _tunnel_fractcoord_4[4] = {0x52, 0x85, 0x98, 0x29}; truelight@0: static const byte _tunnel_fractcoord_5[4] = {0x92, 0x89, 0x58, 0x25}; truelight@0: static const byte _tunnel_fractcoord_6[4] = {0x92, 0x89, 0x56, 0x45}; truelight@0: static const byte _tunnel_fractcoord_7[4] = {0x52, 0x85, 0x96, 0x49}; truelight@0: truelight@0: static uint32 VehicleEnter_TunnelBridge(Vehicle *v, uint tile, int x, int y) truelight@0: { truelight@0: int z; truelight@0: int dir, vdir; truelight@0: byte fc; truelight@0: int h; truelight@0: truelight@0: if ((_map5[tile] & 0xF0) == 0) { truelight@0: z = GetSlopeZ(x, y) - v->z_pos; truelight@0: if (myabs(z) > 2) truelight@0: return 8; truelight@0: truelight@0: if (v->type == VEH_Train) { truelight@0: fc = (x&0xF)+(y<<4); truelight@193: truelight@0: dir = _map5[tile] & 3; truelight@0: vdir = v->direction >> 1; truelight@0: truelight@0: if (v->u.rail.track != 0x40 && dir == vdir) { truelight@0: if (v->subtype == 0 && fc == _tunnel_fractcoord_1[dir]) { truelight@0: if (v->spritenum < 4) tron@541: SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v); truelight@0: return 0; truelight@0: } truelight@0: if (fc == _tunnel_fractcoord_2[dir]) { truelight@0: v->tile = tile; truelight@0: v->u.rail.track = 0x40; truelight@0: v->vehstatus |= VS_HIDDEN; truelight@0: return 4; truelight@0: } truelight@0: } truelight@0: truelight@0: if (dir == (vdir^2) && fc == _tunnel_fractcoord_3[dir] && z == 0) { darkvater@22: /* We're at the tunnel exit ?? */ truelight@0: v->tile = tile; truelight@0: v->u.rail.track = _exit_tunnel_track[dir]; truelight@0: v->vehstatus &= ~VS_HIDDEN; truelight@0: return 4; truelight@0: } truelight@0: } else if (v->type == VEH_Road) { truelight@0: fc = (x&0xF)+(y<<4); truelight@0: dir = _map5[tile] & 3; truelight@0: vdir = v->direction >> 1; truelight@0: truelight@0: // Enter tunnel? truelight@0: if (v->u.road.state != 0xFF && dir == vdir) { truelight@0: if (fc == _tunnel_fractcoord_4[dir] || truelight@0: fc == _tunnel_fractcoord_5[dir]) { truelight@193: truelight@0: v->tile = tile; truelight@0: v->u.road.state = 0xFF; truelight@193: v->vehstatus |= VS_HIDDEN; truelight@0: return 4; truelight@0: } else { truelight@0: return 0; truelight@0: } truelight@0: } truelight@0: truelight@0: if (dir == (vdir^2) && ( darkvater@22: /* We're at the tunnel exit ?? */ truelight@0: fc == _tunnel_fractcoord_6[dir] || truelight@0: fc == _tunnel_fractcoord_7[dir]) && truelight@0: z == 0) { truelight@0: v->tile = tile; truelight@0: v->u.road.state = _road_exit_tunnel_state[dir]; truelight@0: v->u.road.frame = _road_exit_tunnel_frame[dir]; truelight@0: v->vehstatus &= ~VS_HIDDEN; truelight@0: return 4; truelight@0: } truelight@0: } truelight@0: } else if (_map5[tile] & 0x80) { truelight@0: if (v->type == VEH_Road || (v->type == VEH_Train && v->subtype == 0)) { tron@334: if (GetTileSlope(tile, &h) != 0) tron@334: h += 8; // Compensate for possible foundation tron@334: if (!(_map5[tile] & 0x40) || // start/end tile of bridge tron@334: myabs(h - v->z_pos) > 2) { // high above the ground -> on the bridge truelight@0: /* modify speed of vehicle */ truelight@0: uint16 spd = _bridge_speeds[_map2[tile] >> 4]; truelight@0: if (v->type == VEH_Road) spd<<=1; truelight@0: if (spd < v->cur_speed) truelight@0: v->cur_speed = spd; truelight@0: } truelight@0: } truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: uint GetVehicleOutOfTunnelTile(Vehicle *v) truelight@0: { truelight@0: uint tile = v->tile; truelight@0: int delta_tile; truelight@0: byte z; truelight@0: truelight@0: /* locate either ending of the tunnel */ truelight@0: delta_tile = (v->direction&2) ? TILE_XY(0,1) : TILE_XY(1,0); truelight@0: z = v->z_pos; truelight@0: for(;;) { truelight@0: TileInfo ti; truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: truelight@0: if (ti.type == MP_TUNNELBRIDGE && (ti.map5 & 0xF0)==0 && (byte)ti.z == z) truelight@0: break; truelight@0: truelight@0: tile += delta_tile; truelight@0: } truelight@0: return tile; truelight@0: } truelight@0: truelight@0: const TileTypeProcs _tile_type_tunnelbridge_procs = { truelight@0: DrawTile_TunnelBridge, /* draw_tile_proc */ truelight@0: GetSlopeZ_TunnelBridge, /* get_slope_z_proc */ truelight@0: ClearTile_TunnelBridge, /* clear_tile_proc */ truelight@0: GetAcceptedCargo_TunnelBridge, /* get_accepted_cargo_proc */ truelight@0: GetTileDesc_TunnelBridge, /* get_tile_desc_proc */ truelight@0: GetTileTrackStatus_TunnelBridge,/* get_tile_track_status_proc */ truelight@0: ClickTile_TunnelBridge, /* click_tile_proc */ truelight@0: AnimateTile_TunnelBridge, /* animate_tile_proc */ truelight@0: TileLoop_TunnelBridge, /* tile_loop_clear */ truelight@0: ChangeTileOwner_TunnelBridge, /* change_tile_owner_clear */ truelight@0: NULL, /* get_produced_cargo_proc */ truelight@0: VehicleEnter_TunnelBridge, /* vehicle_enter_tile_proc */ truelight@0: NULL, /* vehicle_leave_tile_proc */ dominik@39: GetSlopeTileh_TunnelBridge, /* get_slope_tileh_proc */ truelight@0: };