(svn r3766) Add a function to get the RoadBits from an arbitrary tile
authortron
Sun, 05 Mar 2006 12:22:20 +0000
changeset 3146 36523d434783
parent 3145 349b745dfbf4
child 3147 0a09ce6d651a
(svn r3766) Add a function to get the RoadBits from an arbitrary tile
Makefile
road_cmd.c
road_map.c
road_map.h
station.h
tile.h
--- a/Makefile	Sun Mar 05 11:18:34 2006 +0000
+++ b/Makefile	Sun Mar 05 12:22:20 2006 +0000
@@ -672,6 +672,7 @@
 SRCS += rev.c
 SRCS += road_cmd.c
 SRCS += road_gui.c
+SRCS += road_map.c
 SRCS += roadveh_cmd.c
 SRCS += roadveh_gui.c
 SRCS += saveload.c
--- a/road_cmd.c	Sun Mar 05 11:18:34 2006 +0000
+++ b/road_cmd.c	Sun Mar 05 12:22:20 2006 +0000
@@ -27,38 +27,6 @@
 void RoadVehEnterDepot(Vehicle *v);
 
 
-static bool HasTileRoadAt(TileIndex tile, int i)
-{
-	RoadBits b;
-
-	switch (GetTileType(tile)) {
-		case MP_STREET:
-			switch (GetRoadType(tile)) {
-				case ROAD_NORMAL:   b = GetRoadBits(tile); break;
-				case ROAD_CROSSING: b = GetCrossingRoadBits(tile); break;
-				case ROAD_DEPOT:    return (~_m[tile].m5 & 3) == i;
-				default:            return false;
-			}
-			break;
-
-		case MP_STATION:
-			return
-				IS_BYTE_INSIDE(_m[tile].m5, 0x43, 0x43 + 8) &&
-				(~(_m[tile].m5 - 0x43) & 3) == i;
-
-		case MP_TUNNELBRIDGE:
-			// bail out, if not a bridge middle part with road underneath
-			if ((_m[tile].m5 & 0xF8) != 0xE8) return false;
-			// road direction perpendicular to bridge
-			b = (_m[tile].m5 & 0x01) ? ROAD_X : ROAD_Y;
-
-		default:
-			return false;
-	}
-
-	return HASBIT(b, i);
-}
-
 static bool CheckAllowRemoveRoad(TileIndex tile, uint br, bool *edge_road)
 {
 	int blocks;
@@ -90,10 +58,10 @@
 
 	// Get a bitmask of which neighbouring roads has a tile
 	n = 0;
-	if (blocks&0x25 && HasTileRoadAt(TILE_ADDXY(tile,-1, 0), 1)) n |= 8;
-	if (blocks&0x2A && HasTileRoadAt(TILE_ADDXY(tile, 0, 1), 0)) n |= 4;
-	if (blocks&0x19 && HasTileRoadAt(TILE_ADDXY(tile, 1, 0), 3)) n |= 2;
-	if (blocks&0x16 && HasTileRoadAt(TILE_ADDXY(tile, 0,-1), 2)) n |= 1;
+	if (blocks & 0x25 && GetAnyRoadBits(TILE_ADDXY(tile,-1, 0)) & ROAD_SW) n |= 8;
+	if (blocks & 0x2A && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1)) & ROAD_NW) n |= 4;
+	if (blocks & 0x19 && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0)) & ROAD_NE) n |= 2;
+	if (blocks & 0x16 && GetAnyRoadBits(TILE_ADDXY(tile, 0,-1)) & ROAD_SE) n |= 1;
 
 	// If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
 	// then allow it
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/road_map.c	Sun Mar 05 12:22:20 2006 +0000
@@ -0,0 +1,44 @@
+/* $Id$ */
+
+#include "stdafx.h"
+#include "openttd.h"
+#include "road_map.h"
+#include "station.h"
+
+
+RoadBits GetAnyRoadBits(TileIndex tile)
+{
+	switch (GetTileType(tile)) {
+		case MP_STREET:
+			switch (GetRoadType(tile)) {
+				default:
+				case ROAD_NORMAL:   return GetRoadBits(tile);
+				case ROAD_CROSSING: return GetCrossingRoadBits(tile);
+				case ROAD_DEPOT:    return DiagDirToRoadBits(GB(_m[tile].m5, 0, 2));
+			}
+
+		case MP_STATION:
+			if (!IsRoadStationTile(tile)) return 0;
+			return DiagDirToRoadBits(GetRoadStationDir(tile));
+
+		case MP_TUNNELBRIDGE:
+			if (_m[tile].m5 & 0x80) {
+				// bridge
+				if (_m[tile].m5 & 0x40) {
+					// middle part
+					if ((_m[tile].m5 & 0x38) != 0x28) return 0; // no road under bridge
+					return _m[tile].m5 & 1 ? ROAD_X : ROAD_Y;
+				} else {
+					// ending
+					if (GB(_m[tile].m5, 1, 2) != TRANSPORT_ROAD) return 0; // not a road bridge
+					return _m[tile].m5 & 1 ? ROAD_Y : ROAD_X;
+				}
+			} else {
+				// tunnel
+				if (GB(_m[tile].m5, 2, 2) != TRANSPORT_ROAD) return 0; // not a road tunnel
+				return DiagDirToRoadBits(ReverseDiagDir(GB(_m[tile].m5, 0, 2)));
+			}
+
+		default: return 0;
+	}
+}
--- a/road_map.h	Sun Mar 05 11:18:34 2006 +0000
+++ b/road_map.h	Sun Mar 05 12:22:20 2006 +0000
@@ -22,6 +22,12 @@
 	return ROAD_ALL ^ r;
 }
 
+static inline RoadBits DiagDirToRoadBits(DiagDirection d)
+{
+	return 1 << (3 ^ d);
+}
+
+
 static inline RoadBits GetRoadBits(TileIndex tile)
 {
 	return GB(_m[tile].m5, 0, 4);
@@ -50,6 +56,17 @@
 }
 
 
+/**
+ * Returns the RoadBits on an arbitrary tile
+ * Special behavior:
+ * - road depots: entrance is treated as road piece
+ * - road tunnels: entrance is treated as road piece
+ * - bridge ramps: treated as straight road
+ * - bridge middle parts: bridge itself is ignored
+ */
+RoadBits GetAnyRoadBits(TileIndex);
+
+
 static inline void MakeRoadNormal(TileIndex t, Owner owner, RoadBits bits, uint town)
 {
 	SetTileType(t, MP_STREET);
--- a/station.h	Sun Mar 05 11:18:34 2006 +0000
+++ b/station.h	Sun Mar 05 12:22:20 2006 +0000
@@ -244,7 +244,7 @@
 
 /* Get's the direction the station exit points towards. Ie, returns 0 for a
  * station with the exit NE. */
-static inline byte GetRoadStationDir(TileIndex tile)
+static inline DiagDirection GetRoadStationDir(TileIndex tile)
 {
 	assert(IsRoadStationTile(tile));
 	return (_m[tile].m5 - 0x43) & 3;
--- a/tile.h	Sun Mar 05 11:18:34 2006 +0000
+++ b/tile.h	Sun Mar 05 12:22:20 2006 +0000
@@ -44,6 +44,12 @@
 	INVALID_DIAGDIR = 0xFF,
 } DiagDirection;
 
+static inline DiagDirection ReverseDiagDir(DiagDirection d)
+{
+	return 2 ^ d;
+}
+
+
 /* the 2 axis */
 typedef enum Axis {
 	AXIS_X = 0,