OpenTTD Source: src/rail_cmd.cpp Source File

OpenTTD Source 13.2.1

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