OpenTTD Source 20260531-master-g0e951f3528
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)) {
91 switch (GetRailTileType(t)) {
93 return GetTrackBits(t);
94 default:
95 return TRACK_BIT_NONE;
96 }
97 break;
98
102 if (override != nullptr && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
103 *override = GetTunnelBridgeDirection(t);
104 }
106
107 case TileType::Road:
108 if (!IsLevelCrossing(t)) return TRACK_BIT_NONE;
110 return GetCrossingRailBits(t);
111
113 if (!HasStationRail(t)) return TRACK_BIT_NONE;
116
117 default:
118 return TRACK_BIT_NONE;
119 }
120}
121
129{
130 /* Single track bits are never masked out. */
131 if (HasAtMostOneBit(tracks)) [[likely]] return tracks;
132
133 if (!IsPlainRailTile(t)) return tracks;
134
135 TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
137 /* If the neighbour tile is either not electrified or has no tracks that can be reached
138 * from this tile, mark all trackdirs that can be reached from the neighbour tile
139 * as needing no catenary. We make an exception for blocked station tiles with a matching
140 * axis that still display wires to preserve visual continuity. */
141 TileIndex next_tile = TileAddByDiagDir(t, d);
142 RailType rt = GetTileRailType(next_tile);
143 if (rt == INVALID_RAILTYPE || !HasRailCatenary(rt) ||
145 (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
146 neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
147 }
148 }
149
150 /* If the tracks from either a diagonal crossing or don't overlap, both
151 * trackdirs have to be marked to mask the corresponding track bit. Else
152 * one marked trackdir is enough the mask the track bit. */
153 TrackBits mask;
154 if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
155 /* If the tracks form either a diagonal crossing or don't overlap, both
156 * trackdirs have to be marked to mask the corresponding track bit. */
157 mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
158 /* If that results in no masked tracks and it is not a diagonal crossing,
159 * require only one marked trackdir to mask. */
160 if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
161 } else {
162 /* Require only one marked trackdir to mask the track. */
163 mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
164 /* If that results in an empty set, require both trackdirs for diagonal track. */
165 if ((tracks & mask) == TRACK_BIT_NONE) {
166 if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
167 if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
168 /* If that still is not enough, require both trackdirs for any track. */
169 if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
170 }
171 }
172
173 /* Mask the tracks only if at least one track bit would remain. */
174 return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
175}
176
184{
185 const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
186 SpriteID wires = GetCustomRailSprite(rti, tile, RailSpriteType::Wires, context);
187 return wires == 0 ? SPR_WIRE_BASE : wires;
188}
189
197{
198 const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
199 SpriteID pylons = GetCustomRailSprite(rti, tile, RailSpriteType::Pylons, context);
200 return pylons == 0 ? SPR_PYLON_BASE : pylons;
201}
202
208static void AdjustTileh(TileIndex tile, Slope *tileh)
209{
211 if (IsTunnel(tile)) {
212 *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
213 } else if (*tileh != SLOPE_FLAT) {
214 *tileh = SLOPE_FLAT;
215 } else {
217 }
218 }
219}
220
228static int GetPCPElevation(TileIndex tile, DiagDirection pcp_pos)
229{
230 /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
231 * PCPs are always on a tile edge.
232 *
233 * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
234 * So we have to move it inside the tile, because if the neighboured tile has a foundation,
235 * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
236 *
237 * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
238 * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
239 * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
240 *
241 * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
242 */
243
244 int z = GetSlopePixelZ(TileX(tile) * TILE_SIZE + std::min<int8_t>(_x_pcp_offsets[pcp_pos], TILE_SIZE - 1),
245 TileY(tile) * TILE_SIZE + std::min<int8_t>(_y_pcp_offsets[pcp_pos], TILE_SIZE - 1), true);
246 /* Round the Z to the nearest half tile height. */
247 static const uint HALF_TILE_HEIGHT = TILE_HEIGHT / 2;
248 return (z + HALF_TILE_HEIGHT / 2) / HALF_TILE_HEIGHT * HALF_TILE_HEIGHT;
249}
250
259{
261
262 SpriteID wire_base = GetWireBase(ti->tile);
263
265 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, GetTilePixelZ(ti->tile), sss, IsTransparencySet(TransparencyOption::Catenary));
266}
267
272static void DrawRailCatenaryRailway(const TileInfo *ti)
273{
274 /* Pylons are placed on a tile edge, so we need to take into account
275 * the track configuration of 2 adjacent tiles. */
279 /* Note that ti->tileh has already been adjusted for Foundations */
281
282 /* Half tile slopes coincide only with horizontal/vertical track.
283 * Faking a flat slope results in the correct sprites on positions. */
284 Corner halftile_corner = CORNER_INVALID;
285 if (IsHalftileSlope(tileh[TileSource::Home])) {
286 halftile_corner = GetHalftileSlopeCorner(tileh[TileSource::Home]);
288 }
289
291 DiagDirections pcp_status{};
292 DiagDirections override_pcp{};
295
296 /* Find which rail bits are present, and select the override points.
297 * We don't draw a pylon:
298 * 1) INSIDE a tunnel (we wouldn't see it anyway)
299 * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
300 * because that one is drawn on the bridge. Exception is for length 0 bridges
301 * which have no middle tiles */
302 track_config[TileSource::Home] = GetRailTrackBitsUniversal(ti->tile, &override_pcp);
303 wire_config[TileSource::Home] = MaskWireBits(ti->tile, track_config[TileSource::Home]);
304 /* If a track bit is present that is not in the main direction, the track is level */
305 is_flat[TileSource::Home] = ((track_config[TileSource::Home] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
306
307 AdjustTileh(ti->tile, &tileh[TileSource::Home]);
308
309 SpriteID pylon_normal = GetPylonBase(ti->tile);
310 SpriteID pylon_halftile = (halftile_corner != CORNER_INVALID) ? GetPylonBase(ti->tile, TCX_UPPER_HALFTILE) : pylon_normal;
311
313 SpriteID pylon_base = (halftile_corner != CORNER_INVALID && HasBit(InclinedSlope(i), halftile_corner)) ? pylon_halftile : pylon_normal;
314 TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
315 int elevation = GetPCPElevation(ti->tile, i);
316
317 /* Here's one of the main headaches. GetTileSlope does not correct for possibly
318 * existing foundataions, so we do have to do that manually later on.*/
319 tileh[TileSource::Neighbour] = GetTileSlope(neighbour);
320 track_config[TileSource::Neighbour] = GetRailTrackBitsUniversal(neighbour, nullptr);
321 wire_config[TileSource::Neighbour] = MaskWireBits(neighbour, track_config[TileSource::Neighbour]);
322 if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = TRACK_BIT_NONE;
323
324 /* Ignore station tiles that allow neither wires nor pylons. */
325 if (HasStationTileRail(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = TRACK_BIT_NONE;
326
327 /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
328 * we have to draw all pillars on the current tile. */
329 if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wire_config[TileSource::Neighbour] = track_config[TileSource::Neighbour] = TRACK_BIT_NONE;
330
331 is_flat[TileSource::Neighbour] = ((track_config[TileSource::Neighbour] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
332
333 ppp_preferred[i] = DIRECTIONS_ALL; // We start with preferring everything (end-of-line in any direction)
334 ppp_allowed[i] = _allowed_ppp_on_pcp[i];
335
336 /* We cycle through all the existing tracks at a PCP and see what
337 * PPPs we want to have, or may not have at all */
338 for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
339 /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
341 IsBridgeTile(neighbour) &&
342 GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
343 continue;
344 }
345
346 /* We check whether the track in question (k) is present in the tile
347 * (TrackSourceTile) */
348 DiagDirection pcp_pos = i;
349 if (HasBit(wire_config[_track_source_tile[i][k]], _tracks_at_pcp[i][k])) {
350 /* track found, if track is in the neighbour tile, adjust the number
351 * of the PCP for preferred/allowed determination*/
352 pcp_pos = (_track_source_tile[i][k] == TileSource::Home) ? i : ReverseDiagDir(i);
353 pcp_status.Set(i); // This PCP is in use
354 ppp_preferred[i] &= _preferred_ppp_of_track_at_pcp[_tracks_at_pcp[i][k]][pcp_pos];
355 }
356
357 if (HasBit(track_config[_track_source_tile[i][k]], _tracks_at_pcp[i][k])) {
358 ppp_allowed[i].Reset(_disallowed_ppp_of_track_at_pcp[_tracks_at_pcp[i][k]][pcp_pos]);
359 }
360 }
361
362 /* Deactivate all PPPs if PCP is not used */
363 if (!pcp_status.Test(i)) {
364 ppp_preferred[i].Reset();
365 ppp_allowed[i].Reset();
366 }
367
368 Foundation foundation = FOUNDATION_NONE;
369
370 /* Station and road crossings are always "flat", so adjust the tileh accordingly */
372
373 /* Read the foundations if they are present, and adjust the tileh */
374 if (track_config[TileSource::Neighbour] != TRACK_BIT_NONE && IsTileType(neighbour, TileType::Railway) && HasRailCatenary(GetRailType(neighbour))) foundation = GetRailFoundation(tileh[TileSource::Neighbour], track_config[TileSource::Neighbour]);
375 if (IsBridgeTile(neighbour)) {
377 }
378
380
381 /* Half tile slopes coincide only with horizontal/vertical track.
382 * Faking a flat slope results in the correct sprites on positions. */
384
385 AdjustTileh(neighbour, &tileh[TileSource::Neighbour]);
386
387 /* If we have a straight (and level) track, we want a pylon only every 2 tiles
388 * Delete the PCP if this is the case.
389 * Level means that the slope is the same, or the track is flat */
390 if (tileh[TileSource::Home] == tileh[TileSource::Neighbour] || (is_flat[TileSource::Home] && is_flat[TileSource::Neighbour])) {
391 for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
392 if (ppp_preferred[i] == _ignored_pcp[k][tlg][i]) pcp_status.Reset(i);
393 }
394 }
395
396 /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
397 * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
398 * anything. Note that the preferred PPPs still contain the end-of-line markers.
399 * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
400 if (ppp_allowed[i].Any(ppp_preferred[i])) ppp_allowed[i] &= ppp_preferred[i];
401
402 if (IsBridgeAbove(ti->tile)) {
403 Track bridgetrack = AxisToTrack(GetBridgeAxis(ti->tile));
404 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
405
406 if ((height <= GetTileMaxZ(ti->tile) + 1) &&
407 (i == _pcp_positions[bridgetrack][0] || i == _pcp_positions[bridgetrack][1])) {
408 override_pcp.Set(i);
409 }
410 }
411
412 if (ppp_allowed[i].Any() && pcp_status.Test(i) && !override_pcp.Test(i) &&
414
415 const auto &ppp_orders = _ppp_order[i][GetTileLocationGroup(ti->tile)];
416 for (Direction k = Direction::Begin; k < Direction::End; k++) {
417 Direction temp = ppp_orders[k];
418
419 if (ppp_allowed[i].Test(temp)) {
420 uint x = ti->x + _x_pcp_offsets[i] + _x_ppp_offsets[temp];
421 uint y = ti->y + _y_pcp_offsets[i] + _y_ppp_offsets[temp];
422
423 /* Don't build the pylon if it would be outside the tile */
424 if (!_owned_ppp_on_pcp[i].Test(temp)) {
425 /* We have a neighbour that will draw it, bail out */
426 if (track_config[TileSource::Neighbour] != TRACK_BIT_NONE) break;
427 continue; // No neighbour, go looking for a better position
428 }
429
430 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[temp], PAL_NONE, x, y, elevation,
431 {{-1, -1, 0}, {1, 1, BB_HEIGHT_UNDER_BRIDGE}, {1, 1, 0}}, IsTransparencySet(TransparencyOption::Catenary));
432
433 break; // We already have drawn a pylon, bail out
434 }
435 }
436 }
437 }
438
439 /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawRailCatenaryOnTunnel()) */
440 if (IsTunnelTile(ti->tile)) return;
441
442 /* Don't draw a wire under a low bridge */
444 int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
445
446 if (height <= GetTileMaxZ(ti->tile) + 1) return;
447 }
448
449 /* Don't draw a wire if the station tile does not want any */
450 if (HasStationTileRail(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
451
452 SpriteID wire_normal = GetWireBase(ti->tile);
453 SpriteID wire_halftile = (halftile_corner != CORNER_INVALID) ? GetWireBase(ti->tile, TCX_UPPER_HALFTILE) : wire_normal;
454 Track halftile_track;
455 switch (halftile_corner) {
456 case CORNER_W: halftile_track = TRACK_LEFT; break;
457 case CORNER_S: halftile_track = TRACK_LOWER; break;
458 case CORNER_E: halftile_track = TRACK_RIGHT; break;
459 case CORNER_N: halftile_track = TRACK_UPPER; break;
460 default: halftile_track = INVALID_TRACK; break;
461 }
462
463 /* Drawing of pylons is finished, now draw the wires */
464 for (Track t : SetTrackBitIterator(wire_config[TileSource::Home])) {
465 SpriteID wire_base = (t == halftile_track) ? wire_halftile : wire_normal;
466 uint8_t pcp_config = pcp_status.Test(_pcp_positions[t][0]) +
467 (pcp_status.Test(_pcp_positions[t][1]) << 1);
468 int tileh_selector = !(tileh[TileSource::Home] % 3) * tileh[TileSource::Home] / 3; // tileh for the slopes, 0 otherwise
469
470 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)
471 assert(!IsSteepSlope(tileh[TileSource::Home]));
472 const SortableSpriteStruct &sss = _rail_catenary_sprite_data[_rail_wires[tileh_selector][t][pcp_config]];
473
474 /*
475 * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
476 * Therefore it is safe to use GetSlopePixelZ() for the elevation.
477 * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps, so we round the result up or
478 * down to the nearest full height change.
479 */
480 int z = (GetSlopePixelZ(ti->x + sss.origin.x, ti->y + sss.origin.y, true) + 4) / 8 * 8;
481 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, z, sss, IsTransparencySet(TransparencyOption::Catenary));
482 }
483}
484
493{
495 TileIndex start = GetOtherBridgeEnd(end);
496
497 uint length = GetTunnelBridgeLength(start, end);
498 uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
499
500 Axis axis = GetBridgeAxis(ti->tile);
502
503 RailCatenarySprite offset = (RailCatenarySprite)(axis == Axis::X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
504
505 const SortableSpriteStruct *sss;
506 if ((length % 2) && num == length) {
507 /* Draw the "short" wire on the southern end of the bridge
508 * only needed if the length of the bridge is odd */
509 sss = &_rail_catenary_sprite_data[WIRE_X_FLAT_BOTH + offset];
510 } else {
511 /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
512 sss = &_rail_catenary_sprite_data[WIRE_X_FLAT_SW + (num % 2) + offset];
513 }
514
515 uint height = GetBridgePixelHeight(end);
516
517 SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
518
519 AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x, ti->y, height, *sss, IsTransparencySet(TransparencyOption::Catenary));
520
521 SpriteID pylon_base = GetPylonBase(end, TCX_ON_BRIDGE);
522
523 static constexpr SpriteBounds pylon_bounds{{-1, -1, 0}, {1, 1, BB_HEIGHT_UNDER_BRIDGE}, {1, 1, 0}};
524
525 /* Finished with wires, draw pylons
526 * every other tile needs a pylon on the northern end */
527 if (num % 2) {
529 Direction ppp_pos = (axis == Axis::X ? Direction::NW : Direction::NE);
530 if (HasBit(tlg, (axis == Axis::X ? 0 : 1))) ppp_pos = ReverseDir(ppp_pos);
531 uint x = ti->x + _x_pcp_offsets[pcp_pos] + _x_ppp_offsets[ppp_pos];
532 uint y = ti->y + _y_pcp_offsets[pcp_pos] + _y_ppp_offsets[ppp_pos];
533 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[ppp_pos], PAL_NONE, x, y, height, pylon_bounds, IsTransparencySet(TransparencyOption::Catenary));
534 }
535
536 /* need a pylon on the southern end of the bridge */
537 if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
539 Direction ppp_pos = (axis == Axis::X ? Direction::NW : Direction::NE);
540 if (HasBit(tlg, (axis == Axis::X ? 0 : 1))) ppp_pos = ReverseDir(ppp_pos);
541 uint x = ti->x + _x_pcp_offsets[pcp_pos] + _x_ppp_offsets[ppp_pos];
542 uint y = ti->y + _y_pcp_offsets[pcp_pos] + _y_ppp_offsets[ppp_pos];
543 AddSortableSpriteToDraw(pylon_base + _pylon_sprites[ppp_pos], PAL_NONE, x, y, height, pylon_bounds, IsTransparencySet(TransparencyOption::Catenary));
544 }
545}
546
553{
554 switch (GetTileType(ti->tile)) {
556 if (IsRailDepot(ti->tile)) {
558
559 SpriteID wire_base = GetWireBase(ti->tile);
560
561 /* This wire is not visible with the default depot sprites */
562 AddSortableSpriteToDraw(wire_base + sss.image_offset, PAL_NONE, ti->x, ti->y, GetTileMaxPixelZ(ti->tile), sss, IsTransparencySet(TransparencyOption::Catenary));
563 return;
564 }
565 break;
566
568 case TileType::Road:
570 break;
571
572 default: return;
573 }
575}
576
578void SettingsDisableElrail(int32_t new_value)
579{
580 bool disable = (new_value != 0);
581 UpdateDisableElrailSettingState(disable, true);
582}
583
584void UpdateDisableElrailSettingState(bool disable, bool update_vehicles)
585{
586 /* walk through all train engines */
588 RailVehicleInfo *rv_info = &e->VehInfo<RailVehicleInfo>();
589 /* update railtype of engines intended to use elrail */
591 rv_info->railtypes.Set(RAILTYPE_ELECTRIC, !disable);
592 rv_info->railtypes.Set(RAILTYPE_RAIL, disable || rv_info->intended_railtypes.Test(RAILTYPE_RAIL));
593 }
594 }
595
596 /* when disabling elrails, make sure that all existing trains can run on
597 * normal rail too */
598 if (disable) {
599 for (Train *t : Train::Iterate()) {
600 if (t->railtypes.Test(RAILTYPE_ELECTRIC)) {
601 /* this railroad vehicle is now compatible only with elrail,
602 * so add there also normal rail compatibility */
603 t->compatible_railtypes.Set(RAILTYPE_RAIL);
604 t->railtypes.Reset(RAILTYPE_ELECTRIC);
605 t->railtypes.Set(RAILTYPE_RAIL);
607 }
608 }
609 }
610
611 /* Fix the total power and acceleration for trains */
612 if (update_vehicles) {
613 for (Train *t : Train::Iterate()) {
614 /* power and acceleration is cached only for front engines */
615 if (t->IsFrontEngine()) {
616 t->ConsistChanged(CCF_TRACK);
617 }
618 }
619 }
620
621 for (Company *c : Company::Iterate()) c->avail_railtypes = GetCompanyRailTypes(c->index);
622
623 /* This resets the _last_built_railtype, which will be invalid for electric
624 * rails. It may have unintended consequences if that function is ever
625 * extended, though. */
627}
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.
constexpr bool HasAtMostOneBit(T value)
Test whether value has at most 1 bit 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
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:115
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.
@ Begin
Used to iterate.
@ 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.
@ Begin
Used for iterations.
@ 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:196
void SettingsDisableElrail(int32_t new_value)
Callback for changes to the electrified rails setting.
Definition elrail.cpp:578
void DrawRailCatenaryOnBridge(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition elrail.cpp:492
static SpriteID GetWireBase(TileIndex tile, TileContext context=TCX_NORMAL)
Get the base wire sprite to use.
Definition elrail.cpp:183
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:258
static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
Masks out track bits when neighbouring tiles are unelectrified.
Definition elrail.cpp:128
void DrawRailCatenary(const TileInfo *ti)
Draws overhead wires and pylons for electric railways.
Definition elrail.cpp:552
static void AdjustTileh(TileIndex tile, Slope *tileh)
Corrects the tileh for certain tile types.
Definition elrail.cpp:208
static void DrawRailCatenaryRailway(const TileInfo *ti)
Draws wires and, if required, pylons on a given tile.
Definition elrail.cpp:272
static int GetPCPElevation(TileIndex tile, DiagDirection pcp_pos)
Returns the Z position of a Pylon Control Point.
Definition elrail.cpp:228
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 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 RailCatenarySprite _rail_wires[5][TRACK_END][4]
Selects a Wire (with white and grey ends) depending on whether: a) none (should never happen) b) the ...
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
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:46
@ Wires
Catenary wires.
Definition rail.h:45
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:301
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: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
TrackBits GetCrossingRailBits(Tile tile)
Get the rail track bits of a level crossing.
Definition road_map.h:378
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 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
@ FOUNDATION_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:33
Slope tileh
Slope of the tile.
Definition tile_cmd.h:34
TileIndex tile
Tile index.
Definition tile_cmd.h:35
'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
TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition track_func.h:77
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition track_func.h:603
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:513
TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:531
TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a DiagDirection to the associated diagonal TrackBits.
Definition track_func.h:482
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track Axis::X -> TRACK_X Axis::Y -> TRACK_Y Uses the fact that t...
Definition track_func.h:66
TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition track_func.h:308
TrackBits
Bitfield corresponding to Track.
Definition track_type.h:42
@ TRACK_BIT_Y
Y-axis track.
Definition track_type.h:45
@ TRACK_BIT_CROSS
X-Y-axis cross.
Definition track_type.h:50
@ TRACK_BIT_HORZ
Upper and lower track.
Definition track_type.h:51
@ TRACK_BIT_NONE
No track.
Definition track_type.h:43
@ TRACK_BIT_X
X-axis track.
Definition track_type.h:44
@ TRACK_BIT_MASK
Bitmask for the first 6 bits.
Definition track_type.h:58
@ TRACK_BIT_VERT
Left and right track.
Definition track_type.h:52
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition track_type.h:111
@ TRACKDIR_BIT_Y_NW
Track y-axis, direction north-west.
Definition track_type.h:121
@ TRACKDIR_BIT_X_NE
Track x-axis, direction north-east.
Definition track_type.h:113
@ TRACKDIR_BIT_Y_SE
Track y-axis, direction south-east.
Definition track_type.h:114
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:112
@ TRACKDIR_BIT_X_SW
Track x-axis, direction south-west.
Definition track_type.h:120
Track
These are used to specify a single track.
Definition track_type.h:19
@ INVALID_TRACK
Flag for an invalid track.
Definition track_type.h:28
@ TRACK_LOWER
Track in the lower corner of the tile (south).
Definition track_type.h:24
@ TRACK_LEFT
Track in the left corner of the tile (west).
Definition track_type.h:25
@ TRACK_RIGHT
Track in the right corner of the tile (east).
Definition track_type.h:26
@ TRACK_UPPER
Track in the upper corner of the tile (north).
Definition track_type.h:23
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:658
Functions related to (drawing on) viewports.
static constexpr int BB_HEIGHT_UNDER_BRIDGE
Some values for constructing bounding boxes (BB).