changeset 4728:2edba75a9433 draft

(svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one Say we got 40k for selling the old one and the new one costs 60k, then the player only needs 20k to replace The new engine is still built before selling the old one for various reasons, but now the player gets a loan of the sell value, which is always repaid when replace fails or the old engine is sold. The player will never notice this loan.
author bjarni <bjarni@openttd.org>
date Wed, 04 Oct 2006 19:15:25 +0000
parents 82a9a7ffdd26
children aee45912bb5f
files vehicle.c
diffstat 1 files changed, 24 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/vehicle.c
+++ b/vehicle.c
@@ -1955,6 +1955,7 @@
 static int32 ReplaceVehicle(Vehicle **w, byte flags, int32 total_cost)
 {
 	int32 cost;
+	int32 sell_value;
 	Vehicle *old_v = *w;
 	const Player *p = GetPlayer(old_v->owner);
 	EngineID new_engine_type;
@@ -1972,8 +1973,20 @@
 	/* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */
 	if (replacement_cargo_type == CT_INVALID) return 0;
 
+	sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, CMD_SELL_VEH(old_v->type));
+
+	/* We give the player a loan of the same amount as the sell value.
+	 * This is needed in case he needs the income from the sale to build the new vehicle.
+	 * We take it back if building fails or when we really sell the old engine */
+	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
+	SubtractMoneyFromPlayer(sell_value);
+
 	cost = DoCommand(old_v->tile, new_engine_type, 3, flags, CMD_BUILD_VEH(old_v->type));
-	if (CmdFailed(cost)) return cost;
+	if (CmdFailed(cost)) {
+		SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
+		SubtractMoneyFromPlayer(-sell_value); // Take back the money we just gave the player
+		return cost;
+	}
 
 	if (replacement_cargo_type != CT_NO_REFIT) cost += GetRefitCost(new_engine_type); // add refit cost
 
@@ -2040,9 +2053,18 @@
 	} else { // flags & DC_EXEC not set
 		/* 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)) return CMD_ERROR;
+		if (p->money64 < (cost + total_cost)) {
+			SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
+			SubtractMoneyFromPlayer(-sell_value); // Pay back the loan
+			return CMD_ERROR;
+		}
 	}
 
+	/* Take back the money we just gave the player just before building the vehicle
+	 * The player will get the same amount now that the sale actually takes place */
+	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
+	SubtractMoneyFromPlayer(-sell_value);
+
 	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
 	cost += DoCommand(0, old_v->index, 0, flags, CMD_SELL_VEH(old_v->type));