mirror of https://github.com/OpenTTD/OpenTTD
(svn r4283) -Cleanup: More elrail housekeeping, remove code dublication, more meaningful variable names, simplify control flow..
parent
4ccbf2d807
commit
0c3a0c7ec0
115
elrail.c
115
elrail.c
|
@ -115,9 +115,30 @@ static TrackBits GetRailTrackBitsUniversal(TileIndex t, byte *override)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Corrects the tileh for certain tile types. Returns an effective tileh for the track on the tile.
|
||||||
|
* @param tile The tile to analyse
|
||||||
|
* @param *tileh the tileh
|
||||||
|
*/
|
||||||
|
static void AdjustTileh(TileIndex tile, uint *tileh)
|
||||||
|
{
|
||||||
|
if (IsTunnelTile(tile)) *tileh = 0;
|
||||||
|
if (IsBridgeTile(tile) && IsBridgeRamp(tile)) {
|
||||||
|
if (*tileh != 0) {
|
||||||
|
*tileh = 0;
|
||||||
|
} else {
|
||||||
|
switch (GetBridgeRampDirection(tile)) {
|
||||||
|
case DIAGDIR_NE: *tileh = 12; break;
|
||||||
|
case DIAGDIR_SE: *tileh = 6; break;
|
||||||
|
case DIAGDIR_SW: *tileh = 3; break;
|
||||||
|
case DIAGDIR_NW: *tileh = 9; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Draws wires and, if required, pylons on a given tile
|
/** Draws wires and, if required, pylons on a given tile
|
||||||
* @param ti The Tileinfo to draw the tile for
|
* @param ti The Tileinfo to draw the tile for
|
||||||
* @todo Currently, each pylon is drawn twice (once for each neighbouring tiles use OwnedPPPonPCP for this)
|
|
||||||
*/
|
*/
|
||||||
static void DrawCatenaryRailway(const TileInfo *ti)
|
static void DrawCatenaryRailway(const TileInfo *ti)
|
||||||
{
|
{
|
||||||
|
@ -127,26 +148,16 @@ static void DrawCatenaryRailway(const TileInfo *ti)
|
||||||
TrackBits trackconfig[TS_END];
|
TrackBits trackconfig[TS_END];
|
||||||
bool isflat[TS_END];
|
bool isflat[TS_END];
|
||||||
/* Note that ti->tileh has already been adjusted for Foundations */
|
/* Note that ti->tileh has already been adjusted for Foundations */
|
||||||
uint tileh[TS_END];
|
uint tileh[TS_END] = {ti->tileh, 0};
|
||||||
|
|
||||||
TLG tlg = GetTLG(ti->tile);
|
TLG tlg = GetTLG(ti->tile);
|
||||||
byte PCPstatus = 0;
|
byte PCPstatus = 0;
|
||||||
byte OverridePCP = 0;
|
byte OverridePCP = 0;
|
||||||
byte PPPpreferred[DIAGDIR_END];
|
byte PPPpreferred[DIAGDIR_END];
|
||||||
byte PPPallowed[DIAGDIR_END];
|
byte PPPallowed[DIAGDIR_END];
|
||||||
byte PPPbuffer[DIAGDIR_END];
|
|
||||||
DiagDirection i;
|
DiagDirection i;
|
||||||
Track t;
|
Track t;
|
||||||
|
|
||||||
tileh[0] = ti->tileh;
|
|
||||||
tileh[1] = 0;
|
|
||||||
|
|
||||||
PPPpreferred[0] = PPPpreferred[1] = PPPpreferred[2] = PPPpreferred[3] = 0xFF;
|
|
||||||
PPPallowed[0] = AllowedPPPonPCP[0];
|
|
||||||
PPPallowed[1] = AllowedPPPonPCP[1];
|
|
||||||
PPPallowed[2] = AllowedPPPonPCP[2];
|
|
||||||
PPPallowed[3] = AllowedPPPonPCP[3];
|
|
||||||
|
|
||||||
/* Find which rail bits are present, and select the override points.
|
/* Find which rail bits are present, and select the override points.
|
||||||
We don't draw a pylon:
|
We don't draw a pylon:
|
||||||
1) INSIDE a tunnel (we wouldn't see it anyway)
|
1) INSIDE a tunnel (we wouldn't see it anyway)
|
||||||
|
@ -157,20 +168,7 @@ static void DrawCatenaryRailway(const TileInfo *ti)
|
||||||
/* If a track bit is present that is not in the main direction, the track is level */
|
/* If a track bit is present that is not in the main direction, the track is level */
|
||||||
isflat[TS_HOME] = trackconfig[TS_HOME] & (TRACK_BIT_UPPER | TRACK_BIT_LOWER | TRACK_BIT_LEFT | TRACK_BIT_RIGHT);
|
isflat[TS_HOME] = trackconfig[TS_HOME] & (TRACK_BIT_UPPER | TRACK_BIT_LOWER | TRACK_BIT_LEFT | TRACK_BIT_RIGHT);
|
||||||
|
|
||||||
if (IsTunnelTile(ti->tile)) tileh[TS_HOME] = 0;
|
AdjustTileh(ti->tile, &tileh[TS_HOME]);
|
||||||
if (IsBridgeTile(ti->tile) && IsBridgeRamp(ti->tile)) {
|
|
||||||
if (tileh[TS_HOME] != 0) {
|
|
||||||
tileh[TS_HOME] = 0;
|
|
||||||
} else {
|
|
||||||
switch (GetBridgeRampDirection(ti->tile)) {
|
|
||||||
case DIAGDIR_NE: tileh[TS_HOME] = 12; break;
|
|
||||||
case DIAGDIR_SE: tileh[TS_HOME] = 6; break;
|
|
||||||
case DIAGDIR_SW: tileh[TS_HOME] = 3; break;
|
|
||||||
case DIAGDIR_NW: tileh[TS_HOME] = 9; break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = DIAGDIR_NE; i < DIAGDIR_END; i++) {
|
for (i = DIAGDIR_NE; i < DIAGDIR_END; i++) {
|
||||||
TileIndex neighbour = ti->tile + TileOffsByDir(i);
|
TileIndex neighbour = ti->tile + TileOffsByDir(i);
|
||||||
|
@ -183,21 +181,28 @@ static void DrawCatenaryRailway(const TileInfo *ti)
|
||||||
trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, NULL);
|
trackconfig[TS_NEIGHBOUR] = GetRailTrackBitsUniversal(neighbour, NULL);
|
||||||
isflat[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_UPPER | TRACK_BIT_LOWER | TRACK_BIT_LEFT | TRACK_BIT_RIGHT);
|
isflat[TS_NEIGHBOUR] = trackconfig[TS_NEIGHBOUR] & (TRACK_BIT_UPPER | TRACK_BIT_LOWER | TRACK_BIT_LEFT | TRACK_BIT_RIGHT);
|
||||||
|
|
||||||
|
PPPpreferred[i] = 0xFF; /* We start with preferring everything (end-of-line in any direction) */
|
||||||
|
PPPallowed[i] = AllowedPPPonPCP[i];
|
||||||
|
|
||||||
/* We cycle through all the existing tracks at a PCP and see what
|
/* We cycle through all the existing tracks at a PCP and see what
|
||||||
PPPs we want to have, or may not have at all */
|
PPPs we want to have, or may not have at all */
|
||||||
for (k = 0; k < TRACKS_AT_PCP; k++) {
|
for (k = 0; k < NUM_TRACKS_AT_PCP; k++) {
|
||||||
/* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
|
/* Next to us, we have a bridge head, don't worry about that one, if it shows away from us */
|
||||||
if (
|
if (TrackSourceTile[i][k] == TS_NEIGHBOUR &&
|
||||||
trackorigin[i][k] == TS_NEIGHBOUR &&
|
IsBridgeTile(neighbour) && IsBridgeRamp(neighbour) &&
|
||||||
IsBridgeTile(neighbour) && IsBridgeRamp(neighbour) &&
|
GetBridgeRampDirection(neighbour) == ReverseDiagDir(i)
|
||||||
GetBridgeRampDirection(neighbour) == ReverseDiagDir(i)
|
|
||||||
) continue;
|
) continue;
|
||||||
|
|
||||||
if (HASBIT(trackconfig[trackorigin[i][k]], PPPtracks[i][k])) {
|
/* We check whether the track in question (k) is present in the tile
|
||||||
DiagDirection PCPpos = (trackorigin[i][k] == 0) ? i : ReverseDiagDir(i);
|
(TrackSourceTile) */
|
||||||
PCPstatus |= 1 << i; /* This PCP is in use */
|
if (HASBIT(trackconfig[TrackSourceTile[i][k]], TracksAtPCP[i][k])) {
|
||||||
PPPpreferred[i] &= PreferredPPPofTrackBitAtPCP[PPPtracks[i][k]][PCPpos];
|
/* track found, if track is in the neighbour tile, adjust the number
|
||||||
PPPallowed[i] &= ~DisallowedPPPofTrackBitAtPCP[PPPtracks[i][k]][PCPpos];
|
of the PCP for preferred/allowed determination*/
|
||||||
|
DiagDirection PCPpos = (TrackSourceTile[i][k] == TS_HOME) ? i : ReverseDiagDir(i);
|
||||||
|
SETBIT(PCPstatus, i); /* This PCP is in use */
|
||||||
|
|
||||||
|
PPPpreferred[i] &= PreferredPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
|
||||||
|
PPPallowed[i] &= ~DisallowedPPPofTrackAtPCP[TracksAtPCP[i][k]][PCPpos];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,48 +227,26 @@ static void DrawCatenaryRailway(const TileInfo *ti)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert the real tileh into a pseudo-tileh for the track */
|
AdjustTileh(neighbour, &tileh[TS_NEIGHBOUR]);
|
||||||
if (IsTunnelTile(neighbour)) tileh[TS_NEIGHBOUR] = 0;
|
|
||||||
if (IsBridgeTile(neighbour) && IsBridgeRamp(neighbour)) {
|
|
||||||
if (tileh[TS_NEIGHBOUR] != 0) {
|
|
||||||
tileh[TS_NEIGHBOUR] = 0;
|
|
||||||
} else {
|
|
||||||
switch (GetBridgeRampDirection(neighbour)) {
|
|
||||||
case DIAGDIR_NE: tileh[TS_NEIGHBOUR] = 12; break;
|
|
||||||
case DIAGDIR_SE: tileh[TS_NEIGHBOUR] = 6; break;
|
|
||||||
case DIAGDIR_SW: tileh[TS_NEIGHBOUR] = 3; break;
|
|
||||||
case DIAGDIR_NW: tileh[TS_NEIGHBOUR] = 9; break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If we have a straight (and level) track, we want a pylon only every 2 tiles
|
/* If we have a straight (and level) track, we want a pylon only every 2 tiles
|
||||||
Delete the PCP if this is the case. */
|
Delete the PCP if this is the case. */
|
||||||
/* Level means that the slope is the same, or the track is flat */
|
/* Level means that the slope is the same, or the track is flat */
|
||||||
if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
|
if (tileh[TS_HOME] == tileh[TS_NEIGHBOUR] || (isflat[TS_HOME] && isflat[TS_NEIGHBOUR])) {
|
||||||
for (k = 0; k < NUM_IGNORE_GROUPS; k++)
|
for (k = 0; k < NUM_IGNORE_GROUPS; k++)
|
||||||
if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) PCPstatus &= ~(1 << i);
|
if (PPPpreferred[i] == IgnoredPCP[k][tlg][i]) CLRBIT(PCPstatus, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Now decide where we draw our tiles. First try the preferred PPPs, but they may not exist.
|
/* Now decide where we draw our pylons. First try the preferred PPPs, but they may not exist.
|
||||||
In that case, we try the any of the allowed ones. if they don't exist either, don't draw
|
In that case, we try the any of the allowed ones. if they don't exist either, don't draw
|
||||||
anything */
|
anything. Note that the preferred PPPs still contain the end-of-line markers.
|
||||||
if (PPPpreferred[i] != 0) {
|
Remove those (simply by ANDing with allowed, since these markers are never allowed) */
|
||||||
/* Some of the preferred PPPs (the ones in direct extension of the track bit)
|
if ( (PPPallowed[i] & PPPpreferred[i]) != 0) PPPallowed[i] &= PPPpreferred[i];
|
||||||
have been used as an "end of line" marker. As these are not ALLOWED, this operation
|
|
||||||
cancles them out */
|
|
||||||
PPPbuffer[i] = PPPpreferred[i] & PPPallowed[i];
|
|
||||||
/* We haven't any buffer yet, so try something else. Fixes 90° curves */
|
|
||||||
if (PPPbuffer[i] == 0) PPPbuffer[i] = PPPallowed[i];
|
|
||||||
} else {
|
|
||||||
PPPbuffer[i] = PPPallowed[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (PPPbuffer[i] != 0 && HASBIT(PCPstatus, i) && !HASBIT(OverridePCP, i)) {
|
if (PPPallowed[i] != 0 && HASBIT(PCPstatus, i) && !HASBIT(OverridePCP, i)) {
|
||||||
for (k = 0; k < DIR_END; k++) {
|
for (k = 0; k < DIR_END; k++) {
|
||||||
byte temp = PPPorder[i][GetTLG(ti->tile)][k];
|
byte temp = PPPorder[i][GetTLG(ti->tile)][k];
|
||||||
if (HASBIT(PPPbuffer[i], temp)) {
|
if (HASBIT(PPPallowed[i], temp)) {
|
||||||
uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
|
uint x = ti->x + x_pcp_offsets[i] + x_ppp_offsets[temp];
|
||||||
uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
|
uint y = ti->y + y_pcp_offsets[i] + y_ppp_offsets[temp];
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ typedef enum {
|
||||||
} TileSource;
|
} TileSource;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
TRACKS_AT_PCP = 6
|
NUM_TRACKS_AT_PCP = 6
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Which PPPs are possible at all on a given PCP */
|
/** Which PPPs are possible at all on a given PCP */
|
||||||
|
@ -62,7 +62,7 @@ static const DiagDirection PCPpositions[TRACK_END][2] = {
|
||||||
track, plus the point in extension of the track (to mark end-of-track). PCPs
|
track, plus the point in extension of the track (to mark end-of-track). PCPs
|
||||||
which are not on either end of the track are fully preferred.
|
which are not on either end of the track are fully preferred.
|
||||||
@see PCPpositions */
|
@see PCPpositions */
|
||||||
static byte PreferredPPPofTrackBitAtPCP[TRACK_END][DIAGDIR_END] = {
|
static byte PreferredPPPofTrackAtPCP[TRACK_END][DIAGDIR_END] = {
|
||||||
{ /* X */
|
{ /* X */
|
||||||
1 << DIR_NE | 1 << DIR_SE | 1 << DIR_NW, /* NE */
|
1 << DIR_NE | 1 << DIR_SE | 1 << DIR_NW, /* NE */
|
||||||
PCP_NOT_ON_TRACK, /* SE */
|
PCP_NOT_ON_TRACK, /* SE */
|
||||||
|
@ -178,7 +178,7 @@ static byte IgnoredPCP[NUM_IGNORE_GROUPS][TLG_END][DIAGDIR_END] = {
|
||||||
#undef NO_IGNORE
|
#undef NO_IGNORE
|
||||||
|
|
||||||
/** Which pylons can definately NOT be built */
|
/** Which pylons can definately NOT be built */
|
||||||
static byte DisallowedPPPofTrackBitAtPCP[TRACK_END][DIAGDIR_END] = {
|
static byte DisallowedPPPofTrackAtPCP[TRACK_END][DIAGDIR_END] = {
|
||||||
{1 << DIR_SW | 1 << DIR_NE, 0, 1 << DIR_SW | 1 << DIR_NE, 0 }, /* X */
|
{1 << DIR_SW | 1 << DIR_NE, 0, 1 << DIR_SW | 1 << DIR_NE, 0 }, /* X */
|
||||||
{0, 1 << DIR_NW | 1 << DIR_SE, 0, 1 << DIR_NW | 1 << DIR_SE}, /* Y */
|
{0, 1 << DIR_NW | 1 << DIR_SE, 0, 1 << DIR_NW | 1 << DIR_SE}, /* Y */
|
||||||
{1 << DIR_W | 1 << DIR_E, 0, 0, 1 << DIR_W | 1 << DIR_E }, /* UPPER */
|
{1 << DIR_W | 1 << DIR_E, 0, 0, 1 << DIR_W | 1 << DIR_E }, /* UPPER */
|
||||||
|
@ -188,16 +188,16 @@ static byte DisallowedPPPofTrackBitAtPCP[TRACK_END][DIAGDIR_END] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
/* This array stores which track bits can meet at a tile edge */
|
/* This array stores which track bits can meet at a tile edge */
|
||||||
static const Track PPPtracks[DIAGDIR_END][TRACKS_AT_PCP] = {
|
static const Track TracksAtPCP[DIAGDIR_END][NUM_TRACKS_AT_PCP] = {
|
||||||
{TRACK_X, TRACK_X, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
{TRACK_X, TRACK_X, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
||||||
{TRACK_Y, TRACK_Y, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
{TRACK_Y, TRACK_Y, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
||||||
{TRACK_X, TRACK_X, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
{TRACK_X, TRACK_X, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
||||||
{TRACK_Y, TRACK_Y, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
{TRACK_Y, TRACK_Y, TRACK_UPPER, TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT},
|
||||||
};
|
};
|
||||||
|
|
||||||
/* takes each of the 8 track bits from the array above and
|
/* takes each of the 6 track bits from the array above and
|
||||||
assigns it to the home tile or neighbour tile */
|
assigns it to the home tile or neighbour tile */
|
||||||
static const TileSource trackorigin[DIAGDIR_END][TRACKS_AT_PCP] = {
|
static const TileSource TrackSourceTile[DIAGDIR_END][NUM_TRACKS_AT_PCP] = {
|
||||||
{TS_HOME, TS_NEIGHBOUR, TS_HOME , TS_NEIGHBOUR, TS_NEIGHBOUR, TS_HOME },
|
{TS_HOME, TS_NEIGHBOUR, TS_HOME , TS_NEIGHBOUR, TS_NEIGHBOUR, TS_HOME },
|
||||||
{TS_HOME, TS_NEIGHBOUR, TS_NEIGHBOUR, TS_HOME , TS_NEIGHBOUR, TS_HOME },
|
{TS_HOME, TS_NEIGHBOUR, TS_NEIGHBOUR, TS_HOME , TS_NEIGHBOUR, TS_HOME },
|
||||||
{TS_HOME, TS_NEIGHBOUR, TS_NEIGHBOUR, TS_HOME , TS_HOME , TS_NEIGHBOUR},
|
{TS_HOME, TS_NEIGHBOUR, TS_NEIGHBOUR, TS_HOME , TS_HOME , TS_NEIGHBOUR},
|
||||||
|
|
Loading…
Reference in New Issue