Changeset - r13731:60d669a7b9e1
[Not reviewed]
master
0 8 0
frosch - 15 years ago 2009-11-24 13:12:34
frosch@openttd.org
(svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
8 files changed with 77 insertions and 38 deletions:
0 comments (0 inline, 0 general)
src/aircraft_cmd.cpp
Show inline comments
 
@@ -540,7 +540,9 @@ static void CheckIfAircraftNeedsService(
 

	
 
Money Aircraft::GetRunningCost() const
 
{
 
	return GetVehicleProperty(this, PROP_AIRCRAFT_RUNNING_COST_FACTOR, AircraftVehInfo(this->engine_type)->running_cost) * _price[PR_RUNNING_AIRCRAFT];
 
	const Engine *e = Engine::Get(this->engine_type);
 
	uint cost_factor = GetVehicleProperty(this, PROP_AIRCRAFT_RUNNING_COST_FACTOR, e->u.air.running_cost);
 
	return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor);
 
}
 

	
 
void Aircraft::OnNewDay()
src/economy.cpp
Show inline comments
 
@@ -788,15 +788,25 @@ void InitializeEconomy()
 
}
 

	
 
/**
 
 * Determine a certain base price with range checking
 
 * @param index Price of interest
 
 * @return Base price, or zero if out of range
 
 * Determine a certain price
 
 * @param index Price base
 
 * @param cost_factor Price factor
 
 * @param shift Extra bit shifting after the computation
 
 * @return Price
 
 */
 
Money GetPriceByIndex(Price index)
 
Money GetPrice(Price index, uint cost_factor, int shift)
 
{
 
	if (index >= PR_END) return 0;
 

	
 
	return _price[index];
 
	Money cost = _price[index] * cost_factor;
 

	
 
	if (shift >= 0) {
 
		cost <<= shift;
 
	} else {
 
		cost >>= -shift;
 
	}
 

	
 
	return cost;
 
}
 

	
 
Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type)
src/economy_func.h
Show inline comments
 
@@ -41,7 +41,7 @@ uint MoveGoodsToStation(CargoID type, ui
 
void PrepareUnload(Vehicle *front_v);
 
void LoadUnloadStation(Station *st);
 

	
 
Money GetPriceByIndex(Price index);
 
Money GetPrice(Price index, uint cost_factor, int shift = 0);
 

	
 
void InitializeEconomy();
 
void RecomputePrices();
src/engine.cpp
Show inline comments
 
@@ -204,48 +204,71 @@ uint Engine::GetDisplayDefaultCapacity(u
 

	
 
Money Engine::GetRunningCost() const
 
{
 
	Price base_price;
 
	uint cost_factor;
 
	switch (this->type) {
 
		case VEH_ROAD: {
 
			if (this->u.road.running_cost_class == INVALID_PRICE) return 0;
 
			return GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost) * GetPriceByIndex(this->u.road.running_cost_class) >> 8;
 
		}
 
		case VEH_ROAD:
 
			base_price = this->u.road.running_cost_class;
 
			if (base_price == INVALID_PRICE) return 0;
 
			cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost);
 
			break;
 

	
 
		case VEH_TRAIN: {
 
			if (this->u.rail.running_cost_class == INVALID_PRICE) return 0;
 
			return GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8;
 
		}
 
		case VEH_TRAIN:
 
			base_price = this->u.rail.running_cost_class;
 
			if (base_price == INVALID_PRICE) return 0;
 
			cost_factor = GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost);
 
			break;
 

	
 
		case VEH_SHIP:
 
			return GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost) * _price[PR_RUNNING_SHIP] >> 8;
 
			base_price = PR_RUNNING_SHIP;
 
			cost_factor = GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost);
 
			break;
 

	
 
		case VEH_AIRCRAFT:
 
			return GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost) * _price[PR_RUNNING_AIRCRAFT] >> 8;
 
			base_price = PR_RUNNING_AIRCRAFT;
 
			cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost);
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 

	
 
	return GetPrice(base_price, cost_factor, -8);
 
}
 

	
 
Money Engine::GetCost() const
 
{
 
	Price base_price;
 
	uint cost_factor;
 
	switch (this->type) {
 
		case VEH_ROAD:
 
			return GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor) * (_price[PR_BUILD_VEHICLE_ROAD] >> 3) >> 5;
 
			base_price = PR_BUILD_VEHICLE_ROAD;
 
			cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor);
 
			break;
 

	
 
		case VEH_TRAIN:
 
			if (this->u.rail.railveh_type == RAILVEH_WAGON) {
 
				return (GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor) * _price[PR_BUILD_VEHICLE_WAGON]) >> 8;
 
				base_price = PR_BUILD_VEHICLE_WAGON;
 
				cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
 
			} else {
 
				return GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor) * (_price[PR_BUILD_VEHICLE_TRAIN] >> 3) >> 5;
 
				base_price = PR_BUILD_VEHICLE_TRAIN;
 
				cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
 
			}
 
			break;
 

	
 
		case VEH_SHIP:
 
			return GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor) * (_price[PR_BUILD_VEHICLE_SHIP] >> 3) >> 5;
 
			base_price = PR_BUILD_VEHICLE_SHIP;
 
			cost_factor = GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor);
 
			break;
 

	
 
		case VEH_AIRCRAFT:
 
			return GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor) * (_price[PR_BUILD_VEHICLE_AIRCRAFT] >> 3) >> 5;
 
			base_price = PR_BUILD_VEHICLE_AIRCRAFT;
 
			cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor);
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 

	
 
	return GetPrice(base_price, cost_factor, -8);
 
}
 

	
 
/**
src/roadveh_cmd.cpp
Show inline comments
 
@@ -1827,13 +1827,13 @@ static bool RoadVehController(RoadVehicl
 

	
 
Money RoadVehicle::GetRunningCost() const
 
{
 
	const RoadVehicleInfo *rvi = RoadVehInfo(this->engine_type);
 
	if (rvi->running_cost_class == INVALID_PRICE) return 0;
 
	const Engine *e = Engine::Get(this->engine_type);
 
	if (e->u.road.running_cost_class == INVALID_PRICE) return 0;
 

	
 
	uint cost_factor = GetVehicleProperty(this, PROP_ROADVEH_RUNNING_COST_FACTOR, rvi->running_cost);
 
	uint cost_factor = GetVehicleProperty(this, PROP_ROADVEH_RUNNING_COST_FACTOR, e->u.road.running_cost);
 
	if (cost_factor == 0) return 0;
 

	
 
	return cost_factor * GetPriceByIndex(rvi->running_cost_class);
 
	return GetPrice(e->u.road.running_cost_class, cost_factor);
 
}
 

	
 
bool RoadVehicle::Tick()
src/ship_cmd.cpp
Show inline comments
 
@@ -159,7 +159,9 @@ static void CheckIfShipNeedsService(Vehi
 

	
 
Money Ship::GetRunningCost() const
 
{
 
	return GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, ShipVehInfo(this->engine_type)->running_cost) * _price[PR_RUNNING_SHIP];
 
	const Engine *e = Engine::Get(this->engine_type);
 
	uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
 
	return GetPrice(PR_RUNNING_SHIP, cost_factor);
 
}
 

	
 
void Ship::OnNewDay()
src/train_cmd.cpp
Show inline comments
 
@@ -4443,16 +4443,16 @@ Money Train::GetRunningCost() const
 
	const Train *v = this;
 

	
 
	do {
 
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
 
		if (rvi->running_cost_class == INVALID_PRICE) continue;
 

	
 
		uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, rvi->running_cost);
 
		const Engine *e = Engine::Get(v->engine_type);
 
		if (e->u.rail.running_cost_class == INVALID_PRICE) continue;
 

	
 
		uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->u.rail.running_cost);
 
		if (cost_factor == 0) continue;
 

	
 
		/* Halve running cost for multiheaded parts */
 
		if (v->IsMultiheaded()) cost_factor /= 2;
 

	
 
		cost += cost_factor * GetPriceByIndex(rvi->running_cost_class);
 
		cost += GetPrice(e->u.rail.running_cost_class, cost_factor);
 
	} while ((v = v->GetNextVehicle()) != NULL);
 

	
 
	return cost;
src/vehicle_cmd.cpp
Show inline comments
 
@@ -262,33 +262,35 @@ CommandCost CmdDepotMassAutoReplace(Tile
 
 */
 
static CommandCost GetRefitCost(EngineID engine_type)
 
{
 
	Money base_cost;
 
	ExpensesType expense_type;
 
	const Engine *e = Engine::Get(engine_type);
 
	Price base_price;
 
	uint cost_factor = e->info.refit_cost;
 
	switch (e->type) {
 
		case VEH_SHIP:
 
			base_cost = _price[PR_BUILD_VEHICLE_SHIP];
 
			base_price = PR_BUILD_VEHICLE_SHIP;
 
			expense_type = EXPENSES_SHIP_RUN;
 
			break;
 

	
 
		case VEH_ROAD:
 
			base_cost = _price[PR_BUILD_VEHICLE_ROAD];
 
			base_price = PR_BUILD_VEHICLE_ROAD;
 
			expense_type = EXPENSES_ROADVEH_RUN;
 
			break;
 

	
 
		case VEH_AIRCRAFT:
 
			base_cost = _price[PR_BUILD_VEHICLE_AIRCRAFT];
 
			base_price = PR_BUILD_VEHICLE_AIRCRAFT;
 
			expense_type = EXPENSES_AIRCRAFT_RUN;
 
			break;
 

	
 
		case VEH_TRAIN:
 
			base_cost = 2 * _price[(e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN];
 
			base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
 
			cost_factor <<= 1;
 
			expense_type = EXPENSES_TRAIN_RUN;
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 
	return CommandCost(expense_type, (e->info.refit_cost * base_cost) >> 10);
 
	return CommandCost(expense_type, GetPrice(base_price, cost_factor, -10));
 
}
 

	
 
/**
0 comments (0 inline, 0 general)