(svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
authortron
Fri, 17 Mar 2006 10:10:31 +0000
changeset 3242 1cefa03f0d5e
parent 3241 ffde1325bab8
child 3243 6232a0862ae6
(svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
rail.c
rail_cmd.c
rail_map.h
road_cmd.c
station_cmd.c
train_cmd.c
train_gui.c
tunnelbridge_cmd.c
waypoint.c
--- a/rail.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/rail.c	Fri Mar 17 10:10:31 2006 +0000
@@ -108,43 +108,39 @@
 
 RailType GetTileRailType(TileIndex tile, Trackdir trackdir)
 {
-	RailType type = INVALID_RAILTYPE;
 	DiagDirection exitdir = TrackdirToExitdir(trackdir);
 	switch (GetTileType(tile)) {
 		case MP_RAILWAY:
-			/* railway track */
-			type = _m[tile].m3 & RAILTYPE_MASK;
-			break;
+			return GetRailType(tile);
+
 		case MP_STREET:
 			/* rail/road crossing */
-			if (IsLevelCrossing(tile))
-				type = _m[tile].m4 & RAILTYPE_MASK;
+			if (IsLevelCrossing(tile)) return GetRailTypeCrossing(tile);
 			break;
+
 		case MP_STATION:
-			if (IsTrainStationTile(tile))
-				type = _m[tile].m3 & RAILTYPE_MASK;
+			if (IsTrainStationTile(tile)) return GetRailType(tile);
 			break;
+
 		case MP_TUNNELBRIDGE:
 			if (IsTunnel(tile)) {
 				if (GetTunnelTransportType(tile) == TRANSPORT_RAIL) {
-					return _m[tile].m3 & RAILTYPE_MASK;
+					return GetRailType(tile);
 				}
 			} else {
 				if (IsBridgeRamp(tile)) {
 					if (GetBridgeTransportType(tile) == TRANSPORT_RAIL) {
-						return _m[tile].m3 & RAILTYPE_MASK;
+						return GetRailType(tile);
 					}
 				} else {
 					if (GetBridgeAxis(tile) == DiagDirToAxis(exitdir)) {
 						if (GetBridgeTransportType(tile) == TRANSPORT_RAIL) {
-							/* on the bridge */
-							return (_m[tile].m3 >> 4) & RAILTYPE_MASK;
+							return GetRailTypeOnBridge(tile);
 						}
 					} else {
 						if (IsTransportUnderBridge(tile) &&
 								GetTransportTypeUnderBridge(tile) == TRANSPORT_RAIL) {
-							/* under the bridge */
-							return _m[tile].m3 & RAILTYPE_MASK;
+							return GetRailType(tile);
 						}
 					}
 				}
@@ -154,5 +150,5 @@
 		default:
 			break;
 	}
-	return type;
+	return INVALID_RAILTYPE;
 }
--- a/rail_cmd.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/rail_cmd.c	Fri Mar 17 10:10:31 2006 +0000
@@ -317,7 +317,7 @@
 			}
 			if (m5 & RAIL_TYPE_SPECIAL ||
 					!IsTileOwner(tile, _current_player) ||
-					GB(_m[tile].m3, 0, 4) != p1) {
+					GetRailType(tile) != p1) {
 				// Get detailed error message
 				return DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 			}
@@ -944,7 +944,7 @@
 
 	// change type.
 	if (exec) {
-		SB(_m[tile].m3, 0, 4, totype);
+		SetRailType(tile, totype);
 		MarkTileDirtyByTile(tile);
 	}
 
--- a/rail_map.h	Fri Mar 17 08:06:56 2006 +0000
+++ b/rail_map.h	Fri Mar 17 10:10:31 2006 +0000
@@ -36,7 +36,6 @@
 	RAILTYPE_MONO   = 1,
 	RAILTYPE_MAGLEV = 2,
 	RAILTYPE_END,
-	RAILTYPE_MASK   = 0x3,
 	INVALID_RAILTYPE = 0xFF
 } RailType;
 
@@ -45,6 +44,33 @@
 	return (RailType)GB(_m[t].m3, 0, 4);
 }
 
+// TODO remove this by moving to the same bits as GetRailType()
+static inline RailType GetRailTypeCrossing(TileIndex t)
+{
+	return (RailType)GB(_m[t].m4, 0, 4);
+}
+
+static inline RailType GetRailTypeOnBridge(TileIndex t)
+{
+	return (RailType)GB(_m[t].m3, 4, 4);
+}
+
+static inline void SetRailType(TileIndex t, RailType r)
+{
+	SB(_m[t].m3, 0, 4, r);
+}
+
+// TODO remove this by moving to the same bits as SetRailType()
+static inline void SetRailTypeCrossing(TileIndex t, RailType r)
+{
+	SB(_m[t].m4, 0, 4, r);
+}
+
+static inline void SetRailTypeOnBridge(TileIndex t, RailType r)
+{
+	SB(_m[t].m3, 4, 4, r);
+}
+
 
 /** These are used to specify a single track.
  * Can be translated to a trackbit with TrackToTrackbit */
--- a/road_cmd.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/road_cmd.c	Fri Mar 17 10:10:31 2006 +0000
@@ -195,7 +195,7 @@
 					if (flags & DC_EXEC) {
 						ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
 
-						MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GB(_m[tile].m4, 0, 4));
+						MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailTypeCrossing(tile));
 						MarkTileDirtyByTile(tile);
 					}
 					return cost;
@@ -345,7 +345,7 @@
 			}
 
 			if (flags & DC_EXEC) {
-				MakeRoadCrossing(tile, _current_player, GetTileOwner(tile), roaddir, GB(_m[tile].m3, 0, 4), p2);
+				MakeRoadCrossing(tile, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), p2);
 				MarkTileDirtyByTile(tile);
 			}
 			return _price.build_road * 2;
@@ -428,12 +428,10 @@
 	// not owned by me?
 	if (!CheckTileOwnership(tile) || !EnsureNoVehicle(tile)) return CMD_ERROR;
 
-	// tile is already of requested type?
-	if (GB(_m[tile].m4, 0, 4) == totype) return CMD_ERROR;
+	if (GetRailTypeCrossing(tile) == totype) return CMD_ERROR;
 
 	if (exec) {
-		// change type.
-		SB(_m[tile].m4, 0, 4, totype);
+		SetRailTypeCrossing(tile, totype);
 		MarkTileDirtyByTile(tile);
 	}
 
@@ -773,7 +771,7 @@
 		case ROAD_CROSSING: {
 			if (ti->tileh != 0) DrawFoundation(ti, ti->tileh);
 
-			image = GetRailTypeInfo(GB(_m[ti->tile].m4, 0, 4))->base_sprites.crossing;
+			image = GetRailTypeInfo(GetRailTypeCrossing(ti->tile))->base_sprites.crossing;
 
 			if (GB(ti->map5, 3, 1) == 0) image++; /* direction */
 
--- a/station_cmd.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/station_cmd.c	Fri Mar 17 10:10:31 2006 +0000
@@ -1256,12 +1256,10 @@
 	// tile is not a railroad station?
 	if (_m[tile].m5 >= 8) return CMD_ERROR;
 
-	// tile is already of requested type?
-	if (GB(_m[tile].m3, 0, 4) == totype) return CMD_ERROR;
+	if (GetRailType(tile) == totype) return CMD_ERROR;
 
 	if (exec) {
-		// change type.
-		SB(_m[tile].m3, 0, 4, totype);
+		SetRailType(tile, totype);
 		MarkTileDirtyByTile(tile);
 	}
 
@@ -1949,7 +1947,7 @@
 	uint32 image;
 	const DrawTileSeqStruct *dtss;
 	const DrawTileSprites *t = NULL;
-	RailType railtype = GB(_m[ti->tile].m3, 0, 4);
+	RailType railtype = GetRailType(ti->tile);
 	const RailtypeInfo *rti = GetRailTypeInfo(railtype);
 	SpriteID offset;
 	uint32 relocation = 0;
--- a/train_cmd.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/train_cmd.c	Fri Mar 17 10:10:31 2006 +0000
@@ -2562,7 +2562,7 @@
 			return
 				IsTileOwner(tile, v->owner) && (
 					!IsFrontEngine(v) ||
-					IsCompatibleRail(v->u.rail.railtype, GB(_m[tile].m4, 0, 4))
+					IsCompatibleRail(v->u.rail.railtype, GetRailTypeCrossing(tile))
 				);
 
 		default:
--- a/train_gui.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/train_gui.c	Fri Mar 17 10:10:31 2006 +0000
@@ -335,7 +335,7 @@
 
 	if (tile != 0) {
 		w->caption_color = GetTileOwner(tile);
-		WP(w,buildtrain_d).railtype = GB(_m[tile].m3, 0, 4);
+		WP(w,buildtrain_d).railtype = GetRailType(tile);
 	} else {
 		w->caption_color = _local_player;
 		WP(w,buildtrain_d).railtype = GetBestRailtype(GetPlayer(_local_player));
--- a/tunnelbridge_cmd.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/tunnelbridge_cmd.c	Fri Mar 17 10:10:31 2006 +0000
@@ -333,7 +333,7 @@
 				}
 				transport_under = TRANSPORT_RAIL;
 				owner_under = GetTileOwner(tile);
-				rail_under = GB(_m[tile].m3, 0, 4);
+				rail_under = GetRailType(tile);
 				break;
 
 			case MP_STREET:
@@ -672,7 +672,7 @@
 		for (c = tile + delta; c != endtile; c += delta) {
 			if (IsTransportUnderBridge(c)) {
 				if (GetTransportTypeUnderBridge(c) == TRANSPORT_RAIL) {
-					MakeRailNormal(c, GetTileOwner(c), GetRailBitsUnderBridge(c), GB(_m[c].m3, 0, 3));
+					MakeRailNormal(c, GetTileOwner(c), GetRailBitsUnderBridge(c), GetRailType(tile));
 				} else {
 					uint town = IsTileOwner(c, OWNER_TOWN) ? ClosestTownFromTile(c, (uint)-1)->index : 0;
 					MakeRoadNormal(c, GetTileOwner(c), GetRoadBitsUnderBridge(c), town);
@@ -721,14 +721,14 @@
 	if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_RAIL) {
 		if (!CheckTileOwnership(tile)) return CMD_ERROR;
 
-		if (GB(_m[tile].m3, 0, 4) == totype) return CMD_ERROR;
+		if (GetRailType(tile) == totype) return CMD_ERROR;
 
 		endtile = CheckTunnelBusy(tile, &length);
 		if (endtile == INVALID_TILE) return CMD_ERROR;
 
 		if (exec) {
-			SB(_m[tile].m3, 0, 4, totype);
-			SB(_m[endtile].m3, 0, 4, totype);
+			SetRailType(tile, totype);
+			SetRailType(endtile, totype);
 			MarkTileDirtyByTile(tile);
 			MarkTileDirtyByTile(endtile);
 		}
@@ -741,11 +741,10 @@
 		if (!CheckTileOwnership(tile) || !EnsureNoVehicleZ(tile, TilePixelHeight(tile)))
 			return CMD_ERROR;
 
-		// tile is already of requested type?
-		if (GB(_m[tile].m3, 0, 4) == totype) return CMD_ERROR;
-		// change type.
+		if (GetRailType(tile) == totype) return CMD_ERROR;
+
 		if (exec) {
-			SB(_m[tile].m3, 0, 4, totype);
+			SetRailType(tile, totype);
 			MarkTileDirtyByTile(tile);
 		}
 		return _price.build_rail >> 1;
@@ -771,11 +770,11 @@
 			return CMD_ERROR;
 		}
 
-		if (GB(_m[tile].m3, 0, 4) == totype) return CMD_ERROR;
+		if (GetRailType(tile) == totype) return CMD_ERROR;
 
 		if (exec) {
-			SB(_m[tile].m3, 0, 4, totype);
-			SB(_m[endtile].m3, 0, 4, totype);
+			SetRailType(tile, totype);
+			SetRailType(endtile, totype);
 			MarkTileDirtyByTile(tile);
 			MarkTileDirtyByTile(endtile);
 		}
@@ -783,7 +782,7 @@
 		delta = TileOffsByDir(GetBridgeRampDirection(tile));
 		for (tile += delta; tile != endtile; tile += delta) {
 			if (exec) {
-				SB(_m[tile].m3, 4, 4, totype);
+				SetRailTypeOnBridge(tile, totype);
 				MarkTileDirtyByTile(tile);
 			}
 			cost += _price.build_rail >> 1;
@@ -918,7 +917,7 @@
 
 	if (IsTunnel(ti->tile)) {
 		if (GetTunnelTransportType(ti->tile) == TRANSPORT_RAIL) {
-			image = GetRailTypeInfo(GB(_m[ti->tile].m3, 0, 4))->base_sprites.tunnel;
+			image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.tunnel;
 		} else {
 			image = SPR_TUNNEL_ENTRY_REAR_ROAD;
 		}
@@ -936,9 +935,9 @@
 			RailType rt;
 
 			if (IsBridgeRamp(ti->tile)) {
-				rt = GB(_m[ti->tile].m3, 0, 3);
+				rt = GetRailType(ti->tile);
 			} else {
-				rt = GB(_m[ti->tile].m3, 4, 3);
+				rt = GetRailTypeOnBridge(ti->tile);
 			}
 
 			base_offset = GetRailTypeInfo(rt)->bridge_offset;
@@ -987,7 +986,7 @@
 				}
 
 				if (GetTransportTypeUnderBridge(ti->tile) == TRANSPORT_RAIL) {
-					const RailtypeInfo *rti = GetRailTypeInfo(GB(_m[ti->tile].m3, 0, 4));
+					const RailtypeInfo* rti = GetRailTypeInfo(GetRailType(ti->tile));
 
 					if (ti->tileh == 0) {
 						image = (axis == AXIS_X ? SPR_RAIL_TRACK_Y : SPR_RAIL_TRACK_X);
--- a/waypoint.c	Fri Mar 17 08:06:56 2006 +0000
+++ b/waypoint.c	Fri Mar 17 10:10:31 2006 +0000
@@ -213,7 +213,7 @@
 
 	if (flags & DC_EXEC) {
 		const StationSpec *spec = NULL;
-		MakeRailWaypoint(tile, GetTileOwner(tile), axis, GB(_m[tile].m3, 0, 4), wp->index);
+		MakeRailWaypoint(tile, GetTileOwner(tile), axis, GetRailType(tile), wp->index);
 		MarkTileDirtyByTile(tile);
 
 		if (GB(p1, 0, 8) < GetNumCustomStations(STAT_CLASS_WAYP))
@@ -300,7 +300,7 @@
 		RedrawWaypointSign(wp);
 
 		if (justremove) {
-			MakeRailNormal(tile, GetTileOwner(tile), GetRailWaypointBits(tile), GB(_m[tile].m3, 0, 4));
+			MakeRailNormal(tile, GetTileOwner(tile), GetRailWaypointBits(tile), GetRailType(tile));
 			MarkTileDirtyByTile(tile);
 		} else {
 			DoClearSquare(tile);