(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.

pull/155/head
frosch 15 years ago
parent bc48c70c46
commit fe65c7f833

@ -540,7 +540,9 @@ static void CheckIfAircraftNeedsService(Aircraft *v)
Money Aircraft::GetRunningCost() const 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() void Aircraft::OnNewDay()

@ -788,15 +788,25 @@ void InitializeEconomy()
} }
/** /**
* Determine a certain base price with range checking * Determine a certain price
* @param index Price of interest * @param index Price base
* @return Base price, or zero if out of range * @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; 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) Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type)

@ -41,7 +41,7 @@ uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, Sourc
void PrepareUnload(Vehicle *front_v); void PrepareUnload(Vehicle *front_v);
void LoadUnloadStation(Station *st); void LoadUnloadStation(Station *st);
Money GetPriceByIndex(Price index); Money GetPrice(Price index, uint cost_factor, int shift = 0);
void InitializeEconomy(); void InitializeEconomy();
void RecomputePrices(); void RecomputePrices();

@ -204,48 +204,71 @@ uint Engine::GetDisplayDefaultCapacity(uint16 *mail_capacity) const
Money Engine::GetRunningCost() const Money Engine::GetRunningCost() const
{ {
Price base_price;
uint cost_factor;
switch (this->type) { switch (this->type) {
case VEH_ROAD: { case VEH_ROAD:
if (this->u.road.running_cost_class == INVALID_PRICE) return 0; base_price = this->u.road.running_cost_class;
return GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost) * GetPriceByIndex(this->u.road.running_cost_class) >> 8; 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: { case VEH_TRAIN:
if (this->u.rail.running_cost_class == INVALID_PRICE) return 0; base_price = this->u.rail.running_cost_class;
return GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8; 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: 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: 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(); default: NOT_REACHED();
} }
return GetPrice(base_price, cost_factor, -8);
} }
Money Engine::GetCost() const Money Engine::GetCost() const
{ {
Price base_price;
uint cost_factor;
switch (this->type) { switch (this->type) {
case VEH_ROAD: 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: case VEH_TRAIN:
if (this->u.rail.railveh_type == RAILVEH_WAGON) { 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 { } 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: 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: 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(); default: NOT_REACHED();
} }
return GetPrice(base_price, cost_factor, -8);
} }
/** /**

@ -1827,13 +1827,13 @@ static bool RoadVehController(RoadVehicle *v)
Money RoadVehicle::GetRunningCost() const Money RoadVehicle::GetRunningCost() const
{ {
const RoadVehicleInfo *rvi = RoadVehInfo(this->engine_type); const Engine *e = Engine::Get(this->engine_type);
if (rvi->running_cost_class == INVALID_PRICE) return 0; 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; 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() bool RoadVehicle::Tick()

@ -159,7 +159,9 @@ static void CheckIfShipNeedsService(Vehicle *v)
Money Ship::GetRunningCost() const 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() void Ship::OnNewDay()

@ -4443,16 +4443,16 @@ Money Train::GetRunningCost() const
const Train *v = this; const Train *v = this;
do { do {
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); const Engine *e = Engine::Get(v->engine_type);
if (rvi->running_cost_class == INVALID_PRICE) continue; if (e->u.rail.running_cost_class == INVALID_PRICE) continue;
uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, rvi->running_cost); uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->u.rail.running_cost);
if (cost_factor == 0) continue; if (cost_factor == 0) continue;
/* Halve running cost for multiheaded parts */ /* Halve running cost for multiheaded parts */
if (v->IsMultiheaded()) cost_factor /= 2; 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); } while ((v = v->GetNextVehicle()) != NULL);
return cost; return cost;

@ -262,33 +262,35 @@ CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32
*/ */
static CommandCost GetRefitCost(EngineID engine_type) static CommandCost GetRefitCost(EngineID engine_type)
{ {
Money base_cost;
ExpensesType expense_type; ExpensesType expense_type;
const Engine *e = Engine::Get(engine_type); const Engine *e = Engine::Get(engine_type);
Price base_price;
uint cost_factor = e->info.refit_cost;
switch (e->type) { switch (e->type) {
case VEH_SHIP: case VEH_SHIP:
base_cost = _price[PR_BUILD_VEHICLE_SHIP]; base_price = PR_BUILD_VEHICLE_SHIP;
expense_type = EXPENSES_SHIP_RUN; expense_type = EXPENSES_SHIP_RUN;
break; break;
case VEH_ROAD: case VEH_ROAD:
base_cost = _price[PR_BUILD_VEHICLE_ROAD]; base_price = PR_BUILD_VEHICLE_ROAD;
expense_type = EXPENSES_ROADVEH_RUN; expense_type = EXPENSES_ROADVEH_RUN;
break; break;
case VEH_AIRCRAFT: case VEH_AIRCRAFT:
base_cost = _price[PR_BUILD_VEHICLE_AIRCRAFT]; base_price = PR_BUILD_VEHICLE_AIRCRAFT;
expense_type = EXPENSES_AIRCRAFT_RUN; expense_type = EXPENSES_AIRCRAFT_RUN;
break; break;
case VEH_TRAIN: 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; expense_type = EXPENSES_TRAIN_RUN;
break; break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
return CommandCost(expense_type, (e->info.refit_cost * base_cost) >> 10); return CommandCost(expense_type, GetPrice(base_price, cost_factor, -10));
} }
/** /**

Loading…
Cancel
Save