Hotfix: Conditional order comparator dropdown list was broken.
[openttd-joker.git] / src / order_base.h
blob59e64dd8940233ce1a6167b69f9bef2ee6e03845
1 /* $Id: order_base.h 26267 2014-01-19 09:27:44Z fonsinchen $ */
3 /*
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/>.
8 */
10 /** @file order_base.h Base class for orders. */
12 #ifndef ORDER_BASE_H
13 #define ORDER_BASE_H
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 "date_func.h"
25 #include <memory>
26 #include <vector>
28 typedef Pool<Order, OrderID, 256, 64000> OrderPool;
29 typedef Pool<OrderList, OrderListID, 128, 64000> OrderListPool;
30 extern OrderPool _order_pool;
31 extern OrderListPool _orderlist_pool;
33 struct OrderExtraInfo {
34 uint8 cargo_type_flags[NUM_CARGO] = {}; ///< Load/unload types for each cargo type.
35 uint32 xdata = 0; ///< Extra arbitrary data
36 uint8 xflags = 0; ///< Extra flags
39 /* If you change this, keep in mind that it is saved on 3 places:
40 * - Load_ORDR, all the global orders
41 * - Vehicle -> current_order
42 * - REF_ORDER (all REFs are currently limited to 16 bits!!)
44 struct Order : OrderPool::PoolItem<&_order_pool> {
45 private:
46 friend const struct SaveLoad *GetVehicleDescription(VehicleType vt); ///< Saving and loading the current order of vehicles.
47 friend void Load_VEHS(); ///< Loading of ancient vehicles.
48 friend const struct SaveLoad *GetOrderDescription(); ///< Saving and loading of orders.
49 friend void Load_ORDX(); ///< Saving and loading of orders.
50 friend void Save_ORDX(); ///< Saving and loading of orders.
51 friend void Load_VEOX(); ///< Saving and loading of orders.
52 friend void Save_VEOX(); ///< Saving and loading of orders.
54 uint8 type; ///< The type of order + non-stop flags
55 uint8 flags; ///< Load/unload types, depot order/action types.
56 DestinationID dest; ///< The destination of the order.
58 CargoID refit_cargo; ///< Refit CargoID
60 std::unique_ptr<OrderExtraInfo> extra; ///< Extra order info
62 int8 jump_counter; ///< Counter for the 'jump xx% of times' option
64 uint16 wait_time; ///< How long in ticks to wait at the destination.
65 uint16 travel_time; ///< How long in ticks the journey to this destination should take.
66 uint16 max_speed; ///< How fast the vehicle may go on the way to the destination.
68 void AllocExtraInfo();
69 void DeAllocExtraInfo();
71 inline void CheckExtraInfoAlloced()
73 if (!this->extra) this->AllocExtraInfo();
76 inline uint8 GetXFlags() const
78 return this->extra != nullptr ? this->extra->xflags : 0;
81 inline uint8 &GetXFlagsRef()
83 CheckExtraInfoAlloced();
84 return this->extra->xflags;
87 public:
88 inline uint32 GetXData() const
90 return this->extra != nullptr ? this->extra->xdata : 0;
93 inline uint32& GetXDataRef()
95 CheckExtraInfoAlloced();
96 return this->extra->xdata;
99 Order *next; ///< Pointer to next order. If NULL, end of list
101 Order() : refit_cargo(CT_NO_REFIT), max_speed(UINT16_MAX) {}
102 ~Order();
104 Order(uint32 packed);
106 Order(const Order& other)
108 *this = other;
111 Order(Order&& other) = default;
113 inline Order& operator=(Order const& other)
115 AssignOrder(other);
116 this->next = other.next;
117 this->index = other.index;
118 return *this;
122 * Check whether this order is of the given type.
123 * @param type the type to check against.
124 * @return true if the order matches.
126 inline bool IsType(OrderType type) const { return this->GetType() == type; }
129 * Get the type of order of this order.
130 * @return the order type.
132 inline OrderType GetType() const { return (OrderType)GB(this->type, 0, 4); }
134 void Free();
136 void MakeGoToStation(StationID destination);
137 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);
138 void MakeGoToWaypoint(StationID destination);
139 void MakeLoading(bool ordered);
140 void MakeLeaveStation();
141 void MakeDummy();
142 void MakeConditional(VehicleOrderID order);
143 void MakeImplicit(StationID destination);
144 void MakeWaiting();
147 * Is this a 'goto' order with a real destination?
148 * @return True if the type is either #OT_GOTO_WAYPOINT, #OT_GOTO_DEPOT or #OT_GOTO_STATION.
150 inline bool IsGotoOrder() const
152 return IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION);
156 * Gets the destination of this order.
157 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
158 * @return the destination of the order.
160 inline DestinationID GetDestination() const { return this->dest; }
163 * Sets the destination of this order.
164 * @param destination the new destination of the order.
165 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
167 inline void SetDestination(DestinationID destination) { this->dest = destination; }
170 * Is this order a refit order.
171 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
172 * @return true if a refit should happen.
174 inline bool IsRefit() const { return this->refit_cargo < NUM_CARGO || this->refit_cargo == CT_AUTO_REFIT; }
177 * Is this order a auto-refit order.
178 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
179 * @return true if a auto-refit should happen.
181 inline bool IsAutoRefit() const { return this->refit_cargo == CT_AUTO_REFIT; }
184 * Get the cargo to to refit to.
185 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
186 * @return the cargo type.
188 inline CargoID GetRefitCargo() const { return this->refit_cargo; }
190 void SetRefit(CargoID cargo);
193 * Update the jump_counter of this order.
194 * @param the jump chance in %.
195 * @return whether to jump or not.
196 * @pre IsType(OT_CONDITIONAL) && this->GetConditionVariable() == OCV_PERCENT.
198 bool UpdateJumpCounter(uint8 percent);
200 /** How must the consist be loaded? */
201 inline OrderLoadFlags GetLoadType() const
203 OrderLoadFlags type = (OrderLoadFlags)GB(this->flags, 4, 3);
204 if (type == OLFB_CARGO_TYPE_LOAD_ENCODING) type = OLFB_CARGO_TYPE_LOAD;
205 return type;
209 * How must the consist be loaded for this type of cargo?
210 * @pre GetLoadType() == OLFB_CARGO_TYPE_LOAD
211 * @param cargo_id The cargo type index.
212 * @return The load type for this cargo.
214 inline OrderLoadFlags GetCargoLoadTypeRaw(CargoID cargo_id) const
216 assert(cargo_id < NUM_CARGO);
218 if (!this->extra) return OLF_LOAD_IF_POSSIBLE;
219 return (OrderLoadFlags)GB(this->extra->cargo_type_flags[cargo_id], 4, 4);
223 * How must the consist be loaded for this type of cargo?
224 * @param cargo_id The cargo type index.
225 * @return The load type for this cargo.
227 inline OrderLoadFlags GetCargoLoadType(CargoID cargo_id) const
229 assert(cargo_id < NUM_CARGO);
230 OrderLoadFlags olf = this->GetLoadType();
231 if (olf == OLFB_CARGO_TYPE_LOAD) olf = this->GetCargoLoadTypeRaw(cargo_id);
232 return olf;
235 /** How must the consist be unloaded? */
236 inline OrderUnloadFlags GetUnloadType() const
238 OrderUnloadFlags type = (OrderUnloadFlags)GB(this->flags, 0, 3);
239 if (type == OUFB_CARGO_TYPE_UNLOAD_ENCODING) type = OUFB_CARGO_TYPE_UNLOAD;
240 return type;
244 * How must the consist be unloaded for this type of cargo?
245 * @pre GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD
246 * @param cargo_id The cargo type index.
247 * @return The unload type for this cargo.
249 inline OrderUnloadFlags GetCargoUnloadTypeRaw(CargoID cargo_id) const
251 assert(cargo_id < NUM_CARGO);
252 if (!this->extra) return OUF_UNLOAD_IF_POSSIBLE;
253 return (OrderUnloadFlags)GB(this->extra->cargo_type_flags[cargo_id], 0, 4);
257 * How must the consist be unloaded for this type of cargo?
258 * @param cargo_id The cargo type index.
259 * @return The unload type for this cargo.
261 inline OrderUnloadFlags GetCargoUnloadType(CargoID cargo_id) const
263 assert(cargo_id < NUM_CARGO);
264 OrderUnloadFlags ouf = this->GetUnloadType();
265 if (ouf == OUFB_CARGO_TYPE_UNLOAD) ouf = this->GetCargoUnloadTypeRaw(cargo_id);
266 return ouf;
269 template <typename F> uint32 FilterLoadUnloadTypeCargoMask(F filter_func, uint32 cargo_mask = ~0)
271 if ((this->GetLoadType() == OLFB_CARGO_TYPE_LOAD) || (this->GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD)) {
272 CargoID cargo;
273 uint32 output_mask = cargo_mask;
274 FOR_EACH_SET_BIT(cargo, cargo_mask) {
275 if (!filter_func(this, cargo)) ClrBit(output_mask, cargo);
277 return output_mask;
278 } else {
279 return filter_func(this, FindFirstBit(cargo_mask)) ? cargo_mask : 0;
283 /** At which stations must we stop? */
284 inline OrderNonStopFlags GetNonStopType() const { return (OrderNonStopFlags)GB(this->type, 6, 2); }
285 /** Where must we stop at the platform? */
286 inline OrderStopLocation GetStopLocation() const { return (OrderStopLocation)GB(this->type, 4, 2); }
287 /** What caused us going to the depot? */
288 inline OrderDepotTypeFlags GetDepotOrderType() const { return (OrderDepotTypeFlags)GB(this->flags, 0, 3); }
289 /** What are we going to do when in the depot. */
290 inline OrderDepotActionFlags GetDepotActionType() const { return (OrderDepotActionFlags)GB(this->flags, 4, 3); }
291 /** What waypoint flags? */
292 inline OrderWaypointFlags GetWaypointFlags() const { return (OrderWaypointFlags)GB(this->flags, 0, 8); }
293 /** What variable do we have to compare? */
294 inline OrderConditionVariable GetConditionVariable() const { return (OrderConditionVariable)GB(this->dest, 11, 5); }
295 /** What is the comparator to use? */
296 inline OrderConditionComparator GetConditionComparator() const { return (OrderConditionComparator)GB(this->type, 5, 3); }
297 /** Get the order to skip to. */
298 inline VehicleOrderID GetConditionSkipToOrder() const { return this->flags; }
299 /** Get the value to base the skip on. */
300 inline uint16 GetConditionValue() const { return GB(this->dest, 0, 11); }
302 /** Set how the consist must be loaded. */
303 inline void SetLoadType(OrderLoadFlags load_type)
305 if (load_type == OLFB_CARGO_TYPE_LOAD) load_type = OLFB_CARGO_TYPE_LOAD_ENCODING;
306 SB(this->flags, 4, 3, load_type);
310 * Set how the consist must be loaded for this type of cargo.
311 * @pre GetLoadType() == OLFB_CARGO_TYPE_LOAD
312 * @param load_type The load type.
313 * @param cargo_id The cargo type index.
315 inline void SetLoadType(OrderLoadFlags load_type, CargoID cargo_id)
317 assert(cargo_id < NUM_CARGO);
318 this->CheckExtraInfoAlloced();
319 SB(this->extra->cargo_type_flags[cargo_id], 4, 4, load_type);
321 /** Set how the consist must be unloaded. */
322 inline void SetUnloadType(OrderUnloadFlags unload_type)
324 if (unload_type == OUFB_CARGO_TYPE_UNLOAD) unload_type = OUFB_CARGO_TYPE_UNLOAD_ENCODING;
325 SB(this->flags, 0, 3, unload_type);
329 * Set how the consist must be unloaded for this type of cargo.
330 * @pre GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD
331 * @param unload_type The unload type.
332 * @param cargo_id The cargo type index.
334 inline void SetUnloadType(OrderUnloadFlags unload_type, CargoID cargo_id)
336 assert(cargo_id < NUM_CARGO);
337 this->CheckExtraInfoAlloced();
338 SB(this->extra->cargo_type_flags[cargo_id], 0, 4, unload_type);
340 /** Set whether we must stop at stations or not. */
341 inline void SetNonStopType(OrderNonStopFlags non_stop_type) { SB(this->type, 6, 2, non_stop_type); }
342 /** Set where we must stop at the platform. */
343 inline void SetStopLocation(OrderStopLocation stop_location) { SB(this->type, 4, 2, stop_location); }
344 /** Set the cause to go to the depot. */
345 inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type) { SB(this->flags, 0, 3, depot_order_type); }
346 /** Set what we are going to do in the depot. */
347 inline void SetDepotActionType(OrderDepotActionFlags depot_service_type) { SB(this->flags, 4, 3, depot_service_type); }
348 /** Set waypoint flags. */
349 inline void SetWaypointFlags(OrderWaypointFlags waypoint_flags) { SB(this->flags, 0, 8, waypoint_flags); }
350 /** Set variable we have to compare. */
351 inline void SetConditionVariable(OrderConditionVariable condition_variable) { SB(this->dest, 11, 5, condition_variable); }
352 /** Set the comparator to use. */
353 inline void SetConditionComparator(OrderConditionComparator condition_comparator) { SB(this->type, 5, 3, condition_comparator); }
354 /** Get the order to skip to. */
355 inline void SetConditionSkipToOrder(VehicleOrderID order_id) { this->flags = order_id; }
356 /** Set the value to base the skip on. */
357 inline void SetConditionValue(uint16 value) { SB(this->dest, 0, 11, value); }
359 /* As conditional orders write their "skip to" order all over the flags, we cannot check the
360 * flags to find out if timetabling is enabled. However, as conditional orders are never
361 * autofilled we can be sure that any non-zero values for their wait_time and travel_time are
362 * explicitly set (but travel_time is actually unused for conditionals). */
364 /** Does this order have an explicit wait time set? */
365 inline bool IsWaitTimetabled() const { return this->IsType(OT_CONDITIONAL) ? HasBit(this->GetXFlags(), 0) : HasBit(this->flags, 3); }
366 /** Does this order have an explicit travel time set? */
367 inline bool IsTravelTimetabled() const { return this->IsType(OT_CONDITIONAL) ? this->travel_time > 0 : HasBit(this->flags, 7); }
369 /** Get the time in ticks a vehicle should wait at the destination or 0 if it's not timetabled. */
370 inline uint16 GetTimetabledWait() const { return this->IsWaitTimetabled() ? this->wait_time : 0; }
371 /** Get the time in ticks a vehicle should take to reach the destination or 0 if it's not timetabled. */
372 inline uint16 GetTimetabledTravel() const { return this->IsTravelTimetabled() ? this->travel_time : 0; }
373 /** Get the time in ticks a vehicle will probably wait at the destination (timetabled or not). */
374 inline uint16 GetWaitTime() const { return this->wait_time; }
375 /** Get the time in ticks a vehicle will probably take to reach the destination (timetabled or not). */
376 inline uint16 GetTravelTime() const { return this->travel_time; }
379 * Get the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
380 * destination.
381 * @return maximum speed.
383 inline uint16 GetMaxSpeed() const { return this->max_speed; }
385 /** Set if the wait time is explicitly timetabled (unless the order is conditional). */
386 inline void SetWaitTimetabled(bool timetabled)
388 if (this->IsType(OT_CONDITIONAL)) {
389 SB(this->GetXFlagsRef(), 0, 1, timetabled ? 1 : 0);
390 } else {
391 SB(this->flags, 3, 1, timetabled ? 1 : 0);
395 /** Set if the travel time is explicitly timetabled (unless the order is conditional). */
396 inline void SetTravelTimetabled(bool timetabled) { if (!this->IsType(OT_CONDITIONAL)) SB(this->flags, 7, 1, timetabled ? 1 : 0); }
399 * Set the time in ticks to wait at the destination.
400 * @param time Time to set as wait time.
402 inline void SetWaitTime(uint16 time) { this->wait_time = time; }
405 * Set the time in ticks to take for travelling to the destination.
406 * @param time Time to set as travel time.
408 inline void SetTravelTime(uint16 time) { this->travel_time = time; }
411 * Set the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
412 * destination.
413 * @param speed Speed to be set.
415 inline void SetMaxSpeed(uint16 speed) { this->max_speed = speed; }
417 bool ShouldStopAtStation(const Vehicle *v, StationID station) const;
418 bool CanLeaveWithCargo(bool has_cargo, CargoID cargo) const;
420 TileIndex GetLocation(const Vehicle *v, bool airport = false) const;
422 /** Checks if travel_time and wait_time apply to this order and if they are timetabled. */
423 inline bool IsCompletelyTimetabled() const
425 if (!this->IsTravelTimetabled() && !this->IsType(OT_CONDITIONAL)) return false;
426 if (!this->IsWaitTimetabled() && this->IsType(OT_GOTO_STATION) &&
427 !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) {
428 return false;
430 return true;
433 void AssignOrder(const Order &other);
434 bool Equals(const Order &other) const;
436 uint32 Pack() const;
437 uint16 MapOldOrder() const;
438 void ConvertFromOldSavegame();
441 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord);
442 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord);
444 struct CargoMaskedStationIDStack {
445 uint32 cargo_mask;
446 StationIDStack station;
448 CargoMaskedStationIDStack(uint32 cargo_mask, StationIDStack station)
449 : cargo_mask(cargo_mask), station(station) {}
452 struct CargoStationIDStackSet {
453 private:
454 CargoMaskedStationIDStack first;
455 std::vector<CargoMaskedStationIDStack> more;
457 public:
458 CargoStationIDStackSet()
459 : first(~0, INVALID_STATION) {}
461 const StationIDStack& Get(CargoID cargo) const
463 if (HasBit(first.cargo_mask, cargo)) return first.station;
464 for (size_t i = 0; i < more.size(); i++) {
465 if (HasBit(more[i].cargo_mask, cargo)) return more[i].station;
467 NOT_REACHED();
470 void FillNextStoppingStation(const Vehicle *v, const OrderList *o, const Order *first = NULL, uint hops = 0);
474 /** Working modes for timetable separation. */
475 enum TTSepMode {
476 /** Timetable separation works in fully automatic mode, determining its parameters from the situation in game. */
477 TTS_MODE_AUTO,
479 /** Timetable separation is deactivated altogether. */
480 TTS_MODE_OFF,
482 /** Timetable separation is active in manual time mode. The amount of time between vehicles is set in ticks by the user. */
483 TTS_MODE_MAN_T,
485 /** Timetable separation is active in manual number mode. The user sets a number of vehicles that are supposed to be running
486 * the timetable simultaneously. The algorithm acts according to this specification regardless of the actual number of
487 * running vehicles.*/
488 TTS_MODE_MAN_N,
490 /** Timetable separation works in buffered automatic mode that keeps one vehicle waiting at the first stop as
491 * reserve for delay compensation and behaves like full automatic otherwise. */
492 TTS_MODE_BUFFERED_AUTO,
495 struct TTSepSettings {
496 TTSepMode mode;
497 uint num_veh, sep_ticks;
498 TTSepSettings() : mode(TTS_MODE_AUTO), num_veh(0), sep_ticks(0) { }
501 template <typename F> uint32 FilterCargoMask(F filter_func, uint32 cargo_mask = ~0)
503 CargoID cargo;
504 uint32 output_mask = cargo_mask;
505 FOR_EACH_SET_BIT(cargo, cargo_mask) {
506 if (!filter_func(cargo)) ClrBit(output_mask, cargo);
508 return output_mask;
511 template <typename T, typename F> T CargoMaskValueFilter(uint32 &cargo_mask, F filter_func)
513 CargoID first_cargo_id = FindFirstBit(cargo_mask);
514 T value = filter_func(first_cargo_id);
515 uint32 other_cargo_mask = cargo_mask;
516 ClrBit(other_cargo_mask, first_cargo_id);
517 CargoID cargo;
518 FOR_EACH_SET_BIT(cargo, other_cargo_mask) {
519 if (value != filter_func(cargo)) ClrBit(cargo_mask, cargo);
521 return value;
524 /** Shared order list linking together the linked list of orders and the list
525 * of vehicles sharing this order list.
527 struct OrderList : OrderListPool::PoolItem<&_orderlist_pool> {
528 private:
529 friend void AfterLoadVehicles(bool part_of_load); ///< For instantiating the shared vehicle chain
530 friend const struct SaveLoad *GetOrderListDescription(); ///< Saving and loading of order lists.
532 StationID GetBestLoadableNext(const Vehicle *v, const Order *o1, const Order *o2) const;
534 /** Returns the number of running (i.e. not stopped) vehicles in the shared orders list. */
535 int GetNumRunningVehicles();
537 Order *first; ///< First order of the order list.
538 VehicleOrderID num_orders; ///< NOSAVE: How many orders there are in the list.
539 VehicleOrderID num_manual_orders; ///< NOSAVE: How many manually added orders are there in the list.
540 uint num_vehicles; ///< NOSAVE: Number of vehicles that share this order list.
541 Vehicle *first_shared; ///< NOSAVE: pointer to the first vehicle in the shared order chain.
543 Ticks timetable_duration; ///< NOSAVE: Total timetabled duration of the order list.
544 Ticks total_duration; ///< NOSAVE: Total (timetabled or not) duration of the order list.
545 Ticks last_timetable_init; ///< Contains the last absolute time of initialization in ticks.
546 uint separation_counter; ///< Counts the vehicles that arrive at the first shared order for separation timing.
547 bool is_separation_valid; ///< Is true if the separation has been initialized since last load or vehicle list change.
548 Ticks current_separation; ///< The current separation between vehicles in the shared order list.
549 TTSepMode current_sep_mode; ///< The current mode of vehicle separation.
550 uint num_sep_vehicles; ///< Number of planned vehicles for separation.
552 public:
553 /** Default constructor producing an invalid order list. */
554 OrderList(VehicleOrderID num_orders = INVALID_VEH_ORDER_ID)
555 : first(NULL), num_orders(num_orders), num_manual_orders(0), num_vehicles(0), first_shared(NULL),
556 timetable_duration(0), total_duration(0),
557 last_timetable_init(INVALID_TICKS), separation_counter(0),
558 is_separation_valid(false), current_separation(INVALID_TICKS), current_sep_mode(TTS_MODE_OFF),
559 num_sep_vehicles(0) { }
562 * Create an order list with the given order chain for the given vehicle.
563 * @param chain pointer to the first order of the order chain
564 * @param v any vehicle using this orderlist
566 OrderList(Order *chain, Vehicle *v) { this->Initialize(chain, v); }
568 /** Destructor. Invalidates OrderList for re-usage by the pool. */
569 ~OrderList() {}
571 void Initialize(Order *chain, Vehicle *v);
574 * Get the first order of the order chain.
575 * @return the first order of the chain.
577 inline Order *GetFirstOrder() const { return this->first; }
579 Order *GetOrderAt(int index) const;
581 VehicleOrderID GetIndexOfOrder(const Order *order) const;
584 * Get the last order of the order chain.
585 * @return the last order of the chain.
587 inline Order *GetLastOrder() const { return this->GetOrderAt(this->num_orders - 1); }
590 * Get the order after the given one or the first one, if the given one is the
591 * last one.
592 * @param curr Order to find the next one for.
593 * @return Next order.
595 inline const Order *GetNext(const Order *curr) const { return (curr->next == NULL) ? this->GetFirstOrder() : curr->next; }
598 * Get number of orders in the order list.
599 * @return number of orders in the chain.
601 inline VehicleOrderID GetNumOrders() const { return this->num_orders; }
604 * Get number of manually added orders in the order list.
605 * @return number of manual orders in the chain.
607 inline VehicleOrderID GetNumManualOrders() const { return this->num_manual_orders; }
609 CargoMaskedStationIDStack GetNextStoppingStation(const Vehicle *v, uint32 cargo_mask, const Order *first = NULL, uint hops = 0) const;
610 const Order *GetNextDecisionNode(const Order *next, uint hops, uint32 &cargo_mask) const;
612 void InsertOrderAt(Order *new_order, int index);
613 void DeleteOrderAt(int index);
614 void MoveOrder(int from, int to);
617 * Is this a shared order list?
618 * @return whether this order list is shared among multiple vehicles
620 inline bool IsShared() const { return this->num_vehicles > 1; };
623 * Get the first vehicle of this vehicle chain.
624 * @return the first vehicle of the chain.
626 inline Vehicle *GetFirstSharedVehicle() const { return this->first_shared; }
629 * Return the number of vehicles that share this orders list
630 * @return the count of vehicles that use this shared orders list
632 inline uint GetNumVehicles() const { return this->num_vehicles; }
635 * Returns the amount of separation time between vehicles.
636 * @return the amount of separation time between vehicles.
638 inline uint GetSepTime() const
640 if (this->is_separation_valid) {
641 return this->current_separation;
643 else if (this->IsCompleteTimetable()) {
644 return this->GetTimetableTotalDuration() / this->GetNumVehicles();
646 else {
647 return 1;
651 TTSepSettings GetSepSettings();
653 void SetSepSettings(TTSepSettings s);
654 void SetSepSettings(TTSepMode Mode, uint Parameter);
656 bool IsVehicleInSharedOrdersList(const Vehicle *v) const;
657 int GetPositionInSharedOrderList(const Vehicle *v) const;
660 * Adds the given vehicle to this shared order list.
661 * @note This is supposed to be called after the vehicle has been inserted
662 * into the shared vehicle chain.
663 * @param v vehicle to add to the list
665 inline void AddVehicle(Vehicle *v) { ++this->num_vehicles; }
667 void RemoveVehicle(Vehicle *v);
669 bool IsCompleteTimetable() const;
672 * Gets the total duration of the vehicles timetable or INVALID_TICKS is the timetable is not complete.
673 * @return total timetable duration or INVALID_TICKS for incomplete timetables
675 inline Ticks GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? this->timetable_duration : INVALID_TICKS; }
678 * Gets the known duration of the vehicles timetable even if the timetable is not complete.
679 * @return known timetable duration
681 inline Ticks GetTimetableDurationIncomplete() const { return this->timetable_duration; }
684 * Gets the known duration of the vehicles orders, timetabled or not.
685 * @return known order duration.
687 inline Ticks GetTotalDuration() const { return this->total_duration; }
690 * Must be called if an order's timetable is changed to update internal book keeping.
691 * @param delta By how many ticks has the timetable duration changed
693 void UpdateTimetableDuration(Ticks delta) { this->timetable_duration += delta; }
696 * Must be called if an order's timetable is changed to update internal book keeping.
697 * @param delta By how many ticks has the total duration changed
699 void UpdateTotalDuration(Ticks delta) { this->total_duration += delta; }
701 /* Gets the last absolute time in Ticks since separation was initialized.
702 * @return last arrival time of first vehicle at first order.
704 inline Ticks GetSeparationInitTime() const
706 return this->last_timetable_init;
710 * Gets the current value of the timetable separation counter.
711 * @return the current value of the timetable separation counter.
713 inline uint GetSeparationCounter() const
715 return this->separation_counter;
718 /** Increases the timetable separation counter. */
719 void IncreaseSeparationCounter()
721 this->separation_counter++;
724 /** Marks timetable separation invalid so it has to be initialized again. */
725 void MarkSeparationInvalid()
727 this->is_separation_valid = false;
731 * Returns the new delay for the current vehicle and increases the separation counter.
732 * @return the new delay
734 Ticks SeparateVehicle();
737 * Gets whether the timetable separation is currently valid or not.
738 * @return whether the timetable separation is currently valid or not.
740 inline bool IsSeparationValid() const
742 return this->is_separation_valid;
746 * Gets whether timetable separation is currently switched on or not.
747 * @return whether the timetable separation is currently switched on or not.
749 inline bool IsSeparationOn() const
751 return this->current_sep_mode != TTS_MODE_OFF;
754 /** Initializes the separation system. */
755 void InitializeSeparation();
757 void UpdateSeparationTime();
759 void FreeChain(bool keep_orderlist = false);
761 void DebugCheckSanity() const;
764 #define FOR_ALL_ORDERS_FROM(var, start) FOR_ALL_ITEMS_FROM(Order, order_index, var, start)
765 #define FOR_ALL_ORDERS(var) FOR_ALL_ORDERS_FROM(var, 0)
768 #define FOR_VEHICLE_ORDERS(v, order) for (order = v->GetFirstOrder(); order != NULL; order = order->next)
771 #define FOR_ALL_ORDER_LISTS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderList, orderlist_index, var, start)
772 #define FOR_ALL_ORDER_LISTS(var) FOR_ALL_ORDER_LISTS_FROM(var, 0)
774 #endif /* ORDER_BASE_H */