OpenTTD: src/road_cmd.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

road_cmd.cpp
Go to the documentation of this file.
1 /* $Id: road_cmd.cpp 27157 2015-02-22 14:01:24Z frosch $ */
2
3 /*
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/>.
8 */
9
12 #include "stdafx.h"
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"
19 #include "newgrf.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"
30 #include "roadveh.h"
31 #include "town.h"
32 #include "company_base.h"
33 #include "core/random_func.hpp"
34 #include "newgrf_railtype.h"
35 #include "date_func.h"
36 #include "genworld.h"
37 #include "company_gui.h"
38
39 #include "table/strings.h"
40
41 #include "safeguards.h"
42
47 bool RoadVehiclesAreBuilt()
48 {
49 const RoadVehicle *rv;
50 FOR_ALL_ROADVEHICLES(rv) return true;
51
52 return false;
53 }
54
56 static const RoadBits _invalid_tileh_slopes_road[2][15] = {
57 /* The inverse of the mixable RoadBits on a leveled slope */
58 {
59 ROAD_NONE, // SLOPE_FLAT
60 ROAD_NE | ROAD_SE, // SLOPE_W
61 ROAD_NE | ROAD_NW, // SLOPE_S
62
63 ROAD_NE, // SLOPE_SW
64 ROAD_NW | ROAD_SW, // SLOPE_E
65 ROAD_NONE, // SLOPE_EW
66
67 ROAD_NW, // SLOPE_SE
68 ROAD_NONE, // SLOPE_WSE
69 ROAD_SE | ROAD_SW, // SLOPE_N
70
71 ROAD_SE, // SLOPE_NW
72 ROAD_NONE, // SLOPE_NS
73 ROAD_NONE, // SLOPE_ENW
74
75 ROAD_SW, // SLOPE_NE
76 ROAD_NONE, // SLOPE_SEN
77 ROAD_NONE // SLOPE_NWS
78 },
79 /* The inverse of the allowed straight roads on a slope
80 * (with and without a foundation). */
81 {
82 ROAD_NONE, // SLOPE_FLAT
83 ROAD_NONE, // SLOPE_W Foundation
84 ROAD_NONE, // SLOPE_S Foundation
85
86 ROAD_Y, // SLOPE_SW
87 ROAD_NONE, // SLOPE_E Foundation
88 ROAD_ALL, // SLOPE_EW
89
90 ROAD_X, // SLOPE_SE
91 ROAD_ALL, // SLOPE_WSE
92 ROAD_NONE, // SLOPE_N Foundation
93
94 ROAD_X, // SLOPE_NW
95 ROAD_ALL, // SLOPE_NS
96 ROAD_ALL, // SLOPE_ENW
97
98 ROAD_Y, // SLOPE_NE
99 ROAD_ALL, // SLOPE_SEN
100 ROAD_ALL // SLOPE_NW
101 }
102 };
103
104 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits);
105
116 CommandCost CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, RoadType rt, DoCommandFlag flags, bool town_check)
117 {
118 if (_game_mode == GM_EDITOR || remove == ROAD_NONE) return CommandCost();
119
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();
125
126 /* Only do the special processing if the road is owned
127 * by a town */
128 if (owner != OWNER_TOWN) {
129 if (owner == OWNER_NONE) return CommandCost();
130 CommandCost ret = CheckOwnership(owner);
131 return ret;
132 }
133
134 if (!town_check) return CommandCost();
135
136 if (_cheats.magic_bulldozer.value) return CommandCost();
137
138 Town *t = ClosestTownFromTile(tile, UINT_MAX);
139 if (t == NULL) return CommandCost();
140
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;
145
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;
153
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,
156 * then allow it */
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);
162 }
163 rating_decrease = RATING_ROAD_DOWN_STEP_INNER;
164 }
165 ChangeTownRating(t, rating_decrease, RATING_ROAD_MINIMUM, flags);
166
167 return CommandCost();
168 }
169
170
180 static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits pieces, RoadType rt, bool crossing_check, bool town_check = true)
181 {
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);
185
186 switch (GetTileType(tile)) {
187 case MP_ROAD: {
188 CommandCost ret = EnsureNoVehicleOnGround(tile);
189 if (ret.Failed()) return ret;
190 break;
191 }
192
193 case MP_STATION: {
194 if (!IsDriveThroughStopTile(tile)) return CMD_ERROR;
195
196 CommandCost ret = EnsureNoVehicleOnGround(tile);
197 if (ret.Failed()) return ret;
198 break;
199 }
200
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;
205 break;
206 }
207
208 default:
209 return CMD_ERROR;
210 }
211
212 CommandCost ret = CheckAllowRemoveRoad(tile, pieces, GetRoadOwner(tile, rt), rt, flags, town_check);
213 if (ret.Failed()) return ret;
214
215 if (!IsTileType(tile, MP_ROAD)) {
216 /* If it's the last roadtype, just clear the whole tile */
217 if (rts == RoadTypeToRoadTypes(rt)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
218
219 CommandCost cost(EXPENSES_CONSTRUCTION);
220 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
221 /* Removing any roadbit in the bridge axis removes the roadtype (that's the behaviour remove-long-roads needs) */
222 if ((AxisToRoadBits(DiagDirToAxis(GetTunnelBridgeDirection(tile))) & pieces) == ROAD_NONE) return_cmd_error(rt == ROADTYPE_TRAM ? STR_ERROR_THERE_IS_NO_TRAMWAY : STR_ERROR_THERE_IS_NO_ROAD);
223
224 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
225 /* Pay for *every* tile of the bridge or tunnel */
226 uint len = GetTunnelBridgeLength(other_end, tile) + 2;
227 cost.AddCost(len * _price[PR_CLEAR_ROAD]);
228 if (flags & DC_EXEC) {
229 Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
230 if (c != NULL) {
231 /* A full diagonal road tile has two road bits. */
232 c->infrastructure.road[rt] -= len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
233 DirtyCompanyInfrastructureWindows(c->index);
234 }
235
236 SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt));
237 SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
238
239 /* If the owner of the bridge sells all its road, also move the ownership
240 * to the owner of the other roadtype. */
241 RoadType other_rt = (rt == ROADTYPE_ROAD) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
242 Owner other_owner = GetRoadOwner(tile, other_rt);
243 if (other_owner != GetTileOwner(tile)) {
244 SetTileOwner(tile, other_owner);
245 SetTileOwner(other_end, other_owner);
246 }
247
248 /* Mark tiles dirty that have been repaved */
249 if (IsBridge(tile)) {
250 MarkBridgeDirty(tile);
251 } else {
252 MarkTileDirtyByTile(tile);
253 MarkTileDirtyByTile(other_end);
254 }
255 }
256 } else {
257 assert(IsDriveThroughStopTile(tile));
258 cost.AddCost(_price[PR_CLEAR_ROAD] * 2);
259 if (flags & DC_EXEC) {
260 Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
261 if (c != NULL) {
262 /* A full diagonal road tile has two road bits. */
263 c->infrastructure.road[rt] -= 2;
264 DirtyCompanyInfrastructureWindows(c->index);
265 }
266 SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
267 MarkTileDirtyByTile(tile);
268 }
269 }
270 return cost;
271 }
272
273 switch (GetRoadTileType(tile)) {
274 case ROAD_TILE_NORMAL: {
275 Slope tileh = GetTileSlope(tile);
276
277 /* Steep slopes behave the same as slopes with one corner raised. */
278 if (IsSteepSlope(tileh)) {
279 tileh = SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh));
280 }
281
282 RoadBits present = GetRoadBits(tile, rt);
283 const RoadBits other = GetOtherRoadBits(tile, rt);
284 const Foundation f = GetRoadFoundation(tileh, present);
285
286 if (HasRoadWorks(tile) && _current_company != OWNER_WATER) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
287
288 /* Autocomplete to a straight road
289 * @li if the bits of the other roadtypes result in another foundation
290 * @li if build on slopes is disabled */
291 if ((IsStraightRoad(other) && (other & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) != ROAD_NONE) ||
292 (tileh != SLOPE_FLAT && !_settings_game.construction.build_on_slopes)) {
293 pieces |= MirrorRoadBits(pieces);
294 }
295
296 /* limit the bits to delete to the existing bits. */
297 pieces &= present;
298 if (pieces == ROAD_NONE) return_cmd_error(rt == ROADTYPE_TRAM ? STR_ERROR_THERE_IS_NO_TRAMWAY : STR_ERROR_THERE_IS_NO_ROAD);
299
300 /* Now set present what it will be after the remove */
301 present ^= pieces;
302
303 /* Check for invalid RoadBit combinations on slopes */
304 if (tileh != SLOPE_FLAT && present != ROAD_NONE &&
305 (present & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) == present) {
306 return CMD_ERROR;
307 }
308
309 if (flags & DC_EXEC) {
310 if (HasRoadWorks(tile)) {
311 /* flooding tile with road works, don't forget to remove the effect vehicle too */
312 assert(_current_company == OWNER_WATER);
313 EffectVehicle *v;
314 FOR_ALL_EFFECTVEHICLES(v) {
315 if (TileVirtXY(v->x_pos, v->y_pos) == tile) {
316 delete v;
317 }
318 }
319 }
320
321 Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
322 if (c != NULL) {
323 c->infrastructure.road[rt] -= CountBits(pieces);
324 DirtyCompanyInfrastructureWindows(c->index);
325 }
326
327 if (present == ROAD_NONE) {
328 RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
329 if (rts == ROADTYPES_NONE) {
330 /* Includes MarkTileDirtyByTile() */
331 DoClearSquare(tile);
332 } else {
333 if (rt == ROADTYPE_ROAD && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) {
334 /* Update nearest-town index */
335 const Town *town = CalcClosestTownFromTile(tile);
336 SetTownIndex(tile, town == NULL ? (TownID)INVALID_TOWN : town->index);
337 }
338 SetRoadBits(tile, ROAD_NONE, rt);
339 SetRoadTypes(tile, rts);
340 MarkTileDirtyByTile(tile);
341 }
342 } else {
343 /* When bits are removed, you *always* end up with something that
344 * is not a complete straight road tile. However, trams do not have
345 * onewayness, so they cannot remove it either. */
346 if (rt != ROADTYPE_TRAM) SetDisallowedRoadDirections(tile, DRD_NONE);
347 SetRoadBits(tile, present, rt);
348 MarkTileDirtyByTile(tile);
349 }
350 }
351
352 CommandCost cost(EXPENSES_CONSTRUCTION, CountBits(pieces) * _price[PR_CLEAR_ROAD]);
353 /* If we build a foundation we have to pay for it. */
354 if (f == FOUNDATION_NONE && GetRoadFoundation(tileh, present) != FOUNDATION_NONE) cost.AddCost(_price[PR_BUILD_FOUNDATION]);
355
356 return cost;
357 }
358
359 case ROAD_TILE_CROSSING: {
360 if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) {
361 return CMD_ERROR;
362 }
363
364 /* Don't allow road to be removed from the crossing when there is tram;
365 * we can't draw the crossing without roadbits ;) */
366 if (rt == ROADTYPE_ROAD && HasTileRoadType(tile, ROADTYPE_TRAM) && (flags & DC_EXEC || crossing_check)) return CMD_ERROR;
367
368 if (flags & DC_EXEC) {
369 Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
370 if (c != NULL) {
371 /* A full diagonal road tile has two road bits. */
372 c->infrastructure.road[rt] -= 2;
373 DirtyCompanyInfrastructureWindows(c->index);
374 }
375
376 Track railtrack = GetCrossingRailTrack(tile);
377 RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
378 if (rts == ROADTYPES_NONE) {
379 TrackBits tracks = GetCrossingRailBits(tile);
380 bool reserved = HasCrossingReservation(tile);
381 MakeRailNormal(tile, GetTileOwner(tile), tracks, GetRailType(tile));
382 if (reserved) SetTrackReservation(tile, tracks);
383
384 /* Update rail count for level crossings. The plain track should still be accounted
385 * for, so only subtract the difference to the level crossing cost. */
386 c = Company::GetIfValid(GetTileOwner(tile));
387 if (c != NULL) c->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR - 1;
388 } else {
389 SetRoadTypes(tile, rts);
390 }
391 MarkTileDirtyByTile(tile);
392 YapfNotifyTrackLayoutChange(tile, railtrack);
393 }
394 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_ROAD] * 2);
395 }
396
397 default:
398 case ROAD_TILE_DEPOT:
399 return CMD_ERROR;
400 }
401 }
402
403
415 static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
416 {
417 /* Remove already build pieces */
418 CLRBITS(*pieces, existing);
419
420 /* If we can't build anything stop here */
421 if (*pieces == ROAD_NONE) return CMD_ERROR;
422
423 /* All RoadBit combos are valid on flat land */
424 if (tileh == SLOPE_FLAT) return CommandCost();
425
426 /* Steep slopes behave the same as slopes with one corner raised. */
427 if (IsSteepSlope(tileh)) {
428 tileh = SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh));
429 }
430
431 /* Save the merge of all bits of the current type */
432 RoadBits type_bits = existing | *pieces;
433
434 /* Roads on slopes */
435 if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) {
436
437 /* If we add leveling we've got to pay for it */
438 if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
439
440 return CommandCost();
441 }
442
443 /* Autocomplete uphill roads */
444 *pieces |= MirrorRoadBits(*pieces);
445 type_bits = existing | *pieces;
446
447 /* Uphill roads */
448 if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) &&
449 (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) {
450
451 /* Slopes with foundation ? */
452 if (IsSlopeWithOneCornerRaised(tileh)) {
453
454 /* Prevent build on slopes if it isn't allowed */
455 if (_settings_game.construction.build_on_slopes) {
456
457 /* If we add foundation we've got to pay for it */
458 if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
459
460 return CommandCost();
461 }
462 } else {
463 if (HasExactlyOneBit(existing) && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
464 return CommandCost();
465 }
466 }
467 return CMD_ERROR;
468 }
469
481 CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
482 {
483 CompanyID company = _current_company;
484 CommandCost cost(EXPENSES_CONSTRUCTION);
485
486 RoadBits existing = ROAD_NONE;
487 RoadBits other_bits = ROAD_NONE;
488
489 /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
490 * if a non-company is building the road */
491 if ((Company::IsValidID(company) && p2 != 0) || (company == OWNER_TOWN && !Town::IsValidID(p2)) || (company == OWNER_DEITY && p2 != 0)) return CMD_ERROR;
492 if (company != OWNER_TOWN) {
493 const Town *town = CalcClosestTownFromTile(tile);
494 p2 = (town != NULL) ? town->index : (TownID)INVALID_TOWN;
495
496 if (company == OWNER_DEITY) {
497 company = OWNER_TOWN;
498
499 /* If we are not within a town, we are not owned by the town */
500 if (town == NULL || DistanceSquare(tile, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
501 company = OWNER_NONE;
502 }
503 }
504 }
505
506 RoadBits pieces = Extract<RoadBits, 0, 4>(p1);
507
508 /* do not allow building 'zero' road bits, code wouldn't handle it */
509 if (pieces == ROAD_NONE) return CMD_ERROR;
510
511 RoadType rt = Extract<RoadType, 4, 2>(p1);
512 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
513
514 DisallowedRoadDirections toggle_drd = Extract<DisallowedRoadDirections, 6, 2>(p1);
515
516 Slope tileh = GetTileSlope(tile);
517
518 bool need_to_clear = false;
519 switch (GetTileType(tile)) {
520 case MP_ROAD:
521 switch (GetRoadTileType(tile)) {
522 case ROAD_TILE_NORMAL: {
523 if (HasRoadWorks(tile)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
524
525 other_bits = GetOtherRoadBits(tile, rt);
526 if (!HasTileRoadType(tile, rt)) break;
527
528 existing = GetRoadBits(tile, rt);
529 bool crossing = !IsStraightRoad(existing | pieces);
530 if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
531 /* Junctions cannot be one-way */
532 return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
533 }
534 if ((existing & pieces) == pieces) {
535 /* We only want to set the (dis)allowed road directions */
536 if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM) {
537 if (crossing) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
538
539 Owner owner = GetRoadOwner(tile, ROADTYPE_ROAD);
540 if (owner != OWNER_NONE) {
541 CommandCost ret = CheckOwnership(owner, tile);
542 if (ret.Failed()) return ret;
543 }
544
545 DisallowedRoadDirections dis_existing = GetDisallowedRoadDirections(tile);
546 DisallowedRoadDirections dis_new = dis_existing ^ toggle_drd;
547
548 /* We allow removing disallowed directions to break up
549 * deadlocks, but adding them can break articulated
550 * vehicles. As such, only when less is disallowed,
551 * i.e. bits are removed, we skip the vehicle check. */
552 if (CountBits(dis_existing) <= CountBits(dis_new)) {
553 CommandCost ret = EnsureNoVehicleOnGround(tile);
554 if (ret.Failed()) return ret;
555 }
556
557 /* Ignore half built tiles */
558 if ((flags & DC_EXEC) && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
559 SetDisallowedRoadDirections(tile, dis_new);
560 MarkTileDirtyByTile(tile);
561 }
562 return CommandCost();
563 }
564 return_cmd_error(STR_ERROR_ALREADY_BUILT);
565 }
566 break;
567 }
568
569 case ROAD_TILE_CROSSING:
570 other_bits = GetCrossingRoadBits(tile);
571 if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
572 pieces = other_bits; // we need to pay for both roadbits
573
574 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
575 break;
576
577 case ROAD_TILE_DEPOT:
578 if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
579 goto do_clear;
580
581 default: NOT_REACHED();
582 }
583 break;
584
585 case MP_RAILWAY: {
586 if (IsSteepSlope(tileh)) {
587 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
588 }
589
590 /* Level crossings may only be built on these slopes */
591 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
592 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
593 }
594
595 if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
596
597 if (RailNoLevelCrossings(GetRailType(tile))) {
598 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED);
599 }
600
601 Axis roaddir;
602 switch (GetTrackBits(tile)) {
603 case TRACK_BIT_X:
604 if (pieces & ROAD_X) goto do_clear;
605 roaddir = AXIS_Y;
606 break;
607
608 case TRACK_BIT_Y:
609 if (pieces & ROAD_Y) goto do_clear;
610 roaddir = AXIS_X;
611 break;
612
613 default: goto do_clear;
614 }
615
616 CommandCost ret = EnsureNoVehicleOnGround(tile);
617 if (ret.Failed()) return ret;
618
619 if (flags & DC_EXEC) {
620 Track railtrack = AxisToTrack(OtherAxis(roaddir));
621 YapfNotifyTrackLayoutChange(tile, railtrack);
622 /* Update company infrastructure counts. A level crossing has two road bits. */
623 Company *c = Company::GetIfValid(company);
624 if (c != NULL) {
625 c->infrastructure.road[rt] += 2;
626 if (rt != ROADTYPE_ROAD) c->infrastructure.road[ROADTYPE_ROAD] += 2;
627 DirtyCompanyInfrastructureWindows(company);
628 }
629 /* Update rail count for level crossings. The plain track is already
630 * counted, so only add the difference to the level crossing cost. */
631 c = Company::GetIfValid(GetTileOwner(tile));
632 if (c != NULL) c->infrastructure.rail[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR - 1;
633
634 /* Always add road to the roadtypes (can't draw without it) */
635 bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack);
636 MakeRoadCrossing(tile, company, company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
637 SetCrossingReservation(tile, reserved);
638 UpdateLevelCrossing(tile, false);
639 MarkTileDirtyByTile(tile);
640 }
641 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_ROAD] * (rt == ROADTYPE_ROAD ? 2 : 4));
642 }
643
644 case MP_STATION: {
645 if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
646 if (!IsDriveThroughStopTile(tile)) goto do_clear;
647
648 RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
649 if (pieces & ~curbits) goto do_clear;
650 pieces = curbits; // we need to pay for both roadbits
651
652 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
653 break;
654 }
655
656 case MP_TUNNELBRIDGE: {
657 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) goto do_clear;
658 /* Only allow building the outern roadbit, so building long roads stops at existing bridges */
659 if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) goto do_clear;
660 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
661 /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
662 CommandCost ret = TunnelBridgeIsFree(tile, GetOtherTunnelBridgeEnd(tile));
663 if (ret.Failed()) return ret;
664 break;
665 }
666
667 default: {
668 do_clear:;
669 need_to_clear = true;
670 break;
671 }
672 }
673
674 if (need_to_clear) {
675 CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
676 if (ret.Failed()) return ret;
677 cost.AddCost(ret);
678 }
679
680 if (other_bits != pieces) {
681 /* Check the foundation/slopes when adding road/tram bits */
682 CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
683 /* Return an error if we need to build a foundation (ret != 0) but the
684 * current setting is turned off */
685 if (ret.Failed() || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
686 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
687 }
688 cost.AddCost(ret);
689 }
690
691 if (!need_to_clear) {
692 if (IsTileType(tile, MP_ROAD)) {
693 /* Don't put the pieces that already exist */
694 pieces &= ComplementRoadBits(existing);
695
696 /* Check if new road bits will have the same foundation as other existing road types */
697 if (IsNormalRoad(tile)) {
698 Slope slope = GetTileSlope(tile);
699 Foundation found_new = GetRoadFoundation(slope, pieces | existing);
700
701 /* Test if all other roadtypes can be built at that foundation */
702 for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
703 if (rtest != rt) { // check only other road types
704 RoadBits bits = GetRoadBits(tile, rtest);
705 /* do not check if there are not road bits of given type */
706 if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
707 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
708 }
709 }
710 }
711 }
712 }
713
714 CommandCost ret = EnsureNoVehicleOnGround(tile);
715 if (ret.Failed()) return ret;
716
717 }
718
719 uint num_pieces = (!need_to_clear && IsTileType(tile, MP_TUNNELBRIDGE)) ?
720 /* There are 2 pieces on *every* tile of the bridge or tunnel */
721 2 * (GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2) :
722 /* Count pieces */
723 CountBits(pieces);
724
725 cost.AddCost(num_pieces * _price[PR_BUILD_ROAD]);
726
727 if (flags & DC_EXEC) {
728 switch (GetTileType(tile)) {
729 case MP_ROAD: {
730 RoadTileType rtt = GetRoadTileType(tile);
731 if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
732 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
733 SetRoadOwner(tile, rt, company);
734 if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
735 }
736 if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
737 break;
738 }
739
740 case MP_TUNNELBRIDGE: {
741 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
742
743 SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
744 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
745 SetRoadOwner(other_end, rt, company);
746 SetRoadOwner(tile, rt, company);
747
748 /* Mark tiles dirty that have been repaved */
749 if (IsBridge(tile)) {
750 MarkBridgeDirty(tile);
751 } else {
752 MarkTileDirtyByTile(other_end);
753 MarkTileDirtyByTile(tile);
754 }
755 break;
756 }
757
758 case MP_STATION:
759 assert(IsDriveThroughStopTile(tile));
760 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
761 SetRoadOwner(tile, rt, company);
762 break;
763
764 default:
765 MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, company, company);
766 break;
767 }
768
769 /* Update company infrastructure count. */
770 Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
771 if (c != NULL) {
772 if (IsTileType(tile, MP_TUNNELBRIDGE)) num_pieces *= TUNNELBRIDGE_TRACKBIT_FACTOR;
773 c->infrastructure.road[rt] += num_pieces;
774 DirtyCompanyInfrastructureWindows(c->index);
775 }
776
777 if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
778 existing |= pieces;
779 SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
780 GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
781 }
782
783 MarkTileDirtyByTile(tile);
784 }
785 return cost;
786 }
787
795 static bool CanConnectToRoad(TileIndex tile, RoadType rt, DiagDirection dir)
796 {
797 RoadBits bits = GetAnyRoadBits(tile + TileOffsByDiagDir(dir), rt, false);
798 return (bits & DiagDirToRoadBits(ReverseDiagDir(dir))) != 0;
799 }
800
818 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
819 {
820 DisallowedRoadDirections drd = DRD_NORTHBOUND;
821
822 if (p1 >= MapSize()) return CMD_ERROR;
823
824 TileIndex end_tile = p1;
825 RoadType rt = Extract<RoadType, 3, 2>(p2);
826 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
827
828 Axis axis = Extract<Axis, 2, 1>(p2);
829 /* Only drag in X or Y direction dictated by the direction variable */
830 if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
831 if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
832
833 DiagDirection dir = AxisToDiagDir(axis);
834
835 /* Swap direction, also the half-tile drag var (bit 0 and 1) */
836 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
837 dir = ReverseDiagDir(dir);
838 p2 ^= 3;
839 drd = DRD_SOUTHBOUND;
840 }
841
842 /* On the X-axis, we have to swap the initial bits, so they
843 * will be interpreted correctly in the GTTS. Furthermore
844 * when you just 'click' on one tile to build them. */
845 if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
846 /* No disallowed direction bits have to be toggled */
847 if (!HasBit(p2, 5)) drd = DRD_NONE;
848
849 CommandCost cost(EXPENSES_CONSTRUCTION);
850 CommandCost last_error = CMD_ERROR;
851 TileIndex tile = start_tile;
852 bool had_bridge = false;
853 bool had_tunnel = false;
854 bool had_success = false;
855 bool is_ai = HasBit(p2, 6);
856
857 /* Start tile is the first tile clicked by the user. */
858 for (;;) {
859 RoadBits bits = AxisToRoadBits(axis);
860
861 /* Determine which road parts should be built. */
862 if (!is_ai && start_tile != end_tile) {
863 /* Only build the first and last roadbit if they can connect to something. */
864 if (tile == end_tile && !CanConnectToRoad(tile, rt, dir)) {
865 bits = DiagDirToRoadBits(ReverseDiagDir(dir));
866 } else if (tile == start_tile && !CanConnectToRoad(tile, rt, ReverseDiagDir(dir))) {
867 bits = DiagDirToRoadBits(dir);
868 }
869 } else {
870 /* Road parts only have to be built at the start tile or at the end tile. */
871 if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
872 if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
873 }
874
875 CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
876 if (ret.Failed()) {
877 last_error = ret;
878 if (last_error.GetErrorMessage() != STR_ERROR_ALREADY_BUILT) {
879 if (is_ai) return last_error;
880 break;
881 }
882 } else {
883 had_success = true;
884 /* Only pay for the upgrade on one side of the bridges and tunnels */
885 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
886 if (IsBridge(tile)) {
887 if (!had_bridge || GetTunnelBridgeDirection(tile) == dir) {
888 cost.AddCost(ret);
889 }
890 had_bridge = true;
891 } else { // IsTunnel(tile)
892 if (!had_tunnel || GetTunnelBridgeDirection(tile) == dir) {
893 cost.AddCost(ret);
894 }
895 had_tunnel = true;
896 }
897 } else {
898 cost.AddCost(ret);
899 }
900 }
901
902 if (tile == end_tile) break;
903
904 tile += TileOffsByDiagDir(dir);
905 }
906
907 return had_success ? cost : last_error;
908 }
909
923 CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
924 {
925 CommandCost cost(EXPENSES_CONSTRUCTION);
926
927 if (p1 >= MapSize()) return CMD_ERROR;
928
929 TileIndex end_tile = p1;
930 RoadType rt = Extract<RoadType, 3, 2>(p2);
931 if (!IsValidRoadType(rt)) return CMD_ERROR;
932
933 Axis axis = Extract<Axis, 2, 1>(p2);
934 /* Only drag in X or Y direction dictated by the direction variable */
935 if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
936 if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
937
938 /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
939 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
940 TileIndex t = start_tile;
941 start_tile = end_tile;
942 end_tile = t;
943 p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
944 }
945
946 Money money = GetAvailableMoneyForCommand();
947 TileIndex tile = start_tile;
948 CommandCost last_error = CMD_ERROR;
949 bool had_success = false;
950 /* Start tile is the small number. */
951 for (;;) {
952 RoadBits bits = AxisToRoadBits(axis);
953
954 if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
955 if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
956
957 /* try to remove the halves. */
958 if (bits != 0) {
959 CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
960 if (ret.Succeeded()) {
961 if (flags & DC_EXEC) {
962 money -= ret.GetCost();
963 if (money < 0) {
964 _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
965 return cost;
966 }
967 RemoveRoad(tile, flags, bits, rt, true, false);
968 }
969 cost.AddCost(ret);
970 had_success = true;
971 } else {
972 /* Ownership errors are more important. */
973 if (last_error.GetErrorMessage() != STR_ERROR_OWNED_BY) last_error = ret;
974 }
975 }
976
977 if (tile == end_tile) break;
978
979 tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
980 }
981
982 return had_success ? cost : last_error;
983 }
984
998 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
999 {
1000 DiagDirection dir = Extract<DiagDirection, 0, 2>(p1);
1001 RoadType rt = Extract<RoadType, 2, 2>(p1);
1002
1003 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
1004
1005 Slope tileh = GetTileSlope(tile);
1006 if (tileh != SLOPE_FLAT && (
1007 !_settings_game.construction.build_on_slopes ||
1008 !CanBuildDepotByTileh(dir, tileh)
1009 )) {
1010 return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
1011 }
1012
1013 CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
1014 if (cost.Failed()) return cost;
1015
1016 if (IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
1017
1018 if (!Depot::CanAllocateItem()) return CMD_ERROR;
1019
1020 if (flags & DC_EXEC) {
1021 Depot *dep = new Depot(tile);
1022 dep->build_date = _date;
1023
1024 /* A road depot has two road bits. */
1025 Company::Get(_current_company)->infrastructure.road[rt] += 2;
1026 DirtyCompanyInfrastructureWindows(_current_company);
1027
1028 MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
1029 MarkTileDirtyByTile(tile);
1030 MakeDefaultName(dep);
1031 }
1032 cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
1033 return cost;
1034 }
1035
1036 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
1037 {
1038 if (_current_company != OWNER_WATER) {
1039 CommandCost ret = CheckTileOwnership(tile);
1040 if (ret.Failed()) return ret;
1041 }
1042
1043 CommandCost ret = EnsureNoVehicleOnGround(tile);
1044 if (ret.Failed()) return ret;
1045
1046 if (flags & DC_EXEC) {
1047 Company *c = Company::GetIfValid(GetTileOwner(tile));
1048 if (c != NULL) {
1049 /* A road depot has two road bits. */
1050 c->infrastructure.road[FIND_FIRST_BIT(GetRoadTypes(tile))] -= 2;
1051 DirtyCompanyInfrastructureWindows(c->index);
1052 }
1053
1054 delete Depot::GetByTile(tile);
1055 DoClearSquare(tile);
1056 }
1057
1058 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
1059 }
1060
1061 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
1062 {
1063 switch (GetRoadTileType(tile)) {
1064 case ROAD_TILE_NORMAL: {
1065 RoadBits b = GetAllRoadBits(tile);
1066
1067 /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
1068 if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
1069 CommandCost ret(EXPENSES_CONSTRUCTION);
1070 RoadType rt;
1071 FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
1072 CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
1073 if (tmp_ret.Failed()) return tmp_ret;
1074 ret.AddCost(tmp_ret);
1075 }
1076 return ret;
1077 }
1078 return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
1079 }
1080
1081 case ROAD_TILE_CROSSING: {
1082 RoadTypes rts = GetRoadTypes(tile);
1083 CommandCost ret(EXPENSES_CONSTRUCTION);
1084
1085 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
1086
1087 /* Must iterate over the roadtypes in a reverse manner because
1088 * tram tracks must be removed before the road bits. */
1089 RoadType rt = ROADTYPE_TRAM;
1090 do {
1091 if (HasBit(rts, rt)) {
1092 CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
1093 if (tmp_ret.Failed()) return tmp_ret;
1094 ret.AddCost(tmp_ret);
1095 }
1096 } while (rt-- != ROADTYPE_ROAD);
1097
1098 if (flags & DC_EXEC) {
1099 DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
1100 }
1101 return ret;
1102 }
1103
1104 default:
1105 case ROAD_TILE_DEPOT:
1106 if (flags & DC_AUTO) {
1107 return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
1108 }
1109 return RemoveRoadDepot(tile, flags);
1110 }
1111 }
1112
1113
1114 struct DrawRoadTileStruct {
1115 uint16 image;
1116 byte subcoord_x;
1117 byte subcoord_y;
1118 };
1119
1120 #include "table/road_land.h"
1121
1129 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
1130 {
1131 /* Flat land and land without a road doesn't require a foundation */
1132 if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
1133
1134 /* Steep slopes behave the same as slopes with one corner raised. */
1135 if (IsSteepSlope(tileh)) {
1136 tileh = SlopeWithOneCornerRaised(GetHighestSlopeCorner(tileh));
1137 }
1138
1139 /* Leveled RoadBits on a slope */
1140 if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
1141
1142 /* Straight roads without foundation on a slope */
1143 if (!IsSlopeWithOneCornerRaised(tileh) &&
1144 (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
1145 return FOUNDATION_NONE;
1146
1147 /* Roads on steep Slopes or on Slopes with one corner raised */
1148 return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
1149 }
1150
1151 const byte _road_sloped_sprites[14] = {
1152 0, 0, 2, 0,
1153 0, 1, 0, 0,
1154 3, 0, 0, 0,
1155 0, 0
1156 };
1157
1168 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
1169 {
1170 return (IsOnSnow(tile) &&
1171 !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
1172 roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
1173 }
1174
1180 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
1181 {
1182 /* Do not draw catenary if it is invisible */
1183 if (IsInvisibilitySet(TO_CATENARY)) return;
1184
1185 /* Don't draw the catenary under a low bridge */
1186 if (IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
1187 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
1188
1189 if (height <= GetTileMaxZ(ti->tile) + 1) return;
1190 }
1191
1192 SpriteID front;
1193 SpriteID back;
1194
1195 if (ti->tileh != SLOPE_FLAT) {
1196 back = SPR_TRAMWAY_BACK_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
1197 front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
1198 } else {
1199 back = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
1200 front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
1201 }
1202
1203 AddSortableSpriteToDraw(back, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
1204 AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
1205 }
1206
1215 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
1216 {
1217 int x = ti->x | dx;
1218 int y = ti->y | dy;
1219 int z = ti->z;
1220 if (ti->tileh != SLOPE_FLAT) z = GetSlopePixelZ(x, y);
1221 AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
1222 }
1223
1228 static void DrawRoadBits(TileInfo *ti)
1229 {
1230 RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
1231 RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
1232
1233 SpriteID image = 0;
1234 PaletteID pal = PAL_NONE;
1235
1236 if (ti->tileh != SLOPE_FLAT) {
1237 DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
1238
1239 /* DrawFoundation() modifies ti.
1240 * Default sloped sprites.. */
1241 if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + SPR_ROAD_SLOPE_START;
1242 }
1243
1244 if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
1245
1246 Roadside roadside = GetRoadside(ti->tile);
1247
1248 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
1249 image += 19;
1250 } else {
1251 switch (roadside) {
1252 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
1253 case ROADSIDE_GRASS: break;
1254 case ROADSIDE_GRASS_ROAD_WORKS: break;
1255 default: image -= 19; break; // Paved
1256 }
1257 }
1258
1259 DrawGroundSprite(image, pal);
1260
1261 /* For tram we overlay the road graphics with either tram tracks only
1262 * (when there is actual road beneath the trams) or with tram tracks
1263 * and some dirts which hides the road graphics */
1264 if (tram != ROAD_NONE) {
1265 if (ti->tileh != SLOPE_FLAT) {
1266 image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
1267 } else {
1268 image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
1269 }
1270 image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
1271 DrawGroundSprite(image, pal);
1272 }
1273
1274 if (road != ROAD_NONE) {
1275 DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
1276 if (drd != DRD_NONE) {
1277 DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialPixelZ(8, 8, ti->tileh));
1278 }
1279 }
1280
1281 if (HasRoadWorks(ti->tile)) {
1282 /* Road works */
1283 DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
1284 return;
1285 }
1286
1287 if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
1288
1289 /* Return if full detail is disabled, or we are zoomed fully out. */
1290 if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
1291
1292 /* Do not draw details (street lights, trees) under low bridge */
1293 if (IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
1294 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
1295 int minz = GetTileMaxZ(ti->tile) + 2;
1296
1297 if (roadside == ROADSIDE_TREES) minz++;
1298
1299 if (height < minz) return;
1300 }
1301
1302 /* If there are no road bits, return, as there is nothing left to do */
1303 if (HasAtMostOneBit(road)) return;
1304
1305 /* Draw extra details. */
1306 for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
1307 DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
1308 }
1309 }
1310
1312 static void DrawTile_Road(TileInfo *ti)
1313 {
1314 switch (GetRoadTileType(ti->tile)) {
1315 case ROAD_TILE_NORMAL:
1316 DrawRoadBits(ti);
1317 break;
1318
1319 case ROAD_TILE_CROSSING: {
1320 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
1321
1322 PaletteID pal = PAL_NONE;
1323 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1324
1325 if (rti->UsesOverlay()) {
1326 Axis axis = GetCrossingRailAxis(ti->tile);
1327 SpriteID road = SPR_ROAD_Y + axis;
1328
1329 Roadside roadside = GetRoadside(ti->tile);
1330
1331 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
1332 road += 19;
1333 } else {
1334 switch (roadside) {
1335 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
1336 case ROADSIDE_GRASS: break;
1337 default: road -= 19; break; // Paved
1338 }
1339 }
1340
1341 DrawGroundSprite(road, pal);
1342
1343 SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
1344 /* Draw tracks, but draw PBS reserved tracks darker. */
1345 pal = (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) ? PALETTE_CRASH : PAL_NONE;
1346 DrawGroundSprite(rail, pal);
1347
1348 DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
1349 } else {
1350 SpriteID image = rti->base_sprites.crossing;
1351
1352 if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
1353 if (IsCrossingBarred(ti->tile)) image += 2;
1354
1355 Roadside roadside = GetRoadside(ti->tile);
1356
1357 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
1358 image += 8;
1359 } else {
1360 switch (roadside) {
1361 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
1362 case ROADSIDE_GRASS: break;
1363 default: image += 4; break; // Paved
1364 }
1365 }
1366
1367 DrawGroundSprite(image, pal);
1368
1369 /* PBS debugging, draw reserved tracks darker */
1370 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
1371 DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y, PALETTE_CRASH);
1372 }
1373 }
1374
1375 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
1376 DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
1377 DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
1378 }
1379 if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
1380 break;
1381 }
1382
1383 default:
1384 case ROAD_TILE_DEPOT: {
1385 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
1386
1387 PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
1388
1389 const DrawTileSprites *dts;
1390 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
1391 dts = &_tram_depot[GetRoadDepotDirection(ti->tile)];
1392 } else {
1393 dts = &_road_depot[GetRoadDepotDirection(ti->tile)];
1394 }
1395
1396 DrawGroundSprite(dts->ground.sprite, PAL_NONE);
1397 DrawOrigTileSeq(ti, dts, TO_BUILDINGS, palette);
1398 break;
1399 }
1400 }
1401 DrawBridgeMiddle(ti);
1402 }
1403
1411 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
1412 {
1413 PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
1414 const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
1415
1416 DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
1417 DrawOrigTileSeqInGUI(x, y, dts, palette);
1418 }
1419
1425 void UpdateNearestTownForRoadTiles(bool invalidate)
1426 {
1427 assert(!invalidate || _generating_world);
1428
1429 for (TileIndex t = 0; t < MapSize(); t++) {
1430 if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) {
1431 TownID tid = (TownID)INVALID_TOWN;
1432 if (!invalidate) {
1433 const Town *town = CalcClosestTownFromTile(t);
1434 if (town != NULL) tid = town->index;
1435 }
1436 SetTownIndex(t, tid);
1437 }
1438 }
1439 }
1440
1441 static int GetSlopePixelZ_Road(TileIndex tile, uint x, uint y)
1442 {
1443
1444 if (IsNormalRoad(tile)) {
1445 int z;
1446 Slope tileh = GetTilePixelSlope(tile, &z);
1447 if (tileh == SLOPE_FLAT) return z;
1448
1449 Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
1450 z += ApplyPixelFoundationToSlope(f, &tileh);
1451 return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
1452 } else {
1453 return GetTileMaxPixelZ(tile);
1454 }
1455 }
1456
1457 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
1458 {
1459 if (IsNormalRoad(tile)) {
1460 return GetRoadFoundation(tileh, GetAllRoadBits(tile));
1461 } else {
1462 return FlatteningFoundation(tileh);
1463 }
1464 }
1465
1466 static const Roadside _town_road_types[][2] = {
1467 { ROADSIDE_GRASS, ROADSIDE_GRASS },
1468 { ROADSIDE_PAVED, ROADSIDE_PAVED },
1469 { ROADSIDE_PAVED, ROADSIDE_PAVED },
1470 { ROADSIDE_TREES, ROADSIDE_TREES },
1471 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
1472 };
1473
1474 static const Roadside _town_road_types_2[][2] = {
1475 { ROADSIDE_GRASS, ROADSIDE_GRASS },
1476 { ROADSIDE_PAVED, ROADSIDE_PAVED },
1477 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
1478 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
1479 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
1480 };
1481
1482
1483 static void TileLoop_Road(TileIndex tile)
1484 {
1485 switch (_settings_game.game_creation.landscape) {
1486 case LT_ARCTIC:
1487 if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
1488 ToggleSnow(tile);
1489 MarkTileDirtyByTile(tile);
1490 }
1491 break;
1492
1493 case LT_TROPIC:
1494 if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
1495 ToggleDesert(tile);
1496 MarkTileDirtyByTile(tile);
1497 }
1498 break;
1499 }
1500
1501 if (IsRoadDepot(tile)) return;
1502
1503 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
1504 if (!HasRoadWorks(tile)) {
1505 HouseZonesBits grp = HZB_TOWN_EDGE;
1506
1507 if (t != NULL) {
1508 grp = GetTownRadiusGroup(t, tile);
1509
1510 /* Show an animation to indicate road work */
1511 if (t->road_build_months != 0 &&
1512 (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
1513 IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
1514 if (GetFoundationSlope(tile) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile).Succeeded() && Chance16(1, 40)) {
1515 StartRoadWorks(tile);
1516
1517 if (_settings_client.sound.ambient) SndPlayTileFx(SND_21_JACKHAMMER, tile);
1518 CreateEffectVehicleAbove(
1519 TileX(tile) * TILE_SIZE + 7,
1520 TileY(tile) * TILE_SIZE + 7,
1521 0,
1522 EV_BULLDOZER);
1523 MarkTileDirtyByTile(tile);
1524 return;
1525 }
1526 }
1527 }
1528
1529 {
1530 /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
1531 const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
1532 Roadside cur_rs = GetRoadside(tile);
1533
1534 /* We have our desired type, do nothing */
1535 if (cur_rs == new_rs[0]) return;
1536
1537 /* We have the pre-type of the desired type, switch to the desired type */
1538 if (cur_rs == new_rs[1]) {
1539 cur_rs = new_rs[0];
1540 /* We have barren land, install the pre-type */
1541 } else if (cur_rs == ROADSIDE_BARREN) {
1542 cur_rs = new_rs[1];
1543 /* We're totally off limits, remove any installation and make barren land */
1544 } else {
1545 cur_rs = ROADSIDE_BARREN;
1546 }
1547 SetRoadside(tile, cur_rs);
1548 MarkTileDirtyByTile(tile);
1549 }
1550 } else if (IncreaseRoadWorksCounter(tile)) {
1551 TerminateRoadWorks(tile);
1552
1553 if (_settings_game.economy.mod_road_rebuild) {
1554 /* Generate a nicer town surface */
1555 const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
1556 const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
1557
1558 if (old_rb != new_rb) {
1559 RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
1560 }
1561 }
1562
1563 MarkTileDirtyByTile(tile);
1564 }
1565 }
1566
1567 static bool ClickTile_Road(TileIndex tile)
1568 {
1569 if (!IsRoadDepot(tile)) return false;
1570
1571 ShowDepotWindow(tile, VEH_ROAD);
1572 return true;
1573 }
1574
1575 /* Converts RoadBits to TrackBits */
1576 static const TrackBits _road_trackbits[16] = {
1577 TRACK_BIT_NONE, // ROAD_NONE
1578 TRACK_BIT_NONE, // ROAD_NW
1579 TRACK_BIT_NONE, // ROAD_SW
1580 TRACK_BIT_LEFT, // ROAD_W
1581 TRACK_BIT_NONE, // ROAD_SE
1582 TRACK_BIT_Y, // ROAD_Y
1583 TRACK_BIT_LOWER, // ROAD_S
1584 TRACK_BIT_LEFT | TRACK_BIT_LOWER | TRACK_BIT_Y, // ROAD_Y | ROAD_SW
1585 TRACK_BIT_NONE, // ROAD_NE
1586 TRACK_BIT_UPPER, // ROAD_N
1587 TRACK_BIT_X, // ROAD_X
1588 TRACK_BIT_LEFT | TRACK_BIT_UPPER | TRACK_BIT_X, // ROAD_X | ROAD_NW
1589 TRACK_BIT_RIGHT, // ROAD_E
1590 TRACK_BIT_RIGHT | TRACK_BIT_UPPER | TRACK_BIT_Y, // ROAD_Y | ROAD_NE
1591 TRACK_BIT_RIGHT | TRACK_BIT_LOWER | TRACK_BIT_X, // ROAD_X | ROAD_SE
1592 TRACK_BIT_ALL, // ROAD_ALL
1593 };
1594
1595 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
1596 {
1597 TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
1598 TrackdirBits red_signals = TRACKDIR_BIT_NONE; // crossing barred
1599 switch (mode) {
1600 case TRANSPORT_RAIL:
1601 if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
1602 break;
1603
1604 case TRANSPORT_ROAD:
1605 if ((GetRoadTypes(tile) & sub_mode) == 0) break;
1606 switch (GetRoadTileType(tile)) {
1607 case ROAD_TILE_NORMAL: {
1608 const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
1609 RoadType rt = (RoadType)FindFirstBit(sub_mode);
1610 RoadBits bits = GetRoadBits(tile, rt);
1611
1612 /* no roadbit at this side of tile, return 0 */
1613 if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
1614
1615 uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
1616 if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
1617 break;
1618 }
1619
1620 case ROAD_TILE_CROSSING: {
1621 Axis axis = GetCrossingRoadAxis(tile);
1622
1623 if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
1624
1625 trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
1626 if (IsCrossingBarred(tile)) red_signals = trackdirbits;
1627 break;
1628 }
1629
1630 default:
1631 case ROAD_TILE_DEPOT: {
1632 DiagDirection dir = GetRoadDepotDirection(tile);
1633
1634 if (side != INVALID_DIAGDIR && side != dir) break;
1635
1636 trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
1637 break;
1638 }
1639 }
1640 break;
1641
1642 default: break;
1643 }
1644 return CombineTrackStatus(trackdirbits, red_signals);
1645 }
1646
1647 static const StringID _road_tile_strings[] = {
1648 STR_LAI_ROAD_DESCRIPTION_ROAD,
1649 STR_LAI_ROAD_DESCRIPTION_ROAD,
1650 STR_LAI_ROAD_DESCRIPTION_ROAD,
1651 STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS,
1652 STR_LAI_ROAD_DESCRIPTION_ROAD,
1653 STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD,
1654 STR_LAI_ROAD_DESCRIPTION_ROAD,
1655 STR_LAI_ROAD_DESCRIPTION_ROAD,
1656 };
1657
1658 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
1659 {
1660 Owner rail_owner = INVALID_OWNER;
1661 Owner road_owner = INVALID_OWNER;
1662 Owner tram_owner = INVALID_OWNER;
1663
1664 switch (GetRoadTileType(tile)) {
1665 case ROAD_TILE_CROSSING: {
1666 td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING;
1667 RoadTypes rts = GetRoadTypes(tile);
1668 rail_owner = GetTileOwner(tile);
1669 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
1670 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
1671
1672 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
1673 td->rail_speed = rti->max_speed;
1674
1675 break;
1676 }
1677
1678 case ROAD_TILE_DEPOT:
1679 td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
1680 road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter
1681 td->build_date = Depot::GetByTile(tile)->build_date;
1682 break;
1683
1684 default: {
1685 RoadTypes rts = GetRoadTypes(tile);
1686 td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
1687 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
1688 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
1689 break;
1690 }
1691 }
1692
1693 /* Now we have to discover, if the tile has only one owner or many:
1694 * - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
1695 * - Compare the found owner with the other owners, and test if they differ.
1696 * Note: If road exists it will be the first_owner.
1697 */
1698 Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
1699 bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
1700
1701 if (mixed_owners) {
1702 /* Multiple owners */
1703 td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
1704 td->owner[0] = rail_owner;
1705 td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
1706 td->owner[1] = road_owner;
1707 td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
1708 td->owner[2] = tram_owner;
1709 } else {
1710 /* One to rule them all */
1711 td->owner[0] = first_owner;
1712 }
1713 }
1714
1719 static const byte _roadveh_enter_depot_dir[4] = {
1720 TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
1721 };
1722
1723 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
1724 {
1725 switch (GetRoadTileType(tile)) {
1726 case ROAD_TILE_DEPOT: {
1727 if (v->type != VEH_ROAD) break;
1728
1729 RoadVehicle *rv = RoadVehicle::From(v);
1730 if (rv->frame == RVC_DEPOT_STOP_FRAME &&
1731 _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
1732 rv->state = RVSB_IN_DEPOT;
1733 rv->vehstatus |= VS_HIDDEN;
1734 rv->direction = ReverseDir(rv->direction);
1735 if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
1736 rv->tile = tile;
1737
1738 InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
1739 return VETSB_ENTERED_WORMHOLE;
1740 }
1741 break;
1742 }
1743
1744 default: break;
1745 }
1746 return VETSB_CONTINUE;
1747 }
1748
1749
1750 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
1751 {
1752 if (IsRoadDepot(tile)) {
1753 if (GetTileOwner(tile) == old_owner) {
1754 if (new_owner == INVALID_OWNER) {
1755 DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
1756 } else {
1757 /* A road depot has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
1758 RoadType rt = (RoadType)FIND_FIRST_BIT(GetRoadTypes(tile));
1759 Company::Get(old_owner)->infrastructure.road[rt] -= 2;
1760 Company::Get(new_owner)->infrastructure.road[rt] += 2;
1761
1762 SetTileOwner(tile, new_owner);
1763 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
1764 if (GetRoadOwner(tile, rt) == old_owner) {
1765 SetRoadOwner(tile, rt, new_owner);
1766 }
1767 }
1768 }
1769 }
1770 return;
1771 }
1772
1773 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
1774 /* Update all roadtypes, no matter if they are present */
1775 if (GetRoadOwner(tile, rt) == old_owner) {
1776 if (HasTileRoadType(tile, rt)) {
1777 /* A level crossing has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
1778 uint num_bits = IsLevelCrossing(tile) ? 2 : CountBits(GetRoadBits(tile, rt));
1779 Company::Get(old_owner)->infrastructure.road[rt] -= num_bits;
1780 if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.road[rt] += num_bits;
1781 }
1782
1783 SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
1784 }
1785 }
1786
1787 if (IsLevelCrossing(tile)) {
1788 if (GetTileOwner(tile) == old_owner) {
1789 if (new_owner == INVALID_OWNER) {
1790 DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
1791 } else {
1792 /* Update infrastructure counts. No need to dirty windows here, we'll redraw the whole screen anyway. */
1793 Company::Get(old_owner)->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR;
1794 Company::Get(new_owner)->infrastructure.rail[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR;
1795
1796 SetTileOwner(tile, new_owner);
1797 }
1798 }
1799 }
1800 }
1801
1802 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
1803 {
1804 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
1805 switch (GetRoadTileType(tile)) {
1806 case ROAD_TILE_CROSSING:
1807 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]);
1808 break;
1809
1810 case ROAD_TILE_DEPOT:
1811 if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
1812 break;
1813
1814 case ROAD_TILE_NORMAL: {
1815 RoadBits bits = GetAllRoadBits(tile);
1816 RoadBits bits_copy = bits;
1817 /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
1818 if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
1819 /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
1820 if (bits == bits_copy) {
1821 int z_old;
1822 Slope tileh_old = GetTileSlope(tile, &z_old);
1823
1824 /* Get the slope on top of the foundation */
1825 z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
1826 z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
1827
1828 /* The surface slope must not be changed */
1829 if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
1830 }
1831 }
1832 break;
1833 }
1834
1835 default: NOT_REACHED();
1836 }
1837 }
1838
1839 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
1840 }
1841
1843 extern const TileTypeProcs _tile_type_road_procs = {
1844 DrawTile_Road, // draw_tile_proc
1845 GetSlopePixelZ_Road, // get_slope_z_proc
1846 ClearTile_Road, // clear_tile_proc
1847 NULL, // add_accepted_cargo_proc
1848 GetTileDesc_Road, // get_tile_desc_proc
1849 GetTileTrackStatus_Road, // get_tile_track_status_proc
1850 ClickTile_Road, // click_tile_proc
1851 NULL, // animate_tile_proc
1852 TileLoop_Road, // tile_loop_proc
1853 ChangeTileOwner_Road, // change_tile_owner_proc
1854 NULL, // add_produced_cargo_proc
1855 VehicleEnter_Road, // vehicle_enter_tile_proc
1856 GetFoundation_Road, // get_foundation_proc
1857 TerraformTile_Road, // terraform_tile_proc
1858 };