OpenTTD
rail_cmd.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "cmd_helper.h"
12 #include "viewport_func.h"
13 #include "command_func.h"
14 #include "depot_base.h"
16 #include "newgrf_debug.h"
17 #include "newgrf_railtype.h"
18 #include "train.h"
19 #include "autoslope.h"
20 #include "water.h"
21 #include "tunnelbridge_map.h"
22 #include "vehicle_func.h"
23 #include "sound_func.h"
24 #include "tunnelbridge.h"
25 #include "elrail_func.h"
26 #include "town.h"
27 #include "pbs.h"
28 #include "company_base.h"
29 #include "core/backup_type.hpp"
30 #include "date_func.h"
31 #include "strings_func.h"
32 #include "company_gui.h"
33 #include "object_map.h"
34 
35 #include "table/strings.h"
36 #include "table/railtypes.h"
37 #include "table/track_land.h"
38 
39 #include "safeguards.h"
40 
42 typedef std::vector<Train *> TrainList;
43 
44 RailtypeInfo _railtypes[RAILTYPE_END];
45 std::vector<RailType> _sorted_railtypes;
46 RailTypes _railtypes_hidden_mask;
47 
50  SIGNAL_TO_SOUTHWEST,
51  SIGNAL_TO_NORTHEAST,
52  SIGNAL_TO_SOUTHEAST,
53  SIGNAL_TO_NORTHWEST,
54  SIGNAL_TO_EAST,
55  SIGNAL_TO_WEST,
56  SIGNAL_TO_SOUTH,
57  SIGNAL_TO_NORTH,
58 };
59 
64 {
65  assert_compile(lengthof(_original_railtypes) <= lengthof(_railtypes));
66 
67  uint i = 0;
68  for (; i < lengthof(_original_railtypes); i++) _railtypes[i] = _original_railtypes[i];
69 
70  static const RailtypeInfo empty_railtype = {
71  {0,0,0,0,0,0,0,0,0,0,0,0},
72  {0,0,0,0,0,0,0,0,{}},
73  {0,0,0,0,0,0,0,0},
74  {0,0,0,0,0,0},
75  0, RAILTYPES_NONE, RAILTYPES_NONE, 0, 0, 0, RTFB_NONE, 0, 0, 0, 0, 0,
77  {}, {} };
78  for (; i < lengthof(_railtypes); i++) _railtypes[i] = empty_railtype;
79 
80  _railtypes_hidden_mask = RAILTYPES_NONE;
81 }
82 
83 void ResolveRailTypeGUISprites(RailtypeInfo *rti)
84 {
86  if (cursors_base != 0) {
87  rti->gui_sprites.build_ns_rail = cursors_base + 0;
88  rti->gui_sprites.build_x_rail = cursors_base + 1;
89  rti->gui_sprites.build_ew_rail = cursors_base + 2;
90  rti->gui_sprites.build_y_rail = cursors_base + 3;
91  rti->gui_sprites.auto_rail = cursors_base + 4;
92  rti->gui_sprites.build_depot = cursors_base + 5;
93  rti->gui_sprites.build_tunnel = cursors_base + 6;
94  rti->gui_sprites.convert_rail = cursors_base + 7;
95  rti->cursor.rail_ns = cursors_base + 8;
96  rti->cursor.rail_swne = cursors_base + 9;
97  rti->cursor.rail_ew = cursors_base + 10;
98  rti->cursor.rail_nwse = cursors_base + 11;
99  rti->cursor.autorail = cursors_base + 12;
100  rti->cursor.depot = cursors_base + 13;
101  rti->cursor.tunnel = cursors_base + 14;
102  rti->cursor.convert = cursors_base + 15;
103  }
104 
105  /* Array of default GUI signal sprite numbers. */
106  const SpriteID _signal_lookup[2][SIGTYPE_END] = {
107  {SPR_IMG_SIGNAL_ELECTRIC_NORM, SPR_IMG_SIGNAL_ELECTRIC_ENTRY, SPR_IMG_SIGNAL_ELECTRIC_EXIT,
108  SPR_IMG_SIGNAL_ELECTRIC_COMBO, SPR_IMG_SIGNAL_ELECTRIC_PBS, SPR_IMG_SIGNAL_ELECTRIC_PBS_OWAY},
109 
110  {SPR_IMG_SIGNAL_SEMAPHORE_NORM, SPR_IMG_SIGNAL_SEMAPHORE_ENTRY, SPR_IMG_SIGNAL_SEMAPHORE_EXIT,
111  SPR_IMG_SIGNAL_SEMAPHORE_COMBO, SPR_IMG_SIGNAL_SEMAPHORE_PBS, SPR_IMG_SIGNAL_SEMAPHORE_PBS_OWAY},
112  };
113 
114  for (SignalType type = SIGTYPE_NORMAL; type < SIGTYPE_END; type = (SignalType)(type + 1)) {
115  for (SignalVariant var = SIG_ELECTRIC; var <= SIG_SEMAPHORE; var = (SignalVariant)(var + 1)) {
116  SpriteID red = GetCustomSignalSprite(rti, INVALID_TILE, type, var, SIGNAL_STATE_RED, true);
117  SpriteID green = GetCustomSignalSprite(rti, INVALID_TILE, type, var, SIGNAL_STATE_GREEN, true);
118  rti->gui_sprites.signals[type][var][0] = (red != 0) ? red + SIGNAL_TO_SOUTH : _signal_lookup[var][type];
119  rti->gui_sprites.signals[type][var][1] = (green != 0) ? green + SIGNAL_TO_SOUTH : _signal_lookup[var][type] + 1;
120  }
121  }
122 }
123 
130 static bool CompareRailTypes(const RailType &first, const RailType &second)
131 {
133 }
134 
139 {
140  for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
141  RailtypeInfo *rti = &_railtypes[rt];
142  ResolveRailTypeGUISprites(rti);
143  if (HasBit(rti->flags, RTF_HIDDEN)) SetBit(_railtypes_hidden_mask, rt);
144  }
145 
146  _sorted_railtypes.clear();
147  for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
148  if (_railtypes[rt].label != 0 && !HasBit(_railtypes_hidden_mask, rt)) {
149  _sorted_railtypes.push_back(rt);
150  }
151  }
152  std::sort(_sorted_railtypes.begin(), _sorted_railtypes.end(), CompareRailTypes);
153 }
154 
158 RailType AllocateRailType(RailTypeLabel label)
159 {
160  for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
161  RailtypeInfo *rti = &_railtypes[rt];
162 
163  if (rti->label == 0) {
164  /* Set up new rail type */
166  rti->label = label;
167  rti->alternate_labels.clear();
168 
169  /* Make us compatible with ourself. */
170  rti->powered_railtypes = (RailTypes)(1LL << rt);
171  rti->compatible_railtypes = (RailTypes)(1LL << rt);
172 
173  /* We also introduce ourself. */
174  rti->introduces_railtypes = (RailTypes)(1LL << rt);
175 
176  /* Default sort order; order of allocation, but with some
177  * offsets so it's easier for NewGRF to pick a spot without
178  * changing the order of other (original) rail types.
179  * The << is so you can place other railtypes in between the
180  * other railtypes, the 7 is to be able to place something
181  * before the first (default) rail type. */
182  rti->sorting_order = rt << 4 | 7;
183  return rt;
184  }
185  }
186 
187  return INVALID_RAILTYPE;
188 }
189 
190 static const byte _track_sloped_sprites[14] = {
191  14, 15, 22, 13,
192  0, 21, 17, 12,
193  23, 0, 18, 20,
194  19, 16
195 };
196 
197 
198 /* 4
199  * ---------
200  * |\ /|
201  * | \ 1/ |
202  * | \ / |
203  * | \ / |
204  * 16| \ |32
205  * | / \2 |
206  * | / \ |
207  * | / \ |
208  * |/ \|
209  * ---------
210  * 8
211  */
212 
213 
214 
215 /* MAP2 byte: abcd???? => Signal On? Same coding as map3lo
216  * MAP3LO byte: abcd???? => Signal Exists?
217  * a and b are for diagonals, upper and left,
218  * one for each direction. (ie a == NE->SW, b ==
219  * SW->NE, or v.v., I don't know. b and c are
220  * similar for lower and right.
221  * MAP2 byte: ????abcd => Type of ground.
222  * MAP3LO byte: ????abcd => Type of rail.
223  * MAP5: 00abcdef => rail
224  * 01abcdef => rail w/ signals
225  * 10uuuuuu => unused
226  * 11uuuudd => rail depot
227  */
228 
238 {
239  TrackBits rail_bits = TrackToTrackBits(track);
240  return EnsureNoTrainOnTrackBits(tile, rail_bits);
241 }
242 
250 static CommandCost CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags)
251 {
252  if (!IsPlainRail(tile)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
253 
254  /* So, we have a tile with tracks on it (and possibly signals). Let's see
255  * what tracks first */
256  TrackBits current = GetTrackBits(tile); // The current track layout.
257  TrackBits future = current | to_build; // The track layout we want to build.
258 
259  /* Are we really building something new? */
260  if (current == future) {
261  /* Nothing new is being built */
262  return_cmd_error(STR_ERROR_ALREADY_BUILT);
263  }
264 
265  /* Let's see if we may build this */
266  if ((flags & DC_NO_RAIL_OVERLAP) || HasSignals(tile)) {
267  /* If we are not allowed to overlap (flag is on for ai companies or we have
268  * signals on the tile), check that */
269  if (future != TRACK_BIT_HORZ && future != TRACK_BIT_VERT) {
270  return_cmd_error((flags & DC_NO_RAIL_OVERLAP) ? STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION : STR_ERROR_MUST_REMOVE_SIGNALS_FIRST);
271  }
272  }
273  /* Normally, we may overlap and any combination is valid */
274  return CommandCost();
275 }
276 
277 
283  TRACK_BIT_X,
284 
287  TRACK_BIT_Y,
289 
291  TRACK_BIT_Y,
294 
295  TRACK_BIT_X,
298 };
299 
305  TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_LEFT,
306 
309  TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_RIGHT,
311 
313  TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_LEFT,
316 
317  TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_RIGHT,
319  TRACK_BIT_ALL
320 };
321 
330 {
331  if (bits == TRACK_BIT_NONE) return FOUNDATION_NONE;
332 
333  if (IsSteepSlope(tileh)) {
334  /* Test for inclined foundations */
335  if (bits == TRACK_BIT_X) return FOUNDATION_INCLINED_X;
336  if (bits == TRACK_BIT_Y) return FOUNDATION_INCLINED_Y;
337 
338  /* Get higher track */
339  Corner highest_corner = GetHighestSlopeCorner(tileh);
340  TrackBits higher_track = CornerToTrackBits(highest_corner);
341 
342  /* Only higher track? */
343  if (bits == higher_track) return HalftileFoundation(highest_corner);
344 
345  /* Overlap with higher track? */
346  if (TracksOverlap(bits | higher_track)) return FOUNDATION_INVALID;
347 
348  /* either lower track or both higher and lower track */
349  return ((bits & higher_track) != 0 ? FOUNDATION_STEEP_BOTH : FOUNDATION_STEEP_LOWER);
350  } else {
351  if ((~_valid_tracks_without_foundation[tileh] & bits) == 0) return FOUNDATION_NONE;
352 
353  bool valid_on_leveled = ((~_valid_tracks_on_leveled_foundation[tileh] & bits) == 0);
354 
355  Corner track_corner;
356  switch (bits) {
357  case TRACK_BIT_LEFT: track_corner = CORNER_W; break;
358  case TRACK_BIT_LOWER: track_corner = CORNER_S; break;
359  case TRACK_BIT_RIGHT: track_corner = CORNER_E; break;
360  case TRACK_BIT_UPPER: track_corner = CORNER_N; break;
361 
362  case TRACK_BIT_HORZ:
363  if (tileh == SLOPE_N) return HalftileFoundation(CORNER_N);
364  if (tileh == SLOPE_S) return HalftileFoundation(CORNER_S);
365  return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
366 
367  case TRACK_BIT_VERT:
368  if (tileh == SLOPE_W) return HalftileFoundation(CORNER_W);
369  if (tileh == SLOPE_E) return HalftileFoundation(CORNER_E);
370  return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
371 
372  case TRACK_BIT_X:
374  return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
375 
376  case TRACK_BIT_Y:
378  return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
379 
380  default:
381  return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
382  }
383  /* Single diagonal track */
384 
385  /* Track must be at least valid on leveled foundation */
386  if (!valid_on_leveled) return FOUNDATION_INVALID;
387 
388  /* If slope has three raised corners, build leveled foundation */
390 
391  /* If neighboured corners of track_corner are lowered, build halftile foundation */
392  if ((tileh & SlopeWithThreeCornersRaised(OppositeCorner(track_corner))) == SlopeWithOneCornerRaised(track_corner)) return HalftileFoundation(track_corner);
393 
394  /* else special anti-zig-zag foundation */
395  return SpecialRailFoundation(track_corner);
396  }
397 }
398 
399 
409 static CommandCost CheckRailSlope(Slope tileh, TrackBits rail_bits, TrackBits existing, TileIndex tile)
410 {
411  /* don't allow building on the lower side of a coast */
412  if (GetFloodingBehaviour(tile) != FLOOD_NONE) {
413  if (!IsSteepSlope(tileh) && ((~_valid_tracks_on_leveled_foundation[tileh] & (rail_bits | existing)) != 0)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
414  }
415 
416  Foundation f_new = GetRailFoundation(tileh, rail_bits | existing);
417 
418  /* check track/slope combination */
419  if ((f_new == FOUNDATION_INVALID) ||
421  return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
422  }
423 
424  Foundation f_old = GetRailFoundation(tileh, existing);
425  return CommandCost(EXPENSES_CONSTRUCTION, f_new != f_old ? _price[PR_BUILD_FOUNDATION] : (Money)0);
426 }
427 
428 /* Validate functions for rail building */
429 static inline bool ValParamTrackOrientation(Track track)
430 {
431  return IsValidTrack(track);
432 }
433 
443 CommandCost CmdBuildSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
444 {
445  RailType railtype = Extract<RailType, 0, 6>(p1);
446  Track track = Extract<Track, 0, 3>(p2);
448 
449  if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR;
450 
451  Slope tileh = GetTileSlope(tile);
452  TrackBits trackbit = TrackToTrackBits(track);
453 
454  switch (GetTileType(tile)) {
455  case MP_RAILWAY: {
456  CommandCost ret = CheckTileOwnership(tile);
457  if (ret.Failed()) return ret;
458 
459  if (!IsPlainRail(tile)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); // just get appropriate error message
460 
461  if (!IsCompatibleRail(GetRailType(tile), railtype)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
462 
463  ret = CheckTrackCombination(tile, trackbit, flags);
464  if (ret.Succeeded()) ret = EnsureNoTrainOnTrack(tile, track);
465  if (ret.Failed()) return ret;
466 
467  ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
468  if (ret.Failed()) return ret;
469  cost.AddCost(ret);
470 
471  /* If the rail types don't match, try to convert only if engines of
472  * the new rail type are not powered on the present rail type and engines of
473  * the present rail type are powered on the new rail type. */
474  if (GetRailType(tile) != railtype && !HasPowerOnRail(railtype, GetRailType(tile))) {
475  if (HasPowerOnRail(GetRailType(tile), railtype)) {
476  ret = DoCommand(tile, tile, railtype, flags, CMD_CONVERT_RAIL);
477  if (ret.Failed()) return ret;
478  cost.AddCost(ret);
479  } else {
480  return CMD_ERROR;
481  }
482  }
483 
484  if (flags & DC_EXEC) {
485  SetRailGroundType(tile, RAIL_GROUND_BARREN);
486  TrackBits bits = GetTrackBits(tile);
487  SetTrackBits(tile, bits | trackbit);
488  /* Subtract old infrastructure count. */
489  uint pieces = CountBits(bits);
490  if (TracksOverlap(bits)) pieces *= pieces;
491  Company::Get(GetTileOwner(tile))->infrastructure.rail[GetRailType(tile)] -= pieces;
492  /* Add new infrastructure count. */
493  pieces = CountBits(bits | trackbit);
494  if (TracksOverlap(bits | trackbit)) pieces *= pieces;
495  Company::Get(GetTileOwner(tile))->infrastructure.rail[GetRailType(tile)] += pieces;
497  }
498  break;
499  }
500 
501  case MP_ROAD: {
502  /* Level crossings may only be built on these slopes */
503  if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
504 
506  if (ret.Failed()) return ret;
507 
508  if (IsNormalRoad(tile)) {
509  if (HasRoadWorks(tile)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
510 
511  if (GetDisallowedRoadDirections(tile) != DRD_NONE) return_cmd_error(STR_ERROR_CROSSING_ON_ONEWAY_ROAD);
512 
513  if (RailNoLevelCrossings(railtype)) return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_RAIL);
514 
515  RoadType roadtype_road = GetRoadTypeRoad(tile);
516  RoadType roadtype_tram = GetRoadTypeTram(tile);
517 
518  if (roadtype_road != INVALID_ROADTYPE && RoadNoLevelCrossing(roadtype_road)) return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_ROAD);
519  if (roadtype_tram != INVALID_ROADTYPE && RoadNoLevelCrossing(roadtype_tram)) return_cmd_error(STR_ERROR_CROSSING_DISALLOWED_ROAD);
520 
521  RoadBits road = GetRoadBits(tile, RTT_ROAD);
522  RoadBits tram = GetRoadBits(tile, RTT_TRAM);
523  if ((track == TRACK_X && ((road | tram) & ROAD_X) == 0) ||
524  (track == TRACK_Y && ((road | tram) & ROAD_Y) == 0)) {
525  Owner road_owner = GetRoadOwner(tile, RTT_ROAD);
526  Owner tram_owner = GetRoadOwner(tile, RTT_TRAM);
527  /* Disallow breaking end-of-line of someone else
528  * so trams can still reverse on this tile. */
529  if (Company::IsValidID(tram_owner) && HasExactlyOneBit(tram)) {
530  CommandCost ret = CheckOwnership(tram_owner);
531  if (ret.Failed()) return ret;
532  }
533 
534  uint num_new_road_pieces = (road != ROAD_NONE) ? 2 - CountBits(road) : 0;
535  if (num_new_road_pieces > 0) {
536  cost.AddCost(num_new_road_pieces * RoadBuildCost(roadtype_road));
537  }
538 
539  uint num_new_tram_pieces = (tram != ROAD_NONE) ? 2 - CountBits(tram) : 0;
540  if (num_new_tram_pieces > 0) {
541  cost.AddCost(num_new_tram_pieces * RoadBuildCost(roadtype_tram));
542  }
543 
544  if (flags & DC_EXEC) {
545  MakeRoadCrossing(tile, road_owner, tram_owner, _current_company, (track == TRACK_X ? AXIS_Y : AXIS_X), railtype, roadtype_road, roadtype_tram, GetTownIndex(tile));
546  UpdateLevelCrossing(tile, false);
547  Company::Get(_current_company)->infrastructure.rail[railtype] += LEVELCROSSING_TRACKBIT_FACTOR;
549  if (num_new_road_pieces > 0 && Company::IsValidID(road_owner)) {
550  Company::Get(road_owner)->infrastructure.road[roadtype_road] += num_new_road_pieces;
552  }
553  if (num_new_tram_pieces > 0 && Company::IsValidID(tram_owner)) {
554  Company::Get(tram_owner)->infrastructure.road[roadtype_tram] += num_new_tram_pieces;
556  }
557  }
558  break;
559  }
560  }
561 
562  if (IsLevelCrossing(tile) && GetCrossingRailBits(tile) == trackbit) {
563  return_cmd_error(STR_ERROR_ALREADY_BUILT);
564  }
565  FALLTHROUGH;
566  }
567 
568  default: {
569  /* Will there be flat water on the lower halftile? */
570  bool water_ground = IsTileType(tile, MP_WATER) && IsSlopeWithOneCornerRaised(tileh);
571  bool docking = IsPossibleDockingTile(tile) && IsDockingTile(tile);
572 
573  CommandCost ret = CheckRailSlope(tileh, trackbit, TRACK_BIT_NONE, tile);
574  if (ret.Failed()) return ret;
575  cost.AddCost(ret);
576 
577  ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
578  if (ret.Failed()) return ret;
579  cost.AddCost(ret);
580 
581  if (water_ground) {
582  cost.AddCost(-_price[PR_CLEAR_WATER]);
583  cost.AddCost(_price[PR_CLEAR_ROUGH]);
584  }
585 
586  if (flags & DC_EXEC) {
587  MakeRailNormal(tile, _current_company, trackbit, railtype);
588  if (water_ground) {
589  SetRailGroundType(tile, RAIL_GROUND_WATER);
590  SetDockingTile(tile, docking);
591  }
592  Company::Get(_current_company)->infrastructure.rail[railtype]++;
594  }
595  break;
596  }
597  }
598 
599  if (flags & DC_EXEC) {
600  MarkTileDirtyByTile(tile);
602  YapfNotifyTrackLayoutChange(tile, track);
603  }
604 
605  cost.AddCost(RailBuildCost(railtype));
606  return cost;
607 }
608 
618 CommandCost CmdRemoveSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
619 {
620  Track track = Extract<Track, 0, 3>(p2);
622  bool crossing = false;
623 
624  if (!ValParamTrackOrientation(track)) return CMD_ERROR;
625  TrackBits trackbit = TrackToTrackBits(track);
626 
627  /* Need to read tile owner now because it may change when the rail is removed
628  * Also, in case of floods, _current_company != owner
629  * There may be invalid tiletype even in exec run (when removing long track),
630  * so do not call GetTileOwner(tile) in any case here */
631  Owner owner = INVALID_OWNER;
632 
633  Train *v = nullptr;
634 
635  switch (GetTileType(tile)) {
636  case MP_ROAD: {
637  if (!IsLevelCrossing(tile) || GetCrossingRailBits(tile) != trackbit) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
638 
639  if (_current_company != OWNER_WATER) {
640  CommandCost ret = CheckTileOwnership(tile);
641  if (ret.Failed()) return ret;
642  }
643 
644  if (!(flags & DC_BANKRUPT)) {
646  if (ret.Failed()) return ret;
647  }
648 
649  cost.AddCost(RailClearCost(GetRailType(tile)));
650 
651  if (flags & DC_EXEC) {
652  if (HasReservedTracks(tile, trackbit)) {
653  v = GetTrainForReservation(tile, track);
654  if (v != nullptr) FreeTrainTrackReservation(v);
655  }
656 
657  owner = GetTileOwner(tile);
658  Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR;
660  MakeRoadNormal(tile, GetCrossingRoadBits(tile), GetRoadTypeRoad(tile), GetRoadTypeTram(tile), GetTownIndex(tile), GetRoadOwner(tile, RTT_ROAD), GetRoadOwner(tile, RTT_TRAM));
661  DeleteNewGRFInspectWindow(GSF_RAILTYPES, tile);
662  }
663  break;
664  }
665 
666  case MP_RAILWAY: {
667  TrackBits present;
668  /* There are no rails present at depots. */
669  if (!IsPlainRail(tile)) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
670 
671  if (_current_company != OWNER_WATER) {
672  CommandCost ret = CheckTileOwnership(tile);
673  if (ret.Failed()) return ret;
674  }
675 
676  CommandCost ret = EnsureNoTrainOnTrack(tile, track);
677  if (ret.Failed()) return ret;
678 
679  present = GetTrackBits(tile);
680  if ((present & trackbit) == 0) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
681  if (present == (TRACK_BIT_X | TRACK_BIT_Y)) crossing = true;
682 
683  cost.AddCost(RailClearCost(GetRailType(tile)));
684 
685  /* Charge extra to remove signals on the track, if they are there */
686  if (HasSignalOnTrack(tile, track)) {
687  cost.AddCost(DoCommand(tile, track, 0, flags, CMD_REMOVE_SIGNALS));
688  }
689 
690  if (flags & DC_EXEC) {
691  if (HasReservedTracks(tile, trackbit)) {
692  v = GetTrainForReservation(tile, track);
693  if (v != nullptr) FreeTrainTrackReservation(v);
694  }
695 
696  owner = GetTileOwner(tile);
697 
698  /* Subtract old infrastructure count. */
699  uint pieces = CountBits(present);
700  if (TracksOverlap(present)) pieces *= pieces;
701  Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= pieces;
702  /* Add new infrastructure count. */
703  present ^= trackbit;
704  pieces = CountBits(present);
705  if (TracksOverlap(present)) pieces *= pieces;
706  Company::Get(owner)->infrastructure.rail[GetRailType(tile)] += pieces;
708 
709  if (present == 0) {
710  Slope tileh = GetTileSlope(tile);
711  /* If there is flat water on the lower halftile, convert the tile to shore so the water remains */
712  if (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh)) {
713  bool docking = IsDockingTile(tile);
714  MakeShore(tile);
715  SetDockingTile(tile, docking);
716  } else {
717  DoClearSquare(tile);
718  }
719  DeleteNewGRFInspectWindow(GSF_RAILTYPES, tile);
720  } else {
721  SetTrackBits(tile, present);
722  SetTrackReservation(tile, GetRailReservationTrackBits(tile) & present);
723  }
724  }
725  break;
726  }
727 
728  default: return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
729  }
730 
731  if (flags & DC_EXEC) {
732  /* if we got that far, 'owner' variable is set correctly */
733  assert(Company::IsValidID(owner));
734 
735  MarkTileDirtyByTile(tile);
736  if (crossing) {
737  /* crossing is set when only TRACK_BIT_X and TRACK_BIT_Y are set. As we
738  * are removing one of these pieces, we'll need to update signals for
739  * both directions explicitly, as after the track is removed it won't
740  * 'connect' with the other piece. */
741  AddTrackToSignalBuffer(tile, TRACK_X, owner);
742  AddTrackToSignalBuffer(tile, TRACK_Y, owner);
745  } else {
746  AddTrackToSignalBuffer(tile, track, owner);
747  YapfNotifyTrackLayoutChange(tile, track);
748  }
749 
750  if (v != nullptr) TryPathReserve(v, true);
751  }
752 
753  return cost;
754 }
755 
756 
765 {
766  assert(IsPlainRailTile(t));
767 
768  bool flooded = false;
769  if (GetRailGroundType(t) == RAIL_GROUND_WATER) return flooded;
770 
771  Slope tileh = GetTileSlope(t);
772  TrackBits rail_bits = GetTrackBits(t);
773 
774  if (IsSlopeWithOneCornerRaised(tileh)) {
776 
777  TrackBits to_remove = lower_track & rail_bits;
778  if (to_remove != 0) {
779  Backup<CompanyID> cur_company(_current_company, OWNER_WATER, FILE_LINE);
780  flooded = DoCommand(t, 0, FIND_FIRST_BIT(to_remove), DC_EXEC, CMD_REMOVE_SINGLE_RAIL).Succeeded();
781  cur_company.Restore();
782  if (!flooded) return flooded; // not yet floodable
783  rail_bits = rail_bits & ~to_remove;
784  if (rail_bits == 0) {
785  MakeShore(t);
787  return flooded;
788  }
789  }
790 
791  if (IsNonContinuousFoundation(GetRailFoundation(tileh, rail_bits))) {
792  flooded = true;
793  SetRailGroundType(t, RAIL_GROUND_WATER);
795  }
796  } else {
797  /* Make shore on steep slopes and 'three-corners-raised'-slopes. */
798  if (ApplyFoundationToSlope(GetRailFoundation(tileh, rail_bits), &tileh) == 0) {
799  if (IsSteepSlope(tileh) || IsSlopeWithThreeCornersRaised(tileh)) {
800  flooded = true;
801  SetRailGroundType(t, RAIL_GROUND_WATER);
803  }
804  }
805  }
806  return flooded;
807 }
808 
809 static const TileIndexDiffC _trackdelta[] = {
810  { -1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, 1 },
811  { 0, 0 },
812  { 0, 0 },
813  { 1, 0 }, { 0, -1 }, { 0, -1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
814  { 0, 0 },
815  { 0, 0 }
816 };
817 
818 
819 static CommandCost ValidateAutoDrag(Trackdir *trackdir, TileIndex start, TileIndex end)
820 {
821  int x = TileX(start);
822  int y = TileY(start);
823  int ex = TileX(end);
824  int ey = TileY(end);
825 
826  if (!ValParamTrackOrientation(TrackdirToTrack(*trackdir))) return CMD_ERROR;
827 
828  /* calculate delta x,y from start to end tile */
829  int dx = ex - x;
830  int dy = ey - y;
831 
832  /* calculate delta x,y for the first direction */
833  int trdx = _trackdelta[*trackdir].x;
834  int trdy = _trackdelta[*trackdir].y;
835 
836  if (!IsDiagonalTrackdir(*trackdir)) {
837  trdx += _trackdelta[*trackdir ^ 1].x;
838  trdy += _trackdelta[*trackdir ^ 1].y;
839  }
840 
841  /* validate the direction */
842  while ((trdx <= 0 && dx > 0) ||
843  (trdx >= 0 && dx < 0) ||
844  (trdy <= 0 && dy > 0) ||
845  (trdy >= 0 && dy < 0)) {
846  if (!HasBit(*trackdir, 3)) { // first direction is invalid, try the other
847  SetBit(*trackdir, 3); // reverse the direction
848  trdx = -trdx;
849  trdy = -trdy;
850  } else { // other direction is invalid too, invalid drag
851  return CMD_ERROR;
852  }
853  }
854 
855  /* (for diagonal tracks, this is already made sure of by above test), but:
856  * for non-diagonal tracks, check if the start and end tile are on 1 line */
857  if (!IsDiagonalTrackdir(*trackdir)) {
858  trdx = _trackdelta[*trackdir].x;
859  trdy = _trackdelta[*trackdir].y;
860  if (abs(dx) != abs(dy) && abs(dx) + abs(trdy) != abs(dy) + abs(trdx)) return CMD_ERROR;
861  }
862 
863  return CommandCost();
864 }
865 
879 static CommandCost CmdRailTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
880 {
882  Track track = Extract<Track, 6, 3>(p2);
883  bool remove = HasBit(p2, 9);
884  RailType railtype = Extract<RailType, 0, 6>(p2);
885 
886  if ((!remove && !ValParamRailtype(railtype)) || !ValParamTrackOrientation(track)) return CMD_ERROR;
887  if (p1 >= MapSize()) return CMD_ERROR;
888  TileIndex end_tile = p1;
889  Trackdir trackdir = TrackToTrackdir(track);
890 
891  CommandCost ret = ValidateAutoDrag(&trackdir, tile, end_tile);
892  if (ret.Failed()) return ret;
893 
894  bool had_success = false;
895  CommandCost last_error = CMD_ERROR;
896  for (;;) {
897  CommandCost ret = DoCommand(tile, remove ? 0 : railtype, TrackdirToTrack(trackdir), flags, remove ? CMD_REMOVE_SINGLE_RAIL : CMD_BUILD_SINGLE_RAIL);
898 
899  if (ret.Failed()) {
900  last_error = ret;
901  if (last_error.GetErrorMessage() != STR_ERROR_ALREADY_BUILT && !remove) {
902  if (HasBit(p2, 10)) return last_error;
903  break;
904  }
905 
906  /* Ownership errors are more important. */
907  if (last_error.GetErrorMessage() == STR_ERROR_OWNED_BY && remove) break;
908  } else {
909  had_success = true;
910  total_cost.AddCost(ret);
911  }
912 
913  if (tile == end_tile) break;
914 
915  tile += ToTileIndexDiff(_trackdelta[trackdir]);
916 
917  /* toggle railbit for the non-diagonal tracks */
918  if (!IsDiagonalTrackdir(trackdir)) ToggleBit(trackdir, 0);
919  }
920 
921  if (had_success) return total_cost;
922  return last_error;
923 }
924 
939 CommandCost CmdBuildRailroadTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
940 {
941  return CmdRailTrackHelper(tile, flags, p1, ClrBit(p2, 9), text);
942 }
943 
958 CommandCost CmdRemoveRailroadTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
959 {
960  return CmdRailTrackHelper(tile, flags, p1, SetBit(p2, 9), text);
961 }
962 
975 CommandCost CmdBuildTrainDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
976 {
977  /* check railtype and valid direction for depot (0 through 3), 4 in total */
978  RailType railtype = Extract<RailType, 0, 6>(p1);
979  if (!ValParamRailtype(railtype)) return CMD_ERROR;
980 
981  Slope tileh = GetTileSlope(tile);
982 
983  DiagDirection dir = Extract<DiagDirection, 0, 2>(p2);
984 
986 
987  /* Prohibit construction if
988  * The tile is non-flat AND
989  * 1) build-on-slopes is disabled
990  * 2) the tile is steep i.e. spans two height levels
991  * 3) the exit points in the wrong direction
992  */
993 
994  if (tileh != SLOPE_FLAT) {
996  return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
997  }
998  cost.AddCost(_price[PR_BUILD_FOUNDATION]);
999  }
1000 
1001  cost.AddCost(DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR));
1002  if (cost.Failed()) return cost;
1003 
1004  if (IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
1005 
1006  if (!Depot::CanAllocateItem()) return CMD_ERROR;
1007 
1008  if (flags & DC_EXEC) {
1009  Depot *d = new Depot(tile);
1010  d->build_date = _date;
1011 
1012  MakeRailDepot(tile, _current_company, d->index, dir, railtype);
1013  MarkTileDirtyByTile(tile);
1014  MakeDefaultName(d);
1015 
1016  Company::Get(_current_company)->infrastructure.rail[railtype]++;
1018 
1021  }
1022 
1023  cost.AddCost(_price[PR_BUILD_DEPOT_TRAIN]);
1024  cost.AddCost(RailBuildCost(railtype));
1025  return cost;
1026 }
1027 
1049 CommandCost CmdBuildSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1050 {
1051  Track track = Extract<Track, 0, 3>(p1);
1052  bool ctrl_pressed = HasBit(p1, 3); // was the CTRL button pressed
1053  SignalVariant sigvar = (ctrl_pressed ^ HasBit(p1, 4)) ? SIG_SEMAPHORE : SIG_ELECTRIC; // the signal variant of the new signal
1054  SignalType sigtype = Extract<SignalType, 5, 3>(p1); // the signal type of the new signal
1055  bool convert_signal = HasBit(p1, 8); // convert button pressed
1056  SignalType cycle_start = Extract<SignalType, 9, 3>(p1);
1057  SignalType cycle_stop = Extract<SignalType, 12, 3>(p1);
1058  uint num_dir_cycle = GB(p1, 15, 2);
1059 
1060  if (sigtype > SIGTYPE_LAST) return CMD_ERROR;
1061  if (cycle_start > cycle_stop || cycle_stop > SIGTYPE_LAST) return CMD_ERROR;
1062 
1063  /* You can only build signals on plain rail tiles, and the selected track must exist */
1064  if (!ValParamTrackOrientation(track) || !IsPlainRailTile(tile) ||
1065  !HasTrack(tile, track)) {
1066  return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
1067  }
1068  /* Protect against invalid signal copying */
1069  if (p2 != 0 && (p2 & SignalOnTrack(track)) == 0) return CMD_ERROR;
1070 
1071  CommandCost ret = CheckTileOwnership(tile);
1072  if (ret.Failed()) return ret;
1073 
1074  /* See if this is a valid track combination for signals (no overlap) */
1075  if (TracksOverlap(GetTrackBits(tile))) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
1076 
1077  /* In case we don't want to change an existing signal, return without error. */
1078  if (HasBit(p1, 17) && HasSignalOnTrack(tile, track)) return CommandCost();
1079 
1080  /* you can not convert a signal if no signal is on track */
1081  if (convert_signal && !HasSignalOnTrack(tile, track)) return_cmd_error(STR_ERROR_THERE_ARE_NO_SIGNALS);
1082 
1083  CommandCost cost;
1084  if (!HasSignalOnTrack(tile, track)) {
1085  /* build new signals */
1086  cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS]);
1087  } else {
1088  if (p2 != 0 && sigvar != GetSignalVariant(tile, track)) {
1089  /* convert signals <-> semaphores */
1090  cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS] + _price[PR_CLEAR_SIGNALS]);
1091 
1092  } else if (convert_signal) {
1093  /* convert button pressed */
1094  if (ctrl_pressed || GetSignalVariant(tile, track) != sigvar) {
1095  /* convert electric <-> semaphore */
1096  cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS] + _price[PR_CLEAR_SIGNALS]);
1097  } else {
1098  /* it is free to change signal type: normal-pre-exit-combo */
1099  cost = CommandCost();
1100  }
1101 
1102  } else {
1103  /* it is free to change orientation/pre-exit-combo signals */
1104  cost = CommandCost();
1105  }
1106  }
1107 
1108  if (flags & DC_EXEC) {
1109  Train *v = nullptr;
1110  /* The new/changed signal could block our path. As this can lead to
1111  * stale reservations, we clear the path reservation here and try
1112  * to redo it later on. */
1113  if (HasReservedTracks(tile, TrackToTrackBits(track))) {
1114  v = GetTrainForReservation(tile, track);
1115  if (v != nullptr) FreeTrainTrackReservation(v);
1116  }
1117 
1118  if (!HasSignals(tile)) {
1119  /* there are no signals at all on this tile yet */
1120  SetHasSignals(tile, true);
1121  SetSignalStates(tile, 0xF); // all signals are on
1122  SetPresentSignals(tile, 0); // no signals built by default
1123  SetSignalType(tile, track, sigtype);
1124  SetSignalVariant(tile, track, sigvar);
1125  }
1126 
1127  /* Subtract old signal infrastructure count. */
1128  Company::Get(GetTileOwner(tile))->infrastructure.signal -= CountBits(GetPresentSignals(tile));
1129 
1130  if (p2 == 0) {
1131  if (!HasSignalOnTrack(tile, track)) {
1132  /* build new signals */
1133  SetPresentSignals(tile, GetPresentSignals(tile) | (IsPbsSignal(sigtype) ? KillFirstBit(SignalOnTrack(track)) : SignalOnTrack(track)));
1134  SetSignalType(tile, track, sigtype);
1135  SetSignalVariant(tile, track, sigvar);
1136  while (num_dir_cycle-- > 0) CycleSignalSide(tile, track);
1137  } else {
1138  if (convert_signal) {
1139  /* convert signal button pressed */
1140  if (ctrl_pressed) {
1141  /* toggle the present signal variant: SIG_ELECTRIC <-> SIG_SEMAPHORE */
1142  SetSignalVariant(tile, track, (GetSignalVariant(tile, track) == SIG_ELECTRIC) ? SIG_SEMAPHORE : SIG_ELECTRIC);
1143  /* Query current signal type so the check for PBS signals below works. */
1144  sigtype = GetSignalType(tile, track);
1145  } else {
1146  /* convert the present signal to the chosen type and variant */
1147  SetSignalType(tile, track, sigtype);
1148  SetSignalVariant(tile, track, sigvar);
1149  if (IsPbsSignal(sigtype) && (GetPresentSignals(tile) & SignalOnTrack(track)) == SignalOnTrack(track)) {
1150  SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | KillFirstBit(SignalOnTrack(track)));
1151  }
1152  }
1153 
1154  } else if (ctrl_pressed) {
1155  /* cycle between cycle_start and cycle_end */
1156  sigtype = (SignalType)(GetSignalType(tile, track) + 1);
1157 
1158  if (sigtype < cycle_start || sigtype > cycle_stop) sigtype = cycle_start;
1159 
1160  SetSignalType(tile, track, sigtype);
1161  if (IsPbsSignal(sigtype) && (GetPresentSignals(tile) & SignalOnTrack(track)) == SignalOnTrack(track)) {
1162  SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | KillFirstBit(SignalOnTrack(track)));
1163  }
1164  } else {
1165  /* cycle the signal side: both -> left -> right -> both -> ... */
1166  CycleSignalSide(tile, track);
1167  /* Query current signal type so the check for PBS signals below works. */
1168  sigtype = GetSignalType(tile, track);
1169  }
1170  }
1171  } else {
1172  /* If CmdBuildManySignals is called with copying signals, just copy the
1173  * direction of the first signal given as parameter by CmdBuildManySignals */
1174  SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | (p2 & SignalOnTrack(track)));
1175  SetSignalVariant(tile, track, sigvar);
1176  SetSignalType(tile, track, sigtype);
1177  }
1178 
1179  /* Add new signal infrastructure count. */
1180  Company::Get(GetTileOwner(tile))->infrastructure.signal += CountBits(GetPresentSignals(tile));
1182 
1183  if (IsPbsSignal(sigtype)) {
1184  /* PBS signals should show red unless they are on reserved tiles without a train. */
1185  uint mask = GetPresentSignals(tile) & SignalOnTrack(track);
1186  SetSignalStates(tile, (GetSignalStates(tile) & ~mask) | ((HasBit(GetRailReservationTrackBits(tile), track) && EnsureNoVehicleOnGround(tile).Succeeded() ? UINT_MAX : 0) & mask));
1187  }
1188  MarkTileDirtyByTile(tile);
1190  YapfNotifyTrackLayoutChange(tile, track);
1191  if (v != nullptr) {
1192  /* Extend the train's path if it's not stopped or loading, or not at a safe position. */
1193  if (!(((v->vehstatus & VS_STOPPED) && v->cur_speed == 0) || v->current_order.IsType(OT_LOADING)) ||
1195  TryPathReserve(v, true);
1196  }
1197  }
1198  }
1199 
1200  return cost;
1201 }
1202 
1203 static bool CheckSignalAutoFill(TileIndex &tile, Trackdir &trackdir, int &signal_ctr, bool remove)
1204 {
1205  tile = AddTileIndexDiffCWrap(tile, _trackdelta[trackdir]);
1206  if (tile == INVALID_TILE) return false;
1207 
1208  /* Check for track bits on the new tile */
1210 
1211  if (TracksOverlap(TrackdirBitsToTrackBits(trackdirbits))) return false;
1212  trackdirbits &= TrackdirReachesTrackdirs(trackdir);
1213 
1214  /* No track bits, must stop */
1215  if (trackdirbits == TRACKDIR_BIT_NONE) return false;
1216 
1217  /* Get the first track dir */
1218  trackdir = RemoveFirstTrackdir(&trackdirbits);
1219 
1220  /* Any left? It's a junction so we stop */
1221  if (trackdirbits != TRACKDIR_BIT_NONE) return false;
1222 
1223  switch (GetTileType(tile)) {
1224  case MP_RAILWAY:
1225  if (IsRailDepot(tile)) return false;
1226  if (!remove && HasSignalOnTrack(tile, TrackdirToTrack(trackdir))) return false;
1227  signal_ctr++;
1228  if (IsDiagonalTrackdir(trackdir)) {
1229  signal_ctr++;
1230  /* Ensure signal_ctr even so X and Y pieces get signals */
1231  ClrBit(signal_ctr, 0);
1232  }
1233  return true;
1234 
1235  case MP_ROAD:
1236  if (!IsLevelCrossing(tile)) return false;
1237  signal_ctr += 2;
1238  return true;
1239 
1240  case MP_TUNNELBRIDGE: {
1241  TileIndex orig_tile = tile; // backup old value
1242 
1243  if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) return false;
1244  if (GetTunnelBridgeDirection(tile) != TrackdirToExitdir(trackdir)) return false;
1245 
1246  /* Skip to end of tunnel or bridge
1247  * note that tile is a parameter by reference, so it must be updated */
1248  tile = GetOtherTunnelBridgeEnd(tile);
1249 
1250  signal_ctr += (GetTunnelBridgeLength(orig_tile, tile) + 2) * 2;
1251  return true;
1252  }
1253 
1254  default: return false;
1255  }
1256 }
1257 
1275 static CommandCost CmdSignalTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1276 {
1277  CommandCost total_cost(EXPENSES_CONSTRUCTION);
1278  TileIndex start_tile = tile;
1279 
1280  Track track = Extract<Track, 0, 3>(p2);
1281  bool mode = HasBit(p2, 3);
1282  bool semaphores = HasBit(p2, 4);
1283  bool remove = HasBit(p2, 5);
1284  bool autofill = HasBit(p2, 6);
1285  bool minimise_gaps = HasBit(p2, 10);
1286  byte signal_density = GB(p2, 24, 8);
1287 
1288  if (p1 >= MapSize() || !ValParamTrackOrientation(track)) return CMD_ERROR;
1289  TileIndex end_tile = p1;
1290  if (signal_density == 0 || signal_density > 20) return CMD_ERROR;
1291 
1292  if (!IsPlainRailTile(tile)) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
1293 
1294  /* for vertical/horizontal tracks, double the given signals density
1295  * since the original amount will be too dense (shorter tracks) */
1296  signal_density *= 2;
1297 
1298  Trackdir trackdir = TrackToTrackdir(track);
1299  CommandCost ret = ValidateAutoDrag(&trackdir, tile, end_tile);
1300  if (ret.Failed()) return ret;
1301 
1302  track = TrackdirToTrack(trackdir); // trackdir might have changed, keep track in sync
1303  Trackdir start_trackdir = trackdir;
1304 
1305  /* Must start on a valid track to be able to avoid loops */
1306  if (!HasTrack(tile, track)) return CMD_ERROR;
1307 
1308  SignalType sigtype = (SignalType)GB(p2, 7, 3);
1309  if (sigtype > SIGTYPE_LAST) return CMD_ERROR;
1310 
1311  byte signals;
1312  /* copy the signal-style of the first rail-piece if existing */
1313  if (HasSignalOnTrack(tile, track)) {
1314  signals = GetPresentSignals(tile) & SignalOnTrack(track);
1315  assert(signals != 0);
1316 
1317  /* copy signal/semaphores style (independent of CTRL) */
1318  semaphores = GetSignalVariant(tile, track) != SIG_ELECTRIC;
1319 
1320  sigtype = GetSignalType(tile, track);
1321  /* Don't but copy entry or exit-signal type */
1322  if (sigtype == SIGTYPE_ENTRY || sigtype == SIGTYPE_EXIT) sigtype = SIGTYPE_NORMAL;
1323  } else { // no signals exist, drag a two-way signal stretch
1324  signals = IsPbsSignal(sigtype) ? SignalAlongTrackdir(trackdir) : SignalOnTrack(track);
1325  }
1326 
1327  byte signal_dir = 0;
1328  if (signals & SignalAlongTrackdir(trackdir)) SetBit(signal_dir, 0);
1329  if (signals & SignalAgainstTrackdir(trackdir)) SetBit(signal_dir, 1);
1330 
1331  /* signal_ctr - amount of tiles already processed
1332  * last_used_ctr - amount of tiles before previously placed signal
1333  * signals_density - setting to put signal on every Nth tile (double space on |, -- tracks)
1334  * last_suitable_ctr - amount of tiles before last possible signal place
1335  * last_suitable_tile - last tile where it is possible to place a signal
1336  * last_suitable_trackdir - trackdir of the last tile
1337  **********
1338  * trackdir - trackdir to build with autorail
1339  * semaphores - semaphores or signals
1340  * signals - is there a signal/semaphore on the first tile, copy its style (two-way/single-way)
1341  * and convert all others to semaphore/signal
1342  * remove - 1 remove signals, 0 build signals */
1343  int signal_ctr = 0;
1344  int last_used_ctr = INT_MIN; // initially INT_MIN to force building/removing at the first tile
1345  int last_suitable_ctr = 0;
1346  TileIndex last_suitable_tile = INVALID_TILE;
1347  Trackdir last_suitable_trackdir = INVALID_TRACKDIR;
1348  CommandCost last_error = CMD_ERROR;
1349  bool had_success = false;
1350  for (;;) {
1351  /* only build/remove signals with the specified density */
1352  if (remove || minimise_gaps || signal_ctr % signal_density == 0) {
1353  uint32 p1 = GB(TrackdirToTrack(trackdir), 0, 3);
1354  SB(p1, 3, 1, mode);
1355  SB(p1, 4, 1, semaphores);
1356  SB(p1, 5, 3, sigtype);
1357  if (!remove && signal_ctr == 0) SetBit(p1, 17);
1358 
1359  /* Pick the correct orientation for the track direction */
1360  signals = 0;
1361  if (HasBit(signal_dir, 0)) signals |= SignalAlongTrackdir(trackdir);
1362  if (HasBit(signal_dir, 1)) signals |= SignalAgainstTrackdir(trackdir);
1363 
1364  /* Test tiles in between for suitability as well if minimising gaps. */
1365  bool test_only = !remove && minimise_gaps && signal_ctr < (last_used_ctr + signal_density);
1366  CommandCost ret = DoCommand(tile, p1, signals, test_only ? flags & ~DC_EXEC : flags, remove ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
1367 
1368  if (ret.Succeeded()) {
1369  /* Remember last track piece where we can place a signal. */
1370  last_suitable_ctr = signal_ctr;
1371  last_suitable_tile = tile;
1372  last_suitable_trackdir = trackdir;
1373  } else if (!test_only && last_suitable_tile != INVALID_TILE) {
1374  /* If a signal can't be placed, place it at the last possible position. */
1375  SB(p1, 0, 3, TrackdirToTrack(last_suitable_trackdir));
1376  ClrBit(p1, 17);
1377 
1378  /* Pick the correct orientation for the track direction. */
1379  signals = 0;
1380  if (HasBit(signal_dir, 0)) signals |= SignalAlongTrackdir(last_suitable_trackdir);
1381  if (HasBit(signal_dir, 1)) signals |= SignalAgainstTrackdir(last_suitable_trackdir);
1382 
1383  ret = DoCommand(last_suitable_tile, p1, signals, flags, remove ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
1384  }
1385 
1386  /* Collect cost. */
1387  if (!test_only) {
1388  /* Be user-friendly and try placing signals as much as possible */
1389  if (ret.Succeeded()) {
1390  had_success = true;
1391  total_cost.AddCost(ret);
1392  last_used_ctr = last_suitable_ctr;
1393  last_suitable_tile = INVALID_TILE;
1394  } else {
1395  /* The "No railway" error is the least important one. */
1396  if (ret.GetErrorMessage() != STR_ERROR_THERE_IS_NO_RAILROAD_TRACK ||
1397  last_error.GetErrorMessage() == INVALID_STRING_ID) {
1398  last_error = ret;
1399  }
1400  }
1401  }
1402  }
1403 
1404  if (autofill) {
1405  if (!CheckSignalAutoFill(tile, trackdir, signal_ctr, remove)) break;
1406 
1407  /* Prevent possible loops */
1408  if (tile == start_tile && trackdir == start_trackdir) break;
1409  } else {
1410  if (tile == end_tile) break;
1411 
1412  tile += ToTileIndexDiff(_trackdelta[trackdir]);
1413  signal_ctr++;
1414 
1415  /* toggle railbit for the non-diagonal tracks (|, -- tracks) */
1416  if (IsDiagonalTrackdir(trackdir)) {
1417  signal_ctr++;
1418  } else {
1419  ToggleBit(trackdir, 0);
1420  }
1421  }
1422  }
1423 
1424  return had_success ? total_cost : last_error;
1425 }
1426 
1445 CommandCost CmdBuildSignalTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1446 {
1447  return CmdSignalTrackHelper(tile, flags, p1, p2, text);
1448 }
1449 
1462 CommandCost CmdRemoveSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1463 {
1464  Track track = Extract<Track, 0, 3>(p1);
1465 
1466  if (!ValParamTrackOrientation(track) || !IsPlainRailTile(tile) || !HasTrack(tile, track)) {
1467  return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
1468  }
1469  if (!HasSignalOnTrack(tile, track)) {
1470  return_cmd_error(STR_ERROR_THERE_ARE_NO_SIGNALS);
1471  }
1472 
1473  /* Only water can remove signals from anyone */
1474  if (_current_company != OWNER_WATER) {
1475  CommandCost ret = CheckTileOwnership(tile);
1476  if (ret.Failed()) return ret;
1477  }
1478 
1479  /* Do it? */
1480  if (flags & DC_EXEC) {
1481  Train *v = nullptr;
1482  if (HasReservedTracks(tile, TrackToTrackBits(track))) {
1483  v = GetTrainForReservation(tile, track);
1484  } else if (IsPbsSignal(GetSignalType(tile, track))) {
1485  /* PBS signal, might be the end of a path reservation. */
1486  Trackdir td = TrackToTrackdir(track);
1487  for (int i = 0; v == nullptr && i < 2; i++, td = ReverseTrackdir(td)) {
1488  /* Only test the active signal side. */
1489  if (!HasSignalOnTrackdir(tile, ReverseTrackdir(td))) continue;
1490  TileIndex next = TileAddByDiagDir(tile, TrackdirToExitdir(td));
1492  if (HasReservedTracks(next, tracks)) {
1494  }
1495  }
1496  }
1497  Company::Get(GetTileOwner(tile))->infrastructure.signal -= CountBits(GetPresentSignals(tile));
1498  SetPresentSignals(tile, GetPresentSignals(tile) & ~SignalOnTrack(track));
1499  Company::Get(GetTileOwner(tile))->infrastructure.signal += CountBits(GetPresentSignals(tile));
1501 
1502  /* removed last signal from tile? */
1503  if (GetPresentSignals(tile) == 0) {
1504  SetSignalStates(tile, 0);
1505  SetHasSignals(tile, false);
1506  SetSignalVariant(tile, INVALID_TRACK, SIG_ELECTRIC); // remove any possible semaphores
1507  }
1508 
1509  AddTrackToSignalBuffer(tile, track, GetTileOwner(tile));
1510  YapfNotifyTrackLayoutChange(tile, track);
1511  if (v != nullptr) TryPathReserve(v, false);
1512 
1513  MarkTileDirtyByTile(tile);
1514  }
1515 
1516  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_SIGNALS]);
1517 }
1518 
1537 CommandCost CmdRemoveSignalTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1538 {
1539  return CmdSignalTrackHelper(tile, flags, p1, SetBit(p2, 5), text); // bit 5 is remove bit
1540 }
1541 
1543 static Vehicle *UpdateTrainPowerProc(Vehicle *v, void *data)
1544 {
1545  if (v->type != VEH_TRAIN) return nullptr;
1546 
1547  TrainList *affected_trains = static_cast<TrainList*>(data);
1548  include(*affected_trains, Train::From(v)->First());
1549 
1550  return nullptr;
1551 }
1552 
1565 CommandCost CmdConvertRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1566 {
1567  RailType totype = Extract<RailType, 0, 6>(p2);
1568  TileIndex area_start = p1;
1569  TileIndex area_end = tile;
1570  bool diagonal = HasBit(p2, 6);
1571 
1572  if (!ValParamRailtype(totype)) return CMD_ERROR;
1573  if (area_start >= MapSize()) return CMD_ERROR;
1574 
1575  TrainList affected_trains;
1576 
1578  CommandCost error = CommandCost(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK); // by default, there is no track to convert.
1579  bool found_convertible_track = false; // whether we actually did convert some track (see bug #7633)
1580 
1581  TileIterator *iter = diagonal ? (TileIterator *)new DiagonalTileIterator(area_start, area_end) : new OrthogonalTileIterator(area_start, area_end);
1582  for (; (tile = *iter) != INVALID_TILE; ++(*iter)) {
1583  TileType tt = GetTileType(tile);
1584 
1585  /* Check if there is any track on tile */
1586  switch (tt) {
1587  case MP_RAILWAY:
1588  break;
1589  case MP_STATION:
1590  if (!HasStationRail(tile)) continue;
1591  break;
1592  case MP_ROAD:
1593  if (!IsLevelCrossing(tile)) continue;
1594  if (RailNoLevelCrossings(totype)) {
1595  error.MakeError(STR_ERROR_CROSSING_DISALLOWED_RAIL);
1596  continue;
1597  }
1598  break;
1599  case MP_TUNNELBRIDGE:
1600  if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) continue;
1601  break;
1602  default: continue;
1603  }
1604 
1605  /* Original railtype we are converting from */
1606  RailType type = GetRailType(tile);
1607 
1608  /* Converting to the same type or converting 'hidden' elrail -> rail */
1609  if (type == totype || (_settings_game.vehicle.disable_elrails && totype == RAILTYPE_RAIL && type == RAILTYPE_ELECTRIC)) continue;
1610 
1611  /* Trying to convert other's rail */
1612  CommandCost ret = CheckTileOwnership(tile);
1613  if (ret.Failed()) {
1614  error = ret;
1615  continue;
1616  }
1617 
1618  std::vector<Train *> vehicles_affected;
1619 
1620  /* Vehicle on the tile when not converting Rail <-> ElRail
1621  * Tunnels and bridges have special check later */
1622  if (tt != MP_TUNNELBRIDGE) {
1623  if (!IsCompatibleRail(type, totype)) {
1625  if (ret.Failed()) {
1626  error = ret;
1627  continue;
1628  }
1629  }
1630  if (flags & DC_EXEC) { // we can safely convert, too
1631  TrackBits reserved = GetReservedTrackbits(tile);
1632  Track track;
1633  while ((track = RemoveFirstTrack(&reserved)) != INVALID_TRACK) {
1634  Train *v = GetTrainForReservation(tile, track);
1635  if (v != nullptr && !HasPowerOnRail(v->railtype, totype)) {
1636  /* No power on new rail type, reroute. */
1638  vehicles_affected.push_back(v);
1639  }
1640  }
1641 
1642  /* Update the company infrastructure counters. */
1643  if (!IsRailStationTile(tile) || !IsStationTileBlocked(tile)) {
1644  Company *c = Company::Get(GetTileOwner(tile));
1645  uint num_pieces = IsLevelCrossingTile(tile) ? LEVELCROSSING_TRACKBIT_FACTOR : 1;
1646  if (IsPlainRailTile(tile)) {
1647  TrackBits bits = GetTrackBits(tile);
1648  num_pieces = CountBits(bits);
1649  if (TracksOverlap(bits)) num_pieces *= num_pieces;
1650  }
1651  c->infrastructure.rail[type] -= num_pieces;
1652  c->infrastructure.rail[totype] += num_pieces;
1654  }
1655 
1656  SetRailType(tile, totype);
1657  MarkTileDirtyByTile(tile);
1658  /* update power of train on this tile */
1659  FindVehicleOnPos(tile, &affected_trains, &UpdateTrainPowerProc);
1660  }
1661  }
1662 
1663  switch (tt) {
1664  case MP_RAILWAY:
1665  switch (GetRailTileType(tile)) {
1666  case RAIL_TILE_DEPOT:
1667  if (flags & DC_EXEC) {
1668  /* notify YAPF about the track layout change */
1670 
1671  /* Update build vehicle window related to this depot */
1674  }
1675  found_convertible_track = true;
1676  cost.AddCost(RailConvertCost(type, totype));
1677  break;
1678 
1679  default: // RAIL_TILE_NORMAL, RAIL_TILE_SIGNALS
1680  if (flags & DC_EXEC) {
1681  /* notify YAPF about the track layout change */
1682  TrackBits tracks = GetTrackBits(tile);
1683  while (tracks != TRACK_BIT_NONE) {
1685  }
1686  }
1687  found_convertible_track = true;
1688  cost.AddCost(RailConvertCost(type, totype) * CountBits(GetTrackBits(tile)));
1689  break;
1690  }
1691  break;
1692 
1693  case MP_TUNNELBRIDGE: {
1694  TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
1695 
1696  /* If both ends of tunnel/bridge are in the range, do not try to convert twice -
1697  * it would cause assert because of different test and exec runs */
1698  if (endtile < tile) {
1699  if (diagonal) {
1700  if (DiagonalTileArea(area_start, area_end).Contains(endtile)) continue;
1701  } else {
1702  if (OrthogonalTileArea(area_start, area_end).Contains(endtile)) continue;
1703  }
1704  }
1705 
1706  /* When not converting rail <-> el. rail, any vehicle cannot be in tunnel/bridge */
1707  if (!IsCompatibleRail(GetRailType(tile), totype)) {
1708  CommandCost ret = TunnelBridgeIsFree(tile, endtile);
1709  if (ret.Failed()) {
1710  error = ret;
1711  continue;
1712  }
1713  }
1714 
1715  if (flags & DC_EXEC) {
1717  if (HasTunnelBridgeReservation(tile)) {
1718  Train *v = GetTrainForReservation(tile, track);
1719  if (v != nullptr && !HasPowerOnRail(v->railtype, totype)) {
1720  /* No power on new rail type, reroute. */
1722  vehicles_affected.push_back(v);
1723  }
1724  }
1725 
1726  /* Update the company infrastructure counters. */
1727  uint num_pieces = (GetTunnelBridgeLength(tile, endtile) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
1728  Company *c = Company::Get(GetTileOwner(tile));
1729  c->infrastructure.rail[GetRailType(tile)] -= num_pieces;
1730  c->infrastructure.rail[totype] += num_pieces;
1732 
1733  SetRailType(tile, totype);
1734  SetRailType(endtile, totype);
1735 
1736  FindVehicleOnPos(tile, &affected_trains, &UpdateTrainPowerProc);
1737  FindVehicleOnPos(endtile, &affected_trains, &UpdateTrainPowerProc);
1738 
1739  YapfNotifyTrackLayoutChange(tile, track);
1740  YapfNotifyTrackLayoutChange(endtile, track);
1741 
1742  if (IsBridge(tile)) {
1743  MarkBridgeDirty(tile);
1744  } else {
1745  MarkTileDirtyByTile(tile);
1746  MarkTileDirtyByTile(endtile);
1747  }
1748  }
1749 
1750  found_convertible_track = true;
1751  cost.AddCost((GetTunnelBridgeLength(tile, endtile) + 2) * RailConvertCost(type, totype));
1752  break;
1753  }
1754 
1755  default: // MP_STATION, MP_ROAD
1756  if (flags & DC_EXEC) {
1757  Track track = ((tt == MP_STATION) ? GetRailStationTrack(tile) : GetCrossingRailTrack(tile));
1758  YapfNotifyTrackLayoutChange(tile, track);
1759  }
1760 
1761  found_convertible_track = true;
1762  cost.AddCost(RailConvertCost(type, totype));
1763  break;
1764  }
1765 
1766  for (uint i = 0; i < vehicles_affected.size(); ++i) {
1767  TryPathReserve(vehicles_affected[i], true);
1768  }
1769  }
1770 
1771  if (flags & DC_EXEC) {
1772  /* Railtype changed, update trains as when entering different track */
1773  for (Train *v : affected_trains) {
1774  v->ConsistChanged(CCF_TRACK);
1775  }
1776  }
1777 
1778  delete iter;
1779  return found_convertible_track ? cost : error;
1780 }
1781 
1782 static CommandCost RemoveTrainDepot(TileIndex tile, DoCommandFlag flags)
1783 {
1784  if (_current_company != OWNER_WATER) {
1785  CommandCost ret = CheckTileOwnership(tile);
1786  if (ret.Failed()) return ret;
1787  }
1788 
1790  if (ret.Failed()) return ret;
1791 
1792  if (flags & DC_EXEC) {
1793  /* read variables before the depot is removed */
1795  Owner owner = GetTileOwner(tile);
1796  Train *v = nullptr;
1797 
1798  if (HasDepotReservation(tile)) {
1800  if (v != nullptr) FreeTrainTrackReservation(v);
1801  }
1802 
1803  Company::Get(owner)->infrastructure.rail[GetRailType(tile)]--;
1805 
1806  delete Depot::GetByTile(tile);
1807  DoClearSquare(tile);
1808  AddSideToSignalBuffer(tile, dir, owner);
1810  if (v != nullptr) TryPathReserve(v, true);
1811  }
1812 
1813  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_TRAIN]);
1814 }
1815 
1816 static CommandCost ClearTile_Track(TileIndex tile, DoCommandFlag flags)
1817 {
1819 
1820  if (flags & DC_AUTO) {
1821  if (!IsTileOwner(tile, _current_company)) {
1822  return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
1823  }
1824 
1825  if (IsPlainRail(tile)) {
1826  return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
1827  } else {
1828  return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
1829  }
1830  }
1831 
1832  switch (GetRailTileType(tile)) {
1833  case RAIL_TILE_SIGNALS:
1834  case RAIL_TILE_NORMAL: {
1835  Slope tileh = GetTileSlope(tile);
1836  /* Is there flat water on the lower halftile that gets cleared expensively? */
1837  bool water_ground = (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh));
1838 
1839  TrackBits tracks = GetTrackBits(tile);
1840  while (tracks != TRACK_BIT_NONE) {
1841  Track track = RemoveFirstTrack(&tracks);
1842  CommandCost ret = DoCommand(tile, 0, track, flags, CMD_REMOVE_SINGLE_RAIL);
1843  if (ret.Failed()) return ret;
1844  cost.AddCost(ret);
1845  }
1846 
1847  /* When bankrupting, don't make water dirty, there could be a ship on lower halftile.
1848  * Same holds for non-companies clearing the tile, e.g. disasters. */
1849  if (water_ground && !(flags & DC_BANKRUPT) && Company::IsValidID(_current_company)) {
1851  if (ret.Failed()) return ret;
1852 
1853  /* The track was removed, and left a coast tile. Now also clear the water. */
1854  if (flags & DC_EXEC) DoClearSquare(tile);
1855  cost.AddCost(_price[PR_CLEAR_WATER]);
1856  }
1857 
1858  return cost;
1859  }
1860 
1861  case RAIL_TILE_DEPOT:
1862  return RemoveTrainDepot(tile, flags);
1863 
1864  default:
1865  return CMD_ERROR;
1866  }
1867 }
1868 
1873 static uint GetSaveSlopeZ(uint x, uint y, Track track)
1874 {
1875  switch (track) {
1876  case TRACK_UPPER: x &= ~0xF; y &= ~0xF; break;
1877  case TRACK_LOWER: x |= 0xF; y |= 0xF; break;
1878  case TRACK_LEFT: x |= 0xF; y &= ~0xF; break;
1879  case TRACK_RIGHT: x &= ~0xF; y |= 0xF; break;
1880  default: break;
1881  }
1882  return GetSlopePixelZ(x, y);
1883 }
1884 
1885 static void DrawSingleSignal(TileIndex tile, const RailtypeInfo *rti, Track track, SignalState condition, SignalOffsets image, uint pos)
1886 {
1887  bool side;
1889  case 0: side = false; break; // left
1890  case 2: side = true; break; // right
1891  default: side = _settings_game.vehicle.road_side != 0; break; // driving side
1892  }
1893  static const Point SignalPositions[2][12] = {
1894  { // Signals on the left side
1895  /* LEFT LEFT RIGHT RIGHT UPPER UPPER */
1896  { 8, 5}, {14, 1}, { 1, 14}, { 9, 11}, { 1, 0}, { 3, 10},
1897  /* LOWER LOWER X X Y Y */
1898  {11, 4}, {14, 14}, {11, 3}, { 4, 13}, { 3, 4}, {11, 13}
1899  }, { // Signals on the right side
1900  /* LEFT LEFT RIGHT RIGHT UPPER UPPER */
1901  {14, 1}, {12, 10}, { 4, 6}, { 1, 14}, {10, 4}, { 0, 1},
1902  /* LOWER LOWER X X Y Y */
1903  {14, 14}, { 5, 12}, {11, 13}, { 4, 3}, {13, 4}, { 3, 11}
1904  }
1905  };
1906 
1907  uint x = TileX(tile) * TILE_SIZE + SignalPositions[side][pos].x;
1908  uint y = TileY(tile) * TILE_SIZE + SignalPositions[side][pos].y;
1909 
1910  SignalType type = GetSignalType(tile, track);
1911  SignalVariant variant = GetSignalVariant(tile, track);
1912 
1913  SpriteID sprite = GetCustomSignalSprite(rti, tile, type, variant, condition);
1914  if (sprite != 0) {
1915  sprite += image;
1916  } else {
1917  /* Normal electric signals are stored in a different sprite block than all other signals. */
1918  sprite = (type == SIGTYPE_NORMAL && variant == SIG_ELECTRIC) ? SPR_ORIGINAL_SIGNALS_BASE : SPR_SIGNALS_BASE - 16;
1919  sprite += type * 16 + variant * 64 + image * 2 + condition + (type > SIGTYPE_LAST_NOPBS ? 64 : 0);
1920  }
1921 
1922  AddSortableSpriteToDraw(sprite, PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, GetSaveSlopeZ(x, y, track));
1923 }
1924 
1925 static uint32 _drawtile_track_palette;
1926 
1927 
1928 
1930 struct FenceOffset {
1932  int x_offs;
1933  int y_offs;
1934  int x_size;
1935  int y_size;
1936 };
1937 
1940  { CORNER_INVALID, 0, 1, 16, 1 }, // RFO_FLAT_X_NW
1941  { CORNER_INVALID, 1, 0, 1, 16 }, // RFO_FLAT_Y_NE
1942  { CORNER_W, 8, 8, 1, 1 }, // RFO_FLAT_LEFT
1943  { CORNER_N, 8, 8, 1, 1 }, // RFO_FLAT_UPPER
1944  { CORNER_INVALID, 0, 1, 16, 1 }, // RFO_SLOPE_SW_NW
1945  { CORNER_INVALID, 1, 0, 1, 16 }, // RFO_SLOPE_SE_NE
1946  { CORNER_INVALID, 0, 1, 16, 1 }, // RFO_SLOPE_NE_NW
1947  { CORNER_INVALID, 1, 0, 1, 16 }, // RFO_SLOPE_NW_NE
1948  { CORNER_INVALID, 0, 15, 16, 1 }, // RFO_FLAT_X_SE
1949  { CORNER_INVALID, 15, 0, 1, 16 }, // RFO_FLAT_Y_SW
1950  { CORNER_E, 8, 8, 1, 1 }, // RFO_FLAT_RIGHT
1951  { CORNER_S, 8, 8, 1, 1 }, // RFO_FLAT_LOWER
1952  { CORNER_INVALID, 0, 15, 16, 1 }, // RFO_SLOPE_SW_SE
1953  { CORNER_INVALID, 15, 0, 1, 16 }, // RFO_SLOPE_SE_SW
1954  { CORNER_INVALID, 0, 15, 16, 1 }, // RFO_SLOPE_NE_SE
1955  { CORNER_INVALID, 15, 0, 1, 16 }, // RFO_SLOPE_NW_SW
1956 };
1957 
1965 static void DrawTrackFence(const TileInfo *ti, SpriteID base_image, uint num_sprites, RailFenceOffset rfo)
1966 {
1967  int z = ti->z;
1968  if (_fence_offsets[rfo].height_ref != CORNER_INVALID) {
1969  z += GetSlopePixelZInCorner(RemoveHalftileSlope(ti->tileh), _fence_offsets[rfo].height_ref);
1970  }
1971  AddSortableSpriteToDraw(base_image + (rfo % num_sprites), _drawtile_track_palette,
1972  ti->x + _fence_offsets[rfo].x_offs,
1973  ti->y + _fence_offsets[rfo].y_offs,
1974  _fence_offsets[rfo].x_size,
1975  _fence_offsets[rfo].y_size,
1976  4, z);
1977 }
1978 
1982 static void DrawTrackFence_NW(const TileInfo *ti, SpriteID base_image, uint num_sprites)
1983 {
1985  if (ti->tileh & SLOPE_NW) rfo = (ti->tileh & SLOPE_W) ? RFO_SLOPE_SW_NW : RFO_SLOPE_NE_NW;
1986  DrawTrackFence(ti, base_image, num_sprites, rfo);
1987 }
1988 
1992 static void DrawTrackFence_SE(const TileInfo *ti, SpriteID base_image, uint num_sprites)
1993 {
1995  if (ti->tileh & SLOPE_SE) rfo = (ti->tileh & SLOPE_S) ? RFO_SLOPE_SW_SE : RFO_SLOPE_NE_SE;
1996  DrawTrackFence(ti, base_image, num_sprites, rfo);
1997 }
1998 
2002 static void DrawTrackFence_NE(const TileInfo *ti, SpriteID base_image, uint num_sprites)
2003 {
2005  if (ti->tileh & SLOPE_NE) rfo = (ti->tileh & SLOPE_E) ? RFO_SLOPE_SE_NE : RFO_SLOPE_NW_NE;
2006  DrawTrackFence(ti, base_image, num_sprites, rfo);
2007 }
2008 
2012 static void DrawTrackFence_SW(const TileInfo *ti, SpriteID base_image, uint num_sprites)
2013 {
2015  if (ti->tileh & SLOPE_SW) rfo = (ti->tileh & SLOPE_S) ? RFO_SLOPE_SE_SW : RFO_SLOPE_NW_SW;
2016  DrawTrackFence(ti, base_image, num_sprites, rfo);
2017 }
2018 
2024 static void DrawTrackDetails(const TileInfo *ti, const RailtypeInfo *rti)
2025 {
2026  /* Base sprite for track fences.
2027  * Note: Halftile slopes only have fences on the upper part. */
2028  uint num_sprites = 0;
2029  SpriteID base_image = GetCustomRailSprite(rti, ti->tile, RTSG_FENCES, IsHalftileSlope(ti->tileh) ? TCX_UPPER_HALFTILE : TCX_NORMAL, &num_sprites);
2030  if (base_image == 0) {
2031  base_image = SPR_TRACK_FENCE_FLAT_X;
2032  num_sprites = 8;
2033  }
2034 
2035  assert(num_sprites > 0);
2036 
2037  switch (GetRailGroundType(ti->tile)) {
2038  case RAIL_GROUND_FENCE_NW: DrawTrackFence_NW(ti, base_image, num_sprites); break;
2039  case RAIL_GROUND_FENCE_SE: DrawTrackFence_SE(ti, base_image, num_sprites); break;
2040  case RAIL_GROUND_FENCE_SENW: DrawTrackFence_NW(ti, base_image, num_sprites);
2041  DrawTrackFence_SE(ti, base_image, num_sprites); break;
2042  case RAIL_GROUND_FENCE_NE: DrawTrackFence_NE(ti, base_image, num_sprites); break;
2043  case RAIL_GROUND_FENCE_SW: DrawTrackFence_SW(ti, base_image, num_sprites); break;
2044  case RAIL_GROUND_FENCE_NESW: DrawTrackFence_NE(ti, base_image, num_sprites);
2045  DrawTrackFence_SW(ti, base_image, num_sprites); break;
2046  case RAIL_GROUND_FENCE_VERT1: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_LEFT); break;
2047  case RAIL_GROUND_FENCE_VERT2: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_RIGHT); break;
2048  case RAIL_GROUND_FENCE_HORIZ1: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_UPPER); break;
2049  case RAIL_GROUND_FENCE_HORIZ2: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_LOWER); break;
2050  case RAIL_GROUND_WATER: {
2051  Corner track_corner;
2052  if (IsHalftileSlope(ti->tileh)) {
2053  /* Steep slope or one-corner-raised slope with halftile foundation */
2054  track_corner = GetHalftileSlopeCorner(ti->tileh);
2055  } else {
2056  /* Three-corner-raised slope */
2058  }
2059  switch (track_corner) {
2060  case CORNER_W: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_LEFT); break;
2061  case CORNER_S: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_LOWER); break;
2062  case CORNER_E: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_RIGHT); break;
2063  case CORNER_N: DrawTrackFence(ti, base_image, num_sprites, RFO_FLAT_UPPER); break;
2064  default: NOT_REACHED();
2065  }
2066  break;
2067  }
2068  default: break;
2069  }
2070 }
2071 
2072 /* SubSprite for drawing the track halftile of 'three-corners-raised'-sloped rail sprites. */
2073 static const int INF = 1000; // big number compared to tilesprite size
2074 static const SubSprite _halftile_sub_sprite[4] = {
2075  { -INF , -INF , 32 - 33, INF }, // CORNER_W, clip 33 pixels from right
2076  { -INF , 0 + 7, INF , INF }, // CORNER_S, clip 7 pixels from top
2077  { -31 + 33, -INF , INF , INF }, // CORNER_E, clip 33 pixels from left
2078  { -INF , -INF , INF , 30 - 23 } // CORNER_N, clip 23 pixels from bottom
2079 };
2080 
2081 static inline void DrawTrackSprite(SpriteID sprite, PaletteID pal, const TileInfo *ti, Slope s)
2082 {
2083  DrawGroundSprite(sprite, pal, nullptr, 0, (ti->tileh & s) ? -8 : 0);
2084 }
2085 
2086 static void DrawTrackBitsOverlay(TileInfo *ti, TrackBits track, const RailtypeInfo *rti)
2087 {
2088  RailGroundType rgt = GetRailGroundType(ti->tile);
2089  Foundation f = GetRailFoundation(ti->tileh, track);
2090  Corner halftile_corner = CORNER_INVALID;
2091 
2092  if (IsNonContinuousFoundation(f)) {
2093  /* Save halftile corner */
2095  /* Draw lower part first */
2096  track &= ~CornerToTrackBits(halftile_corner);
2098  }
2099 
2100  DrawFoundation(ti, f);
2101  /* DrawFoundation modifies ti */
2102 
2103  /* Draw ground */
2104  if (rgt == RAIL_GROUND_WATER) {
2105  if (track != TRACK_BIT_NONE || IsSteepSlope(ti->tileh)) {
2106  /* three-corner-raised slope or steep slope with track on upper part */
2107  DrawShoreTile(ti->tileh);
2108  } else {
2109  /* single-corner-raised slope with track on upper part */
2110  DrawGroundSprite(SPR_FLAT_WATER_TILE, PAL_NONE);
2111  }
2112  } else {
2113  SpriteID image;
2114 
2115  switch (rgt) {
2116  case RAIL_GROUND_BARREN: image = SPR_FLAT_BARE_LAND; break;
2117  case RAIL_GROUND_ICE_DESERT: image = SPR_FLAT_SNOW_DESERT_TILE; break;
2118  default: image = SPR_FLAT_GRASS_TILE; break;
2119  }
2120 
2121  image += SlopeToSpriteOffset(ti->tileh);
2122 
2123  DrawGroundSprite(image, PAL_NONE);
2124  }
2125 
2126  bool no_combine = ti->tileh == SLOPE_FLAT && HasBit(rti->flags, RTF_NO_SPRITE_COMBINE);
2127  SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
2128  SpriteID ground = GetCustomRailSprite(rti, ti->tile, no_combine ? RTSG_GROUND_COMPLETE : RTSG_GROUND);
2130 
2131  if (track == TRACK_BIT_NONE) {
2132  /* Half-tile foundation, no track here? */
2133  } else if (no_combine) {
2134  /* Use trackbits as direct index from ground sprite, subtract 1
2135  * because there is no sprite for no bits. */
2136  DrawGroundSprite(ground + track - 1, PAL_NONE);
2137 
2138  /* Draw reserved track bits */
2139  if (pbs & TRACK_BIT_X) DrawGroundSprite(overlay + RTO_X, PALETTE_CRASH);
2140  if (pbs & TRACK_BIT_Y) DrawGroundSprite(overlay + RTO_Y, PALETTE_CRASH);
2141  if (pbs & TRACK_BIT_UPPER) DrawTrackSprite(overlay + RTO_N, PALETTE_CRASH, ti, SLOPE_N);
2142  if (pbs & TRACK_BIT_LOWER) DrawTrackSprite(overlay + RTO_S, PALETTE_CRASH, ti, SLOPE_S);
2143  if (pbs & TRACK_BIT_RIGHT) DrawTrackSprite(overlay + RTO_E, PALETTE_CRASH, ti, SLOPE_E);
2144  if (pbs & TRACK_BIT_LEFT) DrawTrackSprite(overlay + RTO_W, PALETTE_CRASH, ti, SLOPE_W);
2145  } else if (ti->tileh == SLOPE_NW && track == TRACK_BIT_Y) {
2146  DrawGroundSprite(ground + RTO_SLOPE_NW, PAL_NONE);
2148  } else if (ti->tileh == SLOPE_NE && track == TRACK_BIT_X) {
2149  DrawGroundSprite(ground + RTO_SLOPE_NE, PAL_NONE);
2151  } else if (ti->tileh == SLOPE_SE && track == TRACK_BIT_Y) {
2152  DrawGroundSprite(ground + RTO_SLOPE_SE, PAL_NONE);
2154  } else if (ti->tileh == SLOPE_SW && track == TRACK_BIT_X) {
2155  DrawGroundSprite(ground + RTO_SLOPE_SW, PAL_NONE);
2157  } else {
2158  switch (track) {
2159  /* Draw single ground sprite when not overlapping. No track overlay
2160  * is necessary for these sprites. */
2161  case TRACK_BIT_X: DrawGroundSprite(ground + RTO_X, PAL_NONE); break;
2162  case TRACK_BIT_Y: DrawGroundSprite(ground + RTO_Y, PAL_NONE); break;
2163  case TRACK_BIT_UPPER: DrawTrackSprite(ground + RTO_N, PAL_NONE, ti, SLOPE_N); break;
2164  case TRACK_BIT_LOWER: DrawTrackSprite(ground + RTO_S, PAL_NONE, ti, SLOPE_S); break;
2165  case TRACK_BIT_RIGHT: DrawTrackSprite(ground + RTO_E, PAL_NONE, ti, SLOPE_E); break;
2166  case TRACK_BIT_LEFT: DrawTrackSprite(ground + RTO_W, PAL_NONE, ti, SLOPE_W); break;
2167  case TRACK_BIT_CROSS: DrawGroundSprite(ground + RTO_CROSSING_XY, PAL_NONE); break;
2168  case TRACK_BIT_HORZ: DrawTrackSprite(ground + RTO_N, PAL_NONE, ti, SLOPE_N);
2169  DrawTrackSprite(ground + RTO_S, PAL_NONE, ti, SLOPE_S); break;
2170  case TRACK_BIT_VERT: DrawTrackSprite(ground + RTO_E, PAL_NONE, ti, SLOPE_E);
2171  DrawTrackSprite(ground + RTO_W, PAL_NONE, ti, SLOPE_W); break;
2172 
2173  default:
2174  /* We're drawing a junction tile */
2175  if ((track & TRACK_BIT_3WAY_NE) == 0) {
2176  DrawGroundSprite(ground + RTO_JUNCTION_SW, PAL_NONE);
2177  } else if ((track & TRACK_BIT_3WAY_SW) == 0) {
2178  DrawGroundSprite(ground + RTO_JUNCTION_NE, PAL_NONE);
2179  } else if ((track & TRACK_BIT_3WAY_NW) == 0) {
2180  DrawGroundSprite(ground + RTO_JUNCTION_SE, PAL_NONE);
2181  } else if ((track & TRACK_BIT_3WAY_SE) == 0) {
2182  DrawGroundSprite(ground + RTO_JUNCTION_NW, PAL_NONE);
2183  } else {
2184  DrawGroundSprite(ground + RTO_JUNCTION_NSEW, PAL_NONE);
2185  }
2186 
2187  /* Mask out PBS bits as we shall draw them afterwards anyway. */
2188  track &= ~pbs;
2189 
2190  /* Draw regular track bits */
2191  if (track & TRACK_BIT_X) DrawGroundSprite(overlay + RTO_X, PAL_NONE);
2192  if (track & TRACK_BIT_Y) DrawGroundSprite(overlay + RTO_Y, PAL_NONE);
2193  if (track & TRACK_BIT_UPPER) DrawGroundSprite(overlay + RTO_N, PAL_NONE);
2194  if (track & TRACK_BIT_LOWER) DrawGroundSprite(overlay + RTO_S, PAL_NONE);
2195  if (track & TRACK_BIT_RIGHT) DrawGroundSprite(overlay + RTO_E, PAL_NONE);
2196  if (track & TRACK_BIT_LEFT) DrawGroundSprite(overlay + RTO_W, PAL_NONE);
2197  }
2198 
2199  /* Draw reserved track bits */
2200  if (pbs & TRACK_BIT_X) DrawGroundSprite(overlay + RTO_X, PALETTE_CRASH);
2201  if (pbs & TRACK_BIT_Y) DrawGroundSprite(overlay + RTO_Y, PALETTE_CRASH);
2202  if (pbs & TRACK_BIT_UPPER) DrawTrackSprite(overlay + RTO_N, PALETTE_CRASH, ti, SLOPE_N);
2203  if (pbs & TRACK_BIT_LOWER) DrawTrackSprite(overlay + RTO_S, PALETTE_CRASH, ti, SLOPE_S);
2204  if (pbs & TRACK_BIT_RIGHT) DrawTrackSprite(overlay + RTO_E, PALETTE_CRASH, ti, SLOPE_E);
2205  if (pbs & TRACK_BIT_LEFT) DrawTrackSprite(overlay + RTO_W, PALETTE_CRASH, ti, SLOPE_W);
2206  }
2207 
2208  if (IsValidCorner(halftile_corner)) {
2209  DrawFoundation(ti, HalftileFoundation(halftile_corner));
2211  ground = GetCustomRailSprite(rti, ti->tile, RTSG_GROUND, TCX_UPPER_HALFTILE);
2212 
2213  /* Draw higher halftile-overlay: Use the sloped sprites with three corners raised. They probably best fit the lightning. */
2214  Slope fake_slope = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
2215 
2216  SpriteID image;
2217  switch (rgt) {
2218  case RAIL_GROUND_BARREN: image = SPR_FLAT_BARE_LAND; break;
2220  case RAIL_GROUND_HALF_SNOW: image = SPR_FLAT_SNOW_DESERT_TILE; break;
2221  default: image = SPR_FLAT_GRASS_TILE; break;
2222  }
2223 
2224  image += SlopeToSpriteOffset(fake_slope);
2225 
2226  DrawGroundSprite(image, PAL_NONE, &(_halftile_sub_sprite[halftile_corner]));
2227 
2228  track = CornerToTrackBits(halftile_corner);
2229 
2230  int offset;
2231  switch (track) {
2232  default: NOT_REACHED();
2233  case TRACK_BIT_UPPER: offset = RTO_N; break;
2234  case TRACK_BIT_LOWER: offset = RTO_S; break;
2235  case TRACK_BIT_RIGHT: offset = RTO_E; break;
2236  case TRACK_BIT_LEFT: offset = RTO_W; break;
2237  }
2238 
2239  DrawTrackSprite(ground + offset, PAL_NONE, ti, fake_slope);
2241  DrawTrackSprite(overlay + offset, PALETTE_CRASH, ti, fake_slope);
2242  }
2243  }
2244 }
2245 
2251 static void DrawTrackBits(TileInfo *ti, TrackBits track)
2252 {
2253  const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
2254 
2255  if (rti->UsesOverlay()) {
2256  DrawTrackBitsOverlay(ti, track, rti);
2257  return;
2258  }
2259 
2260  RailGroundType rgt = GetRailGroundType(ti->tile);
2261  Foundation f = GetRailFoundation(ti->tileh, track);
2262  Corner halftile_corner = CORNER_INVALID;
2263 
2264  if (IsNonContinuousFoundation(f)) {
2265  /* Save halftile corner */
2267  /* Draw lower part first */
2268  track &= ~CornerToTrackBits(halftile_corner);
2270  }
2271 
2272  DrawFoundation(ti, f);
2273  /* DrawFoundation modifies ti */
2274 
2275  SpriteID image;
2276  PaletteID pal = PAL_NONE;
2277  const SubSprite *sub = nullptr;
2278  bool junction = false;
2279 
2280  /* Select the sprite to use. */
2281  if (track == 0) {
2282  /* Clear ground (only track on halftile foundation) */
2283  if (rgt == RAIL_GROUND_WATER) {
2284  if (IsSteepSlope(ti->tileh)) {
2285  DrawShoreTile(ti->tileh);
2286  image = 0;
2287  } else {
2288  image = SPR_FLAT_WATER_TILE;
2289  }
2290  } else {
2291  switch (rgt) {
2292  case RAIL_GROUND_BARREN: image = SPR_FLAT_BARE_LAND; break;
2293  case RAIL_GROUND_ICE_DESERT: image = SPR_FLAT_SNOW_DESERT_TILE; break;
2294  default: image = SPR_FLAT_GRASS_TILE; break;
2295  }
2296  image += SlopeToSpriteOffset(ti->tileh);
2297  }
2298  } else {
2299  if (ti->tileh != SLOPE_FLAT) {
2300  /* track on non-flat ground */
2301  image = _track_sloped_sprites[ti->tileh - 1] + rti->base_sprites.track_y;
2302  } else {
2303  /* track on flat ground */
2304  switch (track) {
2305  /* single track, select combined track + ground sprite*/
2306  case TRACK_BIT_Y: image = rti->base_sprites.track_y; break;
2307  case TRACK_BIT_X: image = rti->base_sprites.track_y + 1; break;
2308  case TRACK_BIT_UPPER: image = rti->base_sprites.track_y + 2; break;
2309  case TRACK_BIT_LOWER: image = rti->base_sprites.track_y + 3; break;
2310  case TRACK_BIT_RIGHT: image = rti->base_sprites.track_y + 4; break;
2311  case TRACK_BIT_LEFT: image = rti->base_sprites.track_y + 5; break;
2312  case TRACK_BIT_CROSS: image = rti->base_sprites.track_y + 6; break;
2313 
2314  /* double diagonal track, select combined track + ground sprite*/
2315  case TRACK_BIT_HORZ: image = rti->base_sprites.track_ns; break;
2316  case TRACK_BIT_VERT: image = rti->base_sprites.track_ns + 1; break;
2317 
2318  /* junction, select only ground sprite, handle track sprite later */
2319  default:
2320  junction = true;
2321  if ((track & TRACK_BIT_3WAY_NE) == 0) { image = rti->base_sprites.ground; break; }
2322  if ((track & TRACK_BIT_3WAY_SW) == 0) { image = rti->base_sprites.ground + 1; break; }
2323  if ((track & TRACK_BIT_3WAY_NW) == 0) { image = rti->base_sprites.ground + 2; break; }
2324  if ((track & TRACK_BIT_3WAY_SE) == 0) { image = rti->base_sprites.ground + 3; break; }
2325  image = rti->base_sprites.ground + 4;
2326  break;
2327  }
2328  }
2329 
2330  switch (rgt) {
2331  case RAIL_GROUND_BARREN: pal = PALETTE_TO_BARE_LAND; break;
2332  case RAIL_GROUND_ICE_DESERT: image += rti->snow_offset; break;
2333  case RAIL_GROUND_WATER: {
2334  /* three-corner-raised slope */
2335  DrawShoreTile(ti->tileh);
2337  sub = &(_halftile_sub_sprite[track_corner]);
2338  break;
2339  }
2340  default: break;
2341  }
2342  }
2343 
2344  if (image != 0) DrawGroundSprite(image, pal, sub);
2345 
2346  /* Draw track pieces individually for junction tiles */
2347  if (junction) {
2348  if (track & TRACK_BIT_X) DrawGroundSprite(rti->base_sprites.single_x, PAL_NONE);
2349  if (track & TRACK_BIT_Y) DrawGroundSprite(rti->base_sprites.single_y, PAL_NONE);
2350  if (track & TRACK_BIT_UPPER) DrawGroundSprite(rti->base_sprites.single_n, PAL_NONE);
2351  if (track & TRACK_BIT_LOWER) DrawGroundSprite(rti->base_sprites.single_s, PAL_NONE);
2352  if (track & TRACK_BIT_LEFT) DrawGroundSprite(rti->base_sprites.single_w, PAL_NONE);
2353  if (track & TRACK_BIT_RIGHT) DrawGroundSprite(rti->base_sprites.single_e, PAL_NONE);
2354  }
2355 
2356  /* PBS debugging, draw reserved tracks darker */
2357  if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation) {
2358  /* Get reservation, but mask track on halftile slope */
2359  TrackBits pbs = GetRailReservationTrackBits(ti->tile) & track;
2360  if (pbs & TRACK_BIT_X) {
2361  if (ti->tileh == SLOPE_FLAT || ti->tileh == SLOPE_ELEVATED) {
2363  } else {
2364  DrawGroundSprite(_track_sloped_sprites[ti->tileh - 1] + rti->base_sprites.single_sloped - 20, PALETTE_CRASH);
2365  }
2366  }
2367  if (pbs & TRACK_BIT_Y) {
2368  if (ti->tileh == SLOPE_FLAT || ti->tileh == SLOPE_ELEVATED) {
2370  } else {
2371  DrawGroundSprite(_track_sloped_sprites[ti->tileh - 1] + rti->base_sprites.single_sloped - 20, PALETTE_CRASH);
2372  }
2373  }
2374  if (pbs & TRACK_BIT_UPPER) DrawGroundSprite(rti->base_sprites.single_n, PALETTE_CRASH, nullptr, 0, ti->tileh & SLOPE_N ? -(int)TILE_HEIGHT : 0);
2375  if (pbs & TRACK_BIT_LOWER) DrawGroundSprite(rti->base_sprites.single_s, PALETTE_CRASH, nullptr, 0, ti->tileh & SLOPE_S ? -(int)TILE_HEIGHT : 0);
2376  if (pbs & TRACK_BIT_LEFT) DrawGroundSprite(rti->base_sprites.single_w, PALETTE_CRASH, nullptr, 0, ti->tileh & SLOPE_W ? -(int)TILE_HEIGHT : 0);
2377  if (pbs & TRACK_BIT_RIGHT) DrawGroundSprite(rti->base_sprites.single_e, PALETTE_CRASH, nullptr, 0, ti->tileh & SLOPE_E ? -(int)TILE_HEIGHT : 0);
2378  }
2379 
2380  if (IsValidCorner(halftile_corner)) {
2381  DrawFoundation(ti, HalftileFoundation(halftile_corner));
2382 
2383  /* Draw higher halftile-overlay: Use the sloped sprites with three corners raised. They probably best fit the lightning. */
2384  Slope fake_slope = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
2385  image = _track_sloped_sprites[fake_slope - 1] + rti->base_sprites.track_y;
2386  pal = PAL_NONE;
2387  switch (rgt) {
2388  case RAIL_GROUND_BARREN: pal = PALETTE_TO_BARE_LAND; break;
2390  case RAIL_GROUND_HALF_SNOW: image += rti->snow_offset; break; // higher part has snow in this case too
2391  default: break;
2392  }
2393  DrawGroundSprite(image, pal, &(_halftile_sub_sprite[halftile_corner]));
2394 
2395  if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasReservedTracks(ti->tile, CornerToTrackBits(halftile_corner))) {
2396  static const byte _corner_to_track_sprite[] = {3, 1, 2, 0};
2397  DrawGroundSprite(_corner_to_track_sprite[halftile_corner] + rti->base_sprites.single_n, PALETTE_CRASH, nullptr, 0, -(int)TILE_HEIGHT);
2398  }
2399  }
2400 }
2401 
2402 static void DrawSignals(TileIndex tile, TrackBits rails, const RailtypeInfo *rti)
2403 {
2404 #define MAYBE_DRAW_SIGNAL(x, y, z, t) if (IsSignalPresent(tile, x)) DrawSingleSignal(tile, rti, t, GetSingleSignalState(tile, x), y, z)
2405 
2406  if (!(rails & TRACK_BIT_Y)) {
2407  if (!(rails & TRACK_BIT_X)) {
2408  if (rails & TRACK_BIT_LEFT) {
2409  MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_NORTH, 0, TRACK_LEFT);
2410  MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_SOUTH, 1, TRACK_LEFT);
2411  }
2412  if (rails & TRACK_BIT_RIGHT) {
2413  MAYBE_DRAW_SIGNAL(0, SIGNAL_TO_NORTH, 2, TRACK_RIGHT);
2414  MAYBE_DRAW_SIGNAL(1, SIGNAL_TO_SOUTH, 3, TRACK_RIGHT);
2415  }
2416  if (rails & TRACK_BIT_UPPER) {
2417  MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_WEST, 4, TRACK_UPPER);
2418  MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_EAST, 5, TRACK_UPPER);
2419  }
2420  if (rails & TRACK_BIT_LOWER) {
2421  MAYBE_DRAW_SIGNAL(1, SIGNAL_TO_WEST, 6, TRACK_LOWER);
2422  MAYBE_DRAW_SIGNAL(0, SIGNAL_TO_EAST, 7, TRACK_LOWER);
2423  }
2424  } else {
2425  MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_SOUTHWEST, 8, TRACK_X);
2426  MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_NORTHEAST, 9, TRACK_X);
2427  }
2428  } else {
2429  MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_SOUTHEAST, 10, TRACK_Y);
2430  MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_NORTHWEST, 11, TRACK_Y);
2431  }
2432 }
2433 
2434 static void DrawTile_Track(TileInfo *ti)
2435 {
2436  const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
2437 
2438  _drawtile_track_palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
2439 
2440  if (IsPlainRail(ti->tile)) {
2441  TrackBits rails = GetTrackBits(ti->tile);
2442 
2443  DrawTrackBits(ti, rails);
2444 
2446 
2448 
2449  if (HasSignals(ti->tile)) DrawSignals(ti->tile, rails, rti);
2450  } else {
2451  /* draw depot */
2452  const DrawTileSprites *dts;
2453  PaletteID pal = PAL_NONE;
2454  SpriteID relocation;
2455 
2457 
2459  /* Draw rail instead of depot */
2460  dts = &_depot_invisible_gfx_table[GetRailDepotDirection(ti->tile)];
2461  } else {
2462  dts = &_depot_gfx_table[GetRailDepotDirection(ti->tile)];
2463  }
2464 
2465  SpriteID image;
2466  if (rti->UsesOverlay()) {
2467  image = SPR_FLAT_GRASS_TILE;
2468  } else {
2469  image = dts->ground.sprite;
2470  if (image != SPR_FLAT_GRASS_TILE) image += rti->GetRailtypeSpriteOffset();
2471  }
2472 
2473  /* Adjust ground tile for desert and snow. */
2474  if (IsSnowRailGround(ti->tile)) {
2475  if (image != SPR_FLAT_GRASS_TILE) {
2476  image += rti->snow_offset; // tile with tracks
2477  } else {
2478  image = SPR_FLAT_SNOW_DESERT_TILE; // flat ground
2479  }
2480  }
2481 
2482  DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, _drawtile_track_palette));
2483 
2484  if (rti->UsesOverlay()) {
2485  SpriteID ground = GetCustomRailSprite(rti, ti->tile, RTSG_GROUND);
2486 
2487  switch (GetRailDepotDirection(ti->tile)) {
2488  case DIAGDIR_NE:
2489  if (!IsInvisibilitySet(TO_BUILDINGS)) break;
2490  FALLTHROUGH;
2491  case DIAGDIR_SW:
2492  DrawGroundSprite(ground + RTO_X, PAL_NONE);
2493  break;
2494  case DIAGDIR_NW:
2495  if (!IsInvisibilitySet(TO_BUILDINGS)) break;
2496  FALLTHROUGH;
2497  case DIAGDIR_SE:
2498  DrawGroundSprite(ground + RTO_Y, PAL_NONE);
2499  break;
2500  default:
2501  break;
2502  }
2503 
2505  SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
2506 
2507  switch (GetRailDepotDirection(ti->tile)) {
2508  case DIAGDIR_NE:
2509  if (!IsInvisibilitySet(TO_BUILDINGS)) break;
2510  FALLTHROUGH;
2511  case DIAGDIR_SW:
2512  DrawGroundSprite(overlay + RTO_X, PALETTE_CRASH);
2513  break;
2514  case DIAGDIR_NW:
2515  if (!IsInvisibilitySet(TO_BUILDINGS)) break;
2516  FALLTHROUGH;
2517  case DIAGDIR_SE:
2518  DrawGroundSprite(overlay + RTO_Y, PALETTE_CRASH);
2519  break;
2520  default:
2521  break;
2522  }
2523  }
2524  } else {
2525  /* PBS debugging, draw reserved tracks darker */
2526  if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasDepotReservation(ti->tile)) {
2527  switch (GetRailDepotDirection(ti->tile)) {
2528  case DIAGDIR_NE:
2529  if (!IsInvisibilitySet(TO_BUILDINGS)) break;
2530  FALLTHROUGH;
2531  case DIAGDIR_SW:
2533  break;
2534  case DIAGDIR_NW:
2535  if (!IsInvisibilitySet(TO_BUILDINGS)) break;
2536  FALLTHROUGH;
2537  case DIAGDIR_SE:
2539  break;
2540  default:
2541  break;
2542  }
2543  }
2544  }
2545  int depot_sprite = GetCustomRailSprite(rti, ti->tile, RTSG_DEPOT);
2546  relocation = depot_sprite != 0 ? depot_sprite - SPR_RAIL_DEPOT_SE_1 : rti->GetRailtypeSpriteOffset();
2547 
2549 
2550  DrawRailTileSeq(ti, dts, TO_BUILDINGS, relocation, 0, _drawtile_track_palette);
2551  }
2552  DrawBridgeMiddle(ti);
2553 }
2554 
2555 void DrawTrainDepotSprite(int x, int y, int dir, RailType railtype)
2556 {
2557  const DrawTileSprites *dts = &_depot_gfx_table[dir];
2558  const RailtypeInfo *rti = GetRailTypeInfo(railtype);
2559  SpriteID image = rti->UsesOverlay() ? SPR_FLAT_GRASS_TILE : dts->ground.sprite;
2560  uint32 offset = rti->GetRailtypeSpriteOffset();
2561 
2562  if (image != SPR_FLAT_GRASS_TILE) image += offset;
2563  PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
2564 
2565  DrawSprite(image, PAL_NONE, x, y);
2566 
2567  if (rti->UsesOverlay()) {
2569 
2570  switch (dir) {
2571  case DIAGDIR_SW: DrawSprite(ground + RTO_X, PAL_NONE, x, y); break;
2572  case DIAGDIR_SE: DrawSprite(ground + RTO_Y, PAL_NONE, x, y); break;
2573  default: break;
2574  }
2575  }
2576  int depot_sprite = GetCustomRailSprite(rti, INVALID_TILE, RTSG_DEPOT);
2577  if (depot_sprite != 0) offset = depot_sprite - SPR_RAIL_DEPOT_SE_1;
2578 
2579  DrawRailTileSeqInGUI(x, y, dts, offset, 0, palette);
2580 }
2581 
2582 static int GetSlopePixelZ_Track(TileIndex tile, uint x, uint y)
2583 {
2584  if (IsPlainRail(tile)) {
2585  int z;
2586  Slope tileh = GetTilePixelSlope(tile, &z);
2587  if (tileh == SLOPE_FLAT) return z;
2588 
2589  z += ApplyPixelFoundationToSlope(GetRailFoundation(tileh, GetTrackBits(tile)), &tileh);
2590  return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
2591  } else {
2592  return GetTileMaxPixelZ(tile);
2593  }
2594 }
2595 
2596 static Foundation GetFoundation_Track(TileIndex tile, Slope tileh)
2597 {
2598  return IsPlainRail(tile) ? GetRailFoundation(tileh, GetTrackBits(tile)) : FlatteningFoundation(tileh);
2599 }
2600 
2601 static void TileLoop_Track(TileIndex tile)
2602 {
2603  RailGroundType old_ground = GetRailGroundType(tile);
2604  RailGroundType new_ground;
2605 
2606  if (old_ground == RAIL_GROUND_WATER) {
2607  TileLoop_Water(tile);
2608  return;
2609  }
2610 
2612  case LT_ARCTIC: {
2613  int z;
2614  Slope slope = GetTileSlope(tile, &z);
2615  bool half = false;
2616 
2617  /* for non-flat track, use lower part of track
2618  * in other cases, use the highest part with track */
2619  if (IsPlainRail(tile)) {
2620  TrackBits track = GetTrackBits(tile);
2621  Foundation f = GetRailFoundation(slope, track);
2622 
2623  switch (f) {
2624  case FOUNDATION_NONE:
2625  /* no foundation - is the track on the upper side of three corners raised tile? */
2626  if (IsSlopeWithThreeCornersRaised(slope)) z++;
2627  break;
2628 
2629  case FOUNDATION_INCLINED_X:
2630  case FOUNDATION_INCLINED_Y:
2631  /* sloped track - is it on a steep slope? */
2632  if (IsSteepSlope(slope)) z++;
2633  break;
2634 
2636  /* only lower part of steep slope */
2637  z++;
2638  break;
2639 
2640  default:
2641  /* if it is a steep slope, then there is a track on higher part */
2642  if (IsSteepSlope(slope)) z++;
2643  z++;
2644  break;
2645  }
2646 
2648  } else {
2649  /* is the depot on a non-flat tile? */
2650  if (slope != SLOPE_FLAT) z++;
2651  }
2652 
2653  /* 'z' is now the lowest part of the highest track bit -
2654  * for sloped track, it is 'z' of lower part
2655  * for two track bits, it is 'z' of higher track bit
2656  * For non-continuous foundations (and STEEP_BOTH), 'half' is set */
2657  if (z > GetSnowLine()) {
2658  if (half && z - GetSnowLine() == 1) {
2659  /* track on non-continuous foundation, lower part is not under snow */
2660  new_ground = RAIL_GROUND_HALF_SNOW;
2661  } else {
2662  new_ground = RAIL_GROUND_ICE_DESERT;
2663  }
2664  goto set_ground;
2665  }
2666  break;
2667  }
2668 
2669  case LT_TROPIC:
2670  if (GetTropicZone(tile) == TROPICZONE_DESERT) {
2671  new_ground = RAIL_GROUND_ICE_DESERT;
2672  goto set_ground;
2673  }
2674  break;
2675  }
2676 
2677  new_ground = RAIL_GROUND_GRASS;
2678 
2679  if (IsPlainRail(tile) && old_ground != RAIL_GROUND_BARREN) { // wait until bottom is green
2680  /* determine direction of fence */
2681  TrackBits rail = GetTrackBits(tile);
2682 
2683  Owner owner = GetTileOwner(tile);
2684  byte fences = 0;
2685 
2686  for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
2688 
2689  /* Track bit on this edge => no fence. */
2690  if ((rail & dir_to_trackbits[d]) != TRACK_BIT_NONE) continue;
2691 
2692  TileIndex tile2 = tile + TileOffsByDiagDir(d);
2693 
2694  /* Show fences if it's a house, industry, object, road, tunnelbridge or not owned by us. */
2695  if (!IsValidTile(tile2) || IsTileType(tile2, MP_HOUSE) || IsTileType(tile2, MP_INDUSTRY) ||
2696  IsTileType(tile2, MP_ROAD) || (IsTileType(tile2, MP_OBJECT) && !IsObjectType(tile2, OBJECT_OWNED_LAND)) || IsTileType(tile2, MP_TUNNELBRIDGE) || !IsTileOwner(tile2, owner)) {
2697  fences |= 1 << d;
2698  }
2699  }
2700 
2701  switch (fences) {
2702  case 0: break;
2703  case (1 << DIAGDIR_NE): new_ground = RAIL_GROUND_FENCE_NE; break;
2704  case (1 << DIAGDIR_SE): new_ground = RAIL_GROUND_FENCE_SE; break;
2705  case (1 << DIAGDIR_SW): new_ground = RAIL_GROUND_FENCE_SW; break;
2706  case (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_NW; break;
2707  case (1 << DIAGDIR_NE) | (1 << DIAGDIR_SW): new_ground = RAIL_GROUND_FENCE_NESW; break;
2708  case (1 << DIAGDIR_SE) | (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_SENW; break;
2709  case (1 << DIAGDIR_NE) | (1 << DIAGDIR_SE): new_ground = RAIL_GROUND_FENCE_VERT1; break;
2710  case (1 << DIAGDIR_NE) | (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_HORIZ2; break;
2711  case (1 << DIAGDIR_SE) | (1 << DIAGDIR_SW): new_ground = RAIL_GROUND_FENCE_HORIZ1; break;
2712  case (1 << DIAGDIR_SW) | (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_VERT2; break;
2713  default: NOT_REACHED();
2714  }
2715  }
2716 
2717 set_ground:
2718  if (old_ground != new_ground) {
2719  SetRailGroundType(tile, new_ground);
2720  MarkTileDirtyByTile(tile);
2721  }
2722 }
2723 
2724 
2725 static TrackStatus GetTileTrackStatus_Track(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
2726 {
2727  /* Case of half tile slope with water. */
2728  if (mode == TRANSPORT_WATER && IsPlainRail(tile) && GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(GetTileSlope(tile))) {
2729  TrackBits tb = GetTrackBits(tile);
2730  switch (tb) {
2731  default: NOT_REACHED();
2732  case TRACK_BIT_UPPER: tb = TRACK_BIT_LOWER; break;
2733  case TRACK_BIT_LOWER: tb = TRACK_BIT_UPPER; break;
2734  case TRACK_BIT_LEFT: tb = TRACK_BIT_RIGHT; break;
2735  case TRACK_BIT_RIGHT: tb = TRACK_BIT_LEFT; break;
2736  }
2738  }
2739 
2740  if (mode != TRANSPORT_RAIL) return 0;
2741 
2742  TrackBits trackbits = TRACK_BIT_NONE;
2743  TrackdirBits red_signals = TRACKDIR_BIT_NONE;
2744 
2745  switch (GetRailTileType(tile)) {
2746  default: NOT_REACHED();
2747  case RAIL_TILE_NORMAL:
2748  trackbits = GetTrackBits(tile);
2749  break;
2750 
2751  case RAIL_TILE_SIGNALS: {
2752  trackbits = GetTrackBits(tile);
2753  byte a = GetPresentSignals(tile);
2754  uint b = GetSignalStates(tile);
2755 
2756  b &= a;
2757 
2758  /* When signals are not present (in neither direction),
2759  * we pretend them to be green. Otherwise, it depends on
2760  * the signal type. For signals that are only active from
2761  * one side, we set the missing signals explicitly to
2762  * `green'. Otherwise, they implicitly become `red'. */
2763  if (!IsOnewaySignal(tile, TRACK_UPPER) || (a & SignalOnTrack(TRACK_UPPER)) == 0) b |= ~a & SignalOnTrack(TRACK_UPPER);
2764  if (!IsOnewaySignal(tile, TRACK_LOWER) || (a & SignalOnTrack(TRACK_LOWER)) == 0) b |= ~a & SignalOnTrack(TRACK_LOWER);
2765 
2766  if ((b & 0x8) == 0) red_signals |= (TRACKDIR_BIT_LEFT_N | TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_SE | TRACKDIR_BIT_UPPER_E);
2767  if ((b & 0x4) == 0) red_signals |= (TRACKDIR_BIT_LEFT_S | TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_UPPER_W);
2768  if ((b & 0x2) == 0) red_signals |= (TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_E);
2769  if ((b & 0x1) == 0) red_signals |= (TRACKDIR_BIT_RIGHT_S | TRACKDIR_BIT_LOWER_W);
2770 
2771  break;
2772  }
2773 
2774  case RAIL_TILE_DEPOT: {
2776 
2777  if (side != INVALID_DIAGDIR && side != dir) break;
2778 
2779  trackbits = DiagDirToDiagTrackBits(dir);
2780  break;
2781  }
2782  }
2783 
2784  return CombineTrackStatus(TrackBitsToTrackdirBits(trackbits), red_signals);
2785 }
2786 
2787 static bool ClickTile_Track(TileIndex tile)
2788 {
2789  if (!IsRailDepot(tile)) return false;
2790 
2791  ShowDepotWindow(tile, VEH_TRAIN);
2792  return true;
2793 }
2794 
2795 static void GetTileDesc_Track(TileIndex tile, TileDesc *td)
2796 {
2797  const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
2798  td->rail_speed = rti->max_speed;
2799  td->railtype = rti->strings.name;
2800  td->owner[0] = GetTileOwner(tile);
2801  switch (GetRailTileType(tile)) {
2802  case RAIL_TILE_NORMAL:
2803  td->str = STR_LAI_RAIL_DESCRIPTION_TRACK;
2804  break;
2805 
2806  case RAIL_TILE_SIGNALS: {
2807  static const StringID signal_type[6][6] = {
2808  {
2809  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_SIGNALS,
2810  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PRESIGNALS,
2811  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_EXITSIGNALS,
2812  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_COMBOSIGNALS,
2813  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PBSSIGNALS,
2814  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_NOENTRYSIGNALS
2815  },
2816  {
2817  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PRESIGNALS,
2818  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRESIGNALS,
2819  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_EXITSIGNALS,
2820  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_COMBOSIGNALS,
2821  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_PBSSIGNALS,
2822  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_NOENTRYSIGNALS
2823  },
2824  {
2825  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_EXITSIGNALS,
2826  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_EXITSIGNALS,
2827  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXITSIGNALS,
2828  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_COMBOSIGNALS,
2829  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_PBSSIGNALS,
2830  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_NOENTRYSIGNALS
2831  },
2832  {
2833  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_COMBOSIGNALS,
2834  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_COMBOSIGNALS,
2835  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_COMBOSIGNALS,
2836  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBOSIGNALS,
2837  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_PBSSIGNALS,
2838  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_NOENTRYSIGNALS
2839  },
2840  {
2841  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PBSSIGNALS,
2842  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_PBSSIGNALS,
2843  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_PBSSIGNALS,
2844  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_PBSSIGNALS,
2845  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PBSSIGNALS,
2846  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PBS_NOENTRYSIGNALS
2847  },
2848  {
2849  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_NOENTRYSIGNALS,
2850  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_NOENTRYSIGNALS,
2851  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_NOENTRYSIGNALS,
2852  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_NOENTRYSIGNALS,
2853  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PBS_NOENTRYSIGNALS,
2854  STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NOENTRYSIGNALS
2855  }
2856  };
2857 
2858  SignalType primary_signal;
2859  SignalType secondary_signal;
2860  if (HasSignalOnTrack(tile, TRACK_UPPER)) {
2861  primary_signal = GetSignalType(tile, TRACK_UPPER);
2862  secondary_signal = HasSignalOnTrack(tile, TRACK_LOWER) ? GetSignalType(tile, TRACK_LOWER) : primary_signal;
2863  } else {
2864  secondary_signal = primary_signal = GetSignalType(tile, TRACK_LOWER);
2865  }
2866 
2867  td->str = signal_type[secondary_signal][primary_signal];
2868  break;
2869  }
2870 
2871  case RAIL_TILE_DEPOT:
2872  td->str = STR_LAI_RAIL_DESCRIPTION_TRAIN_DEPOT;
2873  if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) {
2874  if (td->rail_speed > 0) {
2875  td->rail_speed = min(td->rail_speed, 61);
2876  } else {
2877  td->rail_speed = 61;
2878  }
2879  }
2880  td->build_date = Depot::GetByTile(tile)->build_date;
2881  break;
2882 
2883  default:
2884  NOT_REACHED();
2885  }
2886 }
2887 
2888 static void ChangeTileOwner_Track(TileIndex tile, Owner old_owner, Owner new_owner)
2889 {
2890  if (!IsTileOwner(tile, old_owner)) return;
2891 
2892  if (new_owner != INVALID_OWNER) {
2893  /* Update company infrastructure counts. No need to dirty windows here, we'll redraw the whole screen anyway. */
2894  uint num_pieces = 1;
2895  if (IsPlainRail(tile)) {
2896  TrackBits bits = GetTrackBits(tile);
2897  num_pieces = CountBits(bits);
2898  if (TracksOverlap(bits)) num_pieces *= num_pieces;
2899  }
2900  RailType rt = GetRailType(tile);
2901  Company::Get(old_owner)->infrastructure.rail[rt] -= num_pieces;
2902  Company::Get(new_owner)->infrastructure.rail[rt] += num_pieces;
2903 
2904  if (HasSignals(tile)) {
2905  uint num_sigs = CountBits(GetPresentSignals(tile));
2906  Company::Get(old_owner)->infrastructure.signal -= num_sigs;
2907  Company::Get(new_owner)->infrastructure.signal += num_sigs;
2908  }
2909 
2910  SetTileOwner(tile, new_owner);
2911  } else {
2913  }
2914 }
2915 
2916 static const byte _fractcoords_behind[4] = { 0x8F, 0x8, 0x80, 0xF8 };
2917 static const byte _fractcoords_enter[4] = { 0x8A, 0x48, 0x84, 0xA8 };
2918 static const int8 _deltacoord_leaveoffset[8] = {
2919  -1, 0, 1, 0, /* x */
2920  0, 1, 0, -1 /* y */
2921 };
2922 
2923 
2931 {
2933  int length = v->CalcNextVehicleOffset();
2934 
2935  switch (dir) {
2936  case DIAGDIR_NE: return ((int)(v->x_pos & 0x0F) - ((_fractcoords_enter[dir] & 0x0F) - (length + 1)));
2937  case DIAGDIR_SE: return -((int)(v->y_pos & 0x0F) - ((_fractcoords_enter[dir] >> 4) + (length + 1)));
2938  case DIAGDIR_SW: return -((int)(v->x_pos & 0x0F) - ((_fractcoords_enter[dir] & 0x0F) + (length + 1)));
2939  case DIAGDIR_NW: return ((int)(v->y_pos & 0x0F) - ((_fractcoords_enter[dir] >> 4) - (length + 1)));
2940  default: NOT_REACHED();
2941  }
2942 }
2943 
2949 {
2950  /* This routine applies only to trains in depot tiles. */
2951  if (u->type != VEH_TRAIN || !IsRailDepotTile(tile)) return VETSB_CONTINUE;
2952 
2953  /* Depot direction. */
2955 
2956  byte fract_coord = (x & 0xF) + ((y & 0xF) << 4);
2957 
2958  /* Make sure a train is not entering the tile from behind. */
2959  if (_fractcoords_behind[dir] == fract_coord) return VETSB_CANNOT_ENTER;
2960 
2961  Train *v = Train::From(u);
2962 
2963  /* Leaving depot? */
2964  if (v->direction == DiagDirToDir(dir)) {
2965  /* Calculate the point where the following wagon should be activated. */
2966  int length = v->CalcNextVehicleOffset();
2967 
2968  byte fract_coord_leave =
2969  ((_fractcoords_enter[dir] & 0x0F) + // x
2970  (length + 1) * _deltacoord_leaveoffset[dir]) +
2971  (((_fractcoords_enter[dir] >> 4) + // y
2972  ((length + 1) * _deltacoord_leaveoffset[dir + 4])) << 4);
2973 
2974  if (fract_coord_leave == fract_coord) {
2975  /* Leave the depot. */
2976  if ((v = v->Next()) != nullptr) {
2977  v->vehstatus &= ~VS_HIDDEN;
2978  v->track = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
2979  }
2980  }
2981  } else if (_fractcoords_enter[dir] == fract_coord) {
2982  /* Entering depot. */
2983  assert(DiagDirToDir(ReverseDiagDir(dir)) == v->direction);
2984  v->track = TRACK_BIT_DEPOT,
2985  v->vehstatus |= VS_HIDDEN;
2986  v->direction = ReverseDir(v->direction);
2987  if (v->Next() == nullptr) VehicleEnterDepot(v->First());
2988  v->tile = tile;
2989 
2991  return VETSB_ENTERED_WORMHOLE;
2992  }
2993 
2994  return VETSB_CONTINUE;
2995 }
2996 
3008 static CommandCost TestAutoslopeOnRailTile(TileIndex tile, uint flags, int z_old, Slope tileh_old, int z_new, Slope tileh_new, TrackBits rail_bits)
3009 {
3010  if (!_settings_game.construction.build_on_slopes || !AutoslopeEnabled()) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
3011 
3012  /* Is the slope-rail_bits combination valid in general? I.e. is it safe to call GetRailFoundation() ? */
3013  if (CheckRailSlope(tileh_new, rail_bits, TRACK_BIT_NONE, tile).Failed()) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
3014 
3015  /* Get the slopes on top of the foundations */
3016  z_old += ApplyFoundationToSlope(GetRailFoundation(tileh_old, rail_bits), &tileh_old);
3017  z_new += ApplyFoundationToSlope(GetRailFoundation(tileh_new, rail_bits), &tileh_new);
3018 
3019  Corner track_corner;
3020  switch (rail_bits) {
3021  case TRACK_BIT_LEFT: track_corner = CORNER_W; break;
3022  case TRACK_BIT_LOWER: track_corner = CORNER_S; break;
3023  case TRACK_BIT_RIGHT: track_corner = CORNER_E; break;
3024  case TRACK_BIT_UPPER: track_corner = CORNER_N; break;
3025 
3026  /* Surface slope must not be changed */
3027  default:
3028  if (z_old != z_new || tileh_old != tileh_new) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
3029  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
3030  }
3031 
3032  /* The height of the track_corner must not be changed. The rest ensures GetRailFoundation() already. */
3033  z_old += GetSlopeZInCorner(RemoveHalftileSlope(tileh_old), track_corner);
3034  z_new += GetSlopeZInCorner(RemoveHalftileSlope(tileh_new), track_corner);
3035  if (z_old != z_new) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
3036 
3037  CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
3038  /* Make the ground dirty, if surface slope has changed */
3039  if (tileh_old != tileh_new) {
3040  /* If there is flat water on the lower halftile add the cost for clearing it */
3041  if (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh_old)) cost.AddCost(_price[PR_CLEAR_WATER]);
3042  if ((flags & DC_EXEC) != 0) SetRailGroundType(tile, RAIL_GROUND_BARREN);
3043  }
3044  return cost;
3045 }
3046 
3050 static Vehicle *EnsureNoShipProc(Vehicle *v, void *data)
3051 {
3052  return v->type == VEH_SHIP ? v : nullptr;
3053 }
3054 
3055 static CommandCost TerraformTile_Track(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
3056 {
3057  int z_old;
3058  Slope tileh_old = GetTileSlope(tile, &z_old);
3059  if (IsPlainRail(tile)) {
3060  TrackBits rail_bits = GetTrackBits(tile);
3061  /* Is there flat water on the lower halftile that must be cleared expensively? */
3062  bool was_water = (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh_old));
3063 
3064  /* Allow clearing the water only if there is no ship */
3065  if (was_water && HasVehicleOnPos(tile, nullptr, &EnsureNoShipProc)) return_cmd_error(STR_ERROR_SHIP_IN_THE_WAY);
3066 
3067  /* First test autoslope. However if it succeeds we still have to test the rest, because non-autoslope terraforming is cheaper. */
3068  CommandCost autoslope_result = TestAutoslopeOnRailTile(tile, flags, z_old, tileh_old, z_new, tileh_new, rail_bits);
3069 
3070  /* When there is only a single horizontal/vertical track, one corner can be terraformed. */
3071  Corner allowed_corner;
3072  switch (rail_bits) {
3073  case TRACK_BIT_RIGHT: allowed_corner = CORNER_W; break;
3074  case TRACK_BIT_UPPER: allowed_corner = CORNER_S; break;
3075  case TRACK_BIT_LEFT: allowed_corner = CORNER_E; break;
3076  case TRACK_BIT_LOWER: allowed_corner = CORNER_N; break;
3077  default: return autoslope_result;
3078  }
3079 
3080  Foundation f_old = GetRailFoundation(tileh_old, rail_bits);
3081 
3082  /* Do not allow terraforming if allowed_corner is part of anti-zig-zag foundations */
3083  if (tileh_old != SLOPE_NS && tileh_old != SLOPE_EW && IsSpecialRailFoundation(f_old)) return autoslope_result;
3084 
3085  /* Everything is valid, which only changes allowed_corner */
3086  for (Corner corner = (Corner)0; corner < CORNER_END; corner = (Corner)(corner + 1)) {
3087  if (allowed_corner == corner) continue;
3088  if (z_old + GetSlopeZInCorner(tileh_old, corner) != z_new + GetSlopeZInCorner(tileh_new, corner)) return autoslope_result;
3089  }
3090 
3091  /* Make the ground dirty */
3092  if ((flags & DC_EXEC) != 0) SetRailGroundType(tile, RAIL_GROUND_BARREN);
3093 
3094  /* allow terraforming */
3095  return CommandCost(EXPENSES_CONSTRUCTION, was_water ? _price[PR_CLEAR_WATER] : (Money)0);
3097  AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRailDepotDirection(tile))) {
3098  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
3099  }
3100  return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
3101 }
3102 
3103 
3104 extern const TileTypeProcs _tile_type_rail_procs = {
3105  DrawTile_Track, // draw_tile_proc
3106  GetSlopePixelZ_Track, // get_slope_z_proc
3107  ClearTile_Track, // clear_tile_proc
3108  nullptr, // add_accepted_cargo_proc
3109  GetTileDesc_Track, // get_tile_desc_proc
3110  GetTileTrackStatus_Track, // get_tile_track_status_proc
3111  ClickTile_Track, // click_tile_proc
3112  nullptr, // animate_tile_proc
3113  TileLoop_Track, // tile_loop_proc
3114  ChangeTileOwner_Track, // change_tile_owner_proc
3115  nullptr, // add_produced_cargo_proc
3116  VehicleEnter_Track, // vehicle_enter_tile_proc
3117  GetFoundation_Track, // get_foundation_proc
3118  TerraformTile_Track, // terraform_tile_proc
3119 };
bool disable_elrails
when true, the elrails are disabled
Functions related to OTTD&#39;s strings.
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Piece of rail on slope with north-west raised.
Definition: rail.h:77
don&#39;t allow building on structures
Definition: command_type.h:345
static bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:47
bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle is on a specific location.
Definition: vehicle.cpp:511
uint ApplyFoundationToSlope(Foundation f, Slope *s)
Applies a foundation to a slope.
Definition: landscape.cpp:162
VehicleSettings vehicle
options for vehicles
static bool HasPowerOnRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType got power on a tile with a given RailType.
Definition: rail.h:332
Functions/types related to NewGRF debugging.
void InitRailTypes()
Resolve sprites of custom rail types.
Definition: rail_cmd.cpp:138
Grass with a fence at the northern side.
Definition: rail_map.h:497
CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits)
Tests if a vehicle interacts with the specified track bits.
Definition: vehicle.cpp:599
the north corner of the tile is raised
Definition: slope_type.h:53
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
Corner
Enumeration of tile corners.
Definition: slope_type.h:22
CommandCost CmdRemoveSignalTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove signals on a stretch of track.
Definition: rail_cmd.cpp:1537
static bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
Checks for the presence of signals along the given trackdir on the given rail tile.
Definition: rail_map.h:426
Vehicle is stopped by the player.
Definition: vehicle_base.h:31
void AddTrackToSignalBuffer(TileIndex tile, Track track, Owner owner)
Add track to signal update buffer.
Definition: signal.cpp:580
struct RailtypeInfo::@36 base_sprites
Struct containing the main sprites.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:79
Definition of stuff that is very close to a company, like the company struct itself.
int CalcNextVehicleOffset() const
Calculate the offset from this vehicle&#39;s center to the following center taking the vehicle lengths in...
Definition: train.h:167
static void DrawTrackFence(const TileInfo *ti, SpriteID base_image, uint num_sprites, RailFenceOffset rfo)
Draw a track fence.
Definition: rail_cmd.cpp:1965
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:304
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:238
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:101
static TransportType GetTunnelBridgeTransportType(TileIndex t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
No track build.
Definition: track_type.h:102
static void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, uint town)
Make a level crossing.
Definition: road_map.h:660
Depot (one entrance)
Definition: rail_map.h:26
Direction direction
facing
Definition: vehicle_base.h:269
static uint GetSaveSlopeZ(uint x, uint y, Track track)
Get surface height in point (x,y) On tiles with halftile foundations move (x,y) to a safe point wrt...
Definition: rail_cmd.cpp:1873
Track upper, direction west.
Definition: track_type.h:112
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
Tile information, used while rendering the tile.
Definition: tile_cmd.h:42
south and east corner are raised
Definition: slope_type.h:57
Flag for an invalid track.
Definition: track_type.h:28
Slope NW, Track Y, Fence NE.
Definition: rail.h:107
static Money RailConvertCost(RailType from, RailType to)
Calculates the cost of rail conversion.
Definition: rail.h:400
SpriteID single_sloped
single piece of rail for slopes
Definition: rail.h:140
Grass with a fence at the NE and SW edges.
Definition: rail_map.h:493
static const TrackBits _valid_tracks_without_foundation[15]
Valid TrackBits on a specific (non-steep)-slope without foundation.
Definition: rail_cmd.cpp:279
the west corner of the tile is raised
Definition: slope_type.h:50
presignal block exit
Definition: signal_type.h:26
void TileLoop_Water(TileIndex tile)
Let a water tile floods its diagonal adjoining tiles called from tunnelbridge_cmd, and by TileLoop_Industry() and TileLoop_Track()
Definition: water_cmd.cpp:1206
byte landscape
the landscape we&#39;re currently in
SignalType
Type of signal, i.e.
Definition: signal_type.h:23
"Arrow" to the south-west
Definition: track_type.h:51
SpriteID auto_rail
button for the autorail construction
Definition: rail.h:154
company buildings - depots, stations, HQ, ...
Definition: transparency.h:27
Tile is desert.
Definition: tile_type.h:71
Track along the x-axis (north-east to south-west)
Definition: track_type.h:21
All possible tracks.
Definition: track_type.h:53
An invalid owner.
Definition: company_type.h:29
static const uint LEVELCROSSING_TRACKBIT_FACTOR
Multiplier for how many regular track bits a level crossing counts.
Definition: economy_type.h:213
Slope FLAT, Track Y, Fence NE.
Definition: rail.h:101
static const RailtypeInfo _original_railtypes[]
Global Railtype definition.
Definition: railtypes.h:19
presignal block entry
Definition: signal_type.h:25
void MarkBridgeDirty(TileIndex begin, TileIndex end, DiagDirection direction, uint bridge_height)
Mark bridge tiles dirty.
Part of an industry.
Definition: tile_type.h:49
Track in the lower corner of the tile (south)
Definition: track_type.h:24
byte _display_opt
What do we want to draw/do?
static FenceOffset _fence_offsets[]
Offsets for drawing fences.
Definition: rail_cmd.cpp:1939
uint GetPartialPixelZ(int x, int y, Slope corners)
Determines height at given coordinate of a slope.
Definition: landscape.cpp:215
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition: vehicle.cpp:537
Train vehicle type.
Definition: vehicle_type.h:24
CommandCost CmdBuildSignalTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build signals on a stretch of track.
Definition: rail_cmd.cpp:1445
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
static Corner GetHalftileFoundationCorner(Foundation f)
Returns the halftile corner of a halftile-foundation.
Definition: slope_func.h:333
Functions related to dates.
SpriteID build_y_rail
button for building single rail in Y direction
Definition: rail.h:153
static VehicleEnterTileStatus VehicleEnter_Track(Vehicle *u, TileIndex tile, int x, int y)
Tile callback routine when vehicle enters tile.
Definition: rail_cmd.cpp:2948
static T ToggleBit(T &x, const uint8 y)
Toggles a bit in a variable.
Northwest.
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:270
CommandCost CmdBuildSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build signals, alternate between double/single, signal/semaphore, pre/exit/combo-signals, and what-else not.
Definition: rail_cmd.cpp:1049
Piece of rail in southern corner.
Definition: rail.h:71
TileType
The different types of tiles.
Definition: tile_type.h:40
static void DrawTrackFence_SE(const TileInfo *ti, SpriteID base_image, uint num_sprites)
Draw fence at SE border matching the tile slope.
Definition: rail_cmd.cpp:1992
Base for the train class.
static bool HasSignalOnTrack(TileIndex tile, Track track)
Checks for the presence of signals (either way) on the given track on the given rail tile...
Definition: rail_map.h:413
RailTypeFlags flags
Bit mask of rail type flags.
Definition: rail.h:208
Left track.
Definition: track_type.h:44
Used for iterations.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Offsets for drawing fences.
Definition: rail_cmd.cpp:1930
static void MakeShore(TileIndex t)
Helper function to make a coast tile.
Definition: water_map.h:375
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:70
north and south corner are raised
Definition: slope_type.h:60
static TrackBits GetCrossingRailBits(TileIndex tile)
Get the rail track bits of a level crossing.
Definition: road_map.h:368
uint16 cur_speed
current speed
Definition: vehicle_base.h:291
A tile with road (or tram tracks)
Definition: tile_type.h:43
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
Ship vehicle type.
Definition: vehicle_type.h:26
Ballast for junction &#39;pointing&#39; NE.
Definition: rail.h:80
int y_offs
Bounding box Y offset.
Definition: rail_cmd.cpp:1933
Depot view; Window numbers:
Definition: window_type.h:344
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:45
bool FloodHalftile(TileIndex t)
Called from water_cmd if a non-flat rail-tile gets flooded and should be converted to shore...
Definition: rail_cmd.cpp:764
Full road along the x-axis (south-west + north-east)
Definition: road_type.h:56
Flag for invalid railtype.
Definition: rail_type.h:34
static const TrackBits _valid_tracks_on_leveled_foundation[15]
Valid TrackBits on a specific (non-steep)-slope with leveled foundation.
Definition: rail_cmd.cpp:301
Ballast for junction &#39;pointing&#39; NW.
Definition: rail.h:82
SignalState
These are states in which a signal can be.
Definition: signal_type.h:44
CommandCost CmdBuildSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build a single piece of rail.
Definition: rail_cmd.cpp:443
bool ValParamRailtype(const RailType rail)
Validate functions for rail building.
Definition: rail.cpp:206
Grass with a fence at the eastern side.
Definition: rail_map.h:494
Piece of rail on slope with north-east raised.
Definition: rail.h:74
static bool IsOnewaySignal(TileIndex t, Track track)
One-way signals can&#39;t be passed the &#39;wrong&#39; way.
Definition: rail_map.h:319
Transport over water.
X-axis track.
Definition: track_type.h:40
static Foundation HalftileFoundation(Corner corner)
Returns the halftile foundation for single horizontal/vertical track.
Definition: slope_func.h:391
static bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition: slope_func.h:24
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:532
void AddSideToSignalBuffer(TileIndex tile, DiagDirection side, Owner owner)
Add side of tile to signal update buffer.
Definition: signal.cpp:612
Piece of rail in western corner.
Definition: rail.h:73
SpriteID single_e
single piece of rail in the eastern corner
Definition: rail.h:138
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it...
Definition: vehicle.cpp:1436
Fence images.
Definition: rail.h:56
PathfinderSettings pf
settings for all pathfinders
Build vehicle; Window numbers:
Definition: window_type.h:376
CommandCost CmdRemoveRailroadTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build rail on a stretch of track.
Definition: rail_cmd.cpp:958
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:382
Vehicle data structure.
Definition: vehicle_base.h:210
Base for all depots (except hangars)
Tile description for the &#39;land area information&#39; tool.
Definition: tile_cmd.h:51
demolish a tile
Definition: command_type.h:180
CommandCost CheckTileOwnership(TileIndex tile)
Check whether the current owner owns the stuff on the given tile.
RailTypeLabelList alternate_labels
Rail type labels this type provides in addition to the main label.
Definition: rail.h:238
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
the east corner of the tile is raised
Definition: slope_type.h:52
flag for invalid roadtype
Definition: road_type.h:27
Icy or sandy.
Definition: rail_map.h:498
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:36
T * First() const
Get the first vehicle in the chain.
Definition: vehicle_base.h:996
Also draw details of track and roads.
Definition: openttd.h:45
Slope FLAT, Track LOWER, Fence N.
Definition: rail.h:111
Helper functions to extract data from command parameters.
void ResetRailTypes()
Reset all rail type information to its default values.
Definition: rail_cmd.cpp:63
struct RailtypeInfo::@38 cursor
Cursors associated with the rail type.
bool forbid_90_deg
forbid trains to make 90 deg turns
Grass with a fence at the SW edge.
Definition: rail_map.h:492
static Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track incidating with that diagdir.
Definition: track_func.h:520
Nothing (dirt)
Definition: rail_map.h:486
static Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:148
A railway.
Definition: tile_type.h:42
static Track GetRailStationTrack(TileIndex t)
Get the rail track of a rail station tile.
Definition: station_map.h:349
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel ...
Definition: rail.h:188
Contains objects such as transmitters and owned land.
Definition: tile_type.h:51
static void DrawTrackFence_NW(const TileInfo *ti, SpriteID base_image, uint num_sprites)
Draw fence at NW border matching the tile slope.
Definition: rail_cmd.cpp:1982
Nothing special.
Construction costs.
Definition: economy_type.h:149
Flag for an invalid DiagDirection.
static void SetHasSignals(TileIndex tile, bool signals)
Add/remove the &#39;has signal&#39; bit from the RailTileType.
Definition: rail_map.h:83
south and west corner are raised
Definition: slope_type.h:56
static void SetTrackBits(TileIndex t, TrackBits b)
Sets the track bits of the given tile.
Definition: rail_map.h:147
Slope FLAT, Track LEFT, Fence E.
Definition: rail.h:102
static void DrawTrackDetails(const TileInfo *ti, const RailtypeInfo *rti)
Draw track fences.
Definition: rail_cmd.cpp:2024
Common return value for all commands.
Definition: command_type.h:23
static bool HasExactlyOneBit(T value)
Test whether value has exactly 1 bit set.
CursorID autorail
Cursor for autorail tool.
Definition: rail.h:166
static Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:99
RailType AllocateRailType(RailTypeLabel label)
Allocate a new rail type label.
Definition: rail_cmd.cpp:158
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:84
"Arrow" to the north-east
Definition: track_type.h:49
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:342
static uint GetSignalStates(TileIndex tile)
Set the states of the signals (Along/AgainstTrackDir)
Definition: rail_map.h:362
byte vehstatus
Status.
Definition: vehicle_base.h:315
static DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:447
struct RailtypeInfo::@39 strings
Strings associated with the rail type.
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
SpriteID track_y
single piece of rail in Y direction, with ground
Definition: rail.h:131
static CommandCost TestAutoslopeOnRailTile(TileIndex tile, uint flags, int z_old, Slope tileh_old, int z_new, Slope tileh_new, TrackBits rail_bits)
Tests if autoslope is allowed.
Definition: rail_cmd.cpp:3008
a flat tile
Definition: slope_type.h:49
uint16 rail_speed
Speed limit of rail (bridges and track)
Definition: tile_cmd.h:64
int z
Height.
Definition: tile_cmd.h:47
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:13
void DrawFoundation(TileInfo *ti, Foundation f)
Draw foundation f at tile ti.
Definition: landscape.cpp:470
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Definition: command_type.h:62
Owner owner[4]
Name of the owner(s)
Definition: tile_cmd.h:53
static Corner GetHighestSlopeCorner(Slope s)
Returns the highest corner of a slope (one corner raised or a steep slope).
Definition: slope_func.h:126
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
CursorID rail_ew
Cursor for building rail in E-W direction.
Definition: rail.h:164
Grass with a fence at the NW and SE edges.
Definition: rail_map.h:490
static TrackBits CornerToTrackBits(Corner corner)
Returns a single horizontal/vertical trackbit that is in a specific tile corner.
Definition: track_func.h:107
void MakeError(StringID message)
Makes this CommandCost behave like an error command.
Definition: command_type.h:100
static bool IsObjectType(TileIndex t, ObjectType type)
Check whether the object on a tile is of a specific type.
Definition: object_map.h:25
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
Use this function to notify YAPF that track layout (or signal configuration) has change.
Definition: yapf_rail.cpp:642
north and east corner are raised
Definition: slope_type.h:58
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:124
static bool HasRoadWorks(TileIndex t)
Check if a tile has road works.
Definition: road_map.h:513
No rail types.
Definition: rail_type.h:47
Date build_date
Date of construction.
Definition: depot_base.h:25
Right track.
Definition: track_type.h:45
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
static byte SignalOnTrack(Track track)
Maps a Track to the bits that store the status of the two signals that can be present on the given tr...
Definition: signal_func.h:42
company bankrupts, skip money check, skip vehicle on tile check in some cases
Definition: command_type.h:350
Functions related to (drawing on) viewports.
Normal rail tile with signals.
Definition: rail_map.h:25
X-Y-axis cross.
Definition: track_type.h:46
SpriteID signals[SIGTYPE_END][2][2]
signal GUI sprites (type, variant, state)
Definition: rail.h:158
static bool HasStationRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:135
static Foundation SpecialRailFoundation(Corner corner)
Returns the special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:403
int16 y
The y value of the coordinate.
Definition: map_type.h:59
RailFenceOffset
Offsets from base sprite for fence sprites.
Definition: rail.h:99
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
static Track TrackBitsToTrack(TrackBits tracks)
Converts TrackBits to Track.
Definition: track_func.h:201
static uint GetPresentSignals(TileIndex tile)
Get whether the given signals are present (Along/AgainstTrackDir)
Definition: rail_map.h:393
Track in the right corner of the tile (east)
Definition: track_type.h:26
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
SpriteID single_y
single piece of rail in Y direction, without ground
Definition: rail.h:135
build a single rail track
Definition: command_type.h:178
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:264
static bool IsBridgeAbove(TileIndex t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
Snow only on higher part of slope (steep or one corner raised)
Definition: rail_map.h:500
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:214
static void DrawRailTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32 total_offset, uint32 newgrf_offset, PaletteID default_palette)
Draw tile sprite sequence in GUI with railroad specifics.
Definition: sprite.h:99
The tile does not flood neighboured tiles.
Definition: water.h:20
static Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition: slope_func.h:76
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:43
static CommandCost CmdSignalTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build many signals by dragging; AutoSignals.
Definition: rail_cmd.cpp:1275
None of the directions are disallowed.
Definition: road_map.h:286
Electric rails.
Definition: rail_type.h:30
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:341
Querying information about the upper part of a tile with halftile foundation.
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
SpriteID ground
ground sprite for a 3-way switch
Definition: rail.h:133
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
All the railtype-specific information is stored here.
void MakeDefaultName(T *obj)
Set the default name for a depot/waypoint.
Definition: town.h:236
Southeast.
Iterator to iterate over a diagonal area of the map.
TileIndex tile
Tile index.
Definition: tile_cmd.h:46
static uint ApplyPixelFoundationToSlope(Foundation f, Slope *s)
Applies a foundation to a slope.
Definition: landscape.h:129
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:441
The y axis.
static RoadBits GetRoadBits(TileIndex t, RoadTramType rtt)
Get the present road bits for a specific road type.
Definition: road_map.h:127
The tile is leveled up to a flat slope.
Definition: slope_type.h:95
byte train_signal_side
show signals on left / driving / right side
Level north halftile non-continuously.
Definition: slope_type.h:105
static bool IsValidTrack(Track track)
Checks if a Track is valid.
Definition: track_func.h:36
static bool IsRailStationTile(TileIndex t)
Is this tile a station tile and a rail station?
Definition: station_map.h:102
Header file for things common for tunnels and bridges.
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:58
#define FIND_FIRST_BIT(x)
Returns the first non-zero bit in a 6-bit value (from right).
Corner height_ref
Corner to use height offset from.
Definition: rail_cmd.cpp:1931
void DirtyCompanyInfrastructureWindows(CompanyID company)
Redraw all windows with company infrastructure counts.
FloodingBehaviour GetFloodingBehaviour(TileIndex tile)
Returns the behaviour of a tile during flooding.
Definition: water_cmd.cpp:1056
byte road_side
the side of the road vehicles drive on
Images for overlaying track.
Definition: rail.h:48
static TownID GetTownIndex(TileIndex t)
Get the index of which town this house/street is attached to.
Definition: town_map.h:22
Slope SE, Track Y, Fence SW.
Definition: rail.h:113
Entry point for OpenTTD to YAPF&#39;s cache.
CommandCost CmdConvertRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Convert one rail type to the other.
Definition: rail_cmd.cpp:1565
StringID GetErrorMessage() const
Returns the error message of a command.
Definition: command_type.h:140
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:61
Track right, direction north.
Definition: track_type.h:115
Grass with a fence at the NW edge.
Definition: rail_map.h:488
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore)
Finds vehicle in tunnel / bridge.
Definition: vehicle.cpp:566
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
Slope FLAT, Track UPPER, Fence S.
Definition: rail.h:103
static DiagDirection GetRailDepotDirection(TileIndex t)
Returns the direction the depot is facing to.
Definition: rail_map.h:171
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
DoCommandFlag
List of flags for a command.
Definition: command_type.h:342
static uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles)
Definition: tunnelbridge.h:25
T * Next() const
Get next vehicle in the chain.
Piece of rail on slope with south-west raised.
Definition: rail.h:76
The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/...
Definition: tile_cmd.h:36
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
Standard non-electric rails.
Definition: rail_type.h:29
Piece of rail in X direction.
Definition: rail.h:68
bool IsSafeWaitingPosition(const Train *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg)
Determine whether a certain track on a tile is a safe position to end a path.
Definition: pbs.cpp:381
static const uint32 VALID_LEVEL_CROSSING_SLOPES
Constant bitset with safe slopes for building a level crossing.
Definition: slope_type.h:86
Left and right track.
Definition: track_type.h:48
bool Succeeded() const
Did this command succeed?
Definition: command_type.h:150
Track left, direction south.
Definition: track_type.h:107
Definition of base types and functions in a cross-platform compatible way.
CommandCost CmdRemoveSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove signals.
Definition: rail_cmd.cpp:1462
Track y-axis, direction south-east.
Definition: track_type.h:104
Represents a diagonal tile area.
Definition: tilearea_type.h:64
static TrackBits GetRailReservationTrackBits(TileIndex t)
Returns the reserved track bits of the tile.
Definition: rail_map.h:194
static CommandCost CmdRailTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build or remove a stretch of railroad tracks.
Definition: rail_cmd.cpp:879
Map accessors for object tiles.
static const uint BB_HEIGHT_UNDER_BRIDGE
Some values for constructing bounding boxes (BB).
Definition: viewport_type.h:92
A number of safeguards to prevent using unsafe methods.
SpriteID build_ns_rail
button for building single rail in N-S direction
Definition: rail.h:150
int16 x
The x value of the coordinate.
Definition: map_type.h:58
Flag for an invalid trackdir.
Definition: track_type.h:89
static PaletteID GroundSpritePaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
Applies PALETTE_MODIFIER_COLOUR to a palette entry of a ground sprite.
Definition: sprite.h:168
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced. ...
Definition: rail.h:263
The vehicle cannot enter the tile.
Definition: tile_cmd.h:37
Water tile.
Definition: tile_type.h:47
static Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
Removes first Trackdir from TrackdirBits and returns it.
Definition: track_func.h:164
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:44
Foundation GetRailFoundation(Slope tileh, TrackBits bits)
Checks if a track combination is valid on a specific slope and returns the needed foundation...
Definition: rail_cmd.cpp:329
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:50
byte sorting_order
The sorting order of this railtype for the toolbar dropdown.
Definition: rail.h:268
void ShowDepotWindow(TileIndex tile, VehicleType type)
Opens a depot window.
Definition: depot_gui.cpp:1096
static bool IsDockingTile(TileIndex t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:365
Slope SW, Track X, Fence NW.
Definition: rail.h:104
The tile has an along Y-axis inclined foundation.
Definition: slope_type.h:97
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:280
Grass with a fence at the NE edge.
Definition: rail_map.h:491
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:16
Train * GetTrainForReservation(TileIndex tile, Track track)
Find the train which has reserved a specific path.
Definition: pbs.cpp:331
No road-part is build.
Definition: road_type.h:51
Bit number for using non-combined junctions.
Definition: rail.h:29
static bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition: slope_func.h:88
Represents the covered area of e.g.
Definition: tilearea_type.h:16
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:211
PBS support routines.
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:38
static void SetSignalStates(TileIndex tile, uint state)
Set the states of the signals (Along/AgainstTrackDir)
Definition: rail_map.h:352
uint16 max_speed
Maximum speed for vehicles travelling on this rail type.
Definition: rail.h:228
void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition: vehicle.cpp:496
SpriteID single_w
single piece of rail in the western corner
Definition: rail.h:139
TileIndex tile
Current tile index.
Definition: vehicle_base.h:228
SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
Get the sprite to draw for a given signal.
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
static Money RoadBuildCost(RoadType roadtype)
Returns the cost of building the specified roadtype.
Definition: road.h:249
static const ObjectType OBJECT_OWNED_LAND
Owned land &#39;flag&#39;.
Definition: object_type.h:19
CursorID rail_nwse
Cursor for building rail in Y direction.
Definition: rail.h:165
static void SetPresentSignals(TileIndex tile, uint signals)
Set whether the given signals are present (Along/AgainstTrackDir)
Definition: rail_map.h:383
Grass with a fence and shore or water on the free halftile.
Definition: rail_map.h:499
Base class for tile iterators.
Definition: tilearea_type.h:99
static Slope SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition: slope_func.h:206
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:1938
Sprites to use and how to display them for train depot tiles.
Crossing of X and Y rail, with ballast.
Definition: rail.h:78
SpriteID single_x
single piece of rail in X direction, without ground
Definition: rail.h:134
SpriteID convert_rail
button for converting rail
Definition: rail.h:157
static Track GetRailDepotTrack(TileIndex t)
Returns the track of a depot, ignoring direction.
Definition: rail_map.h:182
static Money RailClearCost(RailType railtype)
Returns the &#39;cost&#39; of clearing the specified railtype.
Definition: rail.h:383
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
bit mask containing all &#39;simple&#39; slopes
Definition: slope_type.h:61
static void DrawTrackFence_NE(const TileInfo *ti, SpriteID base_image, uint num_sprites)
Draw fence at NE border matching the tile slope.
Definition: rail_cmd.cpp:2002
Depot images.
Definition: rail.h:55
Track left, direction north.
Definition: track_type.h:114
static TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
Maps a trackdir to the trackdirs that can be reached from it (ie, when entering the next tile...
Definition: track_func.h:592
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
static Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:369
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:89
StringID railtype
Type of rail on the tile.
Definition: tile_cmd.h:63
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:233
The tile has a steep slope. The lowest corner is raised by a foundation and the upper halftile is lev...
Definition: slope_type.h:101
CommandCost CmdBuildTrainDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build a train depot.
Definition: rail_cmd.cpp:975
RailTypes
The different railtypes we support, but then a bitmask of them.
Definition: rail_type.h:46
All flags cleared.
Definition: rail.h:33
Piece of rail in Y direction.
Definition: rail.h:69
static CommandCost CheckRailSlope(Slope tileh, TrackBits rail_bits, TrackBits existing, TileIndex tile)
Tests if a track can be build on a tile.
Definition: rail_cmd.cpp:409
Functions related to autoslope.
Functions related to sound.
static DiagDirection GetTunnelBridgeDirection(TileIndex t)
Get the direction pointing to the other end.
static bool IsPlainRailTile(TileIndex t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition: rail_map.h:60
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
static bool AutoslopeEnabled()
Tests if autoslope is enabled for _current_company.
Definition: autoslope.h:44
static TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
Builds a TrackStatus.
Definition: track_func.h:396
bool build_on_slopes
allow building on slopes
bool Failed() const
Did this command fail?
Definition: command_type.h:159
Piece of rail on slope with south-east raised.
Definition: rail.h:75
Slope FLAT, Track X, Fence SE.
Definition: rail.h:108
SpriteID single_n
single piece of rail in the northern corner
Definition: rail.h:136
void UpdateLevelCrossing(TileIndex tile, bool sound=true)
Sets correct crossing state.
Definition: train_cmd.cpp:1672
Piece of rail in eastern corner.
Definition: rail.h:72
east and west corner are raised
Definition: slope_type.h:59
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:659
static Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:255
build a signal
Definition: command_type.h:184
static Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:60
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:33
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:589
SpriteID single_s
single piece of rail in the southern corner
Definition: rail.h:137
Piece of rail in northern corner.
Definition: rail.h:70
static bool HasSignals(TileIndex t)
Checks if a rail tile has signals.
Definition: rail_map.h:72
Slope NE, Track X, Fence NW.
Definition: rail.h:106
SpriteID build_depot
button for building depots
Definition: rail.h:155
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:85
CursorID depot
Cursor for building a depot.
Definition: rail.h:167
static Vehicle * EnsureNoShipProc(Vehicle *v, void *data)
Test-procedure for HasVehicleOnPos to check for a ship.
Definition: rail_cmd.cpp:3050
CommandCost CmdRemoveSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Remove a single piece of track.
Definition: rail_cmd.cpp:618
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:36
static RoadBits GetCrossingRoadBits(TileIndex tile)
Get the road bits of a level crossing.
Definition: road_map.h:348
Complete ground images.
Definition: rail.h:59
convert a rail type
Definition: command_type.h:191
A pair-construct of a TileIndexDiff.
Definition: map_type.h:57
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:136
Bit number for hiding from selection.
Definition: rail.h:28
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
Definition: company_base.h:131
The X axis.
uint32 rail[RAILTYPE_END]
Count of company owned track bits for each rail type.
Definition: company_base.h:32
Bitflag for a depot.
Definition: track_type.h:56
CursorID convert
Cursor for converting track.
Definition: rail.h:169
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Lower track.
Definition: track_type.h:43
static TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
Return the offset between to tiles from a TileIndexDiffC struct.
Definition: map_func.h:230
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:344
static bool CompareRailTypes(const RailType &first, const RailType &second)
Compare railtypes based on their sorting order.
Definition: rail_cmd.cpp:130
static TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:85
The tile/execution is done by "water".
Definition: company_type.h:26
static CommandCost EnsureNoTrainOnTrack(TileIndex tile, Track track)
Tests if a vehicle interacts with the specified track.
Definition: rail_cmd.cpp:237
static int GetSlopePixelZInCorner(Slope tileh, Corner corner)
Determine the Z height of a corner relative to TileZ.
Definition: landscape.h:53
static byte SignalAgainstTrackdir(Trackdir trackdir)
Maps a trackdir to the bit that stores its status in the map arrays, in the direction against the tra...
Definition: signal_func.h:32
No track.
Definition: track_type.h:39
PalSpriteID ground
Palette and sprite for the ground.
Definition: sprite.h:59
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:92
static Owner GetRoadOwner(TileIndex t, RoadTramType rtt)
Get the owner of a specific road type.
Definition: road_map.h:233
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:26
Track along the y-axis (north-west to south-east)
Definition: track_type.h:22
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:27
static bool IsSlopeWithThreeCornersRaised(Slope s)
Tests if a specific slope has exactly three corners raised.
Definition: slope_func.h:195
Track lower, direction east.
Definition: track_type.h:106
Trackdir GetVehicleTrackdir() const
Get the tracks of the train vehicle.
Definition: train_cmd.cpp:4005
static bool IsRailDepot(TileIndex t)
Is this rail tile a rail depot?
Definition: rail_map.h:95
bool IsStationTileBlocked(TileIndex tile)
Check whether a rail station tile is NOT traversable.
GUISettings gui
settings related to the GUI
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:50
static Track RemoveFirstTrack(TrackBits *tracks)
Removes first Track from TrackBits and returns it.
Definition: track_func.h:139
static bool IsRailDepotTile(TileIndex t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:105
static Vehicle * UpdateTrainPowerProc(Vehicle *v, void *data)
Update power of train under which is the railtype being converted.
Definition: rail_cmd.cpp:1543
static bool HasTunnelBridgeReservation(TileIndex t)
Get the reservation state of the rail tunnel/bridge.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
CursorID rail_swne
Cursor for building rail in X direction.
Definition: rail.h:163
Slope FLAT, Track RIGHT, Fence W.
Definition: rail.h:110
Upper track.
Definition: track_type.h:42
Set of callback functions for performing tile operations of a given tile type.
Definition: tile_cmd.h:145
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1587
Used for iterations.
static byte SignalAlongTrackdir(Trackdir trackdir)
Maps a trackdir to the bit that stores its status in the map arrays, in the direction along with the ...
Definition: signal_func.h:22
static const PaletteID PALETTE_TO_BARE_LAND
sets colour to bare land stuff for rail, road and crossings
Definition: sprites.h:1575
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set...
int x_offs
Bounding box X offset.
Definition: rail_cmd.cpp:1932
static bool RoadNoLevelCrossing(RoadType roadtype)
Test if road disallows level crossings.
Definition: road.h:292
static bool IsLevelCrossingTile(TileIndex t)
Return whether a tile is a level crossing tile.
Definition: road_map.h:94
north and west corner are raised
Definition: slope_type.h:55
static Money RailBuildCost(RailType railtype)
Returns the cost of building the specified railtype.
Definition: rail.h:372
Track
These are used to specify a single track.
Definition: track_type.h:19
Cursor and toolbar icon images.
Definition: rail.h:47
static void DrawTrackFence_SW(const TileInfo *ti, SpriteID base_image, uint num_sprites)
Draw fence at SW border matching the tile slope.
Definition: rail_cmd.cpp:2012
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
Bit sets of the above specified bits.
Definition: tile_cmd.h:34
static Trackdir TrackToTrackdir(Track track)
Returns a Trackdir for the given Track.
Definition: track_func.h:287
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:112
int TicksToLeaveDepot(const Train *v)
Compute number of ticks when next wagon will leave a depot.
Definition: rail_cmd.cpp:2930
Old-fashioned semaphore signal.
Definition: signal_type.h:18
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:81
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
TrackBits GetReservedTrackbits(TileIndex t)
Get the reserved trackbits for any tile, regardless of type.
Definition: pbs.cpp:24
void FreeTrainTrackReservation(const Train *v)
Free the reserved path in front of a vehicle.
Definition: train_cmd.cpp:2229
The tile has an along X-axis inclined foundation.
Definition: slope_type.h:96
TransportType
Available types of transport.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Used inside "rail_cmd.cpp" to indicate invalid slope/track combination.
Definition: slope_type.h:113
int x_size
Bounding box X size.
Definition: rail_cmd.cpp:1934
static bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Definition: track_func.h:639
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
int y_size
Bounding box Y size.
Definition: rail_cmd.cpp:1935
A tile of a station.
Definition: tile_type.h:46
SpriteID build_tunnel
button for building a tunnel
Definition: rail.h:156
void DrawRailCatenary(const TileInfo *ti)
Draws overhead wires and pylons for electric railways.
Definition: elrail.cpp:562
Ballast for junction &#39;pointing&#39; SE.
Definition: rail.h:81
"Arrow" to the south-east
Definition: track_type.h:50
static bool HasTrack(TileIndex tile, Track track)
Returns whether the given track is present on the given tile.
Definition: rail_map.h:160
Normal rail tile without signals.
Definition: rail_map.h:24
Ballast for junction &#39;pointing&#39; SW.
Definition: rail.h:79
bool show_track_reservation
highlight reserved tracks.
Used to only draw a part of the sprite.
Definition: gfx_type.h:217
Main group of ground images.
Definition: rail.h:49
Slope SE, Track Y, Fence NE.
Definition: rail.h:105
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:261
SignalOffsets
Enum holding the signal offset in the sprite sheet according to the side it is representing.
Definition: rail_cmd.cpp:49
static bool IsCompatibleRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType can drive on a tile with a given RailType.
Definition: rail.h:319
RailTypes powered_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype generates power ...
Definition: rail.h:185
Track x-axis, direction south-west.
Definition: track_type.h:110
uint GetRailtypeSpriteOffset() const
Offset between the current railtype and normal rail.
Definition: rail.h:292
Track y-axis, direction north-west.
Definition: track_type.h:111
Vehicle is not visible.
Definition: vehicle_base.h:30
static uint CountBits(T value)
Counts the number of set bits in a variable.
The signal is red.
Definition: signal_type.h:45
Functions related to commands.
Coordinates of a point in 2D.
remove a single rail track
Definition: command_type.h:179
struct RailtypeInfo::@37 gui_sprites
struct containing the sprites for the rail GUI.
Iterator to iterate over a tile area (rectangle) of the map.
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
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:575
byte GetSnowLine()
Get the current snow line, either variable or static.
Definition: landscape.cpp:644
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
static bool HasRailCatenaryDrawn(RailType rt)
Test if we should draw rail catenary.
Definition: elrail_func.h:30
Date build_date
Date of construction of tile contents.
Definition: tile_cmd.h:55
uint8 train_acceleration_model
realistic acceleration for trains
header file for electrified rail specific functions
Slope SW, Track X, Fence SE.
Definition: rail.h:112
The tile has a steep slope. The lowest corner is raised by a foundation to allow building railroad on...
Definition: slope_type.h:98
RailGroundType
The ground &#39;under&#39; the rail.
Definition: rail_map.h:485
static uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:415
ConstructionSettings construction
construction of things in-game
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
Functions that have tunnels and bridges in common.
static DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
Gets the disallowed directions.
Definition: road_map.h:301
static bool IsNormalRoad(TileIndex t)
Return whether a tile is a normal road.
Definition: road_map.h:63
int GetSlopeZInCorner(Slope tileh, Corner corner)
Determine the Z height of a corner relative to TileZ.
Definition: landscape.cpp:375
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:31
StringID name
Name of this rail type.
Definition: rail.h:173
Track lower, direction west.
Definition: track_type.h:113
Grass with a fence at the SE edge.
Definition: rail_map.h:489
remove a signal
Definition: command_type.h:185
don&#39;t allow overlap of rails (used in buildrail)
Definition: command_type.h:348
StringID str
Description of the tile.
Definition: tile_cmd.h:52
Used for iterations.
Definition: rail_type.h:33
SpriteID tunnel
tunnel sprites base
Definition: rail.h:142
CursorID rail_ns
Cursor for building rail in N-S direction.
Definition: rail.h:162
Slope NE, Track X, Fence SE.
Definition: rail.h:114
int32 x_pos
x coordinate.
Definition: vehicle_base.h:266
static bool HasReservedTracks(TileIndex tile, TrackBits tracks)
Check whether some of tracks is reserved on a tile.
Definition: pbs.h:58
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static void DrawTrackBits(TileInfo *ti, TrackBits track)
Draw ground sprite and track bits.
Definition: rail_cmd.cpp:2251
DiagDirection
Enumeration for diagonal directions.
void Restore()
Restore the variable.
static TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition: track_func.h:327
Light signal.
Definition: signal_type.h:17
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
Base of the town class.
Track upper, direction east.
Definition: track_type.h:105
Northeast, upper right on your monitor.
SpriteID track_ns
two pieces of rail in North and South corner (East-West direction)
Definition: rail.h:132
GameCreationSettings game_creation
settings used during the creation of a game (map)
static bool IsPlainRail(TileIndex t)
Returns whether this is plain rails, with or without signals.
Definition: rail_map.h:49
A house by a town.
Definition: tile_type.h:44
Full road along the y-axis (north-west + south-east)
Definition: road_type.h:57
static Track GetCrossingRailTrack(TileIndex tile)
Get the rail track of a level crossing.
Definition: road_map.h:358
int32 y_pos
y coordinate.
Definition: vehicle_base.h:267
Ballast for full junction.
Definition: rail.h:83
Slope FLAT, Track X, Fence NW.
Definition: rail.h:100
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:59
static TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition: track_func.h:316
std::vector< Train * > TrainList
Helper type for lists/vectors of trains.
Definition: rail_cmd.cpp:42
static bool HasDepotReservation(TileIndex t)
Get the reservation state of the depot.
Definition: rail_map.h:258
static CommandCost CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags)
Check that the new track bits may be built.
Definition: rail_cmd.cpp:250
Functions related to water (management)
static bool IsNonContinuousFoundation(Foundation f)
Tests if a foundation is a non-continuous foundation, i.e.
Definition: slope_func.h:320
Track x-axis, direction north-east.
Definition: track_type.h:103
SpriteID build_x_rail
button for building single rail in X direction
Definition: rail.h:151
VehicleEnterTileStatus
The returned bits of VehicleEnterTile.
Definition: tile_cmd.h:20
Grass with a fence at the southern side.
Definition: rail_map.h:496
static TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
Add a TileIndexDiffC to a TileIndex and returns the new one.
Definition: map_func.h:300
static void SetTrackReservation(TileIndex t, TrackBits b)
Sets the reserved track bits of the tile.
Definition: rail_map.h:209
SpriteID sprite
The &#39;real&#39; sprite.
Definition: gfx_type.h:23
Slope NW, Track Y, Fence SW.
Definition: rail.h:115
Track in the left corner of the tile (west)
Definition: track_type.h:25
SignalVariant
Variant of the signal, i.e.
Definition: signal_type.h:16
static void MakeRoadNormal(TileIndex t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
Make a normal road tile.
Definition: road_map.h:635
CommandCost CmdBuildRailroadTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build rail on a stretch of track.
Definition: rail_cmd.cpp:939
Date _date
Current date in days (day counter)
Definition: date.cpp:26
bool TryPathReserve(Train *v, bool mark_as_stuck=false, bool first_tile_okay=false)
Try to reserve a path to a safe position.
Definition: train_cmd.cpp:2667
static Direction ReverseDir(Direction d)
Return the reverse of a direction.
normal signal
Definition: signal_type.h:24
static void SetDockingTile(TileIndex t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:355
SpriteID build_ew_rail
button for building single rail in E-W direction
Definition: rail.h:152
the south corner of the tile is raised
Definition: slope_type.h:51
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:44
Y-axis track.
Definition: track_type.h:41
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Delete inspect window for a given feature and index.
static bool IsSpecialRailFoundation(Foundation f)
Tests if a foundation is a special rail foundation for single horizontal/vertical track...
Definition: slope_func.h:345
Valid changes while vehicle is driving, and possibly changing tracks.
Definition: train.h:48
static TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:360
void DrawBridgeMiddle(const TileInfo *ti)
Draw the middle bits of a bridge.
Class for backupping variables and making sure they are restored later.
"Arrow" to the north-west
Definition: track_type.h:52
Used for iterations.
Definition: rail_type.h:28
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:835
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:115
static Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition: slope_func.h:184
static bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:653
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:316
static bool IsBridge(TileIndex t)
Checks if this is a bridge, instead of a tunnel.
Definition: bridge_map.h:24
static Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.
Grass with a fence at the western side.
Definition: rail_map.h:495
Track right, direction south.
Definition: track_type.h:108
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:3316
The signal is green.
Definition: signal_type.h:46
Track in the upper corner of the tile (north)
Definition: track_type.h:23
Slope FLAT, Track Y, Fence SW.
Definition: rail.h:109
SpriteID snow_offset
sprite number difference between a piece of track on a snowy ground and the corresponding one on norm...
Definition: rail.h:182
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:125
Southwest.
Upper and lower track.
Definition: track_type.h:47
std::vector< RailTypeLabel > RailTypeLabelList
List of rail type labels.
Definition: rail.h:119
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:304