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/>.
10 /** @file order_cmd.cpp Handling of orders. */
14 #include "cmd_helper.h"
15 #include "command_func.h"
16 #include "company_func.h"
17 #include "news_func.h"
18 #include "strings_func.h"
19 #include "timetable.h"
20 #include "vehicle_func.h"
21 #include "depot_base.h"
22 #include "core/pool_func.hpp"
23 #include "core/random_func.hpp"
26 #include "station_base.h"
27 #include "waypoint_base.h"
28 #include "company_base.h"
29 #include "order_backup.h"
31 #include "table/strings.h"
33 /* DestinationID must be at least as large as every these below, because it can
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 /** Clean everything up. */
47 if (CleaningPool()) return;
49 /* We can visit oil rigs and buoys that are not our own. They will be shown in
50 * the list of stations. So, we need to invalidate that window if needed. */
51 if (this->IsType(OT_GOTO_STATION
) || this->IsType(OT_GOTO_WAYPOINT
)) {
52 BaseStation
*bs
= BaseStation::GetIfValid(this->GetDestination());
53 if (bs
!= NULL
&& bs
->owner
== OWNER_NONE
) InvalidateWindowClassesData(WC_STATION_LIST
, 0);
59 * @note ONLY use on "current_order" vehicle orders!
63 this->type
= OT_NOTHING
;
70 * Makes this order a Go To Station order.
71 * @param destination the station to go to.
73 void Order::MakeGoToStation(StationID destination
)
75 this->type
= OT_GOTO_STATION
;
77 this->dest
= destination
;
81 * Makes this order a Go To Depot order.
82 * @param destination the depot to go to.
83 * @param order is this order a 'default' order, or an overridden vehicle order?
84 * @param non_stop_type how to get to the depot?
85 * @param action what to do in the depot?
86 * @param cargo the cargo type to change to.
88 void Order::MakeGoToDepot(DepotID destination
, OrderDepotTypeFlags order
, OrderNonStopFlags non_stop_type
, OrderDepotActionFlags action
, CargoID cargo
)
90 this->type
= OT_GOTO_DEPOT
;
91 this->SetDepotOrderType(order
);
92 this->SetDepotActionType(action
);
93 this->SetNonStopType(non_stop_type
);
94 this->dest
= destination
;
95 this->SetRefit(cargo
);
99 * Makes this order a Go To Waypoint order.
100 * @param destination the waypoint to go to.
102 void Order::MakeGoToWaypoint(StationID destination
)
104 this->type
= OT_GOTO_WAYPOINT
;
106 this->dest
= destination
;
110 * Makes this order a Loading order.
111 * @param ordered is this an ordered stop?
113 void Order::MakeLoading(bool ordered
)
115 this->type
= OT_LOADING
;
116 if (!ordered
) this->flags
= 0;
120 * Makes this order a Leave Station order.
122 void Order::MakeLeaveStation()
124 this->type
= OT_LEAVESTATION
;
129 * Makes this order a Dummy order.
131 void Order::MakeDummy()
133 this->type
= OT_DUMMY
;
138 * Makes this order an conditional order.
139 * @param order the order to jump to.
141 void Order::MakeConditional(VehicleOrderID order
)
143 this->type
= OT_CONDITIONAL
;
149 * Makes this order an implicit order.
150 * @param destination the station to go to.
152 void Order::MakeImplicit(StationID destination
)
154 this->type
= OT_IMPLICIT
;
155 this->dest
= destination
;
159 * Make this depot/station order also a refit order.
160 * @param cargo the cargo type to change to.
161 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
163 void Order::SetRefit(CargoID cargo
)
165 this->refit_cargo
= cargo
;
169 * Does this order have the same type, flags and destination?
170 * @param other the second order to compare to.
171 * @return true if the type, flags and destination match.
173 bool Order::Equals(const Order
&other
) const
175 /* In case of go to nearest depot orders we need "only" compare the flags
176 * with the other and not the nearest depot order bit or the actual
177 * destination because those get clear/filled in during the order
178 * evaluation. If we do not do this the order will continuously be seen as
179 * a different order and it will try to find a "nearest depot" every tick. */
180 if ((this->IsType(OT_GOTO_DEPOT
) && this->type
== other
.type
) &&
181 ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0 ||
182 (other
.GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0)) {
183 return this->GetDepotOrderType() == other
.GetDepotOrderType() &&
184 (this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT
) == (other
.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT
);
187 return this->type
== other
.type
&& this->flags
== other
.flags
&& this->dest
== other
.dest
;
191 * Pack this order into a 32 bits integer, or actually only
192 * the type, flags and destination.
193 * @return the packed representation.
194 * @note unpacking is done in the constructor.
196 uint32
Order::Pack() const
198 return this->dest
<< 16 | this->flags
<< 8 | this->type
;
202 * Pack this order into a 16 bits integer as close to the TTD
203 * representation as possible.
204 * @return the TTD-like packed representation.
206 uint16
Order::MapOldOrder() const
208 uint16 order
= this->GetType();
209 switch (this->type
) {
210 case OT_GOTO_STATION
:
211 if (this->GetUnloadType() & OUFB_UNLOAD
) SetBit(order
, 5);
212 if (this->GetLoadType() & OLFB_FULL_LOAD
) SetBit(order
, 6);
213 if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
) SetBit(order
, 7);
214 order
|= GB(this->GetDestination(), 0, 8) << 8;
217 if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS
)) SetBit(order
, 6);
219 order
|= GB(this->GetDestination(), 0, 8) << 8;
222 if (this->GetLoadType() & OLFB_FULL_LOAD
) SetBit(order
, 6);
229 * Create an order based on a packed representation of that order.
230 * @param packed the packed representation.
232 Order::Order(uint32 packed
)
234 this->type
= (OrderType
)GB(packed
, 0, 8);
235 this->flags
= GB(packed
, 8, 8);
236 this->dest
= GB(packed
, 16, 16);
238 this->refit_cargo
= CT_NO_REFIT
;
240 this->travel_time
= 0;
241 this->max_speed
= UINT16_MAX
;
246 * Updates the widgets of a vehicle which contains the order-data
249 void InvalidateVehicleOrder(const Vehicle
*v
, int data
)
251 SetWindowDirty(WC_VEHICLE_VIEW
, v
->index
);
254 /* Calls SetDirty() too */
255 InvalidateWindowData(WC_VEHICLE_ORDERS
, v
->index
, data
);
256 InvalidateWindowData(WC_VEHICLE_TIMETABLE
, v
->index
, data
);
260 SetWindowDirty(WC_VEHICLE_ORDERS
, v
->index
);
261 SetWindowDirty(WC_VEHICLE_TIMETABLE
, v
->index
);
266 * Assign data to an order (from another order)
267 * This function makes sure that the index is maintained correctly
268 * @param other the data to copy (except next pointer).
271 void Order::AssignOrder(const Order
&other
)
273 this->type
= other
.type
;
274 this->flags
= other
.flags
;
275 this->dest
= other
.dest
;
277 this->refit_cargo
= other
.refit_cargo
;
279 this->wait_time
= other
.wait_time
;
280 this->travel_time
= other
.travel_time
;
281 this->max_speed
= other
.max_speed
;
285 * Recomputes everything.
286 * @param chain first order in the chain
287 * @param v one of vehicle that is using this orderlist
289 void OrderList::Initialize(Order
*chain
, Vehicle
*v
)
292 this->first_shared
= v
;
294 this->num_orders
= 0;
295 this->num_manual_orders
= 0;
296 this->num_vehicles
= 1;
297 this->timetable_duration
= 0;
299 for (Order
*o
= this->first
; o
!= NULL
; o
= o
->next
) {
301 if (!o
->IsType(OT_IMPLICIT
)) ++this->num_manual_orders
;
302 this->timetable_duration
+= o
->wait_time
+ o
->travel_time
;
305 for (Vehicle
*u
= this->first_shared
->PreviousShared(); u
!= NULL
; u
= u
->PreviousShared()) {
306 ++this->num_vehicles
;
307 this->first_shared
= u
;
310 for (const Vehicle
*u
= v
->NextShared(); u
!= NULL
; u
= u
->NextShared()) ++this->num_vehicles
;
314 * Free a complete order chain.
315 * @param keep_orderlist If this is true only delete the orders, otherwise also delete the OrderList.
316 * @note do not use on "current_order" vehicle orders!
318 void OrderList::FreeChain(bool keep_orderlist
)
321 for (Order
*o
= this->first
; o
!= NULL
; o
= next
) {
326 if (keep_orderlist
) {
328 this->num_orders
= 0;
329 this->num_manual_orders
= 0;
330 this->timetable_duration
= 0;
337 * Get a certain order of the order chain.
338 * @param index zero-based index of the order within the chain.
339 * @return the order at position index.
341 Order
*OrderList::GetOrderAt(int index
) const
343 if (index
< 0) return NULL
;
345 Order
*order
= this->first
;
347 while (order
!= NULL
&& index
-- > 0) {
354 * Get the next order which will make the given vehicle stop at a station
355 * or refit at a depot or evaluate a non-trivial condition.
356 * @param next The order to start looking at.
357 * @param hops The number of orders we have already looked at.
359 * \li a station order
360 * \li a refitting depot order
361 * \li a non-trivial conditional order
362 * \li NULL if the vehicle won't stop anymore.
364 const Order
*OrderList::GetNextDecisionNode(const Order
*next
, uint hops
) const
366 if (hops
> this->GetNumOrders() || next
== NULL
) return NULL
;
368 if (next
->IsType(OT_CONDITIONAL
)) {
369 if (next
->GetConditionVariable() != OCV_UNCONDITIONALLY
) return next
;
371 /* We can evaluate trivial conditions right away. They're conceptually
372 * the same as regular order progression. */
373 return this->GetNextDecisionNode(
374 this->GetOrderAt(next
->GetConditionSkipToOrder()),
378 if (next
->IsType(OT_GOTO_DEPOT
)) {
379 if (next
->GetDepotActionType() == ODATFB_HALT
) return NULL
;
380 if (next
->IsRefit()) return next
;
383 if (!next
->CanLoadOrUnload()) {
384 return this->GetNextDecisionNode(this->GetNext(next
), hops
+ 1);
391 * Recursively determine the next deterministic station to stop at.
392 * @param v The vehicle we're looking at.
393 * @param first Order to start searching at or NULL to start at cur_implicit_order_index + 1.
394 * @param hops Number of orders we have already looked at.
395 * @return Next stoppping station or INVALID_STATION.
396 * @pre The vehicle is currently loading and v->last_station_visited is meaningful.
397 * @note This function may draw a random number. Don't use it from the GUI.
399 StationIDStack
OrderList::GetNextStoppingStation(const Vehicle
*v
, const Order
*first
, uint hops
) const
402 const Order
*next
= first
;
404 next
= this->GetOrderAt(v
->cur_implicit_order_index
);
406 next
= this->GetFirstOrder();
407 if (next
== NULL
) return INVALID_STATION
;
409 /* GetNext never returns NULL if there is a valid station in the list.
410 * As the given "next" is already valid and a station in the list, we
411 * don't have to check for NULL here. */
412 next
= this->GetNext(next
);
413 assert(next
!= NULL
);
418 next
= this->GetNextDecisionNode(next
, ++hops
);
420 /* Resolve possibly nested conditionals by estimation. */
421 while (next
!= NULL
&& next
->IsType(OT_CONDITIONAL
)) {
422 /* We return both options of conditional orders. */
423 const Order
*skip_to
= this->GetNextDecisionNode(
424 this->GetOrderAt(next
->GetConditionSkipToOrder()), hops
);
425 const Order
*advance
= this->GetNextDecisionNode(
426 this->GetNext(next
), hops
);
427 if (advance
== NULL
|| advance
== first
|| skip_to
== advance
) {
428 next
= (skip_to
== first
) ? NULL
: skip_to
;
429 } else if (skip_to
== NULL
|| skip_to
== first
) {
430 next
= (advance
== first
) ? NULL
: advance
;
432 StationIDStack st1
= this->GetNextStoppingStation(v
, skip_to
, hops
);
433 StationIDStack st2
= this->GetNextStoppingStation(v
, advance
, hops
);
434 while (!st2
.IsEmpty()) st1
.Push(st2
.Pop());
440 /* Don't return a next stop if the vehicle has to unload everything. */
441 if (next
== NULL
|| ((next
->IsType(OT_GOTO_STATION
) || next
->IsType(OT_IMPLICIT
)) &&
442 next
->GetDestination() == v
->last_station_visited
&&
443 (next
->GetUnloadType() & (OUFB_TRANSFER
| OUFB_UNLOAD
)) != 0)) {
444 return INVALID_STATION
;
446 } while (next
->IsType(OT_GOTO_DEPOT
) || next
->GetDestination() == v
->last_station_visited
);
448 return next
->GetDestination();
452 * Insert a new order into the order chain.
453 * @param new_order is the order to insert into the chain.
454 * @param index is the position where the order is supposed to be inserted.
456 void OrderList::InsertOrderAt(Order
*new_order
, int index
)
458 if (this->first
== NULL
) {
459 this->first
= new_order
;
462 /* Insert as first or only order */
463 new_order
->next
= this->first
;
464 this->first
= new_order
;
465 } else if (index
>= this->num_orders
) {
466 /* index is after the last order, add it to the end */
467 this->GetLastOrder()->next
= new_order
;
469 /* Put the new order in between */
470 Order
*order
= this->GetOrderAt(index
- 1);
471 new_order
->next
= order
->next
;
472 order
->next
= new_order
;
476 if (!new_order
->IsType(OT_IMPLICIT
)) ++this->num_manual_orders
;
477 this->timetable_duration
+= new_order
->wait_time
+ new_order
->travel_time
;
479 /* We can visit oil rigs and buoys that are not our own. They will be shown in
480 * the list of stations. So, we need to invalidate that window if needed. */
481 if (new_order
->IsType(OT_GOTO_STATION
) || new_order
->IsType(OT_GOTO_WAYPOINT
)) {
482 BaseStation
*bs
= BaseStation::Get(new_order
->GetDestination());
483 if (bs
->owner
== OWNER_NONE
) InvalidateWindowClassesData(WC_STATION_LIST
, 0);
490 * Remove an order from the order list and delete it.
491 * @param index is the position of the order which is to be deleted.
493 void OrderList::DeleteOrderAt(int index
)
495 if (index
>= this->num_orders
) return;
500 to_remove
= this->first
;
501 this->first
= to_remove
->next
;
503 Order
*prev
= GetOrderAt(index
- 1);
504 to_remove
= prev
->next
;
505 prev
->next
= to_remove
->next
;
508 if (!to_remove
->IsType(OT_IMPLICIT
)) --this->num_manual_orders
;
509 this->timetable_duration
-= (to_remove
->wait_time
+ to_remove
->travel_time
);
514 * Move an order to another position within the order list.
515 * @param from is the zero-based position of the order to move.
516 * @param to is the zero-based position where the order is moved to.
518 void OrderList::MoveOrder(int from
, int to
)
520 if (from
>= this->num_orders
|| to
>= this->num_orders
|| from
== to
) return;
524 /* Take the moving order out of the pointer-chain */
526 moving_one
= this->first
;
527 this->first
= moving_one
->next
;
529 Order
*one_before
= GetOrderAt(from
- 1);
530 moving_one
= one_before
->next
;
531 one_before
->next
= moving_one
->next
;
534 /* Insert the moving_order again in the pointer-chain */
536 moving_one
->next
= this->first
;
537 this->first
= moving_one
;
539 Order
*one_before
= GetOrderAt(to
- 1);
540 moving_one
->next
= one_before
->next
;
541 one_before
->next
= moving_one
;
546 * Removes the vehicle from the shared order list.
547 * @note This is supposed to be called when the vehicle is still in the chain
548 * @param v vehicle to remove from the list
550 void OrderList::RemoveVehicle(Vehicle
*v
)
552 --this->num_vehicles
;
553 if (v
== this->first_shared
) this->first_shared
= v
->NextShared();
557 * Checks whether a vehicle is part of the shared vehicle chain.
558 * @param v is the vehicle to search in the shared vehicle chain.
560 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle
*v
) const
562 for (const Vehicle
*v_shared
= this->first_shared
; v_shared
!= NULL
; v_shared
= v_shared
->NextShared()) {
563 if (v_shared
== v
) return true;
570 * Gets the position of the given vehicle within the shared order vehicle list.
571 * @param v is the vehicle of which to get the position
572 * @return position of v within the shared vehicle chain.
574 int OrderList::GetPositionInSharedOrderList(const Vehicle
*v
) const
577 for (const Vehicle
*v_shared
= v
->PreviousShared(); v_shared
!= NULL
; v_shared
= v_shared
->PreviousShared()) count
++;
582 * Checks whether all orders of the list have a filled timetable.
583 * @return whether all orders have a filled timetable.
585 bool OrderList::IsCompleteTimetable() const
587 for (Order
*o
= this->first
; o
!= NULL
; o
= o
->next
) {
588 /* Implicit orders are, by definition, not timetabled. */
589 if (o
->IsType(OT_IMPLICIT
)) continue;
590 if (!o
->IsCompletelyTimetabled()) return false;
596 * Checks for internal consistency of order list. Triggers assertion if something is wrong.
598 void OrderList::DebugCheckSanity() const
600 VehicleOrderID check_num_orders
= 0;
601 VehicleOrderID check_num_manual_orders
= 0;
602 uint check_num_vehicles
= 0;
603 Ticks check_timetable_duration
= 0;
605 DEBUG(misc
, 6, "Checking OrderList %hu for sanity...", this->index
);
607 for (const Order
*o
= this->first
; o
!= NULL
; o
= o
->next
) {
609 if (!o
->IsType(OT_IMPLICIT
)) ++check_num_manual_orders
;
610 check_timetable_duration
+= o
->wait_time
+ o
->travel_time
;
612 assert(this->num_orders
== check_num_orders
);
613 assert(this->num_manual_orders
== check_num_manual_orders
);
614 assert(this->timetable_duration
== check_timetable_duration
);
616 for (const Vehicle
*v
= this->first_shared
; v
!= NULL
; v
= v
->NextShared()) {
617 ++check_num_vehicles
;
618 assert(v
->orders
.list
== this);
620 assert(this->num_vehicles
== check_num_vehicles
);
621 DEBUG(misc
, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
622 (uint
)this->num_orders
, (uint
)this->num_manual_orders
,
623 this->num_vehicles
, this->timetable_duration
);
627 * Checks whether the order goes to a station or not, i.e. whether the
628 * destination is a station
629 * @param v the vehicle to check for
630 * @param o the order to check
631 * @return true if the destination is a station
633 static inline bool OrderGoesToStation(const Vehicle
*v
, const Order
*o
)
635 return o
->IsType(OT_GOTO_STATION
) ||
636 (v
->type
== VEH_AIRCRAFT
&& o
->IsType(OT_GOTO_DEPOT
) && !(o
->GetDepotActionType() & ODATFB_NEAREST_DEPOT
));
640 * Delete all news items regarding defective orders about a vehicle
641 * This could kill still valid warnings (for example about void order when just
642 * another order gets added), but assume the company will notice the problems,
643 * when (s)he's changing the orders.
645 static void DeleteOrderWarnings(const Vehicle
*v
)
647 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS
);
648 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_VOID_ORDER
);
649 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY
);
650 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY
);
654 * Returns a tile somewhat representing the order destination (not suitable for pathfinding).
655 * @param v The vehicle to get the location for.
656 * @param airport Get the airport tile and not the station location for aircraft.
657 * @return destination of order, or INVALID_TILE if none.
659 TileIndex
Order::GetLocation(const Vehicle
*v
, bool airport
) const
661 switch (this->GetType()) {
662 case OT_GOTO_WAYPOINT
:
663 case OT_GOTO_STATION
:
665 if (airport
&& v
->type
== VEH_AIRCRAFT
) return Station::Get(this->GetDestination())->airport
.tile
;
666 return BaseStation::Get(this->GetDestination())->xy
;
669 if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0) return INVALID_TILE
;
670 return (v
->type
== VEH_AIRCRAFT
) ? Station::Get(this->GetDestination())->xy
: Depot::Get(this->GetDestination())->xy
;
678 * Get the distance between two orders of a vehicle. Conditional orders are resolved
679 * and the bigger distance of the two order branches is returned.
680 * @param prev Origin order.
681 * @param cur Destination order.
682 * @param v The vehicle to get the distance for.
683 * @param conditional_depth Internal param for resolving conditional orders.
684 * @return Maximum distance between the two orders.
686 uint
GetOrderDistance(const Order
*prev
, const Order
*cur
, const Vehicle
*v
, int conditional_depth
)
688 if (cur
->IsType(OT_CONDITIONAL
)) {
689 if (conditional_depth
> v
->GetNumOrders()) return 0;
693 int dist1
= GetOrderDistance(prev
, v
->GetOrder(cur
->GetConditionSkipToOrder()), v
, conditional_depth
);
694 int dist2
= GetOrderDistance(prev
, cur
->next
== NULL
? v
->orders
.list
->GetFirstOrder() : cur
->next
, v
, conditional_depth
);
695 return max(dist1
, dist2
);
698 TileIndex prev_tile
= prev
->GetLocation(v
, true);
699 TileIndex cur_tile
= cur
->GetLocation(v
, true);
700 if (prev_tile
== INVALID_TILE
|| cur_tile
== INVALID_TILE
) return 0;
701 return v
->type
== VEH_AIRCRAFT
? DistanceSquare(prev_tile
, cur_tile
) : DistanceManhattan(prev_tile
, cur_tile
);
705 * Add an order to the orderlist of a vehicle.
707 * @param flags operation to perform
708 * @param p1 various bitstuffed elements
709 * - p1 = (bit 0 - 19) - ID of the vehicle
710 * - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
711 * the order will be inserted before that one
712 * the maximum vehicle order id is 254.
713 * @param p2 packed order to insert
715 * @return the cost of this operation or an error
717 CommandCost
CmdInsertOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
719 VehicleID veh
= GB(p1
, 0, 20);
720 VehicleOrderID sel_ord
= GB(p1
, 20, 8);
723 Vehicle
*v
= Vehicle::GetIfValid(veh
);
724 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
726 CommandCost ret
= CheckOwnership(v
->owner
);
727 if (ret
.Failed()) return ret
;
729 /* Check if the inserted order is to the correct destination (owner, type),
730 * and has the correct flags if any */
731 switch (new_order
.GetType()) {
732 case OT_GOTO_STATION
: {
733 const Station
*st
= Station::GetIfValid(new_order
.GetDestination());
734 if (st
== NULL
) return CMD_ERROR
;
736 if (st
->owner
!= OWNER_NONE
) {
737 CommandCost ret
= CheckOwnership(st
->owner
);
738 if (ret
.Failed()) return ret
;
741 if (!CanVehicleUseStation(v
, st
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER
);
742 for (Vehicle
*u
= v
->FirstShared(); u
!= NULL
; u
= u
->NextShared()) {
743 if (!CanVehicleUseStation(u
, st
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED
);
746 /* Non stop only allowed for ground vehicles. */
747 if (new_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
&& !v
->IsGroundVehicle()) return CMD_ERROR
;
749 /* Filter invalid load/unload types. */
750 switch (new_order
.GetLoadType()) {
751 case OLF_LOAD_IF_POSSIBLE
: case OLFB_FULL_LOAD
: case OLF_FULL_LOAD_ANY
: case OLFB_NO_LOAD
: break;
752 default: return CMD_ERROR
;
754 switch (new_order
.GetUnloadType()) {
755 case OUF_UNLOAD_IF_POSSIBLE
: case OUFB_UNLOAD
: case OUFB_TRANSFER
: case OUFB_NO_UNLOAD
: break;
756 default: return CMD_ERROR
;
759 /* Filter invalid stop locations */
760 switch (new_order
.GetStopLocation()) {
761 case OSL_PLATFORM_NEAR_END
:
762 case OSL_PLATFORM_MIDDLE
:
763 if (v
->type
!= VEH_TRAIN
) return CMD_ERROR
;
765 case OSL_PLATFORM_FAR_END
:
775 case OT_GOTO_DEPOT
: {
776 if ((new_order
.GetDepotActionType() & ODATFB_NEAREST_DEPOT
) == 0) {
777 if (v
->type
== VEH_AIRCRAFT
) {
778 const Station
*st
= Station::GetIfValid(new_order
.GetDestination());
780 if (st
== NULL
) return CMD_ERROR
;
782 CommandCost ret
= CheckOwnership(st
->owner
);
783 if (ret
.Failed()) return ret
;
785 if (!CanVehicleUseStation(v
, st
) || !st
->airport
.HasHangar()) {
789 const Depot
*dp
= Depot::GetIfValid(new_order
.GetDestination());
791 if (dp
== NULL
) return CMD_ERROR
;
793 CommandCost ret
= CheckOwnership(GetTileOwner(dp
->xy
));
794 if (ret
.Failed()) return ret
;
798 if (!IsRailDepotTile(dp
->xy
)) return CMD_ERROR
;
802 if (!IsRoadDepotTile(dp
->xy
)) return CMD_ERROR
;
806 if (!IsShipDepotTile(dp
->xy
)) return CMD_ERROR
;
809 default: return CMD_ERROR
;
814 if (new_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
&& !v
->IsGroundVehicle()) return CMD_ERROR
;
815 if (new_order
.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS
| ((new_order
.GetDepotOrderType() & ODTFB_PART_OF_ORDERS
) != 0 ? ODTFB_SERVICE
: 0))) return CMD_ERROR
;
816 if (new_order
.GetDepotActionType() & ~(ODATFB_HALT
| ODATFB_NEAREST_DEPOT
)) return CMD_ERROR
;
817 if ((new_order
.GetDepotOrderType() & ODTFB_SERVICE
) && (new_order
.GetDepotActionType() & ODATFB_HALT
)) return CMD_ERROR
;
821 case OT_GOTO_WAYPOINT
: {
822 const Waypoint
*wp
= Waypoint::GetIfValid(new_order
.GetDestination());
823 if (wp
== NULL
) return CMD_ERROR
;
826 default: return CMD_ERROR
;
829 if (!(wp
->facilities
& FACIL_TRAIN
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER
);
831 CommandCost ret
= CheckOwnership(wp
->owner
);
832 if (ret
.Failed()) return ret
;
837 if (!(wp
->facilities
& FACIL_DOCK
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER
);
838 if (wp
->owner
!= OWNER_NONE
) {
839 CommandCost ret
= CheckOwnership(wp
->owner
);
840 if (ret
.Failed()) return ret
;
845 /* Order flags can be any of the following for waypoints:
847 * non-stop orders (if any) are only valid for trains */
848 if (new_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
&& v
->type
!= VEH_TRAIN
) return CMD_ERROR
;
852 case OT_CONDITIONAL
: {
853 VehicleOrderID skip_to
= new_order
.GetConditionSkipToOrder();
854 if (skip_to
!= 0 && skip_to
>= v
->GetNumOrders()) return CMD_ERROR
; // Always allow jumping to the first (even when there is no order).
855 if (new_order
.GetConditionVariable() >= OCV_END
) return CMD_ERROR
;
857 OrderConditionComparator occ
= new_order
.GetConditionComparator();
858 if (occ
>= OCC_END
) return CMD_ERROR
;
859 switch (new_order
.GetConditionVariable()) {
860 case OCV_REQUIRES_SERVICE
:
861 if (occ
!= OCC_IS_TRUE
&& occ
!= OCC_IS_FALSE
) return CMD_ERROR
;
864 case OCV_UNCONDITIONALLY
:
865 if (occ
!= OCC_EQUALS
) return CMD_ERROR
;
866 if (new_order
.GetConditionValue() != 0) return CMD_ERROR
;
869 case OCV_LOAD_PERCENTAGE
:
870 case OCV_RELIABILITY
:
871 if (new_order
.GetConditionValue() > 100) return CMD_ERROR
;
874 if (occ
== OCC_IS_TRUE
|| occ
== OCC_IS_FALSE
) return CMD_ERROR
;
880 default: return CMD_ERROR
;
883 if (sel_ord
> v
->GetNumOrders()) return CMD_ERROR
;
885 if (v
->GetNumOrders() >= MAX_VEH_ORDER_ID
) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS
);
886 if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
887 if (v
->orders
.list
== NULL
&& !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
889 if (v
->type
== VEH_SHIP
) {
890 /* Make sure the new destination is not too far away from the previous */
891 const Order
*prev
= NULL
;
894 /* Find the last goto station or depot order before the insert location.
895 * If the order is to be inserted at the beginning of the order list this
896 * finds the last order in the list. */
898 FOR_VEHICLE_ORDERS(v
, o
) {
899 switch (o
->GetType()) {
900 case OT_GOTO_STATION
:
902 case OT_GOTO_WAYPOINT
:
908 if (++n
== sel_ord
&& prev
!= NULL
) break;
912 if (new_order
.IsType(OT_CONDITIONAL
)) {
913 /* The order is not yet inserted, so we have to do the first iteration here. */
914 dist
= GetOrderDistance(prev
, v
->GetOrder(new_order
.GetConditionSkipToOrder()), v
);
916 dist
= GetOrderDistance(prev
, &new_order
, v
);
920 return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION
);
925 if (flags
& DC_EXEC
) {
926 Order
*new_o
= new Order();
927 new_o
->AssignOrder(new_order
);
928 InsertOrder(v
, new_o
, sel_ord
);
931 return CommandCost();
935 * Insert a new order but skip the validation.
936 * @param v The vehicle to insert the order to.
937 * @param new_o The new order.
938 * @param sel_ord The position the order should be inserted at.
940 void InsertOrder(Vehicle
*v
, Order
*new_o
, VehicleOrderID sel_ord
)
942 /* Create new order and link in list */
943 if (v
->orders
.list
== NULL
) {
944 v
->orders
.list
= new OrderList(new_o
, v
);
946 v
->orders
.list
->InsertOrderAt(new_o
, sel_ord
);
949 Vehicle
*u
= v
->FirstShared();
950 DeleteOrderWarnings(u
);
951 for (; u
!= NULL
; u
= u
->NextShared()) {
952 assert(v
->orders
.list
== u
->orders
.list
);
954 /* If there is added an order before the current one, we need
955 * to update the selected order. We do not change implicit/real order indices though.
956 * If the new order is between the current implicit order and real order, the implicit order will
957 * later skip the inserted order. */
958 if (sel_ord
<= u
->cur_real_order_index
) {
959 uint cur
= u
->cur_real_order_index
+ 1;
960 /* Check if we don't go out of bound */
961 if (cur
< u
->GetNumOrders()) {
962 u
->cur_real_order_index
= cur
;
965 if (sel_ord
== u
->cur_implicit_order_index
&& u
->IsGroundVehicle()) {
966 /* We are inserting an order just before the current implicit order.
967 * We do not know whether we will reach current implicit or the newly inserted order first.
968 * So, disable creation of implicit orders until we are on track again. */
969 uint16
&gv_flags
= u
->GetGroundVehicleFlags();
970 SetBit(gv_flags
, GVF_SUPPRESS_IMPLICIT_ORDERS
);
972 if (sel_ord
<= u
->cur_implicit_order_index
) {
973 uint cur
= u
->cur_implicit_order_index
+ 1;
974 /* Check if we don't go out of bound */
975 if (cur
< u
->GetNumOrders()) {
976 u
->cur_implicit_order_index
= cur
;
979 /* Update any possible open window of the vehicle */
980 InvalidateVehicleOrder(u
, INVALID_VEH_ORDER_ID
| (sel_ord
<< 8));
983 /* As we insert an order, the order to skip to will be 'wrong'. */
984 VehicleOrderID cur_order_id
= 0;
986 FOR_VEHICLE_ORDERS(v
, order
) {
987 if (order
->IsType(OT_CONDITIONAL
)) {
988 VehicleOrderID order_id
= order
->GetConditionSkipToOrder();
989 if (order_id
>= sel_ord
) {
990 order
->SetConditionSkipToOrder(order_id
+ 1);
992 if (order_id
== cur_order_id
) {
993 order
->SetConditionSkipToOrder((order_id
+ 1) % v
->GetNumOrders());
999 /* Make sure to rebuild the whole list */
1000 InvalidateWindowClassesData(GetWindowClassForVehicleType(v
->type
), 0);
1004 * Declone an order-list
1005 * @param *dst delete the orders of this vehicle
1006 * @param flags execution flags
1008 static CommandCost
DecloneOrder(Vehicle
*dst
, DoCommandFlag flags
)
1010 if (flags
& DC_EXEC
) {
1011 DeleteVehicleOrders(dst
);
1012 InvalidateVehicleOrder(dst
, VIWD_REMOVE_ALL_ORDERS
);
1013 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst
->type
), 0);
1015 return CommandCost();
1019 * Delete an order from the orderlist of a vehicle.
1020 * @param tile unused
1021 * @param flags operation to perform
1022 * @param p1 the ID of the vehicle
1023 * @param p2 the order to delete (max 255)
1024 * @param text unused
1025 * @return the cost of this operation or an error
1027 CommandCost
CmdDeleteOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1029 VehicleID veh_id
= GB(p1
, 0, 20);
1030 VehicleOrderID sel_ord
= GB(p2
, 0, 8);
1032 Vehicle
*v
= Vehicle::GetIfValid(veh_id
);
1034 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1036 CommandCost ret
= CheckOwnership(v
->owner
);
1037 if (ret
.Failed()) return ret
;
1039 /* If we did not select an order, we maybe want to de-clone the orders */
1040 if (sel_ord
>= v
->GetNumOrders()) return DecloneOrder(v
, flags
);
1042 if (v
->GetOrder(sel_ord
) == NULL
) return CMD_ERROR
;
1044 if (flags
& DC_EXEC
) DeleteOrder(v
, sel_ord
);
1045 return CommandCost();
1049 * Cancel the current loading order of the vehicle as the order was deleted.
1050 * @param v the vehicle
1052 static void CancelLoadingDueToDeletedOrder(Vehicle
*v
)
1054 assert(v
->current_order
.IsType(OT_LOADING
));
1055 /* NON-stop flag is misused to see if a train is in a station that is
1056 * on his order list or not */
1057 v
->current_order
.SetNonStopType(ONSF_STOP_EVERYWHERE
);
1058 /* When full loading, "cancel" that order so the vehicle doesn't
1059 * stay indefinitely at this station anymore. */
1060 if (v
->current_order
.GetLoadType() & OLFB_FULL_LOAD
) v
->current_order
.SetLoadType(OLF_LOAD_IF_POSSIBLE
);
1064 * Delete an order but skip the parameter validation.
1065 * @param v The vehicle to delete the order from.
1066 * @param sel_ord The id of the order to be deleted.
1068 void DeleteOrder(Vehicle
*v
, VehicleOrderID sel_ord
)
1070 v
->orders
.list
->DeleteOrderAt(sel_ord
);
1072 Vehicle
*u
= v
->FirstShared();
1073 DeleteOrderWarnings(u
);
1074 for (; u
!= NULL
; u
= u
->NextShared()) {
1075 assert(v
->orders
.list
== u
->orders
.list
);
1077 if (sel_ord
== u
->cur_real_order_index
&& u
->current_order
.IsType(OT_LOADING
)) {
1078 CancelLoadingDueToDeletedOrder(u
);
1081 if (sel_ord
< u
->cur_real_order_index
) {
1082 u
->cur_real_order_index
--;
1083 } else if (sel_ord
== u
->cur_real_order_index
) {
1084 u
->UpdateRealOrderIndex();
1087 if (sel_ord
< u
->cur_implicit_order_index
) {
1088 u
->cur_implicit_order_index
--;
1089 } else if (sel_ord
== u
->cur_implicit_order_index
) {
1090 /* Make sure the index is valid */
1091 if (u
->cur_implicit_order_index
>= u
->GetNumOrders()) u
->cur_implicit_order_index
= 0;
1093 /* Skip non-implicit orders for the implicit-order-index (e.g. if the current implicit order was deleted */
1094 while (u
->cur_implicit_order_index
!= u
->cur_real_order_index
&& !u
->GetOrder(u
->cur_implicit_order_index
)->IsType(OT_IMPLICIT
)) {
1095 u
->cur_implicit_order_index
++;
1096 if (u
->cur_implicit_order_index
>= u
->GetNumOrders()) u
->cur_implicit_order_index
= 0;
1100 /* Update any possible open window of the vehicle */
1101 InvalidateVehicleOrder(u
, sel_ord
| (INVALID_VEH_ORDER_ID
<< 8));
1104 /* As we delete an order, the order to skip to will be 'wrong'. */
1105 VehicleOrderID cur_order_id
= 0;
1106 Order
*order
= NULL
;
1107 FOR_VEHICLE_ORDERS(v
, order
) {
1108 if (order
->IsType(OT_CONDITIONAL
)) {
1109 VehicleOrderID order_id
= order
->GetConditionSkipToOrder();
1110 if (order_id
>= sel_ord
) {
1111 order_id
= max(order_id
- 1, 0);
1113 if (order_id
== cur_order_id
) {
1114 order_id
= (order_id
+ 1) % v
->GetNumOrders();
1116 order
->SetConditionSkipToOrder(order_id
);
1121 InvalidateWindowClassesData(GetWindowClassForVehicleType(v
->type
), 0);
1125 * Goto order of order-list.
1126 * @param tile unused
1127 * @param flags operation to perform
1128 * @param p1 The ID of the vehicle which order is skipped
1129 * @param p2 the selected order to which we want to skip
1130 * @param text unused
1131 * @return the cost of this operation or an error
1133 CommandCost
CmdSkipToOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1135 VehicleID veh_id
= GB(p1
, 0, 20);
1136 VehicleOrderID sel_ord
= GB(p2
, 0, 8);
1138 Vehicle
*v
= Vehicle::GetIfValid(veh_id
);
1140 if (v
== NULL
|| !v
->IsPrimaryVehicle() || sel_ord
== v
->cur_implicit_order_index
|| sel_ord
>= v
->GetNumOrders() || v
->GetNumOrders() < 2) return CMD_ERROR
;
1142 CommandCost ret
= CheckOwnership(v
->owner
);
1143 if (ret
.Failed()) return ret
;
1145 if (flags
& DC_EXEC
) {
1146 if (v
->current_order
.IsType(OT_LOADING
)) v
->LeaveStation();
1148 v
->cur_implicit_order_index
= v
->cur_real_order_index
= sel_ord
;
1149 v
->UpdateRealOrderIndex();
1151 InvalidateVehicleOrder(v
, VIWD_MODIFY_ORDERS
);
1154 /* We have an aircraft/ship, they have a mini-schedule, so update them all */
1155 if (v
->type
== VEH_AIRCRAFT
) SetWindowClassesDirty(WC_AIRCRAFT_LIST
);
1156 if (v
->type
== VEH_SHIP
) SetWindowClassesDirty(WC_SHIPS_LIST
);
1158 return CommandCost();
1162 * Move an order inside the orderlist
1163 * @param tile unused
1164 * @param flags operation to perform
1165 * @param p1 the ID of the vehicle
1166 * @param p2 order to move and target
1167 * bit 0-15 : the order to move
1168 * bit 16-31 : the target order
1169 * @param text unused
1170 * @return the cost of this operation or an error
1171 * @note The target order will move one place down in the orderlist
1172 * if you move the order upwards else it'll move it one place down
1174 CommandCost
CmdMoveOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1176 VehicleID veh
= GB(p1
, 0, 20);
1177 VehicleOrderID moving_order
= GB(p2
, 0, 16);
1178 VehicleOrderID target_order
= GB(p2
, 16, 16);
1180 Vehicle
*v
= Vehicle::GetIfValid(veh
);
1181 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1183 CommandCost ret
= CheckOwnership(v
->owner
);
1184 if (ret
.Failed()) return ret
;
1186 /* Don't make senseless movements */
1187 if (moving_order
>= v
->GetNumOrders() || target_order
>= v
->GetNumOrders() ||
1188 moving_order
== target_order
|| v
->GetNumOrders() <= 1) return CMD_ERROR
;
1190 Order
*moving_one
= v
->GetOrder(moving_order
);
1191 /* Don't move an empty order */
1192 if (moving_one
== NULL
) return CMD_ERROR
;
1194 if (flags
& DC_EXEC
) {
1195 v
->orders
.list
->MoveOrder(moving_order
, target_order
);
1197 /* Update shared list */
1198 Vehicle
*u
= v
->FirstShared();
1200 DeleteOrderWarnings(u
);
1202 for (; u
!= NULL
; u
= u
->NextShared()) {
1203 /* Update the current order.
1204 * There are multiple ways to move orders, which result in cur_implicit_order_index
1205 * and cur_real_order_index to not longer make any sense. E.g. moving another
1206 * real order between them.
1208 * Basically one could choose to preserve either of them, but not both.
1209 * While both ways are suitable in this or that case from a human point of view, neither
1210 * of them makes really sense.
1211 * However, from an AI point of view, preserving cur_real_order_index is the most
1212 * predictable and transparent behaviour.
1214 * With that decision it basically does not matter what we do to cur_implicit_order_index.
1215 * If we change orders between the implicit- and real-index, the implicit orders are mostly likely
1216 * completely out-dated anyway. So, keep it simple and just keep cur_implicit_order_index as well.
1217 * The worst which can happen is that a lot of implicit orders are removed when reaching current_order.
1219 if (u
->cur_real_order_index
== moving_order
) {
1220 u
->cur_real_order_index
= target_order
;
1221 } else if (u
->cur_real_order_index
> moving_order
&& u
->cur_real_order_index
<= target_order
) {
1222 u
->cur_real_order_index
--;
1223 } else if (u
->cur_real_order_index
< moving_order
&& u
->cur_real_order_index
>= target_order
) {
1224 u
->cur_real_order_index
++;
1227 if (u
->cur_implicit_order_index
== moving_order
) {
1228 u
->cur_implicit_order_index
= target_order
;
1229 } else if (u
->cur_implicit_order_index
> moving_order
&& u
->cur_implicit_order_index
<= target_order
) {
1230 u
->cur_implicit_order_index
--;
1231 } else if (u
->cur_implicit_order_index
< moving_order
&& u
->cur_implicit_order_index
>= target_order
) {
1232 u
->cur_implicit_order_index
++;
1235 assert(v
->orders
.list
== u
->orders
.list
);
1236 /* Update any possible open window of the vehicle */
1237 InvalidateVehicleOrder(u
, moving_order
| (target_order
<< 8));
1240 /* As we move an order, the order to skip to will be 'wrong'. */
1242 FOR_VEHICLE_ORDERS(v
, order
) {
1243 if (order
->IsType(OT_CONDITIONAL
)) {
1244 VehicleOrderID order_id
= order
->GetConditionSkipToOrder();
1245 if (order_id
== moving_order
) {
1246 order_id
= target_order
;
1247 } else if (order_id
> moving_order
&& order_id
<= target_order
) {
1249 } else if (order_id
< moving_order
&& order_id
>= target_order
) {
1252 order
->SetConditionSkipToOrder(order_id
);
1256 /* Make sure to rebuild the whole list */
1257 InvalidateWindowClassesData(GetWindowClassForVehicleType(v
->type
), 0);
1260 return CommandCost();
1264 * Modify an order in the orderlist of a vehicle.
1265 * @param tile unused
1266 * @param flags operation to perform
1267 * @param p1 various bitstuffed elements
1268 * - p1 = (bit 0 - 19) - ID of the vehicle
1269 * - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
1270 * the order will be inserted before that one
1271 * the maximum vehicle order id is 254.
1272 * @param p2 various bitstuffed elements
1273 * - p2 = (bit 0 - 3) - what data to modify (@see ModifyOrderFlags)
1274 * - p2 = (bit 4 - 15) - the data to modify
1275 * @param text unused
1276 * @return the cost of this operation or an error
1278 CommandCost
CmdModifyOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1280 VehicleOrderID sel_ord
= GB(p1
, 20, 8);
1281 VehicleID veh
= GB(p1
, 0, 20);
1282 ModifyOrderFlags mof
= Extract
<ModifyOrderFlags
, 0, 4>(p2
);
1283 uint16 data
= GB(p2
, 4, 11);
1285 if (mof
>= MOF_END
) return CMD_ERROR
;
1287 Vehicle
*v
= Vehicle::GetIfValid(veh
);
1288 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1290 CommandCost ret
= CheckOwnership(v
->owner
);
1291 if (ret
.Failed()) return ret
;
1293 /* Is it a valid order? */
1294 if (sel_ord
>= v
->GetNumOrders()) return CMD_ERROR
;
1296 Order
*order
= v
->GetOrder(sel_ord
);
1297 switch (order
->GetType()) {
1298 case OT_GOTO_STATION
:
1299 if (mof
== MOF_COND_VARIABLE
|| mof
== MOF_COND_COMPARATOR
|| mof
== MOF_DEPOT_ACTION
|| mof
== MOF_COND_VALUE
) return CMD_ERROR
;
1303 if (mof
!= MOF_NON_STOP
&& mof
!= MOF_DEPOT_ACTION
) return CMD_ERROR
;
1306 case OT_GOTO_WAYPOINT
:
1307 if (mof
!= MOF_NON_STOP
) return CMD_ERROR
;
1310 case OT_CONDITIONAL
:
1311 if (mof
!= MOF_COND_VARIABLE
&& mof
!= MOF_COND_COMPARATOR
&& mof
!= MOF_COND_VALUE
&& mof
!= MOF_COND_DESTINATION
) return CMD_ERROR
;
1319 default: NOT_REACHED();
1322 if (!v
->IsGroundVehicle()) return CMD_ERROR
;
1323 if (data
>= ONSF_END
) return CMD_ERROR
;
1324 if (data
== order
->GetNonStopType()) return CMD_ERROR
;
1327 case MOF_STOP_LOCATION
:
1328 if (v
->type
!= VEH_TRAIN
) return CMD_ERROR
;
1329 if (data
>= OSL_END
) return CMD_ERROR
;
1333 if ((data
& ~(OUFB_UNLOAD
| OUFB_TRANSFER
| OUFB_NO_UNLOAD
)) != 0) return CMD_ERROR
;
1334 /* Unload and no-unload are mutual exclusive and so are transfer and no unload. */
1335 if (data
!= 0 && ((data
& (OUFB_UNLOAD
| OUFB_TRANSFER
)) != 0) == ((data
& OUFB_NO_UNLOAD
) != 0)) return CMD_ERROR
;
1336 if (data
== order
->GetUnloadType()) return CMD_ERROR
;
1340 if (data
> OLFB_NO_LOAD
|| data
== 1) return CMD_ERROR
;
1341 if (data
== order
->GetLoadType()) return CMD_ERROR
;
1344 case MOF_DEPOT_ACTION
:
1345 if (data
>= DA_END
) return CMD_ERROR
;
1348 case MOF_COND_VARIABLE
:
1349 if (data
>= OCV_END
) return CMD_ERROR
;
1352 case MOF_COND_COMPARATOR
:
1353 if (data
>= OCC_END
) return CMD_ERROR
;
1354 switch (order
->GetConditionVariable()) {
1355 case OCV_UNCONDITIONALLY
: return CMD_ERROR
;
1357 case OCV_REQUIRES_SERVICE
:
1358 if (data
!= OCC_IS_TRUE
&& data
!= OCC_IS_FALSE
) return CMD_ERROR
;
1362 if (data
== OCC_IS_TRUE
|| data
== OCC_IS_FALSE
) return CMD_ERROR
;
1367 case MOF_COND_VALUE
:
1368 switch (order
->GetConditionVariable()) {
1369 case OCV_UNCONDITIONALLY
: return CMD_ERROR
;
1371 case OCV_LOAD_PERCENTAGE
:
1372 case OCV_RELIABILITY
:
1373 if (data
> 100) return CMD_ERROR
;
1377 if (data
> 2047) return CMD_ERROR
;
1382 case MOF_COND_DESTINATION
:
1383 if (data
>= v
->GetNumOrders()) return CMD_ERROR
;
1387 if (flags
& DC_EXEC
) {
1390 order
->SetNonStopType((OrderNonStopFlags
)data
);
1391 if (data
& ONSF_NO_STOP_AT_DESTINATION_STATION
) order
->SetRefit(CT_NO_REFIT
);
1394 case MOF_STOP_LOCATION
:
1395 order
->SetStopLocation((OrderStopLocation
)data
);
1399 order
->SetUnloadType((OrderUnloadFlags
)data
);
1403 order
->SetLoadType((OrderLoadFlags
)data
);
1404 if (data
& OLFB_NO_LOAD
) order
->SetRefit(CT_NO_REFIT
);
1407 case MOF_DEPOT_ACTION
: {
1410 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() & ~ODTFB_SERVICE
));
1411 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() & ~ODATFB_HALT
));
1415 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() | ODTFB_SERVICE
));
1416 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() & ~ODATFB_HALT
));
1417 order
->SetRefit(CT_NO_REFIT
);
1421 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() & ~ODTFB_SERVICE
));
1422 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() | ODATFB_HALT
));
1423 order
->SetRefit(CT_NO_REFIT
);
1432 case MOF_COND_VARIABLE
: {
1433 order
->SetConditionVariable((OrderConditionVariable
)data
);
1435 OrderConditionComparator occ
= order
->GetConditionComparator();
1436 switch (order
->GetConditionVariable()) {
1437 case OCV_UNCONDITIONALLY
:
1438 order
->SetConditionComparator(OCC_EQUALS
);
1439 order
->SetConditionValue(0);
1442 case OCV_REQUIRES_SERVICE
:
1443 if (occ
!= OCC_IS_TRUE
&& occ
!= OCC_IS_FALSE
) order
->SetConditionComparator(OCC_IS_TRUE
);
1446 case OCV_LOAD_PERCENTAGE
:
1447 case OCV_RELIABILITY
:
1448 if (order
->GetConditionValue() > 100) order
->SetConditionValue(100);
1451 if (occ
== OCC_IS_TRUE
|| occ
== OCC_IS_FALSE
) order
->SetConditionComparator(OCC_EQUALS
);
1457 case MOF_COND_COMPARATOR
:
1458 order
->SetConditionComparator((OrderConditionComparator
)data
);
1461 case MOF_COND_VALUE
:
1462 order
->SetConditionValue(data
);
1465 case MOF_COND_DESTINATION
:
1466 order
->SetConditionSkipToOrder(data
);
1469 default: NOT_REACHED();
1472 /* Update the windows and full load flags, also for vehicles that share the same order list */
1473 Vehicle
*u
= v
->FirstShared();
1474 DeleteOrderWarnings(u
);
1475 for (; u
!= NULL
; u
= u
->NextShared()) {
1476 /* Toggle u->current_order "Full load" flag if it changed.
1477 * However, as the same flag is used for depot orders, check
1478 * whether we are not going to a depot as there are three
1479 * cases where the full load flag can be active and only
1480 * one case where the flag is used for depot orders. In the
1481 * other cases for the OrderTypeByte the flags are not used,
1482 * so do not care and those orders should not be active
1483 * when this function is called.
1485 if (sel_ord
== u
->cur_real_order_index
&&
1486 (u
->current_order
.IsType(OT_GOTO_STATION
) || u
->current_order
.IsType(OT_LOADING
)) &&
1487 u
->current_order
.GetLoadType() != order
->GetLoadType()) {
1488 u
->current_order
.SetLoadType(order
->GetLoadType());
1490 InvalidateVehicleOrder(u
, VIWD_MODIFY_ORDERS
);
1494 return CommandCost();
1498 * Check if an aircraft has enough range for an order list.
1499 * @param v_new Aircraft to check.
1500 * @param v_order Vehicle currently holding the order list.
1501 * @param first First order in the source order list.
1502 * @return True if the aircraft has enough range for the orders, false otherwise.
1504 static bool CheckAircraftOrderDistance(const Aircraft
*v_new
, const Vehicle
*v_order
, const Order
*first
)
1506 if (first
== NULL
|| v_new
->acache
.cached_max_range
== 0) return true;
1508 /* Iterate over all orders to check the distance between all
1509 * 'goto' orders and their respective next order (of any type). */
1510 for (const Order
*o
= first
; o
!= NULL
; o
= o
->next
) {
1511 switch (o
->GetType()) {
1512 case OT_GOTO_STATION
:
1514 case OT_GOTO_WAYPOINT
:
1515 /* If we don't have a next order, we've reached the end and must check the first order instead. */
1516 if (GetOrderDistance(o
, o
->next
!= NULL
? o
->next
: first
, v_order
) > v_new
->acache
.cached_max_range_sqr
) return false;
1527 * Clone/share/copy an order-list of another vehicle.
1528 * @param tile unused
1529 * @param flags operation to perform
1530 * @param p1 various bitstuffed elements
1531 * - p1 = (bit 0-19) - destination vehicle to clone orders to
1532 * - p1 = (bit 30-31) - action to perform
1533 * @param p2 source vehicle to clone orders from, if any (none for CO_UNSHARE)
1534 * @param text unused
1535 * @return the cost of this operation or an error
1537 CommandCost
CmdCloneOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1539 VehicleID veh_src
= GB(p2
, 0, 20);
1540 VehicleID veh_dst
= GB(p1
, 0, 20);
1542 Vehicle
*dst
= Vehicle::GetIfValid(veh_dst
);
1543 if (dst
== NULL
|| !dst
->IsPrimaryVehicle()) return CMD_ERROR
;
1545 CommandCost ret
= CheckOwnership(dst
->owner
);
1546 if (ret
.Failed()) return ret
;
1548 switch (GB(p1
, 30, 2)) {
1550 Vehicle
*src
= Vehicle::GetIfValid(veh_src
);
1553 if (src
== NULL
|| !src
->IsPrimaryVehicle() || dst
->type
!= src
->type
|| dst
== src
) return CMD_ERROR
;
1555 CommandCost ret
= CheckOwnership(src
->owner
);
1556 if (ret
.Failed()) return ret
;
1558 /* Trucks can't share orders with busses (and visa versa) */
1559 if (src
->type
== VEH_ROAD
&& RoadVehicle::From(src
)->IsBus() != RoadVehicle::From(dst
)->IsBus()) {
1563 /* Is the vehicle already in the shared list? */
1564 if (src
->FirstShared() == dst
->FirstShared()) return CMD_ERROR
;
1568 FOR_VEHICLE_ORDERS(src
, order
) {
1569 if (!OrderGoesToStation(dst
, order
)) continue;
1571 /* Allow copying unreachable destinations if they were already unreachable for the source.
1572 * This is basically to allow cloning / autorenewing / autoreplacing vehicles, while the stations
1573 * are temporarily invalid due to reconstruction. */
1574 const Station
*st
= Station::Get(order
->GetDestination());
1575 if (CanVehicleUseStation(src
, st
) && !CanVehicleUseStation(dst
, st
)) {
1576 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER
);
1580 /* Check for aircraft range limits. */
1581 if (dst
->type
== VEH_AIRCRAFT
&& !CheckAircraftOrderDistance(Aircraft::From(dst
), src
, src
->GetFirstOrder())) {
1582 return_cmd_error(STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE
);
1585 if (src
->orders
.list
== NULL
&& !OrderList::CanAllocateItem()) {
1586 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
1589 if (flags
& DC_EXEC
) {
1590 /* If the destination vehicle had a OrderList, destroy it.
1591 * We only reset the order indices, if the new orders are obviously different.
1592 * (We mainly do this to keep the order indices valid and in range.) */
1593 DeleteVehicleOrders(dst
, false, dst
->GetNumOrders() != src
->GetNumOrders());
1595 dst
->orders
.list
= src
->orders
.list
;
1597 /* Link this vehicle in the shared-list */
1598 dst
->AddToShared(src
);
1600 InvalidateVehicleOrder(dst
, VIWD_REMOVE_ALL_ORDERS
);
1601 InvalidateVehicleOrder(src
, VIWD_MODIFY_ORDERS
);
1603 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst
->type
), 0);
1609 Vehicle
*src
= Vehicle::GetIfValid(veh_src
);
1612 if (src
== NULL
|| !src
->IsPrimaryVehicle() || dst
->type
!= src
->type
|| dst
== src
) return CMD_ERROR
;
1614 CommandCost ret
= CheckOwnership(src
->owner
);
1615 if (ret
.Failed()) return ret
;
1617 /* Trucks can't copy all the orders from busses (and visa versa),
1618 * and neither can helicopters and aircraft. */
1620 FOR_VEHICLE_ORDERS(src
, order
) {
1621 if (OrderGoesToStation(dst
, order
) &&
1622 !CanVehicleUseStation(dst
, Station::Get(order
->GetDestination()))) {
1623 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER
);
1627 /* Check for aircraft range limits. */
1628 if (dst
->type
== VEH_AIRCRAFT
&& !CheckAircraftOrderDistance(Aircraft::From(dst
), src
, src
->GetFirstOrder())) {
1629 return_cmd_error(STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE
);
1632 /* make sure there are orders available */
1633 if (!Order::CanAllocateItem(src
->GetNumOrders()) || !OrderList::CanAllocateItem()) {
1634 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
1637 if (flags
& DC_EXEC
) {
1639 Order
*first
= NULL
;
1642 /* If the destination vehicle had an order list, destroy the chain but keep the OrderList.
1643 * We only reset the order indices, if the new orders are obviously different.
1644 * (We mainly do this to keep the order indices valid and in range.) */
1645 DeleteVehicleOrders(dst
, true, dst
->GetNumOrders() != src
->GetNumOrders());
1648 FOR_VEHICLE_ORDERS(src
, order
) {
1649 *order_dst
= new Order();
1650 (*order_dst
)->AssignOrder(*order
);
1651 order_dst
= &(*order_dst
)->next
;
1653 if (dst
->orders
.list
== NULL
) {
1654 dst
->orders
.list
= new OrderList(first
, dst
);
1656 assert(dst
->orders
.list
->GetFirstOrder() == NULL
);
1657 assert(!dst
->orders
.list
->IsShared());
1658 delete dst
->orders
.list
;
1659 assert(OrderList::CanAllocateItem());
1660 dst
->orders
.list
= new OrderList(first
, dst
);
1663 InvalidateVehicleOrder(dst
, VIWD_REMOVE_ALL_ORDERS
);
1665 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst
->type
), 0);
1670 case CO_UNSHARE
: return DecloneOrder(dst
, flags
);
1671 default: return CMD_ERROR
;
1674 return CommandCost();
1678 * Add/remove refit orders from an order
1679 * @param tile Not used
1680 * @param flags operation to perform
1681 * @param p1 VehicleIndex of the vehicle having the order
1684 * - bit 16-23 number of order to modify
1685 * @param text unused
1686 * @return the cost of this operation or an error
1688 CommandCost
CmdOrderRefit(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1690 VehicleID veh
= GB(p1
, 0, 20);
1691 VehicleOrderID order_number
= GB(p2
, 16, 8);
1692 CargoID cargo
= GB(p2
, 0, 8);
1694 if (cargo
>= NUM_CARGO
&& cargo
!= CT_NO_REFIT
&& cargo
!= CT_AUTO_REFIT
) return CMD_ERROR
;
1696 const Vehicle
*v
= Vehicle::GetIfValid(veh
);
1697 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1699 CommandCost ret
= CheckOwnership(v
->owner
);
1700 if (ret
.Failed()) return ret
;
1702 Order
*order
= v
->GetOrder(order_number
);
1703 if (order
== NULL
) return CMD_ERROR
;
1705 /* Automatic refit cargo is only supported for goto station orders. */
1706 if (cargo
== CT_AUTO_REFIT
&& !order
->IsType(OT_GOTO_STATION
)) return CMD_ERROR
;
1708 if (order
->GetLoadType() & OLFB_NO_LOAD
) return CMD_ERROR
;
1710 if (flags
& DC_EXEC
) {
1711 order
->SetRefit(cargo
);
1713 /* Make the depot order an 'always go' order. */
1714 if (cargo
!= CT_NO_REFIT
&& order
->IsType(OT_GOTO_DEPOT
)) {
1715 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() & ~ODTFB_SERVICE
));
1716 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() & ~ODATFB_HALT
));
1719 for (Vehicle
*u
= v
->FirstShared(); u
!= NULL
; u
= u
->NextShared()) {
1720 /* Update any possible open window of the vehicle */
1721 InvalidateVehicleOrder(u
, VIWD_MODIFY_ORDERS
);
1723 /* If the vehicle already got the current depot set as current order, then update current order as well */
1724 if (u
->cur_real_order_index
== order_number
&& (u
->current_order
.GetDepotOrderType() & ODTFB_PART_OF_ORDERS
)) {
1725 u
->current_order
.SetRefit(cargo
);
1730 return CommandCost();
1736 * Check the orders of a vehicle, to see if there are invalid orders and stuff
1739 void CheckOrders(const Vehicle
*v
)
1741 /* Does the user wants us to check things? */
1742 if (_settings_client
.gui
.order_review_system
== 0) return;
1744 /* Do nothing for crashed vehicles */
1745 if (v
->vehstatus
& VS_CRASHED
) return;
1747 /* Do nothing for stopped vehicles if setting is '1' */
1748 if (_settings_client
.gui
.order_review_system
== 1 && (v
->vehstatus
& VS_STOPPED
)) return;
1750 /* do nothing we we're not the first vehicle in a share-chain */
1751 if (v
->FirstShared() != v
) return;
1753 /* Only check every 20 days, so that we don't flood the message log */
1754 if (v
->owner
== _local_company
&& v
->day_counter
% 20 == 0) {
1755 int n_st
, problem_type
= -1;
1759 /* Check the order list */
1762 FOR_VEHICLE_ORDERS(v
, order
) {
1764 if (order
->IsType(OT_DUMMY
)) {
1768 /* Does station have a load-bay for this vehicle? */
1769 if (order
->IsType(OT_GOTO_STATION
)) {
1770 const Station
*st
= Station::Get(order
->GetDestination());
1773 if (!CanVehicleUseStation(v
, st
)) problem_type
= 3;
1777 /* Check if the last and the first order are the same */
1778 if (v
->GetNumOrders() > 1) {
1779 const Order
*last
= v
->GetLastOrder();
1781 if (v
->orders
.list
->GetFirstOrder()->Equals(*last
)) {
1786 /* Do we only have 1 station in our order list? */
1787 if (n_st
< 2 && problem_type
== -1) problem_type
= 0;
1790 if (v
->orders
.list
!= NULL
) v
->orders
.list
->DebugCheckSanity();
1793 /* We don't have a problem */
1794 if (problem_type
< 0) return;
1796 message
= STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS
+ problem_type
;
1797 //DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
1799 SetDParam(0, v
->index
);
1800 AddVehicleAdviceNewsItem(message
, v
->index
);
1805 * Removes an order from all vehicles. Triggers when, say, a station is removed.
1806 * @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
1807 * @param destination The destination. Can be a StationID, DepotID or WaypointID.
1809 void RemoveOrderFromAllVehicles(OrderType type
, DestinationID destination
)
1813 /* Aircraft have StationIDs for depot orders and never use DepotIDs
1814 * This fact is handled specially below
1817 /* Go through all vehicles */
1818 FOR_ALL_VEHICLES(v
) {
1821 order
= &v
->current_order
;
1822 if ((v
->type
== VEH_AIRCRAFT
&& order
->IsType(OT_GOTO_DEPOT
) ? OT_GOTO_STATION
: order
->GetType()) == type
&&
1823 v
->current_order
.GetDestination() == destination
) {
1825 SetWindowDirty(WC_VEHICLE_VIEW
, v
->index
);
1828 /* Clear the order from the order-list */
1830 FOR_VEHICLE_ORDERS(v
, order
) {
1834 OrderType ot
= order
->GetType();
1835 if (ot
== OT_GOTO_DEPOT
&& (order
->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0) continue;
1836 if (ot
== OT_IMPLICIT
|| (v
->type
== VEH_AIRCRAFT
&& ot
== OT_GOTO_DEPOT
)) ot
= OT_GOTO_STATION
;
1837 if (ot
== type
&& order
->GetDestination() == destination
) {
1838 /* We want to clear implicit orders, but we don't want to make them
1839 * dummy orders. They should just vanish. Also check the actual order
1840 * type as ot is currently OT_GOTO_STATION. */
1841 if (order
->IsType(OT_IMPLICIT
)) {
1842 order
= order
->next
; // DeleteOrder() invalidates current order
1844 if (order
!= NULL
) goto restart
;
1849 for (const Vehicle
*w
= v
->FirstShared(); w
!= NULL
; w
= w
->NextShared()) {
1850 /* In GUI, simulate by removing the order and adding it back */
1851 InvalidateVehicleOrder(w
, id
| (INVALID_VEH_ORDER_ID
<< 8));
1852 InvalidateVehicleOrder(w
, (INVALID_VEH_ORDER_ID
<< 8) | id
);
1858 OrderBackup::RemoveOrder(type
, destination
);
1862 * Checks if a vehicle has a depot in its order list.
1863 * @return True iff at least one order is a depot order.
1865 bool Vehicle::HasDepotOrder() const
1869 FOR_VEHICLE_ORDERS(this, order
) {
1870 if (order
->IsType(OT_GOTO_DEPOT
)) return true;
1877 * Delete all orders from a vehicle
1878 * @param v Vehicle whose orders to reset
1879 * @param keep_orderlist If true, do not free the order list, only empty it.
1880 * @param reset_order_indices If true, reset cur_implicit_order_index and cur_real_order_index
1881 * and cancel the current full load order (if the vehicle is loading).
1882 * If false, _you_ have to make sure the order indices are valid after
1883 * your messing with them!
1885 void DeleteVehicleOrders(Vehicle
*v
, bool keep_orderlist
, bool reset_order_indices
)
1887 DeleteOrderWarnings(v
);
1889 if (v
->IsOrderListShared()) {
1890 /* Remove ourself from the shared order list. */
1891 v
->RemoveFromShared();
1892 v
->orders
.list
= NULL
;
1893 } else if (v
->orders
.list
!= NULL
) {
1894 /* Remove the orders */
1895 v
->orders
.list
->FreeChain(keep_orderlist
);
1896 if (!keep_orderlist
) v
->orders
.list
= NULL
;
1899 if (reset_order_indices
) {
1900 v
->cur_implicit_order_index
= v
->cur_real_order_index
= 0;
1901 if (v
->current_order
.IsType(OT_LOADING
)) {
1902 CancelLoadingDueToDeletedOrder(v
);
1908 * Clamp the service interval to the correct min/max. The actual min/max values
1909 * depend on whether it's in percent or days.
1910 * @param interval proposed service interval
1911 * @param company_id the owner of the vehicle
1912 * @return Clamped service interval
1914 uint16
GetServiceIntervalClamped(uint interval
, bool ispercent
)
1916 return ispercent
? Clamp(interval
, MIN_SERVINT_PERCENT
, MAX_SERVINT_PERCENT
) : Clamp(interval
, MIN_SERVINT_DAYS
, MAX_SERVINT_DAYS
);
1921 * Check if a vehicle has any valid orders
1923 * @return false if there are no valid orders
1924 * @note Conditional orders are not considered valid destination orders
1927 static bool CheckForValidOrders(const Vehicle
*v
)
1931 FOR_VEHICLE_ORDERS(v
, order
) {
1932 switch (order
->GetType()) {
1933 case OT_GOTO_STATION
:
1935 case OT_GOTO_WAYPOINT
:
1947 * Compare the variable and value based on the given comparator.
1949 static bool OrderConditionCompare(OrderConditionComparator occ
, int variable
, int value
)
1952 case OCC_EQUALS
: return variable
== value
;
1953 case OCC_NOT_EQUALS
: return variable
!= value
;
1954 case OCC_LESS_THAN
: return variable
< value
;
1955 case OCC_LESS_EQUALS
: return variable
<= value
;
1956 case OCC_MORE_THAN
: return variable
> value
;
1957 case OCC_MORE_EQUALS
: return variable
>= value
;
1958 case OCC_IS_TRUE
: return variable
!= 0;
1959 case OCC_IS_FALSE
: return variable
== 0;
1960 default: NOT_REACHED();
1965 * Process a conditional order and determine the next order.
1966 * @param order the order the vehicle currently has
1967 * @param v the vehicle to update
1968 * @return index of next order to jump to, or INVALID_VEH_ORDER_ID to use the next order
1970 VehicleOrderID
ProcessConditionalOrder(const Order
*order
, const Vehicle
*v
)
1972 if (order
->GetType() != OT_CONDITIONAL
) return INVALID_VEH_ORDER_ID
;
1974 bool skip_order
= false;
1975 OrderConditionComparator occ
= order
->GetConditionComparator();
1976 uint16 value
= order
->GetConditionValue();
1978 switch (order
->GetConditionVariable()) {
1979 case OCV_LOAD_PERCENTAGE
: skip_order
= OrderConditionCompare(occ
, CalcPercentVehicleFilled(v
, NULL
), value
); break;
1980 case OCV_RELIABILITY
: skip_order
= OrderConditionCompare(occ
, ToPercent16(v
->reliability
), value
); break;
1981 case OCV_MAX_SPEED
: skip_order
= OrderConditionCompare(occ
, v
->GetDisplayMaxSpeed() * 10 / 16, value
); break;
1982 case OCV_AGE
: skip_order
= OrderConditionCompare(occ
, v
->age
/ DAYS_IN_LEAP_YEAR
, value
); break;
1983 case OCV_REQUIRES_SERVICE
: skip_order
= OrderConditionCompare(occ
, v
->NeedsServicing(), value
); break;
1984 case OCV_UNCONDITIONALLY
: skip_order
= true; break;
1985 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;
1986 default: NOT_REACHED();
1989 return skip_order
? order
->GetConditionSkipToOrder() : (VehicleOrderID
)INVALID_VEH_ORDER_ID
;
1993 * Update the vehicle's destination tile from an order.
1994 * @param order the order the vehicle currently has
1995 * @param v the vehicle to update
1996 * @param conditional_depth the depth (amount of steps) to go with conditional orders. This to prevent infinite loops.
1997 * @param pbs_look_ahead Whether we are forecasting orders for pbs reservations in advance. If true, the order indices must not be modified.
1999 bool UpdateOrderDest(Vehicle
*v
, const Order
*order
, int conditional_depth
, bool pbs_look_ahead
)
2001 if (conditional_depth
> v
->GetNumOrders()) {
2002 v
->current_order
.Free();
2007 switch (order
->GetType()) {
2008 case OT_GOTO_STATION
:
2009 v
->dest_tile
= v
->GetOrderStationLocation(order
->GetDestination());
2013 if ((order
->GetDepotOrderType() & ODTFB_SERVICE
) && !v
->NeedsServicing()) {
2014 assert(!pbs_look_ahead
);
2015 UpdateVehicleTimetable(v
, true);
2016 v
->IncrementRealOrderIndex();
2020 if (v
->current_order
.GetDepotActionType() & ODATFB_NEAREST_DEPOT
) {
2021 /* We need to search for the nearest depot (hangar). */
2023 DestinationID destination
;
2026 if (v
->FindClosestDepot(&location
, &destination
, &reverse
)) {
2027 /* PBS reservations cannot reverse */
2028 if (pbs_look_ahead
&& reverse
) return false;
2030 v
->dest_tile
= location
;
2031 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());
2033 /* If there is no depot in front, reverse automatically (trains only) */
2034 if (v
->type
== VEH_TRAIN
&& reverse
) DoCommand(v
->tile
, v
->index
, 0, DC_EXEC
, CMD_REVERSE_TRAIN_DIRECTION
);
2036 if (v
->type
== VEH_AIRCRAFT
) {
2037 Aircraft
*a
= Aircraft::From(v
);
2038 if (a
->state
== FLYING
&& a
->targetairport
!= destination
) {
2039 /* The aircraft is now heading for a different hangar than the next in the orders */
2040 extern void AircraftNextAirportPos_and_Order(Aircraft
*a
);
2041 AircraftNextAirportPos_and_Order(a
);
2047 /* If there is no depot, we cannot help PBS either. */
2048 if (pbs_look_ahead
) return false;
2050 UpdateVehicleTimetable(v
, true);
2051 v
->IncrementRealOrderIndex();
2053 if (v
->type
!= VEH_AIRCRAFT
) {
2054 v
->dest_tile
= Depot::Get(order
->GetDestination())->xy
;
2060 case OT_GOTO_WAYPOINT
:
2061 v
->dest_tile
= Waypoint::Get(order
->GetDestination())->xy
;
2064 case OT_CONDITIONAL
: {
2065 assert(!pbs_look_ahead
);
2066 VehicleOrderID next_order
= ProcessConditionalOrder(order
, v
);
2067 if (next_order
!= INVALID_VEH_ORDER_ID
) {
2068 /* Jump to next_order. cur_implicit_order_index becomes exactly that order,
2069 * cur_real_order_index might come after next_order. */
2070 UpdateVehicleTimetable(v
, false);
2071 v
->cur_implicit_order_index
= v
->cur_real_order_index
= next_order
;
2072 v
->UpdateRealOrderIndex();
2073 v
->current_order_time
+= v
->GetOrder(v
->cur_real_order_index
)->travel_time
;
2075 /* Disable creation of implicit orders.
2076 * When inserting them we do not know that we would have to make the conditional orders point to them. */
2077 if (v
->IsGroundVehicle()) {
2078 uint16
&gv_flags
= v
->GetGroundVehicleFlags();
2079 SetBit(gv_flags
, GVF_SUPPRESS_IMPLICIT_ORDERS
);
2082 UpdateVehicleTimetable(v
, true);
2083 v
->IncrementRealOrderIndex();
2093 assert(v
->cur_implicit_order_index
< v
->GetNumOrders());
2094 assert(v
->cur_real_order_index
< v
->GetNumOrders());
2096 /* Get the current order */
2097 order
= v
->GetOrder(v
->cur_real_order_index
);
2098 if (order
!= NULL
&& order
->IsType(OT_IMPLICIT
)) {
2099 assert(v
->GetNumManualOrders() == 0);
2103 if (order
== NULL
) {
2104 v
->current_order
.Free();
2109 v
->current_order
= *order
;
2110 return UpdateOrderDest(v
, order
, conditional_depth
+ 1, pbs_look_ahead
);
2114 * Handle the orders of a vehicle and determine the next place
2115 * to go to if needed.
2116 * @param v the vehicle to do this for.
2117 * @return true *if* the vehicle is eligible for reversing
2118 * (basically only when leaving a station).
2120 bool ProcessOrders(Vehicle
*v
)
2122 switch (v
->current_order
.GetType()) {
2124 /* Let a depot order in the orderlist interrupt. */
2125 if (!(v
->current_order
.GetDepotOrderType() & ODTFB_PART_OF_ORDERS
)) return false;
2131 case OT_LEAVESTATION
:
2132 if (v
->type
!= VEH_AIRCRAFT
) return false;
2139 * Reversing because of order change is allowed only just after leaving a
2140 * station (and the difficulty setting to allowed, of course)
2141 * this can be detected because only after OT_LEAVESTATION, current_order
2142 * will be reset to nothing. (That also happens if no order, but in that case
2143 * it won't hit the point in code where may_reverse is checked)
2145 bool may_reverse
= v
->current_order
.IsType(OT_NOTHING
);
2147 /* Check if we've reached a 'via' destination. */
2148 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
)) &&
2149 IsStationTile(v
->tile
) &&
2150 v
->current_order
.GetDestination() == GetStationIndex(v
->tile
)) {
2151 v
->DeleteUnreachedImplicitOrders();
2152 /* We set the last visited station here because we do not want
2153 * the train to stop at this 'via' station if the next order
2154 * is a no-non-stop order; in that case not setting the last
2155 * visited station will cause the vehicle to still stop. */
2156 v
->last_station_visited
= v
->current_order
.GetDestination();
2157 UpdateVehicleTimetable(v
, true);
2158 v
->IncrementImplicitOrderIndex();
2161 /* Get the current order */
2162 assert(v
->cur_implicit_order_index
== 0 || v
->cur_implicit_order_index
< v
->GetNumOrders());
2163 v
->UpdateRealOrderIndex();
2165 const Order
*order
= v
->GetOrder(v
->cur_real_order_index
);
2166 if (order
!= NULL
&& order
->IsType(OT_IMPLICIT
)) {
2167 assert(v
->GetNumManualOrders() == 0);
2171 /* If no order, do nothing. */
2172 if (order
== NULL
|| (v
->type
== VEH_AIRCRAFT
&& !CheckForValidOrders(v
))) {
2173 if (v
->type
== VEH_AIRCRAFT
) {
2174 /* Aircraft do something vastly different here, so handle separately */
2175 extern void HandleMissingAircraftOrders(Aircraft
*v
);
2176 HandleMissingAircraftOrders(Aircraft::From(v
));
2180 v
->current_order
.Free();
2185 /* If it is unchanged, keep it. */
2186 if (order
->Equals(v
->current_order
) && (v
->type
== VEH_AIRCRAFT
|| v
->dest_tile
!= 0) &&
2187 (v
->type
!= VEH_SHIP
|| !order
->IsType(OT_GOTO_STATION
) || Station::Get(order
->GetDestination())->docks
!= NULL
)) {
2191 /* Otherwise set it, and determine the destination tile. */
2192 v
->current_order
= *order
;
2194 InvalidateVehicleOrder(v
, VIWD_MODIFY_ORDERS
);
2205 SetWindowClassesDirty(GetWindowClassForVehicleType(v
->type
));
2209 return UpdateOrderDest(v
, order
) && may_reverse
;
2213 * Check whether the given vehicle should stop at the given station
2214 * based on this order and the non-stop settings.
2215 * @param v the vehicle that might be stopping.
2216 * @param station the station to stop at.
2217 * @return true if the vehicle should stop.
2219 bool Order::ShouldStopAtStation(const Vehicle
*v
, StationID station
) const
2221 bool is_dest_station
= this->IsType(OT_GOTO_STATION
) && this->dest
== station
;
2223 return (!this->IsType(OT_GOTO_DEPOT
) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS
) != 0) &&
2224 v
->last_station_visited
!= station
&& // Do stop only when we've not just been there
2225 /* Finally do stop when there is no non-stop flag set for this type of station. */
2226 !(this->GetNonStopType() & (is_dest_station
? ONSF_NO_STOP_AT_DESTINATION_STATION
: ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
));
2229 bool Order::CanLoadOrUnload() const
2231 return (this->IsType(OT_GOTO_STATION
) || this->IsType(OT_IMPLICIT
)) &&
2232 (this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
) == 0 &&
2233 ((this->GetLoadType() & OLFB_NO_LOAD
) == 0 ||
2234 (this->GetUnloadType() & OUFB_NO_UNLOAD
) == 0);
2238 * A vehicle can leave the current station with cargo if:
2239 * 1. it can load cargo here OR
2240 * 2a. it could leave the last station with cargo AND
2241 * 2b. it doesn't have to unload all cargo here.
2243 bool Order::CanLeaveWithCargo(bool has_cargo
) const
2245 return (this->GetLoadType() & OLFB_NO_LOAD
) == 0 || (has_cargo
&&
2246 (this->GetUnloadType() & (OUFB_UNLOAD
| OUFB_TRANSFER
)) == 0);