(svn r15306) -Codechange: Deduplicate code by adding Engine::GetDisplayMaxSpeed(), GetPower() and GetDisplayWeight(). (and using them)

This commit is contained in:
frosch 2009-02-01 16:10:06 +00:00
parent 6d5cd47c74
commit c28c4c1986
6 changed files with 80 additions and 71 deletions

View File

@ -118,31 +118,10 @@
{
if (!IsValidEngine(engine_id)) return -1;
switch (::GetEngine(engine_id)->type) {
case VEH_ROAD: {
const RoadVehicleInfo *vi = ::RoadVehInfo(engine_id);
/* Internal speeds are km/h * 2 */
return vi->max_speed / 2;
} break;
case VEH_TRAIN: {
const RailVehicleInfo *vi = ::RailVehInfo(engine_id);
return vi->max_speed;
} break;
case VEH_SHIP: {
const ShipVehicleInfo *vi = ::ShipVehInfo(engine_id);
/* Internal speeds are km/h * 2 */
return vi->max_speed / 2;
} break;
case VEH_AIRCRAFT: {
const AircraftVehicleInfo *vi = ::AircraftVehInfo(engine_id);
return vi->max_speed / _settings_game.vehicle.plane_speed;
} break;
default: NOT_REACHED();
}
const Engine *e = ::GetEngine(engine_id);
int32 max_speed = e->GetDisplayMaxSpeed() * 16 / 10; // convert mph to km-ish/h
if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
return max_speed;
}
/* static */ Money AIEngine::GetPrice(EngineID engine_id)

View File

@ -79,31 +79,10 @@ int32 AIEventEnginePreview::GetCapacity()
int32 AIEventEnginePreview::GetMaxSpeed()
{
switch (::GetEngine(engine)->type) {
case VEH_ROAD: {
const RoadVehicleInfo *vi = ::RoadVehInfo(engine);
/* Internal speeds are km/h * 2 */
return vi->max_speed / 2;
} break;
case VEH_TRAIN: {
const RailVehicleInfo *vi = ::RailVehInfo(engine);
return vi->max_speed;
} break;
case VEH_SHIP: {
const ShipVehicleInfo *vi = ::ShipVehInfo(engine);
/* Internal speeds are km/h * 2 */
return vi->max_speed / 2;
} break;
case VEH_AIRCRAFT: {
const AircraftVehicleInfo *vi = ::AircraftVehInfo(engine);
return vi->max_speed / _settings_game.vehicle.plane_speed;
} break;
default: NOT_REACHED();
}
const Engine *e = ::GetEngine(engine);
int32 max_speed = e->GetDisplayMaxSpeed() * 16 / 10; // convert mph to km-ish/h
if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
return max_speed;
}
Money AIEventEnginePreview::GetPrice()

View File

@ -482,7 +482,7 @@ static int DrawRailWagonPurchaseInfo(int x, int y, EngineID engine_number, const
y += 10;
/* Wagon weight - (including cargo) */
uint weight = GetEngineProperty(engine_number, 0x16, rvi->weight);
uint weight = e->GetDisplayWeight();
SetDParam(0, weight);
SetDParam(1, (GetCargo(rvi->cargo_type)->weight * GetEngineProperty(engine_number, 0x14, rvi->capacity) >> 4) + weight);
DrawString(x, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT, TC_FROMSTRING);
@ -490,9 +490,9 @@ static int DrawRailWagonPurchaseInfo(int x, int y, EngineID engine_number, const
/* Wagon speed limit, displayed if above zero */
if (_settings_game.vehicle.wagon_speed_limits) {
uint max_speed = GetEngineProperty(engine_number, 0x09, rvi->max_speed);
uint max_speed = e->GetDisplayMaxSpeed();
if (max_speed > 0) {
SetDParam(0, max_speed * 10 / 16);
SetDParam(0, max_speed);
DrawString(x, y, STR_PURCHASE_INFO_SPEED, TC_FROMSTRING);
y += 10;
}
@ -511,25 +511,24 @@ static int DrawRailWagonPurchaseInfo(int x, int y, EngineID engine_number, const
/* Draw locomotive specific details */
static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, const RailVehicleInfo *rvi)
{
int multihead = (rvi->railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
uint weight = GetEngineProperty(engine_number, 0x16, rvi->weight);
const Engine *e = GetEngine(engine_number);
uint weight = e->GetDisplayWeight();
/* Purchase Cost - Engine weight */
SetDParam(0, e->GetCost());
SetDParam(1, weight << multihead);
SetDParam(1, weight);
DrawString(x, y, STR_PURCHASE_INFO_COST_WEIGHT, TC_FROMSTRING);
y += 10;
/* Max speed - Engine power */
SetDParam(0, GetEngineProperty(engine_number, 0x09, rvi->max_speed) * 10 / 16);
SetDParam(1, GetEngineProperty(engine_number, 0x0B, rvi->power));
SetDParam(0, e->GetDisplayMaxSpeed());
SetDParam(1, e->GetPower());
DrawString(x, y, STR_PURCHASE_INFO_SPEED_POWER, TC_FROMSTRING);
y += 10;
/* Max tractive effort - not applicable if old acceleration or maglev */
if (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && rvi->railtype != RAILTYPE_MAGLEV) {
SetDParam(0, ((weight << multihead) * 10 * GetEngineProperty(engine_number, 0x1F, rvi->tractive_effort)) / 256);
SetDParam(0, (weight * 10 * GetEngineProperty(engine_number, 0x1F, rvi->tractive_effort)) / 256);
DrawString(x, y, STR_PURCHASE_INFO_MAX_TE, TC_FROMSTRING);
y += 10;
}
@ -553,13 +552,13 @@ static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, cons
}
/* Draw road vehicle specific details */
static int DrawRoadVehPurchaseInfo(int x, int y, EngineID engine_number, const RoadVehicleInfo *rvi)
static int DrawRoadVehPurchaseInfo(int x, int y, EngineID engine_number)
{
const Engine *e = GetEngine(engine_number);
/* Purchase cost - Max speed */
SetDParam(0, e->GetCost());
SetDParam(1, rvi->max_speed * 10 / 32);
SetDParam(1, e->GetDisplayMaxSpeed());
DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
y += 10;
@ -579,7 +578,7 @@ static int DrawShipPurchaseInfo(int x, int y, EngineID engine_number, const Ship
/* Purchase cost - Max speed */
SetDParam(0, e->GetCost());
SetDParam(1, GetEngineProperty(engine_number, 0x0B, svi->max_speed) * 10 / 32);
SetDParam(1, e->GetDisplayMaxSpeed());
DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
y += 10;
@ -606,7 +605,7 @@ static int DrawAircraftPurchaseInfo(int x, int y, EngineID engine_number, const
/* Purchase cost - Max speed */
SetDParam(0, e->GetCost());
SetDParam(1, avi->max_speed * 10 / 16);
SetDParam(1, e->GetDisplayMaxSpeed());
DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
y += 10;
@ -674,7 +673,7 @@ int DrawVehiclePurchaseInfo(int x, int y, uint w, EngineID engine_number)
break;
}
case VEH_ROAD:
y = DrawRoadVehPurchaseInfo(x, y, engine_number, RoadVehInfo(engine_number));
y = DrawRoadVehPurchaseInfo(x, y, engine_number);
break;
case VEH_SHIP:
y = DrawShipPurchaseInfo(x, y, engine_number, ShipVehInfo(engine_number));

View File

@ -154,6 +154,56 @@ Money Engine::GetCost() const
}
}
/**
* Returns max speed for display purposes
* @return max speed in mph
*/
uint Engine::GetDisplayMaxSpeed() const
{
switch (this->type) {
case VEH_TRAIN:
return GetEngineProperty(this->index, 0x09, this->u.rail.max_speed) * 10 / 16;
case VEH_ROAD:
return this->u.road.max_speed * 10 / 32;
case VEH_SHIP:
return GetEngineProperty(this->index, 0x0B, this->u.ship.max_speed) * 10 / 32;
case VEH_AIRCRAFT:
return this->u.air.max_speed * 10 / 16;
default: NOT_REACHED();
}
}
uint Engine::GetPower() const
{
/* Currently only trains have 'power' */
switch (this->type) {
case VEH_TRAIN:
return GetEngineProperty(this->index, 0x0B, this->u.rail.power);
default: NOT_REACHED();
}
}
/**
* Returns the weight for display purposes.
* For dual-headed train-engines this is the weight of both heads
* @return weight in display units metric tons
*/
uint Engine::GetDisplayWeight() const
{
/* Currently only trains have 'weight' */
switch (this->type) {
case VEH_TRAIN:
return GetEngineProperty(this->index, 0x16, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
default: NOT_REACHED();
}
}
/** Sets cached values in Company::num_vehicles and Group::num_vehicles
*/
void SetCachedEngineCounts()

View File

@ -52,6 +52,9 @@ struct Engine : PoolItem<Engine, EngineID, &_Engine_pool> {
Money GetRunningCost() const;
Money GetCost() const;
uint GetDisplayMaxSpeed() const;
uint GetPower() const;
uint GetDisplayWeight() const;
};
static inline bool IsEngineIndex(uint index)

View File

@ -124,13 +124,12 @@ static uint GetTotalCapacityOfArticulatedParts(EngineID engine, VehicleType type
static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw)
{
const RailVehicleInfo *rvi = RailVehInfo(engine);
int multihead = (rvi->railveh_type == RAILVEH_MULTIHEAD) ? 1 : 0;
const Engine *e = GetEngine(engine);
SetDParam(0, e->GetCost());
SetDParam(2, GetEngineProperty(engine, 0x09, rvi->max_speed) * 10 / 16);
SetDParam(3, GetEngineProperty(engine, 0x0B, rvi->power));
SetDParam(1, GetEngineProperty(engine, 0x16, rvi->weight) << multihead);
SetDParam(2, e->GetDisplayMaxSpeed());
SetDParam(3, e->GetPower());
SetDParam(1, e->GetDisplayWeight());
SetDParam(4, e->GetRunningCost());
@ -150,7 +149,7 @@ static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw)
const Engine *e = GetEngine(engine);
SetDParam(0, e->GetCost());
SetDParam(1, avi->max_speed * 10 / 16);
SetDParam(1, e->GetDisplayMaxSpeed());
SetDParam(2, avi->passenger_capacity);
SetDParam(3, avi->mail_capacity);
SetDParam(4, e->GetRunningCost());
@ -164,7 +163,7 @@ static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw)
const Engine *e = GetEngine(engine);
SetDParam(0, e->GetCost());
SetDParam(1, rvi->max_speed * 10 / 32);
SetDParam(1, e->GetDisplayMaxSpeed());
SetDParam(2, e->GetRunningCost());
SetDParam(3, rvi->cargo_type);
SetDParam(4, GetTotalCapacityOfArticulatedParts(engine, VEH_ROAD));
@ -178,7 +177,7 @@ static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw)
const Engine *e = GetEngine(engine);
SetDParam(0, e->GetCost());
SetDParam(1, GetEngineProperty(engine, 0x0B, svi->max_speed) * 10 / 32);
SetDParam(1, e->GetDisplayMaxSpeed());
SetDParam(2, svi->cargo_type);
SetDParam(3, GetEngineProperty(engine, 0x0D, svi->capacity));
SetDParam(4, e->GetRunningCost());