1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-14 19:21:50 +00:00
OpenMW/apps/openmw/mwlua/weatherbindings.cpp
Sebastian Fieber ef687fb3eb optional next
2025-01-26 23:39:04 +01:00

88 lines
5.2 KiB
C++

#include "weatherbindings.hpp"
#include <components/esm3/loadregn.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwworld/esmstore.hpp"
#include "../mwworld/weather.hpp"
namespace MWLua
{
sol::table initWeatherBindings(const Context& context)
{
sol::state_view lua = context.sol();
sol::table api(lua, sol::create);
auto weatherT = lua.new_usertype<MWWorld::Weather>("Weather");
weatherT[sol::meta_function::to_string]
= [](const MWWorld::Weather& w) -> std::string { return "Weather[" + w.mName + "]"; };
weatherT["name"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mName; });
weatherT["windSpeed"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mWindSpeed; });
weatherT["cloudSpeed"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mCloudSpeed; });
weatherT["cloudTexture"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mCloudTexture; });
weatherT["cloudsMaximumPercent"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mCloudsMaximumPercent; });
weatherT["isStorm"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mIsStorm; });
weatherT["stormDirection"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mStormDirection; });
weatherT["glareView"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mGlareView; });
weatherT["rainSpeed"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainSpeed; });
weatherT["rainEntranceSpeed"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainEntranceSpeed; });
weatherT["rainEffect"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainEffect; });
weatherT["rainMaxRaindrops"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainMaxRaindrops; });
weatherT["rainDiameter"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainDiameter; });
weatherT["rainThreshold"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainThreshold; });
weatherT["rainMaxHeight"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainMaxHeight; });
weatherT["rainMinHeight"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainMinHeight; });
weatherT["rainLoopSoundID"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mRainLoopSoundID; });
weatherT["thunderSoundID"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mThunderSoundID; });
weatherT["ambientLoopSoundID"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mAmbientLoopSoundID; });
weatherT["landForDepth"] = sol::readonly_property([](const MWWorld::Weather& w) { return w.mLandFogDepth; });
weatherT["particleEffect"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mParticleEffect; });
weatherT["distantLandFogFactor"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mDL.FogFactor; });
weatherT["distandLandForOffset"]
= sol::readonly_property([](const MWWorld::Weather& w) { return w.mDL.FogOffset; });
api["getCurrent"] = []() { return *MWBase::Environment::get().getWorld()->getCurrentWeather(); };
api["getNext"] = []() -> sol::optional<MWWorld::Weather> {
auto next = MWBase::Environment::get().getWorld()->getNextWeather();
if (next == nullptr)
return sol::nullopt;
return *next;
};
api["getTransition"] = []() { return MWBase::Environment::get().getWorld()->getWeatherTransition(); };
// TODO these return values that already take weather transition into account - maybe good to have?
api["getCurrentSunVisibility"] = []() { return MWBase::Environment::get().getWorld()->getSunVisibility(); };
api["getCurrentSunPercentage"] = []() { return MWBase::Environment::get().getWorld()->getSunPercentage(); };
api["getCurrentWindSpeed"] = []() { return MWBase::Environment::get().getWorld()->getWindSpeed(); };
api["getCurrentStormDirection"] = []() { return MWBase::Environment::get().getWorld()->getStormDirection(); };
api["changeWeather"] = [](std::string_view regionId, const MWWorld::Weather& weather) {
ESM::RefId region = ESM::RefId::stringRefId(regionId);
const ESM::Region* reg = MWBase::Environment::get().getESMStore()->get<ESM::Region>().search(region);
if (reg)
MWBase::Environment::get().getWorld()->changeWeather(region, weather.mId);
else
throw std::runtime_error("Region not found");
};
api["getAllWeather"] = [context]() {
sol::table allWeather(context.sol(), sol::create);
for (const auto& weather : MWBase::Environment::get().getWorld()->getAllWeather())
allWeather.add(weather);
return allWeather;
};
return LuaUtil::makeReadOnly(api);
}
}