2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* @file elrail.cpp
|
2006-09-04 20:40:33 +00:00
|
|
|
* This file deals with displaying wires and pylons for electric railways.
|
|
|
|
* <h2>Basics</h2>
|
|
|
|
*
|
|
|
|
* <h3>Tile Types</h3>
|
|
|
|
*
|
|
|
|
* We have two different types of tiles in the drawing code:
|
|
|
|
* Normal Railway Tiles (NRTs) which can have more than one track on it, and
|
|
|
|
* Special Railways tiles (SRTs) which have only one track (like crossings, depots
|
|
|
|
* stations, etc).
|
|
|
|
*
|
|
|
|
* <h3>Location Categories</h3>
|
|
|
|
*
|
|
|
|
* All tiles are categorized into three location groups (TLG):
|
|
|
|
* Group 0: Tiles with both an even X coordinate and an even Y coordinate
|
|
|
|
* Group 1: Tiles with an even X and an odd Y coordinate
|
|
|
|
* Group 2: Tiles with an odd X and an even Y coordinate
|
2013-01-08 22:46:42 +00:00
|
|
|
* Group 3: Tiles with both an odd X and Y coordinate.
|
2006-09-04 20:40:33 +00:00
|
|
|
*
|
|
|
|
* <h3>Pylon Points</h3>
|
|
|
|
* <h4>Control Points</h4>
|
|
|
|
* A Pylon Control Point (PCP) is a position where a wire (or rather two)
|
|
|
|
* is mounted onto a pylon.
|
|
|
|
* Each NRT does contain 4 PCPs which are bitmapped to a byte
|
|
|
|
* variable and are represented by the DiagDirection enum
|
|
|
|
*
|
|
|
|
* Each track ends on two PCPs and thus requires one pylon on each end. However,
|
|
|
|
* there is one exception: Straight-and-level tracks only have one pylon every
|
|
|
|
* other tile.
|
|
|
|
*
|
|
|
|
* Now on each edge there are two PCPs: One from each adjacent tile. Both PCPs
|
2013-01-08 22:46:42 +00:00
|
|
|
* are merged using an OR operation (i. e. if one tile needs a PCP at the position
|
2006-09-04 20:40:33 +00:00
|
|
|
* in question, both tiles get it).
|
|
|
|
*
|
|
|
|
* <h4>Position Points</h4>
|
|
|
|
* A Pylon Position Point (PPP) is a position where a pylon is located on the
|
|
|
|
* ground. Each PCP owns 8 in (45 degree steps) PPPs that are located around
|
|
|
|
* it. PPPs are represented using the Direction enum. Each track bit has PPPs
|
|
|
|
* that are impossible (because the pylon would be situated on the track) and
|
|
|
|
* some that are preferred (because the pylon would be rectangular to the track).
|
|
|
|
*
|
2013-06-28 20:54:05 +00:00
|
|
|
* @image html elrail_tile.png
|
|
|
|
* @image html elrail_track.png
|
2006-09-04 20:40:33 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-03-29 16:30:26 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
2006-03-30 09:08:43 +00:00
|
|
|
#include "station_map.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "viewport_func.h"
|
2006-11-17 19:31:44 +00:00
|
|
|
#include "train.h"
|
2007-12-21 07:38:36 +00:00
|
|
|
#include "rail_gui.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2008-01-23 22:34:04 +00:00
|
|
|
#include "tunnelbridge.h"
|
2008-05-08 16:48:29 +00:00
|
|
|
#include "elrail_func.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "company_base.h"
|
2010-02-07 22:22:54 +00:00
|
|
|
#include "newgrf_railtype.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/elrail_data.h"
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-05-02 17:42:12 +00:00
|
|
|
/**
|
|
|
|
* Get the tile location group of a tile.
|
|
|
|
* @param t The tile to get the tile location group of.
|
|
|
|
* @return The tile location group.
|
|
|
|
*/
|
2006-03-29 16:30:26 +00:00
|
|
|
static inline TLG GetTLG(TileIndex t)
|
|
|
|
{
|
2007-11-19 21:02:30 +00:00
|
|
|
return (TLG)((HasBit(TileX(t), 0) << 1) + HasBit(TileY(t), 0));
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
struct DualTrackBits {
|
2019-07-08 15:19:07 +00:00
|
|
|
TrackBits primary;
|
|
|
|
TrackBits secondary;
|
2018-12-21 03:27:58 +00:00
|
|
|
};
|
|
|
|
|
2008-01-23 23:22:47 +00:00
|
|
|
/**
|
|
|
|
* Finds which Electrified Rail Bits are present on a given tile.
|
|
|
|
* @param t tile to check
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param override pointer to PCP override, can be nullptr
|
2008-01-23 23:22:47 +00:00
|
|
|
* @return trackbits of tile if it is electrified
|
2006-09-04 20:40:33 +00:00
|
|
|
*/
|
2018-12-21 03:27:58 +00:00
|
|
|
static DualTrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
|
2006-03-29 16:30:26 +00:00
|
|
|
{
|
2018-12-21 03:27:58 +00:00
|
|
|
DualTrackBits out;
|
|
|
|
out.primary = TRACK_BIT_NONE;
|
|
|
|
out.secondary = TRACK_BIT_NONE;
|
2006-03-29 16:30:26 +00:00
|
|
|
switch (GetTileType(t)) {
|
2018-12-21 03:27:58 +00:00
|
|
|
case MP_RAILWAY: {
|
2006-03-29 16:30:26 +00:00
|
|
|
switch (GetRailTileType(t)) {
|
2018-12-21 03:27:58 +00:00
|
|
|
case RAIL_TILE_NORMAL: case RAIL_TILE_SIGNALS: {
|
|
|
|
RailType secondary = GetTileSecondaryRailTypeIfValid(t);
|
|
|
|
TrackBits present_bits = GetTrackBits(t);
|
|
|
|
if (secondary != INVALID_RAILTYPE) {
|
|
|
|
if (HasRailCatenary(GetSecondaryRailType(t))) {
|
|
|
|
out.secondary = present_bits & TRACK_BIT_RT_2;
|
|
|
|
}
|
|
|
|
present_bits &= TRACK_BIT_RT_1;
|
|
|
|
}
|
|
|
|
if (HasRailCatenary(GetRailType(t))) {
|
|
|
|
out.primary = present_bits;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-03-29 16:30:26 +00:00
|
|
|
default:
|
2018-12-21 03:27:58 +00:00
|
|
|
break;
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-12-21 03:27:58 +00:00
|
|
|
}
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
case MP_TUNNELBRIDGE: {
|
|
|
|
if (GetTunnelBridgeTransportType(t) != TRANSPORT_RAIL) break;
|
|
|
|
TrackBits primary_bits = GetPrimaryTunnelBridgeTrackBits(t);
|
|
|
|
TrackBits secondary_bits = GetSecondaryTunnelBridgeTrackBits(t);
|
|
|
|
if (HasRailCatenary(GetRailType(t))) {
|
|
|
|
out.primary = primary_bits;
|
|
|
|
}
|
|
|
|
if (secondary_bits && HasRailCatenary(GetSecondaryRailType(t))) {
|
|
|
|
out.secondary = secondary_bits;
|
|
|
|
}
|
2019-04-11 17:14:13 +00:00
|
|
|
if ((out.primary | out.secondary) && override != nullptr && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
|
2007-12-16 19:30:42 +00:00
|
|
|
*override = 1 << GetTunnelBridgeDirection(t);
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
2018-12-21 03:27:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
case MP_ROAD:
|
2018-12-21 03:27:58 +00:00
|
|
|
if (!IsLevelCrossing(t)) break;
|
|
|
|
if (!HasRailCatenary(GetRailType(t))) break;
|
|
|
|
out.primary = GetCrossingRailBits(t);
|
|
|
|
break;
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
case MP_STATION:
|
2018-12-21 03:27:58 +00:00
|
|
|
if (!HasStationRail(t)) break;
|
|
|
|
if (!HasRailCatenary(GetRailType(t))) break;
|
|
|
|
out.primary = TrackToTrackBits(GetRailStationTrack(t));
|
|
|
|
break;
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
default:
|
2018-12-21 03:27:58 +00:00
|
|
|
break;
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
2018-12-21 03:27:58 +00:00
|
|
|
return out;
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 01:33:59 +00:00
|
|
|
/**
|
|
|
|
* Masks out track bits when neighbouring tiles are unelectrified.
|
|
|
|
*/
|
|
|
|
static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
|
|
|
|
{
|
2021-02-27 19:24:22 +00:00
|
|
|
/* Single track bits are never masked out. */
|
2021-02-27 19:24:22 +00:00
|
|
|
if (likely(HasAtMostOneBit(tracks))) return tracks;
|
|
|
|
|
2009-05-18 01:35:15 +00:00
|
|
|
if (!IsPlainRailTile(t)) return tracks;
|
2009-02-05 01:33:59 +00:00
|
|
|
|
|
|
|
TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
|
|
|
|
for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
|
2013-01-08 22:46:42 +00:00
|
|
|
/* If the neighbour tile is either not electrified or has no tracks that can be reached
|
2009-02-05 01:33:59 +00:00
|
|
|
* from this tile, mark all trackdirs that can be reached from the neighbour tile
|
2012-12-21 18:51:10 +00:00
|
|
|
* as needing no catenary. We make an exception for blocked station tiles with a matching
|
|
|
|
* axis that still display wires to preserve visual continuity. */
|
|
|
|
TileIndex next_tile = TileAddByDiagDir(t, d);
|
2018-12-21 03:27:58 +00:00
|
|
|
RailType rt = GetTileRailTypeByEntryDir(next_tile, d);
|
2016-11-05 11:53:03 +00:00
|
|
|
if (rt == INVALID_RAILTYPE || !HasRailCatenary(rt) ||
|
2012-12-21 18:51:10 +00:00
|
|
|
((TrackStatusToTrackBits(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTracks(d)) == TRACK_BIT_NONE &&
|
|
|
|
(!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
|
2009-02-05 01:33:59 +00:00
|
|
|
neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the tracks from either a diagonal crossing or don't overlap, both
|
|
|
|
* trackdirs have to be marked to mask the corresponding track bit. Else
|
|
|
|
* one marked trackdir is enough the mask the track bit. */
|
2009-02-05 01:33:59 +00:00
|
|
|
TrackBits mask;
|
2009-02-05 01:33:59 +00:00
|
|
|
if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
|
|
|
|
/* If the tracks form either a diagonal crossing or don't overlap, both
|
|
|
|
* trackdirs have to be marked to mask the corresponding track bit. */
|
|
|
|
mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
|
|
|
|
/* If that results in no masked tracks and it is not a diagonal crossing,
|
|
|
|
* require only one marked trackdir to mask. */
|
|
|
|
if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
|
|
|
|
} else {
|
|
|
|
/* Require only one marked trackdir to mask the track. */
|
|
|
|
mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
|
|
|
|
/* If that results in an empty set, require both trackdirs for diagonal track. */
|
|
|
|
if ((tracks & mask) == TRACK_BIT_NONE) {
|
|
|
|
if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
|
|
|
|
if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
|
|
|
|
/* If that still is not enough, require both trackdirs for any track. */
|
|
|
|
if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mask the tracks only if at least one track bit would remain. */
|
|
|
|
return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
|
|
|
|
}
|
|
|
|
|
2009-01-25 19:27:13 +00:00
|
|
|
/**
|
|
|
|
* Get the base wire sprite to use.
|
|
|
|
*/
|
2018-12-21 03:27:58 +00:00
|
|
|
static inline SpriteID GetWireBase(TileIndex tile, RailType rt, TileContext context = TCX_NORMAL)
|
2009-01-25 19:27:13 +00:00
|
|
|
{
|
2018-12-21 03:27:58 +00:00
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(rt);
|
2010-08-09 07:10:42 +00:00
|
|
|
SpriteID wires = GetCustomRailSprite(rti, tile, RTSG_WIRES, context);
|
2010-02-07 22:22:54 +00:00
|
|
|
return wires == 0 ? SPR_WIRE_BASE : wires;
|
2009-01-25 19:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the base pylon sprite to use.
|
|
|
|
*/
|
2018-12-21 03:27:58 +00:00
|
|
|
static inline SpriteID GetPylonBase(TileIndex tile, RailType rt, TileContext context = TCX_NORMAL)
|
2009-01-25 19:27:13 +00:00
|
|
|
{
|
2018-12-21 03:27:58 +00:00
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(rt);
|
2010-08-09 07:10:42 +00:00
|
|
|
SpriteID pylons = GetCustomRailSprite(rti, tile, RTSG_PYLONS, context);
|
2010-02-07 22:22:54 +00:00
|
|
|
return pylons == 0 ? SPR_PYLON_BASE : pylons;
|
2009-01-25 19:27:13 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
|
2006-09-04 20:40:33 +00:00
|
|
|
* @param tile The tile to analyse
|
|
|
|
* @param *tileh the tileh
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
static void AdjustTileh(TileIndex tile, Slope *tileh)
|
2006-04-05 09:17:43 +00:00
|
|
|
{
|
2006-06-27 21:25:53 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
|
if (IsTunnel(tile)) {
|
2007-03-09 15:12:37 +00:00
|
|
|
*tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
|
2018-07-03 18:09:10 +00:00
|
|
|
} else if (IsRailCustomBridgeHeadTile(tile)) {
|
|
|
|
/* no change */
|
2007-03-09 15:12:37 +00:00
|
|
|
} else if (*tileh != SLOPE_FLAT) {
|
|
|
|
*tileh = SLOPE_FLAT;
|
2006-04-05 09:17:43 +00:00
|
|
|
} else {
|
2008-01-25 15:47:58 +00:00
|
|
|
*tileh = InclinedSlope(GetTunnelBridgeDirection(tile));
|
2006-04-05 09:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-07 21:11:12 +00:00
|
|
|
/**
|
|
|
|
* Returns the Z position of a Pylon Control Point.
|
|
|
|
*
|
|
|
|
* @param tile The tile the pylon should stand on.
|
|
|
|
* @param PCPpos The PCP of the tile.
|
|
|
|
* @return The Z position of the PCP.
|
|
|
|
*/
|
2011-11-04 11:52:19 +00:00
|
|
|
static int GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
|
2007-09-07 21:11:12 +00:00
|
|
|
{
|
|
|
|
/* The elevation of the "pylon"-sprite should be the elevation at the PCP.
|
|
|
|
* PCPs are always on a tile edge.
|
|
|
|
*
|
|
|
|
* This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
|
|
|
|
* So we have to move it inside the tile, because if the neighboured tile has a foundation,
|
2011-11-04 10:18:13 +00:00
|
|
|
* that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
|
2007-09-07 21:11:12 +00:00
|
|
|
*
|
|
|
|
* When we move the position inside the tile, we will get a wrong elevation if we have a slope.
|
|
|
|
* To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
|
|
|
|
* This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
|
|
|
|
*
|
2011-11-04 10:18:13 +00:00
|
|
|
* Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
|
2007-09-07 21:11:12 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-08 10:16:18 +00:00
|
|
|
int z = GetSlopePixelZ(TileX(tile) * TILE_SIZE + std::min<int8>(x_pcp_offsets[PCPpos], TILE_SIZE - 1),
|
|
|
|
TileY(tile) * TILE_SIZE + std::min<int8>(y_pcp_offsets[PCPpos], TILE_SIZE - 1));
|
2007-09-07 21:11:12 +00:00
|
|
|
return (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
|
|
|
|
}
|
|
|
|
|
2007-09-19 16:36:42 +00:00
|
|
|
/**
|
|
|
|
* Draws wires on a tunnel tile
|
|
|
|
*
|
|
|
|
* DrawTile_TunnelBridge() calls this function to draw the wires as SpriteCombine with the tunnel roof.
|
|
|
|
*
|
|
|
|
* @param ti The Tileinfo to draw the tile for
|
|
|
|
*/
|
2016-11-05 11:53:03 +00:00
|
|
|
void DrawRailCatenaryOnTunnel(const TileInfo *ti)
|
2007-09-19 16:36:42 +00:00
|
|
|
{
|
|
|
|
/* xmin, ymin, xmax + 1, ymax + 1 of BB */
|
|
|
|
static const int _tunnel_wire_BB[4][4] = {
|
|
|
|
{ 0, 1, 16, 15 }, // NE
|
|
|
|
{ 1, 0, 15, 16 }, // SE
|
|
|
|
{ 0, 1, 16, 15 }, // SW
|
|
|
|
{ 1, 0, 15, 16 }, // NW
|
|
|
|
};
|
|
|
|
|
2007-12-16 15:38:51 +00:00
|
|
|
DiagDirection dir = GetTunnelBridgeDirection(ti->tile);
|
2007-09-19 16:36:42 +00:00
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID wire_base = GetWireBase(ti->tile, GetRailType(ti->tile));
|
2009-01-25 19:27:13 +00:00
|
|
|
|
2016-11-05 11:53:03 +00:00
|
|
|
const SortableSpriteStruct *sss = &RailCatenarySpriteData_Tunnel[dir];
|
2007-09-19 16:36:42 +00:00
|
|
|
const int *BB_data = _tunnel_wire_BB[dir];
|
|
|
|
AddSortableSpriteToDraw(
|
2009-01-25 19:27:13 +00:00
|
|
|
wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
|
2007-09-19 16:36:42 +00:00
|
|
|
BB_data[2] - sss->x_offset, BB_data[3] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset + 1,
|
2011-11-04 10:18:13 +00:00
|
|
|
GetTilePixelZ(ti->tile) + sss->z_offset,
|
2008-02-10 15:53:26 +00:00
|
|
|
IsTransparencySet(TO_CATENARY),
|
2007-09-19 16:36:42 +00:00
|
|
|
BB_data[0] - sss->x_offset, BB_data[1] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Draws wires and, if required, pylons on a given tile
|
2006-09-04 20:40:33 +00:00
|
|
|
* @param ti The Tileinfo to draw the tile for
|
|
|
|
*/
|
2016-11-05 11:53:03 +00:00
|
|
|
static void DrawRailCatenaryRailway(const TileInfo *ti)
|
2006-03-29 16:30:26 +00:00
|
|
|
{
|
|
|
|
/* Pylons are placed on a tile edge, so we need to take into account
|
2006-09-04 20:40:33 +00:00
|
|
|
* the track configuration of 2 adjacent tiles. trackconfig[0] stores the
|
|
|
|
* current tile (home tile) while [1] holds the neighbour */
|
2006-03-29 16:30:26 +00:00
|
|
|
TrackBits trackconfig[TS_END];
|
2009-02-05 01:33:59 +00:00
|
|
|
TrackBits wireconfig[TS_END];
|
2006-03-29 16:30:26 +00:00
|
|
|
bool isflat[TS_END];
|
|
|
|
/* Note that ti->tileh has already been adjusted for Foundations */
|
2006-04-23 13:48:16 +00:00
|
|
|
Slope tileh[TS_END] = { ti->tileh, SLOPE_FLAT };
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2007-10-20 16:50:48 +00:00
|
|
|
/* Half tile slopes coincide only with horizontal/vertical track.
|
|
|
|
* Faking a flat slope results in the correct sprites on positions. */
|
2010-07-11 17:28:19 +00:00
|
|
|
Corner halftile_corner = CORNER_INVALID;
|
|
|
|
if (IsHalftileSlope(tileh[TS_HOME])) {
|
2010-07-12 18:15:37 +00:00
|
|
|
halftile_corner = GetHalftileSlopeCorner(tileh[TS_HOME]);
|
2010-07-11 17:28:19 +00:00
|
|
|
tileh[TS_HOME] = SLOPE_FLAT;
|
|
|
|
}
|
2007-10-20 16:50:48 +00:00
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
TLG tlg = GetTLG(ti->tile);
|
|
|
|
byte PCPstatus = 0;
|
|
|
|
byte OverridePCP = 0;
|
2006-03-31 16:01:59 +00:00
|
|
|
byte PPPpreferred[DIAGDIR_END];
|
|
|
|
byte PPPallowed[DIAGDIR_END];
|
2006-03-29 16:30:26 +00:00
|
|
|
|
|
|
|
/* Find which rail bits are present, and select the override points.
|
2006-09-04 20:40:33 +00:00
|
|
|
* We don't draw a pylon:
|
|
|
|
* 1) INSIDE a tunnel (we wouldn't see it anyway)
|
|
|
|
* 2) on the "far" end of a bridge head (the one that connects to bridge middle),
|
|
|
|
* because that one is drawn on the bridge. Exception is for length 0 bridges
|
|
|
|
* which have no middle tiles */
|
2018-12-21 03:27:58 +00:00
|
|
|
DualTrackBits home_track_config = GetRailTrackBitsUniversal(ti->tile, &OverridePCP);
|
|
|
|
trackconfig[TS_HOME] = home_track_config.primary | home_track_config.secondary;
|
2009-02-05 01:33:59 +00:00
|
|
|
wireconfig[TS_HOME] = MaskWireBits(ti->tile, trackconfig[TS_HOME]);
|
2006-03-29 16:30:26 +00:00
|
|
|
/* If a track bit is present that is not in the main direction, the track is level */
|
2007-01-10 18:56:51 +00:00
|
|
|
isflat[TS_HOME] = ((trackconfig[TS_HOME] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2006-04-05 09:17:43 +00:00
|
|
|
AdjustTileh(ti->tile, &tileh[TS_HOME]);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID pylon_normal = 0;
|
|
|
|
SpriteID pylon_halftile = 0;
|
|
|
|
SpriteID pylon_normal_secondary = 0;
|
|
|
|
SpriteID pylon_halftile_secondary = 0;
|
|
|
|
|
|
|
|
auto get_pylon_sprite = [&](DiagDirection edge, bool halftile) -> SpriteID {
|
|
|
|
static const uint edge_tracks[] = {
|
|
|
|
TRACK_BIT_UPPER | TRACK_BIT_RIGHT, // DIAGDIR_NE
|
|
|
|
TRACK_BIT_LOWER | TRACK_BIT_RIGHT, // DIAGDIR_SE
|
|
|
|
TRACK_BIT_LOWER | TRACK_BIT_LEFT, // DIAGDIR_SW
|
|
|
|
TRACK_BIT_UPPER | TRACK_BIT_LEFT, // DIAGDIR_NW
|
|
|
|
};
|
|
|
|
if (home_track_config.secondary && (home_track_config.secondary & edge_tracks[edge])) {
|
|
|
|
if (pylon_normal_secondary == 0) {
|
|
|
|
pylon_normal_secondary = GetPylonBase(ti->tile, GetSecondaryRailType(ti->tile));
|
|
|
|
}
|
|
|
|
if (halftile) {
|
|
|
|
if (pylon_halftile_secondary == 0) {
|
|
|
|
pylon_halftile_secondary = (halftile_corner != CORNER_INVALID) ? GetPylonBase(ti->tile, GetSecondaryRailType(ti->tile), TCX_UPPER_HALFTILE) : pylon_normal_secondary;
|
|
|
|
}
|
|
|
|
return pylon_halftile_secondary;
|
|
|
|
} else {
|
|
|
|
return pylon_normal_secondary;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pylon_normal == 0) {
|
|
|
|
pylon_normal = GetPylonBase(ti->tile, GetRailType(ti->tile));
|
|
|
|
}
|
|
|
|
if (halftile) {
|
|
|
|
if (pylon_halftile == 0) {
|
|
|
|
pylon_halftile = (halftile_corner != CORNER_INVALID) ? GetPylonBase(ti->tile, GetRailType(ti->tile), TCX_UPPER_HALFTILE) : pylon_normal;
|
|
|
|
}
|
|
|
|
return pylon_halftile;
|
|
|
|
} else {
|
|
|
|
return pylon_normal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-01-25 19:27:13 +00:00
|
|
|
|
2009-02-05 01:33:59 +00:00
|
|
|
for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
|
2010-07-11 17:28:19 +00:00
|
|
|
static const uint edge_corners[] = {
|
|
|
|
1 << CORNER_N | 1 << CORNER_E, // DIAGDIR_NE
|
|
|
|
1 << CORNER_S | 1 << CORNER_E, // DIAGDIR_SE
|
|
|
|
1 << CORNER_S | 1 << CORNER_W, // DIAGDIR_SW
|
|
|
|
1 << CORNER_N | 1 << CORNER_W, // DIAGDIR_NW
|
|
|
|
};
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID pylon_base = get_pylon_sprite(i, halftile_corner != CORNER_INVALID && HasBit(edge_corners[i], halftile_corner));
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
|
2011-11-04 13:40:59 +00:00
|
|
|
int elevation = GetPCPElevation(ti->tile, i);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2011-11-04 10:22:27 +00:00
|
|
|
/* Here's one of the main headaches. GetTileSlope does not correct for possibly
|
2006-09-04 20:40:33 +00:00
|
|
|
* existing foundataions, so we do have to do that manually later on.*/
|
2011-11-04 10:22:27 +00:00
|
|
|
tileh[TS_NEIGHBOUR] = GetTileSlope(neighbour);
|
2019-04-11 17:14:13 +00:00
|
|
|
DualTrackBits neighbour_track_config = GetRailTrackBitsUniversal(neighbour, nullptr);
|
2018-12-21 03:27:58 +00:00
|
|
|
trackconfig[TS_NEIGHBOUR] = neighbour_track_config.primary | neighbour_track_config.secondary;
|
2009-02-05 01:33:59 +00:00
|
|
|
wireconfig[TS_NEIGHBOUR] = MaskWireBits(neighbour, trackconfig[TS_NEIGHBOUR]);
|
|
|
|
if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
|
2007-09-07 21:11:12 +00:00
|
|
|
|
2012-12-21 18:51:06 +00:00
|
|
|
/* Ignore station tiles that allow neither wires nor pylons. */
|
|
|
|
if (IsRailStationTile(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
|
|
|
|
|
2007-09-07 21:11:12 +00:00
|
|
|
/* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
|
|
|
|
* we have to draw all pillars on the current tile. */
|
2009-02-05 01:33:59 +00:00
|
|
|
if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
|
2007-09-07 21:11:12 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
isflat[TS_NEIGHBOUR] = ((trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2007-03-09 15:12:37 +00:00
|
|
|
PPPpreferred[i] = 0xFF; // We start with preferring everything (end-of-line in any direction)
|
2006-04-05 09:17:43 +00:00
|
|
|
PPPallowed[i] = AllowedPPPonPCP[i];
|
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
/* We cycle through all the existing tracks at a PCP and see what
|
2006-09-04 20:40:33 +00:00
|
|
|
* PPPs we want to have, or may not have at all */
|
2008-04-23 20:22:31 +00:00
|
|
|
for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
|
2006-03-29 16:30:26 +00:00
|
|
|
/* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
|
2006-04-05 09:17:43 +00:00
|
|
|
if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
|
2006-12-27 12:38:02 +00:00
|
|
|
IsBridgeTile(neighbour) &&
|
2007-12-16 15:38:51 +00:00
|
|
|
GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
|
2006-06-10 08:37:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2006-04-05 09:17:43 +00:00
|
|
|
/* We check whether the track in question (k) is present in the tile
|
2006-09-04 20:40:33 +00:00
|
|
|
* (TrackSourceTile) */
|
2009-02-05 01:33:59 +00:00
|
|
|
DiagDirection PCPpos = i;
|
|
|
|
if (HasBit(wireconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
|
2006-04-05 09:17:43 +00:00
|
|
|
/* track found, if track is in the neighbour tile, adjust the number
|
2006-09-04 20:40:33 +00:00
|
|
|
* of the PCP for preferred/allowed determination*/
|
2009-02-05 01:33:59 +00:00
|
|
|
PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(PCPstatus, i); // This PCP is in use
|
2009-02-05 02:07:13 +00:00
|
|
|
PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
|
2009-02-05 01:33:59 +00:00
|
|
|
}
|
2006-04-05 09:17:43 +00:00
|
|
|
|
2009-02-05 01:33:59 +00:00
|
|
|
if (HasBit(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
|
2006-04-05 09:17:43 +00:00
|
|
|
PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deactivate all PPPs if PCP is not used */
|
2008-04-23 20:22:31 +00:00
|
|
|
if (!HasBit(PCPstatus, i)) {
|
|
|
|
PPPpreferred[i] = 0;
|
|
|
|
PPPallowed[i] = 0;
|
|
|
|
}
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2011-05-08 16:19:04 +00:00
|
|
|
Foundation foundation = FOUNDATION_NONE;
|
|
|
|
|
|
|
|
/* Station and road crossings are always "flat", so adjust the tileh accordingly */
|
|
|
|
if (IsTileType(neighbour, MP_STATION) || IsTileType(neighbour, MP_ROAD)) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* Read the foundations if they are present, and adjust the tileh */
|
2018-12-21 03:27:58 +00:00
|
|
|
if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE && IsTileType(neighbour, MP_RAILWAY) && HasRailCatenary(GetTileRailTypeByEntryDir(neighbour, i))) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
|
2006-12-27 12:38:02 +00:00
|
|
|
if (IsBridgeTile(neighbour)) {
|
2007-12-16 15:38:51 +00:00
|
|
|
foundation = GetBridgeFoundation(tileh[TS_NEIGHBOUR], DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
|
2006-03-30 09:26:17 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 10:31:46 +00:00
|
|
|
ApplyFoundationToSlope(foundation, &tileh[TS_NEIGHBOUR]);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2008-04-23 20:22:31 +00:00
|
|
|
/* Half tile slopes coincide only with horizontal/vertical track.
|
|
|
|
* Faking a flat slope results in the correct sprites on positions. */
|
2007-10-20 16:50:48 +00:00
|
|
|
if (IsHalftileSlope(tileh[TS_NEIGHBOUR])) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
|
|
|
|
|
2006-04-05 09:17:43 +00:00
|
|
|
AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
|
|
|
/* If we have a straight (and level) track, we want a pylon only every 2 tiles
|
2009-03-15 00:32:18 +00:00
|
|
|
* Delete the PCP if this is the case.
|
|
|
|
* Level means that the slope is the same, or the track is flat */
|
2006-03-29 16:30:26 +00:00
|
|
|
if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
|
2008-04-23 20:22:31 +00:00
|
|
|
for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
|
2007-11-19 21:32:20 +00:00
|
|
|
if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) ClrBit(PCPstatus, i);
|
2008-04-23 20:22:31 +00:00
|
|
|
}
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2006-04-05 09:17:43 +00:00
|
|
|
/* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
|
2006-09-04 20:40:33 +00:00
|
|
|
* In that case, we try the any of the allowed ones. if they don't exist either, don't draw
|
|
|
|
* anything. Note that the preferred PPPs still contain the end-of-line markers.
|
|
|
|
* Remove those (simply by ANDing with allowed, since these markers are never allowed) */
|
2006-06-27 21:25:53 +00:00
|
|
|
if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2014-09-21 11:24:51 +00:00
|
|
|
if (IsBridgeAbove(ti->tile)) {
|
2006-12-27 12:38:02 +00:00
|
|
|
Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
|
2011-11-04 11:30:37 +00:00
|
|
|
int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
|
2006-12-27 12:38:02 +00:00
|
|
|
|
2011-11-04 10:28:20 +00:00
|
|
|
if ((height <= GetTileMaxZ(ti->tile) + 1) &&
|
2007-03-09 15:12:37 +00:00
|
|
|
(i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(OverridePCP, i);
|
2007-03-09 15:12:37 +00:00
|
|
|
}
|
2006-12-27 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2012-07-15 16:17:22 +00:00
|
|
|
if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i) &&
|
|
|
|
(!IsRailStationTile(ti->tile) || CanStationTileHavePylons(ti->tile))) {
|
2008-04-23 20:22:31 +00:00
|
|
|
for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
|
2006-03-29 16:30:26 +00:00
|
|
|
byte temp = PPPorder[i][GetTLG(ti->tile)][k];
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(PPPallowed[i], temp)) {
|
2006-03-29 16:30:26 +00:00
|
|
|
uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
|
|
|
|
uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
|
|
|
|
|
|
|
|
/* Don't build the pylon if it would be outside the tile */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(OwnedPPPonPCP[i], temp)) {
|
2013-01-08 22:46:42 +00:00
|
|
|
/* We have a neighbour that will draw it, bail out */
|
2009-02-05 01:33:59 +00:00
|
|
|
if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE) break;
|
2009-03-15 00:32:18 +00:00
|
|
|
continue; // No neighbour, go looking for a better position
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-01-25 19:27:13 +00:00
|
|
|
AddSortableSpriteToDraw(pylon_base + pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
|
2008-04-23 20:22:31 +00:00
|
|
|
elevation, IsTransparencySet(TO_CATENARY), -1, -1);
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
break; // We already have drawn a pylon, bail out
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 11:53:03 +00:00
|
|
|
/* The wire above the tunnel is drawn together with the tunnel-roof (see DrawRailCatenaryOnTunnel()) */
|
2008-09-11 19:44:30 +00:00
|
|
|
if (IsTunnelTile(ti->tile)) return;
|
|
|
|
|
2006-08-31 11:18:30 +00:00
|
|
|
/* Don't draw a wire under a low bridge */
|
2014-09-21 11:24:51 +00:00
|
|
|
if (IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_BRIDGES)) {
|
2011-11-04 11:30:37 +00:00
|
|
|
int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
|
2006-12-27 12:38:02 +00:00
|
|
|
|
2011-11-04 10:28:20 +00:00
|
|
|
if (height <= GetTileMaxZ(ti->tile) + 1) return;
|
2006-08-31 11:18:30 +00:00
|
|
|
}
|
|
|
|
|
2012-07-15 16:17:22 +00:00
|
|
|
/* Don't draw a wire if the station tile does not want any */
|
|
|
|
if (IsRailStationTile(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
|
|
|
|
|
2010-07-11 17:28:19 +00:00
|
|
|
Track halftile_track;
|
|
|
|
switch (halftile_corner) {
|
|
|
|
case CORNER_W: halftile_track = TRACK_LEFT; break;
|
|
|
|
case CORNER_S: halftile_track = TRACK_LOWER; break;
|
|
|
|
case CORNER_E: halftile_track = TRACK_RIGHT; break;
|
|
|
|
case CORNER_N: halftile_track = TRACK_UPPER; break;
|
|
|
|
default: halftile_track = INVALID_TRACK; break;
|
|
|
|
}
|
2009-01-25 19:27:13 +00:00
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID wire_normal = 0;
|
|
|
|
SpriteID wire_halftile = 0;
|
|
|
|
SpriteID wire_normal_secondary = 0;
|
|
|
|
SpriteID wire_halftile_secondary = 0;
|
|
|
|
|
|
|
|
auto get_wire_sprite = [&](Track track, bool halftile) -> SpriteID {
|
|
|
|
if (home_track_config.secondary && HasTrack(home_track_config.secondary, track)) {
|
|
|
|
if (wire_normal_secondary == 0) {
|
|
|
|
wire_normal_secondary = GetWireBase(ti->tile, GetSecondaryRailType(ti->tile));
|
|
|
|
}
|
|
|
|
if (halftile) {
|
|
|
|
if (wire_halftile_secondary == 0) {
|
|
|
|
wire_halftile_secondary = (halftile_corner != CORNER_INVALID) ? GetWireBase(ti->tile, GetSecondaryRailType(ti->tile), TCX_UPPER_HALFTILE) : wire_normal_secondary;
|
|
|
|
}
|
|
|
|
return wire_halftile_secondary;
|
|
|
|
} else {
|
|
|
|
return wire_normal_secondary;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (wire_normal == 0) {
|
|
|
|
wire_normal = GetWireBase(ti->tile, GetRailType(ti->tile));
|
|
|
|
}
|
|
|
|
if (halftile) {
|
|
|
|
if (wire_halftile == 0) {
|
|
|
|
wire_halftile = (halftile_corner != CORNER_INVALID) ? GetWireBase(ti->tile, GetRailType(ti->tile), TCX_UPPER_HALFTILE) : wire_normal;
|
|
|
|
}
|
|
|
|
return wire_halftile;
|
|
|
|
} else {
|
|
|
|
return wire_normal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
/* Drawing of pylons is finished, now draw the wires */
|
2021-06-13 03:15:36 +00:00
|
|
|
for (Track t : SetTrackBitIterator(wireconfig[TS_HOME])) {
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID wire_base = get_wire_sprite(t, (t == halftile_track));
|
2010-05-11 21:02:26 +00:00
|
|
|
byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
|
|
|
|
(HasBit(PCPstatus, PCPpositions[t][1]) << 1);
|
|
|
|
|
|
|
|
const SortableSpriteStruct *sss;
|
|
|
|
int tileh_selector = !(tileh[TS_HOME] % 3) * tileh[TS_HOME] / 3; // tileh for the slopes, 0 otherwise
|
|
|
|
|
|
|
|
assert(PCPconfig != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
|
|
|
|
assert(!IsSteepSlope(tileh[TS_HOME]));
|
2016-11-05 11:53:03 +00:00
|
|
|
sss = &RailCatenarySpriteData[Wires[tileh_selector][t][PCPconfig]];
|
2010-05-11 21:02:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
|
2011-11-04 10:18:13 +00:00
|
|
|
* Therefore it is safe to use GetSlopePixelZ() for the elevation.
|
|
|
|
* Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
|
2010-05-11 21:02:26 +00:00
|
|
|
*/
|
|
|
|
AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
|
2011-11-04 10:18:13 +00:00
|
|
|
sss->x_size, sss->y_size, sss->z_size, GetSlopePixelZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
|
2010-05-11 21:02:26 +00:00
|
|
|
IsTransparencySet(TO_CATENARY));
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
|
|
|
* Draws wires on a tunnel tile
|
|
|
|
*
|
|
|
|
* DrawTile_TunnelBridge() calls this function to draw the wires on the bridge.
|
|
|
|
*
|
|
|
|
* @param ti The Tileinfo to draw the tile for
|
|
|
|
*/
|
2016-11-05 11:53:03 +00:00
|
|
|
void DrawRailCatenaryOnBridge(const TileInfo *ti)
|
2006-03-29 16:30:26 +00:00
|
|
|
{
|
2006-04-04 12:31:29 +00:00
|
|
|
TileIndex end = GetSouthernBridgeEnd(ti->tile);
|
|
|
|
TileIndex start = GetOtherBridgeEnd(end);
|
|
|
|
|
2008-01-23 22:34:04 +00:00
|
|
|
uint length = GetTunnelBridgeLength(start, end);
|
|
|
|
uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
|
2006-07-22 09:08:34 +00:00
|
|
|
uint height;
|
2006-04-04 12:31:29 +00:00
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
const SortableSpriteStruct *sss;
|
|
|
|
Axis axis = GetBridgeAxis(ti->tile);
|
|
|
|
TLG tlg = GetTLG(ti->tile);
|
|
|
|
|
2016-11-05 11:53:03 +00:00
|
|
|
RailCatenarySprite offset = (RailCatenarySprite)(axis == AXIS_X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
|
|
|
if ((length % 2) && num == length) {
|
2006-04-04 12:31:29 +00:00
|
|
|
/* Draw the "short" wire on the southern end of the bridge
|
|
|
|
* only needed if the length of the bridge is odd */
|
2016-11-05 11:53:03 +00:00
|
|
|
sss = &RailCatenarySpriteData[WIRE_X_FLAT_BOTH + offset];
|
2006-03-29 16:30:26 +00:00
|
|
|
} else {
|
2006-04-04 12:31:29 +00:00
|
|
|
/* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
|
2016-11-05 11:53:03 +00:00
|
|
|
sss = &RailCatenarySpriteData[WIRE_X_FLAT_SW + (num % 2) + offset];
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 10:18:13 +00:00
|
|
|
height = GetBridgePixelHeight(end);
|
2006-07-22 09:08:34 +00:00
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID wire_base = GetWireBase(end, GetRailType(end), TCX_ON_BRIDGE);
|
2009-01-25 19:27:13 +00:00
|
|
|
|
|
|
|
AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
|
2007-07-26 14:07:11 +00:00
|
|
|
sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
|
2008-02-10 15:53:26 +00:00
|
|
|
IsTransparencySet(TO_CATENARY)
|
2006-07-22 09:08:34 +00:00
|
|
|
);
|
2006-04-04 12:31:29 +00:00
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID pylon_base = GetPylonBase(end, GetRailType(end), TCX_ON_BRIDGE);
|
2009-01-25 19:27:13 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Finished with wires, draw pylons
|
|
|
|
* every other tile needs a pylon on the northern end */
|
2006-03-29 16:30:26 +00:00
|
|
|
if (num % 2) {
|
2007-09-07 21:11:12 +00:00
|
|
|
DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_NE : DIAGDIR_NW);
|
|
|
|
Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
|
2007-09-07 21:11:12 +00:00
|
|
|
uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
|
|
|
|
uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
|
2009-01-25 19:27:13 +00:00
|
|
|
AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2006-04-04 12:31:29 +00:00
|
|
|
/* need a pylon on the southern end of the bridge */
|
2008-01-23 22:34:04 +00:00
|
|
|
if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
|
2007-09-07 21:11:12 +00:00
|
|
|
DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE);
|
|
|
|
Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
|
2007-09-07 21:11:12 +00:00
|
|
|
uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
|
|
|
|
uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
|
2009-01-25 19:27:13 +00:00
|
|
|
AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
|
|
|
* Draws overhead wires and pylons for electric railways.
|
|
|
|
* @param ti The TileInfo struct of the tile being drawn
|
2016-11-05 11:53:03 +00:00
|
|
|
* @see DrawRailCatenaryRailway
|
2011-01-18 22:31:06 +00:00
|
|
|
*/
|
2016-11-05 11:53:03 +00:00
|
|
|
void DrawRailCatenary(const TileInfo *ti)
|
2006-03-29 16:30:26 +00:00
|
|
|
{
|
|
|
|
switch (GetTileType(ti->tile)) {
|
|
|
|
case MP_RAILWAY:
|
2007-03-09 15:12:37 +00:00
|
|
|
if (IsRailDepot(ti->tile)) {
|
2016-11-05 11:53:03 +00:00
|
|
|
const SortableSpriteStruct *sss = &RailCatenarySpriteData_Depot[GetRailDepotDirection(ti->tile)];
|
2006-08-31 07:29:19 +00:00
|
|
|
|
2018-12-21 03:27:58 +00:00
|
|
|
SpriteID wire_base = GetWireBase(ti->tile, GetRailType(ti->tile));
|
2009-01-25 19:27:13 +00:00
|
|
|
|
2007-09-07 21:11:12 +00:00
|
|
|
/* This wire is not visible with the default depot sprites */
|
2006-08-31 07:29:19 +00:00
|
|
|
AddSortableSpriteToDraw(
|
2009-01-25 19:27:13 +00:00
|
|
|
wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
|
2006-08-31 07:29:19 +00:00
|
|
|
sss->x_size, sss->y_size, sss->z_size,
|
2011-11-04 10:18:13 +00:00
|
|
|
GetTileMaxPixelZ(ti->tile) + sss->z_offset,
|
2008-02-10 15:53:26 +00:00
|
|
|
IsTransparencySet(TO_CATENARY)
|
2006-08-31 07:29:19 +00:00
|
|
|
);
|
2006-03-29 16:30:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2006-06-09 15:24:04 +00:00
|
|
|
|
|
|
|
case MP_TUNNELBRIDGE:
|
2007-07-29 23:42:59 +00:00
|
|
|
case MP_ROAD:
|
2006-12-27 12:38:02 +00:00
|
|
|
case MP_STATION:
|
2006-03-29 16:30:26 +00:00
|
|
|
break;
|
2006-06-09 15:24:04 +00:00
|
|
|
|
|
|
|
default: return;
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
2016-11-05 11:53:03 +00:00
|
|
|
DrawRailCatenaryRailway(ti);
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
2006-11-17 19:31:44 +00:00
|
|
|
|
2021-05-24 09:47:37 +00:00
|
|
|
void SettingsDisableElrail(int32 new_value)
|
2006-11-17 19:31:44 +00:00
|
|
|
{
|
2021-05-24 09:47:37 +00:00
|
|
|
bool disable = (new_value != 0);
|
2006-11-17 19:31:44 +00:00
|
|
|
|
|
|
|
/* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
|
|
|
|
const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
|
|
|
|
const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
|
|
|
|
|
|
|
|
/* walk through all train engines */
|
2019-12-15 21:45:18 +00:00
|
|
|
for (Engine *e : Engine::IterateType(VEH_TRAIN)) {
|
2008-04-29 21:31:29 +00:00
|
|
|
RailVehicleInfo *rv_info = &e->u.rail;
|
2006-11-17 19:31:44 +00:00
|
|
|
/* if it is an electric rail engine and its railtype is the wrong one */
|
2007-01-24 07:14:09 +00:00
|
|
|
if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
|
2006-11-17 19:31:44 +00:00
|
|
|
/* change it to the proper one */
|
2007-01-24 07:14:09 +00:00
|
|
|
rv_info->railtype = new_railtype;
|
2006-11-17 19:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* when disabling elrails, make sure that all existing trains can run on
|
2009-03-14 18:16:29 +00:00
|
|
|
* normal rail too */
|
2006-11-17 19:31:44 +00:00
|
|
|
if (disable) {
|
2019-12-17 02:37:43 +00:00
|
|
|
for (Train *t : Train::Iterate()) {
|
2009-05-22 22:33:05 +00:00
|
|
|
if (t->railtype == RAILTYPE_ELECTRIC) {
|
2006-11-17 19:31:44 +00:00
|
|
|
/* this railroad vehicle is now compatible only with elrail,
|
2009-03-14 18:16:29 +00:00
|
|
|
* so add there also normal rail compatibility */
|
2009-05-22 22:33:05 +00:00
|
|
|
t->compatible_railtypes |= RAILTYPES_RAIL;
|
|
|
|
t->railtype = RAILTYPE_RAIL;
|
|
|
|
SetBit(t->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL);
|
2006-11-17 19:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-08 15:10:23 +00:00
|
|
|
/* Fix the total power and acceleration for trains */
|
2019-12-17 02:37:43 +00:00
|
|
|
for (Train *t : Train::Iterate()) {
|
2008-07-08 15:10:23 +00:00
|
|
|
/* power and acceleration is cached only for front engines */
|
2009-07-01 22:22:01 +00:00
|
|
|
if (t->IsFrontEngine()) {
|
2014-02-07 23:48:56 +00:00
|
|
|
t->ConsistChanged(CCF_TRACK);
|
2008-07-08 15:10:23 +00:00
|
|
|
}
|
2006-11-17 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 16:22:38 +00:00
|
|
|
for (Company *c : Company::Iterate()) c->avail_railtypes = GetCompanyRailtypes(c->index);
|
2006-11-17 19:31:44 +00:00
|
|
|
|
|
|
|
/* This resets the _last_built_railtype, which will be invalid for electric
|
2009-03-14 18:16:29 +00:00
|
|
|
* rails. It may have unintended consequences if that function is ever
|
|
|
|
* extended, though. */
|
2006-11-17 19:31:44 +00:00
|
|
|
ReinitGuiAfterToggleElrail(disable);
|
|
|
|
}
|