(svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
authortron
Sun, 19 Mar 2006 12:06:12 +0000
changeset 3267 591027d10884
parent 3266 c92504a0c684
child 3268 0ee220b09578
(svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
clear_cmd.c
pathfind.c
rail.h
rail_cmd.c
rail_map.h
road_cmd.c
train_cmd.c
tunnelbridge_cmd.c
water_cmd.c
waypoint.c
--- a/clear_cmd.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/clear_cmd.c	Sun Mar 19 12:06:12 2006 +0000
@@ -96,7 +96,7 @@
 		return r;
 
 	if (IsTileType(tile, MP_RAILWAY)) {
-		static const byte _railway_modes[4] = {8, 0x10, 4, 0x20};
+		static const TrackBits _railway_modes[] = { TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER, TRACK_BIT_RIGHT };
 		static const byte _railway_dangslopes[4] = {0xd, 0xe, 7, 0xb};
 		static const byte _railway_dangslopes2[4] = {0x2, 0x1, 0x8, 0x4};
 
@@ -113,7 +113,7 @@
 
 		// If we have a single diagonal track there, the other side of
 		// tile can be terraformed.
-		if ((_m[tile].m5 & ~0x40) == _railway_modes[mode]) {
+		if (IsPlainRailTile(tile) && GetTrackBits(tile) == _railway_modes[mode]) {
 			if (ts->direction == 1) return 0;
 			skip_clear = true;
 		}
--- a/pathfind.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/pathfind.c	Sun Mar 19 12:06:12 2006 +0000
@@ -782,7 +782,7 @@
 			}
 
 			/* Regular rail tile, determine which tracks exist. */
-			allbits = _m[tile].m5 & TRACK_BIT_MASK;
+			allbits = GetTrackBits(tile);
 			/* Which tracks are reachable? */
 			bits = allbits & DiagdirReachesTracks(direction);
 
--- a/rail.h	Sun Mar 19 09:01:40 2006 +0000
+++ b/rail.h	Sun Mar 19 12:06:12 2006 +0000
@@ -213,14 +213,6 @@
 	return rtt == RAIL_TYPE_NORMAL || rtt == RAIL_TYPE_SIGNALS;
 }
 
-/**
- * Returns the tracks present on the given plain rail tile (IsPlainRailTile())
- */
-static inline TrackBits GetTrackBits(TileIndex tile)
-{
-	assert(GetRailTileType(tile) == RAIL_TYPE_NORMAL || GetRailTileType(tile) == RAIL_TYPE_SIGNALS);
-	return (TrackBits)(_m[tile].m5 & TRACK_BIT_MASK);
-}
 
 /**
  * Returns whether the given track is present on the given tile. Tile must be
--- a/rail_cmd.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/rail_cmd.c	Sun Mar 19 12:06:12 2006 +0000
@@ -130,7 +130,7 @@
 }
 
 
-static const byte _valid_tileh_slopes[][15] = {
+static const TrackBits _valid_tileh_slopes[][15] = {
 
 // set of normal ones
 {
@@ -199,7 +199,7 @@
 	},
 };
 
-uint GetRailFoundation(uint tileh, uint bits)
+uint GetRailFoundation(uint tileh, TrackBits bits)
 {
 	int i;
 
@@ -350,8 +350,9 @@
 				break;
 			}
 
-			if (IsLevelCrossing(tile) && (m5 & 0x08 ? TRACK_X : TRACK_Y) == track)
+			if (IsLevelCrossing(tile) && GetCrossingRailBits(tile) == trackbit) {
 				return_cmd_error(STR_1007_ALREADY_BUILT);
+			}
 			/* FALLTHROUGH */
 
 		default:
@@ -1341,18 +1342,17 @@
 
 static void DrawTile_Track(TileInfo *ti)
 {
-	byte m5;
 	const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
 	PalSpriteID image;
 
 	_drawtile_track_palette = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)));
 
-	m5 = (byte)ti->map5;
 	if (GetRailTileType(ti->tile) != RAIL_TYPE_DEPOT_WAYPOINT) {
+		TrackBits rails = GetTrackBits(ti->tile);
 		bool earth = (_m[ti->tile].m2 & RAIL_MAP2LO_GROUND_MASK) == RAIL_GROUND_BROWN;
 		bool snow = (_m[ti->tile].m2 & RAIL_MAP2LO_GROUND_MASK) == RAIL_GROUND_ICE_DESERT;
 
-		DrawTrackBits(ti, m5 & TRACK_BIT_MASK, earth, snow, false);
+		DrawTrackBits(ti, rails, earth, snow, false);
 
 		if (_display_opt & DO_FULL_DETAIL) {
 			_detailed_track_proc[_m[ti->tile].m2 & RAIL_MAP2LO_GROUND_MASK](ti);
@@ -1370,21 +1370,21 @@
 #define ISON_SIGNAL(x) (m23 & (byte)(0x10 << (x)))
 #define MAYBE_DRAW_SIGNAL(x,y,z) if (HAS_SIGNAL(x)) DrawSignalHelper(ti, ISON_SIGNAL(x), ((y-0x4FB) << 4)|(z))
 
-		if (!(m5 & TRACK_BIT_Y)) {
-			if (!(m5 & TRACK_BIT_X)) {
-				if (m5 & TRACK_BIT_LEFT) {
+		if (!(rails & TRACK_BIT_Y)) {
+			if (!(rails & TRACK_BIT_X)) {
+				if (rails & TRACK_BIT_LEFT) {
 					MAYBE_DRAW_SIGNAL(2, 0x509, 0);
 					MAYBE_DRAW_SIGNAL(3, 0x507, 1);
 				}
-				if (m5 & TRACK_BIT_RIGHT) {
+				if (rails & TRACK_BIT_RIGHT) {
 					MAYBE_DRAW_SIGNAL(0, 0x509, 2);
 					MAYBE_DRAW_SIGNAL(1, 0x507, 3);
 				}
-				if (m5 & TRACK_BIT_UPPER) {
+				if (rails & TRACK_BIT_UPPER) {
 					MAYBE_DRAW_SIGNAL(3, 0x505, 4);
 					MAYBE_DRAW_SIGNAL(2, 0x503, 5);
 				}
-				if (m5 & TRACK_BIT_LOWER) {
+				if (rails & TRACK_BIT_LOWER) {
 					MAYBE_DRAW_SIGNAL(1, 0x505, 6);
 					MAYBE_DRAW_SIGNAL(0, 0x503, 7);
 				}
@@ -1400,7 +1400,7 @@
 	} else {
 		/* draw depots / waypoints */
 		const DrawTrackSeqStruct *drss;
-		byte type = m5 & 0x3F; // 0-3: depots, 4-5: waypoints
+		byte type = ti->map5 & 0x3F; // 0-3: depots, 4-5: waypoints
 
 		if (ti->tileh != 0) DrawFoundation(ti, ti->tileh);
 
@@ -1412,7 +1412,7 @@
 			if (stat != NULL) {
 				DrawTileSeqStruct const *seq;
 				// emulate station tile - open with building
-				const DrawTileSprites *cust = &stat->renderdata[2 + (m5 & 0x1)];
+				const DrawTileSprites *cust = &stat->renderdata[2 + (ti->map5 & 0x1)];
 				uint32 relocation = GetCustomStationRelocation(stat, ComposeWaypointStation(ti->tile), 0);
 
 				/* We don't touch the 0x8000 bit. In all this
@@ -1790,8 +1790,10 @@
 
 	// check if it's a foundation
 	if (ti->tileh != 0) {
-		if ((ti->map5 & 0x80) == 0) {
-			uint f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F);
+		if (GetRailTileType(ti->tile) == RAIL_TYPE_DEPOT_WAYPOINT) {
+			return z + 8;
+		} else {
+			uint f = GetRailFoundation(ti->tileh, GetTrackBits(ti->tile));
 
 			if (f != 0) {
 				if (f < 15) {
@@ -1801,9 +1803,6 @@
 				// inclined foundation
 				th = _inclined_tileh[f - 15];
 			}
-		} else if ((ti->map5 & RAIL_TILE_TYPE_MASK) == RAIL_TYPE_DEPOT_WAYPOINT) {
-			// depot or waypoint
-			return z + 8;
 		}
 		return GetPartialZ(ti->x & 0xF, ti->y & 0xF, th) + z;
 	}
@@ -1814,8 +1813,10 @@
 {
 	// check if it's a foundation
 	if (ti->tileh != 0) {
-		if ((ti->map5 & 0x80) == 0) {
-			uint f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F);
+		if (GetRailTileType(ti->tile) == RAIL_TYPE_DEPOT_WAYPOINT) {
+			return 0;
+		} else {
+			uint f = GetRailFoundation(ti->tileh, GetTrackBits(ti->tile));
 			if (f != 0) {
 				if (f < 15) {
 					// leveled foundation
@@ -1824,9 +1825,6 @@
 				// inclined foundation
 				return _inclined_tileh[f - 15];
 			}
-		} else if ((ti->map5 & 0xC0) == 0xC0) {
-			// depot or waypoint
-			return 0;
 		}
 	}
 	return ti->tileh;
@@ -1876,7 +1874,7 @@
 
 	if (old_ground != RAIL_GROUND_BROWN) { /* wait until bottom is green */
 		/* determine direction of fence */
-		TrackBits rail = _m[tile].m5 & TRACK_BIT_MASK;
+		TrackBits rail = GetTrackBits(tile);
 
 		switch (rail) {
 			case TRACK_BIT_UPPER: new_ground = RAIL_GROUND_FENCE_HORIZ1; break;
@@ -1964,20 +1962,17 @@
 
 static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode)
 {
-	byte m5, a;
+	byte a;
 	uint16 b;
-	uint32 ret;
 
 	if (mode != TRANSPORT_RAIL) return 0;
 
-	m5 = _m[tile].m5;
-
 	if (GetRailTileType(tile) != RAIL_TYPE_DEPOT_WAYPOINT) {
-		ret = (m5 | (m5 << 8)) & 0x3F3F;
+		TrackBits rails = GetTrackBits(tile);
+		uint32 ret = rails * 0x101;
+
 		if (GetRailTileType(tile) != RAIL_TYPE_SIGNALS) {
-			if ( (ret & 0xFF) == 3)
-			/* Diagonal crossing? */
-				ret |= 0x40;
+			if (rails == TRACK_BIT_CROSS) ret |= 0x40;
 		} else {
 			/* has_signals */
 
@@ -1998,13 +1993,14 @@
 			if ((b & 0x20) == 0) ret |= 0x20080000;
 			if ((b & 0x10) == 0) ret |= 0x08200000;
 		}
-	} else if (m5 & 0x40) {
-		static const byte _train_spec_tracks[6] = {1,2,1,2,1,2};
-		m5 = _train_spec_tracks[m5 & 0x3F];
-		ret = (m5 << 8) + m5;
-	} else
-		return 0;
-	return ret;
+		return ret;
+	} else {
+		if (_m[tile].m5 & 0x40) {
+			return GetRailWaypointBits(tile) * 0x101;
+		} else {
+			return 0;
+		}
+	}
 }
 
 static void ClickTile_Track(TileIndex tile)
--- a/rail_map.h	Sun Mar 19 09:01:40 2006 +0000
+++ b/rail_map.h	Sun Mar 19 12:06:12 2006 +0000
@@ -105,6 +105,11 @@
 	TRACK_BIT_MASK  = 0x3FU
 } TrackBits;
 
+static inline TrackBits GetTrackBits(TileIndex tile)
+{
+	return (TrackBits)GB(_m[tile].m5, 0, 6);
+}
+
 
 static inline DiagDirection GetRailDepotDirection(TileIndex t)
 {
--- a/road_cmd.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/road_cmd.c	Sun Mar 19 12:06:12 2006 +0000
@@ -1034,7 +1034,7 @@
 	switch (mode) {
 		case TRANSPORT_RAIL:
 			if (!IsLevelCrossing(tile)) return 0;
-			return _m[tile].m5 & 8 ? 0x101 : 0x202;
+			return GetCrossingRailBits(tile) * 0x101;
 
 		case TRANSPORT_ROAD:
 			switch (GetRoadType(tile)) {
--- a/train_cmd.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/train_cmd.c	Sun Mar 19 12:06:12 2006 +0000
@@ -2652,7 +2652,8 @@
 
 static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
 {
-	if (IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xC0) == 0x40) {
+	if (IsTileType(tile, MP_RAILWAY) &&
+			GetRailTileType(tile) == RAIL_TYPE_SIGNALS) {
 		uint i = FindFirstBit2x64((_m[tile].m5 + (_m[tile].m5 << 8)) & _reachable_tracks[dir]);
 		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]);
 	}
--- a/tunnelbridge_cmd.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/tunnelbridge_cmd.c	Sun Mar 19 12:06:12 2006 +0000
@@ -328,7 +328,8 @@
 				break;
 
 			case MP_RAILWAY:
-				if (_m[tile].m5 != (direction == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X)) {
+				if (GetRailTileType(tile) != RAIL_TYPE_NORMAL ||
+						GetTrackBits(tile) != (direction == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X)) {
 					goto not_valid_below;
 				}
 				transport_under = TRANSPORT_RAIL;
--- a/water_cmd.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/water_cmd.c	Sun Mar 19 12:06:12 2006 +0000
@@ -519,14 +519,21 @@
 		// make coast..
 		switch (GetTileType(target)) {
 			case MP_RAILWAY: {
-				uint slope = GetTileSlope(target, NULL);
-				byte tracks = GB(_m[target].m5, 0, 6);
+				TrackBits tracks;
+				uint slope;
+
+				if (!IsPlainRailTile(tile)) break;
+
+				tracks = GetTrackBits(target);
+				slope = GetTileSlope(target, NULL);
 				if (!(
-						(slope == 1 && tracks == 0x20) ||
-						(slope == 2 && tracks == 0x04) ||
-						(slope == 4 && tracks == 0x10) ||
-						(slope == 8 && tracks == 0x08)))
+							(slope == 1 && tracks == TRACK_BIT_RIGHT) &&
+							(slope == 2 && tracks == TRACK_BIT_UPPER) &&
+							(slope == 4 && tracks == TRACK_BIT_LEFT)  &&
+							(slope == 8 && tracks == TRACK_BIT_LOWER)
+						)) {
 					break;
+				}
 			}
 			/* FALLTHROUGH */
 
--- a/waypoint.c	Sun Mar 19 09:01:40 2006 +0000
+++ b/waypoint.c	Sun Mar 19 12:06:12 2006 +0000
@@ -182,9 +182,10 @@
 	/* if custom gfx are used, make sure it is within bounds */
 	if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR;
 
-	if (!IsTileType(tile, MP_RAILWAY) || (
-				(axis = AXIS_X, _m[tile].m5 != TRACK_BIT_X) &&
-				(axis = AXIS_Y, _m[tile].m5 != TRACK_BIT_Y)
+	if (!IsTileType(tile, MP_RAILWAY) ||
+			GetRailTileType(tile) != RAIL_TYPE_NORMAL || (
+				(axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) &&
+				(axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y)
 			)) {
 		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
 	}