(svn r9988) -Codechange: remove the last direct map accesses, except the ones needed for the savegame saving/loading mechanisms.

This commit is contained in:
rubidium 2007-05-30 13:33:19 +00:00
parent bc41dc4966
commit 70e6102c86
3 changed files with 86 additions and 39 deletions

View File

@ -13,21 +13,21 @@
/* Maps a trackdir to the bit that stores its status in the map arrays, in the /* Maps a trackdir to the bit that stores its status in the map arrays, in the
* direction along with the trackdir */ * direction along with the trackdir */
extern const byte _signal_along_trackdir[] = { extern const byte _signal_along_trackdir[] = {
0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0, 0x8, 0x8, 0x8, 0x2, 0x4, 0x1, 0, 0,
0x40, 0x40, 0x40, 0x10, 0x80, 0x20 0x4, 0x4, 0x4, 0x1, 0x8, 0x2
}; };
/* Maps a trackdir to the bit that stores its status in the map arrays, in the /* Maps a trackdir to the bit that stores its status in the map arrays, in the
* direction against the trackdir */ * direction against the trackdir */
extern const byte _signal_against_trackdir[] = { extern const byte _signal_against_trackdir[] = {
0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0, 0x4, 0x4, 0x4, 0x1, 0x8, 0x2, 0, 0,
0x80, 0x80, 0x80, 0x20, 0x40, 0x10 0x8, 0x8, 0x8, 0x2, 0x4, 0x1
}; };
/* Maps a Track to the bits that store the status of the two signals that can /* Maps a Track to the bits that store the status of the two signals that can
* be present on the given track */ * be present on the given track */
extern const byte _signal_on_track[] = { extern const byte _signal_on_track[] = {
0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30 0xC, 0xC, 0xC, 0x3, 0xC, 0x3
}; };
/* Maps a diagonal direction to the all trackdirs that are connected to any /* Maps a diagonal direction to the all trackdirs that are connected to any

View File

@ -705,8 +705,8 @@ int32 CmdBuildSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (!HasSignals(tile)) { if (!HasSignals(tile)) {
/* there are no signals at all on this tile yet */ /* there are no signals at all on this tile yet */
SetHasSignals(tile, true); SetHasSignals(tile, true);
_m[tile].m2 |= 0xF0; // all signals are on SetSignalStates(tile, 0xF); // all signals are on
_m[tile].m3 &= ~0xF0; // no signals built by default SetPresentSignals(tile, 0); // no signals built by default
SetSignalType(tile, SIGTYPE_NORMAL); SetSignalType(tile, SIGTYPE_NORMAL);
SetSignalVariant(tile, sigvar); SetSignalVariant(tile, sigvar);
} }
@ -714,7 +714,7 @@ int32 CmdBuildSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (p2 == 0) { if (p2 == 0) {
if (!HasSignalOnTrack(tile, track)) { if (!HasSignalOnTrack(tile, track)) {
/* build new signals */ /* build new signals */
_m[tile].m3 |= SignalOnTrack(track); SetPresentSignals(tile, GetPresentSignals(tile) | SignalOnTrack(track));
} else { } else {
if (pre_signal) { if (pre_signal) {
/* cycle between normal -> pre -> exit -> combo -> ... */ /* cycle between normal -> pre -> exit -> combo -> ... */
@ -728,8 +728,7 @@ int32 CmdBuildSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
} else { } else {
/* If CmdBuildManySignals is called with copying signals, just copy the /* If CmdBuildManySignals is called with copying signals, just copy the
* direction of the first signal given as parameter by CmdBuildManySignals */ * direction of the first signal given as parameter by CmdBuildManySignals */
_m[tile].m3 &= ~SignalOnTrack(track); SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | (p2 & SignalOnTrack(track)));
_m[tile].m3 |= p2 & SignalOnTrack(track);
SetSignalVariant(tile, sigvar); SetSignalVariant(tile, sigvar);
} }
@ -784,7 +783,7 @@ static int32 CmdSignalTrackHelper(TileIndex tile, uint32 flags, uint32 p1, uint3
/* copy the signal-style of the first rail-piece if existing */ /* copy the signal-style of the first rail-piece if existing */
if (HasSignals(tile)) { if (HasSignals(tile)) {
signals = _m[tile].m3 & SignalOnTrack(track); signals = GetPresentSignals(tile) & SignalOnTrack(track);
if (signals == 0) signals = SignalOnTrack(track); /* Can this actually occur? */ if (signals == 0) signals = SignalOnTrack(track); /* Can this actually occur? */
/* copy signal/semaphores style (independent of CTRL) */ /* copy signal/semaphores style (independent of CTRL) */
@ -874,11 +873,11 @@ int32 CmdRemoveSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
/* Do it? */ /* Do it? */
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
_m[tile].m3 &= ~SignalOnTrack(track); SetPresentSignals(tile, GetPresentSignals(tile) & ~SignalOnTrack(track));
/* removed last signal from tile? */ /* removed last signal from tile? */
if (GB(_m[tile].m3, 4, 4) == 0) { if (GetPresentSignals(tile) == 0) {
SB(_m[tile].m2, 4, 4, 0); SetSignalStates(tile, 0);
SetHasSignals(tile, false); SetHasSignals(tile, false);
SetSignalVariant(tile, SIG_ELECTRIC); // remove any possible semaphores SetSignalVariant(tile, SIG_ELECTRIC); // remove any possible semaphores
} }
@ -1666,7 +1665,7 @@ static void ChangeSignalStates(SetSignalsData *ssd)
for (i = 0; i != ssd->cur; i++) { for (i = 0; i != ssd->cur; i++) {
TileIndex tile = ssd->tile[i]; TileIndex tile = ssd->tile[i];
byte bit = SignalAgainstTrackdir(ssd->bit[i]); byte bit = SignalAgainstTrackdir(ssd->bit[i]);
uint16 m2 = _m[tile].m2; uint signals = GetSignalStates(tile);
/* presignals don't turn green if there is at least one presignal exit and none are free */ /* presignals don't turn green if there is at least one presignal exit and none are free */
if (IsPresignalEntry(tile)) { if (IsPresignalEntry(tile)) {
@ -1686,10 +1685,10 @@ static void ChangeSignalStates(SetSignalsData *ssd)
if (ssd->stop) { if (ssd->stop) {
make_red: make_red:
/* turn red */ /* turn red */
if ((bit & m2) == 0) continue; if ((bit & signals) == 0) continue;
} else { } else {
/* turn green */ /* turn green */
if ((bit & m2) != 0) continue; if ((bit & signals) != 0) continue;
} }
/* Update signals on the other side of this exit-combo signal; it changed. */ /* Update signals on the other side of this exit-combo signal; it changed. */
@ -1704,7 +1703,7 @@ make_red:
} }
/* it changed, so toggle it */ /* it changed, so toggle it */
_m[tile].m2 = m2 ^ bit; SetSignalStates(tile, signals ^ bit);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
} }
@ -1928,11 +1927,8 @@ static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode, uint
case RAIL_TILE_SIGNALS: { case RAIL_TILE_SIGNALS: {
uint32 ret = GetTrackBits(tile) * 0x101; uint32 ret = GetTrackBits(tile) * 0x101;
byte a; byte a = GetPresentSignals(tile);
uint16 b; uint b = GetSignalStates(tile);
a = _m[tile].m3;
b = _m[tile].m2;
b &= a; b &= a;
@ -1940,13 +1936,13 @@ static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode, uint
* direction), we pretend them to be green. (So if * direction), we pretend them to be green. (So if
* signals are only one way, the other way will * signals are only one way, the other way will
* implicitely become `red' */ * implicitely become `red' */
if ((a & 0xC0) == 0) b |= 0xC0; if ((a & 0xC) == 0) b |= 0xC;
if ((a & 0x30) == 0) b |= 0x30; if ((a & 0x3) == 0) b |= 0x3;
if ((b & 0x80) == 0) ret |= 0x10070000; if ((b & 0x8) == 0) ret |= 0x10070000;
if ((b & 0x40) == 0) ret |= 0x07100000; if ((b & 0x4) == 0) ret |= 0x07100000;
if ((b & 0x20) == 0) ret |= 0x20080000; if ((b & 0x2) == 0) ret |= 0x20080000;
if ((b & 0x10) == 0) ret |= 0x08200000; if ((b & 0x1) == 0) ret |= 0x08200000;
return ret; return ret;
} }

View File

@ -256,11 +256,6 @@ static inline void SetSignalVariant(TileIndex t, SignalVariant v)
SB(_m[t].m2, 2, 1, v); SB(_m[t].m2, 2, 1, v);
} }
static inline bool IsSignalPresent(TileIndex t, byte signalbit)
{
return HASBIT(_m[t].m3, signalbit + 4);
}
/** These are states in which a signal can be. Currently these are only two, so /** These are states in which a signal can be. Currently these are only two, so
* simple boolean logic will do. But do try to compare to this enum instead of * simple boolean logic will do. But do try to compare to this enum instead of
* normal boolean evaluation, since that will make future additions easier. * normal boolean evaluation, since that will make future additions easier.
@ -270,11 +265,67 @@ enum SignalState {
SIGNAL_STATE_GREEN = 1, ///< The signal is green SIGNAL_STATE_GREEN = 1, ///< The signal is green
}; };
static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit) /**
* Set the states of the signals (Along/AgainstTrackDir)
* @param tile the tile to set the states for
* @param state the new state
*/
static inline void SetSignalStates(TileIndex tile, uint state)
{ {
return (SignalState)HASBIT(_m[t].m2, signalbit + 4); SB(_m[tile].m2, 4, 4, state);
} }
/**
* Set the states of the signals (Along/AgainstTrackDir)
* @param tile the tile to set the states for
* @param state the new state
*/
static inline uint GetSignalStates(TileIndex tile)
{
return GB(_m[tile].m2, 4, 4);
}
/**
* Get the state of a single signal
* @param t the tile to get the signal state for
* @param signalbit the signal
* @return the state of the signal
*/
static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
{
return (SignalState)HASBIT(GetSignalStates(t), signalbit);
}
/**
* Set whether the given signals are present (Along/AgainstTrackDir)
* @param tile the tile to set the present signals for
* @param signals the signals that have to be present
*/
static inline void SetPresentSignals(TileIndex tile, uint signals)
{
SB(_m[tile].m3, 4, 4, signals);
}
/**
* Get whether the given signals are present (Along/AgainstTrackDir)
* @param tile the tile to get the present signals for
* @return the signals that are present
*/
static inline uint GetPresentSignals(TileIndex tile)
{
return GB(_m[tile].m3, 4, 4);
}
/**
* Checks whether the given signals is present
* @param t the tile to check on
* @param signalbit the signal
* @return true if and only if the signal is present
*/
static inline bool IsSignalPresent(TileIndex t, byte signalbit)
{
return HASBIT(GetPresentSignals(t), signalbit);
}
/** /**
* Checks for the presence of signals (either way) on the given track on the * Checks for the presence of signals (either way) on the given track on the
@ -285,7 +336,7 @@ static inline bool HasSignalOnTrack(TileIndex tile, Track track)
assert(IsValidTrack(track)); assert(IsValidTrack(track));
return return
GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
(_m[tile].m3 & SignalOnTrack(track)) != 0; (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
} }
/** /**
@ -300,7 +351,7 @@ static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
assert (IsValidTrackdir(trackdir)); assert (IsValidTrackdir(trackdir));
return return
GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
_m[tile].m3 & SignalAlongTrackdir(trackdir); GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
} }
/** /**
@ -313,7 +364,7 @@ static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trac
{ {
assert(IsValidTrackdir(trackdir)); assert(IsValidTrackdir(trackdir));
assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir))); assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
return _m[tile].m2 & SignalAlongTrackdir(trackdir) ? return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
SIGNAL_STATE_GREEN : SIGNAL_STATE_RED; SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
} }