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 "pathfinder/yapf/yapf_cache.h"
00026 #include "newgrf_sound.h"
00027 #include "autoslope.h"
00028 #include "tunnelbridge_map.h"
00029 #include "strings_func.h"
00030 #include "date_func.h"
00031 #include "clear_func.h"
00032 #include "vehicle_func.h"
00033 #include "sound_func.h"
00034 #include "tunnelbridge.h"
00035 #include "cheat_type.h"
00036 #include "elrail_func.h"
00037 #include "pbs.h"
00038 #include "company_base.h"
00039 #include "newgrf_railtype.h"
00040 #include "object_base.h"
00041 #include "water.h"
00042 #include "company_gui.h"
00043
00044 #include "table/strings.h"
00045 #include "table/bridge_land.h"
00046
00047 BridgeSpec _bridge[MAX_BRIDGES];
00048 TileIndex _build_tunnel_endtile;
00049
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
00097 Foundation GetBridgeFoundation(Slope tileh, Axis axis)
00098 {
00099 if (tileh == SLOPE_FLAT ||
00100 ((tileh == SLOPE_NE || tileh == SLOPE_SW) && axis == AXIS_X) ||
00101 ((tileh == SLOPE_NW || tileh == SLOPE_SE) && axis == AXIS_Y)) return FOUNDATION_NONE;
00102
00103 return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh));
00104 }
00105
00113 bool HasBridgeFlatRamp(Slope tileh, Axis axis)
00114 {
00115 ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh);
00116
00117 return (tileh != SLOPE_FLAT);
00118 }
00119
00120 static inline const PalSpriteID *GetBridgeSpriteTable(int index, BridgePieces table)
00121 {
00122 const BridgeSpec *bridge = GetBridgeSpec(index);
00123 assert(table < BRIDGE_PIECE_INVALID);
00124 if (bridge->sprite_table == NULL || bridge->sprite_table[table] == NULL) {
00125 return _bridge_sprite_table[index][table];
00126 } else {
00127 return bridge->sprite_table[table];
00128 }
00129 }
00130
00131
00140 static CommandCost CheckBridgeSlopeNorth(Axis axis, Slope *tileh, int *z)
00141 {
00142 Foundation f = GetBridgeFoundation(*tileh, axis);
00143 *z += ApplyFoundationToSlope(f, tileh);
00144
00145 Slope valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW);
00146 if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
00147
00148 if (f == FOUNDATION_NONE) return CommandCost();
00149
00150 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00151 }
00152
00161 static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, int *z)
00162 {
00163 Foundation f = GetBridgeFoundation(*tileh, axis);
00164 *z += ApplyFoundationToSlope(f, tileh);
00165
00166 Slope valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE);
00167 if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
00168
00169 if (f == FOUNDATION_NONE) return CommandCost();
00170
00171 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00172 }
00173
00180 CommandCost CheckBridgeAvailability(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
00181 {
00182 if (flags & DC_QUERY_COST) {
00183 if (bridge_len <= _settings_game.construction.max_bridge_length) return CommandCost();
00184 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
00185 }
00186
00187 if (bridge_type >= MAX_BRIDGES) return CMD_ERROR;
00188
00189 const BridgeSpec *b = GetBridgeSpec(bridge_type);
00190 if (b->avail_year > _cur_year) return CMD_ERROR;
00191
00192 uint max = min(b->max_length, _settings_game.construction.max_bridge_length);
00193
00194 if (b->min_length > bridge_len) return CMD_ERROR;
00195 if (bridge_len <= max) return CommandCost();
00196 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
00197 }
00198
00211 CommandCost CmdBuildBridge(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00212 {
00213 CompanyID company = _current_company;
00214
00215 RailType railtype = INVALID_RAILTYPE;
00216 RoadTypes roadtypes = ROADTYPES_NONE;
00217
00218
00219 BridgeType bridge_type = GB(p2, 0, 8);
00220
00221 if (!IsValidTile(p1)) return_cmd_error(STR_ERROR_BRIDGE_THROUGH_MAP_BORDER);
00222
00223 TransportType transport_type = Extract<TransportType, 15, 2>(p2);
00224
00225
00226 switch (transport_type) {
00227 case TRANSPORT_ROAD:
00228 roadtypes = Extract<RoadTypes, 8, 2>(p2);
00229 if (!HasExactlyOneBit(roadtypes) || !HasRoadTypesAvail(company, roadtypes)) return CMD_ERROR;
00230 break;
00231
00232 case TRANSPORT_RAIL:
00233 railtype = Extract<RailType, 8, 4>(p2);
00234 if (!ValParamRailtype(railtype)) return CMD_ERROR;
00235 break;
00236
00237 case TRANSPORT_WATER:
00238 break;
00239
00240 default:
00241
00242 return CMD_ERROR;
00243 }
00244 TileIndex tile_start = p1;
00245 TileIndex tile_end = end_tile;
00246
00247 if (company == OWNER_DEITY) {
00248 if (transport_type != TRANSPORT_ROAD) return CMD_ERROR;
00249 const Town *town = CalcClosestTownFromTile(tile_start);
00250
00251 company = OWNER_TOWN;
00252
00253
00254 if (town == NULL || DistanceSquare(tile_start, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
00255 company = OWNER_NONE;
00256 }
00257 }
00258
00259 if (tile_start == tile_end) {
00260 return_cmd_error(STR_ERROR_CAN_T_START_AND_END_ON);
00261 }
00262
00263 Axis direction;
00264 if (TileX(tile_start) == TileX(tile_end)) {
00265 direction = AXIS_Y;
00266 } else if (TileY(tile_start) == TileY(tile_end)) {
00267 direction = AXIS_X;
00268 } else {
00269 return_cmd_error(STR_ERROR_START_AND_END_MUST_BE_IN);
00270 }
00271
00272 if (tile_end < tile_start) Swap(tile_start, tile_end);
00273
00274 uint bridge_len = GetTunnelBridgeLength(tile_start, tile_end);
00275 if (transport_type != TRANSPORT_WATER) {
00276
00277 CommandCost ret = CheckBridgeAvailability(bridge_type, bridge_len, flags);
00278 if (ret.Failed()) return ret;
00279 } else {
00280 if (bridge_len > _settings_game.construction.max_bridge_length) return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
00281 }
00282
00283 int z_start;
00284 int z_end;
00285 Slope tileh_start = GetTileSlope(tile_start, &z_start);
00286 Slope tileh_end = GetTileSlope(tile_end, &z_end);
00287 bool pbs_reservation = false;
00288
00289 CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start);
00290 CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end, &z_end);
00291
00292
00293 if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00294 if (z_start != z_end) return_cmd_error(STR_ERROR_BRIDGEHEADS_NOT_SAME_HEIGHT);
00295
00296 CommandCost cost(EXPENSES_CONSTRUCTION);
00297 Owner owner;
00298 bool is_new_owner;
00299 if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) &&
00300 GetOtherBridgeEnd(tile_start) == tile_end &&
00301 GetTunnelBridgeTransportType(tile_start) == transport_type) {
00302
00303
00304
00305 if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) {
00306 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00307 }
00308
00309
00310 if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) &&
00311 GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed &&
00312 _game_mode != GM_EDITOR) {
00313 Town *t = ClosestTownFromTile(tile_start, UINT_MAX);
00314
00315 if (t == NULL) {
00316 return CMD_ERROR;
00317 } else {
00318 SetDParam(0, t->index);
00319 return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
00320 }
00321 }
00322
00323
00324 if (!(flags & DC_QUERY_COST) && (bridge_type == GetBridgeType(tile_start)) && (transport_type != TRANSPORT_ROAD || (roadtypes & ~GetRoadTypes(tile_start)) == 0)) {
00325 return_cmd_error(STR_ERROR_ALREADY_BUILT);
00326 }
00327
00328
00329 if (!IsTileOwner(tile_start, company) && !IsTileOwner(tile_start, OWNER_TOWN) && !IsTileOwner(tile_start, OWNER_NONE)) {
00330 return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
00331 }
00332
00333 cost.AddCost((bridge_len + 1) * _price[PR_CLEAR_BRIDGE]);
00334 owner = GetTileOwner(tile_start);
00335
00336
00337 is_new_owner = (owner == OWNER_NONE);
00338 if (is_new_owner) owner = company;
00339
00340 switch (transport_type) {
00341 case TRANSPORT_RAIL:
00342
00343 pbs_reservation = HasTunnelBridgeReservation(tile_start);
00344 break;
00345
00346 case TRANSPORT_ROAD:
00347
00348 roadtypes |= GetRoadTypes(tile_start);
00349 break;
00350
00351 default: break;
00352 }
00353 } else {
00354
00355
00356 bool allow_on_slopes = (_settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER);
00357
00358
00359 CommandCost ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00360 if (ret.Failed()) return ret;
00361 cost = ret;
00362
00363 if (terraform_cost_north.Failed() || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00364 cost.AddCost(terraform_cost_north);
00365
00366
00367 ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00368 if (ret.Failed()) return ret;
00369 cost.AddCost(ret);
00370
00371
00372 if (terraform_cost_south.Failed() || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00373 cost.AddCost(terraform_cost_south);
00374
00375 const TileIndex heads[] = {tile_start, tile_end};
00376 for (int i = 0; i < 2; i++) {
00377 if (MayHaveBridgeAbove(heads[i])) {
00378 if (IsBridgeAbove(heads[i])) {
00379 TileIndex north_head = GetNorthernBridgeEnd(heads[i]);
00380
00381 if (direction == GetBridgeAxis(heads[i])) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00382
00383 if (z_start + 1 == GetBridgeHeight(north_head)) {
00384 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00385 }
00386 }
00387 }
00388 }
00389
00390 TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00391 for (TileIndex tile = tile_start + delta; tile != tile_end; tile += delta) {
00392 if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_ERROR_BRIDGE_TOO_LOW_FOR_TERRAIN);
00393
00394 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) {
00395
00396 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00397 }
00398
00399 switch (GetTileType(tile)) {
00400 case MP_WATER:
00401 if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below;
00402 break;
00403
00404 case MP_RAILWAY:
00405 if (!IsPlainRail(tile)) goto not_valid_below;
00406 break;
00407
00408 case MP_ROAD:
00409 if (IsRoadDepot(tile)) goto not_valid_below;
00410 break;
00411
00412 case MP_TUNNELBRIDGE:
00413 if (IsTunnel(tile)) break;
00414 if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below;
00415 if (z_start < GetBridgeHeight(tile)) goto not_valid_below;
00416 break;
00417
00418 case MP_OBJECT: {
00419 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00420 if ((spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) == 0) goto not_valid_below;
00421 if (GetTileMaxZ(tile) + spec->height > z_start) goto not_valid_below;
00422 break;
00423 }
00424
00425 case MP_CLEAR:
00426 break;
00427
00428 default:
00429 not_valid_below:;
00430
00431 ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00432 if (ret.Failed()) return ret;
00433 cost.AddCost(ret);
00434 break;
00435 }
00436
00437 if (flags & DC_EXEC) {
00438
00439
00440
00441 SetBridgeMiddle(tile, direction);
00442 }
00443 }
00444
00445 owner = company;
00446 is_new_owner = true;
00447 }
00448
00449
00450 if (flags & DC_EXEC) {
00451 DiagDirection dir = AxisToDiagDir(direction);
00452
00453 Company *c = Company::GetIfValid(owner);
00454 switch (transport_type) {
00455 case TRANSPORT_RAIL:
00456
00457 if (is_new_owner && c != NULL) c->infrastructure.rail[railtype] += (bridge_len + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
00458 MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype);
00459 MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype);
00460 SetTunnelBridgeReservation(tile_start, pbs_reservation);
00461 SetTunnelBridgeReservation(tile_end, pbs_reservation);
00462 break;
00463
00464 case TRANSPORT_ROAD: {
00465 RoadTypes prev_roadtypes = IsBridgeTile(tile_start) ? GetRoadTypes(tile_start) : ROADTYPES_NONE;
00466 if (is_new_owner) {
00467
00468 if (HasBit(prev_roadtypes, ROADTYPE_ROAD) && GetRoadOwner(tile_start, ROADTYPE_ROAD) == OWNER_NONE) ClrBit(prev_roadtypes, ROADTYPE_ROAD);
00469 if (HasBit(prev_roadtypes, ROADTYPE_TRAM) && GetRoadOwner(tile_start, ROADTYPE_TRAM) == OWNER_NONE) ClrBit(prev_roadtypes, ROADTYPE_TRAM);
00470 }
00471 if (c != NULL) {
00472
00473 RoadType new_rt;
00474 FOR_EACH_SET_ROADTYPE(new_rt, roadtypes ^ prev_roadtypes) {
00475
00476 Company::Get(owner)->infrastructure.road[new_rt] += (bridge_len + 2) * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
00477 }
00478 }
00479 Owner owner_road = owner;
00480 Owner owner_tram = owner;
00481 if (HasBit(prev_roadtypes, ROADTYPE_ROAD)) owner_road = GetRoadOwner(tile_start, ROADTYPE_ROAD);
00482 if (HasBit(prev_roadtypes, ROADTYPE_TRAM)) owner_tram = GetRoadOwner(tile_start, ROADTYPE_TRAM);
00483 MakeRoadBridgeRamp(tile_start, owner, owner_road, owner_tram, bridge_type, dir, roadtypes);
00484 MakeRoadBridgeRamp(tile_end, owner, owner_road, owner_tram, bridge_type, ReverseDiagDir(dir), roadtypes);
00485 break;
00486 }
00487
00488 case TRANSPORT_WATER:
00489 if (is_new_owner && c != NULL) c->infrastructure.water += (bridge_len + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
00490 MakeAqueductBridgeRamp(tile_start, owner, dir);
00491 MakeAqueductBridgeRamp(tile_end, owner, ReverseDiagDir(dir));
00492 break;
00493
00494 default:
00495 NOT_REACHED();
00496 }
00497
00498
00499 TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00500 for (TileIndex tile = tile_start; tile <= tile_end; tile += delta) {
00501 MarkTileDirtyByTile(tile);
00502 }
00503 DirtyCompanyInfrastructureWindows(owner);
00504 }
00505
00506 if ((flags & DC_EXEC) && transport_type == TRANSPORT_RAIL) {
00507 Track track = AxisToTrack(direction);
00508 AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, company);
00509 YapfNotifyTrackLayoutChange(tile_start, track);
00510 }
00511
00512
00513
00514
00515
00516 Company *c = Company::GetIfValid(company);
00517 if (!(flags & DC_QUERY_COST) || (c != NULL && c->is_ai)) {
00518 bridge_len += 2;
00519
00520 switch (transport_type) {
00521 case TRANSPORT_ROAD: cost.AddCost(bridge_len * _price[PR_BUILD_ROAD] * 2 * CountBits(roadtypes)); break;
00522 case TRANSPORT_RAIL: cost.AddCost(bridge_len * RailBuildCost(railtype)); break;
00523 default: break;
00524 }
00525
00526 if (c != NULL) bridge_len = CalcBridgeLenCostFactor(bridge_len);
00527
00528 if (transport_type != TRANSPORT_WATER) {
00529 cost.AddCost((int64)bridge_len * _price[PR_BUILD_BRIDGE] * GetBridgeSpec(bridge_type)->price >> 8);
00530 } else {
00531
00532 cost.AddCost((int64)bridge_len * _price[PR_BUILD_AQUEDUCT]);
00533 }
00534
00535 }
00536
00537 return cost;
00538 }
00539
00540
00551 CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00552 {
00553 CompanyID company = _current_company;
00554
00555 TransportType transport_type = Extract<TransportType, 8, 2>(p1);
00556
00557 RailType railtype = INVALID_RAILTYPE;
00558 RoadTypes rts = ROADTYPES_NONE;
00559 _build_tunnel_endtile = 0;
00560 switch (transport_type) {
00561 case TRANSPORT_RAIL:
00562 railtype = Extract<RailType, 0, 4>(p1);
00563 if (!ValParamRailtype(railtype)) return CMD_ERROR;
00564 break;
00565
00566 case TRANSPORT_ROAD:
00567 rts = Extract<RoadTypes, 0, 2>(p1);
00568 if (!HasExactlyOneBit(rts) || !HasRoadTypesAvail(company, rts)) return CMD_ERROR;
00569 break;
00570
00571 default: return CMD_ERROR;
00572 }
00573
00574 if (company == OWNER_DEITY) {
00575 if (transport_type != TRANSPORT_ROAD) return CMD_ERROR;
00576 const Town *town = CalcClosestTownFromTile(start_tile);
00577
00578 company = OWNER_TOWN;
00579
00580
00581 if (town == NULL || DistanceSquare(start_tile, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
00582 company = OWNER_NONE;
00583 }
00584 }
00585
00586 int start_z;
00587 int end_z;
00588 Slope start_tileh = GetTileSlope(start_tile, &start_z);
00589 DiagDirection direction = GetInclinedSlopeDirection(start_tileh);
00590 if (direction == INVALID_DIAGDIR) return_cmd_error(STR_ERROR_SITE_UNSUITABLE_FOR_TUNNEL);
00591
00592 if (HasTileWaterGround(start_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
00593
00594 CommandCost ret = DoCommand(start_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00595 if (ret.Failed()) return ret;
00596
00597
00598
00599
00600
00601
00602 TileIndexDiff delta = TileOffsByDiagDir(direction);
00603 DiagDirection tunnel_in_way_dir;
00604 if (DiagDirToAxis(direction) == AXIS_Y) {
00605 tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE;
00606 } else {
00607 tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW;
00608 }
00609
00610 TileIndex end_tile = start_tile;
00611
00612
00613 int tiles_coef = 3;
00614
00615 int tiles = 0;
00616
00617 int tiles_bump = 25;
00618
00619 CommandCost cost(EXPENSES_CONSTRUCTION);
00620 Slope end_tileh;
00621 for (;;) {
00622 end_tile += delta;
00623 if (!IsValidTile(end_tile)) return_cmd_error(STR_ERROR_TUNNEL_THROUGH_MAP_BORDER);
00624 end_tileh = GetTileSlope(end_tile, &end_z);
00625
00626 if (start_z == end_z) break;
00627
00628 if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
00629 return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY);
00630 }
00631
00632 tiles++;
00633 if (tiles == tiles_bump) {
00634 tiles_coef++;
00635 tiles_bump *= 2;
00636 }
00637
00638 cost.AddCost(_price[PR_BUILD_TUNNEL]);
00639 cost.AddCost(cost.GetCost() >> tiles_coef);
00640 }
00641
00642
00643 cost.AddCost(_price[PR_BUILD_TUNNEL]);
00644 cost.AddCost(ret);
00645
00646
00647 _build_tunnel_endtile = end_tile;
00648
00649 if (tiles > _settings_game.construction.max_tunnel_length) return_cmd_error(STR_ERROR_TUNNEL_TOO_LONG);
00650
00651 if (HasTileWaterGround(end_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
00652
00653
00654 ret = DoCommand(end_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00655 if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
00656 cost.AddCost(ret);
00657
00658
00659 if (end_tileh != ComplementSlope(start_tileh)) {
00660
00661
00662 ClearedObjectArea *coa = FindClearedObject(end_tile);
00663 if (coa == NULL) {
00664 coa = _cleared_object_areas.Append();
00665 coa->first_tile = end_tile;
00666 coa->area = TileArea(end_tile, 1, 1);
00667 }
00668
00669
00670 TileIndex old_first_tile = coa->first_tile;
00671 coa->first_tile = INVALID_TILE;
00672 ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND);
00673 coa->first_tile = old_first_tile;
00674 if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
00675 cost.AddCost(ret);
00676 }
00677 cost.AddCost(_price[PR_BUILD_TUNNEL]);
00678
00679
00680 switch (transport_type) {
00681 case TRANSPORT_ROAD: cost.AddCost((tiles + 2) * _price[PR_BUILD_ROAD] * 2); break;
00682 case TRANSPORT_RAIL: cost.AddCost((tiles + 2) * RailBuildCost(railtype)); break;
00683 default: NOT_REACHED();
00684 }
00685
00686 if (flags & DC_EXEC) {
00687 Company *c = Company::GetIfValid(company);
00688 uint num_pieces = (tiles + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
00689 if (transport_type == TRANSPORT_RAIL) {
00690 if (!IsTunnelTile(start_tile) && c != NULL) c->infrastructure.rail[railtype] += num_pieces;
00691 MakeRailTunnel(start_tile, company, direction, railtype);
00692 MakeRailTunnel(end_tile, company, ReverseDiagDir(direction), railtype);
00693 AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, company);
00694 YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction));
00695 } else {
00696 if (c != NULL) {
00697 RoadType rt;
00698 FOR_EACH_SET_ROADTYPE(rt, rts ^ (IsTunnelTile(start_tile) ? GetRoadTypes(start_tile) : ROADTYPES_NONE)) {
00699 c->infrastructure.road[rt] += num_pieces * 2;
00700 }
00701 }
00702 MakeRoadTunnel(start_tile, company, direction, rts);
00703 MakeRoadTunnel(end_tile, company, ReverseDiagDir(direction), rts);
00704 }
00705 DirtyCompanyInfrastructureWindows(company);
00706 }
00707
00708 return cost;
00709 }
00710
00711
00717 static inline CommandCost CheckAllowRemoveTunnelBridge(TileIndex tile)
00718 {
00719
00720 if (_current_company == OWNER_WATER || _game_mode == GM_EDITOR) return CommandCost();
00721
00722 switch (GetTunnelBridgeTransportType(tile)) {
00723 case TRANSPORT_ROAD: {
00724 RoadTypes rts = GetRoadTypes(tile);
00725 Owner road_owner = _current_company;
00726 Owner tram_owner = _current_company;
00727
00728 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
00729 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
00730
00731
00732 if (road_owner == OWNER_TOWN && _current_company != OWNER_TOWN && !(_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) {
00733
00734 return CheckTileOwnership(tile);
00735 }
00736 if (road_owner == OWNER_NONE || road_owner == OWNER_TOWN) road_owner = _current_company;
00737 if (tram_owner == OWNER_NONE) tram_owner = _current_company;
00738
00739 CommandCost ret = CheckOwnership(road_owner, tile);
00740 if (ret.Succeeded()) ret = CheckOwnership(tram_owner, tile);
00741 return ret;
00742 }
00743
00744 case TRANSPORT_RAIL:
00745 return CheckOwnership(GetTileOwner(tile));
00746
00747 case TRANSPORT_WATER: {
00748
00749 Owner aqueduct_owner = GetTileOwner(tile);
00750 if (aqueduct_owner == OWNER_NONE) aqueduct_owner = _current_company;
00751 return CheckOwnership(aqueduct_owner);
00752 }
00753
00754 default: NOT_REACHED();
00755 }
00756 }
00757
00764 static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags)
00765 {
00766 CommandCost ret = CheckAllowRemoveTunnelBridge(tile);
00767 if (ret.Failed()) return ret;
00768
00769 TileIndex endtile = GetOtherTunnelEnd(tile);
00770
00771 ret = TunnelBridgeIsFree(tile, endtile);
00772 if (ret.Failed()) return ret;
00773
00774 _build_tunnel_endtile = endtile;
00775
00776 Town *t = NULL;
00777 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00778 t = ClosestTownFromTile(tile, UINT_MAX);
00779
00780
00781
00782 CommandCost ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
00783 if (ret.Failed()) return ret;
00784 }
00785
00786
00787
00788 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00789 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
00790 }
00791
00792 uint len = GetTunnelBridgeLength(tile, endtile) + 2;
00793
00794 if (flags & DC_EXEC) {
00795 if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
00796
00797 DiagDirection dir = GetTunnelBridgeDirection(tile);
00798 Track track = DiagDirToDiagTrack(dir);
00799 Owner owner = GetTileOwner(tile);
00800
00801 Train *v = NULL;
00802 if (HasTunnelBridgeReservation(tile)) {
00803 v = GetTrainForReservation(tile, track);
00804 if (v != NULL) FreeTrainTrackReservation(v);
00805 }
00806
00807 if (Company::IsValidID(owner)) {
00808 Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
00809 DirtyCompanyInfrastructureWindows(owner);
00810 }
00811
00812 DoClearSquare(tile);
00813 DoClearSquare(endtile);
00814
00815
00816 AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner);
00817 AddSideToSignalBuffer(endtile, dir, owner);
00818
00819 YapfNotifyTrackLayoutChange(tile, track);
00820 YapfNotifyTrackLayoutChange(endtile, track);
00821
00822 if (v != NULL) TryPathReserve(v);
00823 } else {
00824 RoadType rt;
00825 FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
00826
00827 Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
00828 if (c != NULL) {
00829 c->infrastructure.road[rt] -= len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
00830 DirtyCompanyInfrastructureWindows(c->index);
00831 }
00832 }
00833
00834 DoClearSquare(tile);
00835 DoClearSquare(endtile);
00836 }
00837 }
00838 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_TUNNEL] * len);
00839 }
00840
00841
00848 static CommandCost DoClearBridge(TileIndex tile, DoCommandFlag flags)
00849 {
00850 CommandCost ret = CheckAllowRemoveTunnelBridge(tile);
00851 if (ret.Failed()) return ret;
00852
00853 TileIndex endtile = GetOtherBridgeEnd(tile);
00854
00855 ret = TunnelBridgeIsFree(tile, endtile);
00856 if (ret.Failed()) return ret;
00857
00858 DiagDirection direction = GetTunnelBridgeDirection(tile);
00859 TileIndexDiff delta = TileOffsByDiagDir(direction);
00860
00861 Town *t = NULL;
00862 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00863 t = ClosestTownFromTile(tile, UINT_MAX);
00864
00865
00866
00867 CommandCost ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
00868 if (ret.Failed()) return ret;
00869 }
00870
00871
00872
00873 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00874 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
00875 }
00876
00877 Money base_cost = (GetTunnelBridgeTransportType(tile) != TRANSPORT_WATER) ? _price[PR_CLEAR_BRIDGE] : _price[PR_CLEAR_AQUEDUCT];
00878 uint len = GetTunnelBridgeLength(tile, endtile) + 2;
00879
00880 if (flags & DC_EXEC) {
00881
00882 bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
00883 Owner owner = GetTileOwner(tile);
00884 int height = GetBridgeHeight(tile);
00885 Train *v = NULL;
00886
00887 if (rail && HasTunnelBridgeReservation(tile)) {
00888 v = GetTrainForReservation(tile, DiagDirToDiagTrack(direction));
00889 if (v != NULL) FreeTrainTrackReservation(v);
00890 }
00891
00892
00893 if (rail) {
00894 if (Company::IsValidID(owner)) Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
00895 } else if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) {
00896 RoadType rt;
00897 FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
00898 Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
00899 if (c != NULL) {
00900
00901 c->infrastructure.road[rt] -= len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
00902 DirtyCompanyInfrastructureWindows(c->index);
00903 }
00904 }
00905 } else {
00906 if (Company::IsValidID(owner)) Company::Get(owner)->infrastructure.water -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
00907 }
00908 DirtyCompanyInfrastructureWindows(owner);
00909
00910 DoClearSquare(tile);
00911 DoClearSquare(endtile);
00912 for (TileIndex c = tile + delta; c != endtile; c += delta) {
00913
00914 if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) {
00915 int minz = GetTileMaxZ(c) + 3;
00916 if (height < minz) SetRoadside(c, ROADSIDE_PAVED);
00917 }
00918 ClearBridgeMiddle(c);
00919 MarkTileDirtyByTile(c);
00920 }
00921
00922 if (rail) {
00923
00924 AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner);
00925 AddSideToSignalBuffer(endtile, direction, owner);
00926
00927 Track track = DiagDirToDiagTrack(direction);
00928 YapfNotifyTrackLayoutChange(tile, track);
00929 YapfNotifyTrackLayoutChange(endtile, track);
00930
00931 if (v != NULL) TryPathReserve(v, true);
00932 }
00933 }
00934
00935 return CommandCost(EXPENSES_CONSTRUCTION, len * base_cost);
00936 }
00937
00944 static CommandCost ClearTile_TunnelBridge(TileIndex tile, DoCommandFlag flags)
00945 {
00946 if (IsTunnel(tile)) {
00947 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST);
00948 return DoClearTunnel(tile, flags);
00949 } else {
00950 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00951 return DoClearBridge(tile, flags);
00952 }
00953 }
00954
00965 static inline void DrawPillar(const PalSpriteID *psid, int x, int y, int z, int w, int h, const SubSprite *subsprite)
00966 {
00967 static const int PILLAR_Z_OFFSET = TILE_HEIGHT - BRIDGE_Z_START;
00968 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);
00969 }
00970
00982 static int DrawPillarColumn(int z_bottom, int z_top, const PalSpriteID *psid, int x, int y, int w, int h)
00983 {
00984 int cur_z;
00985 for (cur_z = z_top; cur_z >= z_bottom; cur_z -= TILE_HEIGHT) {
00986 DrawPillar(psid, x, y, cur_z, w, h, NULL);
00987 }
00988 return cur_z;
00989 }
00990
01002 static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
01003 {
01004 static const int bounding_box_size[2] = {16, 2};
01005 static const int back_pillar_offset[2] = { 0, 9};
01006
01007 static const int INF = 1000;
01008 static const SubSprite half_pillar_sub_sprite[2][2] = {
01009 { { -14, -INF, INF, INF }, { -INF, -INF, -15, INF } },
01010 { { -INF, -INF, 15, INF }, { 16, -INF, INF, INF } },
01011 };
01012
01013 if (psid->sprite == 0) return;
01014
01015
01016 DiagDirection south_dir = AxisToDiagDir(axis);
01017 int z_front_north = ti->z;
01018 int z_back_north = ti->z;
01019 int z_front_south = ti->z;
01020 int z_back_south = ti->z;
01021 GetSlopePixelZOnEdge(ti->tileh, south_dir, &z_front_south, &z_back_south);
01022 GetSlopePixelZOnEdge(ti->tileh, ReverseDiagDir(south_dir), &z_front_north, &z_back_north);
01023
01024
01025 int z_front = max(z_front_north, z_front_south);
01026 int z_back = max(z_back_north, z_back_south);
01027
01028
01029 int w = bounding_box_size[axis];
01030 int h = bounding_box_size[OtherAxis(axis)];
01031
01032 int x_back = x - back_pillar_offset[axis];
01033 int y_back = y - back_pillar_offset[OtherAxis(axis)];
01034
01035
01036 int bottom_z = DrawPillarColumn(z_front, z_bridge, psid, x, y, w, h);
01037 if (z_front_north < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
01038 if (z_front_south < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
01039
01040
01041 int z_bridge_back = z_bridge - 2 * (int)TILE_HEIGHT;
01042 if (drawfarpillar && (z_back_north <= z_bridge_back || z_back_south <= z_bridge_back)) {
01043 bottom_z = DrawPillarColumn(z_back, z_bridge_back, psid, x_back, y_back, w, h);
01044 if (z_back_north < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
01045 if (z_back_south < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
01046 }
01047 }
01048
01058 static void DrawBridgeTramBits(int x, int y, int z, int offset, bool overlay, bool head)
01059 {
01060 static const SpriteID tram_offsets[2][6] = { { 107, 108, 109, 110, 111, 112 }, { 4, 5, 15, 16, 17, 18 } };
01061 static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 };
01062 static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 };
01063
01064 static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 };
01065 static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 };
01066 static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 };
01067 static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 };
01068
01069
01070
01071 if (head || !IsInvisibilitySet(TO_BRIDGES)) {
01072 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE,
01073 x, y, size_x[offset], size_y[offset], 0x28, z,
01074 !head && IsTransparencySet(TO_BRIDGES));
01075 }
01076
01077
01078 if (!IsInvisibilitySet(TO_CATENARY)) {
01079 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset], PAL_NONE,
01080 x, y, size_x[offset], size_y[offset], 0x28, z,
01081 IsTransparencySet(TO_CATENARY));
01082 }
01083
01084
01085 EndSpriteCombine();
01086 StartSpriteCombine();
01087
01088
01089 if (!IsInvisibilitySet(TO_CATENARY)) {
01090 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE,
01091 x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z,
01092 IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]);
01093 }
01094 }
01095
01109 static void DrawTile_TunnelBridge(TileInfo *ti)
01110 {
01111 TransportType transport_type = GetTunnelBridgeTransportType(ti->tile);
01112 DiagDirection tunnelbridge_direction = GetTunnelBridgeDirection(ti->tile);
01113
01114 if (IsTunnel(ti->tile)) {
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124 static const int _tunnel_BB[4][12] = {
01125
01126
01127 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 },
01128 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 },
01129 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 },
01130 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 },
01131 };
01132 const int *BB_data = _tunnel_BB[tunnelbridge_direction];
01133
01134 bool catenary = false;
01135
01136 SpriteID image;
01137 SpriteID railtype_overlay = 0;
01138 if (transport_type == TRANSPORT_RAIL) {
01139 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01140 image = rti->base_sprites.tunnel;
01141 if (rti->UsesOverlay()) {
01142
01143 railtype_overlay = GetCustomRailSprite(rti, ti->tile, RTSG_TUNNEL_PORTAL);
01144 if (railtype_overlay != 0) image = SPR_RAILTYPE_TUNNEL_BASE;
01145 }
01146 } else {
01147 image = SPR_TUNNEL_ENTRY_REAR_ROAD;
01148 }
01149
01150 if (HasTunnelBridgeSnowOrDesert(ti->tile)) image += railtype_overlay != 0 ? 8 : 32;
01151
01152 image += tunnelbridge_direction * 2;
01153 DrawGroundSprite(image, PAL_NONE);
01154
01155 if (transport_type == TRANSPORT_ROAD) {
01156 RoadTypes rts = GetRoadTypes(ti->tile);
01157
01158 if (HasBit(rts, ROADTYPE_TRAM)) {
01159 static const SpriteID tunnel_sprites[2][4] = { { 28, 78, 79, 27 }, { 5, 76, 77, 4 } };
01160
01161 DrawGroundSprite(SPR_TRAMWAY_BASE + tunnel_sprites[rts - ROADTYPES_TRAM][tunnelbridge_direction], PAL_NONE);
01162
01163
01164 if (!IsInvisibilitySet(TO_CATENARY)) {
01165 catenary = true;
01166 StartSpriteCombine();
01167 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);
01168 }
01169 }
01170 } else {
01171 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01172 if (rti->UsesOverlay()) {
01173 SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_TUNNEL);
01174 if (surface != 0) DrawGroundSprite(surface + tunnelbridge_direction, PAL_NONE);
01175 }
01176
01177
01178 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasTunnelBridgeReservation(ti->tile)) {
01179 DrawGroundSprite(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH);
01180 }
01181
01182 if (HasCatenaryDrawn(GetRailType(ti->tile))) {
01183
01184 DrawCatenary(ti);
01185
01186 catenary = true;
01187 StartSpriteCombine();
01188
01189 DrawCatenaryOnTunnel(ti);
01190 }
01191 }
01192
01193 if (railtype_overlay != 0 && !catenary) StartSpriteCombine();
01194
01195 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);
01196
01197 if (railtype_overlay != 0) AddSortableSpriteToDraw(railtype_overlay + tunnelbridge_direction, 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);
01198
01199 if (catenary || railtype_overlay != 0) EndSpriteCombine();
01200
01201
01202 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x, ti->y, BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
01203 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);
01204
01205 DrawBridgeMiddle(ti);
01206 } else {
01207 const PalSpriteID *psid;
01208 int base_offset;
01209 bool ice = HasTunnelBridgeSnowOrDesert(ti->tile);
01210
01211 if (transport_type == TRANSPORT_RAIL) {
01212 base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset;
01213 assert(base_offset != 8);
01214 } else {
01215 base_offset = 8;
01216 }
01217
01218
01219 assert( (base_offset & 0x07) == 0x00);
01220
01221 DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(tunnelbridge_direction)));
01222
01223
01224 base_offset += (6 - tunnelbridge_direction) % 4;
01225
01226
01227 if (transport_type != TRANSPORT_WATER) {
01228 if (ti->tileh == SLOPE_FLAT) base_offset += 4;
01229 psid = &GetBridgeSpriteTable(GetBridgeType(ti->tile), BRIDGE_PIECE_HEAD)[base_offset];
01230 } else {
01231 psid = _aqueduct_sprites + base_offset;
01232 }
01233
01234 if (!ice) {
01235 TileIndex next = ti->tile + TileOffsByDiagDir(tunnelbridge_direction);
01236 if (ti->tileh != SLOPE_FLAT && ti->z == 0 && HasTileWaterClass(next) && GetWaterClass(next) == WATER_CLASS_SEA) {
01237 DrawShoreTile(ti->tileh);
01238 } else {
01239 DrawClearLandTile(ti, 3);
01240 }
01241 } else {
01242 DrawGroundSprite(SPR_FLAT_SNOW_DESERT_TILE + SlopeToSpriteOffset(ti->tileh), PAL_NONE);
01243 }
01244
01245
01246
01247
01248 if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
01249
01250
01251
01252
01253
01254 AddSortableSpriteToDraw(psid->sprite, psid->pal, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 0 : 8, ti->z);
01255
01256 if (transport_type == TRANSPORT_ROAD) {
01257 RoadTypes rts = GetRoadTypes(ti->tile);
01258
01259 if (HasBit(rts, ROADTYPE_TRAM)) {
01260 uint offset = tunnelbridge_direction;
01261 int z = ti->z;
01262 if (ti->tileh != SLOPE_FLAT) {
01263 offset = (offset + 1) & 1;
01264 z += TILE_HEIGHT;
01265 } else {
01266 offset += 2;
01267 }
01268
01269 DrawBridgeTramBits(ti->x, ti->y, z, offset, HasBit(rts, ROADTYPE_ROAD), true);
01270 }
01271 EndSpriteCombine();
01272 } else if (transport_type == TRANSPORT_RAIL) {
01273 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01274 if (rti->UsesOverlay()) {
01275 SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_BRIDGE);
01276 if (surface != 0) {
01277 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
01278 AddSortableSpriteToDraw(surface + ((DiagDirToAxis(tunnelbridge_direction) == AXIS_X) ? RTBO_X : RTBO_Y), PAL_NONE, ti->x, ti->y, 16, 16, 0, ti->z + 8);
01279 } else {
01280 AddSortableSpriteToDraw(surface + RTBO_SLOPE + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, 16, 16, 8, ti->z);
01281 }
01282 }
01283
01284
01285
01286 }
01287
01288
01289 if (_game_mode != GM_MENU &&_settings_client.gui.show_track_reservation && HasTunnelBridgeReservation(ti->tile)) {
01290 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
01291 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);
01292 } else {
01293 AddSortableSpriteToDraw(rti->base_sprites.single_sloped + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
01294 }
01295 }
01296
01297 EndSpriteCombine();
01298 if (HasCatenaryDrawn(GetRailType(ti->tile))) {
01299 DrawCatenary(ti);
01300 }
01301 }
01302
01303 DrawBridgeMiddle(ti);
01304 }
01305 }
01306
01307
01326 static BridgePieces CalcBridgePiece(uint north, uint south)
01327 {
01328 if (north == 1) {
01329 return BRIDGE_PIECE_NORTH;
01330 } else if (south == 1) {
01331 return BRIDGE_PIECE_SOUTH;
01332 } else if (north < south) {
01333 return north & 1 ? BRIDGE_PIECE_INNER_SOUTH : BRIDGE_PIECE_INNER_NORTH;
01334 } else if (north > south) {
01335 return south & 1 ? BRIDGE_PIECE_INNER_NORTH : BRIDGE_PIECE_INNER_SOUTH;
01336 } else {
01337 return north & 1 ? BRIDGE_PIECE_MIDDLE_EVEN : BRIDGE_PIECE_MIDDLE_ODD;
01338 }
01339 }
01340
01345 void DrawBridgeMiddle(const TileInfo *ti)
01346 {
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363 if (!IsBridgeAbove(ti->tile)) return;
01364
01365 TileIndex rampnorth = GetNorthernBridgeEnd(ti->tile);
01366 TileIndex rampsouth = GetSouthernBridgeEnd(ti->tile);
01367 TransportType transport_type = GetTunnelBridgeTransportType(rampsouth);
01368
01369 Axis axis = GetBridgeAxis(ti->tile);
01370 BridgePieces piece = CalcBridgePiece(
01371 GetTunnelBridgeLength(ti->tile, rampnorth) + 1,
01372 GetTunnelBridgeLength(ti->tile, rampsouth) + 1
01373 );
01374
01375 const PalSpriteID *psid;
01376 bool drawfarpillar;
01377 if (transport_type != TRANSPORT_WATER) {
01378 BridgeType type = GetBridgeType(rampsouth);
01379 drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0);
01380
01381 uint base_offset;
01382 if (transport_type == TRANSPORT_RAIL) {
01383 base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset;
01384 } else {
01385 base_offset = 8;
01386 }
01387
01388 psid = base_offset + GetBridgeSpriteTable(type, piece);
01389 } else {
01390 drawfarpillar = true;
01391 psid = _aqueduct_sprites;
01392 }
01393
01394 if (axis != AXIS_X) psid += 4;
01395
01396 int x = ti->x;
01397 int y = ti->y;
01398 uint bridge_z = GetBridgePixelHeight(rampsouth);
01399 int z = bridge_z - BRIDGE_Z_START;
01400
01401
01402 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, x, y, 16, 16, 1, bridge_z - TILE_HEIGHT + BB_Z_SEPARATOR);
01403
01404
01405 if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
01406
01407
01408 if (!IsInvisibilitySet(TO_BRIDGES)) {
01409 if (axis == AXIS_X) {
01410 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 1, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
01411 } else {
01412 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 1, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
01413 }
01414 }
01415
01416 psid++;
01417
01418 if (transport_type == TRANSPORT_ROAD) {
01419 RoadTypes rts = GetRoadTypes(rampsouth);
01420
01421 if (HasBit(rts, ROADTYPE_TRAM)) {
01422
01423 DrawBridgeTramBits(x, y, bridge_z, axis ^ 1, HasBit(rts, ROADTYPE_ROAD), false);
01424 } else {
01425 EndSpriteCombine();
01426 StartSpriteCombine();
01427 }
01428 } else if (transport_type == TRANSPORT_RAIL) {
01429 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(rampsouth));
01430 if (rti->UsesOverlay() && !IsInvisibilitySet(TO_BRIDGES)) {
01431 SpriteID surface = GetCustomRailSprite(rti, rampsouth, RTSG_BRIDGE, TCX_ON_BRIDGE);
01432 if (surface != 0) {
01433 AddSortableSpriteToDraw(surface + axis, PAL_NONE, x, y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
01434 }
01435 }
01436 EndSpriteCombine();
01437
01438 if (HasCatenaryDrawn(GetRailType(rampsouth))) {
01439 DrawCatenaryOnBridge(ti);
01440 }
01441 }
01442
01443
01444 if (!IsInvisibilitySet(TO_BRIDGES)) {
01445 if (axis == AXIS_X) {
01446 y += 12;
01447 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 4, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 3, BRIDGE_Z_START);
01448 } else {
01449 x += 12;
01450 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 4, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 3, 0, BRIDGE_Z_START);
01451 }
01452 }
01453
01454
01455 if (transport_type == TRANSPORT_ROAD) EndSpriteCombine();
01456
01457
01458 if (IsInvisibilitySet(TO_BRIDGES)) return;
01459
01460 psid++;
01461 if (ti->z + 5 == z) {
01462
01463 if (psid->sprite != 0) {
01464 SpriteID image = psid->sprite;
01465 SpriteID pal = psid->pal;
01466 if (IsTransparencySet(TO_BRIDGES)) {
01467 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
01468 pal = PALETTE_TO_TRANSPARENT;
01469 }
01470
01471 DrawGroundSpriteAt(image, pal, x - ti->x, y - ti->y, z - ti->z);
01472 }
01473 } else {
01474
01475 DrawBridgePillars(psid, ti, axis, drawfarpillar, x, y, z);
01476 }
01477 }
01478
01479
01480 static int GetSlopePixelZ_TunnelBridge(TileIndex tile, uint x, uint y)
01481 {
01482 int z;
01483 Slope tileh = GetTilePixelSlope(tile, &z);
01484
01485 x &= 0xF;
01486 y &= 0xF;
01487
01488 if (IsTunnel(tile)) {
01489 uint pos = (DiagDirToAxis(GetTunnelBridgeDirection(tile)) == AXIS_X ? y : x);
01490
01491
01492 if (5 <= pos && pos <= 10) return z;
01493 } else {
01494 DiagDirection dir = GetTunnelBridgeDirection(tile);
01495 uint pos = (DiagDirToAxis(dir) == AXIS_X ? y : x);
01496
01497 z += ApplyPixelFoundationToSlope(GetBridgeFoundation(tileh, DiagDirToAxis(dir)), &tileh);
01498
01499
01500 if (5 <= pos && pos <= 10) {
01501 int delta;
01502
01503 if (tileh != SLOPE_FLAT) return z + TILE_HEIGHT;
01504
01505 switch (dir) {
01506 default: NOT_REACHED();
01507 case DIAGDIR_NE: delta = (TILE_SIZE - 1 - x) / 2; break;
01508 case DIAGDIR_SE: delta = y / 2; break;
01509 case DIAGDIR_SW: delta = x / 2; break;
01510 case DIAGDIR_NW: delta = (TILE_SIZE - 1 - y) / 2; break;
01511 }
01512 return z + 1 + delta;
01513 }
01514 }
01515
01516 return z + GetPartialPixelZ(x, y, tileh);
01517 }
01518
01519 static Foundation GetFoundation_TunnelBridge(TileIndex tile, Slope tileh)
01520 {
01521 return IsTunnel(tile) ? FOUNDATION_NONE : GetBridgeFoundation(tileh, DiagDirToAxis(GetTunnelBridgeDirection(tile)));
01522 }
01523
01524 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
01525 {
01526 TransportType tt = GetTunnelBridgeTransportType(tile);
01527
01528 if (IsTunnel(tile)) {
01529 td->str = (tt == TRANSPORT_RAIL) ? STR_LAI_TUNNEL_DESCRIPTION_RAILROAD : STR_LAI_TUNNEL_DESCRIPTION_ROAD;
01530 } else {
01531 td->str = (tt == TRANSPORT_WATER) ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : GetBridgeSpec(GetBridgeType(tile))->transport_name[tt];
01532 }
01533 td->owner[0] = GetTileOwner(tile);
01534
01535 Owner road_owner = INVALID_OWNER;
01536 Owner tram_owner = INVALID_OWNER;
01537 RoadTypes rts = GetRoadTypes(tile);
01538 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01539 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01540
01541
01542 if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
01543 (road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
01544 uint i = 1;
01545 if (road_owner != INVALID_OWNER) {
01546 td->owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
01547 td->owner[i] = road_owner;
01548 i++;
01549 }
01550 if (tram_owner != INVALID_OWNER) {
01551 td->owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
01552 td->owner[i] = tram_owner;
01553 }
01554 }
01555
01556 if (tt == TRANSPORT_RAIL) {
01557 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
01558 td->rail_speed = rti->max_speed;
01559
01560 if (!IsTunnel(tile)) {
01561 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
01562 if (td->rail_speed == 0 || spd < td->rail_speed) {
01563 td->rail_speed = spd;
01564 }
01565 }
01566 }
01567 }
01568
01569
01570 static void TileLoop_TunnelBridge(TileIndex tile)
01571 {
01572 bool snow_or_desert = HasTunnelBridgeSnowOrDesert(tile);
01573 switch (_settings_game.game_creation.landscape) {
01574 case LT_ARCTIC: {
01575
01576
01577
01578 int z = IsBridge(tile) ? GetTileMaxZ(tile) : GetTileZ(tile);
01579 if (snow_or_desert != (z > GetSnowLine())) {
01580 SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert);
01581 MarkTileDirtyByTile(tile);
01582 }
01583 break;
01584 }
01585
01586 case LT_TROPIC:
01587 if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
01588 SetTunnelBridgeSnowOrDesert(tile, true);
01589 MarkTileDirtyByTile(tile);
01590 }
01591 break;
01592
01593 default:
01594 break;
01595 }
01596 }
01597
01598 static TrackStatus GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
01599 {
01600 TransportType transport_type = GetTunnelBridgeTransportType(tile);
01601 if (transport_type != mode || (transport_type == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0)) return 0;
01602
01603 DiagDirection dir = GetTunnelBridgeDirection(tile);
01604 if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0;
01605 return CombineTrackStatus(TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)), TRACKDIR_BIT_NONE);
01606 }
01607
01608 static void ChangeTileOwner_TunnelBridge(TileIndex tile, Owner old_owner, Owner new_owner)
01609 {
01610 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
01611
01612
01613 uint num_pieces = tile < other_end ? (GetTunnelBridgeLength(tile, other_end) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR : 0;
01614
01615 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
01616
01617 if (GetRoadOwner(tile, rt) == old_owner) {
01618 if (HasBit(GetRoadTypes(tile), rt)) {
01619
01620
01621 Company::Get(old_owner)->infrastructure.road[rt] -= num_pieces * 2;
01622 if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.road[rt] += num_pieces * 2;
01623 }
01624
01625 SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
01626 }
01627 }
01628
01629 if (!IsTileOwner(tile, old_owner)) return;
01630
01631
01632
01633 TransportType tt = GetTunnelBridgeTransportType(tile);
01634 Company *old = Company::Get(old_owner);
01635 if (tt == TRANSPORT_RAIL) {
01636 old->infrastructure.rail[GetRailType(tile)] -= num_pieces;
01637 if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.rail[GetRailType(tile)] += num_pieces;
01638 } else if (tt == TRANSPORT_WATER) {
01639 old->infrastructure.water -= num_pieces;
01640 if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.water += num_pieces;
01641 }
01642
01643 if (new_owner != INVALID_OWNER) {
01644 SetTileOwner(tile, new_owner);
01645 } else {
01646 if (tt == TRANSPORT_RAIL) {
01647
01648
01649 CommandCost ret = DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
01650 assert(ret.Succeeded());
01651 } else {
01652
01653 SetTileOwner(tile, OWNER_NONE);
01654 }
01655 }
01656 }
01657
01663 static const byte TUNNEL_SOUND_FRAME = 1;
01664
01673 extern const byte _tunnel_visibility_frame[DIAGDIR_END] = {12, 8, 8, 12};
01674
01675 static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
01676 {
01677 int z = GetSlopePixelZ(x, y) - v->z_pos;
01678
01679 if (abs(z) > 2) return VETSB_CANNOT_ENTER;
01680
01681 const DiagDirection dir = GetTunnelBridgeDirection(tile);
01682
01683 const DiagDirection vdir = DirToDiagDir(v->direction);
01684
01685 byte pos = (DiagDirToAxis(vdir) == AXIS_X ? x : y) & TILE_UNIT_MASK;
01686
01687 byte frame = (vdir == DIAGDIR_NE || vdir == DIAGDIR_NW) ? TILE_SIZE - 1 - pos : pos;
01688
01689 if (IsTunnel(tile)) {
01690 if (v->type == VEH_TRAIN) {
01691 Train *t = Train::From(v);
01692
01693 if (t->track != TRACK_BIT_WORMHOLE && dir == vdir) {
01694 if (t->IsFrontEngine() && frame == TUNNEL_SOUND_FRAME) {
01695 if (!PlayVehicleSound(t, VSE_TUNNEL) && RailVehInfo(t->engine_type)->engclass == 0) {
01696 SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
01697 }
01698 return VETSB_CONTINUE;
01699 }
01700 if (frame == _tunnel_visibility_frame[dir]) {
01701 t->tile = tile;
01702 t->track = TRACK_BIT_WORMHOLE;
01703 t->vehstatus |= VS_HIDDEN;
01704 return VETSB_ENTERED_WORMHOLE;
01705 }
01706 }
01707
01708 if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
01709
01710 t->tile = tile;
01711 t->track = DiagDirToDiagTrackBits(vdir);
01712 assert(t->track);
01713 t->vehstatus &= ~VS_HIDDEN;
01714 return VETSB_ENTERED_WORMHOLE;
01715 }
01716 } else if (v->type == VEH_ROAD) {
01717 RoadVehicle *rv = RoadVehicle::From(v);
01718
01719
01720 if (rv->state != RVSB_WORMHOLE && dir == vdir) {
01721 if (frame == _tunnel_visibility_frame[dir]) {
01722
01723 assert(frame == rv->frame + 1);
01724 rv->tile = tile;
01725 rv->state = RVSB_WORMHOLE;
01726 rv->vehstatus |= VS_HIDDEN;
01727 return VETSB_ENTERED_WORMHOLE;
01728 } else {
01729 return VETSB_CONTINUE;
01730 }
01731 }
01732
01733
01734 if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
01735 rv->tile = tile;
01736 rv->state = DiagDirToDiagTrackdir(vdir);
01737 rv->frame = frame;
01738 rv->vehstatus &= ~VS_HIDDEN;
01739 return VETSB_ENTERED_WORMHOLE;
01740 }
01741 }
01742 } else {
01743 if (v->type != VEH_SHIP) {
01744
01745 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
01746
01747 if (v->type == VEH_ROAD) spd *= 2;
01748 Vehicle *first = v->First();
01749 first->cur_speed = min(first->cur_speed, spd);
01750 }
01751
01752 if (vdir == dir) {
01753
01754 if (frame != TILE_SIZE - 1) return VETSB_CONTINUE;
01755 switch (v->type) {
01756 case VEH_TRAIN: {
01757 Train *t = Train::From(v);
01758 t->track = TRACK_BIT_WORMHOLE;
01759 ClrBit(t->gv_flags, GVF_GOINGUP_BIT);
01760 ClrBit(t->gv_flags, GVF_GOINGDOWN_BIT);
01761 break;
01762 }
01763
01764 case VEH_ROAD: {
01765 RoadVehicle *rv = RoadVehicle::From(v);
01766 rv->state = RVSB_WORMHOLE;
01767
01768 ClrBit(rv->gv_flags, GVF_GOINGUP_BIT);
01769 ClrBit(rv->gv_flags, GVF_GOINGDOWN_BIT);
01770 break;
01771 }
01772
01773 case VEH_SHIP:
01774 Ship::From(v)->state = TRACK_BIT_WORMHOLE;
01775 break;
01776
01777 default: NOT_REACHED();
01778 }
01779 return VETSB_ENTERED_WORMHOLE;
01780 } else if (vdir == ReverseDiagDir(dir)) {
01781 v->tile = tile;
01782 switch (v->type) {
01783 case VEH_TRAIN: {
01784 Train *t = Train::From(v);
01785 if (t->track == TRACK_BIT_WORMHOLE) {
01786 t->track = DiagDirToDiagTrackBits(vdir);
01787 return VETSB_ENTERED_WORMHOLE;
01788 }
01789 break;
01790 }
01791
01792 case VEH_ROAD: {
01793 RoadVehicle *rv = RoadVehicle::From(v);
01794 if (rv->state == RVSB_WORMHOLE) {
01795 rv->state = DiagDirToDiagTrackdir(vdir);
01796 rv->frame = 0;
01797 return VETSB_ENTERED_WORMHOLE;
01798 }
01799 break;
01800 }
01801
01802 case VEH_SHIP: {
01803 Ship *ship = Ship::From(v);
01804 if (ship->state == TRACK_BIT_WORMHOLE) {
01805 ship->state = DiagDirToDiagTrackBits(vdir);
01806 return VETSB_ENTERED_WORMHOLE;
01807 }
01808 break;
01809 }
01810
01811 default: NOT_REACHED();
01812 }
01813 }
01814 }
01815 return VETSB_CONTINUE;
01816 }
01817
01818 static CommandCost TerraformTile_TunnelBridge(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
01819 {
01820 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled() && IsBridge(tile) && GetTunnelBridgeTransportType(tile) != TRANSPORT_WATER) {
01821 DiagDirection direction = GetTunnelBridgeDirection(tile);
01822 Axis axis = DiagDirToAxis(direction);
01823 CommandCost res;
01824 int z_old;
01825 Slope tileh_old = GetTileSlope(tile, &z_old);
01826
01827
01828 if ((direction == DIAGDIR_NW) || (direction == DIAGDIR_NE)) {
01829 CheckBridgeSlopeSouth(axis, &tileh_old, &z_old);
01830 res = CheckBridgeSlopeSouth(axis, &tileh_new, &z_new);
01831 } else {
01832 CheckBridgeSlopeNorth(axis, &tileh_old, &z_old);
01833 res = CheckBridgeSlopeNorth(axis, &tileh_new, &z_new);
01834 }
01835
01836
01837 if (res.Succeeded() && (z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01838 }
01839
01840 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
01841 }
01842
01843 extern const TileTypeProcs _tile_type_tunnelbridge_procs = {
01844 DrawTile_TunnelBridge,
01845 GetSlopePixelZ_TunnelBridge,
01846 ClearTile_TunnelBridge,
01847 NULL,
01848 GetTileDesc_TunnelBridge,
01849 GetTileTrackStatus_TunnelBridge,
01850 NULL,
01851 NULL,
01852 TileLoop_TunnelBridge,
01853 ChangeTileOwner_TunnelBridge,
01854 NULL,
01855 VehicleEnter_TunnelBridge,
01856 GetFoundation_TunnelBridge,
01857 TerraformTile_TunnelBridge,
01858 };