changeset 18250:f6c3afbfcc60 draft

(svn r23086) -Feature: [NewGRF] Callback to change refit cost depending on old and new cargo type.
author michi_cc <michi_cc@openttd.org>
date Fri, 04 Nov 2011 00:35:05 +0000
parents bc05d52fdf23
children 137b272acca5
files src/newgrf_callbacks.h src/vehicle_cmd.cpp
diffstat 2 files changed, 27 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf_callbacks.h
+++ b/src/newgrf_callbacks.h
@@ -272,6 +272,9 @@
 
 	/** Called to determine if one can alter the ground below an object tile */
 	CBID_OBJECT_AUTOSLOPE                = 0x15D, // 15 bit callback
+
+	/** Called to determine the cost factor for refitting a vehicle. */
+	CBID_VEHICLE_REFIT_COST              = 0x15E, // 15 bit callback
 };
 
 /**
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -31,6 +31,7 @@
 #include "company_base.h"
 #include "order_backup.h"
 #include "ship.h"
+#include "newgrf.h"
 
 #include "table/strings.h"
 
@@ -206,17 +207,36 @@
 	return ret;
 }
 
+/** Helper to run the refit cost callback. */
+static uint GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype)
+{
+	/* Prepare callback param with info about the new cargo type. */
+	const Engine *e = Engine::Get(engine_type);
+	const CargoSpec *cs = CargoSpec::Get(new_cid);
+	uint32 param1 = (cs->classes << 16) | (new_subtype << 8) | e->GetGRF()->cargo_map[new_cid];
+
+	uint16 cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v);
+	if (cb_res != CALLBACK_FAILED) {
+		return GB(cb_res, 0, 14);
+	}
+
+	return e->info.refit_cost;
+}
+
 /**
  * Learn the price of refitting a certain engine
+ * @param v The vehicle we are refitting, can be NULL.
  * @param engine_type Which engine to refit
+ * @param new_cid Cargo type we are refitting to.
+ * @param new_subtype New cargo subtype.
  * @return Price for refitting
  */
-static CommandCost GetRefitCost(EngineID engine_type)
+static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype)
 {
 	ExpensesType expense_type;
 	const Engine *e = Engine::Get(engine_type);
 	Price base_price;
-	uint cost_factor = e->info.refit_cost;
+	uint cost_factor = GetRefitCostFactor(v, engine_type, new_cid, new_subtype);
 	switch (e->type) {
 		case VEH_SHIP:
 			base_price = PR_BUILD_VEHICLE_SHIP;
@@ -301,7 +321,7 @@
 		v->cargo_subtype = temp_subtype;
 
 		if (new_cid != v->cargo_type) {
-			cost.AddCost(GetRefitCost(v->engine_type));
+			cost.AddCost(GetRefitCost(v, v->engine_type, new_cid, new_subtype));
 		}
 
 		if (flags & DC_EXEC) {
@@ -793,7 +813,7 @@
 				CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
 
 				if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
-					total_cost.AddCost(GetRefitCost(v->engine_type));
+					total_cost.AddCost(GetRefitCost(NULL, v->engine_type, v->cargo_type, v->cargo_subtype));
 				}
 			}