diff options
author | rubidium <rubidium@openttd.org> | 2009-01-10 09:51:14 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-01-10 09:51:14 +0000 |
commit | 3d6c09b38d54da55ba7688fab8acb5879a34c861 (patch) | |
tree | c150281da19ec8c75bcae7d7526de3017ee154db | |
parent | ece7d9a16f66102aa2b384c45c13dc2a31cce7f8 (diff) | |
download | openttd-3d6c09b38d54da55ba7688fab8acb5879a34c861.tar.xz |
(svn r14952) -Codechange: unify the "can vehicle go to station" tests
-rw-r--r-- | src/aircraft.h | 23 | ||||
-rw-r--r-- | src/aircraft_cmd.cpp | 2 | ||||
-rw-r--r-- | src/build_vehicle_gui.cpp | 4 | ||||
-rw-r--r-- | src/order_cmd.cpp | 47 | ||||
-rw-r--r-- | src/vehicle.cpp | 52 | ||||
-rw-r--r-- | src/vehicle_func.h | 3 |
6 files changed, 67 insertions, 64 deletions
diff --git a/src/aircraft.h b/src/aircraft.h index b5a231e25..faf888f88 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -34,29 +34,6 @@ static inline bool IsNormalAircraft(const Vehicle *v) return v->subtype <= AIR_AIRCRAFT; } -/** Checks if an aircraft can use the station in question - * @param engine The engine to test - * @param st The station - * @return true if the aircraft can use the station - */ -static inline bool CanAircraftUseStation(EngineID engine, const Station *st) -{ - const AirportFTAClass *apc = st->Airport(); - const AircraftVehicleInfo *avi = AircraftVehInfo(engine); - - return (apc->flags & (avi->subtype & AIR_CTOL ? AirportFTAClass::AIRPLANES : AirportFTAClass::HELICOPTERS)) != 0; -} - -/** Checks if an aircraft can use the station at the tile in question - * @param engine The engine to test - * @param tile The tile where the station is - * @return true if the aircraft can use the station - */ -static inline bool CanAircraftUseStation(EngineID engine, TileIndex tile) -{ - return CanAircraftUseStation(engine, GetStationByTile(tile)); -} - /** * Calculates cargo capacity based on an aircraft's passenger * and mail capacities. diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index bf05fb952..8a1c8470f 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -273,7 +273,7 @@ CommandCost CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, if (!IsHangarTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR; /* Prevent building aircraft types at places which can't handle them */ - if (!CanAircraftUseStation(p1, tile)) return CMD_ERROR; + if (!CanVehicleUseStation(p1, GetStationByTile(tile))) return CMD_ERROR; /* Allocate 2 or 3 vehicle structs, depending on type * vl[0] = aircraft, vl[1] = shadow, [vl[2] = rotor] */ diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 7cfcc6aec..27a1e039f 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -987,6 +987,8 @@ struct BuildVehicleWindow : Window { this->eng_list.Clear(); + const Station *st = GetStation(this->window_number); + /* Make list of all available planes. * Also check to see if the previously selected plane is still available, * and if not, reset selection to INVALID_ENGINE. This could be the case @@ -996,7 +998,7 @@ struct BuildVehicleWindow : Window { EngineID eid = e->index; if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_company)) continue; /* First VEH_END window_numbers are fake to allow a window open for all different types at once */ - if (!this->listview_mode && !CanAircraftUseStation(eid, this->window_number)) continue; + if (!this->listview_mode && !CanVehicleUseStation(eid, st)) continue; *this->eng_list.Append() = eid; if (eid == this->sel_engine) sel_id = eid; diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index c1ac606a0..a0b675f10 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -425,35 +425,8 @@ CommandCost CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, c const Station *st = GetStation(new_order.GetDestination()); - if (st->owner != OWNER_NONE && !CheckOwnership(st->owner)) { - return CMD_ERROR; - } - - switch (v->type) { - case VEH_TRAIN: - if (!(st->facilities & FACIL_TRAIN)) return CMD_ERROR; - break; - - case VEH_ROAD: - if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) { - if (!(st->facilities & FACIL_BUS_STOP)) return CMD_ERROR; - } else { - if (!(st->facilities & FACIL_TRUCK_STOP)) return CMD_ERROR; - } - break; - - case VEH_SHIP: - if (!(st->facilities & FACIL_DOCK)) return CMD_ERROR; - break; - - case VEH_AIRCRAFT: - if (!(st->facilities & FACIL_AIRPORT) || !CanAircraftUseStation(v->engine_type, st)) { - return CMD_ERROR; - } - break; - - default: return CMD_ERROR; - } + if (st->owner != OWNER_NONE && !CheckOwnership(st->owner)) return CMD_ERROR; + if (!CanVehicleUseStation(v, st)) return CMD_ERROR; /* Non stop not allowed for non-trains. */ if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN && v->type != VEH_ROAD) return CMD_ERROR; @@ -481,9 +454,8 @@ CommandCost CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, c const Station *st = GetStation(new_order.GetDestination()); if (!CheckOwnership(st->owner) || - !(st->facilities & FACIL_AIRPORT) || - st->Airport()->nof_depots == 0 || - !CanAircraftUseStation(v->engine_type, st)) { + !CanVehicleUseStation(v, st) || + st->Airport()->nof_depots == 0) { return CMD_ERROR; } } else { @@ -1376,17 +1348,14 @@ CommandCost CmdRestoreOrderIndex(TileIndex tile, uint32 flags, uint32 p1, uint32 static TileIndex GetStationTileForVehicle(const Vehicle *v, const Station *st) { + if (!CanVehicleUseStation(v, st)) return INVALID_TILE; + switch (v->type) { default: NOT_REACHED(); case VEH_TRAIN: return st->train_tile; - case VEH_AIRCRAFT: return CanAircraftUseStation(v->engine_type, st) ? st->airport_tile : INVALID_TILE; + case VEH_AIRCRAFT: return st->airport_tile; case VEH_SHIP: return st->dock_tile; - case VEH_ROAD: - if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) { - return (st->bus_stops != NULL) ? st->bus_stops->xy : INVALID_TILE; - } else { - return (st->truck_stops != NULL) ? st->truck_stops->xy : INVALID_TILE; - } + case VEH_ROAD: return IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? st->bus_stops->xy : st->truck_stops->xy; } } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 7ccb81215..591bfc8e9 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -2255,3 +2255,55 @@ void VehiclesYearlyLoop() } } } + + +/** + * Can this station be used by the given engine type? + * @param engine_type the type of vehicles to test + * @param st the station to test for + * @return true if and only if the vehicle of the type can use this station. + * @note For road vehicles the Vehicle is needed to determine whether it can + * use the station. This function will return true for road vehicles + * when at least one of the facilities is available. + */ +bool CanVehicleUseStation(EngineID engine_type, const Station *st) +{ + assert(IsEngineIndex(engine_type)); + const Engine *e = GetEngine(engine_type); + + switch (e->type) { + case VEH_TRAIN: + return (st->facilities & FACIL_TRAIN) != 0; + + case VEH_ROAD: + /* For road vehicles we need the vehicle to know whether it can actually + * use the station, but if it doesn't have facilities for RVs it is + * certainly not possible that the station can be used. */ + return (st->facilities & (FACIL_BUS_STOP | FACIL_TRUCK_STOP)) != 0; + + case VEH_SHIP: + return (st->facilities & FACIL_DOCK) != 0; + + case VEH_AIRCRAFT: + return (st->facilities & FACIL_AIRPORT) != 0 && + (st->Airport()->flags & (e->u.air.subtype & AIR_CTOL ? AirportFTAClass::AIRPLANES : AirportFTAClass::HELICOPTERS)) != 0; + + default: + return false; + } +} + +/** + * Can this station be used by the given vehicle? + * @param v the vehicle to test + * @param st the station to test for + * @return true if and only if the vehicle can use this station. + */ +bool CanVehicleUseStation(const Vehicle *v, const Station *st) +{ + if (v->type == VEH_ROAD) { + return (st->facilities & (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? FACIL_BUS_STOP : FACIL_TRUCK_STOP)) != 0; + } + + return CanVehicleUseStation(v->engine_type, st); +} diff --git a/src/vehicle_func.h b/src/vehicle_func.h index 616ccee54..c9cc6a141 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -171,4 +171,7 @@ extern const Vehicle *_place_clicked_vehicle; extern VehicleID _new_vehicle_id; extern uint16 _returned_refit_capacity; +bool CanVehicleUseStation(EngineID engine_type, const struct Station *st); +bool CanVehicleUseStation(const Vehicle *v, const struct Station *st); + #endif /* VEHICLE_FUNC_H */ |