summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortron <tron@openttd.org>2005-03-23 11:12:17 +0000
committertron <tron@openttd.org>2005-03-23 11:12:17 +0000
commit04087c9ac9992ab8c750ca4b132b8cd932e30d19 (patch)
treee0cd095411beda9c843c2f0bbb69d1ef2a3ebae9
parentb19553574e336faa9e124577405052acdb3bf7db (diff)
downloadopenttd-04087c9ac9992ab8c750ca4b132b8cd932e30d19.tar.xz
(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
-rw-r--r--aircraft_cmd.c55
1 files changed, 30 insertions, 25 deletions
diff --git a/aircraft_cmd.c b/aircraft_cmd.c
index f48dfb1d6..f7c96dbd6 100644
--- a/aircraft_cmd.c
+++ b/aircraft_cmd.c
@@ -35,31 +35,34 @@ static const SpriteID _aircraft_sprite[] = {
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;
-
- temp_distance = DistanceSquare(v->tile, st->airport_tile);
- if (temp_distance < distance) {
- distance = temp_distance;
- index_to_target = st->index;
- }
+ if (st->owner == v->owner && st->facilities & FACIL_AIRPORT &&
+ GetAirport(st->airport_type)->nof_depots > 0) {
+ uint distance;
+
+ // 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 @@ int32 CmdSendAircraftToHangar(int x, int y, uint32 flags, uint32 p1, uint32 p2)
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;
}