2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2006-09-04 20:40:33 +00:00
|
|
|
/** @file order.h */
|
2005-07-28 06:09:15 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
#ifndef ORDER_H
|
|
|
|
#define ORDER_H
|
|
|
|
|
2008-03-28 16:36:32 +00:00
|
|
|
#include "order_type.h"
|
2006-12-03 17:27:43 +00:00
|
|
|
#include "oldpool.h"
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2007-12-21 22:50:51 +00:00
|
|
|
#include "cargo_type.h"
|
|
|
|
#include "vehicle_type.h"
|
2007-12-25 23:42:52 +00:00
|
|
|
#include "tile_type.h"
|
2008-01-07 00:57:19 +00:00
|
|
|
#include "date_type.h"
|
2008-03-28 16:34:50 +00:00
|
|
|
#include "group_type.h"
|
2005-02-06 10:24:57 +00:00
|
|
|
|
2007-08-02 23:40:19 +00:00
|
|
|
DECLARE_OLD_POOL(Order, Order, 6, 1000)
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* If you change this, keep in mind that it is saved on 3 places:
|
2006-06-27 21:25:53 +00:00
|
|
|
* - Load_ORDR, all the global orders
|
|
|
|
* - Vehicle -> current_order
|
2007-06-20 19:17:22 +00:00
|
|
|
* - REF_ORDER (all REFs are currently limited to 16 bits!!)
|
2006-06-27 21:25:53 +00:00
|
|
|
*/
|
2007-08-02 23:40:19 +00:00
|
|
|
struct Order : PoolItem<Order, OrderID, &_Order_pool> {
|
2007-03-07 12:11:48 +00:00
|
|
|
Order *next; ///< Pointer to next order. If NULL, end of list
|
2007-02-04 13:46:21 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
OrderTypeByte type;
|
2005-01-15 19:06:22 +00:00
|
|
|
uint8 flags;
|
2006-08-26 16:34:03 +00:00
|
|
|
DestinationID dest; ///< The destionation of the order.
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2006-10-03 14:52:39 +00:00
|
|
|
CargoID refit_cargo; // Refit CargoID
|
|
|
|
byte refit_subtype; // Refit subtype
|
2007-03-08 21:39:34 +00:00
|
|
|
|
2007-06-20 19:17:22 +00:00
|
|
|
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.
|
|
|
|
|
2007-08-02 23:40:19 +00:00
|
|
|
Order() : refit_cargo(CT_NO_REFIT) {}
|
|
|
|
~Order() { this->type = OT_NOTHING; }
|
|
|
|
|
2007-08-30 20:40:33 +00:00
|
|
|
/**
|
|
|
|
* Check if a Order really exists.
|
|
|
|
*/
|
|
|
|
inline bool IsValid() const { return this->type != OT_NOTHING; }
|
|
|
|
|
2007-03-08 21:39:34 +00:00
|
|
|
void Free();
|
|
|
|
void FreeChain();
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct BackuppedOrders {
|
2008-01-17 02:09:34 +00:00
|
|
|
BackuppedOrders() : order(NULL), name(NULL) { }
|
|
|
|
~BackuppedOrders() { free(order); free(name); }
|
2007-09-28 21:15:45 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
VehicleID clone;
|
2006-08-26 17:12:24 +00:00
|
|
|
VehicleOrderID orderindex;
|
2008-02-27 21:46:57 +00:00
|
|
|
GroupID group;
|
2007-09-28 21:15:45 +00:00
|
|
|
Order *order;
|
2005-01-15 19:06:22 +00:00
|
|
|
uint16 service_interval;
|
2008-01-12 19:58:06 +00:00
|
|
|
char *name;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2008-01-13 13:36:01 +00:00
|
|
|
extern TileIndex _backup_orders_tile;
|
|
|
|
extern BackuppedOrders _backup_orders_data;
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline VehicleOrderID GetMaxOrderIndex()
|
2006-08-22 20:41:26 +00:00
|
|
|
{
|
|
|
|
/* TODO - This isn't the real content of the function, but
|
|
|
|
* with the new pool-system this will be replaced with one that
|
2006-12-05 13:58:20 +00:00
|
|
|
* _really_ returns the highest index. Now it just returns
|
2006-08-22 20:41:26 +00:00
|
|
|
* the next safe value we are sure about everything is below.
|
|
|
|
*/
|
2006-12-05 13:58:20 +00:00
|
|
|
return GetOrderPoolSize() - 1;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline VehicleOrderID GetNumOrders()
|
2006-12-05 13:58:20 +00:00
|
|
|
{
|
2006-08-22 20:41:26 +00:00
|
|
|
return GetOrderPoolSize();
|
|
|
|
}
|
|
|
|
|
2007-03-08 21:39:34 +00:00
|
|
|
inline void Order::Free()
|
2006-08-22 15:33:35 +00:00
|
|
|
{
|
2007-08-02 23:40:19 +00:00
|
|
|
this->type = OT_NOTHING;
|
|
|
|
this->flags = 0;
|
|
|
|
this->dest = 0;
|
|
|
|
this->next = NULL;
|
2006-08-22 15:33:35 +00:00
|
|
|
}
|
|
|
|
|
2007-03-08 21:39:34 +00:00
|
|
|
inline void Order::FreeChain()
|
2006-08-26 14:22:54 +00:00
|
|
|
{
|
2007-03-08 21:39:34 +00:00
|
|
|
if (next != NULL) next->FreeChain();
|
2007-08-02 23:40:19 +00:00
|
|
|
delete this;
|
2006-08-26 14:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-03-08 21:39:34 +00:00
|
|
|
#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())
|
2005-02-06 10:24:57 +00:00
|
|
|
#define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
#define FOR_VEHICLE_ORDERS(v, order) for (order = v->orders; order != NULL; order = order->next)
|
|
|
|
|
|
|
|
static inline bool HasOrderPoolFree(uint amount)
|
|
|
|
{
|
2005-01-16 14:42:53 +00:00
|
|
|
const Order *order;
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2005-02-06 10:24:57 +00:00
|
|
|
/* There is always room if not all blocks in the pool are reserved */
|
2007-08-01 22:10:54 +00:00
|
|
|
if (_Order_pool.CanAllocateMoreBlocks()) return true;
|
2005-02-06 10:24:57 +00:00
|
|
|
|
2007-08-01 22:10:54 +00:00
|
|
|
FOR_ALL_ORDERS(order) if (!order->IsValid() && --amount == 0) return true;
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Pack and unpack routines */
|
|
|
|
|
|
|
|
static inline uint32 PackOrder(const Order *order)
|
|
|
|
{
|
2006-09-03 08:25:27 +00:00
|
|
|
return order->dest << 16 | order->flags << 8 | order->type;
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Order UnpackOrder(uint32 packed)
|
|
|
|
{
|
|
|
|
Order order;
|
2006-08-22 17:13:49 +00:00
|
|
|
order.type = (OrderType)GB(packed, 0, 8);
|
2005-07-20 15:29:28 +00:00
|
|
|
order.flags = GB(packed, 8, 8);
|
2006-09-03 08:25:27 +00:00
|
|
|
order.dest = GB(packed, 16, 16);
|
2005-01-15 19:06:22 +00:00
|
|
|
order.next = NULL;
|
2005-06-01 11:52:44 +00:00
|
|
|
order.index = 0; // avoid compiler warning
|
2006-10-08 20:54:27 +00:00
|
|
|
order.refit_cargo = CT_NO_REFIT;
|
2006-10-03 16:05:11 +00:00
|
|
|
order.refit_subtype = 0;
|
2007-06-20 19:17:22 +00:00
|
|
|
order.wait_time = 0;
|
|
|
|
order.travel_time = 0;
|
2005-01-15 19:06:22 +00:00
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Functions */
|
2007-09-28 21:15:45 +00:00
|
|
|
void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *order = &_backup_orders_data);
|
|
|
|
void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *order = &_backup_orders_data);
|
2006-08-26 16:34:03 +00:00
|
|
|
void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination);
|
2005-01-15 19:06:22 +00:00
|
|
|
void InvalidateVehicleOrder(const Vehicle *v);
|
|
|
|
bool VehicleHasDepotOrders(const Vehicle *v);
|
2006-03-04 11:15:44 +00:00
|
|
|
void CheckOrders(const Vehicle*);
|
2005-01-15 19:06:22 +00:00
|
|
|
void DeleteVehicleOrders(Vehicle *v);
|
|
|
|
void AssignOrder(Order *order, Order data);
|
2005-11-13 13:43:55 +00:00
|
|
|
bool CheckForValidOrders(const Vehicle* v);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
Order UnpackOldOrder(uint16 packed);
|
|
|
|
|
2008-01-07 00:57:19 +00:00
|
|
|
#define MIN_SERVINT_PERCENT 5
|
|
|
|
#define MAX_SERVINT_PERCENT 90
|
|
|
|
#define MIN_SERVINT_DAYS 30
|
|
|
|
#define MAX_SERVINT_DAYS 800
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the service interval domain.
|
|
|
|
* Get the new proposed service interval for the vehicle is indeed, clamped
|
|
|
|
* within the given bounds. @see MIN_SERVINT_PERCENT ,etc.
|
|
|
|
* @param index proposed service interval
|
|
|
|
* @return service interval
|
|
|
|
*/
|
|
|
|
Date GetServiceIntervalClamped(uint index);
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
#endif /* ORDER_H */
|