summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-05-30 13:33:19 +0000
committerrubidium <rubidium@openttd.org>2007-05-30 13:33:19 +0000
commit70e6102c86b55a6b269bbe931309d9a57b65cd2e (patch)
tree203e85269cca25c61538c52013c462e47d4beb29 /src
parentbc41dc4966cc105937e5351a2a434363596c0e07 (diff)
downloadopenttd-70e6102c86b55a6b269bbe931309d9a57b65cd2e.tar.xz
(svn r9988) -Codechange: remove the last direct map accesses, except the ones needed for the savegame saving/loading mechanisms.
Diffstat (limited to 'src')
-rw-r--r--src/rail.cpp10
-rw-r--r--src/rail_cmd.cpp44
-rw-r--r--src/rail_map.h69
3 files changed, 85 insertions, 38 deletions
diff --git a/src/rail.cpp b/src/rail.cpp
index 86fe50393..a5f2633fd 100644
--- a/src/rail.cpp
+++ b/src/rail.cpp
@@ -13,21 +13,21 @@
/* Maps a trackdir to the bit that stores its status in the map arrays, in the
* direction along with the trackdir */
extern const byte _signal_along_trackdir[] = {
- 0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0,
- 0x40, 0x40, 0x40, 0x10, 0x80, 0x20
+ 0x8, 0x8, 0x8, 0x2, 0x4, 0x1, 0, 0,
+ 0x4, 0x4, 0x4, 0x1, 0x8, 0x2
};
/* Maps a trackdir to the bit that stores its status in the map arrays, in the
* direction against the trackdir */
extern const byte _signal_against_trackdir[] = {
- 0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0,
- 0x80, 0x80, 0x80, 0x20, 0x40, 0x10
+ 0x4, 0x4, 0x4, 0x1, 0x8, 0x2, 0, 0,
+ 0x8, 0x8, 0x8, 0x2, 0x4, 0x1
};
/* Maps a Track to the bits that store the status of the two signals that can
* be present on the given 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
diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp
index ef52fcfad..7ffdc4a63 100644
--- a/src/rail_cmd.cpp
+++ b/src/rail_cmd.cpp
@@ -705,8 +705,8 @@ int32 CmdBuildSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (!HasSignals(tile)) {
/* there are no signals at all on this tile yet */
SetHasSignals(tile, true);
- _m[tile].m2 |= 0xF0; // all signals are on
- _m[tile].m3 &= ~0xF0; // no signals built by default
+ SetSignalStates(tile, 0xF); // all signals are on
+ SetPresentSignals(tile, 0); // no signals built by default
SetSignalType(tile, SIGTYPE_NORMAL);
SetSignalVariant(tile, sigvar);
}
@@ -714,7 +714,7 @@ int32 CmdBuildSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (p2 == 0) {
if (!HasSignalOnTrack(tile, track)) {
/* build new signals */
- _m[tile].m3 |= SignalOnTrack(track);
+ SetPresentSignals(tile, GetPresentSignals(tile) | SignalOnTrack(track));
} else {
if (pre_signal) {
/* cycle between normal -> pre -> exit -> combo -> ... */
@@ -728,8 +728,7 @@ int32 CmdBuildSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
} else {
/* If CmdBuildManySignals is called with copying signals, just copy the
* direction of the first signal given as parameter by CmdBuildManySignals */
- _m[tile].m3 &= ~SignalOnTrack(track);
- _m[tile].m3 |= p2 & SignalOnTrack(track);
+ SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | (p2 & SignalOnTrack(track)));
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 */
if (HasSignals(tile)) {
- signals = _m[tile].m3 & SignalOnTrack(track);
+ signals = GetPresentSignals(tile) & SignalOnTrack(track);
if (signals == 0) signals = SignalOnTrack(track); /* Can this actually occur? */
/* copy signal/semaphores style (independent of CTRL) */
@@ -874,11 +873,11 @@ int32 CmdRemoveSingleSignal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
/* Do it? */
if (flags & DC_EXEC) {
- _m[tile].m3 &= ~SignalOnTrack(track);
+ SetPresentSignals(tile, GetPresentSignals(tile) & ~SignalOnTrack(track));
/* removed last signal from tile? */
- if (GB(_m[tile].m3, 4, 4) == 0) {
- SB(_m[tile].m2, 4, 4, 0);
+ if (GetPresentSignals(tile) == 0) {
+ SetSignalStates(tile, 0);
SetHasSignals(tile, false);
SetSignalVariant(tile, SIG_ELECTRIC); // remove any possible semaphores
}
@@ -1666,7 +1665,7 @@ static void ChangeSignalStates(SetSignalsData *ssd)
for (i = 0; i != ssd->cur; i++) {
TileIndex tile = ssd->tile[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 */
if (IsPresignalEntry(tile)) {
@@ -1686,10 +1685,10 @@ static void ChangeSignalStates(SetSignalsData *ssd)
if (ssd->stop) {
make_red:
/* turn red */
- if ((bit & m2) == 0) continue;
+ if ((bit & signals) == 0) continue;
} else {
/* 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. */
@@ -1704,7 +1703,7 @@ make_red:
}
/* it changed, so toggle it */
- _m[tile].m2 = m2 ^ bit;
+ SetSignalStates(tile, signals ^ bit);
MarkTileDirtyByTile(tile);
}
}
@@ -1928,11 +1927,8 @@ static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode, uint
case RAIL_TILE_SIGNALS: {
uint32 ret = GetTrackBits(tile) * 0x101;
- byte a;
- uint16 b;
-
- a = _m[tile].m3;
- b = _m[tile].m2;
+ byte a = GetPresentSignals(tile);
+ uint b = GetSignalStates(tile);
b &= a;
@@ -1940,13 +1936,13 @@ static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode, uint
* direction), we pretend them to be green. (So if
* signals are only one way, the other way will
* implicitely become `red' */
- if ((a & 0xC0) == 0) b |= 0xC0;
- if ((a & 0x30) == 0) b |= 0x30;
+ if ((a & 0xC) == 0) b |= 0xC;
+ if ((a & 0x3) == 0) b |= 0x3;
- if ((b & 0x80) == 0) ret |= 0x10070000;
- if ((b & 0x40) == 0) ret |= 0x07100000;
- if ((b & 0x20) == 0) ret |= 0x20080000;
- if ((b & 0x10) == 0) ret |= 0x08200000;
+ if ((b & 0x8) == 0) ret |= 0x10070000;
+ if ((b & 0x4) == 0) ret |= 0x07100000;
+ if ((b & 0x2) == 0) ret |= 0x20080000;
+ if ((b & 0x1) == 0) ret |= 0x08200000;
return ret;
}
diff --git a/src/rail_map.h b/src/rail_map.h
index e4fd20b89..1d22fe332 100644
--- a/src/rail_map.h
+++ b/src/rail_map.h
@@ -256,11 +256,6 @@ static inline void SetSignalVariant(TileIndex t, SignalVariant 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
* 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.
@@ -270,11 +265,67 @@ enum SignalState {
SIGNAL_STATE_GREEN = 1, ///< The signal is green
};
+/**
+ * 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)
+{
+ 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(_m[t].m2, signalbit + 4);
+ 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
@@ -285,7 +336,7 @@ static inline bool HasSignalOnTrack(TileIndex tile, Track track)
assert(IsValidTrack(track));
return
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));
return
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(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
- return _m[tile].m2 & SignalAlongTrackdir(trackdir) ?
+ return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
}