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/>.
8 /** @file road_cmd.cpp Commands related to road tiles. */
12 #include "road_internal.h"
13 #include "viewport_func.h"
14 #include "command_func.h"
15 #include "company_func.h"
16 #include "pathfinder/yapf/yapf_cache.h"
17 #include "depot_base.h"
19 #include "autoslope.h"
20 #include "tunnelbridge_map.h"
21 #include "strings_func.h"
22 #include "vehicle_func.h"
23 #include "sound_func.h"
24 #include "tunnelbridge.h"
25 #include "cheat_type.h"
26 #include "effectvehicle_func.h"
27 #include "effectvehicle_base.h"
28 #include "elrail_func.h"
32 #include "company_base.h"
33 #include "core/random_func.hpp"
34 #include "core/container_func.hpp"
35 #include "newgrf_debug.h"
36 #include "newgrf_railtype.h"
37 #include "newgrf_roadtype.h"
38 #include "timer/timer_game_calendar.h"
40 #include "company_gui.h"
41 #include "road_func.h"
43 #include "landscape_cmd.h"
46 #include "table/strings.h"
47 #include "table/roadtypes.h"
49 #include "safeguards.h"
51 /** Helper type for lists/vectors of road vehicles */
52 typedef std::vector
<RoadVehicle
*> RoadVehicleList
;
54 RoadTypeInfo _roadtypes
[ROADTYPE_END
];
55 std::vector
<RoadType
> _sorted_roadtypes
;
56 RoadTypes _roadtypes_hidden_mask
;
59 * Bitmap of road/tram types.
60 * Bit if set if a roadtype is tram.
62 RoadTypes _roadtypes_type
;
65 * Reset all road type information to its default values.
69 static_assert(lengthof(_original_roadtypes
) <= lengthof(_roadtypes
));
71 auto insert
= std::copy(std::begin(_original_roadtypes
), std::end(_original_roadtypes
), std::begin(_roadtypes
));
72 std::fill(insert
, std::end(_roadtypes
), RoadTypeInfo
{});
74 _roadtypes_hidden_mask
= ROADTYPES_NONE
;
75 _roadtypes_type
= ROADTYPES_TRAM
;
78 void ResolveRoadTypeGUISprites(RoadTypeInfo
*rti
)
80 SpriteID cursors_base
= GetCustomRoadSprite(rti
, INVALID_TILE
, ROTSG_CURSORS
);
81 if (cursors_base
!= 0) {
82 rti
->gui_sprites
.build_y_road
= cursors_base
+ 0;
83 rti
->gui_sprites
.build_x_road
= cursors_base
+ 1;
84 rti
->gui_sprites
.auto_road
= cursors_base
+ 2;
85 rti
->gui_sprites
.build_depot
= cursors_base
+ 3;
86 rti
->gui_sprites
.build_tunnel
= cursors_base
+ 4;
87 rti
->gui_sprites
.convert_road
= cursors_base
+ 5;
88 rti
->cursor
.road_swne
= cursors_base
+ 6;
89 rti
->cursor
.road_nwse
= cursors_base
+ 7;
90 rti
->cursor
.autoroad
= cursors_base
+ 8;
91 rti
->cursor
.depot
= cursors_base
+ 9;
92 rti
->cursor
.tunnel
= cursors_base
+ 10;
93 rti
->cursor
.convert_road
= cursors_base
+ 11;
98 * Compare roadtypes based on their sorting order.
99 * @param first The roadtype to compare to.
100 * @param second The roadtype to compare.
101 * @return True iff the first should be sorted before the second.
103 static bool CompareRoadTypes(const RoadType
&first
, const RoadType
&second
)
105 if (RoadTypeIsRoad(first
) == RoadTypeIsRoad(second
)) {
106 return GetRoadTypeInfo(first
)->sorting_order
< GetRoadTypeInfo(second
)->sorting_order
;
108 return RoadTypeIsTram(first
) < RoadTypeIsTram(second
);
112 * Resolve sprites of custom road types
116 for (RoadType rt
= ROADTYPE_BEGIN
; rt
!= ROADTYPE_END
; rt
++) {
117 RoadTypeInfo
*rti
= &_roadtypes
[rt
];
118 ResolveRoadTypeGUISprites(rti
);
119 if (HasBit(rti
->flags
, ROTF_HIDDEN
)) SetBit(_roadtypes_hidden_mask
, rt
);
122 _sorted_roadtypes
.clear();
123 for (RoadType rt
= ROADTYPE_BEGIN
; rt
!= ROADTYPE_END
; rt
++) {
124 if (_roadtypes
[rt
].label
!= 0 && !HasBit(_roadtypes_hidden_mask
, rt
)) {
125 _sorted_roadtypes
.push_back(rt
);
128 std::sort(_sorted_roadtypes
.begin(), _sorted_roadtypes
.end(), CompareRoadTypes
);
132 * Allocate a new road type label
134 RoadType
AllocateRoadType(RoadTypeLabel label
, RoadTramType rtt
)
136 for (RoadType rt
= ROADTYPE_BEGIN
; rt
!= ROADTYPE_END
; rt
++) {
137 RoadTypeInfo
*rti
= &_roadtypes
[rt
];
139 if (rti
->label
== 0) {
140 /* Set up new road type */
141 *rti
= _original_roadtypes
[(rtt
== RTT_TRAM
) ? ROADTYPE_TRAM
: ROADTYPE_ROAD
];
143 rti
->alternate_labels
.clear();
144 rti
->flags
= ROTFB_NONE
;
145 rti
->introduction_date
= CalendarTime::INVALID_DATE
;
147 /* Make us compatible with ourself. */
148 rti
->powered_roadtypes
= (RoadTypes
)(1ULL << rt
);
150 /* We also introduce ourself. */
151 rti
->introduces_roadtypes
= (RoadTypes
)(1ULL << rt
);
153 /* Default sort order; order of allocation, but with some
154 * offsets so it's easier for NewGRF to pick a spot without
155 * changing the order of other (original) road types.
156 * The << is so you can place other roadtypes in between the
157 * other roadtypes, the 7 is to be able to place something
158 * before the first (default) road type. */
159 rti
->sorting_order
= rt
<< 2 | 7;
161 /* Set bitmap of road/tram types */
162 if (rtt
== RTT_TRAM
) {
163 SetBit(_roadtypes_type
, rt
);
165 ClrBit(_roadtypes_type
, rt
);
172 return INVALID_ROADTYPE
;
176 * Verify whether a road vehicle is available.
177 * @return \c true if at least one road vehicle is available, \c false if not
179 bool RoadVehiclesAreBuilt()
181 return !RoadVehicle::Iterate().empty();
185 * Update road infrastructure counts for a company.
186 * @param rt Road type to update count of.
187 * @param o Owner of road piece.
188 * @param count Number of road pieces to adjust.
190 void UpdateCompanyRoadInfrastructure(RoadType rt
, Owner o
, int count
)
192 if (rt
== INVALID_ROADTYPE
) return;
194 Company
*c
= Company::GetIfValid(o
);
195 if (c
== nullptr) return;
197 c
->infrastructure
.road
[rt
] += count
;
198 DirtyCompanyInfrastructureWindows(c
->index
);
201 /** Invalid RoadBits on slopes. */
202 static const RoadBits _invalid_tileh_slopes_road
[2][15] = {
203 /* The inverse of the mixable RoadBits on a leveled slope */
205 ROAD_NONE
, // SLOPE_FLAT
206 ROAD_NE
| ROAD_SE
, // SLOPE_W
207 ROAD_NE
| ROAD_NW
, // SLOPE_S
210 ROAD_NW
| ROAD_SW
, // SLOPE_E
211 ROAD_NONE
, // SLOPE_EW
214 ROAD_NONE
, // SLOPE_WSE
215 ROAD_SE
| ROAD_SW
, // SLOPE_N
218 ROAD_NONE
, // SLOPE_NS
219 ROAD_NONE
, // SLOPE_ENW
222 ROAD_NONE
, // SLOPE_SEN
223 ROAD_NONE
// SLOPE_NWS
225 /* The inverse of the allowed straight roads on a slope
226 * (with and without a foundation). */
228 ROAD_NONE
, // SLOPE_FLAT
229 ROAD_NONE
, // SLOPE_W Foundation
230 ROAD_NONE
, // SLOPE_S Foundation
233 ROAD_NONE
, // SLOPE_E Foundation
234 ROAD_ALL
, // SLOPE_EW
237 ROAD_ALL
, // SLOPE_WSE
238 ROAD_NONE
, // SLOPE_N Foundation
241 ROAD_ALL
, // SLOPE_NS
242 ROAD_ALL
, // SLOPE_ENW
245 ROAD_ALL
, // SLOPE_SEN
250 static Foundation
GetRoadFoundation(Slope tileh
, RoadBits bits
);
253 * Is it allowed to remove the given road bits from the given tile?
254 * @param tile the tile to remove the road from
255 * @param remove the roadbits that are going to be removed
256 * @param owner the actual owner of the roadbits of the tile
257 * @param rt the road type to remove the bits from
258 * @param flags command flags
259 * @param town_check Shall the town rating checked/affected
260 * @return A succeeded command when it is allowed to remove the road bits, a failed command otherwise.
262 CommandCost
CheckAllowRemoveRoad(TileIndex tile
, RoadBits remove
, Owner owner
, RoadTramType rtt
, DoCommandFlag flags
, bool town_check
)
264 if (_game_mode
== GM_EDITOR
|| remove
== ROAD_NONE
) return CommandCost();
266 /* Water can always flood and towns can always remove "normal" road pieces.
267 * Towns are not be allowed to remove non "normal" road pieces, like tram
268 * tracks as that would result in trams that cannot turn. */
269 if (_current_company
== OWNER_WATER
||
270 (rtt
== RTT_ROAD
&& !Company::IsValidID(_current_company
))) return CommandCost();
272 /* Only do the special processing if the road is owned
274 if (owner
!= OWNER_TOWN
) {
275 if (owner
== OWNER_NONE
) return CommandCost();
276 CommandCost ret
= CheckOwnership(owner
);
280 if (!town_check
) return CommandCost();
282 if (_cheats
.magic_bulldozer
.value
) return CommandCost();
284 Town
*t
= ClosestTownFromTile(tile
, UINT_MAX
);
285 if (t
== nullptr) return CommandCost();
287 /* check if you're allowed to remove the street owned by a town
288 * removal allowance depends on difficulty setting */
289 CommandCost ret
= CheckforTownRating(flags
, t
, ROAD_REMOVE
);
290 if (ret
.Failed()) return ret
;
292 /* Get a bitmask of which neighbouring roads has a tile */
293 RoadBits n
= ROAD_NONE
;
294 RoadBits present
= GetAnyRoadBits(tile
, rtt
);
295 if ((present
& ROAD_NE
) && (GetAnyRoadBits(TILE_ADDXY(tile
, -1, 0), rtt
) & ROAD_SW
)) n
|= ROAD_NE
;
296 if ((present
& ROAD_SE
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 0, 1), rtt
) & ROAD_NW
)) n
|= ROAD_SE
;
297 if ((present
& ROAD_SW
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 1, 0), rtt
) & ROAD_NE
)) n
|= ROAD_SW
;
298 if ((present
& ROAD_NW
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 0, -1), rtt
) & ROAD_SE
)) n
|= ROAD_NW
;
300 int rating_decrease
= RATING_ROAD_DOWN_STEP_EDGE
;
301 /* If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
303 if (KillFirstBit(n
) != ROAD_NONE
&& (n
& remove
) != ROAD_NONE
) {
304 /* you can remove all kind of roads with extra dynamite */
305 if (!_settings_game
.construction
.extra_dynamite
) {
306 SetDParam(0, t
->index
);
307 return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS
);
309 rating_decrease
= RATING_ROAD_DOWN_STEP_INNER
;
311 ChangeTownRating(t
, rating_decrease
, RATING_ROAD_MINIMUM
, flags
);
313 return CommandCost();
318 * Delete a piece of road.
319 * @param tile tile where to remove road from
320 * @param flags operation to perform
321 * @param pieces roadbits to remove
322 * @param rt roadtype to remove
323 * @param town_check should we check if the town allows removal?
325 static CommandCost
RemoveRoad(TileIndex tile
, DoCommandFlag flags
, RoadBits pieces
, RoadTramType rtt
, bool town_check
)
327 assert(pieces
!= ROAD_NONE
);
329 RoadType existing_rt
= MayHaveRoad(tile
) ? GetRoadType(tile
, rtt
) : INVALID_ROADTYPE
;
330 /* The tile doesn't have the given road type */
331 if (existing_rt
== INVALID_ROADTYPE
) return_cmd_error((rtt
== RTT_TRAM
) ? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
333 switch (GetTileType(tile
)) {
335 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
336 if (ret
.Failed()) return ret
;
341 if (!IsDriveThroughStopTile(tile
)) return CMD_ERROR
;
343 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
344 if (ret
.Failed()) return ret
;
348 case MP_TUNNELBRIDGE
: {
349 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) return CMD_ERROR
;
350 CommandCost ret
= TunnelBridgeIsFree(tile
, GetOtherTunnelBridgeEnd(tile
));
351 if (ret
.Failed()) return ret
;
359 CommandCost ret
= CheckAllowRemoveRoad(tile
, pieces
, GetRoadOwner(tile
, rtt
), rtt
, flags
, town_check
);
360 if (ret
.Failed()) return ret
;
362 if (!IsTileType(tile
, MP_ROAD
)) {
363 /* If it's the last roadtype, just clear the whole tile */
364 if (GetRoadType(tile
, OtherRoadTramType(rtt
)) == INVALID_ROADTYPE
) return Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
366 CommandCost
cost(EXPENSES_CONSTRUCTION
);
367 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
368 /* Removing any roadbit in the bridge axis removes the roadtype (that's the behaviour remove-long-roads needs) */
369 if ((AxisToRoadBits(DiagDirToAxis(GetTunnelBridgeDirection(tile
))) & pieces
) == ROAD_NONE
) return_cmd_error((rtt
== RTT_TRAM
) ? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
371 TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
372 /* Pay for *every* tile of the bridge or tunnel */
373 uint len
= GetTunnelBridgeLength(other_end
, tile
) + 2;
374 cost
.AddCost(len
* 2 * RoadClearCost(existing_rt
));
375 if (flags
& DC_EXEC
) {
376 /* A full diagonal road tile has two road bits. */
377 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -(int)(len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
));
379 SetRoadType(other_end
, rtt
, INVALID_ROADTYPE
);
380 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
382 /* If the owner of the bridge sells all its road, also move the ownership
383 * to the owner of the other roadtype, unless the bridge owner is a town. */
384 Owner other_owner
= GetRoadOwner(tile
, OtherRoadTramType(rtt
));
385 if (!IsTileOwner(tile
, other_owner
) && !IsTileOwner(tile
, OWNER_TOWN
)) {
386 SetTileOwner(tile
, other_owner
);
387 SetTileOwner(other_end
, other_owner
);
390 /* Mark tiles dirty that have been repaved */
391 if (IsBridge(tile
)) {
392 MarkBridgeDirty(tile
);
394 MarkTileDirtyByTile(tile
);
395 MarkTileDirtyByTile(other_end
);
399 assert(IsDriveThroughStopTile(tile
));
400 cost
.AddCost(RoadClearCost(existing_rt
) * 2);
401 if (flags
& DC_EXEC
) {
402 /* A full diagonal road tile has two road bits. */
403 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -2);
404 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
405 MarkTileDirtyByTile(tile
);
411 switch (GetRoadTileType(tile
)) {
412 case ROAD_TILE_NORMAL
: {
413 Slope tileh
= GetTileSlope(tile
);
415 /* Steep slopes behave the same as slopes with one corner raised. */
416 if (IsSteepSlope(tileh
)) {
417 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
420 RoadBits present
= GetRoadBits(tile
, rtt
);
421 const RoadBits other
= GetRoadBits(tile
, OtherRoadTramType(rtt
));
422 const Foundation f
= GetRoadFoundation(tileh
, present
);
424 if (HasRoadWorks(tile
) && _current_company
!= OWNER_WATER
) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS
);
426 /* Autocomplete to a straight road
427 * @li if the bits of the other roadtypes result in another foundation
428 * @li if build on slopes is disabled */
429 if ((IsStraightRoad(other
) && (other
& _invalid_tileh_slopes_road
[0][tileh
& SLOPE_ELEVATED
]) != ROAD_NONE
) ||
430 (tileh
!= SLOPE_FLAT
&& !_settings_game
.construction
.build_on_slopes
)) {
431 pieces
|= MirrorRoadBits(pieces
);
434 /* limit the bits to delete to the existing bits. */
436 if (pieces
== ROAD_NONE
) return_cmd_error((rtt
== RTT_TRAM
) ? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
438 /* Now set present what it will be after the remove */
441 /* Check for invalid RoadBit combinations on slopes */
442 if (tileh
!= SLOPE_FLAT
&& present
!= ROAD_NONE
&&
443 (present
& _invalid_tileh_slopes_road
[0][tileh
& SLOPE_ELEVATED
]) == present
) {
447 if (flags
& DC_EXEC
) {
448 if (HasRoadWorks(tile
)) {
449 /* flooding tile with road works, don't forget to remove the effect vehicle too */
450 assert(_current_company
== OWNER_WATER
);
451 for (EffectVehicle
*v
: EffectVehicle::Iterate()) {
452 if (TileVirtXY(v
->x_pos
, v
->y_pos
) == tile
) {
458 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -(int)CountBits(pieces
));
460 if (present
== ROAD_NONE
) {
461 /* No other road type, just clear tile. */
462 if (GetRoadType(tile
, OtherRoadTramType(rtt
)) == INVALID_ROADTYPE
) {
463 /* Includes MarkTileDirtyByTile() */
466 if (rtt
== RTT_ROAD
&& IsRoadOwner(tile
, rtt
, OWNER_TOWN
)) {
467 /* Update nearest-town index */
468 const Town
*town
= CalcClosestTownFromTile(tile
);
469 SetTownIndex(tile
, town
== nullptr ? INVALID_TOWN
: town
->index
);
471 if (rtt
== RTT_ROAD
) SetDisallowedRoadDirections(tile
, DRD_NONE
);
472 SetRoadBits(tile
, ROAD_NONE
, rtt
);
473 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
474 MarkTileDirtyByTile(tile
);
477 /* When bits are removed, you *always* end up with something that
478 * is not a complete straight road tile. However, trams do not have
479 * onewayness, so they cannot remove it either. */
480 if (rtt
== RTT_ROAD
) SetDisallowedRoadDirections(tile
, DRD_NONE
);
481 SetRoadBits(tile
, present
, rtt
);
482 MarkTileDirtyByTile(tile
);
486 CommandCost
cost(EXPENSES_CONSTRUCTION
, CountBits(pieces
) * RoadClearCost(existing_rt
));
487 /* If we build a foundation we have to pay for it. */
488 if (f
== FOUNDATION_NONE
&& GetRoadFoundation(tileh
, present
) != FOUNDATION_NONE
) cost
.AddCost(_price
[PR_BUILD_FOUNDATION
]);
493 case ROAD_TILE_CROSSING
: {
494 if (pieces
& ComplementRoadBits(GetCrossingRoadBits(tile
))) {
498 if (flags
& DC_EXEC
) {
499 UpdateAdjacentLevelCrossingTilesOnLevelCrossingRemoval(tile
, GetCrossingRoadAxis(tile
));
501 /* A full diagonal road tile has two road bits. */
502 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -2);
504 Track railtrack
= GetCrossingRailTrack(tile
);
505 if (GetRoadType(tile
, OtherRoadTramType(rtt
)) == INVALID_ROADTYPE
) {
506 TrackBits tracks
= GetCrossingRailBits(tile
);
507 bool reserved
= HasCrossingReservation(tile
);
508 MakeRailNormal(tile
, GetTileOwner(tile
), tracks
, GetRailType(tile
));
509 if (reserved
) SetTrackReservation(tile
, tracks
);
511 /* Update rail count for level crossings. The plain track should still be accounted
512 * for, so only subtract the difference to the level crossing cost. */
513 Company
*c
= Company::GetIfValid(GetTileOwner(tile
));
515 c
->infrastructure
.rail
[GetRailType(tile
)] -= LEVELCROSSING_TRACKBIT_FACTOR
- 1;
516 DirtyCompanyInfrastructureWindows(c
->index
);
519 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
521 MarkTileDirtyByTile(tile
);
522 YapfNotifyTrackLayoutChange(tile
, railtrack
);
524 return CommandCost(EXPENSES_CONSTRUCTION
, RoadClearCost(existing_rt
) * 2);
528 case ROAD_TILE_DEPOT
:
535 * Calculate the costs for roads on slopes
536 * Aside modify the RoadBits to fit on the slopes
538 * @note The RoadBits are modified too!
539 * @param tileh The current slope
540 * @param pieces The RoadBits we want to add
541 * @param existing The existent RoadBits of the current type
542 * @param other The other existent RoadBits
543 * @return The costs for these RoadBits on this slope
545 static CommandCost
CheckRoadSlope(Slope tileh
, RoadBits
*pieces
, RoadBits existing
, RoadBits other
)
547 /* Remove already build pieces */
548 CLRBITS(*pieces
, existing
);
550 /* If we can't build anything stop here */
551 if (*pieces
== ROAD_NONE
) return CMD_ERROR
;
553 /* All RoadBit combos are valid on flat land */
554 if (tileh
== SLOPE_FLAT
) return CommandCost();
556 /* Steep slopes behave the same as slopes with one corner raised. */
557 if (IsSteepSlope(tileh
)) {
558 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
561 /* Save the merge of all bits of the current type */
562 RoadBits type_bits
= existing
| *pieces
;
564 /* Roads on slopes */
565 if (_settings_game
.construction
.build_on_slopes
&& (_invalid_tileh_slopes_road
[0][tileh
] & (other
| type_bits
)) == ROAD_NONE
) {
567 /* If we add leveling we've got to pay for it */
568 if ((other
| existing
) == ROAD_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
570 return CommandCost();
573 /* Autocomplete uphill roads */
574 *pieces
|= MirrorRoadBits(*pieces
);
575 type_bits
= existing
| *pieces
;
578 if (IsStraightRoad(type_bits
) && (other
== type_bits
|| other
== ROAD_NONE
) &&
579 (_invalid_tileh_slopes_road
[1][tileh
] & (other
| type_bits
)) == ROAD_NONE
) {
581 /* Slopes with foundation ? */
582 if (IsSlopeWithOneCornerRaised(tileh
)) {
584 /* Prevent build on slopes if it isn't allowed */
585 if (_settings_game
.construction
.build_on_slopes
) {
587 /* If we add foundation we've got to pay for it */
588 if ((other
| existing
) == ROAD_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
590 return CommandCost();
593 if (HasExactlyOneBit(existing
) && GetRoadFoundation(tileh
, existing
) == FOUNDATION_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
594 return CommandCost();
601 * Build a piece of road.
602 * @param flags operation to perform
603 * @param tile tile where to build road
604 * @param pieces road pieces to build (RoadBits)
605 * @param rt road type
606 * @param toggle_drd disallowed directions to toggle
607 * @param town_id the town that is building the road (0 if not applicable)
608 * @return the cost of this operation or an error
610 CommandCost
CmdBuildRoad(DoCommandFlag flags
, TileIndex tile
, RoadBits pieces
, RoadType rt
, DisallowedRoadDirections toggle_drd
, TownID town_id
)
612 CompanyID company
= _current_company
;
613 CommandCost
cost(EXPENSES_CONSTRUCTION
);
615 RoadBits existing
= ROAD_NONE
;
616 RoadBits other_bits
= ROAD_NONE
;
618 /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
619 * if a non-company is building the road */
620 if ((Company::IsValidID(company
) && town_id
!= 0) || (company
== OWNER_TOWN
&& !Town::IsValidID(town_id
)) || (company
== OWNER_DEITY
&& town_id
!= 0)) return CMD_ERROR
;
621 if (company
!= OWNER_TOWN
) {
622 const Town
*town
= CalcClosestTownFromTile(tile
);
623 town_id
= (town
!= nullptr) ? town
->index
: INVALID_TOWN
;
625 if (company
== OWNER_DEITY
) {
626 company
= OWNER_TOWN
;
628 /* If we are not within a town, we are not owned by the town */
629 if (town
== nullptr || DistanceSquare(tile
, town
->xy
) > town
->cache
.squared_town_zone_radius
[HZB_TOWN_EDGE
]) {
630 company
= OWNER_NONE
;
635 /* do not allow building 'zero' road bits, code wouldn't handle it */
636 if (pieces
== ROAD_NONE
|| !IsValidRoadBits(pieces
) || !IsValidDisallowedRoadDirections(toggle_drd
)) return CMD_ERROR
;
637 if (!ValParamRoadType(rt
)) return CMD_ERROR
;
639 Slope tileh
= GetTileSlope(tile
);
640 RoadTramType rtt
= GetRoadTramType(rt
);
642 bool need_to_clear
= false;
643 switch (GetTileType(tile
)) {
645 switch (GetRoadTileType(tile
)) {
646 case ROAD_TILE_NORMAL
: {
647 if (HasRoadWorks(tile
)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS
);
649 other_bits
= GetRoadBits(tile
, OtherRoadTramType(rtt
));
650 if (!HasTileRoadType(tile
, rtt
)) break;
652 existing
= GetRoadBits(tile
, rtt
);
653 bool crossing
= !IsStraightRoad(existing
| pieces
);
654 if (rtt
== RTT_ROAD
&& (GetDisallowedRoadDirections(tile
) != DRD_NONE
|| toggle_drd
!= DRD_NONE
) && crossing
) {
655 /* Junctions cannot be one-way */
656 return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION
);
658 if ((existing
& pieces
) == pieces
) {
659 /* We only want to set the (dis)allowed road directions */
660 if (toggle_drd
!= DRD_NONE
&& rtt
== RTT_ROAD
) {
661 if (crossing
) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION
);
663 Owner owner
= GetRoadOwner(tile
, rtt
);
664 if (owner
!= OWNER_NONE
) {
665 CommandCost ret
= CheckOwnership(owner
, tile
);
666 if (ret
.Failed()) return ret
;
669 DisallowedRoadDirections dis_existing
= GetDisallowedRoadDirections(tile
);
670 DisallowedRoadDirections dis_new
= dis_existing
^ toggle_drd
;
672 /* We allow removing disallowed directions to break up
673 * deadlocks, but adding them can break articulated
674 * vehicles. As such, only when less is disallowed,
675 * i.e. bits are removed, we skip the vehicle check. */
676 if (CountBits(dis_existing
) <= CountBits(dis_new
)) {
677 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
678 if (ret
.Failed()) return ret
;
681 /* Ignore half built tiles */
682 if ((flags
& DC_EXEC
) && IsStraightRoad(existing
)) {
683 SetDisallowedRoadDirections(tile
, dis_new
);
684 MarkTileDirtyByTile(tile
);
686 return CommandCost();
688 return_cmd_error(STR_ERROR_ALREADY_BUILT
);
690 /* Disallow breaking end-of-line of someone else
691 * so trams can still reverse on this tile. */
692 if (rtt
== RTT_TRAM
&& HasExactlyOneBit(existing
)) {
693 Owner owner
= GetRoadOwner(tile
, rtt
);
694 if (Company::IsValidID(owner
)) {
695 CommandCost ret
= CheckOwnership(owner
);
696 if (ret
.Failed()) return ret
;
702 case ROAD_TILE_CROSSING
:
703 if (RoadNoLevelCrossing(rt
)) {
704 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_ROAD
);
707 other_bits
= GetCrossingRoadBits(tile
);
708 if (pieces
& ComplementRoadBits(other_bits
)) goto do_clear
;
709 pieces
= other_bits
; // we need to pay for both roadbits
711 if (HasTileRoadType(tile
, rtt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
714 case ROAD_TILE_DEPOT
:
715 if ((GetAnyRoadBits(tile
, rtt
) & pieces
) == pieces
) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
718 default: NOT_REACHED();
723 if (IsSteepSlope(tileh
)) {
724 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
727 /* Level crossings may only be built on these slopes */
728 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES
, tileh
)) {
729 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
732 if (!_settings_game
.construction
.crossing_with_competitor
&& company
!= OWNER_TOWN
&& company
!= OWNER_DEITY
) {
733 CommandCost ret
= CheckTileOwnership(tile
);
734 if (ret
.Failed()) return ret
;
737 if (GetRailTileType(tile
) != RAIL_TILE_NORMAL
) goto do_clear
;
739 if (RoadNoLevelCrossing(rt
)) {
740 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_ROAD
);
743 if (RailNoLevelCrossings(GetRailType(tile
))) {
744 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_RAIL
);
748 switch (GetTrackBits(tile
)) {
750 if (pieces
& ROAD_X
) goto do_clear
;
755 if (pieces
& ROAD_Y
) goto do_clear
;
759 default: goto do_clear
;
762 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
763 if (ret
.Failed()) return ret
;
765 if (flags
& DC_EXEC
) {
766 Track railtrack
= AxisToTrack(OtherAxis(roaddir
));
767 YapfNotifyTrackLayoutChange(tile
, railtrack
);
768 /* Update company infrastructure counts. A level crossing has two road bits. */
769 UpdateCompanyRoadInfrastructure(rt
, company
, 2);
771 /* Update rail count for level crossings. The plain track is already
772 * counted, so only add the difference to the level crossing cost. */
773 Company
*c
= Company::GetIfValid(GetTileOwner(tile
));
775 c
->infrastructure
.rail
[GetRailType(tile
)] += LEVELCROSSING_TRACKBIT_FACTOR
- 1;
776 DirtyCompanyInfrastructureWindows(c
->index
);
779 /* Always add road to the roadtypes (can't draw without it) */
780 bool reserved
= HasBit(GetRailReservationTrackBits(tile
), railtrack
);
781 MakeRoadCrossing(tile
, company
, company
, GetTileOwner(tile
), roaddir
, GetRailType(tile
), rtt
== RTT_ROAD
? rt
: INVALID_ROADTYPE
, (rtt
== RTT_TRAM
) ? rt
: INVALID_ROADTYPE
, town_id
);
782 SetCrossingReservation(tile
, reserved
);
783 UpdateLevelCrossing(tile
, false);
784 MarkDirtyAdjacentLevelCrossingTiles(tile
, GetCrossingRoadAxis(tile
));
785 MarkTileDirtyByTile(tile
);
787 return CommandCost(EXPENSES_CONSTRUCTION
, 2 * RoadBuildCost(rt
));
791 if ((GetAnyRoadBits(tile
, rtt
) & pieces
) == pieces
) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
792 if (!IsDriveThroughStopTile(tile
)) goto do_clear
;
794 RoadBits curbits
= AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile
)));
795 if (pieces
& ~curbits
) goto do_clear
;
796 pieces
= curbits
; // we need to pay for both roadbits
798 if (HasTileRoadType(tile
, rtt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
802 case MP_TUNNELBRIDGE
: {
803 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) goto do_clear
;
804 /* Only allow building the outern roadbit, so building long roads stops at existing bridges */
805 if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile
))) != pieces
) goto do_clear
;
806 if (HasTileRoadType(tile
, rtt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
807 /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
808 CommandCost ret
= TunnelBridgeIsFree(tile
, GetOtherTunnelBridgeEnd(tile
));
809 if (ret
.Failed()) return ret
;
815 need_to_clear
= true;
821 CommandCost ret
= Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
822 if (ret
.Failed()) return ret
;
826 if (other_bits
!= pieces
) {
827 /* Check the foundation/slopes when adding road/tram bits */
828 CommandCost ret
= CheckRoadSlope(tileh
, &pieces
, existing
, other_bits
);
829 /* Return an error if we need to build a foundation (ret != 0) but the
830 * current setting is turned off */
831 if (ret
.Failed() || (ret
.GetCost() != 0 && !_settings_game
.construction
.build_on_slopes
)) {
832 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
837 if (!need_to_clear
) {
838 if (IsTileType(tile
, MP_ROAD
)) {
839 /* Don't put the pieces that already exist */
840 pieces
&= ComplementRoadBits(existing
);
842 /* Check if new road bits will have the same foundation as other existing road types */
843 if (IsNormalRoad(tile
)) {
844 Slope slope
= GetTileSlope(tile
);
845 Foundation found_new
= GetRoadFoundation(slope
, pieces
| existing
);
847 RoadBits bits
= GetRoadBits(tile
, OtherRoadTramType(rtt
));
848 /* do not check if there are not road bits of given type */
849 if (bits
!= ROAD_NONE
&& GetRoadFoundation(slope
, bits
) != found_new
) {
850 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
855 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
856 if (ret
.Failed()) return ret
;
858 if (IsNormalRoadTile(tile
)) {
859 /* If the road types don't match, try to convert only if vehicles of
860 * the new road type are not powered on the present road type and vehicles of
861 * the present road type are powered on the new road type. */
862 RoadType existing_rt
= GetRoadType(tile
, rtt
);
863 if (existing_rt
!= INVALID_ROADTYPE
&& existing_rt
!= rt
) {
864 if (HasPowerOnRoad(rt
, existing_rt
)) {
866 } else if (HasPowerOnRoad(existing_rt
, rt
)) {
867 ret
= Command
<CMD_CONVERT_ROAD
>::Do(flags
, tile
, tile
, rt
);
868 if (ret
.Failed()) return ret
;
877 uint num_pieces
= (!need_to_clear
&& IsTileType(tile
, MP_TUNNELBRIDGE
)) ?
878 /* There are 2 pieces on *every* tile of the bridge or tunnel */
879 2 * (GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile
), tile
) + 2) :
883 cost
.AddCost(num_pieces
* RoadBuildCost(rt
));
885 if (flags
& DC_EXEC
) {
886 switch (GetTileType(tile
)) {
888 RoadTileType rttype
= GetRoadTileType(tile
);
889 if (existing
== ROAD_NONE
|| rttype
== ROAD_TILE_CROSSING
) {
890 SetRoadType(tile
, rtt
, rt
);
891 SetRoadOwner(tile
, rtt
, company
);
892 if (rtt
== RTT_ROAD
) SetTownIndex(tile
, town_id
);
894 if (rttype
!= ROAD_TILE_CROSSING
) SetRoadBits(tile
, existing
| pieces
, rtt
);
898 case MP_TUNNELBRIDGE
: {
899 TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
901 SetRoadType(other_end
, rtt
, rt
);
902 SetRoadType(tile
, rtt
, rt
);
903 SetRoadOwner(other_end
, rtt
, company
);
904 SetRoadOwner(tile
, rtt
, company
);
906 /* Mark tiles dirty that have been repaved */
907 if (IsBridge(tile
)) {
908 MarkBridgeDirty(tile
);
910 MarkTileDirtyByTile(other_end
);
911 MarkTileDirtyByTile(tile
);
917 assert(IsDriveThroughStopTile(tile
));
918 SetRoadType(tile
, rtt
, rt
);
919 SetRoadOwner(tile
, rtt
, company
);
924 MakeRoadNormal(tile
, pieces
, (rtt
== RTT_ROAD
) ? rt
: INVALID_ROADTYPE
, (rtt
== RTT_TRAM
) ? rt
: INVALID_ROADTYPE
, town_id
, company
, company
);
928 /* Update company infrastructure count. */
929 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) num_pieces
*= TUNNELBRIDGE_TRACKBIT_FACTOR
;
930 UpdateCompanyRoadInfrastructure(rt
, GetRoadOwner(tile
, rtt
), num_pieces
);
932 if (rtt
== RTT_ROAD
&& IsNormalRoadTile(tile
)) {
934 SetDisallowedRoadDirections(tile
, IsStraightRoad(existing
) ?
935 GetDisallowedRoadDirections(tile
) ^ toggle_drd
: DRD_NONE
);
938 MarkTileDirtyByTile(tile
);
944 * Checks whether a road or tram connection can be found when building a new road or tram.
945 * @param tile Tile at which the road being built will end.
946 * @param rt Roadtype of the road being built.
947 * @param dir Direction that the road is following.
948 * @return True if the next tile at dir direction is suitable for being connected directly by a second roadbit at the end of the road being built.
950 static bool CanConnectToRoad(TileIndex tile
, RoadType rt
, DiagDirection dir
)
952 tile
+= TileOffsByDiagDir(dir
);
953 if (!IsValidTile(tile
) || !MayHaveRoad(tile
)) return false;
955 RoadTramType rtt
= GetRoadTramType(rt
);
956 RoadType existing
= GetRoadType(tile
, rtt
);
957 if (existing
== INVALID_ROADTYPE
) return false;
958 if (!HasPowerOnRoad(existing
, rt
) && !HasPowerOnRoad(rt
, existing
)) return false;
960 RoadBits bits
= GetAnyRoadBits(tile
, rtt
, false);
961 return (bits
& DiagDirToRoadBits(ReverseDiagDir(dir
))) != 0;
965 * Build a long piece of road.
966 * @param flags operation to perform
967 * @param end_tile end tile of drag
968 * @param start_tile start tile of drag
969 * @param rt road type
970 * @param axis direction
971 * @param drd set road direction
972 * @param start_half start tile starts in the 2nd half of tile (p2 & 1). Only used if \c is_ai is set or if we are building a single tile
973 * @param end_half end tile starts in the 2nd half of tile (p2 & 2). Only used if \c is_ai is set or if we are building a single tile
974 * @param is_ai defines two different behaviors for this command:
975 * - false = Build up to an obstacle. Do not build the first and last roadbits unless they can be connected to something, or if we are building a single tile
976 * - true = Fail if an obstacle is found. Always take into account start_half and end_half. This behavior is used for scripts
977 * @return the cost of this operation or an error
979 CommandCost
CmdBuildLongRoad(DoCommandFlag flags
, TileIndex end_tile
, TileIndex start_tile
, RoadType rt
, Axis axis
, DisallowedRoadDirections drd
, bool start_half
, bool end_half
, bool is_ai
)
981 if (start_tile
>= Map::Size()) return CMD_ERROR
;
983 if (!ValParamRoadType(rt
) || !IsValidAxis(axis
) || !IsValidDisallowedRoadDirections(drd
)) return CMD_ERROR
;
985 /* Only drag in X or Y direction dictated by the direction variable */
986 if (axis
== AXIS_X
&& TileY(start_tile
) != TileY(end_tile
)) return CMD_ERROR
; // x-axis
987 if (axis
== AXIS_Y
&& TileX(start_tile
) != TileX(end_tile
)) return CMD_ERROR
; // y-axis
989 DiagDirection dir
= AxisToDiagDir(axis
);
991 /* Swap direction, also the half-tile drag vars. */
992 if (start_tile
> end_tile
|| (start_tile
== end_tile
&& start_half
)) {
993 dir
= ReverseDiagDir(dir
);
994 start_half
= !start_half
;
995 end_half
= !end_half
;
996 if (drd
== DRD_NORTHBOUND
|| drd
== DRD_SOUTHBOUND
) drd
^= DRD_BOTH
;
999 /* On the X-axis, we have to swap the initial bits, so they
1000 * will be interpreted correctly in the GTTS. Furthermore
1001 * when you just 'click' on one tile to build them. */
1002 if ((drd
== DRD_NORTHBOUND
|| drd
== DRD_SOUTHBOUND
) && (axis
== AXIS_Y
) == (start_tile
== end_tile
&& start_half
== end_half
)) drd
^= DRD_BOTH
;
1004 CommandCost
cost(EXPENSES_CONSTRUCTION
);
1005 CommandCost last_error
= CMD_ERROR
;
1006 TileIndex tile
= start_tile
;
1007 bool had_bridge
= false;
1008 bool had_tunnel
= false;
1009 bool had_success
= false;
1010 bool under_tunnelbridge
= false;
1012 /* Start tile is the first tile clicked by the user. */
1014 /* Don't try to place road between tunnelbridge ends */
1015 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
1016 under_tunnelbridge
= !under_tunnelbridge
;
1017 } else if (!under_tunnelbridge
) {
1018 RoadBits bits
= AxisToRoadBits(axis
);
1020 /* Determine which road parts should be built. */
1021 if (!is_ai
&& start_tile
!= end_tile
) {
1022 /* Only build the first and last roadbit if they can connect to something. */
1023 if (tile
== end_tile
&& !CanConnectToRoad(tile
, rt
, dir
)) {
1024 bits
= DiagDirToRoadBits(ReverseDiagDir(dir
));
1025 } else if (tile
== start_tile
&& !CanConnectToRoad(tile
, rt
, ReverseDiagDir(dir
))) {
1026 bits
= DiagDirToRoadBits(dir
);
1029 /* Road parts only have to be built at the start tile or at the end tile. */
1030 if (tile
== end_tile
&& !end_half
) bits
&= DiagDirToRoadBits(ReverseDiagDir(dir
));
1031 if (tile
== start_tile
&& start_half
) bits
&= DiagDirToRoadBits(dir
);
1034 CommandCost ret
= Command
<CMD_BUILD_ROAD
>::Do(flags
, tile
, bits
, rt
, drd
, 0);
1037 if (last_error
.GetErrorMessage() != STR_ERROR_ALREADY_BUILT
) {
1038 if (is_ai
) return last_error
;
1039 if (had_success
) break; // Keep going if we haven't constructed any road yet, skipping the start of the drag
1043 /* Only pay for the upgrade on one side of the bridges and tunnels */
1044 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
1045 if (IsBridge(tile
)) {
1046 if (!had_bridge
|| GetTunnelBridgeDirection(tile
) == dir
) {
1050 } else { // IsTunnel(tile)
1051 if (!had_tunnel
|| GetTunnelBridgeDirection(tile
) == dir
) {
1062 if (tile
== end_tile
) break;
1064 tile
+= TileOffsByDiagDir(dir
);
1067 return had_success
? cost
: last_error
;
1071 * Remove a long piece of road.
1072 * @param flags operation to perform
1073 * @param end_tile end tile of drag
1074 * @param start_tile start tile of drag
1075 * @param rt road type
1076 * @param axis direction
1077 * @param start_half start tile starts in the 2nd half of tile
1078 * @param end_half end tile starts in the 2nd half of tile (p2 & 2)
1079 * @return the cost of this operation or an error
1081 std::tuple
<CommandCost
, Money
> CmdRemoveLongRoad(DoCommandFlag flags
, TileIndex end_tile
, TileIndex start_tile
, RoadType rt
, Axis axis
, bool start_half
, bool end_half
)
1083 CommandCost
cost(EXPENSES_CONSTRUCTION
);
1085 if (start_tile
>= Map::Size()) return { CMD_ERROR
, 0 };
1086 if (!ValParamRoadType(rt
) || !IsValidAxis(axis
)) return { CMD_ERROR
, 0 };
1088 /* Only drag in X or Y direction dictated by the direction variable */
1089 if (axis
== AXIS_X
&& TileY(start_tile
) != TileY(end_tile
)) return { CMD_ERROR
, 0 }; // x-axis
1090 if (axis
== AXIS_Y
&& TileX(start_tile
) != TileX(end_tile
)) return { CMD_ERROR
, 0 }; // y-axis
1092 /* Swap start and ending tile, also the half-tile drag vars. */
1093 if (start_tile
> end_tile
|| (start_tile
== end_tile
&& start_half
)) {
1094 std::swap(start_tile
, end_tile
);
1095 std::swap(start_half
, end_half
);
1098 Money money_available
= GetAvailableMoneyForCommand();
1099 Money money_spent
= 0;
1100 TileIndex tile
= start_tile
;
1101 CommandCost last_error
= CMD_ERROR
;
1102 bool had_success
= false;
1103 /* Start tile is the small number. */
1105 RoadBits bits
= AxisToRoadBits(axis
);
1107 if (tile
== end_tile
&& !end_half
) bits
&= ROAD_NW
| ROAD_NE
;
1108 if (tile
== start_tile
&& start_half
) bits
&= ROAD_SE
| ROAD_SW
;
1110 /* try to remove the halves. */
1112 RoadTramType rtt
= GetRoadTramType(rt
);
1113 CommandCost ret
= RemoveRoad(tile
, flags
& ~DC_EXEC
, bits
, rtt
, true);
1114 if (ret
.Succeeded()) {
1115 if (flags
& DC_EXEC
) {
1116 money_spent
+= ret
.GetCost();
1117 if (money_spent
> 0 && money_spent
> money_available
) {
1118 return { cost
, std::get
<0>(Command
<CMD_REMOVE_LONG_ROAD
>::Do(flags
& ~DC_EXEC
, end_tile
, start_tile
, rt
, axis
, start_half
, end_half
)).GetCost() };
1120 RemoveRoad(tile
, flags
, bits
, rtt
, false);
1125 /* Some errors are more equal than others. */
1126 switch (last_error
.GetErrorMessage()) {
1127 case STR_ERROR_OWNED_BY
:
1128 case STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS
:
1136 if (tile
== end_tile
) break;
1138 tile
+= (axis
== AXIS_Y
) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
1141 return { had_success
? cost
: last_error
, 0 };
1145 * Build a road depot.
1146 * @param tile tile where to build the depot
1147 * @param flags operation to perform
1148 * @param rt road type
1149 * @param dir entrance direction
1150 * @return the cost of this operation or an error
1152 * @todo When checking for the tile slope,
1153 * distinguish between "Flat land required" and "land sloped in wrong direction"
1155 CommandCost
CmdBuildRoadDepot(DoCommandFlag flags
, TileIndex tile
, RoadType rt
, DiagDirection dir
)
1157 if (!ValParamRoadType(rt
) || !IsValidDiagDirection(dir
)) return CMD_ERROR
;
1159 CommandCost
cost(EXPENSES_CONSTRUCTION
);
1161 Slope tileh
= GetTileSlope(tile
);
1162 if (tileh
!= SLOPE_FLAT
) {
1163 if (!_settings_game
.construction
.build_on_slopes
|| !CanBuildDepotByTileh(dir
, tileh
)) {
1164 return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED
);
1166 cost
.AddCost(_price
[PR_BUILD_FOUNDATION
]);
1169 /* Allow the user to rotate the depot instead of having to destroy it and build it again */
1170 bool rotate_existing_depot
= false;
1171 if (IsRoadDepotTile(tile
) && (HasRoadTypeTram(tile
) ? rt
== GetRoadTypeTram(tile
) : rt
== GetRoadTypeRoad(tile
)))
1173 CommandCost ret
= CheckTileOwnership(tile
);
1174 if (ret
.Failed()) return ret
;
1176 if (dir
== GetRoadDepotDirection(tile
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
1178 ret
= EnsureNoVehicleOnGround(tile
);
1179 if (ret
.Failed()) return ret
;
1181 rotate_existing_depot
= true;
1184 if (!rotate_existing_depot
) {
1185 cost
.AddCost(Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
));
1186 if (cost
.Failed()) return cost
;
1188 if (IsBridgeAbove(tile
)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
1190 if (!Depot::CanAllocateItem()) return CMD_ERROR
;
1193 if (flags
& DC_EXEC
) {
1194 if (rotate_existing_depot
) {
1195 SetRoadDepotExitDirection(tile
, dir
);
1197 Depot
*dep
= new Depot(tile
);
1198 dep
->build_date
= TimerGameCalendar::date
;
1199 MakeRoadDepot(tile
, _current_company
, dep
->index
, dir
, rt
);
1200 MakeDefaultName(dep
);
1202 /* A road depot has two road bits. */
1203 UpdateCompanyRoadInfrastructure(rt
, _current_company
, ROAD_DEPOT_TRACKBIT_FACTOR
);
1206 MarkTileDirtyByTile(tile
);
1209 cost
.AddCost(_price
[PR_BUILD_DEPOT_ROAD
]);
1213 static CommandCost
RemoveRoadDepot(TileIndex tile
, DoCommandFlag flags
)
1215 if (_current_company
!= OWNER_WATER
) {
1216 CommandCost ret
= CheckTileOwnership(tile
);
1217 if (ret
.Failed()) return ret
;
1220 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
1221 if (ret
.Failed()) return ret
;
1223 if (flags
& DC_EXEC
) {
1224 Company
*c
= Company::GetIfValid(GetTileOwner(tile
));
1226 /* A road depot has two road bits. */
1227 RoadType rt
= GetRoadTypeRoad(tile
);
1228 if (rt
== INVALID_ROADTYPE
) rt
= GetRoadTypeTram(tile
);
1229 c
->infrastructure
.road
[rt
] -= ROAD_DEPOT_TRACKBIT_FACTOR
;
1230 DirtyCompanyInfrastructureWindows(c
->index
);
1233 delete Depot::GetByTile(tile
);
1234 DoClearSquare(tile
);
1237 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_CLEAR_DEPOT_ROAD
]);
1240 static CommandCost
ClearTile_Road(TileIndex tile
, DoCommandFlag flags
)
1242 switch (GetRoadTileType(tile
)) {
1243 case ROAD_TILE_NORMAL
: {
1244 RoadBits b
= GetAllRoadBits(tile
);
1246 /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
1247 if ((HasExactlyOneBit(b
) && GetRoadBits(tile
, RTT_TRAM
) == ROAD_NONE
) || !(flags
& DC_AUTO
)) {
1248 CommandCost
ret(EXPENSES_CONSTRUCTION
);
1249 for (RoadTramType rtt
: _roadtramtypes
) {
1250 if (!MayHaveRoad(tile
) || GetRoadType(tile
, rtt
) == INVALID_ROADTYPE
) continue;
1252 CommandCost tmp_ret
= RemoveRoad(tile
, flags
, GetRoadBits(tile
, rtt
), rtt
, true);
1253 if (tmp_ret
.Failed()) return tmp_ret
;
1254 ret
.AddCost(tmp_ret
);
1258 return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST
);
1261 case ROAD_TILE_CROSSING
: {
1262 CommandCost
ret(EXPENSES_CONSTRUCTION
);
1264 if (flags
& DC_AUTO
) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST
);
1266 /* Must iterate over the roadtypes in a reverse manner because
1267 * tram tracks must be removed before the road bits. */
1268 for (RoadTramType rtt
: { RTT_TRAM
, RTT_ROAD
}) {
1269 if (!MayHaveRoad(tile
) || GetRoadType(tile
, rtt
) == INVALID_ROADTYPE
) continue;
1271 CommandCost tmp_ret
= RemoveRoad(tile
, flags
, GetCrossingRoadBits(tile
), rtt
, true);
1272 if (tmp_ret
.Failed()) return tmp_ret
;
1273 ret
.AddCost(tmp_ret
);
1276 if (flags
& DC_EXEC
) {
1277 Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
1283 case ROAD_TILE_DEPOT
:
1284 if (flags
& DC_AUTO
) {
1285 return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED
);
1287 return RemoveRoadDepot(tile
, flags
);
1292 struct DrawRoadTileStruct
{
1298 #include "table/road_land.h"
1301 * Get the foundationtype of a RoadBits Slope combination
1303 * @param tileh The Slope part
1304 * @param bits The RoadBits part
1305 * @return The resulting Foundation
1307 static Foundation
GetRoadFoundation(Slope tileh
, RoadBits bits
)
1309 /* Flat land and land without a road doesn't require a foundation */
1310 if (tileh
== SLOPE_FLAT
|| bits
== ROAD_NONE
) return FOUNDATION_NONE
;
1312 /* Steep slopes behave the same as slopes with one corner raised. */
1313 if (IsSteepSlope(tileh
)) {
1314 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
1317 /* Leveled RoadBits on a slope */
1318 if ((_invalid_tileh_slopes_road
[0][tileh
] & bits
) == ROAD_NONE
) return FOUNDATION_LEVELED
;
1320 /* Straight roads without foundation on a slope */
1321 if (!IsSlopeWithOneCornerRaised(tileh
) &&
1322 (_invalid_tileh_slopes_road
[1][tileh
] & bits
) == ROAD_NONE
)
1323 return FOUNDATION_NONE
;
1325 /* Roads on steep Slopes or on Slopes with one corner raised */
1326 return (bits
== ROAD_X
? FOUNDATION_INCLINED_X
: FOUNDATION_INCLINED_Y
);
1329 const byte _road_sloped_sprites
[14] = {
1337 * Get the sprite offset within a spritegroup.
1338 * @param slope Slope
1339 * @param bits Roadbits
1340 * @return Offset for the sprite within the spritegroup.
1342 static uint
GetRoadSpriteOffset(Slope slope
, RoadBits bits
)
1344 if (slope
!= SLOPE_FLAT
) {
1346 case SLOPE_NE
: return 11;
1347 case SLOPE_SE
: return 12;
1348 case SLOPE_SW
: return 13;
1349 case SLOPE_NW
: return 14;
1350 default: NOT_REACHED();
1353 static const uint offsets
[] = {
1359 return offsets
[bits
];
1364 * Should the road be drawn as a unpaved snow/desert road?
1365 * By default, roads are always drawn as unpaved if they are on desert or
1366 * above the snow line, but NewGRFs can override this for desert.
1368 * @param tile The tile the road is on
1369 * @param roadside What sort of road this is
1370 * @return True if snow/desert road sprites should be used.
1372 static bool DrawRoadAsSnowDesert(TileIndex tile
, Roadside roadside
)
1374 return (IsOnSnow(tile
) &&
1375 !(_settings_game
.game_creation
.landscape
== LT_TROPIC
&& HasGrfMiscBit(GMB_DESERT_PAVED_ROADS
) &&
1376 roadside
!= ROADSIDE_BARREN
&& roadside
!= ROADSIDE_GRASS
&& roadside
!= ROADSIDE_GRASS_ROAD_WORKS
));
1380 * Draws the catenary for the RoadType of the given tile
1381 * @param ti information about the tile (slopes, height etc)
1382 * @param rt road type to draw catenary for
1383 * @param rb the roadbits for the tram
1385 void DrawRoadTypeCatenary(const TileInfo
*ti
, RoadType rt
, RoadBits rb
)
1387 /* Don't draw the catenary under a low bridge */
1388 if (IsBridgeAbove(ti
->tile
) && !IsTransparencySet(TO_CATENARY
)) {
1389 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
1391 if (height
<= GetTileMaxZ(ti
->tile
) + 1) return;
1394 if (CountBits(rb
) > 2) {
1395 /* On junctions we check whether neighbouring tiles also have catenary, and possibly
1396 * do not draw catenary towards those neighbours, which do not have catenary. */
1397 RoadBits rb_new
= ROAD_NONE
;
1398 for (DiagDirection dir
= DIAGDIR_BEGIN
; dir
< DIAGDIR_END
; dir
++) {
1399 if (rb
& DiagDirToRoadBits(dir
)) {
1400 TileIndex neighbour
= TileAddByDiagDir(ti
->tile
, dir
);
1401 if (MayHaveRoad(neighbour
)) {
1402 RoadType rt_road
= GetRoadTypeRoad(neighbour
);
1403 RoadType rt_tram
= GetRoadTypeTram(neighbour
);
1405 if ((rt_road
!= INVALID_ROADTYPE
&& HasRoadCatenary(rt_road
)) ||
1406 (rt_tram
!= INVALID_ROADTYPE
&& HasRoadCatenary(rt_tram
))) {
1407 rb_new
|= DiagDirToRoadBits(dir
);
1412 if (CountBits(rb_new
) >= 2) rb
= rb_new
;
1415 const RoadTypeInfo
*rti
= GetRoadTypeInfo(rt
);
1416 SpriteID front
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_CATENARY_FRONT
);
1417 SpriteID back
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_CATENARY_BACK
);
1419 if (front
!= 0 || back
!= 0) {
1420 if (front
!= 0) front
+= GetRoadSpriteOffset(ti
->tileh
, rb
);
1421 if (back
!= 0) back
+= GetRoadSpriteOffset(ti
->tileh
, rb
);
1422 } else if (ti
->tileh
!= SLOPE_FLAT
) {
1423 back
= SPR_TRAMWAY_BACK_WIRES_SLOPED
+ _road_sloped_sprites
[ti
->tileh
- 1];
1424 front
= SPR_TRAMWAY_FRONT_WIRES_SLOPED
+ _road_sloped_sprites
[ti
->tileh
- 1];
1426 back
= SPR_TRAMWAY_BASE
+ _road_backpole_sprites_1
[rb
];
1427 front
= SPR_TRAMWAY_BASE
+ _road_frontwire_sprites_1
[rb
];
1430 /* Catenary uses 1st company colour to help identify owner.
1431 * For tiles with OWNER_TOWN or OWNER_NONE, recolour CC to grey as a neutral colour. */
1432 Owner owner
= GetRoadOwner(ti
->tile
, GetRoadTramType(rt
));
1433 PaletteID pal
= (owner
== OWNER_NONE
|| owner
== OWNER_TOWN
? GENERAL_SPRITE_COLOUR(COLOUR_GREY
) : COMPANY_SPRITE_COLOUR(owner
));
1434 int z_wires
= (ti
->tileh
== SLOPE_FLAT
? 0 : TILE_HEIGHT
) + BB_HEIGHT_UNDER_BRIDGE
;
1436 /* The "back" sprite contains the west, north and east pillars.
1437 * We cut the sprite at 3/8 of the west/east edges to create 3 sprites.
1438 * 3/8 is chosen so that sprites can somewhat graphically extend into the tile. */
1439 static const int INF
= 1000; ///< big number compared to sprite size
1440 static const SubSprite west
= { -INF
, -INF
, -12, INF
};
1441 static const SubSprite north
= { -12, -INF
, 12, INF
};
1442 static const SubSprite east
= { 12, -INF
, INF
, INF
};
1443 AddSortableSpriteToDraw(back
, pal
, ti
->x
, ti
->y
, 16, 1, z_wires
, ti
->z
, IsTransparencySet(TO_CATENARY
), 15, 0, GetSlopePixelZInCorner(ti
->tileh
, CORNER_W
), &west
);
1444 AddSortableSpriteToDraw(back
, pal
, ti
->x
, ti
->y
, 1, 1, z_wires
, ti
->z
, IsTransparencySet(TO_CATENARY
), 0, 0, GetSlopePixelZInCorner(ti
->tileh
, CORNER_N
), &north
);
1445 AddSortableSpriteToDraw(back
, pal
, ti
->x
, ti
->y
, 1, 16, z_wires
, ti
->z
, IsTransparencySet(TO_CATENARY
), 0, 15, GetSlopePixelZInCorner(ti
->tileh
, CORNER_E
), &east
);
1448 /* Draw the "front" sprite (containing south pillar and wires) at a Z height that is both above the vehicles and above the "back" pillars. */
1449 AddSortableSpriteToDraw(front
, pal
, ti
->x
, ti
->y
, 16, 16, z_wires
+ 1, ti
->z
, IsTransparencySet(TO_CATENARY
), 0, 0, z_wires
);
1454 * Draws the catenary for the given tile
1455 * @param ti information about the tile (slopes, height etc)
1457 void DrawRoadCatenary(const TileInfo
*ti
)
1459 RoadBits road
= ROAD_NONE
;
1460 RoadBits tram
= ROAD_NONE
;
1462 if (IsTileType(ti
->tile
, MP_ROAD
)) {
1463 if (IsNormalRoad(ti
->tile
)) {
1464 road
= GetRoadBits(ti
->tile
, RTT_ROAD
);
1465 tram
= GetRoadBits(ti
->tile
, RTT_TRAM
);
1466 } else if (IsLevelCrossing(ti
->tile
)) {
1467 tram
= road
= (GetCrossingRailAxis(ti
->tile
) == AXIS_Y
? ROAD_X
: ROAD_Y
);
1469 } else if (IsTileType(ti
->tile
, MP_STATION
)) {
1470 if (IsRoadStop(ti
->tile
)) {
1471 if (IsDriveThroughStopTile(ti
->tile
)) {
1472 Axis axis
= GetRoadStopDir(ti
->tile
) == DIAGDIR_NE
? AXIS_X
: AXIS_Y
;
1473 tram
= road
= (axis
== AXIS_X
? ROAD_X
: ROAD_Y
);
1475 tram
= road
= DiagDirToRoadBits(GetRoadStopDir(ti
->tile
));
1479 // No road here, no catenary to draw
1483 RoadType rt
= GetRoadTypeRoad(ti
->tile
);
1484 if (rt
!= INVALID_ROADTYPE
&& HasRoadCatenaryDrawn(rt
)) {
1485 DrawRoadTypeCatenary(ti
, rt
, road
);
1488 rt
= GetRoadTypeTram(ti
->tile
);
1489 if (rt
!= INVALID_ROADTYPE
&& HasRoadCatenaryDrawn(rt
)) {
1490 DrawRoadTypeCatenary(ti
, rt
, tram
);
1495 * Draws details on/around the road
1496 * @param img the sprite to draw
1497 * @param ti the tile to draw on
1498 * @param dx the offset from the top of the BB of the tile
1499 * @param dy the offset from the top of the BB of the tile
1500 * @param h the height of the sprite to draw
1501 * @param transparent whether the sprite should be transparent (used for roadside trees)
1503 static void DrawRoadDetail(SpriteID img
, const TileInfo
*ti
, int dx
, int dy
, int h
, bool transparent
)
1508 if (ti
->tileh
!= SLOPE_FLAT
) z
= GetSlopePixelZ(x
, y
);
1509 AddSortableSpriteToDraw(img
, PAL_NONE
, x
, y
, 2, 2, h
, z
, transparent
);
1513 * Draw road underlay and overlay sprites.
1514 * @param ti TileInfo
1515 * @param road_rti Road road type information
1516 * @param tram_rti Tram road type information
1517 * @param road_offset Road sprite offset (based on road bits)
1518 * @param tram_offset Tram sprite offset (based on road bits)
1519 * @param draw_underlay Whether to draw underlays
1521 void DrawRoadOverlays(const TileInfo
*ti
, PaletteID pal
, const RoadTypeInfo
*road_rti
, const RoadTypeInfo
*tram_rti
, uint road_offset
, uint tram_offset
, bool draw_underlay
)
1523 if (draw_underlay
) {
1524 /* Road underlay takes precedence over tram */
1525 if (road_rti
!= nullptr) {
1526 if (road_rti
->UsesOverlay()) {
1527 SpriteID ground
= GetCustomRoadSprite(road_rti
, ti
->tile
, ROTSG_GROUND
);
1528 DrawGroundSprite(ground
+ road_offset
, pal
);
1531 if (tram_rti
->UsesOverlay()) {
1532 SpriteID ground
= GetCustomRoadSprite(tram_rti
, ti
->tile
, ROTSG_GROUND
);
1533 DrawGroundSprite(ground
+ tram_offset
, pal
);
1535 DrawGroundSprite(SPR_TRAMWAY_TRAM
+ tram_offset
, pal
);
1540 /* Draw road overlay */
1541 if (road_rti
!= nullptr) {
1542 if (road_rti
->UsesOverlay()) {
1543 SpriteID ground
= GetCustomRoadSprite(road_rti
, ti
->tile
, ROTSG_OVERLAY
);
1544 if (ground
!= 0) DrawGroundSprite(ground
+ road_offset
, pal
);
1548 /* Draw tram overlay */
1549 if (tram_rti
!= nullptr) {
1550 if (tram_rti
->UsesOverlay()) {
1551 SpriteID ground
= GetCustomRoadSprite(tram_rti
, ti
->tile
, ROTSG_OVERLAY
);
1552 if (ground
!= 0) DrawGroundSprite(ground
+ tram_offset
, pal
);
1553 } else if (road_rti
!= nullptr) {
1554 DrawGroundSprite(SPR_TRAMWAY_OVERLAY
+ tram_offset
, pal
);
1560 * Get ground sprite to draw for a road tile.
1561 * @param ti TileInof
1562 * @param roadside Road side type
1563 * @param rti Road type info
1564 * @param offset Road sprite offset
1565 * @param[out] pal Palette to draw.
1567 static SpriteID
GetRoadGroundSprite(const TileInfo
*ti
, Roadside roadside
, const RoadTypeInfo
*rti
, uint offset
, PaletteID
*pal
)
1569 /* Draw bare ground sprite if no road or road uses overlay system. */
1570 if (rti
== nullptr || rti
->UsesOverlay()) {
1571 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1572 return SPR_FLAT_SNOW_DESERT_TILE
+ SlopeToSpriteOffset(ti
->tileh
);
1576 case ROADSIDE_BARREN
: *pal
= PALETTE_TO_BARE_LAND
;
1577 return SPR_FLAT_GRASS_TILE
+ SlopeToSpriteOffset(ti
->tileh
);
1578 case ROADSIDE_GRASS
:
1579 case ROADSIDE_GRASS_ROAD_WORKS
: return SPR_FLAT_GRASS_TILE
+ SlopeToSpriteOffset(ti
->tileh
);
1580 default: break; // Paved
1584 /* Draw original road base sprite */
1585 SpriteID image
= SPR_ROAD_Y
+ offset
;
1586 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1590 case ROADSIDE_BARREN
: *pal
= PALETTE_TO_BARE_LAND
; break;
1591 case ROADSIDE_GRASS
: break;
1592 case ROADSIDE_GRASS_ROAD_WORKS
: break;
1593 default: image
-= 19; break; // Paved
1601 * Draw ground sprite and road pieces
1602 * @param ti TileInfo
1604 static void DrawRoadBits(TileInfo
*ti
)
1606 RoadBits road
= GetRoadBits(ti
->tile
, RTT_ROAD
);
1607 RoadBits tram
= GetRoadBits(ti
->tile
, RTT_TRAM
);
1609 RoadType road_rt
= GetRoadTypeRoad(ti
->tile
);
1610 RoadType tram_rt
= GetRoadTypeTram(ti
->tile
);
1611 const RoadTypeInfo
*road_rti
= road_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(road_rt
);
1612 const RoadTypeInfo
*tram_rti
= tram_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(tram_rt
);
1614 if (ti
->tileh
!= SLOPE_FLAT
) {
1615 DrawFoundation(ti
, GetRoadFoundation(ti
->tileh
, road
| tram
));
1616 /* DrawFoundation() modifies ti. */
1619 /* Determine sprite offsets */
1620 uint road_offset
= GetRoadSpriteOffset(ti
->tileh
, road
);
1621 uint tram_offset
= GetRoadSpriteOffset(ti
->tileh
, tram
);
1623 /* Draw baseset underlay */
1624 Roadside roadside
= GetRoadside(ti
->tile
);
1626 PaletteID pal
= PAL_NONE
;
1627 SpriteID image
= GetRoadGroundSprite(ti
, roadside
, road_rti
, road
== ROAD_NONE
? tram_offset
: road_offset
, &pal
);
1628 DrawGroundSprite(image
, pal
);
1630 DrawRoadOverlays(ti
, pal
, road_rti
, tram_rti
, road_offset
, tram_offset
);
1633 if (road_rti
!= nullptr) {
1634 DisallowedRoadDirections drd
= GetDisallowedRoadDirections(ti
->tile
);
1635 if (drd
!= DRD_NONE
) {
1636 SpriteID oneway
= GetCustomRoadSprite(road_rti
, ti
->tile
, ROTSG_ONEWAY
);
1638 if (oneway
== 0) oneway
= SPR_ONEWAY_BASE
;
1640 if ((ti
->tileh
== SLOPE_NE
) || (ti
->tileh
== SLOPE_NW
)) {
1641 oneway
+= SPR_ONEWAY_SLOPE_N_OFFSET
;
1642 } else if ((ti
->tileh
== SLOPE_SE
) || (ti
->tileh
== SLOPE_SW
)) {
1643 oneway
+= SPR_ONEWAY_SLOPE_S_OFFSET
;
1646 DrawGroundSpriteAt(oneway
+ drd
- 1 + ((road
== ROAD_X
) ? 0 : 3), PAL_NONE
, 8, 8, GetPartialPixelZ(8, 8, ti
->tileh
));
1650 if (HasRoadWorks(ti
->tile
)) {
1652 DrawGroundSprite((road
| tram
) & ROAD_X
? SPR_EXCAVATION_X
: SPR_EXCAVATION_Y
, PAL_NONE
);
1656 /* Draw road, tram catenary */
1657 DrawRoadCatenary(ti
);
1659 /* Return if full detail is disabled, or we are zoomed fully out. */
1660 if (!HasBit(_display_opt
, DO_FULL_DETAIL
) || _cur_dpi
->zoom
> ZOOM_LVL_DETAIL
) return;
1662 /* Do not draw details (street lights, trees) under low bridge */
1663 if (IsBridgeAbove(ti
->tile
) && (roadside
== ROADSIDE_TREES
|| roadside
== ROADSIDE_STREET_LIGHTS
)) {
1664 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
1665 int minz
= GetTileMaxZ(ti
->tile
) + 2;
1667 if (roadside
== ROADSIDE_TREES
) minz
++;
1669 if (height
< minz
) return;
1672 /* If there are no road bits, return, as there is nothing left to do */
1673 if (HasAtMostOneBit(road
)) return;
1675 /* Do not draw details when invisible. */
1676 if (roadside
== ROADSIDE_TREES
&& IsInvisibilitySet(TO_TREES
)) return;
1677 if (roadside
== ROADSIDE_STREET_LIGHTS
&& IsInvisibilitySet(TO_HOUSES
)) return;
1679 /* Check whether details should be transparent. */
1680 bool is_transparent
= false;
1681 if (roadside
== ROADSIDE_TREES
&& IsTransparencySet(TO_TREES
)) {
1682 is_transparent
= true;
1684 if (roadside
== ROADSIDE_STREET_LIGHTS
&& IsTransparencySet(TO_HOUSES
)) {
1685 is_transparent
= true;
1688 /* Draw extra details. */
1689 for (const DrawRoadTileStruct
*drts
= _road_display_table
[roadside
][road
| tram
]; drts
->image
!= 0; drts
++) {
1690 DrawRoadDetail(drts
->image
, ti
, drts
->subcoord_x
, drts
->subcoord_y
, 0x10, is_transparent
);
1694 /** Tile callback function for rendering a road tile to the screen */
1695 static void DrawTile_Road(TileInfo
*ti
)
1697 switch (GetRoadTileType(ti
->tile
)) {
1698 case ROAD_TILE_NORMAL
:
1702 case ROAD_TILE_CROSSING
: {
1703 if (ti
->tileh
!= SLOPE_FLAT
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
1705 Axis axis
= GetCrossingRailAxis(ti
->tile
);
1707 const RailTypeInfo
*rti
= GetRailTypeInfo(GetRailType(ti
->tile
));
1709 RoadType road_rt
= GetRoadTypeRoad(ti
->tile
);
1710 RoadType tram_rt
= GetRoadTypeTram(ti
->tile
);
1711 const RoadTypeInfo
*road_rti
= road_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(road_rt
);
1712 const RoadTypeInfo
*tram_rti
= tram_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(tram_rt
);
1714 PaletteID pal
= PAL_NONE
;
1716 /* Draw base ground */
1717 if (rti
->UsesOverlay()) {
1718 SpriteID image
= SPR_ROAD_Y
+ axis
;
1720 Roadside roadside
= GetRoadside(ti
->tile
);
1721 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1725 case ROADSIDE_BARREN
: pal
= PALETTE_TO_BARE_LAND
; break;
1726 case ROADSIDE_GRASS
: break;
1727 default: image
-= 19; break; // Paved
1731 DrawGroundSprite(image
, pal
);
1733 SpriteID image
= rti
->base_sprites
.crossing
+ axis
;
1734 if (IsCrossingBarred(ti
->tile
)) image
+= 2;
1736 Roadside roadside
= GetRoadside(ti
->tile
);
1737 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1741 case ROADSIDE_BARREN
: pal
= PALETTE_TO_BARE_LAND
; break;
1742 case ROADSIDE_GRASS
: break;
1743 default: image
+= 4; break; // Paved
1747 DrawGroundSprite(image
, pal
);
1750 DrawRoadOverlays(ti
, pal
, road_rti
, tram_rti
, axis
, axis
);
1752 /* Draw rail/PBS overlay */
1753 bool draw_pbs
= _game_mode
!= GM_MENU
&& _settings_client
.gui
.show_track_reservation
&& HasCrossingReservation(ti
->tile
);
1754 if (rti
->UsesOverlay()) {
1755 pal
= draw_pbs
? PALETTE_CRASH
: PAL_NONE
;
1756 SpriteID rail
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_CROSSING
) + axis
;
1757 DrawGroundSprite(rail
, pal
);
1759 const Axis road_axis
= GetCrossingRoadAxis(ti
->tile
);
1760 const DiagDirection dir1
= AxisToDiagDir(road_axis
);
1761 const DiagDirection dir2
= ReverseDiagDir(dir1
);
1762 uint adjacent_diagdirs
= 0;
1763 for (DiagDirection dir
: { dir1
, dir2
}) {
1764 const TileIndex t
= TileAddByDiagDir(ti
->tile
, dir
);
1765 if (t
< Map::Size() && IsLevelCrossingTile(t
) && GetCrossingRoadAxis(t
) == road_axis
) {
1766 SetBit(adjacent_diagdirs
, dir
);
1770 switch (adjacent_diagdirs
) {
1772 DrawRailTileSeq(ti
, &_crossing_layout
, TO_CATENARY
, rail
, 0, PAL_NONE
);
1775 case (1 << DIAGDIR_NE
):
1776 DrawRailTileSeq(ti
, &_crossing_layout_SW
, TO_CATENARY
, rail
, 0, PAL_NONE
);
1779 case (1 << DIAGDIR_SE
):
1780 DrawRailTileSeq(ti
, &_crossing_layout_NW
, TO_CATENARY
, rail
, 0, PAL_NONE
);
1783 case (1 << DIAGDIR_SW
):
1784 DrawRailTileSeq(ti
, &_crossing_layout_NE
, TO_CATENARY
, rail
, 0, PAL_NONE
);
1787 case (1 << DIAGDIR_NW
):
1788 DrawRailTileSeq(ti
, &_crossing_layout_SE
, TO_CATENARY
, rail
, 0, PAL_NONE
);
1792 /* Show no sprites */
1795 } else if (draw_pbs
|| tram_rti
!= nullptr || road_rti
->UsesOverlay()) {
1796 /* Add another rail overlay, unless there is only the base road sprite. */
1797 pal
= draw_pbs
? PALETTE_CRASH
: PAL_NONE
;
1798 SpriteID rail
= GetCrossingRoadAxis(ti
->tile
) == AXIS_Y
? GetRailTypeInfo(GetRailType(ti
->tile
))->base_sprites
.single_x
: GetRailTypeInfo(GetRailType(ti
->tile
))->base_sprites
.single_y
;
1799 DrawGroundSprite(rail
, pal
);
1802 /* Draw road, tram catenary */
1803 DrawRoadCatenary(ti
);
1805 /* Draw rail catenary */
1806 if (HasRailCatenaryDrawn(GetRailType(ti
->tile
))) DrawRailCatenary(ti
);
1812 case ROAD_TILE_DEPOT
: {
1813 if (ti
->tileh
!= SLOPE_FLAT
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
1815 PaletteID palette
= COMPANY_SPRITE_COLOUR(GetTileOwner(ti
->tile
));
1817 RoadType road_rt
= GetRoadTypeRoad(ti
->tile
);
1818 RoadType tram_rt
= GetRoadTypeTram(ti
->tile
);
1819 const RoadTypeInfo
*rti
= GetRoadTypeInfo(road_rt
== INVALID_ROADTYPE
? tram_rt
: road_rt
);
1821 int relocation
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_DEPOT
);
1822 bool default_gfx
= relocation
== 0;
1824 if (HasBit(rti
->flags
, ROTF_CATENARY
)) {
1825 if (_loaded_newgrf_features
.tram
== TRAMWAY_REPLACE_DEPOT_WITH_TRACK
&& road_rt
== INVALID_ROADTYPE
&& !rti
->UsesOverlay()) {
1826 /* Sprites with track only work for default tram */
1827 relocation
= SPR_TRAMWAY_DEPOT_WITH_TRACK
- SPR_ROAD_DEPOT
;
1828 default_gfx
= false;
1830 /* Sprites without track are always better, if provided */
1831 relocation
= SPR_TRAMWAY_DEPOT_NO_TRACK
- SPR_ROAD_DEPOT
;
1835 relocation
-= SPR_ROAD_DEPOT
;
1838 DiagDirection dir
= GetRoadDepotDirection(ti
->tile
);
1839 const DrawTileSprites
*dts
= &_road_depot
[dir
];
1840 DrawGroundSprite(dts
->ground
.sprite
, PAL_NONE
);
1843 uint offset
= GetRoadSpriteOffset(SLOPE_FLAT
, DiagDirToRoadBits(dir
));
1844 if (rti
->UsesOverlay()) {
1845 SpriteID ground
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_OVERLAY
);
1846 if (ground
!= 0) DrawGroundSprite(ground
+ offset
, PAL_NONE
);
1847 } else if (road_rt
== INVALID_ROADTYPE
) {
1848 DrawGroundSprite(SPR_TRAMWAY_OVERLAY
+ offset
, PAL_NONE
);
1852 DrawRailTileSeq(ti
, dts
, TO_BUILDINGS
, relocation
, 0, palette
);
1856 DrawBridgeMiddle(ti
);
1860 * Draw the road depot sprite.
1861 * @param x The x offset to draw at.
1862 * @param y The y offset to draw at.
1863 * @param dir The direction the depot must be facing.
1864 * @param rt The road type of the depot to draw.
1866 void DrawRoadDepotSprite(int x
, int y
, DiagDirection dir
, RoadType rt
)
1868 PaletteID palette
= COMPANY_SPRITE_COLOUR(_local_company
);
1870 const RoadTypeInfo
*rti
= GetRoadTypeInfo(rt
);
1871 int relocation
= GetCustomRoadSprite(rti
, INVALID_TILE
, ROTSG_DEPOT
);
1872 bool default_gfx
= relocation
== 0;
1874 if (HasBit(rti
->flags
, ROTF_CATENARY
)) {
1875 if (_loaded_newgrf_features
.tram
== TRAMWAY_REPLACE_DEPOT_WITH_TRACK
&& RoadTypeIsTram(rt
) && !rti
->UsesOverlay()) {
1876 /* Sprites with track only work for default tram */
1877 relocation
= SPR_TRAMWAY_DEPOT_WITH_TRACK
- SPR_ROAD_DEPOT
;
1878 default_gfx
= false;
1880 /* Sprites without track are always better, if provided */
1881 relocation
= SPR_TRAMWAY_DEPOT_NO_TRACK
- SPR_ROAD_DEPOT
;
1885 relocation
-= SPR_ROAD_DEPOT
;
1888 const DrawTileSprites
*dts
= &_road_depot
[dir
];
1889 DrawSprite(dts
->ground
.sprite
, PAL_NONE
, x
, y
);
1892 uint offset
= GetRoadSpriteOffset(SLOPE_FLAT
, DiagDirToRoadBits(dir
));
1893 if (rti
->UsesOverlay()) {
1894 SpriteID ground
= GetCustomRoadSprite(rti
, INVALID_TILE
, ROTSG_OVERLAY
);
1895 if (ground
!= 0) DrawSprite(ground
+ offset
, PAL_NONE
, x
, y
);
1896 } else if (RoadTypeIsTram(rt
)) {
1897 DrawSprite(SPR_TRAMWAY_OVERLAY
+ offset
, PAL_NONE
, x
, y
);
1901 DrawRailTileSeqInGUI(x
, y
, dts
, relocation
, 0, palette
);
1905 * Updates cached nearest town for all road tiles
1906 * @param invalidate are we just invalidating cached data?
1907 * @pre invalidate == true implies _generating_world == true
1909 void UpdateNearestTownForRoadTiles(bool invalidate
)
1911 assert(!invalidate
|| _generating_world
);
1913 for (TileIndex t
= 0; t
< Map::Size(); t
++) {
1914 if (IsTileType(t
, MP_ROAD
) && !IsRoadDepot(t
) && !HasTownOwnedRoad(t
)) {
1915 TownID tid
= INVALID_TOWN
;
1917 const Town
*town
= CalcClosestTownFromTile(t
);
1918 if (town
!= nullptr) tid
= town
->index
;
1920 SetTownIndex(t
, tid
);
1925 static int GetSlopePixelZ_Road(TileIndex tile
, uint x
, uint y
, bool)
1928 if (IsNormalRoad(tile
)) {
1930 Slope tileh
= GetTilePixelSlope(tile
, &z
);
1931 if (tileh
== SLOPE_FLAT
) return z
;
1933 Foundation f
= GetRoadFoundation(tileh
, GetAllRoadBits(tile
));
1934 z
+= ApplyPixelFoundationToSlope(f
, &tileh
);
1935 return z
+ GetPartialPixelZ(x
& 0xF, y
& 0xF, tileh
);
1937 return GetTileMaxPixelZ(tile
);
1941 static Foundation
GetFoundation_Road(TileIndex tile
, Slope tileh
)
1943 if (IsNormalRoad(tile
)) {
1944 return GetRoadFoundation(tileh
, GetAllRoadBits(tile
));
1946 return FlatteningFoundation(tileh
);
1950 static const Roadside _town_road_types
[][2] = {
1951 { ROADSIDE_GRASS
, ROADSIDE_GRASS
},
1952 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1953 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1954 { ROADSIDE_TREES
, ROADSIDE_TREES
},
1955 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
}
1958 static_assert(lengthof(_town_road_types
) == HZB_END
);
1960 static const Roadside _town_road_types_2
[][2] = {
1961 { ROADSIDE_GRASS
, ROADSIDE_GRASS
},
1962 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1963 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
},
1964 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
},
1965 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
}
1968 static_assert(lengthof(_town_road_types_2
) == HZB_END
);
1971 static void TileLoop_Road(TileIndex tile
)
1973 switch (_settings_game
.game_creation
.landscape
) {
1975 if (IsOnSnow(tile
) != (GetTileZ(tile
) > GetSnowLine())) {
1977 MarkTileDirtyByTile(tile
);
1982 if (GetTropicZone(tile
) == TROPICZONE_DESERT
&& !IsOnDesert(tile
)) {
1984 MarkTileDirtyByTile(tile
);
1989 if (IsRoadDepot(tile
)) return;
1991 const Town
*t
= ClosestTownFromTile(tile
, UINT_MAX
);
1992 if (!HasRoadWorks(tile
)) {
1993 HouseZonesBits grp
= HZB_TOWN_EDGE
;
1996 grp
= GetTownRadiusGroup(t
, tile
);
1998 /* Show an animation to indicate road work */
1999 if (t
->road_build_months
!= 0 &&
2000 (DistanceManhattan(t
->xy
, tile
) < 8 || grp
!= HZB_TOWN_EDGE
) &&
2001 IsNormalRoad(tile
) && !HasAtMostOneBit(GetAllRoadBits(tile
))) {
2002 if (GetFoundationSlope(tile
) == SLOPE_FLAT
&& EnsureNoVehicleOnGround(tile
).Succeeded() && Chance16(1, 40)) {
2003 StartRoadWorks(tile
);
2005 if (_settings_client
.sound
.ambient
) SndPlayTileFx(SND_21_ROAD_WORKS
, tile
);
2006 CreateEffectVehicleAbove(
2007 TileX(tile
) * TILE_SIZE
+ 7,
2008 TileY(tile
) * TILE_SIZE
+ 7,
2011 MarkTileDirtyByTile(tile
);
2018 /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
2019 const Roadside
*new_rs
= (_settings_game
.game_creation
.landscape
== LT_TOYLAND
) ? _town_road_types_2
[grp
] : _town_road_types
[grp
];
2020 Roadside cur_rs
= GetRoadside(tile
);
2022 /* We have our desired type, do nothing */
2023 if (cur_rs
== new_rs
[0]) return;
2025 /* We have the pre-type of the desired type, switch to the desired type */
2026 if (cur_rs
== new_rs
[1]) {
2028 /* We have barren land, install the pre-type */
2029 } else if (cur_rs
== ROADSIDE_BARREN
) {
2031 /* We're totally off limits, remove any installation and make barren land */
2033 cur_rs
= ROADSIDE_BARREN
;
2035 SetRoadside(tile
, cur_rs
);
2036 MarkTileDirtyByTile(tile
);
2038 } else if (IncreaseRoadWorksCounter(tile
)) {
2039 TerminateRoadWorks(tile
);
2041 if (_settings_game
.economy
.mod_road_rebuild
) {
2042 /* Generate a nicer town surface */
2043 const RoadBits old_rb
= GetAnyRoadBits(tile
, RTT_ROAD
);
2044 const RoadBits new_rb
= CleanUpRoadBits(tile
, old_rb
);
2046 if (old_rb
!= new_rb
) {
2047 RemoveRoad(tile
, DC_EXEC
| DC_AUTO
| DC_NO_WATER
, (old_rb
^ new_rb
), RTT_ROAD
, true);
2049 /* If new_rb is 0, there are now no road pieces left and the tile is no longer a road tile */
2051 MarkTileDirtyByTile(tile
);
2057 /* Possibly change road type */
2058 if (GetRoadOwner(tile
, RTT_ROAD
) == OWNER_TOWN
) {
2059 RoadType rt
= GetTownRoadType();
2060 if (rt
!= GetRoadTypeRoad(tile
)) {
2061 SetRoadType(tile
, RTT_ROAD
, rt
);
2065 MarkTileDirtyByTile(tile
);
2069 static bool ClickTile_Road(TileIndex tile
)
2071 if (!IsRoadDepot(tile
)) return false;
2073 ShowDepotWindow(tile
, VEH_ROAD
);
2077 /* Converts RoadBits to TrackBits */
2078 static const TrackBits _road_trackbits
[16] = {
2079 TRACK_BIT_NONE
, // ROAD_NONE
2080 TRACK_BIT_NONE
, // ROAD_NW
2081 TRACK_BIT_NONE
, // ROAD_SW
2082 TRACK_BIT_LEFT
, // ROAD_W
2083 TRACK_BIT_NONE
, // ROAD_SE
2084 TRACK_BIT_Y
, // ROAD_Y
2085 TRACK_BIT_LOWER
, // ROAD_S
2086 TRACK_BIT_LEFT
| TRACK_BIT_LOWER
| TRACK_BIT_Y
, // ROAD_Y | ROAD_SW
2087 TRACK_BIT_NONE
, // ROAD_NE
2088 TRACK_BIT_UPPER
, // ROAD_N
2089 TRACK_BIT_X
, // ROAD_X
2090 TRACK_BIT_LEFT
| TRACK_BIT_UPPER
| TRACK_BIT_X
, // ROAD_X | ROAD_NW
2091 TRACK_BIT_RIGHT
, // ROAD_E
2092 TRACK_BIT_RIGHT
| TRACK_BIT_UPPER
| TRACK_BIT_Y
, // ROAD_Y | ROAD_NE
2093 TRACK_BIT_RIGHT
| TRACK_BIT_LOWER
| TRACK_BIT_X
, // ROAD_X | ROAD_SE
2094 TRACK_BIT_ALL
, // ROAD_ALL
2097 static TrackStatus
GetTileTrackStatus_Road(TileIndex tile
, TransportType mode
, uint sub_mode
, DiagDirection side
)
2099 TrackdirBits trackdirbits
= TRACKDIR_BIT_NONE
;
2100 TrackdirBits red_signals
= TRACKDIR_BIT_NONE
; // crossing barred
2102 case TRANSPORT_RAIL
:
2103 if (IsLevelCrossing(tile
)) trackdirbits
= TrackBitsToTrackdirBits(GetCrossingRailBits(tile
));
2106 case TRANSPORT_ROAD
: {
2107 RoadTramType rtt
= (RoadTramType
)sub_mode
;
2108 if (!HasTileRoadType(tile
, rtt
)) break;
2109 switch (GetRoadTileType(tile
)) {
2110 case ROAD_TILE_NORMAL
: {
2111 const uint drd_to_multiplier
[DRD_END
] = { 0x101, 0x100, 0x1, 0x0 };
2112 RoadBits bits
= GetRoadBits(tile
, rtt
);
2114 /* no roadbit at this side of tile, return 0 */
2115 if (side
!= INVALID_DIAGDIR
&& (DiagDirToRoadBits(side
) & bits
) == 0) break;
2117 uint multiplier
= drd_to_multiplier
[(rtt
== RTT_TRAM
) ? DRD_NONE
: GetDisallowedRoadDirections(tile
)];
2118 if (!HasRoadWorks(tile
)) trackdirbits
= (TrackdirBits
)(_road_trackbits
[bits
] * multiplier
);
2122 case ROAD_TILE_CROSSING
: {
2123 Axis axis
= GetCrossingRoadAxis(tile
);
2125 if (side
!= INVALID_DIAGDIR
&& axis
!= DiagDirToAxis(side
)) break;
2127 trackdirbits
= TrackBitsToTrackdirBits(AxisToTrackBits(axis
));
2128 if (IsCrossingBarred(tile
)) {
2129 red_signals
= trackdirbits
;
2130 if (TrainOnCrossing(tile
)) break;
2132 auto mask_red_signal_bits_if_crossing_barred
= [&](TileIndex t
, TrackdirBits mask
) {
2133 if (IsLevelCrossingTile(t
) && IsCrossingBarred(t
)) red_signals
&= mask
;
2135 /* Check for blocked adjacent crossing to south, keep only southbound red signal trackdirs, allow northbound traffic */
2136 mask_red_signal_bits_if_crossing_barred(TileAddByDiagDir(tile
, AxisToDiagDir(axis
)), TRACKDIR_BIT_X_SW
| TRACKDIR_BIT_Y_SE
);
2137 /* Check for blocked adjacent crossing to north, keep only northbound red signal trackdirs, allow southbound traffic */
2138 mask_red_signal_bits_if_crossing_barred(TileAddByDiagDir(tile
, ReverseDiagDir(AxisToDiagDir(axis
))), TRACKDIR_BIT_X_NE
| TRACKDIR_BIT_Y_NW
);
2144 case ROAD_TILE_DEPOT
: {
2145 DiagDirection dir
= GetRoadDepotDirection(tile
);
2147 if (side
!= INVALID_DIAGDIR
&& side
!= dir
) break;
2149 trackdirbits
= TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir
));
2158 return CombineTrackStatus(trackdirbits
, red_signals
);
2161 static const StringID _road_tile_strings
[] = {
2162 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2163 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2164 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2165 STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS
,
2166 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2167 STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD
,
2168 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2169 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2172 static void GetTileDesc_Road(TileIndex tile
, TileDesc
*td
)
2174 Owner rail_owner
= INVALID_OWNER
;
2175 Owner road_owner
= INVALID_OWNER
;
2176 Owner tram_owner
= INVALID_OWNER
;
2178 RoadType road_rt
= GetRoadTypeRoad(tile
);
2179 RoadType tram_rt
= GetRoadTypeTram(tile
);
2180 if (road_rt
!= INVALID_ROADTYPE
) {
2181 const RoadTypeInfo
*rti
= GetRoadTypeInfo(road_rt
);
2182 td
->roadtype
= rti
->strings
.name
;
2183 td
->road_speed
= rti
->max_speed
/ 2;
2184 road_owner
= GetRoadOwner(tile
, RTT_ROAD
);
2186 if (tram_rt
!= INVALID_ROADTYPE
) {
2187 const RoadTypeInfo
*rti
= GetRoadTypeInfo(tram_rt
);
2188 td
->tramtype
= rti
->strings
.name
;
2189 td
->tram_speed
= rti
->max_speed
/ 2;
2190 tram_owner
= GetRoadOwner(tile
, RTT_TRAM
);
2193 switch (GetRoadTileType(tile
)) {
2194 case ROAD_TILE_CROSSING
: {
2195 td
->str
= STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING
;
2196 rail_owner
= GetTileOwner(tile
);
2198 const RailTypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
));
2199 td
->railtype
= rti
->strings
.name
;
2200 td
->rail_speed
= rti
->max_speed
;
2205 case ROAD_TILE_DEPOT
:
2206 td
->str
= STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT
;
2207 td
->build_date
= Depot::GetByTile(tile
)->build_date
;
2211 td
->str
= (road_rt
!= INVALID_ROADTYPE
? _road_tile_strings
[GetRoadside(tile
)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY
);
2216 /* Now we have to discover, if the tile has only one owner or many:
2217 * - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
2218 * - Compare the found owner with the other owners, and test if they differ.
2219 * Note: If road exists it will be the first_owner.
2221 Owner first_owner
= (road_owner
== INVALID_OWNER
? tram_owner
: road_owner
);
2222 bool mixed_owners
= (tram_owner
!= INVALID_OWNER
&& tram_owner
!= first_owner
) || (rail_owner
!= INVALID_OWNER
&& rail_owner
!= first_owner
);
2225 /* Multiple owners */
2226 td
->owner_type
[0] = (rail_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_RAIL_OWNER
);
2227 td
->owner
[0] = rail_owner
;
2228 td
->owner_type
[1] = (road_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_ROAD_OWNER
);
2229 td
->owner
[1] = road_owner
;
2230 td
->owner_type
[2] = (tram_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_TRAM_OWNER
);
2231 td
->owner
[2] = tram_owner
;
2233 /* One to rule them all */
2234 td
->owner
[0] = first_owner
;
2239 * Given the direction the road depot is pointing, this is the direction the
2240 * vehicle should be travelling in in order to enter the depot.
2242 static const byte _roadveh_enter_depot_dir
[4] = {
2243 TRACKDIR_X_SW
, TRACKDIR_Y_NW
, TRACKDIR_X_NE
, TRACKDIR_Y_SE
2246 static VehicleEnterTileStatus
VehicleEnter_Road(Vehicle
*v
, TileIndex tile
, int, int)
2248 switch (GetRoadTileType(tile
)) {
2249 case ROAD_TILE_DEPOT
: {
2250 if (v
->type
!= VEH_ROAD
) break;
2252 RoadVehicle
*rv
= RoadVehicle::From(v
);
2253 if (rv
->frame
== RVC_DEPOT_STOP_FRAME
&&
2254 _roadveh_enter_depot_dir
[GetRoadDepotDirection(tile
)] == rv
->state
) {
2255 rv
->state
= RVSB_IN_DEPOT
;
2256 rv
->vehstatus
|= VS_HIDDEN
;
2257 rv
->direction
= ReverseDir(rv
->direction
);
2258 if (rv
->Next() == nullptr) VehicleEnterDepot(rv
->First());
2261 InvalidateWindowData(WC_VEHICLE_DEPOT
, rv
->tile
);
2262 return VETSB_ENTERED_WORMHOLE
;
2269 return VETSB_CONTINUE
;
2273 static void ChangeTileOwner_Road(TileIndex tile
, Owner old_owner
, Owner new_owner
)
2275 if (IsRoadDepot(tile
)) {
2276 if (GetTileOwner(tile
) == old_owner
) {
2277 if (new_owner
== INVALID_OWNER
) {
2278 Command
<CMD_LANDSCAPE_CLEAR
>::Do(DC_EXEC
| DC_BANKRUPT
, tile
);
2280 /* A road depot has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
2281 RoadType rt
= GetRoadTypeRoad(tile
);
2282 if (rt
== INVALID_ROADTYPE
) rt
= GetRoadTypeTram(tile
);
2283 Company::Get(old_owner
)->infrastructure
.road
[rt
] -= 2;
2284 Company::Get(new_owner
)->infrastructure
.road
[rt
] += 2;
2286 SetTileOwner(tile
, new_owner
);
2287 for (RoadTramType rtt
: _roadtramtypes
) {
2288 if (GetRoadOwner(tile
, rtt
) == old_owner
) {
2289 SetRoadOwner(tile
, rtt
, new_owner
);
2297 for (RoadTramType rtt
: _roadtramtypes
) {
2298 /* Update all roadtypes, no matter if they are present */
2299 if (GetRoadOwner(tile
, rtt
) == old_owner
) {
2300 RoadType rt
= GetRoadType(tile
, rtt
);
2301 if (rt
!= INVALID_ROADTYPE
) {
2302 /* A level crossing has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
2303 uint num_bits
= IsLevelCrossing(tile
) ? 2 : CountBits(GetRoadBits(tile
, rtt
));
2304 Company::Get(old_owner
)->infrastructure
.road
[rt
] -= num_bits
;
2305 if (new_owner
!= INVALID_OWNER
) Company::Get(new_owner
)->infrastructure
.road
[rt
] += num_bits
;
2308 SetRoadOwner(tile
, rtt
, new_owner
== INVALID_OWNER
? OWNER_NONE
: new_owner
);
2312 if (IsLevelCrossing(tile
)) {
2313 if (GetTileOwner(tile
) == old_owner
) {
2314 if (new_owner
== INVALID_OWNER
) {
2315 Command
<CMD_REMOVE_SINGLE_RAIL
>::Do(DC_EXEC
| DC_BANKRUPT
, tile
, GetCrossingRailTrack(tile
));
2317 /* Update infrastructure counts. No need to dirty windows here, we'll redraw the whole screen anyway. */
2318 Company::Get(old_owner
)->infrastructure
.rail
[GetRailType(tile
)] -= LEVELCROSSING_TRACKBIT_FACTOR
;
2319 Company::Get(new_owner
)->infrastructure
.rail
[GetRailType(tile
)] += LEVELCROSSING_TRACKBIT_FACTOR
;
2321 SetTileOwner(tile
, new_owner
);
2327 static CommandCost
TerraformTile_Road(TileIndex tile
, DoCommandFlag flags
, int z_new
, Slope tileh_new
)
2329 if (_settings_game
.construction
.build_on_slopes
&& AutoslopeEnabled()) {
2330 switch (GetRoadTileType(tile
)) {
2331 case ROAD_TILE_CROSSING
:
2332 if (!IsSteepSlope(tileh_new
) && (GetTileMaxZ(tile
) == z_new
+ GetSlopeMaxZ(tileh_new
)) && HasBit(VALID_LEVEL_CROSSING_SLOPES
, tileh_new
)) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
2335 case ROAD_TILE_DEPOT
:
2336 if (AutoslopeCheckForEntranceEdge(tile
, z_new
, tileh_new
, GetRoadDepotDirection(tile
))) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
2339 case ROAD_TILE_NORMAL
: {
2340 RoadBits bits
= GetAllRoadBits(tile
);
2341 RoadBits bits_copy
= bits
;
2342 /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
2343 if (CheckRoadSlope(tileh_new
, &bits_copy
, ROAD_NONE
, ROAD_NONE
).Succeeded()) {
2344 /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
2345 if (bits
== bits_copy
) {
2347 Slope tileh_old
= GetTileSlope(tile
, &z_old
);
2349 /* Get the slope on top of the foundation */
2350 z_old
+= ApplyFoundationToSlope(GetRoadFoundation(tileh_old
, bits
), &tileh_old
);
2351 z_new
+= ApplyFoundationToSlope(GetRoadFoundation(tileh_new
, bits
), &tileh_new
);
2353 /* The surface slope must not be changed */
2354 if ((z_old
== z_new
) && (tileh_old
== tileh_new
)) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
2360 default: NOT_REACHED();
2364 return Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
2367 /** Update power of road vehicle under which is the roadtype being converted */
2368 static Vehicle
*UpdateRoadVehPowerProc(Vehicle
*v
, void *data
)
2370 if (v
->type
!= VEH_ROAD
) return nullptr;
2372 RoadVehicleList
*affected_rvs
= static_cast<RoadVehicleList
*>(data
);
2373 include(*affected_rvs
, RoadVehicle::From(v
)->First());
2379 * Checks the tile and returns whether the current player is allowed to convert the roadtype to another roadtype without taking ownership
2380 * @param owner the tile owner.
2381 * @param rtt Road/tram type.
2382 * @return whether the road is convertible
2384 static bool CanConvertUnownedRoadType(Owner owner
, RoadTramType rtt
)
2386 return (owner
== OWNER_NONE
|| (owner
== OWNER_TOWN
&& rtt
== RTT_ROAD
));
2390 * Convert the ownership of the RoadType of the tile if applicable
2391 * @param tile the tile of which convert ownership
2392 * @param num_pieces the count of the roadbits to assign to the new owner
2393 * @param owner the current owner of the RoadType
2394 * @param from_type the old road type
2395 * @param to_type the new road type
2397 static void ConvertRoadTypeOwner(TileIndex tile
, uint num_pieces
, Owner owner
, RoadType from_type
, RoadType to_type
)
2399 // Scenario editor, maybe? Don't touch the owners when converting roadtypes...
2400 if (_current_company
>= MAX_COMPANIES
) return;
2402 // We can't get a company from invalid owners but we can get ownership of roads without an owner
2403 if (owner
>= MAX_COMPANIES
&& owner
!= OWNER_NONE
) return;
2409 SetRoadOwner(tile
, GetRoadTramType(to_type
), (Owner
)_current_company
);
2410 UpdateCompanyRoadInfrastructure(to_type
, _current_company
, num_pieces
);
2414 c
= Company::Get(owner
);
2415 c
->infrastructure
.road
[from_type
] -= num_pieces
;
2416 c
->infrastructure
.road
[to_type
] += num_pieces
;
2417 DirtyCompanyInfrastructureWindows(c
->index
);
2423 * Convert one road subtype to another.
2424 * Not meant to convert from road to tram.
2426 * @param flags operation to perform
2427 * @param tile end tile of road conversion drag
2428 * @param area_start start tile of drag
2429 * @param to_type new roadtype to convert to.
2430 * @return the cost of this operation or an error
2432 CommandCost
CmdConvertRoad(DoCommandFlag flags
, TileIndex tile
, TileIndex area_start
, RoadType to_type
)
2434 TileIndex area_end
= tile
;
2436 if (!ValParamRoadType(to_type
)) return CMD_ERROR
;
2437 if (area_start
>= Map::Size()) return CMD_ERROR
;
2439 RoadVehicleList affected_rvs
;
2440 RoadTramType rtt
= GetRoadTramType(to_type
);
2442 CommandCost
cost(EXPENSES_CONSTRUCTION
);
2443 CommandCost error
= CommandCost((rtt
== RTT_TRAM
) ? STR_ERROR_NO_SUITABLE_TRAMWAY
: STR_ERROR_NO_SUITABLE_ROAD
); // by default, there is no road to convert.
2444 bool found_convertible_road
= false; // whether we actually did convert any road/tram (see bug #7633)
2446 std::unique_ptr
<TileIterator
> iter
= std::make_unique
<OrthogonalTileIterator
>(area_start
, area_end
);
2447 for (; (tile
= *iter
) != INVALID_TILE
; ++(*iter
)) {
2448 /* Is road present on tile? */
2449 if (!MayHaveRoad(tile
)) continue;
2451 /* Converting to the same subtype? */
2452 RoadType from_type
= GetRoadType(tile
, rtt
);
2453 if (from_type
== INVALID_ROADTYPE
|| from_type
== to_type
) continue;
2455 /* Check if there is any infrastructure on tile */
2456 TileType tt
= GetTileType(tile
);
2459 if (!IsRoadStop(tile
)) continue;
2462 if (IsLevelCrossing(tile
) && RoadNoLevelCrossing(to_type
)) {
2463 error
.MakeError(STR_ERROR_CROSSING_DISALLOWED_ROAD
);
2467 case MP_TUNNELBRIDGE
:
2468 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) continue;
2473 /* Trying to convert other's road */
2474 Owner owner
= GetRoadOwner(tile
, rtt
);
2475 if (!CanConvertUnownedRoadType(owner
, rtt
)) {
2476 CommandCost ret
= CheckOwnership(owner
, tile
);
2483 /* Base the ability to replace town roads and bridges on the town's
2484 * acceptance of destructive actions. */
2485 if (owner
== OWNER_TOWN
) {
2486 Town
*t
= ClosestTownFromTile(tile
, _settings_game
.economy
.dist_local_authority
);
2487 CommandCost ret
= CheckforTownRating(DC_NONE
, t
, tt
== MP_TUNNELBRIDGE
? TUNNELBRIDGE_REMOVE
: ROAD_REMOVE
);
2494 /* Vehicle on the tile when not converting normal <-> powered
2495 * Tunnels and bridges have special check later */
2496 if (tt
!= MP_TUNNELBRIDGE
) {
2497 if (!HasPowerOnRoad(from_type
, to_type
)) {
2498 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
2504 if (rtt
== RTT_ROAD
&& owner
== OWNER_TOWN
) {
2505 SetDParamsForOwnedBy(OWNER_TOWN
, tile
);
2506 error
.MakeError(STR_ERROR_OWNED_BY
);
2511 uint num_pieces
= CountBits(GetAnyRoadBits(tile
, rtt
));
2512 if (tt
== MP_STATION
&& IsBayRoadStopTile(tile
)) {
2513 num_pieces
*= ROAD_STOP_TRACKBIT_FACTOR
;
2514 } else if (tt
== MP_ROAD
&& IsRoadDepot(tile
)) {
2515 num_pieces
*= ROAD_DEPOT_TRACKBIT_FACTOR
;
2518 found_convertible_road
= true;
2519 cost
.AddCost(num_pieces
* RoadConvertCost(from_type
, to_type
));
2521 if (flags
& DC_EXEC
) { // we can safely convert, too
2522 /* Call ConvertRoadTypeOwner() to update the company infrastructure counters. */
2523 if (owner
== _current_company
) {
2524 ConvertRoadTypeOwner(tile
, num_pieces
, owner
, from_type
, to_type
);
2527 /* Perform the conversion */
2528 SetRoadType(tile
, rtt
, to_type
);
2529 MarkTileDirtyByTile(tile
);
2531 /* update power of train on this tile */
2532 FindVehicleOnPos(tile
, &affected_rvs
, &UpdateRoadVehPowerProc
);
2534 if (IsRoadDepotTile(tile
)) {
2535 /* Update build vehicle window related to this depot */
2536 InvalidateWindowData(WC_VEHICLE_DEPOT
, tile
);
2537 InvalidateWindowData(WC_BUILD_VEHICLE
, tile
);
2541 TileIndex endtile
= GetOtherTunnelBridgeEnd(tile
);
2543 /* If both ends of tunnel/bridge are in the range, do not try to convert twice -
2544 * it would cause assert because of different test and exec runs */
2545 if (endtile
< tile
) {
2546 if (OrthogonalTileArea(area_start
, area_end
).Contains(endtile
)) continue;
2549 /* When not converting rail <-> el. rail, any vehicle cannot be in tunnel/bridge */
2550 if (!HasPowerOnRoad(from_type
, to_type
)) {
2551 CommandCost ret
= TunnelBridgeIsFree(tile
, endtile
);
2557 if (rtt
== RTT_ROAD
&& owner
== OWNER_TOWN
) {
2558 SetDParamsForOwnedBy(OWNER_TOWN
, tile
);
2559 error
.MakeError(STR_ERROR_OWNED_BY
);
2564 /* There are 2 pieces on *every* tile of the bridge or tunnel */
2565 uint num_pieces
= (GetTunnelBridgeLength(tile
, endtile
) + 2) * 2;
2566 found_convertible_road
= true;
2567 cost
.AddCost(num_pieces
* RoadConvertCost(from_type
, to_type
));
2569 if (flags
& DC_EXEC
) {
2570 /* Update the company infrastructure counters. */
2571 if (owner
== _current_company
) {
2572 /* Each piece should be counted TUNNELBRIDGE_TRACKBIT_FACTOR times
2573 * for the infrastructure counters (cause of #8297). */
2574 ConvertRoadTypeOwner(tile
, num_pieces
* TUNNELBRIDGE_TRACKBIT_FACTOR
, owner
, from_type
, to_type
);
2575 SetTunnelBridgeOwner(tile
, endtile
, _current_company
);
2578 /* Perform the conversion */
2579 SetRoadType(tile
, rtt
, to_type
);
2580 SetRoadType(endtile
, rtt
, to_type
);
2582 FindVehicleOnPos(tile
, &affected_rvs
, &UpdateRoadVehPowerProc
);
2583 FindVehicleOnPos(endtile
, &affected_rvs
, &UpdateRoadVehPowerProc
);
2585 if (IsBridge(tile
)) {
2586 MarkBridgeDirty(tile
);
2588 MarkTileDirtyByTile(tile
);
2589 MarkTileDirtyByTile(endtile
);
2595 if (flags
& DC_EXEC
) {
2596 /* Roadtype changed, update roadvehicles as when entering different track */
2597 for (RoadVehicle
*v
: affected_rvs
) {
2602 return found_convertible_road
? cost
: error
;
2606 /** Tile callback functions for road tiles */
2607 extern const TileTypeProcs _tile_type_road_procs
= {
2608 DrawTile_Road
, // draw_tile_proc
2609 GetSlopePixelZ_Road
, // get_slope_z_proc
2610 ClearTile_Road
, // clear_tile_proc
2611 nullptr, // add_accepted_cargo_proc
2612 GetTileDesc_Road
, // get_tile_desc_proc
2613 GetTileTrackStatus_Road
, // get_tile_track_status_proc
2614 ClickTile_Road
, // click_tile_proc
2615 nullptr, // animate_tile_proc
2616 TileLoop_Road
, // tile_loop_proc
2617 ChangeTileOwner_Road
, // change_tile_owner_proc
2618 nullptr, // add_produced_cargo_proc
2619 VehicleEnter_Road
, // vehicle_enter_tile_proc
2620 GetFoundation_Road
, // get_foundation_proc
2621 TerraformTile_Road
, // terraform_tile_proc