00001
00002
00008 #include "stdafx.h"
00009 #include "openttd.h"
00010 #include "rail_map.h"
00011 #include "landscape.h"
00012 #include "town_type.h"
00013 #include "unmovable_map.h"
00014 #include "viewport_func.h"
00015 #include "command_func.h"
00016 #include "town.h"
00017 #include "variables.h"
00018 #include "train.h"
00019 #include "water_map.h"
00020 #include "yapf/yapf.h"
00021 #include "newgrf_sound.h"
00022 #include "autoslope.h"
00023 #include "tunnelbridge_map.h"
00024 #include "strings_func.h"
00025 #include "date_func.h"
00026 #include "functions.h"
00027 #include "vehicle_func.h"
00028 #include "sound_func.h"
00029 #include "tunnelbridge.h"
00030 #include "engine_base.h"
00031 #include "cheat_type.h"
00032 #include "elrail_func.h"
00033 #include "landscape_type.h"
00034
00035 #include "table/sprites.h"
00036 #include "table/strings.h"
00037 #include "table/bridge_land.h"
00038
00039 BridgeSpec _bridge[MAX_BRIDGES];
00040 TileIndex _build_tunnel_endtile;
00041
00043 void ResetBridges()
00044 {
00045
00046 for (BridgeType i = 0; i < MAX_BRIDGES; i++) {
00047 if (_bridge[i].sprite_table != NULL) {
00048 for (BridgePieces j = BRIDGE_PIECE_NORTH; j < BRIDGE_PIECE_INVALID; j++) free(_bridge[i].sprite_table[j]);
00049 free(_bridge[i].sprite_table);
00050 }
00051 }
00052
00053
00054 memset(&_bridge, 0, sizeof(_bridge));
00055
00056 memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge));
00057 }
00058
00062 int CalcBridgeLenCostFactor(int x)
00063 {
00064 int n;
00065 int r;
00066
00067 if (x < 2) return x;
00068 x -= 2;
00069 for (n = 0, r = 2;; n++) {
00070 if (x <= n) return r + x * n;
00071 r += n * n;
00072 x -= n;
00073 }
00074 }
00075
00076 Foundation GetBridgeFoundation(Slope tileh, Axis axis)
00077 {
00078 if ((tileh == SLOPE_FLAT) ||
00079 (((tileh == SLOPE_NE) || (tileh == SLOPE_SW)) && (axis == AXIS_X)) ||
00080 (((tileh == SLOPE_NW) || (tileh == SLOPE_SE)) && (axis == AXIS_Y))) return FOUNDATION_NONE;
00081
00082 return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh));
00083 }
00084
00092 bool HasBridgeFlatRamp(Slope tileh, Axis axis)
00093 {
00094 ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh);
00095
00096 return (tileh != SLOPE_FLAT);
00097 }
00098
00099 static inline const PalSpriteID *GetBridgeSpriteTable(int index, BridgePieces table)
00100 {
00101 const BridgeSpec *bridge = GetBridgeSpec(index);
00102 assert(table < BRIDGE_PIECE_INVALID);
00103 if (bridge->sprite_table == NULL || bridge->sprite_table[table] == NULL) {
00104 return _bridge_sprite_table[index][table];
00105 } else {
00106 return bridge->sprite_table[table];
00107 }
00108 }
00109
00110
00119 static CommandCost CheckBridgeSlopeNorth(Axis axis, Slope *tileh, uint *z)
00120 {
00121 Foundation f = GetBridgeFoundation(*tileh, axis);
00122 *z += ApplyFoundationToSlope(f, tileh);
00123
00124 Slope valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW);
00125 if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
00126
00127 if (f == FOUNDATION_NONE) return CommandCost();
00128
00129 return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
00130 }
00131
00140 static CommandCost CheckBridgeSlopeSouth(Axis axis, Slope *tileh, uint *z)
00141 {
00142 Foundation f = GetBridgeFoundation(*tileh, axis);
00143 *z += ApplyFoundationToSlope(f, tileh);
00144
00145 Slope valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE);
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.terraform);
00151 }
00152
00153 bool CheckBridge_Stuff(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
00154 {
00155 if (flags & DC_QUERY_COST) {
00156 return bridge_len <= (_settings_game.construction.longbridges ? 100U : 16U);
00157 }
00158
00159 if (bridge_type >= MAX_BRIDGES) return false;
00160
00161 const BridgeSpec *b = GetBridgeSpec(bridge_type);
00162 if (b->avail_year > _cur_year) return false;
00163
00164 uint max = b->max_length;
00165 if (max >= 16 && _settings_game.construction.longbridges) max = 100;
00166
00167 return b->min_length <= bridge_len && bridge_len <= max;
00168 }
00169
00179 CommandCost CmdBuildBridge(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00180 {
00181 BridgeType bridge_type;
00182 RailType railtype = INVALID_RAILTYPE;
00183 RoadTypes roadtypes = ROADTYPES_NONE;
00184 uint x;
00185 uint y;
00186 uint sx;
00187 uint sy;
00188 TileIndex tile_start;
00189 TileIndex tile_end;
00190 Slope tileh_start;
00191 Slope tileh_end;
00192 uint z_start;
00193 uint z_end;
00194 TileIndex tile;
00195 TileIndexDiff delta;
00196 uint bridge_len;
00197 Axis direction;
00198 CommandCost cost(EXPENSES_CONSTRUCTION);
00199 CommandCost ret;
00200 bool replace_bridge = false;
00201 BridgeType replaced_bridge_type;
00202 TransportType transport_type;
00203
00204
00205 bridge_type = GB(p2, 0, 8);
00206
00207 if (p1 >= MapSize()) return CMD_ERROR;
00208
00209 transport_type = (TransportType)GB(p2, 15, 2);
00210
00211
00212 switch (transport_type) {
00213 case TRANSPORT_ROAD:
00214 roadtypes = (RoadTypes)GB(p2, 8, 2);
00215 if (!AreValidRoadTypes(roadtypes) || !HasRoadTypesAvail(_current_company, roadtypes)) return CMD_ERROR;
00216 break;
00217
00218 case TRANSPORT_RAIL:
00219 railtype = (RailType)GB(p2, 8, 7);
00220 if (!ValParamRailtype(railtype)) return CMD_ERROR;
00221 break;
00222
00223 case TRANSPORT_WATER:
00224 break;
00225
00226 default:
00227
00228 return CMD_ERROR;
00229 }
00230
00231 x = TileX(end_tile);
00232 y = TileY(end_tile);
00233 sx = TileX(p1);
00234 sy = TileY(p1);
00235
00236
00237 if (x == sx) {
00238 if (y == sy) return_cmd_error(STR_5008_CANNOT_START_AND_END_ON);
00239 direction = AXIS_Y;
00240 if (y > sy) Swap(y, sy);
00241 } else if (y == sy) {
00242 direction = AXIS_X;
00243 if (x > sx) Swap(x, sx);
00244 } else {
00245 return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN);
00246 }
00247
00248 bridge_len = sx + sy - x - y - 1;
00249 if (transport_type != TRANSPORT_WATER) {
00250
00251 if (!CheckBridge_Stuff(bridge_type, bridge_len, flags)) return_cmd_error(STR_5015_CAN_T_BUILD_BRIDGE_HERE);
00252 }
00253
00254
00255 tile_start = TileXY(x, y);
00256 tile_end = TileXY(sx, sy);
00257 if (IsWaterTile(tile_start) || IsWaterTile(tile_end)) {
00258 return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH);
00259 }
00260
00261 tileh_start = GetTileSlope(tile_start, &z_start);
00262 tileh_end = GetTileSlope(tile_end, &z_end);
00263
00264 CommandCost terraform_cost_north = CheckBridgeSlopeNorth(direction, &tileh_start, &z_start);
00265 CommandCost terraform_cost_south = CheckBridgeSlopeSouth(direction, &tileh_end, &z_end);
00266
00267 if (z_start != z_end) return_cmd_error(STR_BRIDGEHEADS_NOT_SAME_HEIGHT);
00268
00269 if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) &&
00270 GetOtherBridgeEnd(tile_start) == tile_end &&
00271 GetTunnelBridgeTransportType(tile_start) == transport_type) {
00272
00273
00274
00275 if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) {
00276 return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00277 }
00278
00279
00280 if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) &&
00281 GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed) {
00282 Town *t = ClosestTownFromTile(tile_start, UINT_MAX);
00283
00284 if (t == NULL) {
00285 return CMD_ERROR;
00286 } else {
00287 SetDParam(0, t->index);
00288 return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
00289 }
00290 }
00291
00292
00293 if (!(flags & DC_QUERY_COST) && bridge_type == GetBridgeType(tile_start)) {
00294 return_cmd_error(STR_1007_ALREADY_BUILT);
00295 }
00296
00297
00298 if (!IsTileOwner(tile_start, _current_company) && !IsTileOwner(tile_start, OWNER_TOWN)) {
00299 return_cmd_error(STR_1024_AREA_IS_OWNED_BY_ANOTHER);
00300 }
00301
00302 cost.AddCost((bridge_len + 1) * _price.clear_bridge);
00303 replace_bridge = true;
00304 replaced_bridge_type = GetBridgeType(tile_start);
00305
00306
00307 roadtypes |= GetRoadTypes(tile_start);
00308 } else {
00309
00310
00311 bool allow_on_slopes = (_settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER);
00312
00313
00314 ret = DoCommand(tile_start, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00315 if (CmdFailed(ret)) return ret;
00316 cost = ret;
00317
00318 if (CmdFailed(terraform_cost_north) || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes))
00319 return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
00320 cost.AddCost(terraform_cost_north);
00321
00322
00323 ret = DoCommand(tile_end, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00324 if (CmdFailed(ret)) return ret;
00325 cost.AddCost(ret);
00326
00327
00328 if (CmdFailed(terraform_cost_south) || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes))
00329 return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
00330 cost.AddCost(terraform_cost_south);
00331
00332 if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
00333 }
00334
00335 if (!replace_bridge) {
00336 TileIndex Heads[] = {tile_start, tile_end};
00337 int i;
00338
00339 for (i = 0; i < 2; i++) {
00340 if (MayHaveBridgeAbove(Heads[i])) {
00341 if (IsBridgeAbove(Heads[i])) {
00342 TileIndex north_head = GetNorthernBridgeEnd(Heads[i]);
00343
00344 if (direction == GetBridgeAxis(Heads[i])) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00345
00346 if (z_start + TILE_HEIGHT == GetBridgeHeight(north_head)) {
00347 return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00348 }
00349 }
00350 }
00351 }
00352 }
00353
00354
00355 if (flags & DC_EXEC) {
00356 DiagDirection dir = AxisToDiagDir(direction);
00357 Owner owner = replace_bridge ? GetTileOwner(tile_start) : _current_company;
00358
00359 switch (transport_type) {
00360 case TRANSPORT_RAIL:
00361 MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype);
00362 MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype);
00363 break;
00364
00365 case TRANSPORT_ROAD:
00366 MakeRoadBridgeRamp(tile_start, owner, bridge_type, dir, roadtypes);
00367 MakeRoadBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), roadtypes);
00368 break;
00369
00370 case TRANSPORT_WATER:
00371 MakeAqueductBridgeRamp(tile_start, owner, dir);
00372 MakeAqueductBridgeRamp(tile_end, owner, ReverseDiagDir(dir));
00373 break;
00374
00375 default:
00376 NOT_REACHED();
00377 break;
00378 }
00379 MarkTileDirtyByTile(tile_start);
00380 MarkTileDirtyByTile(tile_end);
00381 }
00382
00383 delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00384 for (tile = tile_start + delta; tile != tile_end; tile += delta) {
00385 if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_BRIDGE_TOO_LOW_FOR_TERRAIN);
00386
00387 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) && !replace_bridge) {
00388
00389 return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00390 }
00391
00392 switch (GetTileType(tile)) {
00393 case MP_WATER:
00394 if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below;
00395 break;
00396
00397 case MP_RAILWAY:
00398 if (!IsPlainRailTile(tile)) goto not_valid_below;
00399 break;
00400
00401 case MP_ROAD:
00402 if (IsRoadDepot(tile)) goto not_valid_below;
00403 break;
00404
00405 case MP_TUNNELBRIDGE:
00406 if (IsTunnel(tile)) break;
00407 if (replace_bridge) break;
00408 if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below;
00409 if (z_start < GetBridgeHeight(tile)) goto not_valid_below;
00410 break;
00411
00412 case MP_UNMOVABLE:
00413 if (!IsOwnedLand(tile)) goto not_valid_below;
00414 break;
00415
00416 case MP_CLEAR:
00417 break;
00418
00419 default:
00420 not_valid_below:;
00421
00422 ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00423 if (CmdFailed(ret)) return ret;
00424 cost.AddCost(ret);
00425 break;
00426 }
00427
00428 if (flags & DC_EXEC) {
00429 SetBridgeMiddle(tile, direction);
00430 MarkTileDirtyByTile(tile);
00431 }
00432 }
00433
00434 if (flags & DC_EXEC && transport_type == TRANSPORT_RAIL) {
00435 Track track = AxisToTrack(direction);
00436 AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, _current_company);
00437 YapfNotifyTrackLayoutChange(tile_start, track);
00438 }
00439
00440
00441
00442
00443
00444 if (!(flags & DC_QUERY_COST) || (IsValidCompanyID(_current_company) && GetCompany(_current_company)->is_ai)) {
00445 bridge_len += 2;
00446
00447 if (IsValidCompanyID(_current_company))
00448 bridge_len = CalcBridgeLenCostFactor(bridge_len);
00449
00450 cost.AddCost((int64)bridge_len * _price.build_bridge * GetBridgeSpec(bridge_type)->price >> 8);
00451
00452
00453 if (transport_type == TRANSPORT_WATER) cost.AddCost((int64)bridge_len * _price.clear_water);
00454 }
00455
00456 return cost;
00457 }
00458
00459
00466 CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00467 {
00468 TileIndexDiff delta;
00469 TileIndex end_tile;
00470 DiagDirection direction;
00471 Slope start_tileh;
00472 Slope end_tileh;
00473 TransportType transport_type = (TransportType)GB(p1, 9, 1);
00474 uint start_z;
00475 uint end_z;
00476 CommandCost cost(EXPENSES_CONSTRUCTION);
00477 CommandCost ret;
00478
00479 _build_tunnel_endtile = 0;
00480 if (transport_type == TRANSPORT_RAIL) {
00481 if (!ValParamRailtype((RailType)p1)) return CMD_ERROR;
00482 } else {
00483 const RoadTypes rts = (RoadTypes)GB(p1, 0, 2);
00484 if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_company, rts)) return CMD_ERROR;
00485 }
00486
00487 start_tileh = GetTileSlope(start_tile, &start_z);
00488 direction = GetInclinedSlopeDirection(start_tileh);
00489 if (direction == INVALID_DIAGDIR) return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL);
00490
00491 if (IsWaterTile(start_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
00492
00493 ret = DoCommand(start_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00494 if (CmdFailed(ret)) return ret;
00495
00496
00497
00498
00499
00500
00501 delta = TileOffsByDiagDir(direction);
00502 DiagDirection tunnel_in_way_dir;
00503 if (DiagDirToAxis(direction) == AXIS_Y) {
00504 tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE;
00505 } else {
00506 tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW;
00507 }
00508
00509 end_tile = start_tile;
00510
00512 int tiles_coef = 3;
00514 int tiles = 0;
00516 int tiles_bump = 25;
00517
00518 for (;;) {
00519 end_tile += delta;
00520 if (!IsValidTile(end_tile)) return_cmd_error(STR_TUNNEL_THROUGH_MAP_BORDER);
00521 end_tileh = GetTileSlope(end_tile, &end_z);
00522
00523 if (start_z == end_z) break;
00524
00525 if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
00526 return_cmd_error(STR_5003_ANOTHER_TUNNEL_IN_THE_WAY);
00527 }
00528
00529 tiles++;
00530 if (tiles == tiles_bump) {
00531 tiles_coef++;
00532 tiles_bump *= 2;
00533 }
00534
00535 cost.AddCost(_price.build_tunnel);
00536 cost.AddCost(cost.GetCost() >> tiles_coef);
00537 }
00538
00539
00540 cost.AddCost(_price.build_tunnel);
00541 cost.AddCost(ret);
00542
00543
00544 _build_tunnel_endtile = end_tile;
00545
00546 if (IsWaterTile(end_tile)) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
00547
00548
00549 if (end_tileh != ComplementSlope(start_tileh)) {
00550
00551
00552
00553
00554 ret = DoCommand(end_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR);
00555 if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND);
00556
00557 ret = DoCommand(end_tile, end_tileh & start_tileh, 0, flags, CMD_TERRAFORM_LAND);
00558 if (CmdFailed(ret)) return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND);
00559 } else {
00560 ret = DoCommand(end_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00561 if (CmdFailed(ret)) return ret;
00562 }
00563 cost.AddCost(_price.build_tunnel);
00564 cost.AddCost(ret);
00565
00566 if (flags & DC_EXEC) {
00567 if (transport_type == TRANSPORT_RAIL) {
00568 MakeRailTunnel(start_tile, _current_company, direction, (RailType)GB(p1, 0, 4));
00569 MakeRailTunnel(end_tile, _current_company, ReverseDiagDir(direction), (RailType)GB(p1, 0, 4));
00570 AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, _current_company);
00571 YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction));
00572 } else {
00573 MakeRoadTunnel(start_tile, _current_company, direction, (RoadTypes)GB(p1, 0, 2));
00574 MakeRoadTunnel(end_tile, _current_company, ReverseDiagDir(direction), (RoadTypes)GB(p1, 0, 2));
00575 }
00576 }
00577
00578 return cost;
00579 }
00580
00581
00582 static inline bool CheckAllowRemoveTunnelBridge(TileIndex tile)
00583 {
00584
00585 if (_current_company == OWNER_WATER || _game_mode == GM_EDITOR) return true;
00586
00587 RoadTypes rts = GetRoadTypes(tile);
00588 Owner road_owner = _current_company;
00589 Owner tram_owner = _current_company;
00590
00591 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
00592 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
00593
00594
00595 if (road_owner == OWNER_NONE || (road_owner == OWNER_TOWN && (_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value))) road_owner = _current_company;
00596 if (tram_owner == OWNER_NONE) tram_owner = _current_company;
00597
00598 return CheckOwnership(road_owner) && CheckOwnership(tram_owner);
00599 }
00600
00601 static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags)
00602 {
00603 Town *t = NULL;
00604 TileIndex endtile;
00605
00606 if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
00607
00608 endtile = GetOtherTunnelEnd(tile);
00609
00610 if (HasVehicleOnTunnelBridge(tile, endtile)) return CMD_ERROR;
00611
00612 _build_tunnel_endtile = endtile;
00613
00614 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00615 t = ClosestTownFromTile(tile, UINT_MAX);
00616
00617
00618
00619 if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) {
00620 SetDParam(0, t->index);
00621 return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
00622 }
00623 }
00624
00625
00626
00627 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00628 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
00629 }
00630
00631 if (flags & DC_EXEC) {
00632 if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
00633
00634 DiagDirection dir = GetTunnelBridgeDirection(tile);
00635 Track track = DiagDirToDiagTrack(dir);
00636 Owner owner = GetTileOwner(tile);
00637
00638 Vehicle *v = NULL;
00639 if (GetTunnelBridgeReservation(tile)) {
00640 v = GetTrainForReservation(tile, track);
00641 if (v != NULL) FreeTrainTrackReservation(v);
00642 }
00643
00644 DoClearSquare(tile);
00645 DoClearSquare(endtile);
00646
00647
00648 AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner);
00649 AddSideToSignalBuffer(endtile, dir, owner);
00650
00651 YapfNotifyTrackLayoutChange(tile, track);
00652 YapfNotifyTrackLayoutChange(endtile, track);
00653
00654 if (v != NULL) TryPathReserve(v);
00655 } else {
00656 DoClearSquare(tile);
00657 DoClearSquare(endtile);
00658 }
00659 }
00660 return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (GetTunnelBridgeLength(tile, endtile) + 2));
00661 }
00662
00663
00664 static CommandCost DoClearBridge(TileIndex tile, DoCommandFlag flags)
00665 {
00666 DiagDirection direction;
00667 TileIndexDiff delta;
00668 TileIndex endtile;
00669 Town *t = NULL;
00670
00671 if (!CheckAllowRemoveTunnelBridge(tile)) return CMD_ERROR;
00672
00673 endtile = GetOtherBridgeEnd(tile);
00674
00675 if (HasVehicleOnTunnelBridge(tile, endtile)) return CMD_ERROR;
00676
00677 direction = GetTunnelBridgeDirection(tile);
00678 delta = TileOffsByDiagDir(direction);
00679
00680 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00681 t = ClosestTownFromTile(tile, UINT_MAX);
00682
00683
00684
00685 if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) {
00686 SetDParam(0, t->index);
00687 return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
00688 }
00689 }
00690
00691
00692
00693 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
00694 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
00695 }
00696
00697 if (flags & DC_EXEC) {
00698
00699 bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
00700 Owner owner = GetTileOwner(tile);
00701 uint height = GetBridgeHeight(tile);
00702 Vehicle *v = NULL;
00703
00704 if (rail && GetTunnelBridgeReservation(tile)) {
00705 v = GetTrainForReservation(tile, DiagDirToDiagTrack(direction));
00706 if (v != NULL) FreeTrainTrackReservation(v);
00707 }
00708
00709 DoClearSquare(tile);
00710 DoClearSquare(endtile);
00711 for (TileIndex c = tile + delta; c != endtile; c += delta) {
00712
00713 if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) {
00714 uint minz = GetTileMaxZ(c) + 3 * TILE_HEIGHT;
00715 if (height < minz) SetRoadside(c, ROADSIDE_PAVED);
00716 }
00717 ClearBridgeMiddle(c);
00718 MarkTileDirtyByTile(c);
00719 }
00720
00721 if (rail) {
00722
00723 AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner);
00724 AddSideToSignalBuffer(endtile, direction, owner);
00725
00726 Track track = DiagDirToDiagTrack(direction);
00727 YapfNotifyTrackLayoutChange(tile, track);
00728 YapfNotifyTrackLayoutChange(endtile, track);
00729
00730 if (v != NULL) TryPathReserve(v, true);
00731 }
00732 }
00733
00734 return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * _price.clear_bridge);
00735 }
00736
00737 static CommandCost ClearTile_TunnelBridge(TileIndex tile, DoCommandFlag flags)
00738 {
00739 if (IsTunnel(tile)) {
00740 if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
00741 return DoClearTunnel(tile, flags);
00742 } else {
00743 if (flags & DC_AUTO) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00744 return DoClearBridge(tile, flags);
00745 }
00746
00747 return CMD_ERROR;
00748 }
00749
00761 static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
00762 {
00763
00764 if (IsInvisibilitySet(TO_BRIDGES)) return;
00765
00766 SpriteID image = psid->sprite;
00767
00768 if (image != 0) {
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778 bool side = HasBit(image, 0);
00779
00780
00781 DiagDirection dir = AxisToDiagDir(axis);
00782 if (side != (axis == AXIS_Y)) dir = ReverseDiagDir(dir);
00783
00784
00785 int front_height = ti->z;
00786 int back_height = ti->z;
00787 GetSlopeZOnEdge(ti->tileh, dir, &front_height, &back_height);
00788
00789
00790 int w = (axis == AXIS_X ? 16 : 2);
00791 int h = (axis == AXIS_X ? 2 : 16);
00792
00793 int x_back = x - (axis == AXIS_X ? 0 : 9);
00794 int y_back = y - (axis == AXIS_X ? 9 : 0);
00795
00796 for (int cur_z = z_bridge; cur_z >= front_height || cur_z >= back_height; cur_z -= TILE_HEIGHT) {
00797
00798 if (cur_z >= front_height) {
00799 AddSortableSpriteToDraw(image, psid->pal, x, y, w, h, BB_HEIGHT_UNDER_BRIDGE - 5, cur_z, IsTransparencySet(TO_BRIDGES), 0, 0, -5);
00800 }
00801
00802
00803 if (drawfarpillar && cur_z >= back_height && cur_z < z_bridge - TILE_HEIGHT) {
00804 AddSortableSpriteToDraw(image, psid->pal, x_back, y_back, w, h, BB_HEIGHT_UNDER_BRIDGE - 5, cur_z, IsTransparencySet(TO_BRIDGES), 0, 0, -5);
00805 }
00806 }
00807 }
00808 }
00809
00819 static void DrawBridgeTramBits(int x, int y, byte z, int offset, bool overlay, bool head)
00820 {
00821 static const SpriteID tram_offsets[2][6] = { { 107, 108, 109, 110, 111, 112 }, { 4, 5, 15, 16, 17, 18 } };
00822 static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 };
00823 static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 };
00824
00825 static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 };
00826 static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 };
00827 static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 };
00828 static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 };
00829
00830
00831
00832 if (head || !IsInvisibilitySet(TO_BRIDGES)) {
00833 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE,
00834 x, y, size_x[offset], size_y[offset], 0x28, z,
00835 !head && IsTransparencySet(TO_BRIDGES));
00836 }
00837
00838
00839 if (!IsInvisibilitySet(TO_CATENARY)) {
00840 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset], PAL_NONE,
00841 x, y, size_x[offset], size_y[offset], 0x28, z,
00842 IsTransparencySet(TO_CATENARY));
00843 }
00844
00845
00846 EndSpriteCombine();
00847 StartSpriteCombine();
00848
00849
00850 if (!IsInvisibilitySet(TO_CATENARY)) {
00851 AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE,
00852 x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z,
00853 IsTransparencySet(TO_CATENARY), front_bb_offset_x[offset], front_bb_offset_y[offset]);
00854 }
00855 }
00856
00870 static void DrawTile_TunnelBridge(TileInfo *ti)
00871 {
00872 SpriteID image;
00873 TransportType transport_type = GetTunnelBridgeTransportType(ti->tile);
00874 DiagDirection tunnelbridge_direction = GetTunnelBridgeDirection(ti->tile);
00875
00876 if (IsTunnel(ti->tile)) {
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886 static const int _tunnel_BB[4][12] = {
00887
00888
00889 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 },
00890 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 },
00891 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 },
00892 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 },
00893 };
00894 const int *BB_data = _tunnel_BB[tunnelbridge_direction];
00895
00896 bool catenary = false;
00897
00898 if (transport_type == TRANSPORT_RAIL) {
00899 image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
00900 } else {
00901 image = SPR_TUNNEL_ENTRY_REAR_ROAD;
00902 }
00903
00904 if (HasTunnelBridgeSnowOrDesert(ti->tile)) image += 32;
00905
00906 image += tunnelbridge_direction * 2;
00907 DrawGroundSprite(image, PAL_NONE);
00908
00909
00910 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && (transport_type == TRANSPORT_RAIL && GetTunnelBridgeReservation(ti->tile))) {
00911 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
00912 DrawGroundSprite(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_y : rti->base_sprites.single_x, PALETTE_CRASH);
00913 }
00914
00915 if (transport_type == TRANSPORT_ROAD) {
00916 RoadTypes rts = GetRoadTypes(ti->tile);
00917
00918 if (HasBit(rts, ROADTYPE_TRAM)) {
00919 static const SpriteID tunnel_sprites[2][4] = { { 28, 78, 79, 27 }, { 5, 76, 77, 4 } };
00920
00921 DrawGroundSprite(SPR_TRAMWAY_BASE + tunnel_sprites[rts - ROADTYPES_TRAM][tunnelbridge_direction], PAL_NONE);
00922
00923
00924 if (!IsInvisibilitySet(TO_CATENARY)) {
00925 catenary = true;
00926 StartSpriteCombine();
00927 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);
00928 }
00929 }
00930 } else if (HasCatenaryDrawn(GetRailType(ti->tile))) {
00931
00932 DrawCatenary(ti);
00933
00934 catenary = true;
00935 StartSpriteCombine();
00936
00937 DrawCatenaryOnTunnel(ti);
00938 }
00939
00940 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);
00941
00942 if (catenary) EndSpriteCombine();
00943
00944
00945 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x , ti->y , BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
00946 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);
00947
00948 DrawBridgeMiddle(ti);
00949 } else {
00950 const PalSpriteID *psid;
00951 int base_offset;
00952 bool ice = HasTunnelBridgeSnowOrDesert(ti->tile);
00953
00954 if (transport_type == TRANSPORT_RAIL) {
00955 base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset;
00956 assert(base_offset != 8);
00957 } else {
00958 base_offset = 8;
00959 }
00960
00961
00962 assert( (base_offset & 0x07) == 0x00);
00963
00964 DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(tunnelbridge_direction)));
00965
00966
00967 base_offset += (6 - tunnelbridge_direction) % 4;
00968
00969 if (ti->tileh == SLOPE_FLAT) base_offset += 4;
00970
00971
00972 if (transport_type != TRANSPORT_WATER) {
00973 psid = &GetBridgeSpriteTable(GetBridgeType(ti->tile), BRIDGE_PIECE_HEAD)[base_offset];
00974 } else {
00975 psid = _aqueduct_sprites + base_offset;
00976 }
00977
00978 if (!ice) {
00979 DrawClearLandTile(ti, 3);
00980 } else {
00981 DrawGroundSprite(SPR_FLAT_SNOWY_TILE + _tileh_to_sprite[ti->tileh], PAL_NONE);
00982 }
00983
00984
00985
00986
00987 if (transport_type == TRANSPORT_ROAD) StartSpriteCombine();
00988
00989
00990
00991
00992
00993 AddSortableSpriteToDraw(psid->sprite, psid->pal, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 0 : 8, ti->z);
00994
00995 if (_settings_client.gui.show_track_reservation && transport_type == TRANSPORT_RAIL && GetTunnelBridgeReservation(ti->tile)) {
00996 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
00997 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
00998 AddSortableSpriteToDraw(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_y : rti->base_sprites.single_x, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, ti->z + 8);
00999 } else {
01000 AddSortableSpriteToDraw(rti->base_sprites.single_sloped + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
01001 }
01002 }
01003
01004 if (transport_type == TRANSPORT_ROAD) {
01005 RoadTypes rts = GetRoadTypes(ti->tile);
01006
01007 if (HasBit(rts, ROADTYPE_TRAM)) {
01008 uint offset = tunnelbridge_direction;
01009 uint z = ti->z;
01010 if (ti->tileh != SLOPE_FLAT) {
01011 offset = (offset + 1) & 1;
01012 z += TILE_HEIGHT;
01013 } else {
01014 offset += 2;
01015 }
01016
01017 DrawBridgeTramBits(ti->x, ti->y, z, offset, HasBit(rts, ROADTYPE_ROAD), true);
01018 }
01019 EndSpriteCombine();
01020 } else if (transport_type == TRANSPORT_RAIL) {
01021 if (HasCatenaryDrawn(GetRailType(ti->tile))) {
01022 DrawCatenary(ti);
01023 }
01024 }
01025
01026 DrawBridgeMiddle(ti);
01027 }
01028 }
01029
01030
01048 static BridgePieces CalcBridgePiece(uint north, uint south)
01049 {
01050 if (north == 1) {
01051 return BRIDGE_PIECE_NORTH;
01052 } else if (south == 1) {
01053 return BRIDGE_PIECE_SOUTH;
01054 } else if (north < south) {
01055 return north & 1 ? BRIDGE_PIECE_INNER_SOUTH : BRIDGE_PIECE_INNER_NORTH;
01056 } else if (north > south) {
01057 return south & 1 ? BRIDGE_PIECE_INNER_NORTH : BRIDGE_PIECE_INNER_SOUTH;
01058 } else {
01059 return north & 1 ? BRIDGE_PIECE_MIDDLE_EVEN : BRIDGE_PIECE_MIDDLE_ODD;
01060 }
01061 }
01062
01063
01064 void DrawBridgeMiddle(const TileInfo *ti)
01065 {
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083 static const int BRIDGE_Z_START = 3;
01084
01085 if (!IsBridgeAbove(ti->tile)) return;
01086
01087 TileIndex rampnorth = GetNorthernBridgeEnd(ti->tile);
01088 TileIndex rampsouth = GetSouthernBridgeEnd(ti->tile);
01089 TransportType transport_type = GetTunnelBridgeTransportType(rampsouth);
01090
01091 Axis axis = GetBridgeAxis(ti->tile);
01092 BridgePieces piece = CalcBridgePiece(
01093 GetTunnelBridgeLength(ti->tile, rampnorth) + 1,
01094 GetTunnelBridgeLength(ti->tile, rampsouth) + 1
01095 );
01096
01097 const PalSpriteID *psid;
01098 bool drawfarpillar;
01099 if (transport_type != TRANSPORT_WATER) {
01100 BridgeType type = GetBridgeType(rampsouth);
01101 drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0);
01102
01103 uint base_offset;
01104 if (transport_type == TRANSPORT_RAIL) {
01105 base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset;
01106 } else {
01107 base_offset = 8;
01108 }
01109
01110 psid = base_offset + GetBridgeSpriteTable(type, piece);
01111 } else {
01112 drawfarpillar = true;
01113 psid = _aqueduct_sprites;
01114 }
01115
01116 if (axis != AXIS_X) psid += 4;
01117
01118 int x = ti->x;
01119 int y = ti->y;
01120 uint bridge_z = GetBridgeHeight(rampsouth);
01121 uint z = bridge_z - BRIDGE_Z_START;
01122
01123
01124 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, x, y, 16, 16, 1, bridge_z - TILE_HEIGHT + BB_Z_SEPARATOR);
01125
01126
01127 if (transport_type == TRANSPORT_ROAD) StartSpriteCombine();
01128
01129
01130 if (!IsInvisibilitySet(TO_BRIDGES)) {
01131 if (axis == AXIS_X) {
01132 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 1, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
01133 } else {
01134 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 1, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
01135 }
01136 }
01137
01138 psid++;
01139
01140 if (transport_type == TRANSPORT_ROAD) {
01141 RoadTypes rts = GetRoadTypes(rampsouth);
01142
01143 if (HasBit(rts, ROADTYPE_TRAM)) {
01144
01145 DrawBridgeTramBits(x, y, bridge_z, axis ^ 1, HasBit(rts, ROADTYPE_ROAD), false);
01146 } else {
01147 EndSpriteCombine();
01148 StartSpriteCombine();
01149 }
01150 } else if (transport_type == TRANSPORT_RAIL) {
01151 if (HasCatenaryDrawn(GetRailType(rampsouth))) {
01152 DrawCatenaryOnBridge(ti);
01153 }
01154 }
01155
01156
01157 if (!IsInvisibilitySet(TO_BRIDGES)) {
01158 if (axis == AXIS_X) {
01159 y += 12;
01160 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 4, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 3, BRIDGE_Z_START);
01161 } else {
01162 x += 12;
01163 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 4, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 3, 0, BRIDGE_Z_START);
01164 }
01165 }
01166
01167
01168 if (transport_type == TRANSPORT_ROAD) EndSpriteCombine();
01169
01170
01171 if (IsInvisibilitySet(TO_BRIDGES)) return;
01172
01173 psid++;
01174 if (ti->z + 5 == z) {
01175
01176 if (psid->sprite != 0) {
01177 SpriteID image = psid->sprite;
01178 SpriteID pal = psid->pal;
01179 if (IsTransparencySet(TO_BRIDGES)) {
01180 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
01181 pal = PALETTE_TO_TRANSPARENT;
01182 }
01183
01184 DrawGroundSpriteAt(image, pal, x, y, z);
01185 }
01186 } else if (_settings_client.gui.bridge_pillars) {
01187
01188 DrawBridgePillars(psid, ti, axis, drawfarpillar, x, y, z);
01189 }
01190 }
01191
01192
01193 static uint GetSlopeZ_TunnelBridge(TileIndex tile, uint x, uint y)
01194 {
01195 uint z;
01196 Slope tileh = GetTileSlope(tile, &z);
01197
01198 x &= 0xF;
01199 y &= 0xF;
01200
01201 if (IsTunnel(tile)) {
01202 uint pos = (DiagDirToAxis(GetTunnelBridgeDirection(tile)) == AXIS_X ? y : x);
01203
01204
01205 if (5 <= pos && pos <= 10) return z;
01206 } else {
01207 DiagDirection dir = GetTunnelBridgeDirection(tile);
01208 uint pos = (DiagDirToAxis(dir) == AXIS_X ? y : x);
01209
01210 z += ApplyFoundationToSlope(GetBridgeFoundation(tileh, DiagDirToAxis(dir)), &tileh);
01211
01212
01213 if (5 <= pos && pos <= 10) {
01214 uint delta;
01215
01216 if (tileh != SLOPE_FLAT) return z + TILE_HEIGHT;
01217
01218 switch (dir) {
01219 default: NOT_REACHED();
01220 case DIAGDIR_NE: delta = (TILE_SIZE - 1 - x) / 2; break;
01221 case DIAGDIR_SE: delta = y / 2; break;
01222 case DIAGDIR_SW: delta = x / 2; break;
01223 case DIAGDIR_NW: delta = (TILE_SIZE - 1 - y) / 2; break;
01224 }
01225 return z + 1 + delta;
01226 }
01227 }
01228
01229 return z + GetPartialZ(x, y, tileh);
01230 }
01231
01232 static Foundation GetFoundation_TunnelBridge(TileIndex tile, Slope tileh)
01233 {
01234 return IsTunnel(tile) ? FOUNDATION_NONE : GetBridgeFoundation(tileh, DiagDirToAxis(GetTunnelBridgeDirection(tile)));
01235 }
01236
01237
01238 static void GetAcceptedCargo_TunnelBridge(TileIndex tile, AcceptedCargo ac)
01239 {
01240
01241 }
01242
01243 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
01244 {
01245 TransportType tt = GetTunnelBridgeTransportType(tile);
01246
01247 if (IsTunnel(tile)) {
01248 td->str = (tt == TRANSPORT_RAIL) ? STR_5017_RAILROAD_TUNNEL : STR_5018_ROAD_TUNNEL;
01249 } else {
01250 td->str = (tt == TRANSPORT_WATER) ? STR_AQUEDUCT : GetBridgeSpec(GetBridgeType(tile))->transport_name[tt];
01251 }
01252 td->owner[0] = GetTileOwner(tile);
01253
01254 Owner road_owner = INVALID_OWNER;
01255 Owner tram_owner = INVALID_OWNER;
01256 RoadTypes rts = GetRoadTypes(tile);
01257 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01258 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01259
01260
01261 if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
01262 (road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
01263 uint i = 1;
01264 if (road_owner != INVALID_OWNER) {
01265 td->owner_type[i] = STR_ROAD_OWNER;
01266 td->owner[i] = road_owner;
01267 i++;
01268 }
01269 if (tram_owner != INVALID_OWNER) {
01270 td->owner_type[i] = STR_TRAM_OWNER;
01271 td->owner[i] = tram_owner;
01272 }
01273 }
01274 }
01275
01276
01277 static void AnimateTile_TunnelBridge(TileIndex tile)
01278 {
01279
01280 }
01281
01282 static void TileLoop_TunnelBridge(TileIndex tile)
01283 {
01284 bool snow_or_desert = HasTunnelBridgeSnowOrDesert(tile);
01285 switch (_settings_game.game_creation.landscape) {
01286 case LT_ARCTIC:
01287 if (snow_or_desert != (GetTileZ(tile) > GetSnowLine())) {
01288 SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert);
01289 MarkTileDirtyByTile(tile);
01290 }
01291 break;
01292
01293 case LT_TROPIC:
01294 if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
01295 SetTunnelBridgeSnowOrDesert(tile, true);
01296 MarkTileDirtyByTile(tile);
01297 }
01298 break;
01299
01300 default:
01301 break;
01302 }
01303 }
01304
01305 static bool ClickTile_TunnelBridge(TileIndex tile)
01306 {
01307
01308 return false;
01309 }
01310
01311
01312 static TrackStatus GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
01313 {
01314 TransportType transport_type = GetTunnelBridgeTransportType(tile);
01315 if (transport_type != mode || (transport_type == TRANSPORT_ROAD && (GetRoadTypes(tile) & sub_mode) == 0)) return 0;
01316
01317 DiagDirection dir = GetTunnelBridgeDirection(tile);
01318 if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0;
01319 return CombineTrackStatus(TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)), TRACKDIR_BIT_NONE);
01320 }
01321
01322 static void ChangeTileOwner_TunnelBridge(TileIndex tile, Owner old_owner, Owner new_owner)
01323 {
01324 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
01325
01326 if (GetRoadOwner(tile, rt) == old_owner) {
01327 SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
01328 }
01329 }
01330
01331 if (!IsTileOwner(tile, old_owner)) return;
01332
01333 if (new_owner != INVALID_OWNER) {
01334 SetTileOwner(tile, new_owner);
01335 } else {
01336 if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR))) {
01337
01338
01339
01340
01341 assert(GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL);
01342 SetTileOwner(tile, OWNER_NONE);
01343 }
01344 }
01345 }
01346
01347
01348 static const byte _tunnel_fractcoord_1[4] = {0x8E, 0x18, 0x81, 0xE8};
01349 static const byte _tunnel_fractcoord_2[4] = {0x81, 0x98, 0x87, 0x38};
01350 static const byte _tunnel_fractcoord_3[4] = {0x82, 0x88, 0x86, 0x48};
01351 static const byte _exit_tunnel_track[4] = {1, 2, 1, 2};
01352
01354 static const Trackdir _road_exit_tunnel_state[DIAGDIR_END] = {
01355 TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
01356 };
01357 static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4};
01358
01359 static const byte _tunnel_fractcoord_4[4] = {0x52, 0x85, 0x98, 0x29};
01360 static const byte _tunnel_fractcoord_5[4] = {0x92, 0x89, 0x58, 0x25};
01361 static const byte _tunnel_fractcoord_6[4] = {0x92, 0x89, 0x56, 0x45};
01362 static const byte _tunnel_fractcoord_7[4] = {0x52, 0x85, 0x96, 0x49};
01363
01364 static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
01365 {
01366 int z = GetSlopeZ(x, y) - v->z_pos;
01367
01368 if (abs(z) > 2) return VETSB_CANNOT_ENTER;
01369 const DiagDirection dir = GetTunnelBridgeDirection(tile);
01370
01371 if (IsTunnel(tile)) {
01372 byte fc;
01373 DiagDirection vdir;
01374
01375 if (v->type == VEH_TRAIN) {
01376 fc = (x & 0xF) + (y << 4);
01377
01378 vdir = DirToDiagDir(v->direction);
01379
01380 if (v->u.rail.track != TRACK_BIT_WORMHOLE && dir == vdir) {
01381 if (IsFrontEngine(v) && fc == _tunnel_fractcoord_1[dir]) {
01382 if (!PlayVehicleSound(v, VSE_TUNNEL) && RailVehInfo(v->engine_type)->engclass == 0) {
01383 SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
01384 }
01385 return VETSB_CONTINUE;
01386 }
01387 if (fc == _tunnel_fractcoord_2[dir]) {
01388 v->tile = tile;
01389 v->u.rail.track = TRACK_BIT_WORMHOLE;
01390 v->vehstatus |= VS_HIDDEN;
01391 return VETSB_ENTERED_WORMHOLE;
01392 }
01393 }
01394
01395 if (dir == ReverseDiagDir(vdir) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
01396
01397 v->tile = tile;
01398 v->u.rail.track = (TrackBits)_exit_tunnel_track[dir];
01399 assert(v->u.rail.track);
01400 v->vehstatus &= ~VS_HIDDEN;
01401 return VETSB_ENTERED_WORMHOLE;
01402 }
01403 } else if (v->type == VEH_ROAD) {
01404 fc = (x & 0xF) + (y << 4);
01405 vdir = DirToDiagDir(v->direction);
01406
01407
01408 if (v->u.road.state != RVSB_WORMHOLE && dir == vdir) {
01409 if (fc == _tunnel_fractcoord_4[dir] ||
01410 fc == _tunnel_fractcoord_5[dir]) {
01411 v->tile = tile;
01412 v->u.road.state = RVSB_WORMHOLE;
01413 v->vehstatus |= VS_HIDDEN;
01414 return VETSB_ENTERED_WORMHOLE;
01415 } else {
01416 return VETSB_CONTINUE;
01417 }
01418 }
01419
01420 if (dir == ReverseDiagDir(vdir) && (
01421
01422 fc == _tunnel_fractcoord_6[dir] ||
01423 fc == _tunnel_fractcoord_7[dir]
01424 ) &&
01425 z == 0) {
01426 v->tile = tile;
01427 v->u.road.state = _road_exit_tunnel_state[dir];
01428 v->u.road.frame = _road_exit_tunnel_frame[dir];
01429 v->vehstatus &= ~VS_HIDDEN;
01430 return VETSB_ENTERED_WORMHOLE;
01431 }
01432 }
01433 } else {
01434
01435 if (v->IsPrimaryVehicle() && v->type != VEH_SHIP) {
01436
01437 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
01438
01439 if (v->type == VEH_ROAD) spd *= 2;
01440 if (v->cur_speed > spd) v->cur_speed = spd;
01441 }
01442
01443 if (DirToDiagDir(v->direction) == dir) {
01444 switch (dir) {
01445 default: NOT_REACHED();
01446 case DIAGDIR_NE: if ((x & 0xF) != 0) return VETSB_CONTINUE; break;
01447 case DIAGDIR_SE: if ((y & 0xF) != TILE_SIZE - 1) return VETSB_CONTINUE; break;
01448 case DIAGDIR_SW: if ((x & 0xF) != TILE_SIZE - 1) return VETSB_CONTINUE; break;
01449 case DIAGDIR_NW: if ((y & 0xF) != 0) return VETSB_CONTINUE; break;
01450 }
01451 switch (v->type) {
01452 case VEH_TRAIN:
01453 v->u.rail.track = TRACK_BIT_WORMHOLE;
01454 ClrBit(v->u.rail.flags, VRF_GOINGUP);
01455 ClrBit(v->u.rail.flags, VRF_GOINGDOWN);
01456 break;
01457
01458 case VEH_ROAD:
01459 v->u.road.state = RVSB_WORMHOLE;
01460 break;
01461
01462 case VEH_SHIP:
01463 v->u.ship.state = TRACK_BIT_WORMHOLE;
01464 break;
01465
01466 default: NOT_REACHED();
01467 }
01468 return VETSB_ENTERED_WORMHOLE;
01469 } else if (DirToDiagDir(v->direction) == ReverseDiagDir(dir)) {
01470 v->tile = tile;
01471 switch (v->type) {
01472 case VEH_TRAIN:
01473 if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
01474 v->u.rail.track = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
01475 return VETSB_ENTERED_WORMHOLE;
01476 }
01477 break;
01478
01479 case VEH_ROAD:
01480 if (v->u.road.state == RVSB_WORMHOLE) {
01481 v->u.road.state = _road_exit_tunnel_state[dir];
01482 v->u.road.frame = 0;
01483 return VETSB_ENTERED_WORMHOLE;
01484 }
01485 break;
01486
01487 case VEH_SHIP:
01488 if (v->u.ship.state == TRACK_BIT_WORMHOLE) {
01489 v->u.ship.state = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
01490 return VETSB_ENTERED_WORMHOLE;
01491 }
01492 break;
01493
01494 default: NOT_REACHED();
01495 }
01496 }
01497 }
01498 return VETSB_CONTINUE;
01499 }
01500
01501 static CommandCost TerraformTile_TunnelBridge(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
01502 {
01503 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled() && IsBridge(tile) && GetTunnelBridgeTransportType(tile) != TRANSPORT_WATER) {
01504 DiagDirection direction = GetTunnelBridgeDirection(tile);
01505 Axis axis = DiagDirToAxis(direction);
01506 CommandCost res;
01507 uint z_old;
01508 Slope tileh_old = GetTileSlope(tile, &z_old);
01509
01510
01511 if ((direction == DIAGDIR_NW) || (direction == DIAGDIR_NE)) {
01512 CheckBridgeSlopeSouth(axis, &tileh_old, &z_old);
01513 res = CheckBridgeSlopeSouth(axis, &tileh_new, &z_new);
01514 } else {
01515 CheckBridgeSlopeNorth(axis, &tileh_old, &z_old);
01516 res = CheckBridgeSlopeNorth(axis, &tileh_new, &z_new);
01517 }
01518
01519
01520 if (!CmdFailed(res) && (z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
01521 }
01522
01523 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
01524 }
01525
01526 extern const TileTypeProcs _tile_type_tunnelbridge_procs = {
01527 DrawTile_TunnelBridge,
01528 GetSlopeZ_TunnelBridge,
01529 ClearTile_TunnelBridge,
01530 GetAcceptedCargo_TunnelBridge,
01531 GetTileDesc_TunnelBridge,
01532 GetTileTrackStatus_TunnelBridge,
01533 ClickTile_TunnelBridge,
01534 AnimateTile_TunnelBridge,
01535 TileLoop_TunnelBridge,
01536 ChangeTileOwner_TunnelBridge,
01537 NULL,
01538 VehicleEnter_TunnelBridge,
01539 GetFoundation_TunnelBridge,
01540 TerraformTile_TunnelBridge,
01541 };