changeset 18288:969ade3ed490 draft

(svn r23124) -Change: [NewGRF] Interpret the result of the refit cost callback as a signed value.
author michi_cc <michi_cc@openttd.org>
date Sun, 06 Nov 2011 15:33:23 +0000
parents 820c342996dc
children a086b6cc10b0
files src/vehicle_cmd.cpp
diffstat 1 files changed, 10 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -208,7 +208,7 @@
 }
 
 /** Helper to run the refit cost callback. */
-static uint GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
+static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
 {
 	/* Prepare callback param with info about the new cargo type. */
 	const Engine *e = Engine::Get(engine_type);
@@ -218,7 +218,9 @@
 	uint16 cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v);
 	if (cb_res != CALLBACK_FAILED) {
 		*auto_refit_allowed = HasBit(cb_res, 14);
-		return GB(cb_res, 0, 14);
+		int factor = GB(cb_res, 0, 14);
+		if (factor >= 0x2000) factor -= 0x4000; // Treat as signed integer.
+		return factor;
 	}
 
 	*auto_refit_allowed = e->info.refit_cost == 0;
@@ -239,7 +241,7 @@
 	ExpensesType expense_type;
 	const Engine *e = Engine::Get(engine_type);
 	Price base_price;
-	uint cost_factor = GetRefitCostFactor(v, engine_type, new_cid, new_subtype, auto_refit_allowed);
+	int cost_factor = GetRefitCostFactor(v, engine_type, new_cid, new_subtype, auto_refit_allowed);
 	switch (e->type) {
 		case VEH_SHIP:
 			base_price = PR_BUILD_VEHICLE_SHIP;
@@ -264,7 +266,11 @@
 
 		default: NOT_REACHED();
 	}
-	return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
+	if (cost_factor < 0) {
+		return CommandCost(expense_type, -GetPrice(base_price, -cost_factor, e->GetGRF(), -10));
+	} else {
+		return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
+	}
 }
 
 /**