src/order.h
branchNewGRF_ports
changeset 6743 cabfaa4a0295
parent 6720 35756db7e577
child 6868 7eb395287b3d
equal deleted inserted replaced
6742:1337d6c9b97b 6743:cabfaa4a0295
    81 	CO_SHARE   = 0,
    81 	CO_SHARE   = 0,
    82 	CO_COPY    = 1,
    82 	CO_COPY    = 1,
    83 	CO_UNSHARE = 2
    83 	CO_UNSHARE = 2
    84 };
    84 };
    85 
    85 
       
    86 struct Order;
       
    87 DECLARE_OLD_POOL(Order, Order, 6, 1000)
       
    88 
    86 /* If you change this, keep in mind that it is saved on 3 places:
    89 /* If you change this, keep in mind that it is saved on 3 places:
    87  * - Load_ORDR, all the global orders
    90  * - Load_ORDR, all the global orders
    88  * - Vehicle -> current_order
    91  * - Vehicle -> current_order
    89  * - REF_ORDER (all REFs are currently limited to 16 bits!!)
    92  * - REF_ORDER (all REFs are currently limited to 16 bits!!)
    90  */
    93  */
    91 struct Order {
    94 struct Order : PoolItem<Order, OrderID, &_Order_pool> {
    92 	Order *next;          ///< Pointer to next order. If NULL, end of list
    95 	Order *next;          ///< Pointer to next order. If NULL, end of list
    93 
    96 
    94 	OrderTypeByte type;
    97 	OrderTypeByte type;
    95 	uint8  flags;
    98 	uint8  flags;
    96 	DestinationID dest;   ///< The destionation of the order.
    99 	DestinationID dest;   ///< The destionation of the order.
    97 
   100 
    98 	OrderID index;         ///< Index of the order, is not saved or anything, just for reference
       
    99 
       
   100 	CargoID refit_cargo; // Refit CargoID
   101 	CargoID refit_cargo; // Refit CargoID
   101 	byte refit_subtype; // Refit subtype
   102 	byte refit_subtype; // Refit subtype
   102 
   103 
   103 	uint16 wait_time;    ///< How long in ticks to wait at the destination.
   104 	uint16 wait_time;    ///< How long in ticks to wait at the destination.
   104 	uint16 travel_time;  ///< How long in ticks the journey to this destination should take.
   105 	uint16 travel_time;  ///< How long in ticks the journey to this destination should take.
       
   106 
       
   107 	Order() : refit_cargo(CT_NO_REFIT) {}
       
   108 	~Order() { this->type = OT_NOTHING; }
   105 
   109 
   106 	bool IsValid() const;
   110 	bool IsValid() const;
   107 	void Free();
   111 	void Free();
   108 	void FreeChain();
   112 	void FreeChain();
   109 };
   113 };
   119 };
   123 };
   120 
   124 
   121 VARDEF TileIndex _backup_orders_tile;
   125 VARDEF TileIndex _backup_orders_tile;
   122 VARDEF BackuppedOrders _backup_orders_data[1];
   126 VARDEF BackuppedOrders _backup_orders_data[1];
   123 
   127 
   124 DECLARE_OLD_POOL(Order, Order, 6, 1000)
       
   125 
       
   126 static inline VehicleOrderID GetMaxOrderIndex()
   128 static inline VehicleOrderID GetMaxOrderIndex()
   127 {
   129 {
   128 	/* TODO - This isn't the real content of the function, but
   130 	/* TODO - This isn't the real content of the function, but
   129 	 *  with the new pool-system this will be replaced with one that
   131 	 *  with the new pool-system this will be replaced with one that
   130 	 *  _really_ returns the highest index. Now it just returns
   132 	 *  _really_ returns the highest index. Now it just returns
   141 /**
   143 /**
   142  * Check if a Order really exists.
   144  * Check if a Order really exists.
   143  */
   145  */
   144 inline bool Order::IsValid() const
   146 inline bool Order::IsValid() const
   145 {
   147 {
   146 	return type != OT_NOTHING;
   148 	return this->type != OT_NOTHING;
   147 }
   149 }
   148 
   150 
   149 inline void Order::Free()
   151 inline void Order::Free()
   150 {
   152 {
   151 	type  = OT_NOTHING;
   153 	this->type  = OT_NOTHING;
   152 	flags = 0;
   154 	this->flags = 0;
   153 	dest  = 0;
   155 	this->dest  = 0;
   154 	next  = NULL;
   156 	this->next  = NULL;
   155 }
   157 }
   156 
   158 
   157 inline void Order::FreeChain()
   159 inline void Order::FreeChain()
   158 {
   160 {
   159 	if (next != NULL) next->FreeChain();
   161 	if (next != NULL) next->FreeChain();
   160 	Free();
   162 	delete this;
   161 }
   163 }
   162 
   164 
   163 #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())
   165 #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())
   164 #define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
   166 #define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
   165 
   167 
   169 static inline bool HasOrderPoolFree(uint amount)
   171 static inline bool HasOrderPoolFree(uint amount)
   170 {
   172 {
   171 	const Order *order;
   173 	const Order *order;
   172 
   174 
   173 	/* There is always room if not all blocks in the pool are reserved */
   175 	/* There is always room if not all blocks in the pool are reserved */
   174 	if (_Order_pool.current_blocks < _Order_pool.max_blocks)
   176 	if (_Order_pool.CanAllocateMoreBlocks()) return true;
   175 		return true;
   177 
   176 
   178 	FOR_ALL_ORDERS(order) if (!order->IsValid() && --amount == 0) return true;
   177 	FOR_ALL_ORDERS(order)
       
   178 		if (!order->IsValid())
       
   179 			if (--amount == 0)
       
   180 				return true;
       
   181 
   179 
   182 	return false;
   180 	return false;
   183 }
   181 }
   184 
   182 
   185 
   183