diff --git a/apps/openmw/mwclass/misc.cpp b/apps/openmw/mwclass/misc.cpp index 53c3bc4bcb..4e165d6526 100644 --- a/apps/openmw/mwclass/misc.cpp +++ b/apps/openmw/mwclass/misc.cpp @@ -201,30 +201,14 @@ namespace MWClass return newPtr; } - std::pair Miscellaneous::canBeEquipped(const MWWorld::ConstPtr &ptr, const MWWorld::Ptr &npc) const - { - // Special checks for soul gems. - - if (::Misc::StringUtils::ciEqualPrefix("misc_soulgem", ptr.getCellRef().getRefId())) - { - if (npc != MWBase::Environment::get().getWorld()->getPlayerPtr()) - return std::make_pair(0, "Only the player can use soul gems."); - - if (ptr.getCellRef().getSoul().empty()) - return std::make_pair(0, "#{sNotifyMessage32}"); - - if (!MWBase::Environment::get().getWorld()->getStore().get().search(ptr.getCellRef().getSoul())) - return std::make_pair(0, "Unknown soul creature id."); - } - - return std::make_pair(1, ""); - } - std::unique_ptr Miscellaneous::use (const MWWorld::Ptr& ptr, bool force) const { - if (ptr.getCellRef().getSoul().empty() || !MWBase::Environment::get().getWorld()->getStore().get().search(ptr.getCellRef().getSoul())) - return std::make_unique(); - return std::make_unique(ptr); + const char soulgem_prefix[] = "misc_soulgem"; + + if (::Misc::StringUtils::ciCompareLen(ptr.getCellRef().getRefId(), soulgem_prefix, sizeof(soulgem_prefix) - 1) == 0) + return std::make_unique(ptr); + + return std::make_unique(); } bool Miscellaneous::canSell (const MWWorld::ConstPtr& item, int npcServices) const diff --git a/apps/openmw/mwclass/misc.hpp b/apps/openmw/mwclass/misc.hpp index 95d861955d..ebf38b10be 100644 --- a/apps/openmw/mwclass/misc.hpp +++ b/apps/openmw/mwclass/misc.hpp @@ -45,10 +45,6 @@ namespace MWClass std::string getModel(const MWWorld::ConstPtr &ptr) const override; - std::pair canBeEquipped(const MWWorld::ConstPtr &ptr, const MWWorld::Ptr &npc) const override; - ///< Return 0 if player cannot equip item. 1 if can equip. 2 if it's twohanded weapon. 3 if twohanded weapon conflicts with that. \n - /// Second item in the pair specifies the error message - std::unique_ptr use (const MWWorld::Ptr& ptr, bool force=false) const override; ///< Generate action for using via inventory menu diff --git a/apps/openmw/mwgui/inventorywindow.cpp b/apps/openmw/mwgui/inventorywindow.cpp index f464eed88f..f7cbc30ae1 100644 --- a/apps/openmw/mwgui/inventorywindow.cpp +++ b/apps/openmw/mwgui/inventorywindow.cpp @@ -549,19 +549,6 @@ namespace MWGui } } - // Separate check for the soul gems. - else if (::Misc::StringUtils::ciEqualPrefix("misc_soulgem", ptr.getCellRef().getRefId())) - { - std::pair canEquip = ptr.getClass().canBeEquipped(ptr, player); - - if (canEquip.first == 0) - { - MWBase::Environment::get().getWindowManager()->messageBox(canEquip.second); - updateItemView(); - return; - } - } - // If the item has a script, set OnPCEquip or PCSkipEquip to 1 if (!script.empty()) { diff --git a/apps/openmw/mwscript/containerextensions.cpp b/apps/openmw/mwscript/containerextensions.cpp index 1fafbbd5e8..1bbaa9b60f 100644 --- a/apps/openmw/mwscript/containerextensions.cpp +++ b/apps/openmw/mwscript/containerextensions.cpp @@ -307,7 +307,9 @@ namespace MWScript // With soul gems we prefer filled ones. - if (::Misc::StringUtils::ciEqualPrefix("misc_soulgem", item)) + const char soulgem_prefix[] = "misc_soulgem"; + + if (::Misc::StringUtils::ciCompareLen(item, soulgem_prefix, sizeof(soulgem_prefix) - 1) == 0) { it = invStore.end(); diff --git a/apps/openmw/mwworld/actionsoulgem.cpp b/apps/openmw/mwworld/actionsoulgem.cpp index f7823dabaa..72efa6d4ec 100644 --- a/apps/openmw/mwworld/actionsoulgem.cpp +++ b/apps/openmw/mwworld/actionsoulgem.cpp @@ -1,10 +1,15 @@ #include "actionsoulgem.hpp" +#include + #include "../mwbase/windowmanager.hpp" #include "../mwbase/environment.hpp" +#include "../mwbase/world.hpp" #include "../mwmechanics/actorutil.hpp" +#include "../mwworld/esmstore.hpp" + namespace MWWorld { @@ -23,7 +28,25 @@ namespace MWWorld MWBase::Environment::get().getWindowManager()->messageBox("#{sInventoryMessage5}"); return; } - MWBase::Environment::get().getWindowManager()->showSoulgemDialog(getTarget()); + + const auto& target = getTarget(); + const auto& targetSoul = target.getCellRef().getSoul(); + + if (targetSoul.empty()) + { + MWBase::Environment::get().getWindowManager()->messageBox("#{sNotifyMessage32}"); + return; + } + + if (!MWBase::Environment::get().getWorld()->getStore().get().search(targetSoul)) + { + const auto& message = "Unknown soul creature id \"" + targetSoul + "\"."; + Log(Debug::Warning) << message; + MWBase::Environment::get().getWindowManager()->messageBox(message); + return; + } + + MWBase::Environment::get().getWindowManager()->showSoulgemDialog(target); } diff --git a/components/misc/stringops.hpp b/components/misc/stringops.hpp index db1ed65719..3fa82a27fe 100644 --- a/components/misc/stringops.hpp +++ b/components/misc/stringops.hpp @@ -97,34 +97,6 @@ public: return ciEqual(x, std::string_view(y, n - 1)); } - template - static bool ciEqualPrefix(const X& x, const Y& y) - { - return std::equal(std::begin(x), std::end(x), std::begin(y), - [] (char l, char r) { return toLower(l) == toLower(r); }); - } - - template - static auto ciEqualPrefix(const char(& x)[n], const char(& y)[n]) - { - static_assert(n > 0); - return ciEqualPrefix(std::string_view(x, n - 1), std::string_view(y, n - 1)); - } - - template - static auto ciEqualPrefix(const char(& x)[n], const T& y) - { - static_assert(n > 0); - return ciEqualPrefix(std::string_view(x, n - 1), y); - } - - template - static auto ciEqualPrefix(const T& x, const char(& y)[n]) - { - static_assert(n > 0); - return ciEqualPrefix(x, std::string_view(y, n - 1)); - } - static int ciCompareLen(std::string_view x, std::string_view y, std::size_t len) { std::string_view::const_iterator xit = x.begin();