diff --git a/apps/esmtool/record.cpp b/apps/esmtool/record.cpp index b13668dafc..e293055919 100644 --- a/apps/esmtool/record.cpp +++ b/apps/esmtool/record.cpp @@ -1201,16 +1201,10 @@ namespace EsmTool std::cout << " Name: " << mData.mName << std::endl; std::cout << " Weather:" << std::endl; - std::cout << " Clear: " << (int)mData.mData.mClear << std::endl; - std::cout << " Cloudy: " << (int)mData.mData.mCloudy << std::endl; - std::cout << " Foggy: " << (int)mData.mData.mFoggy << std::endl; - std::cout << " Overcast: " << (int)mData.mData.mOvercast << std::endl; - std::cout << " Rain: " << (int)mData.mData.mOvercast << std::endl; - std::cout << " Thunder: " << (int)mData.mData.mThunder << std::endl; - std::cout << " Ash: " << (int)mData.mData.mAsh << std::endl; - std::cout << " Blight: " << (int)mData.mData.mBlight << std::endl; - std::cout << " Snow: " << (int)mData.mData.mSnow << std::endl; - std::cout << " Blizzard: " << (int)mData.mData.mBlizzard << std::endl; + std::array weathers + = { "Clear", "Cloudy", "Fog", "Overcast", "Rain", "Thunder", "Ash", "Blight", "Snow", "Blizzard" }; + for (size_t i = 0; i < weathers.size(); ++i) + std::cout << " " << weathers[i] << ": " << mData.mData.mProbabilities[i] << std::endl; std::cout << " Map Color: " << mData.mMapColor << std::endl; if (!mData.mSleepList.empty()) std::cout << " Sleep List: " << mData.mSleepList << std::endl; diff --git a/apps/opencs/model/tools/regioncheck.cpp b/apps/opencs/model/tools/regioncheck.cpp index 4affc1bd44..08e8a17714 100644 --- a/apps/opencs/model/tools/regioncheck.cpp +++ b/apps/opencs/model/tools/regioncheck.cpp @@ -47,9 +47,7 @@ void CSMTools::RegionCheckStage::perform(int stage, CSMDoc::Messages& messages) /// \todo test that the ID in mSleeplist exists // test that chances add up to 100 - int chances = region.mData.mClear + region.mData.mCloudy + region.mData.mFoggy + region.mData.mOvercast - + region.mData.mRain + region.mData.mThunder + region.mData.mAsh + region.mData.mBlight + region.mData.mSnow - + region.mData.mBlizzard; + auto chances = std::accumulate(region.mData.mProbabilities.begin(), region.mData.mProbabilities.end(), 0u); if (chances != 100) messages.add(id, "Weather chances do not add up to 100", "", CSMDoc::Message::Severity_Error); diff --git a/apps/opencs/model/world/nestedcoladapterimp.cpp b/apps/opencs/model/world/nestedcoladapterimp.cpp index 0f3670431d..1f9958e84e 100644 --- a/apps/opencs/model/world/nestedcoladapterimp.cpp +++ b/apps/opencs/model/world/nestedcoladapterimp.cpp @@ -1076,31 +1076,8 @@ namespace CSMWorld } else if (subColIndex == 1) { - switch (subRowIndex) - { - case 0: - return region.mData.mClear; - case 1: - return region.mData.mCloudy; - case 2: - return region.mData.mFoggy; - case 3: - return region.mData.mOvercast; - case 4: - return region.mData.mRain; - case 5: - return region.mData.mThunder; - case 6: - return region.mData.mAsh; - case 7: - return region.mData.mBlight; - case 8: - return region.mData.mSnow; - case 9: - return region.mData.mBlizzard; - default: - break; - } + if (subRowIndex >= 0 && subRowIndex < region.mData.mProbabilities.size()) + return region.mData.mProbabilities[subRowIndex]; } throw std::runtime_error("index out of range"); @@ -1110,45 +1087,11 @@ namespace CSMWorld Record& record, const QVariant& value, int subRowIndex, int subColIndex) const { ESM::Region region = record.get(); - unsigned char chance = static_cast(value.toInt()); + uint8_t chance = static_cast(value.toInt()); if (subColIndex == 1) { - switch (subRowIndex) - { - case 0: - region.mData.mClear = chance; - break; - case 1: - region.mData.mCloudy = chance; - break; - case 2: - region.mData.mFoggy = chance; - break; - case 3: - region.mData.mOvercast = chance; - break; - case 4: - region.mData.mRain = chance; - break; - case 5: - region.mData.mThunder = chance; - break; - case 6: - region.mData.mAsh = chance; - break; - case 7: - region.mData.mBlight = chance; - break; - case 8: - region.mData.mSnow = chance; - break; - case 9: - region.mData.mBlizzard = chance; - break; - default: - throw std::runtime_error("index out of range"); - } + region.mData.mProbabilities.at(subRowIndex) = chance; record.setModified(region); } diff --git a/apps/openmw/mwbase/world.hpp b/apps/openmw/mwbase/world.hpp index e5499f6680..14e3b2b3b7 100644 --- a/apps/openmw/mwbase/world.hpp +++ b/apps/openmw/mwbase/world.hpp @@ -232,7 +232,7 @@ namespace MWBase virtual void setMoonColour(bool red) = 0; - virtual void modRegion(const ESM::RefId& regionid, const std::vector& chances) = 0; + virtual void modRegion(const ESM::RefId& regionid, const std::vector& chances) = 0; virtual void changeToInteriorCell( std::string_view cellName, const ESM::Position& position, bool adjustPlayerPos, bool changeEvent = true) diff --git a/apps/openmw/mwscript/skyextensions.cpp b/apps/openmw/mwscript/skyextensions.cpp index 39944970bf..d2b41fb87a 100644 --- a/apps/openmw/mwscript/skyextensions.cpp +++ b/apps/openmw/mwscript/skyextensions.cpp @@ -102,11 +102,11 @@ namespace MWScript std::string_view region{ runtime.getStringLiteral(runtime[0].mInteger) }; runtime.pop(); - std::vector chances; + std::vector chances; chances.reserve(10); while (arg0 > 0) { - chances.push_back(std::clamp(runtime[0].mInteger, 0, 127)); + chances.push_back(std::clamp(runtime[0].mInteger, 0, 100)); runtime.pop(); arg0--; } diff --git a/apps/openmw/mwworld/weather.cpp b/apps/openmw/mwworld/weather.cpp index 3338acd832..0da70bdd48 100644 --- a/apps/openmw/mwworld/weather.cpp +++ b/apps/openmw/mwworld/weather.cpp @@ -285,19 +285,8 @@ namespace MWWorld RegionWeather::RegionWeather(const ESM::Region& region) : mWeather(invalidWeatherID) - , mChances() + , mChances(region.mData.mProbabilities.begin(), region.mData.mProbabilities.end()) { - mChances.reserve(10); - mChances.push_back(region.mData.mClear); - mChances.push_back(region.mData.mCloudy); - mChances.push_back(region.mData.mFoggy); - mChances.push_back(region.mData.mOvercast); - mChances.push_back(region.mData.mRain); - mChances.push_back(region.mData.mThunder); - mChances.push_back(region.mData.mAsh); - mChances.push_back(region.mData.mBlight); - mChances.push_back(region.mData.mSnow); - mChances.push_back(region.mData.mBlizzard); } RegionWeather::RegionWeather(const ESM::RegionWeatherState& state) @@ -313,19 +302,9 @@ namespace MWWorld return state; } - void RegionWeather::setChances(const std::vector& chances) + void RegionWeather::setChances(const std::vector& chances) { - if (mChances.size() < chances.size()) - { - mChances.reserve(chances.size()); - } - - int i = 0; - for (char chance : chances) - { - mChances[i] = chance; - i++; - } + mChances = chances; // Regional weather no longer supports the current type, select a new weather pattern. if ((static_cast(mWeather) >= mChances.size()) || (mChances[mWeather] == 0)) @@ -357,15 +336,14 @@ namespace MWWorld // If chances A and B has values 30 and 70 then by generating 100 numbers 1..100, 30% will be lesser or equal 30 // and 70% will be greater than 30 (in theory). auto& prng = MWBase::Environment::get().getWorld()->getPrng(); - int chance = Misc::Rng::rollDice(100, prng) + 1; // 1..100 - int sum = 0; - int i = 0; - for (; static_cast(i) < mChances.size(); ++i) + unsigned int chance = static_cast(Misc::Rng::rollDice(100, prng) + 1); // 1..100 + unsigned int sum = 0; + for (size_t i = 0; i < mChances.size(); ++i) { sum += mChances[i]; if (chance <= sum) { - mWeather = i; + mWeather = static_cast(i); return; } } @@ -649,7 +627,7 @@ namespace MWWorld } } - void WeatherManager::modRegion(const ESM::RefId& regionID, const std::vector& chances) + void WeatherManager::modRegion(const ESM::RefId& regionID, const std::vector& chances) { // Sets the region's probability for various weather patterns. Note that this appears to be saved permanently. // In Morrowind, this seems to have the following behavior when applied to the current region: diff --git a/apps/openmw/mwworld/weather.hpp b/apps/openmw/mwworld/weather.hpp index 57c69cde11..65f926c096 100644 --- a/apps/openmw/mwworld/weather.hpp +++ b/apps/openmw/mwworld/weather.hpp @@ -230,7 +230,7 @@ namespace MWWorld operator ESM::RegionWeatherState() const; - void setChances(const std::vector& chances); + void setChances(const std::vector& chances); void setWeather(int weatherID); @@ -238,7 +238,7 @@ namespace MWWorld private: int mWeather; - std::vector mChances; + std::vector mChances; void chooseNewWeather(); }; @@ -286,7 +286,7 @@ namespace MWWorld * @param ID of the weather setting to shift to */ void changeWeather(const ESM::RefId& regionID, const unsigned int weatherID); - void modRegion(const ESM::RefId& regionID, const std::vector& chances); + void modRegion(const ESM::RefId& regionID, const std::vector& chances); void playerTeleported(const ESM::RefId& playerRegion, bool isExterior); /** diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index b9bf454895..5be1e52530 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -1879,7 +1879,7 @@ namespace MWWorld mWeatherManager->changeWeather(region, id); } - void World::modRegion(const ESM::RefId& regionid, const std::vector& chances) + void World::modRegion(const ESM::RefId& regionid, const std::vector& chances) { mWeatherManager->modRegion(regionid, chances); } diff --git a/apps/openmw/mwworld/worldimp.hpp b/apps/openmw/mwworld/worldimp.hpp index 46043afe46..4b9a0ccb98 100644 --- a/apps/openmw/mwworld/worldimp.hpp +++ b/apps/openmw/mwworld/worldimp.hpp @@ -329,7 +329,7 @@ namespace MWWorld void setMoonColour(bool red) override; - void modRegion(const ESM::RefId& regionid, const std::vector& chances) override; + void modRegion(const ESM::RefId& regionid, const std::vector& chances) override; void changeToInteriorCell(const std::string_view cellName, const ESM::Position& position, bool adjustPlayerPos, bool changeEvent = true) override; diff --git a/components/esm3/loadregn.cpp b/components/esm3/loadregn.cpp index 3411d49a7c..5148a446c2 100644 --- a/components/esm3/loadregn.cpp +++ b/components/esm3/loadregn.cpp @@ -27,15 +27,14 @@ namespace ESM { esm.getSubHeader(); // Cold weather not included before 1.3 - if (esm.getSubSize() == sizeof(mData)) + if (esm.getSubSize() == mData.mProbabilities.size()) { - esm.getTSized<10>(mData); + esm.getT(mData.mProbabilities); } - else if (esm.getSubSize() == sizeof(mData) - 2) + else if (esm.getSubSize() == mData.mProbabilities.size() - 2) { - mData.mSnow = 0; - mData.mBlizzard = 0; - esm.getExact(&mData, sizeof(mData) - 2); + mData.mProbabilities.fill(0); + esm.getExact(&mData.mProbabilities, esm.getSubSize()); } else { @@ -85,9 +84,9 @@ namespace ESM esm.writeHNOCString("FNAM", mName); if (esm.getVersion() == VER_12) - esm.writeHNT("WEAT", mData, sizeof(mData) - 2); + esm.writeHNT("WEAT", mData.mProbabilities, mData.mProbabilities.size() - 2); else - esm.writeHNT("WEAT", mData); + esm.writeHNT("WEAT", mData.mProbabilities); esm.writeHNOCRefId("BNAM", mSleepList); @@ -104,8 +103,7 @@ namespace ESM void Region::blank() { mRecordFlags = 0; - mData.mClear = mData.mCloudy = mData.mFoggy = mData.mOvercast = mData.mRain = mData.mThunder = mData.mAsh - = mData.mBlight = mData.mSnow = mData.mBlizzard = 0; + mData.mProbabilities.fill(0); mMapColor = 0; diff --git a/components/esm3/loadregn.hpp b/components/esm3/loadregn.hpp index 00c5546e2f..757566a360 100644 --- a/components/esm3/loadregn.hpp +++ b/components/esm3/loadregn.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_REGN_H #define OPENMW_ESM_REGN_H +#include #include #include @@ -24,26 +25,24 @@ namespace ESM /// Return a string descriptor for this record type. Currently used for debugging / error logs only. static std::string_view getRecordType() { return "Region"; } -#pragma pack(push) -#pragma pack(1) struct WEATstruct { // These are probabilities that add up to 100 - unsigned char mClear, mCloudy, mFoggy, mOvercast, mRain, mThunder, mAsh, mBlight, mSnow, mBlizzard; + // Clear, Cloudy, Foggy, Overcast, Rain, Thunder, Ash, Blight, Snow, Blizzard + std::array mProbabilities; }; // 10 bytes -#pragma pack(pop) // Reference to a sound that is played randomly in this region struct SoundRef { ESM::RefId mSound; - unsigned char mChance; + uint8_t mChance; }; WEATstruct mData; - int mMapColor; // RGBA + int32_t mMapColor; // RGBA - unsigned int mRecordFlags; + uint32_t mRecordFlags; // sleepList refers to a leveled list of creatures you can meet if // you sleep outside in this region. RefId mId, mSleepList; diff --git a/components/esm3/weatherstate.cpp b/components/esm3/weatherstate.cpp index 4c6896d37e..a7a10fe624 100644 --- a/components/esm3/weatherstate.cpp +++ b/components/esm3/weatherstate.cpp @@ -41,7 +41,7 @@ namespace ESM esm.getHNT(region.mWeather, regionWeatherRecord); while (esm.isNextSub(regionChanceRecord)) { - char chance; + uint8_t chance; esm.getHT(chance); region.mChances.push_back(chance); } @@ -66,9 +66,9 @@ namespace ESM { esm.writeHNCRefId(regionNameRecord, it->first); esm.writeHNT(regionWeatherRecord, it->second.mWeather); - for (size_t i = 0; i < it->second.mChances.size(); ++i) + for (const uint8_t& chance : it->second.mChances) { - esm.writeHNT(regionChanceRecord, it->second.mChances[i]); + esm.writeHNT(regionChanceRecord, chance); } } } diff --git a/components/esm3/weatherstate.hpp b/components/esm3/weatherstate.hpp index 3fad90ddb4..625ba0ba6e 100644 --- a/components/esm3/weatherstate.hpp +++ b/components/esm3/weatherstate.hpp @@ -13,8 +13,8 @@ namespace ESM struct RegionWeatherState { - int mWeather; - std::vector mChances; + int32_t mWeather; + std::vector mChances; }; struct WeatherState @@ -24,9 +24,9 @@ namespace ESM bool mFastForward; float mWeatherUpdateTime; float mTransitionFactor; - int mCurrentWeather; - int mNextWeather; - int mQueuedWeather; + int32_t mCurrentWeather; + int32_t mNextWeather; + int32_t mQueuedWeather; std::map mRegions; void load(ESMReader& esm);