(svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
authortron
Fri, 03 Mar 2006 21:18:19 +0000
changeset 3136 e2682061ef1d
parent 3135 3a20ced2e34b
child 3137 1d321ced37f3
(svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
roadveh_cmd.c
--- a/roadveh_cmd.c	Fri Mar 03 20:54:54 2006 +0000
+++ b/roadveh_cmd.c	Fri Mar 03 21:18:19 2006 +0000
@@ -633,8 +633,9 @@
 
 	if (order->type == OT_GOTO_STATION) {
 		const Station* st = GetStation(order->station);
-		uint mindist = 0xFFFFFFFF;
 		const RoadStop* rs;
+		TileIndex dest;
+		uint mindist;
 
 		if (order->station == v->last_station_visited) {
 			v->last_station_visited = INVALID_STATION;
@@ -649,9 +650,17 @@
 			return;
 		}
 
-		for (; rs != NULL; rs = rs->next) {
-			if (DistanceManhattan(v->tile, rs->xy) < mindist) v->dest_tile = rs->xy;
+		dest = rs->xy;
+		mindist = DistanceManhattan(v->tile, rs->xy);
+		for (rs = rs->next; rs != NULL; rs = rs->next) {
+			uint dist = DistanceManhattan(v->tile, rs->xy);
+
+			if (dist < mindist) {
+				mindist = dist;
+				dest = rs->xy;
+			}
 		}
+		v->dest_tile = dest;
 	} else if (order->type == OT_GOTO_DEPOT) {
 		v->dest_tile = GetDepot(order->station)->xy;
 	}