9837
|
1 |
/* $Id$ */
|
|
2 |
|
|
3 |
/** @file order_base.h */
|
|
4 |
|
|
5 |
#ifndef ORDER_BASE_H
|
|
6 |
#define ORDER_BASE_H
|
|
7 |
|
|
8 |
#include "order_type.h"
|
|
9 |
#include "oldpool.h"
|
|
10 |
#include "core/bitmath_func.hpp"
|
|
11 |
#include "cargo_type.h"
|
|
12 |
|
|
13 |
DECLARE_OLD_POOL(Order, Order, 6, 1000)
|
|
14 |
|
|
15 |
/* If you change this, keep in mind that it is saved on 3 places:
|
|
16 |
* - Load_ORDR, all the global orders
|
|
17 |
* - Vehicle -> current_order
|
|
18 |
* - REF_ORDER (all REFs are currently limited to 16 bits!!)
|
|
19 |
*/
|
|
20 |
struct Order : PoolItem<Order, OrderID, &_Order_pool> {
|
|
21 |
Order *next; ///< Pointer to next order. If NULL, end of list
|
|
22 |
|
|
23 |
OrderTypeByte type;
|
|
24 |
uint8 flags;
|
|
25 |
DestinationID dest; ///< The destionation of the order.
|
|
26 |
|
|
27 |
CargoID refit_cargo; // Refit CargoID
|
|
28 |
byte refit_subtype; // Refit subtype
|
|
29 |
|
|
30 |
uint16 wait_time; ///< How long in ticks to wait at the destination.
|
|
31 |
uint16 travel_time; ///< How long in ticks the journey to this destination should take.
|
|
32 |
|
|
33 |
Order() : refit_cargo(CT_NO_REFIT) {}
|
|
34 |
~Order() { this->type = OT_NOTHING; }
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Check if a Order really exists.
|
|
38 |
*/
|
|
39 |
inline bool IsValid() const { return this->type != OT_NOTHING; }
|
|
40 |
|
|
41 |
void Free();
|
|
42 |
void FreeChain();
|
|
43 |
};
|
|
44 |
|
|
45 |
static inline VehicleOrderID GetMaxOrderIndex()
|
|
46 |
{
|
|
47 |
/* TODO - This isn't the real content of the function, but
|
|
48 |
* with the new pool-system this will be replaced with one that
|
|
49 |
* _really_ returns the highest index. Now it just returns
|
|
50 |
* the next safe value we are sure about everything is below.
|
|
51 |
*/
|
|
52 |
return GetOrderPoolSize() - 1;
|
|
53 |
}
|
|
54 |
|
|
55 |
static inline VehicleOrderID GetNumOrders()
|
|
56 |
{
|
|
57 |
return GetOrderPoolSize();
|
|
58 |
}
|
|
59 |
|
|
60 |
inline void Order::Free()
|
|
61 |
{
|
|
62 |
this->type = OT_NOTHING;
|
|
63 |
this->flags = 0;
|
|
64 |
this->dest = 0;
|
|
65 |
this->next = NULL;
|
|
66 |
}
|
|
67 |
|
|
68 |
inline void Order::FreeChain()
|
|
69 |
{
|
|
70 |
if (next != NULL) next->FreeChain();
|
|
71 |
delete this;
|
|
72 |
}
|
|
73 |
|
|
74 |
#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())
|
|
75 |
#define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
|
|
76 |
|
|
77 |
|
|
78 |
#define FOR_VEHICLE_ORDERS(v, order) for (order = v->orders; order != NULL; order = order->next)
|
|
79 |
|
|
80 |
static inline bool HasOrderPoolFree(uint amount)
|
|
81 |
{
|
|
82 |
const Order *order;
|
|
83 |
|
|
84 |
/* There is always room if not all blocks in the pool are reserved */
|
|
85 |
if (_Order_pool.CanAllocateMoreBlocks()) return true;
|
|
86 |
|
|
87 |
FOR_ALL_ORDERS(order) if (!order->IsValid() && --amount == 0) return true;
|
|
88 |
|
|
89 |
return false;
|
|
90 |
}
|
|
91 |
|
|
92 |
|
|
93 |
/* Pack and unpack routines */
|
|
94 |
|
|
95 |
static inline uint32 PackOrder(const Order *order)
|
|
96 |
{
|
|
97 |
return order->dest << 16 | order->flags << 8 | order->type;
|
|
98 |
}
|
|
99 |
|
|
100 |
static inline Order UnpackOrder(uint32 packed)
|
|
101 |
{
|
|
102 |
Order order;
|
|
103 |
order.type = (OrderType)GB(packed, 0, 8);
|
|
104 |
order.flags = GB(packed, 8, 8);
|
|
105 |
order.dest = GB(packed, 16, 16);
|
|
106 |
order.next = NULL;
|
|
107 |
order.index = 0; // avoid compiler warning
|
|
108 |
order.refit_cargo = CT_NO_REFIT;
|
|
109 |
order.refit_subtype = 0;
|
|
110 |
order.wait_time = 0;
|
|
111 |
order.travel_time = 0;
|
|
112 |
return order;
|
|
113 |
}
|
|
114 |
|
|
115 |
void AssignOrder(Order *order, Order data);
|
|
116 |
Order UnpackOldOrder(uint16 packed);
|
|
117 |
|
|
118 |
#endif /* ORDER_H */
|