(svn r9988) -Codechange: remove the last direct map accesses, except the ones needed for the savegame saving/loading mechanisms.
authorrubidium
Wed, 30 May 2007 13:33:19 +0000
changeset 7249 3e6c9df9794d
parent 7248 a9ec96b296e5
child 7250 49ca9a9e70f1
(svn r9988) -Codechange: remove the last direct map accesses, except the ones needed for the savegame saving/loading mechanisms.
src/rail.cpp
src/rail_cmd.cpp
src/rail_map.h
--- a/src/rail.cpp	Wed May 30 13:27:56 2007 +0000
+++ b/src/rail.cpp	Wed May 30 13:33:19 2007 +0000
@@ -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
--- a/src/rail_cmd.cpp	Wed May 30 13:27:56 2007 +0000
+++ b/src/rail_cmd.cpp	Wed May 30 13:33:19 2007 +0000
@@ -705,8 +705,8 @@
 		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 @@
 		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 @@
 		} 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 @@
 
 	/* 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 @@
 
 	/* 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 @@
 	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 @@
 		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 @@
 		}
 
 		/* it changed, so toggle it */
-		_m[tile].m2 = m2 ^ bit;
+		SetSignalStates(tile, signals ^ bit);
 		MarkTileDirtyByTile(tile);
 	}
 }
@@ -1928,11 +1927,8 @@
 
 		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 @@
 			 * 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;
 		}
--- a/src/rail_map.h	Wed May 30 13:27:56 2007 +0000
+++ b/src/rail_map.h	Wed May 30 13:33:19 2007 +0000
@@ -256,11 +256,6 @@
 	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 @@
 	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 @@
 	assert(IsValidTrack(track));
 	return
 		GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
-		(_m[tile].m3 & SignalOnTrack(track)) != 0;
+		(GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
 }
 
 /**
@@ -300,7 +351,7 @@
 	assert (IsValidTrackdir(trackdir));
 	return
 		GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
-		_m[tile].m3 & SignalAlongTrackdir(trackdir);
+		GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
 }
 
 /**
@@ -313,7 +364,7 @@
 {
 	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;
 }