4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file road_cmd.cpp Commands related to road tiles. */
13 #include "cmd_helper.h"
14 #include "road_internal.h"
15 #include "viewport_func.h"
16 #include "command_func.h"
17 #include "pathfinder/yapf/yapf_cache.h"
18 #include "depot_base.h"
20 #include "autoslope.h"
21 #include "tunnelbridge_map.h"
22 #include "strings_func.h"
23 #include "vehicle_func.h"
24 #include "sound_func.h"
25 #include "tunnelbridge.h"
26 #include "cheat_type.h"
27 #include "effectvehicle_func.h"
28 #include "effectvehicle_base.h"
29 #include "elrail_func.h"
32 #include "company_base.h"
33 #include "core/random_func.hpp"
34 #include "newgrf_railtype.h"
35 #include "date_func.h"
37 #include "company_gui.h"
39 #include "table/strings.h"
41 #include "safeguards.h"
44 * Verify whether a road vehicle is available.
45 * @return \c true if at least one road vehicle is available, \c false if not
47 bool RoadVehiclesAreBuilt()
49 const RoadVehicle
*rv
;
50 FOR_ALL_ROADVEHICLES(rv
) return true;
55 /** Invalid RoadBits on slopes. */
56 extern const RoadBits _invalid_tileh_slopes_road
[2][15] = {
57 /* The inverse of the mixable RoadBits on a leveled slope */
59 ROAD_NONE
, // SLOPE_FLAT
60 ROAD_NE
| ROAD_SE
, // SLOPE_W
61 ROAD_NE
| ROAD_NW
, // SLOPE_S
64 ROAD_NW
| ROAD_SW
, // SLOPE_E
65 ROAD_NONE
, // SLOPE_EW
68 ROAD_NONE
, // SLOPE_WSE
69 ROAD_SE
| ROAD_SW
, // SLOPE_N
72 ROAD_NONE
, // SLOPE_NS
73 ROAD_NONE
, // SLOPE_ENW
76 ROAD_NONE
, // SLOPE_SEN
77 ROAD_NONE
// SLOPE_NWS
79 /* The inverse of the allowed straight roads on a slope
80 * (with and without a foundation). */
82 ROAD_NONE
, // SLOPE_FLAT
83 ROAD_NONE
, // SLOPE_W Foundation
84 ROAD_NONE
, // SLOPE_S Foundation
87 ROAD_NONE
, // SLOPE_E Foundation
91 ROAD_ALL
, // SLOPE_WSE
92 ROAD_NONE
, // SLOPE_N Foundation
96 ROAD_ALL
, // SLOPE_ENW
99 ROAD_ALL
, // SLOPE_SEN
104 static Foundation
GetRoadFoundation(Slope tileh
, RoadBits bits
);
107 * Is it allowed to remove the given road bits from the given tile?
108 * @param tile the tile to remove the road from
109 * @param remove the roadbits that are going to be removed
110 * @param owner the actual owner of the roadbits of the tile
111 * @param rt the road type to remove the bits from
112 * @param flags command flags
113 * @param town_check Shall the town rating checked/affected
114 * @return A succeeded command when it is allowed to remove the road bits, a failed command otherwise.
116 CommandCost
CheckAllowRemoveRoad(TileIndex tile
, RoadBits remove
, Owner owner
, RoadType rt
, DoCommandFlag flags
, bool town_check
)
118 if (_game_mode
== GM_EDITOR
|| remove
== ROAD_NONE
) return CommandCost();
120 /* Water can always flood and towns can always remove "normal" road pieces.
121 * Towns are not be allowed to remove non "normal" road pieces, like tram
122 * tracks as that would result in trams that cannot turn. */
123 if (_current_company
== OWNER_WATER
||
124 (rt
== ROADTYPE_ROAD
&& !Company::IsValidID(_current_company
))) return CommandCost();
126 /* Only do the special processing if the road is owned
128 if (owner
!= OWNER_TOWN
) {
129 if (owner
== OWNER_NONE
) return CommandCost();
130 CommandCost ret
= CheckOwnership(owner
);
134 if (!town_check
) return CommandCost();
136 if (_cheats
.magic_bulldozer
.value
) return CommandCost();
138 Town
*t
= ClosestTownFromTile(tile
, UINT_MAX
);
139 if (t
== NULL
) return CommandCost();
141 /* check if you're allowed to remove the street owned by a town
142 * removal allowance depends on difficulty setting */
143 CommandCost ret
= CheckforTownRating(flags
, t
, ROAD_REMOVE
);
144 if (ret
.Failed()) return ret
;
146 /* Get a bitmask of which neighbouring roads has a tile */
147 RoadBits n
= ROAD_NONE
;
148 RoadBits present
= GetAnyRoadBits(tile
, rt
);
149 if ((present
& ROAD_NE
) && (GetAnyRoadBits(TILE_ADDXY(tile
, -1, 0), rt
) & ROAD_SW
)) n
|= ROAD_NE
;
150 if ((present
& ROAD_SE
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 0, 1), rt
) & ROAD_NW
)) n
|= ROAD_SE
;
151 if ((present
& ROAD_SW
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 1, 0), rt
) & ROAD_NE
)) n
|= ROAD_SW
;
152 if ((present
& ROAD_NW
) && (GetAnyRoadBits(TILE_ADDXY(tile
, 0, -1), rt
) & ROAD_SE
)) n
|= ROAD_NW
;
154 int rating_decrease
= RATING_ROAD_DOWN_STEP_EDGE
;
155 /* If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
157 if (KillFirstBit(n
) != ROAD_NONE
&& (n
& remove
) != ROAD_NONE
) {
158 /* you can remove all kind of roads with extra dynamite */
159 if (!_settings_game
.construction
.extra_dynamite
) {
160 SetDParam(0, t
->index
);
161 return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS
);
163 rating_decrease
= RATING_ROAD_DOWN_STEP_INNER
;
165 ChangeTownRating(t
, rating_decrease
, RATING_ROAD_MINIMUM
, flags
);
167 return CommandCost();
172 * Delete a piece of road.
173 * @param tile tile where to remove road from
174 * @param flags operation to perform
175 * @param pieces roadbits to remove
176 * @param rt roadtype to remove
177 * @param crossing_check should we check if there is a tram track when we are removing road from crossing?
178 * @param town_check should we check if the town allows removal?
180 static CommandCost
RemoveRoad(TileIndex tile
, DoCommandFlag flags
, RoadBits pieces
, RoadType rt
, bool crossing_check
, bool town_check
= true)
182 RoadTypes rts
= GetRoadTypes(tile
);
183 /* The tile doesn't have the given road type */
184 if (!HasBit(rts
, rt
)) return_cmd_error(rt
== ROADTYPE_TRAM
? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
186 switch (GetTileType(tile
)) {
188 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
189 if (ret
.Failed()) return ret
;
194 if (!IsDriveThroughStopTile(tile
)) return CMD_ERROR
;
196 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
197 if (ret
.Failed()) return ret
;
201 case MP_TUNNELBRIDGE
: {
202 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) return CMD_ERROR
;
203 CommandCost ret
= TunnelBridgeIsFree(tile
, GetOtherTunnelBridgeEnd(tile
));
204 if (ret
.Failed()) return ret
;
212 CommandCost ret
= CheckAllowRemoveRoad(tile
, pieces
, GetRoadOwner(tile
, rt
), rt
, flags
, town_check
);
213 if (ret
.Failed()) return ret
;
215 if (!IsTileType(tile
, MP_ROAD
)) {
216 const bool custom_bridge_head
= IsBridgeTile(tile
) &&
217 HasBridgeFlatRamp(GetTileSlope(tile
), DiagDirToAxis(GetTunnelBridgeDirection(tile
))) &&
218 (_settings_game
.construction
.road_custom_bridge_heads
|| IsRoadCustomBridgeHead(tile
));
220 /* If it's the last roadtype, just clear the whole tile */
221 if (rts
== RoadTypeToRoadTypes(rt
)) return DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
223 CommandCost
cost(EXPENSES_CONSTRUCTION
);
224 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
225 const RoadBits entrance_piece
= DiagDirToRoadBits(GetTunnelBridgeDirection(tile
));
226 const RoadBits axial_pieces
= AxisToRoadBits(DiagDirToAxis(GetTunnelBridgeDirection(tile
)));
227 const RoadBits existing
= IsBridge(tile
) ? GetCustomBridgeHeadRoadBits(tile
, rt
) : axial_pieces
;
228 const RoadType other_rt
= (rt
== ROADTYPE_ROAD
) ? ROADTYPE_TRAM
: ROADTYPE_ROAD
;
230 /* handle case where we would otherwise leave a single bridge entrance piece */
231 if ((existing
& ~pieces
) == entrance_piece
) {
232 pieces
|= entrance_piece
;
235 /* Removing any roadbit in the bridge axis removes the roadtype (that's the behaviour remove-long-roads needs) */
236 if ((existing
& pieces
) == ROAD_NONE
) return_cmd_error(rt
== ROADTYPE_TRAM
? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
238 if (!custom_bridge_head
) pieces
|= axial_pieces
;
240 const TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
241 const uint middle_len
= GetTunnelBridgeLength(other_end
, tile
);
242 uint pieces_count
= 0;
244 const RoadBits other_end_existing
= IsBridge(other_end
) ? GetCustomBridgeHeadRoadBits(other_end
, rt
) : axial_pieces
;
245 RoadBits other_end_pieces
= ROAD_NONE
;
246 if (pieces
& entrance_piece
) {
247 other_end_pieces
|= MirrorRoadBits(entrance_piece
);
248 /* if removing the other end entrance would only leave one piece, remove that too */
249 if (CountBits(other_end_existing
& ~other_end_pieces
) == 1) {
250 other_end_pieces
|= other_end_existing
;
252 pieces_count
+= middle_len
* 2;
253 if ((GetCustomBridgeHeadRoadBits(tile
, other_rt
) & entrance_piece
) == ROAD_NONE
) {
254 /* can't leave no entrance pieces for any road type */
255 return DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
258 pieces_count
+= CountBits(pieces
& existing
);
259 pieces_count
+= CountBits(other_end_pieces
& other_end_existing
);
261 cost
.AddCost(pieces_count
* _price
[PR_CLEAR_ROAD
]);
262 if (flags
& DC_EXEC
) {
263 SubtractRoadTunnelBridgeInfrastructure(tile
, other_end
);
265 const RoadBits bits
= existing
& ~pieces
;
266 const RoadBits other_bits
= other_end_existing
& ~other_end_pieces
;
268 if (bits
== ROAD_NONE
) SetRoadTypes(tile
, GetRoadTypes(tile
) & ~RoadTypeToRoadTypes(rt
));
269 if (other_bits
== ROAD_NONE
) SetRoadTypes(other_end
, GetRoadTypes(other_end
) & ~RoadTypeToRoadTypes(rt
));
271 if (IsBridge(tile
)) {
272 SetCustomBridgeHeadRoadBits(tile
, rt
, bits
);
273 SetCustomBridgeHeadRoadBits(other_end
, rt
, other_bits
);
276 if (bits
== ROAD_NONE
&& other_bits
== ROAD_NONE
) {
277 /* If the owner of the bridge sells all its road, also move the ownership
278 * to the owner of the other roadtype, unless the bridge owner is a town. */
279 Owner other_owner
= GetRoadOwner(tile
, other_rt
);
280 if (!IsTileOwner(tile
, other_owner
) && !IsTileOwner(tile
, OWNER_TOWN
)) {
281 SetTileOwner(tile
, other_owner
);
282 SetTileOwner(other_end
, other_owner
);
286 /* Mark tiles dirty that have been repaved */
287 if (IsBridge(tile
)) {
288 MarkBridgeDirty(tile
);
290 MarkTileDirtyByTile(tile
);
291 MarkTileDirtyByTile(other_end
);
294 AddRoadTunnelBridgeInfrastructure(tile
, other_end
);
295 DirtyAllCompanyInfrastructureWindows();
298 assert(IsDriveThroughStopTile(tile
));
299 cost
.AddCost(_price
[PR_CLEAR_ROAD
] * 2);
300 if (flags
& DC_EXEC
) {
301 Company
*c
= Company::GetIfValid(GetRoadOwner(tile
, rt
));
303 /* A full diagonal road tile has two road bits. */
304 c
->infrastructure
.road
[rt
] -= 2;
305 DirtyCompanyInfrastructureWindows(c
->index
);
307 SetRoadTypes(tile
, GetRoadTypes(tile
) & ~RoadTypeToRoadTypes(rt
));
308 MarkTileDirtyByTile(tile
);
314 switch (GetRoadTileType(tile
)) {
315 case ROAD_TILE_NORMAL
: {
316 Slope tileh
= GetTileSlope(tile
);
318 /* Steep slopes behave the same as slopes with one corner raised. */
319 if (IsSteepSlope(tileh
)) {
320 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
323 RoadBits present
= GetRoadBits(tile
, rt
);
324 const RoadBits other
= GetOtherRoadBits(tile
, rt
);
325 const Foundation f
= GetRoadFoundation(tileh
, present
);
327 if (HasRoadWorks(tile
) && _current_company
!= OWNER_WATER
) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS
);
329 /* Autocomplete to a straight road
330 * @li if the bits of the other roadtypes result in another foundation
331 * @li if build on slopes is disabled */
332 if ((IsStraightRoad(other
) && (other
& _invalid_tileh_slopes_road
[0][tileh
& SLOPE_ELEVATED
]) != ROAD_NONE
) ||
333 (tileh
!= SLOPE_FLAT
&& !_settings_game
.construction
.build_on_slopes
)) {
334 pieces
|= MirrorRoadBits(pieces
);
337 /* limit the bits to delete to the existing bits. */
339 if (pieces
== ROAD_NONE
) return_cmd_error(rt
== ROADTYPE_TRAM
? STR_ERROR_THERE_IS_NO_TRAMWAY
: STR_ERROR_THERE_IS_NO_ROAD
);
341 /* Now set present what it will be after the remove */
344 /* Check for invalid RoadBit combinations on slopes */
345 if (tileh
!= SLOPE_FLAT
&& present
!= ROAD_NONE
&&
346 (present
& _invalid_tileh_slopes_road
[0][tileh
& SLOPE_ELEVATED
]) == present
) {
350 if (flags
& DC_EXEC
) {
351 if (HasRoadWorks(tile
)) {
352 /* flooding tile with road works, don't forget to remove the effect vehicle too */
353 assert(_current_company
== OWNER_WATER
);
355 FOR_ALL_EFFECTVEHICLES(v
) {
356 if (TileVirtXY(v
->x_pos
, v
->y_pos
) == tile
) {
362 Company
*c
= Company::GetIfValid(GetRoadOwner(tile
, rt
));
364 c
->infrastructure
.road
[rt
] -= CountBits(pieces
);
365 DirtyCompanyInfrastructureWindows(c
->index
);
368 if (present
== ROAD_NONE
) {
369 RoadTypes rts
= GetRoadTypes(tile
) & ComplementRoadTypes(RoadTypeToRoadTypes(rt
));
370 if (rts
== ROADTYPES_NONE
) {
371 /* Includes MarkTileDirtyByTile() */
374 if (rt
== ROADTYPE_ROAD
&& IsRoadOwner(tile
, ROADTYPE_ROAD
, OWNER_TOWN
)) {
375 /* Update nearest-town index */
376 const Town
*town
= CalcClosestTownFromTile(tile
);
377 SetTownIndex(tile
, town
== NULL
? (TownID
)INVALID_TOWN
: town
->index
);
379 SetRoadBits(tile
, ROAD_NONE
, rt
);
380 SetRoadTypes(tile
, rts
);
381 MarkTileDirtyByTile(tile
);
384 /* When bits are removed, you *always* end up with something that
385 * is not a complete straight road tile. However, trams do not have
386 * onewayness, so they cannot remove it either. */
387 if (rt
!= ROADTYPE_TRAM
) SetDisallowedRoadDirections(tile
, DRD_NONE
);
388 SetRoadBits(tile
, present
, rt
);
389 MarkTileDirtyByTile(tile
);
393 CommandCost
cost(EXPENSES_CONSTRUCTION
, CountBits(pieces
) * _price
[PR_CLEAR_ROAD
]);
394 /* If we build a foundation we have to pay for it. */
395 if (f
== FOUNDATION_NONE
&& GetRoadFoundation(tileh
, present
) != FOUNDATION_NONE
) cost
.AddCost(_price
[PR_BUILD_FOUNDATION
]);
400 case ROAD_TILE_CROSSING
: {
401 if (pieces
& ComplementRoadBits(GetCrossingRoadBits(tile
))) {
405 /* Don't allow road to be removed from the crossing when there is tram;
406 * we can't draw the crossing without roadbits ;) */
407 if (rt
== ROADTYPE_ROAD
&& HasTileRoadType(tile
, ROADTYPE_TRAM
) && (flags
& DC_EXEC
|| crossing_check
)) return CMD_ERROR
;
409 if (flags
& DC_EXEC
) {
410 Company
*c
= Company::GetIfValid(GetRoadOwner(tile
, rt
));
412 /* A full diagonal road tile has two road bits. */
413 c
->infrastructure
.road
[rt
] -= 2;
414 DirtyCompanyInfrastructureWindows(c
->index
);
417 Track railtrack
= GetCrossingRailTrack(tile
);
418 RoadTypes rts
= GetRoadTypes(tile
) & ComplementRoadTypes(RoadTypeToRoadTypes(rt
));
419 if (rts
== ROADTYPES_NONE
) {
420 TrackBits tracks
= GetCrossingRailBits(tile
);
421 bool reserved
= HasCrossingReservation(tile
);
422 MakeRailNormal(tile
, GetTileOwner(tile
), tracks
, GetRailType(tile
));
423 if (reserved
) SetTrackReservation(tile
, tracks
);
425 /* Update rail count for level crossings. The plain track should still be accounted
426 * for, so only subtract the difference to the level crossing cost. */
427 c
= Company::GetIfValid(GetTileOwner(tile
));
429 c
->infrastructure
.rail
[GetRailType(tile
)] -= LEVELCROSSING_TRACKBIT_FACTOR
- 1;
430 DirtyCompanyInfrastructureWindows(c
->index
);
433 SetRoadTypes(tile
, rts
);
435 MarkTileDirtyByTile(tile
);
436 YapfNotifyTrackLayoutChange(tile
, railtrack
);
438 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_CLEAR_ROAD
] * 2);
442 case ROAD_TILE_DEPOT
:
449 * Calculate the costs for roads on slopes
450 * Aside modify the RoadBits to fit on the slopes
452 * @note The RoadBits are modified too!
453 * @param tileh The current slope
454 * @param pieces The RoadBits we want to add
455 * @param existing The existent RoadBits of the current type
456 * @param other The other existent RoadBits
457 * @return The costs for these RoadBits on this slope
459 static CommandCost
CheckRoadSlope(Slope tileh
, RoadBits
*pieces
, RoadBits existing
, RoadBits other
)
461 /* Remove already build pieces */
462 CLRBITS(*pieces
, existing
);
464 /* If we can't build anything stop here */
465 if (*pieces
== ROAD_NONE
) return CMD_ERROR
;
467 /* All RoadBit combos are valid on flat land */
468 if (tileh
== SLOPE_FLAT
) return CommandCost();
470 /* Steep slopes behave the same as slopes with one corner raised. */
471 if (IsSteepSlope(tileh
)) {
472 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
475 /* Save the merge of all bits of the current type */
476 RoadBits type_bits
= existing
| *pieces
;
478 /* Roads on slopes */
479 if (_settings_game
.construction
.build_on_slopes
&& (_invalid_tileh_slopes_road
[0][tileh
] & (other
| type_bits
)) == ROAD_NONE
) {
481 /* If we add leveling we've got to pay for it */
482 if ((other
| existing
) == ROAD_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
484 return CommandCost();
487 /* Autocomplete uphill roads */
488 *pieces
|= MirrorRoadBits(*pieces
);
489 type_bits
= existing
| *pieces
;
492 if (IsStraightRoad(type_bits
) && (other
== type_bits
|| other
== ROAD_NONE
) &&
493 (_invalid_tileh_slopes_road
[1][tileh
] & (other
| type_bits
)) == ROAD_NONE
) {
495 /* Slopes with foundation ? */
496 if (IsSlopeWithOneCornerRaised(tileh
)) {
498 /* Prevent build on slopes if it isn't allowed */
499 if (_settings_game
.construction
.build_on_slopes
) {
501 /* If we add foundation we've got to pay for it */
502 if ((other
| existing
) == ROAD_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
504 return CommandCost();
507 if (HasExactlyOneBit(existing
) && GetRoadFoundation(tileh
, existing
) == FOUNDATION_NONE
) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
508 return CommandCost();
515 * Build a piece of road.
516 * @param tile tile where to build road
517 * @param flags operation to perform
518 * @param p1 bit 0..3 road pieces to build (RoadBits)
520 * bit 6..7 disallowed directions to toggle
521 * @param p2 the town that is building the road (0 if not applicable)
523 * @return the cost of this operation or an error
525 CommandCost
CmdBuildRoad(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
527 CompanyID company
= _current_company
;
528 CommandCost
cost(EXPENSES_CONSTRUCTION
);
530 RoadBits existing
= ROAD_NONE
;
531 RoadBits other_bits
= ROAD_NONE
;
533 /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
534 * if a non-company is building the road */
535 if ((Company::IsValidID(company
) && p2
!= 0) || (company
== OWNER_TOWN
&& !Town::IsValidID(p2
)) || (company
== OWNER_DEITY
&& p2
!= 0)) return CMD_ERROR
;
536 if (company
!= OWNER_TOWN
) {
537 const Town
*town
= CalcClosestTownFromTile(tile
);
538 p2
= (town
!= NULL
) ? town
->index
: (TownID
)INVALID_TOWN
;
540 if (company
== OWNER_DEITY
) {
541 company
= OWNER_TOWN
;
543 /* If we are not within a town, we are not owned by the town */
544 if (town
== NULL
|| DistanceSquare(tile
, town
->xy
) > town
->cache
.squared_town_zone_radius
[HZB_TOWN_EDGE
]) {
545 company
= OWNER_NONE
;
550 RoadBits pieces
= Extract
<RoadBits
, 0, 4>(p1
);
552 /* do not allow building 'zero' road bits, code wouldn't handle it */
553 if (pieces
== ROAD_NONE
) return CMD_ERROR
;
555 RoadType rt
= Extract
<RoadType
, 4, 2>(p1
);
556 if (!IsValidRoadType(rt
) || !ValParamRoadType(rt
)) return CMD_ERROR
;
558 DisallowedRoadDirections toggle_drd
= Extract
<DisallowedRoadDirections
, 6, 2>(p1
);
560 Slope tileh
= GetTileSlope(tile
);
562 bool need_to_clear
= false;
563 switch (GetTileType(tile
)) {
565 switch (GetRoadTileType(tile
)) {
566 case ROAD_TILE_NORMAL
: {
567 if (HasRoadWorks(tile
)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS
);
569 other_bits
= GetOtherRoadBits(tile
, rt
);
570 if (!HasTileRoadType(tile
, rt
)) break;
572 existing
= GetRoadBits(tile
, rt
);
573 bool crossing
= !IsStraightRoad(existing
| pieces
);
574 if (rt
!= ROADTYPE_TRAM
&& (GetDisallowedRoadDirections(tile
) != DRD_NONE
|| toggle_drd
!= DRD_NONE
) && crossing
) {
575 /* Junctions cannot be one-way */
576 return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION
);
578 if ((existing
& pieces
) == pieces
) {
579 /* We only want to set the (dis)allowed road directions */
580 if (toggle_drd
!= DRD_NONE
&& rt
!= ROADTYPE_TRAM
) {
581 if (crossing
) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION
);
583 Owner owner
= GetRoadOwner(tile
, ROADTYPE_ROAD
);
584 if (owner
!= OWNER_NONE
) {
585 CommandCost ret
= CheckOwnership(owner
, tile
);
586 if (ret
.Failed()) return ret
;
589 DisallowedRoadDirections dis_existing
= GetDisallowedRoadDirections(tile
);
590 DisallowedRoadDirections dis_new
= dis_existing
^ toggle_drd
;
592 /* We allow removing disallowed directions to break up
593 * deadlocks, but adding them can break articulated
594 * vehicles. As such, only when less is disallowed,
595 * i.e. bits are removed, we skip the vehicle check. */
596 if (CountBits(dis_existing
) <= CountBits(dis_new
)) {
597 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
598 if (ret
.Failed()) return ret
;
601 /* Ignore half built tiles */
602 if ((flags
& DC_EXEC
) && rt
!= ROADTYPE_TRAM
&& IsStraightRoad(existing
)) {
603 SetDisallowedRoadDirections(tile
, dis_new
);
604 MarkTileDirtyByTile(tile
);
606 return CommandCost();
608 return_cmd_error(STR_ERROR_ALREADY_BUILT
);
610 /* Disallow breaking end-of-line of someone else
611 * so trams can still reverse on this tile. */
612 if (rt
== ROADTYPE_TRAM
&& HasExactlyOneBit(existing
)) {
613 Owner owner
= GetRoadOwner(tile
, rt
);
614 if (Company::IsValidID(owner
)) {
615 CommandCost ret
= CheckOwnership(owner
);
616 if (ret
.Failed()) return ret
;
622 case ROAD_TILE_CROSSING
:
623 other_bits
= GetCrossingRoadBits(tile
);
624 if (pieces
& ComplementRoadBits(other_bits
)) goto do_clear
;
625 pieces
= other_bits
; // we need to pay for both roadbits
627 if (HasTileRoadType(tile
, rt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
630 case ROAD_TILE_DEPOT
:
631 if ((GetAnyRoadBits(tile
, rt
) & pieces
) == pieces
) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
634 default: NOT_REACHED();
639 if (IsSteepSlope(tileh
)) {
640 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
643 /* Level crossings may only be built on these slopes */
644 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES
, tileh
)) {
645 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
648 if (GetRailTileType(tile
) != RAIL_TILE_NORMAL
) goto do_clear
;
650 if (RailNoLevelCrossings(GetRailType(tile
))) {
651 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED
);
655 switch (GetTrackBits(tile
)) {
657 if (pieces
& ROAD_X
) goto do_clear
;
662 if (pieces
& ROAD_Y
) goto do_clear
;
666 default: goto do_clear
;
669 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
670 if (ret
.Failed()) return ret
;
672 if (flags
& DC_EXEC
) {
673 Track railtrack
= AxisToTrack(OtherAxis(roaddir
));
674 YapfNotifyTrackLayoutChange(tile
, railtrack
);
675 /* Update company infrastructure counts. A level crossing has two road bits. */
676 Company
*c
= Company::GetIfValid(company
);
678 c
->infrastructure
.road
[rt
] += 2;
679 if (rt
!= ROADTYPE_ROAD
) c
->infrastructure
.road
[ROADTYPE_ROAD
] += 2;
680 DirtyCompanyInfrastructureWindows(company
);
682 /* Update rail count for level crossings. The plain track is already
683 * counted, so only add the difference to the level crossing cost. */
684 c
= Company::GetIfValid(GetTileOwner(tile
));
686 c
->infrastructure
.rail
[GetRailType(tile
)] += LEVELCROSSING_TRACKBIT_FACTOR
- 1;
687 DirtyCompanyInfrastructureWindows(c
->index
);
690 /* Always add road to the roadtypes (can't draw without it) */
691 bool reserved
= HasBit(GetRailReservationTrackBits(tile
), railtrack
);
692 MakeRoadCrossing(tile
, company
, company
, GetTileOwner(tile
), roaddir
, GetRailType(tile
), RoadTypeToRoadTypes(rt
) | ROADTYPES_ROAD
, p2
);
693 SetCrossingReservation(tile
, reserved
);
694 UpdateLevelCrossing(tile
, false);
695 MarkTileDirtyByTile(tile
);
697 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_ROAD
] * (rt
== ROADTYPE_ROAD
? 2 : 4));
701 if ((GetAnyRoadBits(tile
, rt
) & pieces
) == pieces
) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
702 if (!IsDriveThroughStopTile(tile
)) goto do_clear
;
704 RoadBits curbits
= AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile
)));
705 if (pieces
& ~curbits
) goto do_clear
;
706 pieces
= curbits
; // we need to pay for both roadbits
708 if (HasTileRoadType(tile
, rt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
712 case MP_TUNNELBRIDGE
: {
713 if (GetTunnelBridgeTransportType(tile
) != TRANSPORT_ROAD
) goto do_clear
;
715 const TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
717 if (IsBridge(tile
)) {
718 const DiagDirection entrance_dir
= GetTunnelBridgeDirection(tile
);
719 const RoadBits entrance_piece
= DiagDirToRoadBits(entrance_dir
);
720 const RoadBits axial_pieces
= AxisToRoadBits(DiagDirToAxis(entrance_dir
));
721 existing
= GetCustomBridgeHeadRoadBits(tile
, rt
);
723 if (!(_settings_game
.construction
.road_custom_bridge_heads
&& HasBridgeFlatRamp(tileh
, DiagDirToAxis(entrance_dir
)))) {
724 /* Ordinary bridge heads only */
725 /* Only allow building the outer roadbit, so building long roads stops at existing bridges */
726 if (MirrorRoadBits(entrance_piece
) != pieces
) goto do_clear
;
727 pieces
= axial_pieces
;
729 if ((existing
& pieces
) == pieces
) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
730 if ((pieces
& ~axial_pieces
) && !_settings_game
.construction
.build_on_slopes
) {
731 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
733 if ((_invalid_tileh_slopes_road
[0][tileh
] & (pieces
& ~entrance_piece
)) != ROAD_NONE
) {
734 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
737 /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
738 CommandCost ret
= TunnelBridgeIsFree(tile
, other_end
);
739 if (ret
.Failed()) return ret
;
741 if ((existing
| pieces
) == entrance_piece
) {
743 * Don't allow the custom bridge head bits to be only the entrance piece
744 * as this makes road vehicles go haywire
746 pieces
= axial_pieces
;
749 RoadBits added_pieces
= (existing
| pieces
) & ~existing
;
750 uint added_pieces_count
= CountBits(added_pieces
);
751 RoadBits other_end_added_pieces
= ROAD_NONE
;
752 RoadBits other_end_existing
= ROAD_NONE
;
754 if (added_pieces
& entrance_piece
) {
755 /* adding road to whole bridge */
757 other_end_added_pieces
= MirrorRoadBits(entrance_piece
);
758 added_pieces_count
+= 1 + (GetTunnelBridgeLength(tile
, other_end
) * 2);
760 other_end_existing
= GetCustomBridgeHeadRoadBits(other_end
, rt
);
761 assert((other_end_added_pieces
& other_end_existing
) == ROAD_NONE
);
763 if (other_end_existing
== ROAD_NONE
) {
765 * Don't allow the other end custom bridge head bits to be only the entrance piece
766 * as this makes road vehicles go haywire
768 other_end_added_pieces
= axial_pieces
;
769 added_pieces_count
++;
773 cost
.AddCost(added_pieces_count
* _price
[PR_BUILD_ROAD
]);
775 if (flags
& DC_EXEC
) {
776 SubtractRoadTunnelBridgeInfrastructure(tile
, other_end
);
778 SetRoadTypes(tile
, GetRoadTypes(tile
) | RoadTypeToRoadTypes(rt
));
779 if (!existing
) SetRoadOwner(tile
, rt
, company
);
780 SetCustomBridgeHeadRoadBits(tile
, rt
, existing
| pieces
);
781 if (other_end_added_pieces
) {
782 SetRoadTypes(other_end
, GetRoadTypes(other_end
) | RoadTypeToRoadTypes(rt
));
783 if (!other_end_existing
) SetRoadOwner(other_end
, rt
, company
);
784 SetCustomBridgeHeadRoadBits(other_end
, rt
, other_end_existing
| other_end_added_pieces
);
787 MarkBridgeDirty(tile
);
789 AddRoadTunnelBridgeInfrastructure(tile
, other_end
);
790 DirtyAllCompanyInfrastructureWindows();
794 } else { // IsTunnel(tile)
795 /* Only allow building the outer roadbit, so building long roads stops at existing bridges */
796 if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile
))) != pieces
) goto do_clear
;
797 if (HasTileRoadType(tile
, rt
)) return_cmd_error(STR_ERROR_ALREADY_BUILT
);
798 /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
799 CommandCost ret
= TunnelBridgeIsFree(tile
, other_end
);
800 if (ret
.Failed()) return ret
;
807 need_to_clear
= true;
813 CommandCost ret
= DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
814 if (ret
.Failed()) return ret
;
818 if (other_bits
!= pieces
) {
819 /* Check the foundation/slopes when adding road/tram bits */
820 CommandCost ret
= CheckRoadSlope(tileh
, &pieces
, existing
, other_bits
);
821 /* Return an error if we need to build a foundation (ret != 0) but the
822 * current setting is turned off */
823 if (ret
.Failed() || (ret
.GetCost() != 0 && !_settings_game
.construction
.build_on_slopes
)) {
824 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
829 if (!need_to_clear
) {
830 if (IsTileType(tile
, MP_ROAD
)) {
831 /* Don't put the pieces that already exist */
832 pieces
&= ComplementRoadBits(existing
);
834 /* Check if new road bits will have the same foundation as other existing road types */
835 if (IsNormalRoad(tile
)) {
836 Slope slope
= GetTileSlope(tile
);
837 Foundation found_new
= GetRoadFoundation(slope
, pieces
| existing
);
839 /* Test if all other roadtypes can be built at that foundation */
840 for (RoadType rtest
= ROADTYPE_ROAD
; rtest
< ROADTYPE_END
; rtest
++) {
841 if (rtest
!= rt
) { // check only other road types
842 RoadBits bits
= GetRoadBits(tile
, rtest
);
843 /* do not check if there are not road bits of given type */
844 if (bits
!= ROAD_NONE
&& GetRoadFoundation(slope
, bits
) != found_new
) {
845 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
852 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
853 if (ret
.Failed()) return ret
;
857 uint num_pieces
= (!need_to_clear
&& IsTileType(tile
, MP_TUNNELBRIDGE
)) ?
858 /* There are 2 pieces on *every* tile of the bridge or tunnel */
859 2 * (GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile
), tile
) + 2) :
863 cost
.AddCost(num_pieces
* _price
[PR_BUILD_ROAD
]);
865 if (flags
& DC_EXEC
) {
866 switch (GetTileType(tile
)) {
868 RoadTileType rtt
= GetRoadTileType(tile
);
869 if (existing
== ROAD_NONE
|| rtt
== ROAD_TILE_CROSSING
) {
870 SetRoadTypes(tile
, GetRoadTypes(tile
) | RoadTypeToRoadTypes(rt
));
871 SetRoadOwner(tile
, rt
, company
);
872 if (rt
== ROADTYPE_ROAD
) SetTownIndex(tile
, p2
);
874 if (rtt
!= ROAD_TILE_CROSSING
) SetRoadBits(tile
, existing
| pieces
, rt
);
878 case MP_TUNNELBRIDGE
: {
879 TileIndex other_end
= GetOtherTunnelBridgeEnd(tile
);
881 SetRoadTypes(other_end
, GetRoadTypes(other_end
) | RoadTypeToRoadTypes(rt
));
882 SetRoadTypes(tile
, GetRoadTypes(tile
) | RoadTypeToRoadTypes(rt
));
883 SetRoadOwner(other_end
, rt
, company
);
884 SetRoadOwner(tile
, rt
, company
);
886 /* Mark tiles dirty that have been repaved */
887 if (IsBridge(tile
)) {
890 MarkTileDirtyByTile(other_end
);
891 MarkTileDirtyByTile(tile
);
897 assert(IsDriveThroughStopTile(tile
));
898 SetRoadTypes(tile
, GetRoadTypes(tile
) | RoadTypeToRoadTypes(rt
));
899 SetRoadOwner(tile
, rt
, company
);
903 MakeRoadNormal(tile
, pieces
, RoadTypeToRoadTypes(rt
), p2
, company
, company
);
907 /* Update company infrastructure count. */
908 Company
*c
= Company::GetIfValid(GetRoadOwner(tile
, rt
));
910 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) num_pieces
*= TUNNELBRIDGE_TRACKBIT_FACTOR
;
911 c
->infrastructure
.road
[rt
] += num_pieces
;
912 DirtyCompanyInfrastructureWindows(c
->index
);
915 if (rt
!= ROADTYPE_TRAM
&& IsNormalRoadTile(tile
)) {
917 SetDisallowedRoadDirections(tile
, IsStraightRoad(existing
) ?
918 GetDisallowedRoadDirections(tile
) ^ toggle_drd
: DRD_NONE
);
921 MarkTileDirtyByTile(tile
);
927 * Checks whether a road or tram connection can be found when building a new road or tram.
928 * @param tile Tile at which the road being built will end.
929 * @param rt Roadtype of the road being built.
930 * @param dir Direction that the road is following.
931 * @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.
933 static bool CanConnectToRoad(TileIndex tile
, RoadType rt
, DiagDirection dir
)
935 RoadBits bits
= GetAnyRoadBits(tile
+ TileOffsByDiagDir(dir
), rt
, false);
936 return (bits
& DiagDirToRoadBits(ReverseDiagDir(dir
))) != 0;
940 * Build a long piece of road.
941 * @param start_tile start tile of drag (the building cost will appear over this tile)
942 * @param flags operation to perform
943 * @param p1 end tile of drag
944 * @param p2 various bitstuffed elements
945 * - 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
946 * - 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
947 * - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4)
948 * - p2 = (bit 3 + 4) - road type
949 * - p2 = (bit 5) - set road direction
950 * - p2 = (bit 6) - defines two different behaviors for this command:
951 * - 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
952 * - 1 = Fail if an obstacle is found. Always take into account bit 0 and 1. This behavior is used for scripts
954 * @return the cost of this operation or an error
956 CommandCost
CmdBuildLongRoad(TileIndex start_tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
958 DisallowedRoadDirections drd
= DRD_NORTHBOUND
;
960 if (p1
>= MapSize()) return CMD_ERROR
;
962 TileIndex end_tile
= p1
;
963 RoadType rt
= Extract
<RoadType
, 3, 2>(p2
);
964 if (!IsValidRoadType(rt
) || !ValParamRoadType(rt
)) return CMD_ERROR
;
966 Axis axis
= Extract
<Axis
, 2, 1>(p2
);
967 /* Only drag in X or Y direction dictated by the direction variable */
968 if (axis
== AXIS_X
&& TileY(start_tile
) != TileY(end_tile
)) return CMD_ERROR
; // x-axis
969 if (axis
== AXIS_Y
&& TileX(start_tile
) != TileX(end_tile
)) return CMD_ERROR
; // y-axis
971 DiagDirection dir
= AxisToDiagDir(axis
);
973 /* Swap direction, also the half-tile drag var (bit 0 and 1) */
974 if (start_tile
> end_tile
|| (start_tile
== end_tile
&& HasBit(p2
, 0))) {
975 dir
= ReverseDiagDir(dir
);
977 drd
= DRD_SOUTHBOUND
;
980 /* On the X-axis, we have to swap the initial bits, so they
981 * will be interpreted correctly in the GTTS. Furthermore
982 * when you just 'click' on one tile to build them. */
983 if ((axis
== AXIS_Y
) == (start_tile
== end_tile
&& HasBit(p2
, 0) == HasBit(p2
, 1))) drd
^= DRD_BOTH
;
984 /* No disallowed direction bits have to be toggled */
985 if (!HasBit(p2
, 5)) drd
= DRD_NONE
;
987 CommandCost
cost(EXPENSES_CONSTRUCTION
);
988 CommandCost last_error
= CMD_ERROR
;
989 TileIndex tile
= start_tile
;
990 bool had_success
= false;
991 bool is_ai
= HasBit(p2
, 6);
993 /* Start tile is the first tile clicked by the user. */
995 RoadBits bits
= AxisToRoadBits(axis
);
997 /* Determine which road parts should be built. */
998 if (!is_ai
&& start_tile
!= end_tile
) {
999 /* Only build the first and last roadbit if they can connect to something. */
1000 if (tile
== end_tile
&& !CanConnectToRoad(tile
, rt
, dir
)) {
1001 bits
= DiagDirToRoadBits(ReverseDiagDir(dir
));
1002 } else if (tile
== start_tile
&& !CanConnectToRoad(tile
, rt
, ReverseDiagDir(dir
))) {
1003 bits
= DiagDirToRoadBits(dir
);
1006 /* Road parts only have to be built at the start tile or at the end tile. */
1007 if (tile
== end_tile
&& !HasBit(p2
, 1)) bits
&= DiagDirToRoadBits(ReverseDiagDir(dir
));
1008 if (tile
== start_tile
&& HasBit(p2
, 0)) bits
&= DiagDirToRoadBits(dir
);
1011 CommandCost ret
= DoCommand(tile
, drd
<< 6 | rt
<< 4 | bits
, 0, flags
, CMD_BUILD_ROAD
);
1014 if (last_error
.GetErrorMessage() != STR_ERROR_ALREADY_BUILT
) {
1015 if (is_ai
) return last_error
;
1023 /* Do not run into or across bridges/tunnels */
1024 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
1025 if (GetTunnelBridgeDirection(tile
) == dir
) break;
1028 if (tile
== end_tile
) break;
1030 tile
+= TileOffsByDiagDir(dir
);
1032 /* Do not run onto a bridge/tunnel tile from below/above */
1033 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
1034 if (GetTunnelBridgeDirection(tile
) == ReverseDiagDir(dir
)) break;
1038 return had_success
? cost
: last_error
;
1042 * Remove a long piece of road.
1043 * @param start_tile start tile of drag
1044 * @param flags operation to perform
1045 * @param p1 end tile of drag
1046 * @param p2 various bitstuffed elements
1047 * - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1)
1048 * - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2)
1049 * - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4)
1050 * - p2 = (bit 3 + 4) - road type
1051 * @param text unused
1052 * @return the cost of this operation or an error
1054 CommandCost
CmdRemoveLongRoad(TileIndex start_tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1056 CommandCost
cost(EXPENSES_CONSTRUCTION
);
1058 if (p1
>= MapSize()) return CMD_ERROR
;
1060 TileIndex end_tile
= p1
;
1061 RoadType rt
= Extract
<RoadType
, 3, 2>(p2
);
1062 if (!IsValidRoadType(rt
)) return CMD_ERROR
;
1064 Axis axis
= Extract
<Axis
, 2, 1>(p2
);
1065 /* Only drag in X or Y direction dictated by the direction variable */
1066 if (axis
== AXIS_X
&& TileY(start_tile
) != TileY(end_tile
)) return CMD_ERROR
; // x-axis
1067 if (axis
== AXIS_Y
&& TileX(start_tile
) != TileX(end_tile
)) return CMD_ERROR
; // y-axis
1069 /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
1070 if (start_tile
> end_tile
|| (start_tile
== end_tile
&& HasBit(p2
, 0))) {
1071 TileIndex t
= start_tile
;
1072 start_tile
= end_tile
;
1074 p2
^= IsInsideMM(p2
& 3, 1, 3) ? 3 : 0;
1077 Money money
= GetAvailableMoneyForCommand();
1078 TileIndex tile
= start_tile
;
1079 CommandCost last_error
= CMD_ERROR
;
1080 bool had_success
= false;
1081 /* Start tile is the small number. */
1083 RoadBits bits
= AxisToRoadBits(axis
);
1085 if (tile
== end_tile
&& !HasBit(p2
, 1)) bits
&= ROAD_NW
| ROAD_NE
;
1086 if (tile
== start_tile
&& HasBit(p2
, 0)) bits
&= ROAD_SE
| ROAD_SW
;
1088 /* try to remove the halves. */
1090 CommandCost ret
= RemoveRoad(tile
, flags
& ~DC_EXEC
, bits
, rt
, true);
1091 if (ret
.Succeeded()) {
1092 if (flags
& DC_EXEC
) {
1093 money
-= ret
.GetCost();
1095 _additional_cash_required
= DoCommand(start_tile
, end_tile
, p2
, flags
& ~DC_EXEC
, CMD_REMOVE_LONG_ROAD
).GetCost();
1098 RemoveRoad(tile
, flags
, bits
, rt
, true, false);
1103 /* Ownership errors are more important. */
1104 if (last_error
.GetErrorMessage() != STR_ERROR_OWNED_BY
) last_error
= ret
;
1108 if (tile
== end_tile
) break;
1110 tile
+= (axis
== AXIS_Y
) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
1113 return had_success
? cost
: last_error
;
1117 * Build a road depot.
1118 * @param tile tile where to build the depot
1119 * @param flags operation to perform
1120 * @param p1 bit 0..1 entrance direction (DiagDirection)
1121 * bit 2..3 road type
1123 * @param text unused
1124 * @return the cost of this operation or an error
1126 * @todo When checking for the tile slope,
1127 * distinguish between "Flat land required" and "land sloped in wrong direction"
1129 CommandCost
CmdBuildRoadDepot(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1131 DiagDirection dir
= Extract
<DiagDirection
, 0, 2>(p1
);
1132 RoadType rt
= Extract
<RoadType
, 2, 2>(p1
);
1134 if (!IsValidRoadType(rt
) || !ValParamRoadType(rt
)) return CMD_ERROR
;
1136 Slope tileh
= GetTileSlope(tile
);
1137 if (tileh
!= SLOPE_FLAT
&& (
1138 !_settings_game
.construction
.build_on_slopes
||
1139 !CanBuildDepotByTileh(dir
, tileh
)
1141 return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED
);
1144 CommandCost cost
= DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
1145 if (cost
.Failed()) return cost
;
1147 if (IsBridgeAbove(tile
)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
1149 if (!Depot::CanAllocateItem()) return CMD_ERROR
;
1151 if (flags
& DC_EXEC
) {
1152 Depot
*dep
= new Depot(tile
);
1153 dep
->build_date
= _date
;
1155 /* A road depot has two road bits. */
1156 Company::Get(_current_company
)->infrastructure
.road
[rt
] += 2;
1157 DirtyCompanyInfrastructureWindows(_current_company
);
1159 MakeRoadDepot(tile
, _current_company
, dep
->index
, dir
, rt
);
1160 MarkTileDirtyByTile(tile
);
1161 MakeDefaultName(dep
);
1163 cost
.AddCost(_price
[PR_BUILD_DEPOT_ROAD
]);
1167 static CommandCost
RemoveRoadDepot(TileIndex tile
, DoCommandFlag flags
)
1169 if (_current_company
!= OWNER_WATER
) {
1170 CommandCost ret
= CheckTileOwnership(tile
);
1171 if (ret
.Failed()) return ret
;
1174 CommandCost ret
= EnsureNoVehicleOnGround(tile
);
1175 if (ret
.Failed()) return ret
;
1177 if (flags
& DC_EXEC
) {
1178 Company
*c
= Company::GetIfValid(GetTileOwner(tile
));
1180 /* A road depot has two road bits. */
1181 c
->infrastructure
.road
[FIND_FIRST_BIT(GetRoadTypes(tile
))] -= 2;
1182 DirtyCompanyInfrastructureWindows(c
->index
);
1185 delete Depot::GetByTile(tile
);
1186 DoClearSquare(tile
);
1189 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_CLEAR_DEPOT_ROAD
]);
1192 static CommandCost
ClearTile_Road(TileIndex tile
, DoCommandFlag flags
)
1194 switch (GetRoadTileType(tile
)) {
1195 case ROAD_TILE_NORMAL
: {
1196 RoadBits b
= GetAllRoadBits(tile
);
1198 /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
1199 if ((HasExactlyOneBit(b
) && GetRoadBits(tile
, ROADTYPE_TRAM
) == ROAD_NONE
) || !(flags
& DC_AUTO
)) {
1200 CommandCost
ret(EXPENSES_CONSTRUCTION
);
1202 FOR_EACH_SET_ROADTYPE(rt
, GetRoadTypes(tile
)) {
1203 CommandCost tmp_ret
= RemoveRoad(tile
, flags
, GetRoadBits(tile
, rt
), rt
, true);
1204 if (tmp_ret
.Failed()) return tmp_ret
;
1205 ret
.AddCost(tmp_ret
);
1209 return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST
);
1212 case ROAD_TILE_CROSSING
: {
1213 RoadTypes rts
= GetRoadTypes(tile
);
1214 CommandCost
ret(EXPENSES_CONSTRUCTION
);
1216 if (flags
& DC_AUTO
) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST
);
1218 /* Must iterate over the roadtypes in a reverse manner because
1219 * tram tracks must be removed before the road bits. */
1220 RoadType rt
= ROADTYPE_TRAM
;
1222 if (HasBit(rts
, rt
)) {
1223 CommandCost tmp_ret
= RemoveRoad(tile
, flags
, GetCrossingRoadBits(tile
), rt
, false);
1224 if (tmp_ret
.Failed()) return tmp_ret
;
1225 ret
.AddCost(tmp_ret
);
1227 } while (rt
-- != ROADTYPE_ROAD
);
1229 if (flags
& DC_EXEC
) {
1230 DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
1236 case ROAD_TILE_DEPOT
:
1237 if (flags
& DC_AUTO
) {
1238 return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED
);
1240 return RemoveRoadDepot(tile
, flags
);
1245 struct DrawRoadTileStruct
{
1251 #include "table/road_land.h"
1254 * Get the foundationtype of a RoadBits Slope combination
1256 * @param tileh The Slope part
1257 * @param bits The RoadBits part
1258 * @return The resulting Foundation
1260 static Foundation
GetRoadFoundation(Slope tileh
, RoadBits bits
)
1262 /* Flat land and land without a road doesn't require a foundation */
1263 if (tileh
== SLOPE_FLAT
|| bits
== ROAD_NONE
) return FOUNDATION_NONE
;
1265 /* Steep slopes behave the same as slopes with one corner raised. */
1266 if (IsSteepSlope(tileh
)) {
1267 tileh
= SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh
));
1270 /* Leveled RoadBits on a slope */
1271 if ((_invalid_tileh_slopes_road
[0][tileh
] & bits
) == ROAD_NONE
) return FOUNDATION_LEVELED
;
1273 /* Straight roads without foundation on a slope */
1274 if (!IsSlopeWithOneCornerRaised(tileh
) &&
1275 (_invalid_tileh_slopes_road
[1][tileh
] & bits
) == ROAD_NONE
)
1276 return FOUNDATION_NONE
;
1278 /* Roads on steep Slopes or on Slopes with one corner raised */
1279 return (bits
== ROAD_X
? FOUNDATION_INCLINED_X
: FOUNDATION_INCLINED_Y
);
1282 const byte _road_sloped_sprites
[14] = {
1290 * Should the road be drawn as a unpaved snow/desert road?
1291 * By default, roads are always drawn as unpaved if they are on desert or
1292 * above the snow line, but NewGRFs can override this for desert.
1294 * @param tile The tile the road is on
1295 * @param roadside What sort of road this is
1296 * @return True if snow/desert road sprites should be used.
1298 static bool DrawRoadAsSnowDesert(TileIndex tile
, Roadside roadside
)
1300 return (IsOnSnow(tile
) &&
1301 !(_settings_game
.game_creation
.landscape
== LT_TROPIC
&& HasGrfMiscBit(GMB_DESERT_PAVED_ROADS
) &&
1302 roadside
!= ROADSIDE_BARREN
&& roadside
!= ROADSIDE_GRASS
&& roadside
!= ROADSIDE_GRASS_ROAD_WORKS
));
1306 * Draws the catenary for the given tile
1307 * @param ti information about the tile (slopes, height etc)
1308 * @param tram the roadbits for the tram
1310 void DrawRoadCatenary(const TileInfo
*ti
, RoadBits tram
)
1312 /* Do not draw catenary if it is invisible */
1313 if (IsInvisibilitySet(TO_CATENARY
)) return;
1315 /* Don't draw the catenary under a low bridge */
1316 if (IsBridgeAbove(ti
->tile
) && !IsTransparencySet(TO_CATENARY
)) {
1317 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
1319 if (height
<= GetTileMaxZ(ti
->tile
) + 1) return;
1325 if (ti
->tileh
!= SLOPE_FLAT
) {
1326 back
= SPR_TRAMWAY_BACK_WIRES_SLOPED
+ _road_sloped_sprites
[ti
->tileh
- 1];
1327 front
= SPR_TRAMWAY_FRONT_WIRES_SLOPED
+ _road_sloped_sprites
[ti
->tileh
- 1];
1329 back
= SPR_TRAMWAY_BASE
+ _road_backpole_sprites_1
[tram
];
1330 front
= SPR_TRAMWAY_BASE
+ _road_frontwire_sprites_1
[tram
];
1333 AddSortableSpriteToDraw(back
, PAL_NONE
, ti
->x
, ti
->y
, 16, 16, TILE_HEIGHT
+ BB_HEIGHT_UNDER_BRIDGE
, ti
->z
, IsTransparencySet(TO_CATENARY
));
1334 AddSortableSpriteToDraw(front
, PAL_NONE
, ti
->x
, ti
->y
, 16, 16, TILE_HEIGHT
+ BB_HEIGHT_UNDER_BRIDGE
, ti
->z
, IsTransparencySet(TO_CATENARY
));
1338 * Draws details on/around the road
1339 * @param img the sprite to draw
1340 * @param ti the tile to draw on
1341 * @param dx the offset from the top of the BB of the tile
1342 * @param dy the offset from the top of the BB of the tile
1343 * @param h the height of the sprite to draw
1345 static void DrawRoadDetail(SpriteID img
, const TileInfo
*ti
, int dx
, int dy
, int h
)
1350 if (ti
->tileh
!= SLOPE_FLAT
) z
= GetSlopePixelZ(x
, y
);
1351 AddSortableSpriteToDraw(img
, PAL_NONE
, x
, y
, 2, 2, h
, z
);
1355 * Draw ground sprite and road pieces
1356 * @param ti TileInfo
1358 void DrawRoadBits(TileInfo
*ti
)
1360 const bool is_bridge
= IsTileType(ti
->tile
, MP_TUNNELBRIDGE
);
1361 RoadBits road
= is_bridge
? GetCustomBridgeHeadRoadBits(ti
->tile
, ROADTYPE_ROAD
) : GetRoadBits(ti
->tile
, ROADTYPE_ROAD
);
1362 RoadBits tram
= is_bridge
? GetCustomBridgeHeadRoadBits(ti
->tile
, ROADTYPE_TRAM
) : GetRoadBits(ti
->tile
, ROADTYPE_TRAM
);
1365 PaletteID pal
= PAL_NONE
;
1367 if (ti
->tileh
!= SLOPE_FLAT
) {
1368 DrawFoundation(ti
, is_bridge
? FOUNDATION_LEVELED
: GetRoadFoundation(ti
->tileh
, road
| tram
));
1370 /* DrawFoundation() modifies ti.
1371 * Default sloped sprites.. */
1372 if (ti
->tileh
!= SLOPE_FLAT
) image
= _road_sloped_sprites
[ti
->tileh
- 1] + SPR_ROAD_SLOPE_START
;
1375 if (image
== 0) image
= _road_tile_sprites_1
[road
!= ROAD_NONE
? road
: tram
];
1377 Roadside roadside
= is_bridge
? ROADSIDE_PAVED
: GetRoadside(ti
->tile
);
1379 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1383 case ROADSIDE_BARREN
: pal
= PALETTE_TO_BARE_LAND
; break;
1384 case ROADSIDE_GRASS
: break;
1385 case ROADSIDE_GRASS_ROAD_WORKS
: break;
1386 default: image
-= 19; break; // Paved
1390 DrawGroundSprite(image
, pal
);
1392 /* For tram we overlay the road graphics with either tram tracks only
1393 * (when there is actual road beneath the trams) or with tram tracks
1394 * and some dirts which hides the road graphics */
1395 if (tram
!= ROAD_NONE
) {
1396 if (ti
->tileh
!= SLOPE_FLAT
) {
1397 image
= _road_sloped_sprites
[ti
->tileh
- 1] + SPR_TRAMWAY_SLOPED_OFFSET
;
1399 image
= _road_tile_sprites_1
[tram
] - SPR_ROAD_Y
;
1401 image
+= (road
== ROAD_NONE
) ? SPR_TRAMWAY_TRAM
: SPR_TRAMWAY_OVERLAY
;
1402 DrawGroundSprite(image
, pal
);
1405 if (!is_bridge
&& road
!= ROAD_NONE
) {
1406 DisallowedRoadDirections drd
= GetDisallowedRoadDirections(ti
->tile
);
1407 if (drd
!= DRD_NONE
) {
1408 DrawGroundSpriteAt(SPR_ONEWAY_BASE
+ drd
- 1 + ((road
== ROAD_X
) ? 0 : 3), PAL_NONE
, 8, 8, GetPartialPixelZ(8, 8, ti
->tileh
));
1412 if (!is_bridge
&& HasRoadWorks(ti
->tile
)) {
1414 DrawGroundSprite((road
| tram
) & ROAD_X
? SPR_EXCAVATION_X
: SPR_EXCAVATION_Y
, PAL_NONE
);
1418 if (tram
!= ROAD_NONE
) DrawRoadCatenary(ti
, tram
);
1420 /* Return if full detail is disabled, or we are zoomed fully out. */
1421 if (!HasBit(_display_opt
, DO_FULL_DETAIL
) || _cur_dpi
->zoom
> ZOOM_LVL_DETAIL
) return;
1423 /* Do not draw details (street lights, trees) under low bridge */
1424 if (IsBridgeAbove(ti
->tile
) && (roadside
== ROADSIDE_TREES
|| roadside
== ROADSIDE_STREET_LIGHTS
)) {
1425 int height
= GetBridgeHeight(GetNorthernBridgeEnd(ti
->tile
));
1426 int minz
= GetTileMaxZ(ti
->tile
) + 2;
1428 if (roadside
== ROADSIDE_TREES
) minz
++;
1430 if (height
< minz
) return;
1433 /* If there are no road bits, return, as there is nothing left to do */
1434 if (HasAtMostOneBit(road
)) return;
1436 /* Draw extra details. */
1437 for (const DrawRoadTileStruct
*drts
= _road_display_table
[roadside
][road
| tram
]; drts
->image
!= 0; drts
++) {
1438 DrawRoadDetail(drts
->image
, ti
, drts
->subcoord_x
, drts
->subcoord_y
, 0x10);
1442 /** Tile callback function for rendering a road tile to the screen */
1443 static void DrawTile_Road(TileInfo
*ti
)
1445 switch (GetRoadTileType(ti
->tile
)) {
1446 case ROAD_TILE_NORMAL
:
1448 DrawOverlay(ti
, MP_ROAD
);
1451 case ROAD_TILE_CROSSING
: {
1452 if (ti
->tileh
!= SLOPE_FLAT
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
1454 DrawOverlay(ti
, MP_ROAD
);
1456 PaletteID pal
= PAL_NONE
;
1457 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(ti
->tile
));
1459 if (rti
->UsesOverlay()) {
1460 Axis axis
= GetCrossingRailAxis(ti
->tile
);
1461 SpriteID road
= SPR_ROAD_Y
+ axis
;
1463 Roadside roadside
= GetRoadside(ti
->tile
);
1465 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1469 case ROADSIDE_BARREN
: pal
= PALETTE_TO_BARE_LAND
; break;
1470 case ROADSIDE_GRASS
: break;
1471 default: road
-= 19; break; // Paved
1475 DrawGroundSprite(road
, pal
);
1477 SpriteID rail
= GetCustomRailSprite(rti
, ti
->tile
, RTSG_CROSSING
) + axis
;
1478 /* Draw tracks, but draw PBS reserved tracks darker. */
1479 pal
= (_game_mode
!= GM_MENU
&& _settings_client
.gui
.show_track_reservation
&& HasCrossingReservation(ti
->tile
)) ? PALETTE_CRASH
: PAL_NONE
;
1480 DrawGroundSprite(rail
, pal
);
1482 DrawRailTileSeq(ti
, &_crossing_layout
, TO_CATENARY
, rail
, 0, PAL_NONE
);
1484 SpriteID image
= rti
->base_sprites
.crossing
;
1486 if (GetCrossingRoadAxis(ti
->tile
) == AXIS_X
) image
++;
1487 if (IsCrossingBarred(ti
->tile
)) image
+= 2;
1489 Roadside roadside
= GetRoadside(ti
->tile
);
1491 if (DrawRoadAsSnowDesert(ti
->tile
, roadside
)) {
1495 case ROADSIDE_BARREN
: pal
= PALETTE_TO_BARE_LAND
; break;
1496 case ROADSIDE_GRASS
: break;
1497 default: image
+= 4; break; // Paved
1501 DrawGroundSprite(image
, pal
);
1503 /* PBS debugging, draw reserved tracks darker */
1504 if (_game_mode
!= GM_MENU
&& _settings_client
.gui
.show_track_reservation
&& HasCrossingReservation(ti
->tile
)) {
1505 DrawGroundSprite(GetCrossingRoadAxis(ti
->tile
) == AXIS_Y
? GetRailTypeInfo(GetRailType(ti
->tile
))->base_sprites
.single_x
: GetRailTypeInfo(GetRailType(ti
->tile
))->base_sprites
.single_y
, PALETTE_CRASH
);
1509 if (HasTileRoadType(ti
->tile
, ROADTYPE_TRAM
)) {
1510 DrawGroundSprite(SPR_TRAMWAY_OVERLAY
+ (GetCrossingRoadAxis(ti
->tile
) ^ 1), pal
);
1511 DrawRoadCatenary(ti
, GetCrossingRoadBits(ti
->tile
));
1513 if (HasRailCatenaryDrawn(GetRailType(ti
->tile
))) DrawRailCatenary(ti
);
1518 case ROAD_TILE_DEPOT
: {
1519 if (ti
->tileh
!= SLOPE_FLAT
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
1521 PaletteID palette
= COMPANY_SPRITE_COLOUR(GetTileOwner(ti
->tile
));
1523 const DrawTileSprites
*dts
;
1524 if (HasTileRoadType(ti
->tile
, ROADTYPE_TRAM
)) {
1525 dts
= &_tram_depot
[GetRoadDepotDirection(ti
->tile
)];
1527 dts
= &_road_depot
[GetRoadDepotDirection(ti
->tile
)];
1530 DrawGroundSprite(dts
->ground
.sprite
, PAL_NONE
);
1531 DrawOverlay(ti
, MP_ROAD
);
1532 DrawOrigTileSeq(ti
, dts
, TO_BUILDINGS
, palette
);
1536 DrawBridgeMiddle(ti
);
1540 * Draw the road depot sprite.
1541 * @param x The x offset to draw at.
1542 * @param y The y offset to draw at.
1543 * @param dir The direction the depot must be facing.
1544 * @param rt The road type of the depot to draw.
1546 void DrawRoadDepotSprite(int x
, int y
, DiagDirection dir
, RoadType rt
)
1548 PaletteID palette
= COMPANY_SPRITE_COLOUR(_local_company
);
1549 const DrawTileSprites
*dts
= (rt
== ROADTYPE_TRAM
) ? &_tram_depot
[dir
] : &_road_depot
[dir
];
1551 DrawSprite(dts
->ground
.sprite
, PAL_NONE
, x
, y
);
1552 DrawOrigTileSeqInGUI(x
, y
, dts
, palette
);
1556 * Updates cached nearest town for all road tiles
1557 * @param invalidate are we just invalidating cached data?
1558 * @pre invalidate == true implies _generating_world == true
1560 void UpdateNearestTownForRoadTiles(bool invalidate
)
1562 assert(!invalidate
|| _generating_world
);
1564 for (TileIndex t
= 0; t
< MapSize(); t
++) {
1565 if (IsTileType(t
, MP_ROAD
) && !IsRoadDepot(t
) && !HasTownOwnedRoad(t
)) {
1566 TownID tid
= (TownID
)INVALID_TOWN
;
1568 const Town
*town
= CalcClosestTownFromTile(t
);
1569 if (town
!= NULL
) tid
= town
->index
;
1571 SetTownIndex(t
, tid
);
1576 static int GetSlopePixelZ_Road(TileIndex tile
, uint x
, uint y
)
1579 if (IsNormalRoad(tile
)) {
1581 Slope tileh
= GetTilePixelSlope(tile
, &z
);
1582 if (tileh
== SLOPE_FLAT
) return z
;
1584 Foundation f
= GetRoadFoundation(tileh
, GetAllRoadBits(tile
));
1585 z
+= ApplyPixelFoundationToSlope(f
, &tileh
);
1586 return z
+ GetPartialPixelZ(x
& 0xF, y
& 0xF, tileh
);
1588 return GetTileMaxPixelZ(tile
);
1592 static Foundation
GetFoundation_Road(TileIndex tile
, Slope tileh
)
1594 if (IsNormalRoad(tile
)) {
1595 return GetRoadFoundation(tileh
, GetAllRoadBits(tile
));
1597 return FlatteningFoundation(tileh
);
1601 static const Roadside _town_road_types
[][2] = {
1602 { ROADSIDE_GRASS
, ROADSIDE_GRASS
},
1603 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1604 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1605 { ROADSIDE_TREES
, ROADSIDE_TREES
},
1606 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
}
1609 static const Roadside _town_road_types_2
[][2] = {
1610 { ROADSIDE_GRASS
, ROADSIDE_GRASS
},
1611 { ROADSIDE_PAVED
, ROADSIDE_PAVED
},
1612 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
},
1613 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
},
1614 { ROADSIDE_STREET_LIGHTS
, ROADSIDE_PAVED
}
1618 static void TileLoop_Road(TileIndex tile
)
1620 switch (_settings_game
.game_creation
.landscape
) {
1622 if (IsOnSnow(tile
) != (GetTileMaxZ(tile
) > GetSnowLine())) {
1624 MarkTileDirtyByTile(tile
);
1629 if (GetTropicZone(tile
) == TROPICZONE_DESERT
&& !IsOnDesert(tile
)) {
1631 MarkTileDirtyByTile(tile
);
1636 if (IsRoadDepot(tile
)) return;
1638 const Town
*t
= ClosestTownFromTile(tile
, UINT_MAX
);
1639 if (!HasRoadWorks(tile
)) {
1640 HouseZonesBits grp
= HZB_TOWN_EDGE
;
1643 grp
= GetTownRadiusGroup(t
, tile
);
1645 /* Show an animation to indicate road work */
1646 if (t
->road_build_months
!= 0 &&
1647 (DistanceManhattan(t
->xy
, tile
) < 8 || grp
!= HZB_TOWN_EDGE
) &&
1648 IsNormalRoad(tile
) && !HasAtMostOneBit(GetAllRoadBits(tile
))) {
1649 if (GetFoundationSlope(tile
) == SLOPE_FLAT
&& EnsureNoVehicleOnGround(tile
).Succeeded() && Chance16(1, 40)) {
1650 StartRoadWorks(tile
);
1652 if (_settings_client
.sound
.ambient
) SndPlayTileFx(SND_21_JACKHAMMER
, tile
);
1653 CreateEffectVehicleAbove(
1654 TileX(tile
) * TILE_SIZE
+ 7,
1655 TileY(tile
) * TILE_SIZE
+ 7,
1658 MarkTileDirtyByTile(tile
, ZOOM_LVL_DRAW_MAP
);
1665 /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
1666 const Roadside
*new_rs
= (_settings_game
.game_creation
.landscape
== LT_TOYLAND
) ? _town_road_types_2
[grp
] : _town_road_types
[grp
];
1667 Roadside cur_rs
= GetRoadside(tile
);
1669 /* We have our desired type, do nothing */
1670 if (cur_rs
== new_rs
[0]) return;
1672 /* We have the pre-type of the desired type, switch to the desired type */
1673 if (cur_rs
== new_rs
[1]) {
1675 /* We have barren land, install the pre-type */
1676 } else if (cur_rs
== ROADSIDE_BARREN
) {
1678 /* We're totally off limits, remove any installation and make barren land */
1680 cur_rs
= ROADSIDE_BARREN
;
1682 SetRoadside(tile
, cur_rs
);
1683 MarkTileDirtyByTile(tile
, ZOOM_LVL_DRAW_MAP
);
1685 } else if (IncreaseRoadWorksCounter(tile
)) {
1686 TerminateRoadWorks(tile
);
1688 if (_settings_game
.economy
.mod_road_rebuild
) {
1689 /* Generate a nicer town surface */
1690 const RoadBits old_rb
= GetAnyRoadBits(tile
, ROADTYPE_ROAD
);
1691 const RoadBits new_rb
= CleanUpRoadBits(tile
, old_rb
);
1693 if (old_rb
!= new_rb
) {
1694 RemoveRoad(tile
, DC_EXEC
| DC_AUTO
| DC_NO_WATER
, (old_rb
^ new_rb
), ROADTYPE_ROAD
, true);
1698 MarkTileDirtyByTile(tile
, ZOOM_LVL_DRAW_MAP
);
1702 static bool ClickTile_Road(TileIndex tile
)
1704 if (!IsRoadDepot(tile
)) return false;
1706 ShowDepotWindow(tile
, VEH_ROAD
);
1710 /* Converts RoadBits to TrackBits */
1711 extern const TrackBits _road_trackbits
[16] = {
1712 TRACK_BIT_NONE
, // ROAD_NONE
1713 TRACK_BIT_NONE
, // ROAD_NW
1714 TRACK_BIT_NONE
, // ROAD_SW
1715 TRACK_BIT_LEFT
, // ROAD_W
1716 TRACK_BIT_NONE
, // ROAD_SE
1717 TRACK_BIT_Y
, // ROAD_Y
1718 TRACK_BIT_LOWER
, // ROAD_S
1719 TRACK_BIT_LEFT
| TRACK_BIT_LOWER
| TRACK_BIT_Y
, // ROAD_Y | ROAD_SW
1720 TRACK_BIT_NONE
, // ROAD_NE
1721 TRACK_BIT_UPPER
, // ROAD_N
1722 TRACK_BIT_X
, // ROAD_X
1723 TRACK_BIT_LEFT
| TRACK_BIT_UPPER
| TRACK_BIT_X
, // ROAD_X | ROAD_NW
1724 TRACK_BIT_RIGHT
, // ROAD_E
1725 TRACK_BIT_RIGHT
| TRACK_BIT_UPPER
| TRACK_BIT_Y
, // ROAD_Y | ROAD_NE
1726 TRACK_BIT_RIGHT
| TRACK_BIT_LOWER
| TRACK_BIT_X
, // ROAD_X | ROAD_SE
1727 TRACK_BIT_ALL
, // ROAD_ALL
1730 static TrackStatus
GetTileTrackStatus_Road(TileIndex tile
, TransportType mode
, uint sub_mode
, DiagDirection side
)
1732 TrackdirBits trackdirbits
= TRACKDIR_BIT_NONE
;
1733 TrackdirBits red_signals
= TRACKDIR_BIT_NONE
; // crossing barred
1735 case TRANSPORT_RAIL
:
1736 if (IsLevelCrossing(tile
)) trackdirbits
= TrackBitsToTrackdirBits(GetCrossingRailBits(tile
));
1739 case TRANSPORT_ROAD
:
1740 if ((GetRoadTypes(tile
) & sub_mode
) == 0) break;
1741 switch (GetRoadTileType(tile
)) {
1742 case ROAD_TILE_NORMAL
: {
1743 const uint drd_to_multiplier
[DRD_END
] = { 0x101, 0x100, 0x1, 0x0 };
1744 RoadType rt
= (RoadType
)FindFirstBit(sub_mode
);
1745 RoadBits bits
= GetRoadBits(tile
, rt
);
1747 /* no roadbit at this side of tile, return 0 */
1748 if (side
!= INVALID_DIAGDIR
&& (DiagDirToRoadBits(side
) & bits
) == 0) break;
1750 uint multiplier
= drd_to_multiplier
[rt
== ROADTYPE_TRAM
? DRD_NONE
: GetDisallowedRoadDirections(tile
)];
1751 if (!HasRoadWorks(tile
)) trackdirbits
= (TrackdirBits
)(_road_trackbits
[bits
] * multiplier
);
1755 case ROAD_TILE_CROSSING
: {
1756 Axis axis
= GetCrossingRoadAxis(tile
);
1758 if (side
!= INVALID_DIAGDIR
&& axis
!= DiagDirToAxis(side
)) break;
1760 trackdirbits
= TrackBitsToTrackdirBits(AxisToTrackBits(axis
));
1761 if (IsCrossingBarred(tile
)) red_signals
= trackdirbits
;
1762 if (IsLevelCrossingTile(TileAddByDiagDir(tile
, AxisToDiagDir(axis
))) &&
1763 IsCrossingBarred(TileAddByDiagDir(tile
, AxisToDiagDir(axis
)))) {
1764 red_signals
&= (TrackdirBits
)0x0102; // magic value. I think TRACKBIT_X_SW and TRACKBIT_X_NE should be swapped
1766 if (IsLevelCrossingTile(TileAddByDiagDir(tile
, ReverseDiagDir(AxisToDiagDir(axis
)))) &&
1767 IsCrossingBarred(TileAddByDiagDir(tile
, ReverseDiagDir(AxisToDiagDir(axis
))))) {
1768 red_signals
&= (TrackdirBits
)0x0201; // inverse of above magic value
1774 case ROAD_TILE_DEPOT
: {
1775 DiagDirection dir
= GetRoadDepotDirection(tile
);
1777 if (side
!= INVALID_DIAGDIR
&& side
!= dir
) break;
1779 trackdirbits
= TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir
));
1787 return CombineTrackStatus(trackdirbits
, red_signals
);
1790 static const StringID _road_tile_strings
[] = {
1791 STR_LAI_ROAD_DESCRIPTION_ROAD
,
1792 STR_LAI_ROAD_DESCRIPTION_ROAD
,
1793 STR_LAI_ROAD_DESCRIPTION_ROAD
,
1794 STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS
,
1795 STR_LAI_ROAD_DESCRIPTION_ROAD
,
1796 STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD
,
1797 STR_LAI_ROAD_DESCRIPTION_ROAD
,
1798 STR_LAI_ROAD_DESCRIPTION_ROAD
,
1801 static void GetTileDesc_Road(TileIndex tile
, TileDesc
*td
)
1803 Owner rail_owner
= INVALID_OWNER
;
1804 Owner road_owner
= INVALID_OWNER
;
1805 Owner tram_owner
= INVALID_OWNER
;
1807 switch (GetRoadTileType(tile
)) {
1808 case ROAD_TILE_CROSSING
: {
1809 td
->str
= STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING
;
1810 RoadTypes rts
= GetRoadTypes(tile
);
1811 rail_owner
= GetTileOwner(tile
);
1812 if (HasBit(rts
, ROADTYPE_ROAD
)) road_owner
= GetRoadOwner(tile
, ROADTYPE_ROAD
);
1813 if (HasBit(rts
, ROADTYPE_TRAM
)) tram_owner
= GetRoadOwner(tile
, ROADTYPE_TRAM
);
1815 const RailtypeInfo
*rti
= GetRailTypeInfo(GetRailType(tile
));
1816 td
->railtype
= rti
->strings
.name
;
1817 td
->rail_speed
= rti
->max_speed
;
1822 case ROAD_TILE_DEPOT
:
1823 td
->str
= STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT
;
1824 road_owner
= GetTileOwner(tile
); // Tile has only one owner, roadtype does not matter
1825 td
->build_date
= Depot::GetByTile(tile
)->build_date
;
1829 RoadTypes rts
= GetRoadTypes(tile
);
1830 td
->str
= (HasBit(rts
, ROADTYPE_ROAD
) ? _road_tile_strings
[GetRoadside(tile
)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY
);
1831 if (HasBit(rts
, ROADTYPE_ROAD
)) road_owner
= GetRoadOwner(tile
, ROADTYPE_ROAD
);
1832 if (HasBit(rts
, ROADTYPE_TRAM
)) tram_owner
= GetRoadOwner(tile
, ROADTYPE_TRAM
);
1837 /* Now we have to discover, if the tile has only one owner or many:
1838 * - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
1839 * - Compare the found owner with the other owners, and test if they differ.
1840 * Note: If road exists it will be the first_owner.
1842 Owner first_owner
= (road_owner
== INVALID_OWNER
? tram_owner
: road_owner
);
1843 bool mixed_owners
= (tram_owner
!= INVALID_OWNER
&& tram_owner
!= first_owner
) || (rail_owner
!= INVALID_OWNER
&& rail_owner
!= first_owner
);
1846 /* Multiple owners */
1847 td
->owner_type
[0] = (rail_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_RAIL_OWNER
);
1848 td
->owner
[0] = rail_owner
;
1849 td
->owner_type
[1] = (road_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_ROAD_OWNER
);
1850 td
->owner
[1] = road_owner
;
1851 td
->owner_type
[2] = (tram_owner
== INVALID_OWNER
? STR_NULL
: STR_LAND_AREA_INFORMATION_TRAM_OWNER
);
1852 td
->owner
[2] = tram_owner
;
1854 /* One to rule them all */
1855 td
->owner
[0] = first_owner
;
1860 * Given the direction the road depot is pointing, this is the direction the
1861 * vehicle should be travelling in in order to enter the depot.
1863 static const byte _roadveh_enter_depot_dir
[4] = {
1864 TRACKDIR_X_SW
, TRACKDIR_Y_NW
, TRACKDIR_X_NE
, TRACKDIR_Y_SE
1867 static VehicleEnterTileStatus
VehicleEnter_Road(Vehicle
*v
, TileIndex tile
, int x
, int y
)
1869 switch (GetRoadTileType(tile
)) {
1870 case ROAD_TILE_DEPOT
: {
1871 if (v
->type
!= VEH_ROAD
) break;
1873 RoadVehicle
*rv
= RoadVehicle::From(v
);
1874 if (rv
->frame
== RVC_DEPOT_STOP_FRAME
&&
1875 _roadveh_enter_depot_dir
[GetRoadDepotDirection(tile
)] == rv
->state
) {
1876 rv
->state
= RVSB_IN_DEPOT
;
1877 rv
->vehstatus
|= VS_HIDDEN
;
1878 rv
->direction
= ReverseDir(rv
->direction
);
1879 if (rv
->Next() == NULL
) VehicleEnterDepot(rv
->First());
1882 InvalidateWindowData(WC_VEHICLE_DEPOT
, rv
->tile
);
1883 return VETSB_ENTERED_WORMHOLE
;
1890 return VETSB_CONTINUE
;
1894 static void ChangeTileOwner_Road(TileIndex tile
, Owner old_owner
, Owner new_owner
)
1896 if (IsRoadDepot(tile
)) {
1897 if (GetTileOwner(tile
) == old_owner
) {
1898 if (new_owner
== INVALID_OWNER
) {
1899 DoCommand(tile
, 0, 0, DC_EXEC
| DC_BANKRUPT
, CMD_LANDSCAPE_CLEAR
);
1901 /* A road depot has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
1902 RoadType rt
= (RoadType
)FIND_FIRST_BIT(GetRoadTypes(tile
));
1903 Company::Get(old_owner
)->infrastructure
.road
[rt
] -= 2;
1904 Company::Get(new_owner
)->infrastructure
.road
[rt
] += 2;
1906 SetTileOwner(tile
, new_owner
);
1907 for (RoadType rt
= ROADTYPE_ROAD
; rt
< ROADTYPE_END
; rt
++) {
1908 if (GetRoadOwner(tile
, rt
) == old_owner
) {
1909 SetRoadOwner(tile
, rt
, new_owner
);
1917 for (RoadType rt
= ROADTYPE_ROAD
; rt
< ROADTYPE_END
; rt
++) {
1918 /* Update all roadtypes, no matter if they are present */
1919 if (GetRoadOwner(tile
, rt
) == old_owner
) {
1920 if (HasTileRoadType(tile
, rt
)) {
1921 /* A level crossing has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
1922 uint num_bits
= IsLevelCrossing(tile
) ? 2 : CountBits(GetRoadBits(tile
, rt
));
1923 Company::Get(old_owner
)->infrastructure
.road
[rt
] -= num_bits
;
1924 if (new_owner
!= INVALID_OWNER
) Company::Get(new_owner
)->infrastructure
.road
[rt
] += num_bits
;
1927 SetRoadOwner(tile
, rt
, new_owner
== INVALID_OWNER
? OWNER_NONE
: new_owner
);
1931 if (IsLevelCrossing(tile
)) {
1932 if (GetTileOwner(tile
) == old_owner
) {
1933 if (new_owner
== INVALID_OWNER
) {
1934 DoCommand(tile
, 0, GetCrossingRailTrack(tile
), DC_EXEC
| DC_BANKRUPT
, CMD_REMOVE_SINGLE_RAIL
);
1936 /* Update infrastructure counts. No need to dirty windows here, we'll redraw the whole screen anyway. */
1937 Company::Get(old_owner
)->infrastructure
.rail
[GetRailType(tile
)] -= LEVELCROSSING_TRACKBIT_FACTOR
;
1938 Company::Get(new_owner
)->infrastructure
.rail
[GetRailType(tile
)] += LEVELCROSSING_TRACKBIT_FACTOR
;
1940 SetTileOwner(tile
, new_owner
);
1946 static CommandCost
TerraformTile_Road(TileIndex tile
, DoCommandFlag flags
, int z_new
, Slope tileh_new
)
1948 if (_settings_game
.construction
.build_on_slopes
&& AutoslopeEnabled()) {
1949 switch (GetRoadTileType(tile
)) {
1950 case ROAD_TILE_CROSSING
:
1951 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
]);
1954 case ROAD_TILE_DEPOT
:
1955 if (AutoslopeCheckForEntranceEdge(tile
, z_new
, tileh_new
, GetRoadDepotDirection(tile
))) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
1958 case ROAD_TILE_NORMAL
: {
1959 RoadBits bits
= GetAllRoadBits(tile
);
1960 RoadBits bits_copy
= bits
;
1961 /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
1962 if (CheckRoadSlope(tileh_new
, &bits_copy
, ROAD_NONE
, ROAD_NONE
).Succeeded()) {
1963 /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
1964 if (bits
== bits_copy
) {
1966 Slope tileh_old
= GetTileSlope(tile
, &z_old
);
1968 /* Get the slope on top of the foundation */
1969 z_old
+= ApplyFoundationToSlope(GetRoadFoundation(tileh_old
, bits
), &tileh_old
);
1970 z_new
+= ApplyFoundationToSlope(GetRoadFoundation(tileh_new
, bits
), &tileh_new
);
1972 /* The surface slope must not be changed */
1973 if ((z_old
== z_new
) && (tileh_old
== tileh_new
)) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
1979 default: NOT_REACHED();
1983 return DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
1986 /** Tile callback functions for road tiles */
1987 extern const TileTypeProcs _tile_type_road_procs
= {
1988 DrawTile_Road
, // draw_tile_proc
1989 GetSlopePixelZ_Road
, // get_slope_z_proc
1990 ClearTile_Road
, // clear_tile_proc
1991 NULL
, // add_accepted_cargo_proc
1992 GetTileDesc_Road
, // get_tile_desc_proc
1993 GetTileTrackStatus_Road
, // get_tile_track_status_proc
1994 ClickTile_Road
, // click_tile_proc
1995 NULL
, // animate_tile_proc
1996 TileLoop_Road
, // tile_loop_proc
1997 ChangeTileOwner_Road
, // change_tile_owner_proc
1998 NULL
, // add_produced_cargo_proc
1999 VehicleEnter_Road
, // vehicle_enter_tile_proc
2000 GetFoundation_Road
, // get_foundation_proc
2001 TerraformTile_Road
, // terraform_tile_proc