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