From e2ae2904bee0fadcaddd41c7363d217ad073ace2 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sat, 22 Feb 2025 16:18:17 +0100 Subject: [PATCH] Add weather 'forecast' as preview --- data/language/en-GB.txt | 1 + .../windows/EditorObjectSelection.cpp | 2 + src/openrct2/localisation/StringIds.h | 1 + src/openrct2/object/ClimateObject.cpp | 49 ++++++++++++++++++- src/openrct2/object/ClimateObject.h | 7 ++- 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/data/language/en-GB.txt b/data/language/en-GB.txt index 6214a83538..a46ddd63d0 100644 --- a/data/language/en-GB.txt +++ b/data/language/en-GB.txt @@ -3802,3 +3802,4 @@ STR_6740 :{WINDOW_COLOUR_2}Cash: {BLACK}{CURRENCY2DP} STR_6741 :{WINDOW_COLOUR_2}Num. rides: {BLACK}{UINT16} STR_6742 :{WINDOW_COLOUR_2}Num. guests: {BLACK}{UINT16} STR_6743 :Climate +STR_6744 :{WINDOW_COLOUR_2}{UINT16}% diff --git a/src/openrct2-ui/windows/EditorObjectSelection.cpp b/src/openrct2-ui/windows/EditorObjectSelection.cpp index 79195175e1..a4fb3431b7 100644 --- a/src/openrct2-ui/windows/EditorObjectSelection.cpp +++ b/src/openrct2-ui/windows/EditorObjectSelection.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1268,6 +1269,7 @@ namespace OpenRCT2::Ui::Windows screenPos.y += DrawTextWrapped(dpi, screenPos, descriptionWidth, STR_WINDOW_COLOUR_2_STRINGID, ft); screenPos.y += kListRowHeight; } + if (GetSelectedObjectType() == ObjectType::ride) { auto* rideObject = reinterpret_cast(_loadedObject.get()); diff --git a/src/openrct2/localisation/StringIds.h b/src/openrct2/localisation/StringIds.h index 91427aef25..7de96cd76f 100644 --- a/src/openrct2/localisation/StringIds.h +++ b/src/openrct2/localisation/StringIds.h @@ -1725,6 +1725,7 @@ enum : StringId STR_DIVE_LOOP_RIGHT = 6728, STR_OBJECT_SELECTION_CLIMATE = 6743, + STR_CLIMATE_WEATHER_PERCENT = 6744, // Have to include resource strings (from scenarios and objects) for the time being now that language is partially working /* MAX_STR_COUNT = 32768 */ // MAX_STR_COUNT - upper limit for number of strings, not the current count strings diff --git a/src/openrct2/object/ClimateObject.cpp b/src/openrct2/object/ClimateObject.cpp index 29ae87b7d3..d17fa97def 100644 --- a/src/openrct2/object/ClimateObject.cpp +++ b/src/openrct2/object/ClimateObject.cpp @@ -13,6 +13,10 @@ #include "../core/Guard.hpp" #include "../core/IStream.hpp" #include "../core/Json.hpp" +#include "../localisation/Formatter.h" +#include "../localisation/StringIds.h" + +#include using namespace OpenRCT2; @@ -48,6 +52,26 @@ void ClimateObject::Unload() static RawClimate readWeatherTable(json_t& weather); static Climate convertRawClimate(const RawClimate& rawClimate); +void ClimateObject::DrawPreview(DrawPixelInfo& dpi, int32_t width, int32_t height) const +{ + const auto dist = getYearlyDistribution(); + const auto totalSize = kNumClimateMonths * kWeatherDistSize; + + for (auto i = 0u; i < EnumValue(WeatherType::Count); i++) + { + auto type = WeatherType(i); + auto imageId = ImageId(ClimateGetWeatherSpriteId(type)); + auto coords = ScreenCoordsXY(8 + (i % 3) * 35, 3 + (i / 3) * 37); + GfxDrawSprite(dpi, imageId, coords); + + auto ft = Formatter(); + ft.Add(dist[i] * 100 / totalSize); + DrawTextEllipsised( + dpi, coords + ScreenCoordsXY{ 12, 22 }, 35, STR_CLIMATE_WEATHER_PERCENT, ft, + { FontStyle::Small, TextAlignment::CENTRE }); + } +} + void ClimateObject::ReadJson(IReadObjectContext* context, json_t& root) { Guard::Assert(root.is_object(), "ClimateObject::ReadJson expects parameter root to be an object"); @@ -58,11 +82,34 @@ void ClimateObject::ReadJson(IReadObjectContext* context, json_t& root) _climate = convertRawClimate(rawClimate); } -const WeatherPattern& ClimateObject::getPatternForMonth(uint8_t month) +const WeatherPattern& ClimateObject::getPatternForMonth(uint8_t month) const { return _climate[month]; } +YearlyDistribution ClimateObject::getYearlyDistribution() const +{ + auto weatherTypeCount = [](const WeatherPattern& pattern, const WeatherType target) { + auto count = 0u; + for (auto type : pattern.distribution) + { + if (type == target) + count++; + } + return count; + }; + + YearlyDistribution dist{}; + for (auto m = 0; m < kNumClimateMonths; m++) + { + auto& pattern = getPatternForMonth(m); + for (auto i = 0u; i < EnumValue(WeatherType::Count); i++) + dist[i] += weatherTypeCount(pattern, WeatherType(i)); + } + + return dist; +} + static Climate convertRawClimate(const RawClimate& rawClimate) { Climate climate{}; diff --git a/src/openrct2/object/ClimateObject.h b/src/openrct2/object/ClimateObject.h index 40e61ea58a..3a965b8563 100644 --- a/src/openrct2/object/ClimateObject.h +++ b/src/openrct2/object/ClimateObject.h @@ -14,6 +14,8 @@ struct IReadObjectContext; +using YearlyDistribution = std::array; + class ClimateObject final : public Object { private: @@ -26,5 +28,8 @@ public: void Load() override; void Unload() override; - const WeatherPattern& getPatternForMonth(uint8_t month); + void DrawPreview(DrawPixelInfo& dpi, int32_t width, int32_t height) const override; + + const WeatherPattern& getPatternForMonth(uint8_t month) const; + YearlyDistribution getYearlyDistribution() const; };