(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
authortron
Sun, 12 Mar 2006 15:04:03 +0000
changeset 3184 118a520164e4
parent 3183 90c676e6a50d
child 3185 a9d0cdff7b84
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
ai/default/default.c
ai/trolly/pathfinder.c
npf.c
pathfind.c
rail.c
rail_cmd.c
roadveh_cmd.c
smallmap_gui.c
tile.h
town_cmd.c
train_cmd.c
tunnel_map.c
tunnel_map.h
tunnelbridge_cmd.c
--- a/ai/default/default.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/ai/default/default.c	Sun Mar 12 15:04:03 2006 +0000
@@ -7,6 +7,7 @@
 #include "../../road_map.h"
 #include "../../tile.h"
 #include "../../player.h"
+#include "../../tunnel_map.h"
 #include "../../vehicle.h"
 #include "../../engine.h"
 #include "../../command.h"
@@ -2147,7 +2148,7 @@
 	TileIndex tilenew;
 
 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
-		if (!(_m[tile].m5 & 0x80)) {
+		if (IsTunnel(tile)) {
 			// Clear the tunnel and continue at the other side of it.
 			if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)))
 				return false;
--- a/ai/trolly/pathfinder.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/ai/trolly/pathfinder.c	Sun Mar 12 15:04:03 2006 +0000
@@ -43,8 +43,7 @@
 		// MP_STREET, but not a road depot?
 		(IsTileType(tile, MP_STREET) && !IsTileDepotType(tile, TRANSPORT_ROAD)) ||
 		(IsTileType(tile, MP_TUNNELBRIDGE) && (
-			// road tunnel?
-			((_m[tile].m5 & 0x80) == 0 && (_m[tile].m5 & 0x4) == 0x4) ||
+			(IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) ||
 			// road bridge?
 			((_m[tile].m5 & 0x80) != 0 && (_m[tile].m5 & 0x2) == 0x2)
 		));
@@ -232,12 +231,10 @@
 			// If the next step is a bridge, we have to enter it the right way
 			if (!PathFinderInfo->rail_or_road && IsRoad(atile)) {
 				if (IsTileType(atile, MP_TUNNELBRIDGE)) {
-					// An existing bridge... let's test the direction ;)
-					if ((_m[atile].m5 & 1U) != (i & 1)) continue;
-					// This problem only is valid for tunnels:
-					// When the last tile was not yet a tunnel, check if we enter from the right side..
-					if ((_m[atile].m5 & 0x80) == 0) {
+					if (IsTunnel(atile)) {
 						if (GetTunnelDirection(atile) != i) continue;
+					} else {
+						if ((_m[atile].m5 & 1U) != DiagDirToAxis(i)) continue;
 					}
 				}
 			}
--- a/npf.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/npf.c	Sun Mar 12 15:04:03 2006 +0000
@@ -260,11 +260,11 @@
 	/* Determine base length */
 	switch (GetTileType(tile)) {
 		case MP_TUNNELBRIDGE:
-			if (GB(_m[tile].m5, 4, 4) == 0) {
+			if (IsTunnel(tile)) {
 				cost = NPFTunnelCost(current);
-				break;
+			} else {
+				cost = NPF_TILE_LENGTH;
 			}
-			cost = NPF_TILE_LENGTH;
 			break;
 
 		case MP_STREET:
@@ -306,7 +306,7 @@
 	/* Determine base length */
 	switch (GetTileType(tile)) {
 		case MP_TUNNELBRIDGE:
-			if (GB(_m[tile].m5, 4, 4) == 0) {
+			if (IsTunnel(tile)) {
 				cost = NPFTunnelCost(current);
 				break;
 			}
@@ -483,7 +483,7 @@
 			}
 			/* if we were on a railway middle part, we are now at a railway bridge ending */
 #endif
-			if ((_m[tile].m5 & 0xFC) == 0 || /* railway tunnel */
+			if ((IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_RAIL) ||
 					(_m[tile].m5 & 0xC6) == 0x80 || /* railway bridge ending */
 					((_m[tile].m5 & 0xF8) == 0xE0 && GB(_m[tile].m5, 0, 1) != (enterdir & 0x1))) { /* railway under bridge */
 				return IsTileOwner(tile, owner);
@@ -517,9 +517,7 @@
 	DEBUG(npf, 4)("Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
 
 	/* Find dest tile */
-	if (IsTileType(src_tile, MP_TUNNELBRIDGE) &&
-			GB(_m[src_tile].m5, 4, 4) == 0 &&
-			GetTunnelDirection(src_tile) == src_exitdir) {
+	if (IsTunnelTile(src_tile) && GetTunnelDirection(src_tile) == src_exitdir) {
 		/* This is a tunnel. We know this tunnel is our type,
 		 * otherwise we wouldn't have got here. It is also facing us,
 		 * so we should skip it's body */
@@ -565,8 +563,7 @@
 	/* I can't enter a tunnel entry/exit tile from a tile above the tunnel. Note
 	 * that I can enter the tunnel from a tile below the tunnel entrance. This
 	 * solves the problem of vehicles wanting to drive off a tunnel entrance */
-	if (IsTileType(dst_tile, MP_TUNNELBRIDGE) && GB(_m[dst_tile].m5, 4, 4) == 0 &&
-			GetTileZ(dst_tile) < GetTileZ(src_tile)) {
+	if (IsTunnelTile(dst_tile) && GetTileZ(dst_tile) < GetTileZ(src_tile)) {
 		return;
 	}
 
--- a/pathfind.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/pathfind.c	Sun Mar 12 15:04:03 2006 +0000
@@ -231,8 +231,7 @@
 
 		tile = TileVirtXY(x, y);
 
-		if (IsTileType(tile, MP_TUNNELBRIDGE) &&
-				GB(_m[tile].m5, 4, 4) == 0 &&               // tunnel entrance/exit
+		if (IsTunnelTile(tile) &&
 				// GetTunnelTransportType(tile) == type &&  // rail/road-tunnel <-- This is not necesary to check, right?
 				ReverseDiagDir(GetTunnelDirection(tile)) == direction &&
 				GetSlopeZ(x + 8, y + 8) == z) {
@@ -283,7 +282,7 @@
 	RememberData rd;
 	TileIndex tile_org = tile;
 
-	if (IsTileType(tile, MP_TUNNELBRIDGE) && GB(_m[tile].m5, 4, 4) == 0) {
+	if (IsTunnelTile(tile)) {
 		if (GetTunnelDirection(tile) != direction ||
 				GetTunnelTransportType(tile) != tpf->tracktype) {
 			return;
@@ -716,21 +715,18 @@
 start_at:
 		// If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
 		//   need to find the exit of the tunnel.
-		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
-			if (GB(_m[tile].m5, 4, 4) == 0 &&
-					GetTunnelDirection(tile) != ReverseDiagDir(direction)) {
-				/* This is a tunnel tile */
-				/* We are not just driving out of the tunnel */
-				if (GetTunnelDirection(tile) != direction ||
-						GetTunnelTransportType(tile) != tpf->tracktype) {
-					// We are not driving into the tunnel, or it is an invalid tunnel
-					continue;
-				}
-				flotr = FindLengthOfTunnel(tile, direction);
-				si.cur_length += flotr.length * DIAG_FACTOR;
-				tile = flotr.tile;
-				// tile now points to the exit tile of the tunnel
+		if (IsTunnelTile(tile) &&
+				GetTunnelDirection(tile) != ReverseDiagDir(direction)) {
+			/* We are not just driving out of the tunnel */
+			if (GetTunnelDirection(tile) != direction ||
+					GetTunnelTransportType(tile) != tpf->tracktype) {
+				// We are not driving into the tunnel, or it is an invalid tunnel
+				continue;
 			}
+			flotr = FindLengthOfTunnel(tile, direction);
+			si.cur_length += flotr.length * DIAG_FACTOR;
+			tile = flotr.tile;
+			// tile now points to the exit tile of the tunnel
 		}
 
 		// This is a special loop used to go through
--- a/rail.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/rail.c	Sun Mar 12 15:04:03 2006 +0000
@@ -4,6 +4,7 @@
 #include "openttd.h"
 #include "rail.h"
 #include "station.h"
+#include "tunnel_map.h"
 
 /* XXX: Below 3 tables store duplicate data. Maybe remove some? */
 /* Maps a trackdir to the bit that stores its status in the map arrays, in the
@@ -123,8 +124,9 @@
 				type = _m[tile].m3 & RAILTYPE_MASK;
 			break;
 		case MP_TUNNELBRIDGE:
-			/* railway tunnel */
-			if ((_m[tile].m5 & 0xFC) == 0) type = _m[tile].m3 & RAILTYPE_MASK;
+			if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_RAIL) {
+				return _m[tile].m3 & RAILTYPE_MASK;
+			}
 			/* railway bridge ending */
 			if ((_m[tile].m5 & 0xC6) == 0x80) type = _m[tile].m3 & RAILTYPE_MASK;
 			/* on railway bridge */
--- a/rail_cmd.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/rail_cmd.c	Sun Mar 12 15:04:03 2006 +0000
@@ -1625,7 +1625,7 @@
 	 * is some kind of invisible black hole, and there is some special magic going
 	 * on in there. This 'workaround' can be removed once the maprewrite is done.
 	 */
-	if (IsTileType(tile, MP_TUNNELBRIDGE) && GB(_m[tile].m5, 4, 4) == 0) {
+	if (IsTunnelTile(tile)) {
 		// It is a tunnel we're checking, we need to do some special stuff
 		// because VehicleFromPos will not find the vihicle otherwise
 		TileIndex end = GetOtherTunnelEnd(tile);
--- a/roadveh_cmd.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/roadveh_cmd.c	Sun Mar 12 15:04:03 2006 +0000
@@ -18,6 +18,7 @@
 #include "player.h"
 #include "sound.h"
 #include "depot.h"
+#include "tunnel_map.h"
 #include "vehicle_gui.h"
 #include "newgrf_engine.h"
 
@@ -1254,8 +1255,7 @@
 			return;
 		}
 
-		if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) &&
-				GB(_m[gp.new_tile].m5, 4, 4) == 0 &&
+		if (IsTunnelTile(gp.new_tile) &&
 				VehicleEnterTile(v, gp.new_tile, gp.x, gp.y) & 4) {
 			//new_dir = RoadGetNewDirection(v, gp.x, gp.y)
 			v->cur_image = GetRoadVehImage(v, v->direction);
--- a/smallmap_gui.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/smallmap_gui.c	Sun Mar 12 15:04:03 2006 +0000
@@ -348,10 +348,10 @@
 	if (t == MP_TUNNELBRIDGE) {
 		TransportType tt;
 
-		if (_m[tile].m5 & 0x80) {
+		if (IsTunnel(tile)) {
+			tt = GetTunnelTransportType(tile);
+		} else {
 			tt = GB(_m[tile].m5, 1, 2);
-		} else {
-			tt = GetTunnelTransportType(tile);
 		}
 		switch (tt) {
 			case TRANSPORT_RAIL: t = MP_RAILWAY; break;
--- a/tile.h	Sun Mar 12 12:19:25 2006 +0000
+++ b/tile.h	Sun Mar 12 15:04:03 2006 +0000
@@ -79,10 +79,6 @@
 	return GetTileType(tile) == type;
 }
 
-static inline bool IsTunnelTile(TileIndex tile)
-{
-	return IsTileType(tile, MP_TUNNELBRIDGE) && GB(_m[tile].m5, 4, 4) == 0;
-}
 
 static inline Owner GetTileOwner(TileIndex tile)
 {
--- a/town_cmd.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/town_cmd.c	Sun Mar 12 15:04:03 2006 +0000
@@ -624,9 +624,7 @@
 		int i;
 
 		// Reached a tunnel? Then continue at the other side of it.
-		if (IsTileType(tile, MP_TUNNELBRIDGE) &&
-				GB(_m[tile].m5, 4, 4) == 0 &&
-				GetTunnelTransportType(tile) == TRANSPORT_ROAD) {
+		if (IsTunnelTile(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) {
 			*tile_ptr = GetOtherTunnelEnd(tile);
 			return;
 		}
--- a/train_cmd.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/train_cmd.c	Sun Mar 12 15:04:03 2006 +0000
@@ -2481,8 +2481,9 @@
 			TileIndex tile = TileVirtXY(v->x_pos, v->y_pos);
 
 			// XXX workaround, whole UP/DOWN detection needs overhaul
-			if (!IsTileType(tile, MP_TUNNELBRIDGE) || (_m[tile].m5 & 0x80) != 0)
+			if (!IsTunnelTile(tile)) {
 				SETBIT(v->u.rail.flags, (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
+			}
 		}
 	}
 
--- a/tunnel_map.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/tunnel_map.c	Sun Mar 12 15:04:03 2006 +0000
@@ -15,8 +15,7 @@
 	do {
 		tile += delta;
 	} while (
-		!IsTileType(tile, MP_TUNNELBRIDGE) ||
-		GB(_m[tile].m5, 4, 4) != 0 ||
+		!IsTunnelTile(tile) ||
 		GetTunnelDirection(tile) != dir ||
 		GetTileZ(tile) != z
 	);
@@ -37,8 +36,7 @@
 
 	return
 		z == height &&
-		IsTileType(tile, MP_TUNNELBRIDGE) &&
-		GB(_m[tile].m5, 4, 4) == 0 &&
+		IsTunnelTile(tile) &&
 		GetTunnelDirection(tile) == dir;
 }
 
--- a/tunnel_map.h	Sun Mar 12 12:19:25 2006 +0000
+++ b/tunnel_map.h	Sun Mar 12 15:04:03 2006 +0000
@@ -9,6 +9,18 @@
 #include "rail.h"
 
 
+static inline bool IsTunnel(TileIndex t)
+{
+	return !HASBIT(_m[t].m5, 7);
+}
+
+
+static inline bool IsTunnelTile(TileIndex t)
+{
+	return IsTileType(t, MP_TUNNELBRIDGE) && IsTunnel(t);
+}
+
+
 static inline DiagDirection GetTunnelDirection(TileIndex t)
 {
 	return (DiagDirection)GB(_m[t].m5, 0, 2);
--- a/tunnelbridge_cmd.c	Sun Mar 12 12:19:25 2006 +0000
+++ b/tunnelbridge_cmd.c	Sun Mar 12 15:04:03 2006 +0000
@@ -525,8 +525,7 @@
 		tile += delta;
 		len++;
 	} while (
-		!IsTileType(tile, MP_TUNNELBRIDGE) ||
-		GB(_m[tile].m5, 4, 4) != 0 ||
+		!IsTunnelTile(tile) ||
 		ReverseDiagDir(GetTunnelDirection(tile)) != dir ||
 		GetTileZ(tile) != z
 	);
@@ -742,7 +741,7 @@
 {
 	byte m5 = _m[tile].m5;
 
-	if ((m5 & 0xF0) == 0) {
+	if (IsTunnel(tile)) {
 		if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
 		return DoClearTunnel(tile, flags);
 	} else if (m5 & 0x80) {
@@ -759,8 +758,7 @@
 	uint length;
 	Vehicle *v;
 
-	if ((_m[tile].m5 & 0xFC) == 0x00) {
-		// railway tunnel
+	if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_RAIL) {
 		if (!CheckTileOwnership(tile)) return CMD_ERROR;
 
 		if (GB(_m[tile].m3, 0, 4) == totype) return CMD_ERROR;
@@ -962,7 +960,7 @@
 	bool ice = _m[ti->tile].m4 & 0x80;
 
 	// draw tunnel?
-	if ((ti->map5 & 0xF0) == 0) {
+	if (IsTunnel(ti->tile)) {
 		if (GetTunnelTransportType(ti->tile) == TRANSPORT_RAIL) {
 			image = GetRailTypeInfo(GB(_m[ti->tile].m3, 0, 4))->base_sprites.tunnel;
 		} else {
@@ -1229,7 +1227,7 @@
 
 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
 {
-	if ((_m[tile].m5 & 0x80) == 0) {
+	if (IsTunnel(tile)) {
 		td->str = (GetTunnelTransportType(tile) == TRANSPORT_RAIL) ?
 			STR_5017_RAILROAD_TUNNEL : STR_5018_ROAD_TUNNEL;
 	} else {
@@ -1291,8 +1289,7 @@
 	uint32 result;
 	byte m5 = _m[tile].m5;
 
-	if ((m5 & 0xF0) == 0) {
-		/* This is a tunnel */
+	if (IsTunnel(tile)) {
 		if (GetTunnelTransportType(tile) == mode) {
 			return DiagDirToAxis(GetTunnelDirection(tile)) == AXIS_X ? 0x101 : 0x202;
 		}
@@ -1371,7 +1368,7 @@
 
 static uint32 VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
 {
-	if (GB(_m[tile].m5, 4, 4) == 0) {
+	if (IsTunnel(tile)) {
 		int z = GetSlopeZ(x, y) - v->z_pos;
 		byte fc;
 		DiagDirection dir;
@@ -1463,9 +1460,7 @@
 	byte z = v->z_pos;
 
 	for (tile = v->tile;; tile += delta) {
-		if (IsTileType(tile, MP_TUNNELBRIDGE) && GB(_m[tile].m5, 4, 4) == 0 &&
-				GetTileZ(tile) == z)
-			break;
+		if (IsTunnelTile(tile) && GetTileZ(tile) == z) break;
 	}
 	return tile;
 }