* Update the vehicle's destination tile from an order. * @param order the order the vehicle currently has * @param v the vehicle to update * @param conditional_depth the depth (amount of steps) to go with conditional orders. This to prevent infinite loops. * @param pbs_look_ahead Whether we are forecasting orders for pbs reservations in advance. If true, the order indices must not be modified.
| 1947 | * @param pbs_look_ahead Whether we are forecasting orders for pbs reservations in advance. If true, the order indices must not be modified. |
| 1948 | */ |
| 1949 | bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool pbs_look_ahead) |
| 1950 | { |
| 1951 | if (conditional_depth > v->GetNumOrders()) { |
| 1952 | v->current_order.Free(); |
| 1953 | v->SetDestTile(TileIndex{}); |
| 1954 | return false; |
| 1955 | } |
| 1956 | |
| 1957 | switch (order->GetType()) { |
| 1958 | case OT_GOTO_STATION: |
| 1959 | v->SetDestTile(v->GetOrderStationLocation(order->GetDestination().ToStationID())); |
| 1960 | return true; |
| 1961 | |
| 1962 | case OT_GOTO_DEPOT: |
| 1963 | if (order->GetDepotOrderType().Test(OrderDepotTypeFlag::Service) && !v->NeedsServicing()) { |
| 1964 | assert(!pbs_look_ahead); |
| 1965 | UpdateVehicleTimetable(v, true); |
| 1966 | v->IncrementRealOrderIndex(); |
| 1967 | break; |
| 1968 | } |
| 1969 | |
| 1970 | if (v->current_order.GetDepotActionType().Test(OrderDepotActionFlag::NearestDepot)) { |
| 1971 | /* If the vehicle can't find its destination, delay its next search. |
| 1972 | * In case many vehicles are in this state, use the vehicle index to spread out pathfinder calls. */ |
| 1973 | if (v->dest_tile == 0 && TimerGameEconomy::date_fract != (v->index % Ticks::DAY_TICKS)) break; |
| 1974 | |
| 1975 | /* We need to search for the nearest depot (hangar). */ |
| 1976 | ClosestDepot closest_depot = v->FindClosestDepot(); |
| 1977 | |
| 1978 | if (closest_depot.found) { |
| 1979 | /* PBS reservations cannot reverse */ |
| 1980 | if (pbs_look_ahead && closest_depot.reverse) return false; |
| 1981 | |
| 1982 | v->SetDestTile(closest_depot.location); |
| 1983 | v->current_order.SetDestination(closest_depot.destination); |
| 1984 | |
| 1985 | /* If there is no depot in front, reverse automatically (trains only) */ |
| 1986 | if (v->type == VEH_TRAIN && closest_depot.reverse) Command<CMD_REVERSE_TRAIN_DIRECTION>::Do(DoCommandFlag::Execute, v->index, false); |
| 1987 | |
| 1988 | if (v->type == VEH_AIRCRAFT) { |
| 1989 | Aircraft *a = Aircraft::From(v); |
| 1990 | if (a->state == FLYING && a->targetairport != closest_depot.destination) { |
| 1991 | /* The aircraft is now heading for a different hangar than the next in the orders */ |
| 1992 | AircraftNextAirportPos_and_Order(a); |
| 1993 | } |
| 1994 | } |
| 1995 | return true; |
| 1996 | } |
| 1997 | |
| 1998 | /* If there is no depot, we cannot help PBS either. */ |
| 1999 | if (pbs_look_ahead) return false; |
| 2000 | |
| 2001 | UpdateVehicleTimetable(v, true); |
| 2002 | v->IncrementRealOrderIndex(); |
| 2003 | } else { |
| 2004 | if (v->type != VEH_AIRCRAFT) { |
| 2005 | v->SetDestTile(Depot::Get(order->GetDestination().ToStationID())->xy); |
| 2006 | } else { |
no test coverage detected