# departures.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "gui.h"
    00015 #include "textbuf_gui.h"
    00016 #include "strings_func.h"
    00017 #include "window_func.h"
    00018 #include "vehicle_func.h"
    00019 #include "string_func.h"
    00020 #include "window_gui.h"
    00021 #include "timetable.h"
    00022 #include "vehiclelist.h"
    00023 #include "company_base.h"
    00024 #include "date_func.h"
    00025 #include "departures_gui.h"
    00026 #include "station_base.h"
    00027 #include "vehicle_gui_base.h"
    00028 #include "vehicle_base.h"
    00029 #include "vehicle_gui.h"
    00030 #include "order_base.h"
    00031 #include "settings_type.h"
    00032 #include "core/smallvec_type.hpp"
    00033 #include "date_type.h"
    00034 #include "company_type.h"
    00035 #include "cargo_type.h"
    00036 #include "departures_func.h"
    00037 #include "departures_type.h"
    00038
    00040 typedef struct OrderDate
    00041 {
    00042   const Order *order;
    00043   const Vehicle *v;
    00044   DateTicks expected_date;
    00045   Ticks lateness;
    00046   DepartureStatus status;
    00047 } OrderDate;
    00048
    00049 static FORCEINLINE bool IsDeparture(const Order *order, StationID station) {
    00050   return (order->GetType() == OT_GOTO_STATION &&
    00051       (StationID)order->GetDestination() == station &&
    00052       (order->GetLoadType() != OLFB_NO_LOAD ||
    00053       _settings_client.gui.departure_show_all_stops) &&
    00054       order->wait_time != 0);
    00055 }
    00056
    00057 static FORCEINLINE bool IsVia(const Order *order, StationID station) {
    00058   return ((order->GetType() == OT_GOTO_STATION ||
    00059       order->GetType() == OT_GOTO_WAYPOINT) &&
    00060       (StationID)order->GetDestination() == station &&
    00061       (order->GetNonStopType() == ONSF_NO_STOP_AT_ANY_STATION ||
    00062       order->GetNonStopType() == ONSF_NO_STOP_AT_DESTINATION_STATION));
    00063 }
    00064
    00065 static FORCEINLINE bool IsArrival(const Order *order, StationID station) {
    00066   return (order->GetType() == OT_GOTO_STATION &&
    00067       (StationID)order->GetDestination() == station &&
    00068       (order->GetUnloadType() != OUFB_NO_UNLOAD ||
    00069       _settings_client.gui.departure_show_all_stops) &&
    00070       order->wait_time != 0);
    00071 }
    00072
    00081 DepartureList* MakeDepartureList(StationID station, bool show_vehicle_types[5], DepartureType type, bool show_vehicles_via)
    00082 {
    00083   /* This function is the meat of the departure boards functionality. */
    00084   /* As an overview, it works by repeatedly considering the best possible next departure to show. */
    00085   /* By best possible we mean the one expected to arrive at the station first. */
    00086   /* However, we do not consider departures whose scheduled time is too far in the future, even if they are expected before some delayed ones. */
    00087   /* This code can probably be made more efficient. I haven't done so in order to keep both its (relative) simplicity and my (relative) sanity. */
    00088   /* Having written that, it's not exactly slow at the moment. */
    00089
    00090   /* The list of departures which will be returned as a result. */
    00091   SmallVector<Departure*, 32> *result = new SmallVector<Departure*, 32>();
    00092
    00093   /* A list of the next scheduled orders to be considered for inclusion in the departure list. */
    00094   SmallVector<OrderDate*, 32> next_orders;
    00095
    00096   /* The maximum possible date for departures to be scheduled to occur. */
    00097   DateTicks max_date = _settings_client.gui.max_departure_time * DAY_TICKS;
    00098
    00099   /* The scheduled order in next_orders with the earliest expected_date field. */
    00100   OrderDate *least_order = NULL;
    00101
    00102   /* Get all the vehicles stopping at this station. */
    00103   /* We do this to get the order which is the first time they will stop at this station. */
    00104   /* This order is stored along with some more information. */
    00105   /* We keep a pointer to the `least' order (the one with the soonest expected completion time). */
    00106   for (uint i = 0; i < 4; ++i) {
    00107     VehicleList vehicles;
    00108
    00109     if (!show_vehicle_types[i]) {
    00110       /* Don't show vehicles whose type we're not interested in. */
    00111       continue;
    00112     }
    00113
    00114         /* MAX_COMPANIES is probably the wrong thing to put here, but it works. GenerateVehicleSortList doesn't check the company when the type of list is VL_STATION_LIST (r20801). */
    00115     if (!GenerateVehicleSortList(&vehicles, VehicleListIdentifier(VL_STATION_LIST, (VehicleType)(VEH_TRAIN + i), MAX_COMPANIES, station).Pack())) {
    00116       /* Something went wrong: panic! */
    00117       return result;
    00118     }
    00119
    00120     /* Get the first order for each vehicle for the station we're interested in that doesn't have No Loading set. */
    00121     /* We find the least order while we're at it. */
    00122     for (const Vehicle **v = vehicles.Begin(); v != vehicles.End(); v++) {
    00123       if (_settings_client.gui.departure_only_passengers) {
    00124         bool carries_passengers = false;
    00125
    00126         const Vehicle *u = *v;
    00127         while (u != NULL) {
    00128           if (u->cargo_type == CT_PASSENGERS) {
    00129             carries_passengers = true;
    00130             break;
    00131           }
    00132           u = u->Next();
    00133         }
    00134
    00135         if (carries_passengers == false) {
    00136           continue;
    00137         }
    00138       }
    00139
    00140       const Order *order = (*v)->GetOrder((*v)->cur_implicit_order_index % (*v)->GetNumOrders());
    00141       DateTicks start_date = (DateTicks)_date_fract - (*v)->current_order_time;
    00142       DepartureStatus status = D_TRAVELLING;
    00143
    00144       /* If the vehicle is stopped in a depot, ignore it. */
    00145       if ((*v)->IsStoppedInDepot()) {
    00146         continue;
    00147       }
    00148
    00149       /* If the vehicle is heading for a depot to stop there, then its departures are cancelled. */
    00150       if ((*v)->current_order.IsType(OT_GOTO_DEPOT) && (*v)->current_order.GetDepotActionType() & ODATFB_HALT) {
    00151         status = D_CANCELLED;
    00152       }
    00153
    00154       if ((*v)->current_order.IsType(OT_LOADING)) {
    00155         /* Account for the vehicle having reached the current order and being in the loading phase. */
    00156         status = D_ARRIVED;
    00157         start_date -= order->travel_time + (((*v)->lateness_counter < 0) ? (*v)->lateness_counter : 0);
    00158       }
    00159
    00160       /* Loop through the vehicle's orders until we've found a suitable order or we've determined that no such order exists. */
    00161       /* We only need to consider each order at most once. */
    00162       bool foundOrder = false;
    00163       for (int i = (*v)->GetNumOrders(); i > 0; --i) {
    00164         start_date += order->travel_time + order->wait_time;
    00165
    00166         /* If the scheduled departure date is too far in the future, stop. */
    00167         if (start_date - (*v)->lateness_counter > max_date) {
    00168           break;
    00169         }
    00170
    00171         /* If the order is a conditional branch, handle it. */
    00172         if (order->IsType(OT_CONDITIONAL)) {
    00173           switch(_settings_client.gui.departure_conditionals) {
    00174               case 0: {
    00175                 /* Give up */
    00176                 break;
    00177               }
    00178               case 1: {
    00179                 /* Take the branch */
    00180                 if (status != D_CANCELLED) {
    00181                   status = D_TRAVELLING;
    00182                 }
    00183                 order = (*v)->GetOrder(order->GetConditionSkipToOrder());
    00184                 if (order == NULL) {
    00185                   break;
    00186                 }
    00187
    00188                 start_date -= order->travel_time;
    00189
    00190                 continue;
    00191               }
    00192               case 2: {
    00193                 /* Do not take the branch */
    00194                 if (status != D_CANCELLED) {
    00195                   status = D_TRAVELLING;
    00196                 }
    00197                 order = (order->next == NULL) ? (*v)->GetFirstOrder() : order->next;
    00198                 continue;
    00199               }
    00200           }
    00201         }
    00202
    00203         /* If an order doesn't have a travel time set, then stop. */
    00204         if (order->travel_time == 0) {
    00205           break;
    00206         }
    00207
    00208         /* Skip it if it's an implicit order. */
    00209         if (order->IsType(OT_IMPLICIT)) {
    00210           order = (order->next == NULL) ? (*v)->GetFirstOrder() : order->next;
    00211           continue;
    00212         }
    00213
    00214         /* If the vehicle will be stopping at and loading from this station, and its wait time is not zero, then it is a departure.
    00215          * If the vehicle will be stopping at and unloading at this station, and its wait time is not zero, then it is an arrival. */
    00216         if ((type == D_DEPARTURE && IsDeparture(order, station)) ||
    00217             (type == D_DEPARTURE && show_vehicles_via && IsVia(order, station)) ||
    00218             (type == D_ARRIVAL && IsArrival(order, station))) {
    00219           /* If the departure was scheduled to have already begun and has been cancelled, do not show it. */
    00220           if (start_date < 0 && status == D_CANCELLED) {
    00221             break;
    00222           }
    00223
    00224           OrderDate *od = new OrderDate();
    00225           od->order = order;
    00226           od->v = *v;
    00227           /* We store the expected date for now, so that vehicles will be shown in order of expected time. */
    00228           od->expected_date = start_date;
    00229           od->lateness = (*v)->lateness_counter > 0 ? (*v)->lateness_counter : 0;
    00230           od->status = status;
    00231
    00232           /* If we are early, use the scheduled date as the expected date. We also take lateness to be zero. */
    00233           if ((*v)->lateness_counter < 0 && !(*v)->current_order.IsType(OT_LOADING)) {
    00234             od->expected_date -= (*v)->lateness_counter;
    00235           }
    00236
    00237           /* Update least_order if this is the current least order. */
    00238           if (least_order == NULL) {
    00239             least_order = od;
    00240           } else if (least_order->expected_date - least_order->lateness - (type == D_ARRIVAL ? least_order->order->wait_time : 0) > od->expected_date - od->lateness - (type == D_ARRIVAL ? od->order->wait_time : 0)) {
    00241             least_order = od;
    00242           }
    00243
    00244           *(next_orders.Append(1)) = od;
    00245
    00246           foundOrder = true;
    00247           /* We're done with this vehicle. */
    00248           break;
    00249         } else {
    00250           /* Go to the next order in the list. */
    00251           if (status != D_CANCELLED) {
    00252             status = D_TRAVELLING;
    00253           }
    00254           order = (order->next == NULL) ? (*v)->GetFirstOrder() : order->next;
    00255         }
    00256       }
    00257     }
    00258   }
    00259
    00260   /* No suitable orders found? Then stop. */
    00261   if (next_orders.Length() == 0) {
    00262     return result;
    00263   }
    00264
    00265   /* We now find as many departures as we can. It's a little involved so I'll try to explain each major step. */
    00266   /* The countdown from 10000 is a safeguard just in case something nasty happens. 10000 seemed large enough. */
    00267   for(int i = 10000; i > 0; --i) {
    00268     /* I should probably try to convince you that this loop always terminates regardless of the safeguard. */
    00269     /* 1. next_orders contains at least one element. */
    00270     /* 2. The loop terminates if result->Length() exceeds a fixed (for this loop) value, or if the least order's scheduled date is later than max_date. */
    00271     /*    (We ignore the case that the least order's scheduled date has overflown, as it is a relative rather than absolute date.) */
    00272     /* 3. Every time we loop round, either result->Length() will have increased -OR- we will have increased the expected_date of one of the elements of next_orders. */
    00273     /* 4. Therefore the loop must eventually terminate. */
    00274
    00275     /* least_order is the best candidate for the next departure. */
    00276
    00277     /* First, we check if we can stop looking for departures yet. */
    00278     if (result->Length() >= _settings_client.gui.max_departures ||
    00279         least_order->expected_date - least_order->lateness > max_date) {
    00280       break;
    00281     }
    00282
    00283     /* We already know the least order and that it's a suitable departure, so make it into a departure. */
    00284     Departure *d = new Departure();
    00285     d->scheduled_date = (DateTicks)_date * DAY_TICKS + least_order->expected_date - least_order->lateness;
    00286     d->lateness = least_order->lateness;
    00287     d->status = least_order->status;
    00288     d->vehicle = least_order->v;
    00289     d->type = type;
    00290     d->order = least_order->order;
    00291
    00292     /* We'll be going through the order list later, so we need a separate variable for it. */
    00293     const Order *order = least_order->order;
    00294
    00295     if (type == D_DEPARTURE) {
    00296       /* Computing departures: */
    00297       /* We want to find out where it will terminate, making a list of the stations it calls at along the way. */
    00298       /* We only count stations where unloading happens as being called at - i.e. pickup-only stations are ignored. */
    00299       /* Where the vehicle terminates is defined as the last unique station called at by the vehicle from the current order. */
    00300
    00301       /* If the vehicle loops round to the current order without a terminus being found, then it terminates upon reaching its current order again. */
    00302
    00303       /* We also determine which station this departure is going via, if any. */
    00304       /* A departure goes via a station if it is the first station for which the vehicle has an order to go via or non-stop via. */
    00305       /* Multiple departures on the same journey may go via different stations. That a departure can go via at most one station is intentional. */
    00306
    00307       /* We keep track of potential via stations along the way. If we call at a station immediately after going via it, then it is the via station. */
    00308       StationID candidate_via = INVALID_STATION;
    00309
    00310       /* Go through the order list, looping if necessary, to find a terminus. */
    00311       /* Get the next order, which may be the vehicle's first order. */
    00312       order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00313       /* We only need to consider each order at most once. */
    00314       bool found_terminus = false;
    00315       for (int i = least_order->v->GetNumOrders(); i > 0; --i) {
    00316         /* If we reach the order at which the departure occurs again, then use the departure station as the terminus. */
    00317         if (order == least_order->order) {
    00318           /* If we're not calling anywhere, then skip this departure. */
    00319           found_terminus = (d->calling_at.Length() > 0);
    00320           break;
    00321         }
    00322
    00323         /* If the order is a conditional branch, handle it. */
    00324         if (order->IsType(OT_CONDITIONAL)) {
    00325           switch(_settings_client.gui.departure_conditionals) {
    00326               case 0: {
    00327                 /* Give up */
    00328                 break;
    00329               }
    00330               case 1: {
    00331                 /* Take the branch */
    00332                 order = least_order->v->GetOrder(order->GetConditionSkipToOrder());
    00333                 if (order == NULL) {
    00334                   break;
    00335                 }
    00336                 continue;
    00337               }
    00338               case 2: {
    00339                 /* Do not take the branch */
    00340                 order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00341                 continue;
    00342               }
    00343           }
    00344         }
    00345
    00346         /* If we reach the original station again, then use it as the terminus. */
    00347         if (order->GetType() == OT_GOTO_STATION &&
    00348             (StationID)order->GetDestination() == station &&
    00349             (order->GetUnloadType() != OUFB_NO_UNLOAD ||
    00350             _settings_client.gui.departure_show_all_stops) &&
    00351             order->GetNonStopType() != ONSF_NO_STOP_AT_ANY_STATION &&
    00352             order->GetNonStopType() != ONSF_NO_STOP_AT_DESTINATION_STATION) {
    00353           /* If we're not calling anywhere, then skip this departure. */
    00354           found_terminus = (d->calling_at.Length() > 0);
    00355           break;
    00356         }
    00357
    00358         /* Check if we're going via this station. */
    00359         if ((order->GetNonStopType() == ONSF_NO_STOP_AT_ANY_STATION ||
    00360             order->GetNonStopType() == ONSF_NO_STOP_AT_DESTINATION_STATION) &&
    00361             order->GetType() == OT_GOTO_STATION &&
    00362             d->via == INVALID_STATION) {
    00363           candidate_via = (StationID)order->GetDestination();
    00364         }
    00365
    00366         /* We're not interested in this order any further if we're not calling at it. */
    00367         if ((order->GetUnloadType() == OUFB_NO_UNLOAD &&
    00368             !_settings_client.gui.departure_show_all_stops) ||
    00369             (order->GetType() != OT_GOTO_STATION &&
    00370             order->GetType() != OT_IMPLICIT) ||
    00371             order->GetNonStopType() == ONSF_NO_STOP_AT_ANY_STATION ||
    00372             order->GetNonStopType() == ONSF_NO_STOP_AT_DESTINATION_STATION) {
    00373           order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00374           continue;
    00375         }
    00376
    00377         /* If this order's station is already in the calling, then the previous called at station is the terminus. */
    00378         if (d->calling_at.Contains((StationID)order->GetDestination())) {
    00379           found_terminus = true;
    00380           break;
    00381         }
    00382
    00383         /* If appropriate, add the station to the calling at list and make it the candidate terminus. */
    00384         if ((order->GetType() == OT_GOTO_STATION ||
    00385             order->GetType() == OT_IMPLICIT) &&
    00386             order->GetNonStopType() != ONSF_NO_STOP_AT_ANY_STATION &&
    00387             order->GetNonStopType() != ONSF_NO_STOP_AT_DESTINATION_STATION) {
    00388           if (d->via == INVALID_STATION && candidate_via == (StationID)order->GetDestination()) {
    00389             d->via = (StationID)order->GetDestination();
    00390           }
    00391           d->terminus = (StationID)order->GetDestination();
    00392           *(d->calling_at.Append(1)) = d->terminus;
    00393         }
    00394
    00395         /* If we unload all at this station, then it is the terminus. */
    00396         if (order->GetType() == OT_GOTO_STATION &&
    00397             order->GetUnloadType() == OUFB_UNLOAD) {
    00398           if (d->calling_at.Length() > 0) {
    00399             found_terminus = true;
    00400           }
    00401           break;
    00402         }
    00403
    00404         /* Get the next order, which may be the vehicle's first order. */
    00405         order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00406       }
    00407
    00408       if (found_terminus) {
    00409         /* Add the departure to the result list. */
    00410         *(result->Append(1)) = d;
    00411
    00412         /* If the vehicle is expected to be late, we want to know what time it will arrive rather than depart. */
    00413         /* This is done because it looked silly to me to have a vehicle not be expected for another few days, yet it be at the same time pulling into the station. */
    00414         if (d->status != D_ARRIVED &&
    00415             d->lateness > 0) {
    00416           d->lateness -= least_order->order->wait_time;
    00417         }
    00418       }
    00419     } else {
    00420       /* Computing arrivals: */
    00421       /* First we need to find the origin of the order. This is somewhat like finding a terminus, but a little more involved since order lists are singly linked. */
    00422       /* The next stage is simpler. We just need to add all the stations called at on the way to the current station. */
    00423       /* Again, we define a station as being called at if the vehicle loads from it. */
    00424
    00425       /* However, the very first thing we do is use the arrival time as the scheduled time instead of the departure time. */
    00426       d->scheduled_date -= order->wait_time;
    00427
    00428       const Order *candidate_origin = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00429       bool found_origin = false;
    00430
    00431       while (candidate_origin != least_order->order) {
    00432         if ((candidate_origin->GetLoadType() != OLFB_NO_LOAD ||
    00433             _settings_client.gui.departure_show_all_stops) &&
    00434             (candidate_origin->GetType() == OT_GOTO_STATION ||
    00435             candidate_origin->GetType() == OT_IMPLICIT) &&
    00436             candidate_origin->GetDestination() != station) {
    00437           const Order *o = (candidate_origin->next == NULL) ? least_order->v->GetFirstOrder() : candidate_origin->next;
    00438           bool found_collision = false;
    00439
    00440           /* Check if the candidate origin's destination appears again before the original order or the station does. */
    00441           while (o != least_order->order) {
    00442             if (o->GetUnloadType() == OUFB_UNLOAD) {
    00443               found_collision = true;
    00444               break;
    00445             }
    00446
    00447             if ((o->GetType() == OT_GOTO_STATION ||
    00448                 o->GetType() == OT_IMPLICIT) &&
    00449                 (o->GetDestination() == candidate_origin->GetDestination() ||
    00450                 o->GetDestination() == station)) {
    00451               found_collision = true;
    00452               break;
    00453             }
    00454
    00455             o = (o->next == NULL) ? least_order->v->GetFirstOrder() : o->next;
    00456           }
    00457
    00458           /* If it doesn't, then we have found the origin. */
    00459           if (!found_collision) {
    00460             found_origin = true;
    00461             break;
    00462           }
    00463         }
    00464
    00465         candidate_origin = (candidate_origin->next == NULL) ? least_order->v->GetFirstOrder() : candidate_origin->next;
    00466       }
    00467
    00468       order = (candidate_origin->next == NULL) ? least_order->v->GetFirstOrder() : candidate_origin->next;
    00469
    00470       while (order != least_order->order) {
    00471         if (order->GetType() == OT_GOTO_STATION &&
    00472             (order->GetLoadType() != OLFB_NO_LOAD ||
    00473             _settings_client.gui.departure_show_all_stops)) {
    00474           *(d->calling_at.Append(1)) = (StationID)order->GetDestination();
    00475         }
    00476
    00477         order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00478       }
    00479
    00480       d->terminus = candidate_origin->GetDestination();
    00481
    00482       if (found_origin) {
    00483         *(result->Append(1)) = d;
    00484       }
    00485     }
    00486
    00487     /* Save on pointer dereferences in the coming loop. */
    00488     order = least_order->order;
    00489
    00490     /* Now we find the next suitable order for being a departure for this vehicle. */
    00491     /* We do this in a similar way to finding the first suitable order for the vehicle. */
    00492
    00493     /* Go to the next order so we don't add the current order again. */
    00494     order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00495     least_order->expected_date += order->travel_time + order->wait_time;
    00496
    00497     /* Go through the order list to find the next candidate departure. */
    00498     /* We only need to consider each order at most once. */
    00499     bool found_next_order = false;
    00500     for (int i = least_order->v->GetNumOrders(); i > 0; --i) {
    00501       /* If the order is a conditional branch, handle it. */
    00502       if (order->IsType(OT_CONDITIONAL)) {
    00503         switch(_settings_client.gui.departure_conditionals) {
    00504             case 0: {
    00505               /* Give up */
    00506               break;
    00507             }
    00508             case 1: {
    00509               /* Take the branch */
    00510               order = least_order->v->GetOrder(order->GetConditionSkipToOrder());
    00511               if (order == NULL) {
    00512                 break;
    00513               }
    00514
    00515               least_order->expected_date += order->wait_time;
    00516
    00517               continue;
    00518             }
    00519             case 2: {
    00520               /* Do not take the branch */
    00521               order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00522               least_order->expected_date += order->travel_time + order->wait_time;
    00523               continue;
    00524             }
    00525         }
    00526       }
    00527
    00528       /* Skip it if it's an implicit order. */
    00529       if (order->IsType(OT_IMPLICIT)) {
    00530         order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00531         continue;
    00532       }
    00533
    00534       /* If an order doesn't have a travel time set, then stop. */
    00535       if (order->travel_time == 0) {
    00536         break;
    00537       }
    00538
    00539       /* If the departure is scheduled to be too late, then stop. */
    00540       if (least_order->expected_date - least_order->lateness > max_date) {
    00541         break;
    00542       }
    00543
    00544       /* If the order loads from this station (or unloads if we're computing arrivals) and has a wait time set, then it is suitable for being a departure. */
    00545       if ((type == D_DEPARTURE && IsDeparture(order, station)) ||
    00546             (type == D_DEPARTURE && show_vehicles_via && IsVia(order, station)) ||
    00547             (type == D_ARRIVAL && IsArrival(order, station))) {
    00548         least_order->order = order;
    00549         found_next_order = true;
    00550         break;
    00551       }
    00552
    00553       order = (order->next == NULL) ? least_order->v->GetFirstOrder() : order->next;
    00554       least_order->expected_date += order->travel_time + order->wait_time;
    00555     }
    00556
    00557     /* If we didn't find a suitable order for being a departure, then we can ignore this vehicle from now on. */
    00558     if (!found_next_order) {
    00559       /* Make sure we don't try to get departures out of this order. */
    00560       /* This is cheaper than deleting it from next_orders. */
    00561       /* If we ever get to a state where _date * DAY_TICKS is close to INT_MAX, then we'll have other problems anyway as departures' scheduled dates will wrap around. */
    00562       least_order->expected_date = INT64_MAX;
    00563     }
    00564
    00565     /* The vehicle can't possibly have arrived at its next candidate departure yet. */
    00566     if (least_order->status == D_ARRIVED) {
    00567       least_order->status = D_TRAVELLING;
    00568     }
    00569
    00570     /* Find the new least order. */
    00571     for (uint i = 0; i < next_orders.Length(); ++i) {
    00572       OrderDate *od = *(next_orders.Get(i));
    00573
    00574       DateTicks lod = least_order->expected_date - least_order->lateness;
    00575       DateTicks odd = od->expected_date - od->lateness;
    00576
    00577       if (type == D_ARRIVAL) {
    00578         lod -= least_order->order->wait_time;
    00579         odd -= od->order->wait_time;
    00580       }
    00581
    00582       if (lod > odd && od->expected_date - od->lateness < max_date) {
    00583         least_order = od;
    00584       }
    00585     }
    00586   }
    00587
    00588   /* Done. Phew! */
    00589   return result;
    00590 }