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"
25 /* If you change this, keep in mind that it is saved on 3 places:
26 * - Load_ORDR, all the global orders
27 * - Vehicle -> current_order
28 * - REF_ORDER (all REFs are currently limited to 16 bits!!)
30 struct Order
: PooledItem
<Order
, OrderID
, 256, 64000> {
32 friend const struct SaveLoad
*GetVehicleDescription(VehicleType vt
); ///< Saving and loading the current order of vehicles.
33 friend void Load_VEHS(LoadBuffer
*reader
); ///< Loading of ancient vehicles.
34 friend const struct SaveLoad
*GetOrderDescription(); ///< Saving and loading of orders.
36 uint8 type
; ///< The type of order + non-stop flags
37 uint8 flags
; ///< Load/unload types, depot order/action types.
38 DestinationID dest
; ///< The destination of the order.
40 CargoID refit_cargo
; ///< Refit CargoID
43 Order
*next
; ///< Pointer to next order. If NULL, end of list
45 uint16 wait_time
; ///< How long in ticks to wait at the destination.
46 uint16 travel_time
; ///< How long in ticks the journey to this destination should take.
47 uint16 max_speed
; ///< How fast the vehicle may go on the way to the destination.
49 Order() : refit_cargo(CT_NO_REFIT
), max_speed(UINT16_MAX
) {}
55 * Check whether this order is of the given type.
56 * @param type the type to check against.
57 * @return true if the order matches.
59 inline bool IsType(OrderType type
) const { return this->GetType() == type
; }
62 * Get the type of order of this order.
63 * @return the order type.
65 inline OrderType
GetType() const { return (OrderType
)GB(this->type
, 0, 4); }
69 void MakeGoToStation(StationID destination
);
70 void MakeGoToDepot(DepotID destination
, OrderDepotTypeFlags order
, OrderNonStopFlags non_stop_type
= ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
, OrderDepotActionFlags action
= ODATF_SERVICE_ONLY
, CargoID cargo
= CT_NO_REFIT
);
71 void MakeGoToWaypoint(StationID destination
);
72 void MakeLoading(bool ordered
);
73 void MakeLeaveStation();
75 void MakeConditional(VehicleOrderID order
);
76 void MakeImplicit(StationID destination
);
79 * Is this a 'goto' order with a real destination?
80 * @return True if the type is either #OT_GOTO_WAYPOINT, #OT_GOTO_DEPOT or #OT_GOTO_STATION.
82 inline bool IsGotoOrder() const
84 return IsType(OT_GOTO_WAYPOINT
) || IsType(OT_GOTO_DEPOT
) || IsType(OT_GOTO_STATION
);
88 * Gets the destination of this order.
89 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
90 * @return the destination of the order.
92 inline DestinationID
GetDestination() const { return this->dest
; }
95 * Sets the destination of this order.
96 * @param destination the new destination of the order.
97 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
99 inline void SetDestination(DestinationID destination
) { this->dest
= destination
; }
102 * Is this order a refit order.
103 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
104 * @return true if a refit should happen.
106 inline bool IsRefit() const { return this->refit_cargo
< NUM_CARGO
|| this->refit_cargo
== CT_AUTO_REFIT
; }
109 * Is this order a auto-refit order.
110 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
111 * @return true if a auto-refit should happen.
113 inline bool IsAutoRefit() const { return this->refit_cargo
== CT_AUTO_REFIT
; }
116 * Get the cargo to to refit to.
117 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
118 * @return the cargo type.
120 inline CargoID
GetRefitCargo() const { return this->refit_cargo
; }
122 void SetRefit(CargoID cargo
);
124 /** How must the consist be loaded? */
125 inline OrderLoadFlags
GetLoadType() const { return (OrderLoadFlags
)GB(this->flags
, 4, 4); }
126 /** How must the consist be unloaded? */
127 inline OrderUnloadFlags
GetUnloadType() const { return (OrderUnloadFlags
)GB(this->flags
, 0, 4); }
128 /** At which stations must we stop? */
129 inline OrderNonStopFlags
GetNonStopType() const { return (OrderNonStopFlags
)GB(this->type
, 6, 2); }
130 /** Where must we stop at the platform? */
131 inline OrderStopLocation
GetStopLocation() const { return (OrderStopLocation
)GB(this->type
, 4, 2); }
132 /** What caused us going to the depot? */
133 inline OrderDepotTypeFlags
GetDepotOrderType() const { return (OrderDepotTypeFlags
)GB(this->flags
, 0, 4); }
134 /** What are we going to do when in the depot. */
135 inline OrderDepotActionFlags
GetDepotActionType() const { return (OrderDepotActionFlags
)GB(this->flags
, 4, 4); }
136 /** What variable do we have to compare? */
137 inline OrderConditionVariable
GetConditionVariable() const { return (OrderConditionVariable
)GB(this->dest
, 11, 5); }
138 /** What is the comparator to use? */
139 inline OrderConditionComparator
GetConditionComparator() const { return (OrderConditionComparator
)GB(this->type
, 5, 3); }
140 /** Get the order to skip to. */
141 inline VehicleOrderID
GetConditionSkipToOrder() const { return this->flags
; }
142 /** Get the value to base the skip on. */
143 inline uint16
GetConditionValue() const { return GB(this->dest
, 0, 11); }
145 /** Set how the consist must be loaded. */
146 inline void SetLoadType(OrderLoadFlags load_type
) { SB(this->flags
, 4, 4, load_type
); }
147 /** Set how the consist must be unloaded. */
148 inline void SetUnloadType(OrderUnloadFlags unload_type
) { SB(this->flags
, 0, 4, unload_type
); }
149 /** Set whether we must stop at stations or not. */
150 inline void SetNonStopType(OrderNonStopFlags non_stop_type
) { SB(this->type
, 6, 2, non_stop_type
); }
151 /** Set where we must stop at the platform. */
152 inline void SetStopLocation(OrderStopLocation stop_location
) { SB(this->type
, 4, 2, stop_location
); }
153 /** Set the cause to go to the depot. */
154 inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type
) { SB(this->flags
, 0, 4, depot_order_type
); }
155 /** Set what we are going to do in the depot. */
156 inline void SetDepotActionType(OrderDepotActionFlags depot_service_type
) { SB(this->flags
, 4, 4, depot_service_type
); }
157 /** Set variable we have to compare. */
158 inline void SetConditionVariable(OrderConditionVariable condition_variable
) { SB(this->dest
, 11, 5, condition_variable
); }
159 /** Set the comparator to use. */
160 inline void SetConditionComparator(OrderConditionComparator condition_comparator
) { SB(this->type
, 5, 3, condition_comparator
); }
161 /** Get the order to skip to. */
162 inline void SetConditionSkipToOrder(VehicleOrderID order_id
) { this->flags
= order_id
; }
163 /** Set the value to base the skip on. */
164 inline void SetConditionValue(uint16 value
) { SB(this->dest
, 0, 11, value
); }
166 bool ShouldStopAtStation(const Vehicle
*v
, StationID station
) const;
167 bool CanLoadOrUnload() const;
168 bool CanLeaveWithCargo(bool has_cargo
) const;
170 TileIndex
GetLocation(const Vehicle
*v
, bool airport
= false) const;
172 /** Checks if this order has travel_time and if needed wait_time set. */
173 inline bool IsCompletelyTimetabled() const
175 if (this->travel_time
== 0 && !this->IsType(OT_CONDITIONAL
)) return false;
176 if (this->wait_time
== 0 && this->IsType(OT_GOTO_STATION
) && !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
)) return false;
180 void AssignOrder(const Order
&other
);
181 bool Equals(const Order
&other
) const;
184 uint16
MapOldOrder() const;
185 void ConvertFromOldSavegame(const SavegameTypeVersion
*stv
);
188 void InsertOrder(Vehicle
*v
, Order
*new_o
, VehicleOrderID sel_ord
);
189 void DeleteOrder(Vehicle
*v
, VehicleOrderID sel_ord
);
192 * Shared order list linking together the linked list of orders and the list
193 * of vehicles sharing this order list.
195 struct OrderList
: PooledItem
<OrderList
, OrderListID
, 128, 64000> {
197 friend void AfterLoadVehicles(const SavegameTypeVersion
*stv
); ///< For instantiating the shared vehicle chain
198 friend const struct SaveLoad
*GetOrderListDescription(); ///< Saving and loading of order lists.
200 StationID
GetBestLoadableNext(const Vehicle
*v
, const Order
*o1
, const Order
*o2
) const;
202 Order
*first
; ///< First order of the order list.
203 VehicleOrderID num_orders
; ///< NOSAVE: How many orders there are in the list.
204 VehicleOrderID num_manual_orders
; ///< NOSAVE: How many manually added orders are there in the list.
205 uint num_vehicles
; ///< NOSAVE: Number of vehicles that share this order list.
206 Vehicle
*first_shared
; ///< NOSAVE: pointer to the first vehicle in the shared order chain.
208 Ticks timetable_duration
; ///< NOSAVE: Total duration of the order list
211 /** Default constructor producing an invalid order list. */
212 OrderList(VehicleOrderID num_orders
= INVALID_VEH_ORDER_ID
)
213 : first(NULL
), num_orders(num_orders
), num_manual_orders(0), num_vehicles(0), first_shared(NULL
),
214 timetable_duration(0) { }
217 * Create an order list with the given order chain for the given vehicle.
218 * @param chain pointer to the first order of the order chain
219 * @param v any vehicle using this orderlist
221 OrderList(Order
*chain
, Vehicle
*v
) { this->Initialize(chain
, v
); }
223 /** Destructor. Invalidates OrderList for re-usage by the pool. */
226 void Initialize(Order
*chain
, Vehicle
*v
);
229 * Get the first order of the order chain.
230 * @return the first order of the chain.
232 inline Order
*GetFirstOrder() const { return this->first
; }
234 Order
*GetOrderAt(int index
) const;
237 * Get the last order of the order chain.
238 * @return the last order of the chain.
240 inline Order
*GetLastOrder() const { return this->GetOrderAt(this->num_orders
- 1); }
243 * Get the order after the given one or the first one, if the given one is the
245 * @param curr Order to find the next one for.
246 * @return Next order.
248 inline const Order
*GetNext(const Order
*curr
) const { return (curr
->next
== NULL
) ? this->GetFirstOrder() : curr
->next
; }
251 * Get number of orders in the order list.
252 * @return number of orders in the chain.
254 inline VehicleOrderID
GetNumOrders() const { return this->num_orders
; }
257 * Get number of manually added orders in the order list.
258 * @return number of manual orders in the chain.
260 inline VehicleOrderID
GetNumManualOrders() const { return this->num_manual_orders
; }
262 StationIDStack
GetNextStoppingStation(const Vehicle
*v
, const Order
*first
= NULL
, uint hops
= 0) const;
263 const Order
*GetNextDecisionNode(const Order
*next
, uint hops
) const;
265 void InsertOrderAt(Order
*new_order
, int index
);
266 void DeleteOrderAt(int index
);
267 void MoveOrder(int from
, int to
);
270 * Is this a shared order list?
271 * @return whether this order list is shared among multiple vehicles
273 inline bool IsShared() const { return this->num_vehicles
> 1; };
276 * Get the first vehicle of this vehicle chain.
277 * @return the first vehicle of the chain.
279 inline Vehicle
*GetFirstSharedVehicle() const { return this->first_shared
; }
282 * Return the number of vehicles that share this orders list
283 * @return the count of vehicles that use this shared orders list
285 inline uint
GetNumVehicles() const { return this->num_vehicles
; }
287 bool IsVehicleInSharedOrdersList(const Vehicle
*v
) const;
288 int GetPositionInSharedOrderList(const Vehicle
*v
) const;
291 * Adds the given vehicle to this shared order list.
292 * @note This is supposed to be called after the vehicle has been inserted
293 * into the shared vehicle chain.
294 * @param v vehicle to add to the list
296 inline void AddVehicle(Vehicle
*v
) { ++this->num_vehicles
; }
298 void RemoveVehicle(Vehicle
*v
);
300 bool IsCompleteTimetable() const;
303 * Gets the total duration of the vehicles timetable or INVALID_TICKS is the timetable is not complete.
304 * @return total timetable duration or INVALID_TICKS for incomplete timetables
306 inline Ticks
GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? this->timetable_duration
: INVALID_TICKS
; }
309 * Gets the known duration of the vehicles timetable even if the timetable is not complete.
310 * @return known timetable duration
312 inline Ticks
GetTimetableDurationIncomplete() const { return this->timetable_duration
; }
315 * Must be called if an order's timetable is changed to update internal book keeping.
316 * @param delta By how many ticks has the timetable duration changed
318 void UpdateOrderTimetable(Ticks delta
) { this->timetable_duration
+= delta
; }
320 void FreeChain(bool keep_orderlist
= false);
322 void DebugCheckSanity() const;
325 #define FOR_ALL_ORDERS_FROM(var, start) FOR_ALL_ITEMS_FROM(Order, order_index, var, start)
326 #define FOR_ALL_ORDERS(var) FOR_ALL_ORDERS_FROM(var, 0)
329 #define FOR_VEHICLE_ORDERS(v, order) for (order = (v->orders.list == NULL) ? NULL : v->orders.list->GetFirstOrder(); order != NULL; order = order->next)
332 #define FOR_ALL_ORDER_LISTS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderList, orderlist_index, var, start)
333 #define FOR_ALL_ORDER_LISTS(var) FOR_ALL_ORDER_LISTS_FROM(var, 0)
335 #endif /* ORDER_BASE_H */