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"
30 #include "cheat_type.h"
32 #include "table/strings.h"
34 #include "safeguards.h"
36 /* DestinationID must be at least as large as every these below, because it can
39 assert_compile(sizeof(DestinationID
) >= sizeof(DepotID
));
40 assert_compile(sizeof(DestinationID
) >= sizeof(StationID
));
42 OrderPool
_order_pool("Order");
43 INSTANTIATE_POOL_METHODS(Order
)
44 OrderListPool
_orderlist_pool("OrderList");
45 INSTANTIATE_POOL_METHODS(OrderList
)
47 /** Clean everything up. */
50 if (CleaningPool()) return;
52 /* We can visit oil rigs and buoys that are not our own. They will be shown in
53 * the list of stations. So, we need to invalidate that window if needed. */
54 if (this->IsType(OT_GOTO_STATION
) || this->IsType(OT_GOTO_WAYPOINT
)) {
55 BaseStation
*bs
= BaseStation::GetIfValid(this->GetDestination());
56 if (bs
!= NULL
&& bs
->owner
== OWNER_NONE
) InvalidateWindowClassesData(WC_STATION_LIST
, 0);
62 * @note ONLY use on "current_order" vehicle orders!
66 this->type
= OT_NOTHING
;
73 * Makes this order a Go To Station order.
74 * @param destination the station to go to.
76 void Order::MakeGoToStation(StationID destination
)
78 this->type
= OT_GOTO_STATION
;
80 this->dest
= destination
;
84 * Makes this order a Go To Depot order.
85 * @param destination the depot to go to.
86 * @param order is this order a 'default' order, or an overridden vehicle order?
87 * @param non_stop_type how to get to the depot?
88 * @param action what to do in the depot?
89 * @param cargo the cargo type to change to.
91 void Order::MakeGoToDepot(DepotID destination
, OrderDepotTypeFlags order
, OrderNonStopFlags non_stop_type
, OrderDepotActionFlags action
, CargoID cargo
)
93 this->type
= OT_GOTO_DEPOT
;
94 this->SetDepotOrderType(order
);
95 this->SetDepotActionType(action
);
96 this->SetNonStopType(non_stop_type
);
97 this->dest
= destination
;
98 this->SetRefit(cargo
);
102 * Makes this order a Go To Waypoint order.
103 * @param destination the waypoint to go to.
105 void Order::MakeGoToWaypoint(StationID destination
)
107 this->type
= OT_GOTO_WAYPOINT
;
109 this->dest
= destination
;
113 * Makes this order a Loading order.
114 * @param ordered is this an ordered stop?
116 void Order::MakeLoading(bool ordered
)
118 this->type
= OT_LOADING
;
119 if (!ordered
) this->flags
= 0;
123 * Makes this order a Leave Station order.
125 void Order::MakeLeaveStation()
127 this->type
= OT_LEAVESTATION
;
132 * Makes this order a Dummy order.
134 void Order::MakeDummy()
136 this->type
= OT_DUMMY
;
141 * Makes this order an conditional order.
142 * @param order the order to jump to.
144 void Order::MakeConditional(VehicleOrderID order
)
146 this->type
= OT_CONDITIONAL
;
152 * Makes this order an implicit order.
153 * @param destination the station to go to.
155 void Order::MakeImplicit(StationID destination
)
157 this->type
= OT_IMPLICIT
;
158 this->dest
= destination
;
162 * Make this depot/station order also a refit order.
163 * @param cargo the cargo type to change to.
164 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
166 void Order::SetRefit(CargoID cargo
)
168 this->refit_cargo
= cargo
;
172 * Does this order have the same type, flags and destination?
173 * @param other the second order to compare to.
174 * @return true if the type, flags and destination match.
176 bool Order::Equals(const Order
&other
) const
178 /* In case of go to nearest depot orders we need "only" compare the flags
179 * with the other and not the nearest depot order bit or the actual
180 * destination because those get clear/filled in during the order
181 * evaluation. If we do not do this the order will continuously be seen as
182 * a different order and it will try to find a "nearest depot" every tick. */
183 if ((this->IsType(OT_GOTO_DEPOT
) && this->type
== other
.type
) &&
184 ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0 ||
185 (other
.GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0)) {
186 return this->GetDepotOrderType() == other
.GetDepotOrderType() &&
187 (this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT
) == (other
.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT
);
190 return this->type
== other
.type
&& this->flags
== other
.flags
&& this->dest
== other
.dest
;
194 * Pack this order into a 32 bits integer, or actually only
195 * the type, flags and destination.
196 * @return the packed representation.
197 * @note unpacking is done in the constructor.
199 uint32
Order::Pack() const
201 return this->dest
<< 16 | this->flags
<< 8 | this->type
;
205 * Pack this order into a 16 bits integer as close to the TTD
206 * representation as possible.
207 * @return the TTD-like packed representation.
209 uint16
Order::MapOldOrder() const
211 uint16 order
= this->GetType();
212 switch (this->type
) {
213 case OT_GOTO_STATION
:
214 if (this->GetUnloadType() & OUFB_UNLOAD
) SetBit(order
, 5);
215 if (this->GetLoadType() & OLFB_FULL_LOAD
) SetBit(order
, 6);
216 if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
) SetBit(order
, 7);
217 order
|= GB(this->GetDestination(), 0, 8) << 8;
220 if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS
)) SetBit(order
, 6);
222 order
|= GB(this->GetDestination(), 0, 8) << 8;
225 if (this->GetLoadType() & OLFB_FULL_LOAD
) SetBit(order
, 6);
232 * Create an order based on a packed representation of that order.
233 * @param packed the packed representation.
235 Order::Order(uint32 packed
)
237 this->type
= (OrderType
)GB(packed
, 0, 8);
238 this->flags
= GB(packed
, 8, 8);
239 this->dest
= GB(packed
, 16, 16);
241 this->refit_cargo
= CT_NO_REFIT
;
243 this->travel_time
= 0;
244 this->max_speed
= UINT16_MAX
;
249 * Updates the widgets of a vehicle which contains the order-data
252 void InvalidateVehicleOrder(const Vehicle
*v
, int data
)
254 SetWindowDirty(WC_VEHICLE_VIEW
, v
->index
);
257 /* Calls SetDirty() too */
258 InvalidateWindowData(WC_VEHICLE_ORDERS
, v
->index
, data
);
259 InvalidateWindowData(WC_VEHICLE_TIMETABLE
, v
->index
, data
);
263 SetWindowDirty(WC_VEHICLE_ORDERS
, v
->index
);
264 SetWindowDirty(WC_VEHICLE_TIMETABLE
, v
->index
);
269 * Assign data to an order (from another order)
270 * This function makes sure that the index is maintained correctly
271 * @param other the data to copy (except next pointer).
274 void Order::AssignOrder(const Order
&other
)
276 this->type
= other
.type
;
277 this->flags
= other
.flags
;
278 this->dest
= other
.dest
;
280 this->refit_cargo
= other
.refit_cargo
;
282 this->wait_time
= other
.wait_time
;
283 this->travel_time
= other
.travel_time
;
284 this->max_speed
= other
.max_speed
;
288 * Recomputes everything.
289 * @param chain first order in the chain
290 * @param v one of vehicle that is using this orderlist
292 void OrderList::Initialize(Order
*chain
, Vehicle
*v
)
295 this->first_shared
= v
;
297 this->num_orders
= 0;
298 this->num_manual_orders
= 0;
299 this->num_vehicles
= 1;
300 this->timetable_duration
= 0;
301 this->total_duration
= 0;
303 for (Order
*o
= this->first
; o
!= NULL
; o
= o
->next
) {
305 if (!o
->IsType(OT_IMPLICIT
)) ++this->num_manual_orders
;
306 this->timetable_duration
+= o
->GetTimetabledWait() + o
->GetTimetabledTravel();
307 this->total_duration
+= o
->GetWaitTime() + o
->GetTravelTime();
310 for (Vehicle
*u
= this->first_shared
->PreviousShared(); u
!= NULL
; u
= u
->PreviousShared()) {
311 ++this->num_vehicles
;
312 this->first_shared
= u
;
315 for (const Vehicle
*u
= v
->NextShared(); u
!= NULL
; u
= u
->NextShared()) ++this->num_vehicles
;
319 * Free a complete order chain.
320 * @param keep_orderlist If this is true only delete the orders, otherwise also delete the OrderList.
321 * @note do not use on "current_order" vehicle orders!
323 void OrderList::FreeChain(bool keep_orderlist
)
326 for (Order
*o
= this->first
; o
!= NULL
; o
= next
) {
331 if (keep_orderlist
) {
333 this->num_orders
= 0;
334 this->num_manual_orders
= 0;
335 this->timetable_duration
= 0;
342 * Get a certain order of the order chain.
343 * @param index zero-based index of the order within the chain.
344 * @return the order at position index.
346 Order
*OrderList::GetOrderAt(int index
) const
348 if (index
< 0) return NULL
;
350 Order
*order
= this->first
;
352 while (order
!= NULL
&& index
-- > 0) {
359 * Get the next order which will make the given vehicle stop at a station
360 * or refit at a depot or evaluate a non-trivial condition.
361 * @param next The order to start looking at.
362 * @param hops The number of orders we have already looked at.
364 * \li a station order
365 * \li a refitting depot order
366 * \li a non-trivial conditional order
367 * \li NULL if the vehicle won't stop anymore.
369 const Order
*OrderList::GetNextDecisionNode(const Order
*next
, uint hops
) const
371 if (hops
> this->GetNumOrders() || next
== NULL
) return NULL
;
373 if (next
->IsType(OT_CONDITIONAL
)) {
374 if (next
->GetConditionVariable() != OCV_UNCONDITIONALLY
) return next
;
376 /* We can evaluate trivial conditions right away. They're conceptually
377 * the same as regular order progression. */
378 return this->GetNextDecisionNode(
379 this->GetOrderAt(next
->GetConditionSkipToOrder()),
383 if (next
->IsType(OT_GOTO_DEPOT
)) {
384 if (next
->GetDepotActionType() == ODATFB_HALT
) return NULL
;
385 if (next
->IsRefit()) return next
;
388 if (!next
->CanLoadOrUnload()) {
389 return this->GetNextDecisionNode(this->GetNext(next
), hops
+ 1);
396 * Recursively determine the next deterministic station to stop at.
397 * @param v The vehicle we're looking at.
398 * @param first Order to start searching at or NULL to start at cur_implicit_order_index + 1.
399 * @param hops Number of orders we have already looked at.
400 * @return Next stoppping station or INVALID_STATION.
401 * @pre The vehicle is currently loading and v->last_station_visited is meaningful.
402 * @note This function may draw a random number. Don't use it from the GUI.
404 StationIDStack
OrderList::GetNextStoppingStation(const Vehicle
*v
, const Order
*first
, uint hops
) const
407 const Order
*next
= first
;
409 next
= this->GetOrderAt(v
->cur_implicit_order_index
);
411 next
= this->GetFirstOrder();
412 if (next
== NULL
) return INVALID_STATION
;
414 /* GetNext never returns NULL if there is a valid station in the list.
415 * As the given "next" is already valid and a station in the list, we
416 * don't have to check for NULL here. */
417 next
= this->GetNext(next
);
418 assert(next
!= NULL
);
423 next
= this->GetNextDecisionNode(next
, ++hops
);
425 /* Resolve possibly nested conditionals by estimation. */
426 while (next
!= NULL
&& next
->IsType(OT_CONDITIONAL
)) {
427 /* We return both options of conditional orders. */
428 const Order
*skip_to
= this->GetNextDecisionNode(
429 this->GetOrderAt(next
->GetConditionSkipToOrder()), hops
);
430 const Order
*advance
= this->GetNextDecisionNode(
431 this->GetNext(next
), hops
);
432 if (advance
== NULL
|| advance
== first
|| skip_to
== advance
) {
433 next
= (skip_to
== first
) ? NULL
: skip_to
;
434 } else if (skip_to
== NULL
|| skip_to
== first
) {
435 next
= (advance
== first
) ? NULL
: advance
;
437 StationIDStack st1
= this->GetNextStoppingStation(v
, skip_to
, hops
);
438 StationIDStack st2
= this->GetNextStoppingStation(v
, advance
, hops
);
439 while (!st2
.IsEmpty()) st1
.Push(st2
.Pop());
445 /* Don't return a next stop if the vehicle has to unload everything. */
446 if (next
== NULL
|| ((next
->IsType(OT_GOTO_STATION
) || next
->IsType(OT_IMPLICIT
)) &&
447 next
->GetDestination() == v
->last_station_visited
&&
448 (next
->GetUnloadType() & (OUFB_TRANSFER
| OUFB_UNLOAD
)) != 0)) {
449 return INVALID_STATION
;
451 } while (next
->IsType(OT_GOTO_DEPOT
) || next
->GetDestination() == v
->last_station_visited
);
453 return next
->GetDestination();
457 * Insert a new order into the order chain.
458 * @param new_order is the order to insert into the chain.
459 * @param index is the position where the order is supposed to be inserted.
461 void OrderList::InsertOrderAt(Order
*new_order
, int index
)
463 if (this->first
== NULL
) {
464 this->first
= new_order
;
467 /* Insert as first or only order */
468 new_order
->next
= this->first
;
469 this->first
= new_order
;
470 } else if (index
>= this->num_orders
) {
471 /* index is after the last order, add it to the end */
472 this->GetLastOrder()->next
= new_order
;
474 /* Put the new order in between */
475 Order
*order
= this->GetOrderAt(index
- 1);
476 new_order
->next
= order
->next
;
477 order
->next
= new_order
;
481 if (!new_order
->IsType(OT_IMPLICIT
)) ++this->num_manual_orders
;
482 this->timetable_duration
+= new_order
->GetTimetabledWait() + new_order
->GetTimetabledTravel();
483 this->total_duration
+= new_order
->GetWaitTime() + new_order
->GetTravelTime();
485 /* We can visit oil rigs and buoys that are not our own. They will be shown in
486 * the list of stations. So, we need to invalidate that window if needed. */
487 if (new_order
->IsType(OT_GOTO_STATION
) || new_order
->IsType(OT_GOTO_WAYPOINT
)) {
488 BaseStation
*bs
= BaseStation::Get(new_order
->GetDestination());
489 if (bs
->owner
== OWNER_NONE
) InvalidateWindowClassesData(WC_STATION_LIST
, 0);
496 * Remove an order from the order list and delete it.
497 * @param index is the position of the order which is to be deleted.
499 void OrderList::DeleteOrderAt(int index
)
501 if (index
>= this->num_orders
) return;
506 to_remove
= this->first
;
507 this->first
= to_remove
->next
;
509 Order
*prev
= GetOrderAt(index
- 1);
510 to_remove
= prev
->next
;
511 prev
->next
= to_remove
->next
;
514 if (!to_remove
->IsType(OT_IMPLICIT
)) --this->num_manual_orders
;
515 this->timetable_duration
-= (to_remove
->GetTimetabledWait() + to_remove
->GetTimetabledTravel());
516 this->total_duration
-= (to_remove
->GetWaitTime() + to_remove
->GetTravelTime());
521 * Move an order to another position within the order list.
522 * @param from is the zero-based position of the order to move.
523 * @param to is the zero-based position where the order is moved to.
525 void OrderList::MoveOrder(int from
, int to
)
527 if (from
>= this->num_orders
|| to
>= this->num_orders
|| from
== to
) return;
531 /* Take the moving order out of the pointer-chain */
533 moving_one
= this->first
;
534 this->first
= moving_one
->next
;
536 Order
*one_before
= GetOrderAt(from
- 1);
537 moving_one
= one_before
->next
;
538 one_before
->next
= moving_one
->next
;
541 /* Insert the moving_order again in the pointer-chain */
543 moving_one
->next
= this->first
;
544 this->first
= moving_one
;
546 Order
*one_before
= GetOrderAt(to
- 1);
547 moving_one
->next
= one_before
->next
;
548 one_before
->next
= moving_one
;
553 * Removes the vehicle from the shared order list.
554 * @note This is supposed to be called when the vehicle is still in the chain
555 * @param v vehicle to remove from the list
557 void OrderList::RemoveVehicle(Vehicle
*v
)
559 --this->num_vehicles
;
560 if (v
== this->first_shared
) this->first_shared
= v
->NextShared();
564 * Checks whether a vehicle is part of the shared vehicle chain.
565 * @param v is the vehicle to search in the shared vehicle chain.
567 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle
*v
) const
569 for (const Vehicle
*v_shared
= this->first_shared
; v_shared
!= NULL
; v_shared
= v_shared
->NextShared()) {
570 if (v_shared
== v
) return true;
577 * Gets the position of the given vehicle within the shared order vehicle list.
578 * @param v is the vehicle of which to get the position
579 * @return position of v within the shared vehicle chain.
581 int OrderList::GetPositionInSharedOrderList(const Vehicle
*v
) const
584 for (const Vehicle
*v_shared
= v
->PreviousShared(); v_shared
!= NULL
; v_shared
= v_shared
->PreviousShared()) count
++;
589 * Checks whether all orders of the list have a filled timetable.
590 * @return whether all orders have a filled timetable.
592 bool OrderList::IsCompleteTimetable() const
594 for (Order
*o
= this->first
; o
!= NULL
; o
= o
->next
) {
595 /* Implicit orders are, by definition, not timetabled. */
596 if (o
->IsType(OT_IMPLICIT
)) continue;
597 if (!o
->IsCompletelyTimetabled()) return false;
603 * Checks for internal consistency of order list. Triggers assertion if something is wrong.
605 void OrderList::DebugCheckSanity() const
607 VehicleOrderID check_num_orders
= 0;
608 VehicleOrderID check_num_manual_orders
= 0;
609 uint check_num_vehicles
= 0;
610 Ticks check_timetable_duration
= 0;
611 Ticks check_total_duration
= 0;
613 DEBUG(misc
, 6, "Checking OrderList %hu for sanity...", this->index
);
615 for (const Order
*o
= this->first
; o
!= NULL
; o
= o
->next
) {
617 if (!o
->IsType(OT_IMPLICIT
)) ++check_num_manual_orders
;
618 check_timetable_duration
+= o
->GetTimetabledWait() + o
->GetTimetabledTravel();
619 check_total_duration
+= o
->GetWaitTime() + o
->GetTravelTime();
621 assert(this->num_orders
== check_num_orders
);
622 assert(this->num_manual_orders
== check_num_manual_orders
);
623 assert(this->timetable_duration
== check_timetable_duration
);
624 assert(this->total_duration
== check_total_duration
);
626 for (const Vehicle
*v
= this->first_shared
; v
!= NULL
; v
= v
->NextShared()) {
627 ++check_num_vehicles
;
628 assert(v
->orders
.list
== this);
630 assert(this->num_vehicles
== check_num_vehicles
);
631 DEBUG(misc
, 6, "... detected %u orders (%u manual), %u vehicles, %i timetabled, %i total",
632 (uint
)this->num_orders
, (uint
)this->num_manual_orders
,
633 this->num_vehicles
, this->timetable_duration
, this->total_duration
);
637 * Checks whether the order goes to a station or not, i.e. whether the
638 * destination is a station
639 * @param v the vehicle to check for
640 * @param o the order to check
641 * @return true if the destination is a station
643 static inline bool OrderGoesToStation(const Vehicle
*v
, const Order
*o
)
645 return o
->IsType(OT_GOTO_STATION
) ||
646 (v
->type
== VEH_AIRCRAFT
&& o
->IsType(OT_GOTO_DEPOT
) && !(o
->GetDepotActionType() & ODATFB_NEAREST_DEPOT
));
650 * Delete all news items regarding defective orders about a vehicle
651 * This could kill still valid warnings (for example about void order when just
652 * another order gets added), but assume the company will notice the problems,
653 * when (s)he's changing the orders.
655 static void DeleteOrderWarnings(const Vehicle
*v
)
657 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS
);
658 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_VOID_ORDER
);
659 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY
);
660 DeleteVehicleNews(v
->index
, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY
);
661 DeleteVehicleNews(v
->index
, STR_NEWS_PLANE_USES_TOO_SHORT_RUNWAY
);
665 * Returns a tile somewhat representing the order destination (not suitable for pathfinding).
666 * @param v The vehicle to get the location for.
667 * @param airport Get the airport tile and not the station location for aircraft.
668 * @return destination of order, or INVALID_TILE if none.
670 TileIndex
Order::GetLocation(const Vehicle
*v
, bool airport
) const
672 switch (this->GetType()) {
673 case OT_GOTO_WAYPOINT
:
674 case OT_GOTO_STATION
:
676 if (airport
&& v
->type
== VEH_AIRCRAFT
) return Station::Get(this->GetDestination())->airport
.tile
;
677 return BaseStation::Get(this->GetDestination())->xy
;
680 if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0) return INVALID_TILE
;
681 return (v
->type
== VEH_AIRCRAFT
) ? Station::Get(this->GetDestination())->xy
: Depot::Get(this->GetDestination())->xy
;
689 * Get the distance between two orders of a vehicle. Conditional orders are resolved
690 * and the bigger distance of the two order branches is returned.
691 * @param prev Origin order.
692 * @param cur Destination order.
693 * @param v The vehicle to get the distance for.
694 * @param conditional_depth Internal param for resolving conditional orders.
695 * @return Maximum distance between the two orders.
697 uint
GetOrderDistance(const Order
*prev
, const Order
*cur
, const Vehicle
*v
, int conditional_depth
)
699 if (cur
->IsType(OT_CONDITIONAL
)) {
700 if (conditional_depth
> v
->GetNumOrders()) return 0;
704 int dist1
= GetOrderDistance(prev
, v
->GetOrder(cur
->GetConditionSkipToOrder()), v
, conditional_depth
);
705 int dist2
= GetOrderDistance(prev
, cur
->next
== NULL
? v
->orders
.list
->GetFirstOrder() : cur
->next
, v
, conditional_depth
);
706 return max(dist1
, dist2
);
709 TileIndex prev_tile
= prev
->GetLocation(v
, true);
710 TileIndex cur_tile
= cur
->GetLocation(v
, true);
711 if (prev_tile
== INVALID_TILE
|| cur_tile
== INVALID_TILE
) return 0;
712 return v
->type
== VEH_AIRCRAFT
? DistanceSquare(prev_tile
, cur_tile
) : DistanceManhattan(prev_tile
, cur_tile
);
716 * Add an order to the orderlist of a vehicle.
718 * @param flags operation to perform
719 * @param p1 various bitstuffed elements
720 * - p1 = (bit 0 - 19) - ID of the vehicle
721 * - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
722 * the order will be inserted before that one
723 * the maximum vehicle order id is 254.
724 * @param p2 packed order to insert
726 * @return the cost of this operation or an error
728 CommandCost
CmdInsertOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
730 VehicleID veh
= GB(p1
, 0, 20);
731 VehicleOrderID sel_ord
= GB(p1
, 20, 8);
734 Vehicle
*v
= Vehicle::GetIfValid(veh
);
735 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
737 CommandCost ret
= CheckOwnership(v
->owner
);
738 if (ret
.Failed()) return ret
;
740 /* Check if the inserted order is to the correct destination (owner, type),
741 * and has the correct flags if any */
742 switch (new_order
.GetType()) {
743 case OT_GOTO_STATION
: {
744 const Station
*st
= Station::GetIfValid(new_order
.GetDestination());
745 if (st
== NULL
) return CMD_ERROR
;
747 if (st
->owner
!= OWNER_NONE
) {
748 CommandCost ret
= CheckOwnership(st
->owner
);
749 if (ret
.Failed()) return ret
;
752 if (!CanVehicleUseStation(v
, st
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER
);
753 for (Vehicle
*u
= v
->FirstShared(); u
!= NULL
; u
= u
->NextShared()) {
754 if (!CanVehicleUseStation(u
, st
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED
);
757 /* Non stop only allowed for ground vehicles. */
758 if (new_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
&& !v
->IsGroundVehicle()) return CMD_ERROR
;
760 /* Filter invalid load/unload types. */
761 switch (new_order
.GetLoadType()) {
762 case OLF_LOAD_IF_POSSIBLE
: case OLFB_FULL_LOAD
: case OLF_FULL_LOAD_ANY
: case OLFB_NO_LOAD
: break;
763 default: return CMD_ERROR
;
765 switch (new_order
.GetUnloadType()) {
766 case OUF_UNLOAD_IF_POSSIBLE
: case OUFB_UNLOAD
: case OUFB_TRANSFER
: case OUFB_NO_UNLOAD
: break;
767 default: return CMD_ERROR
;
770 /* Filter invalid stop locations */
771 switch (new_order
.GetStopLocation()) {
772 case OSL_PLATFORM_NEAR_END
:
773 case OSL_PLATFORM_MIDDLE
:
774 if (v
->type
!= VEH_TRAIN
) return CMD_ERROR
;
776 case OSL_PLATFORM_FAR_END
:
786 case OT_GOTO_DEPOT
: {
787 if ((new_order
.GetDepotActionType() & ODATFB_NEAREST_DEPOT
) == 0) {
788 if (v
->type
== VEH_AIRCRAFT
) {
789 const Station
*st
= Station::GetIfValid(new_order
.GetDestination());
791 if (st
== NULL
) return CMD_ERROR
;
793 CommandCost ret
= CheckOwnership(st
->owner
);
794 if (ret
.Failed()) return ret
;
796 if (!CanVehicleUseStation(v
, st
) || !st
->airport
.HasHangar()) {
800 const Depot
*dp
= Depot::GetIfValid(new_order
.GetDestination());
802 if (dp
== NULL
) return CMD_ERROR
;
804 CommandCost ret
= CheckOwnership(GetTileOwner(dp
->xy
));
805 if (ret
.Failed()) return ret
;
809 if (!IsRailDepotTile(dp
->xy
)) return CMD_ERROR
;
813 if (!IsRoadDepotTile(dp
->xy
)) return CMD_ERROR
;
817 if (!IsShipDepotTile(dp
->xy
)) return CMD_ERROR
;
820 default: return CMD_ERROR
;
825 if (new_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
&& !v
->IsGroundVehicle()) return CMD_ERROR
;
826 if (new_order
.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS
| ((new_order
.GetDepotOrderType() & ODTFB_PART_OF_ORDERS
) != 0 ? ODTFB_SERVICE
: 0))) return CMD_ERROR
;
827 if (new_order
.GetDepotActionType() & ~(ODATFB_HALT
| ODATFB_NEAREST_DEPOT
)) return CMD_ERROR
;
828 if ((new_order
.GetDepotOrderType() & ODTFB_SERVICE
) && (new_order
.GetDepotActionType() & ODATFB_HALT
)) return CMD_ERROR
;
832 case OT_GOTO_WAYPOINT
: {
833 const Waypoint
*wp
= Waypoint::GetIfValid(new_order
.GetDestination());
834 if (wp
== NULL
) return CMD_ERROR
;
837 default: return CMD_ERROR
;
840 if (!(wp
->facilities
& FACIL_TRAIN
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER
);
842 CommandCost ret
= CheckOwnership(wp
->owner
);
843 if (ret
.Failed()) return ret
;
848 if (!(wp
->facilities
& FACIL_DOCK
)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER
);
849 if (wp
->owner
!= OWNER_NONE
) {
850 CommandCost ret
= CheckOwnership(wp
->owner
);
851 if (ret
.Failed()) return ret
;
856 /* Order flags can be any of the following for waypoints:
858 * non-stop orders (if any) are only valid for trains */
859 if (new_order
.GetNonStopType() != ONSF_STOP_EVERYWHERE
&& v
->type
!= VEH_TRAIN
) return CMD_ERROR
;
863 case OT_CONDITIONAL
: {
864 VehicleOrderID skip_to
= new_order
.GetConditionSkipToOrder();
865 if (skip_to
!= 0 && skip_to
>= v
->GetNumOrders()) return CMD_ERROR
; // Always allow jumping to the first (even when there is no order).
866 if (new_order
.GetConditionVariable() >= OCV_END
) return CMD_ERROR
;
868 OrderConditionComparator occ
= new_order
.GetConditionComparator();
869 if (occ
>= OCC_END
) return CMD_ERROR
;
870 switch (new_order
.GetConditionVariable()) {
871 case OCV_REQUIRES_SERVICE
:
872 if (occ
!= OCC_IS_TRUE
&& occ
!= OCC_IS_FALSE
) return CMD_ERROR
;
875 case OCV_UNCONDITIONALLY
:
876 if (occ
!= OCC_EQUALS
) return CMD_ERROR
;
877 if (new_order
.GetConditionValue() != 0) return CMD_ERROR
;
880 case OCV_LOAD_PERCENTAGE
:
881 case OCV_RELIABILITY
:
882 if (new_order
.GetConditionValue() > 100) return CMD_ERROR
;
885 if (occ
== OCC_IS_TRUE
|| occ
== OCC_IS_FALSE
) return CMD_ERROR
;
891 default: return CMD_ERROR
;
894 if (sel_ord
> v
->GetNumOrders()) return CMD_ERROR
;
896 if (v
->GetNumOrders() >= MAX_VEH_ORDER_ID
) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS
);
897 if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
898 if (v
->orders
.list
== NULL
&& !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
900 if (v
->type
== VEH_SHIP
&& _settings_game
.pf
.pathfinder_for_ships
!= VPF_NPF
) {
901 /* Make sure the new destination is not too far away from the previous */
902 const Order
*prev
= NULL
;
905 /* Find the last goto station or depot order before the insert location.
906 * If the order is to be inserted at the beginning of the order list this
907 * finds the last order in the list. */
909 FOR_VEHICLE_ORDERS(v
, o
) {
910 switch (o
->GetType()) {
911 case OT_GOTO_STATION
:
913 case OT_GOTO_WAYPOINT
:
919 if (++n
== sel_ord
&& prev
!= NULL
) break;
923 if (new_order
.IsType(OT_CONDITIONAL
)) {
924 /* The order is not yet inserted, so we have to do the first iteration here. */
925 dist
= GetOrderDistance(prev
, v
->GetOrder(new_order
.GetConditionSkipToOrder()), v
);
927 dist
= GetOrderDistance(prev
, &new_order
, v
);
931 return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION
);
936 if (flags
& DC_EXEC
) {
937 Order
*new_o
= new Order();
938 new_o
->AssignOrder(new_order
);
939 InsertOrder(v
, new_o
, sel_ord
);
942 return CommandCost();
946 * Insert a new order but skip the validation.
947 * @param v The vehicle to insert the order to.
948 * @param new_o The new order.
949 * @param sel_ord The position the order should be inserted at.
951 void InsertOrder(Vehicle
*v
, Order
*new_o
, VehicleOrderID sel_ord
)
953 /* Create new order and link in list */
954 if (v
->orders
.list
== NULL
) {
955 v
->orders
.list
= new OrderList(new_o
, v
);
957 v
->orders
.list
->InsertOrderAt(new_o
, sel_ord
);
960 Vehicle
*u
= v
->FirstShared();
961 DeleteOrderWarnings(u
);
962 for (; u
!= NULL
; u
= u
->NextShared()) {
963 assert(v
->orders
.list
== u
->orders
.list
);
965 /* If there is added an order before the current one, we need
966 * to update the selected order. We do not change implicit/real order indices though.
967 * If the new order is between the current implicit order and real order, the implicit order will
968 * later skip the inserted order. */
969 if (sel_ord
<= u
->cur_real_order_index
) {
970 uint cur
= u
->cur_real_order_index
+ 1;
971 /* Check if we don't go out of bound */
972 if (cur
< u
->GetNumOrders()) {
973 u
->cur_real_order_index
= cur
;
976 if (sel_ord
== u
->cur_implicit_order_index
&& u
->IsGroundVehicle()) {
977 /* We are inserting an order just before the current implicit order.
978 * We do not know whether we will reach current implicit or the newly inserted order first.
979 * So, disable creation of implicit orders until we are on track again. */
980 uint16
&gv_flags
= u
->GetGroundVehicleFlags();
981 SetBit(gv_flags
, GVF_SUPPRESS_IMPLICIT_ORDERS
);
983 if (sel_ord
<= u
->cur_implicit_order_index
) {
984 uint cur
= u
->cur_implicit_order_index
+ 1;
985 /* Check if we don't go out of bound */
986 if (cur
< u
->GetNumOrders()) {
987 u
->cur_implicit_order_index
= cur
;
990 /* Update any possible open window of the vehicle */
991 InvalidateVehicleOrder(u
, INVALID_VEH_ORDER_ID
| (sel_ord
<< 8));
994 /* As we insert an order, the order to skip to will be 'wrong'. */
995 VehicleOrderID cur_order_id
= 0;
997 FOR_VEHICLE_ORDERS(v
, order
) {
998 if (order
->IsType(OT_CONDITIONAL
)) {
999 VehicleOrderID order_id
= order
->GetConditionSkipToOrder();
1000 if (order_id
>= sel_ord
) {
1001 order
->SetConditionSkipToOrder(order_id
+ 1);
1003 if (order_id
== cur_order_id
) {
1004 order
->SetConditionSkipToOrder((order_id
+ 1) % v
->GetNumOrders());
1010 /* Make sure to rebuild the whole list */
1011 InvalidateWindowClassesData(GetWindowClassForVehicleType(v
->type
), 0);
1015 * Declone an order-list
1016 * @param *dst delete the orders of this vehicle
1017 * @param flags execution flags
1019 static CommandCost
DecloneOrder(Vehicle
*dst
, DoCommandFlag flags
)
1021 if (flags
& DC_EXEC
) {
1022 DeleteVehicleOrders(dst
);
1023 InvalidateVehicleOrder(dst
, VIWD_REMOVE_ALL_ORDERS
);
1024 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst
->type
), 0);
1026 return CommandCost();
1030 * Delete an order from the orderlist of a vehicle.
1031 * @param tile unused
1032 * @param flags operation to perform
1033 * @param p1 the ID of the vehicle
1034 * @param p2 the order to delete (max 255)
1035 * @param text unused
1036 * @return the cost of this operation or an error
1038 CommandCost
CmdDeleteOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1040 VehicleID veh_id
= GB(p1
, 0, 20);
1041 VehicleOrderID sel_ord
= GB(p2
, 0, 8);
1043 Vehicle
*v
= Vehicle::GetIfValid(veh_id
);
1045 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1047 CommandCost ret
= CheckOwnership(v
->owner
);
1048 if (ret
.Failed()) return ret
;
1050 /* If we did not select an order, we maybe want to de-clone the orders */
1051 if (sel_ord
>= v
->GetNumOrders()) return DecloneOrder(v
, flags
);
1053 if (v
->GetOrder(sel_ord
) == NULL
) return CMD_ERROR
;
1055 if (flags
& DC_EXEC
) DeleteOrder(v
, sel_ord
);
1056 return CommandCost();
1060 * Cancel the current loading order of the vehicle as the order was deleted.
1061 * @param v the vehicle
1063 static void CancelLoadingDueToDeletedOrder(Vehicle
*v
)
1065 assert(v
->current_order
.IsType(OT_LOADING
));
1066 /* NON-stop flag is misused to see if a train is in a station that is
1067 * on his order list or not */
1068 v
->current_order
.SetNonStopType(ONSF_STOP_EVERYWHERE
);
1069 /* When full loading, "cancel" that order so the vehicle doesn't
1070 * stay indefinitely at this station anymore. */
1071 if (v
->current_order
.GetLoadType() & OLFB_FULL_LOAD
) v
->current_order
.SetLoadType(OLF_LOAD_IF_POSSIBLE
);
1075 * Delete an order but skip the parameter validation.
1076 * @param v The vehicle to delete the order from.
1077 * @param sel_ord The id of the order to be deleted.
1079 void DeleteOrder(Vehicle
*v
, VehicleOrderID sel_ord
)
1081 v
->orders
.list
->DeleteOrderAt(sel_ord
);
1083 Vehicle
*u
= v
->FirstShared();
1084 DeleteOrderWarnings(u
);
1085 for (; u
!= NULL
; u
= u
->NextShared()) {
1086 assert(v
->orders
.list
== u
->orders
.list
);
1088 if (sel_ord
== u
->cur_real_order_index
&& u
->current_order
.IsType(OT_LOADING
)) {
1089 CancelLoadingDueToDeletedOrder(u
);
1092 if (sel_ord
< u
->cur_real_order_index
) {
1093 u
->cur_real_order_index
--;
1094 } else if (sel_ord
== u
->cur_real_order_index
) {
1095 u
->UpdateRealOrderIndex();
1098 if (sel_ord
< u
->cur_implicit_order_index
) {
1099 u
->cur_implicit_order_index
--;
1100 } else if (sel_ord
== u
->cur_implicit_order_index
) {
1101 /* Make sure the index is valid */
1102 if (u
->cur_implicit_order_index
>= u
->GetNumOrders()) u
->cur_implicit_order_index
= 0;
1104 /* Skip non-implicit orders for the implicit-order-index (e.g. if the current implicit order was deleted */
1105 while (u
->cur_implicit_order_index
!= u
->cur_real_order_index
&& !u
->GetOrder(u
->cur_implicit_order_index
)->IsType(OT_IMPLICIT
)) {
1106 u
->cur_implicit_order_index
++;
1107 if (u
->cur_implicit_order_index
>= u
->GetNumOrders()) u
->cur_implicit_order_index
= 0;
1111 /* Update any possible open window of the vehicle */
1112 InvalidateVehicleOrder(u
, sel_ord
| (INVALID_VEH_ORDER_ID
<< 8));
1115 /* As we delete an order, the order to skip to will be 'wrong'. */
1116 VehicleOrderID cur_order_id
= 0;
1117 Order
*order
= NULL
;
1118 FOR_VEHICLE_ORDERS(v
, order
) {
1119 if (order
->IsType(OT_CONDITIONAL
)) {
1120 VehicleOrderID order_id
= order
->GetConditionSkipToOrder();
1121 if (order_id
>= sel_ord
) {
1122 order_id
= max(order_id
- 1, 0);
1124 if (order_id
== cur_order_id
) {
1125 order_id
= (order_id
+ 1) % v
->GetNumOrders();
1127 order
->SetConditionSkipToOrder(order_id
);
1132 InvalidateWindowClassesData(GetWindowClassForVehicleType(v
->type
), 0);
1136 * Goto order of order-list.
1137 * @param tile unused
1138 * @param flags operation to perform
1139 * @param p1 The ID of the vehicle which order is skipped
1140 * @param p2 the selected order to which we want to skip
1141 * @param text unused
1142 * @return the cost of this operation or an error
1144 CommandCost
CmdSkipToOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1146 VehicleID veh_id
= GB(p1
, 0, 20);
1147 VehicleOrderID sel_ord
= GB(p2
, 0, 8);
1149 Vehicle
*v
= Vehicle::GetIfValid(veh_id
);
1151 if (v
== NULL
|| !v
->IsPrimaryVehicle() || sel_ord
== v
->cur_implicit_order_index
|| sel_ord
>= v
->GetNumOrders() || v
->GetNumOrders() < 2) return CMD_ERROR
;
1153 CommandCost ret
= CheckOwnership(v
->owner
);
1154 if (ret
.Failed()) return ret
;
1156 if (flags
& DC_EXEC
) {
1157 if (v
->current_order
.IsType(OT_LOADING
)) v
->LeaveStation();
1159 v
->cur_implicit_order_index
= v
->cur_real_order_index
= sel_ord
;
1160 v
->UpdateRealOrderIndex();
1162 InvalidateVehicleOrder(v
, VIWD_MODIFY_ORDERS
);
1165 /* We have an aircraft/ship, they have a mini-schedule, so update them all */
1166 if (v
->type
== VEH_AIRCRAFT
) SetWindowClassesDirty(WC_AIRCRAFT_LIST
);
1167 if (v
->type
== VEH_SHIP
) SetWindowClassesDirty(WC_SHIPS_LIST
);
1169 return CommandCost();
1173 * Move an order inside the orderlist
1174 * @param tile unused
1175 * @param flags operation to perform
1176 * @param p1 the ID of the vehicle
1177 * @param p2 order to move and target
1178 * bit 0-15 : the order to move
1179 * bit 16-31 : the target order
1180 * @param text unused
1181 * @return the cost of this operation or an error
1182 * @note The target order will move one place down in the orderlist
1183 * if you move the order upwards else it'll move it one place down
1185 CommandCost
CmdMoveOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1187 VehicleID veh
= GB(p1
, 0, 20);
1188 VehicleOrderID moving_order
= GB(p2
, 0, 16);
1189 VehicleOrderID target_order
= GB(p2
, 16, 16);
1191 Vehicle
*v
= Vehicle::GetIfValid(veh
);
1192 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1194 CommandCost ret
= CheckOwnership(v
->owner
);
1195 if (ret
.Failed()) return ret
;
1197 /* Don't make senseless movements */
1198 if (moving_order
>= v
->GetNumOrders() || target_order
>= v
->GetNumOrders() ||
1199 moving_order
== target_order
|| v
->GetNumOrders() <= 1) return CMD_ERROR
;
1201 Order
*moving_one
= v
->GetOrder(moving_order
);
1202 /* Don't move an empty order */
1203 if (moving_one
== NULL
) return CMD_ERROR
;
1205 if (flags
& DC_EXEC
) {
1206 v
->orders
.list
->MoveOrder(moving_order
, target_order
);
1208 /* Update shared list */
1209 Vehicle
*u
= v
->FirstShared();
1211 DeleteOrderWarnings(u
);
1213 for (; u
!= NULL
; u
= u
->NextShared()) {
1214 /* Update the current order.
1215 * There are multiple ways to move orders, which result in cur_implicit_order_index
1216 * and cur_real_order_index to not longer make any sense. E.g. moving another
1217 * real order between them.
1219 * Basically one could choose to preserve either of them, but not both.
1220 * While both ways are suitable in this or that case from a human point of view, neither
1221 * of them makes really sense.
1222 * However, from an AI point of view, preserving cur_real_order_index is the most
1223 * predictable and transparent behaviour.
1225 * With that decision it basically does not matter what we do to cur_implicit_order_index.
1226 * If we change orders between the implicit- and real-index, the implicit orders are mostly likely
1227 * completely out-dated anyway. So, keep it simple and just keep cur_implicit_order_index as well.
1228 * The worst which can happen is that a lot of implicit orders are removed when reaching current_order.
1230 if (u
->cur_real_order_index
== moving_order
) {
1231 u
->cur_real_order_index
= target_order
;
1232 } else if (u
->cur_real_order_index
> moving_order
&& u
->cur_real_order_index
<= target_order
) {
1233 u
->cur_real_order_index
--;
1234 } else if (u
->cur_real_order_index
< moving_order
&& u
->cur_real_order_index
>= target_order
) {
1235 u
->cur_real_order_index
++;
1238 if (u
->cur_implicit_order_index
== moving_order
) {
1239 u
->cur_implicit_order_index
= target_order
;
1240 } else if (u
->cur_implicit_order_index
> moving_order
&& u
->cur_implicit_order_index
<= target_order
) {
1241 u
->cur_implicit_order_index
--;
1242 } else if (u
->cur_implicit_order_index
< moving_order
&& u
->cur_implicit_order_index
>= target_order
) {
1243 u
->cur_implicit_order_index
++;
1246 assert(v
->orders
.list
== u
->orders
.list
);
1247 /* Update any possible open window of the vehicle */
1248 InvalidateVehicleOrder(u
, moving_order
| (target_order
<< 8));
1251 /* As we move an order, the order to skip to will be 'wrong'. */
1253 FOR_VEHICLE_ORDERS(v
, order
) {
1254 if (order
->IsType(OT_CONDITIONAL
)) {
1255 VehicleOrderID order_id
= order
->GetConditionSkipToOrder();
1256 if (order_id
== moving_order
) {
1257 order_id
= target_order
;
1258 } else if (order_id
> moving_order
&& order_id
<= target_order
) {
1260 } else if (order_id
< moving_order
&& order_id
>= target_order
) {
1263 order
->SetConditionSkipToOrder(order_id
);
1267 /* Make sure to rebuild the whole list */
1268 InvalidateWindowClassesData(GetWindowClassForVehicleType(v
->type
), 0);
1271 return CommandCost();
1275 * Modify an order in the orderlist of a vehicle.
1276 * @param tile unused
1277 * @param flags operation to perform
1278 * @param p1 various bitstuffed elements
1279 * - p1 = (bit 0 - 19) - ID of the vehicle
1280 * - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
1281 * the order will be inserted before that one
1282 * the maximum vehicle order id is 254.
1283 * @param p2 various bitstuffed elements
1284 * - p2 = (bit 0 - 3) - what data to modify (@see ModifyOrderFlags)
1285 * - p2 = (bit 4 - 15) - the data to modify
1286 * @param text unused
1287 * @return the cost of this operation or an error
1289 CommandCost
CmdModifyOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1291 VehicleOrderID sel_ord
= GB(p1
, 20, 8);
1292 VehicleID veh
= GB(p1
, 0, 20);
1293 ModifyOrderFlags mof
= Extract
<ModifyOrderFlags
, 0, 4>(p2
);
1294 uint16 data
= GB(p2
, 4, 11);
1296 if (mof
>= MOF_END
) return CMD_ERROR
;
1298 Vehicle
*v
= Vehicle::GetIfValid(veh
);
1299 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1301 CommandCost ret
= CheckOwnership(v
->owner
);
1302 if (ret
.Failed()) return ret
;
1304 /* Is it a valid order? */
1305 if (sel_ord
>= v
->GetNumOrders()) return CMD_ERROR
;
1307 Order
*order
= v
->GetOrder(sel_ord
);
1308 switch (order
->GetType()) {
1309 case OT_GOTO_STATION
:
1310 if (mof
!= MOF_NON_STOP
&& mof
!= MOF_STOP_LOCATION
&& mof
!= MOF_UNLOAD
&& mof
!= MOF_LOAD
) return CMD_ERROR
;
1314 if (mof
!= MOF_NON_STOP
&& mof
!= MOF_DEPOT_ACTION
) return CMD_ERROR
;
1317 case OT_GOTO_WAYPOINT
:
1318 if (mof
!= MOF_NON_STOP
) return CMD_ERROR
;
1321 case OT_CONDITIONAL
:
1322 if (mof
!= MOF_COND_VARIABLE
&& mof
!= MOF_COND_COMPARATOR
&& mof
!= MOF_COND_VALUE
&& mof
!= MOF_COND_DESTINATION
) return CMD_ERROR
;
1330 default: NOT_REACHED();
1333 if (!v
->IsGroundVehicle()) return CMD_ERROR
;
1334 if (data
>= ONSF_END
) return CMD_ERROR
;
1335 if (data
== order
->GetNonStopType()) return CMD_ERROR
;
1338 case MOF_STOP_LOCATION
:
1339 if (v
->type
!= VEH_TRAIN
) return CMD_ERROR
;
1340 if (data
>= OSL_END
) return CMD_ERROR
;
1344 if (order
->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
) return CMD_ERROR
;
1345 if ((data
& ~(OUFB_UNLOAD
| OUFB_TRANSFER
| OUFB_NO_UNLOAD
)) != 0) return CMD_ERROR
;
1346 /* Unload and no-unload are mutual exclusive and so are transfer and no unload. */
1347 if (data
!= 0 && ((data
& (OUFB_UNLOAD
| OUFB_TRANSFER
)) != 0) == ((data
& OUFB_NO_UNLOAD
) != 0)) return CMD_ERROR
;
1348 if (data
== order
->GetUnloadType()) return CMD_ERROR
;
1352 if (order
->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
) return CMD_ERROR
;
1353 if (data
> OLFB_NO_LOAD
|| data
== 1) return CMD_ERROR
;
1354 if (data
== order
->GetLoadType()) return CMD_ERROR
;
1357 case MOF_DEPOT_ACTION
:
1358 if (data
>= DA_END
) return CMD_ERROR
;
1361 case MOF_COND_VARIABLE
:
1362 if (data
>= OCV_END
) return CMD_ERROR
;
1365 case MOF_COND_COMPARATOR
:
1366 if (data
>= OCC_END
) return CMD_ERROR
;
1367 switch (order
->GetConditionVariable()) {
1368 case OCV_UNCONDITIONALLY
: return CMD_ERROR
;
1370 case OCV_REQUIRES_SERVICE
:
1371 if (data
!= OCC_IS_TRUE
&& data
!= OCC_IS_FALSE
) return CMD_ERROR
;
1375 if (data
== OCC_IS_TRUE
|| data
== OCC_IS_FALSE
) return CMD_ERROR
;
1380 case MOF_COND_VALUE
:
1381 switch (order
->GetConditionVariable()) {
1382 case OCV_UNCONDITIONALLY
:
1383 case OCV_REQUIRES_SERVICE
:
1386 case OCV_LOAD_PERCENTAGE
:
1387 case OCV_RELIABILITY
:
1388 if (data
> 100) return CMD_ERROR
;
1392 if (data
> 2047) return CMD_ERROR
;
1397 case MOF_COND_DESTINATION
:
1398 if (data
>= v
->GetNumOrders()) return CMD_ERROR
;
1402 if (flags
& DC_EXEC
) {
1405 order
->SetNonStopType((OrderNonStopFlags
)data
);
1406 if (data
& ONSF_NO_STOP_AT_DESTINATION_STATION
) {
1407 order
->SetRefit(CT_NO_REFIT
);
1408 order
->SetLoadType(OLF_LOAD_IF_POSSIBLE
);
1409 order
->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE
);
1413 case MOF_STOP_LOCATION
:
1414 order
->SetStopLocation((OrderStopLocation
)data
);
1418 order
->SetUnloadType((OrderUnloadFlags
)data
);
1422 order
->SetLoadType((OrderLoadFlags
)data
);
1423 if (data
& OLFB_NO_LOAD
) order
->SetRefit(CT_NO_REFIT
);
1426 case MOF_DEPOT_ACTION
: {
1429 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() & ~ODTFB_SERVICE
));
1430 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() & ~ODATFB_HALT
));
1434 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() | ODTFB_SERVICE
));
1435 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() & ~ODATFB_HALT
));
1436 order
->SetRefit(CT_NO_REFIT
);
1440 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() & ~ODTFB_SERVICE
));
1441 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() | ODATFB_HALT
));
1442 order
->SetRefit(CT_NO_REFIT
);
1451 case MOF_COND_VARIABLE
: {
1452 order
->SetConditionVariable((OrderConditionVariable
)data
);
1454 OrderConditionComparator occ
= order
->GetConditionComparator();
1455 switch (order
->GetConditionVariable()) {
1456 case OCV_UNCONDITIONALLY
:
1457 order
->SetConditionComparator(OCC_EQUALS
);
1458 order
->SetConditionValue(0);
1461 case OCV_REQUIRES_SERVICE
:
1462 if (occ
!= OCC_IS_TRUE
&& occ
!= OCC_IS_FALSE
) order
->SetConditionComparator(OCC_IS_TRUE
);
1463 order
->SetConditionValue(0);
1466 case OCV_LOAD_PERCENTAGE
:
1467 case OCV_RELIABILITY
:
1468 if (order
->GetConditionValue() > 100) order
->SetConditionValue(100);
1471 if (occ
== OCC_IS_TRUE
|| occ
== OCC_IS_FALSE
) order
->SetConditionComparator(OCC_EQUALS
);
1477 case MOF_COND_COMPARATOR
:
1478 order
->SetConditionComparator((OrderConditionComparator
)data
);
1481 case MOF_COND_VALUE
:
1482 order
->SetConditionValue(data
);
1485 case MOF_COND_DESTINATION
:
1486 order
->SetConditionSkipToOrder(data
);
1489 default: NOT_REACHED();
1492 /* Update the windows and full load flags, also for vehicles that share the same order list */
1493 Vehicle
*u
= v
->FirstShared();
1494 DeleteOrderWarnings(u
);
1495 for (; u
!= NULL
; u
= u
->NextShared()) {
1496 /* Toggle u->current_order "Full load" flag if it changed.
1497 * However, as the same flag is used for depot orders, check
1498 * whether we are not going to a depot as there are three
1499 * cases where the full load flag can be active and only
1500 * one case where the flag is used for depot orders. In the
1501 * other cases for the OrderTypeByte the flags are not used,
1502 * so do not care and those orders should not be active
1503 * when this function is called.
1505 if (sel_ord
== u
->cur_real_order_index
&&
1506 (u
->current_order
.IsType(OT_GOTO_STATION
) || u
->current_order
.IsType(OT_LOADING
)) &&
1507 u
->current_order
.GetLoadType() != order
->GetLoadType()) {
1508 u
->current_order
.SetLoadType(order
->GetLoadType());
1510 InvalidateVehicleOrder(u
, VIWD_MODIFY_ORDERS
);
1514 return CommandCost();
1518 * Check if an aircraft has enough range for an order list.
1519 * @param v_new Aircraft to check.
1520 * @param v_order Vehicle currently holding the order list.
1521 * @param first First order in the source order list.
1522 * @return True if the aircraft has enough range for the orders, false otherwise.
1524 static bool CheckAircraftOrderDistance(const Aircraft
*v_new
, const Vehicle
*v_order
, const Order
*first
)
1526 if (first
== NULL
|| v_new
->acache
.cached_max_range
== 0) return true;
1528 /* Iterate over all orders to check the distance between all
1529 * 'goto' orders and their respective next order (of any type). */
1530 for (const Order
*o
= first
; o
!= NULL
; o
= o
->next
) {
1531 switch (o
->GetType()) {
1532 case OT_GOTO_STATION
:
1534 case OT_GOTO_WAYPOINT
:
1535 /* If we don't have a next order, we've reached the end and must check the first order instead. */
1536 if (GetOrderDistance(o
, o
->next
!= NULL
? o
->next
: first
, v_order
) > v_new
->acache
.cached_max_range_sqr
) return false;
1547 * Clone/share/copy an order-list of another vehicle.
1548 * @param tile unused
1549 * @param flags operation to perform
1550 * @param p1 various bitstuffed elements
1551 * - p1 = (bit 0-19) - destination vehicle to clone orders to
1552 * - p1 = (bit 30-31) - action to perform
1553 * @param p2 source vehicle to clone orders from, if any (none for CO_UNSHARE)
1554 * @param text unused
1555 * @return the cost of this operation or an error
1557 CommandCost
CmdCloneOrder(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1559 VehicleID veh_src
= GB(p2
, 0, 20);
1560 VehicleID veh_dst
= GB(p1
, 0, 20);
1562 Vehicle
*dst
= Vehicle::GetIfValid(veh_dst
);
1563 if (dst
== NULL
|| !dst
->IsPrimaryVehicle()) return CMD_ERROR
;
1565 CommandCost ret
= CheckOwnership(dst
->owner
);
1566 if (ret
.Failed()) return ret
;
1568 switch (GB(p1
, 30, 2)) {
1570 Vehicle
*src
= Vehicle::GetIfValid(veh_src
);
1573 if (src
== NULL
|| !src
->IsPrimaryVehicle() || dst
->type
!= src
->type
|| dst
== src
) return CMD_ERROR
;
1575 CommandCost ret
= CheckOwnership(src
->owner
);
1576 if (ret
.Failed()) return ret
;
1578 /* Trucks can't share orders with busses (and visa versa) */
1579 if (src
->type
== VEH_ROAD
&& RoadVehicle::From(src
)->IsBus() != RoadVehicle::From(dst
)->IsBus()) {
1583 /* Is the vehicle already in the shared list? */
1584 if (src
->FirstShared() == dst
->FirstShared()) return CMD_ERROR
;
1588 FOR_VEHICLE_ORDERS(src
, order
) {
1589 if (!OrderGoesToStation(dst
, order
)) continue;
1591 /* Allow copying unreachable destinations if they were already unreachable for the source.
1592 * This is basically to allow cloning / autorenewing / autoreplacing vehicles, while the stations
1593 * are temporarily invalid due to reconstruction. */
1594 const Station
*st
= Station::Get(order
->GetDestination());
1595 if (CanVehicleUseStation(src
, st
) && !CanVehicleUseStation(dst
, st
)) {
1596 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER
);
1600 /* Check for aircraft range limits. */
1601 if (dst
->type
== VEH_AIRCRAFT
&& !CheckAircraftOrderDistance(Aircraft::From(dst
), src
, src
->GetFirstOrder())) {
1602 return_cmd_error(STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE
);
1605 if (src
->orders
.list
== NULL
&& !OrderList::CanAllocateItem()) {
1606 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
1609 if (flags
& DC_EXEC
) {
1610 /* If the destination vehicle had a OrderList, destroy it.
1611 * We only reset the order indices, if the new orders are obviously different.
1612 * (We mainly do this to keep the order indices valid and in range.) */
1613 DeleteVehicleOrders(dst
, false, dst
->GetNumOrders() != src
->GetNumOrders());
1615 dst
->orders
.list
= src
->orders
.list
;
1617 /* Link this vehicle in the shared-list */
1618 dst
->AddToShared(src
);
1620 InvalidateVehicleOrder(dst
, VIWD_REMOVE_ALL_ORDERS
);
1621 InvalidateVehicleOrder(src
, VIWD_MODIFY_ORDERS
);
1623 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst
->type
), 0);
1629 Vehicle
*src
= Vehicle::GetIfValid(veh_src
);
1632 if (src
== NULL
|| !src
->IsPrimaryVehicle() || dst
->type
!= src
->type
|| dst
== src
) return CMD_ERROR
;
1634 CommandCost ret
= CheckOwnership(src
->owner
);
1635 if (ret
.Failed()) return ret
;
1637 /* Trucks can't copy all the orders from busses (and visa versa),
1638 * and neither can helicopters and aircraft. */
1640 FOR_VEHICLE_ORDERS(src
, order
) {
1641 if (OrderGoesToStation(dst
, order
) &&
1642 !CanVehicleUseStation(dst
, Station::Get(order
->GetDestination()))) {
1643 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER
);
1647 /* Check for aircraft range limits. */
1648 if (dst
->type
== VEH_AIRCRAFT
&& !CheckAircraftOrderDistance(Aircraft::From(dst
), src
, src
->GetFirstOrder())) {
1649 return_cmd_error(STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE
);
1652 /* make sure there are orders available */
1653 if (!Order::CanAllocateItem(src
->GetNumOrders()) || !OrderList::CanAllocateItem()) {
1654 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS
);
1657 if (flags
& DC_EXEC
) {
1659 Order
*first
= NULL
;
1662 /* If the destination vehicle had an order list, destroy the chain but keep the OrderList.
1663 * We only reset the order indices, if the new orders are obviously different.
1664 * (We mainly do this to keep the order indices valid and in range.) */
1665 DeleteVehicleOrders(dst
, true, dst
->GetNumOrders() != src
->GetNumOrders());
1668 FOR_VEHICLE_ORDERS(src
, order
) {
1669 *order_dst
= new Order();
1670 (*order_dst
)->AssignOrder(*order
);
1671 order_dst
= &(*order_dst
)->next
;
1673 if (dst
->orders
.list
== NULL
) {
1674 dst
->orders
.list
= new OrderList(first
, dst
);
1676 assert(dst
->orders
.list
->GetFirstOrder() == NULL
);
1677 assert(!dst
->orders
.list
->IsShared());
1678 delete dst
->orders
.list
;
1679 assert(OrderList::CanAllocateItem());
1680 dst
->orders
.list
= new OrderList(first
, dst
);
1683 InvalidateVehicleOrder(dst
, VIWD_REMOVE_ALL_ORDERS
);
1685 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst
->type
), 0);
1690 case CO_UNSHARE
: return DecloneOrder(dst
, flags
);
1691 default: return CMD_ERROR
;
1694 return CommandCost();
1698 * Add/remove refit orders from an order
1699 * @param tile Not used
1700 * @param flags operation to perform
1701 * @param p1 VehicleIndex of the vehicle having the order
1704 * - bit 16-23 number of order to modify
1705 * @param text unused
1706 * @return the cost of this operation or an error
1708 CommandCost
CmdOrderRefit(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
1710 VehicleID veh
= GB(p1
, 0, 20);
1711 VehicleOrderID order_number
= GB(p2
, 16, 8);
1712 CargoID cargo
= GB(p2
, 0, 8);
1714 if (cargo
>= NUM_CARGO
&& cargo
!= CT_NO_REFIT
&& cargo
!= CT_AUTO_REFIT
) return CMD_ERROR
;
1716 const Vehicle
*v
= Vehicle::GetIfValid(veh
);
1717 if (v
== NULL
|| !v
->IsPrimaryVehicle()) return CMD_ERROR
;
1719 CommandCost ret
= CheckOwnership(v
->owner
);
1720 if (ret
.Failed()) return ret
;
1722 Order
*order
= v
->GetOrder(order_number
);
1723 if (order
== NULL
) return CMD_ERROR
;
1725 /* Automatic refit cargo is only supported for goto station orders. */
1726 if (cargo
== CT_AUTO_REFIT
&& !order
->IsType(OT_GOTO_STATION
)) return CMD_ERROR
;
1728 if (order
->GetLoadType() & OLFB_NO_LOAD
) return CMD_ERROR
;
1730 if (flags
& DC_EXEC
) {
1731 order
->SetRefit(cargo
);
1733 /* Make the depot order an 'always go' order. */
1734 if (cargo
!= CT_NO_REFIT
&& order
->IsType(OT_GOTO_DEPOT
)) {
1735 order
->SetDepotOrderType((OrderDepotTypeFlags
)(order
->GetDepotOrderType() & ~ODTFB_SERVICE
));
1736 order
->SetDepotActionType((OrderDepotActionFlags
)(order
->GetDepotActionType() & ~ODATFB_HALT
));
1739 for (Vehicle
*u
= v
->FirstShared(); u
!= NULL
; u
= u
->NextShared()) {
1740 /* Update any possible open window of the vehicle */
1741 InvalidateVehicleOrder(u
, VIWD_MODIFY_ORDERS
);
1743 /* If the vehicle already got the current depot set as current order, then update current order as well */
1744 if (u
->cur_real_order_index
== order_number
&& (u
->current_order
.GetDepotOrderType() & ODTFB_PART_OF_ORDERS
)) {
1745 u
->current_order
.SetRefit(cargo
);
1750 return CommandCost();
1756 * Check the orders of a vehicle, to see if there are invalid orders and stuff
1759 void CheckOrders(const Vehicle
*v
)
1761 /* Does the user wants us to check things? */
1762 if (_settings_client
.gui
.order_review_system
== 0) return;
1764 /* Do nothing for crashed vehicles */
1765 if (v
->vehstatus
& VS_CRASHED
) return;
1767 /* Do nothing for stopped vehicles if setting is '1' */
1768 if (_settings_client
.gui
.order_review_system
== 1 && (v
->vehstatus
& VS_STOPPED
)) return;
1770 /* do nothing we we're not the first vehicle in a share-chain */
1771 if (v
->FirstShared() != v
) return;
1773 /* Only check every 20 days, so that we don't flood the message log */
1774 if (v
->owner
== _local_company
&& v
->day_counter
% 20 == 0) {
1776 StringID message
= INVALID_STRING_ID
;
1778 /* Check the order list */
1781 FOR_VEHICLE_ORDERS(v
, order
) {
1783 if (order
->IsType(OT_DUMMY
)) {
1784 message
= STR_NEWS_VEHICLE_HAS_VOID_ORDER
;
1787 /* Does station have a load-bay for this vehicle? */
1788 if (order
->IsType(OT_GOTO_STATION
)) {
1789 const Station
*st
= Station::Get(order
->GetDestination());
1792 if (!CanVehicleUseStation(v
, st
)) {
1793 message
= STR_NEWS_VEHICLE_HAS_INVALID_ENTRY
;
1794 } else if (v
->type
== VEH_AIRCRAFT
&&
1795 (AircraftVehInfo(v
->engine_type
)->subtype
& AIR_FAST
) &&
1796 (st
->airport
.GetFTA()->flags
& AirportFTAClass::SHORT_STRIP
) &&
1797 _settings_game
.vehicle
.plane_crashes
!= 0 &&
1798 !_cheats
.no_jetcrash
.value
&&
1799 message
== INVALID_STRING_ID
) {
1800 message
= STR_NEWS_PLANE_USES_TOO_SHORT_RUNWAY
;
1805 /* Check if the last and the first order are the same */
1806 if (v
->GetNumOrders() > 1) {
1807 const Order
*last
= v
->GetLastOrder();
1809 if (v
->orders
.list
->GetFirstOrder()->Equals(*last
)) {
1810 message
= STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY
;
1814 /* Do we only have 1 station in our order list? */
1815 if (n_st
< 2 && message
== INVALID_STRING_ID
) message
= STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS
;
1818 if (v
->orders
.list
!= NULL
) v
->orders
.list
->DebugCheckSanity();
1821 /* We don't have a problem */
1822 if (message
== INVALID_STRING_ID
) return;
1824 SetDParam(0, v
->index
);
1825 AddVehicleAdviceNewsItem(message
, v
->index
);
1830 * Removes an order from all vehicles. Triggers when, say, a station is removed.
1831 * @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
1832 * @param destination The destination. Can be a StationID, DepotID or WaypointID.
1834 void RemoveOrderFromAllVehicles(OrderType type
, DestinationID destination
)
1838 /* Aircraft have StationIDs for depot orders and never use DepotIDs
1839 * This fact is handled specially below
1842 /* Go through all vehicles */
1843 FOR_ALL_VEHICLES(v
) {
1846 order
= &v
->current_order
;
1847 if ((v
->type
== VEH_AIRCRAFT
&& order
->IsType(OT_GOTO_DEPOT
) ? OT_GOTO_STATION
: order
->GetType()) == type
&&
1848 v
->current_order
.GetDestination() == destination
) {
1850 SetWindowDirty(WC_VEHICLE_VIEW
, v
->index
);
1853 /* Clear the order from the order-list */
1855 FOR_VEHICLE_ORDERS(v
, order
) {
1859 OrderType ot
= order
->GetType();
1860 if (ot
== OT_GOTO_DEPOT
&& (order
->GetDepotActionType() & ODATFB_NEAREST_DEPOT
) != 0) continue;
1861 if (ot
== OT_IMPLICIT
|| (v
->type
== VEH_AIRCRAFT
&& ot
== OT_GOTO_DEPOT
)) ot
= OT_GOTO_STATION
;
1862 if (ot
== type
&& order
->GetDestination() == destination
) {
1863 /* We want to clear implicit orders, but we don't want to make them
1864 * dummy orders. They should just vanish. Also check the actual order
1865 * type as ot is currently OT_GOTO_STATION. */
1866 if (order
->IsType(OT_IMPLICIT
)) {
1867 order
= order
->next
; // DeleteOrder() invalidates current order
1869 if (order
!= NULL
) goto restart
;
1873 /* Clear wait time */
1874 v
->orders
.list
->UpdateTotalDuration(-order
->GetWaitTime());
1875 if (order
->IsWaitTimetabled()) {
1876 v
->orders
.list
->UpdateTimetableDuration(-order
->GetTimetabledWait());
1877 order
->SetWaitTimetabled(false);
1879 order
->SetWaitTime(0);
1881 /* Clear order, preserving travel time */
1882 bool travel_timetabled
= order
->IsTravelTimetabled();
1884 order
->SetTravelTimetabled(travel_timetabled
);
1886 for (const Vehicle
*w
= v
->FirstShared(); w
!= NULL
; w
= w
->NextShared()) {
1887 /* In GUI, simulate by removing the order and adding it back */
1888 InvalidateVehicleOrder(w
, id
| (INVALID_VEH_ORDER_ID
<< 8));
1889 InvalidateVehicleOrder(w
, (INVALID_VEH_ORDER_ID
<< 8) | id
);
1895 OrderBackup::RemoveOrder(type
, destination
);
1899 * Checks if a vehicle has a depot in its order list.
1900 * @return True iff at least one order is a depot order.
1902 bool Vehicle::HasDepotOrder() const
1906 FOR_VEHICLE_ORDERS(this, order
) {
1907 if (order
->IsType(OT_GOTO_DEPOT
)) return true;
1914 * Delete all orders from a vehicle
1915 * @param v Vehicle whose orders to reset
1916 * @param keep_orderlist If true, do not free the order list, only empty it.
1917 * @param reset_order_indices If true, reset cur_implicit_order_index and cur_real_order_index
1918 * and cancel the current full load order (if the vehicle is loading).
1919 * If false, _you_ have to make sure the order indices are valid after
1920 * your messing with them!
1922 void DeleteVehicleOrders(Vehicle
*v
, bool keep_orderlist
, bool reset_order_indices
)
1924 DeleteOrderWarnings(v
);
1926 if (v
->IsOrderListShared()) {
1927 /* Remove ourself from the shared order list. */
1928 v
->RemoveFromShared();
1929 v
->orders
.list
= NULL
;
1930 } else if (v
->orders
.list
!= NULL
) {
1931 /* Remove the orders */
1932 v
->orders
.list
->FreeChain(keep_orderlist
);
1933 if (!keep_orderlist
) v
->orders
.list
= NULL
;
1936 if (reset_order_indices
) {
1937 v
->cur_implicit_order_index
= v
->cur_real_order_index
= 0;
1938 if (v
->current_order
.IsType(OT_LOADING
)) {
1939 CancelLoadingDueToDeletedOrder(v
);
1945 * Clamp the service interval to the correct min/max. The actual min/max values
1946 * depend on whether it's in percent or days.
1947 * @param interval proposed service interval
1948 * @param company_id the owner of the vehicle
1949 * @return Clamped service interval
1951 uint16
GetServiceIntervalClamped(uint interval
, bool ispercent
)
1953 return ispercent
? Clamp(interval
, MIN_SERVINT_PERCENT
, MAX_SERVINT_PERCENT
) : Clamp(interval
, MIN_SERVINT_DAYS
, MAX_SERVINT_DAYS
);
1958 * Check if a vehicle has any valid orders
1960 * @return false if there are no valid orders
1961 * @note Conditional orders are not considered valid destination orders
1964 static bool CheckForValidOrders(const Vehicle
*v
)
1968 FOR_VEHICLE_ORDERS(v
, order
) {
1969 switch (order
->GetType()) {
1970 case OT_GOTO_STATION
:
1972 case OT_GOTO_WAYPOINT
:
1984 * Compare the variable and value based on the given comparator.
1986 static bool OrderConditionCompare(OrderConditionComparator occ
, int variable
, int value
)
1989 case OCC_EQUALS
: return variable
== value
;
1990 case OCC_NOT_EQUALS
: return variable
!= value
;
1991 case OCC_LESS_THAN
: return variable
< value
;
1992 case OCC_LESS_EQUALS
: return variable
<= value
;
1993 case OCC_MORE_THAN
: return variable
> value
;
1994 case OCC_MORE_EQUALS
: return variable
>= value
;
1995 case OCC_IS_TRUE
: return variable
!= 0;
1996 case OCC_IS_FALSE
: return variable
== 0;
1997 default: NOT_REACHED();
2002 * Process a conditional order and determine the next order.
2003 * @param order the order the vehicle currently has
2004 * @param v the vehicle to update
2005 * @return index of next order to jump to, or INVALID_VEH_ORDER_ID to use the next order
2007 VehicleOrderID
ProcessConditionalOrder(const Order
*order
, const Vehicle
*v
)
2009 if (order
->GetType() != OT_CONDITIONAL
) return INVALID_VEH_ORDER_ID
;
2011 bool skip_order
= false;
2012 OrderConditionComparator occ
= order
->GetConditionComparator();
2013 uint16 value
= order
->GetConditionValue();
2015 switch (order
->GetConditionVariable()) {
2016 case OCV_LOAD_PERCENTAGE
: skip_order
= OrderConditionCompare(occ
, CalcPercentVehicleFilled(v
, NULL
), value
); break;
2017 case OCV_RELIABILITY
: skip_order
= OrderConditionCompare(occ
, ToPercent16(v
->reliability
), value
); break;
2018 case OCV_MAX_SPEED
: skip_order
= OrderConditionCompare(occ
, v
->GetDisplayMaxSpeed() * 10 / 16, value
); break;
2019 case OCV_AGE
: skip_order
= OrderConditionCompare(occ
, v
->age
/ DAYS_IN_LEAP_YEAR
, value
); break;
2020 case OCV_REQUIRES_SERVICE
: skip_order
= OrderConditionCompare(occ
, v
->NeedsServicing(), value
); break;
2021 case OCV_UNCONDITIONALLY
: skip_order
= true; break;
2022 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;
2023 default: NOT_REACHED();
2026 return skip_order
? order
->GetConditionSkipToOrder() : (VehicleOrderID
)INVALID_VEH_ORDER_ID
;
2030 * Update the vehicle's destination tile from an order.
2031 * @param order the order the vehicle currently has
2032 * @param v the vehicle to update
2033 * @param conditional_depth the depth (amount of steps) to go with conditional orders. This to prevent infinite loops.
2034 * @param pbs_look_ahead Whether we are forecasting orders for pbs reservations in advance. If true, the order indices must not be modified.
2036 bool UpdateOrderDest(Vehicle
*v
, const Order
*order
, int conditional_depth
, bool pbs_look_ahead
)
2038 if (conditional_depth
> v
->GetNumOrders()) {
2039 v
->current_order
.Free();
2044 switch (order
->GetType()) {
2045 case OT_GOTO_STATION
:
2046 v
->dest_tile
= v
->GetOrderStationLocation(order
->GetDestination());
2050 if ((order
->GetDepotOrderType() & ODTFB_SERVICE
) && !v
->NeedsServicing()) {
2051 assert(!pbs_look_ahead
);
2052 UpdateVehicleTimetable(v
, true);
2053 v
->IncrementRealOrderIndex();
2057 if (v
->current_order
.GetDepotActionType() & ODATFB_NEAREST_DEPOT
) {
2058 /* We need to search for the nearest depot (hangar). */
2060 DestinationID destination
;
2063 if (v
->FindClosestDepot(&location
, &destination
, &reverse
)) {
2064 /* PBS reservations cannot reverse */
2065 if (pbs_look_ahead
&& reverse
) return false;
2067 v
->dest_tile
= location
;
2068 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());
2070 /* If there is no depot in front, reverse automatically (trains only) */
2071 if (v
->type
== VEH_TRAIN
&& reverse
) DoCommand(v
->tile
, v
->index
, 0, DC_EXEC
, CMD_REVERSE_TRAIN_DIRECTION
);
2073 if (v
->type
== VEH_AIRCRAFT
) {
2074 Aircraft
*a
= Aircraft::From(v
);
2075 if (a
->state
== FLYING
&& a
->targetairport
!= destination
) {
2076 /* The aircraft is now heading for a different hangar than the next in the orders */
2077 extern void AircraftNextAirportPos_and_Order(Aircraft
*a
);
2078 AircraftNextAirportPos_and_Order(a
);
2084 /* If there is no depot, we cannot help PBS either. */
2085 if (pbs_look_ahead
) return false;
2087 UpdateVehicleTimetable(v
, true);
2088 v
->IncrementRealOrderIndex();
2090 if (v
->type
!= VEH_AIRCRAFT
) {
2091 v
->dest_tile
= Depot::Get(order
->GetDestination())->xy
;
2097 case OT_GOTO_WAYPOINT
:
2098 v
->dest_tile
= Waypoint::Get(order
->GetDestination())->xy
;
2101 case OT_CONDITIONAL
: {
2102 assert(!pbs_look_ahead
);
2103 VehicleOrderID next_order
= ProcessConditionalOrder(order
, v
);
2104 if (next_order
!= INVALID_VEH_ORDER_ID
) {
2105 /* Jump to next_order. cur_implicit_order_index becomes exactly that order,
2106 * cur_real_order_index might come after next_order. */
2107 UpdateVehicleTimetable(v
, false);
2108 v
->cur_implicit_order_index
= v
->cur_real_order_index
= next_order
;
2109 v
->UpdateRealOrderIndex();
2110 v
->current_order_time
+= v
->GetOrder(v
->cur_real_order_index
)->GetTimetabledTravel();
2112 /* Disable creation of implicit orders.
2113 * When inserting them we do not know that we would have to make the conditional orders point to them. */
2114 if (v
->IsGroundVehicle()) {
2115 uint16
&gv_flags
= v
->GetGroundVehicleFlags();
2116 SetBit(gv_flags
, GVF_SUPPRESS_IMPLICIT_ORDERS
);
2119 UpdateVehicleTimetable(v
, true);
2120 v
->IncrementRealOrderIndex();
2130 assert(v
->cur_implicit_order_index
< v
->GetNumOrders());
2131 assert(v
->cur_real_order_index
< v
->GetNumOrders());
2133 /* Get the current order */
2134 order
= v
->GetOrder(v
->cur_real_order_index
);
2135 if (order
!= NULL
&& order
->IsType(OT_IMPLICIT
)) {
2136 assert(v
->GetNumManualOrders() == 0);
2140 if (order
== NULL
) {
2141 v
->current_order
.Free();
2146 v
->current_order
= *order
;
2147 return UpdateOrderDest(v
, order
, conditional_depth
+ 1, pbs_look_ahead
);
2151 * Handle the orders of a vehicle and determine the next place
2152 * to go to if needed.
2153 * @param v the vehicle to do this for.
2154 * @return true *if* the vehicle is eligible for reversing
2155 * (basically only when leaving a station).
2157 bool ProcessOrders(Vehicle
*v
)
2159 switch (v
->current_order
.GetType()) {
2161 /* Let a depot order in the orderlist interrupt. */
2162 if (!(v
->current_order
.GetDepotOrderType() & ODTFB_PART_OF_ORDERS
)) return false;
2168 case OT_LEAVESTATION
:
2169 if (v
->type
!= VEH_AIRCRAFT
) return false;
2176 * Reversing because of order change is allowed only just after leaving a
2177 * station (and the difficulty setting to allowed, of course)
2178 * this can be detected because only after OT_LEAVESTATION, current_order
2179 * will be reset to nothing. (That also happens if no order, but in that case
2180 * it won't hit the point in code where may_reverse is checked)
2182 bool may_reverse
= v
->current_order
.IsType(OT_NOTHING
);
2184 /* Check if we've reached a 'via' destination. */
2185 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
)) &&
2186 IsTileType(v
->tile
, MP_STATION
) &&
2187 v
->current_order
.GetDestination() == GetStationIndex(v
->tile
)) {
2188 v
->DeleteUnreachedImplicitOrders();
2189 /* We set the last visited station here because we do not want
2190 * the train to stop at this 'via' station if the next order
2191 * is a no-non-stop order; in that case not setting the last
2192 * visited station will cause the vehicle to still stop. */
2193 v
->last_station_visited
= v
->current_order
.GetDestination();
2194 UpdateVehicleTimetable(v
, true);
2195 v
->IncrementImplicitOrderIndex();
2198 /* Get the current order */
2199 assert(v
->cur_implicit_order_index
== 0 || v
->cur_implicit_order_index
< v
->GetNumOrders());
2200 v
->UpdateRealOrderIndex();
2202 const Order
*order
= v
->GetOrder(v
->cur_real_order_index
);
2203 if (order
!= NULL
&& order
->IsType(OT_IMPLICIT
)) {
2204 assert(v
->GetNumManualOrders() == 0);
2208 /* If no order, do nothing. */
2209 if (order
== NULL
|| (v
->type
== VEH_AIRCRAFT
&& !CheckForValidOrders(v
))) {
2210 if (v
->type
== VEH_AIRCRAFT
) {
2211 /* Aircraft do something vastly different here, so handle separately */
2212 extern void HandleMissingAircraftOrders(Aircraft
*v
);
2213 HandleMissingAircraftOrders(Aircraft::From(v
));
2217 v
->current_order
.Free();
2222 /* If it is unchanged, keep it. */
2223 if (order
->Equals(v
->current_order
) && (v
->type
== VEH_AIRCRAFT
|| v
->dest_tile
!= 0) &&
2224 (v
->type
!= VEH_SHIP
|| !order
->IsType(OT_GOTO_STATION
) || Station::Get(order
->GetDestination())->dock_tile
!= INVALID_TILE
)) {
2228 /* Otherwise set it, and determine the destination tile. */
2229 v
->current_order
= *order
;
2231 InvalidateVehicleOrder(v
, VIWD_MODIFY_ORDERS
);
2242 SetWindowClassesDirty(GetWindowClassForVehicleType(v
->type
));
2246 return UpdateOrderDest(v
, order
) && may_reverse
;
2250 * Check whether the given vehicle should stop at the given station
2251 * based on this order and the non-stop settings.
2252 * @param v the vehicle that might be stopping.
2253 * @param station the station to stop at.
2254 * @return true if the vehicle should stop.
2256 bool Order::ShouldStopAtStation(const Vehicle
*v
, StationID station
) const
2258 bool is_dest_station
= this->IsType(OT_GOTO_STATION
) && this->dest
== station
;
2260 return (!this->IsType(OT_GOTO_DEPOT
) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS
) != 0) &&
2261 v
->last_station_visited
!= station
&& // Do stop only when we've not just been there
2262 /* Finally do stop when there is no non-stop flag set for this type of station. */
2263 !(this->GetNonStopType() & (is_dest_station
? ONSF_NO_STOP_AT_DESTINATION_STATION
: ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
));
2266 bool Order::CanLoadOrUnload() const
2268 return (this->IsType(OT_GOTO_STATION
) || this->IsType(OT_IMPLICIT
)) &&
2269 (this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
) == 0 &&
2270 ((this->GetLoadType() & OLFB_NO_LOAD
) == 0 ||
2271 (this->GetUnloadType() & OUFB_NO_UNLOAD
) == 0);
2275 * A vehicle can leave the current station with cargo if:
2276 * 1. it can load cargo here OR
2277 * 2a. it could leave the last station with cargo AND
2278 * 2b. it doesn't have to unload all cargo here.
2280 bool Order::CanLeaveWithCargo(bool has_cargo
) const
2282 return (this->GetLoadType() & OLFB_NO_LOAD
) == 0 || (has_cargo
&&
2283 (this->GetUnloadType() & (OUFB_UNLOAD
| OUFB_TRANSFER
)) == 0);