00001
00002
00003
00004
00005
00006
00007
00008
00009
00016 #include "stdafx.h"
00017 #include "newgrf_object.h"
00018 #include "viewport_func.h"
00019 #include "cmd_helper.h"
00020 #include "command_func.h"
00021 #include "town.h"
00022 #include "train.h"
00023 #include "ship.h"
00024 #include "roadveh.h"
00025 #include "water_map.h"
00026 #include "pathfinder/yapf/yapf_cache.h"
00027 #include "newgrf_sound.h"
00028 #include "autoslope.h"
00029 #include "tunnelbridge_map.h"
00030 #include "strings_func.h"
00031 #include "date_func.h"
00032 #include "functions.h"
00033 #include "vehicle_func.h"
00034 #include "sound_func.h"
00035 #include "tunnelbridge.h"
00036 #include "cheat_type.h"
00037 #include "elrail_func.h"
00038 #include "pbs.h"
00039 #include "company_base.h"
00040 #include "newgrf_railtype.h"
00041 #include "object_base.h"
00042
00043 #include "table/sprites.h"
00044 #include "table/strings.h"
00045 #include "table/bridge_land.h"
00046
00047 BridgeSpec _bridge[MAX_BRIDGES];
00048 TileIndex _build_tunnel_endtile;
00049
00050
00051 static const int BRIDGE_Z_START = 3;
00052
00054 void ResetBridges()
00055 {
00056
00057 for (BridgeType i = 0; i < MAX_BRIDGES; i++) {
00058 if (_bridge[i].sprite_table != NULL) {
00059 for (BridgePieces j = BRIDGE_PIECE_NORTH; j < BRIDGE_PIECE_INVALID; j++) free(_bridge[i].sprite_table[j]);
00060 free(_bridge[i].sprite_table);
00061 }
00062 }
00063
00064
00065 memset(&_bridge, 0, sizeof(_bridge));
00066
00067 memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge));
00068 }
00069
00076 int CalcBridgeLenCostFactor(int length)
00077 {
00078 if (length < 2) return length;
00079
00080 length -= 2;
00081 int sum = 2;
00082 for (int delta = 1;; delta++) {
00083 for (int count = 0; count < delta; count++) {
00084 if (length == 0) return sum;
00085 sum += delta;
00086 length--;
00087 }
00088 }
00089 }
00090
00091 Foundation GetBridgeFoundation(Slope tileh, Axis axis)
00092 {
00093 if (tileh == SLOPE_FLAT ||
00094 ((tileh == SLOPE_NE || tileh == SLOPE_SW) && axis == AXIS_X) ||
00095 ((tileh == SLOPE_NW || tileh == SLOPE_SE) && axis == AXIS_Y)) return FOUNDATION_NONE;
00096
00097 return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh));
00098 }
00099
00107 bool HasBridgeFlatRamp(Slope tileh, Axis axis)
00108 {
00109 ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh);
00110
00111 return (tileh != SLOPE_FLAT);
00112 }
00113
00114 static inline const PalSpriteID *GetBridgeSpriteTable(int index, BridgePieces table)
00115 {
00116 const BridgeSpec *bridge = GetBridgeSpec(index);
00117 assert(table < BRIDGE_PIECE_INVALID);
00118 if (bridge->sprite_table == NULL || bridge->sprite_table[table] == NULL) {
00119 return _bridge_sprite_table[index][table];
00120 } else {
00121 return bridge->sprite_table[table];
00122 }
00123 }
00124
00125
00134 static CommandCost CheckBridgeSlopeNorth(Axis axis, Slope *tileh, uint *z)
00135 {
00136 Foundation f = GetBridgeFoundation(*tileh, axis);
00137 *z += ApplyFoundationToSlope(f, tileh);
00138
00139 Slope valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW);
00140 if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
00141
00142 if (f == FOUNDATION_NONE) return CommandCost();
00143
00144 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00145 }
00146
00155 static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, uint *z)
00156 {
00157 Foundation f = GetBridgeFoundation(*tileh, axis);
00158 *z += ApplyFoundationToSlope(f, tileh);
00159
00160 Slope valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE);
00161 if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
00162
00163 if (f == FOUNDATION_NONE) return CommandCost();
00164
00165 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00166 }
00167
00174 CommandCost CheckBridgeAvailability(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
00175 {
00176 if (flags & DC_QUERY_COST) {
00177 if (bridge_len <= (_settings_game.construction.longbridges ? MAX_BRIDGE_LENGTH_LONGBRIDGES : MAX_BRIDGE_LENGTH)) return CommandCost();
00178 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
00179 }
00180
00181 if (bridge_type >= MAX_BRIDGES) return CMD_ERROR;
00182
00183 const BridgeSpec *b = GetBridgeSpec(bridge_type);
00184 if (b->avail_year > _cur_year) return CMD_ERROR;
00185
00186 uint max = b->max_length;
00187 if (max >= MAX_BRIDGE_LENGTH && _settings_game.construction.longbridges) max = MAX_BRIDGE_LENGTH_LONGBRIDGES;
00188
00189 if (b->min_length > bridge_len) return CMD_ERROR;
00190 if (bridge_len <= max) return CommandCost();
00191 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
00192 }
00193
00206 CommandCost CmdBuildBridge(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00207 {
00208 RailType railtype = INVALID_RAILTYPE;
00209 RoadTypes roadtypes = ROADTYPES_NONE;
00210
00211
00212 BridgeType bridge_type = GB(p2, 0, 8);
00213
00214 if (!IsValidTile(p1)) return_cmd_error(STR_ERROR_BRIDGE_THROUGH_MAP_BORDER);
00215
00216 TransportType transport_type = Extract<TransportType, 15, 2>(p2);
00217
00218
00219 switch (transport_type) {
00220 case TRANSPORT_ROAD:
00221 roadtypes = Extract<RoadTypes, 8, 2>(p2);
00222 if (!HasExactlyOneBit(roadtypes) || !HasRoadTypesAvail(_current_company, roadtypes)) return CMD_ERROR;
00223 break;
00224
00225 case TRANSPORT_RAIL:
00226 railtype = Extract<RailType, 8, 4>(p2);
00227 if (!ValParamRailtype(railtype)) return CMD_ERROR;
00228 break;
00229
00230 case TRANSPORT_WATER:
00231 break;
00232
00233 default:
00234
00235 return CMD_ERROR;
00236 }
00237 TileIndex tile_start = p1;
00238 TileIndex tile_end = end_tile;
00239
00240 if (tile_start == tile_end) {
00241 return_cmd_error(STR_ERROR_CAN_T_START_AND_END_ON);
00242 }
00243
00244 Axis direction;
00245 if (TileX(tile_start) == TileX(tile_end)) {
00246 direction = AXIS_Y;
00247 } else if (TileY(tile_start) == TileY(tile_end)) {
00248 direction = AXIS_X;
00249 } else {
00250 return_cmd_error(STR_ERROR_START_AND_END_MUST_BE_IN);
00251 }
00252
00253 if (tile_end < tile_start) Swap(tile_start, tile_end);
00254
00255 uint bridge_len = GetTunnelBridgeLength(tile_start, tile_end);
00256 if (transport_type != TRANSPORT_WATER) {
00257
00258 CommandCost ret = CheckBridgeAvailability(bridge_type, bridge_len, flags);
00259 if (ret.Failed()) return ret;
00260 } else {
00261 if (bridge_len > (_settings_game.construction.longbridges ? MAX_BRIDGE_LENGTH_LONGBRIDGES : MAX_BRIDGE_LENGTH)) return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
00262 }
00263
00264 uint z_start;
00265 uint z_end;
00266 Slope tileh_start = GetTileSlope(tile_start, &z_start);
00267 Slope tileh_end = GetTileSlope(tile_end, &z_end);
00268 bool pbs_reservation = false;
00269
00270 CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start);
00271 CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end, &z_end);
00272
00273
00274 if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00275 if (z_start != z_end) return_cmd_error(STR_ERROR_BRIDGEHEADS_NOT_SAME_HEIGHT);
00276
00277 CommandCost cost(EXPENSES_CONSTRUCTION);
00278 Owner owner;
00279 if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) &&
00280 GetOtherBridgeEnd(tile_start) == tile_end &&
00281 GetTunnelBridgeTransportType(tile_start) == transport_type) {
00282
00283
00284
00285 if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) {
00286 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00287 }
00288
00289
00290 if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) &&
00291 GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed) {
00292 Town *t = ClosestTownFromTile(tile_start, UINT_MAX);
00293
00294 if (t == NULL) {
00295 return CMD_ERROR;
00296 } else {
00297 SetDParam(0, t->index);
00298 return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
00299 }
00300 }
00301
00302
00303 if (!(flags & DC_QUERY_COST) && bridge_type == GetBridgeType(tile_start)) {
00304 return_cmd_error(STR_ERROR_ALREADY_BUILT);
00305 }
00306
00307
00308 if (!IsTileOwner(tile_start, _current_company) && !IsTileOwner(tile_start, OWNER_TOWN)) {
00309 return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
00310 }
00311
00312 cost.AddCost((bridge_len + 1) * _price[PR_CLEAR_BRIDGE]);
00313 owner = GetTileOwner(tile_start);
00314
00315 switch (transport_type) {
00316 case TRANSPORT_RAIL:
00317
00318 pbs_reservation = HasTunnelBridgeReservation(tile_start);
00319 break;
00320
00321 case TRANSPORT_ROAD:
00322
00323 roadtypes |= GetRoadTypes(tile_start);
00324 break;
00325
00326 default: break;
00327 }
00328 } else {
00329
00330
00331 bool allow_on_slopes = (_settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER);
00332
00333
00334 CommandCost ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00335 if (ret.Failed()) return ret;
00336 cost = ret;
00337
00338 if (terraform_cost_north.Failed() || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00339 cost.AddCost(terraform_cost_north);
00340
00341
00342 ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00343 if (ret.Failed()) return ret;
00344 cost.AddCost(ret);
00345
00346
00347 if (terraform_cost_south.Failed() || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00348 cost.AddCost(terraform_cost_south);
00349
00350 const TileIndex heads[] = {tile_start, tile_end};
00351 for (int i = 0; i < 2; i++) {
00352 if (MayHaveBridgeAbove(heads[i])) {
00353 if (IsBridgeAbove(heads[i])) {
00354 TileIndex north_head = GetNorthernBridgeEnd(heads[i]);
00355
00356 if (direction == GetBridgeAxis(heads[i])) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00357
00358 if (z_start + TILE_HEIGHT == GetBridgeHeight(north_head)) {
00359 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00360 }
00361 }
00362 }
00363 }
00364
00365 TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00366 for (TileIndex tile = tile_start + delta; tile != tile_end; tile += delta) {
00367 if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_ERROR_BRIDGE_TOO_LOW_FOR_TERRAIN);
00368
00369 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) {
00370
00371 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00372 }
00373
00374 switch (GetTileType(tile)) {
00375 case MP_WATER:
00376 if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below;
00377 break;
00378
00379 case MP_RAILWAY:
00380 if (!IsPlainRail(tile)) goto not_valid_below;
00381 break;
00382
00383 case MP_ROAD:
00384 if (IsRoadDepot(tile)) goto not_valid_below;
00385 break;
00386
00387 case MP_TUNNELBRIDGE:
00388 if (IsTunnel(tile)) break;
00389 if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below;
00390 if (z_start < GetBridgeHeight(tile)) goto not_valid_below;
00391 break;
00392
00393 case MP_OBJECT: {
00394 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00395 if ((spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) == 0) goto not_valid_below;
00396 if (GetTileMaxZ(tile) + spec->height * TILE_HEIGHT > z_start) goto not_valid_below;
00397 break;
00398 }
00399
00400 case MP_CLEAR:
00401 break;
00402
00403 default:
00404 not_valid_below:;
00405
00406 ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00407 if (ret.Failed()) return ret;
00408 cost.AddCost(ret);
00409 break;
00410 }
00411
00412 if (flags & DC_EXEC) {
00413
00414
00415
00416 SetBridgeMiddle(tile, direction);
00417 }
00418 }
00419
00420 owner = _current_company;
00421 }
00422
00423
00424 if (flags & DC_EXEC) {
00425 DiagDirection dir = AxisToDiagDir(direction);
00426
00427 switch (transport_type) {
00428 case TRANSPORT_RAIL:
00429 MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype);
00430 MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype);
00431 SetTunnelBridgeReservation(tile_start, pbs_reservation);
00432 SetTunnelBridgeReservation(tile_end, pbs_reservation);
00433 break;
00434
00435 case TRANSPORT_ROAD:
00436 MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir, roadtypes);
00437 MakeRoadBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), roadtypes);
00438 break;
00439
00440 case TRANSPORT_WATER:
00441 MakeAqueductBridgeRamp(tile_start, owner, dir);
00442 MakeAqueductBridgeRamp(tile_end, owner, ReverseDiagDir(dir));
00443 break;
00444
00445 default:
00446 NOT_REACHED();
00447 }
00448
00449
00450 TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00451 for (TileIndex tile = tile_start; tile <= tile_end; tile += delta) {
00452 MarkTileDirtyByTile(tile);
00453 }
00454 }
00455
00456 if ((flags & DC_EXEC) && transport_type == TRANSPORT_RAIL) {
00457 Track track = AxisToTrack(direction);
00458 AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, _current_company);
00459 YapfNotifyTrackLayoutChange(tile_start, track);
00460 }
00461
00462
00463
00464
00465
00466 Company *c = Company::GetIfValid(_current_company);
00467 if (!(flags & DC_QUERY_COST) || (c != NULL && c->is_ai)) {
00468 bridge_len += 2;
00469
00470 switch (transport_type) {
00471 case TRANSPORT_ROAD: cost.AddCost(bridge_len * _price[PR_BUILD_ROAD] * 2); break;
00472 case TRANSPORT_RAIL: cost.AddCost(bridge_len * RailBuildCost(railtype)); break;
00473 default: break;
00474 }
00475
00476 if (c != NULL) bridge_len = CalcBridgeLenCostFactor(bridge_len);
00477
00478 if (transport_type != TRANSPORT_WATER) {
00479 cost.AddCost((int64)bridge_len * _price[PR_BUILD_BRIDGE] * GetBridgeSpec(bridge_type)->price >> 8);
00480 } else {
00481
00482 cost.AddCost((int64)bridge_len * _price[PR_BUILD_AQUEDUCT]);
00483 }
00484
00485 }
00486
00487 return cost;
00488 }
00489
00490
00501 CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00502 {
00503 TransportType transport_type = Extract<TransportType, 8, 2>(p1);
00504
00505 RailType railtype = INVALID_RAILTYPE;
00506 RoadTypes rts = ROADTYPES_NONE;
00507 _build_tunnel_endtile = 0;
00508 switch (transport_type) {
00509 case TRANSPORT_RAIL:
00510 railtype = Extract<RailType, 0, 4>(p1);
00511 if (!ValParamRailtype(railtype)) return CMD_ERROR;
00512 break;
00513
00514 case TRANSPORT_ROAD:
00515 rts = Extract<RoadTypes, 0, 2>(p1);
00516 if (!HasExactlyOneBit(rts) || !HasRoadTypesAvail(_current_company, rts)) return CMD_ERROR;
00517 break;
00518
00519 default: return CMD_ERROR;
00520 }
00521
00522 uint start_z;
00523 uint end_z;
00524 Slope start_tileh = GetTileSlope(start_tile, &start_z);
00525 DiagDirection direction = GetInclinedSlopeDirection(start_tileh);
00526 if (direction == INVALID_DIAGDIR) return_cmd_error(STR_ERROR_SITE_UNSUITABLE_FOR_TUNNEL);
00527
00528 if (HasTileWaterGround(start_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
00529
00530 CommandCost ret = DoCommand(start_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00531 if (ret.Failed()) return ret;
00532
00533
00534
00535
00536
00537
00538 TileIndexDiff delta = TileOffsByDiagDir(direction);
00539 DiagDirection tunnel_in_way_dir;
00540 if (DiagDirToAxis(direction) == AXIS_Y) {
00541 tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE;
00542 } else {
00543 tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW;
00544 }
00545
00546 TileIndex end_tile = start_tile;
00547
00548
00549 int tiles_coef = 3;
00550
00551 int tiles = 0;
00552
00553 int tiles_bump = 25;
00554
00555 CommandCost cost(EXPENSES_CONSTRUCTION);
00556 Slope end_tileh;
00557 for (;;) {
00558 end_tile += delta;
00559 if (!IsValidTile(end_tile)) return_cmd_error(STR_ERROR_TUNNEL_THROUGH_MAP_BORDER);
00560 end_tileh = GetTileSlope(end_tile, &end_z);
00561
00562 if (start_z == end_z) break;
00563
00564 if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
00565 return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY);
00566 }
00567
00568 tiles++;
00569 if (tiles == tiles_bump) {
00570 tiles_coef++;
00571 tiles_bump *= 2;
00572 }
00573
00574 cost.AddCost(_price[PR_BUILD_TUNNEL]);
00575 cost.AddCost(cost.GetCost() >> tiles_coef);
00576 }
00577
00578
00579 cost.AddCost(_price[PR_BUILD_TUNNEL]);
00580 cost.AddCost(ret);
00581
00582
00583 _build_tunnel_endtile = end_tile;
00584
00585 if (HasTileWaterGround(end_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
00586
00587
00588 ret = DoCommand(end_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00589 if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
00590 cost.AddCost(ret);
00591
00592
00593 if (end_tileh != ComplementSlope(start_tileh)) {
00594
00595
00596 ClearedObjectArea *coa = FindClearedObject(end_tile);
00597 if (coa == NULL) {
00598 coa = _cleared_object_areas.Append();
00599 coa->first_tile = end_tile;
00600 coa->area = TileArea(end_tile, 1, 1);
00601 }
00602
00603
00604 TileIndex old_first_tile = coa->first_tile;
00605 coa->first_tile = INVALID_TILE;
00606 ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND);
00607 coa->first_tile = old_first_tile;
00608 if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
00609 cost.AddCost(ret);
00610 }
00611 cost.AddCost(_price[PR_BUILD_TUNNEL]);
00612
00613
00614 switch (transport_type) {
00615 case TRANSPORT_ROAD: cost.AddCost((tiles + 2) * _price[PR_BUILD_ROAD] * 2); break;
00616 case TRANSPORT_RAIL: cost.AddCost((tiles + 2) * RailBuildCost(railtype)); break;
00617 default: break;
00618 }
00619
00620 if (flags & DC_EXEC) {
00621 if (transport_type == TRANSPORT_RAIL) {
00622 MakeRailTunnel(start_tile, _current_company, direction, railtype);
00623 MakeRailTunnel(end_tile, _current_company, ReverseDiagDir(direction), railtype);
00624 AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_company);
00625 YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction));
00626 } else {
00627 MakeRoadTunnel(start_tile, _current_company, direction, rts);
00628 MakeRoadTunnel(end_tile, _current_company, ReverseDiagDir(direction), rts);
00629 }
00630 }
00631
00632 return cost;
00633 }
00634
00635
00641 static inline CommandCost CheckAllowRemoveTunnelBridge(TileIndex tile)
00642 {
00643
00644 if (_current_company == OWNER_WATER || _game_mode == GM_EDITOR) return CommandCost();
00645
00646 switch (GetTunnelBridgeTransportType(tile)) {
00647 case TRANSPORT_ROAD: {
00648 RoadTypes rts = GetRoadTypes(tile);
00649 Owner road_owner = _current_company;
00650 Owner tram_owner = _current_company;
00651
00652 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
00653 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
00654
00655
00656 if (road_owner == OWNER_TOWN && !(_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) {
00657 return CheckTileOwnership(tile);
00658 }
00659 if (road_owner == OWNER_NONE || road_owner == OWNER_TOWN) road_owner = _current_company;
00660 if (tram_owner == OWNER_NONE) tram_owner = _current_company;
00661
00662 CommandCost ret = CheckOwnership(road_owner, tile);
00663 if (ret.Succeeded()) ret = CheckOwnership(tram_owner, tile);
00664 return ret;
00665 }
00666
00667 case TRANSPORT_RAIL:
00668 case TRANSPORT_WATER:
00669 return CheckOwnership(GetTileOwner(tile));
00670
00671 default: NOT_REACHED();
00672 }
00673 }
00674
00681 static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags)
00682 {
00683 CommandCost ret = CheckAllowRemoveTunnelBridge(tile);
00684 if (ret.Failed()) return ret;
00685
00686 TileIndex endtile = GetOtherTunnelEnd(tile);
00687
00688 ret = TunnelBridgeIsFree(tile, endtile);
00689 if (ret.Failed()) return ret;
00690
00691 _build_tunnel_endtile = endtile;
00692
00693 Town *t = NULL;
00694 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00695 t = ClosestTownFromTile(tile, UINT_MAX);
00696
00697
00698
00699 CommandCost ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
00700 if (ret.Failed()) return ret;
00701 }
00702
00703
00704
00705 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00706 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
00707 }
00708
00709 if (flags & DC_EXEC) {
00710 if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
00711
00712 DiagDirection dir = GetTunnelBridgeDirection(tile);
00713 Track track = DiagDirToDiagTrack(dir);
00714 Owner owner = GetTileOwner(tile);
00715
00716 Train *v = NULL;
00717 if (HasTunnelBridgeReservation(tile)) {
00718 v = GetTrainForReservation(tile, track);
00719 if (v != NULL) FreeTrainTrackReservation(v);
00720 }
00721
00722 DoClearSquare(tile);
00723 DoClearSquare(endtile);
00724
00725
00726 AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner);
00727 AddSideToSignalBuffer(endtile, dir, owner);
00728
00729 YapfNotifyTrackLayoutChange(tile, track);
00730 YapfNotifyTrackLayoutChange(endtile, track);
00731
00732 if (v != NULL) TryPathReserve(v);
00733 } else {
00734 DoClearSquare(tile);
00735 DoClearSquare(endtile);
00736 }
00737 }
00738 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_TUNNEL] * (GetTunnelBridgeLength(tile, endtile) + 2));
00739 }
00740
00741
00748 static CommandCost DoClearBridge(TileIndex tile, DoCommandFlag flags)
00749 {
00750 CommandCost ret = CheckAllowRemoveTunnelBridge(tile);
00751 if (ret.Failed()) return ret;
00752
00753 TileIndex endtile = GetOtherBridgeEnd(tile);
00754
00755 ret = TunnelBridgeIsFree(tile, endtile);
00756 if (ret.Failed()) return ret;
00757
00758 DiagDirection direction = GetTunnelBridgeDirection(tile);
00759 TileIndexDiff delta = TileOffsByDiagDir(direction);
00760
00761 Town *t = NULL;
00762 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00763 t = ClosestTownFromTile(tile, UINT_MAX);
00764
00765
00766
00767 CommandCost ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
00768 if (ret.Failed()) return ret;
00769 }
00770
00771
00772
00773 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00774 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
00775 }
00776
00777 Money base_cost = (GetTunnelBridgeTransportType(tile) != TRANSPORT_WATER) ? _price[PR_CLEAR_BRIDGE] : _price[PR_CLEAR_AQUEDUCT];
00778
00779 if (flags & DC_EXEC) {
00780
00781 bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
00782 Owner owner = GetTileOwner(tile);
00783 uint height = GetBridgeHeight(tile);
00784 Train *v = NULL;
00785
00786 if (rail && HasTunnelBridgeReservation(tile)) {
00787 v = GetTrainForReservation(tile, DiagDirToDiagTrack(direction));
00788 if (v != NULL) FreeTrainTrackReservation(v);
00789 }
00790
00791 DoClearSquare(tile);
00792 DoClearSquare(endtile);
00793 for (TileIndex c = tile + delta; c != endtile; c += delta) {
00794
00795 if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) {
00796 uint minz = GetTileMaxZ(c) + 3 * TILE_HEIGHT;
00797 if (height < minz) SetRoadside(c, ROADSIDE_PAVED);
00798 }
00799 ClearBridgeMiddle(c);
00800 MarkTileDirtyByTile(c);
00801 }
00802
00803 if (rail) {
00804
00805 AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner);
00806 AddSideToSignalBuffer(endtile, direction, owner);
00807
00808 Track track = DiagDirToDiagTrack(direction);
00809 YapfNotifyTrackLayoutChange(tile, track);
00810 YapfNotifyTrackLayoutChange(endtile, track);
00811
00812 if (v != NULL) TryPathReserve(v, true);
00813 }
00814 }
00815
00816 return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * base_cost);
00817 }
00818
00825 static CommandCost ClearTile_TunnelBridge(TileIndex tile, DoCommandFlag flags)
00826 {
00827 if (IsTunnel(tile)) {
00828 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST);
00829 return DoClearTunnel(tile, flags);
00830 } else {
00831 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00832 return DoClearBridge(tile, flags);
00833 }
00834 }
00835
00846 static inline void DrawPillar(const PalSpriteID *psid, int x, int y, int z, int w, int h, const SubSprite *subsprite)
00847 {
00848 static const int PILLAR_Z_OFFSET = TILE_HEIGHT - BRIDGE_Z_START;
00849 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, w, h, BB_HEIGHT_UNDER_BRIDGE - PILLAR_Z_OFFSET, z, IsTransparencySet(TO_BRIDGES), 0, 0, -PILLAR_Z_OFFSET, subsprite);
00850 }
00851
00863 static int DrawPillarColumn(int z_bottom, int z_top, const PalSpriteID *psid, int x, int y, int w, int h)
00864 {
00865 int cur_z;
00866 for (cur_z = z_top; cur_z >= z_bottom; cur_z -= TILE_HEIGHT) {
00867 DrawPillar(psid, x, y, cur_z, w, h, NULL);
00868 }
00869 return cur_z;
00870 }
00871
00883 static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
00884 {
00885 static const int bounding_box_size[2] = {16, 2};
00886 static const int back_pillar_offset[2] = { 0, 9};
00887
00888 static const int INF = 1000;
00889 static const SubSprite half_pillar_sub_sprite[2][2] = {
00890 { { -14, -INF, INF, INF }, { -INF, -INF, -15, INF } },
00891 { { -INF, -INF, 15, INF }, { 16, -INF, INF, INF } },
00892 };
00893
00894 if (psid->sprite == 0) return;
00895
00896
00897 DiagDirection south_dir = AxisToDiagDir(axis);
00898 int z_front_north = ti->z;
00899 int z_back_north = ti->z;
00900 int z_front_south = ti->z;
00901 int z_back_south = ti->z;
00902 GetSlopeZOnEdge(ti->tileh, south_dir, &z_front_south, &z_back_south);
00903 GetSlopeZOnEdge(ti->tileh, ReverseDiagDir(south_dir), &z_front_north, &z_back_north);
00904
00905
00906 int z_front = max(z_front_north, z_front_south);
00907 int z_back = max(z_back_north, z_back_south);
00908
00909
00910 int w = bounding_box_size[axis];
00911 int h = bounding_box_size[OtherAxis(axis)];
00912
00913 int x_back = x - back_pillar_offset[axis];
00914 int y_back = y - back_pillar_offset[OtherAxis(axis)];
00915
00916
00917 int bottom_z = DrawPillarColumn(z_front, z_bridge, psid, x, y, w, h);
00918 if (z_front_north < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
00919 if (z_front_south < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
00920
00921
00922 int z_bridge_back = z_bridge - 2 * (int)TILE_HEIGHT;
00923 if (drawfarpillar && (z_back_north <= z_bridge_back || z_back_south <= z_bridge_back)) {
00924 bottom_z = DrawPillarColumn(z_back, z_bridge_back, psid, x_back, y_back, w, h);
00925 if (z_back_north < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
00926 if (z_back_south < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
00927 }
00928 }
00929
00939 static void DrawBridgeTramBits(int x, int y, byte z, int offset, bool overlay, bool head)
00940 {
00941 static const SpriteID tram_offsets[2][6] = { { 107, 108, 109, 110, 111, 112 }, { 4, 5, 15, 16, 17, 18 } };
00942 static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 };
00943 static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 };
00944
00945 static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 };
00946 static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 };
00947 static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 };
00948 static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 };
00949
00950
00951
00952 if (head || !IsInvisibilitySet(TO_BRIDGES)) {
00953 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE,
00954 x, y, size_x[offset], size_y[offset], 0x28, z,
00955 !head && IsTransparencySet(TO_BRIDGES));
00956 }
00957
00958
00959 if (!IsInvisibilitySet(TO_CATENARY)) {
00960 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset], PAL_NONE,
00961 x, y, size_x[offset], size_y[offset], 0x28, z,
00962 IsTransparencySet(TO_CATENARY));
00963 }
00964
00965
00966 EndSpriteCombine();
00967 StartSpriteCombine();
00968
00969
00970 if (!IsInvisibilitySet(TO_CATENARY)) {
00971 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE,
00972 x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z,
00973 IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]);
00974 }
00975 }
00976
00990 static void DrawTile_TunnelBridge(TileInfo *ti)
00991 {
00992 TransportType transport_type = GetTunnelBridgeTransportType(ti->tile);
00993 DiagDirection tunnelbridge_direction = GetTunnelBridgeDirection(ti->tile);
00994
00995 if (IsTunnel(ti->tile)) {
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005 static const int _tunnel_BB[4][12] = {
01006
01007
01008 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 },
01009 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 },
01010 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 },
01011 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 },
01012 };
01013 const int *BB_data = _tunnel_BB[tunnelbridge_direction];
01014
01015 bool catenary = false;
01016
01017 SpriteID image;
01018 if (transport_type == TRANSPORT_RAIL) {
01019 image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
01020 } else {
01021 image = SPR_TUNNEL_ENTRY_REAR_ROAD;
01022 }
01023
01024 if (HasTunnelBridgeSnowOrDesert(ti->tile)) image += 32;
01025
01026 image += tunnelbridge_direction * 2;
01027 DrawGroundSprite(image, PAL_NONE);
01028
01029
01030 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && (transport_type == TRANSPORT_RAIL && HasTunnelBridgeReservation(ti->tile))) {
01031 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01032 DrawGroundSprite(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH);
01033 }
01034
01035 if (transport_type == TRANSPORT_ROAD) {
01036 RoadTypes rts = GetRoadTypes(ti->tile);
01037
01038 if (HasBit(rts, ROADTYPE_TRAM)) {
01039 static const SpriteID tunnel_sprites[2][4] = { { 28, 78, 79, 27 }, { 5, 76, 77, 4 } };
01040
01041 DrawGroundSprite(SPR_TRAMWAY_BASE + tunnel_sprites[rts - ROADTYPES_TRAM][tunnelbridge_direction], PAL_NONE);
01042
01043
01044 if (!IsInvisibilitySet(TO_CATENARY)) {
01045 catenary = true;
01046 StartSpriteCombine();
01047 AddSortableSpriteToDraw(SPR_TRAMWAY_TUNNEL_WIRES + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, BB_data[10], BB_data[11], TILE_HEIGHT, ti->z, IsTransparencySet(TO_CATENARY), BB_data[8], BB_data[9], BB_Z_SEPARATOR);
01048 }
01049 }
01050 } else {
01051 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01052 if (rti->UsesOverlay()) {
01053 SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_TUNNEL);
01054 if (surface != 0) DrawGroundSprite(surface + tunnelbridge_direction, PAL_NONE);
01055 }
01056
01057 if (HasCatenaryDrawn(GetRailType(ti->tile))) {
01058
01059 DrawCatenary(ti);
01060
01061 catenary = true;
01062 StartSpriteCombine();
01063
01064 DrawCatenaryOnTunnel(ti);
01065 }
01066 }
01067
01068 AddSortableSpriteToDraw(image + 1, PAL_NONE, ti->x + TILE_SIZE - 1, ti->y + TILE_SIZE - 1, BB_data[0], BB_data[1], TILE_HEIGHT, ti->z, false, BB_data[2], BB_data[3], BB_Z_SEPARATOR);
01069
01070 if (catenary) EndSpriteCombine();
01071
01072
01073 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x, ti->y, BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
01074 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x + BB_data[4], ti->y + BB_data[5], BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
01075
01076 DrawBridgeMiddle(ti);
01077 } else {
01078 const PalSpriteID *psid;
01079 int base_offset;
01080 bool ice = HasTunnelBridgeSnowOrDesert(ti->tile);
01081
01082 if (transport_type == TRANSPORT_RAIL) {
01083 base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset;
01084 assert(base_offset != 8);
01085 } else {
01086 base_offset = 8;
01087 }
01088
01089
01090 assert( (base_offset & 0x07) == 0x00);
01091
01092 DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(tunnelbridge_direction)));
01093
01094
01095 base_offset += (6 - tunnelbridge_direction) % 4;
01096
01097 if (ti->tileh == SLOPE_FLAT) base_offset += 4;
01098
01099
01100 if (transport_type != TRANSPORT_WATER) {
01101 psid = &GetBridgeSpriteTable(GetBridgeType(ti->tile), BRIDGE_PIECE_HEAD)[base_offset];
01102 } else {
01103 psid = _aqueduct_sprites + base_offset;
01104 }
01105
01106 if (!ice) {
01107 DrawClearLandTile(ti, 3);
01108 } else {
01109 DrawGroundSprite(SPR_FLAT_SNOW_DESERT_TILE + SlopeToSpriteOffset(ti->tileh), PAL_NONE);
01110 }
01111
01112
01113
01114
01115 if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
01116
01117
01118
01119
01120
01121 AddSortableSpriteToDraw(psid->sprite, psid->pal, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 0 : 8, ti->z);
01122
01123 if (transport_type == TRANSPORT_ROAD) {
01124 RoadTypes rts = GetRoadTypes(ti->tile);
01125
01126 if (HasBit(rts, ROADTYPE_TRAM)) {
01127 uint offset = tunnelbridge_direction;
01128 uint z = ti->z;
01129 if (ti->tileh != SLOPE_FLAT) {
01130 offset = (offset + 1) & 1;
01131 z += TILE_HEIGHT;
01132 } else {
01133 offset += 2;
01134 }
01135
01136 DrawBridgeTramBits(ti->x, ti->y, z, offset, HasBit(rts, ROADTYPE_ROAD), true);
01137 }
01138 EndSpriteCombine();
01139 } else if (transport_type == TRANSPORT_RAIL) {
01140 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01141 if (rti->UsesOverlay()) {
01142 SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_BRIDGE);
01143 if (surface != 0) {
01144 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
01145 AddSortableSpriteToDraw(surface + ((DiagDirToAxis(tunnelbridge_direction) == AXIS_X) ? RTBO_X : RTBO_Y), PAL_NONE, ti->x, ti->y, 16, 16, 0, ti->z + 8);
01146 } else {
01147 AddSortableSpriteToDraw(surface + RTBO_SLOPE + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, 16, 16, 8, ti->z);
01148 }
01149 }
01150
01151
01152
01153 } else if (_game_mode != GM_MENU &&_settings_client.gui.show_track_reservation && HasTunnelBridgeReservation(ti->tile)) {
01154 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
01155 AddSortableSpriteToDraw(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, ti->z + 8);
01156 } else {
01157 AddSortableSpriteToDraw(rti->base_sprites.single_sloped + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
01158 }
01159 }
01160 EndSpriteCombine();
01161 if (HasCatenaryDrawn(GetRailType(ti->tile))) {
01162 DrawCatenary(ti);
01163 }
01164 }
01165
01166 DrawBridgeMiddle(ti);
01167 }
01168 }
01169
01170
01189 static BridgePieces CalcBridgePiece(uint north, uint south)
01190 {
01191 if (north == 1) {
01192 return BRIDGE_PIECE_NORTH;
01193 } else if (south == 1) {
01194 return BRIDGE_PIECE_SOUTH;
01195 } else if (north < south) {
01196 return north & 1 ? BRIDGE_PIECE_INNER_SOUTH : BRIDGE_PIECE_INNER_NORTH;
01197 } else if (north > south) {
01198 return south & 1 ? BRIDGE_PIECE_INNER_NORTH : BRIDGE_PIECE_INNER_SOUTH;
01199 } else {
01200 return north & 1 ? BRIDGE_PIECE_MIDDLE_EVEN : BRIDGE_PIECE_MIDDLE_ODD;
01201 }
01202 }
01203
01204
01205 void DrawBridgeMiddle(const TileInfo *ti)
01206 {
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223 if (!IsBridgeAbove(ti->tile)) return;
01224
01225 TileIndex rampnorth = GetNorthernBridgeEnd(ti->tile);
01226 TileIndex rampsouth = GetSouthernBridgeEnd(ti->tile);
01227 TransportType transport_type = GetTunnelBridgeTransportType(rampsouth);
01228
01229 Axis axis = GetBridgeAxis(ti->tile);
01230 BridgePieces piece = CalcBridgePiece(
01231 GetTunnelBridgeLength(ti->tile, rampnorth) + 1,
01232 GetTunnelBridgeLength(ti->tile, rampsouth) + 1
01233 );
01234
01235 const PalSpriteID *psid;
01236 bool drawfarpillar;
01237 if (transport_type != TRANSPORT_WATER) {
01238 BridgeType type = GetBridgeType(rampsouth);
01239 drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0);
01240
01241 uint base_offset;
01242 if (transport_type == TRANSPORT_RAIL) {
01243 base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset;
01244 } else {
01245 base_offset = 8;
01246 }
01247
01248 psid = base_offset + GetBridgeSpriteTable(type, piece);
01249 } else {
01250 drawfarpillar = true;
01251 psid = _aqueduct_sprites;
01252 }
01253
01254 if (axis != AXIS_X) psid += 4;
01255
01256 int x = ti->x;
01257 int y = ti->y;
01258 uint bridge_z = GetBridgeHeight(rampsouth);
01259 uint z = bridge_z - BRIDGE_Z_START;
01260
01261
01262 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, x, y, 16, 16, 1, bridge_z - TILE_HEIGHT + BB_Z_SEPARATOR);
01263
01264
01265 if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
01266
01267
01268 if (!IsInvisibilitySet(TO_BRIDGES)) {
01269 if (axis == AXIS_X) {
01270 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 1, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
01271 } else {
01272 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 1, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
01273 }
01274 }
01275
01276 psid++;
01277
01278 if (transport_type == TRANSPORT_ROAD) {
01279 RoadTypes rts = GetRoadTypes(rampsouth);
01280
01281 if (HasBit(rts, ROADTYPE_TRAM)) {
01282
01283 DrawBridgeTramBits(x, y, bridge_z, axis ^ 1, HasBit(rts, ROADTYPE_ROAD), false);
01284 } else {
01285 EndSpriteCombine();
01286 StartSpriteCombine();
01287 }
01288 } else if (transport_type == TRANSPORT_RAIL) {
01289 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(rampsouth));
01290 if (rti->UsesOverlay()) {
01291 SpriteID surface = GetCustomRailSprite(rti, rampsouth, RTSG_BRIDGE, TCX_ON_BRIDGE);
01292 if (surface != 0) {
01293 AddSortableSpriteToDraw(surface + axis, PAL_NONE, x, y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
01294 }
01295 }
01296 EndSpriteCombine();
01297
01298 if (HasCatenaryDrawn(GetRailType(rampsouth))) {
01299 DrawCatenaryOnBridge(ti);
01300 }
01301 }
01302
01303
01304 if (!IsInvisibilitySet(TO_BRIDGES)) {
01305 if (axis == AXIS_X) {
01306 y += 12;
01307 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 4, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 3, BRIDGE_Z_START);
01308 } else {
01309 x += 12;
01310 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 4, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 3, 0, BRIDGE_Z_START);
01311 }
01312 }
01313
01314
01315 if (transport_type == TRANSPORT_ROAD) EndSpriteCombine();
01316
01317
01318 if (IsInvisibilitySet(TO_BRIDGES)) return;
01319
01320 psid++;
01321 if (ti->z + 5 == z) {
01322
01323 if (psid->sprite != 0) {
01324 SpriteID image = psid->sprite;
01325 SpriteID pal = psid->pal;
01326 if (IsTransparencySet(TO_BRIDGES)) {
01327 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
01328 pal = PALETTE_TO_TRANSPARENT;
01329 }
01330
01331 DrawGroundSpriteAt(image, pal, x - ti->x, y - ti->y, z - ti->z);
01332 }
01333 } else if (_settings_client.gui.bridge_pillars) {
01334
01335 DrawBridgePillars(psid, ti, axis, drawfarpillar, x, y, z);
01336 }
01337 }
01338
01339
01340 static uint GetSlopeZ_TunnelBridge(TileIndex tile, uint x, uint y)
01341 {
01342 uint z;
01343 Slope tileh = GetTileSlope(tile, &z);
01344
01345 x &= 0xF;
01346 y &= 0xF;
01347
01348 if (IsTunnel(tile)) {
01349 uint pos = (DiagDirToAxis(GetTunnelBridgeDirection(tile)) == AXIS_X ? y : x);
01350
01351
01352 if (5 <= pos && pos <= 10) return z;
01353 } else {
01354 DiagDirection dir = GetTunnelBridgeDirection(tile);
01355 uint pos = (DiagDirToAxis(dir) == AXIS_X ? y : x);
01356
01357 z += ApplyFoundationToSlope(GetBridgeFoundation(tileh, DiagDirToAxis(dir)), &tileh);
01358
01359
01360 if (5 <= pos && pos <= 10) {
01361 uint delta;
01362
01363 if (tileh != SLOPE_FLAT) return z + TILE_HEIGHT;
01364
01365 switch (dir) {
01366 default: NOT_REACHED();
01367 case DIAGDIR_NE: delta = (TILE_SIZE - 1 - x) / 2; break;
01368 case DIAGDIR_SE: delta = y / 2; break;
01369 case DIAGDIR_SW: delta = x / 2; break;
01370 case DIAGDIR_NW: delta = (TILE_SIZE - 1 - y) / 2; break;
01371 }
01372 return z + 1 + delta;
01373 }
01374 }
01375
01376 return z + GetPartialZ(x, y, tileh);
01377 }
01378
01379 static Foundation GetFoundation_TunnelBridge(TileIndex tile, Slope tileh)
01380 {
01381 return IsTunnel(tile) ? FOUNDATION_NONE : GetBridgeFoundation(tileh, DiagDirToAxis(GetTunnelBridgeDirection(tile)));
01382 }
01383
01384 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
01385 {
01386 TransportType tt = GetTunnelBridgeTransportType(tile);
01387
01388 if (IsTunnel(tile)) {
01389 td->str = (tt == TRANSPORT_RAIL) ? STR_LAI_TUNNEL_DESCRIPTION_RAILROAD : STR_LAI_TUNNEL_DESCRIPTION_ROAD;
01390 } else {
01391 td->str = (tt == TRANSPORT_WATER) ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : GetBridgeSpec(GetBridgeType(tile))->transport_name[tt];
01392 }
01393 td->owner[0] = GetTileOwner(tile);
01394
01395 Owner road_owner = INVALID_OWNER;
01396 Owner tram_owner = INVALID_OWNER;
01397 RoadTypes rts = GetRoadTypes(tile);
01398 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01399 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01400
01401
01402 if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
01403 (road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
01404 uint i = 1;
01405 if (road_owner != INVALID_OWNER) {
01406 td->owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
01407 td->owner[i] = road_owner;
01408 i++;
01409 }
01410 if (tram_owner != INVALID_OWNER) {
01411 td->owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
01412 td->owner[i] = tram_owner;
01413 }
01414 }
01415
01416 if (tt == TRANSPORT_RAIL) {
01417 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
01418 td->rail_speed = rti->max_speed;
01419
01420 if (!IsTunnel(tile)) {
01421 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
01422 if (td->rail_speed == 0 || spd < td->rail_speed) {
01423 td->rail_speed = spd;
01424 }
01425 }
01426 }
01427 }
01428
01429
01430 static void TileLoop_TunnelBridge(TileIndex tile)
01431 {
01432 bool snow_or_desert = HasTunnelBridgeSnowOrDesert(tile);
01433 switch (_settings_game.game_creation.landscape) {
01434 case LT_ARCTIC: {
01435
01436
01437
01438 uint z = IsBridge(tile) ? GetTileMaxZ(tile) : GetTileZ(tile);
01439 if (snow_or_desert != (z > GetSnowLine())) {
01440 SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert);
01441 MarkTileDirtyByTile(tile);
01442 }
01443 break;
01444 }
01445
01446 case LT_TROPIC:
01447 if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
01448 SetTunnelBridgeSnowOrDesert(tile, true);
01449 MarkTileDirtyByTile(tile);
01450 }
01451 break;
01452
01453 default:
01454 break;
01455 }
01456 }
01457
01458 static TrackStatus GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
01459 {
01460 TransportType transport_type = GetTunnelBridgeTransportType(tile);
01461 if (transport_type != mode || (transport_type == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0)) return 0;
01462
01463 DiagDirection dir = GetTunnelBridgeDirection(tile);
01464 if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0;
01465 return CombineTrackStatus(TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)), TRACKDIR_BIT_NONE);
01466 }
01467
01468 static void ChangeTileOwner_TunnelBridge(TileIndex tile, Owner old_owner, Owner new_owner)
01469 {
01470 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
01471
01472 if (GetRoadOwner(tile, rt) == old_owner) {
01473 SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
01474 }
01475 }
01476
01477 if (!IsTileOwner(tile, old_owner)) return;
01478
01479 if (new_owner != INVALID_OWNER) {
01480 SetTileOwner(tile, new_owner);
01481 } else {
01482 if (DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR).Failed()) {
01483
01484
01485
01486
01487 assert(GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL);
01488 SetTileOwner(tile, OWNER_NONE);
01489 }
01490 }
01491 }
01492
01498 static const byte TUNNEL_SOUND_FRAME = 1;
01499
01508 extern const byte _tunnel_visibility_frame[DIAGDIR_END] = {12, 8, 8, 12};
01509
01510 static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
01511 {
01512 int z = GetSlopeZ(x, y) - v->z_pos;
01513
01514 if (abs(z) > 2) return VETSB_CANNOT_ENTER;
01515
01516 const DiagDirection dir = GetTunnelBridgeDirection(tile);
01517
01518 const DiagDirection vdir = DirToDiagDir(v->direction);
01519
01520 byte pos = (DiagDirToAxis(vdir) == AXIS_X ? x : y) & TILE_UNIT_MASK;
01521
01522 byte frame = (vdir == DIAGDIR_NE || vdir == DIAGDIR_NW) ? TILE_SIZE - 1 - pos : pos;
01523
01524 if (IsTunnel(tile)) {
01525 if (v->type == VEH_TRAIN) {
01526 Train *t = Train::From(v);
01527
01528 if (t->track != TRACK_BIT_WORMHOLE && dir == vdir) {
01529 if (t->IsFrontEngine() && frame == TUNNEL_SOUND_FRAME) {
01530 if (!PlayVehicleSound(t, VSE_TUNNEL) && RailVehInfo(t->engine_type)->engclass == 0) {
01531 SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
01532 }
01533 return VETSB_CONTINUE;
01534 }
01535 if (frame == _tunnel_visibility_frame[dir]) {
01536 t->tile = tile;
01537 t->track = TRACK_BIT_WORMHOLE;
01538 t->vehstatus |= VS_HIDDEN;
01539 return VETSB_ENTERED_WORMHOLE;
01540 }
01541 }
01542
01543 if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
01544
01545 t->tile = tile;
01546 t->track = DiagDirToDiagTrackBits(vdir);
01547 assert(t->track);
01548 t->vehstatus &= ~VS_HIDDEN;
01549 return VETSB_ENTERED_WORMHOLE;
01550 }
01551 } else if (v->type == VEH_ROAD) {
01552 RoadVehicle *rv = RoadVehicle::From(v);
01553
01554
01555 if (rv->state != RVSB_WORMHOLE && dir == vdir) {
01556 if (frame == _tunnel_visibility_frame[dir]) {
01557
01558 assert(frame == rv->frame + 1);
01559 rv->tile = tile;
01560 rv->state = RVSB_WORMHOLE;
01561 rv->vehstatus |= VS_HIDDEN;
01562 return VETSB_ENTERED_WORMHOLE;
01563 } else {
01564 return VETSB_CONTINUE;
01565 }
01566 }
01567
01568
01569 if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
01570 rv->tile = tile;
01571 rv->state = DiagDirToDiagTrackdir(vdir);
01572 rv->frame = frame;
01573 rv->vehstatus &= ~VS_HIDDEN;
01574 return VETSB_ENTERED_WORMHOLE;
01575 }
01576 }
01577 } else {
01578 if (v->type != VEH_SHIP) {
01579
01580 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
01581
01582 if (v->type == VEH_ROAD) spd *= 2;
01583 Vehicle *first = v->First();
01584 first->cur_speed = min(first->cur_speed, spd);
01585 }
01586
01587 if (vdir == dir) {
01588
01589 if (frame != TILE_SIZE - 1) return VETSB_CONTINUE;
01590 switch (v->type) {
01591 case VEH_TRAIN: {
01592 Train *t = Train::From(v);
01593 t->track = TRACK_BIT_WORMHOLE;
01594 ClrBit(t->gv_flags, GVF_GOINGUP_BIT);
01595 ClrBit(t->gv_flags, GVF_GOINGDOWN_BIT);
01596 break;
01597 }
01598
01599 case VEH_ROAD: {
01600 RoadVehicle *rv = RoadVehicle::From(v);
01601 rv->state = RVSB_WORMHOLE;
01602
01603 ClrBit(rv->gv_flags, GVF_GOINGUP_BIT);
01604 ClrBit(rv->gv_flags, GVF_GOINGDOWN_BIT);
01605 break;
01606 }
01607
01608 case VEH_SHIP:
01609 Ship::From(v)->state = TRACK_BIT_WORMHOLE;
01610 break;
01611
01612 default: NOT_REACHED();
01613 }
01614 return VETSB_ENTERED_WORMHOLE;
01615 } else if (vdir == ReverseDiagDir(dir)) {
01616 v->tile = tile;
01617 switch (v->type) {
01618 case VEH_TRAIN: {
01619 Train *t = Train::From(v);
01620 if (t->track == TRACK_BIT_WORMHOLE) {
01621 t->track = DiagDirToDiagTrackBits(vdir);
01622 return VETSB_ENTERED_WORMHOLE;
01623 }
01624 break;
01625 }
01626
01627 case VEH_ROAD: {
01628 RoadVehicle *rv = RoadVehicle::From(v);
01629 if (rv->state == RVSB_WORMHOLE) {
01630 rv->state = DiagDirToDiagTrackdir(vdir);
01631 rv->frame = 0;
01632 return VETSB_ENTERED_WORMHOLE;
01633 }
01634 break;
01635 }
01636
01637 case VEH_SHIP: {
01638 Ship *ship = Ship::From(v);
01639 if (ship->state == TRACK_BIT_WORMHOLE) {
01640 ship->state = DiagDirToDiagTrackBits(vdir);
01641 return VETSB_ENTERED_WORMHOLE;
01642 }
01643 break;
01644 }
01645
01646 default: NOT_REACHED();
01647 }
01648 }
01649 }
01650 return VETSB_CONTINUE;
01651 }
01652
01653 static CommandCost TerraformTile_TunnelBridge(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
01654 {
01655 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled() && IsBridge(tile) && GetTunnelBridgeTransportType(tile) != TRANSPORT_WATER) {
01656 DiagDirection direction = GetTunnelBridgeDirection(tile);
01657 Axis axis = DiagDirToAxis(direction);
01658 CommandCost res;
01659 uint z_old;
01660 Slope tileh_old = GetTileSlope(tile, &z_old);
01661
01662
01663 if ((direction == DIAGDIR_NW) || (direction == DIAGDIR_NE)) {
01664 CheckBridgeSlopeSouth(axis, &tileh_old, &z_old);
01665 res = CheckBridgeSlopeSouth(axis, &tileh_new, &z_new);
01666 } else {
01667 CheckBridgeSlopeNorth(axis, &tileh_old, &z_old);
01668 res = CheckBridgeSlopeNorth(axis, &tileh_new, &z_new);
01669 }
01670
01671
01672 if (res.Succeeded() && (z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01673 }
01674
01675 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
01676 }
01677
01678 extern const TileTypeProcs _tile_type_tunnelbridge_procs = {
01679 DrawTile_TunnelBridge,
01680 GetSlopeZ_TunnelBridge,
01681 ClearTile_TunnelBridge,
01682 NULL,
01683 GetTileDesc_TunnelBridge,
01684 GetTileTrackStatus_TunnelBridge,
01685 NULL,
01686 NULL,
01687 TileLoop_TunnelBridge,
01688 ChangeTileOwner_TunnelBridge,
01689 NULL,
01690 VehicleEnter_TunnelBridge,
01691 GetFoundation_TunnelBridge,
01692 TerraformTile_TunnelBridge,
01693 };