changeset 8800:2a908464da27 draft

(svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
author frosch <frosch@openttd.org>
date Wed, 02 Apr 2008 13:57:25 +0000
parents f94bd2d555c9
children 9fe7640963e3
files src/ai/default/default.cpp src/pathfind.cpp src/pathfind.h src/roadveh_cmd.cpp src/ship_cmd.cpp
diffstat 5 files changed, 26 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/default/default.cpp
+++ b/src/ai/default/default.cpp
@@ -1922,7 +1922,7 @@
 	arpfd.tile2 = _players_ai[p->index].cur_tile_a;
 	arpfd.flag = false;
 	arpfd.count = 0;
-	FollowTrack(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a), TRANSPORT_RAIL, 0, ReverseDiagDir(_players_ai[p->index].cur_dir_a),
+	FollowTrack(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a), PATHFIND_FLAGS_NONE, TRANSPORT_RAIL, 0, ReverseDiagDir(_players_ai[p->index].cur_dir_a),
 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
 	return arpfd.count > 8;
 }
@@ -2897,7 +2897,7 @@
 
 	while (bits != TRACKDIR_BIT_NONE) {
 		Trackdir trackdir = RemoveFirstTrackdir(&bits);
-		FollowTrack(tile, 0x1000 | TRANSPORT_ROAD, ROADTYPES_ROAD, TrackdirToExitdir(trackdir), (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
+		FollowTrack(tile, PATHFIND_FLAGS_DISABLE_TILE_HASH, TRANSPORT_ROAD, ROADTYPES_ROAD, TrackdirToExitdir(trackdir), (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
 	}
 
 	if (DistanceManhattan(tile, are.dest) <= are.best_dist) return false;
--- a/src/pathfind.cpp
+++ b/src/pathfind.cpp
@@ -139,7 +139,7 @@
 	0x2A,
 };
 
-static void TPFMode2(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
+static void TPFModeShip(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
 {
 	RememberData rd;
 
@@ -177,7 +177,7 @@
 		tpf->the_dir = (Trackdir)(track + (HasBit(_otherdir_mask[direction], track) ? 8 : 0));
 
 		if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length)) {
-			TPFMode2(tpf, tile, _tpf_new_direction[tpf->the_dir]);
+			TPFModeShip(tpf, tile, _tpf_new_direction[tpf->the_dir]);
 		}
 
 		tpf->rd = rd;
@@ -211,7 +211,7 @@
 
 static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 };
 
-static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
+static void TPFModeNormal(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
 {
 	const TileIndex tile_org = tile;
 
@@ -264,10 +264,8 @@
 
 	tpf->rd.cur_length++;
 
-	if ((byte)bits != tpf->var2) {
-		bits &= _tpfmode1_and[direction];
-		bits |= bits >> 8;
-	}
+	bits &= _tpfmode1_and[direction];
+	bits |= bits >> 8;
 	bits &= 0xBF;
 
 	if (bits != 0) {
@@ -282,7 +280,7 @@
 				/* make sure we are not leaving from invalid side */
 				if (TPFSetTileBit(tpf, tile, tpf->the_dir) && CanAccessTileInDir(tile, TrackdirToExitdir(tpf->the_dir), tpf->tracktype) &&
 						!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length) ) {
-					TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]);
+					TPFModeNormal(tpf, tile, _tpf_new_direction[tpf->the_dir]);
 				}
 				tpf->rd = rd;
 			} while (bits != 0);
@@ -290,7 +288,7 @@
 	}
 }
 
-void FollowTrack(TileIndex tile, uint16 flags, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
+void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
 {
 	assert(IsValidDiagDirection(direction));
 
@@ -306,21 +304,18 @@
 	tpf->rd.depth = 0;
 	tpf->rd.last_choosen_track = INVALID_TRACK;
 
-	tpf->var2 = HasBit(flags, 15) ? 0x43 : 0xFF; // 0x8000
+	tpf->disable_tile_hash = (flags & PATHFIND_FLAGS_DISABLE_TILE_HASH) != 0;
 
-	tpf->disable_tile_hash = HasBit(flags, 12);  // 0x1000
-
-
-	tpf->tracktype = (TransportType)(flags & 0xFF);
+	tpf->tracktype = tt;
 	tpf->sub_type = sub_type;
 
-	if (HasBit(flags, 11)) {
+	if ((flags & PATHFIND_FLAGS_SHIP_MODE) != 0) {
 		tpf->enum_proc(tile, data, INVALID_TRACKDIR, 0);
-		TPFMode2(tpf, tile, direction);
+		TPFModeShip(tpf, tile, direction);
 	} else {
 		/* clear the hash_heads */
 		memset(tpf->hash_head, 0, sizeof(tpf->hash_head));
-		TPFMode1(tpf, tile, direction);
+		TPFModeNormal(tpf, tile, direction);
 	}
 
 	if (after_proc != NULL) after_proc(tpf);
--- a/src/pathfind.h
+++ b/src/pathfind.h
@@ -58,7 +58,6 @@
 	TransportType tracktype;
 	uint sub_type;
 
-	byte var2;
 	bool disable_tile_hash;
 
 	uint16 hash_head[0x400];
@@ -67,7 +66,15 @@
 	TrackPathFinderLink links[0x400]; ///< hopefully, this is enough.
 };
 
-void FollowTrack(TileIndex tile, uint16 flags, uint sub_type, DiagDirection direction, TPFEnumProc* enum_proc, TPFAfterProc* after_proc, void* data);
+/** Some flags to modify the behaviour of original pathfinder */
+enum PathfindFlags {
+	PATHFIND_FLAGS_NONE              = 0,
+	PATHFIND_FLAGS_SHIP_MODE         = 0x0800, ///< pathfinder with some optimizations for ships, but does not work for other types.
+	PATHFIND_FLAGS_DISABLE_TILE_HASH = 0x1000, ///< do not check for searching in circles
+};
+DECLARE_ENUM_AS_BIT_SET(PathfindFlags)
+
+void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc* enum_proc, TPFAfterProc* after_proc, void* data);
 void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data);
 
 #endif /* PATHFIND_H */
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -437,7 +437,7 @@
 
 			/* search in all directions */
 			for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
-				FollowTrack(v->tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, d, EnumRoadSignalFindDepot, NULL, &rfdd);
+				FollowTrack(v->tile, PATHFIND_FLAGS_NONE, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, d, EnumRoadSignalFindDepot, NULL, &rfdd);
 			}
 
 			if (rfdd.best_length != UINT_MAX) return GetDepotByTile(rfdd.tile);
@@ -1283,7 +1283,7 @@
 				if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
 				frd.maxtracklen = UINT_MAX;
 				frd.mindist = UINT_MAX;
-				FollowTrack(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
+				FollowTrack(tile, PATHFIND_FLAGS_NONE, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
 
 				if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
 					best_dist = frd.mindist;
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -474,7 +474,7 @@
 		pfs.best_bird_dist = (uint)-1;
 		pfs.best_length = (uint)-1;
 
-		FollowTrack(tile, 0x1800 | TRANSPORT_WATER, 0, (DiagDirection)_ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs);
+		FollowTrack(tile, PATHFIND_FLAGS_SHIP_MODE | PATHFIND_FLAGS_DISABLE_TILE_HASH, TRANSPORT_WATER, 0, (DiagDirection)_ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs);
 
 		if (best_track != INVALID_TRACK) {
 			if (pfs.best_bird_dist != 0) {