celestar@3355: /* $Id$ */
celestar@3355: /** @file elrail.c
celestar@3449: This file deals with displaying wires and pylons for electric railways.
celestar@3355:
Basics
celestar@3355:
celestar@3355: Tile Types
celestar@3355:
celestar@3355: We have two different types of tiles in the drawing code:
celestar@3355: Normal Railway Tiles (NRTs) which can have more than one track on it, and
celestar@3355: Special Railways tiles (SRTs) which have only one track (like crossings, depots
celestar@3355: stations, etc).
celestar@3355:
celestar@3355: Location Categories
celestar@3355:
celestar@3355: All tiles are categorized into three location groups (TLG):
celestar@3355: Group 0: Tiles with both an even X coordinate and an even Y coordinate
celestar@3355: Group 1: Tiles with an even X and an odd Y coordinate
celestar@3355: Group 2: Tiles with an odd X and an even Y coordinate
celestar@3355: Group 3: Tiles with both an odd X and Y coordnate.
celestar@3355:
celestar@3355: Pylon Points
celestar@3355: Control Points
celestar@3355: A Pylon Control Point (PCP) is a position where a wire (or rather two)
celestar@3355: is mounted onto a pylon.
celestar@3449: Each NRT does contain 4 PCPs which are bitmapped to a byte
celestar@3449: variable and are represented by the DiagDirection enum
celestar@3355:
celestar@3449: Each track ends on two PCPs and thus requires one pylon on each end. However,
celestar@3449: there is one exception: Straight-and-level tracks only have one pylon every
celestar@3449: other tile.
celestar@3355:
celestar@3449: Now on each edge there are two PCPs: One from each adjacent tile. Both PCPs
celestar@3449: are merged using an OR operation (i. e. if one tile needs a PCP at the postion
celestar@3449: in question, both tiles get it).
celestar@3355:
celestar@3355: Position Points
celestar@3449: A Pylon Position Point (PPP) is a position where a pylon is located on the
celestar@3449: ground. Each PCP owns 8 in (45 degree steps) PPPs that are located around
celestar@3449: it. PPPs are represented using the Direction enum. Each track bit has PPPs
celestar@3449: that are impossible (because the pylon would be situated on the track) and
celestar@3449: some that are preferred (because the pylon would be rectangular to the track).
celestar@3355:
celestar@3355:
celestar@3355:
celestar@3355:
celestar@3355: */
celestar@3355:
celestar@3355: #include "stdafx.h"
celestar@3355: #include "openttd.h"
tron@3367: #include "station_map.h"
celestar@3355: #include "tile.h"
celestar@3355: #include "viewport.h"
celestar@3355: #include "functions.h" /* We should REALLY get rid of this goddamn file, as it is butt-ugly */
celestar@3355: #include "variables.h" /* ... same here */
celestar@3355: #include "rail.h"
celestar@3355: #include "debug.h"
celestar@3355: #include "tunnel_map.h"
celestar@3355: #include "road_map.h"
celestar@3355: #include "bridge_map.h"
celestar@3355: #include "bridge.h"
celestar@3355: #include "rail_map.h"
celestar@3355: #include "table/sprites.h"
celestar@3355: #include "table/elrail_data.h"
celestar@3355:
celestar@3355: static inline TLG GetTLG(TileIndex t)
celestar@3355: {
celestar@3355: return (HASBIT(TileX(t), 0) << 1) + HASBIT(TileY(t), 0);
celestar@3355: }
celestar@3355:
celestar@3355: /** Finds which Rail Bits are present on a given tile. For bridge tiles,
celestar@3355: * returns track bits under the bridge
celestar@3355: */
celestar@3355: static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
celestar@3355: {
celestar@3355: switch (GetTileType(t)) {
celestar@3355: case MP_RAILWAY:
celestar@3355: if (GetRailType(t) != RAILTYPE_ELECTRIC) return 0;
celestar@3355: switch (GetRailTileType(t)) {
rubidium@3792: case RAIL_TILE_NORMAL: case RAIL_TILE_SIGNALS:
celestar@3355: return GetTrackBits(t);
rubidium@3792: case RAIL_TILE_DEPOT_WAYPOINT:
celestar@3376: if (GetRailTileSubtype(t) == RAIL_SUBTYPE_WAYPOINT) return GetRailWaypointBits(t);
celestar@3355: default:
celestar@3355: return 0;
celestar@3355: }
celestar@3355: break;
celestar@3355: case MP_TUNNELBRIDGE:
celestar@3355: if (IsTunnel(t)) {
celestar@3355: if (GetRailType(t) != RAILTYPE_ELECTRIC) return 0;
celestar@3355: if (override != NULL) *override = 1 << GetTunnelDirection(t);
tron@3367: return DiagDirToAxis(GetTunnelDirection(t)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y;
celestar@3355: } else {
celestar@3355: if (GetRailType(t) != RAILTYPE_ELECTRIC) return 0;
celestar@3933: if (override != NULL && DistanceMax(t, GetOtherBridgeEnd(t)) > 1) {
celestar@3933: *override = 1 << GetBridgeRampDirection(t);
celestar@3355: }
celestar@3933: return DiagDirToAxis(GetBridgeRampDirection(t)) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y;
celestar@3355: }
celestar@3355: case MP_STREET:
rubidium@3793: if (GetRoadTileType(t) != ROAD_TILE_CROSSING) return 0;
tron@3367: if (GetRailTypeCrossing(t) != RAILTYPE_ELECTRIC) return 0;
celestar@3355: return GetCrossingRailBits(t);
celestar@3355: case MP_STATION:
celestar@3403: if (!IsRailwayStation(t)) return 0;
celestar@3355: if (GetRailType(t) != RAILTYPE_ELECTRIC) return 0;
glx@3789: if (!IsStationTileElectrifiable(t)) return 0;
celestar@3375: return TrackToTrackBits(GetRailStationTrack(t));
celestar@3355: default:
celestar@3355: return 0;
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3451: /** Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
celestar@3451: * @param tile The tile to analyse
celestar@3451: * @param *tileh the tileh
celestar@3451: */
tron@3636: static void AdjustTileh(TileIndex tile, Slope* tileh)
celestar@3451: {
tron@3636: if (IsTunnelTile(tile)) *tileh = SLOPE_FLAT;
celestar@3933: if (IsBridgeTile(tile)) {
tron@3636: if (*tileh != SLOPE_FLAT) {
tron@3636: *tileh = SLOPE_FLAT;
celestar@3451: } else {
celestar@3451: switch (GetBridgeRampDirection(tile)) {
tron@3636: case DIAGDIR_NE: *tileh = SLOPE_NE; break;
tron@3636: case DIAGDIR_SE: *tileh = SLOPE_SE; break;
tron@3636: case DIAGDIR_SW: *tileh = SLOPE_SW; break;
tron@3636: case DIAGDIR_NW: *tileh = SLOPE_NW; break;
celestar@3451: default: break;
celestar@3451: }
celestar@3451: }
celestar@3451: }
celestar@3451: }
celestar@3451:
celestar@3355: /** Draws wires and, if required, pylons on a given tile
celestar@3355: * @param ti The Tileinfo to draw the tile for
celestar@3355: */
celestar@3355: static void DrawCatenaryRailway(const TileInfo *ti)
celestar@3355: {
celestar@3355: /* Pylons are placed on a tile edge, so we need to take into account
celestar@3355: the track configuration of 2 adjacent tiles. trackconfig[0] stores the
celestar@3355: current tile (home tile) while [1] holds the neighbour */
celestar@3355: TrackBits trackconfig[TS_END];
celestar@3355: bool isflat[TS_END];
celestar@3355: /* Note that ti->tileh has already been adjusted for Foundations */
tron@3636: Slope tileh[TS_END] = { ti->tileh, SLOPE_FLAT };
celestar@3355:
celestar@3355: TLG tlg = GetTLG(ti->tile);
celestar@3355: byte PCPstatus = 0;
celestar@3355: byte OverridePCP = 0;
orudge@3395: byte PPPpreferred[DIAGDIR_END];
orudge@3395: byte PPPallowed[DIAGDIR_END];
celestar@3355: DiagDirection i;
celestar@3355: Track t;
celestar@3355:
celestar@3355: /* Find which rail bits are present, and select the override points.
celestar@3355: We don't draw a pylon:
celestar@3355: 1) INSIDE a tunnel (we wouldn't see it anyway)
celestar@3355: 2) on the "far" end of a bridge head (the one that connects to bridge middle),
celestar@3355: because that one is drawn on the bridge. Exception is for length 0 bridges
celestar@3355: which have no middle tiles */
celestar@3355: trackconfig[TS_HOME] = GetRailTrackBitsUniversal(ti->tile, &OverridePCP);
celestar@3355: /* If a track bit is present that is not in the main direction, the track is level */
celestar@3461: isflat[TS_HOME] = trackconfig[TS_HOME] & (TRACK_BIT_HORZ | TRACK_BIT_VERT);
celestar@3355:
celestar@3451: AdjustTileh(ti->tile, &tileh[TS_HOME]);
celestar@3355:
celestar@3355: for (i = DIAGDIR_NE; i < DIAGDIR_END; i++) {
celestar@3355: TileIndex neighbour = ti->tile + TileOffsByDir(i);
celestar@3355: uint foundation = 0;
celestar@3355: int k;
celestar@3355:
celestar@3355: /* Here's one of the main headaches. GetTileSlope does not correct for possibly
celestar@3355: existing foundataions, so we do have to do that manually later on.*/
celestar@3355: tileh[TS_NEIGHBOUR] = GetTileSlope(neighbour, NULL);
celestar@3355: trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, NULL);
celestar@3452: if (IsTunnelTile(neighbour) && i != GetTunnelDirection(neighbour)) trackconfig[TS_NEIGHBOUR] = 0;
celestar@3461: isflat[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_HORZ | TRACK_BIT_VERT);
celestar@3355:
celestar@3451: PPPpreferred[i] = 0xFF; /* We start with preferring everything (end-of-line in any direction) */
celestar@3451: PPPallowed[i] = AllowedPPPonPCP[i];
celestar@3451:
celestar@3355: /* We cycle through all the existing tracks at a PCP and see what
celestar@3355: PPPs we want to have, or may not have at all */
celestar@3451: for (k = 0; k < NUM_TRACKS_AT_PCP; k++) {
celestar@3355: /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
celestar@3451: if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
celestar@3933: IsBridgeTile(neighbour) &&
celestar@3933: GetBridgeRampDirection(neighbour) == ReverseDiagDir(i)) {
celestar@3933: continue;
celestar@3933: }
celestar@3355:
celestar@3451: /* We check whether the track in question (k) is present in the tile
celestar@3451: (TrackSourceTile) */
celestar@3451: if (HASBIT(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
celestar@3451: /* track found, if track is in the neighbour tile, adjust the number
celestar@3451: of the PCP for preferred/allowed determination*/
celestar@3451: DiagDirection PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
celestar@3451: SETBIT(PCPstatus, i); /* This PCP is in use */
celestar@3451:
celestar@3451: PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
celestar@3451: PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3355: /* Deactivate all PPPs if PCP is not used */
celestar@3355: PPPpreferred[i] *= HASBIT(PCPstatus, i);
celestar@3355: PPPallowed[i] *= HASBIT(PCPstatus, i);
celestar@3355:
celestar@3880: /* A station is always "flat", so adjust the tileh accordingly */
celestar@3880: if (IsTileType(neighbour, MP_STATION)) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
celestar@3355:
celestar@3355: /* Read the foundataions if they are present, and adjust the tileh */
celestar@3355: if (IsTileType(neighbour, MP_RAILWAY)) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
celestar@3933: if (IsBridgeTile(neighbour)) {
celestar@3368: foundation = GetBridgeFoundation(tileh[TS_NEIGHBOUR], DiagDirToAxis(GetBridgeRampDirection(neighbour)));
celestar@3368: }
celestar@3368:
celestar@3355: if (foundation != 0) {
celestar@3355: if (foundation < 15) {
tron@3636: tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
celestar@3355: } else {
celestar@3355: tileh[TS_NEIGHBOUR] = _inclined_tileh[foundation - 15];
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3451: AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
celestar@3355:
celestar@3355: /* If we have a straight (and level) track, we want a pylon only every 2 tiles
celestar@3355: Delete the PCP if this is the case. */
celestar@3355: /* Level means that the slope is the same, or the track is flat */
celestar@3355: if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
celestar@3355: for (k = 0; k < NUM_IGNORE_GROUPS; k++)
celestar@3451: if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) CLRBIT(PCPstatus, i);
celestar@3355: }
celestar@3355:
celestar@3451: /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
celestar@3355: In that case, we try the any of the allowed ones. if they don't exist either, don't draw
celestar@3451: anything. Note that the preferred PPPs still contain the end-of-line markers.
celestar@3451: Remove those (simply by ANDing with allowed, since these markers are never allowed) */
celestar@3451: if ( (PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
celestar@3355:
celestar@3933: if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile)) {
celestar@3933: Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
celestar@3933: uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile), GetBridgeAxis(ti->tile));
celestar@3933:
celestar@3933: if ((height <= TilePixelHeight(ti->tile) + TILE_HEIGHT) &&
celestar@3933: (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) SETBIT(OverridePCP, i);
celestar@3933: }
celestar@3933:
celestar@3451: if (PPPallowed[i] != 0 && HASBIT(PCPstatus, i) && !HASBIT(OverridePCP, i)) {
celestar@3355: for (k = 0; k < DIR_END; k++) {
celestar@3355: byte temp = PPPorder[i][GetTLG(ti->tile)][k];
celestar@3451: if (HASBIT(PPPallowed[i], temp)) {
celestar@3355: uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
celestar@3355: uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
celestar@3355:
celestar@3355: /* Don't build the pylon if it would be outside the tile */
celestar@3355: if (!HASBIT(OwnedPPPonPCP[i], temp)) {
celestar@3355: /* We have a neighour that will draw it, bail out */
celestar@3355: if (trackconfig[TS_NEIGHBOUR] != 0) break;
celestar@3355: continue; /* No neighbour, go looking for a better position */
celestar@3355: }
celestar@3355:
celestar@3355: AddSortableSpriteToDraw(pylons_normal[temp], x, y, 1, 1, 10,
celestar@3355: GetSlopeZ(ti->x + x_pcp_offsets[i], ti->y + y_pcp_offsets[i]));
celestar@3355: break; /* We already have drawn a pylon, bail out */
celestar@3355: }
celestar@3355: }
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3355: /* Drawing of pylons is finished, now draw the wires */
celestar@3355: for (t = 0; t < TRACK_END; t++) {
celestar@3355: if (HASBIT(trackconfig[TS_HOME], t)) {
celestar@3355:
celestar@3355: byte PCPconfig = HASBIT(PCPstatus, PCPpositions[t][0]) +
celestar@3355: (HASBIT(PCPstatus, PCPpositions[t][1]) << 1);
celestar@3355:
celestar@3355: const SortableSpriteStruct *sss;
celestar@3355: int tileh_selector = !(tileh[TS_HOME] % 3) * tileh[TS_HOME] / 3; /* tileh for the slopes, 0 otherwise */
celestar@3355:
celestar@3933: if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !(_display_opt & DO_TRANS_BUILDINGS)) {
celestar@3933: uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile), GetBridgeAxis(ti->tile));
celestar@3933:
celestar@3933: if (height <= TilePixelHeight(ti->tile) + TILE_HEIGHT) return;
celestar@3933: }
celestar@3355:
celestar@3355: assert(PCPconfig != 0); /* We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that) */
tron@3636: assert(!IsSteepSlope(tileh[TS_HOME]));
celestar@3355: sss = &CatenarySpriteData[Wires[tileh_selector][t][PCPconfig]];
celestar@3355:
celestar@3355: AddSortableSpriteToDraw( sss->image, ti->x + sss->x_offset, ti->y + sss->y_offset,
tron@3645: sss->x_size, sss->y_size, sss->z_size, GetSlopeZ(ti->x + min(sss->x_offset, TILE_SIZE - 1), ti->y + min(sss->y_offset, TILE_SIZE - 1)) + sss->z_offset);
celestar@3355: }
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3355: static void DrawCatenaryOnBridge(const TileInfo *ti)
celestar@3355: {
celestar@3445: TileIndex end = GetSouthernBridgeEnd(ti->tile);
celestar@3445: TileIndex start = GetOtherBridgeEnd(end);
celestar@3445:
celestar@3445: uint length = GetBridgeLength(start, end);
celestar@3355: uint num = DistanceMax(ti->tile, start);
celestar@3445:
celestar@3355: const SortableSpriteStruct *sss;
celestar@3355: Axis axis = GetBridgeAxis(ti->tile);
celestar@3355: TLG tlg = GetTLG(ti->tile);
celestar@3355:
celestar@3355: CatenarySprite offset = axis == AXIS_X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH;
celestar@3355:
celestar@3355: if ((length % 2) && num == length) {
celestar@3445: /* Draw the "short" wire on the southern end of the bridge
celestar@3445: * only needed if the length of the bridge is odd */
celestar@3355: sss = &CatenarySpriteData[WIRE_X_FLAT_BOTH + offset];
celestar@3355: } else {
celestar@3445: /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
celestar@3355: sss = &CatenarySpriteData[WIRE_X_FLAT_SW + (num % 2) + offset];
celestar@3355: }
celestar@3355:
celestar@3445: AddSortableSpriteToDraw( sss->image, ti->x + sss->x_offset, ti->y + sss->y_offset,
celestar@3933: sss->x_size, sss->y_size, sss->z_size, GetBridgeHeight(end, axis) + sss->z_offset);
celestar@3445:
celestar@3445: /* Finished with wires, draw pylons */
celestar@3445: /* every other tile needs a pylon on the northern end */
celestar@3355: if (num % 2) {
celestar@3355: if (axis == AXIS_X) {
celestar@3933: AddSortableSpriteToDraw( pylons_bridge[0 + HASBIT(tlg, 0)], ti->x, ti->y + 4 + 8 * HASBIT(tlg, 0), 1, 1, 10, GetBridgeHeight(end, axis));
celestar@3355: } else {
celestar@3933: AddSortableSpriteToDraw( pylons_bridge[2 + HASBIT(tlg, 1)], ti->x + 4 + 8 * HASBIT(tlg, 1), ti->y, 1, 1, 10, GetBridgeHeight(end, axis));
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3445: /* need a pylon on the southern end of the bridge */
celestar@3445: if (DistanceMax(ti->tile, start) == length) {
celestar@3355: if (axis == AXIS_X) {
celestar@3933: AddSortableSpriteToDraw( pylons_bridge[0 + HASBIT(tlg, 0)], ti->x + 16, ti->y + 4 + 8 * HASBIT(tlg, 0), 1, 1, 10, GetBridgeHeight(end, axis));
celestar@3355: } else {
celestar@3933: AddSortableSpriteToDraw( pylons_bridge[2 + HASBIT(tlg, 1)], ti->x + 4 + 8 * HASBIT(tlg, 1), ti->y + 16, 1, 1, 10, GetBridgeHeight(end, axis));
celestar@3355: }
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3355: void DrawCatenary(const TileInfo *ti)
celestar@3355: {
celestar@3933: if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile)) {
celestar@3933: TileIndex head = GetNorthernBridgeEnd(ti->tile);
celestar@3933:
celestar@3933: if (GetBridgeTransportType(head) == TRANSPORT_RAIL && GetRailType(head) == RAILTYPE_ELECTRIC) {
celestar@3933: DrawCatenaryOnBridge(ti);
celestar@3933: }
celestar@3933: }
celestar@3355: switch (GetTileType(ti->tile)) {
celestar@3355: case MP_RAILWAY:
rubidium@3792: if (GetRailTileType(ti->tile) == RAIL_TILE_DEPOT_WAYPOINT && GetRailTileSubtype(ti->tile) == RAIL_SUBTYPE_DEPOT) {
celestar@3355: const SortableSpriteStruct *sss = &CatenarySpriteData[WIRE_DEPOT_SW + ReverseDiagDir(GetRailDepotDirection(ti->tile))];
celestar@3355: AddSortableSpriteToDraw( sss->image, ti->x + sss->x_offset, ti->y + sss->y_offset,
celestar@3355: sss->x_size, sss->y_size, sss->z_size, GetSlopeZ(ti->x, ti->y) + sss->z_offset);
celestar@3355: return;
celestar@3355: }
celestar@3355: /* Fall through */
celestar@3355: case MP_TUNNELBRIDGE:
celestar@3933: case MP_STREET:
celestar@3933: case MP_STATION:
celestar@3355: DrawCatenaryRailway(ti);
celestar@3355: break;
celestar@3355: default:
celestar@3355: break;
celestar@3355: }
celestar@3355: }
celestar@3355: