Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / order_base.h
blobc9ce31ab6b034653c83c49a8d9bfbbb346991c54
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.
37 /* If you change this, keep in mind that it is saved on 3 places:
38 * - Load_ORDR, all the global orders
39 * - Vehicle -> current_order
40 * - REF_ORDER (all REFs are currently limited to 16 bits!!)
42 struct Order : OrderPool::PoolItem<&_order_pool> {
43 private:
44 friend const struct SaveLoad *GetVehicleDescription(VehicleType vt); ///< Saving and loading the current order of vehicles.
45 friend void Load_VEHS(); ///< Loading of ancient vehicles.
46 friend const struct SaveLoad *GetOrderDescription(); ///< Saving and loading of orders.
47 friend void Load_ORDX(); ///< Saving and loading of orders.
48 friend void Save_ORDX(); ///< Saving and loading of orders.
49 friend void Load_VEOX(); ///< Saving and loading of orders.
50 friend void Save_VEOX(); ///< Saving and loading of orders.
52 uint8 type; ///< The type of order + non-stop flags
53 uint8 flags; ///< Load/unload types, depot order/action types.
54 DestinationID dest; ///< The destination of the order.
56 CargoID refit_cargo; ///< Refit CargoID
58 std::unique_ptr<OrderExtraInfo> extra; ///< Extra order info
60 int8 jump_counter; ///< Counter for the 'jump xx% of times' option
62 uint16 wait_time; ///< How long in ticks to wait at the destination.
63 uint16 travel_time; ///< How long in ticks the journey to this destination should take.
64 uint16 max_speed; ///< How fast the vehicle may go on the way to the destination.
66 void AllocExtraInfo();
67 void DeAllocExtraInfo();
69 inline void CheckExtraInfoAlloced()
71 if (!this->extra) this->AllocExtraInfo();
74 public:
75 Order *next; ///< Pointer to next order. If NULL, end of list
77 Order() : refit_cargo(CT_NO_REFIT), max_speed(UINT16_MAX) {}
78 ~Order();
80 Order(uint32 packed);
82 Order(const Order& other)
84 *this = other;
87 Order(Order&& other) = default;
89 inline Order& operator=(Order const& other)
91 AssignOrder(other);
92 this->next = other.next;
93 this->index = other.index;
94 return *this;
97 /**
98 * Check whether this order is of the given type.
99 * @param type the type to check against.
100 * @return true if the order matches.
102 inline bool IsType(OrderType type) const { return this->GetType() == type; }
105 * Get the type of order of this order.
106 * @return the order type.
108 inline OrderType GetType() const { return (OrderType)GB(this->type, 0, 4); }
110 void Free();
112 void MakeGoToStation(StationID destination);
113 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);
114 void MakeGoToWaypoint(StationID destination);
115 void MakeLoading(bool ordered);
116 void MakeLeaveStation();
117 void MakeDummy();
118 void MakeConditional(VehicleOrderID order);
119 void MakeImplicit(StationID destination);
120 void MakeWaiting();
123 * Is this a 'goto' order with a real destination?
124 * @return True if the type is either #OT_GOTO_WAYPOINT, #OT_GOTO_DEPOT or #OT_GOTO_STATION.
126 inline bool IsGotoOrder() const
128 return IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION);
132 * Gets the destination of this order.
133 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
134 * @return the destination of the order.
136 inline DestinationID GetDestination() const { return this->dest; }
139 * Sets the destination of this order.
140 * @param destination the new destination of the order.
141 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION).
143 inline void SetDestination(DestinationID destination) { this->dest = destination; }
146 * Is this order a refit order.
147 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
148 * @return true if a refit should happen.
150 inline bool IsRefit() const { return this->refit_cargo < NUM_CARGO || this->refit_cargo == CT_AUTO_REFIT; }
153 * Is this order a auto-refit order.
154 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
155 * @return true if a auto-refit should happen.
157 inline bool IsAutoRefit() const { return this->refit_cargo == CT_AUTO_REFIT; }
160 * Get the cargo to to refit to.
161 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION)
162 * @return the cargo type.
164 inline CargoID GetRefitCargo() const { return this->refit_cargo; }
166 void SetRefit(CargoID cargo);
169 * Update the jump_counter of this order.
170 * @param the jump chance in %.
171 * @return whether to jump or not.
172 * @pre IsType(OT_CONDITIONAL) && this->GetConditionVariable() == OCV_PERCENT.
174 bool UpdateJumpCounter(uint8 percent);
176 /** How must the consist be loaded? */
177 inline OrderLoadFlags GetLoadType() const
179 OrderLoadFlags type = (OrderLoadFlags)GB(this->flags, 4, 3);
180 if (type == OLFB_CARGO_TYPE_LOAD_ENCODING) type = OLFB_CARGO_TYPE_LOAD;
181 return type;
185 * How must the consist be loaded for this type of cargo?
186 * @pre GetLoadType() == OLFB_CARGO_TYPE_LOAD
187 * @param cargo_id The cargo type index.
188 * @return The load type for this cargo.
190 inline OrderLoadFlags GetCargoLoadTypeRaw(CargoID cargo_id) const
192 assert(cargo_id < NUM_CARGO);
194 if (!this->extra) return OLF_LOAD_IF_POSSIBLE;
195 return (OrderLoadFlags)GB(this->extra->cargo_type_flags[cargo_id], 4, 4);
199 * How must the consist be loaded for this type of cargo?
200 * @param cargo_id The cargo type index.
201 * @return The load type for this cargo.
203 inline OrderLoadFlags GetCargoLoadType(CargoID cargo_id) const
205 assert(cargo_id < NUM_CARGO);
206 OrderLoadFlags olf = this->GetLoadType();
207 if (olf == OLFB_CARGO_TYPE_LOAD) olf = this->GetCargoLoadTypeRaw(cargo_id);
208 return olf;
211 /** How must the consist be unloaded? */
212 inline OrderUnloadFlags GetUnloadType() const
214 OrderUnloadFlags type = (OrderUnloadFlags)GB(this->flags, 0, 3);
215 if (type == OUFB_CARGO_TYPE_UNLOAD_ENCODING) type = OUFB_CARGO_TYPE_UNLOAD;
216 return type;
220 * How must the consist be unloaded for this type of cargo?
221 * @pre GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD
222 * @param cargo_id The cargo type index.
223 * @return The unload type for this cargo.
225 inline OrderUnloadFlags GetCargoUnloadTypeRaw(CargoID cargo_id) const
227 assert(cargo_id < NUM_CARGO);
228 if (!this->extra) return OUF_UNLOAD_IF_POSSIBLE;
229 return (OrderUnloadFlags)GB(this->extra->cargo_type_flags[cargo_id], 0, 4);
233 * How must the consist be unloaded for this type of cargo?
234 * @param cargo_id The cargo type index.
235 * @return The unload type for this cargo.
237 inline OrderUnloadFlags GetCargoUnloadType(CargoID cargo_id) const
239 assert(cargo_id < NUM_CARGO);
240 OrderUnloadFlags ouf = this->GetUnloadType();
241 if (ouf == OUFB_CARGO_TYPE_UNLOAD) ouf = this->GetCargoUnloadTypeRaw(cargo_id);
242 return ouf;
245 template <typename F> uint32 FilterLoadUnloadTypeCargoMask(F filter_func, uint32 cargo_mask = ~0)
247 if ((this->GetLoadType() == OLFB_CARGO_TYPE_LOAD) || (this->GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD)) {
248 CargoID cargo;
249 uint32 output_mask = cargo_mask;
250 FOR_EACH_SET_BIT(cargo, cargo_mask) {
251 if (!filter_func(this, cargo)) ClrBit(output_mask, cargo);
253 return output_mask;
254 } else {
255 return filter_func(this, FindFirstBit(cargo_mask)) ? cargo_mask : 0;
259 /** At which stations must we stop? */
260 inline OrderNonStopFlags GetNonStopType() const { return (OrderNonStopFlags)GB(this->type, 6, 2); }
261 /** Where must we stop at the platform? */
262 inline OrderStopLocation GetStopLocation() const { return (OrderStopLocation)GB(this->type, 4, 2); }
263 /** What caused us going to the depot? */
264 inline OrderDepotTypeFlags GetDepotOrderType() const { return (OrderDepotTypeFlags)GB(this->flags, 0, 3); }
265 /** What are we going to do when in the depot. */
266 inline OrderDepotActionFlags GetDepotActionType() const { return (OrderDepotActionFlags)GB(this->flags, 4, 3); }
267 /** What waypoint flags? */
268 inline OrderWaypointFlags GetWaypointFlags() const { return (OrderWaypointFlags)GB(this->flags, 0, 8); }
269 /** What variable do we have to compare? */
270 inline OrderConditionVariable GetConditionVariable() const { return (OrderConditionVariable)GB(this->dest, 11, 5); }
271 /** What is the comparator to use? */
272 inline OrderConditionComparator GetConditionComparator() const { return (OrderConditionComparator)GB(this->type, 5, 3); }
273 /** Get the order to skip to. */
274 inline VehicleOrderID GetConditionSkipToOrder() const { return this->flags; }
275 /** Get the value to base the skip on. */
276 inline uint16 GetConditionValue() const { return GB(this->dest, 0, 11); }
278 /** Set how the consist must be loaded. */
279 inline void SetLoadType(OrderLoadFlags load_type)
281 if (load_type == OLFB_CARGO_TYPE_LOAD) load_type = OLFB_CARGO_TYPE_LOAD_ENCODING;
282 SB(this->flags, 4, 3, load_type);
286 * Set how the consist must be loaded for this type of cargo.
287 * @pre GetLoadType() == OLFB_CARGO_TYPE_LOAD
288 * @param load_type The load type.
289 * @param cargo_id The cargo type index.
291 inline void SetLoadType(OrderLoadFlags load_type, CargoID cargo_id)
293 assert(cargo_id < NUM_CARGO);
294 this->CheckExtraInfoAlloced();
295 SB(this->extra->cargo_type_flags[cargo_id], 4, 4, load_type);
297 /** Set how the consist must be unloaded. */
298 inline void SetUnloadType(OrderUnloadFlags unload_type)
300 if (unload_type == OUFB_CARGO_TYPE_UNLOAD) unload_type = OUFB_CARGO_TYPE_UNLOAD_ENCODING;
301 SB(this->flags, 0, 3, unload_type);
305 * Set how the consist must be unloaded for this type of cargo.
306 * @pre GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD
307 * @param unload_type The unload type.
308 * @param cargo_id The cargo type index.
310 inline void SetUnloadType(OrderUnloadFlags unload_type, CargoID cargo_id)
312 assert(cargo_id < NUM_CARGO);
313 this->CheckExtraInfoAlloced();
314 SB(this->extra->cargo_type_flags[cargo_id], 0, 4, unload_type);
316 /** Set whether we must stop at stations or not. */
317 inline void SetNonStopType(OrderNonStopFlags non_stop_type) { SB(this->type, 6, 2, non_stop_type); }
318 /** Set where we must stop at the platform. */
319 inline void SetStopLocation(OrderStopLocation stop_location) { SB(this->type, 4, 2, stop_location); }
320 /** Set the cause to go to the depot. */
321 inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type) { SB(this->flags, 0, 3, depot_order_type); }
322 /** Set what we are going to do in the depot. */
323 inline void SetDepotActionType(OrderDepotActionFlags depot_service_type) { SB(this->flags, 4, 3, depot_service_type); }
324 /** Set waypoint flags. */
325 inline void SetWaypointFlags(OrderWaypointFlags waypoint_flags) { SB(this->flags, 0, 8, waypoint_flags); }
326 /** Set variable we have to compare. */
327 inline void SetConditionVariable(OrderConditionVariable condition_variable) { SB(this->dest, 11, 5, condition_variable); }
328 /** Set the comparator to use. */
329 inline void SetConditionComparator(OrderConditionComparator condition_comparator) { SB(this->type, 5, 3, condition_comparator); }
330 /** Get the order to skip to. */
331 inline void SetConditionSkipToOrder(VehicleOrderID order_id) { this->flags = order_id; }
332 /** Set the value to base the skip on. */
333 inline void SetConditionValue(uint16 value) { SB(this->dest, 0, 11, value); }
335 /* As conditional orders write their "skip to" order all over the flags, we cannot check the
336 * flags to find out if timetabling is enabled. However, as conditional orders are never
337 * autofilled we can be sure that any non-zero values for their wait_time and travel_time are
338 * explicitly set (but travel_time is actually unused for conditionals). */
340 /** Does this order have an explicit wait time set? */
341 inline bool IsWaitTimetabled() const { return this->IsType(OT_CONDITIONAL) ? this->wait_time > 0 : HasBit(this->flags, 3); }
342 /** Does this order have an explicit travel time set? */
343 inline bool IsTravelTimetabled() const { return this->IsType(OT_CONDITIONAL) ? this->travel_time > 0 : HasBit(this->flags, 7); }
345 /** Get the time in ticks a vehicle should wait at the destination or 0 if it's not timetabled. */
346 inline uint16 GetTimetabledWait() const { return this->IsWaitTimetabled() ? this->wait_time : 0; }
347 /** Get the time in ticks a vehicle should take to reach the destination or 0 if it's not timetabled. */
348 inline uint16 GetTimetabledTravel() const { return this->IsTravelTimetabled() ? this->travel_time : 0; }
349 /** Get the time in ticks a vehicle will probably wait at the destination (timetabled or not). */
350 inline uint16 GetWaitTime() const { return this->wait_time; }
351 /** Get the time in ticks a vehicle will probably take to reach the destination (timetabled or not). */
352 inline uint16 GetTravelTime() const { return this->travel_time; }
355 * Get the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
356 * destination.
357 * @return maximum speed.
359 inline uint16 GetMaxSpeed() const { return this->max_speed; }
361 /** Set if the wait time is explicitly timetabled (unless the order is conditional). */
362 inline void SetWaitTimetabled(bool timetabled) { if (!this->IsType(OT_CONDITIONAL)) SB(this->flags, 3, 1, timetabled ? 1 : 0); }
363 /** Set if the travel time is explicitly timetabled (unless the order is conditional). */
364 inline void SetTravelTimetabled(bool timetabled) { if (!this->IsType(OT_CONDITIONAL)) SB(this->flags, 7, 1, timetabled ? 1 : 0); }
367 * Set the time in ticks to wait at the destination.
368 * @param time Time to set as wait time.
370 inline void SetWaitTime(uint16 time) { this->wait_time = time; }
373 * Set the time in ticks to take for travelling to the destination.
374 * @param time Time to set as travel time.
376 inline void SetTravelTime(uint16 time) { this->travel_time = time; }
379 * Set the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the
380 * destination.
381 * @param speed Speed to be set.
383 inline void SetMaxSpeed(uint16 speed) { this->max_speed = speed; }
385 bool ShouldStopAtStation(const Vehicle *v, StationID station) const;
386 bool CanLeaveWithCargo(bool has_cargo, CargoID cargo) const;
388 TileIndex GetLocation(const Vehicle *v, bool airport = false) const;
390 /** Checks if travel_time and wait_time apply to this order and if they are timetabled. */
391 inline bool IsCompletelyTimetabled() const
393 if (!this->IsTravelTimetabled() && !this->IsType(OT_CONDITIONAL)) return false;
394 if (!this->IsWaitTimetabled() && this->IsType(OT_GOTO_STATION) &&
395 !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) {
396 return false;
398 return true;
401 void AssignOrder(const Order &other);
402 bool Equals(const Order &other) const;
404 uint32 Pack() const;
405 uint16 MapOldOrder() const;
406 void ConvertFromOldSavegame();
409 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord);
410 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord);
412 struct CargoMaskedStationIDStack {
413 uint32 cargo_mask;
414 StationIDStack station;
416 CargoMaskedStationIDStack(uint32 cargo_mask, StationIDStack station)
417 : cargo_mask(cargo_mask), station(station) {}
420 struct CargoStationIDStackSet {
421 private:
422 CargoMaskedStationIDStack first;
423 std::vector<CargoMaskedStationIDStack> more;
425 public:
426 CargoStationIDStackSet()
427 : first(~0, INVALID_STATION) {}
429 const StationIDStack& Get(CargoID cargo) const
431 if (HasBit(first.cargo_mask, cargo)) return first.station;
432 for (size_t i = 0; i < more.size(); i++) {
433 if (HasBit(more[i].cargo_mask, cargo)) return more[i].station;
435 NOT_REACHED();
438 void FillNextStoppingStation(const Vehicle *v, const OrderList *o, const Order *first = NULL, uint hops = 0);
442 /** Working modes for timetable separation. */
443 enum TTSepMode {
444 /** Timetable separation works in fully automatic mode, determining its parameters from the situation in game. */
445 TTS_MODE_AUTO,
447 /** Timetable separation is deactivated altogether. */
448 TTS_MODE_OFF,
450 /** Timetable separation is active in manual time mode. The amount of time between vehicles is set in ticks by the user. */
451 TTS_MODE_MAN_T,
453 /** Timetable separation is active in manual number mode. The user sets a number of vehicles that are supposed to be running
454 * the timetable simultaneously. The algorithm acts according to this specification regardless of the actual number of
455 * running vehicles.*/
456 TTS_MODE_MAN_N,
458 /** Timetable separation works in buffered automatic mode that keeps one vehicle waiting at the first stop as
459 * reserve for delay compensation and behaves like full automatic otherwise. */
460 TTS_MODE_BUFFERED_AUTO,
463 struct TTSepSettings {
464 TTSepMode mode;
465 uint num_veh, sep_ticks;
466 TTSepSettings() : mode(TTS_MODE_AUTO), num_veh(0), sep_ticks(0) { }
469 template <typename F> uint32 FilterCargoMask(F filter_func, uint32 cargo_mask = ~0)
471 CargoID cargo;
472 uint32 output_mask = cargo_mask;
473 FOR_EACH_SET_BIT(cargo, cargo_mask) {
474 if (!filter_func(cargo)) ClrBit(output_mask, cargo);
476 return output_mask;
479 template <typename T, typename F> T CargoMaskValueFilter(uint32 &cargo_mask, F filter_func)
481 CargoID first_cargo_id = FindFirstBit(cargo_mask);
482 T value = filter_func(first_cargo_id);
483 uint32 other_cargo_mask = cargo_mask;
484 ClrBit(other_cargo_mask, first_cargo_id);
485 CargoID cargo;
486 FOR_EACH_SET_BIT(cargo, other_cargo_mask) {
487 if (value != filter_func(cargo)) ClrBit(cargo_mask, cargo);
489 return value;
492 /** Shared order list linking together the linked list of orders and the list
493 * of vehicles sharing this order list.
495 struct OrderList : OrderListPool::PoolItem<&_orderlist_pool> {
496 private:
497 friend void AfterLoadVehicles(bool part_of_load); ///< For instantiating the shared vehicle chain
498 friend const struct SaveLoad *GetOrderListDescription(); ///< Saving and loading of order lists.
500 StationID GetBestLoadableNext(const Vehicle *v, const Order *o1, const Order *o2) const;
502 /** Returns the number of running (i.e. not stopped) vehicles in the shared orders list. */
503 int GetNumRunningVehicles();
505 Order *first; ///< First order of the order list.
506 VehicleOrderID num_orders; ///< NOSAVE: How many orders there are in the list.
507 VehicleOrderID num_manual_orders; ///< NOSAVE: How many manually added orders are there in the list.
508 uint num_vehicles; ///< NOSAVE: Number of vehicles that share this order list.
509 Vehicle *first_shared; ///< NOSAVE: pointer to the first vehicle in the shared order chain.
511 Ticks timetable_duration; ///< NOSAVE: Total timetabled duration of the order list.
512 Ticks total_duration; ///< NOSAVE: Total (timetabled or not) duration of the order list.
513 Ticks last_timetable_init; ///< Contains the last absolute time of initialization in ticks.
514 uint separation_counter; ///< Counts the vehicles that arrive at the first shared order for separation timing.
515 bool is_separation_valid; ///< Is true if the separation has been initialized since last load or vehicle list change.
516 Ticks current_separation; ///< The current separation between vehicles in the shared order list.
517 TTSepMode current_sep_mode; ///< The current mode of vehicle separation.
518 uint num_sep_vehicles; ///< Number of planned vehicles for separation.
520 public:
521 /** Default constructor producing an invalid order list. */
522 OrderList(VehicleOrderID num_orders = INVALID_VEH_ORDER_ID)
523 : first(NULL), num_orders(num_orders), num_manual_orders(0), num_vehicles(0), first_shared(NULL),
524 timetable_duration(0), total_duration(0),
525 last_timetable_init(INVALID_TICKS), separation_counter(0),
526 is_separation_valid(false), current_separation(INVALID_TICKS), current_sep_mode(TTS_MODE_OFF),
527 num_sep_vehicles(0) { }
530 * Create an order list with the given order chain for the given vehicle.
531 * @param chain pointer to the first order of the order chain
532 * @param v any vehicle using this orderlist
534 OrderList(Order *chain, Vehicle *v) { this->Initialize(chain, v); }
536 /** Destructor. Invalidates OrderList for re-usage by the pool. */
537 ~OrderList() {}
539 void Initialize(Order *chain, Vehicle *v);
542 * Get the first order of the order chain.
543 * @return the first order of the chain.
545 inline Order *GetFirstOrder() const { return this->first; }
547 Order *GetOrderAt(int index) const;
550 * Get the last order of the order chain.
551 * @return the last order of the chain.
553 inline Order *GetLastOrder() const { return this->GetOrderAt(this->num_orders - 1); }
556 * Get the order after the given one or the first one, if the given one is the
557 * last one.
558 * @param curr Order to find the next one for.
559 * @return Next order.
561 inline const Order *GetNext(const Order *curr) const { return (curr->next == NULL) ? this->GetFirstOrder() : curr->next; }
564 * Get number of orders in the order list.
565 * @return number of orders in the chain.
567 inline VehicleOrderID GetNumOrders() const { return this->num_orders; }
570 * Get number of manually added orders in the order list.
571 * @return number of manual orders in the chain.
573 inline VehicleOrderID GetNumManualOrders() const { return this->num_manual_orders; }
575 CargoMaskedStationIDStack GetNextStoppingStation(const Vehicle *v, uint32 cargo_mask, const Order *first = NULL, uint hops = 0) const;
576 const Order *GetNextDecisionNode(const Order *next, uint hops, uint32 &cargo_mask) const;
578 void InsertOrderAt(Order *new_order, int index);
579 void DeleteOrderAt(int index);
580 void MoveOrder(int from, int to);
583 * Is this a shared order list?
584 * @return whether this order list is shared among multiple vehicles
586 inline bool IsShared() const { return this->num_vehicles > 1; };
589 * Get the first vehicle of this vehicle chain.
590 * @return the first vehicle of the chain.
592 inline Vehicle *GetFirstSharedVehicle() const { return this->first_shared; }
595 * Return the number of vehicles that share this orders list
596 * @return the count of vehicles that use this shared orders list
598 inline uint GetNumVehicles() const { return this->num_vehicles; }
601 * Returns the amount of separation time between vehicles.
602 * @return the amount of separation time between vehicles.
604 inline uint GetSepTime() const
606 if (this->is_separation_valid) {
607 return this->current_separation;
609 else if (this->IsCompleteTimetable()) {
610 return this->GetTimetableTotalDuration() / this->GetNumVehicles();
612 else {
613 return 1;
617 TTSepSettings GetSepSettings();
619 void SetSepSettings(TTSepSettings s);
620 void SetSepSettings(TTSepMode Mode, uint Parameter);
622 bool IsVehicleInSharedOrdersList(const Vehicle *v) const;
623 int GetPositionInSharedOrderList(const Vehicle *v) const;
626 * Adds the given vehicle to this shared order list.
627 * @note This is supposed to be called after the vehicle has been inserted
628 * into the shared vehicle chain.
629 * @param v vehicle to add to the list
631 inline void AddVehicle(Vehicle *v) { ++this->num_vehicles; }
633 void RemoveVehicle(Vehicle *v);
635 bool IsCompleteTimetable() const;
638 * Gets the total duration of the vehicles timetable or INVALID_TICKS is the timetable is not complete.
639 * @return total timetable duration or INVALID_TICKS for incomplete timetables
641 inline Ticks GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? this->timetable_duration : INVALID_TICKS; }
644 * Gets the known duration of the vehicles timetable even if the timetable is not complete.
645 * @return known timetable duration
647 inline Ticks GetTimetableDurationIncomplete() const { return this->timetable_duration; }
650 * Gets the known duration of the vehicles orders, timetabled or not.
651 * @return known order duration.
653 inline Ticks GetTotalDuration() const { return this->total_duration; }
656 * Must be called if an order's timetable is changed to update internal book keeping.
657 * @param delta By how many ticks has the timetable duration changed
659 void UpdateTimetableDuration(Ticks delta) { this->timetable_duration += delta; }
662 * Must be called if an order's timetable is changed to update internal book keeping.
663 * @param delta By how many ticks has the total duration changed
665 void UpdateTotalDuration(Ticks delta) { this->total_duration += delta; }
667 /* Gets the last absolute time in Ticks since separation was initialized.
668 * @return last arrival time of first vehicle at first order.
670 inline Ticks GetSeparationInitTime() const
672 return this->last_timetable_init;
676 * Gets the current value of the timetable separation counter.
677 * @return the current value of the timetable separation counter.
679 inline uint GetSeparationCounter() const
681 return this->separation_counter;
684 /** Increases the timetable separation counter. */
685 void IncreaseSeparationCounter()
687 this->separation_counter++;
690 /** Marks timetable separation invalid so it has to be initialized again. */
691 void MarkSeparationInvalid()
693 this->is_separation_valid = false;
697 * Returns the new delay for the current vehicle and increases the separation counter.
698 * @return the new delay
700 Ticks SeparateVehicle();
703 * Gets whether the timetable separation is currently valid or not.
704 * @return whether the timetable separation is currently valid or not.
706 inline bool IsSeparationValid() const
708 return this->is_separation_valid;
712 * Gets whether timetable separation is currently switched on or not.
713 * @return whether the timetable separation is currently switched on or not.
715 inline bool IsSeparationOn() const
717 return this->current_sep_mode != TTS_MODE_OFF;
720 /** Initializes the separation system. */
721 void InitializeSeparation();
723 void UpdateSeparationTime();
725 void FreeChain(bool keep_orderlist = false);
727 void DebugCheckSanity() const;
730 #define FOR_ALL_ORDERS_FROM(var, start) FOR_ALL_ITEMS_FROM(Order, order_index, var, start)
731 #define FOR_ALL_ORDERS(var) FOR_ALL_ORDERS_FROM(var, 0)
734 #define FOR_VEHICLE_ORDERS(v, order) for (order = v->GetFirstOrder(); order != NULL; order = order->next)
737 #define FOR_ALL_ORDER_LISTS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderList, orderlist_index, var, start)
738 #define FOR_ALL_ORDER_LISTS(var) FOR_ALL_ORDER_LISTS_FROM(var, 0)
740 #endif /* ORDER_BASE_H */