OpenTTD Source 20260621-master-g720d10536d
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 "station_map.h"
57#include "viewport_func.h"
58#include "train.h"
59#include "rail_gui.h"
60#include "tunnelbridge_map.h"
61#include "tunnelbridge.h"
62#include "elrail_func.h"
63#include "company_base.h"
64#include "newgrf_railtype.h"
65
66#include "table/elrail_data.h"
67
68#include "safeguards.h"
69
76{
77 return static_cast<TileLocationGroup>((GB(TileX(t), 0, 1) << 1) + GB(TileY(t), 0, 1));
78}
79
87{
88 switch (GetTileType(t)) {
90 if (!HasRailCatenary(GetRailType(t))) return {};
91 switch (GetRailTileType(t)) {
94 return GetTrackBits(t);
95 default:
96 return {};
97 }
98 break;
99
101 if (GetTunnelBridgeTransportType(t) != TRANSPORT_RAIL) return {};
102 if (!HasRailCatenary(GetRailType(t))) return {};
103 if (override != nullptr && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
104 *override = GetTunnelBridgeDirection(t);
105 }
107
108 case TileType::Road:
109 if (!IsLevelCrossing(t)) return {};
110 if (!HasRailCatenary(GetRailType(t))) return {};
111 return GetCrossingRailTrack(t);
112
114 if (!HasStationRail(t)) return {};
115 if (!HasRailCatenary(GetRailType(t))) return {};
116 return GetRailStationTrack(t);
117
118 default:
119 return {};
120 }
121}
122
130{
131 /* Single track bits are never masked out. */
132 if (tracks.Count() <= 1) [[likely]] return tracks;
133
134 if (!IsPlainRailTile(t)) return tracks;
135
136 TrackdirBits neighbour_tdb{};
138 /* If the neighbour tile is either not electrified or has no tracks that can be reached
139 * from this tile, mark all trackdirs that can be reached from the neighbour tile
140 * as needing no catenary. We make an exception for blocked station tiles with a matching
141 * axis that still display wires to preserve visual continuity. */
142 TileIndex next_tile = TileAddByDiagDir(t, d);
143 RailType rt = GetTileRailType(next_tile);
144 if (rt == INVALID_RAILTYPE || !HasRailCatenary(rt) ||
146 (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
147 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
148 }
149 }
150
151 /* If the tracks from either a diagonal crossing or don't overlap, both
152 * trackdirs have to be marked to mask the corresponding track bit. Else
153 * one marked trackdir is enough the mask the track bit. */
154 TrackBits mask;
155 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
156 /* If the tracks form either a diagonal crossing or don't overlap, both
157 * trackdirs have to be marked to mask the corresponding track bit. */
158 mask = static_cast<TrackBits>(~((neighbour_tdb.base() & (neighbour_tdb.base() >> 8)) & TRACK_BIT_ALL.base()));
159 /* If that results in no masked tracks and it is not a diagonal crossing,
160 * require only one marked trackdir to mask. */
161 if (tracks != TRACK_BIT_CROSS && mask == TRACK_BIT_ALL) mask = TrackdirBitsToTrackBits(neighbour_tdb).Flip(TRACK_BIT_ALL);
162 } else {
163 /* Require only one marked trackdir to mask the track. */
164 mask = TrackdirBitsToTrackBits(neighbour_tdb).Flip(TRACK_BIT_ALL);
165 /* If that results in an empty set, require both trackdirs for diagonal track. */
166 if (!tracks.Any(mask)) {
167 if (!neighbour_tdb.Test(Trackdir::X_NE) || !neighbour_tdb.Test(Trackdir::X_SW)) mask.Set(Track::X);
168 if (!neighbour_tdb.Test(Trackdir::Y_NW) || !neighbour_tdb.Test(Trackdir::Y_SE)) mask.Set(Track::Y);
169 /* If that still is not enough, require both trackdirs for any track. */
170 if (!tracks.Any(mask)) mask = static_cast<TrackBits>(~((neighbour_tdb.base() & (neighbour_tdb.base() >> 8)) & TRACK_BIT_ALL.base()));
171 }
172 }
173
174 /* Mask the tracks only if at least one track bit would remain. */
175 return tracks.Any(mask) ? tracks & mask : tracks;
176}
177
185{
186 const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
187 SpriteID wires = GetCustomRailSprite(rti, tile, RailSpriteType::Wires, context);
188 return wires == 0 ? SPR_WIRE_BASE : wires;
189}
190
198{
199 const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
200 SpriteID pylons = GetCustomRailSprite(rti, tile, RailSpriteType::Pylons, context);
201 return pylons == 0 ? SPR_PYLON_BASE : pylons;
202}
203
209static void AdjustTileh(TileIndex tile, Slope *tileh)
210{
212 if (IsTunnel(tile)) {
213 *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
214 } else if (*tileh != SLOPE_FLAT) {
215 *tileh = SLOPE_FLAT;
216 } else {
218 }
219 }
220}
221
229static int GetPCPElevation(TileIndex tile, DiagDirection pcp_pos)
230{
231 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
232 * PCPs are always on a tile edge.
233 *
234 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
235 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
236 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
237 *
238 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
239 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
240 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
241 *
242 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
243 */
244
245 int z = GetSlopePixelZ(TileX(tile) * TILE_SIZE + std::min<int8_t>(_x_pcp_offsets[pcp_pos], TILE_SIZE - 1),
246 TileY(tile) * TILE_SIZE + std::min<int8_t>(_y_pcp_offsets[pcp_pos], TILE_SIZE - 1), true);
247 /* Round the Z to the nearest half tile height. */
248 static const uint HALF_TILE_HEIGHT = TILE_HEIGHT / 2;
249 return (z + HALF_TILE_HEIGHT / 2) / HALF_TILE_HEIGHT * HALF_TILE_HEIGHT;
250}
251
260{
262
263 SpriteID wire_base = GetWireBase(ti->tile);
264
266 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, GetTilePixelZ(ti->tile), sss, IsTransparencySet(TransparencyOption::Catenary));
267}
268
273static void DrawRailCatenaryRailway(const TileInfo *ti)
274{
275 /* Pylons are placed on a tile edge, so we need to take into account
276 * the track configuration of 2 adjacent tiles. */
280 /* Note that ti->tileh has already been adjusted for Foundations */
282
283 /* Half tile slopes coincide only with horizontal/vertical track.
284 * Faking a flat slope results in the correct sprites on positions. */
285 Corner halftile_corner = CORNER_INVALID;
286 if (IsHalftileSlope(tileh[TileSource::Home])) {
287 halftile_corner = GetHalftileSlopeCorner(tileh[TileSource::Home]);
289 }
290
292 DiagDirections pcp_status{};
293 DiagDirections override_pcp{};
296
297 /* Find which rail bits are present, and select the override points.
298 * We don't draw a pylon:
299 * 1) INSIDE a tunnel (we wouldn't see it anyway)
300 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
301 * because that one is drawn on the bridge. Exception is for length 0 bridges
302 * which have no middle tiles */
303 track_config[TileSource::Home] = GetRailTrackBitsUniversal(ti->tile, &override_pcp);
304 wire_config[TileSource::Home] = MaskWireBits(ti->tile, track_config[TileSource::Home]);
305 /* If a track bit is present that is not in the main direction, the track is level */
307
308 AdjustTileh(ti->tile, &tileh[TileSource::Home]);
309
310 SpriteID pylon_normal = GetPylonBase(ti->tile);
311 SpriteID pylon_halftile = IsValidCorner(halftile_corner) ? GetPylonBase(ti->tile, TCX_UPPER_HALFTILE) : pylon_normal;
312
314 SpriteID pylon_base = (IsValidCorner(halftile_corner) && HasBit(InclinedSlope(i), halftile_corner)) ? pylon_halftile : pylon_normal;
315 TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
316 int elevation = GetPCPElevation(ti->tile, i);
317
318 /* Here's one of the main headaches. GetTileSlope does not correct for possibly
319 * existing foundataions, so we do have to do that manually later on.*/
320 tileh[TileSource::Neighbour] = GetTileSlope(neighbour);
321 track_config[TileSource::Neighbour] = GetRailTrackBitsUniversal(neighbour, nullptr);
322 wire_config[TileSource::Neighbour] = MaskWireBits(neighbour, track_config[TileSource::Neighbour]);
323 if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = {};
324
325 /* Ignore station tiles that allow neither wires nor pylons. */
326 if (HasStationTileRail(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = {};
327
328 /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
329 * we have to draw all pillars on the current tile. */
330 if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = {};
331
333
334 ppp_preferred[i] = DIRECTIONS_ALL; // We start with preferring everything (end-of-line in any direction)
335 ppp_allowed[i] = _allowed_ppp_on_pcp[i];
336
337 /* We cycle through all the existing tracks at a PCP and see what
338 * PPPs we want to have, or may not have at all */
339 for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
340 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
342 IsBridgeTile(neighbour) &&
343 GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
344 continue;
345 }
346
347 /* We check whether the track in question (k) is present in the tile
348 * (TrackSourceTile) */
349 DiagDirection pcp_pos = i;
350 if (wire_config[_track_source_tile[i][k]].Test(_tracks_at_pcp[i][k])) {
351 /* track found, if track is in the neighbour tile, adjust the number
352 * of the PCP for preferred/allowed determination*/
353 pcp_pos = (_track_source_tile[i][k] == TileSource::Home) ? i : ReverseDiagDir(i);
354 pcp_status.Set(i); // This PCP is in use
355 ppp_preferred[i] &= _preferred_ppp_of_track_at_pcp[_tracks_at_pcp[i][k]][pcp_pos];
356 }
357
358 if (track_config[_track_source_tile[i][k]].Test(_tracks_at_pcp[i][k])) {
359 ppp_allowed[i].Reset(_disallowed_ppp_of_track_at_pcp[_tracks_at_pcp[i][k]][pcp_pos]);
360 }
361 }
362
363 /* Deactivate all PPPs if PCP is not used */
364 if (!pcp_status.Test(i)) {
365 ppp_preferred[i].Reset();
366 ppp_allowed[i].Reset();
367 }
368
369 Foundation foundation = Foundation::None;
370
371 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
373
374 /* Read the foundations if they are present, and adjust the tileh */
375 if (track_config[TileSource::Neighbour].Any() && IsTileType(neighbour, TileType::Railway) && HasRailCatenary(GetRailType(neighbour))) foundation = GetRailFoundation(tileh[TileSource::Neighbour], track_config[TileSource::Neighbour]);
376 if (IsBridgeTile(neighbour)) {
378 }
379
381
382 /* Half tile slopes coincide only with horizontal/vertical track.
383 * Faking a flat slope results in the correct sprites on positions. */
385
386 AdjustTileh(neighbour, &tileh[TileSource::Neighbour]);
387
388 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
389 * Delete the PCP if this is the case.
390 * Level means that the slope is the same, or the track is flat */
391 if (tileh[TileSource::Home] == tileh[TileSource::Neighbour] || (is_flat[TileSource::Home] && is_flat[TileSource::Neighbour])) {
392 for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
393 if (ppp_preferred[i] == _ignored_pcp[k][tlg][i]) pcp_status.Reset(i);
394 }
395 }
396
397 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
398 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
399 * anything. Note that the preferred PPPs still contain the end-of-line markers.
400 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
401 if (ppp_allowed[i].Any(ppp_preferred[i])) ppp_allowed[i] &= ppp_preferred[i];
402
403 if (IsBridgeAbove(ti->tile)) {
404 Track bridgetrack = AxisToTrack(GetBridgeAxis(ti->tile));
405 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
406
407 if ((height <= GetTileMaxZ(ti->tile) + 1) &&
408 (i == _pcp_positions[bridgetrack][0] || i == _pcp_positions[bridgetrack][1])) {
409 override_pcp.Set(i);
410 }
411 }
412
413 if (ppp_allowed[i].Any() && pcp_status.Test(i) && !override_pcp.Test(i) &&
415
416 const auto &ppp_orders = _ppp_order[i][GetTileLocationGroup(ti->tile)];
418 Direction temp = ppp_orders[k];
419
420 if (ppp_allowed[i].Test(temp)) {
421 uint x = ti->x + _x_pcp_offsets[i] + _x_ppp_offsets[temp];
422 uint y = ti->y + _y_pcp_offsets[i] + _y_ppp_offsets[temp];
423
424 /* Don't build the pylon if it would be outside the tile */
425 if (!_owned_ppp_on_pcp[i].Test(temp)) {
426 /* We have a neighbour that will draw it, bail out */
427 if (track_config[TileSource::Neighbour].Any()) break;
428 continue; // No neighbour, go looking for a better position
429 }
430
431 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[temp], PAL_NONE, x, y, elevation,
432 {{-1, -1, 0}, {1, 1, BB_HEIGHT_UNDER_BRIDGE}, {1, 1, 0}}, IsTransparencySet(TransparencyOption::Catenary));
433
434 break; // We already have drawn a pylon, bail out
435 }
436 }
437 }
438 }
439
440 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawRailCatenaryOnTunnel()) */
441 if (IsTunnelTile(ti->tile)) return;
442
443 /* Don't draw a wire under a low bridge */
445 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
446
447 if (height <= GetTileMaxZ(ti->tile) + 1) return;
448 }
449
450 /* Don't draw a wire if the station tile does not want any */
451 if (HasStationTileRail(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
452
453 SpriteID wire_normal = GetWireBase(ti->tile);
454 SpriteID wire_halftile = IsValidCorner(halftile_corner) ? GetWireBase(ti->tile, TCX_UPPER_HALFTILE) : wire_normal;
455 Track halftile_track;
456 switch (halftile_corner) {
457 case CORNER_W: halftile_track = Track::Left; break;
458 case CORNER_S: halftile_track = Track::Lower; break;
459 case CORNER_E: halftile_track = Track::Right; break;
460 case CORNER_N: halftile_track = Track::Upper; break;
461 default: halftile_track = Track::Invalid; break;
462 }
463
464 /* Drawing of pylons is finished, now draw the wires */
465 for (Track t : wire_config[TileSource::Home]) {
466 SpriteID wire_base = (t == halftile_track) ? wire_halftile : wire_normal;
467 uint8_t pcp_config = pcp_status.Test(_pcp_positions[t][0]) +
468 (pcp_status.Test(_pcp_positions[t][1]) << 1);
469 int tileh_selector = !(tileh[TileSource::Home] % 3) * tileh[TileSource::Home] / 3; // tileh for the slopes, 0 otherwise
470
471 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)
472 assert(!IsSteepSlope(tileh[TileSource::Home]));
473 const SortableSpriteStruct &sss = _rail_catenary_sprite_data[_rail_wires[tileh_selector][to_underlying(t)][pcp_config]];
474
475 /*
476 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
477 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
478 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps, so we round the result up or
479 * down to the nearest full height change.
480 */
481 int z = (GetSlopePixelZ(ti->x + sss.origin.x, ti->y + sss.origin.y, true) + 4) / 8 * 8;
482 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, z, sss, IsTransparencySet(TransparencyOption::Catenary));
483 }
484}
485
494{
496 TileIndex start = GetOtherBridgeEnd(end);
497
498 uint length = GetTunnelBridgeLength(start, end);
499 uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
500
501 Axis axis = GetBridgeAxis(ti->tile);
503
504 RailCatenarySprite offset = (RailCatenarySprite)(axis == Axis::X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
505
506 const SortableSpriteStruct *sss;
507 if ((length % 2) && num == length) {
508 /* Draw the "short" wire on the southern end of the bridge
509 * only needed if the length of the bridge is odd */
510 sss = &_rail_catenary_sprite_data[WIRE_X_FLAT_BOTH + offset];
511 } else {
512 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
513 sss = &_rail_catenary_sprite_data[WIRE_X_FLAT_SW + (num % 2) + offset];
514 }
515
516 uint height = GetBridgePixelHeight(end);
517
518 SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
519
520 AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x, ti->y, height, *sss, IsTransparencySet(TransparencyOption::Catenary));
521
522 SpriteID pylon_base = GetPylonBase(end, TCX_ON_BRIDGE);
523
524 static constexpr SpriteBounds pylon_bounds{{-1, -1, 0}, {1, 1, BB_HEIGHT_UNDER_BRIDGE}, {1, 1, 0}};
525
526 /* Finished with wires, draw pylons
527 * every other tile needs a pylon on the northern end */
528 if (num % 2) {
530 Direction ppp_pos = (axis == Axis::X ? Direction::NW : Direction::NE);
531 if (HasBit(tlg, (axis == Axis::X ? 0 : 1))) ppp_pos = ReverseDir(ppp_pos);
532 uint x = ti->x + _x_pcp_offsets[pcp_pos] + _x_ppp_offsets[ppp_pos];
533 uint y = ti->y + _y_pcp_offsets[pcp_pos] + _y_ppp_offsets[ppp_pos];
534 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[ppp_pos], PAL_NONE, x, y, height, pylon_bounds, IsTransparencySet(TransparencyOption::Catenary));
535 }
536
537 /* need a pylon on the southern end of the bridge */
538 if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
540 Direction ppp_pos = (axis == Axis::X ? Direction::NW : Direction::NE);
541 if (HasBit(tlg, (axis == Axis::X ? 0 : 1))) ppp_pos = ReverseDir(ppp_pos);
542 uint x = ti->x + _x_pcp_offsets[pcp_pos] + _x_ppp_offsets[ppp_pos];
543 uint y = ti->y + _y_pcp_offsets[pcp_pos] + _y_ppp_offsets[ppp_pos];
544 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[ppp_pos], PAL_NONE, x, y, height, pylon_bounds, IsTransparencySet(TransparencyOption::Catenary));
545 }
546}
547
554{
555 switch (GetTileType(ti->tile)) {
557 if (IsRailDepot(ti->tile)) {
559
560 SpriteID wire_base = GetWireBase(ti->tile);
561
562 /* This wire is not visible with the default depot sprites */
563 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, GetTileMaxPixelZ(ti->tile), sss, IsTransparencySet(TransparencyOption::Catenary));
564 return;
565 }
566 break;
567
569 case TileType::Road:
571 break;
572
573 default: return;
574 }
576}
577
579void SettingsDisableElrail(int32_t new_value)
580{
581 bool disable = (new_value != 0);
582 UpdateDisableElrailSettingState(disable, true);
583}
584
585void UpdateDisableElrailSettingState(bool disable, bool update_vehicles)
586{
587 /* walk through all train engines */
589 RailVehicleInfo *rv_info = &e->VehInfo<RailVehicleInfo>();
590 /* update railtype of engines intended to use elrail */
592 rv_info->railtypes.Set(RAILTYPE_ELECTRIC, !disable);
593 rv_info->railtypes.Set(RAILTYPE_RAIL, disable || rv_info->intended_railtypes.Test(RAILTYPE_RAIL));
594 }
595 }
596
597 /* when disabling elrails, make sure that all existing trains can run on
598 * normal rail too */
599 if (disable) {
600 for (Train *t : Train::Iterate()) {
601 if (t->railtypes.Test(RAILTYPE_ELECTRIC)) {
602 /* this railroad vehicle is now compatible only with elrail,
603 * so add there also normal rail compatibility */
604 t->compatible_railtypes.Set(RAILTYPE_RAIL);
605 t->railtypes.Reset(RAILTYPE_ELECTRIC);
606 t->railtypes.Set(RAILTYPE_RAIL);
608 }
609 }
610 }
611
612 /* Fix the total power and acceleration for trains */
613 if (update_vehicles) {
614 for (Train *t : Train::Iterate()) {
615 /* power and acceleration is cached only for front engines */
616 if (t->IsFrontEngine()) {
617 t->ConsistChanged(CCF_TRACK);
618 }
619 }
620 }
621
622 for (Company *c : Company::Iterate()) c->avail_railtypes = GetCompanyRailTypes(c->index);
623
624 /* This resets the _last_built_railtype, which will be invalid for electric
625 * rails. It may have unintended consequences if that function is ever
626 * extended, though. */
628}
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.
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:117
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.
static SpriteID GetPylonBase(TileIndex tile, TileContext context=TCX_NORMAL)
Get the base pylon sprite to use.
Definition elrail.cpp:197
void SettingsDisableElrail(int32_t new_value)
Callback for changes to the electrified rails setting.
Definition elrail.cpp:579
void DrawRailCatenaryOnBridge(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition elrail.cpp:493
static SpriteID GetWireBase(TileIndex tile, TileContext context=TCX_NORMAL)
Get the base wire sprite to use.
Definition elrail.cpp:184
static TileLocationGroup GetTileLocationGroup(TileIndex t)
Get the tile location group of a tile.
Definition elrail.cpp:75
void DrawRailCatenaryOnTunnel(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition elrail.cpp:259
static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
Masks out track bits when neighbouring tiles are unelectrified.
Definition elrail.cpp:129
void DrawRailCatenary(const TileInfo *ti)
Draws overhead wires and pylons for electric railways.
Definition elrail.cpp:553
static void AdjustTileh(TileIndex tile, Slope *tileh)
Corrects the tileh for certain tile types.
Definition elrail.cpp:209
static void DrawRailCatenaryRailway(const TileInfo *ti)
Draws wires and, if required, pylons on a given tile.
Definition elrail.cpp:273
static int GetPCPElevation(TileIndex tile, DiagDirection pcp_pos)
Returns the Z position of a Pylon Control Point.
Definition elrail.cpp:229
static TrackBits GetRailTrackBitsUniversal(TileIndex t, DiagDirections *override)
Finds which Electrified Rail Bits are present on a given tile.
Definition elrail.cpp:86
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
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.
@ TCX_UPPER_HALFTILE
Querying information about the upper part of a tile with halftile foundation.
@ TCX_ON_BRIDGE
Querying information about stuff on the bridge (via some bridgehead).
@ TCX_NORMAL
Nothing special.
@ 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:303
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:310
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:25
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:32
@ RAILTYPE_ELECTRIC
Electric rails.
Definition rail_type.h:28
@ RAILTYPE_RAIL
Standard non-electric rails.
Definition rail_type.h:27
Track GetCrossingRailTrack(Tile tile)
Get the rail track of a level crossing.
Definition road_map.h:368
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:69
@ Invalid
Invalid marker.
Definition road_type.h:41
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:148
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:47
Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition slope_func.h:256
Corner
Enumeration of tile corners.
Definition slope_type.h:21
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:98
@ None
The tile has no foundation, the slope remains unchanged.
Definition slope_type.h:99
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:74
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
Definition engine_type.h:78
RailTypes intended_railtypes
Intended railtypes, regardless of elrail being enabled or disabled.
Definition engine_type.h:79
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...
@ TRANSPORT_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:664
Functions related to (drawing on) viewports.
static constexpr int BB_HEIGHT_UNDER_BRIDGE
Some values for constructing bounding boxes (BB).