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_base.h Base class for orders. */
15 #include "order_type.h"
16 #include "core/pool_type.hpp"
17 #include "core/bitmath_func.hpp"
18 #include "cargo_type.h"
19 #include "depot_type.h"
20 #include "station_type.h"
21 #include "vehicle_type.h"
22 #include "date_type.h"
23 #include "saveload/saveload_buffer.h"
27 uint8 type
; ///< The type of order + non-stop flags
28 uint8 flags
; ///< Load/unload types, depot order/action types.
29 DestinationID dest
; ///< The destination of the order.
31 CargoMask refit_cargo_mask
; ///< Refit cargo mask
33 uint16 wait_time
; ///< How long in ticks to wait at the destination.
34 uint16 travel_time
; ///< How long in ticks the journey to this destination should take.
35 uint16 max_speed
; ///< How fast the vehicle may go on the way to the destination.
37 CONSTEXPR
BaseOrder() : type(OT_NOTHING
), flags(0), dest(),
39 wait_time(0), travel_time(0), max_speed(UINT16_MAX
)
43 CONSTEXPR
BaseOrder (uint8 t
, uint8 f
, DestinationID d
)
44 : type(t
), flags(f
), dest(d
), refit_cargo_mask(0),
45 wait_time(0), travel_time(0), max_speed(UINT16_MAX
)
49 BaseOrder (uint32 packed
)
50 : type(GB(packed
, 0, 8)), flags(GB(packed
, 8, 8)),
51 dest(GB(packed
, 16, 16)), refit_cargo_mask(0),
52 wait_time(0), travel_time(0), max_speed(UINT16_MAX
)
57 * Check whether this order is of the given type.
58 * @param type the type to check against.
59 * @return true if the order matches.
61 inline bool IsType(OrderType type
) const { return this->GetType() == type
; }
64 * Get the type of order of this order.
65 * @return the order type.
67 inline OrderType
GetType() const { return (OrderType
)GB(this->type
, 0, 4); }
70 void MakeGoToStation(StationID destination
);
71 void MakeGoToDepot(DepotID destination
, OrderDepotTypeFlags order
, OrderNonStopFlags non_stop_type
= ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
, OrderDepotActionFlags action
= ODATF_SERVICE_ONLY
);
72 void SetGoToDepotDestination (DepotID destination
);
73 void MakeGoToWaypoint(StationID destination
);
74 void MakeLoading(bool ordered
);
75 void MakeLeaveStation();
77 void MakeConditional(VehicleOrderID order
);
78 void MakeImplicit(StationID destination
);
81 * Is this a 'goto' order with a real destination?
82 * @return True if the type is either #OT_GOTO_WAYPOINT, #OT_GOTO_DEPOT or #OT_GOTO_STATION.
84 inline bool IsGotoOrder() const
86 return IsType(OT_GOTO_WAYPOINT
) || IsType(OT_GOTO_DEPOT
) || IsType(OT_GOTO_STATION
);
90 * Gets the destination of this order.
91 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
92 * @return the destination of the order.
94 inline DestinationID
GetDestination() const { return this->dest
; }
97 * Sets the destination of this order.
98 * @param destination the new destination of the order.
99 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
101 inline void SetDestination(DestinationID destination
) { this->dest
= destination
; }
104 * Is this order a refit order.
105 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
106 * @return true if a refit should happen.
108 inline bool IsRefit() const { return this->refit_cargo_mask
!= 0; }
111 * Is this order a auto-refit order.
112 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
113 * @return true if a auto-refit should happen.
115 inline bool IsAutoRefit() const { return !HasAtMostOneBit (this->refit_cargo_mask
); }
118 * Get the cargo mask of allowed refits.
119 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
120 * @return the cargo mask.
122 inline CargoMask
GetRefitCargoMask() const { return this->refit_cargo_mask
; }
125 * Get the cargo to to refit to.
126 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
127 * @return the cargo type.
129 inline CargoID
GetRefitCargo() const { return FindFirstBit (this->refit_cargo_mask
); }
131 void SetRefitMask (CargoMask mask
= 0);
133 /** How must the consist be loaded? */
134 inline OrderLoadFlags
GetLoadType() const { return (OrderLoadFlags
)GB(this->flags
, 4, 3); }
135 /** How must the consist be unloaded? */
136 inline OrderUnloadFlags
GetUnloadType() const { return (OrderUnloadFlags
)GB(this->flags
, 0, 3); }
137 /** At which stations must we stop? */
138 inline OrderNonStopFlags
GetNonStopType() const { return (OrderNonStopFlags
)GB(this->type
, 6, 2); }
139 /** Where must we stop at the platform? */
140 inline OrderStopLocation
GetStopLocation() const { return (OrderStopLocation
)GB(this->type
, 4, 2); }
141 /** What caused us going to the depot? */
142 inline OrderDepotTypeFlags
GetDepotOrderType() const { return (OrderDepotTypeFlags
)GB(this->flags
, 0, 3); }
143 /** What are we going to do when in the depot. */
144 inline OrderDepotActionFlags
GetDepotActionType() const { return (OrderDepotActionFlags
)GB(this->flags
, 4, 3); }
145 /** What variable do we have to compare? */
146 inline OrderConditionVariable
GetConditionVariable() const { return (OrderConditionVariable
)GB(this->dest
, 11, 5); }
147 /** What is the comparator to use? */
148 inline OrderConditionComparator
GetConditionComparator() const { return (OrderConditionComparator
)GB(this->type
, 5, 3); }
149 /** Get the order to skip to. */
150 inline VehicleOrderID
GetConditionSkipToOrder() const { return this->flags
; }
151 /** Get the value to base the skip on. */
152 inline uint16
GetConditionValue() const { return GB(this->dest
, 0, 11); }
154 /** Set how the consist must be loaded. */
155 inline void SetLoadType(OrderLoadFlags load_type
) { SB(this->flags
, 4, 3, load_type
); }
156 /** Set how the consist must be unloaded. */
157 inline void SetUnloadType(OrderUnloadFlags unload_type
) { SB(this->flags
, 0, 3, unload_type
); }
158 /** Set whether we must stop at stations or not. */
159 inline void SetNonStopType(OrderNonStopFlags non_stop_type
) { SB(this->type
, 6, 2, non_stop_type
); }
160 /** Set where we must stop at the platform. */
161 inline void SetStopLocation(OrderStopLocation stop_location
) { SB(this->type
, 4, 2, stop_location
); }
162 /** Set the cause to go to the depot. */
163 inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type
) { SB(this->flags
, 0, 3, depot_order_type
); }
164 /** Set what we are going to do in the depot. */
165 inline void SetDepotActionType(OrderDepotActionFlags depot_service_type
) { SB(this->flags
, 4, 3, depot_service_type
); }
166 /** Set variable we have to compare. */
167 inline void SetConditionVariable(OrderConditionVariable condition_variable
) { SB(this->dest
, 11, 5, condition_variable
); }
168 /** Set the comparator to use. */
169 inline void SetConditionComparator(OrderConditionComparator condition_comparator
) { SB(this->type
, 5, 3, condition_comparator
); }
170 /** Get the order to skip to. */
171 inline void SetConditionSkipToOrder(VehicleOrderID order_id
) { this->flags
= order_id
; }
172 /** Set the value to base the skip on. */
173 inline void SetConditionValue(uint16 value
) { SB(this->dest
, 0, 11, value
); }
175 /* As conditional orders write their "skip to" order all over the flags, we cannot check the
176 * flags to find out if timetabling is enabled. However, as conditional orders are never
177 * autofilled we can be sure that any non-zero values for their wait_time and travel_time are
178 * explicitly set (but travel_time is actually unused for conditionals). */
180 /** Does this order have an explicit wait time set? */
181 inline bool IsWaitTimetabled() const { return this->IsType(OT_CONDITIONAL
) ? this->wait_time
> 0 : HasBit(this->flags
, 3); }
182 /** Does this order have an explicit travel time set? */
183 inline bool IsTravelTimetabled() const { return this->IsType(OT_CONDITIONAL
) ? this->travel_time
> 0 : HasBit(this->flags
, 7); }
185 /** Get the time in ticks a vehicle should wait at the destination or 0 if it's not timetabled. */
186 inline uint16
GetTimetabledWait() const { return this->IsWaitTimetabled() ? this->wait_time
: 0; }
187 /** Get the time in ticks a vehicle should take to reach the destination or 0 if it's not timetabled. */
188 inline uint16
GetTimetabledTravel() const { return this->IsTravelTimetabled() ? this->travel_time
: 0; }
189 /** Get the time in ticks a vehicle will probably wait at the destination (timetabled or not). */
190 inline uint16
GetWaitTime() const { return this->wait_time
; }
191 /** Get the time in ticks a vehicle will probably take to reach the destination (timetabled or not). */
192 inline uint16
GetTravelTime() const { return this->travel_time
; }
195 * Get the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
197 * @return maximum speed.
199 inline uint16
GetMaxSpeed() const { return this->max_speed
; }
201 /** Set if the wait time is explicitly timetabled (unless the order is conditional). */
202 inline void SetWaitTimetabled(bool timetabled
) { if (!this->IsType(OT_CONDITIONAL
)) SB(this->flags
, 3, 1, timetabled
? 1 : 0); }
203 /** Set if the travel time is explicitly timetabled (unless the order is conditional). */
204 inline void SetTravelTimetabled(bool timetabled
) { if (!this->IsType(OT_CONDITIONAL
)) SB(this->flags
, 7, 1, timetabled
? 1 : 0); }
207 * Set the time in ticks to wait at the destination.
208 * @param time Time to set as wait time.
210 inline void SetWaitTime(uint16 time
) { this->wait_time
= time
; }
213 * Set the time in ticks to take for travelling to the destination.
214 * @param time Time to set as travel time.
216 inline void SetTravelTime(uint16 time
) { this->travel_time
= time
; }
219 * Set the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
221 * @param speed Speed to be set.
223 inline void SetMaxSpeed(uint16 speed
) { this->max_speed
= speed
; }
225 bool ShouldStopAtStation(const Vehicle
*v
, StationID station
) const;
226 bool CanLoadOrUnload() const;
227 bool CanLeaveWithCargo(bool has_cargo
) const;
229 TileIndex
GetLocation(const Vehicle
*v
, bool airport
= false) const;
231 /** Checks if travel_time and wait_time apply to this order and if they are timetabled. */
232 inline bool IsCompletelyTimetabled() const
234 if (!this->IsTravelTimetabled() && !this->IsType(OT_CONDITIONAL
)) return false;
235 if (!this->IsWaitTimetabled() && this->IsType(OT_GOTO_STATION
) &&
236 !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
)) {
242 void AssignOrder (const BaseOrder
&other
);
243 bool Equals (const BaseOrder
&other
) const;
246 uint16
MapOldOrder() const;
247 void ConvertFromOldSavegame(const SavegameTypeVersion
*stv
);
250 /* If you change this, keep in mind that it is saved on 3 places:
251 * - Load_ORDR, all the global orders
252 * - Vehicle -> current_order
253 * - REF_ORDER (all REFs are currently limited to 16 bits!!)
255 struct Order
: PooledItem
<Order
, OrderID
, 256, 64000>, BaseOrder
{
256 Order
*next
; ///< Pointer to next order. If NULL, end of list
258 Order() : BaseOrder() {}
261 Order (const BaseOrder
&o
) : BaseOrder(o
), next(NULL
) { }
263 Order(uint32 packed
) : BaseOrder(packed
), next(NULL
) { }
266 void InsertOrder(Vehicle
*v
, Order
*new_o
, VehicleOrderID sel_ord
);
267 void DeleteOrder(Vehicle
*v
, VehicleOrderID sel_ord
);
270 * Shared order list linking together the linked list of orders and the list
271 * of vehicles sharing this order list.
273 struct OrderList
: PooledItem
<OrderList
, OrderListID
, 128, 64000> {
275 friend void AfterLoadVehicles(const SavegameTypeVersion
*stv
); ///< For instantiating the shared vehicle chain
276 friend const struct SaveLoad
*GetOrderListDescription(); ///< Saving and loading of order lists.
278 StationID
GetBestLoadableNext(const Vehicle
*v
, const Order
*o1
, const Order
*o2
) const;
280 Order
*first
; ///< First order of the order list.
281 VehicleOrderID num_orders
; ///< NOSAVE: How many orders there are in the list.
282 VehicleOrderID num_manual_orders
; ///< NOSAVE: How many manually added orders are there in the list.
283 uint num_vehicles
; ///< NOSAVE: Number of vehicles that share this order list.
284 Vehicle
*first_shared
; ///< NOSAVE: pointer to the first vehicle in the shared order chain.
286 Ticks timetable_duration
; ///< NOSAVE: Total timetabled duration of the order list.
287 Ticks total_duration
; ///< NOSAVE: Total (timetabled or not) duration of the order list.
290 /** Default constructor producing an invalid order list. */
291 OrderList(VehicleOrderID num_orders
= INVALID_VEH_ORDER_ID
)
292 : first(NULL
), num_orders(num_orders
), num_manual_orders(0), num_vehicles(0), first_shared(NULL
),
293 timetable_duration(0), total_duration(0) { }
296 * Create an order list with the given order chain for the given vehicle.
297 * @param chain pointer to the first order of the order chain
298 * @param v any vehicle using this orderlist
300 OrderList(Order
*chain
, Vehicle
*v
) { this->Initialize(chain
, v
); }
302 /** Destructor. Invalidates OrderList for re-usage by the pool. */
305 void Initialize(Order
*chain
, Vehicle
*v
);
308 * Get the first order of the order chain.
309 * @return the first order of the chain.
311 inline Order
*GetFirstOrder() const { return this->first
; }
313 Order
*GetOrderAt(int index
) const;
316 * Get the last order of the order chain.
317 * @return the last order of the chain.
319 inline Order
*GetLastOrder() const { return this->GetOrderAt(this->num_orders
- 1); }
322 * Get the order after the given one or the first one, if the given one is the
324 * @param curr Order to find the next one for.
325 * @return Next order.
327 inline const Order
*GetNext(const Order
*curr
) const { return (curr
->next
== NULL
) ? this->GetFirstOrder() : curr
->next
; }
330 * Get number of orders in the order list.
331 * @return number of orders in the chain.
333 inline VehicleOrderID
GetNumOrders() const { return this->num_orders
; }
336 * Get number of manually added orders in the order list.
337 * @return number of manual orders in the chain.
339 inline VehicleOrderID
GetNumManualOrders() const { return this->num_manual_orders
; }
341 void AppendNextStoppingStations (StationIDStack
*result
, const Vehicle
*v
,
342 const Order
*first
= NULL
, uint hops
= 0) const;
344 const Order
*GetNextDecisionNode(const Order
*next
, uint hops
) const;
346 void InsertOrderAt(Order
*new_order
, int index
);
347 void DeleteOrderAt(int index
);
348 void MoveOrder(int from
, int to
);
351 * Is this a shared order list?
352 * @return whether this order list is shared among multiple vehicles
354 inline bool IsShared() const { return this->num_vehicles
> 1; };
357 * Get the first vehicle of this vehicle chain.
358 * @return the first vehicle of the chain.
360 inline Vehicle
*GetFirstSharedVehicle() const { return this->first_shared
; }
363 * Return the number of vehicles that share this orders list
364 * @return the count of vehicles that use this shared orders list
366 inline uint
GetNumVehicles() const { return this->num_vehicles
; }
368 bool IsVehicleInSharedOrdersList(const Vehicle
*v
) const;
369 int GetPositionInSharedOrderList(const Vehicle
*v
) const;
372 * Adds the given vehicle to this shared order list.
373 * @note This is supposed to be called after the vehicle has been inserted
374 * into the shared vehicle chain.
375 * @param v vehicle to add to the list
377 inline void AddVehicle(Vehicle
*v
) { ++this->num_vehicles
; }
379 void RemoveVehicle(Vehicle
*v
);
381 bool IsCompleteTimetable() const;
384 * Gets the total duration of the vehicles timetable or INVALID_TICKS is the timetable is not complete.
385 * @return total timetable duration or INVALID_TICKS for incomplete timetables
387 inline Ticks
GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? this->timetable_duration
: INVALID_TICKS
; }
390 * Gets the known duration of the vehicles timetable even if the timetable is not complete.
391 * @return known timetable duration
393 inline Ticks
GetTimetableDurationIncomplete() const { return this->timetable_duration
; }
396 * Gets the known duration of the vehicles orders, timetabled or not.
397 * @return known order duration.
399 inline Ticks
GetTotalDuration() const { return this->total_duration
; }
402 * Must be called if an order's timetable is changed to update internal book keeping.
403 * @param delta By how many ticks has the timetable duration changed
405 void UpdateTimetableDuration(Ticks delta
) { this->timetable_duration
+= delta
; }
408 * Must be called if an order's timetable is changed to update internal book keeping.
409 * @param delta By how many ticks has the total duration changed
411 void UpdateTotalDuration(Ticks delta
) { this->total_duration
+= delta
; }
413 void FreeChain(bool keep_orderlist
= false);
415 void DebugCheckSanity() const;
418 #define FOR_ALL_ORDERS_FROM(var, start) FOR_ALL_ITEMS_FROM(Order, order_index, var, start)
419 #define FOR_ALL_ORDERS(var) FOR_ALL_ORDERS_FROM(var, 0)
422 #define FOR_VEHICLE_ORDERS(v, order) for (order = (v->orders.list == NULL) ? NULL : v->orders.list->GetFirstOrder(); order != NULL; order = order->next)
425 #define FOR_ALL_ORDER_LISTS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderList, orderlist_index, var, start)
426 #define FOR_ALL_ORDER_LISTS(var) FOR_ALL_ORDER_LISTS_FROM(var, 0)
428 #endif /* ORDER_BASE_H */