celestar@3355: /* $Id$ */
celestar@3355: /** @file elrail.c
rubidium@4549: * This file deals with displaying wires and pylons for electric railways.
rubidium@4549: *
Basics
rubidium@4549: *
rubidium@4549: * Tile Types
rubidium@4549: *
rubidium@4549: * We have two different types of tiles in the drawing code:
rubidium@4549: * Normal Railway Tiles (NRTs) which can have more than one track on it, and
rubidium@4549: * Special Railways tiles (SRTs) which have only one track (like crossings, depots
rubidium@4549: * stations, etc).
rubidium@4549: *
rubidium@4549: * Location Categories
rubidium@4549: *
rubidium@4549: * All tiles are categorized into three location groups (TLG):
rubidium@4549: * Group 0: Tiles with both an even X coordinate and an even Y coordinate
rubidium@4549: * Group 1: Tiles with an even X and an odd Y coordinate
rubidium@4549: * Group 2: Tiles with an odd X and an even Y coordinate
rubidium@4549: * Group 3: Tiles with both an odd X and Y coordnate.
rubidium@4549: *
rubidium@4549: * Pylon Points
rubidium@4549: * Control Points
rubidium@4549: * A Pylon Control Point (PCP) is a position where a wire (or rather two)
rubidium@4549: * is mounted onto a pylon.
rubidium@4549: * Each NRT does contain 4 PCPs which are bitmapped to a byte
rubidium@4549: * variable and are represented by the DiagDirection enum
rubidium@4549: *
rubidium@4549: * Each track ends on two PCPs and thus requires one pylon on each end. However,
rubidium@4549: * there is one exception: Straight-and-level tracks only have one pylon every
rubidium@4549: * other tile.
rubidium@4549: *
rubidium@4549: * Now on each edge there are two PCPs: One from each adjacent tile. Both PCPs
rubidium@4549: * are merged using an OR operation (i. e. if one tile needs a PCP at the postion
rubidium@4549: * in question, both tiles get it).
rubidium@4549: *
rubidium@4549: * Position Points
rubidium@4549: * A Pylon Position Point (PPP) is a position where a pylon is located on the
rubidium@4549: * ground. Each PCP owns 8 in (45 degree steps) PPPs that are located around
rubidium@4549: * it. PPPs are represented using the Direction enum. Each track bit has PPPs
rubidium@4549: * that are impossible (because the pylon would be situated on the track) and
rubidium@4549: * some that are preferred (because the pylon would be rectangular to the track).
rubidium@4549: *
rubidium@4549: *
rubidium@4549: *
rubidium@4549: *
rubidium@4549: */
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,
rubidium@4549: * returns track bits under the bridge
rubidium@4549: */
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;
tron@4077:
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@4158: return AxisToTrackBits(DiagDirToAxis(GetTunnelDirection(t)));
celestar@3355: } else {
celestar@3355: if (GetRailType(t) != RAILTYPE_ELECTRIC) return 0;
tron@3977: if (IsBridgeMiddle(t)) {
tron@3977: if (IsTransportUnderBridge(t) &&
tron@3977: GetTransportTypeUnderBridge(t) == TRANSPORT_RAIL) {
tron@3977: return GetRailBitsUnderBridge(t);
tron@3977: } else {
tron@3977: return 0;
tron@3977: }
tron@3977: } else {
tron@3977: if (override != NULL && DistanceMax(t, GetOtherBridgeEnd(t)) > 1) *override = 1 << GetBridgeRampDirection(t);
tron@3977:
tron@4158: return AxisToTrackBits(DiagDirToAxis(GetBridgeRampDirection(t)));
celestar@3355: }
celestar@3355: }
tron@4077:
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);
tron@4077:
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));
tron@4077:
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.
rubidium@4549: * @param tile The tile to analyse
rubidium@4549: * @param *tileh the tileh
rubidium@4549: */
belugas@4171: static void AdjustTileh(TileIndex tile, Slope *tileh)
celestar@3451: {
tron@4077: if (IsTileType(tile, MP_TUNNELBRIDGE)) {
tron@4077: if (IsTunnel(tile)) {
tron@3636: *tileh = SLOPE_FLAT;
celestar@3451: } else {
tron@4077: if (IsBridgeRamp(tile)) {
tron@4077: if (*tileh != SLOPE_FLAT) {
tron@4077: *tileh = SLOPE_FLAT;
tron@4077: } else {
tron@4077: switch (GetBridgeRampDirection(tile)) {
tron@4077: case DIAGDIR_NE: *tileh = SLOPE_NE; break;
tron@4077: case DIAGDIR_SE: *tileh = SLOPE_SE; break;
tron@4077: case DIAGDIR_SW: *tileh = SLOPE_SW; break;
tron@4077: case DIAGDIR_NW: *tileh = SLOPE_NW; break;
tron@4077: default: break;
tron@4077: }
tron@4077: }
celestar@3451: }
celestar@3451: }
celestar@3451: }
celestar@3451: }
celestar@3451:
celestar@3355: /** Draws wires and, if required, pylons on a given tile
rubidium@4549: * @param ti The Tileinfo to draw the tile for
rubidium@4549: */
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
rubidium@4549: * the track configuration of 2 adjacent tiles. trackconfig[0] stores the
rubidium@4549: * 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.
rubidium@4549: * We don't draw a pylon:
rubidium@4549: * 1) INSIDE a tunnel (we wouldn't see it anyway)
rubidium@4549: * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
rubidium@4549: * because that one is drawn on the bridge. Exception is for length 0 bridges
rubidium@4549: * 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++) {
Darkvater@4559: TileIndex neighbour = ti->tile + TileOffsByDiagDir(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
rubidium@4549: * 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
rubidium@4549: * 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 &&
tron@3977: IsBridgeTile(neighbour) && IsBridgeRamp(neighbour) &&
tron@4000: GetBridgeRampDirection(neighbour) == ReverseDiagDir(i)) {
tron@4000: continue;
tron@4000: }
celestar@3355:
celestar@3451: /* We check whether the track in question (k) is present in the tile
rubidium@4549: * (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
rubidium@4549: * 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 */
glx@4448: if (IsTileType(neighbour, MP_RAILWAY) && GetRailType(neighbour) == RAILTYPE_ELECTRIC) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
tron@3977: if (IsBridgeTile(neighbour) && IsBridgeRamp(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
rubidium@4549: * 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.
rubidium@4549: * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
rubidium@4549: * anything. Note that the preferred PPPs still contain the end-of-line markers.
rubidium@4549: * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
tron@4077: if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
celestar@3355:
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];
tron@4077:
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:
tron@4480: /* Don't draw a wire under a low bridge */
tron@4480: if (IsBridgeTile(ti->tile) &&
tron@4480: IsBridgeMiddle(ti->tile) &&
tron@4480: !(_display_opt & DO_TRANS_BUILDINGS) &&
tron@4480: GetBridgeHeight(ti->tile) <= TilePixelHeight(ti->tile) + TILE_HEIGHT) {
tron@4480: return;
tron@4480: }
tron@4480:
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@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);
tron@4159: uint height;
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:
tron@4159: height = GetBridgeHeight(ti->tile);
tron@4159:
celestar@3445: AddSortableSpriteToDraw( sss->image, ti->x + sss->x_offset, ti->y + sss->y_offset,
tron@4159: sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset
tron@4159: );
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) {
tron@4159: AddSortableSpriteToDraw(pylons_bridge[0 + HASBIT(tlg, 0)], ti->x, ti->y + 4 + 8 * HASBIT(tlg, 0), 1, 1, 10, height);
celestar@3355: } else {
tron@4159: AddSortableSpriteToDraw(pylons_bridge[2 + HASBIT(tlg, 1)], ti->x + 4 + 8 * HASBIT(tlg, 1), ti->y, 1, 1, 10, height);
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) {
tron@4159: AddSortableSpriteToDraw(pylons_bridge[0 + HASBIT(tlg, 0)], ti->x + 16, ti->y + 4 + 8 * HASBIT(tlg, 0), 1, 1, 10, height);
celestar@3355: } else {
tron@4159: AddSortableSpriteToDraw(pylons_bridge[2 + HASBIT(tlg, 1)], ti->x + 4 + 8 * HASBIT(tlg, 1), ti->y + 16, 1, 1, 10, height);
celestar@3355: }
celestar@3355: }
celestar@3355: }
celestar@3355:
celestar@3355: void DrawCatenary(const TileInfo *ti)
celestar@3355: {
celestar@3355: switch (GetTileType(ti->tile)) {
celestar@3355: case MP_RAILWAY:
tron@4182: if (IsRailDepot(ti->tile)) {
tron@4470: const SortableSpriteStruct* sss = &CatenarySpriteData_Depot[GetRailDepotDirection(ti->tile)];
tron@4470:
tron@4470: AddSortableSpriteToDraw(
tron@4470: sss->image, ti->x + sss->x_offset, ti->y + sss->y_offset,
tron@4470: sss->x_size, sss->y_size, sss->z_size,
tron@4470: GetTileMaxZ(ti->tile) + sss->z_offset
tron@4470: );
celestar@3355: return;
celestar@3355: }
tron@3995: break;
tron@3995:
celestar@3355: case MP_TUNNELBRIDGE:
tron@3995: if (IsBridge(ti->tile) && IsBridgeMiddle(ti->tile) && GetRailTypeOnBridge(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenaryOnBridge(ti);
celestar@3355: break;
tron@3995:
tron@3995: case MP_STREET: break;
tron@3995: case MP_STATION: break;
tron@3995:
tron@3995: default: return;
celestar@3355: }
tron@3995: DrawCatenaryRailway(ti);
celestar@3355: }