OpenTTD
road_cmd.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 * 2 * _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, unless the bridge owner is a town. */
241  RoadType other_rt = (rt == ROADTYPE_ROAD) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
242  Owner other_owner = GetRoadOwner(tile, other_rt);
243  if (!IsTileOwner(tile, other_owner) && !IsTileOwner(tile, OWNER_TOWN)) {
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;
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)) {
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) ||
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;
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);
325  }
326 
327  if (present == ROAD_NONE) {
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 ? 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. */
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;
374  }
375 
376  Track railtrack = GetCrossingRailTrack(tile);
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. */
387  if (c != NULL) {
390  }
391  } else {
392  SetRoadTypes(tile, rts);
393  }
394  MarkTileDirtyByTile(tile);
395  YapfNotifyTrackLayoutChange(tile, railtrack);
396  }
397  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_ROAD] * 2);
398  }
399 
400  default:
401  case ROAD_TILE_DEPOT:
402  return CMD_ERROR;
403  }
404 }
405 
406 
418 static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
419 {
420  /* Remove already build pieces */
421  CLRBITS(*pieces, existing);
422 
423  /* If we can't build anything stop here */
424  if (*pieces == ROAD_NONE) return CMD_ERROR;
425 
426  /* All RoadBit combos are valid on flat land */
427  if (tileh == SLOPE_FLAT) return CommandCost();
428 
429  /* Steep slopes behave the same as slopes with one corner raised. */
430  if (IsSteepSlope(tileh)) {
432  }
433 
434  /* Save the merge of all bits of the current type */
435  RoadBits type_bits = existing | *pieces;
436 
437  /* Roads on slopes */
438  if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) {
439 
440  /* If we add leveling we've got to pay for it */
441  if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
442 
443  return CommandCost();
444  }
445 
446  /* Autocomplete uphill roads */
447  *pieces |= MirrorRoadBits(*pieces);
448  type_bits = existing | *pieces;
449 
450  /* Uphill roads */
451  if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) &&
452  (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) {
453 
454  /* Slopes with foundation ? */
455  if (IsSlopeWithOneCornerRaised(tileh)) {
456 
457  /* Prevent build on slopes if it isn't allowed */
459 
460  /* If we add foundation we've got to pay for it */
461  if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
462 
463  return CommandCost();
464  }
465  } else {
466  if (HasExactlyOneBit(existing) && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
467  return CommandCost();
468  }
469  }
470  return CMD_ERROR;
471 }
472 
484 CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
485 {
486  CompanyID company = _current_company;
488 
489  RoadBits existing = ROAD_NONE;
490  RoadBits other_bits = ROAD_NONE;
491 
492  /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
493  * if a non-company is building the road */
494  if ((Company::IsValidID(company) && p2 != 0) || (company == OWNER_TOWN && !Town::IsValidID(p2)) || (company == OWNER_DEITY && p2 != 0)) return CMD_ERROR;
495  if (company != OWNER_TOWN) {
496  const Town *town = CalcClosestTownFromTile(tile);
497  p2 = (town != NULL) ? town->index : INVALID_TOWN;
498 
499  if (company == OWNER_DEITY) {
500  company = OWNER_TOWN;
501 
502  /* If we are not within a town, we are not owned by the town */
503  if (town == NULL || DistanceSquare(tile, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
504  company = OWNER_NONE;
505  }
506  }
507  }
508 
509  RoadBits pieces = Extract<RoadBits, 0, 4>(p1);
510 
511  /* do not allow building 'zero' road bits, code wouldn't handle it */
512  if (pieces == ROAD_NONE) return CMD_ERROR;
513 
514  RoadType rt = Extract<RoadType, 4, 2>(p1);
515  if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
516 
517  DisallowedRoadDirections toggle_drd = Extract<DisallowedRoadDirections, 6, 2>(p1);
518 
519  Slope tileh = GetTileSlope(tile);
520 
521  bool need_to_clear = false;
522  switch (GetTileType(tile)) {
523  case MP_ROAD:
524  switch (GetRoadTileType(tile)) {
525  case ROAD_TILE_NORMAL: {
526  if (HasRoadWorks(tile)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
527 
528  other_bits = GetOtherRoadBits(tile, rt);
529  if (!HasTileRoadType(tile, rt)) break;
530 
531  existing = GetRoadBits(tile, rt);
532  bool crossing = !IsStraightRoad(existing | pieces);
533  if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
534  /* Junctions cannot be one-way */
535  return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
536  }
537  if ((existing & pieces) == pieces) {
538  /* We only want to set the (dis)allowed road directions */
539  if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM) {
540  if (crossing) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
541 
543  if (owner != OWNER_NONE) {
544  CommandCost ret = CheckOwnership(owner, tile);
545  if (ret.Failed()) return ret;
546  }
547 
549  DisallowedRoadDirections dis_new = dis_existing ^ toggle_drd;
550 
551  /* We allow removing disallowed directions to break up
552  * deadlocks, but adding them can break articulated
553  * vehicles. As such, only when less is disallowed,
554  * i.e. bits are removed, we skip the vehicle check. */
555  if (CountBits(dis_existing) <= CountBits(dis_new)) {
557  if (ret.Failed()) return ret;
558  }
559 
560  /* Ignore half built tiles */
561  if ((flags & DC_EXEC) && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
562  SetDisallowedRoadDirections(tile, dis_new);
563  MarkTileDirtyByTile(tile);
564  }
565  return CommandCost();
566  }
567  return_cmd_error(STR_ERROR_ALREADY_BUILT);
568  }
569  /* Disallow breaking end-of-line of someone else
570  * so trams can still reverse on this tile. */
571  if (rt == ROADTYPE_TRAM && HasExactlyOneBit(existing)) {
572  Owner owner = GetRoadOwner(tile, rt);
573  if (Company::IsValidID(owner)) {
574  CommandCost ret = CheckOwnership(owner);
575  if (ret.Failed()) return ret;
576  }
577  }
578  break;
579  }
580 
581  case ROAD_TILE_CROSSING:
582  other_bits = GetCrossingRoadBits(tile);
583  if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
584  pieces = other_bits; // we need to pay for both roadbits
585 
586  if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
587  break;
588 
589  case ROAD_TILE_DEPOT:
590  if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
591  goto do_clear;
592 
593  default: NOT_REACHED();
594  }
595  break;
596 
597  case MP_RAILWAY: {
598  if (IsSteepSlope(tileh)) {
599  return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
600  }
601 
602  /* Level crossings may only be built on these slopes */
603  if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
604  return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
605  }
606 
607  if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
608 
609  if (RailNoLevelCrossings(GetRailType(tile))) {
610  return_cmd_error(STR_ERROR_CROSSING_DISALLOWED);
611  }
612 
613  Axis roaddir;
614  switch (GetTrackBits(tile)) {
615  case TRACK_BIT_X:
616  if (pieces & ROAD_X) goto do_clear;
617  roaddir = AXIS_Y;
618  break;
619 
620  case TRACK_BIT_Y:
621  if (pieces & ROAD_Y) goto do_clear;
622  roaddir = AXIS_X;
623  break;
624 
625  default: goto do_clear;
626  }
627 
629  if (ret.Failed()) return ret;
630 
631  if (flags & DC_EXEC) {
632  Track railtrack = AxisToTrack(OtherAxis(roaddir));
633  YapfNotifyTrackLayoutChange(tile, railtrack);
634  /* Update company infrastructure counts. A level crossing has two road bits. */
635  Company *c = Company::GetIfValid(company);
636  if (c != NULL) {
637  c->infrastructure.road[rt] += 2;
638  if (rt != ROADTYPE_ROAD) c->infrastructure.road[ROADTYPE_ROAD] += 2;
640  }
641  /* Update rail count for level crossings. The plain track is already
642  * counted, so only add the difference to the level crossing cost. */
644  if (c != NULL) {
647  }
648 
649  /* Always add road to the roadtypes (can't draw without it) */
650  bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack);
651  MakeRoadCrossing(tile, company, company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
652  SetCrossingReservation(tile, reserved);
653  UpdateLevelCrossing(tile, false);
654  MarkTileDirtyByTile(tile);
655  }
656  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_ROAD] * (rt == ROADTYPE_ROAD ? 2 : 4));
657  }
658 
659  case MP_STATION: {
660  if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
661  if (!IsDriveThroughStopTile(tile)) goto do_clear;
662 
664  if (pieces & ~curbits) goto do_clear;
665  pieces = curbits; // we need to pay for both roadbits
666 
667  if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
668  break;
669  }
670 
671  case MP_TUNNELBRIDGE: {
672  if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) goto do_clear;
673  /* Only allow building the outern roadbit, so building long roads stops at existing bridges */
674  if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) goto do_clear;
675  if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
676  /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */
678  if (ret.Failed()) return ret;
679  break;
680  }
681 
682  default: {
683 do_clear:;
684  need_to_clear = true;
685  break;
686  }
687  }
688 
689  if (need_to_clear) {
690  CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
691  if (ret.Failed()) return ret;
692  cost.AddCost(ret);
693  }
694 
695  if (other_bits != pieces) {
696  /* Check the foundation/slopes when adding road/tram bits */
697  CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
698  /* Return an error if we need to build a foundation (ret != 0) but the
699  * current setting is turned off */
700  if (ret.Failed() || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
701  return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
702  }
703  cost.AddCost(ret);
704  }
705 
706  if (!need_to_clear) {
707  if (IsTileType(tile, MP_ROAD)) {
708  /* Don't put the pieces that already exist */
709  pieces &= ComplementRoadBits(existing);
710 
711  /* Check if new road bits will have the same foundation as other existing road types */
712  if (IsNormalRoad(tile)) {
713  Slope slope = GetTileSlope(tile);
714  Foundation found_new = GetRoadFoundation(slope, pieces | existing);
715 
716  /* Test if all other roadtypes can be built at that foundation */
717  for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
718  if (rtest != rt) { // check only other road types
719  RoadBits bits = GetRoadBits(tile, rtest);
720  /* do not check if there are not road bits of given type */
721  if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
722  return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
723  }
724  }
725  }
726  }
727  }
728 
730  if (ret.Failed()) return ret;
731 
732  }
733 
734  uint num_pieces = (!need_to_clear && IsTileType(tile, MP_TUNNELBRIDGE)) ?
735  /* There are 2 pieces on *every* tile of the bridge or tunnel */
736  2 * (GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2) :
737  /* Count pieces */
738  CountBits(pieces);
739 
740  cost.AddCost(num_pieces * _price[PR_BUILD_ROAD]);
741 
742  if (flags & DC_EXEC) {
743  switch (GetTileType(tile)) {
744  case MP_ROAD: {
745  RoadTileType rtt = GetRoadTileType(tile);
746  if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
747  SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
748  SetRoadOwner(tile, rt, company);
749  if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
750  }
751  if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
752  break;
753  }
754 
755  case MP_TUNNELBRIDGE: {
756  TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
757 
758  SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
759  SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
760  SetRoadOwner(other_end, rt, company);
761  SetRoadOwner(tile, rt, company);
762 
763  /* Mark tiles dirty that have been repaved */
764  if (IsBridge(tile)) {
765  MarkBridgeDirty(tile);
766  } else {
767  MarkTileDirtyByTile(other_end);
768  MarkTileDirtyByTile(tile);
769  }
770  break;
771  }
772 
773  case MP_STATION:
774  assert(IsDriveThroughStopTile(tile));
775  SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
776  SetRoadOwner(tile, rt, company);
777  break;
778 
779  default:
780  MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, company, company);
781  break;
782  }
783 
784  /* Update company infrastructure count. */
785  Company *c = Company::GetIfValid(GetRoadOwner(tile, rt));
786  if (c != NULL) {
787  if (IsTileType(tile, MP_TUNNELBRIDGE)) num_pieces *= TUNNELBRIDGE_TRACKBIT_FACTOR;
788  c->infrastructure.road[rt] += num_pieces;
790  }
791 
792  if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
793  existing |= pieces;
795  GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
796  }
797 
798  MarkTileDirtyByTile(tile);
799  }
800  return cost;
801 }
802 
811 {
812  tile += TileOffsByDiagDir(dir);
813  if (!IsValidTile(tile)) return false;
814  RoadBits bits = GetAnyRoadBits(tile, rt, false);
815  return (bits & DiagDirToRoadBits(ReverseDiagDir(dir))) != 0;
816 }
817 
835 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
836 {
838 
839  if (p1 >= MapSize()) return CMD_ERROR;
840 
841  TileIndex end_tile = p1;
842  RoadType rt = Extract<RoadType, 3, 2>(p2);
843  if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
844 
845  Axis axis = Extract<Axis, 2, 1>(p2);
846  /* Only drag in X or Y direction dictated by the direction variable */
847  if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
848  if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
849 
850  DiagDirection dir = AxisToDiagDir(axis);
851 
852  /* Swap direction, also the half-tile drag var (bit 0 and 1) */
853  if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
854  dir = ReverseDiagDir(dir);
855  p2 ^= 3;
856  drd = DRD_SOUTHBOUND;
857  }
858 
859  /* On the X-axis, we have to swap the initial bits, so they
860  * will be interpreted correctly in the GTTS. Furthermore
861  * when you just 'click' on one tile to build them. */
862  if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
863  /* No disallowed direction bits have to be toggled */
864  if (!HasBit(p2, 5)) drd = DRD_NONE;
865 
867  CommandCost last_error = CMD_ERROR;
868  TileIndex tile = start_tile;
869  bool had_bridge = false;
870  bool had_tunnel = false;
871  bool had_success = false;
872  bool is_ai = HasBit(p2, 6);
873 
874  /* Start tile is the first tile clicked by the user. */
875  for (;;) {
876  RoadBits bits = AxisToRoadBits(axis);
877 
878  /* Determine which road parts should be built. */
879  if (!is_ai && start_tile != end_tile) {
880  /* Only build the first and last roadbit if they can connect to something. */
881  if (tile == end_tile && !CanConnectToRoad(tile, rt, dir)) {
882  bits = DiagDirToRoadBits(ReverseDiagDir(dir));
883  } else if (tile == start_tile && !CanConnectToRoad(tile, rt, ReverseDiagDir(dir))) {
884  bits = DiagDirToRoadBits(dir);
885  }
886  } else {
887  /* Road parts only have to be built at the start tile or at the end tile. */
888  if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
889  if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
890  }
891 
892  CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
893  if (ret.Failed()) {
894  last_error = ret;
895  if (last_error.GetErrorMessage() != STR_ERROR_ALREADY_BUILT) {
896  if (is_ai) return last_error;
897  break;
898  }
899  } else {
900  had_success = true;
901  /* Only pay for the upgrade on one side of the bridges and tunnels */
902  if (IsTileType(tile, MP_TUNNELBRIDGE)) {
903  if (IsBridge(tile)) {
904  if (!had_bridge || GetTunnelBridgeDirection(tile) == dir) {
905  cost.AddCost(ret);
906  }
907  had_bridge = true;
908  } else { // IsTunnel(tile)
909  if (!had_tunnel || GetTunnelBridgeDirection(tile) == dir) {
910  cost.AddCost(ret);
911  }
912  had_tunnel = true;
913  }
914  } else {
915  cost.AddCost(ret);
916  }
917  }
918 
919  if (tile == end_tile) break;
920 
921  tile += TileOffsByDiagDir(dir);
922  }
923 
924  return had_success ? cost : last_error;
925 }
926 
940 CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
941 {
943 
944  if (p1 >= MapSize()) return CMD_ERROR;
945 
946  TileIndex end_tile = p1;
947  RoadType rt = Extract<RoadType, 3, 2>(p2);
948  if (!IsValidRoadType(rt)) return CMD_ERROR;
949 
950  Axis axis = Extract<Axis, 2, 1>(p2);
951  /* Only drag in X or Y direction dictated by the direction variable */
952  if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
953  if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
954 
955  /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
956  if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
957  TileIndex t = start_tile;
958  start_tile = end_tile;
959  end_tile = t;
960  p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
961  }
962 
964  TileIndex tile = start_tile;
965  CommandCost last_error = CMD_ERROR;
966  bool had_success = false;
967  /* Start tile is the small number. */
968  for (;;) {
969  RoadBits bits = AxisToRoadBits(axis);
970 
971  if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
972  if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
973 
974  /* try to remove the halves. */
975  if (bits != 0) {
976  CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
977  if (ret.Succeeded()) {
978  if (flags & DC_EXEC) {
979  money -= ret.GetCost();
980  if (money < 0) {
981  _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
982  return cost;
983  }
984  RemoveRoad(tile, flags, bits, rt, true, false);
985  }
986  cost.AddCost(ret);
987  had_success = true;
988  } else {
989  /* Ownership errors are more important. */
990  if (last_error.GetErrorMessage() != STR_ERROR_OWNED_BY) last_error = ret;
991  }
992  }
993 
994  if (tile == end_tile) break;
995 
996  tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
997  }
998 
999  return had_success ? cost : last_error;
1000 }
1001 
1015 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1016 {
1017  DiagDirection dir = Extract<DiagDirection, 0, 2>(p1);
1018  RoadType rt = Extract<RoadType, 2, 2>(p1);
1019 
1020  if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
1021 
1023 
1024  Slope tileh = GetTileSlope(tile);
1025  if (tileh != SLOPE_FLAT) {
1027  return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
1028  }
1029  cost.AddCost(_price[PR_BUILD_FOUNDATION]);
1030  }
1031 
1032  cost.AddCost(DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR));
1033  if (cost.Failed()) return cost;
1034 
1035  if (IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
1036 
1037  if (!Depot::CanAllocateItem()) return CMD_ERROR;
1038 
1039  if (flags & DC_EXEC) {
1040  Depot *dep = new Depot(tile);
1041  dep->build_date = _date;
1042 
1043  /* A road depot has two road bits. */
1044  Company::Get(_current_company)->infrastructure.road[rt] += 2;
1046 
1047  MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
1048  MarkTileDirtyByTile(tile);
1049  MakeDefaultName(dep);
1050  }
1051  cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
1052  return cost;
1053 }
1054 
1055 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
1056 {
1057  if (_current_company != OWNER_WATER) {
1058  CommandCost ret = CheckTileOwnership(tile);
1059  if (ret.Failed()) return ret;
1060  }
1061 
1063  if (ret.Failed()) return ret;
1064 
1065  if (flags & DC_EXEC) {
1067  if (c != NULL) {
1068  /* A road depot has two road bits. */
1071  }
1072 
1073  delete Depot::GetByTile(tile);
1074  DoClearSquare(tile);
1075  }
1076 
1077  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
1078 }
1079 
1080 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
1081 {
1082  switch (GetRoadTileType(tile)) {
1083  case ROAD_TILE_NORMAL: {
1084  RoadBits b = GetAllRoadBits(tile);
1085 
1086  /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
1087  if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
1089  RoadType rt;
1090  FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
1091  CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
1092  if (tmp_ret.Failed()) return tmp_ret;
1093  ret.AddCost(tmp_ret);
1094  }
1095  return ret;
1096  }
1097  return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
1098  }
1099 
1100  case ROAD_TILE_CROSSING: {
1101  RoadTypes rts = GetRoadTypes(tile);
1103 
1104  if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
1105 
1106  /* Must iterate over the roadtypes in a reverse manner because
1107  * tram tracks must be removed before the road bits. */
1108  RoadType rt = ROADTYPE_TRAM;
1109  do {
1110  if (HasBit(rts, rt)) {
1111  CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
1112  if (tmp_ret.Failed()) return tmp_ret;
1113  ret.AddCost(tmp_ret);
1114  }
1115  } while (rt-- != ROADTYPE_ROAD);
1116 
1117  if (flags & DC_EXEC) {
1118  DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
1119  }
1120  return ret;
1121  }
1122 
1123  default:
1124  case ROAD_TILE_DEPOT:
1125  if (flags & DC_AUTO) {
1126  return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
1127  }
1128  return RemoveRoadDepot(tile, flags);
1129  }
1130 }
1131 
1132 
1134  uint16 image;
1135  byte subcoord_x;
1136  byte subcoord_y;
1137 };
1138 
1139 #include "table/road_land.h"
1140 
1149 {
1150  /* Flat land and land without a road doesn't require a foundation */
1151  if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
1152 
1153  /* Steep slopes behave the same as slopes with one corner raised. */
1154  if (IsSteepSlope(tileh)) {
1156  }
1157 
1158  /* Leveled RoadBits on a slope */
1159  if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
1160 
1161  /* Straight roads without foundation on a slope */
1162  if (!IsSlopeWithOneCornerRaised(tileh) &&
1163  (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
1164  return FOUNDATION_NONE;
1165 
1166  /* Roads on steep Slopes or on Slopes with one corner raised */
1167  return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
1168 }
1169 
1170 const byte _road_sloped_sprites[14] = {
1171  0, 0, 2, 0,
1172  0, 1, 0, 0,
1173  3, 0, 0, 0,
1174  0, 0
1175 };
1176 
1186 static bool DrawRoadAsSnowDesert(TileIndex tile, Roadside roadside)
1187 {
1188  return (IsOnSnow(tile) &&
1189  !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
1190  roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
1191 }
1192 
1198 void DrawRoadCatenary(const TileInfo *ti, RoadBits tram)
1199 {
1200  /* Do not draw catenary if it is invisible */
1201  if (IsInvisibilitySet(TO_CATENARY)) return;
1202 
1203  /* Don't draw the catenary under a low bridge */
1205  int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
1206 
1207  if (height <= GetTileMaxZ(ti->tile) + 1) return;
1208  }
1209 
1210  SpriteID front;
1211  SpriteID back;
1212 
1213  if (ti->tileh != SLOPE_FLAT) {
1214  back = SPR_TRAMWAY_BACK_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
1215  front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
1216  } else {
1217  back = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
1218  front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
1219  }
1220 
1221  AddSortableSpriteToDraw(back, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
1222  AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
1223 }
1224 
1233 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
1234 {
1235  int x = ti->x | dx;
1236  int y = ti->y | dy;
1237  int z = ti->z;
1238  if (ti->tileh != SLOPE_FLAT) z = GetSlopePixelZ(x, y);
1239  AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
1240 }
1241 
1246 static void DrawRoadBits(TileInfo *ti)
1247 {
1248  RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
1249  RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
1250 
1251  SpriteID image = 0;
1252  PaletteID pal = PAL_NONE;
1253 
1254  if (ti->tileh != SLOPE_FLAT) {
1255  DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
1256 
1257  /* DrawFoundation() modifies ti.
1258  * Default sloped sprites.. */
1259  if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + SPR_ROAD_SLOPE_START;
1260  }
1261 
1262  if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
1263 
1264  Roadside roadside = GetRoadside(ti->tile);
1265 
1266  if (DrawRoadAsSnowDesert(ti->tile, roadside)) {
1267  image += 19;
1268  } else {
1269  switch (roadside) {
1270  case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
1271  case ROADSIDE_GRASS: break;
1272  case ROADSIDE_GRASS_ROAD_WORKS: break;
1273  default: image -= 19; break; // Paved
1274  }
1275  }
1276 
1277  DrawGroundSprite(image, pal);
1278 
1279  /* For tram we overlay the road graphics with either tram tracks only
1280  * (when there is actual road beneath the trams) or with tram tracks
1281  * and some dirts which hides the road graphics */
1282  if (tram != ROAD_NONE) {
1283  if (ti->tileh != SLOPE_FLAT) {
1284  image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
1285  } else {
1286  image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
1287  }
1288  image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
1289  DrawGroundSprite(image, pal);
1290  }
1291 
1292  if (road != ROAD_NONE) {
1294  if (drd != DRD_NONE) {
1295  DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialPixelZ(8, 8, ti->tileh));
1296  }
1297  }
1298 
1299  if (HasRoadWorks(ti->tile)) {
1300  /* Road works */
1301  DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
1302  return;
1303  }
1304 
1305  if (tram != ROAD_NONE) DrawRoadCatenary(ti, tram);
1306 
1307  /* Return if full detail is disabled, or we are zoomed fully out. */
1308  if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
1309 
1310  /* Do not draw details (street lights, trees) under low bridge */
1311  if (IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
1312  int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
1313  int minz = GetTileMaxZ(ti->tile) + 2;
1314 
1315  if (roadside == ROADSIDE_TREES) minz++;
1316 
1317  if (height < minz) return;
1318  }
1319 
1320  /* If there are no road bits, return, as there is nothing left to do */
1321  if (HasAtMostOneBit(road)) return;
1322 
1323  /* Draw extra details. */
1324  for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
1325  DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
1326  }
1327 }
1328 
1330 static void DrawTile_Road(TileInfo *ti)
1331 {
1332  switch (GetRoadTileType(ti->tile)) {
1333  case ROAD_TILE_NORMAL:
1334  DrawRoadBits(ti);
1335  break;
1336 
1337  case ROAD_TILE_CROSSING: {
1339 
1340  PaletteID pal = PAL_NONE;
1341  const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1342 
1343  if (rti->UsesOverlay()) {
1344  Axis axis = GetCrossingRailAxis(ti->tile);
1345  SpriteID road = SPR_ROAD_Y + axis;
1346 
1347  Roadside roadside = GetRoadside(ti->tile);
1348 
1349  if (DrawRoadAsSnowDesert(ti->tile, roadside)) {
1350  road += 19;
1351  } else {
1352  switch (roadside) {
1353  case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
1354  case ROADSIDE_GRASS: break;
1355  default: road -= 19; break; // Paved
1356  }
1357  }
1358 
1359  DrawGroundSprite(road, pal);
1360 
1361  SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
1362  /* Draw tracks, but draw PBS reserved tracks darker. */
1363  pal = (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) ? PALETTE_CRASH : PAL_NONE;
1364  DrawGroundSprite(rail, pal);
1365 
1366  DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
1367  } else {
1368  SpriteID image = rti->base_sprites.crossing;
1369 
1370  if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
1371  if (IsCrossingBarred(ti->tile)) image += 2;
1372 
1373  Roadside roadside = GetRoadside(ti->tile);
1374 
1375  if (DrawRoadAsSnowDesert(ti->tile, roadside)) {
1376  image += 8;
1377  } else {
1378  switch (roadside) {
1379  case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
1380  case ROADSIDE_GRASS: break;
1381  default: image += 4; break; // Paved
1382  }
1383  }
1384 
1385  DrawGroundSprite(image, pal);
1386 
1387  /* PBS debugging, draw reserved tracks darker */
1388  if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
1390  }
1391  }
1392 
1393  if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
1394  DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
1396  }
1398  break;
1399  }
1400 
1401  default:
1402  case ROAD_TILE_DEPOT: {
1404 
1405  PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
1406 
1407  const DrawTileSprites *dts;
1408  if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
1409  dts = &_tram_depot[GetRoadDepotDirection(ti->tile)];
1410  } else {
1411  dts = &_road_depot[GetRoadDepotDirection(ti->tile)];
1412  }
1413 
1414  DrawGroundSprite(dts->ground.sprite, PAL_NONE);
1415  DrawOrigTileSeq(ti, dts, TO_BUILDINGS, palette);
1416  break;
1417  }
1418  }
1419  DrawBridgeMiddle(ti);
1420 }
1421 
1429 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
1430 {
1431  PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
1432  const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
1433 
1434  DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
1435  DrawOrigTileSeqInGUI(x, y, dts, palette);
1436 }
1437 
1443 void UpdateNearestTownForRoadTiles(bool invalidate)
1444 {
1445  assert(!invalidate || _generating_world);
1446 
1447  for (TileIndex t = 0; t < MapSize(); t++) {
1448  if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) {
1449  TownID tid = INVALID_TOWN;
1450  if (!invalidate) {
1451  const Town *town = CalcClosestTownFromTile(t);
1452  if (town != NULL) tid = town->index;
1453  }
1454  SetTownIndex(t, tid);
1455  }
1456  }
1457 }
1458 
1459 static int GetSlopePixelZ_Road(TileIndex tile, uint x, uint y)
1460 {
1461 
1462  if (IsNormalRoad(tile)) {
1463  int z;
1464  Slope tileh = GetTilePixelSlope(tile, &z);
1465  if (tileh == SLOPE_FLAT) return z;
1466 
1467  Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
1468  z += ApplyPixelFoundationToSlope(f, &tileh);
1469  return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
1470  } else {
1471  return GetTileMaxPixelZ(tile);
1472  }
1473 }
1474 
1475 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
1476 {
1477  if (IsNormalRoad(tile)) {
1478  return GetRoadFoundation(tileh, GetAllRoadBits(tile));
1479  } else {
1480  return FlatteningFoundation(tileh);
1481  }
1482 }
1483 
1484 static const Roadside _town_road_types[][2] = {
1487  { ROADSIDE_PAVED, ROADSIDE_PAVED },
1489  { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
1490 };
1491 
1492 static const Roadside _town_road_types_2[][2] = {
1495  { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
1496  { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
1497  { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
1498 };
1499 
1500 
1501 static void TileLoop_Road(TileIndex tile)
1502 {
1504  case LT_ARCTIC:
1505  if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
1506  ToggleSnow(tile);
1507  MarkTileDirtyByTile(tile);
1508  }
1509  break;
1510 
1511  case LT_TROPIC:
1512  if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
1513  ToggleDesert(tile);
1514  MarkTileDirtyByTile(tile);
1515  }
1516  break;
1517  }
1518 
1519  if (IsRoadDepot(tile)) return;
1520 
1521  const Town *t = ClosestTownFromTile(tile, UINT_MAX);
1522  if (!HasRoadWorks(tile)) {
1523  HouseZonesBits grp = HZB_TOWN_EDGE;
1524 
1525  if (t != NULL) {
1526  grp = GetTownRadiusGroup(t, tile);
1527 
1528  /* Show an animation to indicate road work */
1529  if (t->road_build_months != 0 &&
1530  (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
1531  IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
1532  if (GetFoundationSlope(tile) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile).Succeeded() && Chance16(1, 40)) {
1533  StartRoadWorks(tile);
1534 
1535  if (_settings_client.sound.ambient) SndPlayTileFx(SND_21_JACKHAMMER, tile);
1537  TileX(tile) * TILE_SIZE + 7,
1538  TileY(tile) * TILE_SIZE + 7,
1539  0,
1540  EV_BULLDOZER);
1541  MarkTileDirtyByTile(tile);
1542  return;
1543  }
1544  }
1545  }
1546 
1547  {
1548  /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
1549  const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
1550  Roadside cur_rs = GetRoadside(tile);
1551 
1552  /* We have our desired type, do nothing */
1553  if (cur_rs == new_rs[0]) return;
1554 
1555  /* We have the pre-type of the desired type, switch to the desired type */
1556  if (cur_rs == new_rs[1]) {
1557  cur_rs = new_rs[0];
1558  /* We have barren land, install the pre-type */
1559  } else if (cur_rs == ROADSIDE_BARREN) {
1560  cur_rs = new_rs[1];
1561  /* We're totally off limits, remove any installation and make barren land */
1562  } else {
1563  cur_rs = ROADSIDE_BARREN;
1564  }
1565  SetRoadside(tile, cur_rs);
1566  MarkTileDirtyByTile(tile);
1567  }
1568  } else if (IncreaseRoadWorksCounter(tile)) {
1569  TerminateRoadWorks(tile);
1570 
1572  /* Generate a nicer town surface */
1573  const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
1574  const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
1575 
1576  if (old_rb != new_rb) {
1577  RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
1578  }
1579  }
1580 
1581  MarkTileDirtyByTile(tile);
1582  }
1583 }
1584 
1585 static bool ClickTile_Road(TileIndex tile)
1586 {
1587  if (!IsRoadDepot(tile)) return false;
1588 
1589  ShowDepotWindow(tile, VEH_ROAD);
1590  return true;
1591 }
1592 
1593 /* Converts RoadBits to TrackBits */
1594 static const TrackBits _road_trackbits[16] = {
1595  TRACK_BIT_NONE, // ROAD_NONE
1596  TRACK_BIT_NONE, // ROAD_NW
1597  TRACK_BIT_NONE, // ROAD_SW
1598  TRACK_BIT_LEFT, // ROAD_W
1599  TRACK_BIT_NONE, // ROAD_SE
1600  TRACK_BIT_Y, // ROAD_Y
1601  TRACK_BIT_LOWER, // ROAD_S
1602  TRACK_BIT_LEFT | TRACK_BIT_LOWER | TRACK_BIT_Y, // ROAD_Y | ROAD_SW
1603  TRACK_BIT_NONE, // ROAD_NE
1604  TRACK_BIT_UPPER, // ROAD_N
1605  TRACK_BIT_X, // ROAD_X
1606  TRACK_BIT_LEFT | TRACK_BIT_UPPER | TRACK_BIT_X, // ROAD_X | ROAD_NW
1607  TRACK_BIT_RIGHT, // ROAD_E
1608  TRACK_BIT_RIGHT | TRACK_BIT_UPPER | TRACK_BIT_Y, // ROAD_Y | ROAD_NE
1609  TRACK_BIT_RIGHT | TRACK_BIT_LOWER | TRACK_BIT_X, // ROAD_X | ROAD_SE
1610  TRACK_BIT_ALL, // ROAD_ALL
1611 };
1612 
1613 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
1614 {
1615  TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
1616  TrackdirBits red_signals = TRACKDIR_BIT_NONE; // crossing barred
1617  switch (mode) {
1618  case TRANSPORT_RAIL:
1619  if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
1620  break;
1621 
1622  case TRANSPORT_ROAD:
1623  if ((GetRoadTypes(tile) & sub_mode) == 0) break;
1624  switch (GetRoadTileType(tile)) {
1625  case ROAD_TILE_NORMAL: {
1626  const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
1627  RoadType rt = (RoadType)FindFirstBit(sub_mode);
1628  RoadBits bits = GetRoadBits(tile, rt);
1629 
1630  /* no roadbit at this side of tile, return 0 */
1631  if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
1632 
1633  uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
1634  if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
1635  break;
1636  }
1637 
1638  case ROAD_TILE_CROSSING: {
1639  Axis axis = GetCrossingRoadAxis(tile);
1640 
1641  if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
1642 
1643  trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
1644  if (IsCrossingBarred(tile)) red_signals = trackdirbits;
1645  break;
1646  }
1647 
1648  default:
1649  case ROAD_TILE_DEPOT: {
1651 
1652  if (side != INVALID_DIAGDIR && side != dir) break;
1653 
1654  trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
1655  break;
1656  }
1657  }
1658  break;
1659 
1660  default: break;
1661  }
1662  return CombineTrackStatus(trackdirbits, red_signals);
1663 }
1664 
1665 static const StringID _road_tile_strings[] = {
1666  STR_LAI_ROAD_DESCRIPTION_ROAD,
1667  STR_LAI_ROAD_DESCRIPTION_ROAD,
1668  STR_LAI_ROAD_DESCRIPTION_ROAD,
1669  STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS,
1670  STR_LAI_ROAD_DESCRIPTION_ROAD,
1671  STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD,
1672  STR_LAI_ROAD_DESCRIPTION_ROAD,
1673  STR_LAI_ROAD_DESCRIPTION_ROAD,
1674 };
1675 
1676 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
1677 {
1678  Owner rail_owner = INVALID_OWNER;
1679  Owner road_owner = INVALID_OWNER;
1680  Owner tram_owner = INVALID_OWNER;
1681 
1682  switch (GetRoadTileType(tile)) {
1683  case ROAD_TILE_CROSSING: {
1684  td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING;
1685  RoadTypes rts = GetRoadTypes(tile);
1686  rail_owner = GetTileOwner(tile);
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 
1690  const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
1691  td->railtype = rti->strings.name;
1692  td->rail_speed = rti->max_speed;
1693 
1694  break;
1695  }
1696 
1697  case ROAD_TILE_DEPOT:
1698  td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
1699  road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter
1700  td->build_date = Depot::GetByTile(tile)->build_date;
1701  break;
1702 
1703  default: {
1704  RoadTypes rts = GetRoadTypes(tile);
1705  td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
1706  if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
1707  if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
1708  break;
1709  }
1710  }
1711 
1712  /* Now we have to discover, if the tile has only one owner or many:
1713  * - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available)
1714  * - Compare the found owner with the other owners, and test if they differ.
1715  * Note: If road exists it will be the first_owner.
1716  */
1717  Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
1718  bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
1719 
1720  if (mixed_owners) {
1721  /* Multiple owners */
1722  td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
1723  td->owner[0] = rail_owner;
1724  td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
1725  td->owner[1] = road_owner;
1726  td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
1727  td->owner[2] = tram_owner;
1728  } else {
1729  /* One to rule them all */
1730  td->owner[0] = first_owner;
1731  }
1732 }
1733 
1738 static const byte _roadveh_enter_depot_dir[4] = {
1740 };
1741 
1742 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
1743 {
1744  switch (GetRoadTileType(tile)) {
1745  case ROAD_TILE_DEPOT: {
1746  if (v->type != VEH_ROAD) break;
1747 
1748  RoadVehicle *rv = RoadVehicle::From(v);
1749  if (rv->frame == RVC_DEPOT_STOP_FRAME &&
1751  rv->state = RVSB_IN_DEPOT;
1752  rv->vehstatus |= VS_HIDDEN;
1753  rv->direction = ReverseDir(rv->direction);
1754  if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
1755  rv->tile = tile;
1756 
1758  return VETSB_ENTERED_WORMHOLE;
1759  }
1760  break;
1761  }
1762 
1763  default: break;
1764  }
1765  return VETSB_CONTINUE;
1766 }
1767 
1768 
1769 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
1770 {
1771  if (IsRoadDepot(tile)) {
1772  if (GetTileOwner(tile) == old_owner) {
1773  if (new_owner == INVALID_OWNER) {
1775  } else {
1776  /* A road depot has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
1778  Company::Get(old_owner)->infrastructure.road[rt] -= 2;
1779  Company::Get(new_owner)->infrastructure.road[rt] += 2;
1780 
1781  SetTileOwner(tile, new_owner);
1782  for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
1783  if (GetRoadOwner(tile, rt) == old_owner) {
1784  SetRoadOwner(tile, rt, new_owner);
1785  }
1786  }
1787  }
1788  }
1789  return;
1790  }
1791 
1792  for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
1793  /* Update all roadtypes, no matter if they are present */
1794  if (GetRoadOwner(tile, rt) == old_owner) {
1795  if (HasTileRoadType(tile, rt)) {
1796  /* A level crossing has two road bits. No need to dirty windows here, we'll redraw the whole screen anyway. */
1797  uint num_bits = IsLevelCrossing(tile) ? 2 : CountBits(GetRoadBits(tile, rt));
1798  Company::Get(old_owner)->infrastructure.road[rt] -= num_bits;
1799  if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.road[rt] += num_bits;
1800  }
1801 
1802  SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
1803  }
1804  }
1805 
1806  if (IsLevelCrossing(tile)) {
1807  if (GetTileOwner(tile) == old_owner) {
1808  if (new_owner == INVALID_OWNER) {
1810  } else {
1811  /* Update infrastructure counts. No need to dirty windows here, we'll redraw the whole screen anyway. */
1812  Company::Get(old_owner)->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR;
1813  Company::Get(new_owner)->infrastructure.rail[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR;
1814 
1815  SetTileOwner(tile, new_owner);
1816  }
1817  }
1818  }
1819 }
1820 
1821 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
1822 {
1824  switch (GetRoadTileType(tile)) {
1825  case ROAD_TILE_CROSSING:
1826  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]);
1827  break;
1828 
1829  case ROAD_TILE_DEPOT:
1830  if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
1831  break;
1832 
1833  case ROAD_TILE_NORMAL: {
1834  RoadBits bits = GetAllRoadBits(tile);
1835  RoadBits bits_copy = bits;
1836  /* Check if the slope-road_bits combination is valid at all, i.e. it is safe to call GetRoadFoundation(). */
1837  if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
1838  /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */
1839  if (bits == bits_copy) {
1840  int z_old;
1841  Slope tileh_old = GetTileSlope(tile, &z_old);
1842 
1843  /* Get the slope on top of the foundation */
1844  z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
1845  z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
1846 
1847  /* The surface slope must not be changed */
1848  if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
1849  }
1850  }
1851  break;
1852  }
1853 
1854  default: NOT_REACHED();
1855  }
1856  }
1857 
1858  return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
1859 }
1860 
1862 extern const TileTypeProcs _tile_type_road_procs = {
1863  DrawTile_Road, // draw_tile_proc
1864  GetSlopePixelZ_Road, // get_slope_z_proc
1865  ClearTile_Road, // clear_tile_proc
1866  NULL, // add_accepted_cargo_proc
1867  GetTileDesc_Road, // get_tile_desc_proc
1868  GetTileTrackStatus_Road, // get_tile_track_status_proc
1869  ClickTile_Road, // click_tile_proc
1870  NULL, // animate_tile_proc
1871  TileLoop_Road, // tile_loop_proc
1872  ChangeTileOwner_Road, // change_tile_owner_proc
1873  NULL, // add_produced_cargo_proc
1874  VehicleEnter_Road, // vehicle_enter_tile_proc
1875  GetFoundation_Road, // get_foundation_proc
1876  TerraformTile_Road, // terraform_tile_proc
1877 };
Functions related to OTTD&#39;s strings.
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:98
Road vehicle states.
don&#39;t allow building on structures
Definition: command_type.h:346
uint ApplyFoundationToSlope(Foundation f, Slope *s)
Applies a foundation to a slope.
Definition: landscape.cpp:164
Bulldozer at roadworks.
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:20
SpriteID crossing
level crossing, rail in X direction
Definition: rail.h:135
byte state
Definition: roadveh.h:89
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
Definition of stuff that is very close to a company, like the company struct itself.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
void UpdateNearestTownForRoadTiles(bool invalidate)
Updates cached nearest town for all road tiles.
Definition: road_cmd.cpp:1443
NewGRF handling of rail types.
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:298
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:240
static TransportType GetTunnelBridgeTransportType(TileIndex t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
static void DrawOrigTileSeqInGUI(int x, int y, const DrawTileSprites *dts, PaletteID default_palette)
Draw TTD sprite sequence in GUI.
Definition: sprite.h:117
Tile information, used while rendering the tile.
Definition: tile_cmd.h:44
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:200
CommandCost CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, RoadType rt, DoCommandFlag flags, bool town_check)
Is it allowed to remove the given road bits from the given tile?
Definition: road_cmd.cpp:116
Level crossing.
Definition: road_map.h:25
All zoomlevels below or equal to this, will result in details on the screen, like road-work...
Definition: zoom_type.h:45
byte landscape
the landscape we&#39;re currently in
DirectionByte direction
facing
Definition: vehicle_base.h:271
company buildings - depots, stations, HQ, ...
Definition: transparency.h:29
Tile is desert.
Definition: tile_type.h:73
static const RoadBits _invalid_tileh_slopes_road[2][15]
Invalid RoadBits on slopes.
Definition: road_cmd.cpp:56
#define ToggleDesert
Toggle the snow/desert state of a road tile.
Definition: road_map.h:435
Y-axis and direction to north-west.
Definition: track_type.h:85
static RoadBits ComplementRoadBits(RoadBits r)
Calculate the complement of a RoadBits value.
Definition: road_func.h:85
static const uint LEVELCROSSING_TRACKBIT_FACTOR
Multiplier for how many regular track bits a level crossing counts.
Definition: economy_type.h:216
#define FOR_ALL_EFFECTVEHICLES(var)
Iterate over disaster vehicles.
void MarkBridgeDirty(TileIndex begin, TileIndex end, DiagDirection direction, uint bridge_height)
Mark bridge tiles dirty.
static void StartRoadWorks(TileIndex t)
Start road works on a tile.
Definition: road_map.h:505
byte _display_opt
What do we want to draw/do?
EconomySettings economy
settings to change the economy
uint32 squared_town_zone_radius[HZB_END]
UpdateTownRadius updates this given the house count.
Definition: town.h:50
static RoadBits GetAllRoadBits(TileIndex tile)
Get all set RoadBits on the given tile.
Definition: road_map.h:139
uint GetPartialPixelZ(int x, int y, Slope corners)
Determines height at given coordinate of a slope.
Definition: landscape.cpp:217
WindowFlags flags
Window flags.
Definition: window_gui.h:312
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition: vehicle.cpp:539
Flag for an invalid DiagDirection.
bool ValParamRoadType(const RoadType rt)
Validate functions for rail building.
Definition: road.cpp:129
static bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
Check if a specific road type is owned by an owner.
Definition: road_map.h:237
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Money GetAvailableMoneyForCommand()
Definition: command.cpp:518
static const SpriteID SPR_ONEWAY_BASE
One way road sprites.
Definition: sprites.h:285
Functions related to dates.
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
Basic road type.
Definition: road_type.h:24
static bool HasCrossingReservation(TileIndex t)
Get the reservation state of the rail crossing.
Definition: road_map.h:350
remove a complete road (not a "half" one)
Definition: command_type.h:202
Track
These are used to specify a single track.
Definition: track_type.h:21
static TrackBits GetCrossingRailBits(TileIndex tile)
Get the rail track bits of a level crossing.
Definition: road_map.h:338
CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build a piece of road.
Definition: road_cmd.cpp:484
A tile with road (or tram tracks)
Definition: tile_type.h:45
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
static TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition: track_func.h:98
Depot view; Window numbers:
Definition: window_type.h:346
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:47
Full road along the x-axis (south-west + north-east)
Definition: road_type.h:61
Functions used internally by the roads.
Used for iterations.
Definition: road_type.h:26
bool extra_dynamite
extra dynamite
Functions related to vehicles.
static TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track bits incidating with that diagdir. ...
Definition: track_func.h:534
static void MakeRoadDepot(TileIndex t, Owner owner, DepotID did, DiagDirection dir, RoadType rt)
Make a road depot.
Definition: road_map.h:600
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it...
Definition: vehicle.cpp:1437
South-west part.
Definition: road_type.h:58
X-axis and direction to south-west.
Definition: track_type.h:84
Vehicle data structure.
Definition: vehicle_base.h:212
void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
Draw the road depot sprite.
Definition: road_cmd.cpp:1429
static RoadTypes RoadTypeToRoadTypes(RoadType rt)
Maps a RoadType to the corresponding RoadTypes value.
Definition: road_func.h:56
static int GetSlopeMaxZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height) ...
Definition: slope_func.h:162
Base for all depots (except hangars)
static bool HasTileRoadType(TileIndex t, RoadType rt)
Check if a tile has a specific road type.
Definition: road_map.h:188
Tile description for the &#39;land area information&#39; tool.
Definition: tile_cmd.h:53
demolish a tile
Definition: command_type.h:182
CommandCost CheckTileOwnership(TileIndex tile)
Check whether the current owner owns the stuff on the given tile.
bool ambient
Play ambient, industry and town sounds.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
static RoadBits GetRoadBits(TileIndex t, RoadType rt)
Get the present road bits for a specific road type.
Definition: road_map.h:111
#define FOR_EACH_SET_ROADTYPE(var, road_types)
Iterate through each set RoadType in a RoadTypes value.
Definition: road_func.h:28
static Roadside GetRoadside(TileIndex tile)
Get the decorations of a road.
Definition: road_map.h:463
Sprite constructs for road depots.
A special vehicle is one of the following:
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:38
T * First() const
Get the first vehicle in the chain.
Also draw details of track and roads.
Definition: openttd.h:47
Helper functions to extract data from command parameters.
int GetBridgeHeight(TileIndex t)
Get the height (&#39;z&#39;) of a bridge.
Definition: bridge_map.cpp:72
build a "half" road
Definition: command_type.h:203
static void TerminateRoadWorks(TileIndex t)
Terminate road works on a tile.
Definition: road_map.h:521
A railway.
Definition: tile_type.h:44
static DiagDirection GetRoadDepotDirection(TileIndex t)
Get the direction of the exit of a road depot.
Definition: road_map.h:535
static Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track AXIS_X -> TRACK_X AXIS_Y -> TRACK_Y Uses the fact that the...
Definition: track_func.h:76
Functions related to world/map generation.
Money GetCost() const
The costs as made up to this moment.
Definition: command_type.h:84
#define CLRBITS(x, y)
Clears several bits in a variable.
static const byte _roadveh_enter_depot_dir[4]
Given the direction the road depot is pointing, this is the direction the vehicle should be travellin...
Definition: road_cmd.cpp:1738
Common return value for all commands.
Definition: command_type.h:25
static bool HasExactlyOneBit(T value)
Test whether value has exactly 1 bit set.
static Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:101
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:68
static void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
Sets the disallowed directions.
Definition: road_map.h:282
RoadType
The different roadtypes we support.
Definition: road_type.h:22
static bool RailNoLevelCrossings(RailType rt)
Test if a RailType disallows build of level crossings.
Definition: rail.h:336
static bool IsDriveThroughStopTile(TileIndex t)
Is tile t a drive through road stop station?
Definition: station_map.h:234
byte vehstatus
Status.
Definition: vehicle_base.h:317
EffectVehicle * CreateEffectVehicleAbove(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular location.
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
static bool CanConnectToRoad(TileIndex tile, RoadType rt, DiagDirection dir)
Checks whether a road or tram connection can be found when building a new road or tram...
Definition: road_cmd.cpp:810
a flat tile
Definition: slope_type.h:51
Road with paved sidewalks.
Definition: road_map.h:450
uint16 rail_speed
Speed limit of rail (bridges and track)
Definition: tile_cmd.h:66
int z
Height.
Definition: tile_cmd.h:49
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
Road on grass.
Definition: road_map.h:449
void DrawFoundation(TileInfo *ti, Foundation f)
Draw foundation f at tile ti.
Definition: landscape.cpp:472
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Definition: command_type.h:64
Owner owner[4]
Name of the owner(s)
Definition: tile_cmd.h:55
static Corner GetHighestSlopeCorner(Slope s)
Returns the highest corner of a slope (one corner raised or a steep slope).
Definition: slope_func.h:128
Roadside
The possible road side decorations.
Definition: road_map.h:447
TileIndex GetNorthernBridgeEnd(TileIndex t)
Finds the northern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:41
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
Use this function to notify YAPF that track layout (or signal configuration) has change.
Definition: yapf_rail.cpp:644
static bool IsValidRoadType(RoadType rt)
Whether the given roadtype is valid.
Definition: road_func.h:35
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:118
static bool HasRoadWorks(TileIndex t)
Check if a tile has road works.
Definition: road_map.h:483
Date build_date
Date of construction.
Definition: depot_base.h:27
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:123
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
company bankrupts, skip money check, skip vehicle on tile check in some cases
Definition: command_type.h:351
Normal road.
Definition: road_map.h:24
Functions related to (drawing on) viewports.
Pseudo random number generator.
The object is owned by a superuser / goal script.
Definition: company_type.h:29
Right track.
Definition: track_type.h:48
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:61
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:163
SpriteID single_y
single piece of rail in Y direction, without ground
Definition: rail.h:129
static bool IsBridgeAbove(TileIndex t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
Buses, trucks and trams belong to this class.
Definition: roadveh.h:88
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:216
static bool IsOnSnow(TileIndex t)
Check if a road tile has snow/desert.
Definition: road_map.h:429
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold...
Definition: town_cmd.cpp:3329
static void SetCrossingReservation(TileIndex t, bool b)
Set the reservation state of the rail crossing.
Definition: road_map.h:363
struct RailtypeInfo::@40 strings
Strings associated with the rail type.
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
static RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:144
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:45
None of the directions are disallowed.
Definition: road_map.h:256
Full 4-way crossing.
Definition: road_type.h:69
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
#define IsOnDesert
Check if a road tile has snow/desert.
Definition: road_map.h:423
All northbound traffic is disallowed.
Definition: road_map.h:258
Foundation
Enumeration for Foundations.
Definition: slope_type.h:95
Types related to cheating.
TileIndex xy
town center tile
Definition: town.h:56
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
All possible tracks.
Definition: track_type.h:56
void MakeDefaultName(T *obj)
Set the default name for a depot/waypoint.
Definition: town.h:229
TileIndex tile
Tile index.
Definition: tile_cmd.h:48
static uint ApplyPixelFoundationToSlope(Foundation f, Slope *s)
Applies a foundation to a slope.
Definition: landscape.h:131
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:440
The y axis.
static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
Draws details on/around the road.
Definition: road_cmd.cpp:1233
static Owner GetRoadOwner(TileIndex t, RoadType rt)
Get the owner of a specific road type.
Definition: road_map.h:199
StringID owner_type[4]
Type of each owner.
Definition: tile_cmd.h:56
Road with trees on paved sidewalks.
Definition: road_map.h:453
The tile is leveled up to a flat slope.
Definition: slope_type.h:97
RoadTileType
The different types of road tiles.
Definition: road_map.h:23
Level crossing overlay images.
Definition: rail.h:49
Sentinel.
Definition: road_map.h:260
SoundSettings sound
sound effect settings
Header file for things common for tunnels and bridges.
X-axis track.
Definition: track_type.h:43
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:60
#define FIND_FIRST_BIT(x)
Returns the first non-zero bit in a 6-bit value (from right).
static void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
Set the owner of a specific road type.
Definition: road_map.h:220
void DirtyCompanyInfrastructureWindows(CompanyID company)
Redraw all windows with company infrastructure counts.
minimum rating after removing town owned road
Definition: town_type.h:67
static bool HasTownOwnedRoad(TileIndex t)
Checks if given tile has town owned road.
Definition: road_map.h:249
static DiagDirection GetRoadStopDir(TileIndex t)
Gets the direction the road stop entrance points towards.
Definition: station_map.h:258
Entry point for OpenTTD to YAPF&#39;s cache.
StringID GetErrorMessage() const
Returns the error message of a command.
Definition: command_type.h:142
static void DrawRoadBits(TileInfo *ti)
Draw ground sprite and road pieces.
Definition: road_cmd.cpp:1246
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore)
Finds vehicle in tunnel / bridge.
Definition: vehicle.cpp:568
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:180
struct RailtypeInfo::@37 base_sprites
Struct containing the main sprites.
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
DoCommandFlag
List of flags for a command.
Definition: command_type.h:343
static uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles)
Definition: tunnelbridge.h:26
T * Next() const
Get next vehicle in the chain.
The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/...
Definition: tile_cmd.h:38
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
static const uint32 VALID_LEVEL_CROSSING_SLOPES
Constant bitset with safe slopes for building a level crossing.
Definition: slope_type.h:88
bool Succeeded() const
Did this command succeed?
Definition: command_type.h:152
const TileTypeProcs _tile_type_road_procs
Tile callback functions for road tiles.
Definition of base types and functions in a cross-platform compatible way.
static TrackBits GetRailReservationTrackBits(TileIndex t)
Returns the reserved track bits of the tile.
Definition: rail_map.h:195
Road on barren land.
Definition: road_map.h:448
CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build a road depot.
Definition: road_cmd.cpp:1015
uint32 road[ROADTYPE_END]
Count of company owned track bits for each road type.
Definition: company_base.h:32
static const uint BB_HEIGHT_UNDER_BRIDGE
Some values for constructing bounding boxes (BB).
Definition: viewport_type.h:74
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
Definition: map_func.h:260
A number of safeguards to prevent using unsafe methods.
Trams.
Definition: road_type.h:25
bool value
tells if the bool cheat is active or not
Definition: cheat_type.h:20
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:46
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:55
void ShowDepotWindow(TileIndex tile, VehicleType type)
Opens a depot window.
Definition: depot_gui.cpp:1100
The tile has an along Y-axis inclined foundation.
Definition: slope_type.h:99
static Axis GetCrossingRailAxis(TileIndex t)
Get the rail axis of a level crossing.
Definition: road_map.h:307
The vehicle is in a depot.
Definition: roadveh.h:39
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:282
static Axis GetCrossingRoadAxis(TileIndex t)
Get the road axis of a level crossing.
Definition: road_map.h:295
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:18
No road-part is build.
Definition: road_type.h:56
static bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition: slope_func.h:90
Road with street lights on paved sidewalks.
Definition: road_map.h:451
GUI Functions related to companies.
static const uint TUNNELBRIDGE_TRACKBIT_FACTOR
Multiplier for how many regular track bits a tunnel/bridge counts.
Definition: economy_type.h:214
static void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
Set the present road bits for a specific road type.
Definition: road_map.h:151
don&#39;t allow building on water
Definition: command_type.h:348
uint16 max_speed
Maximum speed for vehicles travelling on this rail type.
Definition: rail.h:222
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
North-east part.
Definition: road_type.h:60
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
The tile has no ownership.
Definition: company_type.h:27
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:1785
Base class for all effect vehicles.
SpriteID single_x
single piece of rail in X direction, without ground
Definition: rail.h:128
DiagDirection
Enumeration for diagonal directions.
bit mask containing all &#39;simple&#39; slopes
Definition: slope_type.h:63
bool RoadVehiclesAreBuilt()
Verify whether a road vehicle is available.
Definition: road_cmd.cpp:47
Road vehicle type.
Definition: vehicle_type.h:27
static void ToggleSnow(TileIndex t)
Toggle the snow/desert state of a road tile.
Definition: road_map.h:440
static Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:371
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Definition: town_cmd.cpp:2039
static void DrawRailTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32 total_offset, uint32 newgrf_offset, PaletteID default_palette)
Draw tile sprite sequence on tile with railroad specifics.
Definition: sprite.h:91
StringID railtype
Type of rail on the tile.
Definition: tile_cmd.h:65
RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition: road_map.cpp:35
RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
Clean up unnecessary RoadBits of a planed tile.
Definition: road.cpp:46
Functions related to autoslope.
Functions related to sound.
static DiagDirection GetTunnelBridgeDirection(TileIndex t)
Get the direction pointing to the other end.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
static bool AutoslopeEnabled()
Tests if autoslope is enabled for _current_company.
Definition: autoslope.h:46
static TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
Builds a TrackStatus.
Definition: track_func.h:398
bool build_on_slopes
allow building on slopes
bool Failed() const
Did this command fail?
Definition: command_type.h:161
All directions are disallowed.
Definition: road_map.h:259
void UpdateLevelCrossing(TileIndex tile, bool sound=true)
Sets correct crossing state.
Definition: train_cmd.cpp:1679
static void DrawOrigTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, PaletteID default_palette)
Draw TTD sprite sequence on tile.
Definition: sprite.h:109
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:654
catenary
Definition: transparency.h:32
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:35
void DrawRoadCatenary(const TileInfo *ti, RoadBits tram)
Draws the catenary for the given tile.
Definition: road_cmd.cpp:1198
static void SetRoadside(TileIndex tile, Roadside s)
Set the decorations of a road.
Definition: road_map.h:473
North-west part.
Definition: road_type.h:57
uint8 FindFirstBit(uint32 x)
Search the first set bit in a 32 bit variable.
static bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
South-east part.
Definition: road_type.h:59
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:37
static RoadBits GetCrossingRoadBits(TileIndex tile)
Get the road bits of a level crossing.
Definition: road_map.h:318
bool mod_road_rebuild
roadworks remove unnecessary RoadBits
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:137
The X axis.
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
Definition: company_base.h:130
uint32 rail[RAILTYPE_END]
Count of company owned track bits for each rail type.
Definition: company_base.h:34
Y-axis and direction to south-east.
Definition: track_type.h:77
Construction costs.
Definition: economy_type.h:151
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Town * CalcClosestTownFromTile(TileIndex tile, uint threshold=UINT_MAX)
Return the town closest to the given tile within threshold.
Definition: town_cmd.cpp:3304
static TileIndex GetOtherTunnelBridgeEnd(TileIndex t)
Determines type of the wormhole and returns its other end.
Transport by train.
execute the given command
Definition: command_type.h:345
Slope GetFoundationSlope(TileIndex tile, int *z)
Get slope of a tile on top of a (possible) foundation If a tile does not have a foundation, the function returns the same as GetTileSlope.
Definition: landscape.cpp:424
removing a roadpiece at the edge
Definition: town_type.h:66
removing a roadpiece in the middle
Definition: town_type.h:65
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:196
static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
Calculate the costs for roads on slopes Aside modify the RoadBits to fit on the slopes.
Definition: road_cmd.cpp:418
PalSpriteID ground
Palette and sprite for the ground.
Definition: sprite.h:61
static T KillFirstBit(T value)
Clear the first bit in an integer.
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:61
static bool DrawRoadAsSnowDesert(TileIndex tile, Roadside roadside)
Should the road be drawn as a unpaved snow/desert road? By default, roads are always drawn as unpaved...
Definition: road_cmd.cpp:1186
static bool IsNormalRoadTile(TileIndex t)
Return whether a tile is a normal road tile.
Definition: road_map.h:57
static bool CanBuildDepotByTileh(DiagDirection direction, Slope tileh)
Find out if the slope of the tile is suitable to build a depot of given direction.
Definition: depot_func.h:28
static RoadTileType GetRoadTileType(TileIndex t)
Get the type of the road tile.
Definition: road_map.h:35
GUISettings gui
settings related to the GUI
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
Lower track.
Definition: track_type.h:46
static RoadBits MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:99
Upper track.
Definition: track_type.h:45
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:143
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
Set of callback functions for performing tile operations of a given tile type.
Definition: tile_cmd.h:144
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1580
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
static const PaletteID PALETTE_TO_BARE_LAND
sets colour to bare land stuff for rail, road and crossings
Definition: sprites.h:1568
A town owns the tile, or a town is expanding.
Definition: company_type.h:26
static void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
Make a level crossing.
Definition: road_map.h:578
static bool HasAtMostOneBit(T value)
Test whether value has at most 1 bit set.
static bool IncreaseRoadWorksCounter(TileIndex t)
Increase the progress counter of road works.
Definition: road_map.h:493
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
Cheat magic_bulldozer
dynamite industries, objects
Definition: cheat_type.h:29
Bit sets of the above specified bits.
Definition: tile_cmd.h:36
static bool IsStraightRoad(RoadBits r)
Check if we&#39;ve got a straight road.
Definition: road_func.h:129
Removal of a road owned by the town.
Definition: town.h:152
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:96
The tile has an along X-axis inclined foundation.
Definition: slope_type.h:98
TransportType
Available types of transport.
CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build a long piece of road.
Definition: road_cmd.cpp:835
static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
Get the foundationtype of a RoadBits Slope combination.
Definition: road_cmd.cpp:1148
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
A tile of a station.
Definition: tile_type.h:48
void DrawRailCatenary(const TileInfo *ti)
Draws overhead wires and pylons for electric railways.
Definition: elrail.cpp:564
TownCache cache
Container for all cacheable data.
Definition: town.h:58
Normal rail tile without signals.
Definition: rail_map.h:25
Town data structure.
Definition: town.h:55
bool show_track_reservation
highlight reserved tracks.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
Transport by road vehicle.
Vehicle is not visible.
Definition: vehicle_base.h:32
static uint CountBits(T value)
Counts the number of set bits in a variable.
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:106
Functions related to commands.
Road on grass with road works.
Definition: road_map.h:454
No roadtypes.
Definition: road_type.h:37
remove a single rail track
Definition: command_type.h:181
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition: viewport.cpp:570
byte GetSnowLine()
Get the current snow line, either variable or static.
Definition: landscape.cpp:646
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
static bool HasRailCatenaryDrawn(RailType rt)
Test if we should draw rail catenary.
Definition: elrail_func.h:32
static RoadBits GetOtherRoadBits(TileIndex t, RoadType rt)
Get all RoadBits set on a tile except from the given RoadType.
Definition: road_map.h:128
Date build_date
Date of construction of tile contents.
Definition: tile_cmd.h:57
header file for electrified rail specific functions
static Axis OtherAxis(Axis a)
Select the other axis as provided.
static RoadTypes ComplementRoadTypes(RoadTypes r)
Returns the RoadTypes which are not present in the given RoadTypes.
Definition: road_func.h:70
static const SpriteID SPR_TRAMWAY_BASE
Tramway sprites.
Definition: sprites.h:266
DisallowedRoadDirections
Which directions are disallowed ?
Definition: road_map.h:255
static void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
Make a normal road tile.
Definition: road_map.h:554
ConstructionSettings construction
construction of things in-game
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable...
Definition: window_gui.h:326
Functions that have tunnels and bridges in common.
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:181
static void DrawTile_Road(TileInfo *ti)
Tile callback function for rendering a road tile to the screen.
Definition: road_cmd.cpp:1330
static DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
Gets the disallowed directions.
Definition: road_map.h:271
static bool IsNormalRoad(TileIndex t)
Return whether a tile is a normal road.
Definition: road_map.h:47
static RoadTypes GetRoadTypes(TileIndex t)
Get the present road types of a tile.
Definition: road_map.h:166
static bool AutoslopeCheckForEntranceEdge(TileIndex tile, int z_new, Slope tileh_new, DiagDirection entrance)
Autoslope check for tiles with an entrance on an edge.
Definition: autoslope.h:33
StringID name
Name of this rail type.
Definition: rail.h:167
All southbound traffic is disallowed.
Definition: road_map.h:257
StringID str
Description of the tile.
Definition: tile_cmd.h:54
int32 x_pos
x coordinate.
Definition: vehicle_base.h:268
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition: track_func.h:329
Base of the town class.
static bool IsCrossingBarred(TileIndex t)
Check if the level crossing is barred.
Definition: road_map.h:386
GameCreationSettings game_creation
settings used during the creation of a game (map)
No track build.
Definition: track_type.h:107
Full road along the y-axis (north-west + south-east)
Definition: road_type.h:62
static Track GetCrossingRailTrack(TileIndex tile)
Get the rail track of a level crossing.
Definition: road_map.h:328
int32 y_pos
y coordinate.
Definition: vehicle_base.h:269
Left track.
Definition: track_type.h:47
void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite at a specific world-coordinate relative to the current tile.
Definition: viewport.cpp:547
static bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:61
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the &#39;Square&#39; distance between the two given tiles.
Definition: map.cpp:176
Owner
Enum for all companies/owners.
Definition: company_type.h:20
CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove a long piece of road.
Definition: road_cmd.cpp:940
static DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
VehicleEnterTileStatus
The returned bits of VehicleEnterTile.
Definition: tile_cmd.h:22
static void SetTrackReservation(TileIndex t, TrackBits b)
Sets the reserved track bits of the tile.
Definition: rail_map.h:210
SpriteID sprite
The &#39;real&#39; sprite.
Definition: gfx_type.h:25
static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits pieces, RoadType rt, bool crossing_check, bool town_check=true)
Delete a piece of road.
Definition: road_cmd.cpp:180
static void SetRoadTypes(TileIndex t, RoadTypes rt)
Set the present road types of a tile.
Definition: road_map.h:176
byte road_build_months
fund road reconstruction in action?
Definition: town.h:98
static void SetTownIndex(TileIndex t, TownID index)
Set the town index for a road or house tile.
Definition: town_map.h:36
Date _date
Current date in days (day counter)
Definition: date.cpp:28
X-axis and direction to north-east.
Definition: track_type.h:76
static bool HasGrfMiscBit(GrfMiscBit bit)
Check for grf miscellaneous bits.
Definition: newgrf.h:174
static Direction ReverseDir(Direction d)
Return the reverse of a direction.
The tile/execution is done by "water".
Definition: company_type.h:28
Depot (one entrance)
Definition: road_map.h:26
VehicleTypeByte type
Type of vehicle.
Definition: vehicle_type.h:56
void DrawBridgeMiddle(const TileInfo *ti)
Draw the middle bits of a bridge.
Functions related to effect vehicles.
No track.
Definition: track_type.h:42
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:50
Axis
Allow incrementing of DiagDirDiff variables.
static bool IsRoadDepot(TileIndex t)
Return whether a tile is a road depot.
Definition: road_map.h:89
static RoadBits AxisToRoadBits(Axis a)
Create the road-part which belongs to the given Axis.
Definition: road_func.h:159
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:834
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:116
static bool IsBridge(TileIndex t)
Checks if this is a bridge, instead of a tunnel.
Definition: bridge_map.h:24
Y-axis track.
Definition: track_type.h:44
CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type)
Does the town authority allow the (destructive) action of the current company?
Definition: town_cmd.cpp:3445
An invalid owner.
Definition: company_type.h:31
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3301
void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags)
Changes town rating of the current company.
Definition: town_cmd.cpp:3408
Cheats _cheats
All the cheats.
Definition: cheat.cpp:18
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:306
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201
Base for the NewGRF implementation.