From 7113db8b9733f63d671381e791be5c9c4c8e5f9c Mon Sep 17 00:00:00 2001 From: Zackhasacat Date: Fri, 1 Sep 2023 20:33:42 +0000 Subject: [PATCH] Clear selected enchanted item and/or spell with actor.clearSelectedCastable() --- apps/openmw/mwlua/magicbindings.cpp | 35 +++++++++++++++++++++++++---- files/lua_api/openmw/types.lua | 5 +++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwlua/magicbindings.cpp b/apps/openmw/mwlua/magicbindings.cpp index d1dab50574..4d4f111ab1 100644 --- a/apps/openmw/mwlua/magicbindings.cpp +++ b/apps/openmw/mwlua/magicbindings.cpp @@ -24,6 +24,7 @@ #include "../mwmechanics/spellutil.hpp" #include "../mwworld/class.hpp" #include "../mwworld/esmstore.hpp" +#include "../mwworld/inventorystore.hpp" #include "../mwworld/worldmodel.hpp" #include "localscripts.hpp" @@ -615,17 +616,43 @@ namespace MWLua context.mLuaManager->addAction([obj = Object(ptr), spellId]() { const MWWorld::Ptr& ptr = obj.ptr(); auto& stats = ptr.getClass().getCreatureStats(ptr); + + if (spellId.empty()) + { + if (ptr == MWBase::Environment::get().getWorld()->getPlayerPtr()) + MWBase::Environment::get().getWindowManager()->unsetSelectedSpell(); + else + stats.getSpells().setSelectedSpell(ESM::RefId()); + return; + } if (!stats.getSpells().hasSpell(spellId)) throw std::runtime_error("Actor doesn't know spell " + spellId.toDebugString()); + if (ptr == MWBase::Environment::get().getWorld()->getPlayerPtr()) { - int chance = 0; - if (!spellId.empty()) - chance = MWMechanics::getSpellSuccessChance(spellId, ptr); + int chance = MWMechanics::getSpellSuccessChance(spellId, ptr); MWBase::Environment::get().getWindowManager()->setSelectedSpell(spellId, chance); } else - ptr.getClass().getCreatureStats(ptr).getSpells().setSelectedSpell(spellId); + stats.getSpells().setSelectedSpell(spellId); + }); + }; + + actor["clearSelectedCastable"] = [context](const SelfObject& o) { + if (!o.ptr().getClass().isActor()) + throw std::runtime_error("Actor expected"); + context.mLuaManager->addAction([obj = Object(o.ptr())]() { + const MWWorld::Ptr& ptr = obj.ptr(); + auto& stats = ptr.getClass().getCreatureStats(ptr); + if (ptr.getClass().hasInventoryStore(ptr)) + { + MWWorld::InventoryStore& inventory = ptr.getClass().getInventoryStore(ptr); + inventory.setSelectedEnchantItem(inventory.end()); + } + if (ptr == MWBase::Environment::get().getWorld()->getPlayerPtr()) + MWBase::Environment::get().getWindowManager()->unsetSelectedSpell(); + else + stats.getSpells().setSelectedSpell(ESM::RefId()); }); }; diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index ab913bc788..5137f61e8f 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -161,6 +161,11 @@ -- @param openmw.core#GameObject actor -- @param openmw.core#Spell spell Spell (can be nil) +--- +-- Clears the actor's selected castable(spell or enchanted item) +-- @function [parent=#Actor] clearSelectedCastable +-- @param openmw.core#GameObject actor + --- -- Get currently selected enchanted item -- @function [parent=#Actor] getSelectedEnchantedItem