Changeset - r10925:e45ad06a0739
[Not reviewed]
master
0 6 0
glx - 16 years ago 2009-01-24 20:14:15
glx@openttd.org
(svn r15261) -Add: added Engine::GetRunningCost() to remove some code duplication. Also stops AIs decrementing vehicle counter of first company
6 files changed with 43 insertions and 81 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_engine.cpp
Show inline comments
 
@@ -193,34 +193,7 @@
 
{
 
	if (!IsValidEngine(engine_id)) return -1;
 

	
 
	/* We need to create an instance in order to obtain GetRunningCost.
 
	 *  This means we temporary allocate a vehicle in the pool, but
 
	 *  there is no other way.. */
 
	Vehicle *vehicle;
 
	switch (::GetEngine(engine_id)->type) {
 
		case VEH_ROAD: {
 
			vehicle = new RoadVehicle();
 
		} break;
 

	
 
		case VEH_TRAIN: {
 
			vehicle = new Train();
 
		} break;
 

	
 
		case VEH_SHIP: {
 
			vehicle = new Ship();
 
		} break;
 

	
 
		case VEH_AIRCRAFT: {
 
			vehicle = new Aircraft();
 
		} break;
 

	
 
		default: NOT_REACHED();
 
	}
 

	
 
	vehicle->engine_type = engine_id;
 
	Money runningCost = vehicle->GetRunningCost();
 
	delete vehicle;
 
	return runningCost >> 8;
 
	return ::GetEngine(engine_id)->GetRunningCost();
 
}
 

	
 
/* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
src/ai/api/ai_event_types.cpp
Show inline comments
 
@@ -140,34 +140,7 @@ Money AIEventEnginePreview::GetPrice()
 

	
 
Money AIEventEnginePreview::GetRunningCost()
 
{
 
	/* We need to create an instance in order to obtain GetRunningCost.
 
	 *  This means we temporary allocate a vehicle in the pool, but
 
	 *  there is no other way.. */
 
	Vehicle *vehicle;
 
	switch (::GetEngine(engine)->type) {
 
		case VEH_ROAD: {
 
			vehicle = new RoadVehicle();
 
		} break;
 

	
 
		case VEH_TRAIN: {
 
			vehicle = new Train();
 
		} break;
 

	
 
		case VEH_SHIP: {
 
			vehicle = new Ship();
 
		} break;
 

	
 
		case VEH_AIRCRAFT: {
 
			vehicle = new Aircraft();
 
		} break;
 

	
 
		default: NOT_REACHED();
 
	}
 

	
 
	vehicle->engine_type = engine;
 
	Money runningCost = vehicle->GetRunningCost();
 
	delete vehicle;
 
	return runningCost >> 8;
 
	return ::GetEngine(engine)->GetRunningCost();
 
}
 

	
 
AIVehicle::VehicleType AIEventEnginePreview::GetVehicleType()
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -165,11 +165,8 @@ static int CDECL TrainEnginePowerSorter(
 

	
 
static int CDECL TrainEngineRunningCostSorter(const void *a, const void *b)
 
{
 
	const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a);
 
	const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b);
 

	
 
	Money va = rvi_a->running_cost * GetPriceByIndex(rvi_a->running_cost_class);
 
	Money vb = rvi_b->running_cost * GetPriceByIndex(rvi_b->running_cost_class);
 
	Money va = GetEngine(*(const EngineID*)a)->GetRunningCost();
 
	Money vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
 
	int r = ClampToI32(va - vb);
 

	
 
	return _internal_sort_order ? -r : r;
 
@@ -186,8 +183,8 @@ static int CDECL TrainEnginePowerVsRunni
 
		* Because of this, the return value have to be reversed as well and we return b - a instead of a - b.
 
		* Another thing is that both power and running costs should be doubled for multiheaded engines.
 
		* Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */
 
	Money va = (rvi_a->running_cost * GetPriceByIndex(rvi_a->running_cost_class)) / max(1U, (uint)rvi_a->power);
 
	Money vb = (rvi_b->running_cost * GetPriceByIndex(rvi_b->running_cost_class)) / max(1U, (uint)rvi_b->power);
 
	Money va = (GetEngine(*(const EngineID*)a)->GetRunningCost()) / max(1U, (uint)rvi_a->power);
 
	Money vb = (GetEngine(*(const EngineID*)b)->GetRunningCost()) / max(1U, (uint)rvi_b->power);
 
	int r = ClampToI32(vb - va);
 

	
 
	return _internal_sort_order ? -r : r;
 
@@ -244,11 +241,8 @@ static int CDECL RoadVehEngineSpeedSorte
 

	
 
static int CDECL RoadVehEngineRunningCostSorter(const void *a, const void *b)
 
{
 
	const RoadVehicleInfo *rvi_a = RoadVehInfo(*(const EngineID*)a);
 
	const RoadVehicleInfo *rvi_b = RoadVehInfo(*(const EngineID*)b);
 

	
 
	Money va = rvi_a->running_cost * GetPriceByIndex(rvi_a->running_cost_class);
 
	Money vb = rvi_b->running_cost * GetPriceByIndex(rvi_b->running_cost_class);
 
	Money va = GetEngine(*(const EngineID*)a)->GetRunningCost();
 
	Money vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
 
	int r = ClampToI32(va - vb);
 

	
 
	if (r == 0) {
 
@@ -292,8 +286,8 @@ static int CDECL ShipEngineSpeedSorter(c
 

	
 
static int CDECL ShipEngineRunningCostSorter(const void *a, const void *b)
 
{
 
	const int va = ShipVehInfo(*(const EngineID*)a)->running_cost;
 
	const int vb = ShipVehInfo(*(const EngineID*)b)->running_cost;
 
	const int va = GetEngine(*(const EngineID*)a)->GetRunningCost();
 
	const int vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
 
	const int r = va - vb;
 

	
 
	if (r == 0) {
 
@@ -342,8 +336,8 @@ static int CDECL AircraftEngineSpeedSort
 

	
 
static int CDECL AircraftEngineRunningCostSorter(const void *a, const void *b)
 
{
 
	const int va = AircraftVehInfo(*(const EngineID*)a)->running_cost;
 
	const int vb = AircraftVehInfo(*(const EngineID*)b)->running_cost;
 
	const int va = GetEngine(*(const EngineID*)a)->GetRunningCost();
 
	const int vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
 
	const int r = va - vb;
 

	
 
	if (r == 0) {
 
@@ -512,7 +506,7 @@ static int DrawRailWagonPurchaseInfo(int
 

	
 
	/* Running cost */
 
	if (rvi->running_cost_class != 0xFF) {
 
		SetDParam(0, GetEngineProperty(engine_number, 0x0D, rvi->running_cost) * GetPriceByIndex(rvi->running_cost_class) >> 8);
 
		SetDParam(0, GetEngine(engine_number)->GetRunningCost());
 
		DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
 
		y += 10;
 
	}
 
@@ -547,7 +541,7 @@ static int DrawRailEnginePurchaseInfo(in
 

	
 
	/* Running cost */
 
	if (rvi->running_cost_class != 0xFF) {
 
		SetDParam(0, GetEngineProperty(engine_number, 0x0D, rvi->running_cost) * GetPriceByIndex(rvi->running_cost_class) >> 8);
 
		SetDParam(0, GetEngine(engine_number)->GetRunningCost());
 
		DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
 
		y += 10;
 
	}
 
@@ -573,7 +567,7 @@ static int DrawRoadVehPurchaseInfo(int x
 
	y += 10;
 

	
 
	/* Running cost */
 
	SetDParam(0, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) >> 8);
 
	SetDParam(0, GetEngine(engine_number)->GetRunningCost());
 
	DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
 
	y += 10;
 

	
 
@@ -598,7 +592,7 @@ static int DrawShipPurchaseInfo(int x, i
 
	y += 10;
 

	
 
	/* Running cost */
 
	SetDParam(0, GetEngineProperty(engine_number, 0x0F, svi->running_cost) * _price.ship_running >> 8);
 
	SetDParam(0, GetEngine(engine_number)->GetRunningCost());
 
	DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
 
	y += 10;
 

	
 
@@ -633,7 +627,7 @@ static int DrawAircraftPurchaseInfo(int 
 
	y += 10;
 

	
 
	/* Running cost */
 
	SetDParam(0, GetEngineProperty(engine_number, 0x0E, avi->running_cost) * _price.aircraft_running >> 8);
 
	SetDParam(0, GetEngine(engine_number)->GetRunningCost());
 
	DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
 
	y += 10;
 

	
src/engine.cpp
Show inline comments
 
@@ -118,6 +118,25 @@ Engine::~Engine()
 
	free(this->name);
 
}
 

	
 
Money Engine::GetRunningCost() const
 
{
 
	switch (this->type) {
 
		case VEH_ROAD:
 
			return this->u.road.running_cost * GetPriceByIndex(this->u.road.running_cost_class) >> 8;
 

	
 
		case VEH_TRAIN:
 
			return GetEngineProperty(this->index, 0x0D, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8;
 

	
 
		case VEH_SHIP:
 
			return GetEngineProperty(this->index, 0x0F, this->u.ship.running_cost) * _price.ship_running >> 8;
 

	
 
		case VEH_AIRCRAFT:
 
			return GetEngineProperty(this->index, 0x0E, this->u.air.running_cost) * _price.aircraft_running >> 8;
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/** Sets cached values in Company::num_vehicles and Group::num_vehicles
 
 */
 
void SetCachedEngineCounts()
src/engine_base.h
Show inline comments
 
@@ -6,6 +6,7 @@
 
#define ENGINE_BASE_H
 

	
 
#include "engine_type.h"
 
#include "economy_type.h"
 
#include "oldpool.h"
 

	
 
DECLARE_OLD_POOL(Engine, Engine, 6, 10000)
 
@@ -48,6 +49,8 @@ struct Engine : PoolItem<Engine, EngineI
 
	~Engine();
 

	
 
	inline bool IsValid() const { return this->info.climates != 0; }
 

	
 
	Money GetRunningCost() const;
 
};
 

	
 
static inline bool IsEngineIndex(uint index)
src/engine_gui.cpp
Show inline comments
 
@@ -135,7 +135,7 @@ static void DrawTrainEngineInfo(EngineID
 
	SetDParam(3, GetEngineProperty(engine, 0x0B, rvi->power));
 
	SetDParam(1, GetEngineProperty(engine, 0x16, rvi->weight) << multihead);
 

	
 
	SetDParam(4, GetEngineProperty(engine, 0x0D, rvi->running_cost) * GetPriceByIndex(rvi->running_cost_class) >> 8);
 
	SetDParam(4, GetEngine(engine)->GetRunningCost());
 

	
 
	uint capacity = GetTotalCapacityOfArticulatedParts(engine, VEH_TRAIN);
 
	if (capacity != 0) {
 
@@ -154,7 +154,7 @@ static void DrawAircraftEngineInfo(Engin
 
	SetDParam(1, avi->max_speed * 10 / 16);
 
	SetDParam(2, avi->passenger_capacity);
 
	SetDParam(3, avi->mail_capacity);
 
	SetDParam(4, GetEngineProperty(engine, 0x0E, avi->running_cost) * _price.aircraft_running >> 8);
 
	SetDParam(4, GetEngine(engine)->GetRunningCost());
 

	
 
	DrawStringMultiCenter(x, y, STR_A02E_COST_MAX_SPEED_CAPACITY, maxw);
 
}
 
@@ -165,7 +165,7 @@ static void DrawRoadVehEngineInfo(Engine
 

	
 
	SetDParam(0, (_price.roadveh_base >> 3) * GetEngineProperty(engine, 0x11, rvi->cost_factor) >> 5);
 
	SetDParam(1, rvi->max_speed * 10 / 32);
 
	SetDParam(2, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) >> 8);
 
	SetDParam(2, GetEngine(engine)->GetRunningCost());
 
	SetDParam(3, rvi->cargo_type);
 
	SetDParam(4, GetTotalCapacityOfArticulatedParts(engine, VEH_ROAD));
 

	
 
@@ -179,7 +179,7 @@ static void DrawShipEngineInfo(EngineID 
 
	SetDParam(1, GetEngineProperty(engine, 0x0B, svi->max_speed) * 10 / 32);
 
	SetDParam(2, svi->cargo_type);
 
	SetDParam(3, GetEngineProperty(engine, 0x0D, svi->capacity));
 
	SetDParam(4, GetEngineProperty(engine, 0x0F, svi->running_cost) * _price.ship_running >> 8);
 
	SetDParam(4, GetEngine(engine)->GetRunningCost());
 
	DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw);
 
}
 

	
0 comments (0 inline, 0 general)