(svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
authortron
Wed, 23 Mar 2005 11:12:17 +0000
changeset 1532 83719caec8d1
parent 1531 4ee3c51e2b32
child 1533 f735e49c8deb
(svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
While here also do some const gymnastics
aircraft_cmd.c
--- a/aircraft_cmd.c	Wed Mar 23 08:24:13 2005 +0000
+++ b/aircraft_cmd.c	Wed Mar 23 11:12:17 2005 +0000
@@ -35,31 +35,34 @@
 	0x0EBD, 0x0EC5
 };
 
-// use this to find the nearest hangar to v
-// bit 16 is set in the return value if the player do not have any airports with a hangar (like helipads only)
-static uint32 FindNearestHangar(Vehicle *v)
+/* Find the nearest hangar to v
+ * INVALID_STATION is returned, if the player does not have any suitable
+ * airports (like helipads only)
+ */
+static uint16 FindNearestHangar(const Vehicle *v)
 {
-	Station *st;
-	uint temp_distance, distance = 0xFFFF;
-	uint16 index_to_target = 0;
+	const Station *st;
+	uint best = 0;
+	uint16 index = INVALID_STATION;
 
 	FOR_ALL_STATIONS(st) {
-		if (st->owner == v->owner && st->facilities & FACIL_AIRPORT) {
-			if (GetAirport(st->airport_type)->terminals != NULL) {
-				// don't crash the planes if we know they can't land at the airport
-				if (HASBIT(v->subtype,1) && st->airport_type == AT_SMALL && !_cheats.no_jetcrash.value) continue;
+		if (st->owner == v->owner && st->facilities & FACIL_AIRPORT &&
+				GetAirport(st->airport_type)->nof_depots > 0) {
+			uint distance;
 
-				temp_distance = DistanceSquare(v->tile, st->airport_tile);
-				if (temp_distance < distance) {
-					distance = temp_distance;
-					index_to_target = st->index;
-				}
+			// don't crash the plane if we know it can't land at the airport
+			if (HASBIT(v->subtype, 1) && st->airport_type == AT_SMALL &&
+					!_cheats.no_jetcrash.value)
+				continue;
+
+			distance = DistanceSquare(v->tile, st->airport_tile);
+			if (distance < best || index == INVALID_STATION) {
+				best = distance;
+				index = st->index;
 			}
 		}
 	}
-	if (distance == 65000)
-		SETBIT(index_to_target, 16);
-	return index_to_target;
+	return index;
 }
 
 // returns true if vehicle v have an airport in the schedule, that has a hangar
@@ -438,14 +441,16 @@
 		st = GetStation(next_airport_index);
 		// If an airport doesn't have terminals (so no landing space for airports),
 		// it surely doesn't have any hangars
-		if (st->xy == 0 || st->airport_tile == 0 || GetAirport(st->airport_type)->terminals == NULL) {
+		if (st->xy == 0 || st->airport_tile == 0 ||
+				GetAirport(st->airport_type)->nof_depots == 0) {
 			if (p2 == 0) {
-				// the aircraft have to search for a hangar on it's own
-				uint32 temp = FindNearestHangar(v);
+				// the aircraft has to search for a hangar on its own
+				uint16 station = FindNearestHangar(v);
+
 				next_airport_has_hangar = false;
-				if (HASBIT(temp, 16)) return CMD_ERROR; // the player does not own a hangar
-				st = GetStation(temp);
-				next_airport_index = (uint16)temp;
+				if (station == INVALID_STATION) return CMD_ERROR;
+				st = GetStation(station);
+				next_airport_index = station;
 			} else {
 				return CMD_ERROR;
 			}