1
/* $Id: order.h 12296 2008-02-27 21:46:57Z glx $ */
9
#include "core/bitmath_func.hpp"
10
#include "cargo_type.h"
11
#include "vehicle_type.h"
12
#include "tile_type.h"
13
#include "date_type.h"
16
INVALID_VEH_ORDER_ID = 0xFF,
19
static const OrderID INVALID_ORDER = 0xFFFF;
34
/* It needs to be 8bits, because we save and load it as such */
35
/** Define basic enum properties */
36
template <> struct EnumPropsT<OrderType> : MakeEnumPropsT<OrderType, byte, OT_BEGIN, OT_END, OT_END> {};
37
typedef TinyEnumT<OrderType> OrderTypeByte;
40
/* Order flags -- please use OF instead OF and use HASBIT/SETBIT/CLEARBIT */
42
/** Order flag masks - these are for direct bit operations */
45
/** vehicle will transfer cargo (i. e. not deliver to nearby industry/town even if accepted there) */
47
/** If OFB_TRANSFER is not set, drop any cargo loaded. If accepted, deliver, otherwise cargo remains at the station.
48
* No new cargo is loaded onto the vehicle whatsoever */
50
/** Wait for full load of all vehicles, or of at least one cargo type, depending on patch setting
51
* @todo make this two different flags */
55
/** The current depot-order was initiated because it was in the vehicle's order list */
56
OFB_PART_OF_ORDERS = 0x2,
57
/** if OFB_PART_OF_ORDERS is not set, this will cause the vehicle to be stopped in the depot */
58
OFB_HALT_IN_DEPOT = 0x4,
59
/** if OFB_PART_OF_ORDERS is set, this will cause the order only be come active if the vehicle needs servicing */
60
OFB_SERVICE_IF_NEEDED = 0x4, //used when OFB_PART_OF_ORDERS is set.
63
/** This causes the vehicle not to stop at intermediate OR the destination station (depending on patch settings)
64
* @todo make this two different flags */
68
/** Order flags bits - these are for the *BIT macros
69
* for descrption of flags, see OrderFlagMasks
76
OF_PART_OF_ORDERS = 1,
78
OF_SERVICE_IF_NEEDED = 2,
83
/* Possible clone options */
91
DECLARE_OLD_POOL(Order, Order, 6, 1000)
93
/* If you change this, keep in mind that it is saved on 3 places:
94
* - Load_ORDR, all the global orders
95
* - Vehicle -> current_order
96
* - REF_ORDER (all REFs are currently limited to 16 bits!!)
98
struct Order : PoolItem<Order, OrderID, &_Order_pool> {
99
Order *next; ///< Pointer to next order. If NULL, end of list
103
DestinationID dest; ///< The destionation of the order.
105
CargoID refit_cargo; // Refit CargoID
106
byte refit_subtype; // Refit subtype
108
uint16 wait_time; ///< How long in ticks to wait at the destination.
109
uint16 travel_time; ///< How long in ticks the journey to this destination should take.
111
Order() : refit_cargo(CT_NO_REFIT) {}
112
~Order() { this->type = OT_NOTHING; }
115
* Check if a Order really exists.
117
inline bool IsValid() const { return this->type != OT_NOTHING; }
123
struct BackuppedOrders {
124
BackuppedOrders() : order(NULL), name(NULL) { }
125
~BackuppedOrders() { free(order); free(name); }
128
VehicleOrderID orderindex;
131
uint16 service_interval;
135
extern TileIndex _backup_orders_tile;
136
extern BackuppedOrders _backup_orders_data;
138
static inline VehicleOrderID GetMaxOrderIndex()
140
/* TODO - This isn't the real content of the function, but
141
* with the new pool-system this will be replaced with one that
142
* _really_ returns the highest index. Now it just returns
143
* the next safe value we are sure about everything is below.
145
return GetOrderPoolSize() - 1;
148
static inline VehicleOrderID GetNumOrders()
150
return GetOrderPoolSize();
153
inline void Order::Free()
155
this->type = OT_NOTHING;
161
inline void Order::FreeChain()
163
if (next != NULL) next->FreeChain();
167
#define FOR_ALL_ORDERS_FROM(order, start) for (order = GetOrder(start); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) if (order->IsValid())
168
#define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
171
#define FOR_VEHICLE_ORDERS(v, order) for (order = v->orders; order != NULL; order = order->next)
173
static inline bool HasOrderPoolFree(uint amount)
177
/* There is always room if not all blocks in the pool are reserved */
178
if (_Order_pool.CanAllocateMoreBlocks()) return true;
180
FOR_ALL_ORDERS(order) if (!order->IsValid() && --amount == 0) return true;
186
/* Pack and unpack routines */
188
static inline uint32 PackOrder(const Order *order)
190
return order->dest << 16 | order->flags << 8 | order->type;
193
static inline Order UnpackOrder(uint32 packed)
196
order.type = (OrderType)GB(packed, 0, 8);
197
order.flags = GB(packed, 8, 8);
198
order.dest = GB(packed, 16, 16);
200
order.index = 0; // avoid compiler warning
201
order.refit_cargo = CT_NO_REFIT;
202
order.refit_subtype = 0;
204
order.travel_time = 0;
209
void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *order = &_backup_orders_data);
210
void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *order = &_backup_orders_data);
211
void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination);
212
void InvalidateVehicleOrder(const Vehicle *v);
213
bool VehicleHasDepotOrders(const Vehicle *v);
214
void CheckOrders(const Vehicle*);
215
void DeleteVehicleOrders(Vehicle *v);
216
void AssignOrder(Order *order, Order data);
217
bool CheckForValidOrders(const Vehicle* v);
219
Order UnpackOldOrder(uint16 packed);
221
#define MIN_SERVINT_PERCENT 5
222
#define MAX_SERVINT_PERCENT 90
223
#define MIN_SERVINT_DAYS 30
224
#define MAX_SERVINT_DAYS 800
227
* Get the service interval domain.
228
* Get the new proposed service interval for the vehicle is indeed, clamped
229
* within the given bounds. @see MIN_SERVINT_PERCENT ,etc.
230
* @param index proposed service interval
231
* @return service interval
233
Date GetServiceIntervalClamped(uint index);