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/>.
9 * @file tunnelbridge_cmd.cpp
10 * This file deals with tunnels and bridges (non-gui stuff)
11 * @todo separate this file into two
15 #include "newgrf_object.h"
16 #include "viewport_func.h"
17 #include "command_func.h"
22 #include "pathfinder/yapf/yapf_cache.h"
23 #include "pathfinder/water_regions.h"
24 #include "newgrf_sound.h"
25 #include "autoslope.h"
26 #include "tunnelbridge_map.h"
27 #include "strings_func.h"
28 #include "timer/timer_game_calendar.h"
29 #include "clear_func.h"
30 #include "vehicle_func.h"
31 #include "sound_func.h"
32 #include "tunnelbridge.h"
33 #include "cheat_type.h"
34 #include "elrail_func.h"
36 #include "company_base.h"
37 #include "newgrf_railtype.h"
38 #include "newgrf_roadtype.h"
39 #include "object_base.h"
41 #include "company_gui.h"
42 #include "station_func.h"
43 #include "tunnelbridge_cmd.h"
44 #include "landscape_cmd.h"
45 #include "terraform_cmd.h"
47 #include "table/strings.h"
48 #include "table/bridge_land.h"
50 #include "safeguards.h"
52 BridgeSpec _bridge
[MAX_BRIDGES
]; ///< The specification of all bridges.
53 TileIndex _build_tunnel_endtile
; ///< The end of a tunnel; as hidden return from the tunnel build command for GUI purposes.
55 /** Z position of the bridge sprites relative to bridge height (downwards) */
56 static const int BRIDGE_Z_START
= 3;
60 * Mark bridge tiles dirty.
61 * Note: The bridge does not need to exist, everything is passed via parameters.
62 * @param begin Start tile.
63 * @param end End tile.
64 * @param direction Direction from \a begin to \a end.
65 * @param bridge_height Bridge height level.
67 void MarkBridgeDirty(TileIndex begin
, TileIndex end
, DiagDirection direction
, uint bridge_height
)
69 TileIndexDiff delta
= TileOffsByDiagDir(direction
);
70 for (TileIndex t
= begin
; t
!= end
; t
+= delta
) {
71 MarkTileDirtyByTile(t
, bridge_height
- TileHeight(t
));
73 MarkTileDirtyByTile(end
);
77 * Mark bridge tiles dirty.
78 * @param tile Bridge head.
80 void MarkBridgeDirty(TileIndex tile
)
82 MarkBridgeDirty(tile
, GetOtherTunnelBridgeEnd(tile
), GetTunnelBridgeDirection(tile
), GetBridgeHeight(tile
));
85 /** Reset the data been eventually changed by the grf loaded. */
88 /* First, free sprite table data */
89 for (BridgeType i
= 0; i
< MAX_BRIDGES
; i
++) {
90 if (_bridge
[i
].sprite_table
!= nullptr) {
91 for (BridgePieces j
= BRIDGE_PIECE_NORTH
; j
< BRIDGE_PIECE_INVALID
; j
++) free(_bridge
[i
].sprite_table
[j
]);
92 free(_bridge
[i
].sprite_table
);
96 /* Then, wipe out current bridges */
97 memset(&_bridge
, 0, sizeof(_bridge
));
98 /* And finally, reinstall default data */
99 memcpy(&_bridge
, &_orig_bridge
, sizeof(_orig_bridge
));
103 * Calculate the price factor for building a long bridge.
104 * 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,
105 * @param length Length of the bridge.
106 * @return Price factor for the bridge.
108 int CalcBridgeLenCostFactor(int length
)
110 if (length
< 2) return length
;
114 for (int delta
= 1;; delta
++) {
115 for (int count
= 0; count
< delta
; count
++) {
116 if (length
== 0) return sum
;
124 * Get the foundation for a bridge.
125 * @param tileh The slope to build the bridge on.
126 * @param axis The axis of the bridge entrance.
127 * @return The foundation required.
129 Foundation
GetBridgeFoundation(Slope tileh
, Axis axis
)
131 if (tileh
== SLOPE_FLAT
||
132 ((tileh
== SLOPE_NE
|| tileh
== SLOPE_SW
) && axis
== AXIS_X
) ||
133 ((tileh
== SLOPE_NW
|| tileh
== SLOPE_SE
) && axis
== AXIS_Y
)) return FOUNDATION_NONE
;
135 return (HasSlopeHighestCorner(tileh
) ? InclinedFoundation(axis
) : FlatteningFoundation(tileh
));
139 * Determines if the track on a bridge ramp is flat or goes up/down.
141 * @param tileh Slope of the tile under the bridge head
142 * @param axis Orientation of bridge
143 * @return true iff the track is flat.
145 bool HasBridgeFlatRamp(Slope tileh
, Axis axis
)
147 ApplyFoundationToSlope(GetBridgeFoundation(tileh
, axis
), &tileh
);
148 /* If the foundation slope is flat the bridge has a non-flat ramp and vice versa. */
149 return (tileh
!= SLOPE_FLAT
);
152 static inline const PalSpriteID
*GetBridgeSpriteTable(int index
, BridgePieces table
)
154 const BridgeSpec
*bridge
= GetBridgeSpec(index
);
155 assert(table
< BRIDGE_PIECE_INVALID
);
156 if (bridge
->sprite_table
== nullptr || bridge
->sprite_table
[table
] == nullptr) {
157 return _bridge_sprite_table
[index
][table
];
159 return bridge
->sprite_table
[table
];
165 * Determines the foundation for the bridge head, and tests if the resulting slope is valid.
167 * @param bridge_piece Direction of the bridge head.
168 * @param axis Axis of the bridge
169 * @param tileh Slope of the tile under the north bridge head; returns slope on top of foundation
170 * @param z TileZ corresponding to tileh, gets modified as well
171 * @return Error or cost for bridge foundation
173 static CommandCost
CheckBridgeSlope(BridgePieces bridge_piece
, Axis axis
, Slope
*tileh
, int *z
)
175 assert(bridge_piece
== BRIDGE_PIECE_NORTH
|| bridge_piece
== BRIDGE_PIECE_SOUTH
);
177 Foundation f
= GetBridgeFoundation(*tileh
, axis
);
178 *z
+= ApplyFoundationToSlope(f
, tileh
);
180 Slope valid_inclined
;
181 if (bridge_piece
== BRIDGE_PIECE_NORTH
) {
182 valid_inclined
= (axis
== AXIS_X
? SLOPE_NE
: SLOPE_NW
);
184 valid_inclined
= (axis
== AXIS_X
? SLOPE_SW
: SLOPE_SE
);
186 if ((*tileh
!= SLOPE_FLAT
) && (*tileh
!= valid_inclined
)) return CMD_ERROR
;
188 if (f
== FOUNDATION_NONE
) return CommandCost();
190 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
194 * Is a bridge of the specified type and length available?
195 * @param bridge_type Wanted type of bridge.
196 * @param bridge_len Wanted length of the bridge.
197 * @param flags Type of operation.
198 * @return A succeeded (the requested bridge is available) or failed (it cannot be built) command.
200 CommandCost
CheckBridgeAvailability(BridgeType bridge_type
, uint bridge_len
, DoCommandFlag flags
)
202 if (flags
& DC_QUERY_COST
) {
203 if (bridge_len
<= _settings_game
.construction
.max_bridge_length
) return CommandCost();
204 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG
);
207 if (bridge_type
>= MAX_BRIDGES
) return CMD_ERROR
;
209 const BridgeSpec
*b
= GetBridgeSpec(bridge_type
);
210 if (b
->avail_year
> TimerGameCalendar::year
) return CMD_ERROR
;
212 uint max
= std::min(b
->max_length
, _settings_game
.construction
.max_bridge_length
);
214 if (b
->min_length
> bridge_len
) return CMD_ERROR
;
215 if (bridge_len
<= max
) return CommandCost();
216 return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG
);
220 * Calculate the base cost of clearing a tunnel/bridge per tile.
221 * @param tile Start tile of the tunnel/bridge.
222 * @return How much clearing this tunnel/bridge costs per tile.
224 static Money
TunnelBridgeClearCost(TileIndex tile
, Price base_price
)
226 Money base_cost
= _price
[base_price
];
228 /* Add the cost of the transport that is on the tunnel/bridge. */
229 switch (GetTunnelBridgeTransportType(tile
)) {
230 case TRANSPORT_ROAD
: {
231 RoadType road_rt
= GetRoadTypeRoad(tile
);
232 RoadType tram_rt
= GetRoadTypeTram(tile
);
234 if (road_rt
!= INVALID_ROADTYPE
) {
235 base_cost
+= 2 * RoadClearCost(road_rt
);
237 if (tram_rt
!= INVALID_ROADTYPE
) {
238 base_cost
+= 2 * RoadClearCost(tram_rt
);
242 case TRANSPORT_RAIL
: base_cost
+= RailClearCost(GetRailType(tile
)); break;
243 /* Aquaducts have their own clear price. */
244 case TRANSPORT_WATER
: base_cost
= _price
[PR_CLEAR_AQUEDUCT
]; break;
253 * @param flags type of operation
254 * @param tile_end end tile
255 * @param tile_start start tile
256 * @param transport_type transport type.
257 * @param bridge_type bridge type (hi bh)
258 * @param road_rail_type rail type or road types.
259 * @return the cost of this operation or an error
261 CommandCost
CmdBuildBridge(DoCommandFlag flags
, TileIndex tile_end
, TileIndex tile_start
, TransportType transport_type
, BridgeType bridge_type
, byte road_rail_type
)
263 CompanyID company
= _current_company
;
265 RailType railtype
= INVALID_RAILTYPE
;
266 RoadType roadtype
= INVALID_ROADTYPE
;
268 if (!IsValidTile(tile_start
)) return_cmd_error(STR_ERROR_BRIDGE_THROUGH_MAP_BORDER
);
271 switch (transport_type
) {
273 roadtype
= (RoadType
)road_rail_type
;
274 if (!ValParamRoadType(roadtype
)) return CMD_ERROR
;
278 railtype
= (RailType
)road_rail_type
;
279 if (!ValParamRailType(railtype
)) return CMD_ERROR
;
282 case TRANSPORT_WATER
:
286 /* Airports don't have bridges. */
290 if (company
== OWNER_DEITY
) {
291 if (transport_type
!= TRANSPORT_ROAD
) return CMD_ERROR
;
292 const Town
*town
= CalcClosestTownFromTile(tile_start
);
294 company
= OWNER_TOWN
;
296 /* If we are not within a town, we are not owned by the town */
297 if (town
== nullptr || DistanceSquare(tile_start
, town
->xy
) > town
->cache
.squared_town_zone_radius
[HZB_TOWN_EDGE
]) {
298 company
= OWNER_NONE
;
302 if (tile_start
== tile_end
) {
303 return_cmd_error(STR_ERROR_CAN_T_START_AND_END_ON
);
307 if (TileX(tile_start
) == TileX(tile_end
)) {
309 } else if (TileY(tile_start
) == TileY(tile_end
)) {
312 return_cmd_error(STR_ERROR_START_AND_END_MUST_BE_IN
);
315 if (tile_end
< tile_start
) Swap(tile_start
, tile_end
);
317 uint bridge_len
= GetTunnelBridgeLength(tile_start
, tile_end
);
318 if (transport_type
!= TRANSPORT_WATER
) {
319 /* set and test bridge length, availability */
320 CommandCost ret
= CheckBridgeAvailability(bridge_type
, bridge_len
, flags
);
321 if (ret
.Failed()) return ret
;
323 if (bridge_len
> _settings_game
.construction
.max_bridge_length
) return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG
);
325 bridge_len
+= 2; // begin and end tiles/ramps
329 Slope tileh_start
= GetTileSlope(tile_start
, &z_start
);
330 Slope tileh_end
= GetTileSlope(tile_end
, &z_end
);
331 bool pbs_reservation
= false;
333 CommandCost terraform_cost_north
= CheckBridgeSlope(BRIDGE_PIECE_NORTH
, direction
, &tileh_start
, &z_start
);
334 CommandCost terraform_cost_south
= CheckBridgeSlope(BRIDGE_PIECE_SOUTH
, direction
, &tileh_end
, &z_end
);
336 /* Aqueducts can't be built of flat land. */
337 if (transport_type
== TRANSPORT_WATER
&& (tileh_start
== SLOPE_FLAT
|| tileh_end
== SLOPE_FLAT
)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
338 if (z_start
!= z_end
) return_cmd_error(STR_ERROR_BRIDGEHEADS_NOT_SAME_HEIGHT
);
340 CommandCost
cost(EXPENSES_CONSTRUCTION
);
343 RoadType road_rt
= INVALID_ROADTYPE
;
344 RoadType tram_rt
= INVALID_ROADTYPE
;
345 if (IsBridgeTile(tile_start
) && IsBridgeTile(tile_end
) &&
346 GetOtherBridgeEnd(tile_start
) == tile_end
&&
347 GetTunnelBridgeTransportType(tile_start
) == transport_type
) {
348 /* Replace a current bridge. */
350 switch (transport_type
) {
352 /* Keep the reservation, the path stays valid. */
353 pbs_reservation
= HasTunnelBridgeReservation(tile_start
);
357 /* Do not remove road types when upgrading a bridge */
358 road_rt
= GetRoadTypeRoad(tile_start
);
359 tram_rt
= GetRoadTypeTram(tile_start
);
365 /* If this is a railway bridge, make sure the railtypes match. */
366 if (transport_type
== TRANSPORT_RAIL
&& GetRailType(tile_start
) != railtype
) {
367 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
370 /* If this is a road bridge, make sure the roadtype matches. */
371 if (transport_type
== TRANSPORT_ROAD
) {
372 RoadType existing_rt
= RoadTypeIsRoad(roadtype
) ? road_rt
: tram_rt
;
373 if (existing_rt
!= roadtype
&& existing_rt
!= INVALID_ROADTYPE
) {
374 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
378 /* Do not replace town bridges with lower speed bridges, unless in scenario editor. */
379 if (!(flags
& DC_QUERY_COST
) && IsTileOwner(tile_start
, OWNER_TOWN
) &&
380 GetBridgeSpec(bridge_type
)->speed
< GetBridgeSpec(GetBridgeType(tile_start
))->speed
&&
381 _game_mode
!= GM_EDITOR
) {
382 Town
*t
= ClosestTownFromTile(tile_start
, UINT_MAX
);
387 SetDParam(0, t
->index
);
388 return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS
);
392 /* Do not replace the bridge with the same bridge type. */
393 if (!(flags
& DC_QUERY_COST
) && (bridge_type
== GetBridgeType(tile_start
)) && (transport_type
!= TRANSPORT_ROAD
|| road_rt
== roadtype
|| tram_rt
== roadtype
)) {
394 return_cmd_error(STR_ERROR_ALREADY_BUILT
);
397 /* Do not allow replacing another company's bridges. */
398 if (!IsTileOwner(tile_start
, company
) && !IsTileOwner(tile_start
, OWNER_TOWN
) && !IsTileOwner(tile_start
, OWNER_NONE
)) {
399 return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER
);
402 /* The cost of clearing the current bridge. */
403 cost
.AddCost(bridge_len
* TunnelBridgeClearCost(tile_start
, PR_CLEAR_BRIDGE
));
404 owner
= GetTileOwner(tile_start
);
406 /* If bridge belonged to bankrupt company, it has a new owner now */
407 is_new_owner
= (owner
== OWNER_NONE
);
408 if (is_new_owner
) owner
= company
;
410 /* Build a new bridge. */
412 bool allow_on_slopes
= (_settings_game
.construction
.build_on_slopes
&& transport_type
!= TRANSPORT_WATER
);
414 /* Try and clear the start landscape */
415 CommandCost ret
= Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile_start
);
416 if (ret
.Failed()) return ret
;
419 if (terraform_cost_north
.Failed() || (terraform_cost_north
.GetCost() != 0 && !allow_on_slopes
)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
420 cost
.AddCost(terraform_cost_north
);
422 /* Try and clear the end landscape */
423 ret
= Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile_end
);
424 if (ret
.Failed()) return ret
;
427 /* false - end tile slope check */
428 if (terraform_cost_south
.Failed() || (terraform_cost_south
.GetCost() != 0 && !allow_on_slopes
)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
429 cost
.AddCost(terraform_cost_south
);
431 const TileIndex heads
[] = {tile_start
, tile_end
};
432 for (int i
= 0; i
< 2; i
++) {
433 if (IsBridgeAbove(heads
[i
])) {
434 TileIndex north_head
= GetNorthernBridgeEnd(heads
[i
]);
436 if (direction
== GetBridgeAxis(heads
[i
])) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
438 if (z_start
+ 1 == GetBridgeHeight(north_head
)) {
439 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
444 TileIndexDiff delta
= (direction
== AXIS_X
? TileDiffXY(1, 0) : TileDiffXY(0, 1));
445 for (TileIndex tile
= tile_start
+ delta
; tile
!= tile_end
; tile
+= delta
) {
446 if (GetTileMaxZ(tile
) > z_start
) return_cmd_error(STR_ERROR_BRIDGE_TOO_LOW_FOR_TERRAIN
);
448 if (z_start
>= (GetTileZ(tile
) + _settings_game
.construction
.max_bridge_height
)) {
450 * Disallow too high bridges.
451 * Properly rendering a map where very high bridges (might) exist is expensive.
452 * See http://www.tt-forums.net/viewtopic.php?f=33&t=40844&start=980#p1131762
453 * for a detailed discussion. z_start here is one heightlevel below the bridge level.
455 return_cmd_error(STR_ERROR_BRIDGE_TOO_HIGH_FOR_TERRAIN
);
458 if (IsBridgeAbove(tile
)) {
459 /* Disallow crossing bridges for the time being */
460 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
463 switch (GetTileType(tile
)) {
465 if (!IsWater(tile
) && !IsCoast(tile
)) goto not_valid_below
;
469 if (!IsPlainRail(tile
)) goto not_valid_below
;
473 if (IsRoadDepot(tile
)) goto not_valid_below
;
476 case MP_TUNNELBRIDGE
:
477 if (IsTunnel(tile
)) break;
478 if (direction
== DiagDirToAxis(GetTunnelBridgeDirection(tile
))) goto not_valid_below
;
479 if (z_start
< GetBridgeHeight(tile
)) goto not_valid_below
;
483 const ObjectSpec
*spec
= ObjectSpec::GetByTile(tile
);
484 if ((spec
->flags
& OBJECT_FLAG_ALLOW_UNDER_BRIDGE
) == 0) goto not_valid_below
;
485 if (GetTileMaxZ(tile
) + spec
->height
> z_start
) goto not_valid_below
;
494 /* try and clear the middle landscape */
495 ret
= Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
496 if (ret
.Failed()) return ret
;
501 if (flags
& DC_EXEC
) {
502 /* We do this here because when replacing a bridge with another
503 * type calling SetBridgeMiddle isn't needed. After all, the
504 * tile already has the has_bridge_above bits set. */
505 SetBridgeMiddle(tile
, direction
);
513 bool hasroad
= road_rt
!= INVALID_ROADTYPE
;
514 bool hastram
= tram_rt
!= INVALID_ROADTYPE
;
515 if (transport_type
== TRANSPORT_ROAD
) {
516 if (RoadTypeIsRoad(roadtype
)) road_rt
= roadtype
;
517 if (RoadTypeIsTram(roadtype
)) tram_rt
= roadtype
;
521 if (flags
& DC_EXEC
) {
522 DiagDirection dir
= AxisToDiagDir(direction
);
524 Company
*c
= Company::GetIfValid(company
);
525 switch (transport_type
) {
527 /* Add to company infrastructure count if required. */
528 if (is_new_owner
&& c
!= nullptr) c
->infrastructure
.rail
[railtype
] += bridge_len
* TUNNELBRIDGE_TRACKBIT_FACTOR
;
529 MakeRailBridgeRamp(tile_start
, owner
, bridge_type
, dir
, railtype
);
530 MakeRailBridgeRamp(tile_end
, owner
, bridge_type
, ReverseDiagDir(dir
), railtype
);
531 SetTunnelBridgeReservation(tile_start
, pbs_reservation
);
532 SetTunnelBridgeReservation(tile_end
, pbs_reservation
);
535 case TRANSPORT_ROAD
: {
537 /* Also give unowned present roadtypes to new owner */
538 if (hasroad
&& GetRoadOwner(tile_start
, RTT_ROAD
) == OWNER_NONE
) hasroad
= false;
539 if (hastram
&& GetRoadOwner(tile_start
, RTT_TRAM
) == OWNER_NONE
) hastram
= false;
542 /* Add all new road types to the company infrastructure counter. */
543 if (!hasroad
&& road_rt
!= INVALID_ROADTYPE
) {
544 /* A full diagonal road tile has two road bits. */
545 c
->infrastructure
.road
[road_rt
] += bridge_len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
;
547 if (!hastram
&& tram_rt
!= INVALID_ROADTYPE
) {
548 /* A full diagonal road tile has two road bits. */
549 c
->infrastructure
.road
[tram_rt
] += bridge_len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
;
552 Owner owner_road
= hasroad
? GetRoadOwner(tile_start
, RTT_ROAD
) : company
;
553 Owner owner_tram
= hastram
? GetRoadOwner(tile_start
, RTT_TRAM
) : company
;
554 MakeRoadBridgeRamp(tile_start
, owner
, owner_road
, owner_tram
, bridge_type
, dir
, road_rt
, tram_rt
);
555 MakeRoadBridgeRamp(tile_end
, owner
, owner_road
, owner_tram
, bridge_type
, ReverseDiagDir(dir
), road_rt
, tram_rt
);
559 case TRANSPORT_WATER
:
560 if (is_new_owner
&& c
!= nullptr) c
->infrastructure
.water
+= bridge_len
* TUNNELBRIDGE_TRACKBIT_FACTOR
;
561 MakeAqueductBridgeRamp(tile_start
, owner
, dir
);
562 MakeAqueductBridgeRamp(tile_end
, owner
, ReverseDiagDir(dir
));
563 CheckForDockingTile(tile_start
);
564 CheckForDockingTile(tile_end
);
571 /* Mark all tiles dirty */
572 MarkBridgeDirty(tile_start
, tile_end
, AxisToDiagDir(direction
), z_start
);
573 DirtyCompanyInfrastructureWindows(company
);
576 if ((flags
& DC_EXEC
) && transport_type
== TRANSPORT_RAIL
) {
577 Track track
= AxisToTrack(direction
);
578 AddSideToSignalBuffer(tile_start
, INVALID_DIAGDIR
, company
);
579 YapfNotifyTrackLayoutChange(tile_start
, track
);
582 /* Human players that build bridges get a selection to choose from (DC_QUERY_COST)
583 * It's unnecessary to execute this command every time for every bridge.
584 * So it is done only for humans and cost is computed in bridge_gui.cpp.
585 * For (non-spectated) AI, Towns this has to be of course calculated. */
586 Company
*c
= Company::GetIfValid(company
);
587 if (!(flags
& DC_QUERY_COST
) || (c
!= nullptr && c
->is_ai
&& company
!= _local_company
)) {
588 switch (transport_type
) {
590 if (road_rt
!= INVALID_ROADTYPE
) {
591 cost
.AddCost(bridge_len
* 2 * RoadBuildCost(road_rt
));
593 if (tram_rt
!= INVALID_ROADTYPE
) {
594 cost
.AddCost(bridge_len
* 2 * RoadBuildCost(tram_rt
));
598 case TRANSPORT_RAIL
: cost
.AddCost(bridge_len
* RailBuildCost(railtype
)); break;
602 if (c
!= nullptr) bridge_len
= CalcBridgeLenCostFactor(bridge_len
);
604 if (transport_type
!= TRANSPORT_WATER
) {
605 cost
.AddCost((int64_t)bridge_len
* _price
[PR_BUILD_BRIDGE
] * GetBridgeSpec(bridge_type
)->price
>> 8);
607 /* Aqueducts use a separate base cost. */
608 cost
.AddCost((int64_t)bridge_len
* _price
[PR_BUILD_AQUEDUCT
]);
619 * @param flags type of operation
620 * @param start_tile start tile of tunnel
621 * @param transport_type transport type
622 * @param road_rail_type railtype or roadtype
623 * @return the cost of this operation or an error
625 CommandCost
CmdBuildTunnel(DoCommandFlag flags
, TileIndex start_tile
, TransportType transport_type
, byte road_rail_type
)
627 CompanyID company
= _current_company
;
629 RailType railtype
= INVALID_RAILTYPE
;
630 RoadType roadtype
= INVALID_ROADTYPE
;
631 _build_tunnel_endtile
= 0;
632 switch (transport_type
) {
634 railtype
= (RailType
)road_rail_type
;
635 if (!ValParamRailType(railtype
)) return CMD_ERROR
;
639 roadtype
= (RoadType
)road_rail_type
;
640 if (!ValParamRoadType(roadtype
)) return CMD_ERROR
;
643 default: return CMD_ERROR
;
646 if (company
== OWNER_DEITY
) {
647 if (transport_type
!= TRANSPORT_ROAD
) return CMD_ERROR
;
648 const Town
*town
= CalcClosestTownFromTile(start_tile
);
650 company
= OWNER_TOWN
;
652 /* If we are not within a town, we are not owned by the town */
653 if (town
== nullptr || DistanceSquare(start_tile
, town
->xy
) > town
->cache
.squared_town_zone_radius
[HZB_TOWN_EDGE
]) {
654 company
= OWNER_NONE
;
660 Slope start_tileh
= GetTileSlope(start_tile
, &start_z
);
661 DiagDirection direction
= GetInclinedSlopeDirection(start_tileh
);
662 if (direction
== INVALID_DIAGDIR
) return_cmd_error(STR_ERROR_SITE_UNSUITABLE_FOR_TUNNEL
);
664 if (HasTileWaterGround(start_tile
)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER
);
666 CommandCost ret
= Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, start_tile
);
667 if (ret
.Failed()) return ret
;
669 /* XXX - do NOT change 'ret' in the loop, as it is used as the price
670 * for the clearing of the entrance of the tunnel. Assigning it to
671 * cost before the loop will yield different costs depending on start-
672 * position, because of increased-cost-by-length: 'cost += cost >> 3' */
674 TileIndexDiff delta
= TileOffsByDiagDir(direction
);
675 DiagDirection tunnel_in_way_dir
;
676 if (DiagDirToAxis(direction
) == AXIS_Y
) {
677 tunnel_in_way_dir
= (TileX(start_tile
) < (Map::MaxX() / 2)) ? DIAGDIR_SW
: DIAGDIR_NE
;
679 tunnel_in_way_dir
= (TileY(start_tile
) < (Map::MaxX() / 2)) ? DIAGDIR_SE
: DIAGDIR_NW
;
682 TileIndex end_tile
= start_tile
;
684 /* Tile shift coefficient. Will decrease for very long tunnels to avoid exponential growth of price*/
686 /* Number of tiles from start of tunnel */
688 /* Number of tiles at which the cost increase coefficient per tile is halved */
691 CommandCost
cost(EXPENSES_CONSTRUCTION
);
695 if (!IsValidTile(end_tile
)) return_cmd_error(STR_ERROR_TUNNEL_THROUGH_MAP_BORDER
);
696 end_tileh
= GetTileSlope(end_tile
, &end_z
);
698 if (start_z
== end_z
) break;
700 if (!_cheats
.crossing_tunnels
.value
&& IsTunnelInWayDir(end_tile
, start_z
, tunnel_in_way_dir
)) {
701 return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY
);
705 if (tiles
== tiles_bump
) {
710 cost
.AddCost(_price
[PR_BUILD_TUNNEL
]);
711 cost
.AddCost(cost
.GetCost() >> tiles_coef
); // add a multiplier for longer tunnels
714 /* Add the cost of the entrance */
715 cost
.AddCost(_price
[PR_BUILD_TUNNEL
]);
718 /* if the command fails from here on we want the end tile to be highlighted */
719 _build_tunnel_endtile
= end_tile
;
721 if (tiles
> _settings_game
.construction
.max_tunnel_length
) return_cmd_error(STR_ERROR_TUNNEL_TOO_LONG
);
723 if (HasTileWaterGround(end_tile
)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER
);
725 /* Clear the tile in any case */
726 ret
= Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, end_tile
);
727 if (ret
.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND
);
730 /* slope of end tile must be complementary to the slope of the start tile */
731 if (end_tileh
!= ComplementSlope(start_tileh
)) {
732 /* Mark the tile as already cleared for the terraform command.
733 * Do this for all tiles (like trees), not only objects. */
734 ClearedObjectArea
*coa
= FindClearedObject(end_tile
);
735 if (coa
== nullptr) {
736 coa
= &_cleared_object_areas
.emplace_back(ClearedObjectArea
{ end_tile
, TileArea(end_tile
, 1, 1) });
739 /* Hide the tile from the terraforming command */
740 TileIndex old_first_tile
= coa
->first_tile
;
741 coa
->first_tile
= INVALID_TILE
;
743 /* CMD_TERRAFORM_LAND may append further items to _cleared_object_areas,
744 * however it will never erase or re-order existing items.
745 * _cleared_object_areas is a value-type self-resizing vector, therefore appending items
746 * may result in a backing-store re-allocation, which would invalidate the coa pointer.
747 * The index of the coa pointer into the _cleared_object_areas vector remains valid,
748 * and can be used safely after the CMD_TERRAFORM_LAND operation.
749 * Deliberately clear the coa pointer to avoid leaving dangling pointers which could
750 * inadvertently be dereferenced.
752 ClearedObjectArea
*begin
= _cleared_object_areas
.data();
753 assert(coa
>= begin
&& coa
< begin
+ _cleared_object_areas
.size());
754 size_t coa_index
= coa
- begin
;
755 assert(coa_index
< UINT_MAX
); // more than 2**32 cleared areas would be a bug in itself
758 ret
= std::get
<0>(Command
<CMD_TERRAFORM_LAND
>::Do(flags
, end_tile
, end_tileh
& start_tileh
, false));
759 _cleared_object_areas
[(uint
)coa_index
].first_tile
= old_first_tile
;
760 if (ret
.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND
);
763 cost
.AddCost(_price
[PR_BUILD_TUNNEL
]);
765 /* Pay for the rail/road in the tunnel including entrances */
766 switch (transport_type
) {
767 case TRANSPORT_ROAD
: cost
.AddCost((tiles
+ 2) * RoadBuildCost(roadtype
) * 2); break;
768 case TRANSPORT_RAIL
: cost
.AddCost((tiles
+ 2) * RailBuildCost(railtype
)); break;
769 default: NOT_REACHED();
772 if (flags
& DC_EXEC
) {
773 Company
*c
= Company::GetIfValid(company
);
774 uint num_pieces
= (tiles
+ 2) * TUNNELBRIDGE_TRACKBIT_FACTOR
;
775 if (transport_type
== TRANSPORT_RAIL
) {
776 if (c
!= nullptr) c
->infrastructure
.rail
[railtype
] += num_pieces
;
777 MakeRailTunnel(start_tile
, company
, direction
, railtype
);
778 MakeRailTunnel(end_tile
, company
, ReverseDiagDir(direction
), railtype
);
779 AddSideToSignalBuffer(start_tile
, INVALID_DIAGDIR
, company
);
780 YapfNotifyTrackLayoutChange(start_tile
, DiagDirToDiagTrack(direction
));
782 if (c
!= nullptr) c
->infrastructure
.road
[roadtype
] += num_pieces
* 2; // A full diagonal road has two road bits.
783 RoadType road_rt
= RoadTypeIsRoad(roadtype
) ? roadtype
: INVALID_ROADTYPE
;
784 RoadType tram_rt
= RoadTypeIsTram(roadtype
) ? roadtype
: INVALID_ROADTYPE
;
785 MakeRoadTunnel(start_tile
, company
, direction
, road_rt
, tram_rt
);
786 MakeRoadTunnel(end_tile
, company
, ReverseDiagDir(direction
), road_rt
, tram_rt
);
788 DirtyCompanyInfrastructureWindows(company
);
796 * Are we allowed to remove the tunnel or bridge at \a tile?
797 * @param tile End point of the tunnel or bridge.
798 * @return A succeeded command if the tunnel or bridge may be removed, a failed command otherwise.
800 static inline CommandCost
CheckAllowRemoveTunnelBridge(TileIndex tile
)
802 /* Floods can remove anything as well as the scenario editor */
803 if (_current_company
== OWNER_WATER
|| _game_mode
== GM_EDITOR
) return CommandCost();
805 switch (GetTunnelBridgeTransportType(tile
)) {
806 case TRANSPORT_ROAD
: {
807 RoadType road_rt
= GetRoadTypeRoad(tile
);
808 RoadType tram_rt
= GetRoadTypeTram(tile
);
809 Owner road_owner
= _current_company
;
810 Owner tram_owner
= _current_company
;
812 if (road_rt
!= INVALID_ROADTYPE
) road_owner
= GetRoadOwner(tile
, RTT_ROAD
);
813 if (tram_rt
!= INVALID_ROADTYPE
) tram_owner
= GetRoadOwner(tile
, RTT_TRAM
);
815 /* We can remove unowned road and if the town allows it */
816 if (road_owner
== OWNER_TOWN
&& _current_company
!= OWNER_TOWN
&& !(_settings_game
.construction
.extra_dynamite
|| _cheats
.magic_bulldozer
.value
)) {
817 /* Town does not allow */
818 return CheckTileOwnership(tile
);
820 if (road_owner
== OWNER_NONE
|| road_owner
== OWNER_TOWN
) road_owner
= _current_company
;
821 if (tram_owner
== OWNER_NONE
) tram_owner
= _current_company
;
823 CommandCost ret
= CheckOwnership(road_owner
, tile
);
824 if (ret
.Succeeded()) ret
= CheckOwnership(tram_owner
, tile
);
829 return CheckOwnership(GetTileOwner(tile
));
831 case TRANSPORT_WATER
: {
832 /* Always allow to remove aqueducts without owner. */
833 Owner aqueduct_owner
= GetTileOwner(tile
);
834 if (aqueduct_owner
== OWNER_NONE
) aqueduct_owner
= _current_company
;
835 return CheckOwnership(aqueduct_owner
);
838 default: NOT_REACHED();
843 * Remove a tunnel from the game, update town rating, etc.
844 * @param tile Tile containing one of the endpoints of the tunnel.
845 * @param flags Command flags.
846 * @return Succeeded or failed command.
848 static CommandCost
DoClearTunnel(TileIndex tile
, DoCommandFlag flags
)
850 CommandCost ret
= CheckAllowRemoveTunnelBridge(tile
);
851 if (ret
.Failed()) return ret
;
853 TileIndex endtile
= GetOtherTunnelEnd(tile
);
855 ret
= TunnelBridgeIsFree(tile
, endtile
);
856 if (ret
.Failed()) return ret
;
858 _build_tunnel_endtile
= endtile
;
861 if (IsTileOwner(tile
, OWNER_TOWN
) && _game_mode
!= GM_EDITOR
) {
862 t
= ClosestTownFromTile(tile
, UINT_MAX
); // town penalty rating
864 /* Check if you are allowed to remove the tunnel owned by a town
865 * Removal depends on difficulty settings */
866 ret
= CheckforTownRating(flags
, t
, TUNNELBRIDGE_REMOVE
);
867 if (ret
.Failed()) return ret
;
870 /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
871 * you have a "Poor" (0) town rating */
872 if (IsTileOwner(tile
, OWNER_TOWN
) && _game_mode
!= GM_EDITOR
) {
873 ChangeTownRating(t
, RATING_TUNNEL_BRIDGE_DOWN_STEP
, RATING_TUNNEL_BRIDGE_MINIMUM
, flags
);
876 Money base_cost
= TunnelBridgeClearCost(tile
, PR_CLEAR_TUNNEL
);
877 uint len
= GetTunnelBridgeLength(tile
, endtile
) + 2; // Don't forget the end tiles.
879 if (flags
& DC_EXEC
) {
880 if (GetTunnelBridgeTransportType(tile
) == TRANSPORT_RAIL
) {
881 /* We first need to request values before calling DoClearSquare */
882 DiagDirection dir
= GetTunnelBridgeDirection(tile
);
883 Track track
= DiagDirToDiagTrack(dir
);
884 Owner owner
= GetTileOwner(tile
);
887 if (HasTunnelBridgeReservation(tile
)) {
888 v
= GetTrainForReservation(tile
, track
);
889 if (v
!= nullptr) FreeTrainTrackReservation(v
);
892 if (Company::IsValidID(owner
)) {
893 Company::Get(owner
)->infrastructure
.rail
[GetRailType(tile
)] -= len
* TUNNELBRIDGE_TRACKBIT_FACTOR
;
894 DirtyCompanyInfrastructureWindows(owner
);
898 DoClearSquare(endtile
);
900 /* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */
901 AddSideToSignalBuffer(tile
, ReverseDiagDir(dir
), owner
);
902 AddSideToSignalBuffer(endtile
, dir
, owner
);
904 YapfNotifyTrackLayoutChange(tile
, track
);
905 YapfNotifyTrackLayoutChange(endtile
, track
);
907 if (v
!= nullptr) TryPathReserve(v
);
909 /* A full diagonal road tile has two road bits. */
910 UpdateCompanyRoadInfrastructure(GetRoadTypeRoad(tile
), GetRoadOwner(tile
, RTT_ROAD
), -(int)(len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
));
911 UpdateCompanyRoadInfrastructure(GetRoadTypeTram(tile
), GetRoadOwner(tile
, RTT_TRAM
), -(int)(len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
));
914 DoClearSquare(endtile
);
918 return CommandCost(EXPENSES_CONSTRUCTION
, len
* base_cost
);
923 * Remove a bridge from the game, update town rating, etc.
924 * @param tile Tile containing one of the endpoints of the bridge.
925 * @param flags Command flags.
926 * @return Succeeded or failed command.
928 static CommandCost
DoClearBridge(TileIndex tile
, DoCommandFlag flags
)
930 CommandCost ret
= CheckAllowRemoveTunnelBridge(tile
);
931 if (ret
.Failed()) return ret
;
933 TileIndex endtile
= GetOtherBridgeEnd(tile
);
935 ret
= TunnelBridgeIsFree(tile
, endtile
);
936 if (ret
.Failed()) return ret
;
938 DiagDirection direction
= GetTunnelBridgeDirection(tile
);
939 TileIndexDiff delta
= TileOffsByDiagDir(direction
);
942 if (IsTileOwner(tile
, OWNER_TOWN
) && _game_mode
!= GM_EDITOR
) {
943 t
= ClosestTownFromTile(tile
, UINT_MAX
); // town penalty rating
945 /* Check if you are allowed to remove the bridge owned by a town
946 * Removal depends on difficulty settings */
947 ret
= CheckforTownRating(flags
, t
, TUNNELBRIDGE_REMOVE
);
948 if (ret
.Failed()) return ret
;
951 /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
952 * you have a "Poor" (0) town rating */
953 if (IsTileOwner(tile
, OWNER_TOWN
) && _game_mode
!= GM_EDITOR
) {
954 ChangeTownRating(t
, RATING_TUNNEL_BRIDGE_DOWN_STEP
, RATING_TUNNEL_BRIDGE_MINIMUM
, flags
);
957 Money base_cost
= TunnelBridgeClearCost(tile
, PR_CLEAR_BRIDGE
);
958 uint len
= GetTunnelBridgeLength(tile
, endtile
) + 2; // Don't forget the end tiles.
960 if (flags
& DC_EXEC
) {
961 /* read this value before actual removal of bridge */
962 bool rail
= GetTunnelBridgeTransportType(tile
) == TRANSPORT_RAIL
;
963 Owner owner
= GetTileOwner(tile
);
964 int height
= GetBridgeHeight(tile
);
967 if (rail
&& HasTunnelBridgeReservation(tile
)) {
968 v
= GetTrainForReservation(tile
, DiagDirToDiagTrack(direction
));
969 if (v
!= nullptr) FreeTrainTrackReservation(v
);
972 /* Update company infrastructure counts. */
974 if (Company::IsValidID(owner
)) Company::Get(owner
)->infrastructure
.rail
[GetRailType(tile
)] -= len
* TUNNELBRIDGE_TRACKBIT_FACTOR
;
975 } else if (GetTunnelBridgeTransportType(tile
) == TRANSPORT_ROAD
) {
976 /* A full diagonal road tile has two road bits. */
977 UpdateCompanyRoadInfrastructure(GetRoadTypeRoad(tile
), GetRoadOwner(tile
, RTT_ROAD
), -(int)(len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
));
978 UpdateCompanyRoadInfrastructure(GetRoadTypeTram(tile
), GetRoadOwner(tile
, RTT_TRAM
), -(int)(len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
));
980 if (Company::IsValidID(owner
)) Company::Get(owner
)->infrastructure
.water
-= len
* TUNNELBRIDGE_TRACKBIT_FACTOR
;
982 DirtyCompanyInfrastructureWindows(owner
);
985 DoClearSquare(endtile
);
987 for (TileIndex c
= tile
+ delta
; c
!= endtile
; c
+= delta
) {
988 /* do not let trees appear from 'nowhere' after removing bridge */
989 if (IsNormalRoadTile(c
) && GetRoadside(c
) == ROADSIDE_TREES
) {
990 int minz
= GetTileMaxZ(c
) + 3;
991 if (height
< minz
) SetRoadside(c
, ROADSIDE_PAVED
);
993 ClearBridgeMiddle(c
);
994 MarkTileDirtyByTile(c
, height
- TileHeight(c
));
998 /* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */
999 AddSideToSignalBuffer(tile
, ReverseDiagDir(direction
), owner
);
1000 AddSideToSignalBuffer(endtile
, direction
, owner
);
1002 Track track
= DiagDirToDiagTrack(direction
);
1003 YapfNotifyTrackLayoutChange(tile
, track
);
1004 YapfNotifyTrackLayoutChange(endtile
, track
);
1006 if (v
!= nullptr) TryPathReserve(v
, true);
1010 return CommandCost(EXPENSES_CONSTRUCTION
, len
* base_cost
);
1014 * Remove a tunnel or a bridge from the game.
1015 * @param tile Tile containing one of the endpoints.
1016 * @param flags Command flags.
1017 * @return Succeeded or failed command.
1019 static CommandCost
ClearTile_TunnelBridge(TileIndex tile
, DoCommandFlag flags
)
1021 if (IsTunnel(tile
)) {
1022 if (flags
& DC_AUTO
) return_cmd_error(STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST
);
1023 return DoClearTunnel(tile
, flags
);
1024 } else { // IsBridge(tile)
1025 if (flags
& DC_AUTO
) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
1026 return DoClearBridge(tile
, flags
);
1031 * Draw a single pillar sprite.
1032 * @param psid Pillarsprite
1036 * @param w Bounding box size in X direction
1037 * @param h Bounding box size in Y direction
1038 * @param subsprite Optional subsprite for drawing halfpillars
1040 static inline void DrawPillar(const PalSpriteID
*psid
, int x
, int y
, int z
, int w
, int h
, const SubSprite
*subsprite
)
1042 static const int PILLAR_Z_OFFSET
= TILE_HEIGHT
- BRIDGE_Z_START
; ///< Start offset of pillar wrt. bridge (downwards)
1043 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
);
1047 * Draw two bridge pillars (north and south).
1048 * @param z_bottom Bottom Z
1049 * @param z_top Top Z
1050 * @param psid Pillarsprite
1053 * @param w Bounding box size in X direction
1054 * @param h Bounding box size in Y direction
1055 * @return Reached Z at the bottom
1057 static int DrawPillarColumn(int z_bottom
, int z_top
, const PalSpriteID
*psid
, int x
, int y
, int w
, int h
)
1060 for (cur_z
= z_top
; cur_z
>= z_bottom
; cur_z
-= TILE_HEIGHT
) {
1061 DrawPillar(psid
, x
, y
, cur_z
, w
, h
, nullptr);
1067 * Draws the pillars under high bridges.
1069 * @param psid Image and palette of a bridge pillar.
1070 * @param ti #TileInfo of current bridge-middle-tile.
1071 * @param axis Orientation of bridge.
1072 * @param drawfarpillar Whether to draw the pillar at the back
1073 * @param x Sprite X position of front pillar.
1074 * @param y Sprite Y position of front pillar.
1075 * @param z_bridge Absolute height of bridge bottom.
1077 static void DrawBridgePillars(const PalSpriteID
*psid
, const TileInfo
*ti
, Axis axis
, bool drawfarpillar
, int x
, int y
, int z_bridge
)
1079 static const int bounding_box_size
[2] = {16, 2}; ///< bounding box size of pillars along bridge direction
1080 static const int back_pillar_offset
[2] = { 0, 9}; ///< sprite position offset of back facing pillar
1082 static const int INF
= 1000; ///< big number compared to sprite size
1083 static const SubSprite half_pillar_sub_sprite
[2][2] = {
1084 { { -14, -INF
, INF
, INF
}, { -INF
, -INF
, -15, INF
} }, // X axis, north and south
1085 { { -INF
, -INF
, 15, INF
}, { 16, -INF
, INF
, INF
} }, // Y axis, north and south
1088 if (psid
->sprite
== 0) return;
1090 /* Determine ground height under pillars */
1091 DiagDirection south_dir
= AxisToDiagDir(axis
);
1092 int z_front_north
= ti
->z
;
1093 int z_back_north
= ti
->z
;
1094 int z_front_south
= ti
->z
;
1095 int z_back_south
= ti
->z
;
1096 GetSlopePixelZOnEdge(ti
->tileh
, south_dir
, &z_front_south
, &z_back_south
);
1097 GetSlopePixelZOnEdge(ti
->tileh
, ReverseDiagDir(south_dir
), &z_front_north
, &z_back_north
);
1099 /* Shared height of pillars */
1100 int z_front
= std::max(z_front_north
, z_front_south
);
1101 int z_back
= std::max(z_back_north
, z_back_south
);
1103 /* x and y size of bounding-box of pillars */
1104 int w
= bounding_box_size
[axis
];
1105 int h
= bounding_box_size
[OtherAxis(axis
)];
1106 /* sprite position of back facing pillar */
1107 int x_back
= x
- back_pillar_offset
[axis
];
1108 int y_back
= y
- back_pillar_offset
[OtherAxis(axis
)];
1110 /* Draw front pillars */
1111 int bottom_z
= DrawPillarColumn(z_front
, z_bridge
, psid
, x
, y
, w
, h
);
1112 if (z_front_north
< z_front
) DrawPillar(psid
, x
, y
, bottom_z
, w
, h
, &half_pillar_sub_sprite
[axis
][0]);
1113 if (z_front_south
< z_front
) DrawPillar(psid
, x
, y
, bottom_z
, w
, h
, &half_pillar_sub_sprite
[axis
][1]);
1115 /* Draw back pillars, skip top two parts, which are hidden by the bridge */
1116 int z_bridge_back
= z_bridge
- 2 * (int)TILE_HEIGHT
;
1117 if (drawfarpillar
&& (z_back_north
<= z_bridge_back
|| z_back_south
<= z_bridge_back
)) {
1118 bottom_z
= DrawPillarColumn(z_back
, z_bridge_back
, psid
, x_back
, y_back
, w
, h
);
1119 if (z_back_north
< z_back
) DrawPillar(psid
, x_back
, y_back
, bottom_z
, w
, h
, &half_pillar_sub_sprite
[axis
][0]);
1120 if (z_back_south
< z_back
) DrawPillar(psid
, x_back
, y_back
, bottom_z
, w
, h
, &half_pillar_sub_sprite
[axis
][1]);
1125 * Retrieve the sprites required for catenary on a road/tram bridge.
1126 * @param rti RoadTypeInfo for the road or tram type to get catenary for
1127 * @param head_tile Bridge head tile with roadtype information
1128 * @param offset Sprite offset identifying flat to sloped bridge tiles
1129 * @param head Are we drawing bridge head?
1130 * @param[out] spr_back Back catenary sprite to use
1131 * @param[out] spr_front Front catenary sprite to use
1133 static void GetBridgeRoadCatenary(const RoadTypeInfo
*rti
, TileIndex head_tile
, int offset
, bool head
, SpriteID
&spr_back
, SpriteID
&spr_front
)
1135 static const SpriteID back_offsets
[6] = { 95, 96, 99, 102, 100, 101 };
1136 static const SpriteID front_offsets
[6] = { 97, 98, 103, 106, 104, 105 };
1138 /* Simplified from DrawRoadTypeCatenary() to remove all the special cases required for regular ground road */
1139 spr_back
= GetCustomRoadSprite(rti
, head_tile
, ROTSG_CATENARY_BACK
, head
? TCX_NORMAL
: TCX_ON_BRIDGE
);
1140 spr_front
= GetCustomRoadSprite(rti
, head_tile
, ROTSG_CATENARY_FRONT
, head
? TCX_NORMAL
: TCX_ON_BRIDGE
);
1141 if (spr_back
== 0 && spr_front
== 0) {
1142 spr_back
= SPR_TRAMWAY_BASE
+ back_offsets
[offset
];
1143 spr_front
= SPR_TRAMWAY_BASE
+ front_offsets
[offset
];
1145 if (spr_back
!= 0) spr_back
+= 23 + offset
;
1146 if (spr_front
!= 0) spr_front
+= 23 + offset
;
1151 * Draws the road and trambits over an already drawn (lower end) of a bridge.
1152 * @param head_tile bridge head tile with roadtype information
1153 * @param x the x of the bridge
1154 * @param y the y of the bridge
1155 * @param z the z of the bridge
1156 * @param offset sprite offset identifying flat to sloped bridge tiles
1157 * @param head are we drawing bridge head?
1159 static void DrawBridgeRoadBits(TileIndex head_tile
, int x
, int y
, int z
, int offset
, bool head
)
1161 RoadType road_rt
= GetRoadTypeRoad(head_tile
);
1162 RoadType tram_rt
= GetRoadTypeTram(head_tile
);
1163 const RoadTypeInfo
*road_rti
= road_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(road_rt
);
1164 const RoadTypeInfo
*tram_rti
= tram_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(tram_rt
);
1166 SpriteID seq_back
[4] = { 0 };
1167 bool trans_back
[4] = { false };
1168 SpriteID seq_front
[4] = { 0 };
1169 bool trans_front
[4] = { false };
1171 static const SpriteID overlay_offsets
[6] = { 0, 1, 11, 12, 13, 14 };
1172 if (head
|| !IsInvisibilitySet(TO_BRIDGES
)) {
1173 /* Road underlay takes precedence over tram */
1174 trans_back
[0] = !head
&& IsTransparencySet(TO_BRIDGES
);
1175 if (road_rti
!= nullptr) {
1176 if (road_rti
->UsesOverlay()) {
1177 seq_back
[0] = GetCustomRoadSprite(road_rti
, head_tile
, ROTSG_BRIDGE
, head
? TCX_NORMAL
: TCX_ON_BRIDGE
) + offset
;
1179 } else if (tram_rti
!= nullptr) {
1180 if (tram_rti
->UsesOverlay()) {
1181 seq_back
[0] = GetCustomRoadSprite(tram_rti
, head_tile
, ROTSG_BRIDGE
, head
? TCX_NORMAL
: TCX_ON_BRIDGE
) + offset
;
1183 seq_back
[0] = SPR_TRAMWAY_BRIDGE
+ offset
;
1187 /* Draw road overlay */
1188 trans_back
[1] = !head
&& IsTransparencySet(TO_BRIDGES
);
1189 if (road_rti
!= nullptr) {
1190 if (road_rti
->UsesOverlay()) {
1191 seq_back
[1] = GetCustomRoadSprite(road_rti
, head_tile
, ROTSG_OVERLAY
, head
? TCX_NORMAL
: TCX_ON_BRIDGE
);
1192 if (seq_back
[1] != 0) seq_back
[1] += overlay_offsets
[offset
];
1196 /* Draw tram overlay */
1197 trans_back
[2] = !head
&& IsTransparencySet(TO_BRIDGES
);
1198 if (tram_rti
!= nullptr) {
1199 if (tram_rti
->UsesOverlay()) {
1200 seq_back
[2] = GetCustomRoadSprite(tram_rti
, head_tile
, ROTSG_OVERLAY
, head
? TCX_NORMAL
: TCX_ON_BRIDGE
);
1201 if (seq_back
[2] != 0) seq_back
[2] += overlay_offsets
[offset
];
1202 } else if (road_rti
!= nullptr) {
1203 seq_back
[2] = SPR_TRAMWAY_OVERLAY
+ overlay_offsets
[offset
];
1207 /* Road catenary takes precedence over tram */
1208 trans_back
[3] = IsTransparencySet(TO_CATENARY
);
1209 trans_front
[0] = IsTransparencySet(TO_CATENARY
);
1210 if (road_rti
!= nullptr && HasRoadCatenaryDrawn(road_rt
)) {
1211 GetBridgeRoadCatenary(road_rti
, head_tile
, offset
, head
, seq_back
[3], seq_front
[0]);
1212 } else if (tram_rti
!= nullptr && HasRoadCatenaryDrawn(tram_rt
)) {
1213 GetBridgeRoadCatenary(tram_rti
, head_tile
, offset
, head
, seq_back
[3], seq_front
[0]);
1217 static const uint size_x
[6] = { 1, 16, 16, 1, 16, 1 };
1218 static const uint size_y
[6] = { 16, 1, 1, 16, 1, 16 };
1219 static const uint front_bb_offset_x
[6] = { 15, 0, 0, 15, 0, 15 };
1220 static const uint front_bb_offset_y
[6] = { 0, 15, 15, 0, 15, 0 };
1222 /* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called
1223 * The bounding boxes here are the same as for bridge front/roof */
1224 for (uint i
= 0; i
< lengthof(seq_back
); ++i
) {
1225 if (seq_back
[i
] != 0) {
1226 AddSortableSpriteToDraw(seq_back
[i
], PAL_NONE
,
1227 x
, y
, size_x
[offset
], size_y
[offset
], 0x28, z
,
1232 /* Start a new SpriteCombine for the front part */
1234 StartSpriteCombine();
1236 for (uint i
= 0; i
< lengthof(seq_front
); ++i
) {
1237 if (seq_front
[i
] != 0) {
1238 AddSortableSpriteToDraw(seq_front
[i
], PAL_NONE
,
1239 x
, y
, size_x
[offset
] + front_bb_offset_x
[offset
], size_y
[offset
] + front_bb_offset_y
[offset
], 0x28, z
,
1241 front_bb_offset_x
[offset
], front_bb_offset_y
[offset
]);
1247 * Draws a tunnel of bridge tile.
1248 * For tunnels, this is rather simple, as you only need to draw the entrance.
1249 * Bridges are a bit more complex. base_offset is where the sprite selection comes into play
1250 * and it works a bit like a bitmask.<p> For bridge heads:
1251 * @param ti TileInfo of the structure to draw
1252 * <ul><li>Bit 0: direction</li>
1253 * <li>Bit 1: northern or southern heads</li>
1254 * <li>Bit 2: Set if the bridge head is sloped</li>
1255 * <li>Bit 3 and more: Railtype Specific subset</li>
1257 * Please note that in this code, "roads" are treated as railtype 1, whilst the real railtypes are 0, 2 and 3
1259 static void DrawTile_TunnelBridge(TileInfo
*ti
)
1261 TransportType transport_type
= GetTunnelBridgeTransportType(ti
->tile
);
1262 DiagDirection tunnelbridge_direction
= GetTunnelBridgeDirection(ti
->tile
);
1264 if (IsTunnel(ti
->tile
)) {
1265 /* Front view of tunnel bounding boxes:
1267 * 122223 <- BB_Z_SEPARATOR
1269 * 1 3 1,3 = empty helper BB
1270 * 1 3 2 = SpriteCombine of tunnel-roof and catenary (tram & elrail)
1274 static const int _tunnel_BB
[4][12] = {
1275 /* tunnnel-roof | Z-separator | tram-catenary
1276 * w h bb_x bb_y| x y w h |bb_x bb_y w h */
1277 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 }, // NE
1278 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 }, // SE
1279 { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 }, // SW
1280 { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 }, // NW
1282 const int *BB_data
= _tunnel_BB
[tunnelbridge_direction
];
1284 bool catenary
= false;
1287 SpriteID railtype_overlay
= 0;
1288 if (transport_type
== TRANSPORT_RAIL
) {
1289 const RailTypeInfo
*rti
= GetRailTypeInfo(GetRailType(ti
->tile
));
1290 image
= rti
->base_sprites
.tunnel
;
1291 if (rti
->UsesOverlay()) {
1292 /* Check if the railtype has custom tunnel portals. */
1293 railtype_overlay
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_TUNNEL_PORTAL
);
1294 if (railtype_overlay
!= 0) image
= SPR_RAILTYPE_TUNNEL_BASE
; // Draw blank grass tunnel base.
1297 image
= SPR_TUNNEL_ENTRY_REAR_ROAD
;
1300 if (HasTunnelBridgeSnowOrDesert(ti
->tile
)) image
+= railtype_overlay
!= 0 ? 8 : 32;
1302 image
+= tunnelbridge_direction
* 2;
1303 DrawGroundSprite(image
, PAL_NONE
);
1305 if (transport_type
== TRANSPORT_ROAD
) {
1306 RoadType road_rt
= GetRoadTypeRoad(ti
->tile
);
1307 RoadType tram_rt
= GetRoadTypeTram(ti
->tile
);
1308 const RoadTypeInfo
*road_rti
= road_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(road_rt
);
1309 const RoadTypeInfo
*tram_rti
= tram_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(tram_rt
);
1310 uint sprite_offset
= DiagDirToAxis(tunnelbridge_direction
) == AXIS_X
? 1 : 0;
1311 bool draw_underlay
= true;
1313 /* Road underlay takes precedence over tram */
1314 if (road_rti
!= nullptr) {
1315 if (road_rti
->UsesOverlay()) {
1316 SpriteID ground
= GetCustomRoadSprite(road_rti
, ti
->tile
, ROTSG_TUNNEL
);
1318 DrawGroundSprite(ground
+ tunnelbridge_direction
, PAL_NONE
);
1319 draw_underlay
= false;
1323 if (tram_rti
->UsesOverlay()) {
1324 SpriteID ground
= GetCustomRoadSprite(tram_rti
, ti
->tile
, ROTSG_TUNNEL
);
1326 DrawGroundSprite(ground
+ tunnelbridge_direction
, PAL_NONE
);
1327 draw_underlay
= false;
1332 DrawRoadOverlays(ti
, PAL_NONE
, road_rti
, tram_rti
, sprite_offset
, sprite_offset
, draw_underlay
);
1334 /* Road catenary takes precedence over tram */
1335 SpriteID catenary_sprite_base
= 0;
1336 if (road_rti
!= nullptr && HasRoadCatenaryDrawn(road_rt
)) {
1337 catenary_sprite_base
= GetCustomRoadSprite(road_rti
, ti
->tile
, ROTSG_CATENARY_FRONT
);
1338 if (catenary_sprite_base
== 0) {
1339 catenary_sprite_base
= SPR_TRAMWAY_TUNNEL_WIRES
;
1341 catenary_sprite_base
+= 19;
1343 } else if (tram_rti
!= nullptr && HasRoadCatenaryDrawn(tram_rt
)) {
1344 catenary_sprite_base
= GetCustomRoadSprite(tram_rti
, ti
->tile
, ROTSG_CATENARY_FRONT
);
1345 if (catenary_sprite_base
== 0) {
1346 catenary_sprite_base
= SPR_TRAMWAY_TUNNEL_WIRES
;
1348 catenary_sprite_base
+= 19;
1352 if (catenary_sprite_base
!= 0) {
1354 StartSpriteCombine();
1355 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
);
1358 const RailTypeInfo
*rti
= GetRailTypeInfo(GetRailType(ti
->tile
));
1359 if (rti
->UsesOverlay()) {
1360 SpriteID surface
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_TUNNEL
);
1361 if (surface
!= 0) DrawGroundSprite(surface
+ tunnelbridge_direction
, PAL_NONE
);
1364 /* PBS debugging, draw reserved tracks darker */
1365 if (_game_mode
!= GM_MENU
&& _settings_client
.gui
.show_track_reservation
&& HasTunnelBridgeReservation(ti
->tile
)) {
1366 if (rti
->UsesOverlay()) {
1367 SpriteID overlay
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_OVERLAY
);
1368 DrawGroundSprite(overlay
+ RTO_X
+ DiagDirToAxis(tunnelbridge_direction
), PALETTE_CRASH
);
1370 DrawGroundSprite(DiagDirToAxis(tunnelbridge_direction
) == AXIS_X
? rti
->base_sprites
.single_x
: rti
->base_sprites
.single_y
, PALETTE_CRASH
);
1374 if (HasRailCatenaryDrawn(GetRailType(ti
->tile
))) {
1375 /* Maybe draw pylons on the entry side */
1376 DrawRailCatenary(ti
);
1379 StartSpriteCombine();
1380 /* Draw wire above the ramp */
1381 DrawRailCatenaryOnTunnel(ti
);
1385 if (railtype_overlay
!= 0 && !catenary
) StartSpriteCombine();
1387 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
);
1388 /* Draw railtype tunnel portal overlay if defined. */
1389 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
);
1391 if (catenary
|| railtype_overlay
!= 0) EndSpriteCombine();
1393 /* Add helper BB for sprite sorting that separates the tunnel from things beside of it. */
1394 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX
, PAL_NONE
, ti
->x
, ti
->y
, BB_data
[6], BB_data
[7], TILE_HEIGHT
, ti
->z
);
1395 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
);
1397 DrawBridgeMiddle(ti
);
1398 } else { // IsBridge(ti->tile)
1399 const PalSpriteID
*psid
;
1401 bool ice
= HasTunnelBridgeSnowOrDesert(ti
->tile
);
1403 if (transport_type
== TRANSPORT_RAIL
) {
1404 base_offset
= GetRailTypeInfo(GetRailType(ti
->tile
))->bridge_offset
;
1405 assert(base_offset
!= 8); // This one is used for roads
1410 /* as the lower 3 bits are used for other stuff, make sure they are clear */
1411 assert( (base_offset
& 0x07) == 0x00);
1413 DrawFoundation(ti
, GetBridgeFoundation(ti
->tileh
, DiagDirToAxis(tunnelbridge_direction
)));
1415 /* HACK Wizardry to convert the bridge ramp direction into a sprite offset */
1416 base_offset
+= (6 - tunnelbridge_direction
) % 4;
1418 /* Table number BRIDGE_PIECE_HEAD always refers to the bridge heads for any bridge type */
1419 if (transport_type
!= TRANSPORT_WATER
) {
1420 if (ti
->tileh
== SLOPE_FLAT
) base_offset
+= 4; // sloped bridge head
1421 psid
= &GetBridgeSpriteTable(GetBridgeType(ti
->tile
), BRIDGE_PIECE_HEAD
)[base_offset
];
1423 psid
= _aqueduct_sprites
+ base_offset
;
1427 TileIndex next
= ti
->tile
+ TileOffsByDiagDir(tunnelbridge_direction
);
1428 if (ti
->tileh
!= SLOPE_FLAT
&& ti
->z
== 0 && HasTileWaterClass(next
) && GetWaterClass(next
) == WATER_CLASS_SEA
) {
1429 DrawShoreTile(ti
->tileh
);
1431 DrawClearLandTile(ti
, 3);
1434 DrawGroundSprite(SPR_FLAT_SNOW_DESERT_TILE
+ SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
1439 /* Draw Trambits and PBS Reservation as SpriteCombine */
1440 if (transport_type
== TRANSPORT_ROAD
|| transport_type
== TRANSPORT_RAIL
) StartSpriteCombine();
1442 /* HACK set the height of the BB of a sloped ramp to 1 so a vehicle on
1443 * it doesn't disappear behind it
1445 /* Bridge heads are drawn solid no matter how invisibility/transparency is set */
1446 AddSortableSpriteToDraw(psid
->sprite
, psid
->pal
, ti
->x
, ti
->y
, 16, 16, ti
->tileh
== SLOPE_FLAT
? 0 : 8, ti
->z
);
1448 if (transport_type
== TRANSPORT_ROAD
) {
1449 uint offset
= tunnelbridge_direction
;
1451 if (ti
->tileh
!= SLOPE_FLAT
) {
1452 offset
= (offset
+ 1) & 1;
1458 /* DrawBridgeRoadBits() calls EndSpriteCombine() and StartSpriteCombine() */
1459 DrawBridgeRoadBits(ti
->tile
, ti
->x
, ti
->y
, z
, offset
, true);
1462 } else if (transport_type
== TRANSPORT_RAIL
) {
1463 const RailTypeInfo
*rti
= GetRailTypeInfo(GetRailType(ti
->tile
));
1464 if (rti
->UsesOverlay()) {
1465 SpriteID surface
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_BRIDGE
);
1467 if (HasBridgeFlatRamp(ti
->tileh
, DiagDirToAxis(tunnelbridge_direction
))) {
1468 AddSortableSpriteToDraw(surface
+ ((DiagDirToAxis(tunnelbridge_direction
) == AXIS_X
) ? RTBO_X
: RTBO_Y
), PAL_NONE
, ti
->x
, ti
->y
, 16, 16, 0, ti
->z
+ 8);
1470 AddSortableSpriteToDraw(surface
+ RTBO_SLOPE
+ tunnelbridge_direction
, PAL_NONE
, ti
->x
, ti
->y
, 16, 16, 8, ti
->z
);
1473 /* Don't fallback to non-overlay sprite -- the spec states that
1474 * if an overlay is present then the bridge surface must be
1478 /* PBS debugging, draw reserved tracks darker */
1479 if (_game_mode
!= GM_MENU
&& _settings_client
.gui
.show_track_reservation
&& HasTunnelBridgeReservation(ti
->tile
)) {
1480 if (rti
->UsesOverlay()) {
1481 SpriteID overlay
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_OVERLAY
);
1482 if (HasBridgeFlatRamp(ti
->tileh
, DiagDirToAxis(tunnelbridge_direction
))) {
1483 AddSortableSpriteToDraw(overlay
+ RTO_X
+ DiagDirToAxis(tunnelbridge_direction
), PALETTE_CRASH
, ti
->x
, ti
->y
, 16, 16, 0, ti
->z
+ 8);
1485 AddSortableSpriteToDraw(overlay
+ RTO_SLOPE_NE
+ tunnelbridge_direction
, PALETTE_CRASH
, ti
->x
, ti
->y
, 16, 16, 8, ti
->z
);
1488 if (HasBridgeFlatRamp(ti
->tileh
, DiagDirToAxis(tunnelbridge_direction
))) {
1489 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);
1491 AddSortableSpriteToDraw(rti
->base_sprites
.single_sloped
+ tunnelbridge_direction
, PALETTE_CRASH
, ti
->x
, ti
->y
, 16, 16, 8, ti
->z
);
1497 if (HasRailCatenaryDrawn(GetRailType(ti
->tile
))) {
1498 DrawRailCatenary(ti
);
1502 DrawBridgeMiddle(ti
);
1508 * Compute bridge piece. Computes the bridge piece to display depending on the position inside the bridge.
1509 * bridges pieces sequence (middle parts).
1510 * Note that it is not covering the bridge heads, which are always referenced by the same sprite table.
1511 * bridge len 1: BRIDGE_PIECE_NORTH
1512 * bridge len 2: BRIDGE_PIECE_NORTH BRIDGE_PIECE_SOUTH
1513 * bridge len 3: BRIDGE_PIECE_NORTH BRIDGE_PIECE_MIDDLE_ODD BRIDGE_PIECE_SOUTH
1514 * bridge len 4: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH
1515 * bridge len 5: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_MIDDLE_EVEN BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH
1516 * 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
1517 * 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
1518 * #0 - always as first, #1 - always as last (if len>1)
1519 * #2,#3 are to pair in order
1520 * for odd bridges: #5 is going in the bridge middle if on even position, #4 on odd (counting from 0)
1521 * @param north Northernmost tile of bridge
1522 * @param south Southernmost tile of bridge
1523 * @return Index of bridge piece
1525 static BridgePieces
CalcBridgePiece(uint north
, uint south
)
1528 return BRIDGE_PIECE_NORTH
;
1529 } else if (south
== 1) {
1530 return BRIDGE_PIECE_SOUTH
;
1531 } else if (north
< south
) {
1532 return north
& 1 ? BRIDGE_PIECE_INNER_SOUTH
: BRIDGE_PIECE_INNER_NORTH
;
1533 } else if (north
> south
) {
1534 return south
& 1 ? BRIDGE_PIECE_INNER_NORTH
: BRIDGE_PIECE_INNER_SOUTH
;
1536 return north
& 1 ? BRIDGE_PIECE_MIDDLE_EVEN
: BRIDGE_PIECE_MIDDLE_ODD
;
1541 * Draw the middle bits of a bridge.
1542 * @param ti Tile information of the tile to draw it on.
1544 void DrawBridgeMiddle(const TileInfo
*ti
)
1546 /* Sectional view of bridge bounding boxes:
1548 * 1 2 1,2 = SpriteCombine of Bridge front/(back&floor) and RoadCatenary
1549 * 1 2 3 = empty helper BB
1550 * 1 7 2 4,5 = pillars under higher bridges
1551 * 1 6 88888 6 2 6 = elrail-pylons
1552 * 1 6 88888 6 2 7 = elrail-wire
1553 * 1 6 88888 6 2 <- TILE_HEIGHT 8 = rail-vehicle on bridge
1554 * 3333333333333 <- BB_Z_SEPARATOR
1556 * 4 5 <- BB_HEIGHT_UNDER_BRIDGE
1562 if (!IsBridgeAbove(ti
->tile
)) return;
1564 TileIndex rampnorth
= GetNorthernBridgeEnd(ti
->tile
);
1565 TileIndex rampsouth
= GetSouthernBridgeEnd(ti
->tile
);
1566 TransportType transport_type
= GetTunnelBridgeTransportType(rampsouth
);
1568 Axis axis
= GetBridgeAxis(ti
->tile
);
1569 BridgePieces piece
= CalcBridgePiece(
1570 GetTunnelBridgeLength(ti
->tile
, rampnorth
) + 1,
1571 GetTunnelBridgeLength(ti
->tile
, rampsouth
) + 1
1574 const PalSpriteID
*psid
;
1576 if (transport_type
!= TRANSPORT_WATER
) {
1577 BridgeType type
= GetBridgeType(rampsouth
);
1578 drawfarpillar
= !HasBit(GetBridgeSpec(type
)->flags
, 0);
1581 if (transport_type
== TRANSPORT_RAIL
) {
1582 base_offset
= GetRailTypeInfo(GetRailType(rampsouth
))->bridge_offset
;
1587 psid
= base_offset
+ GetBridgeSpriteTable(type
, piece
);
1589 drawfarpillar
= true;
1590 psid
= _aqueduct_sprites
;
1593 if (axis
!= AXIS_X
) psid
+= 4;
1597 uint bridge_z
= GetBridgePixelHeight(rampsouth
);
1598 int z
= bridge_z
- BRIDGE_Z_START
;
1600 /* Add a bounding box that separates the bridge from things below it. */
1601 AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX
, PAL_NONE
, x
, y
, 16, 16, 1, bridge_z
- TILE_HEIGHT
+ BB_Z_SEPARATOR
);
1603 /* Draw Trambits as SpriteCombine */
1604 if (transport_type
== TRANSPORT_ROAD
|| transport_type
== TRANSPORT_RAIL
) StartSpriteCombine();
1606 /* Draw floor and far part of bridge*/
1607 if (!IsInvisibilitySet(TO_BRIDGES
)) {
1608 if (axis
== AXIS_X
) {
1609 AddSortableSpriteToDraw(psid
->sprite
, psid
->pal
, x
, y
, 16, 1, 0x28, z
, IsTransparencySet(TO_BRIDGES
), 0, 0, BRIDGE_Z_START
);
1611 AddSortableSpriteToDraw(psid
->sprite
, psid
->pal
, x
, y
, 1, 16, 0x28, z
, IsTransparencySet(TO_BRIDGES
), 0, 0, BRIDGE_Z_START
);
1617 if (transport_type
== TRANSPORT_ROAD
) {
1618 /* DrawBridgeRoadBits() calls EndSpriteCombine() and StartSpriteCombine() */
1619 DrawBridgeRoadBits(rampsouth
, x
, y
, bridge_z
, axis
^ 1, false);
1620 } else if (transport_type
== TRANSPORT_RAIL
) {
1621 const RailTypeInfo
*rti
= GetRailTypeInfo(GetRailType(rampsouth
));
1622 if (rti
->UsesOverlay() && !IsInvisibilitySet(TO_BRIDGES
)) {
1623 SpriteID surface
= GetCustomRailSprite(rti
, rampsouth
, RTSG_BRIDGE
, TCX_ON_BRIDGE
);
1625 AddSortableSpriteToDraw(surface
+ axis
, PAL_NONE
, x
, y
, 16, 16, 0, bridge_z
, IsTransparencySet(TO_BRIDGES
));
1629 if (_game_mode
!= GM_MENU
&& _settings_client
.gui
.show_track_reservation
&& !IsInvisibilitySet(TO_BRIDGES
) && HasTunnelBridgeReservation(rampnorth
)) {
1630 if (rti
->UsesOverlay()) {
1631 SpriteID overlay
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_OVERLAY
);
1632 AddSortableSpriteToDraw(overlay
+ RTO_X
+ axis
, PALETTE_CRASH
, ti
->x
, ti
->y
, 16, 16, 0, bridge_z
, IsTransparencySet(TO_BRIDGES
));
1634 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
));
1640 if (HasRailCatenaryDrawn(GetRailType(rampsouth
))) {
1641 DrawRailCatenaryOnBridge(ti
);
1645 /* draw roof, the component of the bridge which is logically between the vehicle and the camera */
1646 if (!IsInvisibilitySet(TO_BRIDGES
)) {
1647 if (axis
== AXIS_X
) {
1649 if (psid
->sprite
& SPRITE_MASK
) AddSortableSpriteToDraw(psid
->sprite
, psid
->pal
, x
, y
, 16, 4, 0x28, z
, IsTransparencySet(TO_BRIDGES
), 0, 3, BRIDGE_Z_START
);
1652 if (psid
->sprite
& SPRITE_MASK
) AddSortableSpriteToDraw(psid
->sprite
, psid
->pal
, x
, y
, 4, 16, 0x28, z
, IsTransparencySet(TO_BRIDGES
), 3, 0, BRIDGE_Z_START
);
1656 /* Draw TramFront as SpriteCombine */
1657 if (transport_type
== TRANSPORT_ROAD
) EndSpriteCombine();
1659 /* Do not draw anything more if bridges are invisible */
1660 if (IsInvisibilitySet(TO_BRIDGES
)) return;
1663 DrawBridgePillars(psid
, ti
, axis
, drawfarpillar
, x
, y
, z
);
1667 static int GetSlopePixelZ_TunnelBridge(TileIndex tile
, uint x
, uint y
, bool ground_vehicle
)
1670 Slope tileh
= GetTilePixelSlope(tile
, &z
);
1675 if (IsTunnel(tile
)) {
1676 /* In the tunnel entrance? */
1677 if (ground_vehicle
) return z
;
1678 } else { // IsBridge(tile)
1679 DiagDirection dir
= GetTunnelBridgeDirection(tile
);
1680 z
+= ApplyPixelFoundationToSlope(GetBridgeFoundation(tileh
, DiagDirToAxis(dir
)), &tileh
);
1682 /* On the bridge ramp? */
1683 if (ground_vehicle
) {
1684 if (tileh
!= SLOPE_FLAT
) return z
+ TILE_HEIGHT
;
1687 default: NOT_REACHED();
1688 case DIAGDIR_NE
: tileh
= SLOPE_NE
; break;
1689 case DIAGDIR_SE
: tileh
= SLOPE_SE
; break;
1690 case DIAGDIR_SW
: tileh
= SLOPE_SW
; break;
1691 case DIAGDIR_NW
: tileh
= SLOPE_NW
; break;
1696 return z
+ GetPartialPixelZ(x
, y
, tileh
);
1699 static Foundation
GetFoundation_TunnelBridge(TileIndex tile
, Slope tileh
)
1701 return IsTunnel(tile
) ? FOUNDATION_NONE
: GetBridgeFoundation(tileh
, DiagDirToAxis(GetTunnelBridgeDirection(tile
)));
1704 static void GetTileDesc_TunnelBridge(TileIndex tile
, TileDesc
*td
)
1706 TransportType tt
= GetTunnelBridgeTransportType(tile
);
1708 if (IsTunnel(tile
)) {
1709 td
->str
= (tt
== TRANSPORT_RAIL
) ? STR_LAI_TUNNEL_DESCRIPTION_RAILROAD
: STR_LAI_TUNNEL_DESCRIPTION_ROAD
;
1710 } else { // IsBridge(tile)
1711 td
->str
= (tt
== TRANSPORT_WATER
) ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT
: GetBridgeSpec(GetBridgeType(tile
))->transport_name
[tt
];
1713 td
->owner
[0] = GetTileOwner(tile
);
1715 Owner road_owner
= INVALID_OWNER
;
1716 Owner tram_owner
= INVALID_OWNER
;
1717 RoadType road_rt
= GetRoadTypeRoad(tile
);
1718 RoadType tram_rt
= GetRoadTypeTram(tile
);
1719 if (road_rt
!= INVALID_ROADTYPE
) {
1720 const RoadTypeInfo
*rti
= GetRoadTypeInfo(road_rt
);
1721 td
->roadtype
= rti
->strings
.name
;
1722 td
->road_speed
= rti
->max_speed
/ 2;
1723 road_owner
= GetRoadOwner(tile
, RTT_ROAD
);
1725 if (tram_rt
!= INVALID_ROADTYPE
) {
1726 const RoadTypeInfo
*rti
= GetRoadTypeInfo(tram_rt
);
1727 td
->tramtype
= rti
->strings
.name
;
1728 td
->tram_speed
= rti
->max_speed
/ 2;
1729 tram_owner
= GetRoadOwner(tile
, RTT_TRAM
);
1732 /* Is there a mix of owners? */
1733 if ((tram_owner
!= INVALID_OWNER
&& tram_owner
!= td
->owner
[0]) ||
1734 (road_owner
!= INVALID_OWNER
&& road_owner
!= td
->owner
[0])) {
1736 if (road_owner
!= INVALID_OWNER
) {
1737 td
->owner_type
[i
] = STR_LAND_AREA_INFORMATION_ROAD_OWNER
;
1738 td
->owner
[i
] = road_owner
;
1741 if (tram_owner
!= INVALID_OWNER
) {
1742 td
->owner_type
[i
] = STR_LAND_AREA_INFORMATION_TRAM_OWNER
;
1743 td
->owner
[i
] = tram_owner
;
1747 if (tt
== TRANSPORT_RAIL
) {
1748 const RailTypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
));
1749 td
->rail_speed
= rti
->max_speed
;
1750 td
->railtype
= rti
->strings
.name
;
1752 if (!IsTunnel(tile
)) {
1753 uint16_t spd
= GetBridgeSpec(GetBridgeType(tile
))->speed
;
1754 /* rail speed special-cases 0 as unlimited, hides display of limit etc. */
1755 if (spd
== UINT16_MAX
) spd
= 0;
1756 if (td
->rail_speed
== 0 || spd
< td
->rail_speed
) {
1757 td
->rail_speed
= spd
;
1760 } else if (tt
== TRANSPORT_ROAD
&& !IsTunnel(tile
)) {
1761 uint16_t spd
= GetBridgeSpec(GetBridgeType(tile
))->speed
;
1762 /* road speed special-cases 0 as unlimited, hides display of limit etc. */
1763 if (spd
== UINT16_MAX
) spd
= 0;
1764 if (road_rt
!= INVALID_ROADTYPE
&& (td
->road_speed
== 0 || spd
< td
->road_speed
)) td
->road_speed
= spd
;
1765 if (tram_rt
!= INVALID_ROADTYPE
&& (td
->tram_speed
== 0 || spd
< td
->tram_speed
)) td
->tram_speed
= spd
;
1770 static void TileLoop_TunnelBridge(TileIndex tile
)
1772 bool snow_or_desert
= HasTunnelBridgeSnowOrDesert(tile
);
1773 switch (_settings_game
.game_creation
.landscape
) {
1775 /* As long as we do not have a snow density, we want to use the density
1776 * from the entry edge. For tunnels this is the lowest point for bridges the highest point.
1777 * (Independent of foundations) */
1778 int z
= IsBridge(tile
) ? GetTileMaxZ(tile
) : GetTileZ(tile
);
1779 if (snow_or_desert
!= (z
> GetSnowLine())) {
1780 SetTunnelBridgeSnowOrDesert(tile
, !snow_or_desert
);
1781 MarkTileDirtyByTile(tile
);
1787 if (GetTropicZone(tile
) == TROPICZONE_DESERT
&& !snow_or_desert
) {
1788 SetTunnelBridgeSnowOrDesert(tile
, true);
1789 MarkTileDirtyByTile(tile
);
1798 static TrackStatus
GetTileTrackStatus_TunnelBridge(TileIndex tile
, TransportType mode
, uint sub_mode
, DiagDirection side
)
1800 TransportType transport_type
= GetTunnelBridgeTransportType(tile
);
1801 if (transport_type
!= mode
|| (transport_type
== TRANSPORT_ROAD
&& !HasTileRoadType(tile
, (RoadTramType
)sub_mode
))) return 0;
1803 DiagDirection dir
= GetTunnelBridgeDirection(tile
);
1804 if (side
!= INVALID_DIAGDIR
&& side
!= ReverseDiagDir(dir
)) return 0;
1805 return CombineTrackStatus(TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir
)), TRACKDIR_BIT_NONE
);
1808 static void ChangeTileOwner_TunnelBridge(TileIndex tile
, Owner old_owner
, Owner new_owner
)
1810 TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
1811 /* Set number of pieces to zero if it's the southern tile as we
1812 * don't want to update the infrastructure counts twice. */
1813 uint num_pieces
= tile
< other_end
? (GetTunnelBridgeLength(tile
, other_end
) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR
: 0;
1815 for (RoadTramType rtt
: _roadtramtypes
) {
1816 /* Update all roadtypes, no matter if they are present */
1817 if (GetRoadOwner(tile
, rtt
) == old_owner
) {
1818 RoadType rt
= GetRoadType(tile
, rtt
);
1819 if (rt
!= INVALID_ROADTYPE
) {
1820 /* Update company infrastructure counts. A full diagonal road tile has two road bits.
1821 * No need to dirty windows here, we'll redraw the whole screen anyway. */
1822 Company::Get(old_owner
)->infrastructure
.road
[rt
] -= num_pieces
* 2;
1823 if (new_owner
!= INVALID_OWNER
) Company::Get(new_owner
)->infrastructure
.road
[rt
] += num_pieces
* 2;
1826 SetRoadOwner(tile
, rtt
, new_owner
== INVALID_OWNER
? OWNER_NONE
: new_owner
);
1830 if (!IsTileOwner(tile
, old_owner
)) return;
1832 /* Update company infrastructure counts for rail and water as well.
1833 * No need to dirty windows here, we'll redraw the whole screen anyway. */
1834 TransportType tt
= GetTunnelBridgeTransportType(tile
);
1835 Company
*old
= Company::Get(old_owner
);
1836 if (tt
== TRANSPORT_RAIL
) {
1837 old
->infrastructure
.rail
[GetRailType(tile
)] -= num_pieces
;
1838 if (new_owner
!= INVALID_OWNER
) Company::Get(new_owner
)->infrastructure
.rail
[GetRailType(tile
)] += num_pieces
;
1839 } else if (tt
== TRANSPORT_WATER
) {
1840 old
->infrastructure
.water
-= num_pieces
;
1841 if (new_owner
!= INVALID_OWNER
) Company::Get(new_owner
)->infrastructure
.water
+= num_pieces
;
1844 if (new_owner
!= INVALID_OWNER
) {
1845 SetTileOwner(tile
, new_owner
);
1847 if (tt
== TRANSPORT_RAIL
) {
1848 /* Since all of our vehicles have been removed, it is safe to remove the rail
1849 * bridge / tunnel. */
1850 [[maybe_unused
]] CommandCost ret
= Command
<CMD_LANDSCAPE_CLEAR
>::Do(DC_EXEC
| DC_BANKRUPT
, tile
);
1851 assert(ret
.Succeeded());
1853 /* In any other case, we can safely reassign the ownership to OWNER_NONE. */
1854 SetTileOwner(tile
, OWNER_NONE
);
1860 * Helper to prepare the ground vehicle when entering a bridge. This get called
1861 * when entering the bridge, at the last frame of travel on the bridge head.
1862 * Our calling function gets called before UpdateInclination/UpdateZPosition,
1863 * which normally controls the Z-coordinate. However, in the wormhole of the
1864 * bridge the vehicle is in a strange state so UpdateInclination does not get
1865 * called for the wormhole of the bridge and as such the going up/down bits
1866 * would remain set. As such, this function clears those. In doing so, the call
1867 * to UpdateInclination will not update the Z-coordinate, so that has to be
1868 * done here as well.
1869 * @param gv The ground vehicle entering the bridge.
1871 template <typename T
>
1872 static void PrepareToEnterBridge(T
*gv
)
1874 if (HasBit(gv
->gv_flags
, GVF_GOINGUP_BIT
)) {
1876 ClrBit(gv
->gv_flags
, GVF_GOINGUP_BIT
);
1878 ClrBit(gv
->gv_flags
, GVF_GOINGDOWN_BIT
);
1883 * Frame when the 'enter tunnel' sound should be played. This is the second
1884 * frame on a tile, so the sound is played shortly after entering the tunnel
1885 * tile, while the vehicle is still visible.
1887 static const byte TUNNEL_SOUND_FRAME
= 1;
1890 * Frame when a vehicle should be hidden in a tunnel with a certain direction.
1891 * This differs per direction, because of visibility / bounding box issues.
1892 * Note that direction, in this case, is the direction leading into the tunnel.
1893 * When entering a tunnel, hide the vehicle when it reaches the given frame.
1894 * When leaving a tunnel, show the vehicle when it is one frame further
1895 * to the 'outside', i.e. at (TILE_SIZE-1) - (frame) + 1
1897 extern const byte _tunnel_visibility_frame
[DIAGDIR_END
] = {12, 8, 8, 12};
1899 static VehicleEnterTileStatus
VehicleEnter_TunnelBridge(Vehicle
*v
, TileIndex tile
, int x
, int y
)
1901 int z
= GetSlopePixelZ(x
, y
, true) - v
->z_pos
;
1903 if (abs(z
) > 2) return VETSB_CANNOT_ENTER
;
1904 /* Direction into the wormhole */
1905 const DiagDirection dir
= GetTunnelBridgeDirection(tile
);
1906 /* Direction of the vehicle */
1907 const DiagDirection vdir
= DirToDiagDir(v
->direction
);
1908 /* New position of the vehicle on the tile */
1909 byte pos
= (DiagDirToAxis(vdir
) == AXIS_X
? x
: y
) & TILE_UNIT_MASK
;
1910 /* Number of units moved by the vehicle since entering the tile */
1911 byte frame
= (vdir
== DIAGDIR_NE
|| vdir
== DIAGDIR_NW
) ? TILE_SIZE
- 1 - pos
: pos
;
1913 if (IsTunnel(tile
)) {
1914 if (v
->type
== VEH_TRAIN
) {
1915 Train
*t
= Train::From(v
);
1917 if (t
->track
!= TRACK_BIT_WORMHOLE
&& dir
== vdir
) {
1918 if (t
->IsFrontEngine() && frame
== TUNNEL_SOUND_FRAME
) {
1919 if (!PlayVehicleSound(t
, VSE_TUNNEL
) && RailVehInfo(t
->engine_type
)->engclass
== 0) {
1920 SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL
, v
);
1922 return VETSB_CONTINUE
;
1924 if (frame
== _tunnel_visibility_frame
[dir
]) {
1926 t
->track
= TRACK_BIT_WORMHOLE
;
1927 t
->vehstatus
|= VS_HIDDEN
;
1928 return VETSB_ENTERED_WORMHOLE
;
1932 if (dir
== ReverseDiagDir(vdir
) && frame
== TILE_SIZE
- _tunnel_visibility_frame
[dir
] && z
== 0) {
1933 /* We're at the tunnel exit ?? */
1935 t
->track
= DiagDirToDiagTrackBits(vdir
);
1937 t
->vehstatus
&= ~VS_HIDDEN
;
1938 return VETSB_ENTERED_WORMHOLE
;
1940 } else if (v
->type
== VEH_ROAD
) {
1941 RoadVehicle
*rv
= RoadVehicle::From(v
);
1944 if (rv
->state
!= RVSB_WORMHOLE
&& dir
== vdir
) {
1945 if (frame
== _tunnel_visibility_frame
[dir
]) {
1946 /* Frame should be equal to the next frame number in the RV's movement */
1947 assert(frame
== rv
->frame
+ 1);
1949 rv
->state
= RVSB_WORMHOLE
;
1950 rv
->vehstatus
|= VS_HIDDEN
;
1951 return VETSB_ENTERED_WORMHOLE
;
1953 return VETSB_CONTINUE
;
1957 /* We're at the tunnel exit ?? */
1958 if (dir
== ReverseDiagDir(vdir
) && frame
== TILE_SIZE
- _tunnel_visibility_frame
[dir
] && z
== 0) {
1960 rv
->state
= DiagDirToDiagTrackdir(vdir
);
1962 rv
->vehstatus
&= ~VS_HIDDEN
;
1963 return VETSB_ENTERED_WORMHOLE
;
1966 } else { // IsBridge(tile)
1967 if (v
->type
!= VEH_SHIP
) {
1968 /* modify speed of vehicle */
1969 uint16_t spd
= GetBridgeSpec(GetBridgeType(tile
))->speed
;
1971 if (v
->type
== VEH_ROAD
) spd
*= 2;
1972 Vehicle
*first
= v
->First();
1973 first
->cur_speed
= std::min(first
->cur_speed
, spd
);
1977 /* Vehicle enters bridge at the last frame inside this tile. */
1978 if (frame
!= TILE_SIZE
- 1) return VETSB_CONTINUE
;
1981 Train
*t
= Train::From(v
);
1982 t
->track
= TRACK_BIT_WORMHOLE
;
1983 PrepareToEnterBridge(t
);
1988 RoadVehicle
*rv
= RoadVehicle::From(v
);
1989 rv
->state
= RVSB_WORMHOLE
;
1990 PrepareToEnterBridge(rv
);
1995 Ship::From(v
)->state
= TRACK_BIT_WORMHOLE
;
1998 default: NOT_REACHED();
2000 return VETSB_ENTERED_WORMHOLE
;
2001 } else if (vdir
== ReverseDiagDir(dir
)) {
2005 Train
*t
= Train::From(v
);
2006 if (t
->track
== TRACK_BIT_WORMHOLE
) {
2007 t
->track
= DiagDirToDiagTrackBits(vdir
);
2008 return VETSB_ENTERED_WORMHOLE
;
2014 RoadVehicle
*rv
= RoadVehicle::From(v
);
2015 if (rv
->state
== RVSB_WORMHOLE
) {
2016 rv
->state
= DiagDirToDiagTrackdir(vdir
);
2018 return VETSB_ENTERED_WORMHOLE
;
2024 Ship
*ship
= Ship::From(v
);
2025 if (ship
->state
== TRACK_BIT_WORMHOLE
) {
2026 ship
->state
= DiagDirToDiagTrackBits(vdir
);
2027 return VETSB_ENTERED_WORMHOLE
;
2032 default: NOT_REACHED();
2036 return VETSB_CONTINUE
;
2039 static CommandCost
TerraformTile_TunnelBridge(TileIndex tile
, DoCommandFlag flags
, int z_new
, Slope tileh_new
)
2041 if (_settings_game
.construction
.build_on_slopes
&& AutoslopeEnabled() && IsBridge(tile
) && GetTunnelBridgeTransportType(tile
) != TRANSPORT_WATER
) {
2042 DiagDirection direction
= GetTunnelBridgeDirection(tile
);
2043 Axis axis
= DiagDirToAxis(direction
);
2046 Slope tileh_old
= GetTileSlope(tile
, &z_old
);
2048 /* Check if new slope is valid for bridges in general (so we can safely call GetBridgeFoundation()) */
2049 if ((direction
== DIAGDIR_NW
) || (direction
== DIAGDIR_NE
)) {
2050 CheckBridgeSlope(BRIDGE_PIECE_SOUTH
, axis
, &tileh_old
, &z_old
);
2051 res
= CheckBridgeSlope(BRIDGE_PIECE_SOUTH
, axis
, &tileh_new
, &z_new
);
2053 CheckBridgeSlope(BRIDGE_PIECE_NORTH
, axis
, &tileh_old
, &z_old
);
2054 res
= CheckBridgeSlope(BRIDGE_PIECE_NORTH
, axis
, &tileh_new
, &z_new
);
2057 /* Surface slope is valid and remains unchanged? */
2058 if (res
.Succeeded() && (z_old
== z_new
) && (tileh_old
== tileh_new
)) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
2061 return Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
2064 extern const TileTypeProcs _tile_type_tunnelbridge_procs
= {
2065 DrawTile_TunnelBridge
, // draw_tile_proc
2066 GetSlopePixelZ_TunnelBridge
, // get_slope_z_proc
2067 ClearTile_TunnelBridge
, // clear_tile_proc
2068 nullptr, // add_accepted_cargo_proc
2069 GetTileDesc_TunnelBridge
, // get_tile_desc_proc
2070 GetTileTrackStatus_TunnelBridge
, // get_tile_track_status_proc
2071 nullptr, // click_tile_proc
2072 nullptr, // animate_tile_proc
2073 TileLoop_TunnelBridge
, // tile_loop_proc
2074 ChangeTileOwner_TunnelBridge
, // change_tile_owner_proc
2075 nullptr, // add_produced_cargo_proc
2076 VehicleEnter_TunnelBridge
, // vehicle_enter_tile_proc
2077 GetFoundation_TunnelBridge
, // get_foundation_proc
2078 TerraformTile_TunnelBridge
, // terraform_tile_proc