diff src/order_cmd.cpp @ 6263:715aa17970ab draft

(svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
author bjarni <bjarni@openttd.org>
date Thu, 08 Mar 2007 21:39:34 +0000
parents 4a39d6291d58
children 34db8eef99ef
line wrap: on
line diff
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -49,7 +49,7 @@
 
 	// Sanity check
 	// TTD stores invalid orders as OT_NOTHING with non-zero flags/station
-	if (order.type == OT_NOTHING && (order.flags != 0 || order.dest != 0)) {
+	if (!order.IsValid() && (order.flags != 0 || order.dest != 0)) {
 		order.type = OT_DUMMY;
 		order.flags = 0;
 	}
@@ -116,7 +116,7 @@
 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 	 * TODO - This is just a temporary stage, this will be removed. */
 	for (order = GetOrder(0); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) {
-		if (!IsValidOrder(order)) {
+		if (!order->IsValid()) {
 			OrderID index = order->index;
 
 			memset(order, 0, sizeof(*order));
@@ -496,8 +496,7 @@
 		}
 
 		/* Give the item free */
-		order->type = OT_NOTHING;
-		order->next = NULL;
+		order->Free();
 
 		u = GetFirstVehicleFromSharedList(v);
 		DeleteOrderWarnings(u);
@@ -871,9 +870,8 @@
 			*dest = *order;
 			dest++;
 		}
-		/* End the list with an OT_NOTHING */
-		dest->type = OT_NOTHING;
-		dest->next = NULL;
+		/* End the list with an empty order */
+		dest->Free();
 	}
 }
 
@@ -902,7 +900,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].type != OT_NOTHING; i++) {
+	for (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;
 	}
@@ -1112,8 +1110,6 @@
  */
 void DeleteVehicleOrders(Vehicle *v)
 {
-	Order *cur, *next;
-
 	DeleteOrderWarnings(v);
 
 	/* If we have a shared order-list, don't delete the list, but just
@@ -1146,7 +1142,7 @@
 	}
 
 	/* Remove the orders */
-	cur = v->orders;
+	Order *cur = v->orders;
 	v->orders = NULL;
 	v->num_orders = 0;
 
@@ -1162,12 +1158,8 @@
 			case VEH_AIRCRAFT: window_class = WC_AIRCRAFT_LIST; break;
 		}
 		DeleteWindowById(window_class, (cur->index << 16) | (v->type << 11) | VLW_SHARED_ORDERS | v->owner);
-	}
 
-	while (cur != NULL) {
-		next = cur->next;
-		DeleteOrder(cur);
-		cur = next;
+		cur->FreeChain(); // Free the orders.
 	}
 }
 
@@ -1274,9 +1266,9 @@
 		/* Update all the next pointer */
 		for (i = 1; i < len; ++i) {
 			/* The orders were built like this:
-			 *   Vehicle one had order[0], and as long as order++.type was not
-			 *   OT_NOTHING, it was part of the order-list of that vehicle */
-			if (GetOrder(i)->type != OT_NOTHING)
+			 *   While the order is valid, set the previous will get it's next pointer set
+			 *   We start with index 1 because no order will have the first in it's next pointer */
+			if (GetOrder(i)->IsValid())
 				GetOrder(i - 1)->next = GetOrder(i);
 		}
 	} else {