# order_cmd.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 "cmd_helper.h"
    00015 #include "command_func.h"
    00016 #include "company_func.h"
    00017 #include "news_func.h"
    00018 #include "vehicle_gui.h"
    00019 #include "strings_func.h"
    00020 #include "window_func.h"
    00021 #include "timetable.h"
    00022 #include "vehicle_func.h"
    00023 #include "depot_base.h"
    00024 #include "core/pool_func.hpp"
    00025 #include "aircraft.h"
    00026 #include "roadveh.h"
    00027 #include "station_base.h"
    00028 #include "waypoint_base.h"
    00029 #include "company_base.h"
    00030 #include "infrastructure_func.h"
    00031
    00032 #include "table/strings.h"
    00033
    00034 /* DestinationID must be at least as large as every these below, because it can
    00035  * be any of them
    00036  */
    00037 assert_compile(sizeof(DestinationID) >= sizeof(DepotID));
    00038 assert_compile(sizeof(DestinationID) >= sizeof(StationID));
    00039
    00040 OrderPool _order_pool("Order");
    00041 INSTANTIATE_POOL_METHODS(Order)
    00042 OrderListPool _orderlist_pool("OrderList");
    00043 INSTANTIATE_POOL_METHODS(OrderList)
    00044
    00049 void Order::Free()
    00050 {
    00051   this->type  = OT_NOTHING;
    00052   this->flags = 0;
    00053   this->dest  = 0;
    00054   this->next  = NULL;
    00055 }
    00056
    00061 void Order::MakeGoToStation(StationID destination)
    00062 {
    00063   this->type = OT_GOTO_STATION;
    00064   this->flags = 0;
    00065   this->dest = destination;
    00066 }
    00067
    00077 void Order::MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type, OrderDepotActionFlags action, CargoID cargo, byte subtype)
    00078 {
    00079   this->type = OT_GOTO_DEPOT;
    00080   this->SetDepotOrderType(order);
    00081   this->SetDepotActionType(action);
    00082   this->SetNonStopType(non_stop_type);
    00083   this->dest = destination;
    00084   this->SetRefit(cargo, subtype);
    00085 }
    00086
    00091 void Order::MakeGoToWaypoint(StationID destination)
    00092 {
    00093   this->type = OT_GOTO_WAYPOINT;
    00094   this->flags = 0;
    00095   this->dest = destination;
    00096 }
    00097
    00102 void Order::MakeLoading(bool ordered)
    00103 {
    00104   this->type = OT_LOADING;
    00105   if (!ordered) this->flags = 0;
    00106 }
    00107
    00111 void Order::MakeLeaveStation()
    00112 {
    00113   this->type = OT_LEAVESTATION;
    00114   this->flags = 0;
    00115 }
    00116
    00120 void Order::MakeDummy()
    00121 {
    00122   this->type = OT_DUMMY;
    00123   this->flags = 0;
    00124 }
    00125
    00130 void Order::MakeConditional(VehicleOrderID order)
    00131 {
    00132   this->type = OT_CONDITIONAL;
    00133   this->flags = order;
    00134   this->dest = 0;
    00135 }
    00136
    00141 void Order::MakeImplicit(StationID destination)
    00142 {
    00143   this->type = OT_IMPLICIT;
    00144   this->dest = destination;
    00145 }
    00146
    00153 void Order::SetRefit(CargoID cargo, byte subtype)
    00154 {
    00155   this->refit_cargo = cargo;
    00156   this->refit_subtype = subtype;
    00157 }
    00158
    00164 bool Order::Equals(const Order &other) const
    00165 {
    00166   /* In case of go to nearest depot orders we need "only" compare the flags
    00167    * with the other and not the nearest depot order bit or the actual
    00168    * destination because those get clear/filled in during the order
    00169    * evaluation. If we do not do this the order will continuously be seen as
    00170    * a different order and it will try to find a "nearest depot" every tick. */
    00171   if ((this->IsType(OT_GOTO_DEPOT) && this->type == other.type) &&
    00172       ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0 ||
    00173        (other.GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0)) {
    00174     return this->GetDepotOrderType() == other.GetDepotOrderType() &&
    00175         (this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT) == (other.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT);
    00176   }
    00177
    00178   return this->type == other.type && this->flags == other.flags && this->dest == other.dest;
    00179 }
    00180
    00187 uint32 Order::Pack() const
    00188 {
    00189   return this->dest << 16 | this->flags << 8 | this->type;
    00190 }
    00191
    00197 uint16 Order::MapOldOrder() const
    00198 {
    00199   uint16 order = this->GetType();
    00200   switch (this->type) {
    00201     case OT_GOTO_STATION:
    00202       if (this->GetUnloadType() & OUFB_UNLOAD) SetBit(order, 5);
    00203       if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
    00204       if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) SetBit(order, 7);
    00205       order |= GB(this->GetDestination(), 0, 8) << 8;
    00206       break;
    00207     case OT_GOTO_DEPOT:
    00208       if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) SetBit(order, 6);
    00209       SetBit(order, 7);
    00210       order |= GB(this->GetDestination(), 0, 8) << 8;
    00211       break;
    00212     case OT_LOADING:
    00213       if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
    00214       break;
    00215   }
    00216   return order;
    00217 }
    00218
    00223 Order::Order(uint32 packed)
    00224 {
    00225   this->type    = (OrderType)GB(packed,  0,  8);
    00226   this->flags   = GB(packed,  8,  8);
    00227   this->dest    = GB(packed, 16, 16);
    00228   this->next    = NULL;
    00229   this->refit_cargo   = CT_NO_REFIT;
    00230   this->refit_subtype = 0;
    00231   this->wait_time     = 0;
    00232   this->travel_time   = 0;
    00233 }
    00234
    00240 void InvalidateVehicleOrder(const Vehicle *v, int data)
    00241 {
    00242   SetWindowDirty(WC_VEHICLE_VIEW, v->index);
    00243
    00244   if (data != 0) {
    00245     /* Calls SetDirty() too */
    00246     InvalidateWindowData(WC_VEHICLE_ORDERS,    v->index, data);
    00247     InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
    00248     return;
    00249   }
    00250
    00251   SetWindowDirty(WC_VEHICLE_ORDERS,    v->index);
    00252   SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
    00253 }
    00254
    00262 void Order::AssignOrder(const Order &other)
    00263 {
    00264   this->type  = other.type;
    00265   this->flags = other.flags;
    00266   this->dest  = other.dest;
    00267
    00268   this->refit_cargo   = other.refit_cargo;
    00269   this->refit_subtype = other.refit_subtype;
    00270
    00271   this->wait_time   = other.wait_time;
    00272   this->travel_time = other.travel_time;
    00273 }
    00274
    00280 void OrderList::Initialize(Order *chain, Vehicle *v)
    00281 {
    00282   this->first = chain;
    00283   this->first_shared = v;
    00284
    00285   this->num_orders = 0;
    00286   this->num_manual_orders = 0;
    00287   this->num_vehicles = 1;
    00288   this->timetable_duration = 0;
    00289
    00290   for (Order *o = this->first; o != NULL; o = o->next) {
    00291     ++this->num_orders;
    00292     if (!o->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
    00293     this->timetable_duration += o->wait_time + o->travel_time;
    00294   }
    00295
    00296   for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
    00297     ++this->num_vehicles;
    00298     this->first_shared = u;
    00299   }
    00300
    00301   for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
    00302 }
    00303
    00309 void OrderList::FreeChain(bool keep_orderlist)
    00310 {
    00311   Order *next;
    00312   for (Order *o = this->first; o != NULL; o = next) {
    00313     next = o->next;
    00314     delete o;
    00315   }
    00316
    00317   if (keep_orderlist) {
    00318     this->first = NULL;
    00319     this->num_orders = 0;
    00320     this->num_manual_orders = 0;
    00321     this->timetable_duration = 0;
    00322   } else {
    00323     delete this;
    00324   }
    00325 }
    00326
    00332 Order *OrderList::GetOrderAt(int index) const
    00333 {
    00334   if (index < 0) return NULL;
    00335
    00336   Order *order = this->first;
    00337
    00338   while (order != NULL && index-- > 0) {
    00339     order = order->next;
    00340   }
    00341   return order;
    00342 }
    00343
    00351 StationID OrderList::GetNextStoppingStation(const Order *next, StationID curr_station, uint hops) const
    00352 {
    00353   if (next == NULL || hops > this->GetNumOrders()) {
    00354     return INVALID_STATION;
    00355   }
    00356
    00357   if (next->IsType(OT_CONDITIONAL)) {
    00358     StationID skip_to = this->GetNextStoppingStation(this->GetOrderAt(next->GetConditionSkipToOrder()), curr_station, hops + 1);
    00359     StationID advance = this->GetNextStoppingStation(this->GetNext(next), curr_station, hops + 1);
    00360     return (skip_to == advance) ? skip_to : INVALID_STATION;
    00361   }
    00362
    00363   if (!(next->IsType(OT_GOTO_STATION) || next->IsType(OT_IMPLICIT)) ||
    00364       (next->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) != 0 ||
    00365       next->GetDestination() == curr_station) {
    00366     return GetNextStoppingStation(this->GetNext(next), curr_station, hops + 1);
    00367   }
    00368
    00369   return next->GetDestination();
    00370 }
    00371
    00378 StationID OrderList::GetNextStoppingStation(VehicleOrderID curr_order, StationID curr_station) const
    00379 {
    00380   const Order *curr = this->GetOrderAt(curr_order);
    00381   if (curr == NULL) {
    00382     curr = this->GetFirstOrder();
    00383     if (curr == NULL) {
    00384       return INVALID_STATION;
    00385     }
    00386   }
    00387
    00388   /* If we're not at a station or the current order doesn't yield the station
    00389    * we're at, we have to check the current order; otherwise we have to check
    00390    * the next one.
    00391    */
    00392   if (curr_station == INVALID_STATION ||
    00393       !(curr->IsType(OT_GOTO_STATION) || curr->IsType(OT_IMPLICIT)) ||
    00394       curr_station != curr->GetDestination()) {
    00395     return this->GetNextStoppingStation(curr, curr_station, 0);
    00396   } else {
    00397     return this->GetNextStoppingStation(this->GetNext(curr), curr_station, 1);
    00398   }
    00399 }
    00400
    00406 void OrderList::InsertOrderAt(Order *new_order, int index)
    00407 {
    00408   if (this->first == NULL) {
    00409     this->first = new_order;
    00410   } else {
    00411     if (index == 0) {
    00412       /* Insert as first or only order */
    00413       new_order->next = this->first;
    00414       this->first = new_order;
    00415     } else if (index >= this->num_orders) {
    00416       /* index is after the last order, add it to the end */
    00417       this->GetLastOrder()->next = new_order;
    00418     } else {
    00419       /* Put the new order in between */
    00420       Order *order = this->GetOrderAt(index - 1);
    00421       new_order->next = order->next;
    00422       order->next = new_order;
    00423     }
    00424   }
    00425   ++this->num_orders;
    00426   if (!new_order->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
    00427   this->timetable_duration += new_order->wait_time + new_order->travel_time;
    00428 }
    00429
    00430
    00435 void OrderList::DeleteOrderAt(int index)
    00436 {
    00437   if (index >= this->num_orders) return;
    00438
    00439   Order *to_remove;
    00440
    00441   if (index == 0) {
    00442     to_remove = this->first;
    00443     this->first = to_remove->next;
    00444   } else {
    00445     Order *prev = GetOrderAt(index - 1);
    00446     to_remove = prev->next;
    00447     prev->next = to_remove->next;
    00448   }
    00449   --this->num_orders;
    00450   if (!to_remove->IsType(OT_IMPLICIT)) --this->num_manual_orders;
    00451   this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
    00452   delete to_remove;
    00453 }
    00454
    00460 void OrderList::MoveOrder(int from, int to)
    00461 {
    00462   if (from >= this->num_orders || to >= this->num_orders || from == to) return;
    00463
    00464   Order *moving_one;
    00465
    00466   /* Take the moving order out of the pointer-chain */
    00467   if (from == 0) {
    00468     moving_one = this->first;
    00469     this->first = moving_one->next;
    00470   } else {
    00471     Order *one_before = GetOrderAt(from - 1);
    00472     moving_one = one_before->next;
    00473     one_before->next = moving_one->next;
    00474   }
    00475
    00476   /* Insert the moving_order again in the pointer-chain */
    00477   if (to == 0) {
    00478     moving_one->next = this->first;
    00479     this->first = moving_one;
    00480   } else {
    00481     Order *one_before = GetOrderAt(to - 1);
    00482     moving_one->next = one_before->next;
    00483     one_before->next = moving_one;
    00484   }
    00485 }
    00486
    00492 void OrderList::RemoveVehicle(Vehicle *v)
    00493 {
    00494   --this->num_vehicles;
    00495   if (v == this->first_shared) this->first_shared = v->NextShared();
    00496 }
    00497
    00502 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
    00503 {
    00504   for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
    00505     if (v_shared == v) return true;
    00506   }
    00507
    00508   return false;
    00509 }
    00510
    00516 int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
    00517 {
    00518   int count = 0;
    00519   for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
    00520   return count;
    00521 }
    00522
    00527 bool OrderList::IsCompleteTimetable() const
    00528 {
    00529   for (Order *o = this->first; o != NULL; o = o->next) {
    00530     /* Implicit orders are, by definition, not timetabled. */
    00531     if (o->IsType(OT_IMPLICIT)) continue;
    00532     if (!o->IsCompletelyTimetabled()) return false;
    00533   }
    00534   return true;
    00535 }
    00536
    00540 void OrderList::DebugCheckSanity() const
    00541 {
    00542   VehicleOrderID check_num_orders = 0;
    00543   VehicleOrderID check_num_manual_orders = 0;
    00544   uint check_num_vehicles = 0;
    00545   Ticks check_timetable_duration = 0;
    00546
    00547   DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
    00548
    00549   for (const Order *o = this->first; o != NULL; o = o->next) {
    00550     ++check_num_orders;
    00551     if (!o->IsType(OT_IMPLICIT)) ++check_num_manual_orders;
    00552     check_timetable_duration += o->wait_time + o->travel_time;
    00553   }
    00554   assert(this->num_orders == check_num_orders);
    00555   assert(this->num_manual_orders == check_num_manual_orders);
    00556   assert(this->timetable_duration == check_timetable_duration);
    00557
    00558   for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
    00559     ++check_num_vehicles;
    00560     assert(v->orders.list == this);
    00561   }
    00562   assert(this->num_vehicles == check_num_vehicles);
    00563   DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
    00564       (uint)this->num_orders, (uint)this->num_manual_orders,
    00565       this->num_vehicles, this->timetable_duration);
    00566 }
    00567
    00575 static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
    00576 {
    00577   return o->IsType(OT_GOTO_STATION) ||
    00578       (v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
    00579 }
    00580
    00587 static void DeleteOrderWarnings(const Vehicle *v)
    00588 {
    00589   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
    00590   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
    00591   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
    00592   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
    00593 }
    00594
    00600 TileIndex Order::GetLocation(const Vehicle *v) const
    00601 {
    00602   switch (this->GetType()) {
    00603     case OT_GOTO_WAYPOINT:
    00604     case OT_GOTO_STATION:
    00605     case OT_IMPLICIT:
    00606       return BaseStation::Get(this->GetDestination())->xy;
    00607
    00608     case OT_GOTO_DEPOT:
    00609       if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
    00610       return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
    00611
    00612     default:
    00613       return INVALID_TILE;
    00614   }
    00615 }
    00616
    00617 static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
    00618 {
    00619   assert(v->type == VEH_SHIP);
    00620
    00621   if (cur->IsType(OT_CONDITIONAL)) {
    00622     if (conditional_depth > v->GetNumOrders()) return 0;
    00623
    00624     conditional_depth++;
    00625
    00626     int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
    00627     int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
    00628     return max(dist1, dist2);
    00629   }
    00630
    00631   TileIndex prev_tile = prev->GetLocation(v);
    00632   TileIndex cur_tile = cur->GetLocation(v);
    00633   if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
    00634   return DistanceManhattan(prev_tile, cur_tile);
    00635 }
    00636
    00650 CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00651 {
    00652   VehicleID veh          = GB(p1,  0, 20);
    00653   VehicleOrderID sel_ord = GB(p1, 20, 8);
    00654   Order new_order(p2);
    00655
    00656   Vehicle *v = Vehicle::GetIfValid(veh);
    00657   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00658
    00659   CommandCost ret = CheckOwnership(v->owner);
    00660   if (ret.Failed()) return ret;
    00661
    00662   /* Check if the inserted order is to the correct destination (owner, type),
    00663    * and has the correct flags if any */
    00664   switch (new_order.GetType()) {
    00665     case OT_GOTO_STATION: {
    00666       const Station *st = Station::GetIfValid(new_order.GetDestination());
    00667       if (st == NULL) return CMD_ERROR;
    00668
    00669       if (st->owner != OWNER_NONE) {
    00670         CommandCost ret = CheckInfraUsageAllowed(v->type, st->owner);
    00671         if (ret.Failed()) return ret;
    00672       }
    00673
    00674       if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
    00675       for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
    00676         if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
    00677       }
    00678
    00679       /* Non stop only allowed for ground vehicles. */
    00680       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
    00681
    00682       /* Filter invalid load/unload types. */
    00683       switch (new_order.GetLoadType()) {
    00684         case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
    00685         default: return CMD_ERROR;
    00686       }
    00687       switch (new_order.GetUnloadType()) {
    00688         case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
    00689         default: return CMD_ERROR;
    00690       }
    00691
    00692       /* Filter invalid stop locations */
    00693       switch (new_order.GetStopLocation()) {
    00694         case OSL_PLATFORM_NEAR_END:
    00695         case OSL_PLATFORM_MIDDLE:
    00696           if (v->type != VEH_TRAIN) return CMD_ERROR;
    00697           /* FALL THROUGH */
    00698         case OSL_PLATFORM_FAR_END:
    00699           break;
    00700
    00701         default:
    00702           return CMD_ERROR;
    00703       }
    00704
    00705       break;
    00706     }
    00707
    00708     case OT_GOTO_DEPOT: {
    00709       if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
    00710         if (v->type == VEH_AIRCRAFT) {
    00711           const Station *st = Station::GetIfValid(new_order.GetDestination());
    00712
    00713           if (st == NULL) return CMD_ERROR;
    00714
    00715           CommandCost ret = CheckInfraUsageAllowed(v->type, st->owner);
    00716           if (ret.Failed()) return ret;
    00717
    00718           if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
    00719             return CMD_ERROR;
    00720           }
    00721         } else {
    00722           const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
    00723
    00724           if (dp == NULL) return CMD_ERROR;
    00725
    00726           CommandCost ret = CheckInfraUsageAllowed(v->type, GetTileOwner(dp->xy), dp->xy);
    00727           if (ret.Failed()) return ret;
    00728
    00729           switch (v->type) {
    00730             case VEH_TRAIN:
    00731               if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
    00732               break;
    00733
    00734             case VEH_ROAD:
    00735               if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
    00736               break;
    00737
    00738             case VEH_SHIP:
    00739               if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
    00740               break;
    00741
    00742             default: return CMD_ERROR;
    00743           }
    00744         }
    00745       }
    00746
    00747       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
    00748       if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
    00749       if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
    00750       if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
    00751       break;
    00752     }
    00753
    00754     case OT_GOTO_WAYPOINT: {
    00755       const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
    00756       if (wp == NULL) return CMD_ERROR;
    00757
    00758       switch (v->type) {
    00759         default: return CMD_ERROR;
    00760
    00761         case VEH_TRAIN: {
    00762           if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
    00763
    00764           CommandCost ret = CheckInfraUsageAllowed(v->type, wp->owner);
    00765           if (ret.Failed()) return ret;
    00766           break;
    00767         }
    00768
    00769         case VEH_SHIP:
    00770           if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
    00771           if (wp->owner != OWNER_NONE) {
    00772             CommandCost ret = CheckInfraUsageAllowed(v->type, wp->owner);
    00773             if (ret.Failed()) return ret;
    00774           }
    00775           break;
    00776       }
    00777
    00778       /* Order flags can be any of the following for waypoints:
    00779        * [non-stop]
    00780        * non-stop orders (if any) are only valid for trains */
    00781       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
    00782       break;
    00783     }
    00784
    00785     case OT_CONDITIONAL: {
    00786       VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
    00787       if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR; // Always allow jumping to the first (even when there is no order).
    00788       if (new_order.GetConditionVariable() > OCV_END) return CMD_ERROR;
    00789
    00790       OrderConditionComparator occ = new_order.GetConditionComparator();
    00791       if (occ > OCC_END) return CMD_ERROR;
    00792       switch (new_order.GetConditionVariable()) {
    00793         case OCV_REQUIRES_SERVICE:
    00794           if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
    00795           break;
    00796
    00797         case OCV_UNCONDITIONALLY:
    00798           if (occ != OCC_EQUALS) return CMD_ERROR;
    00799           if (new_order.GetConditionValue() != 0) return CMD_ERROR;
    00800           break;
    00801
    00802         case OCV_LOAD_PERCENTAGE:
    00803         case OCV_RELIABILITY:
    00804           if (new_order.GetConditionValue() > 100) return CMD_ERROR;
    00805           /* FALL THROUGH */
    00806         default:
    00807           if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
    00808           break;
    00809       }
    00810       break;
    00811     }
    00812
    00813     default: return CMD_ERROR;
    00814   }
    00815
    00816   if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
    00817
    00818   if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
    00819   if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    00820   if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    00821
    00822   if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
    00823     /* Make sure the new destination is not too far away from the previous */
    00824     const Order *prev = NULL;
    00825     uint n = 0;
    00826
    00827     /* Find the last goto station or depot order before the insert location.
    00828      * If the order is to be inserted at the beginning of the order list this
    00829      * finds the last order in the list. */
    00830     const Order *o;
    00831     FOR_VEHICLE_ORDERS(v, o) {
    00832       switch (o->GetType()) {
    00833         case OT_GOTO_STATION:
    00834         case OT_GOTO_DEPOT:
    00835         case OT_GOTO_WAYPOINT:
    00836           prev = o;
    00837           break;
    00838
    00839         default: break;
    00840       }
    00841       if (++n == sel_ord && prev != NULL) break;
    00842     }
    00843     if (prev != NULL) {
    00844       uint dist = GetOrderDistance(prev, &new_order, v);
    00845       if (dist >= 130) {
    00846         return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
    00847       }
    00848     }
    00849   }
    00850
    00851   if (flags & DC_EXEC) {
    00852     Order *new_o = new Order();
    00853     new_o->AssignOrder(new_order);
    00854     InsertOrder(v, new_o, sel_ord);
    00855   }
    00856
    00857   return CommandCost();
    00858 }
    00859
    00866 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
    00867 {
    00868   /* Create new order and link in list */
    00869   if (v->orders.list == NULL) {
    00870     v->orders.list = new OrderList(new_o, v);
    00871   } else {
    00872     v->orders.list->InsertOrderAt(new_o, sel_ord);
    00873   }
    00874
    00875   Vehicle *u = v->FirstShared();
    00876   DeleteOrderWarnings(u);
    00877   for (; u != NULL; u = u->NextShared()) {
    00878     assert(v->orders.list == u->orders.list);
    00879
    00880     /* If there is added an order before the current one, we need
    00881      * to update the selected order. We do not change implicit/real order indices though.
    00882      * If the new order is between the current implicit order and real order, the implicit order will
    00883      * later skip the inserted order. */
    00884     if (sel_ord <= u->cur_real_order_index) {
    00885       uint cur = u->cur_real_order_index + 1;
    00886       /* Check if we don't go out of bound */
    00887       if (cur < u->GetNumOrders()) {
    00888         u->cur_real_order_index = cur;
    00889       }
    00890     }
    00891     if (sel_ord == u->cur_implicit_order_index && u->IsGroundVehicle()) {
    00892       /* We are inserting an order just before the current implicit order.
    00893        * We do not know whether we will reach current implicit or the newly inserted order first.
    00894        * So, disable creation of implicit orders until we are on track again. */
    00895       uint16 &gv_flags = u->GetGroundVehicleFlags();
    00896       SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
    00897     }
    00898     if (sel_ord <= u->cur_implicit_order_index) {
    00899       uint cur = u->cur_implicit_order_index + 1;
    00900       /* Check if we don't go out of bound */
    00901       if (cur < u->GetNumOrders()) {
    00902         u->cur_implicit_order_index = cur;
    00903       }
    00904     }
    00905     /* Update any possible open window of the vehicle */
    00906     InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
    00907
    00908     RecalcFrozenIfLoading(u);
    00909   }
    00910
    00911   /* As we insert an order, the order to skip to will be 'wrong'. */
    00912   VehicleOrderID cur_order_id = 0;
    00913   Order *order;
    00914   FOR_VEHICLE_ORDERS(v, order) {
    00915     if (order->IsType(OT_CONDITIONAL)) {
    00916       VehicleOrderID order_id = order->GetConditionSkipToOrder();
    00917       if (order_id >= sel_ord) {
    00918         order->SetConditionSkipToOrder(order_id + 1);
    00919       }
    00920       if (order_id == cur_order_id) {
    00921         order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
    00922       }
    00923     }
    00924     cur_order_id++;
    00925   }
    00926
    00927   /* Make sure to rebuild the whole list */
    00928   InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
    00929 }
    00930
    00936 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
    00937 {
    00938   if (flags & DC_EXEC) {
    00939     DeleteVehicleOrders(dst);
    00940     InvalidateVehicleOrder(dst, -1);
    00941
    00942     RecalcFrozenIfLoading(dst);
    00943
    00944     InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
    00945   }
    00946   return CommandCost();
    00947 }
    00948
    00958 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00959 {
    00960   VehicleID veh_id = GB(p1, 0, 20);
    00961   VehicleOrderID sel_ord = GB(p2, 0, 8);
    00962
    00963   Vehicle *v = Vehicle::GetIfValid(veh_id);
    00964
    00965   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00966
    00967   CommandCost ret = CheckOwnership(v->owner);
    00968   if (ret.Failed()) return ret;
    00969
    00970   /* If we did not select an order, we maybe want to de-clone the orders */
    00971   if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
    00972
    00973   if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
    00974
    00975   if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
    00976   return CommandCost();
    00977 }
    00978
    00983 static void CancelLoadingDueToDeletedOrder(Vehicle *v)
    00984 {
    00985   assert(v->current_order.IsType(OT_LOADING));
    00986   /* NON-stop flag is misused to see if a train is in a station that is
    00987    * on his order list or not */
    00988   v->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
    00989   /* When full loading, "cancel" that order so the vehicle doesn't
    00990    * stay indefinitely at this station anymore. */
    00991   if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) v->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
    00992 }
    00993
    00999 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
    01000 {
    01001   v->orders.list->DeleteOrderAt(sel_ord);
    01002
    01003   Vehicle *u = v->FirstShared();
    01004   DeleteOrderWarnings(u);
    01005   for (; u != NULL; u = u->NextShared()) {
    01006     assert(v->orders.list == u->orders.list);
    01007
    01008     if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
    01009       CancelLoadingDueToDeletedOrder(u);
    01010     }
    01011
    01012     if (sel_ord < u->cur_real_order_index) {
    01013       u->cur_real_order_index--;
    01014     } else if (sel_ord == u->cur_real_order_index) {
    01015       u->UpdateRealOrderIndex();
    01016     }
    01017
    01018     if (sel_ord < u->cur_implicit_order_index) {
    01019       u->cur_implicit_order_index--;
    01020     } else if (sel_ord == u->cur_implicit_order_index) {
    01021       /* Make sure the index is valid */
    01022       if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
    01023
    01024       /* Skip non-implicit orders for the implicit-order-index (e.g. if the current implicit order was deleted */
    01025       while (u->cur_implicit_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_implicit_order_index)->IsType(OT_IMPLICIT)) {
    01026         u->cur_implicit_order_index++;
    01027         if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
    01028       }
    01029     }
    01030
    01031     /* Update any possible open window of the vehicle */
    01032     InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
    01033
    01034     RecalcFrozenIfLoading(u);
    01035   }
    01036
    01037   /* As we delete an order, the order to skip to will be 'wrong'. */
    01038   VehicleOrderID cur_order_id = 0;
    01039   Order *order = NULL;
    01040   FOR_VEHICLE_ORDERS(v, order) {
    01041     if (order->IsType(OT_CONDITIONAL)) {
    01042       VehicleOrderID order_id = order->GetConditionSkipToOrder();
    01043       if (order_id >= sel_ord) {
    01044         order_id = max(order_id - 1, 0);
    01045       }
    01046       if (order_id == cur_order_id) {
    01047         order_id = (order_id + 1) % v->GetNumOrders();
    01048       }
    01049       order->SetConditionSkipToOrder(order_id);
    01050     }
    01051     cur_order_id++;
    01052   }
    01053
    01054   InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
    01055 }
    01056
    01066 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01067 {
    01068   VehicleID veh_id = GB(p1, 0, 20);
    01069   VehicleOrderID sel_ord = GB(p2, 0, 8);
    01070
    01071   Vehicle *v = Vehicle::GetIfValid(veh_id);
    01072
    01073   if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_implicit_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
    01074
    01075   CommandCost ret = CheckOwnership(v->owner);
    01076   if (ret.Failed()) return ret;
    01077
    01078   if (flags & DC_EXEC) {
    01079     if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
    01080
    01081     v->cur_implicit_order_index = v->cur_real_order_index = sel_ord;
    01082     v->UpdateRealOrderIndex();
    01083
    01084     InvalidateVehicleOrder(v, -2);
    01085
    01086     if (_settings_game.order.timetable_separation) v->ClearSeparation();
    01087     if (_settings_game.order.timetable_separation) ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
    01088   }
    01089
    01090   /* We have an aircraft/ship, they have a mini-schedule, so update them all */
    01091   if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
    01092   if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
    01093
    01094   return CommandCost();
    01095 }
    01096
    01110 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01111 {
    01112   VehicleID veh = GB(p1, 0, 20);
    01113   VehicleOrderID moving_order = GB(p2,  0, 16);
    01114   VehicleOrderID target_order = GB(p2, 16, 16);
    01115
    01116   Vehicle *v = Vehicle::GetIfValid(veh);
    01117   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    01118
    01119   CommandCost ret = CheckOwnership(v->owner);
    01120   if (ret.Failed()) return ret;
    01121
    01122   /* Don't make senseless movements */
    01123   if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
    01124       moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
    01125
    01126   Order *moving_one = v->GetOrder(moving_order);
    01127   /* Don't move an empty order */
    01128   if (moving_one == NULL) return CMD_ERROR;
    01129
    01130   if (flags & DC_EXEC) {
    01131     v->orders.list->MoveOrder(moving_order, target_order);
    01132
    01133     /* Update shared list */
    01134     Vehicle *u = v->FirstShared();
    01135
    01136     DeleteOrderWarnings(u);
    01137
    01138     for (; u != NULL; u = u->NextShared()) {
    01139       /* Update the current order.
    01140        * There are multiple ways to move orders, which result in cur_implicit_order_index
    01141        * and cur_real_order_index to not longer make any sense. E.g. moving another
    01142        * real order between them.
    01143        *
    01144        * Basically one could choose to preserve either of them, but not both.
    01145        * While both ways are suitable in this or that case from a human point of view, neither
    01146        * of them makes really sense.
    01147        * However, from an AI point of view, preserving cur_real_order_index is the most
    01148        * predictable and transparent behaviour.
    01149        *
    01150        * With that decision it basically does not matter what we do to cur_implicit_order_index.
    01151        * If we change orders between the implict- and real-index, the implicit orders are mostly likely
    01152        * completely out-dated anyway. So, keep it simple and just keep cur_implicit_order_index as well.
    01153        * The worst which can happen is that a lot of implicit orders are removed when reaching current_order.
    01154        */
    01155       if (u->cur_real_order_index == moving_order) {
    01156         u->cur_real_order_index = target_order;
    01157       } else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
    01158         u->cur_real_order_index--;
    01159       } else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
    01160         u->cur_real_order_index++;
    01161       }
    01162
    01163       if (u->cur_implicit_order_index == moving_order) {
    01164         u->cur_implicit_order_index = target_order;
    01165       } else if (u->cur_implicit_order_index > moving_order && u->cur_implicit_order_index <= target_order) {
    01166         u->cur_implicit_order_index--;
    01167       } else if (u->cur_implicit_order_index < moving_order && u->cur_implicit_order_index >= target_order) {
    01168         u->cur_implicit_order_index++;
    01169       }
    01170
    01171       assert(v->orders.list == u->orders.list);
    01172       /* Update any possible open window of the vehicle */
    01173       InvalidateVehicleOrder(u, moving_order | (target_order << 8));
    01174
    01175       RecalcFrozenIfLoading(u);
    01176     }
    01177
    01178     /* As we move an order, the order to skip to will be 'wrong'. */
    01179     Order *order;
    01180     FOR_VEHICLE_ORDERS(v, order) {
    01181       if (order->IsType(OT_CONDITIONAL)) {
    01182         VehicleOrderID order_id = order->GetConditionSkipToOrder();
    01183         if (order_id == moving_order) {
    01184           order_id = target_order;
    01185         } else if (order_id > moving_order && order_id <= target_order) {
    01186           order_id--;
    01187         } else if (order_id < moving_order && order_id >= target_order) {
    01188           order_id++;
    01189         }
    01190         order->SetConditionSkipToOrder(order_id);
    01191       }
    01192     }
    01193
    01194     /* Make sure to rebuild the whole list */
    01195     InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
    01196   }
    01197
    01198   return CommandCost();
    01199 }
    01200
    01216 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01217 {
    01218   VehicleOrderID sel_ord = GB(p1, 20,  8);
    01219   VehicleID veh          = GB(p1,  0, 20);
    01220   ModifyOrderFlags mof   = Extract<ModifyOrderFlags, 0, 4>(p2);
    01221   uint16 data            = GB(p2,  4, 11);
    01222
    01223   if (mof >= MOF_END) return CMD_ERROR;
    01224
    01225   Vehicle *v = Vehicle::GetIfValid(veh);
    01226   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    01227
    01228   CommandCost ret = CheckOwnership(v->owner);
    01229   if (ret.Failed()) return ret;
    01230
    01231   /* Is it a valid order? */
    01232   if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
    01233
    01234   Order *order = v->GetOrder(sel_ord);
    01235   switch (order->GetType()) {
    01236     case OT_GOTO_STATION:
    01237       if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
    01238       break;
    01239
    01240     case OT_GOTO_DEPOT:
    01241       if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
    01242       break;
    01243
    01244     case OT_GOTO_WAYPOINT:
    01245       if (mof != MOF_NON_STOP) return CMD_ERROR;
    01246       break;
    01247
    01248     case OT_CONDITIONAL:
    01249       if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
    01250       break;
    01251
    01252     default:
    01253       return CMD_ERROR;
    01254   }
    01255
    01256   switch (mof) {
    01257     default: NOT_REACHED();
    01258
    01259     case MOF_NON_STOP:
    01260       if (!v->IsGroundVehicle()) return CMD_ERROR;
    01261       if (data >= ONSF_END) return CMD_ERROR;
    01262       if (data == order->GetNonStopType()) return CMD_ERROR;
    01263       break;
    01264
    01265     case MOF_STOP_LOCATION:
    01266       if (v->type != VEH_TRAIN) return CMD_ERROR;
    01267       if (data >= OSL_END) return CMD_ERROR;
    01268       break;
    01269
    01270     case MOF_UNLOAD:
    01271       if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
    01272       /* Unload and no-unload are mutual exclusive and so are transfer and no unload. */
    01273       if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
    01274       if (data == order->GetUnloadType()) return CMD_ERROR;
    01275       break;
    01276
    01277     case MOF_LOAD:
    01278       if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
    01279       if (data == order->GetLoadType()) return CMD_ERROR;
    01280       break;
    01281
    01282     case MOF_DEPOT_ACTION:
    01283       if (data >= DA_END) return CMD_ERROR;
    01284       break;
    01285
    01286     case MOF_COND_VARIABLE:
    01287       if (data >= OCV_END) return CMD_ERROR;
    01288       break;
    01289
    01290     case MOF_COND_COMPARATOR:
    01291       if (data >= OCC_END) return CMD_ERROR;
    01292       switch (order->GetConditionVariable()) {
    01293         case OCV_UNCONDITIONALLY: return CMD_ERROR;
    01294
    01295         case OCV_REQUIRES_SERVICE:
    01296           if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
    01297           break;
    01298
    01299         default:
    01300           if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
    01301           break;
    01302       }
    01303       break;
    01304
    01305     case MOF_COND_VALUE:
    01306       switch (order->GetConditionVariable()) {
    01307         case OCV_UNCONDITIONALLY: return CMD_ERROR;
    01308
    01309         case OCV_LOAD_PERCENTAGE:
    01310         case OCV_RELIABILITY:
    01311           if (data > 100) return CMD_ERROR;
    01312           break;
    01313
    01314         default:
    01315           if (data > 2047) return CMD_ERROR;
    01316           break;
    01317       }
    01318       break;
    01319
    01320     case MOF_COND_DESTINATION:
    01321       if (data >= v->GetNumOrders()) return CMD_ERROR;
    01322       break;
    01323   }
    01324
    01325   if (flags & DC_EXEC) {
    01326     switch (mof) {
    01327       case MOF_NON_STOP:
    01328         order->SetNonStopType((OrderNonStopFlags)data);
    01329         break;
    01330
    01331       case MOF_STOP_LOCATION:
    01332         order->SetStopLocation((OrderStopLocation)data);
    01333         break;
    01334
    01335       case MOF_UNLOAD:
    01336         order->SetUnloadType((OrderUnloadFlags)data);
    01337         break;
    01338
    01339       case MOF_LOAD:
    01340         order->SetLoadType((OrderLoadFlags)data);
    01341         break;
    01342
    01343       case MOF_DEPOT_ACTION: {
    01344         switch (data) {
    01345           case DA_ALWAYS_GO:
    01346             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
    01347             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
    01348             break;
    01349
    01350           case DA_SERVICE:
    01351             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
    01352             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
    01353             break;
    01354
    01355           case DA_STOP:
    01356             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
    01357             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
    01358             break;
    01359
    01360           default:
    01361             NOT_REACHED();
    01362         }
    01363         break;
    01364       }
    01365
    01366       case MOF_COND_VARIABLE: {
    01367         order->SetConditionVariable((OrderConditionVariable)data);
    01368
    01369         OrderConditionComparator occ = order->GetConditionComparator();
    01370         switch (order->GetConditionVariable()) {
    01371           case OCV_UNCONDITIONALLY:
    01372             order->SetConditionComparator(OCC_EQUALS);
    01373             order->SetConditionValue(0);
    01374             break;
    01375
    01376           case OCV_REQUIRES_SERVICE:
    01377             if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
    01378             break;
    01379
    01380           case OCV_LOAD_PERCENTAGE:
    01381           case OCV_RELIABILITY:
    01382             if (order->GetConditionValue() > 100) order->SetConditionValue(100);
    01383             /* FALL THROUGH */
    01384           default:
    01385             if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
    01386             break;
    01387         }
    01388         break;
    01389       }
    01390
    01391       case MOF_COND_COMPARATOR:
    01392         order->SetConditionComparator((OrderConditionComparator)data);
    01393         break;
    01394
    01395       case MOF_COND_VALUE:
    01396         order->SetConditionValue(data);
    01397         break;
    01398
    01399       case MOF_COND_DESTINATION:
    01400         order->SetConditionSkipToOrder(data);
    01401         break;
    01402
    01403       default: NOT_REACHED();
    01404     }
    01405
    01406     /* Update the windows and full load flags, also for vehicles that share the same order list */
    01407     Vehicle *u = v->FirstShared();
    01408     DeleteOrderWarnings(u);
    01409     for (; u != NULL; u = u->NextShared()) {
    01410       /* Toggle u->current_order "Full load" flag if it changed.
    01411        * However, as the same flag is used for depot orders, check
    01412        * whether we are not going to a depot as there are three
    01413        * cases where the full load flag can be active and only
    01414        * one case where the flag is used for depot orders. In the
    01415        * other cases for the OrderTypeByte the flags are not used,
    01416        * so do not care and those orders should not be active
    01417        * when this function is called.
    01418        */
    01419       if (sel_ord == u->cur_real_order_index &&
    01420           (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
    01421           u->current_order.GetLoadType() != order->GetLoadType()) {
    01422         u->current_order.SetLoadType(order->GetLoadType());
    01423       }
    01424       InvalidateVehicleOrder(u, -2);
    01425
    01426       RecalcFrozenIfLoading(u);
    01427     }
    01428   }
    01429
    01430   return CommandCost();
    01431 }
    01432
    01444 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01445 {
    01446   VehicleID veh_src = GB(p2, 0, 20);
    01447   VehicleID veh_dst = GB(p1, 0, 20);
    01448
    01449   Vehicle *dst = Vehicle::GetIfValid(veh_dst);
    01450   if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
    01451
    01452   CommandCost ret = CheckOwnership(dst->owner);
    01453   if (ret.Failed()) return ret;
    01454
    01455   switch (GB(p1, 30, 2)) {
    01456     case CO_SHARE: {
    01457       Vehicle *src = Vehicle::GetIfValid(veh_src);
    01458
    01459       /* Sanity checks */
    01460       if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
    01461
    01462       CommandCost ret = CheckOwnership(src->owner);
    01463       if (ret.Failed()) return ret;
    01464
    01465       /* Trucks can't share orders with busses (and visa versa) */
    01466       if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
    01467         return CMD_ERROR;
    01468       }
    01469
    01470       /* Is the vehicle already in the shared list? */
    01471       if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
    01472
    01473       const Order *order;
    01474
    01475       FOR_VEHICLE_ORDERS(src, order) {
    01476         if (OrderGoesToStation(dst, order) &&
    01477             !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
    01478           return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
    01479         }
    01480       }
    01481
    01482       if (src->orders.list == NULL && !OrderList::CanAllocateItem()) {
    01483         return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    01484       }
    01485
    01486       if (flags & DC_EXEC) {
    01487         /* If the destination vehicle had a OrderList, destroy it.
    01488          * We only reset the order indices, if the new orders are obviously different.
    01489          * (We mainly do this to keep the order indices valid and in range.) */
    01490         DeleteVehicleOrders(dst, false, dst->GetNumOrders() != src->GetNumOrders());
    01491
    01492         dst->orders.list = src->orders.list;
    01493
    01494         /* Link this vehicle in the shared-list */
    01495         dst->AddToShared(src);
    01496
    01497         /* Set automation bit if target has it */
    01498         if (HasBit(src->vehicle_flags, VF_AUTOMATE_TIMETABLE))
    01499           SetBit(dst->vehicle_flags, VF_AUTOMATE_TIMETABLE);
    01500
    01501         if (_settings_game.order.timetable_separation) dst->ClearSeparation();
    01502         if (_settings_game.order.timetable_separation) ClrBit(dst->vehicle_flags, VF_TIMETABLE_STARTED);
    01503
    01504         InvalidateVehicleOrder(dst, -1);
    01505         InvalidateVehicleOrder(src, -2);
    01506
    01507         InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
    01508       }
    01509       break;
    01510     }
    01511
    01512     case CO_COPY: {
    01513       Vehicle *src = Vehicle::GetIfValid(veh_src);
    01514
    01515       /* Sanity checks */
    01516       if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
    01517
    01518       CommandCost ret = CheckOwnership(src->owner);
    01519       if (ret.Failed()) return ret;
    01520
    01521       /* Trucks can't copy all the orders from busses (and visa versa),
    01522        * and neither can helicopters and aircarft. */
    01523       const Order *order;
    01524       FOR_VEHICLE_ORDERS(src, order) {
    01525         if (OrderGoesToStation(dst, order) &&
    01526             !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
    01527           return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
    01528         }
    01529       }
    01530
    01531       /* make sure there are orders available */
    01532       int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
    01533       if (!Order::CanAllocateItem(delta) ||
    01534           ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
    01535         return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    01536       }
    01537
    01538       if (flags & DC_EXEC) {
    01539         const Order *order;
    01540         Order *first = NULL;
    01541         Order **order_dst;
    01542
    01543         /* If the destination vehicle had an order list, destroy the chain but keep the OrderList.
    01544          * We only reset the order indices, if the new orders are obviously different.
    01545          * (We mainly do this to keep the order indices valid and in range.) */
    01546         DeleteVehicleOrders(dst, true, dst->GetNumOrders() != src->GetNumOrders());
    01547
    01548         order_dst = &first;
    01549         FOR_VEHICLE_ORDERS(src, order) {
    01550           *order_dst = new Order();
    01551           (*order_dst)->AssignOrder(*order);
    01552           order_dst = &(*order_dst)->next;
    01553         }
    01554         if (dst->orders.list == NULL) {
    01555           dst->orders.list = new OrderList(first, dst);
    01556         } else {
    01557           assert(dst->orders.list->GetFirstOrder() == NULL);
    01558           assert(!dst->orders.list->IsShared());
    01559           delete dst->orders.list;
    01560           assert(OrderList::CanAllocateItem());
    01561           dst->orders.list = new OrderList(first, dst);
    01562         }
    01563
    01564         InvalidateVehicleOrder(dst, -1);
    01565
    01566         InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
    01567       }
    01568       break;
    01569     }
    01570
    01571     case CO_UNSHARE: return DecloneOrder(dst, flags);
    01572     default: return CMD_ERROR;
    01573   }
    01574
    01575   RecalcFrozenIfLoading(dst);
    01576
    01577   return CommandCost();
    01578 }
    01579
    01592 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01593 {
    01594   VehicleID veh = GB(p1, 0, 20);
    01595   VehicleOrderID order_number  = GB(p2, 16, 8);
    01596   CargoID cargo = GB(p2, 0, 8);
    01597   byte subtype  = GB(p2, 8, 8);
    01598
    01599   if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT) return CMD_ERROR;
    01600
    01601   const Vehicle *v = Vehicle::GetIfValid(veh);
    01602   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    01603
    01604   CommandCost ret = CheckOwnership(v->owner);
    01605   if (ret.Failed()) return ret;
    01606
    01607   Order *order = v->GetOrder(order_number);
    01608   if (order == NULL) return CMD_ERROR;
    01609
    01610   if (flags & DC_EXEC) {
    01611     order->SetRefit(cargo, subtype);
    01612
    01613     for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
    01614       /* Update any possible open window of the vehicle */
    01615       InvalidateVehicleOrder(u, -2);
    01616
    01617       /* If the vehicle already got the current depot set as current order, then update current order as well */
    01618       if (u->cur_real_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
    01619         u->current_order.SetRefit(cargo, subtype);
    01620       }
    01621     }
    01622   }
    01623
    01624   return CommandCost();
    01625 }
    01626
    01627
    01633 void CheckOrders(const Vehicle *v)
    01634 {
    01635   /* Does the user wants us to check things? */
    01636   if (_settings_client.gui.order_review_system == 0) return;
    01637
    01638   /* Do nothing for crashed vehicles */
    01639   if (v->vehstatus & VS_CRASHED) return;
    01640
    01641   /* Do nothing for stopped vehicles if setting is '1' */
    01642   if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
    01643
    01644   /* do nothing we we're not the first vehicle in a share-chain */
    01645   if (v->FirstShared() != v) return;
    01646
    01647   /* Only check every 20 days, so that we don't flood the message log */
    01648   if (v->owner == _local_company && v->day_counter % 20 == 0) {
    01649     int n_st, problem_type = -1;
    01650     const Order *order;
    01651     int message = 0;
    01652
    01653     /* Check the order list */
    01654     n_st = 0;
    01655
    01656     FOR_VEHICLE_ORDERS(v, order) {
    01657       /* Dummy order? */
    01658       if (order->IsType(OT_DUMMY)) {
    01659         problem_type = 1;
    01660         break;
    01661       }
    01662       /* Does station have a load-bay for this vehicle? */
    01663       if (order->IsType(OT_GOTO_STATION)) {
    01664         const Station *st = Station::Get(order->GetDestination());
    01665
    01666         n_st++;
    01667         if (!CanVehicleUseStation(v, st)) problem_type = 3;
    01668       }
    01669     }
    01670
    01671     /* Check if the last and the first order are the same */
    01672     if (v->GetNumOrders() > 1) {
    01673       const Order *last = v->GetLastOrder();
    01674
    01675       if (v->orders.list->GetFirstOrder()->Equals(*last)) {
    01676         problem_type = 2;
    01677       }
    01678     }
    01679
    01680     /* Do we only have 1 station in our order list? */
    01681     if (n_st < 2 && problem_type == -1) problem_type = 0;
    01682
    01683 #ifndef NDEBUG
    01684     if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
    01685 #endif
    01686
    01687     /* We don't have a problem */
    01688     if (problem_type < 0) return;
    01689
    01690     message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
    01691     //DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
    01692
    01693     SetDParam(0, v->index);
    01694     AddVehicleNewsItem(
    01695       message,
    01696       NS_ADVICE,
    01697       v->index
    01698     );
    01699   }
    01700 }
    01701
    01707 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
    01708 {
    01709   Vehicle *v;
    01710
    01711   /* Aircraft have StationIDs for depot orders and never use DepotIDs
    01712    * This fact is handled specially below
    01713    */
    01714
    01715   /* Go through all vehicles */
    01716   FOR_ALL_VEHICLES(v) {
    01717     Order *order;
    01718
    01719     order = &v->current_order;
    01720     if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
    01721         v->current_order.GetDestination() == destination) {
    01722       order->MakeDummy();
    01723       SetWindowDirty(WC_VEHICLE_VIEW, v->index);
    01724     }
    01725
    01726     /* Clear the order from the order-list */
    01727     int id = -1;
    01728     FOR_VEHICLE_ORDERS(v, order) {
    01729       id++;
    01730 restart:
    01731
    01732       OrderType ot = order->GetType();
    01733       if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
    01734       if (ot == OT_IMPLICIT || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
    01735       if (ot == type && order->GetDestination() == destination) {
    01736         /* We want to clear implicit orders, but we don't want to make them
    01737          * dummy orders. They should just vanish. Also check the actual order
    01738          * type as ot is currently OT_GOTO_STATION. */
    01739         if (order->IsType(OT_IMPLICIT)) {
    01740           order = order->next; // DeleteOrder() invalidates current order
    01741           DeleteOrder(v, id);
    01742           if (order != NULL) goto restart;
    01743           break;
    01744         }
    01745
    01746         order->MakeDummy();
    01747         for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
    01748           /* In GUI, simulate by removing the order and adding it back */
    01749           InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
    01750           InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
    01751
    01752           RecalcFrozenIfLoading(w);
    01753         }
    01754       }
    01755     }
    01756   }
    01757 }
    01758
    01763 bool Vehicle::HasDepotOrder() const
    01764 {
    01765   const Order *order;
    01766
    01767   FOR_VEHICLE_ORDERS(this, order) {
    01768     if (order->IsType(OT_GOTO_DEPOT)) return true;
    01769   }
    01770
    01771   return false;
    01772 }
    01773
    01783 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
    01784 {
    01785   DeleteOrderWarnings(v);
    01786
    01787   if (v->IsOrderListShared()) {
    01788     /* Remove ourself from the shared order list. */
    01789     v->RemoveFromShared();
    01790     v->orders.list = NULL;
    01791   } else if (v->orders.list != NULL) {
    01792     /* Remove the orders */
    01793     v->orders.list->FreeChain(keep_orderlist);
    01794     if (!keep_orderlist) v->orders.list = NULL;
    01795   }
    01796
    01797   if (reset_order_indices) {
    01798     v->cur_implicit_order_index = v->cur_real_order_index = 0;
    01799     if (v->current_order.IsType(OT_LOADING)) {
    01800       CancelLoadingDueToDeletedOrder(v);
    01801     }
    01802   }
    01803
    01804   RecalcFrozenIfLoading(v);
    01805 }
    01806
    01814 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
    01815 {
    01816   return (Company::Get(company_id)->settings.vehicle.servint_ispercent) ? Clamp(interval, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(interval, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
    01817 }
    01818
    01827 static bool CheckForValidOrders(const Vehicle *v)
    01828 {
    01829   const Order *order;
    01830
    01831   FOR_VEHICLE_ORDERS(v, order) {
    01832     switch (order->GetType()) {
    01833       case OT_GOTO_STATION:
    01834       case OT_GOTO_DEPOT:
    01835       case OT_GOTO_WAYPOINT:
    01836         return true;
    01837
    01838       default:
    01839         break;
    01840     }
    01841   }
    01842
    01843   return false;
    01844 }
    01845
    01849 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
    01850 {
    01851   switch (occ) {
    01852     case OCC_EQUALS:      return variable == value;
    01853     case OCC_NOT_EQUALS:  return variable != value;
    01854     case OCC_LESS_THAN:   return variable <  value;
    01855     case OCC_LESS_EQUALS: return variable <= value;
    01856     case OCC_MORE_THAN:   return variable >  value;
    01857     case OCC_MORE_EQUALS: return variable >= value;
    01858     case OCC_IS_TRUE:     return variable != 0;
    01859     case OCC_IS_FALSE:    return variable == 0;
    01860     default: NOT_REACHED();
    01861   }
    01862 }
    01863
    01870 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
    01871 {
    01872   if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
    01873
    01874   bool skip_order = false;
    01875   OrderConditionComparator occ = order->GetConditionComparator();
    01876   uint16 value = order->GetConditionValue();
    01877
    01878   switch (order->GetConditionVariable()) {
    01879     case OCV_LOAD_PERCENTAGE:  skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
    01880     case OCV_RELIABILITY:      skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability),       value); break;
    01881     case OCV_MAX_SPEED:        skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
    01882     case OCV_AGE:              skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR,        value); break;
    01883     case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(),               value); break;
    01884     case OCV_UNCONDITIONALLY:  skip_order = true; break;
    01885     default: NOT_REACHED();
    01886   }
    01887
    01888   return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
    01889 }
    01890
    01897 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth)
    01898 {
    01899   if (conditional_depth > v->GetNumOrders()) return false;
    01900
    01901   switch (order->GetType()) {
    01902     case OT_GOTO_STATION:
    01903       v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
    01904       return true;
    01905
    01906     case OT_GOTO_DEPOT:
    01907       if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
    01908         UpdateVehicleTimetable(v, true);
    01909         v->IncrementRealOrderIndex();
    01910         break;
    01911       }
    01912
    01913       if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
    01914         /* We need to search for the nearest depot (hangar). */
    01915         TileIndex location;
    01916         DestinationID destination;
    01917         bool reverse;
    01918
    01919         if (v->FindClosestDepot(&location, &destination, &reverse)) {
    01920           v->dest_tile = location;
    01921           v->current_order.MakeGoToDepot(destination, v->current_order.GetDepotOrderType(), v->current_order.GetNonStopType(), (OrderDepotActionFlags)(v->current_order.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT), v->current_order.GetRefitCargo(), v->current_order.GetRefitSubtype());
    01922
    01923           /* If there is no depot in front, reverse automatically (trains only) */
    01924           if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
    01925
    01926           if (v->type == VEH_AIRCRAFT) {
    01927             Aircraft *a = Aircraft::From(v);
    01928             if (a->state == FLYING && a->targetairport != destination) {
    01929               /* The aircraft is now heading for a different hangar than the next in the orders */
    01930               extern void AircraftNextAirportPos_and_Order(Aircraft *a);
    01931               AircraftNextAirportPos_and_Order(a);
    01932             }
    01933           }
    01934           return true;
    01935         }
    01936
    01937         UpdateVehicleTimetable(v, true);
    01938         v->IncrementRealOrderIndex();
    01939       } else {
    01940         if (v->type != VEH_AIRCRAFT) {
    01941           v->dest_tile = Depot::Get(order->GetDestination())->xy;
    01942         }
    01943         return true;
    01944       }
    01945       break;
    01946
    01947     case OT_GOTO_WAYPOINT:
    01948       v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
    01949       return true;
    01950
    01951     case OT_CONDITIONAL: {
    01952       VehicleOrderID next_order = ProcessConditionalOrder(order, v);
    01953       if (next_order != INVALID_VEH_ORDER_ID) {
    01954         /* Jump to next_order. cur_implicit_order_index becomes exactly that order,
    01955          * cur_real_order_index might come after next_order. */
    01956         UpdateVehicleTimetable(v, false);
    01957         v->cur_implicit_order_index = v->cur_real_order_index = next_order;
    01958         v->UpdateRealOrderIndex();
    01959         v->current_order_time += v->GetOrder(v->cur_real_order_index)->travel_time;
    01960
    01961         /* Disable creation of implicit orders.
    01962          * When inserting them we do not know that we would have to make the conditional orders point to them. */
    01963         if (v->IsGroundVehicle()) {
    01964           uint16 &gv_flags = v->GetGroundVehicleFlags();
    01965           SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
    01966         }
    01967       } else {
    01968         UpdateVehicleTimetable(v, true);
    01969         v->IncrementRealOrderIndex();
    01970       }
    01971       break;
    01972     }
    01973
    01974     default:
    01975       v->dest_tile = 0;
    01976       return false;
    01977   }
    01978
    01979   assert(v->cur_implicit_order_index < v->GetNumOrders());
    01980   assert(v->cur_real_order_index < v->GetNumOrders());
    01981
    01982   /* Get the current order */
    01983   order = v->GetOrder(v->cur_real_order_index);
    01984   if (order != NULL && order->IsType(OT_IMPLICIT)) {
    01985     assert(v->GetNumManualOrders() == 0);
    01986     order = NULL;
    01987   }
    01988
    01989   if (order == NULL) {
    01990     v->current_order.Free();
    01991     v->dest_tile = 0;
    01992     return false;
    01993   }
    01994
    01995   v->current_order = *order;
    01996   return UpdateOrderDest(v, order, conditional_depth + 1);
    01997 }
    01998
    02006 bool ProcessOrders(Vehicle *v)
    02007 {
    02008   switch (v->current_order.GetType()) {
    02009     case OT_GOTO_DEPOT:
    02010       /* Let a depot order in the orderlist interrupt. */
    02011       if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
    02012       break;
    02013
    02014     case OT_LOADING:
    02015       return false;
    02016
    02017     case OT_LEAVESTATION:
    02018       if (v->type != VEH_AIRCRAFT) return false;
    02019       break;
    02020
    02021     default: break;
    02022   }
    02023
    02031   bool may_reverse = v->current_order.IsType(OT_NOTHING);
    02032
    02033   /* Check if we've reached a 'via' destination. */
    02034   if (((v->current_order.IsType(OT_GOTO_STATION) && (v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) || v->current_order.IsType(OT_GOTO_WAYPOINT)) &&
    02035       IsTileType(v->tile, MP_STATION) &&
    02036       v->current_order.GetDestination() == GetStationIndex(v->tile)) {
    02037     v->DeleteUnreachedImplicitOrders();
    02038     /* We set the last visited station here because we do not want
    02039      * the train to stop at this 'via' station if the next order
    02040      * is a no-non-stop order; in that case not setting the last
    02041      * visited station will cause the vehicle to still stop. */
    02042     v->last_station_visited = v->current_order.GetDestination();
    02043     UpdateVehicleTimetable(v, true);
    02044     v->IncrementImplicitOrderIndex();
    02045   }
    02046
    02047   /* Get the current order */
    02048   assert(v->cur_implicit_order_index == 0 || v->cur_implicit_order_index < v->GetNumOrders());
    02049   v->UpdateRealOrderIndex();
    02050
    02051   const Order *order = v->GetOrder(v->cur_real_order_index);
    02052   if (order != NULL && order->IsType(OT_IMPLICIT)) {
    02053     assert(v->GetNumManualOrders() == 0);
    02054     order = NULL;
    02055   }
    02056
    02057   /* If no order, do nothing. */
    02058   if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
    02059     if (v->type == VEH_AIRCRAFT) {
    02060       /* Aircraft do something vastly different here, so handle separately */
    02061       extern void HandleMissingAircraftOrders(Aircraft *v);
    02062       HandleMissingAircraftOrders(Aircraft::From(v));
    02063       return false;
    02064     }
    02065
    02066     v->current_order.Free();
    02067     v->dest_tile = 0;
    02068     return false;
    02069   }
    02070
    02071   /* If it is unchanged, keep it. */
    02072   if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
    02073       (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
    02074     return false;
    02075   }
    02076
    02077   /* Otherwise set it, and determine the destination tile. */
    02078   v->current_order = *order;
    02079
    02080   InvalidateVehicleOrder(v, -2);
    02081   switch (v->type) {
    02082     default:
    02083       NOT_REACHED();
    02084
    02085     case VEH_ROAD:
    02086     case VEH_TRAIN:
    02087       break;
    02088
    02089     case VEH_AIRCRAFT:
    02090     case VEH_SHIP:
    02091       SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
    02092       break;
    02093   }
    02094
    02095   return UpdateOrderDest(v, order) && may_reverse;
    02096 }
    02097
    02105 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
    02106 {
    02107   bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
    02108
    02109   return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
    02110       v->last_station_visited != station && // Do stop only when we've not just been there
    02111       /* Finally do stop when there is no non-stop flag set for this type of station. */
    02112       !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
    02113 }