(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
authorbjarni
Wed, 07 May 2008 22:08:20 +0000
changeset 10459 bb8f386d5b18
parent 10458 c0a45f0594cf
child 10460 a834c2e45ae1
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
src/autoreplace_cmd.cpp
--- a/src/autoreplace_cmd.cpp	Wed May 07 21:51:13 2008 +0000
+++ b/src/autoreplace_cmd.cpp	Wed May 07 22:08:20 2008 +0000
@@ -244,11 +244,13 @@
 				}
 			}
 		}
-		/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
-		MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
+		if (CmdSucceeded(cost)) {
+			/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
+			MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
 
-		// Get the name of the old vehicle if it has a custom name.
-		if (old_v->name != NULL) vehicle_name = strdup(old_v->name);
+			/* Get the name of the old vehicle if it has a custom name. */
+			if (old_v->name != NULL) vehicle_name = strdup(old_v->name);
+		}
 	} else { // flags & DC_EXEC not set
 		CommandCost tmp_move;
 
@@ -282,6 +284,8 @@
 	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
 	cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
 
+	if (CmdFailed(cost)) return cost;
+
 	if (new_front) {
 		/* now we assign the old unitnumber to the new vehicle */
 		new_v->unitnumber = cached_unitnumber;
@@ -426,16 +430,16 @@
 			/* Now replace the vehicle */
 			cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine));
 
-			if (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE) {
+			if (CmdSucceeded(cost) && (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) {
 				/* now we bought a new engine and sold the old one. We need to fix the
 				 * pointers in order to avoid pointing to the old one for trains: these
 				 * pointers should point to the front engine and not the cars
 				 */
 				v = w;
 			}
-		} while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
+		} while (CmdSucceeded(cost) && w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
 
-		if (v->type == VEH_TRAIN && p->renew_keep_length) {
+		if (CmdSucceeded(cost) && v->type == VEH_TRAIN && p->renew_keep_length) {
 			/* Remove wagons until the wanted length is reached */
 			cost.AddCost(WagonRemoval(v, old_total_length));
 		}