(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
authorbjarni
Wed, 07 May 2008 23:00:11 +0000
changeset 10460 a834c2e45ae1
parent 10459 bb8f386d5b18
child 10461 48556aee54da
(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
Now it's updated when it's the front vehicle and it's every time it's the front vehicle and only if it's the front vehicle (nomatter if the replacement works or not)
src/autoreplace_cmd.cpp
--- a/src/autoreplace_cmd.cpp	Wed May 07 22:08:20 2008 +0000
+++ b/src/autoreplace_cmd.cpp	Wed May 07 23:00:11 2008 +0000
@@ -427,10 +427,14 @@
 				/* We are going to try to replace a vehicle but we don't have any backup so we will make one. */
 				backup.Backup(v, p);
 			}
-			/* Now replace the vehicle */
+			/* Now replace the vehicle.
+			 * First we need to cache if it's the front vehicle as we need to update the v pointer if it is.
+			 * If the replacement fails then we can't trust the data in the vehicle hence the reason to cache the result. */
+			bool IsFront = w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE;
+
 			cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine));
 
-			if (CmdSucceeded(cost) && (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) {
+			if (IsFront) {
 				/* 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