2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file order_base.h Base class for orders. */
13 #include "order_type.h"
14 #include "core/pool_type.hpp"
15 #include "core/bitmath_func.hpp"
16 #include "cargo_type.h"
17 #include "depot_type.h"
18 #include "station_type.h"
19 #include "vehicle_type.h"
20 #include "timer/timer_game_tick.h"
21 #include "saveload/saveload.h"
23 typedef Pool
<Order
, OrderID
, 256, 0xFF0000> OrderPool
;
24 typedef Pool
<OrderList
, OrderListID
, 128, 64000> OrderListPool
;
25 extern OrderPool _order_pool
;
26 extern OrderListPool _orderlist_pool
;
28 template <typename
, typename
>
29 class EndianBufferWriter
;
31 /* If you change this, keep in mind that it is saved on 3 places:
32 * - Load_ORDR, all the global orders
33 * - Vehicle -> current_order
34 * - REF_ORDER (all REFs are currently limited to 16 bits!!)
36 struct Order
: OrderPool::PoolItem
<&_order_pool
> {
38 friend struct VEHSChunkHandler
; ///< Loading of ancient vehicles.
39 friend SaveLoadTable
GetOrderDescription(); ///< Saving and loading of orders.
40 /* So we can use private/protected variables in the saveload code */
41 friend class SlVehicleCommon
;
42 friend class SlVehicleDisaster
;
44 template <typename Tcont
, typename Titer
>
45 friend EndianBufferWriter
<Tcont
, Titer
> &operator <<(EndianBufferWriter
<Tcont
, Titer
> &buffer
, const Order
&data
);
46 friend class EndianBufferReader
&operator >>(class EndianBufferReader
&buffer
, Order
&order
);
48 uint8_t type
; ///< The type of order + non-stop flags
49 uint8_t flags
; ///< Load/unload types, depot order/action types.
50 DestinationID dest
; ///< The destination of the order.
52 CargoID refit_cargo
; ///< Refit CargoID
54 uint16_t wait_time
; ///< How long in ticks to wait at the destination.
55 uint16_t travel_time
; ///< How long in ticks the journey to this destination should take.
56 uint16_t max_speed
; ///< How fast the vehicle may go on the way to the destination.
59 Order
*next
; ///< Pointer to next order. If nullptr, end of list
61 Order() : flags(0), refit_cargo(CARGO_NO_REFIT
), wait_time(0), travel_time(0), max_speed(UINT16_MAX
) {}
64 Order(uint32_t packed
);
67 * Check whether this order is of the given type.
68 * @param type the type to check against.
69 * @return true if the order matches.
71 inline bool IsType(OrderType type
) const { return this->GetType() == type
; }
74 * Get the type of order of this order.
75 * @return the order type.
77 inline OrderType
GetType() const { return (OrderType
)GB(this->type
, 0, 4); }
81 void MakeGoToStation(StationID destination
);
82 void MakeGoToDepot(DepotID destination
, OrderDepotTypeFlags order
, OrderNonStopFlags non_stop_type
= ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
, OrderDepotActionFlags action
= ODATF_SERVICE_ONLY
, CargoID cargo
= CARGO_NO_REFIT
);
83 void MakeGoToWaypoint(StationID destination
);
84 void MakeLoading(bool ordered
);
85 void MakeLeaveStation();
87 void MakeConditional(VehicleOrderID order
);
88 void MakeImplicit(StationID destination
);
91 * Is this a 'goto' order with a real destination?
92 * @return True if the type is either #OT_GOTO_WAYPOINT, #OT_GOTO_DEPOT or #OT_GOTO_STATION.
94 inline bool IsGotoOrder() const
96 return IsType(OT_GOTO_WAYPOINT
) || IsType(OT_GOTO_DEPOT
) || IsType(OT_GOTO_STATION
);
100 * Gets the destination of this order.
101 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
102 * @return the destination of the order.
104 inline DestinationID
GetDestination() const { return this->dest
; }
107 * Sets the destination of this order.
108 * @param destination the new destination of the order.
109 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
111 inline void SetDestination(DestinationID destination
) { this->dest
= destination
; }
114 * Is this order a refit order.
115 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
116 * @return true if a refit should happen.
118 inline bool IsRefit() const { return this->refit_cargo
< NUM_CARGO
|| this->refit_cargo
== CARGO_AUTO_REFIT
; }
121 * Is this order a auto-refit order.
122 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
123 * @return true if a auto-refit should happen.
125 inline bool IsAutoRefit() const { return this->refit_cargo
== CARGO_AUTO_REFIT
; }
128 * Get the cargo to to refit to.
129 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
130 * @return the cargo type.
132 inline CargoID
GetRefitCargo() const { return this->refit_cargo
; }
134 void SetRefit(CargoID cargo
);
136 /** How must the consist be loaded? */
137 inline OrderLoadFlags
GetLoadType() const { return (OrderLoadFlags
)GB(this->flags
, 4, 3); }
138 /** How must the consist be unloaded? */
139 inline OrderUnloadFlags
GetUnloadType() const { return (OrderUnloadFlags
)GB(this->flags
, 0, 3); }
140 /** At which stations must we stop? */
141 inline OrderNonStopFlags
GetNonStopType() const { return (OrderNonStopFlags
)GB(this->type
, 6, 2); }
142 /** Where must we stop at the platform? */
143 inline OrderStopLocation
GetStopLocation() const { return (OrderStopLocation
)GB(this->type
, 4, 2); }
144 /** What caused us going to the depot? */
145 inline OrderDepotTypeFlags
GetDepotOrderType() const { return (OrderDepotTypeFlags
)GB(this->flags
, 0, 3); }
146 /** What are we going to do when in the depot. */
147 inline OrderDepotActionFlags
GetDepotActionType() const { return (OrderDepotActionFlags
)GB(this->flags
, 3, 4); }
148 /** What variable do we have to compare? */
149 inline OrderConditionVariable
GetConditionVariable() const { return (OrderConditionVariable
)GB(this->dest
, 11, 5); }
150 /** What is the comparator to use? */
151 inline OrderConditionComparator
GetConditionComparator() const { return (OrderConditionComparator
)GB(this->type
, 5, 3); }
152 /** Get the order to skip to. */
153 inline VehicleOrderID
GetConditionSkipToOrder() const { return this->flags
; }
154 /** Get the value to base the skip on. */
155 inline uint16_t GetConditionValue() const { return GB(this->dest
, 0, 11); }
157 /** Set how the consist must be loaded. */
158 inline void SetLoadType(OrderLoadFlags load_type
) { SB(this->flags
, 4, 3, load_type
); }
159 /** Set how the consist must be unloaded. */
160 inline void SetUnloadType(OrderUnloadFlags unload_type
) { SB(this->flags
, 0, 3, unload_type
); }
161 /** Set whether we must stop at stations or not. */
162 inline void SetNonStopType(OrderNonStopFlags non_stop_type
) { SB(this->type
, 6, 2, non_stop_type
); }
163 /** Set where we must stop at the platform. */
164 inline void SetStopLocation(OrderStopLocation stop_location
) { SB(this->type
, 4, 2, stop_location
); }
165 /** Set the cause to go to the depot. */
166 inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type
) { SB(this->flags
, 0, 3, depot_order_type
); }
167 /** Set what we are going to do in the depot. */
168 inline void SetDepotActionType(OrderDepotActionFlags depot_service_type
) { SB(this->flags
, 3, 4, depot_service_type
); }
169 /** Set variable we have to compare. */
170 inline void SetConditionVariable(OrderConditionVariable condition_variable
) { SB(this->dest
, 11, 5, condition_variable
); }
171 /** Set the comparator to use. */
172 inline void SetConditionComparator(OrderConditionComparator condition_comparator
) { SB(this->type
, 5, 3, condition_comparator
); }
173 /** Get the order to skip to. */
174 inline void SetConditionSkipToOrder(VehicleOrderID order_id
) { this->flags
= order_id
; }
175 /** Set the value to base the skip on. */
176 inline void SetConditionValue(uint16_t value
) { SB(this->dest
, 0, 11, value
); }
178 /* As conditional orders write their "skip to" order all over the flags, we cannot check the
179 * flags to find out if timetabling is enabled. However, as conditional orders are never
180 * autofilled we can be sure that any non-zero values for their wait_time and travel_time are
181 * explicitly set (but travel_time is actually unused for conditionals). */
183 /** Does this order have an explicit wait time set? */
184 inline bool IsWaitTimetabled() const { return this->IsType(OT_CONDITIONAL
) ? this->wait_time
> 0 : HasBit(this->flags
, 3); }
185 /** Does this order have an explicit travel time set? */
186 inline bool IsTravelTimetabled() const { return this->IsType(OT_CONDITIONAL
) ? this->travel_time
> 0 : HasBit(this->flags
, 7); }
188 /** Get the time in ticks a vehicle should wait at the destination or 0 if it's not timetabled. */
189 inline uint16_t GetTimetabledWait() const { return this->IsWaitTimetabled() ? this->wait_time
: 0; }
190 /** Get the time in ticks a vehicle should take to reach the destination or 0 if it's not timetabled. */
191 inline uint16_t GetTimetabledTravel() const { return this->IsTravelTimetabled() ? this->travel_time
: 0; }
192 /** Get the time in ticks a vehicle will probably wait at the destination (timetabled or not). */
193 inline uint16_t GetWaitTime() const { return this->wait_time
; }
194 /** Get the time in ticks a vehicle will probably take to reach the destination (timetabled or not). */
195 inline uint16_t GetTravelTime() const { return this->travel_time
; }
198 * Get the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
200 * @return maximum speed.
202 inline uint16_t GetMaxSpeed() const { return this->max_speed
; }
204 /** Set if the wait time is explicitly timetabled (unless the order is conditional). */
205 inline void SetWaitTimetabled(bool timetabled
) { if (!this->IsType(OT_CONDITIONAL
)) SB(this->flags
, 3, 1, timetabled
? 1 : 0); }
206 /** Set if the travel time is explicitly timetabled (unless the order is conditional). */
207 inline void SetTravelTimetabled(bool timetabled
) { if (!this->IsType(OT_CONDITIONAL
)) SB(this->flags
, 7, 1, timetabled
? 1 : 0); }
210 * Set the time in ticks to wait at the destination.
211 * @param time Time to set as wait time.
213 inline void SetWaitTime(uint16_t time
) { this->wait_time
= time
; }
216 * Set the time in ticks to take for travelling to the destination.
217 * @param time Time to set as travel time.
219 inline void SetTravelTime(uint16_t time
) { this->travel_time
= time
; }
222 * Set the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
224 * @param speed Speed to be set.
226 inline void SetMaxSpeed(uint16_t speed
) { this->max_speed
= speed
; }
228 bool ShouldStopAtStation(const Vehicle
*v
, StationID station
) const;
229 bool CanLoadOrUnload() const;
230 bool CanLeaveWithCargo(bool has_cargo
) const;
232 TileIndex
GetLocation(const Vehicle
*v
, bool airport
= false) const;
234 /** Checks if travel_time and wait_time apply to this order and if they are timetabled. */
235 inline bool IsCompletelyTimetabled() const
237 if (!this->IsTravelTimetabled() && !this->IsType(OT_CONDITIONAL
)) return false;
238 if (!this->IsWaitTimetabled() && this->IsType(OT_GOTO_STATION
) &&
239 !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
)) {
245 void AssignOrder(const Order
&other
);
246 bool Equals(const Order
&other
) const;
248 uint32_t Pack() const;
249 uint16_t MapOldOrder() const;
250 void ConvertFromOldSavegame();
253 void InsertOrder(Vehicle
*v
, Order
*new_o
, VehicleOrderID sel_ord
);
254 void DeleteOrder(Vehicle
*v
, VehicleOrderID sel_ord
);
257 * Shared order list linking together the linked list of orders and the list
258 * of vehicles sharing this order list.
260 struct OrderList
: OrderListPool::PoolItem
<&_orderlist_pool
> {
262 friend void AfterLoadVehicles(bool part_of_load
); ///< For instantiating the shared vehicle chain
263 friend SaveLoadTable
GetOrderListDescription(); ///< Saving and loading of order lists.
265 Order
*first
; ///< First order of the order list.
266 VehicleOrderID num_orders
; ///< NOSAVE: How many orders there are in the list.
267 VehicleOrderID num_manual_orders
; ///< NOSAVE: How many manually added orders are there in the list.
268 uint num_vehicles
; ///< NOSAVE: Number of vehicles that share this order list.
269 Vehicle
*first_shared
; ///< NOSAVE: pointer to the first vehicle in the shared order chain.
271 TimerGameTick::Ticks timetable_duration
; ///< NOSAVE: Total timetabled duration of the order list.
272 TimerGameTick::Ticks total_duration
; ///< NOSAVE: Total (timetabled or not) duration of the order list.
275 /** Default constructor producing an invalid order list. */
276 OrderList(VehicleOrderID num_orders
= INVALID_VEH_ORDER_ID
)
277 : first(nullptr), num_orders(num_orders
), num_manual_orders(0), num_vehicles(0), first_shared(nullptr),
278 timetable_duration(0), total_duration(0) { }
281 * Create an order list with the given order chain for the given vehicle.
282 * @param chain pointer to the first order of the order chain
283 * @param v any vehicle using this orderlist
285 OrderList(Order
*chain
, Vehicle
*v
) { this->Initialize(chain
, v
); }
287 /** Destructor. Invalidates OrderList for re-usage by the pool. */
290 void Initialize(Order
*chain
, Vehicle
*v
);
292 void RecalculateTimetableDuration();
295 * Get the first order of the order chain.
296 * @return the first order of the chain.
298 inline Order
*GetFirstOrder() const { return this->first
; }
300 Order
*GetOrderAt(int index
) const;
303 * Get the last order of the order chain.
304 * @return the last order of the chain.
306 inline Order
*GetLastOrder() const { return this->GetOrderAt(this->num_orders
- 1); }
309 * Get the order after the given one or the first one, if the given one is the
311 * @param curr Order to find the next one for.
312 * @return Next order.
314 inline const Order
*GetNext(const Order
*curr
) const { return (curr
->next
== nullptr) ? this->GetFirstOrder() : curr
->next
; }
317 * Get number of orders in the order list.
318 * @return number of orders in the chain.
320 inline VehicleOrderID
GetNumOrders() const { return this->num_orders
; }
323 * Get number of manually added orders in the order list.
324 * @return number of manual orders in the chain.
326 inline VehicleOrderID
GetNumManualOrders() const { return this->num_manual_orders
; }
328 StationIDStack
GetNextStoppingStation(const Vehicle
*v
, const Order
*first
= nullptr, uint hops
= 0) const;
329 const Order
*GetNextDecisionNode(const Order
*next
, uint hops
) const;
331 void InsertOrderAt(Order
*new_order
, int index
);
332 void DeleteOrderAt(int index
);
333 void MoveOrder(int from
, int to
);
336 * Is this a shared order list?
337 * @return whether this order list is shared among multiple vehicles
339 inline bool IsShared() const { return this->num_vehicles
> 1; };
342 * Get the first vehicle of this vehicle chain.
343 * @return the first vehicle of the chain.
345 inline Vehicle
*GetFirstSharedVehicle() const { return this->first_shared
; }
348 * Return the number of vehicles that share this orders list
349 * @return the count of vehicles that use this shared orders list
351 inline uint
GetNumVehicles() const { return this->num_vehicles
; }
354 * Adds the given vehicle to this shared order list.
355 * @note This is supposed to be called after the vehicle has been inserted
356 * into the shared vehicle chain.
357 * @param v vehicle to add to the list
359 inline void AddVehicle([[maybe_unused
]] Vehicle
*v
) { ++this->num_vehicles
; }
361 void RemoveVehicle(Vehicle
*v
);
363 bool IsCompleteTimetable() const;
366 * Gets the total duration of the vehicles timetable or Ticks::INVALID_TICKS is the timetable is not complete.
367 * @return total timetable duration or Ticks::INVALID_TICKS for incomplete timetables
369 inline TimerGameTick::Ticks
GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? this->timetable_duration
: Ticks::INVALID_TICKS
; }
372 * Gets the known duration of the vehicles timetable even if the timetable is not complete.
373 * @return known timetable duration
375 inline TimerGameTick::Ticks
GetTimetableDurationIncomplete() const { return this->timetable_duration
; }
378 * Gets the known duration of the vehicles orders, timetabled or not.
379 * @return known order duration.
381 inline TimerGameTick::Ticks
GetTotalDuration() const { return this->total_duration
; }
384 * Must be called if an order's timetable is changed to update internal book keeping.
385 * @param delta By how many ticks has the timetable duration changed
387 void UpdateTimetableDuration(TimerGameTick::Ticks delta
) { this->timetable_duration
+= delta
; }
390 * Must be called if an order's timetable is changed to update internal book keeping.
391 * @param delta By how many ticks has the total duration changed
393 void UpdateTotalDuration(TimerGameTick::Ticks delta
) { this->total_duration
+= delta
; }
395 void FreeChain(bool keep_orderlist
= false);
397 void DebugCheckSanity() const;
400 #endif /* ORDER_BASE_H */