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 "cargodest_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::MakeAutomatic(StationID destination)
00142 {
00143   this->type = OT_AUTOMATIC;
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 
00239 /* static */ void Order::PostDestructor(size_t index)
00240 {
00241   Vehicle *v;
00242   FOR_ALL_VEHICLES(v) {
00243     if (v->current_order.index == index) v->current_order.index = INVALID_ORDER;
00244     if (v->last_order_id == index) v->last_order_id = INVALID_ORDER;
00245   }
00246 
00247   InvalidateOrderRouteLinks((OrderID)index);
00248 }
00249 
00255 void InvalidateVehicleOrder(const Vehicle *v, int data)
00256 {
00257   SetWindowDirty(WC_VEHICLE_VIEW, v->index);
00258 
00259   if (data != 0) {
00260     /* Calls SetDirty() too */
00261     InvalidateWindowData(WC_VEHICLE_ORDERS,    v->index, data);
00262     InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
00263     return;
00264   }
00265 
00266   SetWindowDirty(WC_VEHICLE_ORDERS,    v->index);
00267   SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
00268 }
00269 
00277 void Order::AssignOrder(const Order &other)
00278 {
00279   this->type  = other.type;
00280   this->flags = other.flags;
00281   this->dest  = other.dest;
00282 
00283   this->refit_cargo   = other.refit_cargo;
00284   this->refit_subtype = other.refit_subtype;
00285 
00286   this->wait_time   = other.wait_time;
00287   this->travel_time = other.travel_time;
00288 }
00289 
00295 void OrderList::Initialize(Order *chain, Vehicle *v)
00296 {
00297   this->first = chain;
00298   this->first_shared = v;
00299 
00300   this->num_orders = 0;
00301   this->num_manual_orders = 0;
00302   this->num_vehicles = 1;
00303   this->timetable_duration = 0;
00304 
00305   for (Order *o = this->first; o != NULL; o = o->next) {
00306     ++this->num_orders;
00307     if (!o->IsType(OT_AUTOMATIC)) ++this->num_manual_orders;
00308     this->timetable_duration += o->wait_time + o->travel_time;
00309   }
00310 
00311   for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
00312     ++this->num_vehicles;
00313     this->first_shared = u;
00314   }
00315 
00316   for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
00317 }
00318 
00324 void OrderList::FreeChain(bool keep_orderlist)
00325 {
00326   Order *next;
00327   for (Order *o = this->first; o != NULL; o = next) {
00328     next = o->next;
00329     delete o;
00330   }
00331 
00332   if (keep_orderlist) {
00333     this->first = NULL;
00334     this->num_orders = 0;
00335     this->num_manual_orders = 0;
00336     this->timetable_duration = 0;
00337   } else {
00338     delete this;
00339   }
00340 }
00341 
00347 Order *OrderList::GetOrderAt(int index) const
00348 {
00349   if (index < 0) return NULL;
00350 
00351   Order *order = this->first;
00352 
00353   while (order != NULL && index-- > 0) {
00354     order = order->next;
00355   }
00356   return order;
00357 }
00358 
00364 void OrderList::InsertOrderAt(Order *new_order, int index)
00365 {
00366   if (this->first == NULL) {
00367     this->first = new_order;
00368   } else {
00369     if (index == 0) {
00370       /* Insert as first or only order */
00371       new_order->next = this->first;
00372       this->first = new_order;
00373     } else if (index >= this->num_orders) {
00374       /* index is after the last order, add it to the end */
00375       this->GetLastOrder()->next = new_order;
00376     } else {
00377       /* Put the new order in between */
00378       Order *order = this->GetOrderAt(index - 1);
00379       new_order->next = order->next;
00380       order->next = new_order;
00381     }
00382   }
00383   ++this->num_orders;
00384   if (!new_order->IsType(OT_AUTOMATIC)) ++this->num_manual_orders;
00385   this->timetable_duration += new_order->wait_time + new_order->travel_time;
00386 }
00387 
00388 
00393 void OrderList::DeleteOrderAt(int index)
00394 {
00395   if (index >= this->num_orders) return;
00396 
00397   Order *to_remove;
00398 
00399   if (index == 0) {
00400     to_remove = this->first;
00401     this->first = to_remove->next;
00402   } else {
00403     Order *prev = GetOrderAt(index - 1);
00404     to_remove = prev->next;
00405     prev->next = to_remove->next;
00406   }
00407   --this->num_orders;
00408   if (!to_remove->IsType(OT_AUTOMATIC)) --this->num_manual_orders;
00409   this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
00410   delete to_remove;
00411 }
00412 
00418 void OrderList::MoveOrder(int from, int to)
00419 {
00420   if (from >= this->num_orders || to >= this->num_orders || from == to) return;
00421 
00422   Order *moving_one;
00423 
00424   /* Take the moving order out of the pointer-chain */
00425   if (from == 0) {
00426     moving_one = this->first;
00427     this->first = moving_one->next;
00428   } else {
00429     Order *one_before = GetOrderAt(from - 1);
00430     moving_one = one_before->next;
00431     one_before->next = moving_one->next;
00432   }
00433 
00434   /* Insert the moving_order again in the pointer-chain */
00435   if (to == 0) {
00436     moving_one->next = this->first;
00437     this->first = moving_one;
00438   } else {
00439     Order *one_before = GetOrderAt(to - 1);
00440     moving_one->next = one_before->next;
00441     one_before->next = moving_one;
00442   }
00443 }
00444 
00450 void OrderList::RemoveVehicle(Vehicle *v)
00451 {
00452   --this->num_vehicles;
00453   if (v == this->first_shared) this->first_shared = v->NextShared();
00454 }
00455 
00460 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
00461 {
00462   for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
00463     if (v_shared == v) return true;
00464   }
00465 
00466   return false;
00467 }
00468 
00474 int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
00475 {
00476   int count = 0;
00477   for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
00478   return count;
00479 }
00480 
00485 bool OrderList::IsCompleteTimetable() const
00486 {
00487   for (Order *o = this->first; o != NULL; o = o->next) {
00488     /* Automatic orders are, by definition, not timetabled. */
00489     if (o->IsType(OT_AUTOMATIC)) continue;
00490     if (!o->IsCompletelyTimetabled()) return false;
00491   }
00492   return true;
00493 }
00494 
00498 void OrderList::DebugCheckSanity() const
00499 {
00500   VehicleOrderID check_num_orders = 0;
00501   VehicleOrderID check_num_manual_orders = 0;
00502   uint check_num_vehicles = 0;
00503   Ticks check_timetable_duration = 0;
00504 
00505   DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
00506 
00507   for (const Order *o = this->first; o != NULL; o = o->next) {
00508     ++check_num_orders;
00509     if (!o->IsType(OT_AUTOMATIC)) ++check_num_manual_orders;
00510     check_timetable_duration += o->wait_time + o->travel_time;
00511   }
00512   assert(this->num_orders == check_num_orders);
00513   assert(this->num_manual_orders == check_num_manual_orders);
00514   assert(this->timetable_duration == check_timetable_duration);
00515 
00516   for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
00517     ++check_num_vehicles;
00518     assert(v->orders.list == this);
00519   }
00520   assert(this->num_vehicles == check_num_vehicles);
00521   DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
00522       (uint)this->num_orders, (uint)this->num_manual_orders,
00523       this->num_vehicles, this->timetable_duration);
00524 }
00525 
00533 static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
00534 {
00535   return o->IsType(OT_GOTO_STATION) ||
00536       (v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
00537 }
00538 
00545 static void DeleteOrderWarnings(const Vehicle *v)
00546 {
00547   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
00548   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
00549   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
00550   DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
00551 }
00552 
00558 TileIndex Order::GetLocation(const Vehicle *v) const
00559 {
00560   switch (this->GetType()) {
00561     case OT_GOTO_WAYPOINT:
00562     case OT_GOTO_STATION:
00563     case OT_AUTOMATIC:
00564       return BaseStation::Get(this->GetDestination())->xy;
00565 
00566     case OT_GOTO_DEPOT:
00567       if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
00568       return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
00569 
00570     default:
00571       return INVALID_TILE;
00572   }
00573 }
00574 
00575 static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
00576 {
00577   assert(v->type == VEH_SHIP);
00578 
00579   if (cur->IsType(OT_CONDITIONAL)) {
00580     if (conditional_depth > v->GetNumOrders()) return 0;
00581 
00582     conditional_depth++;
00583 
00584     int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
00585     int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
00586     return max(dist1, dist2);
00587   }
00588 
00589   TileIndex prev_tile = prev->GetLocation(v);
00590   TileIndex cur_tile = cur->GetLocation(v);
00591   if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
00592   return DistanceManhattan(prev_tile, cur_tile);
00593 }
00594 
00608 CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00609 {
00610   VehicleID veh          = GB(p1,  0, 20);
00611   VehicleOrderID sel_ord = GB(p1, 20, 8);
00612   Order new_order(p2);
00613 
00614   Vehicle *v = Vehicle::GetIfValid(veh);
00615   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00616 
00617   CommandCost ret = CheckOwnership(v->owner);
00618   if (ret.Failed()) return ret;
00619 
00620   /* Check if the inserted order is to the correct destination (owner, type),
00621    * and has the correct flags if any */
00622   switch (new_order.GetType()) {
00623     case OT_GOTO_STATION: {
00624       const Station *st = Station::GetIfValid(new_order.GetDestination());
00625       if (st == NULL) return CMD_ERROR;
00626 
00627       if (st->owner != OWNER_NONE) {
00628         CommandCost ret = CheckOwnership(st->owner);
00629         if (ret.Failed()) return ret;
00630       }
00631 
00632       if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00633       for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
00634         if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
00635       }
00636 
00637       /* Non stop only allowed for ground vehicles. */
00638       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00639 
00640       /* Filter invalid load/unload types. */
00641       switch (new_order.GetLoadType()) {
00642         case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
00643         default: return CMD_ERROR;
00644       }
00645       switch (new_order.GetUnloadType()) {
00646         case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
00647         default: return CMD_ERROR;
00648       }
00649 
00650       /* Filter invalid stop locations */
00651       switch (new_order.GetStopLocation()) {
00652         case OSL_PLATFORM_NEAR_END:
00653         case OSL_PLATFORM_MIDDLE:
00654           if (v->type != VEH_TRAIN) return CMD_ERROR;
00655           /* FALL THROUGH */
00656         case OSL_PLATFORM_FAR_END:
00657           break;
00658 
00659         default:
00660           return CMD_ERROR;
00661       }
00662 
00663       break;
00664     }
00665 
00666     case OT_GOTO_DEPOT: {
00667       if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
00668         if (v->type == VEH_AIRCRAFT) {
00669           const Station *st = Station::GetIfValid(new_order.GetDestination());
00670 
00671           if (st == NULL) return CMD_ERROR;
00672 
00673           CommandCost ret = CheckOwnership(st->owner);
00674           if (ret.Failed()) return ret;
00675 
00676           if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
00677             return CMD_ERROR;
00678           }
00679         } else {
00680           const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
00681 
00682           if (dp == NULL) return CMD_ERROR;
00683 
00684           CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
00685           if (ret.Failed()) return ret;
00686 
00687           switch (v->type) {
00688             case VEH_TRAIN:
00689               if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
00690               break;
00691 
00692             case VEH_ROAD:
00693               if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
00694               break;
00695 
00696             case VEH_SHIP:
00697               if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
00698               break;
00699 
00700             default: return CMD_ERROR;
00701           }
00702         }
00703       }
00704 
00705       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00706       if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
00707       if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
00708       if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
00709       break;
00710     }
00711 
00712     case OT_GOTO_WAYPOINT: {
00713       const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
00714       if (wp == NULL) return CMD_ERROR;
00715 
00716       switch (v->type) {
00717         default: return CMD_ERROR;
00718 
00719         case VEH_TRAIN: {
00720           if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00721 
00722           CommandCost ret = CheckOwnership(wp->owner);
00723           if (ret.Failed()) return ret;
00724           break;
00725         }
00726 
00727         case VEH_SHIP:
00728           if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00729           if (wp->owner != OWNER_NONE) {
00730             CommandCost ret = CheckOwnership(wp->owner);
00731             if (ret.Failed()) return ret;
00732           }
00733           break;
00734       }
00735 
00736       /* Order flags can be any of the following for waypoints:
00737        * [non-stop]
00738        * non-stop orders (if any) are only valid for trains */
00739       if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
00740       break;
00741     }
00742 
00743     case OT_CONDITIONAL: {
00744       VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
00745       if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR; // Always allow jumping to the first (even when there is no order).
00746       if (new_order.GetConditionVariable() > OCV_END) return CMD_ERROR;
00747 
00748       OrderConditionComparator occ = new_order.GetConditionComparator();
00749       if (occ > OCC_END) return CMD_ERROR;
00750       switch (new_order.GetConditionVariable()) {
00751         case OCV_REQUIRES_SERVICE:
00752           if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
00753           break;
00754 
00755         case OCV_UNCONDITIONALLY:
00756           if (occ != OCC_EQUALS) return CMD_ERROR;
00757           if (new_order.GetConditionValue() != 0) return CMD_ERROR;
00758           break;
00759 
00760         case OCV_LOAD_PERCENTAGE:
00761         case OCV_RELIABILITY:
00762           if (new_order.GetConditionValue() > 100) return CMD_ERROR;
00763           /* FALL THROUGH */
00764         default:
00765           if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
00766           break;
00767       }
00768       break;
00769     }
00770 
00771     default: return CMD_ERROR;
00772   }
00773 
00774   if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
00775 
00776   if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
00777   if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00778   if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00779 
00780   if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
00781     /* Make sure the new destination is not too far away from the previous */
00782     const Order *prev = NULL;
00783     uint n = 0;
00784 
00785     /* Find the last goto station or depot order before the insert location.
00786      * If the order is to be inserted at the beginning of the order list this
00787      * finds the last order in the list. */
00788     const Order *o;
00789     FOR_VEHICLE_ORDERS(v, o) {
00790       switch (o->GetType()) {
00791         case OT_GOTO_STATION:
00792         case OT_GOTO_DEPOT:
00793         case OT_GOTO_WAYPOINT:
00794           prev = o;
00795           break;
00796 
00797         default: break;
00798       }
00799       if (++n == sel_ord && prev != NULL) break;
00800     }
00801     if (prev != NULL) {
00802       uint dist = GetOrderDistance(prev, &new_order, v);
00803       if (dist >= 130) {
00804         return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
00805       }
00806     }
00807   }
00808 
00809   if (flags & DC_EXEC) {
00810     Order *new_o = new Order();
00811     new_o->AssignOrder(new_order);
00812     InsertOrder(v, new_o, sel_ord);
00813   }
00814 
00815   return CommandCost();
00816 }
00817 
00824 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
00825 {
00826   /* Create new order and link in list */
00827   if (v->orders.list == NULL) {
00828     v->orders.list = new OrderList(new_o, v);
00829   } else {
00830     v->orders.list->InsertOrderAt(new_o, sel_ord);
00831   }
00832 
00833   Vehicle *u = v->FirstShared();
00834   DeleteOrderWarnings(u);
00835   for (; u != NULL; u = u->NextShared()) {
00836     assert(v->orders.list == u->orders.list);
00837 
00838     /* If there is added an order before the current one, we need
00839      * to update the selected order. We do not change automatic/real order indices though.
00840      * If the new order is between the current auto order and real order, the auto order will
00841      * later skip the inserted order. */
00842     if (sel_ord <= u->cur_real_order_index) {
00843       uint cur = u->cur_real_order_index + 1;
00844       /* Check if we don't go out of bound */
00845       if (cur < u->GetNumOrders()) {
00846         u->cur_real_order_index = cur;
00847       }
00848     }
00849     if (sel_ord == u->cur_auto_order_index && u->IsGroundVehicle()) {
00850       /* We are inserting an order just before the current automatic order.
00851        * We do not know whether we will reach current automatic or the newly inserted order first.
00852        * So, disable creation of automatic orders until we are on track again. */
00853       uint16 &gv_flags = u->GetGroundVehicleFlags();
00854       SetBit(gv_flags, GVF_SUPPRESS_AUTOMATIC_ORDERS);
00855     }
00856     if (sel_ord <= u->cur_auto_order_index) {
00857       uint cur = u->cur_auto_order_index + 1;
00858       /* Check if we don't go out of bound */
00859       if (cur < u->GetNumOrders()) {
00860         u->cur_auto_order_index = cur;
00861       }
00862     }
00863     /* Update any possible open window of the vehicle */
00864     InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
00865   }
00866 
00867   /* As we insert an order, the order to skip to will be 'wrong'. */
00868   VehicleOrderID cur_order_id = 0;
00869   Order *order;
00870   FOR_VEHICLE_ORDERS(v, order) {
00871     if (order->IsType(OT_CONDITIONAL)) {
00872       VehicleOrderID order_id = order->GetConditionSkipToOrder();
00873       if (order_id >= sel_ord) {
00874         order->SetConditionSkipToOrder(order_id + 1);
00875       }
00876       if (order_id == cur_order_id) {
00877         order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
00878       }
00879     }
00880     cur_order_id++;
00881   }
00882 
00883   PrefillRouteLinks(v);
00884 
00885   /* Make sure to rebuild the whole list */
00886   InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00887 }
00888 
00894 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
00895 {
00896   if (flags & DC_EXEC) {
00897     DeleteVehicleOrders(dst);
00898     InvalidateVehicleOrder(dst, -1);
00899     InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
00900   }
00901   return CommandCost();
00902 }
00903 
00913 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00914 {
00915   VehicleID veh_id = GB(p1, 0, 20);
00916   VehicleOrderID sel_ord = GB(p2, 0, 8);
00917 
00918   Vehicle *v = Vehicle::GetIfValid(veh_id);
00919 
00920   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00921 
00922   CommandCost ret = CheckOwnership(v->owner);
00923   if (ret.Failed()) return ret;
00924 
00925   /* If we did not select an order, we maybe want to de-clone the orders */
00926   if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
00927 
00928   if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
00929 
00930   if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
00931   return CommandCost();
00932 }
00933 
00938 static void CancelLoadingDueToDeletedOrder(Vehicle *v)
00939 {
00940   assert(v->current_order.IsType(OT_LOADING));
00941   /* NON-stop flag is misused to see if a train is in a station that is
00942    * on his order list or not */
00943   v->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
00944   /* When full loading, "cancel" that order so the vehicle doesn't
00945    * stay indefinitely at this station anymore. */
00946   if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) v->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
00947 }
00948 
00954 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
00955 {
00956   v->orders.list->DeleteOrderAt(sel_ord);
00957 
00958   Vehicle *u = v->FirstShared();
00959   DeleteOrderWarnings(u);
00960   PrefillRouteLinks(u);
00961   for (; u != NULL; u = u->NextShared()) {
00962     assert(v->orders.list == u->orders.list);
00963 
00964     if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
00965       CancelLoadingDueToDeletedOrder(u);
00966     }
00967 
00968     if (sel_ord < u->cur_real_order_index) {
00969       u->cur_real_order_index--;
00970     } else if (sel_ord == u->cur_real_order_index) {
00971       u->UpdateRealOrderIndex();
00972     }
00973 
00974     if (sel_ord < u->cur_auto_order_index) {
00975       u->cur_auto_order_index--;
00976     } else if (sel_ord == u->cur_auto_order_index) {
00977       /* Make sure the index is valid */
00978       if (u->cur_auto_order_index >= u->GetNumOrders()) u->cur_auto_order_index = 0;
00979 
00980       /* Skip non-automatic orders for the auto-order-index (e.g. if the current auto order was deleted */
00981       while (u->cur_auto_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_auto_order_index)->IsType(OT_AUTOMATIC)) {
00982         u->cur_auto_order_index++;
00983         if (u->cur_auto_order_index >= u->GetNumOrders()) u->cur_auto_order_index = 0;
00984       }
00985     }
00986 
00987     /* Update any possible open window of the vehicle */
00988     InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
00989 
00990     /* Clear the next unload station of all cargo packets, it might not be in the orders anymore. */
00991     u->cargo.InvalidateNextStation();
00992   }
00993 
00994   /* As we delete an order, the order to skip to will be 'wrong'. */
00995   VehicleOrderID cur_order_id = 0;
00996   Order *order = NULL;
00997   FOR_VEHICLE_ORDERS(v, order) {
00998     if (order->IsType(OT_CONDITIONAL)) {
00999       VehicleOrderID order_id = order->GetConditionSkipToOrder();
01000       if (order_id >= sel_ord) {
01001         order_id = max(order_id - 1, 0);
01002       }
01003       if (order_id == cur_order_id) {
01004         order_id = (order_id + 1) % v->GetNumOrders();
01005       }
01006       order->SetConditionSkipToOrder(order_id);
01007     }
01008     cur_order_id++;
01009   }
01010 
01011   InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01012 }
01013 
01023 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01024 {
01025   VehicleID veh_id = GB(p1, 0, 20);
01026   VehicleOrderID sel_ord = GB(p2, 0, 8);
01027 
01028   Vehicle *v = Vehicle::GetIfValid(veh_id);
01029 
01030   if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_auto_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
01031 
01032   CommandCost ret = CheckOwnership(v->owner);
01033   if (ret.Failed()) return ret;
01034 
01035   if (flags & DC_EXEC) {
01036     v->cur_auto_order_index = v->cur_real_order_index = sel_ord;
01037     v->UpdateRealOrderIndex();
01038 
01039     if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
01040 
01041     InvalidateVehicleOrder(v, -2);
01042   }
01043 
01044   /* We have an aircraft/ship, they have a mini-schedule, so update them all */
01045   if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
01046   if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
01047 
01048   return CommandCost();
01049 }
01050 
01064 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01065 {
01066   VehicleID veh = GB(p1, 0, 20);
01067   VehicleOrderID moving_order = GB(p2,  0, 16);
01068   VehicleOrderID target_order = GB(p2, 16, 16);
01069 
01070   Vehicle *v = Vehicle::GetIfValid(veh);
01071   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01072 
01073   CommandCost ret = CheckOwnership(v->owner);
01074   if (ret.Failed()) return ret;
01075 
01076   /* Don't make senseless movements */
01077   if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
01078       moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
01079 
01080   Order *moving_one = v->GetOrder(moving_order);
01081   /* Don't move an empty order */
01082   if (moving_one == NULL) return CMD_ERROR;
01083 
01084   if (flags & DC_EXEC) {
01085     v->orders.list->MoveOrder(moving_order, target_order);
01086 
01087     /* Update shared list */
01088     Vehicle *u = v->FirstShared();
01089 
01090     DeleteOrderWarnings(u);
01091 
01092     for (; u != NULL; u = u->NextShared()) {
01093       /* Update the current order.
01094        * There are multiple ways to move orders, which result in cur_auto_order_index
01095        * and cur_real_order_index to not longer make any sense. E.g. moving another
01096        * real order between them.
01097        *
01098        * Basically one could choose to preserve either of them, but not both.
01099        * While both ways are suitable in this or that case from a human point of view, neither
01100        * of them makes really sense.
01101        * However, from an AI point of view, preserving cur_real_order_index is the most
01102        * predictable and transparent behaviour.
01103        *
01104        * With that decision it basically does not matter what we do to cur_auto_order_index.
01105        * If we change orders between the auto- and real-index, the auto orders are mostly likely
01106        * completely out-dated anyway. So, keep it simple and just keep cur_auto_order_index as well.
01107        * The worst which can happen is that a lot of automatic orders are removed when reaching current_order.
01108        */
01109       if (u->cur_real_order_index == moving_order) {
01110         u->cur_real_order_index = target_order;
01111       } else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
01112         u->cur_real_order_index--;
01113       } else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
01114         u->cur_real_order_index++;
01115       }
01116 
01117       if (u->cur_auto_order_index == moving_order) {
01118         u->cur_auto_order_index = target_order;
01119       } else if (u->cur_auto_order_index > moving_order && u->cur_auto_order_index <= target_order) {
01120         u->cur_auto_order_index--;
01121       } else if (u->cur_auto_order_index < moving_order && u->cur_auto_order_index >= target_order) {
01122         u->cur_auto_order_index++;
01123       }
01124 
01125       assert(v->orders.list == u->orders.list);
01126       /* Update any possible open window of the vehicle */
01127       InvalidateVehicleOrder(u, moving_order | (target_order << 8));
01128     }
01129 
01130     /* As we move an order, the order to skip to will be 'wrong'. */
01131     Order *order;
01132     FOR_VEHICLE_ORDERS(v, order) {
01133       if (order->IsType(OT_CONDITIONAL)) {
01134         VehicleOrderID order_id = order->GetConditionSkipToOrder();
01135         if (order_id == moving_order) {
01136           order_id = target_order;
01137         } else if (order_id > moving_order && order_id <= target_order) {
01138           order_id--;
01139         } else if (order_id < moving_order && order_id >= target_order) {
01140           order_id++;
01141         }
01142         order->SetConditionSkipToOrder(order_id);
01143       }
01144     }
01145 
01146     /* Make sure to rebuild the whole list */
01147     InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01148   }
01149 
01150   return CommandCost();
01151 }
01152 
01168 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01169 {
01170   VehicleOrderID sel_ord = GB(p1, 20,  8);
01171   VehicleID veh          = GB(p1,  0, 20);
01172   ModifyOrderFlags mof   = Extract<ModifyOrderFlags, 0, 4>(p2);
01173   uint16 data            = GB(p2,  4, 11);
01174 
01175   if (mof >= MOF_END) return CMD_ERROR;
01176 
01177   Vehicle *v = Vehicle::GetIfValid(veh);
01178   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01179 
01180   CommandCost ret = CheckOwnership(v->owner);
01181   if (ret.Failed()) return ret;
01182 
01183   /* Is it a valid order? */
01184   if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
01185 
01186   Order *order = v->GetOrder(sel_ord);
01187   switch (order->GetType()) {
01188     case OT_GOTO_STATION:
01189       if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
01190       break;
01191 
01192     case OT_GOTO_DEPOT:
01193       if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
01194       break;
01195 
01196     case OT_GOTO_WAYPOINT:
01197       if (mof != MOF_NON_STOP) return CMD_ERROR;
01198       break;
01199 
01200     case OT_CONDITIONAL:
01201       if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
01202       break;
01203 
01204     default:
01205       return CMD_ERROR;
01206   }
01207 
01208   switch (mof) {
01209     default: NOT_REACHED();
01210 
01211     case MOF_NON_STOP:
01212       if (!v->IsGroundVehicle()) return CMD_ERROR;
01213       if (data >= ONSF_END) return CMD_ERROR;
01214       if (data == order->GetNonStopType()) return CMD_ERROR;
01215       break;
01216 
01217     case MOF_STOP_LOCATION:
01218       if (v->type != VEH_TRAIN) return CMD_ERROR;
01219       if (data >= OSL_END) return CMD_ERROR;
01220       break;
01221 
01222     case MOF_UNLOAD:
01223       if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
01224       /* Unload and no-unload are mutual exclusive and so are transfer and no unload. */
01225       if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
01226       if (data == order->GetUnloadType()) return CMD_ERROR;
01227       break;
01228 
01229     case MOF_LOAD:
01230       if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
01231       if (data == order->GetLoadType()) return CMD_ERROR;
01232       break;
01233 
01234     case MOF_DEPOT_ACTION:
01235       if (data >= DA_END) return CMD_ERROR;
01236       break;
01237 
01238     case MOF_COND_VARIABLE:
01239       if (data >= OCV_END) return CMD_ERROR;
01240       break;
01241 
01242     case MOF_COND_COMPARATOR:
01243       if (data >= OCC_END) return CMD_ERROR;
01244       switch (order->GetConditionVariable()) {
01245         case OCV_UNCONDITIONALLY: return CMD_ERROR;
01246 
01247         case OCV_REQUIRES_SERVICE:
01248           if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
01249           break;
01250 
01251         default:
01252           if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
01253           break;
01254       }
01255       break;
01256 
01257     case MOF_COND_VALUE:
01258       switch (order->GetConditionVariable()) {
01259         case OCV_UNCONDITIONALLY: return CMD_ERROR;
01260 
01261         case OCV_LOAD_PERCENTAGE:
01262         case OCV_RELIABILITY:
01263           if (data > 100) return CMD_ERROR;
01264           break;
01265 
01266         default:
01267           if (data > 2047) return CMD_ERROR;
01268           break;
01269       }
01270       break;
01271 
01272     case MOF_COND_DESTINATION:
01273       if (data >= v->GetNumOrders()) return CMD_ERROR;
01274       break;
01275   }
01276 
01277   if (flags & DC_EXEC) {
01278     switch (mof) {
01279       case MOF_NON_STOP:
01280         order->SetNonStopType((OrderNonStopFlags)data);
01281         if (data & ONSF_NO_STOP_AT_DESTINATION_STATION) InvalidateOrderRouteLinks(order->index);
01282         PrefillRouteLinks(v);
01283         break;
01284 
01285       case MOF_STOP_LOCATION:
01286         order->SetStopLocation((OrderStopLocation)data);
01287         break;
01288 
01289       case MOF_UNLOAD:
01290         order->SetUnloadType((OrderUnloadFlags)data);
01291         break;
01292 
01293       case MOF_LOAD:
01294         order->SetLoadType((OrderLoadFlags)data);
01295         break;
01296 
01297       case MOF_DEPOT_ACTION: {
01298         switch (data) {
01299           case DA_ALWAYS_GO:
01300             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01301             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01302             break;
01303 
01304           case DA_SERVICE:
01305             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
01306             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01307             break;
01308 
01309           case DA_STOP:
01310             order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01311             order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
01312             break;
01313 
01314           default:
01315             NOT_REACHED();
01316         }
01317         break;
01318       }
01319 
01320       case MOF_COND_VARIABLE: {
01321         order->SetConditionVariable((OrderConditionVariable)data);
01322 
01323         OrderConditionComparator occ = order->GetConditionComparator();
01324         switch (order->GetConditionVariable()) {
01325           case OCV_UNCONDITIONALLY:
01326             order->SetConditionComparator(OCC_EQUALS);
01327             order->SetConditionValue(0);
01328             break;
01329 
01330           case OCV_REQUIRES_SERVICE:
01331             if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
01332             break;
01333 
01334           case OCV_LOAD_PERCENTAGE:
01335           case OCV_RELIABILITY:
01336             if (order->GetConditionValue() > 100) order->SetConditionValue(100);
01337             /* FALL THROUGH */
01338           default:
01339             if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
01340             break;
01341         }
01342         break;
01343       }
01344 
01345       case MOF_COND_COMPARATOR:
01346         order->SetConditionComparator((OrderConditionComparator)data);
01347         break;
01348 
01349       case MOF_COND_VALUE:
01350         order->SetConditionValue(data);
01351         break;
01352 
01353       case MOF_COND_DESTINATION:
01354         order->SetConditionSkipToOrder(data);
01355         break;
01356 
01357       default: NOT_REACHED();
01358     }
01359 
01360     /* Update the windows and full load flags, also for vehicles that share the same order list */
01361     Vehicle *u = v->FirstShared();
01362     DeleteOrderWarnings(u);
01363     for (; u != NULL; u = u->NextShared()) {
01364       /* Toggle u->current_order "Full load" flag if it changed.
01365        * However, as the same flag is used for depot orders, check
01366        * whether we are not going to a depot as there are three
01367        * cases where the full load flag can be active and only
01368        * one case where the flag is used for depot orders. In the
01369        * other cases for the OrderTypeByte the flags are not used,
01370        * so do not care and those orders should not be active
01371        * when this function is called.
01372        */
01373       if (sel_ord == u->cur_real_order_index &&
01374           (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
01375           u->current_order.GetLoadType() != order->GetLoadType()) {
01376         u->current_order.SetLoadType(order->GetLoadType());
01377       }
01378       InvalidateVehicleOrder(u, -2);
01379 
01380       /* Invalidate the next unload station of all packets as we might not unload there anymore. */
01381       u->cargo.InvalidateNextStation();
01382     }
01383   }
01384 
01385   return CommandCost();
01386 }
01387 
01399 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01400 {
01401   VehicleID veh_src = GB(p2, 0, 20);
01402   VehicleID veh_dst = GB(p1, 0, 20);
01403 
01404   Vehicle *dst = Vehicle::GetIfValid(veh_dst);
01405   if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
01406 
01407   CommandCost ret = CheckOwnership(dst->owner);
01408   if (ret.Failed()) return ret;
01409 
01410   switch (GB(p1, 30, 2)) {
01411     case CO_SHARE: {
01412       Vehicle *src = Vehicle::GetIfValid(veh_src);
01413 
01414       /* Sanity checks */
01415       if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01416 
01417       CommandCost ret = CheckOwnership(src->owner);
01418       if (ret.Failed()) return ret;
01419 
01420       /* Trucks can't share orders with busses (and visa versa) */
01421       if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
01422         return CMD_ERROR;
01423       }
01424 
01425       /* Is the vehicle already in the shared list? */
01426       if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
01427 
01428       const Order *order;
01429 
01430       FOR_VEHICLE_ORDERS(src, order) {
01431         if (OrderGoesToStation(dst, order) &&
01432             !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01433           return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01434         }
01435       }
01436 
01437       if (src->orders.list == NULL && !OrderList::CanAllocateItem()) {
01438         return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01439       }
01440 
01441       if (flags & DC_EXEC) {
01442         /* If the destination vehicle had a OrderList, destroy it.
01443          * We only reset the order indices, if the new orders are obviously different.
01444          * (We mainly do this to keep the order indices valid and in range.) */
01445         DeleteVehicleOrders(dst, false, dst->GetNumOrders() != src->GetNumOrders());
01446 
01447         dst->orders.list = src->orders.list;
01448 
01449         /* Link this vehicle in the shared-list */
01450         dst->AddToShared(src);
01451         PrefillRouteLinks(dst);
01452 
01453         InvalidateVehicleOrder(dst, -1);
01454         InvalidateVehicleOrder(src, -2);
01455 
01456         InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01457       }
01458       break;
01459     }
01460 
01461     case CO_COPY: {
01462       Vehicle *src = Vehicle::GetIfValid(veh_src);
01463 
01464       /* Sanity checks */
01465       if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01466 
01467       CommandCost ret = CheckOwnership(src->owner);
01468       if (ret.Failed()) return ret;
01469 
01470       /* Trucks can't copy all the orders from busses (and visa versa),
01471        * and neither can helicopters and aircarft. */
01472       const Order *order;
01473       FOR_VEHICLE_ORDERS(src, order) {
01474         if (OrderGoesToStation(dst, order) &&
01475             !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01476           return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01477         }
01478       }
01479 
01480       /* make sure there are orders available */
01481       int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
01482       if (!Order::CanAllocateItem(delta) ||
01483           ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
01484         return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01485       }
01486 
01487       if (flags & DC_EXEC) {
01488         const Order *order;
01489         Order *first = NULL;
01490         Order **order_dst;
01491 
01492         /* If the destination vehicle had an order list, destroy the chain but keep the OrderList.
01493          * We only reset the order indices, if the new orders are obviously different.
01494          * (We mainly do this to keep the order indices valid and in range.) */
01495         DeleteVehicleOrders(dst, true, dst->GetNumOrders() != src->GetNumOrders());
01496 
01497         order_dst = &first;
01498         FOR_VEHICLE_ORDERS(src, order) {
01499           *order_dst = new Order();
01500           (*order_dst)->AssignOrder(*order);
01501           order_dst = &(*order_dst)->next;
01502         }
01503         if (dst->orders.list == NULL) {
01504           dst->orders.list = new OrderList(first, dst);
01505         } else {
01506           assert(dst->orders.list->GetFirstOrder() == NULL);
01507           assert(!dst->orders.list->IsShared());
01508           delete dst->orders.list;
01509           assert(OrderList::CanAllocateItem());
01510           dst->orders.list = new OrderList(first, dst);
01511         }
01512 
01513         PrefillRouteLinks(dst);
01514         InvalidateVehicleOrder(dst, -1);
01515 
01516         InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01517       }
01518       break;
01519     }
01520 
01521     case CO_UNSHARE: return DecloneOrder(dst, flags);
01522     default: return CMD_ERROR;
01523   }
01524 
01525   return CommandCost();
01526 }
01527 
01540 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01541 {
01542   VehicleID veh = GB(p1, 0, 20);
01543   VehicleOrderID order_number  = GB(p2, 16, 8);
01544   CargoID cargo = GB(p2, 0, 8);
01545   byte subtype  = GB(p2, 8, 8);
01546 
01547   if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT) return CMD_ERROR;
01548 
01549   const Vehicle *v = Vehicle::GetIfValid(veh);
01550   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01551 
01552   CommandCost ret = CheckOwnership(v->owner);
01553   if (ret.Failed()) return ret;
01554 
01555   Order *order = v->GetOrder(order_number);
01556   if (order == NULL) return CMD_ERROR;
01557 
01558   if (flags & DC_EXEC) {
01559     order->SetRefit(cargo, subtype);
01560 
01561     for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
01562       /* Update any possible open window of the vehicle */
01563       InvalidateVehicleOrder(u, -2);
01564 
01565       /* If the vehicle already got the current depot set as current order, then update current order as well */
01566       if (u->cur_real_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
01567         u->current_order.SetRefit(cargo, subtype);
01568       }
01569     }
01570   }
01571 
01572   return CommandCost();
01573 }
01574 
01575 
01581 void CheckOrders(const Vehicle *v)
01582 {
01583   /* Does the user wants us to check things? */
01584   if (_settings_client.gui.order_review_system == 0) return;
01585 
01586   /* Do nothing for crashed vehicles */
01587   if (v->vehstatus & VS_CRASHED) return;
01588 
01589   /* Do nothing for stopped vehicles if setting is '1' */
01590   if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
01591 
01592   /* do nothing we we're not the first vehicle in a share-chain */
01593   if (v->FirstShared() != v) return;
01594 
01595   /* Only check every 20 days, so that we don't flood the message log */
01596   if (v->owner == _local_company && v->day_counter % 20 == 0) {
01597     int n_st, problem_type = -1;
01598     const Order *order;
01599     int message = 0;
01600 
01601     /* Check the order list */
01602     n_st = 0;
01603 
01604     FOR_VEHICLE_ORDERS(v, order) {
01605       /* Dummy order? */
01606       if (order->IsType(OT_DUMMY)) {
01607         problem_type = 1;
01608         break;
01609       }
01610       /* Does station have a load-bay for this vehicle? */
01611       if (order->IsType(OT_GOTO_STATION)) {
01612         const Station *st = Station::Get(order->GetDestination());
01613 
01614         n_st++;
01615         if (!CanVehicleUseStation(v, st)) problem_type = 3;
01616       }
01617     }
01618 
01619     /* Check if the last and the first order are the same */
01620     if (v->GetNumOrders() > 1) {
01621       const Order *last = v->GetLastOrder();
01622 
01623       if (v->orders.list->GetFirstOrder()->Equals(*last)) {
01624         problem_type = 2;
01625       }
01626     }
01627 
01628     /* Do we only have 1 station in our order list? */
01629     if (n_st < 2 && problem_type == -1) problem_type = 0;
01630 
01631 #ifndef NDEBUG
01632     if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
01633 #endif
01634 
01635     /* We don't have a problem */
01636     if (problem_type < 0) return;
01637 
01638     message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
01639     //DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
01640 
01641     SetDParam(0, v->index);
01642     AddVehicleNewsItem(
01643       message,
01644       NS_ADVICE,
01645       v->index
01646     );
01647   }
01648 }
01649 
01655 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
01656 {
01657   Vehicle *v;
01658 
01659   /* Aircraft have StationIDs for depot orders and never use DepotIDs
01660    * This fact is handled specially below
01661    */
01662 
01663   /* Go through all vehicles */
01664   FOR_ALL_VEHICLES(v) {
01665     Order *order;
01666 
01667     order = &v->current_order;
01668     if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
01669         v->current_order.GetDestination() == destination) {
01670       order->MakeDummy();
01671       SetWindowDirty(WC_VEHICLE_VIEW, v->index);
01672     }
01673 
01674     /* Clear the order from the order-list */
01675     int id = -1;
01676     FOR_VEHICLE_ORDERS(v, order) {
01677       id++;
01678 restart:
01679 
01680       OrderType ot = order->GetType();
01681       if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
01682       if (ot == OT_AUTOMATIC || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
01683       if (ot == type && order->GetDestination() == destination) {
01684         /* We want to clear automatic orders, but we don't want to make them
01685          * dummy orders. They should just vanish. Also check the actual order
01686          * type as ot is currently OT_GOTO_STATION. */
01687         if (order->IsType(OT_AUTOMATIC)) {
01688           order = order->next; // DeleteOrder() invalidates current order
01689           DeleteOrder(v, id);
01690           if (order != NULL) goto restart;
01691           break;
01692         }
01693 
01694         order->MakeDummy();
01695         for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
01696           /* In GUI, simulate by removing the order and adding it back */
01697           InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
01698           InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
01699         }
01700       }
01701     }
01702   }
01703 }
01704 
01709 bool Vehicle::HasDepotOrder() const
01710 {
01711   const Order *order;
01712 
01713   FOR_VEHICLE_ORDERS(this, order) {
01714     if (order->IsType(OT_GOTO_DEPOT)) return true;
01715   }
01716 
01717   return false;
01718 }
01719 
01729 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
01730 {
01731   DeleteOrderWarnings(v);
01732 
01733   if (v->IsOrderListShared()) {
01734     /* Remove ourself from the shared order list. */
01735     v->RemoveFromShared();
01736     v->orders.list = NULL;
01737   } else if (v->orders.list != NULL) {
01738     /* Remove the orders */
01739     v->orders.list->FreeChain(keep_orderlist);
01740     if (!keep_orderlist) v->orders.list = NULL;
01741   }
01742 
01743   /* Invalidate the next unload station of all cargo. */
01744   v->cargo.InvalidateNextStation();
01745 
01746   if (reset_order_indices) {
01747     v->cur_auto_order_index = v->cur_real_order_index = 0;
01748     if (v->current_order.IsType(OT_LOADING)) {
01749       CancelLoadingDueToDeletedOrder(v);
01750     }
01751   }
01752 }
01753 
01761 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
01762 {
01763   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);
01764 }
01765 
01774 static bool CheckForValidOrders(const Vehicle *v)
01775 {
01776   const Order *order;
01777 
01778   FOR_VEHICLE_ORDERS(v, order) {
01779     switch (order->GetType()) {
01780       case OT_GOTO_STATION:
01781       case OT_GOTO_DEPOT:
01782       case OT_GOTO_WAYPOINT:
01783         return true;
01784 
01785       default:
01786         break;
01787     }
01788   }
01789 
01790   return false;
01791 }
01792 
01796 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
01797 {
01798   switch (occ) {
01799     case OCC_EQUALS:      return variable == value;
01800     case OCC_NOT_EQUALS:  return variable != value;
01801     case OCC_LESS_THAN:   return variable <  value;
01802     case OCC_LESS_EQUALS: return variable <= value;
01803     case OCC_MORE_THAN:   return variable >  value;
01804     case OCC_MORE_EQUALS: return variable >= value;
01805     case OCC_IS_TRUE:     return variable != 0;
01806     case OCC_IS_FALSE:    return variable == 0;
01807     default: NOT_REACHED();
01808   }
01809 }
01810 
01817 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
01818 {
01819   if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
01820 
01821   bool skip_order = false;
01822   OrderConditionComparator occ = order->GetConditionComparator();
01823   uint16 value = order->GetConditionValue();
01824 
01825   switch (order->GetConditionVariable()) {
01826     case OCV_LOAD_PERCENTAGE:  skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
01827     case OCV_RELIABILITY:      skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability),       value); break;
01828     case OCV_MAX_SPEED:        skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
01829     case OCV_AGE:              skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR,        value); break;
01830     case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(),               value); break;
01831     case OCV_UNCONDITIONALLY:  skip_order = true; break;
01832     default: NOT_REACHED();
01833   }
01834 
01835   return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
01836 }
01837 
01844 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth)
01845 {
01846   if (conditional_depth > v->GetNumOrders()) return false;
01847 
01848   switch (order->GetType()) {
01849     case OT_GOTO_STATION:
01850       v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
01851       return true;
01852 
01853     case OT_GOTO_DEPOT:
01854       if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
01855         UpdateVehicleTimetable(v, true);
01856         v->IncrementRealOrderIndex();
01857         break;
01858       }
01859 
01860       if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
01861         /* We need to search for the nearest depot (hangar). */
01862         TileIndex location;
01863         DestinationID destination;
01864         bool reverse;
01865 
01866         if (v->FindClosestDepot(&location, &destination, &reverse)) {
01867           v->dest_tile = location;
01868           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());
01869 
01870           /* If there is no depot in front, reverse automatically (trains only) */
01871           if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
01872 
01873           if (v->type == VEH_AIRCRAFT) {
01874             Aircraft *a = Aircraft::From(v);
01875             if (a->state == FLYING && a->targetairport != destination) {
01876               /* The aircraft is now heading for a different hangar than the next in the orders */
01877               extern void AircraftNextAirportPos_and_Order(Aircraft *a);
01878               AircraftNextAirportPos_and_Order(a);
01879             }
01880           }
01881           return true;
01882         }
01883 
01884         UpdateVehicleTimetable(v, true);
01885         v->IncrementRealOrderIndex();
01886       } else {
01887         if (v->type != VEH_AIRCRAFT) {
01888           v->dest_tile = Depot::Get(order->GetDestination())->xy;
01889         }
01890         return true;
01891       }
01892       break;
01893 
01894     case OT_GOTO_WAYPOINT:
01895       v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
01896       return true;
01897 
01898     case OT_CONDITIONAL: {
01899       VehicleOrderID next_order = ProcessConditionalOrder(order, v);
01900       if (next_order != INVALID_VEH_ORDER_ID) {
01901         /* Jump to next_order. cur_auto_order_index becomes exactly that order,
01902          * cur_real_order_index might come after next_order. */
01903         UpdateVehicleTimetable(v, false);
01904         v->cur_auto_order_index = v->cur_real_order_index = next_order;
01905         v->UpdateRealOrderIndex();
01906         v->current_order_time += v->GetOrder(v->cur_real_order_index)->travel_time;
01907 
01908         /* Disable creation of automatic orders.
01909          * When inserting them we do not know that we would have to make the conditional orders point to them. */
01910         if (v->IsGroundVehicle()) {
01911           uint16 &gv_flags = v->GetGroundVehicleFlags();
01912           SetBit(gv_flags, GVF_SUPPRESS_AUTOMATIC_ORDERS);
01913         }
01914       } else {
01915         UpdateVehicleTimetable(v, true);
01916         v->IncrementRealOrderIndex();
01917       }
01918       break;
01919     }
01920 
01921     default:
01922       v->dest_tile = 0;
01923       return false;
01924   }
01925 
01926   assert(v->cur_auto_order_index < v->GetNumOrders());
01927   assert(v->cur_real_order_index < v->GetNumOrders());
01928 
01929   /* Get the current order */
01930   order = v->GetOrder(v->cur_real_order_index);
01931   if (order != NULL && order->IsType(OT_AUTOMATIC)) {
01932     assert(v->GetNumManualOrders() == 0);
01933     order = NULL;
01934   }
01935 
01936   if (order == NULL) {
01937     v->current_order.Free();
01938     v->dest_tile = 0;
01939     return false;
01940   }
01941 
01942   v->current_order = *order;
01943   /* Set the index of the current order to the index of the auto order,
01944    * this is needed as the index is used for route link generation. */
01945   Order *auto_order = v->GetOrder(v->cur_auto_order_index);
01946   v->current_order.index = auto_order->index;
01947 
01948   return UpdateOrderDest(v, order, conditional_depth + 1);
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->DeleteUnreachedAutoOrders();
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->IncrementAutoOrderIndex();
01997   }
01998 
01999   /* Get the current order */
02000   assert(v->cur_auto_order_index == 0 || v->cur_auto_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_AUTOMATIC)) {
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   /* Set the index of the current order to the index of the auto order,
02032    * this is needed as the index is used for route link generation. */
02033   Order *auto_order = v->GetOrder(v->cur_auto_order_index);
02034   v->current_order.index = auto_order->index;
02035 
02036   InvalidateVehicleOrder(v, -2);
02037   switch (v->type) {
02038     default:
02039       NOT_REACHED();
02040 
02041     case VEH_ROAD:
02042     case VEH_TRAIN:
02043       break;
02044 
02045     case VEH_AIRCRAFT:
02046     case VEH_SHIP:
02047       SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
02048       break;
02049   }
02050 
02051   return UpdateOrderDest(v, order) && may_reverse;
02052 }
02053 
02061 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
02062 {
02063   bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
02064 
02065   return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
02066       v->last_station_visited != station && // Do stop only when we've not just been there
02067       /* Finally do stop when there is no non-stop flag set for this type of station. */
02068       !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
02069 }

Generated on Sun May 8 07:30:16 2011 for OpenTTD by  doxygen 1.6.1