changeset 8398:a6304b304752 draft

(svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
author smatz <smatz@openttd.org>
date Wed, 23 Jan 2008 22:34:04 +0000
parents 9134575844ba
children 48103a2817a5
files projects/openttd.vcproj projects/openttd_vs80.vcproj projects/openttd_vs90.vcproj source.list src/ai/default/default.cpp src/ai/trolly/build.cpp src/ai/trolly/pathfinder.cpp src/bridge.h src/bridge_gui.cpp src/elrail.cpp src/npf.cpp src/pathfind.cpp src/pathfind.h src/rail_cmd.cpp src/road_cmd.cpp src/tunnelbridge.h src/tunnelbridge_cmd.cpp src/yapf/follow_track.hpp src/yapf/yapf.hpp
diffstat 19 files changed, 90 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- a/projects/openttd.vcproj
+++ b/projects/openttd.vcproj
@@ -709,6 +709,9 @@
 				RelativePath=".\..\src\transparency_gui.h">
 			</File>
 			<File
+				RelativePath=".\..\src\tunnelbridge.h">
+			</File>
+			<File
 				RelativePath=".\..\src\variables.h">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj
+++ b/projects/openttd_vs80.vcproj
@@ -1176,6 +1176,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\tunnelbridge.h"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\variables.h"
 				>
 			</File>
--- a/projects/openttd_vs90.vcproj
+++ b/projects/openttd_vs90.vcproj
@@ -1173,6 +1173,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\tunnelbridge.h"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\variables.h"
 				>
 			</File>
--- a/source.list
+++ b/source.list
@@ -202,6 +202,7 @@
 train.h
 transparency.h
 transparency_gui.h
+tunnelbridge.h
 variables.h
 vehicle.h
 vehicle_gui.h
--- a/src/ai/default/default.cpp
+++ b/src/ai/default/default.cpp
@@ -31,6 +31,7 @@
 #include "../../player_base.h"
 #include "../../settings_type.h"
 #include "default.h"
+#include "../../tunnelbridge.h"
 
 #include "../../table/ai_rail.h"
 
@@ -2173,7 +2174,7 @@
 
 	if (arf.best_ptr[0] & 0x80) {
 		int i;
-		int32 bridge_len = GetBridgeLength(arf.bridge_end_tile, _players_ai[p->index].cur_tile_a);
+		int32 bridge_len = GetTunnelBridgeLength(arf.bridge_end_tile, _players_ai[p->index].cur_tile_a);
 
 		/* Figure out which (rail)bridge type to build
 		 * start with best bridge, then go down to worse and worse bridges
@@ -3065,7 +3066,7 @@
 		int i;
 		int32 bridge_len;
 		_players_ai[p->index].cur_tile_a = arf.bridge_end_tile;
-		bridge_len = GetBridgeLength(tile, _players_ai[p->index].cur_tile_a); // tile
+		bridge_len = GetTunnelBridgeLength(tile, _players_ai[p->index].cur_tile_a); // tile
 
 		/* Figure out what (road)bridge type to build
 		 * start with best bridge, then go down to worse and worse bridges
--- a/src/ai/trolly/build.cpp
+++ b/src/ai/trolly/build.cpp
@@ -15,6 +15,8 @@
 #include "../../player_base.h"
 #include "../../player_func.h"
 #include "../ai.h"
+#include "../../tunnelbridge.h"
+
 
 // Build HQ
 //  Params:
@@ -58,7 +60,7 @@
 	int bridge_type, bridge_len, type, type2;
 
 	// Find a good bridgetype (the best money can buy)
-	bridge_len = GetBridgeLength(tile_a, tile_b);
+	bridge_len = GetTunnelBridgeLength(tile_a, tile_b);
 	type = type2 = 0;
 	for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) {
 		if (CheckBridge_Stuff(bridge_type, bridge_len)) {
--- a/src/ai/trolly/pathfinder.cpp
+++ b/src/ai/trolly/pathfinder.cpp
@@ -14,6 +14,7 @@
 #include "../../variables.h"
 #include "../../player_base.h"
 #include "../../player_func.h"
+#include "../../tunnelbridge.h"
 
 
 #define TEST_STATION_NO_DIR 0xFF
@@ -320,7 +321,7 @@
 				new_tile += TileOffsByDiagDir(dir);
 
 				// Precheck, is the length allowed?
-				if (!CheckBridge_Stuff(0, GetBridgeLength(tile, new_tile))) break;
+				if (!CheckBridge_Stuff(0, GetTunnelBridgeLength(tile, new_tile))) break;
 
 				// Check if we hit the station-tile.. we don't like that!
 				if (TILES_BETWEEN(new_tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) break;
@@ -425,14 +426,14 @@
 		int r;
 		// Tunnels are very expensive when build on long routes..
 		// Ironicly, we are using BridgeCode here ;)
-		r = AI_PATHFINDER_TUNNEL_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile);
+		r = AI_PATHFINDER_TUNNEL_PENALTY * GetTunnelBridgeLength(current->tile, parent->path.node.tile);
 		res += r + (r >> 8);
 	}
 
 	// Are we part of a bridge?
 	if ((AI_PATHFINDER_FLAG_BRIDGE & current->user_data[0]) != 0) {
 		// That means for every length a penalty
-		res += AI_PATHFINDER_BRIDGE_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile);
+		res += AI_PATHFINDER_BRIDGE_PENALTY * GetTunnelBridgeLength(current->tile, parent->path.node.tile);
 		// Check if we are going up or down, first for the starting point
 		// In user_data[0] is at the 8th bit the direction
 		if (!HasBridgeFlatRamp(parent_tileh, (Axis)((current->user_data[0] >> 8) & 1))) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
--- a/src/bridge.h
+++ b/src/bridge.h
@@ -43,7 +43,6 @@
 void DrawBridgeMiddle(const TileInfo *ti);
 
 bool CheckBridge_Stuff(byte bridge_type, uint bridge_len);
-uint32 GetBridgeLength(TileIndex begin, TileIndex end);
 int CalcBridgeLenCostFactor(int x);
 
 #endif /* BRIDGE_H */
--- a/src/bridge_gui.cpp
+++ b/src/bridge_gui.cpp
@@ -16,6 +16,7 @@
 #include "map_func.h"
 #include "viewport_func.h"
 #include "gfx_func.h"
+#include "tunnelbridge.h"
 
 #include "table/strings.h"
 
@@ -182,7 +183,7 @@
 		/* check which bridges can be built
 		 * get absolute bridge length
 		 * length of the middle parts of the bridge */
-		const uint bridge_len = GetBridgeLength(start, end);
+		const uint bridge_len = GetTunnelBridgeLength(start, end);
 		/* total length of bridge */
 		const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
 
--- a/src/elrail.cpp
+++ b/src/elrail.cpp
@@ -64,6 +64,7 @@
 #include "tunnelbridge_map.h"
 #include "vehicle_func.h"
 #include "player_base.h"
+#include "tunnelbridge.h"
 
 #include "table/sprites.h"
 #include "table/elrail_data.h"
@@ -383,8 +384,8 @@
 	TileIndex end = GetSouthernBridgeEnd(ti->tile);
 	TileIndex start = GetOtherBridgeEnd(end);
 
-	uint length = GetBridgeLength(start, end);
-	uint num = DistanceMax(ti->tile, start);
+	uint length = GetTunnelBridgeLength(start, end);
+	uint num = GetTunnelBridgeLength(ti->tile, start) + 1;
 	uint height;
 
 	const SortableSpriteStruct *sss;
@@ -421,7 +422,7 @@
 	}
 
 	/* need a pylon on the southern end of the bridge */
-	if (DistanceMax(ti->tile, start) == length) {
+	if (GetTunnelBridgeLength(ti->tile, start) + 1 == length) {
 		DiagDirection PCPpos = (axis == AXIS_X ? DIAGDIR_SW : DIAGDIR_SE);
 		Direction PPPpos = (axis == AXIS_X ? DIR_NW : DIR_NE);
 		if (HasBit(tlg, (axis == AXIS_X ? 0 : 1))) PPPpos = ReverseDir(PPPpos);
--- a/src/npf.cpp
+++ b/src/npf.cpp
@@ -22,6 +22,7 @@
 #include "functions.h"
 #include "vehicle_base.h"
 #include "settings_type.h"
+#include "tunnelbridge.h"
 
 static AyStar _npf_aystar;
 
@@ -176,9 +177,7 @@
 	if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
 		/* We just popped out if this tunnel, since were
 		 * facing the tunnel exit */
-		FindLengthOfTunnelResult flotr;
-		flotr = FindLengthOfTunnel(tile, ReverseDiagDir(exitdir));
-		return flotr.length * NPF_TILE_LENGTH;
+		return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
 		/* @todo: Penalty for tunnels? */
 	} else {
 		/* We are entering the tunnel, the enter tile is just a
@@ -189,7 +188,7 @@
 
 static inline uint NPFBridgeCost(AyStarNode *current)
 {
-	return NPF_TILE_LENGTH * GetBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
+	return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
 }
 
 static uint NPFSlopeCost(AyStarNode* current)
--- a/src/pathfind.cpp
+++ b/src/pathfind.cpp
@@ -17,6 +17,7 @@
 #include "depot.h"
 #include "tunnelbridge_map.h"
 #include "core/random_func.hpp"
+#include "tunnelbridge.h"
 
 /* remember which tiles we have already visited so we don't visit them again. */
 static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
@@ -192,32 +193,6 @@
 
 }
 
-
-/* Returns the end tile and the length of a tunnel. The length does not
- * include the starting tile (entry), it does include the end tile (exit).
- */
-FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection dir)
-{
-	TileIndexDiff delta = TileOffsByDiagDir(dir);
-	uint z = GetTileZ(tile);
-	FindLengthOfTunnelResult flotr;
-
-	flotr.length = 0;
-
-	dir = ReverseDiagDir(dir);
-	do {
-		flotr.length++;
-		tile += delta;
-	} while(
-		!IsTunnelTile(tile) ||
-		GetTunnelBridgeDirection(tile) != dir ||
-		GetTileZ(tile) != z
-	);
-
-	flotr.tile = tile;
-	return flotr;
-}
-
 /**
  * Checks if any vehicle can enter/leave tile in given diagdir
  * Checks only for rail/road depots and road non-drivethrough stations
@@ -257,7 +232,7 @@
 		if (dir == direction) {
 			TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
 
-			tpf->rd.cur_length += DistanceManhattan(tile, endtile);
+			tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1;
 
 			TPFSetTileBit(tpf, tile, 14);
 			TPFSetTileBit(tpf, endtile, 14);
@@ -665,8 +640,6 @@
 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 			if (IsTunnel(tile)) {
 				if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) {
-					FindLengthOfTunnelResult flotr;
-
 					/* We are not just driving out of the tunnel */
 					if (GetTunnelBridgeDirection(tile) != direction ||
 							GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
@@ -677,9 +650,10 @@
 						bits = TRACK_BIT_NONE;
 						break;
 					}
-					flotr = FindLengthOfTunnel(tile, direction);
-					si.cur_length += flotr.length * DIAG_FACTOR;
-					tile = flotr.tile;
+
+					TileIndex endtile = GetOtherTunnelEnd(tile);
+					si.cur_length += DIAG_FACTOR * (GetTunnelBridgeLength(tile, endtile) + 1);
+					tile = endtile;
 					/* tile now points to the exit tile of the tunnel */
 				}
 			} else { // IsBridge(tile)
@@ -693,7 +667,7 @@
 					}
 				}
 				tile_end = GetOtherBridgeEnd(tile);
-				si.cur_length += DistanceManhattan(tile, tile_end) * DIAG_FACTOR;
+				si.cur_length += DIAG_FACTOR * (GetTunnelBridgeLength(tile, tile_end) + 1);
 				tile = tile_end;
 			}
 		}
--- a/src/pathfind.h
+++ b/src/pathfind.h
@@ -68,13 +68,6 @@
 };
 
 void FollowTrack(TileIndex tile, uint16 flags, uint sub_type, DiagDirection direction, TPFEnumProc* enum_proc, TPFAfterProc* after_proc, void* data);
-
-struct FindLengthOfTunnelResult {
-	TileIndex tile;
-	int length;
-};
-FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection direction);
-
 void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data);
 
 #endif /* PATHFIND_H */
--- a/src/rail_cmd.cpp
+++ b/src/rail_cmd.cpp
@@ -40,6 +40,7 @@
 #include "vehicle_func.h"
 #include "sound_func.h"
 #include "signal_func.h"
+#include "tunnelbridge.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -939,7 +940,7 @@
 			 * note that tile is a parameter by reference, so it must be updated */
 			tile = GetOtherTunnelBridgeEnd(tile);
 
-			signal_ctr += 2 + DistanceMax(orig_tile, tile) * 2;
+			signal_ctr += (GetTunnelBridgeLength(orig_tile, tile) + 2) * 2;
 			return true;
 		}
 
@@ -1294,7 +1295,7 @@
 						}
 					}
 
-					cost.AddCost((DistanceManhattan(tile, endtile) + 1) * RailConvertCost(type, totype));
+					cost.AddCost((GetTunnelBridgeLength(tile, endtile) + 2) * RailConvertCost(type, totype));
 				} break;
 
 				default: // MP_STATION, MP_ROAD
--- a/src/road_cmd.cpp
+++ b/src/road_cmd.cpp
@@ -33,6 +33,7 @@
 #include "vehicle_base.h"
 #include "sound_func.h"
 #include "road_func.h"
+#include "tunnelbridge.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -159,7 +160,7 @@
 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 			TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
 			/* Pay for *every* tile of the bridge or tunnel */
-			cost.AddCost((DistanceManhattan(other_end, tile) + 1) * _price.remove_road);
+			cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price.remove_road);
 			if (flags & DC_EXEC) {
 				SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt));
 				SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
@@ -551,7 +552,7 @@
 	cost.AddCost(CountBits(pieces) * _price.build_road);
 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 		/* Pay for *every* tile of the bridge or tunnel */
-		cost.MultiplyCost(DistanceManhattan(GetOtherTunnelBridgeEnd(tile), tile) + 1);
+		cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
 	}
 
 	if (flags & DC_EXEC) {
new file mode 100644
--- /dev/null
+++ b/src/tunnelbridge.h
@@ -0,0 +1,26 @@
+/* $Id$ */
+
+/** @file tunnelbridge.h Header file for things common for tunnels and bridges */
+
+#ifndef TUNNELBRIDGE_H
+#define TUNNELBRIDGE_H
+
+#include "tile_type.h"
+#include "map_func.h"
+#include "tunnelbridge_map.h"
+
+/**
+ * Calculates the length of a tunnel or a bridge (without end tiles)
+ * @return length of bridge/tunnel middle
+ */
+static inline uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
+{
+	int x1 = TileX(begin);
+	int y1 = TileY(begin);
+	int x2 = TileX(end);
+	int y2 = TileY(end);
+
+	return abs(x2 + y2 - x1 - y1) - 1;
+}
+
+#endif /* TUNNELBRIDGE_H */
--- a/src/tunnelbridge_cmd.cpp
+++ b/src/tunnelbridge_cmd.cpp
@@ -32,6 +32,7 @@
 #include "vehicle_func.h"
 #include "sound_func.h"
 #include "signal_func.h"
+#include "tunnelbridge.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -160,17 +161,6 @@
 	return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
 }
 
-
-uint32 GetBridgeLength(TileIndex begin, TileIndex end)
-{
-	int x1 = TileX(begin);
-	int y1 = TileY(begin);
-	int x2 = TileX(end);
-	int y2 = TileY(end);
-
-	return abs(x2 + y2 - x1 - y1) - 1;
-}
-
 bool CheckBridge_Stuff(byte bridge_type, uint bridge_len)
 {
 	const Bridge *b = &_bridge[bridge_type];
@@ -633,7 +623,7 @@
 			DoClearSquare(endtile);
 		}
 	}
-	return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (DistanceManhattan(tile, endtile) + 1));
+	return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_tunnel * (GetTunnelBridgeLength(tile, endtile) + 2));
 }
 
 
@@ -693,7 +683,7 @@
 		}
 	}
 
-	return CommandCost(EXPENSES_CONSTRUCTION, (DistanceManhattan(tile, endtile) + 1) * _price.clear_bridge);
+	return CommandCost(EXPENSES_CONSTRUCTION, (GetTunnelBridgeLength(tile, endtile) + 2) * _price.clear_bridge);
 }
 
 static CommandCost ClearTile_TunnelBridge(TileIndex tile, byte flags)
@@ -1018,8 +1008,8 @@
 
 	axis = GetBridgeAxis(ti->tile);
 	piece = CalcBridgePiece(
-		DistanceManhattan(ti->tile, rampnorth),
-		DistanceManhattan(ti->tile, rampsouth)
+		GetTunnelBridgeLength(ti->tile, rampnorth) + 1,
+		GetTunnelBridgeLength(ti->tile, rampsouth) + 1
 	);
 	type = GetBridgeType(rampsouth);
 
--- a/src/yapf/follow_track.hpp
+++ b/src/yapf/follow_track.hpp
@@ -76,32 +76,22 @@
 		m_is_station = m_is_bridge = m_is_tunnel = false;
 		m_tiles_skipped = 0;
 
-		// extra handling for tunnels in our direction
-		if (IsTunnelTile(m_old_tile)) {
-			DiagDirection tunnel_enterdir = GetTunnelBridgeDirection(m_old_tile);
-			if (tunnel_enterdir == m_exitdir) {
-				// we are entering the tunnel
-				FindLengthOfTunnelResult flotr = FindLengthOfTunnel(m_old_tile, m_exitdir);
-				m_new_tile = flotr.tile;
-				m_is_tunnel = true;
-				m_tiles_skipped = flotr.length - 1;
+		// extra handling for tunnels and bridges in our direction
+		if (IsTileType(m_old_tile, MP_TUNNELBRIDGE)) {
+			DiagDirection enterdir = GetTunnelBridgeDirection(m_old_tile);
+			if (enterdir == m_exitdir) {
+				// we are entering the tunnel / bridge
+				if (IsTunnel(m_old_tile)) {
+					m_is_tunnel = true;
+					m_new_tile = GetOtherTunnelEnd(m_old_tile);
+				} else { // IsBridge(m_old_tile)
+					m_is_bridge = true;
+					m_new_tile = GetOtherBridgeEnd(m_old_tile);
+				}
+				m_tiles_skipped = GetTunnelBridgeLength(m_new_tile, m_old_tile);
 				return;
 			}
-			assert(ReverseDiagDir(tunnel_enterdir) == m_exitdir);
-		}
-
-		// extra handling for bridge ramp in our direction
-		if (IsBridgeTile(m_old_tile)) {
-			DiagDirection bridge_enterdir = GetTunnelBridgeDirection(m_old_tile);
-			if (bridge_enterdir == m_exitdir) {
-				// we are entering the bridge ramp
-				m_new_tile = GetOtherBridgeEnd(m_old_tile);
-				uint32 bridge_length = GetBridgeLength(m_old_tile, m_new_tile);
-				m_tiles_skipped = bridge_length;
-				m_is_bridge = true;
-				return;
-			}
-			assert(ReverseDiagDir(bridge_enterdir) == m_exitdir);
+			assert(ReverseDiagDir(enterdir) == m_exitdir);
 		}
 
 		// normal or station tile, do one step
--- a/src/yapf/yapf.hpp
+++ b/src/yapf/yapf.hpp
@@ -23,6 +23,7 @@
 #include "../waypoint.h"
 #include "../debug.h"
 #include "../settings_type.h"
+#include "../tunnelbridge.h"
 
 extern uint64 _rdtsc();