(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / order_cmd.cpp
blob5f69fcd1472339c99fd50298f5e296e39dcb0a2f
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file order_cmd.cpp Handling of orders. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "cmd_helper.h"
15 #include "command_func.h"
16 #include "company_func.h"
17 #include "news_func.h"
18 #include "vehicle_gui.h"
19 #include "strings_func.h"
20 #include "window_func.h"
21 #include "timetable.h"
22 #include "vehicle_func.h"
23 #include "depot_base.h"
24 #include "core/pool_func.hpp"
25 #include "aircraft.h"
26 #include "roadveh.h"
27 #include "station_base.h"
28 #include "waypoint_base.h"
29 #include "company_base.h"
31 #include "table/strings.h"
33 /* DestinationID must be at least as large as every these below, because it can
34 * be any of them
36 assert_compile(sizeof(DestinationID) >= sizeof(DepotID));
37 assert_compile(sizeof(DestinationID) >= sizeof(StationID));
39 OrderPool _order_pool("Order");
40 INSTANTIATE_POOL_METHODS(Order)
41 OrderListPool _orderlist_pool("OrderList");
42 INSTANTIATE_POOL_METHODS(OrderList)
44 /**
45 * 'Free' the order
46 * @note ONLY use on "current_order" vehicle orders!
48 void Order::Free()
50 this->type = OT_NOTHING;
51 this->flags = 0;
52 this->dest = 0;
53 this->next = NULL;
56 /**
57 * Makes this order a Go To Station order.
58 * @param destination the station to go to.
60 void Order::MakeGoToStation(StationID destination)
62 this->type = OT_GOTO_STATION;
63 this->flags = 0;
64 this->dest = destination;
67 /**
68 * Makes this order a Go To Depot order.
69 * @param destination the depot to go to.
70 * @param order is this order a 'default' order, or an overriden vehicle order?
71 * @param non_stop_type how to get to the depot?
72 * @param action what to do in the depot?
73 * @param cargo the cargo type to change to.
74 * @param subtype the subtype to change to.
76 void Order::MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type, OrderDepotActionFlags action, CargoID cargo, byte subtype)
78 this->type = OT_GOTO_DEPOT;
79 this->SetDepotOrderType(order);
80 this->SetDepotActionType(action);
81 this->SetNonStopType(non_stop_type);
82 this->dest = destination;
83 this->SetRefit(cargo, subtype);
86 /**
87 * Makes this order a Go To Waypoint order.
88 * @param destination the waypoint to go to.
90 void Order::MakeGoToWaypoint(StationID destination)
92 this->type = OT_GOTO_WAYPOINT;
93 this->flags = 0;
94 this->dest = destination;
97 /**
98 * Makes this order a Loading order.
99 * @param ordered is this an ordered stop?
101 void Order::MakeLoading(bool ordered)
103 this->type = OT_LOADING;
104 if (!ordered) this->flags = 0;
108 * Makes this order a Leave Station order.
110 void Order::MakeLeaveStation()
112 this->type = OT_LEAVESTATION;
113 this->flags = 0;
117 * Makes this order a Dummy order.
119 void Order::MakeDummy()
121 this->type = OT_DUMMY;
122 this->flags = 0;
126 * Makes this order an conditional order.
127 * @param order the order to jump to.
129 void Order::MakeConditional(VehicleOrderID order)
131 this->type = OT_CONDITIONAL;
132 this->flags = order;
133 this->dest = 0;
137 * Makes this order an implicit order.
138 * @param destination the station to go to.
140 void Order::MakeImplicit(StationID destination)
142 this->type = OT_IMPLICIT;
143 this->dest = destination;
147 * Make this depot order also a refit order.
148 * @param cargo the cargo type to change to.
149 * @param subtype the subtype to change to.
150 * @pre IsType(OT_GOTO_DEPOT).
152 void Order::SetRefit(CargoID cargo, byte subtype)
154 this->refit_cargo = cargo;
155 this->refit_subtype = subtype;
159 * Does this order have the same type, flags and destination?
160 * @param other the second order to compare to.
161 * @return true if the type, flags and destination match.
163 bool Order::Equals(const Order &other) const
165 /* In case of go to nearest depot orders we need "only" compare the flags
166 * with the other and not the nearest depot order bit or the actual
167 * destination because those get clear/filled in during the order
168 * evaluation. If we do not do this the order will continuously be seen as
169 * a different order and it will try to find a "nearest depot" every tick. */
170 if ((this->IsType(OT_GOTO_DEPOT) && this->type == other.type) &&
171 ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0 ||
172 (other.GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0)) {
173 return this->GetDepotOrderType() == other.GetDepotOrderType() &&
174 (this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT) == (other.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT);
177 return this->type == other.type && this->flags == other.flags && this->dest == other.dest;
181 * Pack this order into a 32 bits integer, or actually only
182 * the type, flags and destination.
183 * @return the packed representation.
184 * @note unpacking is done in the constructor.
186 uint32 Order::Pack() const
188 return this->dest << 16 | this->flags << 8 | this->type;
192 * Pack this order into a 16 bits integer as close to the TTD
193 * representation as possible.
194 * @return the TTD-like packed representation.
196 uint16 Order::MapOldOrder() const
198 uint16 order = this->GetType();
199 switch (this->type) {
200 case OT_GOTO_STATION:
201 if (this->GetUnloadType() & OUFB_UNLOAD) SetBit(order, 5);
202 if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
203 if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) SetBit(order, 7);
204 order |= GB(this->GetDestination(), 0, 8) << 8;
205 break;
206 case OT_GOTO_DEPOT:
207 if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) SetBit(order, 6);
208 SetBit(order, 7);
209 order |= GB(this->GetDestination(), 0, 8) << 8;
210 break;
211 case OT_LOADING:
212 if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
213 break;
215 return order;
219 * Create an order based on a packed representation of that order.
220 * @param packed the packed representation.
222 Order::Order(uint32 packed)
224 this->type = (OrderType)GB(packed, 0, 8);
225 this->flags = GB(packed, 8, 8);
226 this->dest = GB(packed, 16, 16);
227 this->next = NULL;
228 this->refit_cargo = CT_NO_REFIT;
229 this->refit_subtype = 0;
230 this->wait_time = 0;
231 this->travel_time = 0;
236 * Updates the widgets of a vehicle which contains the order-data
239 void InvalidateVehicleOrder(const Vehicle *v, int data)
241 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
243 if (data != 0) {
244 /* Calls SetDirty() too */
245 InvalidateWindowData(WC_VEHICLE_ORDERS, v->index, data);
246 InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
247 return;
250 SetWindowDirty(WC_VEHICLE_ORDERS, v->index);
251 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
256 * Assign data to an order (from another order)
257 * This function makes sure that the index is maintained correctly
258 * @param other the data to copy (except next pointer).
261 void Order::AssignOrder(const Order &other)
263 this->type = other.type;
264 this->flags = other.flags;
265 this->dest = other.dest;
267 this->refit_cargo = other.refit_cargo;
268 this->refit_subtype = other.refit_subtype;
270 this->wait_time = other.wait_time;
271 this->travel_time = other.travel_time;
275 * Recomputes everything.
276 * @param chain first order in the chain
277 * @param v one of vehicle that is using this orderlist
279 void OrderList::Initialize(Order *chain, Vehicle *v)
281 this->first = chain;
282 this->first_shared = v;
284 this->num_orders = 0;
285 this->num_manual_orders = 0;
286 this->num_vehicles = 1;
287 this->timetable_duration = 0;
289 for (Order *o = this->first; o != NULL; o = o->next) {
290 ++this->num_orders;
291 if (!o->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
292 this->timetable_duration += o->wait_time + o->travel_time;
295 for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
296 ++this->num_vehicles;
297 this->first_shared = u;
300 for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
304 * Free a complete order chain.
305 * @param keep_orderlist If this is true only delete the orders, otherwise also delete the OrderList.
306 * @note do not use on "current_order" vehicle orders!
308 void OrderList::FreeChain(bool keep_orderlist)
310 Order *next;
311 for (Order *o = this->first; o != NULL; o = next) {
312 next = o->next;
313 delete o;
316 if (keep_orderlist) {
317 this->first = NULL;
318 this->num_orders = 0;
319 this->num_manual_orders = 0;
320 this->timetable_duration = 0;
321 } else {
322 delete this;
327 * Get a certain order of the order chain.
328 * @param index zero-based index of the order within the chain.
329 * @return the order at position index.
331 Order *OrderList::GetOrderAt(int index) const
333 if (index < 0) return NULL;
335 Order *order = this->first;
337 while (order != NULL && index-- > 0) {
338 order = order->next;
340 return order;
344 * Insert a new order into the order chain.
345 * @param new_order is the order to insert into the chain.
346 * @param index is the position where the order is supposed to be inserted.
348 void OrderList::InsertOrderAt(Order *new_order, int index)
350 if (this->first == NULL) {
351 this->first = new_order;
352 } else {
353 if (index == 0) {
354 /* Insert as first or only order */
355 new_order->next = this->first;
356 this->first = new_order;
357 } else if (index >= this->num_orders) {
358 /* index is after the last order, add it to the end */
359 this->GetLastOrder()->next = new_order;
360 } else {
361 /* Put the new order in between */
362 Order *order = this->GetOrderAt(index - 1);
363 new_order->next = order->next;
364 order->next = new_order;
367 ++this->num_orders;
368 if (!new_order->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
369 this->timetable_duration += new_order->wait_time + new_order->travel_time;
374 * Remove an order from the order list and delete it.
375 * @param index is the position of the order which is to be deleted.
377 void OrderList::DeleteOrderAt(int index)
379 if (index >= this->num_orders) return;
381 Order *to_remove;
383 if (index == 0) {
384 to_remove = this->first;
385 this->first = to_remove->next;
386 } else {
387 Order *prev = GetOrderAt(index - 1);
388 to_remove = prev->next;
389 prev->next = to_remove->next;
391 --this->num_orders;
392 if (!to_remove->IsType(OT_IMPLICIT)) --this->num_manual_orders;
393 this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
394 delete to_remove;
398 * Move an order to another position within the order list.
399 * @param from is the zero-based position of the order to move.
400 * @param to is the zero-based position where the order is moved to.
402 void OrderList::MoveOrder(int from, int to)
404 if (from >= this->num_orders || to >= this->num_orders || from == to) return;
406 Order *moving_one;
408 /* Take the moving order out of the pointer-chain */
409 if (from == 0) {
410 moving_one = this->first;
411 this->first = moving_one->next;
412 } else {
413 Order *one_before = GetOrderAt(from - 1);
414 moving_one = one_before->next;
415 one_before->next = moving_one->next;
418 /* Insert the moving_order again in the pointer-chain */
419 if (to == 0) {
420 moving_one->next = this->first;
421 this->first = moving_one;
422 } else {
423 Order *one_before = GetOrderAt(to - 1);
424 moving_one->next = one_before->next;
425 one_before->next = moving_one;
430 * Removes the vehicle from the shared order list.
431 * @note This is supposed to be called when the vehicle is still in the chain
432 * @param v vehicle to remove from the list
434 void OrderList::RemoveVehicle(Vehicle *v)
436 --this->num_vehicles;
437 if (v == this->first_shared) this->first_shared = v->NextShared();
441 * Checks whether a vehicle is part of the shared vehicle chain.
442 * @param v is the vehicle to search in the shared vehicle chain.
444 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
446 for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
447 if (v_shared == v) return true;
450 return false;
454 * Gets the position of the given vehicle within the shared order vehicle list.
455 * @param v is the vehicle of which to get the position
456 * @return position of v within the shared vehicle chain.
458 int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
460 int count = 0;
461 for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
462 return count;
466 * Checks whether all orders of the list have a filled timetable.
467 * @return whether all orders have a filled timetable.
469 bool OrderList::IsCompleteTimetable() const
471 for (Order *o = this->first; o != NULL; o = o->next) {
472 /* Implicit orders are, by definition, not timetabled. */
473 if (o->IsType(OT_IMPLICIT)) continue;
474 if (!o->IsCompletelyTimetabled()) return false;
476 return true;
480 * Checks for internal consistency of order list. Triggers assertion if something is wrong.
482 void OrderList::DebugCheckSanity() const
484 VehicleOrderID check_num_orders = 0;
485 VehicleOrderID check_num_manual_orders = 0;
486 uint check_num_vehicles = 0;
487 Ticks check_timetable_duration = 0;
489 DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
491 for (const Order *o = this->first; o != NULL; o = o->next) {
492 ++check_num_orders;
493 if (!o->IsType(OT_IMPLICIT)) ++check_num_manual_orders;
494 check_timetable_duration += o->wait_time + o->travel_time;
496 assert(this->num_orders == check_num_orders);
497 assert(this->num_manual_orders == check_num_manual_orders);
498 assert(this->timetable_duration == check_timetable_duration);
500 for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
501 ++check_num_vehicles;
502 assert(v->orders.list == this);
504 assert(this->num_vehicles == check_num_vehicles);
505 DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
506 (uint)this->num_orders, (uint)this->num_manual_orders,
507 this->num_vehicles, this->timetable_duration);
511 * Checks whether the order goes to a station or not, i.e. whether the
512 * destination is a station
513 * @param v the vehicle to check for
514 * @param o the order to check
515 * @return true if the destination is a station
517 static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
519 return o->IsType(OT_GOTO_STATION) ||
520 (v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
524 * Delete all news items regarding defective orders about a vehicle
525 * This could kill still valid warnings (for example about void order when just
526 * another order gets added), but assume the company will notice the problems,
527 * when (s)he's changing the orders.
529 static void DeleteOrderWarnings(const Vehicle *v)
531 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
532 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
533 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
534 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
538 * Returns a tile somewhat representing the order destination (not suitable for pathfinding).
539 * @param v The vehicle to get the location for.
540 * @return destination of order, or INVALID_TILE if none.
542 TileIndex Order::GetLocation(const Vehicle *v) const
544 switch (this->GetType()) {
545 case OT_GOTO_WAYPOINT:
546 case OT_GOTO_STATION:
547 case OT_IMPLICIT:
548 return BaseStation::Get(this->GetDestination())->xy;
550 case OT_GOTO_DEPOT:
551 if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
552 return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
554 default:
555 return INVALID_TILE;
559 static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
561 assert(v->type == VEH_SHIP);
563 if (cur->IsType(OT_CONDITIONAL)) {
564 if (conditional_depth > v->GetNumOrders()) return 0;
566 conditional_depth++;
568 int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
569 int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
570 return max(dist1, dist2);
573 TileIndex prev_tile = prev->GetLocation(v);
574 TileIndex cur_tile = cur->GetLocation(v);
575 if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
576 return DistanceManhattan(prev_tile, cur_tile);
580 * Add an order to the orderlist of a vehicle.
581 * @param tile unused
582 * @param flags operation to perform
583 * @param p1 various bitstuffed elements
584 * - p1 = (bit 0 - 19) - ID of the vehicle
585 * - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
586 * the order will be inserted before that one
587 * the maximum vehicle order id is 254.
588 * @param p2 packed order to insert
589 * @param text unused
590 * @return the cost of this operation or an error
592 CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
594 VehicleID veh = GB(p1, 0, 20);
595 VehicleOrderID sel_ord = GB(p1, 20, 8);
596 Order new_order(p2);
598 Vehicle *v = Vehicle::GetIfValid(veh);
599 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
601 CommandCost ret = CheckOwnership(v->owner);
602 if (ret.Failed()) return ret;
604 /* Check if the inserted order is to the correct destination (owner, type),
605 * and has the correct flags if any */
606 switch (new_order.GetType()) {
607 case OT_GOTO_STATION: {
608 const Station *st = Station::GetIfValid(new_order.GetDestination());
609 if (st == NULL) return CMD_ERROR;
611 if (st->owner != OWNER_NONE) {
612 CommandCost ret = CheckOwnership(st->owner);
613 if (ret.Failed()) return ret;
616 if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
617 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
618 if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
621 /* Non stop only allowed for ground vehicles. */
622 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
624 /* Filter invalid load/unload types. */
625 switch (new_order.GetLoadType()) {
626 case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
627 default: return CMD_ERROR;
629 switch (new_order.GetUnloadType()) {
630 case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
631 default: return CMD_ERROR;
634 /* Filter invalid stop locations */
635 switch (new_order.GetStopLocation()) {
636 case OSL_PLATFORM_NEAR_END:
637 case OSL_PLATFORM_MIDDLE:
638 if (v->type != VEH_TRAIN) return CMD_ERROR;
639 /* FALL THROUGH */
640 case OSL_PLATFORM_FAR_END:
641 break;
643 default:
644 return CMD_ERROR;
647 break;
650 case OT_GOTO_DEPOT: {
651 if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
652 if (v->type == VEH_AIRCRAFT) {
653 const Station *st = Station::GetIfValid(new_order.GetDestination());
655 if (st == NULL) return CMD_ERROR;
657 CommandCost ret = CheckOwnership(st->owner);
658 if (ret.Failed()) return ret;
660 if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
661 return CMD_ERROR;
663 } else {
664 const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
666 if (dp == NULL) return CMD_ERROR;
668 CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
669 if (ret.Failed()) return ret;
671 switch (v->type) {
672 case VEH_TRAIN:
673 if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
674 break;
676 case VEH_ROAD:
677 if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
678 break;
680 case VEH_SHIP:
681 if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
682 break;
684 default: return CMD_ERROR;
689 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
690 if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
691 if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
692 if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
693 break;
696 case OT_GOTO_WAYPOINT: {
697 const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
698 if (wp == NULL) return CMD_ERROR;
700 switch (v->type) {
701 default: return CMD_ERROR;
703 case VEH_TRAIN: {
704 if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
706 CommandCost ret = CheckOwnership(wp->owner);
707 if (ret.Failed()) return ret;
708 break;
711 case VEH_SHIP:
712 if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
713 if (wp->owner != OWNER_NONE) {
714 CommandCost ret = CheckOwnership(wp->owner);
715 if (ret.Failed()) return ret;
717 break;
720 /* Order flags can be any of the following for waypoints:
721 * [non-stop]
722 * non-stop orders (if any) are only valid for trains */
723 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
724 break;
727 case OT_CONDITIONAL: {
728 VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
729 if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR; // Always allow jumping to the first (even when there is no order).
730 if (new_order.GetConditionVariable() >= OCV_END) return CMD_ERROR;
732 OrderConditionComparator occ = new_order.GetConditionComparator();
733 if (occ >= OCC_END) return CMD_ERROR;
734 switch (new_order.GetConditionVariable()) {
735 case OCV_REQUIRES_SERVICE:
736 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
737 break;
739 case OCV_UNCONDITIONALLY:
740 if (occ != OCC_EQUALS) return CMD_ERROR;
741 if (new_order.GetConditionValue() != 0) return CMD_ERROR;
742 break;
744 case OCV_LOAD_PERCENTAGE:
745 case OCV_RELIABILITY:
746 if (new_order.GetConditionValue() > 100) return CMD_ERROR;
747 /* FALL THROUGH */
748 default:
749 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
750 break;
752 break;
755 default: return CMD_ERROR;
758 if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
760 if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
761 if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
762 if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
764 if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
765 /* Make sure the new destination is not too far away from the previous */
766 const Order *prev = NULL;
767 uint n = 0;
769 /* Find the last goto station or depot order before the insert location.
770 * If the order is to be inserted at the beginning of the order list this
771 * finds the last order in the list. */
772 const Order *o;
773 FOR_VEHICLE_ORDERS(v, o) {
774 switch (o->GetType()) {
775 case OT_GOTO_STATION:
776 case OT_GOTO_DEPOT:
777 case OT_GOTO_WAYPOINT:
778 prev = o;
779 break;
781 default: break;
783 if (++n == sel_ord && prev != NULL) break;
785 if (prev != NULL) {
786 uint dist = GetOrderDistance(prev, &new_order, v);
787 if (dist >= 130) {
788 return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
793 if (flags & DC_EXEC) {
794 Order *new_o = new Order();
795 new_o->AssignOrder(new_order);
796 InsertOrder(v, new_o, sel_ord);
799 return CommandCost();
803 * Insert a new order but skip the validation.
804 * @param v The vehicle to insert the order to.
805 * @param new_o The new order.
806 * @param sel_ord The position the order should be inserted at.
808 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
810 /* Create new order and link in list */
811 if (v->orders.list == NULL) {
812 v->orders.list = new OrderList(new_o, v);
813 } else {
814 v->orders.list->InsertOrderAt(new_o, sel_ord);
817 Vehicle *u = v->FirstShared();
818 DeleteOrderWarnings(u);
819 for (; u != NULL; u = u->NextShared()) {
820 assert(v->orders.list == u->orders.list);
822 /* If there is added an order before the current one, we need
823 * to update the selected order. We do not change implicit/real order indices though.
824 * If the new order is between the current implicit order and real order, the implicit order will
825 * later skip the inserted order. */
826 if (sel_ord <= u->cur_real_order_index) {
827 uint cur = u->cur_real_order_index + 1;
828 /* Check if we don't go out of bound */
829 if (cur < u->GetNumOrders()) {
830 u->cur_real_order_index = cur;
833 if (sel_ord == u->cur_implicit_order_index && u->IsGroundVehicle()) {
834 /* We are inserting an order just before the current implicit order.
835 * We do not know whether we will reach current implicit or the newly inserted order first.
836 * So, disable creation of implicit orders until we are on track again. */
837 uint16 &gv_flags = u->GetGroundVehicleFlags();
838 SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
840 if (sel_ord <= u->cur_implicit_order_index) {
841 uint cur = u->cur_implicit_order_index + 1;
842 /* Check if we don't go out of bound */
843 if (cur < u->GetNumOrders()) {
844 u->cur_implicit_order_index = cur;
847 /* Update any possible open window of the vehicle */
848 InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
851 /* As we insert an order, the order to skip to will be 'wrong'. */
852 VehicleOrderID cur_order_id = 0;
853 Order *order;
854 FOR_VEHICLE_ORDERS(v, order) {
855 if (order->IsType(OT_CONDITIONAL)) {
856 VehicleOrderID order_id = order->GetConditionSkipToOrder();
857 if (order_id >= sel_ord) {
858 order->SetConditionSkipToOrder(order_id + 1);
860 if (order_id == cur_order_id) {
861 order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
864 cur_order_id++;
867 /* Make sure to rebuild the whole list */
868 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
872 * Declone an order-list
873 * @param *dst delete the orders of this vehicle
874 * @param flags execution flags
876 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
878 if (flags & DC_EXEC) {
879 DeleteVehicleOrders(dst);
880 InvalidateVehicleOrder(dst, -1);
881 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
883 return CommandCost();
887 * Delete an order from the orderlist of a vehicle.
888 * @param tile unused
889 * @param flags operation to perform
890 * @param p1 the ID of the vehicle
891 * @param p2 the order to delete (max 255)
892 * @param text unused
893 * @return the cost of this operation or an error
895 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
897 VehicleID veh_id = GB(p1, 0, 20);
898 VehicleOrderID sel_ord = GB(p2, 0, 8);
900 Vehicle *v = Vehicle::GetIfValid(veh_id);
902 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
904 CommandCost ret = CheckOwnership(v->owner);
905 if (ret.Failed()) return ret;
907 /* If we did not select an order, we maybe want to de-clone the orders */
908 if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
910 if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
912 if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
913 return CommandCost();
917 * Cancel the current loading order of the vehicle as the order was deleted.
918 * @param v the vehicle
920 static void CancelLoadingDueToDeletedOrder(Vehicle *v)
922 assert(v->current_order.IsType(OT_LOADING));
923 /* NON-stop flag is misused to see if a train is in a station that is
924 * on his order list or not */
925 v->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
926 /* When full loading, "cancel" that order so the vehicle doesn't
927 * stay indefinitely at this station anymore. */
928 if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) v->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
932 * Delete an order but skip the parameter validation.
933 * @param v The vehicle to delete the order from.
934 * @param sel_ord The id of the order to be deleted.
936 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
938 v->orders.list->DeleteOrderAt(sel_ord);
940 Vehicle *u = v->FirstShared();
941 DeleteOrderWarnings(u);
942 for (; u != NULL; u = u->NextShared()) {
943 assert(v->orders.list == u->orders.list);
945 if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
946 CancelLoadingDueToDeletedOrder(u);
949 if (sel_ord < u->cur_real_order_index) {
950 u->cur_real_order_index--;
951 } else if (sel_ord == u->cur_real_order_index) {
952 u->UpdateRealOrderIndex();
955 if (sel_ord < u->cur_implicit_order_index) {
956 u->cur_implicit_order_index--;
957 } else if (sel_ord == u->cur_implicit_order_index) {
958 /* Make sure the index is valid */
959 if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
961 /* Skip non-implicit orders for the implicit-order-index (e.g. if the current implicit order was deleted */
962 while (u->cur_implicit_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_implicit_order_index)->IsType(OT_IMPLICIT)) {
963 u->cur_implicit_order_index++;
964 if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
968 /* Update any possible open window of the vehicle */
969 InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
972 /* As we delete an order, the order to skip to will be 'wrong'. */
973 VehicleOrderID cur_order_id = 0;
974 Order *order = NULL;
975 FOR_VEHICLE_ORDERS(v, order) {
976 if (order->IsType(OT_CONDITIONAL)) {
977 VehicleOrderID order_id = order->GetConditionSkipToOrder();
978 if (order_id >= sel_ord) {
979 order_id = max(order_id - 1, 0);
981 if (order_id == cur_order_id) {
982 order_id = (order_id + 1) % v->GetNumOrders();
984 order->SetConditionSkipToOrder(order_id);
986 cur_order_id++;
989 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
993 * Goto order of order-list.
994 * @param tile unused
995 * @param flags operation to perform
996 * @param p1 The ID of the vehicle which order is skipped
997 * @param p2 the selected order to which we want to skip
998 * @param text unused
999 * @return the cost of this operation or an error
1001 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1003 VehicleID veh_id = GB(p1, 0, 20);
1004 VehicleOrderID sel_ord = GB(p2, 0, 8);
1006 Vehicle *v = Vehicle::GetIfValid(veh_id);
1008 if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_implicit_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
1010 CommandCost ret = CheckOwnership(v->owner);
1011 if (ret.Failed()) return ret;
1013 if (flags & DC_EXEC) {
1014 v->cur_implicit_order_index = v->cur_real_order_index = sel_ord;
1015 v->UpdateRealOrderIndex();
1017 if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
1019 InvalidateVehicleOrder(v, -2);
1022 /* We have an aircraft/ship, they have a mini-schedule, so update them all */
1023 if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
1024 if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
1026 return CommandCost();
1030 * Move an order inside the orderlist
1031 * @param tile unused
1032 * @param flags operation to perform
1033 * @param p1 the ID of the vehicle
1034 * @param p2 order to move and target
1035 * bit 0-15 : the order to move
1036 * bit 16-31 : the target order
1037 * @param text unused
1038 * @return the cost of this operation or an error
1039 * @note The target order will move one place down in the orderlist
1040 * if you move the order upwards else it'll move it one place down
1042 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1044 VehicleID veh = GB(p1, 0, 20);
1045 VehicleOrderID moving_order = GB(p2, 0, 16);
1046 VehicleOrderID target_order = GB(p2, 16, 16);
1048 Vehicle *v = Vehicle::GetIfValid(veh);
1049 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
1051 CommandCost ret = CheckOwnership(v->owner);
1052 if (ret.Failed()) return ret;
1054 /* Don't make senseless movements */
1055 if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
1056 moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
1058 Order *moving_one = v->GetOrder(moving_order);
1059 /* Don't move an empty order */
1060 if (moving_one == NULL) return CMD_ERROR;
1062 if (flags & DC_EXEC) {
1063 v->orders.list->MoveOrder(moving_order, target_order);
1065 /* Update shared list */
1066 Vehicle *u = v->FirstShared();
1068 DeleteOrderWarnings(u);
1070 for (; u != NULL; u = u->NextShared()) {
1071 /* Update the current order.
1072 * There are multiple ways to move orders, which result in cur_implicit_order_index
1073 * and cur_real_order_index to not longer make any sense. E.g. moving another
1074 * real order between them.
1076 * Basically one could choose to preserve either of them, but not both.
1077 * While both ways are suitable in this or that case from a human point of view, neither
1078 * of them makes really sense.
1079 * However, from an AI point of view, preserving cur_real_order_index is the most
1080 * predictable and transparent behaviour.
1082 * With that decision it basically does not matter what we do to cur_implicit_order_index.
1083 * If we change orders between the implict- and real-index, the implicit orders are mostly likely
1084 * completely out-dated anyway. So, keep it simple and just keep cur_implicit_order_index as well.
1085 * The worst which can happen is that a lot of implicit orders are removed when reaching current_order.
1087 if (u->cur_real_order_index == moving_order) {
1088 u->cur_real_order_index = target_order;
1089 } else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
1090 u->cur_real_order_index--;
1091 } else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
1092 u->cur_real_order_index++;
1095 if (u->cur_implicit_order_index == moving_order) {
1096 u->cur_implicit_order_index = target_order;
1097 } else if (u->cur_implicit_order_index > moving_order && u->cur_implicit_order_index <= target_order) {
1098 u->cur_implicit_order_index--;
1099 } else if (u->cur_implicit_order_index < moving_order && u->cur_implicit_order_index >= target_order) {
1100 u->cur_implicit_order_index++;
1103 assert(v->orders.list == u->orders.list);
1104 /* Update any possible open window of the vehicle */
1105 InvalidateVehicleOrder(u, moving_order | (target_order << 8));
1108 /* As we move an order, the order to skip to will be 'wrong'. */
1109 Order *order;
1110 FOR_VEHICLE_ORDERS(v, order) {
1111 if (order->IsType(OT_CONDITIONAL)) {
1112 VehicleOrderID order_id = order->GetConditionSkipToOrder();
1113 if (order_id == moving_order) {
1114 order_id = target_order;
1115 } else if (order_id > moving_order && order_id <= target_order) {
1116 order_id--;
1117 } else if (order_id < moving_order && order_id >= target_order) {
1118 order_id++;
1120 order->SetConditionSkipToOrder(order_id);
1124 /* Make sure to rebuild the whole list */
1125 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
1128 return CommandCost();
1132 * Modify an order in the orderlist of a vehicle.
1133 * @param tile unused
1134 * @param flags operation to perform
1135 * @param p1 various bitstuffed elements
1136 * - p1 = (bit 0 - 19) - ID of the vehicle
1137 * - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
1138 * the order will be inserted before that one
1139 * the maximum vehicle order id is 254.
1140 * @param p2 various bitstuffed elements
1141 * - p2 = (bit 0 - 3) - what data to modify (@see ModifyOrderFlags)
1142 * - p2 = (bit 4 - 15) - the data to modify
1143 * @param text unused
1144 * @return the cost of this operation or an error
1146 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1148 VehicleOrderID sel_ord = GB(p1, 20, 8);
1149 VehicleID veh = GB(p1, 0, 20);
1150 ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
1151 uint16 data = GB(p2, 4, 11);
1153 if (mof >= MOF_END) return CMD_ERROR;
1155 Vehicle *v = Vehicle::GetIfValid(veh);
1156 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
1158 CommandCost ret = CheckOwnership(v->owner);
1159 if (ret.Failed()) return ret;
1161 /* Is it a valid order? */
1162 if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
1164 Order *order = v->GetOrder(sel_ord);
1165 switch (order->GetType()) {
1166 case OT_GOTO_STATION:
1167 if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
1168 break;
1170 case OT_GOTO_DEPOT:
1171 if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
1172 break;
1174 case OT_GOTO_WAYPOINT:
1175 if (mof != MOF_NON_STOP) return CMD_ERROR;
1176 break;
1178 case OT_CONDITIONAL:
1179 if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
1180 break;
1182 default:
1183 return CMD_ERROR;
1186 switch (mof) {
1187 default: NOT_REACHED();
1189 case MOF_NON_STOP:
1190 if (!v->IsGroundVehicle()) return CMD_ERROR;
1191 if (data >= ONSF_END) return CMD_ERROR;
1192 if (data == order->GetNonStopType()) return CMD_ERROR;
1193 break;
1195 case MOF_STOP_LOCATION:
1196 if (v->type != VEH_TRAIN) return CMD_ERROR;
1197 if (data >= OSL_END) return CMD_ERROR;
1198 break;
1200 case MOF_UNLOAD:
1201 if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
1202 /* Unload and no-unload are mutual exclusive and so are transfer and no unload. */
1203 if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
1204 if (data == order->GetUnloadType()) return CMD_ERROR;
1205 break;
1207 case MOF_LOAD:
1208 if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
1209 if (data == order->GetLoadType()) return CMD_ERROR;
1210 break;
1212 case MOF_DEPOT_ACTION:
1213 if (data >= DA_END) return CMD_ERROR;
1214 break;
1216 case MOF_COND_VARIABLE:
1217 if (data >= OCV_END) return CMD_ERROR;
1218 break;
1220 case MOF_COND_COMPARATOR:
1221 if (data >= OCC_END) return CMD_ERROR;
1222 switch (order->GetConditionVariable()) {
1223 case OCV_UNCONDITIONALLY: return CMD_ERROR;
1225 case OCV_REQUIRES_SERVICE:
1226 if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
1227 break;
1229 default:
1230 if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
1231 break;
1233 break;
1235 case MOF_COND_VALUE:
1236 switch (order->GetConditionVariable()) {
1237 case OCV_UNCONDITIONALLY: return CMD_ERROR;
1239 case OCV_LOAD_PERCENTAGE:
1240 case OCV_RELIABILITY:
1241 if (data > 100) return CMD_ERROR;
1242 break;
1244 default:
1245 if (data > 2047) return CMD_ERROR;
1246 break;
1248 break;
1250 case MOF_COND_DESTINATION:
1251 if (data >= v->GetNumOrders()) return CMD_ERROR;
1252 break;
1255 if (flags & DC_EXEC) {
1256 switch (mof) {
1257 case MOF_NON_STOP:
1258 order->SetNonStopType((OrderNonStopFlags)data);
1259 break;
1261 case MOF_STOP_LOCATION:
1262 order->SetStopLocation((OrderStopLocation)data);
1263 break;
1265 case MOF_UNLOAD:
1266 order->SetUnloadType((OrderUnloadFlags)data);
1267 break;
1269 case MOF_LOAD:
1270 order->SetLoadType((OrderLoadFlags)data);
1271 break;
1273 case MOF_DEPOT_ACTION: {
1274 switch (data) {
1275 case DA_ALWAYS_GO:
1276 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
1277 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
1278 break;
1280 case DA_SERVICE:
1281 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
1282 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
1283 order->SetRefit(CT_NO_REFIT);
1284 break;
1286 case DA_STOP:
1287 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
1288 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
1289 order->SetRefit(CT_NO_REFIT);
1290 break;
1292 default:
1293 NOT_REACHED();
1295 break;
1298 case MOF_COND_VARIABLE: {
1299 order->SetConditionVariable((OrderConditionVariable)data);
1301 OrderConditionComparator occ = order->GetConditionComparator();
1302 switch (order->GetConditionVariable()) {
1303 case OCV_UNCONDITIONALLY:
1304 order->SetConditionComparator(OCC_EQUALS);
1305 order->SetConditionValue(0);
1306 break;
1308 case OCV_REQUIRES_SERVICE:
1309 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
1310 break;
1312 case OCV_LOAD_PERCENTAGE:
1313 case OCV_RELIABILITY:
1314 if (order->GetConditionValue() > 100) order->SetConditionValue(100);
1315 /* FALL THROUGH */
1316 default:
1317 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
1318 break;
1320 break;
1323 case MOF_COND_COMPARATOR:
1324 order->SetConditionComparator((OrderConditionComparator)data);
1325 break;
1327 case MOF_COND_VALUE:
1328 order->SetConditionValue(data);
1329 break;
1331 case MOF_COND_DESTINATION:
1332 order->SetConditionSkipToOrder(data);
1333 break;
1335 default: NOT_REACHED();
1338 /* Update the windows and full load flags, also for vehicles that share the same order list */
1339 Vehicle *u = v->FirstShared();
1340 DeleteOrderWarnings(u);
1341 for (; u != NULL; u = u->NextShared()) {
1342 /* Toggle u->current_order "Full load" flag if it changed.
1343 * However, as the same flag is used for depot orders, check
1344 * whether we are not going to a depot as there are three
1345 * cases where the full load flag can be active and only
1346 * one case where the flag is used for depot orders. In the
1347 * other cases for the OrderTypeByte the flags are not used,
1348 * so do not care and those orders should not be active
1349 * when this function is called.
1351 if (sel_ord == u->cur_real_order_index &&
1352 (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
1353 u->current_order.GetLoadType() != order->GetLoadType()) {
1354 u->current_order.SetLoadType(order->GetLoadType());
1356 InvalidateVehicleOrder(u, -2);
1360 return CommandCost();
1364 * Clone/share/copy an order-list of another vehicle.
1365 * @param tile unused
1366 * @param flags operation to perform
1367 * @param p1 various bitstuffed elements
1368 * - p1 = (bit 0-19) - destination vehicle to clone orders to
1369 * - p1 = (bit 30-31) - action to perform
1370 * @param p2 source vehicle to clone orders from, if any (none for CO_UNSHARE)
1371 * @param text unused
1372 * @return the cost of this operation or an error
1374 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1376 VehicleID veh_src = GB(p2, 0, 20);
1377 VehicleID veh_dst = GB(p1, 0, 20);
1379 Vehicle *dst = Vehicle::GetIfValid(veh_dst);
1380 if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
1382 CommandCost ret = CheckOwnership(dst->owner);
1383 if (ret.Failed()) return ret;
1385 switch (GB(p1, 30, 2)) {
1386 case CO_SHARE: {
1387 Vehicle *src = Vehicle::GetIfValid(veh_src);
1389 /* Sanity checks */
1390 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
1392 CommandCost ret = CheckOwnership(src->owner);
1393 if (ret.Failed()) return ret;
1395 /* Trucks can't share orders with busses (and visa versa) */
1396 if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
1397 return CMD_ERROR;
1400 /* Is the vehicle already in the shared list? */
1401 if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
1403 const Order *order;
1405 FOR_VEHICLE_ORDERS(src, order) {
1406 if (OrderGoesToStation(dst, order) &&
1407 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
1408 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
1412 if (src->orders.list == NULL && !OrderList::CanAllocateItem()) {
1413 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
1416 if (flags & DC_EXEC) {
1417 /* If the destination vehicle had a OrderList, destroy it.
1418 * We only reset the order indices, if the new orders are obviously different.
1419 * (We mainly do this to keep the order indices valid and in range.) */
1420 DeleteVehicleOrders(dst, false, dst->GetNumOrders() != src->GetNumOrders());
1422 dst->orders.list = src->orders.list;
1424 /* Link this vehicle in the shared-list */
1425 dst->AddToShared(src);
1427 InvalidateVehicleOrder(dst, -1);
1428 InvalidateVehicleOrder(src, -2);
1430 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
1432 break;
1435 case CO_COPY: {
1436 Vehicle *src = Vehicle::GetIfValid(veh_src);
1438 /* Sanity checks */
1439 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
1441 CommandCost ret = CheckOwnership(src->owner);
1442 if (ret.Failed()) return ret;
1444 /* Trucks can't copy all the orders from busses (and visa versa),
1445 * and neither can helicopters and aircarft. */
1446 const Order *order;
1447 FOR_VEHICLE_ORDERS(src, order) {
1448 if (OrderGoesToStation(dst, order) &&
1449 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
1450 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
1454 /* make sure there are orders available */
1455 int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
1456 if (!Order::CanAllocateItem(delta) ||
1457 ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
1458 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
1461 if (flags & DC_EXEC) {
1462 const Order *order;
1463 Order *first = NULL;
1464 Order **order_dst;
1466 /* If the destination vehicle had an order list, destroy the chain but keep the OrderList.
1467 * We only reset the order indices, if the new orders are obviously different.
1468 * (We mainly do this to keep the order indices valid and in range.) */
1469 DeleteVehicleOrders(dst, true, dst->GetNumOrders() != src->GetNumOrders());
1471 order_dst = &first;
1472 FOR_VEHICLE_ORDERS(src, order) {
1473 *order_dst = new Order();
1474 (*order_dst)->AssignOrder(*order);
1475 order_dst = &(*order_dst)->next;
1477 if (dst->orders.list == NULL) {
1478 dst->orders.list = new OrderList(first, dst);
1479 } else {
1480 assert(dst->orders.list->GetFirstOrder() == NULL);
1481 assert(!dst->orders.list->IsShared());
1482 delete dst->orders.list;
1483 assert(OrderList::CanAllocateItem());
1484 dst->orders.list = new OrderList(first, dst);
1487 InvalidateVehicleOrder(dst, -1);
1489 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
1491 break;
1494 case CO_UNSHARE: return DecloneOrder(dst, flags);
1495 default: return CMD_ERROR;
1498 return CommandCost();
1502 * Add/remove refit orders from an order
1503 * @param tile Not used
1504 * @param flags operation to perform
1505 * @param p1 VehicleIndex of the vehicle having the order
1506 * @param p2 bitmask
1507 * - bit 0-7 CargoID
1508 * - bit 8-15 Cargo subtype
1509 * - bit 16-23 number of order to modify
1510 * @param text unused
1511 * @return the cost of this operation or an error
1513 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1515 VehicleID veh = GB(p1, 0, 20);
1516 VehicleOrderID order_number = GB(p2, 16, 8);
1517 CargoID cargo = GB(p2, 0, 8);
1518 byte subtype = GB(p2, 8, 8);
1520 if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT) return CMD_ERROR;
1522 const Vehicle *v = Vehicle::GetIfValid(veh);
1523 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
1525 CommandCost ret = CheckOwnership(v->owner);
1526 if (ret.Failed()) return ret;
1528 Order *order = v->GetOrder(order_number);
1529 if (order == NULL) return CMD_ERROR;
1531 if (flags & DC_EXEC) {
1532 order->SetRefit(cargo, subtype);
1534 /* Make the depot order an 'always go' order. */
1535 if (cargo != CT_NO_REFIT) {
1536 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
1537 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
1540 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
1541 /* Update any possible open window of the vehicle */
1542 InvalidateVehicleOrder(u, -2);
1544 /* If the vehicle already got the current depot set as current order, then update current order as well */
1545 if (u->cur_real_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
1546 u->current_order.SetRefit(cargo, subtype);
1551 return CommandCost();
1557 * Check the orders of a vehicle, to see if there are invalid orders and stuff
1560 void CheckOrders(const Vehicle *v)
1562 /* Does the user wants us to check things? */
1563 if (_settings_client.gui.order_review_system == 0) return;
1565 /* Do nothing for crashed vehicles */
1566 if (v->vehstatus & VS_CRASHED) return;
1568 /* Do nothing for stopped vehicles if setting is '1' */
1569 if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
1571 /* do nothing we we're not the first vehicle in a share-chain */
1572 if (v->FirstShared() != v) return;
1574 /* Only check every 20 days, so that we don't flood the message log */
1575 if (v->owner == _local_company && v->day_counter % 20 == 0) {
1576 int n_st, problem_type = -1;
1577 const Order *order;
1578 int message = 0;
1580 /* Check the order list */
1581 n_st = 0;
1583 FOR_VEHICLE_ORDERS(v, order) {
1584 /* Dummy order? */
1585 if (order->IsType(OT_DUMMY)) {
1586 problem_type = 1;
1587 break;
1589 /* Does station have a load-bay for this vehicle? */
1590 if (order->IsType(OT_GOTO_STATION)) {
1591 const Station *st = Station::Get(order->GetDestination());
1593 n_st++;
1594 if (!CanVehicleUseStation(v, st)) problem_type = 3;
1598 /* Check if the last and the first order are the same */
1599 if (v->GetNumOrders() > 1) {
1600 const Order *last = v->GetLastOrder();
1602 if (v->orders.list->GetFirstOrder()->Equals(*last)) {
1603 problem_type = 2;
1607 /* Do we only have 1 station in our order list? */
1608 if (n_st < 2 && problem_type == -1) problem_type = 0;
1610 #ifndef NDEBUG
1611 if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
1612 #endif
1614 /* We don't have a problem */
1615 if (problem_type < 0) return;
1617 message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
1618 //DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
1620 SetDParam(0, v->index);
1621 AddVehicleNewsItem(
1622 message,
1623 NS_ADVICE,
1624 v->index
1630 * Removes an order from all vehicles. Triggers when, say, a station is removed.
1631 * @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
1632 * @param destination The destination. Can be a StationID, DepotID or WaypointID.
1634 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
1636 Vehicle *v;
1638 /* Aircraft have StationIDs for depot orders and never use DepotIDs
1639 * This fact is handled specially below
1642 /* Go through all vehicles */
1643 FOR_ALL_VEHICLES(v) {
1644 Order *order;
1646 order = &v->current_order;
1647 if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
1648 v->current_order.GetDestination() == destination) {
1649 order->MakeDummy();
1650 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
1653 /* Clear the order from the order-list */
1654 int id = -1;
1655 FOR_VEHICLE_ORDERS(v, order) {
1656 id++;
1657 restart:
1659 OrderType ot = order->GetType();
1660 if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
1661 if (ot == OT_IMPLICIT || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
1662 if (ot == type && order->GetDestination() == destination) {
1663 /* We want to clear implicit orders, but we don't want to make them
1664 * dummy orders. They should just vanish. Also check the actual order
1665 * type as ot is currently OT_GOTO_STATION. */
1666 if (order->IsType(OT_IMPLICIT)) {
1667 order = order->next; // DeleteOrder() invalidates current order
1668 DeleteOrder(v, id);
1669 if (order != NULL) goto restart;
1670 break;
1673 order->MakeDummy();
1674 for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
1675 /* In GUI, simulate by removing the order and adding it back */
1676 InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
1677 InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
1685 * Checks if a vehicle has a depot in its order list.
1686 * @return True iff at least one order is a depot order.
1688 bool Vehicle::HasDepotOrder() const
1690 const Order *order;
1692 FOR_VEHICLE_ORDERS(this, order) {
1693 if (order->IsType(OT_GOTO_DEPOT)) return true;
1696 return false;
1700 * Delete all orders from a vehicle
1701 * @param v Vehicle whose orders to reset
1702 * @param keep_orderlist If true, do not free the order list, only empty it.
1703 * @param reset_order_indices If true, reset cur_implicit_order_index and cur_real_order_index
1704 * and cancel the current full load order (if the vehicle is loading).
1705 * If false, _you_ have to make sure the order indices are valid after
1706 * your messing with them!
1708 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
1710 DeleteOrderWarnings(v);
1712 if (v->IsOrderListShared()) {
1713 /* Remove ourself from the shared order list. */
1714 v->RemoveFromShared();
1715 v->orders.list = NULL;
1716 } else if (v->orders.list != NULL) {
1717 /* Remove the orders */
1718 v->orders.list->FreeChain(keep_orderlist);
1719 if (!keep_orderlist) v->orders.list = NULL;
1722 if (reset_order_indices) {
1723 v->cur_implicit_order_index = v->cur_real_order_index = 0;
1724 if (v->current_order.IsType(OT_LOADING)) {
1725 CancelLoadingDueToDeletedOrder(v);
1731 * Clamp the service interval to the correct min/max. The actual min/max values
1732 * depend on whether it's in percent or days.
1733 * @param interval proposed service interval
1734 * @param company_id the owner of the vehicle
1735 * @return Clamped service interval
1737 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
1739 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);
1744 * Check if a vehicle has any valid orders
1746 * @return false if there are no valid orders
1747 * @note Conditional orders are not considered valid destination orders
1750 static bool CheckForValidOrders(const Vehicle *v)
1752 const Order *order;
1754 FOR_VEHICLE_ORDERS(v, order) {
1755 switch (order->GetType()) {
1756 case OT_GOTO_STATION:
1757 case OT_GOTO_DEPOT:
1758 case OT_GOTO_WAYPOINT:
1759 return true;
1761 default:
1762 break;
1766 return false;
1770 * Compare the variable and value based on the given comparator.
1772 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
1774 switch (occ) {
1775 case OCC_EQUALS: return variable == value;
1776 case OCC_NOT_EQUALS: return variable != value;
1777 case OCC_LESS_THAN: return variable < value;
1778 case OCC_LESS_EQUALS: return variable <= value;
1779 case OCC_MORE_THAN: return variable > value;
1780 case OCC_MORE_EQUALS: return variable >= value;
1781 case OCC_IS_TRUE: return variable != 0;
1782 case OCC_IS_FALSE: return variable == 0;
1783 default: NOT_REACHED();
1788 * Process a conditional order and determine the next order.
1789 * @param order the order the vehicle currently has
1790 * @param v the vehicle to update
1791 * @return index of next order to jump to, or INVALID_VEH_ORDER_ID to use the next order
1793 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
1795 if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
1797 bool skip_order = false;
1798 OrderConditionComparator occ = order->GetConditionComparator();
1799 uint16 value = order->GetConditionValue();
1801 switch (order->GetConditionVariable()) {
1802 case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
1803 case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
1804 case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
1805 case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break;
1806 case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break;
1807 case OCV_UNCONDITIONALLY: skip_order = true; break;
1808 case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, max(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1, 0) / DAYS_IN_LEAP_YEAR, value); break;
1809 default: NOT_REACHED();
1812 return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
1816 * Update the vehicle's destination tile from an order.
1817 * @param order the order the vehicle currently has
1818 * @param v the vehicle to update
1819 * @param conditional_depth the depth (amount of steps) to go with conditional orders. This to prevent infinite loops.
1820 * @param pbs_look_ahead Whether we are forecasting orders for pbs reservations in advance. If true, the order indices must not be modified.
1822 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool pbs_look_ahead)
1824 if (conditional_depth > v->GetNumOrders()) return false;
1826 switch (order->GetType()) {
1827 case OT_GOTO_STATION:
1828 v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
1829 return true;
1831 case OT_GOTO_DEPOT:
1832 if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
1833 assert(!pbs_look_ahead);
1834 UpdateVehicleTimetable(v, true);
1835 v->IncrementRealOrderIndex();
1836 break;
1839 if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
1840 /* We need to search for the nearest depot (hangar). */
1841 TileIndex location;
1842 DestinationID destination;
1843 bool reverse;
1845 if (v->FindClosestDepot(&location, &destination, &reverse)) {
1846 /* PBS reservations cannot reverse */
1847 if (pbs_look_ahead && reverse) return false;
1849 v->dest_tile = location;
1850 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());
1852 /* If there is no depot in front, reverse automatically (trains only) */
1853 if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
1855 if (v->type == VEH_AIRCRAFT) {
1856 Aircraft *a = Aircraft::From(v);
1857 if (a->state == FLYING && a->targetairport != destination) {
1858 /* The aircraft is now heading for a different hangar than the next in the orders */
1859 extern void AircraftNextAirportPos_and_Order(Aircraft *a);
1860 AircraftNextAirportPos_and_Order(a);
1863 return true;
1866 /* If there is no depot, we cannot help PBS either. */
1867 if (pbs_look_ahead) return false;
1869 UpdateVehicleTimetable(v, true);
1870 v->IncrementRealOrderIndex();
1871 } else {
1872 if (v->type != VEH_AIRCRAFT) {
1873 v->dest_tile = Depot::Get(order->GetDestination())->xy;
1875 return true;
1877 break;
1879 case OT_GOTO_WAYPOINT:
1880 v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
1881 return true;
1883 case OT_CONDITIONAL: {
1884 assert(!pbs_look_ahead);
1885 VehicleOrderID next_order = ProcessConditionalOrder(order, v);
1886 if (next_order != INVALID_VEH_ORDER_ID) {
1887 /* Jump to next_order. cur_implicit_order_index becomes exactly that order,
1888 * cur_real_order_index might come after next_order. */
1889 UpdateVehicleTimetable(v, false);
1890 v->cur_implicit_order_index = v->cur_real_order_index = next_order;
1891 v->UpdateRealOrderIndex();
1892 v->current_order_time += v->GetOrder(v->cur_real_order_index)->travel_time;
1894 /* Disable creation of implicit orders.
1895 * When inserting them we do not know that we would have to make the conditional orders point to them. */
1896 if (v->IsGroundVehicle()) {
1897 uint16 &gv_flags = v->GetGroundVehicleFlags();
1898 SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
1900 } else {
1901 UpdateVehicleTimetable(v, true);
1902 v->IncrementRealOrderIndex();
1904 break;
1907 default:
1908 v->dest_tile = 0;
1909 return false;
1912 assert(v->cur_implicit_order_index < v->GetNumOrders());
1913 assert(v->cur_real_order_index < v->GetNumOrders());
1915 /* Get the current order */
1916 order = v->GetOrder(v->cur_real_order_index);
1917 if (order != NULL && order->IsType(OT_IMPLICIT)) {
1918 assert(v->GetNumManualOrders() == 0);
1919 order = NULL;
1922 if (order == NULL) {
1923 v->current_order.Free();
1924 v->dest_tile = 0;
1925 return false;
1928 v->current_order = *order;
1929 return UpdateOrderDest(v, order, conditional_depth + 1, pbs_look_ahead);
1933 * Handle the orders of a vehicle and determine the next place
1934 * to go to if needed.
1935 * @param v the vehicle to do this for.
1936 * @return true *if* the vehicle is eligible for reversing
1937 * (basically only when leaving a station).
1939 bool ProcessOrders(Vehicle *v)
1941 switch (v->current_order.GetType()) {
1942 case OT_GOTO_DEPOT:
1943 /* Let a depot order in the orderlist interrupt. */
1944 if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
1945 break;
1947 case OT_LOADING:
1948 return false;
1950 case OT_LEAVESTATION:
1951 if (v->type != VEH_AIRCRAFT) return false;
1952 break;
1954 default: break;
1958 * Reversing because of order change is allowed only just after leaving a
1959 * station (and the difficulty setting to allowed, of course)
1960 * this can be detected because only after OT_LEAVESTATION, current_order
1961 * will be reset to nothing. (That also happens if no order, but in that case
1962 * it won't hit the point in code where may_reverse is checked)
1964 bool may_reverse = v->current_order.IsType(OT_NOTHING);
1966 /* Check if we've reached a 'via' destination. */
1967 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)) &&
1968 IsTileType(v->tile, MP_STATION) &&
1969 v->current_order.GetDestination() == GetStationIndex(v->tile)) {
1970 v->DeleteUnreachedImplicitOrders();
1971 /* We set the last visited station here because we do not want
1972 * the train to stop at this 'via' station if the next order
1973 * is a no-non-stop order; in that case not setting the last
1974 * visited station will cause the vehicle to still stop. */
1975 v->last_station_visited = v->current_order.GetDestination();
1976 UpdateVehicleTimetable(v, true);
1977 v->IncrementImplicitOrderIndex();
1980 /* Get the current order */
1981 assert(v->cur_implicit_order_index == 0 || v->cur_implicit_order_index < v->GetNumOrders());
1982 v->UpdateRealOrderIndex();
1984 const Order *order = v->GetOrder(v->cur_real_order_index);
1985 if (order != NULL && order->IsType(OT_IMPLICIT)) {
1986 assert(v->GetNumManualOrders() == 0);
1987 order = NULL;
1990 /* If no order, do nothing. */
1991 if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
1992 if (v->type == VEH_AIRCRAFT) {
1993 /* Aircraft do something vastly different here, so handle separately */
1994 extern void HandleMissingAircraftOrders(Aircraft *v);
1995 HandleMissingAircraftOrders(Aircraft::From(v));
1996 return false;
1999 v->current_order.Free();
2000 v->dest_tile = 0;
2001 return false;
2004 /* If it is unchanged, keep it. */
2005 if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
2006 (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
2007 return false;
2010 /* Otherwise set it, and determine the destination tile. */
2011 v->current_order = *order;
2013 InvalidateVehicleOrder(v, -2);
2014 switch (v->type) {
2015 default:
2016 NOT_REACHED();
2018 case VEH_ROAD:
2019 case VEH_TRAIN:
2020 break;
2022 case VEH_AIRCRAFT:
2023 case VEH_SHIP:
2024 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
2025 break;
2028 return UpdateOrderDest(v, order) && may_reverse;
2032 * Check whether the given vehicle should stop at the given station
2033 * based on this order and the non-stop settings.
2034 * @param v the vehicle that might be stopping.
2035 * @param station the station to stop at.
2036 * @return true if the vehicle should stop.
2038 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
2040 bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
2042 return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
2043 v->last_station_visited != station && // Do stop only when we've not just been there
2044 /* Finally do stop when there is no non-stop flag set for this type of station. */
2045 !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));