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