(svn r11184) -Codechange: cleanup the code related to backup orders.
authorrubidium
Fri, 28 Sep 2007 21:15:45 +0000
changeset 8149 eb78fb69096f
parent 8148 e836b6e36f6a
child 8150 3b6a36860a47
(svn r11184) -Codechange: cleanup the code related to backup orders.
src/ai/default/default.cpp
src/aircraft_gui.cpp
src/depot_gui.cpp
src/order.h
src/order_cmd.cpp
src/roadveh_gui.cpp
src/ship_gui.cpp
src/train_gui.cpp
--- a/src/ai/default/default.cpp	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/ai/default/default.cpp	Fri Sep 28 21:15:45 2007 +0000
@@ -305,7 +305,7 @@
 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 @@
 	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);
--- a/src/aircraft_gui.cpp	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/aircraft_gui.cpp	Fri Sep 28 21:15:45 2007 +0000
@@ -95,7 +95,7 @@
 
 		if (v->tile == _backup_orders_tile) {
 			_backup_orders_tile = 0;
-			RestoreVehicleOrders(v, _backup_orders_data);
+			RestoreVehicleOrders(v);
 		}
 		ShowVehicleViewWindow(v);
 	}
--- a/src/depot_gui.cpp	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/depot_gui.cpp	Fri Sep 28 21:15:45 2007 +0000
@@ -928,7 +928,7 @@
 
 						if (is_engine) {
 							_backup_orders_tile = v->tile;
-							BackupVehicleOrders(v, _backup_orders_data);
+							BackupVehicleOrders(v);
 						}
 
 						switch (v->type) {
--- a/src/order.h	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/order.h	Fri Sep 28 21:15:45 2007 +0000
@@ -116,18 +116,18 @@
 	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 @@
 }
 
 /* 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);
--- a/src/order_cmd.cpp	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/order_cmd.cpp	Fri Sep 28 21:15:45 2007 +0000
@@ -317,10 +317,6 @@
 
 	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 @@
  */
 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 @@
 		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 @@
  * 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 @@
 	 *  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;
 	}
--- a/src/roadveh_gui.cpp	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/roadveh_gui.cpp	Fri Sep 28 21:15:45 2007 +0000
@@ -128,7 +128,7 @@
 	v = GetVehicle(_new_vehicle_id);
 	if (v->tile == _backup_orders_tile) {
 		_backup_orders_tile = 0;
-		RestoreVehicleOrders(v, _backup_orders_data);
+		RestoreVehicleOrders(v);
 	}
 	ShowVehicleViewWindow(v);
 }
--- a/src/ship_gui.cpp	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/ship_gui.cpp	Fri Sep 28 21:15:45 2007 +0000
@@ -35,7 +35,7 @@
 	v = GetVehicle(_new_vehicle_id);
 	if (v->tile == _backup_orders_tile) {
 		_backup_orders_tile = 0;
-		RestoreVehicleOrders(v, _backup_orders_data);
+		RestoreVehicleOrders(v);
 	}
 	ShowVehicleViewWindow(v);
 }
--- a/src/train_gui.cpp	Fri Sep 28 19:24:52 2007 +0000
+++ b/src/train_gui.cpp	Fri Sep 28 21:15:45 2007 +0000
@@ -54,7 +54,7 @@
 	v = GetVehicle(_new_vehicle_id);
 	if (tile == _backup_orders_tile) {
 		_backup_orders_tile = 0;
-		RestoreVehicleOrders(v, _backup_orders_data);
+		RestoreVehicleOrders(v);
 	}
 	ShowVehicleViewWindow(v);
 }