Fix ca19a0d: Send the proper network command when loading favorite face
[openttd-github.git] / src / tunnelbridge_cmd.cpp
blobdc345c8fee799a201074b1303e80291a93b0f2b7
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /**
9 * @file tunnelbridge_cmd.cpp
10 * This file deals with tunnels and bridges (non-gui stuff)
11 * @todo separate this file into two
14 #include "stdafx.h"
15 #include "newgrf_object.h"
16 #include "viewport_func.h"
17 #include "command_func.h"
18 #include "town.h"
19 #include "train.h"
20 #include "ship.h"
21 #include "roadveh.h"
22 #include "pathfinder/yapf/yapf_cache.h"
23 #include "newgrf_sound.h"
24 #include "autoslope.h"
25 #include "tunnelbridge_map.h"
26 #include "strings_func.h"
27 #include "date_func.h"
28 #include "clear_func.h"
29 #include "vehicle_func.h"
30 #include "sound_func.h"
31 #include "tunnelbridge.h"
32 #include "cheat_type.h"
33 #include "elrail_func.h"
34 #include "pbs.h"
35 #include "company_base.h"
36 #include "newgrf_railtype.h"
37 #include "newgrf_roadtype.h"
38 #include "object_base.h"
39 #include "water.h"
40 #include "company_gui.h"
41 #include "station_func.h"
42 #include "tunnelbridge_cmd.h"
43 #include "landscape_cmd.h"
44 #include "terraform_cmd.h"
46 #include "table/strings.h"
47 #include "table/bridge_land.h"
49 #include "safeguards.h"
51 BridgeSpec _bridge[MAX_BRIDGES]; ///< The specification of all bridges.
52 TileIndex _build_tunnel_endtile; ///< The end of a tunnel; as hidden return from the tunnel build command for GUI purposes.
54 /** Z position of the bridge sprites relative to bridge height (downwards) */
55 static const int BRIDGE_Z_START = 3;
58 /**
59 * Mark bridge tiles dirty.
60 * Note: The bridge does not need to exist, everything is passed via parameters.
61 * @param begin Start tile.
62 * @param end End tile.
63 * @param direction Direction from \a begin to \a end.
64 * @param bridge_height Bridge height level.
66 void MarkBridgeDirty(TileIndex begin, TileIndex end, DiagDirection direction, uint bridge_height)
68 TileIndexDiff delta = TileOffsByDiagDir(direction);
69 for (TileIndex t = begin; t != end; t += delta) {
70 MarkTileDirtyByTile(t, bridge_height - TileHeight(t));
72 MarkTileDirtyByTile(end);
75 /**
76 * Mark bridge tiles dirty.
77 * @param tile Bridge head.
79 void MarkBridgeDirty(TileIndex tile)
81 MarkBridgeDirty(tile, GetOtherTunnelBridgeEnd(tile), GetTunnelBridgeDirection(tile), GetBridgeHeight(tile));
84 /** Reset the data been eventually changed by the grf loaded. */
85 void ResetBridges()
87 /* First, free sprite table data */
88 for (BridgeType i = 0; i < MAX_BRIDGES; i++) {
89 if (_bridge[i].sprite_table != nullptr) {
90 for (BridgePieces j = BRIDGE_PIECE_NORTH; j < BRIDGE_PIECE_INVALID; j++) free(_bridge[i].sprite_table[j]);
91 free(_bridge[i].sprite_table);
95 /* Then, wipe out current bridges */
96 memset(&_bridge, 0, sizeof(_bridge));
97 /* And finally, reinstall default data */
98 memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge));
102 * Calculate the price factor for building a long bridge.
103 * Basically the cost delta is 1,1, 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5, 6,6,6,6,6,6, 7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,
104 * @param length Length of the bridge.
105 * @return Price factor for the bridge.
107 int CalcBridgeLenCostFactor(int length)
109 if (length < 2) return length;
111 length -= 2;
112 int sum = 2;
113 for (int delta = 1;; delta++) {
114 for (int count = 0; count < delta; count++) {
115 if (length == 0) return sum;
116 sum += delta;
117 length--;
123 * Get the foundation for a bridge.
124 * @param tileh The slope to build the bridge on.
125 * @param axis The axis of the bridge entrance.
126 * @return The foundation required.
128 Foundation GetBridgeFoundation(Slope tileh, Axis axis)
130 if (tileh == SLOPE_FLAT ||
131 ((tileh == SLOPE_NE || tileh == SLOPE_SW) && axis == AXIS_X) ||
132 ((tileh == SLOPE_NW || tileh == SLOPE_SE) && axis == AXIS_Y)) return FOUNDATION_NONE;
134 return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh));
138 * Determines if the track on a bridge ramp is flat or goes up/down.
140 * @param tileh Slope of the tile under the bridge head
141 * @param axis Orientation of bridge
142 * @return true iff the track is flat.
144 bool HasBridgeFlatRamp(Slope tileh, Axis axis)
146 ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), &tileh);
147 /* If the foundation slope is flat the bridge has a non-flat ramp and vice versa. */
148 return (tileh != SLOPE_FLAT);
151 static inline const PalSpriteID *GetBridgeSpriteTable(int index, BridgePieces table)
153 const BridgeSpec *bridge = GetBridgeSpec(index);
154 assert(table < BRIDGE_PIECE_INVALID);
155 if (bridge->sprite_table == nullptr || bridge->sprite_table[table] == nullptr) {
156 return _bridge_sprite_table[index][table];
157 } else {
158 return bridge->sprite_table[table];
164 * Determines the foundation for the bridge head, and tests if the resulting slope is valid.
166 * @param bridge_piece Direction of the bridge head.
167 * @param axis Axis of the bridge
168 * @param tileh Slope of the tile under the north bridge head; returns slope on top of foundation
169 * @param z TileZ corresponding to tileh, gets modified as well
170 * @return Error or cost for bridge foundation
172 static CommandCost CheckBridgeSlope(BridgePieces bridge_piece, Axis axis, Slope *tileh, int *z)
174 assert(bridge_piece == BRIDGE_PIECE_NORTH || bridge_piece == BRIDGE_PIECE_SOUTH);
176 Foundation f = GetBridgeFoundation(*tileh, axis);
177 *z += ApplyFoundationToSlope(f, tileh);
179 Slope valid_inclined;
180 if (bridge_piece == BRIDGE_PIECE_NORTH) {
181 valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW);
182 } else {
183 valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE);
185 if ((*tileh != SLOPE_FLAT) && (*tileh != valid_inclined)) return CMD_ERROR;
187 if (f == FOUNDATION_NONE) return CommandCost();
189 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
193 * Is a bridge of the specified type and length available?
194 * @param bridge_type Wanted type of bridge.
195 * @param bridge_len Wanted length of the bridge.
196 * @param flags Type of operation.
197 * @return A succeeded (the requested bridge is available) or failed (it cannot be built) command.
199 CommandCost CheckBridgeAvailability(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
201 if (flags & DC_QUERY_COST) {
202 if (bridge_len <= _settings_game.construction.max_bridge_length) return CommandCost();
203 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
206 if (bridge_type >= MAX_BRIDGES) return CMD_ERROR;
208 const BridgeSpec *b = GetBridgeSpec(bridge_type);
209 if (b->avail_year > _cur_year) return CMD_ERROR;
211 uint max = std::min(b->max_length, _settings_game.construction.max_bridge_length);
213 if (b->min_length > bridge_len) return CMD_ERROR;
214 if (bridge_len <= max) return CommandCost();
215 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
219 * Calculate the base cost of clearing a tunnel/bridge per tile.
220 * @param tile Start tile of the tunnel/bridge.
221 * @return How much clearing this tunnel/bridge costs per tile.
223 static Money TunnelBridgeClearCost(TileIndex tile, Price base_price)
225 Money base_cost = _price[base_price];
227 /* Add the cost of the transport that is on the tunnel/bridge. */
228 switch (GetTunnelBridgeTransportType(tile)) {
229 case TRANSPORT_ROAD: {
230 RoadType road_rt = GetRoadTypeRoad(tile);
231 RoadType tram_rt = GetRoadTypeTram(tile);
233 if (road_rt != INVALID_ROADTYPE) {
234 base_cost += 2 * RoadClearCost(road_rt);
236 if (tram_rt != INVALID_ROADTYPE) {
237 base_cost += 2 * RoadClearCost(tram_rt);
239 } break;
241 case TRANSPORT_RAIL: base_cost += RailClearCost(GetRailType(tile)); break;
242 /* Aquaducts have their own clear price. */
243 case TRANSPORT_WATER: base_cost = _price[PR_CLEAR_AQUEDUCT]; break;
244 default: break;
247 return base_cost;
251 * Build a Bridge
252 * @param flags type of operation
253 * @param tile_end end tile
254 * @param tile_start start tile
255 * @param transport_type transport type.
256 * @param bridge_type bridge type (hi bh)
257 * @param road_rail_type rail type or road types.
258 * @return the cost of this operation or an error
260 CommandCost CmdBuildBridge(DoCommandFlag flags, TileIndex tile_end, TileIndex tile_start, TransportType transport_type, BridgeType bridge_type, byte road_rail_type)
262 CompanyID company = _current_company;
264 RailType railtype = INVALID_RAILTYPE;
265 RoadType roadtype = INVALID_ROADTYPE;
267 if (!IsValidTile(tile_start)) return_cmd_error(STR_ERROR_BRIDGE_THROUGH_MAP_BORDER);
269 /* type of bridge */
270 switch (transport_type) {
271 case TRANSPORT_ROAD:
272 roadtype = (RoadType)road_rail_type;
273 if (!ValParamRoadType(roadtype)) return CMD_ERROR;
274 break;
276 case TRANSPORT_RAIL:
277 railtype = (RailType)road_rail_type;
278 if (!ValParamRailtype(railtype)) return CMD_ERROR;
279 break;
281 case TRANSPORT_WATER:
282 break;
284 default:
285 /* Airports don't have bridges. */
286 return CMD_ERROR;
289 if (company == OWNER_DEITY) {
290 if (transport_type != TRANSPORT_ROAD) return CMD_ERROR;
291 const Town *town = CalcClosestTownFromTile(tile_start);
293 company = OWNER_TOWN;
295 /* If we are not within a town, we are not owned by the town */
296 if (town == nullptr || DistanceSquare(tile_start, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
297 company = OWNER_NONE;
301 if (tile_start == tile_end) {
302 return_cmd_error(STR_ERROR_CAN_T_START_AND_END_ON);
305 Axis direction;
306 if (TileX(tile_start) == TileX(tile_end)) {
307 direction = AXIS_Y;
308 } else if (TileY(tile_start) == TileY(tile_end)) {
309 direction = AXIS_X;
310 } else {
311 return_cmd_error(STR_ERROR_START_AND_END_MUST_BE_IN);
314 if (tile_end < tile_start) Swap(tile_start, tile_end);
316 uint bridge_len = GetTunnelBridgeLength(tile_start, tile_end);
317 if (transport_type != TRANSPORT_WATER) {
318 /* set and test bridge length, availability */
319 CommandCost ret = CheckBridgeAvailability(bridge_type, bridge_len, flags);
320 if (ret.Failed()) return ret;
321 } else {
322 if (bridge_len > _settings_game.construction.max_bridge_length) return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
324 bridge_len += 2; // begin and end tiles/ramps
326 int z_start;
327 int z_end;
328 Slope tileh_start = GetTileSlope(tile_start, &z_start);
329 Slope tileh_end = GetTileSlope(tile_end, &z_end);
330 bool pbs_reservation = false;
332 CommandCost terraform_cost_north = CheckBridgeSlope(BRIDGE_PIECE_NORTH, direction, &tileh_start, &z_start);
333 CommandCost terraform_cost_south = CheckBridgeSlope(BRIDGE_PIECE_SOUTH, direction, &tileh_end, &z_end);
335 /* Aqueducts can't be built of flat land. */
336 if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
337 if (z_start != z_end) return_cmd_error(STR_ERROR_BRIDGEHEADS_NOT_SAME_HEIGHT);
339 CommandCost cost(EXPENSES_CONSTRUCTION);
340 Owner owner;
341 bool is_new_owner;
342 RoadType road_rt = INVALID_ROADTYPE;
343 RoadType tram_rt = INVALID_ROADTYPE;
344 if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) &&
345 GetOtherBridgeEnd(tile_start) == tile_end &&
346 GetTunnelBridgeTransportType(tile_start) == transport_type) {
347 /* Replace a current bridge. */
349 switch (transport_type) {
350 case TRANSPORT_RAIL:
351 /* Keep the reservation, the path stays valid. */
352 pbs_reservation = HasTunnelBridgeReservation(tile_start);
353 break;
355 case TRANSPORT_ROAD:
356 /* Do not remove road types when upgrading a bridge */
357 road_rt = GetRoadTypeRoad(tile_start);
358 tram_rt = GetRoadTypeTram(tile_start);
359 break;
361 default: break;
364 /* If this is a railway bridge, make sure the railtypes match. */
365 if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) {
366 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
369 /* If this is a road bridge, make sure the roadtype matches. */
370 if (transport_type == TRANSPORT_ROAD) {
371 RoadType existing_rt = RoadTypeIsRoad(roadtype) ? road_rt : tram_rt;
372 if (existing_rt != roadtype && existing_rt != INVALID_ROADTYPE) {
373 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
377 /* Do not replace town bridges with lower speed bridges, unless in scenario editor. */
378 if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) &&
379 GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed &&
380 _game_mode != GM_EDITOR) {
381 Town *t = ClosestTownFromTile(tile_start, UINT_MAX);
383 if (t == nullptr) {
384 return CMD_ERROR;
385 } else {
386 SetDParam(0, t->index);
387 return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
391 /* Do not replace the bridge with the same bridge type. */
392 if (!(flags & DC_QUERY_COST) && (bridge_type == GetBridgeType(tile_start)) && (transport_type != TRANSPORT_ROAD || road_rt == roadtype || tram_rt == roadtype)) {
393 return_cmd_error(STR_ERROR_ALREADY_BUILT);
396 /* Do not allow replacing another company's bridges. */
397 if (!IsTileOwner(tile_start, company) && !IsTileOwner(tile_start, OWNER_TOWN) && !IsTileOwner(tile_start, OWNER_NONE)) {
398 return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
401 /* The cost of clearing the current bridge. */
402 cost.AddCost(bridge_len * TunnelBridgeClearCost(tile_start, PR_CLEAR_BRIDGE));
403 owner = GetTileOwner(tile_start);
405 /* If bridge belonged to bankrupt company, it has a new owner now */
406 is_new_owner = (owner == OWNER_NONE);
407 if (is_new_owner) owner = company;
408 } else {
409 /* Build a new bridge. */
411 bool allow_on_slopes = (_settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER);
413 /* Try and clear the start landscape */
414 CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile_start);
415 if (ret.Failed()) return ret;
416 cost = ret;
418 if (terraform_cost_north.Failed() || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
419 cost.AddCost(terraform_cost_north);
421 /* Try and clear the end landscape */
422 ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile_end);
423 if (ret.Failed()) return ret;
424 cost.AddCost(ret);
426 /* false - end tile slope check */
427 if (terraform_cost_south.Failed() || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
428 cost.AddCost(terraform_cost_south);
430 const TileIndex heads[] = {tile_start, tile_end};
431 for (int i = 0; i < 2; i++) {
432 if (IsBridgeAbove(heads[i])) {
433 TileIndex north_head = GetNorthernBridgeEnd(heads[i]);
435 if (direction == GetBridgeAxis(heads[i])) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
437 if (z_start + 1 == GetBridgeHeight(north_head)) {
438 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
443 TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
444 for (TileIndex tile = tile_start + delta; tile != tile_end; tile += delta) {
445 if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_ERROR_BRIDGE_TOO_LOW_FOR_TERRAIN);
447 if (z_start >= (GetTileZ(tile) + _settings_game.construction.max_bridge_height)) {
449 * Disallow too high bridges.
450 * Properly rendering a map where very high bridges (might) exist is expensive.
451 * See http://www.tt-forums.net/viewtopic.php?f=33&t=40844&start=980#p1131762
452 * for a detailed discussion. z_start here is one heightlevel below the bridge level.
454 return_cmd_error(STR_ERROR_BRIDGE_TOO_HIGH_FOR_TERRAIN);
457 if (IsBridgeAbove(tile)) {
458 /* Disallow crossing bridges for the time being */
459 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
462 switch (GetTileType(tile)) {
463 case MP_WATER:
464 if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below;
465 break;
467 case MP_RAILWAY:
468 if (!IsPlainRail(tile)) goto not_valid_below;
469 break;
471 case MP_ROAD:
472 if (IsRoadDepot(tile)) goto not_valid_below;
473 break;
475 case MP_TUNNELBRIDGE:
476 if (IsTunnel(tile)) break;
477 if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below;
478 if (z_start < GetBridgeHeight(tile)) goto not_valid_below;
479 break;
481 case MP_OBJECT: {
482 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
483 if ((spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) == 0) goto not_valid_below;
484 if (GetTileMaxZ(tile) + spec->height > z_start) goto not_valid_below;
485 break;
488 case MP_CLEAR:
489 break;
491 default:
492 not_valid_below:;
493 /* try and clear the middle landscape */
494 ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile);
495 if (ret.Failed()) return ret;
496 cost.AddCost(ret);
497 break;
500 if (flags & DC_EXEC) {
501 /* We do this here because when replacing a bridge with another
502 * type calling SetBridgeMiddle isn't needed. After all, the
503 * tile already has the has_bridge_above bits set. */
504 SetBridgeMiddle(tile, direction);
508 owner = company;
509 is_new_owner = true;
512 bool hasroad = road_rt != INVALID_ROADTYPE;
513 bool hastram = tram_rt != INVALID_ROADTYPE;
514 if (transport_type == TRANSPORT_ROAD) {
515 if (RoadTypeIsRoad(roadtype)) road_rt = roadtype;
516 if (RoadTypeIsTram(roadtype)) tram_rt = roadtype;
519 /* do the drill? */
520 if (flags & DC_EXEC) {
521 DiagDirection dir = AxisToDiagDir(direction);
523 Company *c = Company::GetIfValid(company);
524 switch (transport_type) {
525 case TRANSPORT_RAIL:
526 /* Add to company infrastructure count if required. */
527 if (is_new_owner && c != nullptr) c->infrastructure.rail[railtype] += bridge_len * TUNNELBRIDGE_TRACKBIT_FACTOR;
528 MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype);
529 MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype);
530 SetTunnelBridgeReservation(tile_start, pbs_reservation);
531 SetTunnelBridgeReservation(tile_end, pbs_reservation);
532 break;
534 case TRANSPORT_ROAD: {
535 if (is_new_owner) {
536 /* Also give unowned present roadtypes to new owner */
537 if (hasroad && GetRoadOwner(tile_start, RTT_ROAD) == OWNER_NONE) hasroad = false;
538 if (hastram && GetRoadOwner(tile_start, RTT_TRAM) == OWNER_NONE) hastram = false;
540 if (c != nullptr) {
541 /* Add all new road types to the company infrastructure counter. */
542 if (!hasroad && road_rt != INVALID_ROADTYPE) {
543 /* A full diagonal road tile has two road bits. */
544 c->infrastructure.road[road_rt] += bridge_len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
546 if (!hastram && tram_rt != INVALID_ROADTYPE) {
547 /* A full diagonal road tile has two road bits. */
548 c->infrastructure.road[tram_rt] += bridge_len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
551 Owner owner_road = hasroad ? GetRoadOwner(tile_start, RTT_ROAD) : company;
552 Owner owner_tram = hastram ? GetRoadOwner(tile_start, RTT_TRAM) : company;
553 MakeRoadBridgeRamp(tile_start, owner, owner_road, owner_tram, bridge_type, dir, road_rt, tram_rt);
554 MakeRoadBridgeRamp(tile_end, owner, owner_road, owner_tram, bridge_type, ReverseDiagDir(dir), road_rt, tram_rt);
555 break;
558 case TRANSPORT_WATER:
559 if (is_new_owner && c != nullptr) c->infrastructure.water += bridge_len * TUNNELBRIDGE_TRACKBIT_FACTOR;
560 MakeAqueductBridgeRamp(tile_start, owner, dir);
561 MakeAqueductBridgeRamp(tile_end, owner, ReverseDiagDir(dir));
562 CheckForDockingTile(tile_start);
563 CheckForDockingTile(tile_end);
564 break;
566 default:
567 NOT_REACHED();
570 /* Mark all tiles dirty */
571 MarkBridgeDirty(tile_start, tile_end, AxisToDiagDir(direction), z_start);
572 DirtyCompanyInfrastructureWindows(company);
575 if ((flags & DC_EXEC) && transport_type == TRANSPORT_RAIL) {
576 Track track = AxisToTrack(direction);
577 AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, company);
578 YapfNotifyTrackLayoutChange(tile_start, track);
581 /* Human players that build bridges get a selection to choose from (DC_QUERY_COST)
582 * It's unnecessary to execute this command every time for every bridge.
583 * So it is done only for humans and cost is computed in bridge_gui.cpp.
584 * For (non-spectated) AI, Towns this has to be of course calculated. */
585 Company *c = Company::GetIfValid(company);
586 if (!(flags & DC_QUERY_COST) || (c != nullptr && c->is_ai && company != _local_company)) {
587 switch (transport_type) {
588 case TRANSPORT_ROAD:
589 if (road_rt != INVALID_ROADTYPE) {
590 cost.AddCost(bridge_len * 2 * RoadBuildCost(road_rt));
592 if (tram_rt != INVALID_ROADTYPE) {
593 cost.AddCost(bridge_len * 2 * RoadBuildCost(tram_rt));
595 break;
597 case TRANSPORT_RAIL: cost.AddCost(bridge_len * RailBuildCost(railtype)); break;
598 default: break;
601 if (c != nullptr) bridge_len = CalcBridgeLenCostFactor(bridge_len);
603 if (transport_type != TRANSPORT_WATER) {
604 cost.AddCost((int64)bridge_len * _price[PR_BUILD_BRIDGE] * GetBridgeSpec(bridge_type)->price >> 8);
605 } else {
606 /* Aqueducts use a separate base cost. */
607 cost.AddCost((int64)bridge_len * _price[PR_BUILD_AQUEDUCT]);
612 return cost;
617 * Build Tunnel.
618 * @param flags type of operation
619 * @param start_tile start tile of tunnel
620 * @param transport_type transport type
621 * @param road_rail_type railtype or roadtype
622 * @return the cost of this operation or an error
624 CommandCost CmdBuildTunnel(DoCommandFlag flags, TileIndex start_tile, TransportType transport_type, byte road_rail_type)
626 CompanyID company = _current_company;
628 RailType railtype = INVALID_RAILTYPE;
629 RoadType roadtype = INVALID_ROADTYPE;
630 _build_tunnel_endtile = 0;
631 switch (transport_type) {
632 case TRANSPORT_RAIL:
633 railtype = (RailType)road_rail_type;
634 if (!ValParamRailtype(railtype)) return CMD_ERROR;
635 break;
637 case TRANSPORT_ROAD:
638 roadtype = (RoadType)road_rail_type;
639 if (!ValParamRoadType(roadtype)) return CMD_ERROR;
640 break;
642 default: return CMD_ERROR;
645 if (company == OWNER_DEITY) {
646 if (transport_type != TRANSPORT_ROAD) return CMD_ERROR;
647 const Town *town = CalcClosestTownFromTile(start_tile);
649 company = OWNER_TOWN;
651 /* If we are not within a town, we are not owned by the town */
652 if (town == nullptr || DistanceSquare(start_tile, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
653 company = OWNER_NONE;
657 int start_z;
658 int end_z;
659 Slope start_tileh = GetTileSlope(start_tile, &start_z);
660 DiagDirection direction = GetInclinedSlopeDirection(start_tileh);
661 if (direction == INVALID_DIAGDIR) return_cmd_error(STR_ERROR_SITE_UNSUITABLE_FOR_TUNNEL);
663 if (HasTileWaterGround(start_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
665 CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, start_tile);
666 if (ret.Failed()) return ret;
668 /* XXX - do NOT change 'ret' in the loop, as it is used as the price
669 * for the clearing of the entrance of the tunnel. Assigning it to
670 * cost before the loop will yield different costs depending on start-
671 * position, because of increased-cost-by-length: 'cost += cost >> 3' */
673 TileIndexDiff delta = TileOffsByDiagDir(direction);
674 DiagDirection tunnel_in_way_dir;
675 if (DiagDirToAxis(direction) == AXIS_Y) {
676 tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE;
677 } else {
678 tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW;
681 TileIndex end_tile = start_tile;
683 /* Tile shift coefficient. Will decrease for very long tunnels to avoid exponential growth of price*/
684 int tiles_coef = 3;
685 /* Number of tiles from start of tunnel */
686 int tiles = 0;
687 /* Number of tiles at which the cost increase coefficient per tile is halved */
688 int tiles_bump = 25;
690 CommandCost cost(EXPENSES_CONSTRUCTION);
691 Slope end_tileh;
692 for (;;) {
693 end_tile += delta;
694 if (!IsValidTile(end_tile)) return_cmd_error(STR_ERROR_TUNNEL_THROUGH_MAP_BORDER);
695 end_tileh = GetTileSlope(end_tile, &end_z);
697 if (start_z == end_z) break;
699 if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
700 return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY);
703 tiles++;
704 if (tiles == tiles_bump) {
705 tiles_coef++;
706 tiles_bump *= 2;
709 cost.AddCost(_price[PR_BUILD_TUNNEL]);
710 cost.AddCost(cost.GetCost() >> tiles_coef); // add a multiplier for longer tunnels
713 /* Add the cost of the entrance */
714 cost.AddCost(_price[PR_BUILD_TUNNEL]);
715 cost.AddCost(ret);
717 /* if the command fails from here on we want the end tile to be highlighted */
718 _build_tunnel_endtile = end_tile;
720 if (tiles > _settings_game.construction.max_tunnel_length) return_cmd_error(STR_ERROR_TUNNEL_TOO_LONG);
722 if (HasTileWaterGround(end_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
724 /* Clear the tile in any case */
725 ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, end_tile);
726 if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
727 cost.AddCost(ret);
729 /* slope of end tile must be complementary to the slope of the start tile */
730 if (end_tileh != ComplementSlope(start_tileh)) {
731 /* Mark the tile as already cleared for the terraform command.
732 * Do this for all tiles (like trees), not only objects. */
733 ClearedObjectArea *coa = FindClearedObject(end_tile);
734 if (coa == nullptr) {
735 coa = &_cleared_object_areas.emplace_back(ClearedObjectArea{ end_tile, TileArea(end_tile, 1, 1) });
738 /* Hide the tile from the terraforming command */
739 TileIndex old_first_tile = coa->first_tile;
740 coa->first_tile = INVALID_TILE;
742 /* CMD_TERRAFORM_LAND may append further items to _cleared_object_areas,
743 * however it will never erase or re-order existing items.
744 * _cleared_object_areas is a value-type self-resizing vector, therefore appending items
745 * may result in a backing-store re-allocation, which would invalidate the coa pointer.
746 * The index of the coa pointer into the _cleared_object_areas vector remains valid,
747 * and can be used safely after the CMD_TERRAFORM_LAND operation.
748 * Deliberately clear the coa pointer to avoid leaving dangling pointers which could
749 * inadvertently be dereferenced.
751 ClearedObjectArea *begin = _cleared_object_areas.data();
752 assert(coa >= begin && coa < begin + _cleared_object_areas.size());
753 size_t coa_index = coa - begin;
754 assert(coa_index < UINT_MAX); // more than 2**32 cleared areas would be a bug in itself
755 coa = nullptr;
757 ret = std::get<0>(Command<CMD_TERRAFORM_LAND>::Do(flags, end_tile, end_tileh & start_tileh, false));
758 _cleared_object_areas[(uint)coa_index].first_tile = old_first_tile;
759 if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
760 cost.AddCost(ret);
762 cost.AddCost(_price[PR_BUILD_TUNNEL]);
764 /* Pay for the rail/road in the tunnel including entrances */
765 switch (transport_type) {
766 case TRANSPORT_ROAD: cost.AddCost((tiles + 2) * RoadBuildCost(roadtype) * 2); break;
767 case TRANSPORT_RAIL: cost.AddCost((tiles + 2) * RailBuildCost(railtype)); break;
768 default: NOT_REACHED();
771 if (flags & DC_EXEC) {
772 Company *c = Company::GetIfValid(company);
773 uint num_pieces = (tiles + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
774 if (transport_type == TRANSPORT_RAIL) {
775 if (c != nullptr) c->infrastructure.rail[railtype] += num_pieces;
776 MakeRailTunnel(start_tile, company, direction, railtype);
777 MakeRailTunnel(end_tile, company, ReverseDiagDir(direction), railtype);
778 AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, company);
779 YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction));
780 } else {
781 if (c != nullptr) c->infrastructure.road[roadtype] += num_pieces * 2; // A full diagonal road has two road bits.
782 RoadType road_rt = RoadTypeIsRoad(roadtype) ? roadtype : INVALID_ROADTYPE;
783 RoadType tram_rt = RoadTypeIsTram(roadtype) ? roadtype : INVALID_ROADTYPE;
784 MakeRoadTunnel(start_tile, company, direction, road_rt, tram_rt);
785 MakeRoadTunnel(end_tile, company, ReverseDiagDir(direction), road_rt, tram_rt);
787 DirtyCompanyInfrastructureWindows(company);
790 return cost;
795 * Are we allowed to remove the tunnel or bridge at \a tile?
796 * @param tile End point of the tunnel or bridge.
797 * @return A succeeded command if the tunnel or bridge may be removed, a failed command otherwise.
799 static inline CommandCost CheckAllowRemoveTunnelBridge(TileIndex tile)
801 /* Floods can remove anything as well as the scenario editor */
802 if (_current_company == OWNER_WATER || _game_mode == GM_EDITOR) return CommandCost();
804 switch (GetTunnelBridgeTransportType(tile)) {
805 case TRANSPORT_ROAD: {
806 RoadType road_rt = GetRoadTypeRoad(tile);
807 RoadType tram_rt = GetRoadTypeTram(tile);
808 Owner road_owner = _current_company;
809 Owner tram_owner = _current_company;
811 if (road_rt != INVALID_ROADTYPE) road_owner = GetRoadOwner(tile, RTT_ROAD);
812 if (tram_rt != INVALID_ROADTYPE) tram_owner = GetRoadOwner(tile, RTT_TRAM);
814 /* We can remove unowned road and if the town allows it */
815 if (road_owner == OWNER_TOWN && _current_company != OWNER_TOWN && !(_settings_game.construction.extra_dynamite || _cheats.magic_bulldozer.value)) {
816 /* Town does not allow */
817 return CheckTileOwnership(tile);
819 if (road_owner == OWNER_NONE || road_owner == OWNER_TOWN) road_owner = _current_company;
820 if (tram_owner == OWNER_NONE) tram_owner = _current_company;
822 CommandCost ret = CheckOwnership(road_owner, tile);
823 if (ret.Succeeded()) ret = CheckOwnership(tram_owner, tile);
824 return ret;
827 case TRANSPORT_RAIL:
828 return CheckOwnership(GetTileOwner(tile));
830 case TRANSPORT_WATER: {
831 /* Always allow to remove aqueducts without owner. */
832 Owner aqueduct_owner = GetTileOwner(tile);
833 if (aqueduct_owner == OWNER_NONE) aqueduct_owner = _current_company;
834 return CheckOwnership(aqueduct_owner);
837 default: NOT_REACHED();
842 * Remove a tunnel from the game, update town rating, etc.
843 * @param tile Tile containing one of the endpoints of the tunnel.
844 * @param flags Command flags.
845 * @return Succeeded or failed command.
847 static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags)
849 CommandCost ret = CheckAllowRemoveTunnelBridge(tile);
850 if (ret.Failed()) return ret;
852 TileIndex endtile = GetOtherTunnelEnd(tile);
854 ret = TunnelBridgeIsFree(tile, endtile);
855 if (ret.Failed()) return ret;
857 _build_tunnel_endtile = endtile;
859 Town *t = nullptr;
860 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
861 t = ClosestTownFromTile(tile, UINT_MAX); // town penalty rating
863 /* Check if you are allowed to remove the tunnel owned by a town
864 * Removal depends on difficulty settings */
865 CommandCost ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
866 if (ret.Failed()) return ret;
869 /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
870 * you have a "Poor" (0) town rating */
871 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
872 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
875 Money base_cost = TunnelBridgeClearCost(tile, PR_CLEAR_TUNNEL);
876 uint len = GetTunnelBridgeLength(tile, endtile) + 2; // Don't forget the end tiles.
878 if (flags & DC_EXEC) {
879 if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
880 /* We first need to request values before calling DoClearSquare */
881 DiagDirection dir = GetTunnelBridgeDirection(tile);
882 Track track = DiagDirToDiagTrack(dir);
883 Owner owner = GetTileOwner(tile);
885 Train *v = nullptr;
886 if (HasTunnelBridgeReservation(tile)) {
887 v = GetTrainForReservation(tile, track);
888 if (v != nullptr) FreeTrainTrackReservation(v);
891 if (Company::IsValidID(owner)) {
892 Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
893 DirtyCompanyInfrastructureWindows(owner);
896 DoClearSquare(tile);
897 DoClearSquare(endtile);
899 /* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */
900 AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner);
901 AddSideToSignalBuffer(endtile, dir, owner);
903 YapfNotifyTrackLayoutChange(tile, track);
904 YapfNotifyTrackLayoutChange(endtile, track);
906 if (v != nullptr) TryPathReserve(v);
907 } else {
908 /* A full diagonal road tile has two road bits. */
909 UpdateCompanyRoadInfrastructure(GetRoadTypeRoad(tile), GetRoadOwner(tile, RTT_ROAD), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
910 UpdateCompanyRoadInfrastructure(GetRoadTypeTram(tile), GetRoadOwner(tile, RTT_TRAM), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
912 DoClearSquare(tile);
913 DoClearSquare(endtile);
917 return CommandCost(EXPENSES_CONSTRUCTION, len * base_cost);
922 * Remove a bridge from the game, update town rating, etc.
923 * @param tile Tile containing one of the endpoints of the bridge.
924 * @param flags Command flags.
925 * @return Succeeded or failed command.
927 static CommandCost DoClearBridge(TileIndex tile, DoCommandFlag flags)
929 CommandCost ret = CheckAllowRemoveTunnelBridge(tile);
930 if (ret.Failed()) return ret;
932 TileIndex endtile = GetOtherBridgeEnd(tile);
934 ret = TunnelBridgeIsFree(tile, endtile);
935 if (ret.Failed()) return ret;
937 DiagDirection direction = GetTunnelBridgeDirection(tile);
938 TileIndexDiff delta = TileOffsByDiagDir(direction);
940 Town *t = nullptr;
941 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
942 t = ClosestTownFromTile(tile, UINT_MAX); // town penalty rating
944 /* Check if you are allowed to remove the bridge owned by a town
945 * Removal depends on difficulty settings */
946 CommandCost ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
947 if (ret.Failed()) return ret;
950 /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
951 * you have a "Poor" (0) town rating */
952 if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
953 ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM, flags);
956 Money base_cost = TunnelBridgeClearCost(tile, PR_CLEAR_BRIDGE);
957 uint len = GetTunnelBridgeLength(tile, endtile) + 2; // Don't forget the end tiles.
959 if (flags & DC_EXEC) {
960 /* read this value before actual removal of bridge */
961 bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
962 Owner owner = GetTileOwner(tile);
963 int height = GetBridgeHeight(tile);
964 Train *v = nullptr;
966 if (rail && HasTunnelBridgeReservation(tile)) {
967 v = GetTrainForReservation(tile, DiagDirToDiagTrack(direction));
968 if (v != nullptr) FreeTrainTrackReservation(v);
971 bool removetile = false;
972 bool removeendtile = false;
974 /* Update company infrastructure counts. */
975 if (rail) {
976 if (Company::IsValidID(owner)) Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
977 } else if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) {
978 /* A full diagonal road tile has two road bits. */
979 UpdateCompanyRoadInfrastructure(GetRoadTypeRoad(tile), GetRoadOwner(tile, RTT_ROAD), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
980 UpdateCompanyRoadInfrastructure(GetRoadTypeTram(tile), GetRoadOwner(tile, RTT_TRAM), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
981 } else { // Aqueduct
982 if (Company::IsValidID(owner)) Company::Get(owner)->infrastructure.water -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
983 removetile = IsDockingTile(tile);
984 removeendtile = IsDockingTile(endtile);
986 DirtyCompanyInfrastructureWindows(owner);
988 DoClearSquare(tile);
989 DoClearSquare(endtile);
991 if (removetile) RemoveDockingTile(tile);
992 if (removeendtile) RemoveDockingTile(endtile);
993 for (TileIndex c = tile + delta; c != endtile; c += delta) {
994 /* do not let trees appear from 'nowhere' after removing bridge */
995 if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) {
996 int minz = GetTileMaxZ(c) + 3;
997 if (height < minz) SetRoadside(c, ROADSIDE_PAVED);
999 ClearBridgeMiddle(c);
1000 MarkTileDirtyByTile(c, height - TileHeight(c));
1003 if (rail) {
1004 /* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */
1005 AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner);
1006 AddSideToSignalBuffer(endtile, direction, owner);
1008 Track track = DiagDirToDiagTrack(direction);
1009 YapfNotifyTrackLayoutChange(tile, track);
1010 YapfNotifyTrackLayoutChange(endtile, track);
1012 if (v != nullptr) TryPathReserve(v, true);
1016 return CommandCost(EXPENSES_CONSTRUCTION, len * base_cost);
1020 * Remove a tunnel or a bridge from the game.
1021 * @param tile Tile containing one of the endpoints.
1022 * @param flags Command flags.
1023 * @return Succeeded or failed command.
1025 static CommandCost ClearTile_TunnelBridge(TileIndex tile, DoCommandFlag flags)
1027 if (IsTunnel(tile)) {
1028 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST);
1029 return DoClearTunnel(tile, flags);
1030 } else { // IsBridge(tile)
1031 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
1032 return DoClearBridge(tile, flags);
1037 * Draw a single pillar sprite.
1038 * @param psid Pillarsprite
1039 * @param x Pillar X
1040 * @param y Pillar Y
1041 * @param z Pillar Z
1042 * @param w Bounding box size in X direction
1043 * @param h Bounding box size in Y direction
1044 * @param subsprite Optional subsprite for drawing halfpillars
1046 static inline void DrawPillar(const PalSpriteID *psid, int x, int y, int z, int w, int h, const SubSprite *subsprite)
1048 static const int PILLAR_Z_OFFSET = TILE_HEIGHT - BRIDGE_Z_START; ///< Start offset of pillar wrt. bridge (downwards)
1049 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);
1053 * Draw two bridge pillars (north and south).
1054 * @param z_bottom Bottom Z
1055 * @param z_top Top Z
1056 * @param psid Pillarsprite
1057 * @param x Pillar X
1058 * @param y Pillar Y
1059 * @param w Bounding box size in X direction
1060 * @param h Bounding box size in Y direction
1061 * @return Reached Z at the bottom
1063 static int DrawPillarColumn(int z_bottom, int z_top, const PalSpriteID *psid, int x, int y, int w, int h)
1065 int cur_z;
1066 for (cur_z = z_top; cur_z >= z_bottom; cur_z -= TILE_HEIGHT) {
1067 DrawPillar(psid, x, y, cur_z, w, h, nullptr);
1069 return cur_z;
1073 * Draws the pillars under high bridges.
1075 * @param psid Image and palette of a bridge pillar.
1076 * @param ti #TileInfo of current bridge-middle-tile.
1077 * @param axis Orientation of bridge.
1078 * @param drawfarpillar Whether to draw the pillar at the back
1079 * @param x Sprite X position of front pillar.
1080 * @param y Sprite Y position of front pillar.
1081 * @param z_bridge Absolute height of bridge bottom.
1083 static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
1085 static const int bounding_box_size[2] = {16, 2}; ///< bounding box size of pillars along bridge direction
1086 static const int back_pillar_offset[2] = { 0, 9}; ///< sprite position offset of back facing pillar
1088 static const int INF = 1000; ///< big number compared to sprite size
1089 static const SubSprite half_pillar_sub_sprite[2][2] = {
1090 { { -14, -INF, INF, INF }, { -INF, -INF, -15, INF } }, // X axis, north and south
1091 { { -INF, -INF, 15, INF }, { 16, -INF, INF, INF } }, // Y axis, north and south
1094 if (psid->sprite == 0) return;
1096 /* Determine ground height under pillars */
1097 DiagDirection south_dir = AxisToDiagDir(axis);
1098 int z_front_north = ti->z;
1099 int z_back_north = ti->z;
1100 int z_front_south = ti->z;
1101 int z_back_south = ti->z;
1102 GetSlopePixelZOnEdge(ti->tileh, south_dir, &z_front_south, &z_back_south);
1103 GetSlopePixelZOnEdge(ti->tileh, ReverseDiagDir(south_dir), &z_front_north, &z_back_north);
1105 /* Shared height of pillars */
1106 int z_front = std::max(z_front_north, z_front_south);
1107 int z_back = std::max(z_back_north, z_back_south);
1109 /* x and y size of bounding-box of pillars */
1110 int w = bounding_box_size[axis];
1111 int h = bounding_box_size[OtherAxis(axis)];
1112 /* sprite position of back facing pillar */
1113 int x_back = x - back_pillar_offset[axis];
1114 int y_back = y - back_pillar_offset[OtherAxis(axis)];
1116 /* Draw front pillars */
1117 int bottom_z = DrawPillarColumn(z_front, z_bridge, psid, x, y, w, h);
1118 if (z_front_north < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
1119 if (z_front_south < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
1121 /* Draw back pillars, skip top two parts, which are hidden by the bridge */
1122 int z_bridge_back = z_bridge - 2 * (int)TILE_HEIGHT;
1123 if (drawfarpillar && (z_back_north <= z_bridge_back || z_back_south <= z_bridge_back)) {
1124 bottom_z = DrawPillarColumn(z_back, z_bridge_back, psid, x_back, y_back, w, h);
1125 if (z_back_north < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
1126 if (z_back_south < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
1131 * Retrieve the sprites required for catenary on a road/tram bridge.
1132 * @param rti RoadTypeInfo for the road or tram type to get catenary for
1133 * @param head_tile Bridge head tile with roadtype information
1134 * @param offset Sprite offset identifying flat to sloped bridge tiles
1135 * @param head Are we drawing bridge head?
1136 * @param[out] spr_back Back catenary sprite to use
1137 * @param[out] spr_front Front catenary sprite to use
1139 static void GetBridgeRoadCatenary(const RoadTypeInfo *rti, TileIndex head_tile, int offset, bool head, SpriteID &spr_back, SpriteID &spr_front)
1141 static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 };
1142 static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 };
1144 /* Simplified from DrawRoadTypeCatenary() to remove all the special cases required for regular ground road */
1145 spr_back = GetCustomRoadSprite(rti, head_tile, ROTSG_CATENARY_BACK, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1146 spr_front = GetCustomRoadSprite(rti, head_tile, ROTSG_CATENARY_FRONT, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1147 if (spr_back == 0 && spr_front == 0) {
1148 spr_back = SPR_TRAMWAY_BASE + back_offsets[offset];
1149 spr_front = SPR_TRAMWAY_BASE + front_offsets[offset];
1150 } else {
1151 if (spr_back != 0) spr_back += 23 + offset;
1152 if (spr_front != 0) spr_front += 23 + offset;
1157 * Draws the road and trambits over an already drawn (lower end) of a bridge.
1158 * @param head_tile bridge head tile with roadtype information
1159 * @param x the x of the bridge
1160 * @param y the y of the bridge
1161 * @param z the z of the bridge
1162 * @param offset sprite offset identifying flat to sloped bridge tiles
1163 * @param head are we drawing bridge head?
1165 static void DrawBridgeRoadBits(TileIndex head_tile, int x, int y, int z, int offset, bool head)
1167 RoadType road_rt = GetRoadTypeRoad(head_tile);
1168 RoadType tram_rt = GetRoadTypeTram(head_tile);
1169 const RoadTypeInfo *road_rti = road_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(road_rt);
1170 const RoadTypeInfo *tram_rti = tram_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(tram_rt);
1172 SpriteID seq_back[4] = { 0 };
1173 bool trans_back[4] = { false };
1174 SpriteID seq_front[4] = { 0 };
1175 bool trans_front[4] = { false };
1177 static const SpriteID overlay_offsets[6] = { 0, 1, 11, 12, 13, 14 };
1178 if (head || !IsInvisibilitySet(TO_BRIDGES)) {
1179 /* Road underlay takes precedence over tram */
1180 trans_back[0] = !head && IsTransparencySet(TO_BRIDGES);
1181 if (road_rti != nullptr) {
1182 if (road_rti->UsesOverlay()) {
1183 seq_back[0] = GetCustomRoadSprite(road_rti, head_tile, ROTSG_BRIDGE, head ? TCX_NORMAL : TCX_ON_BRIDGE) + offset;
1185 } else if (tram_rti != nullptr) {
1186 if (tram_rti->UsesOverlay()) {
1187 seq_back[0] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_BRIDGE, head ? TCX_NORMAL : TCX_ON_BRIDGE) + offset;
1188 } else {
1189 seq_back[0] = SPR_TRAMWAY_BRIDGE + offset;
1193 /* Draw road overlay */
1194 trans_back[1] = !head && IsTransparencySet(TO_BRIDGES);
1195 if (road_rti != nullptr) {
1196 if (road_rti->UsesOverlay()) {
1197 seq_back[1] = GetCustomRoadSprite(road_rti, head_tile, ROTSG_OVERLAY, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1198 if (seq_back[1] != 0) seq_back[1] += overlay_offsets[offset];
1202 /* Draw tram overlay */
1203 trans_back[2] = !head && IsTransparencySet(TO_BRIDGES);
1204 if (tram_rti != nullptr) {
1205 if (tram_rti->UsesOverlay()) {
1206 seq_back[2] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_OVERLAY, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1207 if (seq_back[2] != 0) seq_back[2] += overlay_offsets[offset];
1208 } else if (road_rti != nullptr) {
1209 seq_back[2] = SPR_TRAMWAY_OVERLAY + overlay_offsets[offset];
1213 /* Road catenary takes precedence over tram */
1214 trans_back[3] = IsTransparencySet(TO_CATENARY);
1215 trans_front[0] = IsTransparencySet(TO_CATENARY);
1216 if (road_rti != nullptr && HasRoadCatenaryDrawn(road_rt)) {
1217 GetBridgeRoadCatenary(road_rti, head_tile, offset, head, seq_back[3], seq_front[0]);
1218 } else if (tram_rti != nullptr && HasRoadCatenaryDrawn(tram_rt)) {
1219 GetBridgeRoadCatenary(tram_rti, head_tile, offset, head, seq_back[3], seq_front[0]);
1223 static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 };
1224 static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 };
1225 static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 };
1226 static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 };
1228 /* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called
1229 * The bounding boxes here are the same as for bridge front/roof */
1230 for (uint i = 0; i < lengthof(seq_back); ++i) {
1231 if (seq_back[i] != 0) {
1232 AddSortableSpriteToDraw(seq_back[i], PAL_NONE,
1233 x, y, size_x[offset], size_y[offset], 0x28, z,
1234 trans_back[i]);
1238 /* Start a new SpriteCombine for the front part */
1239 EndSpriteCombine();
1240 StartSpriteCombine();
1242 for (uint i = 0; i < lengthof(seq_front); ++i) {
1243 if (seq_front[i] != 0) {
1244 AddSortableSpriteToDraw(seq_front[i], PAL_NONE,
1245 x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z,
1246 trans_front[i],
1247 front_bb_offset_x[offset], front_bb_offset_y[offset]);
1253 * Draws a tunnel of bridge tile.
1254 * For tunnels, this is rather simple, as you only need to draw the entrance.
1255 * Bridges are a bit more complex. base_offset is where the sprite selection comes into play
1256 * and it works a bit like a bitmask.<p> For bridge heads:
1257 * @param ti TileInfo of the structure to draw
1258 * <ul><li>Bit 0: direction</li>
1259 * <li>Bit 1: northern or southern heads</li>
1260 * <li>Bit 2: Set if the bridge head is sloped</li>
1261 * <li>Bit 3 and more: Railtype Specific subset</li>
1262 * </ul>
1263 * Please note that in this code, "roads" are treated as railtype 1, whilst the real railtypes are 0, 2 and 3
1265 static void DrawTile_TunnelBridge(TileInfo *ti)
1267 TransportType transport_type = GetTunnelBridgeTransportType(ti->tile);
1268 DiagDirection tunnelbridge_direction = GetTunnelBridgeDirection(ti->tile);
1270 if (IsTunnel(ti->tile)) {
1271 /* Front view of tunnel bounding boxes:
1273 * 122223 <- BB_Z_SEPARATOR
1274 * 1 3
1275 * 1 3 1,3 = empty helper BB
1276 * 1 3 2 = SpriteCombine of tunnel-roof and catenary (tram & elrail)
1280 static const int _tunnel_BB[4][12] = {
1281 /* tunnnel-roof | Z-separator | tram-catenary
1282 * w h bb_x bb_y| x y w h |bb_x bb_y w h */
1283 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 }, // NE
1284 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 }, // SE
1285 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 }, // SW
1286 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 }, // NW
1288 const int *BB_data = _tunnel_BB[tunnelbridge_direction];
1290 bool catenary = false;
1292 SpriteID image;
1293 SpriteID railtype_overlay = 0;
1294 if (transport_type == TRANSPORT_RAIL) {
1295 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1296 image = rti->base_sprites.tunnel;
1297 if (rti->UsesOverlay()) {
1298 /* Check if the railtype has custom tunnel portals. */
1299 railtype_overlay = GetCustomRailSprite(rti, ti->tile, RTSG_TUNNEL_PORTAL);
1300 if (railtype_overlay != 0) image = SPR_RAILTYPE_TUNNEL_BASE; // Draw blank grass tunnel base.
1302 } else {
1303 image = SPR_TUNNEL_ENTRY_REAR_ROAD;
1306 if (HasTunnelBridgeSnowOrDesert(ti->tile)) image += railtype_overlay != 0 ? 8 : 32;
1308 image += tunnelbridge_direction * 2;
1309 DrawGroundSprite(image, PAL_NONE);
1311 if (transport_type == TRANSPORT_ROAD) {
1312 RoadType road_rt = GetRoadTypeRoad(ti->tile);
1313 RoadType tram_rt = GetRoadTypeTram(ti->tile);
1314 const RoadTypeInfo *road_rti = road_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(road_rt);
1315 const RoadTypeInfo *tram_rti = tram_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(tram_rt);
1316 uint sprite_offset = DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? 1 : 0;
1318 DrawRoadOverlays(ti, PAL_NONE, road_rti, tram_rti, sprite_offset, sprite_offset);
1320 /* Road catenary takes precedence over tram */
1321 SpriteID catenary_sprite_base = 0;
1322 if (road_rti != nullptr && HasRoadCatenaryDrawn(road_rt)) {
1323 catenary_sprite_base = GetCustomRoadSprite(road_rti, ti->tile, ROTSG_CATENARY_FRONT);
1324 if (catenary_sprite_base == 0) {
1325 catenary_sprite_base = SPR_TRAMWAY_TUNNEL_WIRES;
1326 } else {
1327 catenary_sprite_base += 19;
1329 } else if (tram_rti != nullptr && HasRoadCatenaryDrawn(tram_rt)) {
1330 catenary_sprite_base = GetCustomRoadSprite(tram_rti, ti->tile, ROTSG_CATENARY_FRONT);
1331 if (catenary_sprite_base == 0) {
1332 catenary_sprite_base = SPR_TRAMWAY_TUNNEL_WIRES;
1333 } else {
1334 catenary_sprite_base += 19;
1338 if (catenary_sprite_base != 0) {
1339 catenary = true;
1340 StartSpriteCombine();
1341 AddSortableSpriteToDraw(catenary_sprite_base + 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);
1343 } else {
1344 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1345 if (rti->UsesOverlay()) {
1346 SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_TUNNEL);
1347 if (surface != 0) DrawGroundSprite(surface + tunnelbridge_direction, PAL_NONE);
1350 /* PBS debugging, draw reserved tracks darker */
1351 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasTunnelBridgeReservation(ti->tile)) {
1352 if (rti->UsesOverlay()) {
1353 SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
1354 DrawGroundSprite(overlay + RTO_X + DiagDirToAxis(tunnelbridge_direction), PALETTE_CRASH);
1355 } else {
1356 DrawGroundSprite(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH);
1360 if (HasRailCatenaryDrawn(GetRailType(ti->tile))) {
1361 /* Maybe draw pylons on the entry side */
1362 DrawRailCatenary(ti);
1364 catenary = true;
1365 StartSpriteCombine();
1366 /* Draw wire above the ramp */
1367 DrawRailCatenaryOnTunnel(ti);
1371 if (railtype_overlay != 0 && !catenary) StartSpriteCombine();
1373 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);
1374 /* Draw railtype tunnel portal overlay if defined. */
1375 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);
1377 if (catenary || railtype_overlay != 0) EndSpriteCombine();
1379 /* Add helper BB for sprite sorting that separates the tunnel from things beside of it. */
1380 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x, ti->y, BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
1381 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);
1383 DrawBridgeMiddle(ti);
1384 } else { // IsBridge(ti->tile)
1385 const PalSpriteID *psid;
1386 int base_offset;
1387 bool ice = HasTunnelBridgeSnowOrDesert(ti->tile);
1389 if (transport_type == TRANSPORT_RAIL) {
1390 base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset;
1391 assert(base_offset != 8); // This one is used for roads
1392 } else {
1393 base_offset = 8;
1396 /* as the lower 3 bits are used for other stuff, make sure they are clear */
1397 assert( (base_offset & 0x07) == 0x00);
1399 DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(tunnelbridge_direction)));
1401 /* HACK Wizardry to convert the bridge ramp direction into a sprite offset */
1402 base_offset += (6 - tunnelbridge_direction) % 4;
1404 /* Table number BRIDGE_PIECE_HEAD always refers to the bridge heads for any bridge type */
1405 if (transport_type != TRANSPORT_WATER) {
1406 if (ti->tileh == SLOPE_FLAT) base_offset += 4; // sloped bridge head
1407 psid = &GetBridgeSpriteTable(GetBridgeType(ti->tile), BRIDGE_PIECE_HEAD)[base_offset];
1408 } else {
1409 psid = _aqueduct_sprites + base_offset;
1412 if (!ice) {
1413 TileIndex next = ti->tile + TileOffsByDiagDir(tunnelbridge_direction);
1414 if (ti->tileh != SLOPE_FLAT && ti->z == 0 && HasTileWaterClass(next) && GetWaterClass(next) == WATER_CLASS_SEA) {
1415 DrawShoreTile(ti->tileh);
1416 } else {
1417 DrawClearLandTile(ti, 3);
1419 } else {
1420 DrawGroundSprite(SPR_FLAT_SNOW_DESERT_TILE + SlopeToSpriteOffset(ti->tileh), PAL_NONE);
1423 /* draw ramp */
1425 /* Draw Trambits and PBS Reservation as SpriteCombine */
1426 if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
1428 /* HACK set the height of the BB of a sloped ramp to 1 so a vehicle on
1429 * it doesn't disappear behind it
1431 /* Bridge heads are drawn solid no matter how invisibility/transparency is set */
1432 AddSortableSpriteToDraw(psid->sprite, psid->pal, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 0 : 8, ti->z);
1434 if (transport_type == TRANSPORT_ROAD) {
1435 uint offset = tunnelbridge_direction;
1436 int z = ti->z;
1437 if (ti->tileh != SLOPE_FLAT) {
1438 offset = (offset + 1) & 1;
1439 z += TILE_HEIGHT;
1440 } else {
1441 offset += 2;
1444 /* DrawBridgeRoadBits() calls EndSpriteCombine() and StartSpriteCombine() */
1445 DrawBridgeRoadBits(ti->tile, ti->x, ti->y, z, offset, true);
1447 EndSpriteCombine();
1448 } else if (transport_type == TRANSPORT_RAIL) {
1449 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1450 if (rti->UsesOverlay()) {
1451 SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_BRIDGE);
1452 if (surface != 0) {
1453 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
1454 AddSortableSpriteToDraw(surface + ((DiagDirToAxis(tunnelbridge_direction) == AXIS_X) ? RTBO_X : RTBO_Y), PAL_NONE, ti->x, ti->y, 16, 16, 0, ti->z + 8);
1455 } else {
1456 AddSortableSpriteToDraw(surface + RTBO_SLOPE + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, 16, 16, 8, ti->z);
1459 /* Don't fallback to non-overlay sprite -- the spec states that
1460 * if an overlay is present then the bridge surface must be
1461 * present. */
1464 /* PBS debugging, draw reserved tracks darker */
1465 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasTunnelBridgeReservation(ti->tile)) {
1466 if (rti->UsesOverlay()) {
1467 SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
1468 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
1469 AddSortableSpriteToDraw(overlay + RTO_X + DiagDirToAxis(tunnelbridge_direction), PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, ti->z + 8);
1470 } else {
1471 AddSortableSpriteToDraw(overlay + RTO_SLOPE_NE + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
1473 } else {
1474 if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
1475 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);
1476 } else {
1477 AddSortableSpriteToDraw(rti->base_sprites.single_sloped + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
1482 EndSpriteCombine();
1483 if (HasRailCatenaryDrawn(GetRailType(ti->tile))) {
1484 DrawRailCatenary(ti);
1488 DrawBridgeMiddle(ti);
1494 * Compute bridge piece. Computes the bridge piece to display depending on the position inside the bridge.
1495 * bridges pieces sequence (middle parts).
1496 * Note that it is not covering the bridge heads, which are always referenced by the same sprite table.
1497 * bridge len 1: BRIDGE_PIECE_NORTH
1498 * bridge len 2: BRIDGE_PIECE_NORTH BRIDGE_PIECE_SOUTH
1499 * bridge len 3: BRIDGE_PIECE_NORTH BRIDGE_PIECE_MIDDLE_ODD BRIDGE_PIECE_SOUTH
1500 * bridge len 4: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH
1501 * bridge len 5: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_MIDDLE_EVEN BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH
1502 * bridge len 6: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH
1503 * bridge len 7: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_MIDDLE_ODD BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH
1504 * #0 - always as first, #1 - always as last (if len>1)
1505 * #2,#3 are to pair in order
1506 * for odd bridges: #5 is going in the bridge middle if on even position, #4 on odd (counting from 0)
1507 * @param north Northernmost tile of bridge
1508 * @param south Southernmost tile of bridge
1509 * @return Index of bridge piece
1511 static BridgePieces CalcBridgePiece(uint north, uint south)
1513 if (north == 1) {
1514 return BRIDGE_PIECE_NORTH;
1515 } else if (south == 1) {
1516 return BRIDGE_PIECE_SOUTH;
1517 } else if (north < south) {
1518 return north & 1 ? BRIDGE_PIECE_INNER_SOUTH : BRIDGE_PIECE_INNER_NORTH;
1519 } else if (north > south) {
1520 return south & 1 ? BRIDGE_PIECE_INNER_NORTH : BRIDGE_PIECE_INNER_SOUTH;
1521 } else {
1522 return north & 1 ? BRIDGE_PIECE_MIDDLE_EVEN : BRIDGE_PIECE_MIDDLE_ODD;
1527 * Draw the middle bits of a bridge.
1528 * @param ti Tile information of the tile to draw it on.
1530 void DrawBridgeMiddle(const TileInfo *ti)
1532 /* Sectional view of bridge bounding boxes:
1534 * 1 2 1,2 = SpriteCombine of Bridge front/(back&floor) and RoadCatenary
1535 * 1 2 3 = empty helper BB
1536 * 1 7 2 4,5 = pillars under higher bridges
1537 * 1 6 88888 6 2 6 = elrail-pylons
1538 * 1 6 88888 6 2 7 = elrail-wire
1539 * 1 6 88888 6 2 <- TILE_HEIGHT 8 = rail-vehicle on bridge
1540 * 3333333333333 <- BB_Z_SEPARATOR
1541 * <- unused
1542 * 4 5 <- BB_HEIGHT_UNDER_BRIDGE
1543 * 4 5
1544 * 4 5
1548 if (!IsBridgeAbove(ti->tile)) return;
1550 TileIndex rampnorth = GetNorthernBridgeEnd(ti->tile);
1551 TileIndex rampsouth = GetSouthernBridgeEnd(ti->tile);
1552 TransportType transport_type = GetTunnelBridgeTransportType(rampsouth);
1554 Axis axis = GetBridgeAxis(ti->tile);
1555 BridgePieces piece = CalcBridgePiece(
1556 GetTunnelBridgeLength(ti->tile, rampnorth) + 1,
1557 GetTunnelBridgeLength(ti->tile, rampsouth) + 1
1560 const PalSpriteID *psid;
1561 bool drawfarpillar;
1562 if (transport_type != TRANSPORT_WATER) {
1563 BridgeType type = GetBridgeType(rampsouth);
1564 drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0);
1566 uint base_offset;
1567 if (transport_type == TRANSPORT_RAIL) {
1568 base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset;
1569 } else {
1570 base_offset = 8;
1573 psid = base_offset + GetBridgeSpriteTable(type, piece);
1574 } else {
1575 drawfarpillar = true;
1576 psid = _aqueduct_sprites;
1579 if (axis != AXIS_X) psid += 4;
1581 int x = ti->x;
1582 int y = ti->y;
1583 uint bridge_z = GetBridgePixelHeight(rampsouth);
1584 int z = bridge_z - BRIDGE_Z_START;
1586 /* Add a bounding box that separates the bridge from things below it. */
1587 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, x, y, 16, 16, 1, bridge_z - TILE_HEIGHT + BB_Z_SEPARATOR);
1589 /* Draw Trambits as SpriteCombine */
1590 if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
1592 /* Draw floor and far part of bridge*/
1593 if (!IsInvisibilitySet(TO_BRIDGES)) {
1594 if (axis == AXIS_X) {
1595 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 1, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
1596 } else {
1597 AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 1, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
1601 psid++;
1603 if (transport_type == TRANSPORT_ROAD) {
1604 /* DrawBridgeRoadBits() calls EndSpriteCombine() and StartSpriteCombine() */
1605 DrawBridgeRoadBits(rampsouth, x, y, bridge_z, axis ^ 1, false);
1606 } else if (transport_type == TRANSPORT_RAIL) {
1607 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(rampsouth));
1608 if (rti->UsesOverlay() && !IsInvisibilitySet(TO_BRIDGES)) {
1609 SpriteID surface = GetCustomRailSprite(rti, rampsouth, RTSG_BRIDGE, TCX_ON_BRIDGE);
1610 if (surface != 0) {
1611 AddSortableSpriteToDraw(surface + axis, PAL_NONE, x, y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
1615 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && !IsInvisibilitySet(TO_BRIDGES) && HasTunnelBridgeReservation(rampnorth)) {
1616 if (rti->UsesOverlay()) {
1617 SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
1618 AddSortableSpriteToDraw(overlay + RTO_X + axis, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
1619 } else {
1620 AddSortableSpriteToDraw(axis == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
1624 EndSpriteCombine();
1626 if (HasRailCatenaryDrawn(GetRailType(rampsouth))) {
1627 DrawRailCatenaryOnBridge(ti);
1631 /* draw roof, the component of the bridge which is logically between the vehicle and the camera */
1632 if (!IsInvisibilitySet(TO_BRIDGES)) {
1633 if (axis == AXIS_X) {
1634 y += 12;
1635 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 4, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 3, BRIDGE_Z_START);
1636 } else {
1637 x += 12;
1638 if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 4, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 3, 0, BRIDGE_Z_START);
1642 /* Draw TramFront as SpriteCombine */
1643 if (transport_type == TRANSPORT_ROAD) EndSpriteCombine();
1645 /* Do not draw anything more if bridges are invisible */
1646 if (IsInvisibilitySet(TO_BRIDGES)) return;
1648 psid++;
1649 DrawBridgePillars(psid, ti, axis, drawfarpillar, x, y, z);
1653 static int GetSlopePixelZ_TunnelBridge(TileIndex tile, uint x, uint y)
1655 int z;
1656 Slope tileh = GetTilePixelSlope(tile, &z);
1658 x &= 0xF;
1659 y &= 0xF;
1661 if (IsTunnel(tile)) {
1662 uint pos = (DiagDirToAxis(GetTunnelBridgeDirection(tile)) == AXIS_X ? y : x);
1664 /* In the tunnel entrance? */
1665 if (5 <= pos && pos <= 10) return z;
1666 } else { // IsBridge(tile)
1667 DiagDirection dir = GetTunnelBridgeDirection(tile);
1668 uint pos = (DiagDirToAxis(dir) == AXIS_X ? y : x);
1670 z += ApplyPixelFoundationToSlope(GetBridgeFoundation(tileh, DiagDirToAxis(dir)), &tileh);
1672 /* On the bridge ramp? */
1673 if (5 <= pos && pos <= 10) {
1674 int delta;
1676 if (tileh != SLOPE_FLAT) return z + TILE_HEIGHT;
1678 switch (dir) {
1679 default: NOT_REACHED();
1680 case DIAGDIR_NE: delta = (TILE_SIZE - 1 - x) / 2; break;
1681 case DIAGDIR_SE: delta = y / 2; break;
1682 case DIAGDIR_SW: delta = x / 2; break;
1683 case DIAGDIR_NW: delta = (TILE_SIZE - 1 - y) / 2; break;
1685 return z + 1 + delta;
1689 return z + GetPartialPixelZ(x, y, tileh);
1692 static Foundation GetFoundation_TunnelBridge(TileIndex tile, Slope tileh)
1694 return IsTunnel(tile) ? FOUNDATION_NONE : GetBridgeFoundation(tileh, DiagDirToAxis(GetTunnelBridgeDirection(tile)));
1697 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
1699 TransportType tt = GetTunnelBridgeTransportType(tile);
1701 if (IsTunnel(tile)) {
1702 td->str = (tt == TRANSPORT_RAIL) ? STR_LAI_TUNNEL_DESCRIPTION_RAILROAD : STR_LAI_TUNNEL_DESCRIPTION_ROAD;
1703 } else { // IsBridge(tile)
1704 td->str = (tt == TRANSPORT_WATER) ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : GetBridgeSpec(GetBridgeType(tile))->transport_name[tt];
1706 td->owner[0] = GetTileOwner(tile);
1708 Owner road_owner = INVALID_OWNER;
1709 Owner tram_owner = INVALID_OWNER;
1710 RoadType road_rt = GetRoadTypeRoad(tile);
1711 RoadType tram_rt = GetRoadTypeTram(tile);
1712 if (road_rt != INVALID_ROADTYPE) {
1713 const RoadTypeInfo *rti = GetRoadTypeInfo(road_rt);
1714 td->roadtype = rti->strings.name;
1715 td->road_speed = rti->max_speed / 2;
1716 road_owner = GetRoadOwner(tile, RTT_ROAD);
1718 if (tram_rt != INVALID_ROADTYPE) {
1719 const RoadTypeInfo *rti = GetRoadTypeInfo(tram_rt);
1720 td->tramtype = rti->strings.name;
1721 td->tram_speed = rti->max_speed / 2;
1722 tram_owner = GetRoadOwner(tile, RTT_TRAM);
1725 /* Is there a mix of owners? */
1726 if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
1727 (road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
1728 uint i = 1;
1729 if (road_owner != INVALID_OWNER) {
1730 td->owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
1731 td->owner[i] = road_owner;
1732 i++;
1734 if (tram_owner != INVALID_OWNER) {
1735 td->owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
1736 td->owner[i] = tram_owner;
1740 if (tt == TRANSPORT_RAIL) {
1741 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
1742 td->rail_speed = rti->max_speed;
1743 td->railtype = rti->strings.name;
1745 if (!IsTunnel(tile)) {
1746 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
1747 if (td->rail_speed == 0 || spd < td->rail_speed) {
1748 td->rail_speed = spd;
1751 } else if (tt == TRANSPORT_ROAD && !IsTunnel(tile)) {
1752 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
1753 if (road_rt != INVALID_ROADTYPE && (td->road_speed == 0 || spd < td->road_speed)) td->road_speed = spd;
1754 if (tram_rt != INVALID_ROADTYPE && (td->tram_speed == 0 || spd < td->tram_speed)) td->tram_speed = spd;
1759 static void TileLoop_TunnelBridge(TileIndex tile)
1761 bool snow_or_desert = HasTunnelBridgeSnowOrDesert(tile);
1762 switch (_settings_game.game_creation.landscape) {
1763 case LT_ARCTIC: {
1764 /* As long as we do not have a snow density, we want to use the density
1765 * from the entry edge. For tunnels this is the lowest point for bridges the highest point.
1766 * (Independent of foundations) */
1767 int z = IsBridge(tile) ? GetTileMaxZ(tile) : GetTileZ(tile);
1768 if (snow_or_desert != (z > GetSnowLine())) {
1769 SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert);
1770 MarkTileDirtyByTile(tile);
1772 break;
1775 case LT_TROPIC:
1776 if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
1777 SetTunnelBridgeSnowOrDesert(tile, true);
1778 MarkTileDirtyByTile(tile);
1780 break;
1782 default:
1783 break;
1787 static TrackStatus GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
1789 TransportType transport_type = GetTunnelBridgeTransportType(tile);
1790 if (transport_type != mode || (transport_type == TRANSPORT_ROAD && !HasTileRoadType(tile, (RoadTramType)sub_mode))) return 0;
1792 DiagDirection dir = GetTunnelBridgeDirection(tile);
1793 if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0;
1794 return CombineTrackStatus(TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)), TRACKDIR_BIT_NONE);
1797 static void ChangeTileOwner_TunnelBridge(TileIndex tile, Owner old_owner, Owner new_owner)
1799 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
1800 /* Set number of pieces to zero if it's the southern tile as we
1801 * don't want to update the infrastructure counts twice. */
1802 uint num_pieces = tile < other_end ? (GetTunnelBridgeLength(tile, other_end) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR : 0;
1804 for (RoadTramType rtt : _roadtramtypes) {
1805 /* Update all roadtypes, no matter if they are present */
1806 if (GetRoadOwner(tile, rtt) == old_owner) {
1807 RoadType rt = GetRoadType(tile, rtt);
1808 if (rt != INVALID_ROADTYPE) {
1809 /* Update company infrastructure counts. A full diagonal road tile has two road bits.
1810 * No need to dirty windows here, we'll redraw the whole screen anyway. */
1811 Company::Get(old_owner)->infrastructure.road[rt] -= num_pieces * 2;
1812 if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.road[rt] += num_pieces * 2;
1815 SetRoadOwner(tile, rtt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
1819 if (!IsTileOwner(tile, old_owner)) return;
1821 /* Update company infrastructure counts for rail and water as well.
1822 * No need to dirty windows here, we'll redraw the whole screen anyway. */
1823 TransportType tt = GetTunnelBridgeTransportType(tile);
1824 Company *old = Company::Get(old_owner);
1825 if (tt == TRANSPORT_RAIL) {
1826 old->infrastructure.rail[GetRailType(tile)] -= num_pieces;
1827 if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.rail[GetRailType(tile)] += num_pieces;
1828 } else if (tt == TRANSPORT_WATER) {
1829 old->infrastructure.water -= num_pieces;
1830 if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.water += num_pieces;
1833 if (new_owner != INVALID_OWNER) {
1834 SetTileOwner(tile, new_owner);
1835 } else {
1836 if (tt == TRANSPORT_RAIL) {
1837 /* Since all of our vehicles have been removed, it is safe to remove the rail
1838 * bridge / tunnel. */
1839 [[maybe_unused]] CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(DC_EXEC | DC_BANKRUPT, tile);
1840 assert(ret.Succeeded());
1841 } else {
1842 /* In any other case, we can safely reassign the ownership to OWNER_NONE. */
1843 SetTileOwner(tile, OWNER_NONE);
1849 * Frame when the 'enter tunnel' sound should be played. This is the second
1850 * frame on a tile, so the sound is played shortly after entering the tunnel
1851 * tile, while the vehicle is still visible.
1853 static const byte TUNNEL_SOUND_FRAME = 1;
1856 * Frame when a vehicle should be hidden in a tunnel with a certain direction.
1857 * This differs per direction, because of visibility / bounding box issues.
1858 * Note that direction, in this case, is the direction leading into the tunnel.
1859 * When entering a tunnel, hide the vehicle when it reaches the given frame.
1860 * When leaving a tunnel, show the vehicle when it is one frame further
1861 * to the 'outside', i.e. at (TILE_SIZE-1) - (frame) + 1
1863 extern const byte _tunnel_visibility_frame[DIAGDIR_END] = {12, 8, 8, 12};
1865 static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
1867 int z = GetSlopePixelZ(x, y) - v->z_pos;
1869 if (abs(z) > 2) return VETSB_CANNOT_ENTER;
1870 /* Direction into the wormhole */
1871 const DiagDirection dir = GetTunnelBridgeDirection(tile);
1872 /* Direction of the vehicle */
1873 const DiagDirection vdir = DirToDiagDir(v->direction);
1874 /* New position of the vehicle on the tile */
1875 byte pos = (DiagDirToAxis(vdir) == AXIS_X ? x : y) & TILE_UNIT_MASK;
1876 /* Number of units moved by the vehicle since entering the tile */
1877 byte frame = (vdir == DIAGDIR_NE || vdir == DIAGDIR_NW) ? TILE_SIZE - 1 - pos : pos;
1879 if (IsTunnel(tile)) {
1880 if (v->type == VEH_TRAIN) {
1881 Train *t = Train::From(v);
1883 if (t->track != TRACK_BIT_WORMHOLE && dir == vdir) {
1884 if (t->IsFrontEngine() && frame == TUNNEL_SOUND_FRAME) {
1885 if (!PlayVehicleSound(t, VSE_TUNNEL) && RailVehInfo(t->engine_type)->engclass == 0) {
1886 SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
1888 return VETSB_CONTINUE;
1890 if (frame == _tunnel_visibility_frame[dir]) {
1891 t->tile = tile;
1892 t->track = TRACK_BIT_WORMHOLE;
1893 t->vehstatus |= VS_HIDDEN;
1894 return VETSB_ENTERED_WORMHOLE;
1898 if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
1899 /* We're at the tunnel exit ?? */
1900 t->tile = tile;
1901 t->track = DiagDirToDiagTrackBits(vdir);
1902 assert(t->track);
1903 t->vehstatus &= ~VS_HIDDEN;
1904 return VETSB_ENTERED_WORMHOLE;
1906 } else if (v->type == VEH_ROAD) {
1907 RoadVehicle *rv = RoadVehicle::From(v);
1909 /* Enter tunnel? */
1910 if (rv->state != RVSB_WORMHOLE && dir == vdir) {
1911 if (frame == _tunnel_visibility_frame[dir]) {
1912 /* Frame should be equal to the next frame number in the RV's movement */
1913 assert(frame == rv->frame + 1);
1914 rv->tile = tile;
1915 rv->state = RVSB_WORMHOLE;
1916 rv->vehstatus |= VS_HIDDEN;
1917 return VETSB_ENTERED_WORMHOLE;
1918 } else {
1919 return VETSB_CONTINUE;
1923 /* We're at the tunnel exit ?? */
1924 if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
1925 rv->tile = tile;
1926 rv->state = DiagDirToDiagTrackdir(vdir);
1927 rv->frame = frame;
1928 rv->vehstatus &= ~VS_HIDDEN;
1929 return VETSB_ENTERED_WORMHOLE;
1932 } else { // IsBridge(tile)
1933 if (v->type != VEH_SHIP) {
1934 /* modify speed of vehicle */
1935 uint16 spd = GetBridgeSpec(GetBridgeType(tile))->speed;
1937 if (v->type == VEH_ROAD) spd *= 2;
1938 Vehicle *first = v->First();
1939 first->cur_speed = std::min(first->cur_speed, spd);
1942 if (vdir == dir) {
1943 /* Vehicle enters bridge at the last frame inside this tile. */
1944 if (frame != TILE_SIZE - 1) return VETSB_CONTINUE;
1945 switch (v->type) {
1946 case VEH_TRAIN: {
1947 Train *t = Train::From(v);
1948 t->track = TRACK_BIT_WORMHOLE;
1949 ClrBit(t->gv_flags, GVF_GOINGUP_BIT);
1950 ClrBit(t->gv_flags, GVF_GOINGDOWN_BIT);
1951 break;
1954 case VEH_ROAD: {
1955 RoadVehicle *rv = RoadVehicle::From(v);
1956 rv->state = RVSB_WORMHOLE;
1957 /* There are no slopes inside bridges / tunnels. */
1958 ClrBit(rv->gv_flags, GVF_GOINGUP_BIT);
1959 ClrBit(rv->gv_flags, GVF_GOINGDOWN_BIT);
1960 break;
1963 case VEH_SHIP:
1964 Ship::From(v)->state = TRACK_BIT_WORMHOLE;
1965 break;
1967 default: NOT_REACHED();
1969 return VETSB_ENTERED_WORMHOLE;
1970 } else if (vdir == ReverseDiagDir(dir)) {
1971 v->tile = tile;
1972 switch (v->type) {
1973 case VEH_TRAIN: {
1974 Train *t = Train::From(v);
1975 if (t->track == TRACK_BIT_WORMHOLE) {
1976 t->track = DiagDirToDiagTrackBits(vdir);
1977 return VETSB_ENTERED_WORMHOLE;
1979 break;
1982 case VEH_ROAD: {
1983 RoadVehicle *rv = RoadVehicle::From(v);
1984 if (rv->state == RVSB_WORMHOLE) {
1985 rv->state = DiagDirToDiagTrackdir(vdir);
1986 rv->frame = 0;
1987 return VETSB_ENTERED_WORMHOLE;
1989 break;
1992 case VEH_SHIP: {
1993 Ship *ship = Ship::From(v);
1994 if (ship->state == TRACK_BIT_WORMHOLE) {
1995 ship->state = DiagDirToDiagTrackBits(vdir);
1996 return VETSB_ENTERED_WORMHOLE;
1998 break;
2001 default: NOT_REACHED();
2005 return VETSB_CONTINUE;
2008 static CommandCost TerraformTile_TunnelBridge(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
2010 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled() && IsBridge(tile) && GetTunnelBridgeTransportType(tile) != TRANSPORT_WATER) {
2011 DiagDirection direction = GetTunnelBridgeDirection(tile);
2012 Axis axis = DiagDirToAxis(direction);
2013 CommandCost res;
2014 int z_old;
2015 Slope tileh_old = GetTileSlope(tile, &z_old);
2017 /* Check if new slope is valid for bridges in general (so we can safely call GetBridgeFoundation()) */
2018 if ((direction == DIAGDIR_NW) || (direction == DIAGDIR_NE)) {
2019 CheckBridgeSlope(BRIDGE_PIECE_SOUTH, axis, &tileh_old, &z_old);
2020 res = CheckBridgeSlope(BRIDGE_PIECE_SOUTH, axis, &tileh_new, &z_new);
2021 } else {
2022 CheckBridgeSlope(BRIDGE_PIECE_NORTH, axis, &tileh_old, &z_old);
2023 res = CheckBridgeSlope(BRIDGE_PIECE_NORTH, axis, &tileh_new, &z_new);
2026 /* Surface slope is valid and remains unchanged? */
2027 if (res.Succeeded() && (z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
2030 return Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile);
2033 extern const TileTypeProcs _tile_type_tunnelbridge_procs = {
2034 DrawTile_TunnelBridge, // draw_tile_proc
2035 GetSlopePixelZ_TunnelBridge, // get_slope_z_proc
2036 ClearTile_TunnelBridge, // clear_tile_proc
2037 nullptr, // add_accepted_cargo_proc
2038 GetTileDesc_TunnelBridge, // get_tile_desc_proc
2039 GetTileTrackStatus_TunnelBridge, // get_tile_track_status_proc
2040 nullptr, // click_tile_proc
2041 nullptr, // animate_tile_proc
2042 TileLoop_TunnelBridge, // tile_loop_proc
2043 ChangeTileOwner_TunnelBridge, // change_tile_owner_proc
2044 nullptr, // add_produced_cargo_proc
2045 VehicleEnter_TunnelBridge, // vehicle_enter_tile_proc
2046 GetFoundation_TunnelBridge, // get_foundation_proc
2047 TerraformTile_TunnelBridge, // terraform_tile_proc