From c7e364c8ddd6875aaef2ac9b5b0bc5023cd1d0b3 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Tue, 4 Feb 2025 16:20:08 +0100 Subject: [PATCH] Use patterns from climate objects instead of hardcoded internals --- src/openrct2/object/ClimateObject.cpp | 5 ++ src/openrct2/object/ClimateObject.h | 2 + src/openrct2/world/Climate.cpp | 97 +++++++-------------------- src/openrct2/world/Climate.h | 6 +- 4 files changed, 36 insertions(+), 74 deletions(-) diff --git a/src/openrct2/object/ClimateObject.cpp b/src/openrct2/object/ClimateObject.cpp index a4e521f246..31c1439687 100644 --- a/src/openrct2/object/ClimateObject.cpp +++ b/src/openrct2/object/ClimateObject.cpp @@ -57,6 +57,11 @@ void ClimateObject::ReadJson(IReadObjectContext* context, json_t& root) _climate = convertRawClimate(rawClimate); } +const WeatherPattern& ClimateObject::getPatternForMonth(uint8_t month) +{ + return _climate[month]; +} + static Climate convertRawClimate(const RawClimate& rawClimate) { Climate climate{}; diff --git a/src/openrct2/object/ClimateObject.h b/src/openrct2/object/ClimateObject.h index 1e07f6fff2..40e61ea58a 100644 --- a/src/openrct2/object/ClimateObject.h +++ b/src/openrct2/object/ClimateObject.h @@ -25,4 +25,6 @@ public: void ReadJson(IReadObjectContext* context, json_t& root) override; void Load() override; void Unload() override; + + const WeatherPattern& getPatternForMonth(uint8_t month); }; diff --git a/src/openrct2/world/Climate.cpp b/src/openrct2/world/Climate.cpp index 317ddcef1f..5e3ceb4dd3 100644 --- a/src/openrct2/world/Climate.cpp +++ b/src/openrct2/world/Climate.cpp @@ -21,6 +21,8 @@ #include "../config/Config.h" #include "../core/EnumUtils.hpp" #include "../drawing/Drawing.h" +#include "../object/ClimateObject.h" +#include "../object/ObjectManager.h" #include "../profiling/Profiling.h" #include "../util/Util.h" #include "../windows/Intent.h" @@ -39,13 +41,6 @@ enum class ThunderStatus playing, }; -struct WeatherPattern -{ - int8_t baseTemperature; - int8_t randomBias; - WeatherType distribution[24]; -}; - struct WeatherTrait { int8_t temperatureDelta; @@ -56,7 +51,6 @@ struct WeatherTrait }; // TODO: no need for these to be declared extern, just move the definitions up -extern const WeatherPattern* kClimatePatterns[4]; extern const WeatherTrait kClimateWeatherTraits[EnumValue(WeatherType::Count)]; extern const FilterPaletteID kClimateWeatherGloomColours[4]; @@ -95,10 +89,15 @@ int32_t ClimateCelsiusToFahrenheit(int32_t celsius) */ void ClimateReset(ClimateType climate) { - auto weather = WeatherType::PartiallyCloudy; - int32_t month = GetDate().GetMonth(); + auto& objManager = GetContext()->GetObjectManager(); + auto* climateObj = objManager.GetLoadedObject(0); + if (climateObj == nullptr) + return; - const WeatherPattern& pattern = kClimatePatterns[EnumValue(climate)][month]; + int32_t month = GetDate().GetMonth(); + const WeatherPattern& pattern = climateObj->getPatternForMonth(month); + + auto weather = WeatherType::PartiallyCloudy; const WeatherTrait& trait = kClimateWeatherTraits[EnumValue(weather)]; auto& gameState = GetGameState(); @@ -208,12 +207,17 @@ void ClimateUpdate() void ClimateForceWeather(WeatherType weather) { - auto& gameState = GetGameState(); - int32_t month = GetDate().GetMonth(); + auto& objManager = GetContext()->GetObjectManager(); + auto* climateObj = objManager.GetLoadedObject(0); + if (climateObj == nullptr) + return; + + int32_t month = GetDate().GetMonth(); + const WeatherPattern& pattern = climateObj->getPatternForMonth(month); - const auto& pattern = kClimatePatterns[EnumValue(gameState.Climate)][month]; const auto& trait = kClimateWeatherTraits[EnumValue(weather)]; + auto& gameState = GetGameState(); gameState.WeatherCurrent.weatherType = weather; gameState.WeatherCurrent.weatherGloom = trait.gloomLevel; gameState.WeatherCurrent.level = trait.level; @@ -303,14 +307,20 @@ static int8_t ClimateStepWeatherLevel(int8_t currentWeatherLevel, int8_t nextWea */ static void ClimateDetermineFutureWeather(uint32_t randomValue) { + auto& objManager = GetContext()->GetObjectManager(); + auto* climateObj = objManager.GetLoadedObject(0); + if (climateObj == nullptr) + return; + int32_t month = GetDate().GetMonth(); - auto& gameState = GetGameState(); + const WeatherPattern& pattern = climateObj->getPatternForMonth(month); // Generate a random index with values 0 up to randomBias-1 // and choose weather from the distribution table accordingly - const auto& pattern = kClimatePatterns[EnumValue(gameState.Climate)][month]; const auto randomIndex = ((randomValue % 256) * pattern.randomBias) / 256; const auto nextWeather = pattern.distribution[randomIndex]; + + auto& gameState = GetGameState(); gameState.WeatherNext.weatherType = nextWeather; const auto& nextWeatherTrait = kClimateWeatherTraits[EnumValue(nextWeather)]; @@ -482,59 +492,4 @@ const WeatherTrait kClimateWeatherTraits[EnumValue(WeatherType::Count)] = { }; // clang-format on -constexpr auto S = WeatherType::Sunny; -constexpr auto P = WeatherType::PartiallyCloudy; -constexpr auto C = WeatherType::Cloudy; -constexpr auto R = WeatherType::Rain; -constexpr auto H = WeatherType::HeavyRain; -constexpr auto T = WeatherType::Thunder; - -static constexpr WeatherPattern kClimatePatternsCoolAndWet[] = { - { 8, 18, { S, P, P, P, P, P, C, C, C, C, C, C, C, R, R, R, H, H, S, S, S, S, S } }, - { 10, 21, { P, P, P, P, P, C, C, C, C, C, C, C, C, C, R, R, R, H, H, H, T, S, S } }, - { 14, 17, { S, S, S, P, P, P, P, P, P, C, C, C, C, R, R, R, H, S, S, S, S, S, S } }, - { 17, 17, { S, S, S, S, P, P, P, P, P, P, P, C, C, C, C, R, R, S, S, S, S, S, S } }, - { 19, 23, { S, S, S, S, S, S, S, S, S, S, P, P, P, P, P, P, C, C, C, C, C, R, H } }, - { 20, 23, { S, S, S, S, S, S, P, P, P, P, P, P, P, P, C, C, C, C, R, H, H, H, T } }, - { 16, 19, { S, S, S, P, P, P, P, P, C, C, C, C, C, C, R, R, H, H, T, S, S, S, S } }, - { 13, 16, { S, S, P, P, P, P, C, C, C, C, C, C, R, R, H, T, S, S, S, S, S, S, S } }, -}; -static constexpr WeatherPattern kClimatePatternsWarm[] = { - { 12, 21, { S, S, S, S, S, P, P, P, P, P, P, P, P, C, C, C, C, C, C, C, H, S, S } }, - { 13, 22, { S, S, S, S, S, P, P, P, P, P, P, C, C, C, C, C, C, C, C, C, R, T, S } }, - { 16, 17, { S, S, S, S, S, S, P, P, P, P, P, P, C, C, C, C, R, S, S, S, S, S, S } }, - { 19, 18, { S, S, S, S, S, S, P, P, P, P, P, P, P, C, C, C, C, R, S, S, S, S, S } }, - { 21, 22, { S, S, S, S, S, S, S, S, S, S, P, P, P, P, P, P, P, P, P, C, C, C, S } }, - { 22, 17, { S, S, S, S, S, S, S, S, S, P, P, P, P, P, C, C, T, S, S, S, S, S, S } }, - { 19, 17, { S, S, S, S, S, P, P, P, P, P, C, C, C, C, C, C, R, S, S, S, S, S, S } }, - { 16, 17, { S, S, P, P, P, P, P, C, C, C, C, C, C, C, C, C, H, S, S, S, S, S, S } }, -}; -static constexpr WeatherPattern kClimatePatternsHotAndDry[] = { - { 12, 15, { S, S, S, S, P, P, P, P, P, P, P, P, C, C, R, S, S, S, S, S, S, S, S } }, - { 14, 12, { S, S, S, S, S, P, P, P, P, P, C, C, S, S, S, S, S, S, S, S, S, S, S } }, - { 16, 11, { S, S, S, S, S, S, P, P, P, P, C, S, S, S, S, S, S, S, S, S, S, S, S } }, - { 19, 9, { S, S, S, S, S, S, P, P, P, S, S, S, S, S, S, S, S, S, S, S, S, S, S } }, - { 21, 13, { S, S, S, S, S, S, S, S, S, S, P, P, P, S, S, S, S, S, S, S, S, S, S } }, - { 22, 11, { S, S, S, S, S, S, S, S, S, P, P, S, S, S, S, S, S, S, S, S, S, S, S } }, - { 21, 12, { S, S, S, S, S, S, S, P, P, P, C, T, S, S, S, S, S, S, S, S, S, S, S } }, - { 16, 13, { S, S, S, S, S, S, S, S, P, P, P, C, R, S, S, S, S, S, S, S, S, S, S } }, -}; -static constexpr WeatherPattern kClimatePatternsCold[] = { - { 4, 18, { S, S, S, S, P, P, P, P, P, C, C, C, C, C, C, C, R, H, S, S, S, S, S } }, - { 5, 21, { S, S, S, S, P, P, P, P, P, C, C, C, C, C, C, C, C, C, R, H, T, S, S } }, - { 7, 17, { S, S, S, S, P, P, P, P, P, P, P, C, C, C, C, R, H, S, S, S, S, S, S } }, - { 9, 17, { S, S, S, S, P, P, P, P, P, P, P, C, C, C, C, R, R, S, S, S, S, S, S } }, - { 10, 23, { S, S, S, S, S, S, S, S, S, S, P, P, P, P, P, P, C, C, C, C, C, R, H } }, - { 11, 23, { S, S, S, S, S, S, P, P, P, P, P, P, P, P, P, P, C, C, C, C, R, H, T } }, - { 9, 19, { S, S, S, S, S, P, P, P, P, P, C, C, C, C, C, C, R, H, T, S, S, S, S } }, - { 6, 16, { S, S, P, P, P, P, C, C, C, C, C, C, R, R, H, T, S, S, S, S, S, S, S } }, -}; - -const WeatherPattern* kClimatePatterns[] = { - kClimatePatternsCoolAndWet, - kClimatePatternsWarm, - kClimatePatternsHotAndDry, - kClimatePatternsCold, -}; - #pragma endregion diff --git a/src/openrct2/world/Climate.h b/src/openrct2/world/Climate.h index 292e7f348e..0518dc0e0b 100644 --- a/src/openrct2/world/Climate.h +++ b/src/openrct2/world/Climate.h @@ -42,14 +42,14 @@ static constexpr auto kNumWeatherTypes = EnumValue(WeatherType::Count); static constexpr auto kNumClimateMonths = 8; static constexpr auto kWeatherDistSize = 23; -struct ClimateMonth +struct WeatherPattern { int8_t baseTemperature; int8_t randomBias; - WeatherType distribution[kWeatherDistSize]{}; + WeatherType distribution[kWeatherDistSize]; }; -using Climate = std::array; +using Climate = std::array; enum class WeatherEffectType : uint8_t {