From ece199176e1db93f97d55c33edec339c6e9bcfec Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Tue, 29 Oct 2024 11:27:00 +0100 Subject: [PATCH] Move UnitConversion from Util into its own compilation unit --- src/openrct2-ui/windows/InstallTrack.cpp | 2 +- src/openrct2-ui/windows/MapGen.cpp | 2 +- src/openrct2-ui/windows/Park.cpp | 2 +- src/openrct2-ui/windows/Ride.cpp | 1 + src/openrct2-ui/windows/TrackList.cpp | 1 + src/openrct2/core/UnitConversion.cpp | 78 +++++++++++++++++++ src/openrct2/core/UnitConversion.h | 27 +++++++ src/openrct2/libopenrct2.vcxproj | 2 + src/openrct2/localisation/Formatting.cpp | 2 +- src/openrct2/ride/RideRatings.cpp | 1 + src/openrct2/ride/TrackDesign.cpp | 1 + src/openrct2/scenario/Scenario.cpp | 1 + .../scripting/bindings/ride/ScRide.cpp | 1 + src/openrct2/util/Util.cpp | 65 ---------------- src/openrct2/util/Util.h | 12 --- 15 files changed, 117 insertions(+), 81 deletions(-) create mode 100644 src/openrct2/core/UnitConversion.cpp create mode 100644 src/openrct2/core/UnitConversion.h diff --git a/src/openrct2-ui/windows/InstallTrack.cpp b/src/openrct2-ui/windows/InstallTrack.cpp index ac0dc485ff..6636334a91 100644 --- a/src/openrct2-ui/windows/InstallTrack.cpp +++ b/src/openrct2-ui/windows/InstallTrack.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,6 @@ #include #include #include -#include #include #include diff --git a/src/openrct2-ui/windows/MapGen.cpp b/src/openrct2-ui/windows/MapGen.cpp index bfc7dc89a7..0fd661cb60 100644 --- a/src/openrct2-ui/windows/MapGen.cpp +++ b/src/openrct2-ui/windows/MapGen.cpp @@ -16,13 +16,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include #include #include diff --git a/src/openrct2-ui/windows/Park.cpp b/src/openrct2-ui/windows/Park.cpp index cc951ae8a6..d238b16a40 100644 --- a/src/openrct2-ui/windows/Park.cpp +++ b/src/openrct2-ui/windows/Park.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -31,7 +32,6 @@ #include #include #include -#include #include namespace OpenRCT2::Ui::Windows diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index 90038c6eed..7825032de6 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include diff --git a/src/openrct2-ui/windows/TrackList.cpp b/src/openrct2-ui/windows/TrackList.cpp index 30da441681..60ef169386 100644 --- a/src/openrct2-ui/windows/TrackList.cpp +++ b/src/openrct2-ui/windows/TrackList.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include diff --git a/src/openrct2/core/UnitConversion.cpp b/src/openrct2/core/UnitConversion.cpp new file mode 100644 index 0000000000..2dcaf979ec --- /dev/null +++ b/src/openrct2/core/UnitConversion.cpp @@ -0,0 +1,78 @@ +/***************************************************************************** + * Copyright (c) 2014-2024 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#include "UnitConversion.h" + +namespace OpenRCT2 +{ + int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres) + { + // 1 metre squared = 10.7639104 feet squared + // RCT2 approximates as 11 + return squaredMetres * 11; + } + + int32_t MetresToFeet(int32_t metres) + { + // 1 metre = 3.2808399 feet + // RCT2 approximates as 3.28125 + return (metres * 840) / 256; + } + + int32_t FeetToMetres(int32_t feet) + { + return feet * 256 / 840; + } + + int32_t MphToKmph(int32_t mph) + { + // 1 mph = 1.60934 kmph + // RCT2 approximates as 1.609375 + return (mph * 1648) >> 10; + } + + int32_t MphToDmps(int32_t mph) + { + // 1 mph = 4.4704 decimeters/s + return (mph * 73243) >> 14; + } + + int32_t BaseZToMetres(int16_t baseZ) + { + return (baseZ / 2 - 7) * 1.5; + } + + uint8_t MetresToBaseZ(int16_t metres) + { + return ((metres / 1.5) + 7) * 2; + } + + int32_t HeightUnitsToMetres(int32_t heightUnit) + { + // 1 unit = 0.75 metres + return (heightUnit * 3) >> 2; + } + + int32_t ToHumanReadableSpeed(int32_t baseSpeed) + { + // Divide this value by 29127 to get the human-readable max speed + // (in RCT2, display_speed = (max_speed * 9) >> 18) + return (baseSpeed * 9) >> 18; + } + + uint16_t ToHumanReadableAirTime(uint16_t airTime) + { + return airTime * 3; + } + + int32_t ToHumanReadableRideLength(int32_t rideLength) + { + return rideLength >> 16; + } +} // namespace OpenRCT2 diff --git a/src/openrct2/core/UnitConversion.h b/src/openrct2/core/UnitConversion.h new file mode 100644 index 0000000000..89614ff0a0 --- /dev/null +++ b/src/openrct2/core/UnitConversion.h @@ -0,0 +1,27 @@ +/***************************************************************************** + * Copyright (c) 2014-2024 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include + +namespace OpenRCT2 +{ + int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres); + int32_t MetresToFeet(int32_t metres); + int32_t FeetToMetres(int32_t feet); + int32_t MphToKmph(int32_t mph); + int32_t MphToDmps(int32_t mph); + int32_t BaseZToMetres(int16_t baseZ); + uint8_t MetresToBaseZ(int16_t metres); + int32_t HeightUnitsToMetres(int32_t heightUnit); + int32_t ToHumanReadableSpeed(int32_t baseSpeed); + uint16_t ToHumanReadableAirTime(uint16_t airTime); + int32_t ToHumanReadableRideLength(int32_t rideLength); +} // namespace OpenRCT2 diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index c1268858a1..95e2371cd2 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -232,6 +232,7 @@ + @@ -771,6 +772,7 @@ + diff --git a/src/openrct2/localisation/Formatting.cpp b/src/openrct2/localisation/Formatting.cpp index 10cc645550..0002d55fca 100644 --- a/src/openrct2/localisation/Formatting.cpp +++ b/src/openrct2/localisation/Formatting.cpp @@ -13,9 +13,9 @@ #include "../Diagnostic.h" #include "../config/Config.h" #include "../core/String.hpp" +#include "../core/UnitConversion.h" #include "../object/ObjectManager.h" #include "../object/PeepNamesObject.h" -#include "../util/Util.h" #include "Currency.h" #include "FormatCodes.h" #include "Formatter.h" diff --git a/src/openrct2/ride/RideRatings.cpp b/src/openrct2/ride/RideRatings.cpp index 20226b1a4c..2a0a4e2540 100644 --- a/src/openrct2/ride/RideRatings.cpp +++ b/src/openrct2/ride/RideRatings.cpp @@ -13,6 +13,7 @@ #include "../Context.h" #include "../GameState.h" #include "../OpenRCT2.h" +#include "../core/UnitConversion.h" #include "../interface/Window.h" #include "../localisation/Localisation.Date.h" #include "../profiling/Profiling.h" diff --git a/src/openrct2/ride/TrackDesign.cpp b/src/openrct2/ride/TrackDesign.cpp index 677965d7ff..118283752a 100644 --- a/src/openrct2/ride/TrackDesign.cpp +++ b/src/openrct2/ride/TrackDesign.cpp @@ -35,6 +35,7 @@ #include "../core/File.h" #include "../core/Numerics.hpp" #include "../core/String.hpp" +#include "../core/UnitConversion.h" #include "../drawing/X8DrawingEngine.h" #include "../interface/Viewport.h" #include "../localisation/StringIds.h" diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index 5500175e3b..c99959915f 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -24,6 +24,7 @@ #include "../core/Guard.hpp" #include "../core/Path.hpp" #include "../core/Random.hpp" +#include "../core/UnitConversion.h" #include "../entity/Duck.h" #include "../entity/Guest.h" #include "../entity/Staff.h" diff --git a/src/openrct2/scripting/bindings/ride/ScRide.cpp b/src/openrct2/scripting/bindings/ride/ScRide.cpp index e50dda64a0..60566ead32 100644 --- a/src/openrct2/scripting/bindings/ride/ScRide.cpp +++ b/src/openrct2/scripting/bindings/ride/ScRide.cpp @@ -12,6 +12,7 @@ # include "ScRide.hpp" # include "../../../Context.h" +# include "../../../core/UnitConversion.h" # include "../../../ride/Ride.h" # include "../../../ride/RideData.h" # include "../../Duktape.hpp" diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index b82533016b..d6d24ac6ed 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -24,71 +24,6 @@ #include #include -int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres) -{ - // 1 metre squared = 10.7639104 feet squared - // RCT2 approximates as 11 - return squaredMetres * 11; -} - -int32_t MetresToFeet(int32_t metres) -{ - // 1 metre = 3.2808399 feet - // RCT2 approximates as 3.28125 - return (metres * 840) / 256; -} - -int32_t FeetToMetres(int32_t feet) -{ - return feet * 256 / 840; -} - -int32_t MphToKmph(int32_t mph) -{ - // 1 mph = 1.60934 kmph - // RCT2 approximates as 1.609375 - return (mph * 1648) >> 10; -} - -int32_t MphToDmps(int32_t mph) -{ - // 1 mph = 4.4704 decimeters/s - return (mph * 73243) >> 14; -} - -int32_t BaseZToMetres(int16_t baseZ) -{ - return (baseZ / 2 - 7) * 1.5; -} - -uint8_t MetresToBaseZ(int16_t metres) -{ - return ((metres / 1.5) + 7) * 2; -} - -int32_t HeightUnitsToMetres(int32_t heightUnit) -{ - // 1 unit = 0.75 metres - return (heightUnit * 3) >> 2; -} - -int32_t ToHumanReadableSpeed(int32_t baseSpeed) -{ - // Divide this value by 29127 to get the human-readable max speed - // (in RCT2, display_speed = (max_speed * 9) >> 18) - return (baseSpeed * 9) >> 18; -} - -uint16_t ToHumanReadableAirTime(uint16_t airTime) -{ - return airTime * 3; -} - -int32_t ToHumanReadableRideLength(int32_t rideLength) -{ - return rideLength >> 16; -} - /* Case insensitive logical compare */ // Example: // - Guest 10 diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index 94bea63235..e4fff3a7bb 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -19,18 +19,6 @@ #include #include -int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres); -int32_t MetresToFeet(int32_t metres); -int32_t FeetToMetres(int32_t feet); -int32_t MphToKmph(int32_t mph); -int32_t MphToDmps(int32_t mph); -int32_t BaseZToMetres(int16_t baseZ); -uint8_t MetresToBaseZ(int16_t metres); -int32_t HeightUnitsToMetres(int32_t heightUnit); -int32_t ToHumanReadableSpeed(int32_t baseSpeed); -uint16_t ToHumanReadableAirTime(uint16_t airTime); -int32_t ToHumanReadableRideLength(int32_t rideLength); - int32_t StrLogicalCmp(char const* a, char const* b); char* SafeStrCpy(char* destination, const char* source, size_t num); char* SafeStrCat(char* destination, const char* source, size_t size);