(svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
authorbjarni
Thu, 08 Mar 2007 21:39:34 +0000
changeset 6263 6bd0726c74e2
parent 6262 312d9c28da04
child 6264 c2138d65160b
(svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
src/aircraft_cmd.cpp
src/disaster_cmd.cpp
src/oldloader.cpp
src/order.h
src/order_cmd.cpp
src/order_gui.cpp
src/roadveh_cmd.cpp
src/ship_cmd.cpp
src/train_cmd.cpp
--- a/src/aircraft_cmd.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/aircraft_cmd.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -1273,8 +1273,7 @@
 
 			if (CmdFailed(ret)) CrashAirplane(v);
 		} else if (v->current_order.type != OT_GOTO_DEPOT) {
-			v->current_order.type = OT_NOTHING;
-			v->current_order.flags = 0;
+			v->current_order.Free();
 		}
 		return;
 	}
@@ -1326,8 +1325,7 @@
 			}
 
 			Order b = v->current_order;
-			v->current_order.type = OT_NOTHING;
-			v->current_order.flags = 0;
+			v->current_order.Free();
 			MarkAircraftDirty(v);
 			if (!(b.flags & OF_NON_STOP)) return;
 			break;
@@ -1552,8 +1550,7 @@
 
 	/* if we were sent to the depot, stay there */
 	if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) {
-		v->current_order.type = OT_NOTHING;
-		v->current_order.flags = 0;
+		v->current_order.Free();
 		return;
 	}
 
@@ -1601,7 +1598,7 @@
 		return;
 	}
 
-	if (v->current_order.type == OT_NOTHING) return;
+	if (!v->current_order.IsValid()) return;
 
 	/* if the block of the next position is busy, stay put */
 	if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
@@ -1622,8 +1619,7 @@
 			}
 			break;
 		default:  // orders have been deleted (no orders), goto depot and don't bother us
-			v->current_order.type = OT_NOTHING;
-			v->current_order.flags = 0;
+			v->current_order.Free();
 			v->u.air.state = HANGAR;
 	}
 	AirportMove(v, apc);
--- a/src/disaster_cmd.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/disaster_cmd.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -135,9 +135,7 @@
 	v->owner = OWNER_NONE;
 	v->vehstatus = VS_UNCLICKABLE;
 	v->u.disaster.image_override = 0;
-	v->current_order.type = OT_NOTHING;
-	v->current_order.flags = 0;
-	v->current_order.dest = 0;
+	v->current_order.Free();
 
 	DisasterVehicleUpdateImage(v);
 	VehiclePositionChanged(v);
--- a/src/oldloader.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/oldloader.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -486,9 +486,9 @@
 	AssignOrder(GetOrder(num), UnpackOldOrder(_old_order));
 
 	/* Relink the orders to eachother (in TTD(Patch) the orders for one
-	vehicle are behind eachother, with OT_NOTHING as indication that
+	vehicle are behind eachother, with an invalid order (OT_NOTHING) as indication that
 	it is the last order */
-	if (num > 0 && GetOrder(num)->type != OT_NOTHING)
+	if (num > 0 && GetOrder(num)->IsValid())
 		GetOrder(num - 1)->next = GetOrder(num);
 
 	return true;
--- a/src/order.h	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/order.h	Thu Mar 08 21:39:34 2007 +0000
@@ -99,6 +99,10 @@
 
 	CargoID refit_cargo; // Refit CargoID
 	byte refit_subtype; // Refit subtype
+
+	bool IsValid() const;
+	void Free();
+	void FreeChain();
 };
 
 #define MAX_BACKUP_ORDER_COUNT 40
@@ -134,18 +138,26 @@
 /**
  * Check if a Order really exists.
  */
-static inline bool IsValidOrder(const Order *o)
+inline bool Order::IsValid() const
 {
-	return o->type != OT_NOTHING;
+	return type != OT_NOTHING;
 }
 
-static inline void DeleteOrder(Order *o)
+inline void Order::Free()
 {
-	o->type = OT_NOTHING;
-	o->next = NULL;
+	type  = OT_NOTHING;
+	flags = 0;
+	dest  = 0;
+	next  = NULL;
 }
 
-#define FOR_ALL_ORDERS_FROM(order, start) for (order = GetOrder(start); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) if (IsValidOrder(order))
+inline void Order::FreeChain()
+{
+	if (next != NULL) next->FreeChain();
+	Free();
+}
+
+#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())
 #define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
 
 
@@ -160,7 +172,7 @@
 		return true;
 
 	FOR_ALL_ORDERS(order)
-		if (order->type == OT_NOTHING)
+		if (!order->IsValid())
 			if (--amount == 0)
 				return true;
 
--- a/src/order_cmd.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/order_cmd.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -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 {
--- a/src/order_gui.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/order_gui.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -309,8 +309,7 @@
 	}
 
 	// not found
-	order.type = OT_NOTHING;
-	order.flags = 0;
+	order.Free();
 	order.dest = INVALID_STATION;
 	return order;
 }
@@ -347,7 +346,7 @@
 	if (u != NULL && HandleOrderVehClick(v, u, w)) return;
 
 	cmd = GetOrderCmdFromTile(v, tile);
-	if (cmd.type == OT_NOTHING) return;
+	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 (WP(w,order_d).sel != -1) WP(w,order_d).sel++;
--- a/src/roadveh_cmd.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/roadveh_cmd.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -682,8 +682,7 @@
 	order = GetVehicleOrder(v, v->cur_order_index);
 
 	if (order == NULL) {
-		v->current_order.type = OT_NOTHING;
-		v->current_order.flags = 0;
+		v->current_order.Free();
 		v->dest_tile = 0;
 		ClearSlot(v);
 		return;
@@ -1618,8 +1617,7 @@
 				v->cur_speed = 0;
 				return;
 			}
-			v->current_order.type = OT_NOTHING;
-			v->current_order.flags = 0;
+			v->current_order.Free();
 			ClearSlot(v);
 		}
 
--- a/src/ship_cmd.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/ship_cmd.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -264,8 +264,7 @@
 	order = GetVehicleOrder(v, v->cur_order_index);
 
 	if (order == NULL) {
-		v->current_order.type  = OT_NOTHING;
-		v->current_order.flags = 0;
+		v->current_order.Free();
 		v->dest_tile = 0;
 		return;
 	}
@@ -705,8 +704,7 @@
 			/* A leave station order only needs one tick to get processed, so we can
 			 * always skip ahead. */
 			if (v->current_order.type == OT_LEAVESTATION) {
-				v->current_order.type = OT_NOTHING;
-				v->current_order.flags = 0;
+				v->current_order.Free();
 				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
 			} else if (v->dest_tile != 0) {
 				/* We have a target, let's see if we reached it... */
--- a/src/train_cmd.cpp	Thu Mar 08 21:05:05 2007 +0000
+++ b/src/train_cmd.cpp	Thu Mar 08 21:39:34 2007 +0000
@@ -2454,8 +2454,7 @@
 
 	// If no order, do nothing.
 	if (order == NULL) {
-		v->current_order.type = OT_NOTHING;
-		v->current_order.flags = 0;
+		v->current_order.Free();
 		v->dest_tile = 0;
 		return false;
 	}
@@ -2910,8 +2909,7 @@
 					}
 
 					if (v->current_order.type == OT_LEAVESTATION) {
-						v->current_order.type = OT_NOTHING;
-						v->current_order.flags = 0;
+						v->current_order.Free();
 						InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
 					}
 				}