OpenTTD
elrail.cpp
Go to the documentation of this file.
1 /* $Id: elrail.cpp 27812 2017-03-20 17:57:24Z peter1138 $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
57 #include "stdafx.h"
58 #include "station_map.h"
59 #include "viewport_func.h"
60 #include "train.h"
61 #include "rail_gui.h"
62 #include "tunnelbridge_map.h"
63 #include "tunnelbridge.h"
64 #include "elrail_func.h"
65 #include "company_base.h"
66 #include "newgrf_railtype.h"
67 
68 #include "table/elrail_data.h"
69 
70 #include "safeguards.h"
71 
77 static inline TLG GetTLG(TileIndex t)
78 {
79  return (TLG)((HasBit(TileX(t), 0) << 1) + HasBit(TileY(t), 0));
80 }
81 
89 {
90  switch (GetTileType(t)) {
91  case MP_RAILWAY:
93  switch (GetRailTileType(t)) {
95  return GetTrackBits(t);
96  default:
97  return TRACK_BIT_NONE;
98  }
99  break;
100 
101  case MP_TUNNELBRIDGE:
103  if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
104  if (override != NULL && (IsTunnel(t) || GetTunnelBridgeLength(t, GetOtherBridgeEnd(t)) > 0)) {
105  *override = 1 << GetTunnelBridgeDirection(t);
106  }
108 
109  case MP_ROAD:
110  if (!IsLevelCrossing(t)) return TRACK_BIT_NONE;
111  if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
112  return GetCrossingRailBits(t);
113 
114  case MP_STATION:
115  if (!HasStationRail(t)) return TRACK_BIT_NONE;
116  if (!HasRailCatenary(GetRailType(t))) return TRACK_BIT_NONE;
118 
119  default:
120  return TRACK_BIT_NONE;
121  }
122 }
123 
128 {
129  if (!IsPlainRailTile(t)) return tracks;
130 
131  TrackdirBits neighbour_tdb = TRACKDIR_BIT_NONE;
132  for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
133  /* If the neighbour tile is either not electrified or has no tracks that can be reached
134  * from this tile, mark all trackdirs that can be reached from the neighbour tile
135  * as needing no catenary. We make an exception for blocked station tiles with a matching
136  * axis that still display wires to preserve visual continuity. */
137  TileIndex next_tile = TileAddByDiagDir(t, d);
138  RailType rt = GetTileRailType(next_tile);
139  if (rt == INVALID_RAILTYPE || !HasRailCatenary(rt) ||
141  (!HasStationTileRail(next_tile) || GetRailStationAxis(next_tile) != DiagDirToAxis(d) || !CanStationTileHaveWires(next_tile)))) {
142  neighbour_tdb |= DiagdirReachesTrackdirs(ReverseDiagDir(d));
143  }
144  }
145 
146  /* If the tracks from either a diagonal crossing or don't overlap, both
147  * trackdirs have to be marked to mask the corresponding track bit. Else
148  * one marked trackdir is enough the mask the track bit. */
149  TrackBits mask;
150  if (tracks == TRACK_BIT_CROSS || !TracksOverlap(tracks)) {
151  /* If the tracks form either a diagonal crossing or don't overlap, both
152  * trackdirs have to be marked to mask the corresponding track bit. */
153  mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
154  /* If that results in no masked tracks and it is not a diagonal crossing,
155  * require only one marked trackdir to mask. */
156  if (tracks != TRACK_BIT_CROSS && (mask & TRACK_BIT_MASK) == TRACK_BIT_MASK) mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
157  } else {
158  /* Require only one marked trackdir to mask the track. */
159  mask = ~TrackdirBitsToTrackBits(neighbour_tdb);
160  /* If that results in an empty set, require both trackdirs for diagonal track. */
161  if ((tracks & mask) == TRACK_BIT_NONE) {
162  if ((neighbour_tdb & TRACKDIR_BIT_X_NE) == 0 || (neighbour_tdb & TRACKDIR_BIT_X_SW) == 0) mask |= TRACK_BIT_X;
163  if ((neighbour_tdb & TRACKDIR_BIT_Y_NW) == 0 || (neighbour_tdb & TRACKDIR_BIT_Y_SE) == 0) mask |= TRACK_BIT_Y;
164  /* If that still is not enough, require both trackdirs for any track. */
165  if ((tracks & mask) == TRACK_BIT_NONE) mask = ~(TrackBits)((neighbour_tdb & (neighbour_tdb >> 8)) & TRACK_BIT_MASK);
166  }
167  }
168 
169  /* Mask the tracks only if at least one track bit would remain. */
170  return (tracks & mask) != TRACK_BIT_NONE ? tracks & mask : tracks;
171 }
172 
176 static inline SpriteID GetWireBase(TileIndex tile, TileContext context = TCX_NORMAL)
177 {
178  const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
179  SpriteID wires = GetCustomRailSprite(rti, tile, RTSG_WIRES, context);
180  return wires == 0 ? SPR_WIRE_BASE : wires;
181 }
182 
186 static inline SpriteID GetPylonBase(TileIndex tile, TileContext context = TCX_NORMAL)
187 {
188  const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
189  SpriteID pylons = GetCustomRailSprite(rti, tile, RTSG_PYLONS, context);
190  return pylons == 0 ? SPR_PYLON_BASE : pylons;
191 }
192 
198 static void AdjustTileh(TileIndex tile, Slope *tileh)
199 {
200  if (IsTileType(tile, MP_TUNNELBRIDGE)) {
201  if (IsTunnel(tile)) {
202  *tileh = SLOPE_STEEP; // XXX - Hack to make tunnel entrances to always have a pylon
203  } else if (*tileh != SLOPE_FLAT) {
204  *tileh = SLOPE_FLAT;
205  } else {
206  *tileh = InclinedSlope(GetTunnelBridgeDirection(tile));
207  }
208  }
209 }
210 
218 static int GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
219 {
220  /* The elevation of the "pylon"-sprite should be the elevation at the PCP.
221  * PCPs are always on a tile edge.
222  *
223  * This position can be outside of the tile, i.e. ?_pcp_offset == TILE_SIZE > TILE_SIZE - 1.
224  * So we have to move it inside the tile, because if the neighboured tile has a foundation,
225  * that does not smoothly connect to the current tile, we will get a wrong elevation from GetSlopePixelZ().
226  *
227  * When we move the position inside the tile, we will get a wrong elevation if we have a slope.
228  * To catch all cases we round the Z position to the next (TILE_HEIGHT / 2).
229  * This will return the correct elevation for slopes and will also detect non-continuous elevation on edges.
230  *
231  * Also note that the result of GetSlopePixelZ() is very special on bridge-ramps.
232  */
233 
234  int z = GetSlopePixelZ(TileX(tile) * TILE_SIZE + min(x_pcp_offsets[PCPpos], TILE_SIZE - 1), TileY(tile) * TILE_SIZE + min(y_pcp_offsets[PCPpos], TILE_SIZE - 1));
235  return (z + 2) & ~3; // this means z = (z + TILE_HEIGHT / 4) / (TILE_HEIGHT / 2) * (TILE_HEIGHT / 2);
236 }
237 
246 {
247  /* xmin, ymin, xmax + 1, ymax + 1 of BB */
248  static const int _tunnel_wire_BB[4][4] = {
249  { 0, 1, 16, 15 }, // NE
250  { 1, 0, 15, 16 }, // SE
251  { 0, 1, 16, 15 }, // SW
252  { 1, 0, 15, 16 }, // NW
253  };
254 
256 
257  SpriteID wire_base = GetWireBase(ti->tile);
258 
259  const SortableSpriteStruct *sss = &RailCatenarySpriteData_Tunnel[dir];
260  const int *BB_data = _tunnel_wire_BB[dir];
262  wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
263  BB_data[2] - sss->x_offset, BB_data[3] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset + 1,
264  GetTilePixelZ(ti->tile) + sss->z_offset,
266  BB_data[0] - sss->x_offset, BB_data[1] - sss->y_offset, BB_Z_SEPARATOR - sss->z_offset
267  );
268 }
269 
274 static void DrawRailCatenaryRailway(const TileInfo *ti)
275 {
276  /* Pylons are placed on a tile edge, so we need to take into account
277  * the track configuration of 2 adjacent tiles. trackconfig[0] stores the
278  * current tile (home tile) while [1] holds the neighbour */
279  TrackBits trackconfig[TS_END];
280  TrackBits wireconfig[TS_END];
281  bool isflat[TS_END];
282  /* Note that ti->tileh has already been adjusted for Foundations */
283  Slope tileh[TS_END] = { ti->tileh, SLOPE_FLAT };
284 
285  /* Half tile slopes coincide only with horizontal/vertical track.
286  * Faking a flat slope results in the correct sprites on positions. */
287  Corner halftile_corner = CORNER_INVALID;
288  if (IsHalftileSlope(tileh[TS_HOME])) {
289  halftile_corner = GetHalftileSlopeCorner(tileh[TS_HOME]);
290  tileh[TS_HOME] = SLOPE_FLAT;
291  }
292 
293  TLG tlg = GetTLG(ti->tile);
294  byte PCPstatus = 0;
295  byte OverridePCP = 0;
296  byte PPPpreferred[DIAGDIR_END];
297  byte PPPallowed[DIAGDIR_END];
298 
299  /* Find which rail bits are present, and select the override points.
300  * We don't draw a pylon:
301  * 1) INSIDE a tunnel (we wouldn't see it anyway)
302  * 2) on the "far" end of a bridge head (the one that connects to bridge middle),
303  * because that one is drawn on the bridge. Exception is for length 0 bridges
304  * which have no middle tiles */
305  trackconfig[TS_HOME] = GetRailTrackBitsUniversal(ti->tile, &OverridePCP);
306  wireconfig[TS_HOME] = MaskWireBits(ti->tile, trackconfig[TS_HOME]);
307  /* If a track bit is present that is not in the main direction, the track is level */
308  isflat[TS_HOME] = ((trackconfig[TS_HOME] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
309 
310  AdjustTileh(ti->tile, &tileh[TS_HOME]);
311 
312  SpriteID pylon_normal = GetPylonBase(ti->tile);
313  SpriteID pylon_halftile = (halftile_corner != CORNER_INVALID) ? GetPylonBase(ti->tile, TCX_UPPER_HALFTILE) : pylon_normal;
314 
315  for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
316  static const uint edge_corners[] = {
317  1 << CORNER_N | 1 << CORNER_E, // DIAGDIR_NE
318  1 << CORNER_S | 1 << CORNER_E, // DIAGDIR_SE
319  1 << CORNER_S | 1 << CORNER_W, // DIAGDIR_SW
320  1 << CORNER_N | 1 << CORNER_W, // DIAGDIR_NW
321  };
322  SpriteID pylon_base = (halftile_corner != CORNER_INVALID && HasBit(edge_corners[i], halftile_corner)) ? pylon_halftile : pylon_normal;
323  TileIndex neighbour = ti->tile + TileOffsByDiagDir(i);
324  int elevation = GetPCPElevation(ti->tile, i);
325 
326  /* Here's one of the main headaches. GetTileSlope does not correct for possibly
327  * existing foundataions, so we do have to do that manually later on.*/
328  tileh[TS_NEIGHBOUR] = GetTileSlope(neighbour);
329  trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, NULL);
330  wireconfig[TS_NEIGHBOUR] = MaskWireBits(neighbour, trackconfig[TS_NEIGHBOUR]);
331  if (IsTunnelTile(neighbour) && i != GetTunnelBridgeDirection(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
332 
333  /* Ignore station tiles that allow neither wires nor pylons. */
334  if (IsRailStationTile(neighbour) && !CanStationTileHavePylons(neighbour) && !CanStationTileHaveWires(neighbour)) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
335 
336  /* If the neighboured tile does not smoothly connect to the current tile (because of a foundation),
337  * we have to draw all pillars on the current tile. */
338  if (elevation != GetPCPElevation(neighbour, ReverseDiagDir(i))) wireconfig[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] = TRACK_BIT_NONE;
339 
340  isflat[TS_NEIGHBOUR] = ((trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_HORZ | TRACK_BIT_VERT)) != 0);
341 
342  PPPpreferred[i] = 0xFF; // We start with preferring everything (end-of-line in any direction)
343  PPPallowed[i] = AllowedPPPonPCP[i];
344 
345  /* We cycle through all the existing tracks at a PCP and see what
346  * PPPs we want to have, or may not have at all */
347  for (uint k = 0; k < NUM_TRACKS_AT_PCP; k++) {
348  /* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
349  if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
350  IsBridgeTile(neighbour) &&
351  GetTunnelBridgeDirection(neighbour) == ReverseDiagDir(i)) {
352  continue;
353  }
354 
355  /* We check whether the track in question (k) is present in the tile
356  * (TrackSourceTile) */
357  DiagDirection PCPpos = i;
358  if (HasBit(wireconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
359  /* track found, if track is in the neighbour tile, adjust the number
360  * of the PCP for preferred/allowed determination*/
361  PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
362  SetBit(PCPstatus, i); // This PCP is in use
363  PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
364  }
365 
366  if (HasBit(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
367  PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
368  }
369  }
370 
371  /* Deactivate all PPPs if PCP is not used */
372  if (!HasBit(PCPstatus, i)) {
373  PPPpreferred[i] = 0;
374  PPPallowed[i] = 0;
375  }
376 
377  Foundation foundation = FOUNDATION_NONE;
378 
379  /* Station and road crossings are always "flat", so adjust the tileh accordingly */
380  if (IsTileType(neighbour, MP_STATION) || IsTileType(neighbour, MP_ROAD)) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
381 
382  /* Read the foundations if they are present, and adjust the tileh */
383  if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE && IsTileType(neighbour, MP_RAILWAY) && HasRailCatenary(GetRailType(neighbour))) foundation = GetRailFoundation(tileh[TS_NEIGHBOUR], trackconfig[TS_NEIGHBOUR]);
384  if (IsBridgeTile(neighbour)) {
385  foundation = GetBridgeFoundation(tileh[TS_NEIGHBOUR], DiagDirToAxis(GetTunnelBridgeDirection(neighbour)));
386  }
387 
388  ApplyFoundationToSlope(foundation, &tileh[TS_NEIGHBOUR]);
389 
390  /* Half tile slopes coincide only with horizontal/vertical track.
391  * Faking a flat slope results in the correct sprites on positions. */
392  if (IsHalftileSlope(tileh[TS_NEIGHBOUR])) tileh[TS_NEIGHBOUR] = SLOPE_FLAT;
393 
394  AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
395 
396  /* If we have a straight (and level) track, we want a pylon only every 2 tiles
397  * Delete the PCP if this is the case.
398  * Level means that the slope is the same, or the track is flat */
399  if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
400  for (uint k = 0; k < NUM_IGNORE_GROUPS; k++) {
401  if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) ClrBit(PCPstatus, i);
402  }
403  }
404 
405  /* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
406  * In that case, we try the any of the allowed ones. if they don't exist either, don't draw
407  * anything. Note that the preferred PPPs still contain the end-of-line markers.
408  * Remove those (simply by ANDing with allowed, since these markers are never allowed) */
409  if ((PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
410 
411  if (IsBridgeAbove(ti->tile)) {
412  Track bridgetrack = GetBridgeAxis(ti->tile) == AXIS_X ? TRACK_X : TRACK_Y;
413  int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
414 
415  if ((height <= GetTileMaxZ(ti->tile) + 1) &&
416  (i == PCPpositions[bridgetrack][0] || i == PCPpositions[bridgetrack][1])) {
417  SetBit(OverridePCP, i);
418  }
419  }
420 
421  if (PPPallowed[i] != 0 && HasBit(PCPstatus, i) && !HasBit(OverridePCP, i) &&
423  for (Direction k = DIR_BEGIN; k < DIR_END; k++) {
424  byte temp = PPPorder[i][GetTLG(ti->tile)][k];
425 
426  if (HasBit(PPPallowed[i], temp)) {
427  uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
428  uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
429 
430  /* Don't build the pylon if it would be outside the tile */
431  if (!HasBit(OwnedPPPonPCP[i], temp)) {
432  /* We have a neighbour that will draw it, bail out */
433  if (trackconfig[TS_NEIGHBOUR] != TRACK_BIT_NONE) break;
434  continue; // No neighbour, go looking for a better position
435  }
436 
437  AddSortableSpriteToDraw(pylon_base + pylon_sprites[temp], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE,
438  elevation, IsTransparencySet(TO_CATENARY), -1, -1);
439 
440  break; // We already have drawn a pylon, bail out
441  }
442  }
443  }
444  }
445 
446  /* The wire above the tunnel is drawn together with the tunnel-roof (see DrawRailCatenaryOnTunnel()) */
447  if (IsTunnelTile(ti->tile)) return;
448 
449  /* Don't draw a wire under a low bridge */
451  int height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
452 
453  if (height <= GetTileMaxZ(ti->tile) + 1) return;
454  }
455 
456  /* Don't draw a wire if the station tile does not want any */
457  if (IsRailStationTile(ti->tile) && !CanStationTileHaveWires(ti->tile)) return;
458 
459  SpriteID wire_normal = GetWireBase(ti->tile);
460  SpriteID wire_halftile = (halftile_corner != CORNER_INVALID) ? GetWireBase(ti->tile, TCX_UPPER_HALFTILE) : wire_normal;
461  Track halftile_track;
462  switch (halftile_corner) {
463  case CORNER_W: halftile_track = TRACK_LEFT; break;
464  case CORNER_S: halftile_track = TRACK_LOWER; break;
465  case CORNER_E: halftile_track = TRACK_RIGHT; break;
466  case CORNER_N: halftile_track = TRACK_UPPER; break;
467  default: halftile_track = INVALID_TRACK; break;
468  }
469 
470  /* Drawing of pylons is finished, now draw the wires */
471  Track t;
472  FOR_EACH_SET_TRACK(t, wireconfig[TS_HOME]) {
473  SpriteID wire_base = (t == halftile_track) ? wire_halftile : wire_normal;
474  byte PCPconfig = HasBit(PCPstatus, PCPpositions[t][0]) +
475  (HasBit(PCPstatus, PCPpositions[t][1]) << 1);
476 
477  const SortableSpriteStruct *sss;
478  int tileh_selector = !(tileh[TS_HOME] % 3) * tileh[TS_HOME] / 3; // tileh for the slopes, 0 otherwise
479 
480  assert(PCPconfig != 0); // We have a pylon on neither end of the wire, that doesn't work (since we have no sprites for that)
481  assert(!IsSteepSlope(tileh[TS_HOME]));
482  sss = &RailCatenarySpriteData[Wires[tileh_selector][t][PCPconfig]];
483 
484  /*
485  * The "wire"-sprite position is inside the tile, i.e. 0 <= sss->?_offset < TILE_SIZE.
486  * Therefore it is safe to use GetSlopePixelZ() for the elevation.
487  * Also note that the result of GetSlopePixelZ() is very special for bridge-ramps.
488  */
489  AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
490  sss->x_size, sss->y_size, sss->z_size, GetSlopePixelZ(ti->x + sss->x_offset, ti->y + sss->y_offset) + sss->z_offset,
492  }
493 }
494 
503 {
505  TileIndex start = GetOtherBridgeEnd(end);
506 
507  uint length = GetTunnelBridgeLength(start, end);
508  uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
509  uint height;
510 
511  const SortableSpriteStruct *sss;
512  Axis axis = GetBridgeAxis(ti->tile);
513  TLG tlg = GetTLG(ti->tile);
514 
515  RailCatenarySprite offset = (RailCatenarySprite)(axis == AXIS_X ? 0 : WIRE_Y_FLAT_BOTH - WIRE_X_FLAT_BOTH);
516 
517  if ((length % 2) && num == length) {
518  /* Draw the "short" wire on the southern end of the bridge
519  * only needed if the length of the bridge is odd */
520  sss = &RailCatenarySpriteData[WIRE_X_FLAT_BOTH + offset];
521  } else {
522  /* Draw "long" wires on all other tiles of the bridge (one pylon every two tiles) */
523  sss = &RailCatenarySpriteData[WIRE_X_FLAT_SW + (num % 2) + offset];
524  }
525 
526  height = GetBridgePixelHeight(end);
527 
528  SpriteID wire_base = GetWireBase(end, TCX_ON_BRIDGE);
529 
530  AddSortableSpriteToDraw(wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
531  sss->x_size, sss->y_size, sss->z_size, height + sss->z_offset,
533  );
534 
535  SpriteID pylon_base = GetPylonBase(end, TCX_ON_BRIDGE);
536 
537  /* Finished with wires, draw pylons
538  * every other tile needs a pylon on the northern end */
539  if (num % 2) {
540  DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_NE : DIAGDIR_NW);
541  Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
542  if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
543  uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
544  uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
545  AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
546  }
547 
548  /* need a pylon on the southern end of the bridge */
549  if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
550  DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE);
551  Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
552  if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
553  uint x = ti->x + x_pcp_offsets[PCPpos] + x_ppp_offsets[PPPpos];
554  uint y = ti->y + y_pcp_offsets[PCPpos] + y_ppp_offsets[PPPpos];
555  AddSortableSpriteToDraw(pylon_base + pylon_sprites[PPPpos], PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, height, IsTransparencySet(TO_CATENARY), -1, -1);
556  }
557 }
558 
564 void DrawRailCatenary(const TileInfo *ti)
565 {
566  switch (GetTileType(ti->tile)) {
567  case MP_RAILWAY:
568  if (IsRailDepot(ti->tile)) {
569  const SortableSpriteStruct *sss = &RailCatenarySpriteData_Depot[GetRailDepotDirection(ti->tile)];
570 
571  SpriteID wire_base = GetWireBase(ti->tile);
572 
573  /* This wire is not visible with the default depot sprites */
575  wire_base + sss->image_offset, PAL_NONE, ti->x + sss->x_offset, ti->y + sss->y_offset,
576  sss->x_size, sss->y_size, sss->z_size,
577  GetTileMaxPixelZ(ti->tile) + sss->z_offset,
579  );
580  return;
581  }
582  break;
583 
584  case MP_TUNNELBRIDGE:
585  case MP_ROAD:
586  case MP_STATION:
587  break;
588 
589  default: return;
590  }
592 }
593 
594 bool SettingsDisableElrail(int32 p1)
595 {
596  Company *c;
597  Train *t;
598  bool disable = (p1 != 0);
599 
600  /* we will now walk through all electric train engines and change their railtypes if it is the wrong one*/
601  const RailType old_railtype = disable ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL;
602  const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC;
603 
604  /* walk through all train engines */
605  Engine *e;
606  FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
607  RailVehicleInfo *rv_info = &e->u.rail;
608  /* if it is an electric rail engine and its railtype is the wrong one */
609  if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) {
610  /* change it to the proper one */
611  rv_info->railtype = new_railtype;
612  }
613  }
614 
615  /* when disabling elrails, make sure that all existing trains can run on
616  * normal rail too */
617  if (disable) {
618  FOR_ALL_TRAINS(t) {
619  if (t->railtype == RAILTYPE_ELECTRIC) {
620  /* this railroad vehicle is now compatible only with elrail,
621  * so add there also normal rail compatibility */
622  t->compatible_railtypes |= RAILTYPES_RAIL;
623  t->railtype = RAILTYPE_RAIL;
625  }
626  }
627  }
628 
629  /* Fix the total power and acceleration for trains */
630  FOR_ALL_TRAINS(t) {
631  /* power and acceleration is cached only for front engines */
632  if (t->IsFrontEngine()) {
634  }
635  }
636 
637  FOR_ALL_COMPANIES(c) c->avail_railtypes = GetCompanyRailtypes(c->index);
638 
639  /* This resets the _last_built_railtype, which will be invalid for electric
640  * rails. It may have unintended consequences if that function is ever
641  * extended, though. */
643  return true;
644 }
TileIndex GetOtherBridgeEnd(TileIndex tile)
Starting at one bridge end finds the other bridge end.
Definition: bridge_map.cpp:61
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:89
static bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:49
uint ApplyFoundationToSlope(Foundation f, Slope *s)
Applies a foundation to a slope.
Definition: landscape.cpp:99
Corner
Enumeration of tile corners.
Definition: slope_type.h:24
Definition of stuff that is very close to a company, like the company struct itself.
NewGRF handling of rail types.
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:296
static TransportType GetTunnelBridgeTransportType(TileIndex t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Tile information, used while rendering the tile.
Definition: tile_cmd.h:44
void DrawRailCatenaryOnBridge(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition: elrail.cpp:502
Maps accessors for stations.
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
Bitmask for the first 6 bits.
Definition: track_type.h:57
static const byte AllowedPPPonPCP[DIAGDIR_END]
Which PPPs are possible at all on a given PCP.
Definition: elrail_data.h:45
static int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.h:287
static bool IsBridgeTile(TileIndex t)
checks if there is a bridge on this tile
Definition: bridge_map.h:35
Base for the train class.
static const byte DisallowedPPPofTrackAtPCP[TRACK_END][DIAGDIR_END]
Which pylons can definitely NOT be built.
Definition: elrail_data.h:199
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
bool CanStationTileHaveWires(TileIndex tile)
Check if a rail station tile shall have wires when electrified.
Track
These are used to specify a single track.
Definition: track_type.h:21
static TrackBits GetCrossingRailBits(TileIndex tile)
Get the rail track bits of a level crossing.
Definition: road_map.h:338
A tile with road (or tram tracks)
Definition: tile_type.h:45
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:47
static const uint BB_Z_SEPARATOR
Separates the bridge/tunnel from the things under/above it.
Definition: viewport_type.h:75
static TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track bits incidating with that diagdir. ...
Definition: track_func.h:512
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:372
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
static SpriteID GetPylonBase(TileIndex tile, TileContext context=TCX_NORMAL)
Get the base pylon sprite to use.
Definition: elrail.cpp:186
static void DrawRailCatenaryRailway(const TileInfo *ti)
Draws wires and, if required, pylons on a given tile.
Definition: elrail.cpp:274
Northeast, upper right on your monitor.
static const byte PreferredPPPofTrackAtPCP[TRACK_END][DIAGDIR_END]
Preferred points of each trackbit.
Definition: elrail_data.h:81
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:38
Track in the left corner of the tile (west)
Definition: track_type.h:27
int GetBridgeHeight(TileIndex t)
Get the height (&#39;z&#39;) of a bridge.
Definition: bridge_map.cpp:72
static Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:150
A railway.
Definition: tile_type.h:44
static Track GetRailStationTrack(TileIndex t)
Get the rail track of a rail station tile.
Definition: station_map.h:350
Standard non-electric rails.
Definition: rail_type.h:31
Nothing special.
static const byte OwnedPPPonPCP[DIAGDIR_END]
Which of the PPPs are inside the tile.
Definition: elrail_data.h:57
static TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction...
Definition: track_func.h:543
Used for iterations.
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:68
a flat tile
Definition: slope_type.h:51
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
Direction
Defines the 8 directions on the map.
Functions/types etc.
Catenary pylons.
Definition: rail.h:45
TileIndex GetNorthernBridgeEnd(TileIndex t)
Finds the northern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:41
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:116
Functions related to (drawing on) viewports.
Normal rail tile with signals.
Definition: rail_map.h:26
Northeast.
static bool HasStationRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:136
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:115
static bool IsBridgeAbove(TileIndex t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
Querying information about stuff on the bridge (via some bridgehead).
Track x-axis, direction north-east.
Definition: track_type.h:108
Non-electrified rails.
Definition: rail_type.h:54
Left and right track.
Definition: track_type.h:51
indicates the slope is steep
Definition: slope_type.h:56
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:45
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
Querying information about the upper part of a tile with halftile foundation.
Foundation
Enumeration for Foundations.
Definition: slope_type.h:95
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a give tiletype.
Definition: tile_map.h:143
TileIndex tile
Tile index.
Definition: tile_cmd.h:48
Track along the y-axis (north-west to south-east)
Definition: track_type.h:24
static int GetPCPElevation(TileIndex tile, DiagDirection PCPpos)
Returns the Z position of a Pylon Control Point.
Definition: elrail.cpp:218
static bool IsRailStationTile(TileIndex t)
Is this tile a station tile and a rail station?
Definition: station_map.h:103
Header file for things common for tunnels and bridges.
void ConsistChanged(ConsistChangeFlags allowed_changes)
Recalculates the cached stuff of a train.
Definition: train_cmd.cpp:129
X-axis track.
Definition: track_type.h:43
void ReinitGuiAfterToggleElrail(bool disable)
Re-initialize rail-build toolbar after toggling support for electric trains.
Definition: rail_gui.cpp:1891
EngineClass engclass
Class of engine for this vehicle.
Definition: engine_type.h:53
Foundation GetBridgeFoundation(Slope tileh, Axis axis)
Get the foundation for a bridge.
static DiagDirection GetRailDepotDirection(TileIndex t)
Returns the direction the depot is facing to.
Definition: rail_map.h:172
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Track in the right corner of the tile (east)
Definition: track_type.h:28
static uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles)
Definition: tunnelbridge.h:26
static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
Finds which Electrified Rail Bits are present on a given tile.
Definition: elrail.cpp:88
Southeast.
Southwest.
static bool HasStationTileRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:147
Definition of base types and functions in a cross-platform compatible way.
static const DiagDirection PCPpositions[TRACK_END][2]
Maps a track bit onto two PCP positions.
Definition: elrail_data.h:65
static const uint BB_HEIGHT_UNDER_BRIDGE
Some values for constructing bounding boxes (BB).
Definition: viewport_type.h:74
A number of safeguards to prevent using unsafe methods.
#define FOR_EACH_SET_TRACK(var, track_bits)
Iterate through each set Track in a TrackBits value.
Definition: track_func.h:29
Track in the lower corner of the tile (south)
Definition: track_type.h:26
static Axis GetRailStationAxis(TileIndex t)
Get the rail direction of a rail station.
Definition: station_map.h:338
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:46
void DrawRailCatenaryOnTunnel(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition: elrail.cpp:245
Used for iterations.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:882
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:328
Northwest.
static bool HasRailCatenary(RailType rt)
Test if a rail type has catenary.
Definition: elrail_func.h:23
DiagDirection
Enumeration for diagonal directions.
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
TileIndex GetSouthernBridgeEnd(TileIndex t)
Finds the southern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:51
static DiagDirection GetTunnelBridgeDirection(TileIndex t)
Get the direction pointing to the other end.
static bool IsPlainRailTile(TileIndex t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition: rail_map.h:61
RailTypes GetCompanyRailtypes(CompanyID company)
Get the rail types the given company can build.
Definition: rail.cpp:257
static SpriteID GetWireBase(TileIndex tile, TileContext context=TCX_NORMAL)
Get the base wire sprite to use.
Definition: elrail.cpp:176
static Axis GetBridgeAxis(TileIndex t)
Get the axis of the bridge that goes over the tile.
Definition: bridge_map.h:68
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:688
catenary
Definition: transparency.h:32
Used to iterate.
Information about a rail vehicle.
Definition: engine_type.h:43
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:509
TLG
Tile Location group.
Definition: elrail_data.h:22
static TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction...
Definition: track_func.h:561
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:88
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:37
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:137
The X axis.
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
X-Y-axis cross.
Definition: track_type.h:49
Transport by train.
static TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:87
static Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition: slope_func.h:258
RailType GetTileRailType(TileIndex tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition: rail.cpp:157
Electric train engine is allowed to run on normal rail. */.
Definition: train.h:32
Flag for an invalid track.
Definition: track_type.h:30
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
static bool IsRailDepot(TileIndex t)
Is this rail tile a rail depot?
Definition: rail_map.h:96
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
static void AdjustTileh(TileIndex tile, Slope *tileh)
Corrects the tileh for certain tile types.
Definition: elrail.cpp:198
Electric rails.
Definition: rail_type.h:32
static TLG GetTLG(TileIndex t)
Get the tile location group of a tile.
Definition: elrail.cpp:77
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:215
Track x-axis, direction south-west.
Definition: track_type.h:115
Upper and lower track.
Definition: track_type.h:50
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static const byte IgnoredPCP[NUM_IGNORE_GROUPS][TLG_END][DIAGDIR_END]
In case we have a straight line, we place pylon only every two tiles, so there are certain tiles whic...
Definition: elrail_data.h:124
RailTypes avail_railtypes
Rail types available to the company.
Definition: company_base.h:70
static int GetBridgePixelHeight(TileIndex tile)
Get the height (&#39;z&#39;) of a bridge in pixels.
Definition: bridge_map.h:84
Track y-axis, direction north-west.
Definition: track_type.h:116
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
RailCatenarySprite
Refers to a certain element of the catenary.
Definition: elrail_data.h:422
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:96
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
A tile of a station.
Definition: tile_type.h:48
void DrawRailCatenary(const TileInfo *ti)
Draws overhead wires and pylons for electric railways.
Definition: elrail.cpp:564
Normal rail tile without signals.
Definition: rail_map.h:25
static TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
Definition: track_func.h:351
bool CanStationTileHavePylons(TileIndex tile)
Check if a rail station tile shall have pylons when electrified.
Catenary wires.
Definition: rail.h:44
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:106
header file for electrified rail specific functions
Functions that have tunnels and bridges in common.
static bool IsTunnelTile(TileIndex t)
Is this a tunnel (entrance)?
Definition: tunnel_map.h:35
Northwest.
Track y-axis, direction south-east.
Definition: track_type.h:109
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
No track build.
Definition: track_type.h:107
Flag for invalid railtype.
Definition: rail_type.h:36
static TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition: track_func.h:318
Stores all the data for overhead wire and pylon drawing.
Track along the x-axis (north-east to south-west)
Definition: track_type.h:23
Used to iterate.
bridges
Definition: transparency.h:30
static Direction ReverseDir(Direction d)
Return the reverse of a direction.
static TrackBits MaskWireBits(TileIndex t, TrackBits tracks)
Masks out track bits when neighbouring tiles are unelectrified.
Definition: elrail.cpp:127
Track in the upper corner of the tile (north)
Definition: track_type.h:25
static bool IsTunnel(TileIndex t)
Is this a tunnel (entrance)?
Definition: tunnel_map.h:24
Valid changes while vehicle is driving, and possibly changing tracks.
Definition: train.h:51
No track.
Definition: track_type.h:42
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:50
Axis
Allow incrementing of DiagDirDiff variables.
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:116
static bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:633
Y-axis track.
Definition: track_type.h:44
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:299
Train vehicle type.
Definition: vehicle_type.h:24
bool SettingsDisableElrail(int32 p1)
_settings_game.disable_elrail callback
Definition: elrail.cpp:594
TileContext
Context for tile accesses.