From 100ae8efcc592ec44f0cc161e0045fb84b67c45e Mon Sep 17 00:00:00 2001 From: smatz Date: Thu, 27 Aug 2009 13:31:26 +0000 Subject: (svn r17292) -Codechange: use unified ToPercent() function to convert fract numbers to percents --- src/ai/api/ai_engine.cpp | 2 +- src/ai/api/ai_vehicle.cpp | 2 +- src/build_vehicle_gui.cpp | 2 +- src/core/math_func.hpp | 22 ++++++++++++++++++++++ src/industry_gui.cpp | 6 +++--- src/order_cmd.cpp | 2 +- src/station_gui.cpp | 2 +- src/vehicle_gui.cpp | 2 +- 8 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/ai/api/ai_engine.cpp b/src/ai/api/ai_engine.cpp index d25f522e1..a3f5c39cd 100644 --- a/src/ai/api/ai_engine.cpp +++ b/src/ai/api/ai_engine.cpp @@ -99,7 +99,7 @@ if (!IsValidEngine(engine_id)) return -1; if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1; - return (::Engine::Get(engine_id)->reliability * 100 >> 16); + return ::ToPercent16(::Engine::Get(engine_id)->reliability); } /* static */ int32 AIEngine::GetMaxSpeed(EngineID engine_id) diff --git a/src/ai/api/ai_vehicle.cpp b/src/ai/api/ai_vehicle.cpp index e15062daa..acf1a3bd8 100644 --- a/src/ai/api/ai_vehicle.cpp +++ b/src/ai/api/ai_vehicle.cpp @@ -438,5 +438,5 @@ if (!IsValidVehicle(vehicle_id)) return -1; const Vehicle *v = ::Vehicle::Get(vehicle_id); - return v->reliability * 100 >> 16; + return ::ToPercent16(v->reliability); } diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 8b377047d..1d67a221f 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -691,7 +691,7 @@ int DrawVehiclePurchaseInfo(int left, int right, int y, EngineID engine_number) y += FONT_HEIGHT_NORMAL; /* Reliability */ - SetDParam(0, e->reliability * 100 >> 16); + SetDParam(0, ToPercent16(e->reliability)); DrawString(left, right, y, STR_PURCHASE_INFO_RELIABILITY); y += FONT_HEIGHT_NORMAL; } diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp index 39ed16c98..b27fe9a01 100644 --- a/src/core/math_func.hpp +++ b/src/core/math_func.hpp @@ -269,6 +269,28 @@ static FORCEINLINE void Swap(T &a, T &b) b = t; } +/** + * Converts a "fract" value 0..255 to "percent" value 0..100 + * @param i value to convert, range 0..255 + * @return value in range 0..100 + */ +static FORCEINLINE uint ToPercent8(uint i) +{ + assert(i < 256); + return i * 101 >> 8; +} + +/** + * Converts a "fract" value 0..65535 to "percent" value 0..100 + * @param i value to convert, range 0..65535 + * @return value in range 0..100 + */ +static FORCEINLINE uint ToPercent16(uint i) +{ + assert(i < 65536); + return i * 101 >> 16; +} + int LeastCommonMultiple(int a, int b); int GreatestCommonDivisor(int a, int b); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 9cc76a5ef..6276d9411 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -554,7 +554,7 @@ public: SetDParam(1, i->last_month_production[j]); SetDParam(2, GetCargoSuffix(j + 3, CST_VIEW, i, i->type, ind)); - SetDParam(3, i->last_month_pct_transported[j] * 100 >> 8); + SetDParam(3, ToPercent8(i->last_month_pct_transported[j])); uint x = 4 + (IsProductionAlterable(i) ? 30 : 0); DrawString(x, this->widget[IVW_INFO].right, y, STR_INDUSTRY_VIEW_TRANSPORTED); /* Let's put out those buttons.. */ @@ -833,7 +833,7 @@ protected: assert(id < lengthof(i->produced_cargo)); if (i->produced_cargo[id] == CT_INVALID) return 101; - return i->last_month_pct_transported[id] * 100 >> 8; + return ToPercent8(i->last_month_pct_transported[id]); } /** @@ -968,7 +968,7 @@ public: /* Transported productions */ for (byte j = 0; j < lengthof(i->produced_cargo); j++) { if (i->produced_cargo[j] == CT_INVALID) continue; - SetDParam(p++, i->last_month_pct_transported[j] * 100 >> 8); + SetDParam(p++, ToPercent8(i->last_month_pct_transported[j])); } /* Drawing the right string */ diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 35ba61bd9..0b344b08d 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1630,7 +1630,7 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v) switch (order->GetConditionVariable()) { case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break; - case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, v->reliability * 100 >> 16, value); break; + case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break; case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break; case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break; case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break; diff --git a/src/station_gui.cpp b/src/station_gui.cpp index cd6a3df53..424e8e7c7 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -954,7 +954,7 @@ struct StationViewWindow : public Window { if (!HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP)) continue; SetDParam(0, cs->name); - SetDParam(2, ge->rating * 101 >> 8); + SetDParam(2, ToPercent8(ge->rating)); SetDParam(1, STR_CARGO_RATING_APPALLING + (ge->rating >> 5)); DrawString(this->widget[SVW_ACCEPTLIST].left + 8, this->widget[SVW_ACCEPTLIST].right - 2, y, STR_STATION_VIEW_CARGO_RATING); y += 10; diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 381fe01a5..f4b9eacbb 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1424,7 +1424,7 @@ struct VehicleDetailsWindow : Window { DrawString(2, this->width - 2, 35, STR_VEHICLE_INFO_PROFIT_THIS_YEAR_LAST_YEAR); /* Draw breakdown & reliability */ - SetDParam(0, v->reliability * 100 >> 16); + SetDParam(0, ToPercent16(v->reliability)); SetDParam(1, v->breakdowns_since_last_service); DrawString(2, this->width - 2, 45, STR_VEHICLE_INFO_RELIABILITY_BREAKDOWNS); -- cgit v1.2.3-54-g00ecf