Files @ r4381:c965d1f3016a
Branch filter:

Location: cpp/openttd-patchpack/source/order.h - annotation

belugas
(svn r6131) -Codechange : Complete all missing _ttdpatch_flags entries
-Feature : both unifiedmaglevmode are now set.
Maglev and monorail are not allowed to run on each other tracks and will not be.
Setting those flags will allow grfsets as the Norvegian one to be loaded
-Codechange : link the TTDPatch's irregularstations with OTTD's nonuniform_stations
-Codechange : Reformat the whole array (thanks Rubidium, it sure looks better now)
r2186:5ee653b1b5e1
r2186:5ee653b1b5e1
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r2159:2c706fe6b0a7
r1314:8a8968a910c2
r1314:8a8968a910c2
r1024:c7c4672d839e
r4351:04a502e12263
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r4351:04a502e12263
r1024:c7c4672d839e
r1024:c7c4672d839e
r1615:6d516e05db7d
r2214:59bf23688ead
r2214:59bf23688ead
r1530:c89d38477ba0
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r1530:c89d38477ba0
r1530:c89d38477ba0
r2214:59bf23688ead
r4344:bff007ae1fd1
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r1530:c89d38477ba0
r1530:c89d38477ba0
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r1024:c7c4672d839e
r1024:c7c4672d839e
r2214:59bf23688ead
r4077:259c4c4aacad
r4077:259c4c4aacad
r4077:259c4c4aacad
r1024:c7c4672d839e
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r2214:59bf23688ead
r1024:c7c4672d839e
r1024:c7c4672d839e
r1615:6d516e05db7d
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1053:58d626056498
r1024:c7c4672d839e
r4077:259c4c4aacad
r4077:259c4c4aacad
r4077:259c4c4aacad
r4077:259c4c4aacad
r1024:c7c4672d839e
r4351:04a502e12263
r1024:c7c4672d839e
r3347:b0be9ba36dd3
r1024:c7c4672d839e
r3173:acfc19891f47
r1024:c7c4672d839e
r3173:acfc19891f47
r1024:c7c4672d839e
r1024:c7c4672d839e
r2433:404fea06bb41
r2433:404fea06bb41
r1024:c7c4672d839e
r1024:c7c4672d839e
r1786:f4dff9cc496a
r2433:404fea06bb41
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1314:8a8968a910c2
r1024:c7c4672d839e
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1024:c7c4672d839e
r1024:c7c4672d839e
r1314:8a8968a910c2
r1024:c7c4672d839e
r1024:c7c4672d839e
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4354:95cd3ce6deea
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r4346:fa4ac6b6f852
r1314:8a8968a910c2
r1314:8a8968a910c2
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1043:4bce03f32f7e
r1024:c7c4672d839e
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1314:8a8968a910c2
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1093:18f56ef2d029
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r4351:04a502e12263
r2140:efb49af98a17
r2140:efb49af98a17
r1024:c7c4672d839e
r1884:a82518e64689
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1796:4bbf4ea0805e
r2630:73464cd368d6
r4351:04a502e12263
r1024:c7c4672d839e
r1024:c7c4672d839e
r3140:72affb391e90
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r2630:73464cd368d6
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
r1024:c7c4672d839e
/* $Id$ */

/** @file order.h
  */

#ifndef ORDER_H
#define ORDER_H

#include "macros.h"
#include "pool.h"

/* Order types */
typedef enum OrderTypes {
	OT_NOTHING       = 0,
	OT_GOTO_STATION  = 1,
	OT_GOTO_DEPOT    = 2,
	OT_LOADING       = 3,
	OT_LEAVESTATION  = 4,
	OT_DUMMY         = 5,
	OT_GOTO_WAYPOINT = 6
} OrderType;

/* Order flags -- please use OFB instead OF and use HASBIT/SETBIT/CLEARBIT */

/** Order flag masks - these are for direct bit operations */
enum OrderFlagMasks {
	//Flags for stations:
	/** vehicle will transfer cargo (i. e. not deliver to nearby industry/town even if accepted there) */
	OF_TRANSFER           = 0x1,
	/** If OF_TRANSFER is not set, drop any cargo loaded. If accepted, deliver, otherwise cargo remains at the station.
      * No new cargo is loaded onto the vehicle whatsoever */
	OF_UNLOAD             = 0x2,
	/** Wait for full load of all vehicles, or of at least one cargo type, depending on patch setting
	  * @todo make this two different flags */
	OF_FULL_LOAD          = 0x4,

	//Flags for depots:
	/** The current depot-order was initiated because it was in the vehicle's order list */
	OF_PART_OF_ORDERS     = 0x2,
	/** if OF_PART_OF_ORDERS is not set, this will cause the vehicle to be stopped in the depot */
	OF_HALT_IN_DEPOT      = 0x4,
	/** if OF_PART_OF_ORDERS is set, this will cause the order only be come active if the vehicle needs servicing */
	OF_SERVICE_IF_NEEDED  = 0x4, //used when OF_PART_OF_ORDERS is set.

	//Common flags
	/** This causes the vehicle not to stop at intermediate OR the destination station (depending on patch settings)
	  * @todo make this two different flags */
	OF_NON_STOP           = 0x8
};

/** Order flags bits - these are for the *BIT macros
 * for descrption of flags, see OrderFlagMasks
 * @see OrderFlagMasks
 */
enum {
	OFB_TRANSFER          = 0,
	OFB_UNLOAD            = 1,
	OFB_FULL_LOAD         = 2,
	OFB_PART_OF_ORDERS    = 1,
	OFB_HALT_IN_DEPOT     = 2,
	OFB_SERVICE_IF_NEEDED = 2,
	OFB_NON_STOP          = 3
};


/* Possible clone options */
enum {
	CO_SHARE   = 0,
	CO_COPY    = 1,
	CO_UNSHARE = 2
};


/* If you change this, keep in mind that it is saved on 3 places:
 * - Load_ORDR, all the global orders
 * - Vehicle -> current_order
 * - REF_SHEDULE (all REFs are currently limited to 16 bits!!)
 */
typedef struct Order {
	OrderType type;
	uint8  flags;
	StationID station;

	struct Order *next;   ///< Pointer to next order. If NULL, end of list

	uint16 index;         ///< Index of the order, is not saved or anything, just for reference
} Order;

#define MAX_BACKUP_ORDER_COUNT 40

typedef struct {
	VehicleID clone;
	OrderID orderindex;
	Order order[MAX_BACKUP_ORDER_COUNT + 1];
	uint16 service_interval;
	char name[32];
} BackuppedOrders;

VARDEF TileIndex _backup_orders_tile;
VARDEF BackuppedOrders _backup_orders_data[1];

extern MemoryPool _order_pool;

/**
 * Get the pointer to the order with index 'index'
 */
static inline Order *GetOrder(uint index)
{
	return (Order*)GetItemFromPool(&_order_pool, index);
}

/**
 * Get the current size of the OrderPool
 */
static inline uint16 GetOrderPoolSize(void)
{
	return _order_pool.total_items;
}

static inline OrderID GetOrderArraySize(void)
{
	/* TODO - This isn't the real content of the function, but
	 *  with the new pool-system this will be replaced with one that
	 *  _really_ returns the highest index + 1. Now it just returns
	 *  the next safe value we are sure about everything is below.
	 */
	return GetOrderPoolSize();
}

/**
 * Check if a Order really exists.
 */
static inline bool IsValidOrder(const Order *o)
{
	return o->type != OT_NOTHING;
}

#define FOR_ALL_ORDERS_FROM(order, start) for (order = GetOrder(start); order != NULL; order = (order->index + 1 < GetOrderPoolSize()) ? GetOrder(order->index + 1) : NULL) if (IsValidOrder(order))
#define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)


#define FOR_VEHICLE_ORDERS(v, order) for (order = v->orders; order != NULL; order = order->next)

static inline bool HasOrderPoolFree(uint amount)
{
	const Order *order;

	/* There is always room if not all blocks in the pool are reserved */
	if (_order_pool.current_blocks < _order_pool.max_blocks)
		return true;

	FOR_ALL_ORDERS(order)
		if (order->type == OT_NOTHING)
			if (--amount == 0)
				return true;

	return false;
}

static inline bool IsOrderPoolFull(void)
{
	return !HasOrderPoolFree(1);
}

/* Pack and unpack routines */

static inline uint32 PackOrder(const Order *order)
{
	return order->station << 16 | order->flags << 8 | order->type;
}

static inline Order UnpackOrder(uint32 packed)
{
	Order order;
	order.type    = (OrderType)GB(packed,  0,  8);
	order.flags   = GB(packed,  8,  8);
	order.station = GB(packed, 16, 16);
	order.next    = NULL;
	order.index   = 0; // avoid compiler warning
	return order;
}

/* Functions */
void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *order);
void RestoreVehicleOrders(const Vehicle* v, const BackuppedOrders* order);
void RemoveOrderFromAllVehicles(OrderType type, StationID destination);
void InvalidateVehicleOrder(const Vehicle *v);
bool VehicleHasDepotOrders(const Vehicle *v);
void CheckOrders(const Vehicle*);
void DeleteVehicleOrders(Vehicle *v);
bool IsOrderListShared(const Vehicle *v);
void AssignOrder(Order *order, Order data);
bool CheckForValidOrders(const Vehicle* v);

Order UnpackOldOrder(uint16 packed);

#endif /* ORDER_H */