roadveh_cmd.c
changeset 1709 4407b7f5f607
parent 1698 3f979f9ecf6c
child 1745 1113e1272ca2
--- a/roadveh_cmd.c	Sun Apr 17 19:44:05 2005 +0000
+++ b/roadveh_cmd.c	Mon Apr 18 05:42:59 2005 +0000
@@ -652,8 +652,7 @@
 		st = GetStation(order->station);
 
 		{
-			int32 *dist;
-			int32 mindist = 0xFFFFFFFF;
+			uint mindist = 0xFFFFFFFF;
 			int num;
 			RoadStopType type;
 			RoadStop *rs;
@@ -670,18 +669,12 @@
 				return;
 			}
 
-			dist = malloc(num * sizeof(int32));
-
-			do {
-				*dist = DistanceSquare(v->tile, rs->xy);
-				if (*dist < mindist) {
+			for (rs = GetPrimaryRoadStop(st, type); rs != NULL; rs = rs->next) {
+				if (DistanceManhattan(v->tile, rs->xy) < mindist) {
 					v->dest_tile = rs->xy;
 				}
-				rs = rs->next;
-			} while ( rs != NULL );
+			}
 
-			free(dist);
-			dist = NULL;
 		}
 	} else if (order->type == OT_GOTO_DEPOT) {
 		v->dest_tile = GetDepot(order->station)->xy;
@@ -1157,29 +1150,17 @@
 	return best_track;
 }
 
-static int RoadFindPathToStation(const Vehicle *v, TileIndex tile)
+static uint RoadFindPathToStation(const Vehicle *v, TileIndex tile)
 {
-	FindRoadToChooseData frd;
-	int i, best_track = -1;
-	uint best_dist = (uint) -1, best_maxlen = (uint) -1;
-
-	frd.dest = tile;
-	frd.maxtracklen = (uint) -1;
-	frd.mindist = (uint) -1;
+  NPFFindStationOrTileData fstd;
+  byte trackdir = _dir_to_diag_trackdir[(v->direction >> 1) & 3];
 
-	for (i = 0; i < 4; i++) {
-		FollowTrack(v->tile, 0x2000 | TRANSPORT_ROAD, i, (TPFEnumProc*)EnumRoadTrackFindDist, NULL, &frd);
+  fstd.dest_coords = tile;
+  fstd.station_index = -1;	// indicates that the destination is a tile, not a station
 
-		if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen )) {
-			best_dist = frd.mindist;
-			best_maxlen = frd.maxtracklen;
-			best_track = i;
-		}
-	}
-	return best_maxlen;
+  return NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->owner).best_path_dist;
 }
 
-
 typedef struct RoadDriveEntry {
 	byte x,y;
 } RoadDriveEntry;
@@ -1672,17 +1653,12 @@
 		}
 
 		//We do not have a slot, so make one
-		if (v->u.road.slot == NULL && rs != NULL) {
+		if (v->u.road.slot == NULL && rs != NULL && IsTileType(v->tile, MP_STREET)) {
 		//first we need to find out how far our stations are away.
 
 			DEBUG(ms, 2) ("Multistop: Attempting to obtain a slot for vehicle %d at station %d (0x%x)", v->unitnumber, st->index, st->xy);
 			do {
-				stop->dist = 0xFFFFFFFF;
-
-				//FIXME This doesn't fully work yet, as it only goes
-				//to one tile BEFORE the stop in question and doesn't
-				//regard the direction of the exit
-				stop->dist = RoadFindPathToStation(v, rs->xy);
+				stop->dist = RoadFindPathToStation(v, rs->xy) / NPF_TILE_LENGTH;
 				DEBUG(ms, 3) ("Multistop: Distance to stop at 0x%x is %d", rs->xy, stop->dist);
 				stop->rs = rs;
 
@@ -1694,7 +1670,7 @@
 				rs = rs->next;
 			} while (rs != NULL);
 
-			if (mindist < 120) {	//if we're reasonably close, get us a slot
+			if (mindist < 180) {	//if we're reasonably close, get us a slot
 				int k;
 				bubblesort(firststop, num, sizeof(StopStruct), dist_compare);