mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-25 06:35:30 +00:00
use ciCompareLen, move checks to Miscellaneous::use and ActionSoulgem
This commit is contained in:
parent
8817f802cb
commit
3698d96b46
@ -201,30 +201,14 @@ namespace MWClass
|
||||
return newPtr;
|
||||
}
|
||||
|
||||
std::pair<int, std::string> 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<ESM::Creature>().search(ptr.getCellRef().getSoul()))
|
||||
return std::make_pair(0, "Unknown soul creature id.");
|
||||
}
|
||||
|
||||
return std::make_pair(1, "");
|
||||
}
|
||||
|
||||
std::unique_ptr<MWWorld::Action> Miscellaneous::use (const MWWorld::Ptr& ptr, bool force) const
|
||||
{
|
||||
if (ptr.getCellRef().getSoul().empty() || !MWBase::Environment::get().getWorld()->getStore().get<ESM::Creature>().search(ptr.getCellRef().getSoul()))
|
||||
return std::make_unique<MWWorld::NullAction>();
|
||||
return std::make_unique<MWWorld::ActionSoulgem>(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<MWWorld::ActionSoulgem>(ptr);
|
||||
|
||||
return std::make_unique<MWWorld::NullAction>();
|
||||
}
|
||||
|
||||
bool Miscellaneous::canSell (const MWWorld::ConstPtr& item, int npcServices) const
|
||||
|
@ -45,10 +45,6 @@ namespace MWClass
|
||||
|
||||
std::string getModel(const MWWorld::ConstPtr &ptr) const override;
|
||||
|
||||
std::pair<int, std::string> 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<MWWorld::Action> use (const MWWorld::Ptr& ptr, bool force=false) const override;
|
||||
///< Generate action for using via inventory menu
|
||||
|
||||
|
@ -549,19 +549,6 @@ namespace MWGui
|
||||
}
|
||||
}
|
||||
|
||||
// Separate check for the soul gems.
|
||||
else if (::Misc::StringUtils::ciEqualPrefix("misc_soulgem", ptr.getCellRef().getRefId()))
|
||||
{
|
||||
std::pair<int, std::string> 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())
|
||||
{
|
||||
|
@ -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();
|
||||
|
||||
|
@ -1,10 +1,15 @@
|
||||
#include "actionsoulgem.hpp"
|
||||
|
||||
#include <components/debug/debuglog.hpp>
|
||||
|
||||
#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<ESM::Creature>().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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -97,34 +97,6 @@ public:
|
||||
return ciEqual(x, std::string_view(y, n - 1));
|
||||
}
|
||||
|
||||
template <class X, class Y>
|
||||
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 <std::size_t n>
|
||||
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 <std::size_t n, class T>
|
||||
static auto ciEqualPrefix(const char(& x)[n], const T& y)
|
||||
{
|
||||
static_assert(n > 0);
|
||||
return ciEqualPrefix(std::string_view(x, n - 1), y);
|
||||
}
|
||||
|
||||
template <std::size_t n, class T>
|
||||
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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user