# order_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: order_cmd.cpp 23257 2011-11-18 21:15:09Z rubidium $ */
    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
    00031 #include "table/strings.h"
    00032
    00033 /* DestinationID must be at least as large as every these below, because it can
    00034  * be any of them
    00035  */
    00036 assert_compile(sizeof(DestinationID) >= sizeof(DepotID));
    00037 assert_compile(sizeof(DestinationID) >= sizeof(StationID));
    00038
    00039 OrderPool _order_pool("Order");
    00040 INSTANTIATE_POOL_METHODS(Order)
    00041 OrderListPool _orderlist_pool("OrderList");
    00042 INSTANTIATE_POOL_METHODS(OrderList)
    00043
    00045 Order::~Order()
    00046 {
    00047   if (CleaningPool()) return;
    00048
    00049   /* We can visit oil rigs and buoys that are not our own. They will be shown in
    00050    * the list of stations. So, we need to invalidate that window if needed. */
    00051   if (this->IsType(OT_GOTO_STATION) || this->IsType(OT_GOTO_WAYPOINT)) {
    00052     BaseStation *bs = BaseStation::GetIfValid(this->GetDestination());
    00053     if (bs != NULL && bs->owner == OWNER_NONE) InvalidateWindowClassesData(WC_STATION_LIST, 0);
    00054   }
    00055 }
    00056
    00061 void Order::Free()
    00062 {
    00063   this->type  = OT_NOTHING;
    00064   this->flags = 0;
    00065   this->dest  = 0;
    00066   this->next  = NULL;
    00067 }
    00068
    00073 void Order::MakeGoToStation(StationID destination)
    00074 {
    00075   this->type = OT_GOTO_STATION;
    00076   this->flags = 0;
    00077   this->dest = destination;
    00078 }
    00079
    00089 void Order::MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type, OrderDepotActionFlags action, CargoID cargo, byte subtype)
    00090 {
    00091   this->type = OT_GOTO_DEPOT;
    00092   this->SetDepotOrderType(order);
    00093   this->SetDepotActionType(action);
    00094   this->SetNonStopType(non_stop_type);
    00095   this->dest = destination;
    00096   this->SetRefit(cargo, subtype);
    00097 }
    00098
    00103 void Order::MakeGoToWaypoint(StationID destination)
    00104 {
    00105   this->type = OT_GOTO_WAYPOINT;
    00106   this->flags = 0;
    00107   this->dest = destination;
    00108 }
    00109
    00114 void Order::MakeLoading(bool ordered)
    00115 {
    00116   this->type = OT_LOADING;
    00117   if (!ordered) this->flags = 0;
    00118 }
    00119
    00123 void Order::MakeLeaveStation()
    00124 {
    00125   this->type = OT_LEAVESTATION;
    00126   this->flags = 0;
    00127 }
    00128
    00132 void Order::MakeDummy()
    00133 {
    00134   this->type = OT_DUMMY;
    00135   this->flags = 0;
    00136 }
    00137
    00142 void Order::MakeConditional(VehicleOrderID order)
    00143 {
    00144   this->type = OT_CONDITIONAL;
    00145   this->flags = order;
    00146   this->dest = 0;
    00147 }
    00148
    00153 void Order::MakeImplicit(StationID destination)
    00154 {
    00155   this->type = OT_IMPLICIT;
    00156   this->dest = destination;
    00157 }
    00158
    00165 void Order::SetRefit(CargoID cargo, byte subtype)
    00166 {
    00167   this->refit_cargo = cargo;
    00168   this->refit_subtype = subtype;
    00169 }
    00170
    00176 bool Order::Equals(const Order &other) const
    00177 {
    00178   /* In case of go to nearest depot orders we need "only" compare the flags
    00179    * with the other and not the nearest depot order bit or the actual
    00180    * destination because those get clear/filled in during the order
    00181    * evaluation. If we do not do this the order will continuously be seen as
    00182    * a different order and it will try to find a "nearest depot" every tick. */
    00183   if ((this->IsType(OT_GOTO_DEPOT) && this->type == other.type) &&
    00184       ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0 ||
    00185        (other.GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0)) {
    00186     return this->GetDepotOrderType() == other.GetDepotOrderType() &&
    00187         (this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT) == (other.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT);
    00188   }
    00189
    00190   return this->type == other.type && this->flags == other.flags && this->dest == other.dest;
    00191 }
    00192
    00199 uint32 Order::Pack() const
    00200 {
    00201   return this->dest << 16 | this->flags << 8 | this->type;
    00202 }
    00203
    00209 uint16 Order::MapOldOrder() const
    00210 {
    00211   uint16 order = this->GetType();
    00212   switch (this->type) {
    00213     case OT_GOTO_STATION:
    00214       if (this->GetUnloadType() & OUFB_UNLOAD) SetBit(order, 5);
    00215       if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
    00216       if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) SetBit(order, 7);
    00217       order |= GB(this->GetDestination(), 0, 8) << 8;
    00218       break;
    00219     case OT_GOTO_DEPOT:
    00220       if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) SetBit(order, 6);
    00221       SetBit(order, 7);
    00222       order |= GB(this->GetDestination(), 0, 8) << 8;
    00223       break;
    00224     case OT_LOADING:
    00225       if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
    00226       break;
    00227   }
    00228   return order;
    00229 }
    00230
    00235 Order::Order(uint32 packed)
    00236 {
    00237   this->type    = (OrderType)GB(packed,  0,  8);
    00238   this->flags   = GB(packed,  8,  8);
    00239   this->dest    = GB(packed, 16, 16);
    00240   this->next    = NULL;
    00241   this->refit_cargo   = CT_NO_REFIT;
    00242   this->refit_subtype = 0;
    00243   this->wait_time     = 0;
    00244   this->travel_time   = 0;
    00245 }
    00246
    00252 void InvalidateVehicleOrder(const Vehicle *v, int data)
    00253 {
    00254   SetWindowDirty(WC_VEHICLE_VIEW, v->index);
    00255
    00256   if (data != 0) {
    00257     /* Calls SetDirty() too */
    00258     InvalidateWindowData(WC_VEHICLE_ORDERS,    v->index, data);
    00259     InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
    00260     return;
    00261   }
    00262
    00263   SetWindowDirty(WC_VEHICLE_ORDERS,    v->index);
    00264   SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
    00265 }
    00266
    00274 void Order::AssignOrder(const Order &other)
    00275 {
    00276   this->type  = other.type;
    00277   this->flags = other.flags;
    00278   this->dest  = other.dest;
    00279
    00280   this->refit_cargo   = other.refit_cargo;
    00281   this->refit_subtype = other.refit_subtype;
    00282
    00283   this->wait_time   = other.wait_time;
    00284   this->travel_time = other.travel_time;
    00285 }
    00286
    00292 void OrderList::Initialize(Order *chain, Vehicle *v)
    00293 {
    00294   this->first = chain;
    00295   this->first_shared = v;
    00296
    00297   this->num_orders = 0;
    00298   this->num_manual_orders = 0;
    00299   this->num_vehicles = 1;
    00300   this->timetable_duration = 0;
    00301
    00302   for (Order *o = this->first; o != NULL; o = o->next) {
    00303     ++this->num_orders;
    00304     if (!o->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
    00305     this->timetable_duration += o->wait_time + o->travel_time;
    00306   }
    00307
    00308   for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
    00309     ++this->num_vehicles;
    00310     this->first_shared = u;
    00311   }
    00312
    00313   for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
    00314 }
    00315
    00321 void OrderList::FreeChain(bool keep_orderlist)
    00322 {
    00323   Order *next;
    00324   for (Order *o = this->first; o != NULL; o = next) {
    00325     next = o->next;
    00326     delete o;
    00327   }
    00328
    00329   if (keep_orderlist) {
    00330     this->first = NULL;
    00331     this->num_orders = 0;
    00332     this->num_manual_orders = 0;
    00333     this->timetable_duration = 0;
    00334   } else {
    00335     delete this;
    00336   }
    00337 }
    00338
    00344 Order *OrderList::GetOrderAt(int index) const
    00345 {
    00346   if (index < 0) return NULL;
    00347
    00348   Order *order = this->first;
    00349
    00350   while (order != NULL && index-- > 0) {
    00351     order = order->next;
    00352   }
    00353   return order;
    00354 }
    00355
    00361 void OrderList::InsertOrderAt(Order *new_order, int index)
    00362 {
    00363   if (this->first == NULL) {
    00364     this->first = new_order;
    00365   } else {
    00366     if (index == 0) {
    00367       /* Insert as first or only order */
    00368       new_order->next = this->first;
    00369       this->first = new_order;
    00370     } else if (index >= this->num_orders) {
    00371       /* index is after the last order, add it to the end */
    00372       this->GetLastOrder()->next = new_order;
    00373     } else {
    00374       /* Put the new order in between */
    00375       Order *order = this->GetOrderAt(index - 1);
    00376       new_order->next = order->next;
    00377       order->next = new_order;
    00378     }
    00379   }
    00380   ++this->num_orders;
    00381   if (!new_order->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
    00382   this->timetable_duration += new_order->wait_time + new_order->travel_time;
    00383
    00384   /* We can visit oil rigs and buoys that are not our own. They will be shown in
    00385    * the list of stations. So, we need to invalidate that window if needed. */
    00386   if (new_order->IsType(OT_GOTO_STATION) || new_order->IsType(OT_GOTO_WAYPOINT)) {
    00387     BaseStation *bs = BaseStation::Get(new_order->GetDestination());
    00388     if (bs->owner == OWNER_NONE) InvalidateWindowClassesData(WC_STATION_LIST, 0);
    00389   }
    00390
    00391 }
    00392
    00393
    00398 void OrderList::DeleteOrderAt(int index)
    00399 {
    00400   if (index >= this->num_orders) return;
    00401
    00402   Order *to_remove;
    00403
    00404   if (index == 0) {
    00405     to_remove = this->first;
    00406     this->first = to_remove->next;
    00407   } else {
    00408     Order *prev = GetOrderAt(index - 1);
    00409     to_remove = prev->next;
    00410     prev->next = to_remove->next;
    00411   }
    00412   --this->num_orders;
    00413   if (!to_remove->IsType(OT_IMPLICIT)) --this->num_manual_orders;
    00414   this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
    00415   delete to_remove;
    00416 }
    00417
    00423 void OrderList::MoveOrder(int from, int to)
    00424 {
    00425   if (from >= this->num_orders || to >= this->num_orders || from == to) return;
    00426
    00427   Order *moving_one;
    00428
    00429   /* Take the moving order out of the pointer-chain */
    00430   if (from == 0) {
    00431     moving_one = this->first;
    00432     this->first = moving_one->next;
    00433   } else {
    00434     Order *one_before = GetOrderAt(from - 1);
    00435     moving_one = one_before->next;
    00436     one_before->next = moving_one->next;
    00437   }
    00438
    00439   /* Insert the moving_order again in the pointer-chain */
    00440   if (to == 0) {
    00441     moving_one->next = this->first;
    00442     this->first = moving_one;
    00443   } else {
    00444     Order *one_before = GetOrderAt(to - 1);
    00445     moving_one->next = one_before->next;
    00446     one_before->next = moving_one;
    00447   }
    00448 }
    00449
    00455 void OrderList::RemoveVehicle(Vehicle *v)
    00456 {
    00457   --this->num_vehicles;
    00458   if (v == this->first_shared) this->first_shared = v->NextShared();
    00459 }
    00460
    00465 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
    00466 {
    00467   for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
    00468     if (v_shared == v) return true;
    00469   }
    00470
    00471   return false;
    00472 }
    00473
    00479 int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
    00480 {
    00481   int count = 0;
    00482   for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
    00483   return count;
    00484 }
    00485
    00490 bool OrderList::IsCompleteTimetable() const
    00491 {
    00492   for (Order *o = this->first; o != NULL; o = o->next) {
    00493     /* Implicit orders are, by definition, not timetabled. */
    00494     if (o->IsType(OT_IMPLICIT)) continue;
    00495     if (!o->IsCompletelyTimetabled()) return false;
    00496   }
    00497   return true;
    00498 }
    00499
    00503 void OrderList::DebugCheckSanity() const
    00504 {
    00505   VehicleOrderID check_num_orders = 0;
    00506   VehicleOrderID check_num_manual_orders = 0;
    00507   uint check_num_vehicles = 0;
    00508   Ticks check_timetable_duration = 0;
    00509
    00510   DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
    00511
    00512   for (const Order *o = this->first; o != NULL; o = o->next) {
    00513     ++check_num_orders;
    00514     if (!o->IsType(OT_IMPLICIT)) ++check_num_manual_orders;
    00515     check_timetable_duration += o->wait_time + o->travel_time;
    00516   }
    00517   assert(this->num_orders == check_num_orders);
    00518   assert(this->num_manual_orders == check_num_manual_orders);
    00519   assert(this->timetable_duration == check_timetable_duration);
    00520
    00521   for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
    00522     ++check_num_vehicles;
    00523     assert(v->orders.list == this);
    00524   }
    00525   assert(this->num_vehicles == check_num_vehicles);
    00526   DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
    00527       (uint)this->num_orders, (uint)this->num_manual_orders,
    00528       this->num_vehicles, this->timetable_duration);
    00529 }
    00530
    00538 static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
    00539 {
    00540   return o->IsType(OT_GOTO_STATION) ||
    00541       (v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
    00542 }
    00543
    00550 static void DeleteOrderWarnings(const Vehicle *v)
    00551 {
    00552   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
    00553   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
    00554   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
    00555   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
    00556 }
    00557
    00563 TileIndex Order::GetLocation(const Vehicle *v) const
    00564 {
    00565   switch (this->GetType()) {
    00566     case OT_GOTO_WAYPOINT:
    00567     case OT_GOTO_STATION:
    00568     case OT_IMPLICIT:
    00569       return BaseStation::Get(this->GetDestination())->xy;
    00570
    00571     case OT_GOTO_DEPOT:
    00572       if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
    00573       return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
    00574
    00575     default:
    00576       return INVALID_TILE;
    00577   }
    00578 }
    00579
    00580 static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
    00581 {
    00582   assert(v->type == VEH_SHIP);
    00583
    00584   if (cur->IsType(OT_CONDITIONAL)) {
    00585     if (conditional_depth > v->GetNumOrders()) return 0;
    00586
    00587     conditional_depth++;
    00588
    00589     int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
    00590     int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
    00591     return max(dist1, dist2);
    00592   }
    00593
    00594   TileIndex prev_tile = prev->GetLocation(v);
    00595   TileIndex cur_tile = cur->GetLocation(v);
    00596   if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
    00597   return DistanceManhattan(prev_tile, cur_tile);
    00598 }
    00599
    00613 CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00614 {
    00615   VehicleID veh          = GB(p1,  0, 20);
    00616   VehicleOrderID sel_ord = GB(p1, 20, 8);
    00617   Order new_order(p2);
    00618
    00619   Vehicle *v = Vehicle::GetIfValid(veh);
    00620   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00621
    00622   CommandCost ret = CheckOwnership(v->owner);
    00623   if (ret.Failed()) return ret;
    00624
    00625   /* Check if the inserted order is to the correct destination (owner, type),
    00626    * and has the correct flags if any */
    00627   switch (new_order.GetType()) {
    00628     case OT_GOTO_STATION: {
    00629       const Station *st = Station::GetIfValid(new_order.GetDestination());
    00630       if (st == NULL) return CMD_ERROR;
    00631
    00632       if (st->owner != OWNER_NONE) {
    00633         CommandCost ret = CheckOwnership(st->owner);
    00634         if (ret.Failed()) return ret;
    00635       }
    00636
    00637       if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
    00638       for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
    00639         if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
    00640       }
    00641
    00642       /* Non stop only allowed for ground vehicles. */
    00643       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
    00644
    00645       /* Filter invalid load/unload types. */
    00646       switch (new_order.GetLoadType()) {
    00647         case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
    00648         default: return CMD_ERROR;
    00649       }
    00650       switch (new_order.GetUnloadType()) {
    00651         case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
    00652         default: return CMD_ERROR;
    00653       }
    00654
    00655       /* Filter invalid stop locations */
    00656       switch (new_order.GetStopLocation()) {
    00657         case OSL_PLATFORM_NEAR_END:
    00658         case OSL_PLATFORM_MIDDLE:
    00659           if (v->type != VEH_TRAIN) return CMD_ERROR;
    00660           /* FALL THROUGH */
    00661         case OSL_PLATFORM_FAR_END:
    00662           break;
    00663
    00664         default:
    00665           return CMD_ERROR;
    00666       }
    00667
    00668       break;
    00669     }
    00670
    00671     case OT_GOTO_DEPOT: {
    00672       if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
    00673         if (v->type == VEH_AIRCRAFT) {
    00674           const Station *st = Station::GetIfValid(new_order.GetDestination());
    00675
    00676           if (st == NULL) return CMD_ERROR;
    00677
    00678           CommandCost ret = CheckOwnership(st->owner);
    00679           if (ret.Failed()) return ret;
    00680
    00681           if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
    00682             return CMD_ERROR;
    00683           }
    00684         } else {
    00685           const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
    00686
    00687           if (dp == NULL) return CMD_ERROR;
    00688
    00689           CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
    00690           if (ret.Failed()) return ret;
    00691
    00692           switch (v->type) {
    00693             case VEH_TRAIN:
    00694               if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
    00695               break;
    00696
    00697             case VEH_ROAD:
    00698               if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
    00699               break;
    00700
    00701             case VEH_SHIP:
    00702               if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
    00703               break;
    00704
    00705             default: return CMD_ERROR;
    00706           }
    00707         }
    00708       }
    00709
    00710       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
    00711       if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
    00712       if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
    00713       if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
    00714       break;
    00715     }
    00716
    00717     case OT_GOTO_WAYPOINT: {
    00718       const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
    00719       if (wp == NULL) return CMD_ERROR;
    00720
    00721       switch (v->type) {
    00722         default: return CMD_ERROR;
    00723
    00724         case VEH_TRAIN: {
    00725           if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
    00726
    00727           CommandCost ret = CheckOwnership(wp->owner);
    00728           if (ret.Failed()) return ret;
    00729           break;
    00730         }
    00731
    00732         case VEH_SHIP:
    00733           if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
    00734           if (wp->owner != OWNER_NONE) {
    00735             CommandCost ret = CheckOwnership(wp->owner);
    00736             if (ret.Failed()) return ret;
    00737           }
    00738           break;
    00739       }
    00740
    00741       /* Order flags can be any of the following for waypoints:
    00742        * [non-stop]
    00743        * non-stop orders (if any) are only valid for trains */
    00744       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
    00745       break;
    00746     }
    00747
    00748     case OT_CONDITIONAL: {
    00749       VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
    00750       if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR; // Always allow jumping to the first (even when there is no order).
    00751       if (new_order.GetConditionVariable() >= OCV_END) return CMD_ERROR;
    00752
    00753       OrderConditionComparator occ = new_order.GetConditionComparator();
    00754       if (occ >= OCC_END) return CMD_ERROR;
    00755       switch (new_order.GetConditionVariable()) {
    00756         case OCV_REQUIRES_SERVICE:
    00757           if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
    00758           break;
    00759
    00760         case OCV_UNCONDITIONALLY:
    00761           if (occ != OCC_EQUALS) return CMD_ERROR;
    00762           if (new_order.GetConditionValue() != 0) return CMD_ERROR;
    00763           break;
    00764
    00765         case OCV_LOAD_PERCENTAGE:
    00766         case OCV_RELIABILITY:
    00767           if (new_order.GetConditionValue() > 100) return CMD_ERROR;
    00768           /* FALL THROUGH */
    00769         default:
    00770           if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
    00771           break;
    00772       }
    00773       break;
    00774     }
    00775
    00776     default: return CMD_ERROR;
    00777   }
    00778
    00779   if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
    00780
    00781   if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
    00782   if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    00783   if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    00784
    00785   if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
    00786     /* Make sure the new destination is not too far away from the previous */
    00787     const Order *prev = NULL;
    00788     uint n = 0;
    00789
    00790     /* Find the last goto station or depot order before the insert location.
    00791      * If the order is to be inserted at the beginning of the order list this
    00792      * finds the last order in the list. */
    00793     const Order *o;
    00794     FOR_VEHICLE_ORDERS(v, o) {
    00795       switch (o->GetType()) {
    00796         case OT_GOTO_STATION:
    00797         case OT_GOTO_DEPOT:
    00798         case OT_GOTO_WAYPOINT:
    00799           prev = o;
    00800           break;
    00801
    00802         default: break;
    00803       }
    00804       if (++n == sel_ord && prev != NULL) break;
    00805     }
    00806     if (prev != NULL) {
    00807       uint dist = GetOrderDistance(prev, &new_order, v);
    00808       if (dist >= 130) {
    00809         return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
    00810       }
    00811     }
    00812   }
    00813
    00814   if (flags & DC_EXEC) {
    00815     Order *new_o = new Order();
    00816     new_o->AssignOrder(new_order);
    00817     InsertOrder(v, new_o, sel_ord);
    00818   }
    00819
    00820   return CommandCost();
    00821 }
    00822
    00829 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
    00830 {
    00831   /* Create new order and link in list */
    00832   if (v->orders.list == NULL) {
    00833     v->orders.list = new OrderList(new_o, v);
    00834   } else {
    00835     v->orders.list->InsertOrderAt(new_o, sel_ord);
    00836   }
    00837
    00838   Vehicle *u = v->FirstShared();
    00839   DeleteOrderWarnings(u);
    00840   for (; u != NULL; u = u->NextShared()) {
    00841     assert(v->orders.list == u->orders.list);
    00842
    00843     /* If there is added an order before the current one, we need
    00844      * to update the selected order. We do not change implicit/real order indices though.
    00845      * If the new order is between the current implicit order and real order, the implicit order will
    00846      * later skip the inserted order. */
    00847     if (sel_ord <= u->cur_real_order_index) {
    00848       uint cur = u->cur_real_order_index + 1;
    00849       /* Check if we don't go out of bound */
    00850       if (cur < u->GetNumOrders()) {
    00851         u->cur_real_order_index = cur;
    00852       }
    00853     }
    00854     if (sel_ord == u->cur_implicit_order_index && u->IsGroundVehicle()) {
    00855       /* We are inserting an order just before the current implicit order.
    00856        * We do not know whether we will reach current implicit or the newly inserted order first.
    00857        * So, disable creation of implicit orders until we are on track again. */
    00858       uint16 &gv_flags = u->GetGroundVehicleFlags();
    00859       SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
    00860     }
    00861     if (sel_ord <= u->cur_implicit_order_index) {
    00862       uint cur = u->cur_implicit_order_index + 1;
    00863       /* Check if we don't go out of bound */
    00864       if (cur < u->GetNumOrders()) {
    00865         u->cur_implicit_order_index = cur;
    00866       }
    00867     }
    00868     /* Update any possible open window of the vehicle */
    00869     InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
    00870   }
    00871
    00872   /* As we insert an order, the order to skip to will be 'wrong'. */
    00873   VehicleOrderID cur_order_id = 0;
    00874   Order *order;
    00875   FOR_VEHICLE_ORDERS(v, order) {
    00876     if (order->IsType(OT_CONDITIONAL)) {
    00877       VehicleOrderID order_id = order->GetConditionSkipToOrder();
    00878       if (order_id >= sel_ord) {
    00879         order->SetConditionSkipToOrder(order_id + 1);
    00880       }
    00881       if (order_id == cur_order_id) {
    00882         order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
    00883       }
    00884     }
    00885     cur_order_id++;
    00886   }
    00887
    00888   /* Make sure to rebuild the whole list */
    00889   InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
    00890 }
    00891
    00897 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
    00898 {
    00899   if (flags & DC_EXEC) {
    00900     DeleteVehicleOrders(dst);
    00901     InvalidateVehicleOrder(dst, -1);
    00902     InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
    00903   }
    00904   return CommandCost();
    00905 }
    00906
    00916 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00917 {
    00918   VehicleID veh_id = GB(p1, 0, 20);
    00919   VehicleOrderID sel_ord = GB(p2, 0, 8);
    00920
    00921   Vehicle *v = Vehicle::GetIfValid(veh_id);
    00922
    00923   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00924
    00925   CommandCost ret = CheckOwnership(v->owner);
    00926   if (ret.Failed()) return ret;
    00927
    00928   /* If we did not select an order, we maybe want to de-clone the orders */
    00929   if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
    00930
    00931   if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
    00932
    00933   if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
    00934   return CommandCost();
    00935 }
    00936
    00941 static void CancelLoadingDueToDeletedOrder(Vehicle *v)
    00942 {
    00943   assert(v->current_order.IsType(OT_LOADING));
    00944   /* NON-stop flag is misused to see if a train is in a station that is
    00945    * on his order list or not */
    00946   v->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
    00947   /* When full loading, "cancel" that order so the vehicle doesn't
    00948    * stay indefinitely at this station anymore. */
    00949   if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) v->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
    00950 }
    00951
    00957 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
    00958 {
    00959   v->orders.list->DeleteOrderAt(sel_ord);
    00960
    00961   Vehicle *u = v->FirstShared();
    00962   DeleteOrderWarnings(u);
    00963   for (; u != NULL; u = u->NextShared()) {
    00964     assert(v->orders.list == u->orders.list);
    00965
    00966     if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
    00967       CancelLoadingDueToDeletedOrder(u);
    00968     }
    00969
    00970     if (sel_ord < u->cur_real_order_index) {
    00971       u->cur_real_order_index--;
    00972     } else if (sel_ord == u->cur_real_order_index) {
    00973       u->UpdateRealOrderIndex();
    00974     }
    00975
    00976     if (sel_ord < u->cur_implicit_order_index) {
    00977       u->cur_implicit_order_index--;
    00978     } else if (sel_ord == u->cur_implicit_order_index) {
    00979       /* Make sure the index is valid */
    00980       if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
    00981
    00982       /* Skip non-implicit orders for the implicit-order-index (e.g. if the current implicit order was deleted */
    00983       while (u->cur_implicit_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_implicit_order_index)->IsType(OT_IMPLICIT)) {
    00984         u->cur_implicit_order_index++;
    00985         if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
    00986       }
    00987     }
    00988
    00989     /* Update any possible open window of the vehicle */
    00990     InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
    00991   }
    00992
    00993   /* As we delete an order, the order to skip to will be 'wrong'. */
    00994   VehicleOrderID cur_order_id = 0;
    00995   Order *order = NULL;
    00996   FOR_VEHICLE_ORDERS(v, order) {
    00997     if (order->IsType(OT_CONDITIONAL)) {
    00998       VehicleOrderID order_id = order->GetConditionSkipToOrder();
    00999       if (order_id >= sel_ord) {
    01000         order_id = max(order_id - 1, 0);
    01001       }
    01002       if (order_id == cur_order_id) {
    01003         order_id = (order_id + 1) % v->GetNumOrders();
    01004       }
    01005       order->SetConditionSkipToOrder(order_id);
    01006     }
    01007     cur_order_id++;
    01008   }
    01009
    01010   InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
    01011 }
    01012
    01022 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01023 {
    01024   VehicleID veh_id = GB(p1, 0, 20);
    01025   VehicleOrderID sel_ord = GB(p2, 0, 8);
    01026
    01027   Vehicle *v = Vehicle::GetIfValid(veh_id);
    01028
    01029   if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_implicit_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
    01030
    01031   CommandCost ret = CheckOwnership(v->owner);
    01032   if (ret.Failed()) return ret;
    01033
    01034   if (flags & DC_EXEC) {
    01035     v->cur_implicit_order_index = v->cur_real_order_index = sel_ord;
    01036     v->UpdateRealOrderIndex();
    01037
    01038     if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
    01039
    01040     InvalidateVehicleOrder(v, -2);
    01041   }
    01042
    01043   /* We have an aircraft/ship, they have a mini-schedule, so update them all */
    01044   if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
    01045   if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
    01046
    01047   return CommandCost();
    01048 }
    01049
    01063 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01064 {
    01065   VehicleID veh = GB(p1, 0, 20);
    01066   VehicleOrderID moving_order = GB(p2,  0, 16);
    01067   VehicleOrderID target_order = GB(p2, 16, 16);
    01068
    01069   Vehicle *v = Vehicle::GetIfValid(veh);
    01070   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    01071
    01072   CommandCost ret = CheckOwnership(v->owner);
    01073   if (ret.Failed()) return ret;
    01074
    01075   /* Don't make senseless movements */
    01076   if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
    01077       moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
    01078
    01079   Order *moving_one = v->GetOrder(moving_order);
    01080   /* Don't move an empty order */
    01081   if (moving_one == NULL) return CMD_ERROR;
    01082
    01083   if (flags & DC_EXEC) {
    01084     v->orders.list->MoveOrder(moving_order, target_order);
    01085
    01086     /* Update shared list */
    01087     Vehicle *u = v->FirstShared();
    01088
    01089     DeleteOrderWarnings(u);
    01090
    01091     for (; u != NULL; u = u->NextShared()) {
    01092       /* Update the current order.
    01093        * There are multiple ways to move orders, which result in cur_implicit_order_index
    01094        * and cur_real_order_index to not longer make any sense. E.g. moving another
    01095        * real order between them.
    01096        *
    01097        * Basically one could choose to preserve either of them, but not both.
    01098        * While both ways are suitable in this or that case from a human point of view, neither
    01099        * of them makes really sense.
    01100        * However, from an AI point of view, preserving cur_real_order_index is the most
    01101        * predictable and transparent behaviour.
    01102        *
    01103        * With that decision it basically does not matter what we do to cur_implicit_order_index.
    01104        * If we change orders between the implict- and real-index, the implicit orders are mostly likely
    01105        * completely out-dated anyway. So, keep it simple and just keep cur_implicit_order_index as well.
    01106        * The worst which can happen is that a lot of implicit orders are removed when reaching current_order.
    01107        */
    01108       if (u->cur_real_order_index == moving_order) {
    01109         u->cur_real_order_index = target_order;
    01110       } else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
    01111         u->cur_real_order_index--;
    01112       } else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
    01113         u->cur_real_order_index++;
    01114       }
    01115
    01116       if (u->cur_implicit_order_index == moving_order) {
    01117         u->cur_implicit_order_index = target_order;
    01118       } else if (u->cur_implicit_order_index > moving_order && u->cur_implicit_order_index <= target_order) {
    01119         u->cur_implicit_order_index--;
    01120       } else if (u->cur_implicit_order_index < moving_order && u->cur_implicit_order_index >= target_order) {
    01121         u->cur_implicit_order_index++;
    01122       }
    01123
    01124       assert(v->orders.list == u->orders.list);
    01125       /* Update any possible open window of the vehicle */
    01126       InvalidateVehicleOrder(u, moving_order | (target_order << 8));
    01127     }
    01128
    01129     /* As we move an order, the order to skip to will be 'wrong'. */
    01130     Order *order;
    01131     FOR_VEHICLE_ORDERS(v, order) {
    01132       if (order->IsType(OT_CONDITIONAL)) {
    01133         VehicleOrderID order_id = order->GetConditionSkipToOrder();
    01134         if (order_id == moving_order) {
    01135           order_id = target_order;
    01136         } else if (order_id > moving_order && order_id <= target_order) {
    01137           order_id--;
    01138         } else if (order_id < moving_order && order_id >= target_order) {
    01139           order_id++;
    01140         }
    01141         order->SetConditionSkipToOrder(order_id);
    01142       }
    01143     }
    01144
    01145     /* Make sure to rebuild the whole list */
    01146     InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
    01147   }
    01148
    01149   return CommandCost();
    01150 }
    01151
    01167 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01168 {
    01169   VehicleOrderID sel_ord = GB(p1, 20,  8);
    01170   VehicleID veh          = GB(p1,  0, 20);
    01171   ModifyOrderFlags mof   = Extract<ModifyOrderFlags, 0, 4>(p2);
    01172   uint16 data            = GB(p2,  4, 11);
    01173
    01174   if (mof >= MOF_END) return CMD_ERROR;
    01175
    01176   Vehicle *v = Vehicle::GetIfValid(veh);
    01177   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    01178
    01179   CommandCost ret = CheckOwnership(v->owner);
    01180   if (ret.Failed()) return ret;
    01181
    01182   /* Is it a valid order? */
    01183   if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
    01184
    01185   Order *order = v->GetOrder(sel_ord);
    01186   switch (order->GetType()) {
    01187     case OT_GOTO_STATION:
    01188       if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
    01189       break;
    01190
    01191     case OT_GOTO_DEPOT:
    01192       if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
    01193       break;
    01194
    01195     case OT_GOTO_WAYPOINT:
    01196       if (mof != MOF_NON_STOP) return CMD_ERROR;
    01197       break;
    01198
    01199     case OT_CONDITIONAL:
    01200       if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
    01201       break;
    01202
    01203     default:
    01204       return CMD_ERROR;
    01205   }
    01206
    01207   switch (mof) {
    01208     default: NOT_REACHED();
    01209
    01210     case MOF_NON_STOP:
    01211       if (!v->IsGroundVehicle()) return CMD_ERROR;
    01212       if (data >= ONSF_END) return CMD_ERROR;
    01213       if (data == order->GetNonStopType()) return CMD_ERROR;
    01214       break;
    01215
    01216     case MOF_STOP_LOCATION:
    01217       if (v->type != VEH_TRAIN) return CMD_ERROR;
    01218       if (data >= OSL_END) return CMD_ERROR;
    01219       break;
    01220
    01221     case MOF_UNLOAD:
    01222       if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
    01223       /* Unload and no-unload are mutual exclusive and so are transfer and no unload. */
    01224       if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
    01225       if (data == order->GetUnloadType()) return CMD_ERROR;
    01226       break;
    01227
    01228     case MOF_LOAD:
    01229       if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
    01230       if (data == order->GetLoadType()) return CMD_ERROR;
    01231       break;
    01232
    01233     case MOF_DEPOT_ACTION:
    01234       if (data >= DA_END) return CMD_ERROR;
    01235       break;
    01236
    01237     case MOF_COND_VARIABLE:
    01238       if (data >= OCV_END) return CMD_ERROR;
    01239       break;
    01240
    01241     case MOF_COND_COMPARATOR:
    01242       if (data >= OCC_END) return CMD_ERROR;
    01243       switch (order->GetConditionVariable()) {
    01244         case OCV_UNCONDITIONALLY: return CMD_ERROR;
    01245
    01246         case OCV_REQUIRES_SERVICE:
    01247           if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
    01248           break;
    01249
    01250         default:
    01251           if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
    01252           break;
    01253       }
    01254       break;
    01255
    01256     case MOF_COND_VALUE:
    01257       switch (order->GetConditionVariable()) {
    01258         case OCV_UNCONDITIONALLY: return CMD_ERROR;
    01259
    01260         case OCV_LOAD_PERCENTAGE:
    01261         case OCV_RELIABILITY:
    01262           if (data > 100) return CMD_ERROR;
    01263           break;
    01264
    01265         default:
    01266           if (data > 2047) return CMD_ERROR;
    01267           break;
    01268       }
    01269       break;
    01270
    01271     case MOF_COND_DESTINATION:
    01272       if (data >= v->GetNumOrders()) return CMD_ERROR;
    01273       break;
    01274   }
    01275
    01276   if (flags & DC_EXEC) {
    01277     switch (mof) {
    01278       case MOF_NON_STOP:
    01279         order->SetNonStopType((OrderNonStopFlags)data);
    01280         break;
    01281
    01282       case MOF_STOP_LOCATION:
    01283         order->SetStopLocation((OrderStopLocation)data);
    01284         break;
    01285
    01286       case MOF_UNLOAD:
    01287         order->SetUnloadType((OrderUnloadFlags)data);
    01288         break;
    01289
    01290       case MOF_LOAD:
    01291         order->SetLoadType((OrderLoadFlags)data);
    01292         break;
    01293
    01294       case MOF_DEPOT_ACTION: {
    01295         switch (data) {
    01296           case DA_ALWAYS_GO:
    01297             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
    01298             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
    01299             break;
    01300
    01301           case DA_SERVICE:
    01302             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
    01303             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
    01304             order->SetRefit(CT_NO_REFIT);
    01305             break;
    01306
    01307           case DA_STOP:
    01308             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
    01309             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
    01310             order->SetRefit(CT_NO_REFIT);
    01311             break;
    01312
    01313           default:
    01314             NOT_REACHED();
    01315         }
    01316         break;
    01317       }
    01318
    01319       case MOF_COND_VARIABLE: {
    01320         order->SetConditionVariable((OrderConditionVariable)data);
    01321
    01322         OrderConditionComparator occ = order->GetConditionComparator();
    01323         switch (order->GetConditionVariable()) {
    01324           case OCV_UNCONDITIONALLY:
    01325             order->SetConditionComparator(OCC_EQUALS);
    01326             order->SetConditionValue(0);
    01327             break;
    01328
    01329           case OCV_REQUIRES_SERVICE:
    01330             if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
    01331             break;
    01332
    01333           case OCV_LOAD_PERCENTAGE:
    01334           case OCV_RELIABILITY:
    01335             if (order->GetConditionValue() > 100) order->SetConditionValue(100);
    01336             /* FALL THROUGH */
    01337           default:
    01338             if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
    01339             break;
    01340         }
    01341         break;
    01342       }
    01343
    01344       case MOF_COND_COMPARATOR:
    01345         order->SetConditionComparator((OrderConditionComparator)data);
    01346         break;
    01347
    01348       case MOF_COND_VALUE:
    01349         order->SetConditionValue(data);
    01350         break;
    01351
    01352       case MOF_COND_DESTINATION:
    01353         order->SetConditionSkipToOrder(data);
    01354         break;
    01355
    01356       default: NOT_REACHED();
    01357     }
    01358
    01359     /* Update the windows and full load flags, also for vehicles that share the same order list */
    01360     Vehicle *u = v->FirstShared();
    01361     DeleteOrderWarnings(u);
    01362     for (; u != NULL; u = u->NextShared()) {
    01363       /* Toggle u->current_order "Full load" flag if it changed.
    01364        * However, as the same flag is used for depot orders, check
    01365        * whether we are not going to a depot as there are three
    01366        * cases where the full load flag can be active and only
    01367        * one case where the flag is used for depot orders. In the
    01368        * other cases for the OrderTypeByte the flags are not used,
    01369        * so do not care and those orders should not be active
    01370        * when this function is called.
    01371        */
    01372       if (sel_ord == u->cur_real_order_index &&
    01373           (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
    01374           u->current_order.GetLoadType() != order->GetLoadType()) {
    01375         u->current_order.SetLoadType(order->GetLoadType());
    01376       }
    01377       InvalidateVehicleOrder(u, -2);
    01378     }
    01379   }
    01380
    01381   return CommandCost();
    01382 }
    01383
    01395 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01396 {
    01397   VehicleID veh_src = GB(p2, 0, 20);
    01398   VehicleID veh_dst = GB(p1, 0, 20);
    01399
    01400   Vehicle *dst = Vehicle::GetIfValid(veh_dst);
    01401   if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
    01402
    01403   CommandCost ret = CheckOwnership(dst->owner);
    01404   if (ret.Failed()) return ret;
    01405
    01406   switch (GB(p1, 30, 2)) {
    01407     case CO_SHARE: {
    01408       Vehicle *src = Vehicle::GetIfValid(veh_src);
    01409
    01410       /* Sanity checks */
    01411       if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
    01412
    01413       CommandCost ret = CheckOwnership(src->owner);
    01414       if (ret.Failed()) return ret;
    01415
    01416       /* Trucks can't share orders with busses (and visa versa) */
    01417       if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
    01418         return CMD_ERROR;
    01419       }
    01420
    01421       /* Is the vehicle already in the shared list? */
    01422       if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
    01423
    01424       const Order *order;
    01425
    01426       FOR_VEHICLE_ORDERS(src, order) {
    01427         if (OrderGoesToStation(dst, order) &&
    01428             !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
    01429           return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
    01430         }
    01431       }
    01432
    01433       if (src->orders.list == NULL && !OrderList::CanAllocateItem()) {
    01434         return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    01435       }
    01436
    01437       if (flags & DC_EXEC) {
    01438         /* If the destination vehicle had a OrderList, destroy it.
    01439          * We only reset the order indices, if the new orders are obviously different.
    01440          * (We mainly do this to keep the order indices valid and in range.) */
    01441         DeleteVehicleOrders(dst, false, dst->GetNumOrders() != src->GetNumOrders());
    01442
    01443         dst->orders.list = src->orders.list;
    01444
    01445         /* Link this vehicle in the shared-list */
    01446         dst->AddToShared(src);
    01447
    01448         InvalidateVehicleOrder(dst, -1);
    01449         InvalidateVehicleOrder(src, -2);
    01450
    01451         InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
    01452       }
    01453       break;
    01454     }
    01455
    01456     case CO_COPY: {
    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 copy all the orders from busses (and visa versa),
    01466        * and neither can helicopters and aircarft. */
    01467       const Order *order;
    01468       FOR_VEHICLE_ORDERS(src, order) {
    01469         if (OrderGoesToStation(dst, order) &&
    01470             !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
    01471           return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
    01472         }
    01473       }
    01474
    01475       /* make sure there are orders available */
    01476       int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
    01477       if (!Order::CanAllocateItem(delta) ||
    01478           ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
    01479         return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
    01480       }
    01481
    01482       if (flags & DC_EXEC) {
    01483         const Order *order;
    01484         Order *first = NULL;
    01485         Order **order_dst;
    01486
    01487         /* If the destination vehicle had an order list, destroy the chain but keep the OrderList.
    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, true, dst->GetNumOrders() != src->GetNumOrders());
    01491
    01492         order_dst = &first;
    01493         FOR_VEHICLE_ORDERS(src, order) {
    01494           *order_dst = new Order();
    01495           (*order_dst)->AssignOrder(*order);
    01496           order_dst = &(*order_dst)->next;
    01497         }
    01498         if (dst->orders.list == NULL) {
    01499           dst->orders.list = new OrderList(first, dst);
    01500         } else {
    01501           assert(dst->orders.list->GetFirstOrder() == NULL);
    01502           assert(!dst->orders.list->IsShared());
    01503           delete dst->orders.list;
    01504           dst->orders.list = new OrderList(first, dst);
    01505         }
    01506
    01507         InvalidateVehicleOrder(dst, -1);
    01508
    01509         InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
    01510       }
    01511       break;
    01512     }
    01513
    01514     case CO_UNSHARE: return DecloneOrder(dst, flags);
    01515     default: return CMD_ERROR;
    01516   }
    01517
    01518   return CommandCost();
    01519 }
    01520
    01533 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01534 {
    01535   VehicleID veh = GB(p1, 0, 20);
    01536   VehicleOrderID order_number  = GB(p2, 16, 8);
    01537   CargoID cargo = GB(p2, 0, 8);
    01538   byte subtype  = GB(p2, 8, 8);
    01539
    01540   if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT) return CMD_ERROR;
    01541
    01542   const Vehicle *v = Vehicle::GetIfValid(veh);
    01543   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    01544
    01545   CommandCost ret = CheckOwnership(v->owner);
    01546   if (ret.Failed()) return ret;
    01547
    01548   Order *order = v->GetOrder(order_number);
    01549   if (order == NULL) return CMD_ERROR;
    01550
    01551   if (flags & DC_EXEC) {
    01552     order->SetRefit(cargo, subtype);
    01553
    01554     /* Make the depot order an 'always go' order. */
    01555     if (cargo != CT_NO_REFIT) {
    01556       order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
    01557       order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
    01558     }
    01559
    01560     for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
    01561       /* Update any possible open window of the vehicle */
    01562       InvalidateVehicleOrder(u, -2);
    01563
    01564       /* If the vehicle already got the current depot set as current order, then update current order as well */
    01565       if (u->cur_real_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
    01566         u->current_order.SetRefit(cargo, subtype);
    01567       }
    01568     }
    01569   }
    01570
    01571   return CommandCost();
    01572 }
    01573
    01574
    01580 void CheckOrders(const Vehicle *v)
    01581 {
    01582   /* Does the user wants us to check things? */
    01583   if (_settings_client.gui.order_review_system == 0) return;
    01584
    01585   /* Do nothing for crashed vehicles */
    01586   if (v->vehstatus & VS_CRASHED) return;
    01587
    01588   /* Do nothing for stopped vehicles if setting is '1' */
    01589   if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
    01590
    01591   /* do nothing we we're not the first vehicle in a share-chain */
    01592   if (v->FirstShared() != v) return;
    01593
    01594   /* Only check every 20 days, so that we don't flood the message log */
    01595   if (v->owner == _local_company && v->day_counter % 20 == 0) {
    01596     int n_st, problem_type = -1;
    01597     const Order *order;
    01598     int message = 0;
    01599
    01600     /* Check the order list */
    01601     n_st = 0;
    01602
    01603     FOR_VEHICLE_ORDERS(v, order) {
    01604       /* Dummy order? */
    01605       if (order->IsType(OT_DUMMY)) {
    01606         problem_type = 1;
    01607         break;
    01608       }
    01609       /* Does station have a load-bay for this vehicle? */
    01610       if (order->IsType(OT_GOTO_STATION)) {
    01611         const Station *st = Station::Get(order->GetDestination());
    01612
    01613         n_st++;
    01614         if (!CanVehicleUseStation(v, st)) problem_type = 3;
    01615       }
    01616     }
    01617
    01618     /* Check if the last and the first order are the same */
    01619     if (v->GetNumOrders() > 1) {
    01620       const Order *last = v->GetLastOrder();
    01621
    01622       if (v->orders.list->GetFirstOrder()->Equals(*last)) {
    01623         problem_type = 2;
    01624       }
    01625     }
    01626
    01627     /* Do we only have 1 station in our order list? */
    01628     if (n_st < 2 && problem_type == -1) problem_type = 0;
    01629
    01630 #ifndef NDEBUG
    01631     if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
    01632 #endif
    01633
    01634     /* We don't have a problem */
    01635     if (problem_type < 0) return;
    01636
    01637     message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
    01638     //DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
    01639
    01640     SetDParam(0, v->index);
    01641     AddVehicleNewsItem(
    01642       message,
    01643       NS_ADVICE,
    01644       v->index
    01645     );
    01646   }
    01647 }
    01648
    01654 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
    01655 {
    01656   Vehicle *v;
    01657
    01658   /* Aircraft have StationIDs for depot orders and never use DepotIDs
    01659    * This fact is handled specially below
    01660    */
    01661
    01662   /* Go through all vehicles */
    01663   FOR_ALL_VEHICLES(v) {
    01664     Order *order;
    01665
    01666     order = &v->current_order;
    01667     if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
    01668         v->current_order.GetDestination() == destination) {
    01669       order->MakeDummy();
    01670       SetWindowDirty(WC_VEHICLE_VIEW, v->index);
    01671     }
    01672
    01673     /* Clear the order from the order-list */
    01674     int id = -1;
    01675     FOR_VEHICLE_ORDERS(v, order) {
    01676       id++;
    01677 restart:
    01678
    01679       OrderType ot = order->GetType();
    01680       if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
    01681       if (ot == OT_IMPLICIT || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
    01682       if (ot == type && order->GetDestination() == destination) {
    01683         /* We want to clear implicit orders, but we don't want to make them
    01684          * dummy orders. They should just vanish. Also check the actual order
    01685          * type as ot is currently OT_GOTO_STATION. */
    01686         if (order->IsType(OT_IMPLICIT)) {
    01687           order = order->next; // DeleteOrder() invalidates current order
    01688           DeleteOrder(v, id);
    01689           if (order != NULL) goto restart;
    01690           break;
    01691         }
    01692
    01693         order->MakeDummy();
    01694         for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
    01695           /* In GUI, simulate by removing the order and adding it back */
    01696           InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
    01697           InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
    01698         }
    01699       }
    01700     }
    01701   }
    01702 }
    01703
    01708 bool Vehicle::HasDepotOrder() const
    01709 {
    01710   const Order *order;
    01711
    01712   FOR_VEHICLE_ORDERS(this, order) {
    01713     if (order->IsType(OT_GOTO_DEPOT)) return true;
    01714   }
    01715
    01716   return false;
    01717 }
    01718
    01728 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
    01729 {
    01730   DeleteOrderWarnings(v);
    01731
    01732   if (v->IsOrderListShared()) {
    01733     /* Remove ourself from the shared order list. */
    01734     v->RemoveFromShared();
    01735     v->orders.list = NULL;
    01736   } else if (v->orders.list != NULL) {
    01737     /* Remove the orders */
    01738     v->orders.list->FreeChain(keep_orderlist);
    01739     if (!keep_orderlist) v->orders.list = NULL;
    01740   }
    01741
    01742   if (reset_order_indices) {
    01743     v->cur_implicit_order_index = v->cur_real_order_index = 0;
    01744     if (v->current_order.IsType(OT_LOADING)) {
    01745       CancelLoadingDueToDeletedOrder(v);
    01746     }
    01747   }
    01748 }
    01749
    01757 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
    01758 {
    01759   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);
    01760 }
    01761
    01770 static bool CheckForValidOrders(const Vehicle *v)
    01771 {
    01772   const Order *order;
    01773
    01774   FOR_VEHICLE_ORDERS(v, order) {
    01775     switch (order->GetType()) {
    01776       case OT_GOTO_STATION:
    01777       case OT_GOTO_DEPOT:
    01778       case OT_GOTO_WAYPOINT:
    01779         return true;
    01780
    01781       default:
    01782         break;
    01783     }
    01784   }
    01785
    01786   return false;
    01787 }
    01788
    01792 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
    01793 {
    01794   switch (occ) {
    01795     case OCC_EQUALS:      return variable == value;
    01796     case OCC_NOT_EQUALS:  return variable != value;
    01797     case OCC_LESS_THAN:   return variable <  value;
    01798     case OCC_LESS_EQUALS: return variable <= value;
    01799     case OCC_MORE_THAN:   return variable >  value;
    01800     case OCC_MORE_EQUALS: return variable >= value;
    01801     case OCC_IS_TRUE:     return variable != 0;
    01802     case OCC_IS_FALSE:    return variable == 0;
    01803     default: NOT_REACHED();
    01804   }
    01805 }
    01806
    01813 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
    01814 {
    01815   if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
    01816
    01817   bool skip_order = false;
    01818   OrderConditionComparator occ = order->GetConditionComparator();
    01819   uint16 value = order->GetConditionValue();
    01820
    01821   switch (order->GetConditionVariable()) {
    01822     case OCV_LOAD_PERCENTAGE:  skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
    01823     case OCV_RELIABILITY:      skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability),       value); break;
    01824     case OCV_MAX_SPEED:        skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
    01825     case OCV_AGE:              skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR,        value); break;
    01826     case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(),               value); break;
    01827     case OCV_UNCONDITIONALLY:  skip_order = true; break;
    01828     default: NOT_REACHED();
    01829   }
    01830
    01831   return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
    01832 }
    01833
    01841 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool pbs_look_ahead)
    01842 {
    01843   if (conditional_depth > v->GetNumOrders()) return false;
    01844
    01845   switch (order->GetType()) {
    01846     case OT_GOTO_STATION:
    01847       v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
    01848       return true;
    01849
    01850     case OT_GOTO_DEPOT:
    01851       if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
    01852         assert(!pbs_look_ahead);
    01853         UpdateVehicleTimetable(v, true);
    01854         v->IncrementRealOrderIndex();
    01855         break;
    01856       }
    01857
    01858       if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
    01859         /* We need to search for the nearest depot (hangar). */
    01860         TileIndex location;
    01861         DestinationID destination;
    01862         bool reverse;
    01863
    01864         if (v->FindClosestDepot(&location, &destination, &reverse)) {
    01865           /* PBS reservations cannot reverse */
    01866           if (pbs_look_ahead && reverse) return false;
    01867
    01868           v->dest_tile = location;
    01869           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());
    01870
    01871           /* If there is no depot in front, reverse automatically (trains only) */
    01872           if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
    01873
    01874           if (v->type == VEH_AIRCRAFT) {
    01875             Aircraft *a = Aircraft::From(v);
    01876             if (a->state == FLYING && a->targetairport != destination) {
    01877               /* The aircraft is now heading for a different hangar than the next in the orders */
    01878               extern void AircraftNextAirportPos_and_Order(Aircraft *a);
    01879               AircraftNextAirportPos_and_Order(a);
    01880             }
    01881           }
    01882           return true;
    01883         }
    01884
    01885         /* If there is no depot, we cannot help PBS either. */
    01886         if (pbs_look_ahead) return false;
    01887
    01888         UpdateVehicleTimetable(v, true);
    01889         v->IncrementRealOrderIndex();
    01890       } else {
    01891         if (v->type != VEH_AIRCRAFT) {
    01892           v->dest_tile = Depot::Get(order->GetDestination())->xy;
    01893         }
    01894         return true;
    01895       }
    01896       break;
    01897
    01898     case OT_GOTO_WAYPOINT:
    01899       v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
    01900       return true;
    01901
    01902     case OT_CONDITIONAL: {
    01903       assert(!pbs_look_ahead);
    01904       VehicleOrderID next_order = ProcessConditionalOrder(order, v);
    01905       if (next_order != INVALID_VEH_ORDER_ID) {
    01906         /* Jump to next_order. cur_implicit_order_index becomes exactly that order,
    01907          * cur_real_order_index might come after next_order. */
    01908         UpdateVehicleTimetable(v, false);
    01909         v->cur_implicit_order_index = v->cur_real_order_index = next_order;
    01910         v->UpdateRealOrderIndex();
    01911         v->current_order_time += v->GetOrder(v->cur_real_order_index)->travel_time;
    01912
    01913         /* Disable creation of implicit orders.
    01914          * When inserting them we do not know that we would have to make the conditional orders point to them. */
    01915         if (v->IsGroundVehicle()) {
    01916           uint16 &gv_flags = v->GetGroundVehicleFlags();
    01917           SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
    01918         }
    01919       } else {
    01920         UpdateVehicleTimetable(v, true);
    01921         v->IncrementRealOrderIndex();
    01922       }
    01923       break;
    01924     }
    01925
    01926     default:
    01927       v->dest_tile = 0;
    01928       return false;
    01929   }
    01930
    01931   assert(v->cur_implicit_order_index < v->GetNumOrders());
    01932   assert(v->cur_real_order_index < v->GetNumOrders());
    01933
    01934   /* Get the current order */
    01935   order = v->GetOrder(v->cur_real_order_index);
    01936   if (order != NULL && order->IsType(OT_IMPLICIT)) {
    01937     assert(v->GetNumManualOrders() == 0);
    01938     order = NULL;
    01939   }
    01940
    01941   if (order == NULL) {
    01942     v->current_order.Free();
    01943     v->dest_tile = 0;
    01944     return false;
    01945   }
    01946
    01947   v->current_order = *order;
    01948   return UpdateOrderDest(v, order, conditional_depth + 1, pbs_look_ahead);
    01949 }
    01950
    01958 bool ProcessOrders(Vehicle *v)
    01959 {
    01960   switch (v->current_order.GetType()) {
    01961     case OT_GOTO_DEPOT:
    01962       /* Let a depot order in the orderlist interrupt. */
    01963       if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
    01964       break;
    01965
    01966     case OT_LOADING:
    01967       return false;
    01968
    01969     case OT_LEAVESTATION:
    01970       if (v->type != VEH_AIRCRAFT) return false;
    01971       break;
    01972
    01973     default: break;
    01974   }
    01975
    01983   bool may_reverse = v->current_order.IsType(OT_NOTHING);
    01984
    01985   /* Check if we've reached a 'via' destination. */
    01986   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)) &&
    01987       IsTileType(v->tile, MP_STATION) &&
    01988       v->current_order.GetDestination() == GetStationIndex(v->tile)) {
    01989     v->DeleteUnreachedImplicitOrders();
    01990     /* We set the last visited station here because we do not want
    01991      * the train to stop at this 'via' station if the next order
    01992      * is a no-non-stop order; in that case not setting the last
    01993      * visited station will cause the vehicle to still stop. */
    01994     v->last_station_visited = v->current_order.GetDestination();
    01995     UpdateVehicleTimetable(v, true);
    01996     v->IncrementImplicitOrderIndex();
    01997   }
    01998
    01999   /* Get the current order */
    02000   assert(v->cur_implicit_order_index == 0 || v->cur_implicit_order_index < v->GetNumOrders());
    02001   v->UpdateRealOrderIndex();
    02002
    02003   const Order *order = v->GetOrder(v->cur_real_order_index);
    02004   if (order != NULL && order->IsType(OT_IMPLICIT)) {
    02005     assert(v->GetNumManualOrders() == 0);
    02006     order = NULL;
    02007   }
    02008
    02009   /* If no order, do nothing. */
    02010   if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
    02011     if (v->type == VEH_AIRCRAFT) {
    02012       /* Aircraft do something vastly different here, so handle separately */
    02013       extern void HandleMissingAircraftOrders(Aircraft *v);
    02014       HandleMissingAircraftOrders(Aircraft::From(v));
    02015       return false;
    02016     }
    02017
    02018     v->current_order.Free();
    02019     v->dest_tile = 0;
    02020     return false;
    02021   }
    02022
    02023   /* If it is unchanged, keep it. */
    02024   if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
    02025       (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
    02026     return false;
    02027   }
    02028
    02029   /* Otherwise set it, and determine the destination tile. */
    02030   v->current_order = *order;
    02031
    02032   InvalidateVehicleOrder(v, -2);
    02033   switch (v->type) {
    02034     default:
    02035       NOT_REACHED();
    02036
    02037     case VEH_ROAD:
    02038     case VEH_TRAIN:
    02039       break;
    02040
    02041     case VEH_AIRCRAFT:
    02042     case VEH_SHIP:
    02043       SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
    02044       break;
    02045   }
    02046
    02047   return UpdateOrderDest(v, order) && may_reverse;
    02048 }
    02049
    02057 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
    02058 {
    02059   bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
    02060
    02061   return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
    02062       v->last_station_visited != station && // Do stop only when we've not just been there
    02063       /* Finally do stop when there is no non-stop flag set for this type of station. */
    02064       !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
    02065 }
    02066
    02067 void InitializeOrders()
    02068 {
    02069   _order_pool.CleanPool();
    02070   _orderlist_pool.CleanPool();
    02071 }