OpenTTD Source 20261112-master-gbcbec5e63f
elrail.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
54
55#include "stdafx.h"
56#include "bridge.h"
57#include "station_map.h"
58#include "viewport_func.h"
59#include "train.h"
60#include "rail_gui.h"
61#include "tunnelbridge_map.h"
62#include "tunnelbridge.h"
63#include "elrail_func.h"
64#include "company_base.h"
65#include "newgrf_railtype.h"
66
67#include "table/elrail_data.h"
68
69#include "safeguards.h"
70
77{
78 return static_cast<TileLocationGroup>((GB(TileX(t), 0, 1) << 1) + GB(TileY(t), 0, 1));
79}
80
88{
89 switch (GetTileType(t)) {
91 if (!HasRailCatenary(GetRailType(t))) return {};
92 switch (GetRailTileType(t)) {
95 return GetTrackBits(t);
96 default:
97 return {};
98 }
99 break;
100
103 if (!HasRailCatenary(GetRailType(t))) return {};
104 if (override != nullptr && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
105 *override = GetTunnelBridgeDirection(t);
106 }
108
109 case TileType::Road:
110 if (!IsLevelCrossing(t)) return {};
111 if (!HasRailCatenary(GetRailType(t))) return {};
112 return GetCrossingRailTrack(t);
113
115 if (!HasStationRail(t)) return {};
116 if (!HasRailCatenary(GetRailType(t))) return {};
117 return GetRailStationTrack(t);
118
119 default:
120 return {};
121 }
122}
123
131{
132 /* Single track bits are never masked out. */
133 if (tracks.Count() <= 1) [[likely]] return tracks;
134
135 if (!IsPlainRailTile(t)) return tracks;
136
137 TrackdirBits neighbour_tdb{};
139 /* If the neighbour tile is either not electrified or has no tracks that can be reached
140 * from this tile, mark all trackdirs that can be reached from the neighbour tile
141 * as needing no catenary. We make an exception for blocked station tiles with a matching
142 * axis that still display wires to preserve visual continuity. */
143 TileIndex next_tile = TileAddByDiagDir(t, d);
144 RailType rt = GetTileRailType(next_tile);
145 if (rt == INVALID_RAILTYPE || !HasRailCatenary(rt) ||
147 (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
148 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
149 }
150 }
151
152 /* If the tracks from either a diagonal crossing or don't overlap, both
153 * trackdirs have to be marked to mask the corresponding track bit. Else
154 * one marked trackdir is enough the mask the track bit. */
155 TrackBits mask;
156 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
157 /* If the tracks form either a diagonal crossing or don't overlap, both
158 * trackdirs have to be marked to mask the corresponding track bit. */
159 mask = static_cast<TrackBits>(~((neighbour_tdb.base() & (neighbour_tdb.base() >> 8)) & TRACK_BIT_ALL.base()));
160 /* If that results in no masked tracks and it is not a diagonal crossing,
161 * require only one marked trackdir to mask. */
162 if (tracks != TRACK_BIT_CROSS && mask == TRACK_BIT_ALL) mask = TrackdirBitsToTrackBits(neighbour_tdb).Flip(TRACK_BIT_ALL);
163 } else {
164 /* Require only one marked trackdir to mask the track. */
165 mask = TrackdirBitsToTrackBits(neighbour_tdb).Flip(TRACK_BIT_ALL);
166 /* If that results in an empty set, require both trackdirs for diagonal track. */
167 if (!tracks.Any(mask)) {
168 if (!neighbour_tdb.Test(Trackdir::X_NE) || !neighbour_tdb.Test(Trackdir::X_SW)) mask.Set(Track::X);
169 if (!neighbour_tdb.Test(Trackdir::Y_NW) || !neighbour_tdb.Test(Trackdir::Y_SE)) mask.Set(Track::Y);
170 /* If that still is not enough, require both trackdirs for any track. */
171 if (!tracks.Any(mask)) mask = static_cast<TrackBits>(~((neighbour_tdb.base() & (neighbour_tdb.base() >> 8)) & TRACK_BIT_ALL.base()));
172 }
173 }
174
175 /* Mask the tracks only if at least one track bit would remain. */
176 return tracks.Any(mask) ? tracks & mask : tracks;
177}
178
186{
187 const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
188 SpriteID wires = GetCustomRailSprite(rti, tile, RailSpriteType::Wires, context);
189 return wires == 0 ? SPR_WIRE_BASE : wires;
190}
191
199{
200 const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
201 SpriteID pylons = GetCustomRailSprite(rti, tile, RailSpriteType::Pylons, context);
202 return pylons == 0 ? SPR_PYLON_BASE : pylons;
203}
204
210static void AdjustTileh(TileIndex tile, Slope *tileh)
211{
213 if (IsTunnel(tile)) {
214 *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
215 } else if (*tileh != SLOPE_FLAT) {
216 *tileh = SLOPE_FLAT;
217 } else {
219 }
220 }
221}
222
230static int GetPCPElevation(TileIndex tile, DiagDirection pcp_pos)
231{
232 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
233 * PCPs are always on a tile edge.
234 *
235 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
236 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
237 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
238 *
239 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
240 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
241 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
242 *
243 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
244 */
245
246 int z = GetSlopePixelZ(TileX(tile) * TILE_SIZE + std::min<int8_t>(_x_pcp_offsets[pcp_pos], TILE_SIZE - 1),
247 TileY(tile) * TILE_SIZE + std::min<int8_t>(_y_pcp_offsets[pcp_pos], TILE_SIZE - 1), true);
248 /* Round the Z to the nearest half tile height. */
249 static const uint HALF_TILE_HEIGHT = TILE_HEIGHT / 2;
250 return (z + HALF_TILE_HEIGHT / 2) / HALF_TILE_HEIGHT * HALF_TILE_HEIGHT;
251}
252
261{
263
264 SpriteID wire_base = GetWireBase(ti->tile);
265
267 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, GetTilePixelZ(ti->tile), sss, IsTransparencySet(TransparencyOption::Catenary));
268}
269
274static void DrawRailCatenaryRailway(const TileInfo *ti)
275{
276 /* Pylons are placed on a tile edge, so we need to take into account
277 * the track configuration of 2 adjacent tiles. */
281 /* Note that ti->tileh has already been adjusted for Foundations */
283
284 /* Half tile slopes coincide only with horizontal/vertical track.
285 * Faking a flat slope results in the correct sprites on positions. */
286 Corner halftile_corner = Corner::Invalid;
287 if (IsHalftileSlope(tileh[TileSource::Home])) {
288 halftile_corner = GetHalftileSlopeCorner(tileh[TileSource::Home]);
290 }
291
293 DiagDirections pcp_status{};
294 DiagDirections override_pcp{};
297
298 /* Find which rail bits are present, and select the override points.
299 * We don't draw a pylon:
300 * 1) INSIDE a tunnel (we wouldn't see it anyway)
301 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
302 * because that one is drawn on the bridge. Exception is for length 0 bridges
303 * which have no middle tiles */
304 track_config[TileSource::Home] = GetRailTrackBitsUniversal(ti->tile, &override_pcp);
305 wire_config[TileSource::Home] = MaskWireBits(ti->tile, track_config[TileSource::Home]);
306 /* If a track bit is present that is not in the main direction, the track is level */
308
309 AdjustTileh(ti->tile, &tileh[TileSource::Home]);
310
311 SpriteID pylon_normal = GetPylonBase(ti->tile);
312 SpriteID pylon_halftile = IsValidCorner(halftile_corner) ? GetPylonBase(ti->tile, TileContext::UpperHalftile) : pylon_normal;
313
315 SpriteID pylon_base = (IsValidCorner(halftile_corner) && HasBit(InclinedSlope(i), to_underlying(halftile_corner))) ? pylon_halftile : pylon_normal;
316 TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
317 int elevation = GetPCPElevation(ti->tile, i);
318
319 /* Here's one of the main headaches. GetTileSlope does not correct for possibly
320 * existing foundataions, so we do have to do that manually later on.*/
321 tileh[TileSource::Neighbour] = GetTileSlope(neighbour);
322 track_config[TileSource::Neighbour] = GetRailTrackBitsUniversal(neighbour, nullptr);
323 wire_config[TileSource::Neighbour] = MaskWireBits(neighbour, track_config[TileSource::Neighbour]);
324 if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = {};
325
326 /* Ignore station tiles that allow neither wires nor pylons. */
327 if (HasStationTileRail(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = {};
328
329 /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
330 * we have to draw all pillars on the current tile. */
331 if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = {};
332
334
335 ppp_preferred[i] = DIRECTIONS_ALL; // We start with preferring everything (end-of-line in any direction)
336 ppp_allowed[i] = _allowed_ppp_on_pcp[i];
337
338 /* We cycle through all the existing tracks at a PCP and see what
339 * PPPs we want to have, or may not have at all */
340 for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
341 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
343 IsBridgeTile(neighbour) &&
344 GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
345 continue;
346 }
347
348 /* We check whether the track in question (k) is present in the tile
349 * (TrackSourceTile) */
350 DiagDirection pcp_pos = i;
351 if (wire_config[_track_source_tile[i][k]].Test(_tracks_at_pcp[i][k])) {
352 /* track found, if track is in the neighbour tile, adjust the number
353 * of the PCP for preferred/allowed determination*/
354 pcp_pos = (_track_source_tile[i][k] == TileSource::Home) ? i : ReverseDiagDir(i);
355 pcp_status.Set(i); // This PCP is in use
356 ppp_preferred[i] &= _preferred_ppp_of_track_at_pcp[_tracks_at_pcp[i][k]][pcp_pos];
357 }
358
359 if (track_config[_track_source_tile[i][k]].Test(_tracks_at_pcp[i][k])) {
360 ppp_allowed[i].Reset(_disallowed_ppp_of_track_at_pcp[_tracks_at_pcp[i][k]][pcp_pos]);
361 }
362 }
363
364 /* Deactivate all PPPs if PCP is not used */
365 if (!pcp_status.Test(i)) {
366 ppp_preferred[i].Reset();
367 ppp_allowed[i].Reset();
368 }
369
370 Foundation foundation = Foundation::None;
371
372 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
374
375 /* Read the foundations if they are present, and adjust the tileh */
376 if (track_config[TileSource::Neighbour].Any() && IsTileType(neighbour, TileType::Railway) && HasRailCatenary(GetRailType(neighbour))) foundation = GetRailFoundation(tileh[TileSource::Neighbour], track_config[TileSource::Neighbour]);
377 if (IsBridgeTile(neighbour)) {
379 }
380
382
383 /* Half tile slopes coincide only with horizontal/vertical track.
384 * Faking a flat slope results in the correct sprites on positions. */
386
387 AdjustTileh(neighbour, &tileh[TileSource::Neighbour]);
388
389 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
390 * Delete the PCP if this is the case.
391 * Level means that the slope is the same, or the track is flat */
392 if (tileh[TileSource::Home] == tileh[TileSource::Neighbour] || (is_flat[TileSource::Home] && is_flat[TileSource::Neighbour])) {
393 for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
394 if (ppp_preferred[i] == _ignored_pcp[k][tlg][i]) pcp_status.Reset(i);
395 }
396 }
397
398 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
399 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
400 * anything. Note that the preferred PPPs still contain the end-of-line markers.
401 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
402 if (ppp_allowed[i].Any(ppp_preferred[i])) ppp_allowed[i] &= ppp_preferred[i];
403
404 if (IsBridgeAbove(ti->tile)) {
405 Track bridgetrack = AxisToTrack(GetBridgeAxis(ti->tile));
406 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
407
408 if ((height <= GetTileMaxZ(ti->tile) + 1) &&
409 (i == _pcp_positions[bridgetrack][0] || i == _pcp_positions[bridgetrack][1])) {
410 override_pcp.Set(i);
411 }
412 }
413
414 if (ppp_allowed[i].Any() && pcp_status.Test(i) && !override_pcp.Test(i) &&
416
417 const auto &ppp_orders = _ppp_order[i][GetTileLocationGroup(ti->tile)];
419 Direction temp = ppp_orders[k];
420
421 if (ppp_allowed[i].Test(temp)) {
422 uint x = ti->x + _x_pcp_offsets[i] + _x_ppp_offsets[temp];
423 uint y = ti->y + _y_pcp_offsets[i] + _y_ppp_offsets[temp];
424
425 /* Don't build the pylon if it would be outside the tile */
426 if (!_owned_ppp_on_pcp[i].Test(temp)) {
427 /* We have a neighbour that will draw it, bail out */
428 if (track_config[TileSource::Neighbour].Any()) break;
429 continue; // No neighbour, go looking for a better position
430 }
431
432 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[temp], PAL_NONE, x, y, elevation,
433 {{-1, -1, 0}, {1, 1, BB_HEIGHT_UNDER_BRIDGE}, {1, 1, 0}}, IsTransparencySet(TransparencyOption::Catenary));
434
435 break; // We already have drawn a pylon, bail out
436 }
437 }
438 }
439 }
440
441 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawRailCatenaryOnTunnel()) */
442 if (IsTunnelTile(ti->tile)) return;
443
444 /* Don't draw a wire under a low bridge */
446 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
447
448 if (height <= GetTileMaxZ(ti->tile) + 1) return;
449 }
450
451 /* Don't draw a wire if the station tile does not want any */
452 if (HasStationTileRail(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
453
454 SpriteID wire_normal = GetWireBase(ti->tile);
455 SpriteID wire_halftile = IsValidCorner(halftile_corner) ? GetWireBase(ti->tile, TileContext::UpperHalftile) : wire_normal;
456 Track halftile_track;
457 switch (halftile_corner) {
458 case Corner::W: halftile_track = Track::Left; break;
459 case Corner::S: halftile_track = Track::Lower; break;
460 case Corner::E: halftile_track = Track::Right; break;
461 case Corner::N: halftile_track = Track::Upper; break;
462 default: halftile_track = Track::Invalid; break;
463 }
464
465 /* Drawing of pylons is finished, now draw the wires */
466 for (Track t : wire_config[TileSource::Home]) {
467 SpriteID wire_base = (t == halftile_track) ? wire_halftile : wire_normal;
468 uint8_t pcp_config = pcp_status.Test(_pcp_positions[t][0]) +
469 (pcp_status.Test(_pcp_positions[t][1]) << 1);
470 int tileh_selector = !(tileh[TileSource::Home] % 3) * tileh[TileSource::Home] / 3; // tileh for the slopes, 0 otherwise
471
472 assert(pcp_config != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
473 assert(!IsSteepSlope(tileh[TileSource::Home]));
474 const SortableSpriteStruct &sss = _rail_catenary_sprite_data[_rail_wires[tileh_selector][to_underlying(t)][pcp_config]];
475
476 /*
477 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
478 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
479 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps, so we round the result up or
480 * down to the nearest full height change.
481 */
482 int z = (GetSlopePixelZ(ti->x + sss.origin.x, ti->y + sss.origin.y, true) + 4) / 8 * 8;
483 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, z, sss, IsTransparencySet(TransparencyOption::Catenary));
484 }
485}
486
495{
497 TileIndex start = GetOtherBridgeEnd(end);
498
499 uint length = GetTunnelBridgeLength(start, end);
500 uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
501
502 Axis axis = GetBridgeAxis(ti->tile);
504
505 RailCatenarySprite offset = (RailCatenarySprite)(axis == Axis::X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
506
507 const SortableSpriteStruct *sss;
508 if ((length % 2) && num == length) {
509 /* Draw the "short" wire on the southern end of the bridge
510 * only needed if the length of the bridge is odd */
511 sss = &_rail_catenary_sprite_data[WIRE_X_FLAT_BOTH + offset];
512 } else {
513 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
514 sss = &_rail_catenary_sprite_data[WIRE_X_FLAT_SW + (num % 2) + offset];
515 }
516
517 uint height = GetBridgePixelHeight(end);
518
520
521 AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x, ti->y, height, *sss, IsTransparencySet(TransparencyOption::Catenary));
522
524
525 static constexpr SpriteBounds pylon_bounds{{-1, -1, 0}, {1, 1, BB_HEIGHT_UNDER_BRIDGE}, {1, 1, 0}};
526
527 /* Finished with wires, draw pylons
528 * every other tile needs a pylon on the northern end */
529 if (num % 2) {
531 Direction ppp_pos = (axis == Axis::X ? Direction::NW : Direction::NE);
532 if (HasBit(tlg, (axis == Axis::X ? 0 : 1))) ppp_pos = ReverseDir(ppp_pos);
533 uint x = ti->x + _x_pcp_offsets[pcp_pos] + _x_ppp_offsets[ppp_pos];
534 uint y = ti->y + _y_pcp_offsets[pcp_pos] + _y_ppp_offsets[ppp_pos];
535 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[ppp_pos], PAL_NONE, x, y, height, pylon_bounds, IsTransparencySet(TransparencyOption::Catenary));
536 }
537
538 /* need a pylon on the southern end of the bridge */
539 if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
541 Direction ppp_pos = (axis == Axis::X ? Direction::NW : Direction::NE);
542 if (HasBit(tlg, (axis == Axis::X ? 0 : 1))) ppp_pos = ReverseDir(ppp_pos);
543 uint x = ti->x + _x_pcp_offsets[pcp_pos] + _x_ppp_offsets[ppp_pos];
544 uint y = ti->y + _y_pcp_offsets[pcp_pos] + _y_ppp_offsets[ppp_pos];
545 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[ppp_pos], PAL_NONE, x, y, height, pylon_bounds, IsTransparencySet(TransparencyOption::Catenary));
546 }
547}
548
555{
556 switch (GetTileType(ti->tile)) {
558 if (IsRailDepot(ti->tile)) {
560
561 SpriteID wire_base = GetWireBase(ti->tile);
562
563 /* This wire is not visible with the default depot sprites */
564 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, GetTileMaxPixelZ(ti->tile), sss, IsTransparencySet(TransparencyOption::Catenary));
565 return;
566 }
567 break;
568
570 case TileType::Road:
572 break;
573
574 default: return;
575 }
577}
578
580void SettingsDisableElrail(int32_t new_value)
581{
582 bool disable = (new_value != 0);
583 UpdateDisableElrailSettingState(disable, true);
584}
585
586void UpdateDisableElrailSettingState(bool disable, bool update_vehicles)
587{
588 /* walk through all train engines */
590 RailVehicleInfo *rv_info = &e->VehInfo<RailVehicleInfo>();
591 /* update railtype of engines intended to use elrail */
593 rv_info->railtypes.Set(RAILTYPE_ELECTRIC, !disable);
594 rv_info->railtypes.Set(RAILTYPE_RAIL, disable || rv_info->intended_railtypes.Test(RAILTYPE_RAIL));
595 }
596 }
597
598 /* when disabling elrails, make sure that all existing trains can run on
599 * normal rail too */
600 if (disable) {
601 for (Train *t : Train::Iterate()) {
602 if (t->railtypes.Test(RAILTYPE_ELECTRIC)) {
603 /* this railroad vehicle is now compatible only with elrail,
604 * so add there also normal rail compatibility */
605 t->compatible_railtypes.Set(RAILTYPE_RAIL);
606 t->railtypes.Reset(RAILTYPE_ELECTRIC);
607 t->railtypes.Set(RAILTYPE_RAIL);
609 }
610 }
611 }
612
613 /* Fix the total power and acceleration for trains */
614 if (update_vehicles) {
615 for (Train *t : Train::Iterate()) {
616 /* power and acceleration is cached only for front engines */
617 if (t->IsFrontEngine()) {
618 t->ConsistChanged(CCF_TRACK);
619 }
620 }
621 }
622
623 for (Company *c : Company::Iterate()) c->avail_railtypes = GetCompanyRailTypes(c->index);
624
625 /* This resets the _last_built_railtype, which will be invalid for electric
626 * rails. It may have unintended consequences if that function is ever
627 * extended, though. */
629}
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Header file for bridges.
Foundation GetBridgeFoundation(Slope tileh, Axis axis)
Get the foundation for a bridge.
TileIndex GetSouthernBridgeEnd(TileIndex t)
Finds the southern end of a bridge starting at a middle tile.
TileIndex GetOtherBridgeEnd(TileIndex tile)
Starting at one bridge end finds the other bridge end.
TileIndex GetNorthernBridgeEnd(TileIndex t)
Finds the northern end of a bridge starting at a middle tile.
int GetBridgeHeight(TileIndex t)
Get the height ('z') of a bridge.
bool IsBridgeTile(Tile t)
checks if there is a bridge on this tile
Definition bridge_map.h:35
int GetBridgePixelHeight(TileIndex tile)
Get the height ('z') of a bridge in pixels.
Definition bridge_map.h:84
bool IsBridgeAbove(Tile t)
checks if a bridge is set above the ground of this tile
Definition bridge_map.h:45
Axis GetBridgeAxis(Tile t)
Get the axis of the bridge that goes over the tile.
Definition bridge_map.h:68
uint Count() const
Count the number of set bits.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr Timpl & Flip()
Flip all bits.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
Iterate a range of enum values.
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:134
Definition of stuff that is very close to a company, like the company struct itself.
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Direction ReverseDir(Direction d)
Return the reverse of a direction.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
EnumIndexArray< T, DiagDirection, DiagDirection::End > DiagDirectionIndexArray
Array with DiagDirection as index.
Direction
Defines the 8 directions on the map.
@ NW
Northwest.
@ End
Used to iterate.
@ NE
Northeast.
Axis
Enumeration for the two axis X and Y.
@ X
The X axis.
DiagDirection
Enumeration for diagonal directions.
@ SW
Southwest.
@ NW
Northwest.
@ End
Used for iterations.
@ NE
Northeast, upper right on your monitor.
@ SE
Southeast.
static constexpr Directions DIRECTIONS_ALL
All possible directions.
EnumBitSet< DiagDirection, uint8_t > DiagDirections
Bitset of DiagDirection elements.
void SettingsDisableElrail(int32_t new_value)
Callback for changes to the electrified rails setting.
Definition elrail.cpp:580
static SpriteID GetWireBase(TileIndex tile, TileContext context=TileContext::Normal)
Get the base wire sprite to use.
Definition elrail.cpp:185
void DrawRailCatenaryOnBridge(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition elrail.cpp:494
static SpriteID GetPylonBase(TileIndex tile, TileContext context=TileContext::Normal)
Get the base pylon sprite to use.
Definition elrail.cpp:198
static TileLocationGroup GetTileLocationGroup(TileIndex t)
Get the tile location group of a tile.
Definition elrail.cpp:76
void DrawRailCatenaryOnTunnel(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition elrail.cpp:260
static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
Masks out track bits when neighbouring tiles are unelectrified.
Definition elrail.cpp:130
void DrawRailCatenary(const TileInfo *ti)
Draws overhead wires and pylons for electric railways.
Definition elrail.cpp:554
static void AdjustTileh(TileIndex tile, Slope *tileh)
Corrects the tileh for certain tile types.
Definition elrail.cpp:210
static void DrawRailCatenaryRailway(const TileInfo *ti)
Draws wires and, if required, pylons on a given tile.
Definition elrail.cpp:274
static int GetPCPElevation(TileIndex tile, DiagDirection pcp_pos)
Returns the Z position of a Pylon Control Point.
Definition elrail.cpp:230
static TrackBits GetRailTrackBitsUniversal(TileIndex t, DiagDirections *override)
Finds which Electrified Rail Bits are present on a given tile.
Definition elrail.cpp:87
Stores all the data for overhead wire and pylon drawing.
static const DiagDirectionIndexArray< std::array< DirectionIndexArray< Direction >, TLG_END > > _ppp_order
Several PPPs maybe exist, here they are sorted in order of preference.
static const DiagDirectionIndexArray< int8_t > _x_pcp_offsets
static const DirectionIndexArray< uint8_t > _pylon_sprites
The type of pylon to draw at each PPP.
@ Neighbour
Neighbouring tile.
Definition elrail_data.h:35
@ Home
Home tile.
Definition elrail_data.h:34
static const DiagDirectionIndexArray< SortableSpriteStruct > _rail_catenary_sprite_data_tunnel
Catenary sprite data of a tunnel for each direction.
RailCatenarySprite
Refers to a certain element of the catenary.
static const TrackIndexArray< DiagDirectionIndexArray< Directions > > _disallowed_ppp_of_track_at_pcp
Which pylons can definitely NOT be built.
static const TrackIndexArray< std::array< DiagDirection, 2 > > _pcp_positions
Maps a track bit onto two PCP positions.
Definition elrail_data.h:62
static const DiagDirectionIndexArray< int8_t > _y_pcp_offsets
static const DirectionIndexArray< int8_t > _x_ppp_offsets
static const DiagDirectionIndexArray< Directions > _owned_ppp_on_pcp
Which of the PPPs are inside the tile.
Definition elrail_data.h:54
static const TrackIndexArray< DiagDirectionIndexArray< Directions > > _preferred_ppp_of_track_at_pcp
Preferred points of each trackbit.
Definition elrail_data.h:77
static const DiagDirectionIndexArray< Directions > _allowed_ppp_on_pcp
Which PPPs are possible at all on a given PCP.
Definition elrail_data.h:42
static const DiagDirectionIndexArray< std::array< Track, NUM_TRACKS_AT_PCP > > _tracks_at_pcp
This array stores which track bits can meet at a tile edge.
static const DiagDirectionIndexArray< Directions > _ignored_pcp[NUM_IGNORE_GROUPS][TLG_END]
In case we have a straight line, we place pylon only every two tiles, so there are certain tiles whic...
static const RailCatenarySprite _rail_wires[5][to_underlying(Track::End)][4]
Selects a Wire (with white and grey ends) depending on whether: a) none (should never happen) b) the ...
static const DiagDirectionIndexArray< SortableSpriteStruct > _rail_catenary_sprite_data_depot
Catenary sprite data of a depot for each direction.
static const DiagDirectionIndexArray< std::array< TileSource, NUM_TRACKS_AT_PCP > > _track_source_tile
Takes each of the 6 track bits from the array above and assigns it to the home tile or neighbour tile...
TileLocationGroup
Tile Location group.
Definition elrail_data.h:20
static const DirectionIndexArray< int8_t > _y_ppp_offsets
Header file for electrified rail specific functions.
bool HasRailCatenary(RailType rt)
Test if a rail type has catenary.
Definition elrail_func.h:22
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
static const SpriteID SPR_PYLON_BASE
Definition sprites.h:532
static const SpriteID SPR_WIRE_BASE
Definition sprites.h:531
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, RoadTramType sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
uint ApplyFoundationToSlope(Foundation f, Slope &s)
Applies a foundation to a slope.
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition map_func.h:615
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:574
TileContext
Context for tile accesses.
@ UpperHalftile
Querying information about the upper part of a tile with halftile foundation.
@ Normal
Nothing special.
@ OnBridge
Querying information about stuff on the bridge (via some bridgehead).
@ Any
Use first found.
SpriteID GetCustomRailSprite(const RailTypeInfo *rti, TileIndex tile, RailSpriteType rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
NewGRF handling of rail types.
RailTypes GetCompanyRailTypes(CompanyID company, bool introduces)
Get the rail types the given company can build.
Definition rail.cpp:137
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:39
@ Pylons
Catenary pylons.
Definition rail.h:48
@ Wires
Catenary wires.
Definition rail.h:47
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:320
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:311
void ReinitGuiAfterToggleElrail(bool disable)
Re-initialize rail-build toolbar after toggling support for electric trains.
Functions/types etc.
static RailTileType GetRailTileType(Tile t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition rail_map.h:36
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
static bool IsRailDepot(Tile t)
Is this rail tile a rail depot?
Definition rail_map.h:95
TrackBits GetTrackBits(Tile tile)
Gets the track bits of the given tile.
Definition rail_map.h:136
static bool IsPlainRailTile(Tile t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition rail_map.h:60
@ Normal
Normal rail tile without signals.
Definition rail_map.h:24
@ Signals
Normal rail tile with signals.
Definition rail_map.h:25
DiagDirection GetRailDepotDirection(Tile t)
Returns the direction the depot is facing to.
Definition rail_map.h:171
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:26
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:33
@ RAILTYPE_ELECTRIC
Electric rails.
Definition rail_type.h:29
@ RAILTYPE_RAIL
Standard non-electric rails.
Definition rail_type.h:28
Track GetCrossingRailTrack(Tile tile)
Get the rail track of a level crossing.
Definition road_map.h:379
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:69
@ Invalid
Invalid marker.
Definition road_type.h:42
A number of safeguards to prevent using unsafe methods.
static constexpr Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition slope_func.h:158
static constexpr bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition slope_func.h:24
static constexpr bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition slope_func.h:36
static constexpr bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition slope_func.h:57
Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition slope_func.h:266
Corner
Enumeration of tile corners.
Definition slope_type.h:21
@ E
East tile corner.
Definition slope_type.h:24
@ Invalid
Invalid marker.
Definition slope_type.h:27
@ S
South tile corner.
Definition slope_type.h:23
@ W
West tile corner.
Definition slope_type.h:22
@ N
North tile corner.
Definition slope_type.h:25
Slope
Enumeration for the slope-type.
Definition slope_type.h:53
@ SLOPE_FLAT
a flat tile
Definition slope_type.h:54
@ SLOPE_STEEP
indicates the slope is steep
Definition slope_type.h:59
Foundation
Enumeration for Foundations.
Definition slope_type.h:119
@ None
The tile has no foundation, the slope remains unchanged.
Definition slope_type.h:120
Maps accessors for stations.
Track GetRailStationTrack(Tile t)
Get the rail track of a rail station tile.
bool HasStationTileRail(Tile t)
Has this station tile a rail?
bool CanStationTileHaveWires(Tile t)
Can tile t have catenary wires?
Axis GetRailStationAxis(Tile t)
Get the rail direction of a rail station.
bool HasStationRail(Tile t)
Has this station tile a rail?
bool CanStationTileHavePylons(Tile t)
Can tile t have catenary pylons?
Definition of base types and functions in a cross-platform compatible way.
T x
X coordinate.
T y
Y coordinate.
static Pool::IterateWrapper< Vehicle > Iterate(size_t from=0)
Information about a rail vehicle.
Definition engine_type.h:73
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
Definition engine_type.h:77
RailTypes intended_railtypes
Intended railtypes, regardless of elrail being enabled or disabled.
Definition engine_type.h:78
Coord3D< int8_t > origin
Position of northern corner within tile.
Definition sprite.h:19
Tile information, used while rendering the tile.
Definition tile_cmd.h:34
Slope tileh
Slope of the tile.
Definition tile_cmd.h:35
TileIndex tile
Tile index.
Definition tile_cmd.h:36
'Train' is either a loco or a wagon.
Definition train.h:97
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition tile_map.cpp:135
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.h:302
int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition tile_map.h:312
Slope GetTileSlope(TileIndex tile)
Return the slope of a given tile inside the map.
Definition tile_map.h:279
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
static constexpr uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in ZOOM_BASE.
Definition tile_type.h:18
@ TunnelBridge
Tunnel entry/exit and bridge heads.
Definition tile_type.h:58
@ Station
A tile of a station or airport.
Definition tile_type.h:54
@ Railway
A tile with railway.
Definition tile_type.h:50
@ Road
A tile with road and/or tram tracks.
Definition tile_type.h:51
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition track_func.h:540
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:450
TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:468
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track Axis::X -> Track::X Axis::Y -> Track::Y Uses the fact that...
Definition track_func.h:62
Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a DiagDirection to the associated diagonal Track.
Definition track_func.h:419
TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition track_func.h:281
EnumBitSet< Trackdir, uint16_t > TrackdirBits
Bitset of Trackdir elements.
Definition track_type.h:93
EnumBitSet< Track, uint8_t > TrackBits
Bitset of Track elements.
Definition track_type.h:43
static constexpr TrackBits TRACK_BIT_ALL
All possible tracks.
Definition track_type.h:52
@ X_NE
X-axis and direction to north-east.
Definition track_type.h:64
@ X_SW
X-axis and direction to south-west.
Definition track_type.h:72
@ Y_SE
Y-axis and direction to south-east.
Definition track_type.h:65
@ Y_NW
Y-axis and direction to north-west.
Definition track_type.h:73
static constexpr TrackBits TRACK_BIT_CROSS
X-Y-axis cross.
Definition track_type.h:45
Track
These are used to specify a single track.
Definition track_type.h:19
@ X
Track along the x-axis (north-east to south-west).
Definition track_type.h:21
@ Upper
Track in the upper corner of the tile (north).
Definition track_type.h:23
@ Invalid
Flag for an invalid track.
Definition track_type.h:32
@ Y
Track along the y-axis (north-west to south-east).
Definition track_type.h:22
@ Right
Track in the right corner of the tile (east).
Definition track_type.h:26
@ Left
Track in the left corner of the tile (west).
Definition track_type.h:25
@ Lower
Track in the lower corner of the tile (south).
Definition track_type.h:24
Base for the train class.
static constexpr ConsistChangeFlags CCF_TRACK
Valid changes while vehicle is driving, and possibly changing tracks.
Definition train.h:53
@ AllowedOnNormalRail
Electric train engine is allowed to run on normal rail. *‍/.
Definition train.h:30
bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
@ Rail
Transport by train.
bool IsTunnelTile(Tile t)
Is this a tunnel (entrance)?
Definition tunnel_map.h:34
bool IsTunnel(Tile t)
Is this a tunnel (entrance)?
Definition tunnel_map.h:23
Header file for things common for tunnels and bridges.
uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles).
Functions that have tunnels and bridges in common.
DiagDirection GetTunnelBridgeDirection(Tile t)
Get the direction pointing to the other end.
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
@ Train
Train vehicle type.
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int z, const SpriteBounds &bounds, bool transparent, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition viewport.cpp:665
Functions related to (drawing on) viewports.
static constexpr int BB_HEIGHT_UNDER_BRIDGE
Some values for constructing bounding boxes (BB).