Changeset - r3237:505d7d17fa9c
[Not reviewed]
master
0 3 0
tron - 19 years ago 2006-03-16 21:44:58
tron@openttd.org
(svn r3911) Add functions to retrieve/set the signal variant (electric/semaphore)
3 files changed with 26 insertions and 29 deletions:
0 comments (0 inline, 0 general)
rail.h
Show inline comments
 
@@ -58,15 +58,12 @@ typedef enum RailTypes {
 
	RAILTYPE_MAGLEV = 2,
 
	RAILTYPE_END,
 
	RAILTYPE_MASK   = 0x3,
 
	INVALID_RAILTYPE = 0xFF,
 
} RailType;
 

	
 
enum {
 
	SIG_SEMAPHORE_MASK = 1 << 2,
 
};
 

	
 
/** These are used to specify a single track. Can be translated to a trackbit
 
 * with TrackToTrackbit */
 
typedef enum Tracks {
 
	TRACK_X     = 0,
 
	TRACK_Y     = 1,
 
@@ -528,26 +525,12 @@ static inline SignalType GetSignalType(T
 
{
 
	assert(IsValidTrack(track));
 
	assert(GetRailTileType(tile) == RAIL_TYPE_SIGNALS);
 
	return (SignalType)(_m[tile].m4 & SIGTYPE_MASK);
 
}
 

	
 
/**
 
 * Checks if this tile contains semaphores (returns true) or normal signals
 
 * (returns false) on the given track. Does not check if there are actually
 
 * signals on the track, you should use HasSignalsOnTrack() for that.
 
 *
 
 * Note that currently, the track argument is not used, since
 
 * semaphores/electric signals cannot be mixed. This function is trying to be
 
 * future-compatible, though.
 
 */
 
static inline bool HasSemaphores(TileIndex tile, Track track)
 
{
 
	assert(IsValidTrack(track));
 
	return (_m[tile].m4 & SIG_SEMAPHORE_MASK) != 0;
 
}
 

	
 
/**
 
 * Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
 
 * Note that there is no check if the given trackdir is actually present on
 
 * the tile!
 
 * The given trackdir is used when there are (could be) multiple rail types on
rail_cmd.c
Show inline comments
 
@@ -678,19 +678,20 @@ int32 CmdBuildTrainDepot(int x, int y, u
 
 * @param p2 used for CmdBuildManySignals() to copy direction of first signal
 
 * TODO: p2 should be replaced by two bits for "along" and "against" the track.
 
 */
 
int32 CmdBuildSingleSignal(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	TileIndex tile = TileVirtXY(x, y);
 
	bool semaphore;
 
	SignalVariant sigvar;
 
	bool pre_signal;
 
	Track track = (Track)(p1 & 0x7);
 
	int32 cost;
 

	
 
	// Same bit, used in different contexts
 
	semaphore = pre_signal = HASBIT(p1, 3);
 
	sigvar = HASBIT(p1, 3) ? SIG_SEMAPHORE : SIG_ELECTRIC;
 
	pre_signal = HASBIT(p1, 3);
 

	
 
	if (!ValParamTrackOrientation(track) || !IsTileType(tile, MP_RAILWAY) || !EnsureNoVehicle(tile))
 
		return CMD_ERROR;
 

	
 
	/* Protect against invalid signal copying */
 
	if (p2 != 0 && (p2 & SignalOnTrack(track)) == 0) return CMD_ERROR;
 
@@ -715,13 +716,13 @@ int32 CmdBuildSingleSignal(int x, int y,
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	if (!HasSignalOnTrack(tile, track)) {
 
		// build new signals
 
		cost = _price.build_signals;
 
	} else {
 
		if (p2 != 0 && semaphore != HasSemaphores(tile, track)) {
 
		if (p2 != 0 && sigvar != GetSignalVariant(tile)) {
 
			// convert signals <-> semaphores
 
			cost = _price.build_signals + _price.remove_signals;
 
		} else {
 
			// it is free to change orientation/pre-exit-combo signals
 
			cost = 0;
 
		}
 
@@ -730,13 +731,14 @@ int32 CmdBuildSingleSignal(int x, int y,
 
	if (flags & DC_EXEC) {
 
		if (GetRailTileType(tile) != RAIL_TYPE_SIGNALS) {
 
			// there are no signals at all on this tile yet
 
			_m[tile].m5 |= RAIL_TYPE_SIGNALS; // change into signals
 
			_m[tile].m2 |= 0xF0;              // all signals are on
 
			_m[tile].m3 &= ~0xF0;          // no signals built by default
 
			_m[tile].m4 = semaphore ? SIG_SEMAPHORE_MASK : 0;
 
			_m[tile].m4 = 0;
 
			SetSignalVariant(tile, sigvar);
 
		}
 

	
 
		if (p2 == 0) {
 
			if (!HasSignalOnTrack(tile, track)) {
 
				// build new signals
 
				_m[tile].m3 |= SignalOnTrack(track);
 
@@ -770,18 +772,13 @@ int32 CmdBuildSingleSignal(int x, int y,
 
			}
 
		} 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);
 
			// convert between signal<->semaphores when dragging
 
			if (semaphore) {
 
				SETBIT(_m[tile].m4, 2);
 
			} else {
 
				CLRBIT(_m[tile].m4, 2);
 
			}
 
			SetSignalVariant(tile, sigvar);
 
		}
 

	
 
		MarkTileDirtyByTile(tile);
 
		SetSignalsOnBothDir(tile, track);
 
	}
 

	
 
@@ -833,13 +830,14 @@ static int32 CmdSignalTrackHelper(int x,
 

	
 
	// copy the signal-style of the first rail-piece if existing
 
	if (GetRailTileType(tile) == RAIL_TYPE_SIGNALS && GetTrackBits(tile) != 0) { /* XXX: GetTrackBits check useless? */
 
		signals = _m[tile].m3 & SignalOnTrack(track);
 
		if (signals == 0) signals = SignalOnTrack(track); /* Can this actually occur? */
 

	
 
		semaphores = (HasSemaphores(tile, track) ? 8 : 0); // copy signal/semaphores style (independent of CTRL)
 
		// copy signal/semaphores style (independent of CTRL)
 
		semaphores = (GetSignalVariant(tile) == SIG_ELECTRIC ? 0 : 8);
 
	} else // no signals exist, drag a two-way signal stretch
 
		signals = SignalOnTrack(track);
 

	
 
	/* signal_ctr         - amount of tiles already processed
 
	 * signals_density    - patch setting to put signal on every Nth tile (double space on |, -- tracks)
 
	 **********
 
@@ -911,13 +909,13 @@ int32 CmdRemoveSingleSignal(int x, int y
 
		_m[tile].m3 &= ~SignalOnTrack(track);
 

	
 
		/* removed last signal from tile? */
 
		if (GB(_m[tile].m3, 4, 4) == 0) {
 
			SB(_m[tile].m2, 4, 4, 0);
 
			SB(_m[tile].m5, 6, 2, RAIL_TYPE_NORMAL >> 6); // XXX >> because the constant is meant for direct application, not use with SB
 
			CLRBIT(_m[tile].m4, 2); // remove any possible semaphores
 
			SetSignalVariant(tile, SIG_ELECTRIC); // remove any possible semaphores
 
		}
 

	
 
		SetSignalsOnBothDir(tile, track);
 

	
 
		MarkTileDirtyByTile(tile);
 
	}
rail_map.h
Show inline comments
 
@@ -17,12 +17,28 @@ static inline DiagDirection GetRailDepot
 
static inline TrackBits GetRailWaypointBits(TileIndex t)
 
{
 
	return _m[t].m5 & RAIL_WAYPOINT_TRACK_MASK ? TRACK_BIT_Y : TRACK_BIT_X;
 
}
 

	
 

	
 
typedef enum SignalVariant {
 
	SIG_ELECTRIC  = 0,
 
	SIG_SEMAPHORE = 1
 
} SignalVariant;
 

	
 
static inline SignalVariant GetSignalVariant(TileIndex t)
 
{
 
	return (SignalVariant)GB(_m[t].m4, 2, 1);
 
}
 

	
 
static inline void SetSignalVariant(TileIndex t, SignalVariant v)
 
{
 
	SB(_m[t].m4, 2, 1, v);
 
}
 

	
 

	
 
static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
 
{
 
	SetTileType(t, MP_RAILWAY);
 
	SetTileOwner(t, o);
 
	_m[t].m2 = 0;
 
	_m[t].m3 = r;
0 comments (0 inline, 0 general)