From b8a9fcad68eee11cc6ca25f486ff4cc21708edaf Mon Sep 17 00:00:00 2001 From: Mads Buvik Sandvei Date: Mon, 17 Jul 2023 17:06:28 +0200 Subject: [PATCH] fixes based on comments by ptmikheev --- apps/openmw/mwgui/inventoryitemmodel.cpp | 9 -------- apps/openmw/mwgui/inventoryitemmodel.hpp | 1 - apps/openmw/mwgui/itemmodel.cpp | 12 +---------- apps/openmw/mwgui/itemmodel.hpp | 5 ----- apps/openmw/mwworld/class.cpp | 10 +++++++++ apps/openmw/mwworld/class.hpp | 1 + apps/openmw/mwworld/worldimp.cpp | 27 ++++++------------------ apps/openmw/mwworld/worldimp.hpp | 2 +- 8 files changed, 20 insertions(+), 47 deletions(-) diff --git a/apps/openmw/mwgui/inventoryitemmodel.cpp b/apps/openmw/mwgui/inventoryitemmodel.cpp index 9ba7ad6a1f..1bd51acc1e 100644 --- a/apps/openmw/mwgui/inventoryitemmodel.cpp +++ b/apps/openmw/mwgui/inventoryitemmodel.cpp @@ -53,15 +53,6 @@ namespace MWGui return *mActor.getClass().getContainerStore(mActor).add(item.mBase, count, allowAutoEquip); } - MWWorld::Ptr InventoryItemModel::unstackItem(const ItemStack& item, size_t count) - { - MWWorld::ContainerStore& store = mActor.getClass().getContainerStore(mActor); - auto it = store.unstack(item.mBase, count); - if (it != store.end()) - return *it; - return MWWorld::Ptr(); - } - void InventoryItemModel::removeItem(const ItemStack& item, size_t count) { int removed = 0; diff --git a/apps/openmw/mwgui/inventoryitemmodel.hpp b/apps/openmw/mwgui/inventoryitemmodel.hpp index 373f1ecd3a..5310016d74 100644 --- a/apps/openmw/mwgui/inventoryitemmodel.hpp +++ b/apps/openmw/mwgui/inventoryitemmodel.hpp @@ -18,7 +18,6 @@ namespace MWGui bool onTakeItem(const MWWorld::Ptr& item, int count) override; MWWorld::Ptr addItem(const ItemStack& item, size_t count, bool allowAutoEquip = true) override; - MWWorld::Ptr unstackItem(const ItemStack& item, size_t count) override; void removeItem(const ItemStack& item, size_t count) override; /// Move items from this model to \a otherModel. diff --git a/apps/openmw/mwgui/itemmodel.cpp b/apps/openmw/mwgui/itemmodel.cpp index a9edae34b7..23b777e91f 100644 --- a/apps/openmw/mwgui/itemmodel.cpp +++ b/apps/openmw/mwgui/itemmodel.cpp @@ -67,16 +67,11 @@ namespace MWGui ret.getCellRef().unsetRefNum(); ret.getRefData().setLuaScripts(nullptr); MWBase::Environment::get().getWorldModel()->registerPtr(ret); + MWBase::Environment::get().getWorldModel()->registerPtr(item.mBase); } return ret; } - MWWorld::Ptr ItemModel::unstackItem(const ItemStack& item, size_t count) - { - // By default does nothing - return MWWorld::Ptr(); - } - bool ItemModel::allowedToUseItems() const { return true; @@ -151,11 +146,6 @@ namespace MWGui return mSourceModel->onTakeItem(item, count); } - MWWorld::Ptr ProxyItemModel::unstackItem(const ItemStack& item, size_t count) - { - return mSourceModel->unstackItem(item, count); - } - MWWorld::Ptr ProxyItemModel::addItem(const ItemStack& item, size_t count, bool allowAutoEquip) { return mSourceModel->addItem(item, count, allowAutoEquip); diff --git a/apps/openmw/mwgui/itemmodel.hpp b/apps/openmw/mwgui/itemmodel.hpp index f7e6cf01f1..78bfd508f0 100644 --- a/apps/openmw/mwgui/itemmodel.hpp +++ b/apps/openmw/mwgui/itemmodel.hpp @@ -66,10 +66,6 @@ namespace MWGui virtual MWWorld::Ptr moveItem( const ItemStack& item, size_t count, ItemModel* otherModel, bool allowAutoEquip = true); - /// Unstacks items from this model and returns a ptr to the new remainder stack. - /// @note Returns en empty ptr if there is no remainder or the item model does not support unstacking. - virtual MWWorld::Ptr unstackItem(const ItemStack& item, size_t count); - virtual MWWorld::Ptr addItem(const ItemStack& item, size_t count, bool allowAutoEquip = true) = 0; virtual void removeItem(const ItemStack& item, size_t count) = 0; @@ -100,7 +96,6 @@ namespace MWGui bool onDropItem(const MWWorld::Ptr& item, int count) override; bool onTakeItem(const MWWorld::Ptr& item, int count) override; - MWWorld::Ptr unstackItem(const ItemStack& item, size_t count) override; MWWorld::Ptr addItem(const ItemStack& item, size_t count, bool allowAutoEquip = true) override; void removeItem(const ItemStack& item, size_t count) override; ModelIndex getIndex(const ItemStack& item) override; diff --git a/apps/openmw/mwworld/class.cpp b/apps/openmw/mwworld/class.cpp index 03e624a608..28d351902b 100644 --- a/apps/openmw/mwworld/class.cpp +++ b/apps/openmw/mwworld/class.cpp @@ -391,10 +391,20 @@ namespace MWWorld return newPtr; } + Ptr Class::moveToCell(const Ptr& ptr, CellStore& cell, const ESM::Position& pos, int count) const + { + Ptr newPtr = moveToCell(ptr, cell); + newPtr.getRefData().setPosition(pos); + newPtr.getCellRef().setPosition(pos); + newPtr.getRefData().setCount(count); + return newPtr; + } + MWWorld::Ptr Class::copyToCell(const ConstPtr& ptr, CellStore& cell, const ESM::Position& pos, int count) const { Ptr newPtr = copyToCell(ptr, cell, count); newPtr.getRefData().setPosition(pos); + newPtr.getCellRef().setPosition(pos); return newPtr; } diff --git a/apps/openmw/mwworld/class.hpp b/apps/openmw/mwworld/class.hpp index fbd2b98a74..9aa72be041 100644 --- a/apps/openmw/mwworld/class.hpp +++ b/apps/openmw/mwworld/class.hpp @@ -317,6 +317,7 @@ namespace MWWorld // The original is expected to be removed after calling this function, // but this function itself doesn't remove the original. virtual Ptr moveToCell(const Ptr& ptr, CellStore& cell) const; + Ptr moveToCell(const Ptr& ptr, CellStore& cell, const ESM::Position& pos, int count) const; Ptr copyToCell(const ConstPtr& ptr, CellStore& cell, const ESM::Position& pos, int count) const; diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 23b507f4dc..5285f3567b 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -2063,7 +2063,7 @@ namespace MWWorld MWWorld::Ptr dropped = object.getClass().copyToCell(object, *cell, pos, count); - addObjectToCell(dropped, cell, pos, adjustPos); + initObjectInCell(dropped, *cell, adjustPos); return dropped; } @@ -2079,30 +2079,16 @@ namespace MWWorld cell = &mWorldModel.getExterior(index); } - MWWorld::Ptr dropped = object.getClass().moveToCell(object, *cell); - dropped.getRefData().setCount(count); - dropped.getRefData().setPosition(pos); + MWWorld::Ptr dropped = object.getClass().moveToCell(object, *cell, pos, count); - addObjectToCell(dropped, cell, pos, adjustPos); + initObjectInCell(dropped, *cell, adjustPos); return dropped; } - void World::addObjectToCell(const Ptr& object, CellStore* cell, ESM::Position pos, bool adjustPos) + void World::initObjectInCell(const Ptr& object, CellStore& cell, bool adjustPos) { - if (!cell) - throw std::runtime_error("addObjectToCell(): cannot add object to null cell"); - if (cell->isExterior()) - { - const ESM::ExteriorCellLocation index - = ESM::positionToExteriorCellLocation(pos.pos[0], pos.pos[1], cell->getCell()->getWorldSpace()); - cell = &mWorldModel.getExterior(index); - } - - // Reset some position values that could be uninitialized if this item came from a container - object.getCellRef().setPosition(pos); - - if (mWorldScene->isCellActive(*cell)) + if (mWorldScene->isCellActive(cell)) { if (object.getRefData().isEnabled()) { @@ -2113,7 +2099,7 @@ namespace MWWorld { mLocalScripts.add(script, object); } - addContainerScripts(object, cell); + addContainerScripts(object, &cell); } if (!object.getClass().isActor() && adjustPos && object.getRefData().getBaseNode()) @@ -2125,6 +2111,7 @@ namespace MWWorld osg::BoundingBox bounds = computeBounds.getBoundingBox(); if (bounds.valid()) { + ESM::Position pos = object.getRefData().getPosition(); bounds.set(bounds._min - pos.asVec3(), bounds._max - pos.asVec3()); osg::Vec3f adjust( diff --git a/apps/openmw/mwworld/worldimp.hpp b/apps/openmw/mwworld/worldimp.hpp index b2ea5986fa..30d3102cc2 100644 --- a/apps/openmw/mwworld/worldimp.hpp +++ b/apps/openmw/mwworld/worldimp.hpp @@ -143,7 +143,7 @@ namespace MWWorld void updateWeather(float duration, bool paused = false); - void addObjectToCell(const Ptr& ptr, CellStore* cell, ESM::Position pos, bool adjustPos); + void initObjectInCell(const Ptr& ptr, CellStore& cell, bool adjustPos); Ptr moveObjectToCell(const Ptr& ptr, CellStore* cell, ESM::Position pos, int count, bool adjustPos); Ptr copyObjectToCell(const ConstPtr& ptr, CellStore* cell, ESM::Position pos, int count, bool adjustPos);