(svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
authorbjarni
Mon, 04 Jun 2007 20:46:01 +0000
changeset 7296 341c1174517d
parent 7295 4af0cd4cab3d
child 7297 1df77fb920bd
(svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
src/autoreplace_cmd.cpp
--- a/src/autoreplace_cmd.cpp	Mon Jun 04 20:42:49 2007 +0000
+++ b/src/autoreplace_cmd.cpp	Mon Jun 04 20:46:01 2007 +0000
@@ -246,9 +246,19 @@
 			GetName(vehicle_name, old_v->string_id & 0x7FF, lastof(vehicle_name));
 		}
 	} else { // flags & DC_EXEC not set
+		int32 tmp_move = 0;
+		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v) && old_v->next != NULL) {
+			/* Verify that the wagons can be placed on the engine in question.
+			 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
+			DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v));
+			Vehicle *temp = GetVehicle(_new_vehicle_id);
+			tmp_move = DoCommand(0, (temp->index << 16) | old_v->next->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
+			DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
+		}
+
 		/* Ensure that the player will not end up having negative money while autoreplacing
 		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
-		if (p->money64 < (cost + total_cost)) {
+		if (CmdFailed(tmp_move) || p->money64 < (cost + total_cost)) {
 			SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 			SubtractMoneyFromPlayer(-sell_value); // Pay back the loan
 			return CMD_ERROR;
@@ -344,6 +354,8 @@
 			/* Now replace the vehicle */
 			temp_cost = ReplaceVehicle(&w, flags, cost);
 
+			if (CmdFailed(temp_cost)) break; // replace failed for some reason. Leave the vehicle alone
+
 			if (flags & DC_EXEC &&
 					(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
@@ -352,10 +364,7 @@
 				 */
 				v = w;
 			}
-
-			if (!CmdFailed(temp_cost)) {
-				cost += temp_cost;
-			}
+			cost += temp_cost;
 		} while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
 
 		if (!(flags & DC_EXEC) && (p->money64 < (int32)(cost + p->engine_renew_money) || cost == 0)) {