(svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.

pull/155/head
rubidium 17 years ago
parent fff12fd22e
commit dc80195754

@ -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 @@ struct Aircraft : public Vehicle {
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();
};

@ -80,7 +80,7 @@ static void AircraftDetailsWndProc(Window *w, WindowEvent *e)
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);
}

@ -6,7 +6,8 @@
#define ROADVEH_H
#include "vehicle.h"
#include "engine.h"
#include "variables.h"
enum RoadVehicleSubType {
RVST_FRONT,
@ -83,6 +84,7 @@ struct RoadVehicle : public Vehicle {
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();
};

@ -91,7 +91,7 @@ static void RoadVehDetailsWndProc(Window *w, WindowEvent *e)
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);
}

@ -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 @@ struct Ship: public Vehicle {
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();
};

@ -50,7 +50,7 @@ static void ShipDetailsWndProc(Window *w, WindowEvent *e)
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);
}

@ -274,6 +274,7 @@ struct Train : public Vehicle {
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();
};

@ -3313,6 +3313,25 @@ static void TrainLocoHandler(Vehicle *v, bool mode)
}
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 @@ static void CheckIfTrainNeedsService(Vehicle *v)
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 @@ void OnNewDay_Train(Vehicle *v)
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;

@ -209,7 +209,7 @@ static void DrawTrainDetailsWindow(Window *w)
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());

@ -426,11 +426,23 @@ struct Vehicle : PoolItem<Vehicle, VehicleID, &_Vehicle_pool> {
*/
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; }
};

Loading…
Cancel
Save