1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-12-14 12:13:07 +00:00

Merge branch 'soulless' into 'master'

Properly copy items between containers

See merge request OpenMW/openmw!4139
This commit is contained in:
psi29a 2024-06-10 13:49:51 +00:00
commit 3ef4180464
4 changed files with 92 additions and 81 deletions

View file

@ -7,6 +7,7 @@
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp" #include "../mwworld/containerstore.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/mechanicsmanager.hpp" #include "../mwbase/mechanicsmanager.hpp"
@ -114,7 +115,8 @@ namespace MWGui
MWWorld::ContainerStore& store = source.first.getClass().getContainerStore(source.first); MWWorld::ContainerStore& store = source.first.getClass().getContainerStore(source.first);
if (item.mBase.getContainerStore() == &store) if (item.mBase.getContainerStore() == &store)
throw std::runtime_error("Item to copy needs to be from a different container!"); throw std::runtime_error("Item to copy needs to be from a different container!");
return *store.add(item.mBase.getCellRef().getRefId(), count, allowAutoEquip); MWWorld::ManualRef newRef(*MWBase::Environment::get().getESMStore(), item.mBase, count);
return *store.add(newRef.getPtr(), count, allowAutoEquip);
} }
void ContainerItemModel::removeItem(const ItemStack& item, size_t count) void ContainerItemModel::removeItem(const ItemStack& item, size_t count)

View file

@ -8,6 +8,7 @@
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp" #include "../mwworld/containerstore.hpp"
#include "../mwworld/inventorystore.hpp" #include "../mwworld/inventorystore.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/mechanicsmanager.hpp" #include "../mwbase/mechanicsmanager.hpp"
@ -57,8 +58,9 @@ namespace MWGui
{ {
if (item.mBase.getContainerStore() == &mActor.getClass().getContainerStore(mActor)) if (item.mBase.getContainerStore() == &mActor.getClass().getContainerStore(mActor))
throw std::runtime_error("Item to copy needs to be from a different container!"); throw std::runtime_error("Item to copy needs to be from a different container!");
return *mActor.getClass().getContainerStore(mActor).add(
item.mBase.getCellRef().getRefId(), count, allowAutoEquip); MWWorld::ManualRef newRef(*MWBase::Environment::get().getESMStore(), item.mBase, count);
return *mActor.getClass().getContainerStore(mActor).add(newRef.getPtr(), count, allowAutoEquip);
} }
void InventoryItemModel::removeItem(const ItemStack& item, size_t count) void InventoryItemModel::removeItem(const ItemStack& item, size_t count)

View file

@ -19,87 +19,91 @@ namespace
refValue = MWWorld::LiveCellRef<T>(cellRef, base); refValue = MWWorld::LiveCellRef<T>(cellRef, base);
ptrValue = MWWorld::Ptr(&std::any_cast<MWWorld::LiveCellRef<T>&>(refValue), nullptr); ptrValue = MWWorld::Ptr(&std::any_cast<MWWorld::LiveCellRef<T>&>(refValue), nullptr);
} }
template <typename T>
void create(
const MWWorld::Store<T>& list, const MWWorld::Ptr& templatePtr, std::any& refValue, MWWorld::Ptr& ptrValue)
{
refValue = *static_cast<MWWorld::LiveCellRef<T>*>(templatePtr.getBase());
ptrValue = MWWorld::Ptr(&std::any_cast<MWWorld::LiveCellRef<T>&>(refValue), nullptr);
} }
MWWorld::ManualRef::ManualRef(const MWWorld::ESMStore& store, const ESM::RefId& name, const int count) template <typename F>
void visitRefStore(const MWWorld::ESMStore& store, ESM::RefId name, F func)
{ {
switch (store.find(name)) switch (store.find(name))
{ {
case ESM::REC_ACTI: case ESM::REC_ACTI:
create(store.get<ESM::Activator>(), name, mRef, mPtr); return func(store.get<ESM::Activator>());
break;
case ESM::REC_ALCH: case ESM::REC_ALCH:
create(store.get<ESM::Potion>(), name, mRef, mPtr); return func(store.get<ESM::Potion>());
break;
case ESM::REC_APPA: case ESM::REC_APPA:
create(store.get<ESM::Apparatus>(), name, mRef, mPtr); return func(store.get<ESM::Apparatus>());
break;
case ESM::REC_ARMO: case ESM::REC_ARMO:
create(store.get<ESM::Armor>(), name, mRef, mPtr); return func(store.get<ESM::Armor>());
break;
case ESM::REC_BOOK: case ESM::REC_BOOK:
create(store.get<ESM::Book>(), name, mRef, mPtr); return func(store.get<ESM::Book>());
break;
case ESM::REC_CLOT: case ESM::REC_CLOT:
create(store.get<ESM::Clothing>(), name, mRef, mPtr); return func(store.get<ESM::Clothing>());
break;
case ESM::REC_CONT: case ESM::REC_CONT:
create(store.get<ESM::Container>(), name, mRef, mPtr); return func(store.get<ESM::Container>());
break;
case ESM::REC_CREA: case ESM::REC_CREA:
create(store.get<ESM::Creature>(), name, mRef, mPtr); return func(store.get<ESM::Creature>());
break;
case ESM::REC_DOOR: case ESM::REC_DOOR:
create(store.get<ESM::Door>(), name, mRef, mPtr); return func(store.get<ESM::Door>());
break;
case ESM::REC_INGR: case ESM::REC_INGR:
create(store.get<ESM::Ingredient>(), name, mRef, mPtr); return func(store.get<ESM::Ingredient>());
break;
case ESM::REC_LEVC: case ESM::REC_LEVC:
create(store.get<ESM::CreatureLevList>(), name, mRef, mPtr); return func(store.get<ESM::CreatureLevList>());
break;
case ESM::REC_LEVI: case ESM::REC_LEVI:
create(store.get<ESM::ItemLevList>(), name, mRef, mPtr); return func(store.get<ESM::ItemLevList>());
break;
case ESM::REC_LIGH: case ESM::REC_LIGH:
create(store.get<ESM::Light>(), name, mRef, mPtr); return func(store.get<ESM::Light>());
break;
case ESM::REC_LOCK: case ESM::REC_LOCK:
create(store.get<ESM::Lockpick>(), name, mRef, mPtr); return func(store.get<ESM::Lockpick>());
break;
case ESM::REC_MISC: case ESM::REC_MISC:
create(store.get<ESM::Miscellaneous>(), name, mRef, mPtr); return func(store.get<ESM::Miscellaneous>());
break;
case ESM::REC_NPC_: case ESM::REC_NPC_:
create(store.get<ESM::NPC>(), name, mRef, mPtr); return func(store.get<ESM::NPC>());
break;
case ESM::REC_PROB: case ESM::REC_PROB:
create(store.get<ESM::Probe>(), name, mRef, mPtr); return func(store.get<ESM::Probe>());
break;
case ESM::REC_REPA: case ESM::REC_REPA:
create(store.get<ESM::Repair>(), name, mRef, mPtr); return func(store.get<ESM::Repair>());
break;
case ESM::REC_STAT: case ESM::REC_STAT:
create(store.get<ESM::Static>(), name, mRef, mPtr); return func(store.get<ESM::Static>());
break;
case ESM::REC_WEAP: case ESM::REC_WEAP:
create(store.get<ESM::Weapon>(), name, mRef, mPtr); return func(store.get<ESM::Weapon>());
break;
case ESM::REC_BODY: case ESM::REC_BODY:
create(store.get<ESM::BodyPart>(), name, mRef, mPtr); return func(store.get<ESM::BodyPart>());
break;
case ESM::REC_STAT4: case ESM::REC_STAT4:
create(store.get<ESM4::Static>(), name, mRef, mPtr); return func(store.get<ESM4::Static>());
break;
case ESM::REC_TERM4: case ESM::REC_TERM4:
create(store.get<ESM4::Terminal>(), name, mRef, mPtr); return func(store.get<ESM4::Terminal>());
break;
case 0: case 0:
throw std::logic_error("failed to create manual cell ref for " + name.toDebugString() + " (unknown ID)"); throw std::logic_error(
"failed to create manual cell ref for " + name.toDebugString() + " (unknown ID)");
default: default:
throw std::logic_error("failed to create manual cell ref for " + name.toDebugString() + " (unknown type)"); throw std::logic_error(
"failed to create manual cell ref for " + name.toDebugString() + " (unknown type)");
} }
}
}
MWWorld::ManualRef::ManualRef(const MWWorld::ESMStore& store, const ESM::RefId& name, const int count)
{
auto cb = [&](const auto& store) { create(store, name, mRef, mPtr); };
visitRefStore(store, name, cb);
mPtr.getCellRef().setCount(count); mPtr.getCellRef().setCount(count);
} }
MWWorld::ManualRef::ManualRef(const ESMStore& store, const Ptr& template_, const int count)
{
auto cb = [&](const auto& store) { create(store, template_, mRef, mPtr); };
visitRefStore(store, template_.getCellRef().getRefId(), cb);
mPtr.getCellRef().setCount(count);
mPtr.getCellRef().unsetRefNum();
mPtr.getRefData().setLuaScripts(nullptr);
}

View file

@ -7,9 +7,11 @@
namespace MWWorld namespace MWWorld
{ {
/// \brief Manually constructed live cell ref /// \brief Manually constructed live cell ref. The resulting Ptr shares its lifetime with this ManualRef and must
/// not be used past its end.
class ManualRef class ManualRef
{ {
// Stores the ref (LiveCellRef<T>) by value.
std::any mRef; std::any mRef;
Ptr mPtr; Ptr mPtr;
@ -18,6 +20,7 @@ namespace MWWorld
public: public:
ManualRef(const MWWorld::ESMStore& store, const ESM::RefId& name, const int count = 1); ManualRef(const MWWorld::ESMStore& store, const ESM::RefId& name, const int count = 1);
ManualRef(const MWWorld::ESMStore& store, const MWWorld::Ptr& template_, const int count = 1);
const Ptr& getPtr() const { return mPtr; } const Ptr& getPtr() const { return mPtr; }
}; };