Changeset - r7653:8b8bc2df3a7d
[Not reviewed]
master
0 8 0
rubidium - 17 years ago 2007-09-28 21:15:45
rubidium@openttd.org
(svn r11184) -Codechange: cleanup the code related to backup orders.
8 files changed with 32 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/ai/default/default.cpp
Show inline comments
 
@@ -305,7 +305,7 @@ static void AiRestoreVehicleOrders(Vehic
 
static void AiHandleReplaceTrain(Player *p)
 
{
 
	const Vehicle* v = p->ai.cur_veh;
 
	BackuppedOrders orderbak[1];
 
	BackuppedOrders orderbak;
 
	EngineID veh;
 

	
 
	// wait until the vehicle reaches the depot.
 
@@ -318,13 +318,13 @@ static void AiHandleReplaceTrain(Player 
 
	if (veh != INVALID_ENGINE) {
 
		TileIndex tile;
 

	
 
		BackupVehicleOrders(v, orderbak);
 
		BackupVehicleOrders(v, &orderbak);
 
		tile = v->tile;
 

	
 
		if (CmdSucceeded(DoCommand(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
 
				CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE))) {
 
			VehicleID veh = _new_vehicle_id;
 
			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
 
			AiRestoreVehicleOrders(GetVehicle(veh), &orderbak);
 
			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
 

	
 
			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
src/aircraft_gui.cpp
Show inline comments
 
@@ -95,7 +95,7 @@ void CcBuildAircraft(bool success, TileI
 

	
 
		if (v->tile == _backup_orders_tile) {
 
			_backup_orders_tile = 0;
 
			RestoreVehicleOrders(v, _backup_orders_data);
 
			RestoreVehicleOrders(v);
 
		}
 
		ShowVehicleViewWindow(v);
 
	}
src/depot_gui.cpp
Show inline comments
 
@@ -928,7 +928,7 @@ static void DepotWndProc(Window *w, Wind
 

	
 
						if (is_engine) {
 
							_backup_orders_tile = v->tile;
 
							BackupVehicleOrders(v, _backup_orders_data);
 
							BackupVehicleOrders(v);
 
						}
 

	
 
						switch (v->type) {
src/order.h
Show inline comments
 
@@ -116,18 +116,18 @@ struct Order : PoolItem<Order, OrderID, 
 
	void FreeChain();
 
};
 

	
 
#define MAX_BACKUP_ORDER_COUNT 40
 
struct BackuppedOrders {
 
	BackuppedOrders() : order(NULL) { }
 

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

	
 
VARDEF TileIndex _backup_orders_tile;
 
VARDEF BackuppedOrders _backup_orders_data[1];
 
VARDEF BackuppedOrders _backup_orders_data;
 

	
 
static inline VehicleOrderID GetMaxOrderIndex()
 
{
 
@@ -200,8 +200,8 @@ static inline Order UnpackOrder(uint32 p
 
}
 

	
 
/* Functions */
 
void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *order);
 
void RestoreVehicleOrders(const Vehicle* v, const BackuppedOrders* order);
 
void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *order = &_backup_orders_data);
 
void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *order = &_backup_orders_data);
 
void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination);
 
void InvalidateVehicleOrder(const Vehicle *v);
 
bool VehicleHasDepotOrders(const Vehicle *v);
src/order_cmd.cpp
Show inline comments
 
@@ -317,10 +317,6 @@ CommandCost CmdInsertOrder(TileIndex til
 

	
 
	if (!HasOrderPoolFree(1)) return_cmd_error(STR_8831_NO_MORE_SPACE_FOR_ORDERS);
 

	
 
	/* XXX - This limit is only here because the backuppedorders can't
 
	 * handle any more then this.. */
 
	if (v->num_orders >= MAX_BACKUP_ORDER_COUNT) return_cmd_error(STR_8832_TOO_MANY_ORDERS);
 

	
 
	if (v->type == VEH_SHIP &&
 
			IsHumanPlayer(v->owner) &&
 
			!_patches.new_pathfinding_all) {
 
@@ -920,6 +916,10 @@ CommandCost CmdOrderRefit(TileIndex tile
 
 */
 
void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *bak)
 
{
 
	/* Make sure we always have freed the stuff */
 
	free(bak->order);
 
	bak->order = NULL;
 

	
 
	/* Save general info */
 
	bak->orderindex       = v->cur_order_index;
 
	bak->service_interval = v->service_interval;
 
@@ -939,13 +939,21 @@ void BackupVehicleOrders(const Vehicle *
 
		bak->clone = u->index;
 
	} else {
 
		/* Else copy the orders */
 
		Order *order, *dest;
 

	
 
		dest = bak->order;
 

	
 
		/* We do not have shared orders */
 
		bak->clone = INVALID_VEHICLE;
 

	
 

	
 
		/* Count the number of orders */
 
		uint cnt = 0;
 
		const Order *order;
 
		FOR_VEHICLE_ORDERS(v, order) cnt++;
 

	
 
		/* Allocate memory for the orders plus an end-of-orders marker */
 
		bak->order = MallocT<Order>(cnt + 1);
 

	
 
		Order *dest = bak->order;
 

	
 
		/* Copy the orders */
 
		FOR_VEHICLE_ORDERS(v, order) {
 
			*dest = *order;
 
@@ -961,12 +969,10 @@ void BackupVehicleOrders(const Vehicle *
 
 * Restore vehicle orders that are backupped via BackupVehicleOrders
 
 *
 
 */
 
void RestoreVehicleOrders(const Vehicle* v, const BackuppedOrders* bak)
 
void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *bak)
 
{
 
	uint i;
 

	
 
	/* If we have a custom name, process that */
 
	if (bak->name[0] != 0) {
 
	if (!StrEmpty(bak->name)) {
 
		_cmd_text = bak->name;
 
		DoCommandP(0, v->index, 0, NULL, CMD_NAME_VEHICLE);
 
	}
 
@@ -981,7 +987,7 @@ void RestoreVehicleOrders(const Vehicle*
 
	 *  order number is one more than the current amount of orders, and because
 
	 *  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 (i = 0; bak->order[i].IsValid(); i++) {
 
	for (uint i = 0; bak->order[i].IsValid(); i++) {
 
		if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
 
			break;
 
	}
src/roadveh_gui.cpp
Show inline comments
 
@@ -128,7 +128,7 @@ void CcBuildRoadVeh(bool success, TileIn
 
	v = GetVehicle(_new_vehicle_id);
 
	if (v->tile == _backup_orders_tile) {
 
		_backup_orders_tile = 0;
 
		RestoreVehicleOrders(v, _backup_orders_data);
 
		RestoreVehicleOrders(v);
 
	}
 
	ShowVehicleViewWindow(v);
 
}
src/ship_gui.cpp
Show inline comments
 
@@ -35,7 +35,7 @@ void CcBuildShip(bool success, TileIndex
 
	v = GetVehicle(_new_vehicle_id);
 
	if (v->tile == _backup_orders_tile) {
 
		_backup_orders_tile = 0;
 
		RestoreVehicleOrders(v, _backup_orders_data);
 
		RestoreVehicleOrders(v);
 
	}
 
	ShowVehicleViewWindow(v);
 
}
src/train_gui.cpp
Show inline comments
 
@@ -54,7 +54,7 @@ void CcBuildLoco(bool success, TileIndex
 
	v = GetVehicle(_new_vehicle_id);
 
	if (tile == _backup_orders_tile) {
 
		_backup_orders_tile = 0;
 
		RestoreVehicleOrders(v, _backup_orders_data);
 
		RestoreVehicleOrders(v);
 
	}
 
	ShowVehicleViewWindow(v);
 
}
0 comments (0 inline, 0 general)