diff options
author | frosch <frosch@openttd.org> | 2009-02-01 16:10:06 +0000 |
---|---|---|
committer | frosch <frosch@openttd.org> | 2009-02-01 16:10:06 +0000 |
commit | c28c4c1986a9a25cbf67ed84c6ba3d51447bbb6f (patch) | |
tree | db4e289e273064a04dbb517c6f5ff509c4fa81f7 /src | |
parent | 6d5cd47c74be39ce6ffd92addcab417f178cbaad (diff) | |
download | openttd-c28c4c1986a9a25cbf67ed84c6ba3d51447bbb6f.tar.xz |
(svn r15306) -Codechange: Deduplicate code by adding Engine::GetDisplayMaxSpeed(), GetPower() and GetDisplayWeight(). (and using them)
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/api/ai_engine.cpp | 29 | ||||
-rw-r--r-- | src/ai/api/ai_event_types.cpp | 29 | ||||
-rw-r--r-- | src/build_vehicle_gui.cpp | 27 | ||||
-rw-r--r-- | src/engine.cpp | 50 | ||||
-rw-r--r-- | src/engine_base.h | 3 | ||||
-rw-r--r-- | src/engine_gui.cpp | 13 |
6 files changed, 80 insertions, 71 deletions
diff --git a/src/ai/api/ai_engine.cpp b/src/ai/api/ai_engine.cpp index 564dbc018..762da14b1 100644 --- a/src/ai/api/ai_engine.cpp +++ b/src/ai/api/ai_engine.cpp @@ -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) diff --git a/src/ai/api/ai_event_types.cpp b/src/ai/api/ai_event_types.cpp index 1cf14f32d..c495e082e 100644 --- a/src/ai/api/ai_event_types.cpp +++ b/src/ai/api/ai_event_types.cpp @@ -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() diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index a3e104a0c..a67ec2eb2 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -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)); diff --git a/src/engine.cpp b/src/engine.cpp index 1e388a3e1..20be6ebd8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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() diff --git a/src/engine_base.h b/src/engine_base.h index d5fd93329..787ae804b 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -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) diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index 0cf3d99d3..be6af32ce 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -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()); |