(svn r3857) Add and use GetBridgeRampDirection()
authortron
Tue, 14 Mar 2006 12:00:11 +0000
changeset 3196 5cec26c5ab75
parent 3195 3b9b4a034c69
child 3197 078a10118842
(svn r3857) Add and use GetBridgeRampDirection()
Note: This slightly changes the behavior of GetAnyRoadBits() to only return a road piece for the start of the bridge ramp instead of a full ROAD_[XY]
ai/default/default.c
bridge_map.h
road_map.c
road_map.h
tunnelbridge_cmd.c
--- a/ai/default/default.c	Tue Mar 14 09:00:10 2006 +0000
+++ b/ai/default/default.c	Tue Mar 14 12:00:11 2006 +0000
@@ -2,6 +2,7 @@
 
 #include "../../stdafx.h"
 #include "../../openttd.h"
+#include "../../bridge_map.h"
 #include "../../functions.h"
 #include "../../map.h"
 #include "../../rail_map.h"
@@ -2161,7 +2162,7 @@
 
 			// Check if the bridge points in the right direction.
 			// This is not really needed the first place AiRemoveTileAndGoForward is called.
-			if ((_m[tile].m5 & 1) != (p->ai.cur_dir_a & 1)) return false;
+			if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1U)) return false;
 
 			// Find other side of bridge.
 			offs = TileOffsByDir(p->ai.cur_dir_a);
@@ -3673,18 +3674,18 @@
 				CMD_REMOVE_ROAD);
 		}
 	} else if (IsTileType(tile, MP_TUNNELBRIDGE)) {
-		byte b;
-
 		if (!IsTileOwner(tile, _current_player) || (_m[tile].m5 & 0xC6) != 0x80)
 			return;
 
 		m5 = 0;
 
-		b = _m[tile].m5 & 0x21;
-		if (b == 0) goto pos_0;
-		if (b == 1) goto pos_3;
-		if (b == 0x20) goto pos_2;
-		goto pos_1;
+		switch (GetBridgeRampDirection(tile)) {
+			default:
+			case DIAGDIR_NE: goto pos_2;
+			case DIAGDIR_SE: goto pos_3;
+			case DIAGDIR_SW: goto pos_0;
+			case DIAGDIR_NW: goto pos_1;
+		}
 	}
 }
 
--- a/bridge_map.h	Tue Mar 14 09:00:10 2006 +0000
+++ b/bridge_map.h	Tue Mar 14 12:00:11 2006 +0000
@@ -3,12 +3,25 @@
 #ifndef BRIDGE_MAP_H
 #define BRIDGE_MAP_H
 
+#include "direction.h"
 #include "macros.h"
 #include "map.h"
 #include "rail.h"
 #include "tile.h"
 
 
+/**
+ * Get the direction pointing onto the bridge
+ */
+static inline DiagDirection GetBridgeRampDirection(TileIndex t)
+{
+	/* Heavy wizardry to convert the X/Y (bit 0) + N/S (bit 5) encoding of
+	 * bridges to a DiagDirection
+	 */
+	return (DiagDirection)((6 - (_m[t].m5 >> 4 & 2) - (_m[t].m5 & 1)) % 4);
+}
+
+
 static inline void SetClearUnderBridge(TileIndex t)
 {
 	SetTileOwner(t, OWNER_NONE);
--- a/road_map.c	Tue Mar 14 09:00:10 2006 +0000
+++ b/road_map.c	Tue Mar 14 12:00:11 2006 +0000
@@ -2,6 +2,7 @@
 
 #include "stdafx.h"
 #include "openttd.h"
+#include "bridge_map.h"
 #include "functions.h"
 #include "road_map.h"
 #include "station.h"
@@ -33,7 +34,7 @@
 				} 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;
+					return DiagDirToRoadBits(ReverseDiagDir(GetBridgeRampDirection(tile)));
 				}
 			} else {
 				// tunnel
--- a/road_map.h	Tue Mar 14 09:00:10 2006 +0000
+++ b/road_map.h	Tue Mar 14 12:00:11 2006 +0000
@@ -73,7 +73,7 @@
  * 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 ramps: start of the ramp is treated as road piece
  * - bridge middle parts: bridge itself is ignored
  */
 RoadBits GetAnyRoadBits(TileIndex);
--- a/tunnelbridge_cmd.c	Tue Mar 14 09:00:10 2006 +0000
+++ b/tunnelbridge_cmd.c	Tue Mar 14 12:00:11 2006 +0000
@@ -1019,10 +1019,9 @@
 				if (f) DrawFoundation(ti, f);
 			}
 
-			/* Cope for the direction of the bridge */
-			if (HASBIT(ti->map5, 0)) base_offset++;
+			// HACK Wizardry to convert the bridge ramp direction into a sprite offset
+			base_offset += (6 - GetBridgeRampDirection(ti->tile)) % 4;
 
-			if (ti->map5 & 0x20) base_offset += 2; // which side
 			if (ti->tileh == 0) base_offset += 4; // sloped bridge head
 
 			/* Table number 6 always refers to the bridge heads for any bridge type */