1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-07-01 08:11:34 +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/containerstore.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/mechanicsmanager.hpp"
@ -114,7 +115,8 @@ namespace MWGui
MWWorld::ContainerStore& store = source.first.getClass().getContainerStore(source.first);
if (item.mBase.getContainerStore() == &store)
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)

View file

@ -8,6 +8,7 @@
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwworld/inventorystore.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/mechanicsmanager.hpp"
@ -57,8 +58,9 @@ namespace MWGui
{
if (item.mBase.getContainerStore() == &mActor.getClass().getContainerStore(mActor))
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)

View file

@ -19,87 +19,91 @@ namespace
refValue = MWWorld::LiveCellRef<T>(cellRef, base);
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);
}
template <typename F>
void visitRefStore(const MWWorld::ESMStore& store, ESM::RefId name, F func)
{
switch (store.find(name))
{
case ESM::REC_ACTI:
return func(store.get<ESM::Activator>());
case ESM::REC_ALCH:
return func(store.get<ESM::Potion>());
case ESM::REC_APPA:
return func(store.get<ESM::Apparatus>());
case ESM::REC_ARMO:
return func(store.get<ESM::Armor>());
case ESM::REC_BOOK:
return func(store.get<ESM::Book>());
case ESM::REC_CLOT:
return func(store.get<ESM::Clothing>());
case ESM::REC_CONT:
return func(store.get<ESM::Container>());
case ESM::REC_CREA:
return func(store.get<ESM::Creature>());
case ESM::REC_DOOR:
return func(store.get<ESM::Door>());
case ESM::REC_INGR:
return func(store.get<ESM::Ingredient>());
case ESM::REC_LEVC:
return func(store.get<ESM::CreatureLevList>());
case ESM::REC_LEVI:
return func(store.get<ESM::ItemLevList>());
case ESM::REC_LIGH:
return func(store.get<ESM::Light>());
case ESM::REC_LOCK:
return func(store.get<ESM::Lockpick>());
case ESM::REC_MISC:
return func(store.get<ESM::Miscellaneous>());
case ESM::REC_NPC_:
return func(store.get<ESM::NPC>());
case ESM::REC_PROB:
return func(store.get<ESM::Probe>());
case ESM::REC_REPA:
return func(store.get<ESM::Repair>());
case ESM::REC_STAT:
return func(store.get<ESM::Static>());
case ESM::REC_WEAP:
return func(store.get<ESM::Weapon>());
case ESM::REC_BODY:
return func(store.get<ESM::BodyPart>());
case ESM::REC_STAT4:
return func(store.get<ESM4::Static>());
case ESM::REC_TERM4:
return func(store.get<ESM4::Terminal>());
case 0:
throw std::logic_error(
"failed to create manual cell ref for " + name.toDebugString() + " (unknown ID)");
default:
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)
{
switch (store.find(name))
{
case ESM::REC_ACTI:
create(store.get<ESM::Activator>(), name, mRef, mPtr);
break;
case ESM::REC_ALCH:
create(store.get<ESM::Potion>(), name, mRef, mPtr);
break;
case ESM::REC_APPA:
create(store.get<ESM::Apparatus>(), name, mRef, mPtr);
break;
case ESM::REC_ARMO:
create(store.get<ESM::Armor>(), name, mRef, mPtr);
break;
case ESM::REC_BOOK:
create(store.get<ESM::Book>(), name, mRef, mPtr);
break;
case ESM::REC_CLOT:
create(store.get<ESM::Clothing>(), name, mRef, mPtr);
break;
case ESM::REC_CONT:
create(store.get<ESM::Container>(), name, mRef, mPtr);
break;
case ESM::REC_CREA:
create(store.get<ESM::Creature>(), name, mRef, mPtr);
break;
case ESM::REC_DOOR:
create(store.get<ESM::Door>(), name, mRef, mPtr);
break;
case ESM::REC_INGR:
create(store.get<ESM::Ingredient>(), name, mRef, mPtr);
break;
case ESM::REC_LEVC:
create(store.get<ESM::CreatureLevList>(), name, mRef, mPtr);
break;
case ESM::REC_LEVI:
create(store.get<ESM::ItemLevList>(), name, mRef, mPtr);
break;
case ESM::REC_LIGH:
create(store.get<ESM::Light>(), name, mRef, mPtr);
break;
case ESM::REC_LOCK:
create(store.get<ESM::Lockpick>(), name, mRef, mPtr);
break;
case ESM::REC_MISC:
create(store.get<ESM::Miscellaneous>(), name, mRef, mPtr);
break;
case ESM::REC_NPC_:
create(store.get<ESM::NPC>(), name, mRef, mPtr);
break;
case ESM::REC_PROB:
create(store.get<ESM::Probe>(), name, mRef, mPtr);
break;
case ESM::REC_REPA:
create(store.get<ESM::Repair>(), name, mRef, mPtr);
break;
case ESM::REC_STAT:
create(store.get<ESM::Static>(), name, mRef, mPtr);
break;
case ESM::REC_WEAP:
create(store.get<ESM::Weapon>(), name, mRef, mPtr);
break;
case ESM::REC_BODY:
create(store.get<ESM::BodyPart>(), name, mRef, mPtr);
break;
case ESM::REC_STAT4:
create(store.get<ESM4::Static>(), name, mRef, mPtr);
break;
case ESM::REC_TERM4:
create(store.get<ESM4::Terminal>(), name, mRef, mPtr);
break;
case 0:
throw std::logic_error("failed to create manual cell ref for " + name.toDebugString() + " (unknown ID)");
default:
throw std::logic_error("failed to create manual cell ref for " + name.toDebugString() + " (unknown type)");
}
auto cb = [&](const auto& store) { create(store, name, mRef, mPtr); };
visitRefStore(store, name, cb);
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
{
/// \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
{
// Stores the ref (LiveCellRef<T>) by value.
std::any mRef;
Ptr mPtr;
@ -18,6 +20,7 @@ namespace MWWorld
public:
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; }
};