(svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
authorrubidium
Wed, 29 Aug 2007 21:27:16 +0000
changeset 7984 c539c9368e3c
parent 7983 06d272a9618b
child 7985 c7b966f7592e
(svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
src/aircraft.h
src/aircraft_gui.cpp
src/roadveh.h
src/roadveh_gui.cpp
src/ship.h
src/ship_gui.cpp
src/train.h
src/train_cmd.cpp
src/train_gui.cpp
src/vehicle.h
--- a/src/aircraft.h	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/aircraft.h	Wed Aug 29 21:27:16 2007 +0000
@@ -7,6 +7,8 @@
 
 #include "station_map.h"
 #include "vehicle.h"
+#include "engine.h"
+#include "variables.h"
 
 /** An aircraft can be one ot those types */
 enum AircraftSubType {
@@ -130,6 +132,7 @@
 	int GetImage(Direction direction) const;
 	int GetDisplaySpeed() const { return this->cur_speed * 10 / 16; }
 	int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 16; }
+	Money GetRunningCost() const { return AircraftVehInfo(this->engine_type)->running_cost * _price.aircraft_running; }
 	void Tick();
 };
 
--- a/src/aircraft_gui.cpp	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/aircraft_gui.cpp	Wed Aug 29 21:27:16 2007 +0000
@@ -80,7 +80,7 @@
 
 			SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED);
 			SetDParam(2, v->max_age / 366);
-			SetDParam(3, _price.aircraft_running * AircraftVehInfo(v->engine_type)->running_cost >> 8);
+			SetDParam(3, v->GetDisplayRunningCost());
 			DrawString(2, 15, STR_A00D_AGE_RUNNING_COST_YR, 0);
 		}
 
--- a/src/roadveh.h	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/roadveh.h	Wed Aug 29 21:27:16 2007 +0000
@@ -6,7 +6,8 @@
 #define ROADVEH_H
 
 #include "vehicle.h"
-
+#include "engine.h"
+#include "variables.h"
 
 enum RoadVehicleSubType {
 	RVST_FRONT,
@@ -83,6 +84,7 @@
 	int GetImage(Direction direction) const;
 	int GetDisplaySpeed() const { return this->cur_speed * 10 / 32; }
 	int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 32; }
+	Money GetRunningCost() const { return RoadVehInfo(this->engine_type)->running_cost * _price.roadveh_running; }
 	void Tick();
 };
 
--- a/src/roadveh_gui.cpp	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/roadveh_gui.cpp	Wed Aug 29 21:27:16 2007 +0000
@@ -91,7 +91,7 @@
 
 			SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED);
 			SetDParam(2, v->max_age / 366);
-			SetDParam(3, RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running >> 8);
+			SetDParam(3, v->GetDisplayRunningCost());
 			DrawString(2, 15, STR_900D_AGE_RUNNING_COST_YR, 0);
 		}
 
--- a/src/ship.h	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/ship.h	Wed Aug 29 21:27:16 2007 +0000
@@ -6,6 +6,8 @@
 #define SHIP_H
 
 #include "vehicle.h"
+#include "engine.h"
+#include "variables.h"
 
 void CcBuildShip(bool success, TileIndex tile, uint32 p1, uint32 p2);
 void RecalcShipStuff(Vehicle *v);
@@ -48,6 +50,7 @@
 	int GetImage(Direction direction) const;
 	int GetDisplaySpeed() const { return this->cur_speed * 10 / 32; }
 	int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 32; }
+	Money GetRunningCost() const { return ShipVehInfo(this->engine_type)->running_cost * _price.ship_running; }
 	void Tick();
 };
 
--- a/src/ship_gui.cpp	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/ship_gui.cpp	Wed Aug 29 21:27:16 2007 +0000
@@ -50,7 +50,7 @@
 
 			SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED);
 			SetDParam(2, v->max_age / 366);
-			SetDParam(3, ShipVehInfo(v->engine_type)->running_cost * _price.ship_running >> 8);
+			SetDParam(3, v->GetDisplayRunningCost());
 			DrawString(2, 15, STR_9812_AGE_RUNNING_COST_YR, 0);
 		}
 
--- a/src/train.h	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/train.h	Wed Aug 29 21:27:16 2007 +0000
@@ -274,6 +274,7 @@
 	int GetImage(Direction direction) const;
 	int GetDisplaySpeed() const { return this->cur_speed * 10 / 16; }
 	int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed * 10 / 16; }
+	Money GetRunningCost() const;
 	void Tick();
 };
 
--- a/src/train_cmd.cpp	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/train_cmd.cpp	Wed Aug 29 21:27:16 2007 +0000
@@ -3313,6 +3313,25 @@
 }
 
 
+
+Money Train::GetRunningCost() const
+{
+	Money cost = 0;
+	const Vehicle *v = this;
+
+	do {
+		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
+
+		byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost_base);
+		if (cost_factor == 0) continue;
+
+		cost += cost_factor * _price.running_rail[rvi->running_cost_class];
+	} while ((v = GetNextVehicle(v)) != NULL);
+
+	return cost;
+}
+
+
 void Train::Tick()
 {
 	if (_age_cargo_skip_counter == 0) this->cargo.AgeCargo();
@@ -3384,22 +3403,6 @@
 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
 }
 
-Money GetTrainRunningCost(const Vehicle *v)
-{
-	Money cost = 0;
-
-	do {
-		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
-
-		byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost_base);
-		if (cost_factor == 0) continue;
-
-		cost += cost_factor * _price.running_rail[rvi->running_cost_class];
-	} while ((v = GetNextVehicle(v)) != NULL);
-
-	return cost;
-}
-
 void OnNewDay_Train(Vehicle *v)
 {
 	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
@@ -3420,7 +3423,7 @@
 
 		if ((v->vehstatus & VS_STOPPED) == 0) {
 			/* running costs */
-			CommandCost cost(GetTrainRunningCost(v) / 364);
+			CommandCost cost(v->GetRunningCost() / 364);
 
 			v->profit_this_year -= cost.GetCost() >> 8;
 
--- a/src/train_gui.cpp	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/train_gui.cpp	Wed Aug 29 21:27:16 2007 +0000
@@ -209,7 +209,7 @@
 
 	SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED);
 	SetDParam(2, v->max_age / 366);
-	SetDParam(3, GetTrainRunningCost(v) >> 8);
+	SetDParam(3, v->GetDisplayRunningCost());
 	DrawString(x, 15, STR_885D_AGE_RUNNING_COST_YR, 0);
 
 	SetDParam(2, v->GetDisplayMaxSpeed());
--- a/src/vehicle.h	Wed Aug 29 21:08:35 2007 +0000
+++ b/src/vehicle.h	Wed Aug 29 21:27:16 2007 +0000
@@ -427,10 +427,22 @@
 	virtual int GetDisplayMaxSpeed() const { return 0; }
 
 	/**
+	 * Gets the running cost of a vehicle
+	 * @return the vehicle's running cost
+	 */
+	virtual Money GetRunningCost() const { return 0; }
+
+	/**
 	 * Calls the tick handler of the vehicle
 	 */
 	virtual void Tick() {};
 
+	/**
+	 * Gets the running cost of a vehicle  that can be sent into SetDParam for string processing.
+	 * @return the vehicle's running cost
+	 */
+	Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
+
 	bool IsValid() const { return this->type != VEH_INVALID; }
 };