Changeset - r8839:479975d442dd
[Not reviewed]
master
0 7 0
rubidium - 16 years ago 2008-04-06 07:22:26
rubidium@openttd.org
(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
7 files changed with 56 insertions and 70 deletions:
0 comments (0 inline, 0 general)
src/ai/default/default.cpp
Show inline comments
 
@@ -318,7 +318,7 @@ static void AiRestoreVehicleOrders(Vehic
 
	if (bak->order == NULL) return;
 

	
 
	for (uint i = 0; !bak->order[i].IsType(OT_NOTHING); i++) {
 
		if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
 
		if (!DoCommandP(0, v->index + (i << 16), bak->order[i].Pack(), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
 
			break;
 
	}
 
}
 
@@ -2558,7 +2558,7 @@ handle_nocash:
 
		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
 
			order.flags |= OFB_FULL_LOAD;
 

	
 
		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
 
		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 
	}
 

	
 
	DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_TRAIN);
 
@@ -3293,7 +3293,7 @@ static void AiStateBuildRoadVehicles(Pla
 
		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
 
			order.flags |= OFB_FULL_LOAD;
 

	
 
		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
 
		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 
	}
 

	
 
	DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
 
@@ -3572,7 +3572,7 @@ static void AiStateBuildAircraftVehicles
 
		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
 
			order.flags |= OFB_FULL_LOAD;
 

	
 
		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
 
		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 
	}
 

	
 
	DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
src/ai/trolly/trolly.cpp
Show inline comments
 
@@ -1185,20 +1185,20 @@ static void AiNew_State_GiveOrders(Playe
 
	if (_patches.gotodepot) {
 
		idx = 0;
 
		order.MakeGoToDepot(GetDepotByTile(_players_ainew[p->index].depot_tile)->index, true);
 
		AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
 
		AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 
	}
 

	
 
	idx = 0;
 
	order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].to_tile));
 
	if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].to_deliver)
 
		order.flags |= OFB_FULL_LOAD;
 
	AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
 
	AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 

	
 
	idx = 0;
 
	order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].from_tile));
 
	if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].from_deliver)
 
		order.flags |= OFB_FULL_LOAD;
 
	AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
 
	AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 

	
 
	// Start the engines!
 
	_players_ainew[p->index].state = AI_STATE_START_VEHICLE;
src/newgrf_engine.cpp
Show inline comments
 
@@ -681,8 +681,8 @@ static uint32 VehicleGetVariable(const R
 
		case 0x01: return MapOldSubType(v);
 
		case 0x04: return v->index;
 
		case 0x05: return GB(v->index, 8, 8);
 
		case 0x0A: return PackOrder(&v->current_order);
 
		case 0x0B: return GB(PackOrder(&v->current_order), 8, 8);
 
		case 0x0A: return v->current_order.Pack();
 
		case 0x0B: return GB(v->current_order.Pack(), 8, 8);
 
		case 0x0C: return v->num_orders;
 
		case 0x0D: return v->cur_order_index;
 
		case 0x10: return v->load_unload_time_rem;
src/oldloader.cpp
Show inline comments
 
@@ -504,7 +504,7 @@ static bool LoadOldOrder(LoadgameState *
 
{
 
	if (!LoadChunk(ls, NULL, order_chunk)) return false;
 

	
 
	(new (num) Order())->AssignOrder(UnpackOldOrder(_old_order));
 
	new (num) Order(UnpackOldOrder(_old_order));
 

	
 
	/* Relink the orders to eachother (in TTD(Patch) the orders for one
 
	vehicle are behind eachother, with an invalid order (OT_NOTHING) as indication that
src/order_base.h
Show inline comments
 
@@ -26,10 +26,6 @@ private:
 
	friend const struct SaveLoad *GetVehicleDescription(VehicleType vt); ///< Saving and loading the current order of vehicles.
 
	friend void Load_VEHS();                                             ///< Loading of ancient vehicles.
 
	friend const struct SaveLoad *GetOrderDescription();                 ///< Saving and loading of orders.
 
	friend uint32 PackOrder(const Order *order);                         ///< 'Compressing' an order.
 
	friend Order UnpackOrder(uint32 packed);                             ///< 'Uncompressing' an order.
 
	friend Order UnpackOldOrder(uint16 packed);                          ///< 'Uncompressing' a loaded old order.
 
	friend Order UnpackVersion4Order(uint16 packed);                     ///< 'Uncompressing' a loaded ancient order.
 

	
 
	OrderTypeByte type;   ///< The type of order
 

	
 
@@ -49,6 +45,12 @@ public:
 
	~Order() { this->type = OT_NOTHING; }
 

	
 
	/**
 
	 * Create an order based on a packed representation of that order.
 
	 * @param packed the packed representation.
 
	 */
 
	Order(uint32 packed);
 

	
 
	/**
 
	 * Check if a Order really exists.
 
	 * @return true if the order is valid.
 
	 */
 
@@ -158,6 +160,14 @@ public:
 
	 * @return true if the type, flags and destination match.
 
	 */
 
	bool Equals(const Order &other) const;
 

	
 
	/**
 
	 * Pack this order into a 32 bits integer, or actually only
 
	 * the type, flags and destination.
 
	 * @return the packed representation.
 
	 * @note unpacking is done in the constructor.
 
	 */
 
	uint32 Pack() const;
 
};
 

	
 
static inline VehicleOrderID GetMaxOrderIndex()
 
@@ -182,8 +192,6 @@ static inline VehicleOrderID GetNumOrder
 
#define FOR_VEHICLE_ORDERS(v, order) for (order = v->orders; order != NULL; order = order->next)
 

	
 
/* (Un)pack routines */
 
uint32 PackOrder(const Order *order);
 
Order UnpackOrder(uint32 packed);
 
Order UnpackOldOrder(uint16 packed);
 

	
 
#endif /* ORDER_H */
src/order_cmd.cpp
Show inline comments
 
@@ -119,24 +119,32 @@ static bool HasOrderPoolFree(uint amount
 
	return false;
 
}
 

	
 
uint32 PackOrder(const Order *order)
 
uint32 Order::Pack() const
 
{
 
	return order->dest << 16 | order->flags << 8 | order->type;
 
	return this->dest << 16 | this->flags << 8 | this->type;
 
}
 

	
 
Order UnpackOrder(uint32 packed)
 
Order::Order(uint32 packed)
 
{
 
	Order order;
 
	order.type    = (OrderType)GB(packed,  0,  8);
 
	order.flags   = GB(packed,  8,  8);
 
	order.dest    = GB(packed, 16, 16);
 
	order.next    = NULL;
 
	order.index   = 0; // avoid compiler warning
 
	order.refit_cargo   = CT_NO_REFIT;
 
	order.refit_subtype = 0;
 
	order.wait_time     = 0;
 
	order.travel_time   = 0;
 
	return order;
 
	this->type    = (OrderType)GB(packed,  0,  8);
 
	this->flags   = GB(packed,  8,  8);
 
	this->dest    = GB(packed, 16, 16);
 
	this->next    = NULL;
 
	this->index   = 0; // avoid compiler warning
 
	this->refit_cargo   = CT_NO_REFIT;
 
	this->refit_subtype = 0;
 
	this->wait_time     = 0;
 
	this->travel_time   = 0;
 
}
 

	
 
/**
 
 *
 
 * Unpacks a order from savegames with version 4 and lower
 
 *
 
 */
 
static Order UnpackVersion4Order(uint16 packed)
 
{
 
	return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
 
}
 

	
 
/**
 
@@ -146,23 +154,14 @@ Order UnpackOrder(uint32 packed)
 
 */
 
Order UnpackOldOrder(uint16 packed)
 
{
 
	Order order;
 
	order.type    = (OrderType)GB(packed, 0, 4);
 
	order.flags   = GB(packed, 4, 4);
 
	order.dest    = GB(packed, 8, 8);
 
	order.next    = NULL;
 
	Order order = UnpackVersion4Order(packed);
 

	
 
	order.refit_cargo   = CT_NO_REFIT;
 
	order.refit_subtype = 0;
 
	order.wait_time     = 0;
 
	order.travel_time   = 0;
 
	order.index = 0; // avoid compiler warning
 

	
 
	// Sanity check
 
	// TTD stores invalid orders as OT_NOTHING with non-zero flags/station
 
	/*
 
	 * Sanity check
 
	 * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
 
	 */
 
	if (!order.IsValid() && (order.flags != 0 || order.dest != 0)) {
 
		order.type = OT_DUMMY;
 
		order.flags = 0;
 
		order.MakeDummy();
 
	}
 

	
 
	return order;
 
@@ -170,26 +169,6 @@ Order UnpackOldOrder(uint16 packed)
 

	
 
/**
 
 *
 
 * Unpacks a order from savegames with version 4 and lower
 
 *
 
 */
 
Order UnpackVersion4Order(uint16 packed)
 
{
 
	Order order;
 
	order.type  = (OrderType)GB(packed, 0, 4);
 
	order.flags = GB(packed, 4, 4);
 
	order.dest  = GB(packed, 8, 8);
 
	order.next  = NULL;
 
	order.index = 0; // avoid compiler warning
 
	order.refit_cargo   = CT_NO_REFIT;
 
	order.refit_subtype = 0;
 
	order.wait_time     = 0;
 
	order.travel_time   = 0;
 
	return order;
 
}
 

	
 
/**
 
 *
 
 * Updates the widgets of a vehicle which contains the order-data
 
 *
 
 */
 
@@ -276,7 +255,7 @@ CommandCost CmdInsertOrder(TileIndex til
 
	Vehicle *v;
 
	VehicleID veh   = GB(p1,  0, 16);
 
	VehicleOrderID sel_ord = GB(p1, 16, 16);
 
	Order new_order = UnpackOrder(p2);
 
	Order new_order(p2);
 

	
 
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
 

	
 
@@ -1099,7 +1078,7 @@ void RestoreVehicleOrders(const Vehicle 
 
		 *  in network the commands are queued before send, the second insert always
 
		 *  fails in test mode. By bypassing the test-mode, that no longer is a problem. */
 
		for (uint i = 0; bak->order[i].IsValid(); i++) {
 
			if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL,
 
			if (!DoCommandP(0, v->index + (i << 16), bak->order[i].Pack(), NULL,
 
					CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) {
 
				break;
 
			}
 
@@ -1589,8 +1568,7 @@ static void Load_ORDR()
 
			SlArray(orders, len, SLE_UINT32);
 

	
 
			for (i = 0; i < len; ++i) {
 
				Order *order = new (i) Order();
 
				order->AssignOrder(UnpackOrder(orders[i]));
 
				new (i) Order(orders[i]);
 
			}
 

	
 
			free(orders);
src/order_gui.cpp
Show inline comments
 
@@ -373,7 +373,7 @@ static void OrdersPlaceObj(const Vehicle
 
	cmd = GetOrderCmdFromTile(v, tile);
 
	if (!cmd.IsValid()) return;
 

	
 
	if (DoCommandP(v->tile, v->index + (OrderGetSel(w) << 16), PackOrder(&cmd), NULL, CMD_INSERT_ORDER | CMD_MSG(STR_8833_CAN_T_INSERT_NEW_ORDER))) {
 
	if (DoCommandP(v->tile, v->index + (OrderGetSel(w) << 16), cmd.Pack(), NULL, CMD_INSERT_ORDER | CMD_MSG(STR_8833_CAN_T_INSERT_NEW_ORDER))) {
 
		if (WP(w, order_d).sel != -1) WP(w,order_d).sel++;
 
		ResetObjectToPlace();
 
	}
0 comments (0 inline, 0 general)