1
0
Fork 0

(svn r10760) -Codechange: make the order struct use the pool item class as super class.

release/0.6
rubidium 2007-08-02 23:40:19 +00:00
parent e4149482ec
commit d751ce56cb
2 changed files with 21 additions and 75 deletions

View File

@ -83,26 +83,30 @@ enum {
CO_UNSHARE = 2 CO_UNSHARE = 2
}; };
struct Order;
DECLARE_OLD_POOL(Order, Order, 6, 1000)
/* If you change this, keep in mind that it is saved on 3 places: /* If you change this, keep in mind that it is saved on 3 places:
* - Load_ORDR, all the global orders * - Load_ORDR, all the global orders
* - Vehicle -> current_order * - Vehicle -> current_order
* - REF_ORDER (all REFs are currently limited to 16 bits!!) * - REF_ORDER (all REFs are currently limited to 16 bits!!)
*/ */
struct Order { struct Order : PoolItem<Order, OrderID, &_Order_pool> {
Order *next; ///< Pointer to next order. If NULL, end of list Order *next; ///< Pointer to next order. If NULL, end of list
OrderTypeByte type; OrderTypeByte type;
uint8 flags; uint8 flags;
DestinationID dest; ///< The destionation of the order. DestinationID dest; ///< The destionation of the order.
OrderID index; ///< Index of the order, is not saved or anything, just for reference
CargoID refit_cargo; // Refit CargoID CargoID refit_cargo; // Refit CargoID
byte refit_subtype; // Refit subtype byte refit_subtype; // Refit subtype
uint16 wait_time; ///< How long in ticks to wait at the destination. uint16 wait_time; ///< How long in ticks to wait at the destination.
uint16 travel_time; ///< How long in ticks the journey to this destination should take. uint16 travel_time; ///< How long in ticks the journey to this destination should take.
Order() : refit_cargo(CT_NO_REFIT) {}
~Order() { this->type = OT_NOTHING; }
bool IsValid() const; bool IsValid() const;
void Free(); void Free();
void FreeChain(); void FreeChain();
@ -121,8 +125,6 @@ struct BackuppedOrders {
VARDEF TileIndex _backup_orders_tile; VARDEF TileIndex _backup_orders_tile;
VARDEF BackuppedOrders _backup_orders_data[1]; VARDEF BackuppedOrders _backup_orders_data[1];
DECLARE_OLD_POOL(Order, Order, 6, 1000)
static inline VehicleOrderID GetMaxOrderIndex() static inline VehicleOrderID GetMaxOrderIndex()
{ {
/* TODO - This isn't the real content of the function, but /* TODO - This isn't the real content of the function, but
@ -143,21 +145,21 @@ static inline VehicleOrderID GetNumOrders()
*/ */
inline bool Order::IsValid() const inline bool Order::IsValid() const
{ {
return type != OT_NOTHING; return this->type != OT_NOTHING;
} }
inline void Order::Free() inline void Order::Free()
{ {
type = OT_NOTHING; this->type = OT_NOTHING;
flags = 0; this->flags = 0;
dest = 0; this->dest = 0;
next = NULL; this->next = NULL;
} }
inline void Order::FreeChain() inline void Order::FreeChain()
{ {
if (next != NULL) next->FreeChain(); if (next != NULL) next->FreeChain();
Free(); delete this;
} }
#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()) #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())

View File

@ -20,19 +20,7 @@
#include "cargotype.h" #include "cargotype.h"
#include "strings.h" #include "strings.h"
/** DEFINE_OLD_POOL_GENERIC(Order, Order)
* Called if a new block is added to the order-pool
*/
static void OrderPoolNewBlock(uint start_item)
{
Order *order;
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (order = GetOrder(start_item); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) order->index = start_item++;
}
DEFINE_OLD_POOL(Order, Order, OrderPoolNewBlock, NULL)
/** /**
* *
@ -111,41 +99,6 @@ static void SwapOrders(Order *order1, Order *order2)
order2->next = temp_order.next; order2->next = temp_order.next;
} }
/**
*
* Allocate a new order
*
* @return Order* if a free space is found, else NULL.
*
*/
static Order *AllocateOrder()
{
Order *order;
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (order = GetOrder(0); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) {
if (!order->IsValid()) {
OrderID index = order->index;
memset(order, 0, sizeof(*order));
order->index = index;
order->next = NULL;
order->refit_cargo = CT_NO_REFIT;
order->refit_subtype = 0;
order->wait_time = 0;
order->travel_time = 0;
return order;
}
}
/* Check if we can add a block to the pool */
if (AddBlockToPool(&_Order_pool)) return AllocateOrder();
return NULL;
}
/** /**
* *
* Assign data to an order (from an other order) * Assign data to an order (from an other order)
@ -395,7 +348,7 @@ CommandCost CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
Vehicle *u; Vehicle *u;
Order *new_o = AllocateOrder(); Order *new_o = new Order();
AssignOrder(new_o, new_order); AssignOrder(new_o, new_order);
/* Create new order and link in list */ /* Create new order and link in list */
@ -890,7 +843,7 @@ CommandCost CmdCloneOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
order_dst = &dst->orders; order_dst = &dst->orders;
FOR_VEHICLE_ORDERS(src, order) { FOR_VEHICLE_ORDERS(src, order) {
*order_dst = AllocateOrder(); *order_dst = new Order();
AssignOrder(*order_dst, *order); AssignOrder(*order_dst, *order);
order_dst = &(*order_dst)->next; order_dst = &(*order_dst)->next;
} }
@ -1367,10 +1320,8 @@ static void Load_ORDR()
SlArray(orders, len, SLE_UINT16); SlArray(orders, len, SLE_UINT16);
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
if (!AddBlockIfNeeded(&_Order_pool, i)) Order *order = new (i) Order();
error("Orders: failed loading savegame: too many orders"); AssignOrder(order, UnpackVersion4Order(orders[i]));
AssignOrder(GetOrder(i), UnpackVersion4Order(orders[i]));
} }
} else if (CheckSavegameVersionOldStyle(5, 2)) { } else if (CheckSavegameVersionOldStyle(5, 2)) {
uint32 orders[5000]; uint32 orders[5000];
@ -1381,10 +1332,8 @@ static void Load_ORDR()
SlArray(orders, len, SLE_UINT32); SlArray(orders, len, SLE_UINT32);
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
if (!AddBlockIfNeeded(&_Order_pool, i)) Order *order = new (i) Order();
error("Orders: failed loading savegame: too many orders"); AssignOrder(order, UnpackOrder(orders[i]));
AssignOrder(GetOrder(i), UnpackOrder(orders[i]));
} }
} }
@ -1400,12 +1349,7 @@ static void Load_ORDR()
int index; int index;
while ((index = SlIterateArray()) != -1) { while ((index = SlIterateArray()) != -1) {
Order *order; Order *order = new (index) Order();
if (!AddBlockIfNeeded(&_Order_pool, index))
error("Orders: failed loading savegame: too many orders");
order = GetOrder(index);
SlObject(order, _order_desc); SlObject(order, _order_desc);
} }
} }