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. */
11 #include "cmd_helper.h"
13 #include "road_internal.h"
14 #include "viewport_func.h"
15 #include "command_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"
31 #include "company_base.h"
32 #include "core/random_func.hpp"
33 #include "newgrf_debug.h"
34 #include "newgrf_railtype.h"
35 #include "newgrf_roadtype.h"
36 #include "date_func.h"
38 #include "company_gui.h"
39 #include "road_func.h"
41 #include "table/strings.h"
42 #include "table/roadtypes.h"
44 #include "safeguards.h"
46 /** Helper type for lists/vectors of road vehicles */
47 typedef std::vector
<RoadVehicle
*> RoadVehicleList
;
49 RoadTypeInfo _roadtypes
[ROADTYPE_END
];
50 std::vector
<RoadType
> _sorted_roadtypes
;
51 RoadTypes _roadtypes_hidden_mask
;
54 * Bitmap of road/tram types.
55 * Bit if set if a roadtype is tram.
57 RoadTypes _roadtypes_type
;
60 * Reset all road type information to its default values.
64 static_assert(lengthof(_original_roadtypes
) <= lengthof(_roadtypes
));
67 for (; i
< lengthof(_original_roadtypes
); i
++) _roadtypes
[i
] = _original_roadtypes
[i
];
69 static const RoadTypeInfo empty_roadtype
= {
72 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {}, {}, 0, {}, {} },
73 ROADTYPES_NONE
, ROTFB_NONE
, 0, 0, 0, 0,
74 RoadTypeLabelList(), 0, 0, ROADTYPES_NONE
, ROADTYPES_NONE
, 0,
76 for (; i
< lengthof(_roadtypes
); i
++) _roadtypes
[i
] = empty_roadtype
;
78 _roadtypes_hidden_mask
= ROADTYPES_NONE
;
79 _roadtypes_type
= ROADTYPES_TRAM
;
82 void ResolveRoadTypeGUISprites(RoadTypeInfo
*rti
)
84 SpriteID cursors_base
= GetCustomRoadSprite(rti
, INVALID_TILE
, ROTSG_CURSORS
);
85 if (cursors_base
!= 0) {
86 rti
->gui_sprites
.build_y_road
= cursors_base
+ 0;
87 rti
->gui_sprites
.build_x_road
= cursors_base
+ 1;
88 rti
->gui_sprites
.auto_road
= cursors_base
+ 2;
89 rti
->gui_sprites
.build_depot
= cursors_base
+ 3;
90 rti
->gui_sprites
.build_tunnel
= cursors_base
+ 4;
91 rti
->gui_sprites
.convert_road
= cursors_base
+ 5;
92 rti
->cursor
.road_swne
= cursors_base
+ 6;
93 rti
->cursor
.road_nwse
= cursors_base
+ 7;
94 rti
->cursor
.autoroad
= cursors_base
+ 8;
95 rti
->cursor
.depot
= cursors_base
+ 9;
96 rti
->cursor
.tunnel
= cursors_base
+ 10;
97 rti
->cursor
.convert_road
= cursors_base
+ 11;
102 * Compare roadtypes based on their sorting order.
103 * @param first The roadtype to compare to.
104 * @param second The roadtype to compare.
105 * @return True iff the first should be sorted before the second.
107 static bool CompareRoadTypes(const RoadType
&first
, const RoadType
&second
)
109 if (RoadTypeIsRoad(first
) == RoadTypeIsRoad(second
)) {
110 return GetRoadTypeInfo(first
)->sorting_order
< GetRoadTypeInfo(second
)->sorting_order
;
112 return RoadTypeIsTram(first
) < RoadTypeIsTram(second
);
116 * Resolve sprites of custom road types
120 for (RoadType rt
= ROADTYPE_BEGIN
; rt
!= ROADTYPE_END
; rt
++) {
121 RoadTypeInfo
*rti
= &_roadtypes
[rt
];
122 ResolveRoadTypeGUISprites(rti
);
123 if (HasBit(rti
->flags
, ROTF_HIDDEN
)) SetBit(_roadtypes_hidden_mask
, rt
);
126 _sorted_roadtypes
.clear();
127 for (RoadType rt
= ROADTYPE_BEGIN
; rt
!= ROADTYPE_END
; rt
++) {
128 if (_roadtypes
[rt
].label
!= 0 && !HasBit(_roadtypes_hidden_mask
, rt
)) {
129 _sorted_roadtypes
.push_back(rt
);
132 std::sort(_sorted_roadtypes
.begin(), _sorted_roadtypes
.end(), CompareRoadTypes
);
136 * Allocate a new road type label
138 RoadType
AllocateRoadType(RoadTypeLabel label
, RoadTramType rtt
)
140 for (RoadType rt
= ROADTYPE_BEGIN
; rt
!= ROADTYPE_END
; rt
++) {
141 RoadTypeInfo
*rti
= &_roadtypes
[rt
];
143 if (rti
->label
== 0) {
144 /* Set up new road type */
145 *rti
= _original_roadtypes
[(rtt
== RTT_TRAM
) ? ROADTYPE_TRAM
: ROADTYPE_ROAD
];
147 rti
->alternate_labels
.clear();
148 rti
->flags
= ROTFB_NONE
;
149 rti
->introduction_date
= INVALID_DATE
;
151 /* Make us compatible with ourself. */
152 rti
->powered_roadtypes
= (RoadTypes
)(1ULL << rt
);
154 /* We also introduce ourself. */
155 rti
->introduces_roadtypes
= (RoadTypes
)(1ULL << rt
);
157 /* Default sort order; order of allocation, but with some
158 * offsets so it's easier for NewGRF to pick a spot without
159 * changing the order of other (original) road types.
160 * The << is so you can place other roadtypes in between the
161 * other roadtypes, the 7 is to be able to place something
162 * before the first (default) road type. */
163 rti
->sorting_order
= rt
<< 2 | 7;
165 /* Set bitmap of road/tram types */
166 if (rtt
== RTT_TRAM
) {
167 SetBit(_roadtypes_type
, rt
);
169 ClrBit(_roadtypes_type
, rt
);
176 return INVALID_ROADTYPE
;
180 * Verify whether a road vehicle is available.
181 * @return \c true if at least one road vehicle is available, \c false if not
183 bool RoadVehiclesAreBuilt()
185 return !RoadVehicle::Iterate().empty();
189 * Update road infrastructure counts for a company.
190 * @param rt Road type to update count of.
191 * @param o Owner of road piece.
192 * @param count Number of road pieces to adjust.
194 void UpdateCompanyRoadInfrastructure(RoadType rt
, Owner o
, int count
)
196 if (rt
== INVALID_ROADTYPE
) return;
198 Company
*c
= Company::GetIfValid(o
);
199 if (c
== nullptr) return;
201 c
->infrastructure
.road
[rt
] += count
;
202 DirtyCompanyInfrastructureWindows(c
->index
);
205 /** Invalid RoadBits on slopes. */
206 static const RoadBits _invalid_tileh_slopes_road
[2][15] = {
207 /* The inverse of the mixable RoadBits on a leveled slope */
209 ROAD_NONE
, // SLOPE_FLAT
210 ROAD_NE
| ROAD_SE
, // SLOPE_W
211 ROAD_NE
| ROAD_NW
, // SLOPE_S
214 ROAD_NW
| ROAD_SW
, // SLOPE_E
215 ROAD_NONE
, // SLOPE_EW
218 ROAD_NONE
, // SLOPE_WSE
219 ROAD_SE
| ROAD_SW
, // SLOPE_N
222 ROAD_NONE
, // SLOPE_NS
223 ROAD_NONE
, // SLOPE_ENW
226 ROAD_NONE
, // SLOPE_SEN
227 ROAD_NONE
// SLOPE_NWS
229 /* The inverse of the allowed straight roads on a slope
230 * (with and without a foundation). */
232 ROAD_NONE
, // SLOPE_FLAT
233 ROAD_NONE
, // SLOPE_W Foundation
234 ROAD_NONE
, // SLOPE_S Foundation
237 ROAD_NONE
, // SLOPE_E Foundation
238 ROAD_ALL
, // SLOPE_EW
241 ROAD_ALL
, // SLOPE_WSE
242 ROAD_NONE
, // SLOPE_N Foundation
245 ROAD_ALL
, // SLOPE_NS
246 ROAD_ALL
, // SLOPE_ENW
249 ROAD_ALL
, // SLOPE_SEN
254 static Foundation
GetRoadFoundation(Slope tileh
, RoadBits bits
);
257 * Is it allowed to remove the given road bits from the given tile?
258 * @param tile the tile to remove the road from
259 * @param remove the roadbits that are going to be removed
260 * @param owner the actual owner of the roadbits of the tile
261 * @param rt the road type to remove the bits from
262 * @param flags command flags
263 * @param town_check Shall the town rating checked/affected
264 * @return A succeeded command when it is allowed to remove the road bits, a failed command otherwise.
266 CommandCost
CheckAllowRemoveRoad(TileIndex tile
, RoadBits remove
, Owner owner
, RoadTramType rtt
, DoCommandFlag flags
, bool town_check
)
268 if (_game_mode
== GM_EDITOR
|| remove
== ROAD_NONE
) return CommandCost();
270 /* Water can always flood and towns can always remove "normal" road pieces.
271 * Towns are not be allowed to remove non "normal" road pieces, like tram
272 * tracks as that would result in trams that cannot turn. */
273 if (_current_company
== OWNER_WATER
||
274 (rtt
== RTT_ROAD
&& !Company::IsValidID(_current_company
))) return CommandCost();
276 /* Only do the special processing if the road is owned
278 if (owner
!= OWNER_TOWN
) {
279 if (owner
== OWNER_NONE
) return CommandCost();
280 CommandCost ret
= CheckOwnership(owner
);
284 if (!town_check
) return CommandCost();
286 if (_cheats
.magic_bulldozer
.value
) return CommandCost();
288 Town
*t
= ClosestTownFromTile(tile
, UINT_MAX
);
289 if (t
== nullptr) return CommandCost();
291 /* check if you're allowed to remove the street owned by a town
292 * removal allowance depends on difficulty setting */
293 CommandCost ret
= CheckforTownRating(flags
, t
, ROAD_REMOVE
);
294 if (ret
.Failed()) return ret
;
296 /* Get a bitmask of which neighbouring roads has a tile */
297 RoadBits n
= ROAD_NONE
;
298 RoadBits present
= GetAnyRoadBits(tile
, rtt
);
299 if ((present
& ROAD_NE
) && (GetAnyRoadBits(TILE_ADDXY(tile
, -1, 0), rtt
) & ROAD_SW
)) n
|= ROAD_NE
;
300 if ((present
& ROAD_SE
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 0, 1), rtt
) & ROAD_NW
)) n
|= ROAD_SE
;
301 if ((present
& ROAD_SW
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 1, 0), rtt
) & ROAD_NE
)) n
|= ROAD_SW
;
302 if ((present
& ROAD_NW
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 0, -1), rtt
) & ROAD_SE
)) n
|= ROAD_NW
;
304 int rating_decrease
= RATING_ROAD_DOWN_STEP_EDGE
;
305 /* If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
307 if (KillFirstBit(n
) != ROAD_NONE
&& (n
& remove
) != ROAD_NONE
) {
308 /* you can remove all kind of roads with extra dynamite */
309 if (!_settings_game
.construction
.extra_dynamite
) {
310 SetDParam(0, t
->index
);
311 return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS
);
313 rating_decrease
= RATING_ROAD_DOWN_STEP_INNER
;
315 ChangeTownRating(t
, rating_decrease
, RATING_ROAD_MINIMUM
, flags
);
317 return CommandCost();
322 * Delete a piece of road.
323 * @param tile tile where to remove road from
324 * @param flags operation to perform
325 * @param pieces roadbits to remove
326 * @param rt roadtype to remove
327 * @param crossing_check should we check if there is a tram track when we are removing road from crossing?
328 * @param town_check should we check if the town allows removal?
330 static CommandCost
RemoveRoad(TileIndex tile
, DoCommandFlag flags
, RoadBits pieces
, RoadTramType rtt
, bool crossing_check
, bool town_check
= true)
332 assert(pieces
!= ROAD_NONE
);
334 RoadType existing_rt
= MayHaveRoad(tile
) ? GetRoadType(tile
, rtt
) : INVALID_ROADTYPE
;
335 /* The tile doesn't have the given road type */
336 if (existing_rt
== INVALID_ROADTYPE
) return_cmd_error((rtt
== RTT_TRAM
) ? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
338 switch (GetTileType(tile
)) {
340 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
341 if (ret
.Failed()) return ret
;
346 if (!IsDriveThroughStopTile(tile
)) return CMD_ERROR
;
348 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
349 if (ret
.Failed()) return ret
;
353 case MP_TUNNELBRIDGE
: {
354 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) return CMD_ERROR
;
355 CommandCost ret
= TunnelBridgeIsFree(tile
, GetOtherTunnelBridgeEnd(tile
));
356 if (ret
.Failed()) return ret
;
364 CommandCost ret
= CheckAllowRemoveRoad(tile
, pieces
, GetRoadOwner(tile
, rtt
), rtt
, flags
, town_check
);
365 if (ret
.Failed()) return ret
;
367 if (!IsTileType(tile
, MP_ROAD
)) {
368 /* If it's the last roadtype, just clear the whole tile */
369 if (GetRoadType(tile
, OtherRoadTramType(rtt
)) == INVALID_ROADTYPE
) return DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
371 CommandCost
cost(EXPENSES_CONSTRUCTION
);
372 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
373 /* Removing any roadbit in the bridge axis removes the roadtype (that's the behaviour remove-long-roads needs) */
374 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
);
376 TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
377 /* Pay for *every* tile of the bridge or tunnel */
378 uint len
= GetTunnelBridgeLength(other_end
, tile
) + 2;
379 cost
.AddCost(len
* 2 * RoadClearCost(existing_rt
));
380 if (flags
& DC_EXEC
) {
381 /* A full diagonal road tile has two road bits. */
382 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -(int)(len
* 2 * TUNNELBRIDGE_TRACKBIT_FACTOR
));
384 SetRoadType(other_end
, rtt
, INVALID_ROADTYPE
);
385 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
387 /* If the owner of the bridge sells all its road, also move the ownership
388 * to the owner of the other roadtype, unless the bridge owner is a town. */
389 Owner other_owner
= GetRoadOwner(tile
, OtherRoadTramType(rtt
));
390 if (!IsTileOwner(tile
, other_owner
) && !IsTileOwner(tile
, OWNER_TOWN
)) {
391 SetTileOwner(tile
, other_owner
);
392 SetTileOwner(other_end
, other_owner
);
395 /* Mark tiles dirty that have been repaved */
396 if (IsBridge(tile
)) {
397 MarkBridgeDirty(tile
);
399 MarkTileDirtyByTile(tile
);
400 MarkTileDirtyByTile(other_end
);
404 assert(IsDriveThroughStopTile(tile
));
405 cost
.AddCost(RoadClearCost(existing_rt
) * 2);
406 if (flags
& DC_EXEC
) {
407 /* A full diagonal road tile has two road bits. */
408 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -2);
409 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
410 MarkTileDirtyByTile(tile
);
416 switch (GetRoadTileType(tile
)) {
417 case ROAD_TILE_NORMAL
: {
418 Slope tileh
= GetTileSlope(tile
);
420 /* Steep slopes behave the same as slopes with one corner raised. */
421 if (IsSteepSlope(tileh
)) {
422 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
425 RoadBits present
= GetRoadBits(tile
, rtt
);
426 const RoadBits other
= GetRoadBits(tile
, OtherRoadTramType(rtt
));
427 const Foundation f
= GetRoadFoundation(tileh
, present
);
429 if (HasRoadWorks(tile
) && _current_company
!= OWNER_WATER
) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS
);
431 /* Autocomplete to a straight road
432 * @li if the bits of the other roadtypes result in another foundation
433 * @li if build on slopes is disabled */
434 if ((IsStraightRoad(other
) && (other
& _invalid_tileh_slopes_road
[0][tileh
& SLOPE_ELEVATED
]) != ROAD_NONE
) ||
435 (tileh
!= SLOPE_FLAT
&& !_settings_game
.construction
.build_on_slopes
)) {
436 pieces
|= MirrorRoadBits(pieces
);
439 /* limit the bits to delete to the existing bits. */
441 if (pieces
== ROAD_NONE
) return_cmd_error((rtt
== RTT_TRAM
) ? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
443 /* Now set present what it will be after the remove */
446 /* Check for invalid RoadBit combinations on slopes */
447 if (tileh
!= SLOPE_FLAT
&& present
!= ROAD_NONE
&&
448 (present
& _invalid_tileh_slopes_road
[0][tileh
& SLOPE_ELEVATED
]) == present
) {
452 if (flags
& DC_EXEC
) {
453 if (HasRoadWorks(tile
)) {
454 /* flooding tile with road works, don't forget to remove the effect vehicle too */
455 assert(_current_company
== OWNER_WATER
);
456 for (EffectVehicle
*v
: EffectVehicle::Iterate()) {
457 if (TileVirtXY(v
->x_pos
, v
->y_pos
) == tile
) {
463 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -(int)CountBits(pieces
));
465 if (present
== ROAD_NONE
) {
466 /* No other road type, just clear tile. */
467 if (GetRoadType(tile
, OtherRoadTramType(rtt
)) == INVALID_ROADTYPE
) {
468 /* Includes MarkTileDirtyByTile() */
471 if (rtt
== RTT_ROAD
&& IsRoadOwner(tile
, rtt
, OWNER_TOWN
)) {
472 /* Update nearest-town index */
473 const Town
*town
= CalcClosestTownFromTile(tile
);
474 SetTownIndex(tile
, town
== nullptr ? INVALID_TOWN
: town
->index
);
476 SetRoadBits(tile
, ROAD_NONE
, rtt
);
477 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
478 MarkTileDirtyByTile(tile
);
481 /* When bits are removed, you *always* end up with something that
482 * is not a complete straight road tile. However, trams do not have
483 * onewayness, so they cannot remove it either. */
484 if (rtt
== RTT_ROAD
) SetDisallowedRoadDirections(tile
, DRD_NONE
);
485 SetRoadBits(tile
, present
, rtt
);
486 MarkTileDirtyByTile(tile
);
490 CommandCost
cost(EXPENSES_CONSTRUCTION
, CountBits(pieces
) * RoadClearCost(existing_rt
));
491 /* If we build a foundation we have to pay for it. */
492 if (f
== FOUNDATION_NONE
&& GetRoadFoundation(tileh
, present
) != FOUNDATION_NONE
) cost
.AddCost(_price
[PR_BUILD_FOUNDATION
]);
497 case ROAD_TILE_CROSSING
: {
498 if (pieces
& ComplementRoadBits(GetCrossingRoadBits(tile
))) {
502 if (flags
& DC_EXEC
) {
503 /* A full diagonal road tile has two road bits. */
504 UpdateCompanyRoadInfrastructure(existing_rt
, GetRoadOwner(tile
, rtt
), -2);
506 Track railtrack
= GetCrossingRailTrack(tile
);
507 if (GetRoadType(tile
, OtherRoadTramType(rtt
)) == INVALID_ROADTYPE
) {
508 TrackBits tracks
= GetCrossingRailBits(tile
);
509 bool reserved
= HasCrossingReservation(tile
);
510 MakeRailNormal(tile
, GetTileOwner(tile
), tracks
, GetRailType(tile
));
511 if (reserved
) SetTrackReservation(tile
, tracks
);
513 /* Update rail count for level crossings. The plain track should still be accounted
514 * for, so only subtract the difference to the level crossing cost. */
515 Company
*c
= Company::GetIfValid(GetTileOwner(tile
));
517 c
->infrastructure
.rail
[GetRailType(tile
)] -= LEVELCROSSING_TRACKBIT_FACTOR
- 1;
518 DirtyCompanyInfrastructureWindows(c
->index
);
521 SetRoadType(tile
, rtt
, INVALID_ROADTYPE
);
523 MarkTileDirtyByTile(tile
);
524 YapfNotifyTrackLayoutChange(tile
, railtrack
);
526 return CommandCost(EXPENSES_CONSTRUCTION
, RoadClearCost(existing_rt
) * 2);
530 case ROAD_TILE_DEPOT
:
537 * Calculate the costs for roads on slopes
538 * Aside modify the RoadBits to fit on the slopes
540 * @note The RoadBits are modified too!
541 * @param tileh The current slope
542 * @param pieces The RoadBits we want to add
543 * @param existing The existent RoadBits of the current type
544 * @param other The other existent RoadBits
545 * @return The costs for these RoadBits on this slope
547 static CommandCost
CheckRoadSlope(Slope tileh
, RoadBits
*pieces
, RoadBits existing
, RoadBits other
)
549 /* Remove already build pieces */
550 CLRBITS(*pieces
, existing
);
552 /* If we can't build anything stop here */
553 if (*pieces
== ROAD_NONE
) return CMD_ERROR
;
555 /* All RoadBit combos are valid on flat land */
556 if (tileh
== SLOPE_FLAT
) return CommandCost();
558 /* Steep slopes behave the same as slopes with one corner raised. */
559 if (IsSteepSlope(tileh
)) {
560 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
563 /* Save the merge of all bits of the current type */
564 RoadBits type_bits
= existing
| *pieces
;
566 /* Roads on slopes */
567 if (_settings_game
.construction
.build_on_slopes
&& (_invalid_tileh_slopes_road
[0][tileh
] & (other
| type_bits
)) == ROAD_NONE
) {
569 /* If we add leveling we've got to pay for it */
570 if ((other
| existing
) == ROAD_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
572 return CommandCost();
575 /* Autocomplete uphill roads */
576 *pieces
|= MirrorRoadBits(*pieces
);
577 type_bits
= existing
| *pieces
;
580 if (IsStraightRoad(type_bits
) && (other
== type_bits
|| other
== ROAD_NONE
) &&
581 (_invalid_tileh_slopes_road
[1][tileh
] & (other
| type_bits
)) == ROAD_NONE
) {
583 /* Slopes with foundation ? */
584 if (IsSlopeWithOneCornerRaised(tileh
)) {
586 /* Prevent build on slopes if it isn't allowed */
587 if (_settings_game
.construction
.build_on_slopes
) {
589 /* If we add foundation we've got to pay for it */
590 if ((other
| existing
) == ROAD_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
592 return CommandCost();
595 if (HasExactlyOneBit(existing
) && GetRoadFoundation(tileh
, existing
) == FOUNDATION_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
596 return CommandCost();
603 * Build a piece of road.
604 * @param tile tile where to build road
605 * @param flags operation to perform
606 * @param p1 bit 0..3 road pieces to build (RoadBits)
608 * bit 11..12 disallowed directions to toggle
609 * @param p2 the town that is building the road (0 if not applicable)
611 * @return the cost of this operation or an error
613 CommandCost
CmdBuildRoad(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
615 CompanyID company
= _current_company
;
616 CommandCost
cost(EXPENSES_CONSTRUCTION
);
618 RoadBits existing
= ROAD_NONE
;
619 RoadBits other_bits
= ROAD_NONE
;
621 /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
622 * if a non-company is building the road */
623 if ((Company::IsValidID(company
) && p2
!= 0) || (company
== OWNER_TOWN
&& !Town::IsValidID(p2
)) || (company
== OWNER_DEITY
&& p2
!= 0)) return CMD_ERROR
;
624 if (company
!= OWNER_TOWN
) {
625 const Town
*town
= CalcClosestTownFromTile(tile
);
626 p2
= (town
!= nullptr) ? town
->index
: INVALID_TOWN
;
628 if (company
== OWNER_DEITY
) {
629 company
= OWNER_TOWN
;
631 /* If we are not within a town, we are not owned by the town */
632 if (town
== nullptr || DistanceSquare(tile
, town
->xy
) > town
->cache
.squared_town_zone_radius
[HZB_TOWN_EDGE
]) {
633 company
= OWNER_NONE
;
638 RoadBits pieces
= Extract
<RoadBits
, 0, 4>(p1
);
640 /* do not allow building 'zero' road bits, code wouldn't handle it */
641 if (pieces
== ROAD_NONE
) return CMD_ERROR
;
643 RoadType rt
= Extract
<RoadType
, 4, 6>(p1
);
644 if (!ValParamRoadType(rt
)) return CMD_ERROR
;
646 DisallowedRoadDirections toggle_drd
= Extract
<DisallowedRoadDirections
, 11, 2>(p1
);
648 Slope tileh
= GetTileSlope(tile
);
649 RoadTramType rtt
= GetRoadTramType(rt
);
651 bool need_to_clear
= false;
652 switch (GetTileType(tile
)) {
654 switch (GetRoadTileType(tile
)) {
655 case ROAD_TILE_NORMAL
: {
656 if (HasRoadWorks(tile
)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS
);
658 other_bits
= GetRoadBits(tile
, OtherRoadTramType(rtt
));
659 if (!HasTileRoadType(tile
, rtt
)) break;
661 existing
= GetRoadBits(tile
, rtt
);
662 bool crossing
= !IsStraightRoad(existing
| pieces
);
663 if (rtt
== RTT_ROAD
&& (GetDisallowedRoadDirections(tile
) != DRD_NONE
|| toggle_drd
!= DRD_NONE
) && crossing
) {
664 /* Junctions cannot be one-way */
665 return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION
);
667 if ((existing
& pieces
) == pieces
) {
668 /* We only want to set the (dis)allowed road directions */
669 if (toggle_drd
!= DRD_NONE
&& rtt
== RTT_ROAD
) {
670 if (crossing
) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION
);
672 Owner owner
= GetRoadOwner(tile
, rtt
);
673 if (owner
!= OWNER_NONE
) {
674 CommandCost ret
= CheckOwnership(owner
, tile
);
675 if (ret
.Failed()) return ret
;
678 DisallowedRoadDirections dis_existing
= GetDisallowedRoadDirections(tile
);
679 DisallowedRoadDirections dis_new
= dis_existing
^ toggle_drd
;
681 /* We allow removing disallowed directions to break up
682 * deadlocks, but adding them can break articulated
683 * vehicles. As such, only when less is disallowed,
684 * i.e. bits are removed, we skip the vehicle check. */
685 if (CountBits(dis_existing
) <= CountBits(dis_new
)) {
686 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
687 if (ret
.Failed()) return ret
;
690 /* Ignore half built tiles */
691 if ((flags
& DC_EXEC
) && IsStraightRoad(existing
)) {
692 SetDisallowedRoadDirections(tile
, dis_new
);
693 MarkTileDirtyByTile(tile
);
695 return CommandCost();
697 return_cmd_error(STR_ERROR_ALREADY_BUILT
);
699 /* Disallow breaking end-of-line of someone else
700 * so trams can still reverse on this tile. */
701 if (rtt
== RTT_TRAM
&& HasExactlyOneBit(existing
)) {
702 Owner owner
= GetRoadOwner(tile
, rtt
);
703 if (Company::IsValidID(owner
)) {
704 CommandCost ret
= CheckOwnership(owner
);
705 if (ret
.Failed()) return ret
;
711 case ROAD_TILE_CROSSING
:
712 if (RoadNoLevelCrossing(rt
)) {
713 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_ROAD
);
716 other_bits
= GetCrossingRoadBits(tile
);
717 if (pieces
& ComplementRoadBits(other_bits
)) goto do_clear
;
718 pieces
= other_bits
; // we need to pay for both roadbits
720 if (HasTileRoadType(tile
, rtt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
723 case ROAD_TILE_DEPOT
:
724 if ((GetAnyRoadBits(tile
, rtt
) & pieces
) == pieces
) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
727 default: NOT_REACHED();
732 if (IsSteepSlope(tileh
)) {
733 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
736 /* Level crossings may only be built on these slopes */
737 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES
, tileh
)) {
738 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
741 if (GetRailTileType(tile
) != RAIL_TILE_NORMAL
) goto do_clear
;
743 if (RoadNoLevelCrossing(rt
)) {
744 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_ROAD
);
747 if (RailNoLevelCrossings(GetRailType(tile
))) {
748 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_RAIL
);
752 switch (GetTrackBits(tile
)) {
754 if (pieces
& ROAD_X
) goto do_clear
;
759 if (pieces
& ROAD_Y
) goto do_clear
;
763 default: goto do_clear
;
766 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
767 if (ret
.Failed()) return ret
;
769 if (flags
& DC_EXEC
) {
770 Track railtrack
= AxisToTrack(OtherAxis(roaddir
));
771 YapfNotifyTrackLayoutChange(tile
, railtrack
);
772 /* Update company infrastructure counts. A level crossing has two road bits. */
773 UpdateCompanyRoadInfrastructure(rt
, company
, 2);
775 /* Update rail count for level crossings. The plain track is already
776 * counted, so only add the difference to the level crossing cost. */
777 Company
*c
= Company::GetIfValid(GetTileOwner(tile
));
779 c
->infrastructure
.rail
[GetRailType(tile
)] += LEVELCROSSING_TRACKBIT_FACTOR
- 1;
780 DirtyCompanyInfrastructureWindows(c
->index
);
783 /* Always add road to the roadtypes (can't draw without it) */
784 bool reserved
= HasBit(GetRailReservationTrackBits(tile
), railtrack
);
785 MakeRoadCrossing(tile
, company
, company
, GetTileOwner(tile
), roaddir
, GetRailType(tile
), rtt
== RTT_ROAD
? rt
: INVALID_ROADTYPE
, (rtt
== RTT_TRAM
) ? rt
: INVALID_ROADTYPE
, p2
);
786 SetCrossingReservation(tile
, reserved
);
787 UpdateLevelCrossing(tile
, false);
788 MarkTileDirtyByTile(tile
);
790 return CommandCost(EXPENSES_CONSTRUCTION
, 2 * RoadBuildCost(rt
));
794 if ((GetAnyRoadBits(tile
, rtt
) & pieces
) == pieces
) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
795 if (!IsDriveThroughStopTile(tile
)) goto do_clear
;
797 RoadBits curbits
= AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile
)));
798 if (pieces
& ~curbits
) goto do_clear
;
799 pieces
= curbits
; // we need to pay for both roadbits
801 if (HasTileRoadType(tile
, rtt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
805 case MP_TUNNELBRIDGE
: {
806 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) goto do_clear
;
807 /* Only allow building the outern roadbit, so building long roads stops at existing bridges */
808 if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile
))) != pieces
) goto do_clear
;
809 if (HasTileRoadType(tile
, rtt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
810 /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
811 CommandCost ret
= TunnelBridgeIsFree(tile
, GetOtherTunnelBridgeEnd(tile
));
812 if (ret
.Failed()) return ret
;
818 need_to_clear
= true;
824 CommandCost ret
= DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
825 if (ret
.Failed()) return ret
;
829 if (other_bits
!= pieces
) {
830 /* Check the foundation/slopes when adding road/tram bits */
831 CommandCost ret
= CheckRoadSlope(tileh
, &pieces
, existing
, other_bits
);
832 /* Return an error if we need to build a foundation (ret != 0) but the
833 * current setting is turned off */
834 if (ret
.Failed() || (ret
.GetCost() != 0 && !_settings_game
.construction
.build_on_slopes
)) {
835 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
840 if (!need_to_clear
) {
841 if (IsTileType(tile
, MP_ROAD
)) {
842 /* Don't put the pieces that already exist */
843 pieces
&= ComplementRoadBits(existing
);
845 /* Check if new road bits will have the same foundation as other existing road types */
846 if (IsNormalRoad(tile
)) {
847 Slope slope
= GetTileSlope(tile
);
848 Foundation found_new
= GetRoadFoundation(slope
, pieces
| existing
);
850 RoadBits bits
= GetRoadBits(tile
, OtherRoadTramType(rtt
));
851 /* do not check if there are not road bits of given type */
852 if (bits
!= ROAD_NONE
&& GetRoadFoundation(slope
, bits
) != found_new
) {
853 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
858 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
859 if (ret
.Failed()) return ret
;
861 if (IsNormalRoadTile(tile
)) {
862 /* If the road types don't match, try to convert only if vehicles of
863 * the new road type are not powered on the present road type and vehicles of
864 * the present road type are powered on the new road type. */
865 RoadType existing_rt
= GetRoadType(tile
, rtt
);
866 if (existing_rt
!= INVALID_ROADTYPE
&& existing_rt
!= rt
) {
867 if (HasPowerOnRoad(rt
, existing_rt
)) {
869 } else if (HasPowerOnRoad(existing_rt
, rt
)) {
870 CommandCost ret
= DoCommand(tile
, tile
, rt
, flags
, CMD_CONVERT_ROAD
);
871 if (ret
.Failed()) return ret
;
880 uint num_pieces
= (!need_to_clear
&& IsTileType(tile
, MP_TUNNELBRIDGE
)) ?
881 /* There are 2 pieces on *every* tile of the bridge or tunnel */
882 2 * (GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile
), tile
) + 2) :
886 cost
.AddCost(num_pieces
* RoadBuildCost(rt
));
888 if (flags
& DC_EXEC
) {
889 switch (GetTileType(tile
)) {
891 RoadTileType rttype
= GetRoadTileType(tile
);
892 if (existing
== ROAD_NONE
|| rttype
== ROAD_TILE_CROSSING
) {
893 SetRoadType(tile
, rtt
, rt
);
894 SetRoadOwner(tile
, rtt
, company
);
895 if (rtt
== RTT_ROAD
) SetTownIndex(tile
, p2
);
897 if (rttype
!= ROAD_TILE_CROSSING
) SetRoadBits(tile
, existing
| pieces
, rtt
);
901 case MP_TUNNELBRIDGE
: {
902 TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
904 SetRoadType(other_end
, rtt
, rt
);
905 SetRoadType(tile
, rtt
, rt
);
906 SetRoadOwner(other_end
, rtt
, company
);
907 SetRoadOwner(tile
, rtt
, company
);
909 /* Mark tiles dirty that have been repaved */
910 if (IsBridge(tile
)) {
911 MarkBridgeDirty(tile
);
913 MarkTileDirtyByTile(other_end
);
914 MarkTileDirtyByTile(tile
);
920 assert(IsDriveThroughStopTile(tile
));
921 SetRoadType(tile
, rtt
, rt
);
922 SetRoadOwner(tile
, rtt
, company
);
927 MakeRoadNormal(tile
, pieces
, (rtt
== RTT_ROAD
) ? rt
: INVALID_ROADTYPE
, (rtt
== RTT_TRAM
) ? rt
: INVALID_ROADTYPE
, p2
, company
, company
);
931 /* Update company infrastructure count. */
932 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) num_pieces
*= TUNNELBRIDGE_TRACKBIT_FACTOR
;
933 UpdateCompanyRoadInfrastructure(rt
, GetRoadOwner(tile
, rtt
), num_pieces
);
935 if (rtt
== RTT_ROAD
&& IsNormalRoadTile(tile
)) {
937 SetDisallowedRoadDirections(tile
, IsStraightRoad(existing
) ?
938 GetDisallowedRoadDirections(tile
) ^ toggle_drd
: DRD_NONE
);
941 MarkTileDirtyByTile(tile
);
947 * Checks whether a road or tram connection can be found when building a new road or tram.
948 * @param tile Tile at which the road being built will end.
949 * @param rt Roadtype of the road being built.
950 * @param dir Direction that the road is following.
951 * @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.
953 static bool CanConnectToRoad(TileIndex tile
, RoadType rt
, DiagDirection dir
)
955 tile
+= TileOffsByDiagDir(dir
);
956 if (!IsValidTile(tile
) || !MayHaveRoad(tile
)) return false;
958 RoadTramType rtt
= GetRoadTramType(rt
);
959 RoadType existing
= GetRoadType(tile
, rtt
);
960 if (existing
== INVALID_ROADTYPE
) return false;
961 if (!HasPowerOnRoad(existing
, rt
) && !HasPowerOnRoad(rt
, existing
)) return false;
963 RoadBits bits
= GetAnyRoadBits(tile
, rtt
, false);
964 return (bits
& DiagDirToRoadBits(ReverseDiagDir(dir
))) != 0;
968 * Build a long piece of road.
969 * @param start_tile start tile of drag (the building cost will appear over this tile)
970 * @param flags operation to perform
971 * @param p1 end tile of drag
972 * @param p2 various bitstuffed elements
973 * - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1). Only used if bit 6 is set or if we are building a single tile
974 * - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2). Only used if bit 6 is set or if we are building a single tile
975 * - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4)
976 * - p2 = (bit 3..8) - road type
977 * - p2 = (bit 10) - set road direction
978 * - p2 = (bit 11) - defines two different behaviors for this command:
979 * - 0 = 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
980 * - 1 = Fail if an obstacle is found. Always take into account bit 0 and 1. This behavior is used for scripts
982 * @return the cost of this operation or an error
984 CommandCost
CmdBuildLongRoad(TileIndex start_tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
986 DisallowedRoadDirections drd
= DRD_NORTHBOUND
;
988 if (p1
>= MapSize()) return CMD_ERROR
;
989 TileIndex end_tile
= p1
;
991 RoadType rt
= Extract
<RoadType
, 3, 6>(p2
);
992 if (!ValParamRoadType(rt
)) return CMD_ERROR
;
994 Axis axis
= Extract
<Axis
, 2, 1>(p2
);
995 /* Only drag in X or Y direction dictated by the direction variable */
996 if (axis
== AXIS_X
&& TileY(start_tile
) != TileY(end_tile
)) return CMD_ERROR
; // x-axis
997 if (axis
== AXIS_Y
&& TileX(start_tile
) != TileX(end_tile
)) return CMD_ERROR
; // y-axis
999 DiagDirection dir
= AxisToDiagDir(axis
);
1001 /* Swap direction, also the half-tile drag var (bit 0 and 1) */
1002 if (start_tile
> end_tile
|| (start_tile
== end_tile
&& HasBit(p2
, 0))) {
1003 dir
= ReverseDiagDir(dir
);
1005 drd
= DRD_SOUTHBOUND
;
1008 /* On the X-axis, we have to swap the initial bits, so they
1009 * will be interpreted correctly in the GTTS. Furthermore
1010 * when you just 'click' on one tile to build them. */
1011 if ((axis
== AXIS_Y
) == (start_tile
== end_tile
&& HasBit(p2
, 0) == HasBit(p2
, 1))) drd
^= DRD_BOTH
;
1012 /* No disallowed direction bits have to be toggled */
1013 if (!HasBit(p2
, 10)) drd
= DRD_NONE
;
1015 CommandCost
cost(EXPENSES_CONSTRUCTION
);
1016 CommandCost last_error
= CMD_ERROR
;
1017 TileIndex tile
= start_tile
;
1018 bool had_bridge
= false;
1019 bool had_tunnel
= false;
1020 bool had_success
= false;
1021 bool is_ai
= HasBit(p2
, 11);
1023 /* Start tile is the first tile clicked by the user. */
1025 RoadBits bits
= AxisToRoadBits(axis
);
1027 /* Determine which road parts should be built. */
1028 if (!is_ai
&& start_tile
!= end_tile
) {
1029 /* Only build the first and last roadbit if they can connect to something. */
1030 if (tile
== end_tile
&& !CanConnectToRoad(tile
, rt
, dir
)) {
1031 bits
= DiagDirToRoadBits(ReverseDiagDir(dir
));
1032 } else if (tile
== start_tile
&& !CanConnectToRoad(tile
, rt
, ReverseDiagDir(dir
))) {
1033 bits
= DiagDirToRoadBits(dir
);
1036 /* Road parts only have to be built at the start tile or at the end tile. */
1037 if (tile
== end_tile
&& !HasBit(p2
, 1)) bits
&= DiagDirToRoadBits(ReverseDiagDir(dir
));
1038 if (tile
== start_tile
&& HasBit(p2
, 0)) bits
&= DiagDirToRoadBits(dir
);
1041 CommandCost ret
= DoCommand(tile
, drd
<< 11 | rt
<< 4 | bits
, 0, flags
, CMD_BUILD_ROAD
);
1044 if (last_error
.GetErrorMessage() != STR_ERROR_ALREADY_BUILT
) {
1045 if (is_ai
) return last_error
;
1050 /* Only pay for the upgrade on one side of the bridges and tunnels */
1051 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
1052 if (IsBridge(tile
)) {
1053 if (!had_bridge
|| GetTunnelBridgeDirection(tile
) == dir
) {
1057 } else { // IsTunnel(tile)
1058 if (!had_tunnel
|| GetTunnelBridgeDirection(tile
) == dir
) {
1068 if (tile
== end_tile
) break;
1070 tile
+= TileOffsByDiagDir(dir
);
1073 return had_success
? cost
: last_error
;
1077 * Remove a long piece of road.
1078 * @param start_tile start tile of drag
1079 * @param flags operation to perform
1080 * @param p1 end tile of drag
1081 * @param p2 various bitstuffed elements
1082 * - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1)
1083 * - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2)
1084 * - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4)
1085 * - p2 = (bit 3 - 8) - road type
1086 * @param text unused
1087 * @return the cost of this operation or an error
1089 CommandCost
CmdRemoveLongRoad(TileIndex start_tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1091 CommandCost
cost(EXPENSES_CONSTRUCTION
);
1093 if (p1
>= MapSize()) return CMD_ERROR
;
1095 TileIndex end_tile
= p1
;
1096 RoadType rt
= Extract
<RoadType
, 3, 6>(p2
);
1097 if (!ValParamRoadType(rt
)) return CMD_ERROR
;
1099 Axis axis
= Extract
<Axis
, 2, 1>(p2
);
1100 /* Only drag in X or Y direction dictated by the direction variable */
1101 if (axis
== AXIS_X
&& TileY(start_tile
) != TileY(end_tile
)) return CMD_ERROR
; // x-axis
1102 if (axis
== AXIS_Y
&& TileX(start_tile
) != TileX(end_tile
)) return CMD_ERROR
; // y-axis
1104 /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
1105 if (start_tile
> end_tile
|| (start_tile
== end_tile
&& HasBit(p2
, 0))) {
1106 TileIndex t
= start_tile
;
1107 start_tile
= end_tile
;
1109 p2
^= IsInsideMM(p2
& 3, 1, 3) ? 3 : 0;
1112 Money money_available
= GetAvailableMoneyForCommand();
1113 Money money_spent
= 0;
1114 TileIndex tile
= start_tile
;
1115 CommandCost last_error
= CMD_ERROR
;
1116 bool had_success
= false;
1117 /* Start tile is the small number. */
1119 RoadBits bits
= AxisToRoadBits(axis
);
1121 if (tile
== end_tile
&& !HasBit(p2
, 1)) bits
&= ROAD_NW
| ROAD_NE
;
1122 if (tile
== start_tile
&& HasBit(p2
, 0)) bits
&= ROAD_SE
| ROAD_SW
;
1124 /* try to remove the halves. */
1126 RoadTramType rtt
= GetRoadTramType(rt
);
1127 CommandCost ret
= RemoveRoad(tile
, flags
& ~DC_EXEC
, bits
, rtt
, true);
1128 if (ret
.Succeeded()) {
1129 if (flags
& DC_EXEC
) {
1130 money_spent
+= ret
.GetCost();
1131 if (money_spent
> 0 && money_spent
> money_available
) {
1132 _additional_cash_required
= DoCommand(start_tile
, end_tile
, p2
, flags
& ~DC_EXEC
, CMD_REMOVE_LONG_ROAD
).GetCost();
1135 RemoveRoad(tile
, flags
, bits
, rtt
, true, false);
1140 /* Ownership errors are more important. */
1141 if (last_error
.GetErrorMessage() != STR_ERROR_OWNED_BY
) last_error
= ret
;
1145 if (tile
== end_tile
) break;
1147 tile
+= (axis
== AXIS_Y
) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
1150 return had_success
? cost
: last_error
;
1154 * Build a road depot.
1155 * @param tile tile where to build the depot
1156 * @param flags operation to perform
1157 * @param p1 bit 0..1 entrance direction (DiagDirection)
1158 * bit 2..7 road type
1160 * @param text unused
1161 * @return the cost of this operation or an error
1163 * @todo When checking for the tile slope,
1164 * distinguish between "Flat land required" and "land sloped in wrong direction"
1166 CommandCost
CmdBuildRoadDepot(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1168 DiagDirection dir
= Extract
<DiagDirection
, 0, 2>(p1
);
1170 RoadType rt
= Extract
<RoadType
, 2, 6>(p1
);
1171 if (!ValParamRoadType(rt
)) return CMD_ERROR
;
1173 CommandCost
cost(EXPENSES_CONSTRUCTION
);
1175 Slope tileh
= GetTileSlope(tile
);
1176 if (tileh
!= SLOPE_FLAT
) {
1177 if (!_settings_game
.construction
.build_on_slopes
|| !CanBuildDepotByTileh(dir
, tileh
)) {
1178 return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED
);
1180 cost
.AddCost(_price
[PR_BUILD_FOUNDATION
]);
1183 cost
.AddCost(DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
));
1184 if (cost
.Failed()) return cost
;
1186 if (IsBridgeAbove(tile
)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
1188 if (!Depot::CanAllocateItem()) return CMD_ERROR
;
1190 if (flags
& DC_EXEC
) {
1191 Depot
*dep
= new Depot(tile
);
1192 dep
->build_date
= _date
;
1194 /* A road depot has two road bits. */
1195 UpdateCompanyRoadInfrastructure(rt
, _current_company
, ROAD_DEPOT_TRACKBIT_FACTOR
);
1197 MakeRoadDepot(tile
, _current_company
, dep
->index
, dir
, rt
);
1198 MarkTileDirtyByTile(tile
);
1199 MakeDefaultName(dep
);
1201 cost
.AddCost(_price
[PR_BUILD_DEPOT_ROAD
]);
1205 static CommandCost
RemoveRoadDepot(TileIndex tile
, DoCommandFlag flags
)
1207 if (_current_company
!= OWNER_WATER
) {
1208 CommandCost ret
= CheckTileOwnership(tile
);
1209 if (ret
.Failed()) return ret
;
1212 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
1213 if (ret
.Failed()) return ret
;
1215 if (flags
& DC_EXEC
) {
1216 Company
*c
= Company::GetIfValid(GetTileOwner(tile
));
1218 /* A road depot has two road bits. */
1219 RoadType rt
= GetRoadTypeRoad(tile
);
1220 if (rt
== INVALID_ROADTYPE
) rt
= GetRoadTypeTram(tile
);
1221 c
->infrastructure
.road
[rt
] -= ROAD_DEPOT_TRACKBIT_FACTOR
;
1222 DirtyCompanyInfrastructureWindows(c
->index
);
1225 delete Depot::GetByTile(tile
);
1226 DoClearSquare(tile
);
1229 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_CLEAR_DEPOT_ROAD
]);
1232 static CommandCost
ClearTile_Road(TileIndex tile
, DoCommandFlag flags
)
1234 switch (GetRoadTileType(tile
)) {
1235 case ROAD_TILE_NORMAL
: {
1236 RoadBits b
= GetAllRoadBits(tile
);
1238 /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
1239 if ((HasExactlyOneBit(b
) && GetRoadBits(tile
, RTT_TRAM
) == ROAD_NONE
) || !(flags
& DC_AUTO
)) {
1240 CommandCost
ret(EXPENSES_CONSTRUCTION
);
1241 FOR_ALL_ROADTRAMTYPES(rtt
) {
1242 if (!MayHaveRoad(tile
) || GetRoadType(tile
, rtt
) == INVALID_ROADTYPE
) continue;
1244 CommandCost tmp_ret
= RemoveRoad(tile
, flags
, GetRoadBits(tile
, rtt
), rtt
, true);
1245 if (tmp_ret
.Failed()) return tmp_ret
;
1246 ret
.AddCost(tmp_ret
);
1250 return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST
);
1253 case ROAD_TILE_CROSSING
: {
1254 CommandCost
ret(EXPENSES_CONSTRUCTION
);
1256 if (flags
& DC_AUTO
) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST
);
1258 /* Must iterate over the roadtypes in a reverse manner because
1259 * tram tracks must be removed before the road bits. */
1260 for (RoadTramType rtt
: { RTT_TRAM
, RTT_ROAD
}) {
1261 if (!MayHaveRoad(tile
) || GetRoadType(tile
, rtt
) == INVALID_ROADTYPE
) continue;
1263 CommandCost tmp_ret
= RemoveRoad(tile
, flags
, GetCrossingRoadBits(tile
), rtt
, false);
1264 if (tmp_ret
.Failed()) return tmp_ret
;
1265 ret
.AddCost(tmp_ret
);
1268 if (flags
& DC_EXEC
) {
1269 DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
1275 case ROAD_TILE_DEPOT
:
1276 if (flags
& DC_AUTO
) {
1277 return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED
);
1279 return RemoveRoadDepot(tile
, flags
);
1284 struct DrawRoadTileStruct
{
1290 #include "table/road_land.h"
1293 * Get the foundationtype of a RoadBits Slope combination
1295 * @param tileh The Slope part
1296 * @param bits The RoadBits part
1297 * @return The resulting Foundation
1299 static Foundation
GetRoadFoundation(Slope tileh
, RoadBits bits
)
1301 /* Flat land and land without a road doesn't require a foundation */
1302 if (tileh
== SLOPE_FLAT
|| bits
== ROAD_NONE
) return FOUNDATION_NONE
;
1304 /* Steep slopes behave the same as slopes with one corner raised. */
1305 if (IsSteepSlope(tileh
)) {
1306 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
1309 /* Leveled RoadBits on a slope */
1310 if ((_invalid_tileh_slopes_road
[0][tileh
] & bits
) == ROAD_NONE
) return FOUNDATION_LEVELED
;
1312 /* Straight roads without foundation on a slope */
1313 if (!IsSlopeWithOneCornerRaised(tileh
) &&
1314 (_invalid_tileh_slopes_road
[1][tileh
] & bits
) == ROAD_NONE
)
1315 return FOUNDATION_NONE
;
1317 /* Roads on steep Slopes or on Slopes with one corner raised */
1318 return (bits
== ROAD_X
? FOUNDATION_INCLINED_X
: FOUNDATION_INCLINED_Y
);
1321 const byte _road_sloped_sprites
[14] = {
1329 * Get the sprite offset within a spritegroup.
1330 * @param slope Slope
1331 * @param bits Roadbits
1332 * @return Offset for the sprite within the spritegroup.
1334 static uint
GetRoadSpriteOffset(Slope slope
, RoadBits bits
)
1336 if (slope
!= SLOPE_FLAT
) {
1338 case SLOPE_NE
: return 11;
1339 case SLOPE_SE
: return 12;
1340 case SLOPE_SW
: return 13;
1341 case SLOPE_NW
: return 14;
1342 default: NOT_REACHED();
1345 static const uint offsets
[] = {
1351 return offsets
[bits
];
1356 * Should the road be drawn as a unpaved snow/desert road?
1357 * By default, roads are always drawn as unpaved if they are on desert or
1358 * above the snow line, but NewGRFs can override this for desert.
1360 * @param tile The tile the road is on
1361 * @param roadside What sort of road this is
1362 * @return True if snow/desert road sprites should be used.
1364 static bool DrawRoadAsSnowDesert(TileIndex tile
, Roadside roadside
)
1366 return (IsOnSnow(tile
) &&
1367 !(_settings_game
.game_creation
.landscape
== LT_TROPIC
&& HasGrfMiscBit(GMB_DESERT_PAVED_ROADS
) &&
1368 roadside
!= ROADSIDE_BARREN
&& roadside
!= ROADSIDE_GRASS
&& roadside
!= ROADSIDE_GRASS_ROAD_WORKS
));
1372 * Draws the catenary for the RoadType of the given tile
1373 * @param ti information about the tile (slopes, height etc)
1374 * @param rt road type to draw catenary for
1375 * @param rb the roadbits for the tram
1377 void DrawRoadTypeCatenary(const TileInfo
*ti
, RoadType rt
, RoadBits rb
)
1379 /* Don't draw the catenary under a low bridge */
1380 if (IsBridgeAbove(ti
->tile
) && !IsTransparencySet(TO_CATENARY
)) {
1381 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
1383 if (height
<= GetTileMaxZ(ti
->tile
) + 1) return;
1386 if (CountBits(rb
) > 2) {
1387 /* On junctions we check whether neighbouring tiles also have catenary, and possibly
1388 * do not draw catenary towards those neighbours, which do not have catenary. */
1389 RoadBits rb_new
= ROAD_NONE
;
1390 for (DiagDirection dir
= DIAGDIR_BEGIN
; dir
< DIAGDIR_END
; dir
++) {
1391 if (rb
& DiagDirToRoadBits(dir
)) {
1392 TileIndex neighbour
= TileAddByDiagDir(ti
->tile
, dir
);
1393 if (MayHaveRoad(neighbour
)) {
1394 RoadType rt_road
= GetRoadTypeRoad(neighbour
);
1395 RoadType rt_tram
= GetRoadTypeTram(neighbour
);
1397 if ((rt_road
!= INVALID_ROADTYPE
&& HasRoadCatenary(rt_road
)) ||
1398 (rt_tram
!= INVALID_ROADTYPE
&& HasRoadCatenary(rt_tram
))) {
1399 rb_new
|= DiagDirToRoadBits(dir
);
1404 if (CountBits(rb_new
) >= 2) rb
= rb_new
;
1407 const RoadTypeInfo
* rti
= GetRoadTypeInfo(rt
);
1408 SpriteID front
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_CATENARY_FRONT
);
1409 SpriteID back
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_CATENARY_BACK
);
1411 if (front
!= 0 || back
!= 0) {
1412 if (front
!= 0) front
+= GetRoadSpriteOffset(ti
->tileh
, rb
);
1413 if (back
!= 0) back
+= GetRoadSpriteOffset(ti
->tileh
, rb
);
1414 } else if (ti
->tileh
!= SLOPE_FLAT
) {
1415 back
= SPR_TRAMWAY_BACK_WIRES_SLOPED
+ _road_sloped_sprites
[ti
->tileh
- 1];
1416 front
= SPR_TRAMWAY_FRONT_WIRES_SLOPED
+ _road_sloped_sprites
[ti
->tileh
- 1];
1418 back
= SPR_TRAMWAY_BASE
+ _road_backpole_sprites_1
[rb
];
1419 front
= SPR_TRAMWAY_BASE
+ _road_frontwire_sprites_1
[rb
];
1422 /* Catenary uses 1st company colour to help identify owner.
1423 * For tiles with OWNER_TOWN or OWNER_NONE, recolour CC to grey as a neutral colour. */
1424 Owner owner
= GetRoadOwner(ti
->tile
, GetRoadTramType(rt
));
1425 PaletteID pal
= (owner
== OWNER_NONE
|| owner
== OWNER_TOWN
? GENERAL_SPRITE_COLOUR(COLOUR_GREY
) : COMPANY_SPRITE_COLOUR(owner
));
1426 if (back
!= 0) AddSortableSpriteToDraw(back
, pal
, ti
->x
, ti
->y
, 16, 16, TILE_HEIGHT
+ BB_HEIGHT_UNDER_BRIDGE
, ti
->z
, IsTransparencySet(TO_CATENARY
));
1427 if (front
!= 0) AddSortableSpriteToDraw(front
, pal
, ti
->x
, ti
->y
, 16, 16, TILE_HEIGHT
+ BB_HEIGHT_UNDER_BRIDGE
, ti
->z
, IsTransparencySet(TO_CATENARY
));
1431 * Draws the catenary for the given tile
1432 * @param ti information about the tile (slopes, height etc)
1434 void DrawRoadCatenary(const TileInfo
*ti
)
1436 RoadBits road
= ROAD_NONE
;
1437 RoadBits tram
= ROAD_NONE
;
1439 if (IsTileType(ti
->tile
, MP_ROAD
)) {
1440 if (IsNormalRoad(ti
->tile
)) {
1441 road
= GetRoadBits(ti
->tile
, RTT_ROAD
);
1442 tram
= GetRoadBits(ti
->tile
, RTT_TRAM
);
1443 } else if (IsLevelCrossing(ti
->tile
)) {
1444 tram
= road
= (GetCrossingRailAxis(ti
->tile
) == AXIS_Y
? ROAD_X
: ROAD_Y
);
1446 } else if (IsTileType(ti
->tile
, MP_STATION
)) {
1447 if (IsRoadStop(ti
->tile
)) {
1448 if (IsDriveThroughStopTile(ti
->tile
)) {
1449 Axis axis
= GetRoadStopDir(ti
->tile
) == DIAGDIR_NE
? AXIS_X
: AXIS_Y
;
1450 tram
= road
= (axis
== AXIS_X
? ROAD_X
: ROAD_Y
);
1452 tram
= road
= DiagDirToRoadBits(GetRoadStopDir(ti
->tile
));
1456 // No road here, no catenary to draw
1460 RoadType rt
= GetRoadTypeRoad(ti
->tile
);
1461 if (rt
!= INVALID_ROADTYPE
&& HasRoadCatenaryDrawn(rt
)) {
1462 DrawRoadTypeCatenary(ti
, rt
, road
);
1465 rt
= GetRoadTypeTram(ti
->tile
);
1466 if (rt
!= INVALID_ROADTYPE
&& HasRoadCatenaryDrawn(rt
)) {
1467 DrawRoadTypeCatenary(ti
, rt
, tram
);
1472 * Draws details on/around the road
1473 * @param img the sprite to draw
1474 * @param ti the tile to draw on
1475 * @param dx the offset from the top of the BB of the tile
1476 * @param dy the offset from the top of the BB of the tile
1477 * @param h the height of the sprite to draw
1478 * @param transparent whether the sprite should be transparent (used for roadside trees)
1480 static void DrawRoadDetail(SpriteID img
, const TileInfo
*ti
, int dx
, int dy
, int h
, bool transparent
)
1485 if (ti
->tileh
!= SLOPE_FLAT
) z
= GetSlopePixelZ(x
, y
);
1486 AddSortableSpriteToDraw(img
, PAL_NONE
, x
, y
, 2, 2, h
, z
, transparent
);
1490 * Draw road underlay and overlay sprites.
1491 * @param ti TileInfo
1492 * @param road_rti Road road type information
1493 * @param tram_rti Tram road type information
1494 * @param road_offset Road sprite offset (based on road bits)
1495 * @param tram_offset Tram sprite offset (based on road bits)
1497 void DrawRoadOverlays(const TileInfo
*ti
, PaletteID pal
, const RoadTypeInfo
*road_rti
, const RoadTypeInfo
*tram_rti
, uint road_offset
, uint tram_offset
)
1499 /* Road underlay takes precedence over tram */
1500 if (road_rti
!= nullptr) {
1501 if (road_rti
->UsesOverlay()) {
1502 SpriteID ground
= GetCustomRoadSprite(road_rti
, ti
->tile
, ROTSG_GROUND
);
1503 DrawGroundSprite(ground
+ road_offset
, pal
);
1506 if (tram_rti
->UsesOverlay()) {
1507 SpriteID ground
= GetCustomRoadSprite(tram_rti
, ti
->tile
, ROTSG_GROUND
);
1508 DrawGroundSprite(ground
+ tram_offset
, pal
);
1510 DrawGroundSprite(SPR_TRAMWAY_TRAM
+ tram_offset
, pal
);
1514 /* Draw road overlay */
1515 if (road_rti
!= nullptr) {
1516 if (road_rti
->UsesOverlay()) {
1517 SpriteID ground
= GetCustomRoadSprite(road_rti
, ti
->tile
, ROTSG_OVERLAY
);
1518 if (ground
!= 0) DrawGroundSprite(ground
+ road_offset
, pal
);
1522 /* Draw tram overlay */
1523 if (tram_rti
!= nullptr) {
1524 if (tram_rti
->UsesOverlay()) {
1525 SpriteID ground
= GetCustomRoadSprite(tram_rti
, ti
->tile
, ROTSG_OVERLAY
);
1526 if (ground
!= 0) DrawGroundSprite(ground
+ tram_offset
, pal
);
1527 } else if (road_rti
!= nullptr) {
1528 DrawGroundSprite(SPR_TRAMWAY_OVERLAY
+ tram_offset
, pal
);
1534 * Get ground sprite to draw for a road tile.
1535 * @param ti TileInof
1536 * @param roadside Road side type
1537 * @param rti Road type info
1538 * @param offset Road sprite offset
1539 * @param[out] pal Palette to draw.
1541 static SpriteID
GetRoadGroundSprite(const TileInfo
*ti
, Roadside roadside
, const RoadTypeInfo
*rti
, uint offset
, PaletteID
*pal
)
1543 /* Draw bare ground sprite if no road or road uses overlay system. */
1544 if (rti
== nullptr || rti
->UsesOverlay()) {
1545 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1546 return SPR_FLAT_SNOW_DESERT_TILE
+ SlopeToSpriteOffset(ti
->tileh
);
1550 case ROADSIDE_BARREN
: *pal
= PALETTE_TO_BARE_LAND
;
1551 return SPR_FLAT_GRASS_TILE
+ SlopeToSpriteOffset(ti
->tileh
);
1552 case ROADSIDE_GRASS
:
1553 case ROADSIDE_GRASS_ROAD_WORKS
: return SPR_FLAT_GRASS_TILE
+ SlopeToSpriteOffset(ti
->tileh
);
1554 default: break; // Paved
1558 /* Draw original road base sprite */
1559 SpriteID image
= SPR_ROAD_Y
+ offset
;
1560 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1564 case ROADSIDE_BARREN
: *pal
= PALETTE_TO_BARE_LAND
; break;
1565 case ROADSIDE_GRASS
: break;
1566 case ROADSIDE_GRASS_ROAD_WORKS
: break;
1567 default: image
-= 19; break; // Paved
1575 * Draw ground sprite and road pieces
1576 * @param ti TileInfo
1578 static void DrawRoadBits(TileInfo
*ti
)
1580 RoadBits road
= GetRoadBits(ti
->tile
, RTT_ROAD
);
1581 RoadBits tram
= GetRoadBits(ti
->tile
, RTT_TRAM
);
1583 RoadType road_rt
= GetRoadTypeRoad(ti
->tile
);
1584 RoadType tram_rt
= GetRoadTypeTram(ti
->tile
);
1585 const RoadTypeInfo
*road_rti
= road_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(road_rt
);
1586 const RoadTypeInfo
*tram_rti
= tram_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(tram_rt
);
1588 if (ti
->tileh
!= SLOPE_FLAT
) {
1589 DrawFoundation(ti
, GetRoadFoundation(ti
->tileh
, road
| tram
));
1590 /* DrawFoundation() modifies ti. */
1593 /* Determine sprite offsets */
1594 uint road_offset
= GetRoadSpriteOffset(ti
->tileh
, road
);
1595 uint tram_offset
= GetRoadSpriteOffset(ti
->tileh
, tram
);
1597 /* Draw baseset underlay */
1598 Roadside roadside
= GetRoadside(ti
->tile
);
1600 PaletteID pal
= PAL_NONE
;
1601 SpriteID image
= GetRoadGroundSprite(ti
, roadside
, road_rti
, road
== ROAD_NONE
? tram_offset
: road_offset
, &pal
);
1602 DrawGroundSprite(image
, pal
);
1604 DrawRoadOverlays(ti
, pal
, road_rti
, tram_rti
, road_offset
, tram_offset
);
1607 if (road_rti
!= nullptr) {
1608 DisallowedRoadDirections drd
= GetDisallowedRoadDirections(ti
->tile
);
1609 if (drd
!= DRD_NONE
) {
1610 DrawGroundSpriteAt(SPR_ONEWAY_BASE
+ drd
- 1 + ((road
== ROAD_X
) ? 0 : 3), PAL_NONE
, 8, 8, GetPartialPixelZ(8, 8, ti
->tileh
));
1614 if (HasRoadWorks(ti
->tile
)) {
1616 DrawGroundSprite((road
| tram
) & ROAD_X
? SPR_EXCAVATION_X
: SPR_EXCAVATION_Y
, PAL_NONE
);
1620 /* Draw road, tram catenary */
1621 DrawRoadCatenary(ti
);
1623 /* Return if full detail is disabled, or we are zoomed fully out. */
1624 if (!HasBit(_display_opt
, DO_FULL_DETAIL
) || _cur_dpi
->zoom
> ZOOM_LVL_DETAIL
) return;
1626 /* Do not draw details (street lights, trees) under low bridge */
1627 if (IsBridgeAbove(ti
->tile
) && (roadside
== ROADSIDE_TREES
|| roadside
== ROADSIDE_STREET_LIGHTS
)) {
1628 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
1629 int minz
= GetTileMaxZ(ti
->tile
) + 2;
1631 if (roadside
== ROADSIDE_TREES
) minz
++;
1633 if (height
< minz
) return;
1636 /* If there are no road bits, return, as there is nothing left to do */
1637 if (HasAtMostOneBit(road
)) return;
1639 if (roadside
== ROADSIDE_TREES
&& IsInvisibilitySet(TO_TREES
)) return;
1640 bool is_transparent
= roadside
== ROADSIDE_TREES
&& IsTransparencySet(TO_TREES
);
1642 /* Draw extra details. */
1643 for (const DrawRoadTileStruct
*drts
= _road_display_table
[roadside
][road
| tram
]; drts
->image
!= 0; drts
++) {
1644 DrawRoadDetail(drts
->image
, ti
, drts
->subcoord_x
, drts
->subcoord_y
, 0x10, is_transparent
);
1648 /** Tile callback function for rendering a road tile to the screen */
1649 static void DrawTile_Road(TileInfo
*ti
)
1651 switch (GetRoadTileType(ti
->tile
)) {
1652 case ROAD_TILE_NORMAL
:
1656 case ROAD_TILE_CROSSING
: {
1657 if (ti
->tileh
!= SLOPE_FLAT
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
1659 Axis axis
= GetCrossingRailAxis(ti
->tile
);
1661 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(ti
->tile
));
1663 RoadType road_rt
= GetRoadTypeRoad(ti
->tile
);
1664 RoadType tram_rt
= GetRoadTypeTram(ti
->tile
);
1665 const RoadTypeInfo
*road_rti
= road_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(road_rt
);
1666 const RoadTypeInfo
*tram_rti
= tram_rt
== INVALID_ROADTYPE
? nullptr : GetRoadTypeInfo(tram_rt
);
1668 PaletteID pal
= PAL_NONE
;
1670 /* Draw base ground */
1671 if (rti
->UsesOverlay()) {
1672 SpriteID image
= SPR_ROAD_Y
+ axis
;
1674 Roadside roadside
= GetRoadside(ti
->tile
);
1675 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1679 case ROADSIDE_BARREN
: pal
= PALETTE_TO_BARE_LAND
; break;
1680 case ROADSIDE_GRASS
: break;
1681 default: image
-= 19; break; // Paved
1685 DrawGroundSprite(image
, pal
);
1687 SpriteID image
= rti
->base_sprites
.crossing
+ axis
;
1688 if (IsCrossingBarred(ti
->tile
)) image
+= 2;
1690 Roadside roadside
= GetRoadside(ti
->tile
);
1691 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1695 case ROADSIDE_BARREN
: pal
= PALETTE_TO_BARE_LAND
; break;
1696 case ROADSIDE_GRASS
: break;
1697 default: image
+= 4; break; // Paved
1701 DrawGroundSprite(image
, pal
);
1704 DrawRoadOverlays(ti
, pal
, road_rti
, tram_rti
, axis
, axis
);
1706 /* Draw rail/PBS overlay */
1707 bool draw_pbs
= _game_mode
!= GM_MENU
&& _settings_client
.gui
.show_track_reservation
&& HasCrossingReservation(ti
->tile
);
1708 if (rti
->UsesOverlay()) {
1709 PaletteID pal
= draw_pbs
? PALETTE_CRASH
: PAL_NONE
;
1710 SpriteID rail
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_CROSSING
) + axis
;
1711 DrawGroundSprite(rail
, pal
);
1713 DrawRailTileSeq(ti
, &_crossing_layout
, TO_CATENARY
, rail
, 0, PAL_NONE
);
1714 } else if (draw_pbs
|| tram_rti
!= nullptr || road_rti
->UsesOverlay()) {
1715 /* Add another rail overlay, unless there is only the base road sprite. */
1716 PaletteID pal
= draw_pbs
? PALETTE_CRASH
: PAL_NONE
;
1717 SpriteID rail
= GetCrossingRoadAxis(ti
->tile
) == AXIS_Y
? GetRailTypeInfo(GetRailType(ti
->tile
))->base_sprites
.single_x
: GetRailTypeInfo(GetRailType(ti
->tile
))->base_sprites
.single_y
;
1718 DrawGroundSprite(rail
, pal
);
1721 /* Draw road, tram catenary */
1722 DrawRoadCatenary(ti
);
1724 /* Draw rail catenary */
1725 if (HasRailCatenaryDrawn(GetRailType(ti
->tile
))) DrawRailCatenary(ti
);
1731 case ROAD_TILE_DEPOT
: {
1732 if (ti
->tileh
!= SLOPE_FLAT
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
1734 PaletteID palette
= COMPANY_SPRITE_COLOUR(GetTileOwner(ti
->tile
));
1736 RoadType road_rt
= GetRoadTypeRoad(ti
->tile
);
1737 RoadType tram_rt
= GetRoadTypeTram(ti
->tile
);
1738 const RoadTypeInfo
*rti
= GetRoadTypeInfo(road_rt
== INVALID_ROADTYPE
? tram_rt
: road_rt
);
1740 int relocation
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_DEPOT
);
1741 bool default_gfx
= relocation
== 0;
1743 if (HasBit(rti
->flags
, ROTF_CATENARY
)) {
1744 if (_loaded_newgrf_features
.tram
== TRAMWAY_REPLACE_DEPOT_WITH_TRACK
&& road_rt
== INVALID_ROADTYPE
&& !rti
->UsesOverlay()) {
1745 /* Sprites with track only work for default tram */
1746 relocation
= SPR_TRAMWAY_DEPOT_WITH_TRACK
- SPR_ROAD_DEPOT
;
1747 default_gfx
= false;
1749 /* Sprites without track are always better, if provided */
1750 relocation
= SPR_TRAMWAY_DEPOT_NO_TRACK
- SPR_ROAD_DEPOT
;
1754 relocation
-= SPR_ROAD_DEPOT
;
1757 DiagDirection dir
= GetRoadDepotDirection(ti
->tile
);
1758 const DrawTileSprites
*dts
= &_road_depot
[dir
];
1759 DrawGroundSprite(dts
->ground
.sprite
, PAL_NONE
);
1762 uint offset
= GetRoadSpriteOffset(SLOPE_FLAT
, DiagDirToRoadBits(dir
));
1763 if (rti
->UsesOverlay()) {
1764 SpriteID ground
= GetCustomRoadSprite(rti
, ti
->tile
, ROTSG_OVERLAY
);
1765 if (ground
!= 0) DrawGroundSprite(ground
+ offset
, PAL_NONE
);
1766 } else if (road_rt
== INVALID_ROADTYPE
) {
1767 DrawGroundSprite(SPR_TRAMWAY_OVERLAY
+ offset
, PAL_NONE
);
1771 DrawRailTileSeq(ti
, dts
, TO_BUILDINGS
, relocation
, 0, palette
);
1775 DrawBridgeMiddle(ti
);
1779 * Draw the road depot sprite.
1780 * @param x The x offset to draw at.
1781 * @param y The y offset to draw at.
1782 * @param dir The direction the depot must be facing.
1783 * @param rt The road type of the depot to draw.
1785 void DrawRoadDepotSprite(int x
, int y
, DiagDirection dir
, RoadType rt
)
1787 PaletteID palette
= COMPANY_SPRITE_COLOUR(_local_company
);
1789 const RoadTypeInfo
* rti
= GetRoadTypeInfo(rt
);
1790 int relocation
= GetCustomRoadSprite(rti
, INVALID_TILE
, ROTSG_DEPOT
);
1791 bool default_gfx
= relocation
== 0;
1793 if (HasBit(rti
->flags
, ROTF_CATENARY
)) {
1794 if (_loaded_newgrf_features
.tram
== TRAMWAY_REPLACE_DEPOT_WITH_TRACK
&& RoadTypeIsTram(rt
) && !rti
->UsesOverlay()) {
1795 /* Sprites with track only work for default tram */
1796 relocation
= SPR_TRAMWAY_DEPOT_WITH_TRACK
- SPR_ROAD_DEPOT
;
1797 default_gfx
= false;
1799 /* Sprites without track are always better, if provided */
1800 relocation
= SPR_TRAMWAY_DEPOT_NO_TRACK
- SPR_ROAD_DEPOT
;
1804 relocation
-= SPR_ROAD_DEPOT
;
1807 const DrawTileSprites
*dts
= &_road_depot
[dir
];
1808 DrawSprite(dts
->ground
.sprite
, PAL_NONE
, x
, y
);
1811 uint offset
= GetRoadSpriteOffset(SLOPE_FLAT
, DiagDirToRoadBits(dir
));
1812 if (rti
->UsesOverlay()) {
1813 SpriteID ground
= GetCustomRoadSprite(rti
, INVALID_TILE
, ROTSG_OVERLAY
);
1814 if (ground
!= 0) DrawSprite(ground
+ offset
, PAL_NONE
, x
, y
);
1815 } else if (RoadTypeIsTram(rt
)) {
1816 DrawSprite(SPR_TRAMWAY_OVERLAY
+ offset
, PAL_NONE
, x
, y
);
1820 DrawRailTileSeqInGUI(x
, y
, dts
, relocation
, 0, palette
);
1824 * Updates cached nearest town for all road tiles
1825 * @param invalidate are we just invalidating cached data?
1826 * @pre invalidate == true implies _generating_world == true
1828 void UpdateNearestTownForRoadTiles(bool invalidate
)
1830 assert(!invalidate
|| _generating_world
);
1832 for (TileIndex t
= 0; t
< MapSize(); t
++) {
1833 if (IsTileType(t
, MP_ROAD
) && !IsRoadDepot(t
) && !HasTownOwnedRoad(t
)) {
1834 TownID tid
= INVALID_TOWN
;
1836 const Town
*town
= CalcClosestTownFromTile(t
);
1837 if (town
!= nullptr) tid
= town
->index
;
1839 SetTownIndex(t
, tid
);
1844 static int GetSlopePixelZ_Road(TileIndex tile
, uint x
, uint y
)
1847 if (IsNormalRoad(tile
)) {
1849 Slope tileh
= GetTilePixelSlope(tile
, &z
);
1850 if (tileh
== SLOPE_FLAT
) return z
;
1852 Foundation f
= GetRoadFoundation(tileh
, GetAllRoadBits(tile
));
1853 z
+= ApplyPixelFoundationToSlope(f
, &tileh
);
1854 return z
+ GetPartialPixelZ(x
& 0xF, y
& 0xF, tileh
);
1856 return GetTileMaxPixelZ(tile
);
1860 static Foundation
GetFoundation_Road(TileIndex tile
, Slope tileh
)
1862 if (IsNormalRoad(tile
)) {
1863 return GetRoadFoundation(tileh
, GetAllRoadBits(tile
));
1865 return FlatteningFoundation(tileh
);
1869 static const Roadside _town_road_types
[][2] = {
1870 { ROADSIDE_GRASS
, ROADSIDE_GRASS
},
1871 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1872 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1873 { ROADSIDE_TREES
, ROADSIDE_TREES
},
1874 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
}
1877 static const Roadside _town_road_types_2
[][2] = {
1878 { ROADSIDE_GRASS
, ROADSIDE_GRASS
},
1879 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1880 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
},
1881 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
},
1882 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
}
1886 static void TileLoop_Road(TileIndex tile
)
1888 switch (_settings_game
.game_creation
.landscape
) {
1890 if (IsOnSnow(tile
) != (GetTileZ(tile
) > GetSnowLine())) {
1892 MarkTileDirtyByTile(tile
);
1897 if (GetTropicZone(tile
) == TROPICZONE_DESERT
&& !IsOnDesert(tile
)) {
1899 MarkTileDirtyByTile(tile
);
1904 if (IsRoadDepot(tile
)) return;
1906 const Town
*t
= ClosestTownFromTile(tile
, UINT_MAX
);
1907 if (!HasRoadWorks(tile
)) {
1908 HouseZonesBits grp
= HZB_TOWN_EDGE
;
1911 grp
= GetTownRadiusGroup(t
, tile
);
1913 /* Show an animation to indicate road work */
1914 if (t
->road_build_months
!= 0 &&
1915 (DistanceManhattan(t
->xy
, tile
) < 8 || grp
!= HZB_TOWN_EDGE
) &&
1916 IsNormalRoad(tile
) && !HasAtMostOneBit(GetAllRoadBits(tile
))) {
1917 if (GetFoundationSlope(tile
) == SLOPE_FLAT
&& EnsureNoVehicleOnGround(tile
).Succeeded() && Chance16(1, 40)) {
1918 StartRoadWorks(tile
);
1920 if (_settings_client
.sound
.ambient
) SndPlayTileFx(SND_21_JACKHAMMER
, tile
);
1921 CreateEffectVehicleAbove(
1922 TileX(tile
) * TILE_SIZE
+ 7,
1923 TileY(tile
) * TILE_SIZE
+ 7,
1926 MarkTileDirtyByTile(tile
);
1933 /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
1934 const Roadside
*new_rs
= (_settings_game
.game_creation
.landscape
== LT_TOYLAND
) ? _town_road_types_2
[grp
] : _town_road_types
[grp
];
1935 Roadside cur_rs
= GetRoadside(tile
);
1937 /* We have our desired type, do nothing */
1938 if (cur_rs
== new_rs
[0]) return;
1940 /* We have the pre-type of the desired type, switch to the desired type */
1941 if (cur_rs
== new_rs
[1]) {
1943 /* We have barren land, install the pre-type */
1944 } else if (cur_rs
== ROADSIDE_BARREN
) {
1946 /* We're totally off limits, remove any installation and make barren land */
1948 cur_rs
= ROADSIDE_BARREN
;
1950 SetRoadside(tile
, cur_rs
);
1951 MarkTileDirtyByTile(tile
);
1953 } else if (IncreaseRoadWorksCounter(tile
)) {
1954 TerminateRoadWorks(tile
);
1956 if (_settings_game
.economy
.mod_road_rebuild
) {
1957 /* Generate a nicer town surface */
1958 const RoadBits old_rb
= GetAnyRoadBits(tile
, RTT_ROAD
);
1959 const RoadBits new_rb
= CleanUpRoadBits(tile
, old_rb
);
1961 if (old_rb
!= new_rb
) {
1962 RemoveRoad(tile
, DC_EXEC
| DC_AUTO
| DC_NO_WATER
, (old_rb
^ new_rb
), RTT_ROAD
, true);
1964 /* If new_rb is 0, there are now no road pieces left and the tile is no longer a road tile */
1966 MarkTileDirtyByTile(tile
);
1972 /* Possibly change road type */
1973 if (GetRoadOwner(tile
, RTT_ROAD
) == OWNER_TOWN
) {
1974 RoadType rt
= GetTownRoadType(t
);
1975 if (rt
!= GetRoadTypeRoad(tile
)) {
1976 SetRoadType(tile
, RTT_ROAD
, rt
);
1980 MarkTileDirtyByTile(tile
);
1984 static bool ClickTile_Road(TileIndex tile
)
1986 if (!IsRoadDepot(tile
)) return false;
1988 ShowDepotWindow(tile
, VEH_ROAD
);
1992 /* Converts RoadBits to TrackBits */
1993 static const TrackBits _road_trackbits
[16] = {
1994 TRACK_BIT_NONE
, // ROAD_NONE
1995 TRACK_BIT_NONE
, // ROAD_NW
1996 TRACK_BIT_NONE
, // ROAD_SW
1997 TRACK_BIT_LEFT
, // ROAD_W
1998 TRACK_BIT_NONE
, // ROAD_SE
1999 TRACK_BIT_Y
, // ROAD_Y
2000 TRACK_BIT_LOWER
, // ROAD_S
2001 TRACK_BIT_LEFT
| TRACK_BIT_LOWER
| TRACK_BIT_Y
, // ROAD_Y | ROAD_SW
2002 TRACK_BIT_NONE
, // ROAD_NE
2003 TRACK_BIT_UPPER
, // ROAD_N
2004 TRACK_BIT_X
, // ROAD_X
2005 TRACK_BIT_LEFT
| TRACK_BIT_UPPER
| TRACK_BIT_X
, // ROAD_X | ROAD_NW
2006 TRACK_BIT_RIGHT
, // ROAD_E
2007 TRACK_BIT_RIGHT
| TRACK_BIT_UPPER
| TRACK_BIT_Y
, // ROAD_Y | ROAD_NE
2008 TRACK_BIT_RIGHT
| TRACK_BIT_LOWER
| TRACK_BIT_X
, // ROAD_X | ROAD_SE
2009 TRACK_BIT_ALL
, // ROAD_ALL
2012 static TrackStatus
GetTileTrackStatus_Road(TileIndex tile
, TransportType mode
, uint sub_mode
, DiagDirection side
)
2014 TrackdirBits trackdirbits
= TRACKDIR_BIT_NONE
;
2015 TrackdirBits red_signals
= TRACKDIR_BIT_NONE
; // crossing barred
2017 case TRANSPORT_RAIL
:
2018 if (IsLevelCrossing(tile
)) trackdirbits
= TrackBitsToTrackdirBits(GetCrossingRailBits(tile
));
2021 case TRANSPORT_ROAD
: {
2022 RoadTramType rtt
= (RoadTramType
)sub_mode
;
2023 if (!HasTileRoadType(tile
, rtt
)) break;
2024 switch (GetRoadTileType(tile
)) {
2025 case ROAD_TILE_NORMAL
: {
2026 const uint drd_to_multiplier
[DRD_END
] = { 0x101, 0x100, 0x1, 0x0 };
2027 RoadBits bits
= GetRoadBits(tile
, rtt
);
2029 /* no roadbit at this side of tile, return 0 */
2030 if (side
!= INVALID_DIAGDIR
&& (DiagDirToRoadBits(side
) & bits
) == 0) break;
2032 uint multiplier
= drd_to_multiplier
[(rtt
== RTT_TRAM
) ? DRD_NONE
: GetDisallowedRoadDirections(tile
)];
2033 if (!HasRoadWorks(tile
)) trackdirbits
= (TrackdirBits
)(_road_trackbits
[bits
] * multiplier
);
2037 case ROAD_TILE_CROSSING
: {
2038 Axis axis
= GetCrossingRoadAxis(tile
);
2040 if (side
!= INVALID_DIAGDIR
&& axis
!= DiagDirToAxis(side
)) break;
2042 trackdirbits
= TrackBitsToTrackdirBits(AxisToTrackBits(axis
));
2043 if (IsCrossingBarred(tile
)) red_signals
= trackdirbits
;
2048 case ROAD_TILE_DEPOT
: {
2049 DiagDirection dir
= GetRoadDepotDirection(tile
);
2051 if (side
!= INVALID_DIAGDIR
&& side
!= dir
) break;
2053 trackdirbits
= TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir
));
2062 return CombineTrackStatus(trackdirbits
, red_signals
);
2065 static const StringID _road_tile_strings
[] = {
2066 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2067 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2068 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2069 STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS
,
2070 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2071 STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD
,
2072 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2073 STR_LAI_ROAD_DESCRIPTION_ROAD
,
2076 static void GetTileDesc_Road(TileIndex tile
, TileDesc
*td
)
2078 Owner rail_owner
= INVALID_OWNER
;
2079 Owner road_owner
= INVALID_OWNER
;
2080 Owner tram_owner
= INVALID_OWNER
;
2082 RoadType road_rt
= GetRoadTypeRoad(tile
);
2083 RoadType tram_rt
= GetRoadTypeTram(tile
);
2084 if (road_rt
!= INVALID_ROADTYPE
) {
2085 const RoadTypeInfo
*rti
= GetRoadTypeInfo(road_rt
);
2086 td
->roadtype
= rti
->strings
.name
;
2087 td
->road_speed
= rti
->max_speed
/ 2;
2088 road_owner
= GetRoadOwner(tile
, RTT_ROAD
);
2090 if (tram_rt
!= INVALID_ROADTYPE
) {
2091 const RoadTypeInfo
*rti
= GetRoadTypeInfo(tram_rt
);
2092 td
->tramtype
= rti
->strings
.name
;
2093 td
->tram_speed
= rti
->max_speed
/ 2;
2094 tram_owner
= GetRoadOwner(tile
, RTT_TRAM
);
2097 switch (GetRoadTileType(tile
)) {
2098 case ROAD_TILE_CROSSING
: {
2099 td
->str
= STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING
;
2100 rail_owner
= GetTileOwner(tile
);
2102 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
));
2103 td
->railtype
= rti
->strings
.name
;
2104 td
->rail_speed
= rti
->max_speed
;
2109 case ROAD_TILE_DEPOT
:
2110 td
->str
= STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT
;
2111 td
->build_date
= Depot::GetByTile(tile
)->build_date
;
2115 td
->str
= (road_rt
!= INVALID_ROADTYPE
? _road_tile_strings
[GetRoadside(tile
)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY
);
2120 /* Now we have to discover, if the tile has only one owner or many:
2121 * - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
2122 * - Compare the found owner with the other owners, and test if they differ.
2123 * Note: If road exists it will be the first_owner.
2125 Owner first_owner
= (road_owner
== INVALID_OWNER
? tram_owner
: road_owner
);
2126 bool mixed_owners
= (tram_owner
!= INVALID_OWNER
&& tram_owner
!= first_owner
) || (rail_owner
!= INVALID_OWNER
&& rail_owner
!= first_owner
);
2129 /* Multiple owners */
2130 td
->owner_type
[0] = (rail_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_RAIL_OWNER
);
2131 td
->owner
[0] = rail_owner
;
2132 td
->owner_type
[1] = (road_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_ROAD_OWNER
);
2133 td
->owner
[1] = road_owner
;
2134 td
->owner_type
[2] = (tram_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_TRAM_OWNER
);
2135 td
->owner
[2] = tram_owner
;
2137 /* One to rule them all */
2138 td
->owner
[0] = first_owner
;
2143 * Given the direction the road depot is pointing, this is the direction the
2144 * vehicle should be travelling in in order to enter the depot.
2146 static const byte _roadveh_enter_depot_dir
[4] = {
2147 TRACKDIR_X_SW
, TRACKDIR_Y_NW
, TRACKDIR_X_NE
, TRACKDIR_Y_SE
2150 static VehicleEnterTileStatus
VehicleEnter_Road(Vehicle
*v
, TileIndex tile
, int x
, int y
)
2152 switch (GetRoadTileType(tile
)) {
2153 case ROAD_TILE_DEPOT
: {
2154 if (v
->type
!= VEH_ROAD
) break;
2156 RoadVehicle
*rv
= RoadVehicle::From(v
);
2157 if (rv
->frame
== RVC_DEPOT_STOP_FRAME
&&
2158 _roadveh_enter_depot_dir
[GetRoadDepotDirection(tile
)] == rv
->state
) {
2159 rv
->state
= RVSB_IN_DEPOT
;
2160 rv
->vehstatus
|= VS_HIDDEN
;
2161 rv
->direction
= ReverseDir(rv
->direction
);
2162 if (rv
->Next() == nullptr) VehicleEnterDepot(rv
->First());
2165 InvalidateWindowData(WC_VEHICLE_DEPOT
, rv
->tile
);
2166 return VETSB_ENTERED_WORMHOLE
;
2173 return VETSB_CONTINUE
;
2177 static void ChangeTileOwner_Road(TileIndex tile
, Owner old_owner
, Owner new_owner
)
2179 if (IsRoadDepot(tile
)) {
2180 if (GetTileOwner(tile
) == old_owner
) {
2181 if (new_owner
== INVALID_OWNER
) {
2182 DoCommand(tile
, 0, 0, DC_EXEC
| DC_BANKRUPT
, CMD_LANDSCAPE_CLEAR
);
2184 /* A road depot has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
2185 RoadType rt
= GetRoadTypeRoad(tile
);
2186 if (rt
== INVALID_ROADTYPE
) rt
= GetRoadTypeTram(tile
);
2187 Company::Get(old_owner
)->infrastructure
.road
[rt
] -= 2;
2188 Company::Get(new_owner
)->infrastructure
.road
[rt
] += 2;
2190 SetTileOwner(tile
, new_owner
);
2191 FOR_ALL_ROADTRAMTYPES(rtt
) {
2192 if (GetRoadOwner(tile
, rtt
) == old_owner
) {
2193 SetRoadOwner(tile
, rtt
, new_owner
);
2201 FOR_ALL_ROADTRAMTYPES(rtt
) {
2202 /* Update all roadtypes, no matter if they are present */
2203 if (GetRoadOwner(tile
, rtt
) == old_owner
) {
2204 RoadType rt
= GetRoadType(tile
, rtt
);
2205 if (rt
!= INVALID_ROADTYPE
) {
2206 /* A level crossing has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
2207 uint num_bits
= IsLevelCrossing(tile
) ? 2 : CountBits(GetRoadBits(tile
, rtt
));
2208 Company::Get(old_owner
)->infrastructure
.road
[rt
] -= num_bits
;
2209 if (new_owner
!= INVALID_OWNER
) Company::Get(new_owner
)->infrastructure
.road
[rt
] += num_bits
;
2212 SetRoadOwner(tile
, rtt
, new_owner
== INVALID_OWNER
? OWNER_NONE
: new_owner
);
2216 if (IsLevelCrossing(tile
)) {
2217 if (GetTileOwner(tile
) == old_owner
) {
2218 if (new_owner
== INVALID_OWNER
) {
2219 DoCommand(tile
, 0, GetCrossingRailTrack(tile
), DC_EXEC
| DC_BANKRUPT
, CMD_REMOVE_SINGLE_RAIL
);
2221 /* Update infrastructure counts. No need to dirty windows here, we'll redraw the whole screen anyway. */
2222 Company::Get(old_owner
)->infrastructure
.rail
[GetRailType(tile
)] -= LEVELCROSSING_TRACKBIT_FACTOR
;
2223 Company::Get(new_owner
)->infrastructure
.rail
[GetRailType(tile
)] += LEVELCROSSING_TRACKBIT_FACTOR
;
2225 SetTileOwner(tile
, new_owner
);
2231 static CommandCost
TerraformTile_Road(TileIndex tile
, DoCommandFlag flags
, int z_new
, Slope tileh_new
)
2233 if (_settings_game
.construction
.build_on_slopes
&& AutoslopeEnabled()) {
2234 switch (GetRoadTileType(tile
)) {
2235 case ROAD_TILE_CROSSING
:
2236 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
]);
2239 case ROAD_TILE_DEPOT
:
2240 if (AutoslopeCheckForEntranceEdge(tile
, z_new
, tileh_new
, GetRoadDepotDirection(tile
))) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
2243 case ROAD_TILE_NORMAL
: {
2244 RoadBits bits
= GetAllRoadBits(tile
);
2245 RoadBits bits_copy
= bits
;
2246 /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
2247 if (CheckRoadSlope(tileh_new
, &bits_copy
, ROAD_NONE
, ROAD_NONE
).Succeeded()) {
2248 /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
2249 if (bits
== bits_copy
) {
2251 Slope tileh_old
= GetTileSlope(tile
, &z_old
);
2253 /* Get the slope on top of the foundation */
2254 z_old
+= ApplyFoundationToSlope(GetRoadFoundation(tileh_old
, bits
), &tileh_old
);
2255 z_new
+= ApplyFoundationToSlope(GetRoadFoundation(tileh_new
, bits
), &tileh_new
);
2257 /* The surface slope must not be changed */
2258 if ((z_old
== z_new
) && (tileh_old
== tileh_new
)) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
2264 default: NOT_REACHED();
2268 return DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
2271 /** Update power of road vehicle under which is the roadtype being converted */
2272 static Vehicle
*UpdateRoadVehPowerProc(Vehicle
*v
, void *data
)
2274 if (v
->type
!= VEH_ROAD
) return nullptr;
2276 RoadVehicleList
*affected_rvs
= static_cast<RoadVehicleList
*>(data
);
2277 include(*affected_rvs
, RoadVehicle::From(v
)->First());
2283 * Checks the tile and returns whether the current player is allowed to convert the roadtype to another roadtype without taking ownership
2284 * @param owner the tile owner.
2285 * @param rtt Road/tram type.
2286 * @return whether the road is convertible
2288 static bool CanConvertUnownedRoadType(Owner owner
, RoadTramType rtt
)
2290 return (owner
== OWNER_NONE
|| (owner
== OWNER_TOWN
&& rtt
== RTT_ROAD
));
2294 * Convert the ownership of the RoadType of the tile if applicable
2295 * @param tile the tile of which convert ownership
2296 * @param num_pieces the count of the roadbits to assign to the new owner
2297 * @param owner the current owner of the RoadType
2298 * @param from_type the old road type
2299 * @param to_type the new road type
2301 static void ConvertRoadTypeOwner(TileIndex tile
, uint num_pieces
, Owner owner
, RoadType from_type
, RoadType to_type
)
2303 // Scenario editor, maybe? Don't touch the owners when converting roadtypes...
2304 if (_current_company
>= MAX_COMPANIES
) return;
2306 // We can't get a company from invalid owners but we can get ownership of roads without an owner
2307 if (owner
>= MAX_COMPANIES
&& owner
!= OWNER_NONE
) return;
2313 SetRoadOwner(tile
, GetRoadTramType(to_type
), (Owner
)_current_company
);
2314 UpdateCompanyRoadInfrastructure(to_type
, _current_company
, num_pieces
);
2318 c
= Company::Get(owner
);
2319 c
->infrastructure
.road
[from_type
] -= num_pieces
;
2320 c
->infrastructure
.road
[to_type
] += num_pieces
;
2321 DirtyCompanyInfrastructureWindows(c
->index
);
2327 * Convert one road subtype to another.
2328 * Not meant to convert from road to tram.
2330 * @param tile end tile of road conversion drag
2331 * @param flags operation to perform
2332 * @param p1 start tile of drag
2333 * @param p2 various bitstuffed elements:
2334 * - p2 = (bit 0..5) new roadtype to convert to.
2335 * @param text unused
2336 * @return the cost of this operation or an error
2338 CommandCost
CmdConvertRoad(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
2340 RoadType to_type
= Extract
<RoadType
, 0, 6>(p2
);
2342 TileIndex area_start
= p1
;
2343 TileIndex area_end
= tile
;
2345 if (!ValParamRoadType(to_type
)) return CMD_ERROR
;
2346 if (area_start
>= MapSize()) return CMD_ERROR
;
2348 RoadVehicleList affected_rvs
;
2349 RoadTramType rtt
= GetRoadTramType(to_type
);
2351 CommandCost
cost(EXPENSES_CONSTRUCTION
);
2352 CommandCost error
= CommandCost((rtt
== RTT_TRAM
) ? STR_ERROR_NO_SUITABLE_TRAMWAY
: STR_ERROR_NO_SUITABLE_ROAD
); // by default, there is no road to convert.
2353 bool found_convertible_road
= false; // whether we actually did convert any road/tram (see bug #7633)
2355 TileIterator
*iter
= new OrthogonalTileIterator(area_start
, area_end
);
2356 for (; (tile
= *iter
) != INVALID_TILE
; ++(*iter
)) {
2357 /* Is road present on tile? */
2358 if (!MayHaveRoad(tile
)) continue;
2360 /* Converting to the same subtype? */
2361 RoadType from_type
= GetRoadType(tile
, rtt
);
2362 if (from_type
== INVALID_ROADTYPE
|| from_type
== to_type
) continue;
2364 /* Check if there is any infrastructure on tile */
2365 TileType tt
= GetTileType(tile
);
2368 if (!IsRoadStop(tile
)) continue;
2371 if (IsLevelCrossing(tile
) && RoadNoLevelCrossing(to_type
)) {
2372 error
.MakeError(STR_ERROR_CROSSING_DISALLOWED_ROAD
);
2376 case MP_TUNNELBRIDGE
:
2377 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) continue;
2382 /* Trying to convert other's road */
2383 Owner owner
= GetRoadOwner(tile
, rtt
);
2384 if (!CanConvertUnownedRoadType(owner
, rtt
)) {
2385 CommandCost ret
= CheckOwnership(owner
, tile
);
2392 /* Base the ability to replace town roads and bridges on the town's
2393 * acceptance of destructive actions. */
2394 if (owner
== OWNER_TOWN
) {
2395 Town
*t
= ClosestTownFromTile(tile
, _settings_game
.economy
.dist_local_authority
);
2396 CommandCost ret
= CheckforTownRating(DC_NONE
, t
, tt
== MP_TUNNELBRIDGE
? TUNNELBRIDGE_REMOVE
: ROAD_REMOVE
);
2403 /* Vehicle on the tile when not converting normal <-> powered
2404 * Tunnels and bridges have special check later */
2405 if (tt
!= MP_TUNNELBRIDGE
) {
2406 if (!HasPowerOnRoad(from_type
, to_type
)) {
2407 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
2413 if (rtt
== RTT_ROAD
&& owner
== OWNER_TOWN
) {
2414 error
.MakeError(STR_ERROR_OWNED_BY
);
2415 GetNameOfOwner(OWNER_TOWN
, tile
);
2420 uint num_pieces
= CountBits(GetAnyRoadBits(tile
, rtt
));
2421 if (tt
== MP_STATION
&& IsStandardRoadStopTile(tile
)) {
2422 num_pieces
*= ROAD_STOP_TRACKBIT_FACTOR
;
2423 } else if (tt
== MP_ROAD
&& IsRoadDepot(tile
)) {
2424 num_pieces
*= ROAD_DEPOT_TRACKBIT_FACTOR
;
2427 found_convertible_road
= true;
2428 cost
.AddCost(num_pieces
* RoadConvertCost(from_type
, to_type
));
2430 if (flags
& DC_EXEC
) { // we can safely convert, too
2431 /* Call ConvertRoadTypeOwner() to update the company infrastructure counters. */
2432 if (owner
== _current_company
) {
2433 ConvertRoadTypeOwner(tile
, num_pieces
, owner
, from_type
, to_type
);
2436 /* Perform the conversion */
2437 SetRoadType(tile
, rtt
, to_type
);
2438 MarkTileDirtyByTile(tile
);
2440 /* update power of train on this tile */
2441 FindVehicleOnPos(tile
, &affected_rvs
, &UpdateRoadVehPowerProc
);
2443 if (IsRoadDepotTile(tile
)) {
2444 /* Update build vehicle window related to this depot */
2445 InvalidateWindowData(WC_VEHICLE_DEPOT
, tile
);
2446 InvalidateWindowData(WC_BUILD_VEHICLE
, tile
);
2450 TileIndex endtile
= GetOtherTunnelBridgeEnd(tile
);
2452 /* If both ends of tunnel/bridge are in the range, do not try to convert twice -
2453 * it would cause assert because of different test and exec runs */
2454 if (endtile
< tile
) {
2455 if (OrthogonalTileArea(area_start
, area_end
).Contains(endtile
)) continue;
2458 /* When not converting rail <-> el. rail, any vehicle cannot be in tunnel/bridge */
2459 if (!HasPowerOnRoad(from_type
, to_type
)) {
2460 CommandCost ret
= TunnelBridgeIsFree(tile
, endtile
);
2466 if (rtt
== RTT_ROAD
&& owner
== OWNER_TOWN
) {
2467 error
.MakeError(STR_ERROR_OWNED_BY
);
2468 GetNameOfOwner(OWNER_TOWN
, tile
);
2473 /* There are 2 pieces on *every* tile of the bridge or tunnel */
2474 uint num_pieces
= (GetTunnelBridgeLength(tile
, endtile
) + 2) * 2;
2475 found_convertible_road
= true;
2476 cost
.AddCost(num_pieces
* RoadConvertCost(from_type
, to_type
));
2478 if (flags
& DC_EXEC
) {
2479 /* Update the company infrastructure counters. */
2480 if (owner
== _current_company
) {
2481 /* Each piece should be counted TUNNELBRIDGE_TRACKBIT_FACTOR times
2482 * for the infrastructure counters (cause of #8297). */
2483 ConvertRoadTypeOwner(tile
, num_pieces
* TUNNELBRIDGE_TRACKBIT_FACTOR
, owner
, from_type
, to_type
);
2484 SetTunnelBridgeOwner(tile
, endtile
, _current_company
);
2487 /* Perform the conversion */
2488 SetRoadType(tile
, rtt
, to_type
);
2489 SetRoadType(endtile
, rtt
, to_type
);
2491 FindVehicleOnPos(tile
, &affected_rvs
, &UpdateRoadVehPowerProc
);
2492 FindVehicleOnPos(endtile
, &affected_rvs
, &UpdateRoadVehPowerProc
);
2494 if (IsBridge(tile
)) {
2495 MarkBridgeDirty(tile
);
2497 MarkTileDirtyByTile(tile
);
2498 MarkTileDirtyByTile(endtile
);
2504 if (flags
& DC_EXEC
) {
2505 /* Roadtype changed, update roadvehicles as when entering different track */
2506 for (RoadVehicle
*v
: affected_rvs
) {
2512 return found_convertible_road
? cost
: error
;
2516 /** Tile callback functions for road tiles */
2517 extern const TileTypeProcs _tile_type_road_procs
= {
2518 DrawTile_Road
, // draw_tile_proc
2519 GetSlopePixelZ_Road
, // get_slope_z_proc
2520 ClearTile_Road
, // clear_tile_proc
2521 nullptr, // add_accepted_cargo_proc
2522 GetTileDesc_Road
, // get_tile_desc_proc
2523 GetTileTrackStatus_Road
, // get_tile_track_status_proc
2524 ClickTile_Road
, // click_tile_proc
2525 nullptr, // animate_tile_proc
2526 TileLoop_Road
, // tile_loop_proc
2527 ChangeTileOwner_Road
, // change_tile_owner_proc
2528 nullptr, // add_produced_cargo_proc
2529 VehicleEnter_Road
, // vehicle_enter_tile_proc
2530 GetFoundation_Road
, // get_foundation_proc
2531 TerraformTile_Road
, // terraform_tile_proc