(svn r13662) -Fix [FS#2113]: crash when adding conditional orders to ships.
authorrubidium
Mon, 30 Jun 2008 14:49:50 +0000
changeset 11104 947ac0a0f3a2
parent 11103 22fda2a0c52f
child 11105 bbcfdef0958e
(svn r13662) -Fix [FS#2113]: crash when adding conditional orders to ships.
src/order_cmd.cpp
--- a/src/order_cmd.cpp	Mon Jun 30 14:48:44 2008 +0000
+++ b/src/order_cmd.cpp	Mon Jun 30 14:49:50 2008 +0000
@@ -298,6 +298,18 @@
 	}
 }
 
+static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
+{
+	if (cur->IsType(OT_CONDITIONAL)) {
+		if (conditional_depth > v->num_orders) return 0;
+
+		conditional_depth++;
+		return max(GetOrderDistance(prev, &v->orders[cur->GetConditionSkipToOrder()], v, conditional_depth),
+				GetOrderDistance(prev, (prev + 1 == &v->orders[v->num_orders]) ? v->orders : (prev + 1), v, conditional_depth));
+	}
+
+	return DistanceManhattan(GetOrderLocation(*prev), GetOrderLocation(*cur));
+}
 
 /** Add an order to the orderlist of a vehicle.
  * @param tile unused
@@ -468,10 +480,7 @@
 			if (++n == sel_ord && prev != NULL) break;
 		}
 		if (prev != NULL) {
-			uint dist = DistanceManhattan(
-				GetOrderLocation(*prev),
-				GetOrderLocation(new_order)
-			);
+			uint dist = GetOrderDistance(prev, &new_order, v);
 			if (dist >= 130) {
 				return_cmd_error(STR_0210_TOO_FAR_FROM_PREVIOUS_DESTINATIO);
 			}