Skip to content

An OpenMW mod that lets you play the whole Elder Scrolls III: Morrowind as a top-down isometric game — with full online co-op or MMO modes.

C++

Containers: ones right next to the one opened open with it, as one window

Alan Saunders9aeb372e5efe
diff --git a/apps/openmw/mwgui/container.cpp b/apps/openmw/mwgui/container.cpp
index 2b106d7..bfcaf7c 100644
--- a/apps/openmw/mwgui/container.cpp
+++ b/apps/openmw/mwgui/container.cpp
@@ -1,8 +1,15 @@
#include "container.hpp"
+#include <algorithm>
+#include <cmath>
+
#include <MyGUI_Button.h>
#include <MyGUI_InputManager.h>
+#include <osg/Vec2f>
+#include <osg/Vec3f>
+
+#include <components/esm3/loadcont.hpp>
#include <components/settings/values.hpp>
#include "../mwbase/environment.hpp"
@@ -17,6 +24,7 @@
#include "../mwmp/Networking.hpp"
#include "../mwmp/ObjectList.hpp"
+#include "../mwworld/cellstore.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/inventorystore.hpp"
@@ -37,6 +45,77 @@
#include "sortfilteritemmodel.hpp"
#include "tooltips.hpp"
+namespace
+{
+ // Isowind: how near other containers have to be to the one opened (from where each stands) to open with it as
+ // one: crates stacked on it or right beside it, a row of barrels
+ constexpr float sNearbyContainerRange = 128.f; // across
+ constexpr float sNearbyContainerHeight = 256.f; // above or below
+
+ /// Isowind: the ownership rule for containers opening together: taking from \a other must be no different, as far
+ /// as the law goes, from taking from \a opened (the same owner, or both unowned, and the same faction and rank)
+ bool haveSameOwner(const MWWorld::Ptr& opened, const MWWorld::Ptr& other)
+ {
+ const MWWorld::CellRef& openedRef = opened.getCellRef();
+ const MWWorld::CellRef& otherRef = other.getCellRef();
+ if (openedRef.getOwner() != otherRef.getOwner() || openedRef.getFaction() != otherRef.getFaction()
+ || openedRef.getGlobalVariable() != otherRef.getGlobalVariable())
+ return false;
+ if (!openedRef.getFaction().empty() && openedRef.getFactionRank() != otherRef.getFactionRank())
+ return false;
+ // The evidence chest is never free to take from, whoever owns it (see MechanicsManager::isAllowedToUse)
+ return (openedRef.getRefId() == "stolen_goods") == (otherRef.getRefId() == "stolen_goods");
+ }
+
+ /// Isowind: whether \a other opens with \a opened, as one container
+ bool opensWith(const MWWorld::Ptr& opened, const MWWorld::Ptr& other)
+ {
+ if (other == opened || other.getType() != ESM::REC_CONT || other.getCell() != opened.getCell())
+ return false;
+ if (other.mRef->isDeleted() || !other.getRefData().isEnabled())
+ return false;
+
+ const osg::Vec3f offset
+ = other.getRefData().getPosition().asVec3() - opened.getRefData().getPosition().asVec3();
+ if (std::abs(offset.z()) > sNearbyContainerHeight
+ || osg::Vec2f(offset.x(), offset.y()).length() > sNearbyContainerRange)
+ return false;
+
+ // Ones that have to be opened by themselves: locked, trapped, or with a script (a quest's OnActivate)
+ const MWWorld::CellRef& cellRef = other.getCellRef();
+ if (cellRef.isLocked() || !cellRef.getTrap().empty() || !other.getClass().getScript(other).empty())
+ return false;
+ // Plants (harvested by themselves) and ones that refill
+ if (other.get<ESM::Container>()->mBase->mFlags & (ESM::Container::Organic | ESM::Container::Respawn))
+ return false;
+
+ return haveSameOwner(opened, other);
+ }
+
+ /// Isowind: the containers that open with \a opened (nearest first)
+ std::vector<MWWorld::Ptr> findNearbyContainers(const MWWorld::Ptr& opened)
+ {
+ std::vector<MWWorld::Ptr> found;
+ // Only a container (not a body), and not a plant
+ if (opened.getType() != ESM::REC_CONT || !opened.isInCell()
+ || (opened.get<ESM::Container>()->mBase->mFlags & ESM::Container::Organic))
+ return found;
+
+ opened.getCell()->forEachType<ESM::Container>([&](const MWWorld::Ptr& other) {
+ if (opensWith(opened, other))
+ found.push_back(other);
+ return true;
+ });
+
+ const osg::Vec3f position = opened.getRefData().getPosition().asVec3();
+ std::sort(found.begin(), found.end(), [&](const MWWorld::Ptr& left, const MWWorld::Ptr& right) {
+ return (left.getRefData().getPosition().asVec3() - position).length2()
+ < (right.getRefData().getPosition().asVec3() - position).length2();
+ });
+ return found;
+ }
+}
+
namespace MWGui
{
@@ -46,6 +125,7 @@ namespace MWGui
, mItemTransfer(&itemTransfer)
, mSortModel(nullptr)
, mModel(nullptr)
+ , mMergedModel(nullptr)
, mSelectedItem(-1)
, mUpdateNextFrame(false)
, mTreatNextOpenAsLoot(false)
@@ -126,10 +206,12 @@ namespace MWGui
return;
// Isowind multiplayer: the server decides; the drag starts when it agrees (ObjectList::editContainers)
+ // (asked of each container the items are in, when the window shows several)
if (mwmp::hooks::isMultiplayer())
{
- mwmp::hooks::sendContainerChange(mPtr, mwmp::BaseObjectList::REMOVE, mwmp::BaseObjectList::DRAG,
- { { item.mBase, static_cast<int>(count) } });
+ for (const auto& [container, items] : splitBySource({ { item.mBase, static_cast<int>(count) } }))
+ mwmp::hooks::sendContainerChange(
+ container, mwmp::BaseObjectList::REMOVE, mwmp::BaseObjectList::DRAG, items);
return;
}
@@ -195,6 +277,8 @@ namespace MWGui
bool loot = mPtr.getClass().isActor() && mPtr.getClass().getCreatureStats(mPtr).isDead();
+ mNearbyContainers.clear();
+ mMergedModel = nullptr;
std::unique_ptr<ItemModel> model;
if (mPtr.getClass().hasInventoryStore(mPtr))
{
@@ -209,7 +293,19 @@ namespace MWGui
}
else
{
- model = std::make_unique<ContainerItemModel>(container);
+ // Isowind: crates stacked on this one or right beside it (hard to tell apart from above) open with it, as
+ // one; which ones is up to opensWith
+ mNearbyContainers = findNearbyContainers(mPtr);
+ if (mNearbyContainers.empty())
+ model = std::make_unique<ContainerItemModel>(container);
+ else
+ {
+ std::vector<MWWorld::Ptr> sources{ container };
+ sources.insert(sources.end(), mNearbyContainers.begin(), mNearbyContainers.end());
+ auto mergedModel = std::make_unique<ContainerItemModel>(sources);
+ mMergedModel = mergedModel.get();
+ model = std::move(mergedModel);
+ }
}
mDisposeCorpseButton->setVisible(loot);
@@ -231,6 +327,8 @@ namespace MWGui
mItemView->setModel(nullptr);
mModel = nullptr;
mSortModel = nullptr;
+ mMergedModel = nullptr;
+ mNearbyContainers.clear();
}
void ContainerWindow::onOpen()
@@ -283,9 +381,12 @@ namespace MWGui
break;
items.emplace_back(item.mBase, static_cast<int>(item.mCount));
}
- mwmp::hooks::sendContainerChange(
- mPtr, mwmp::BaseObjectList::REMOVE, mwmp::BaseObjectList::TAKE_ALL, items);
- if (items.empty())
+ // (from each container the window shows; the first reply closes it)
+ const auto bySource = splitBySource(items);
+ for (const auto& [container, containerItems] : bySource)
+ mwmp::hooks::sendContainerChange(
+ container, mwmp::BaseObjectList::REMOVE, mwmp::BaseObjectList::TAKE_ALL, containerItems);
+ if (bySource.empty())
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Container);
return;
}
@@ -459,6 +560,13 @@ namespace MWGui
{
checkReferenceAvailable();
+ // Isowind: one of those opened with it went away (disabled, deleted): the window shows what's left
+ if (!mPtr.isEmpty()
+ && std::any_of(mNearbyContainers.begin(), mNearbyContainers.end(), [](const MWWorld::Ptr& container) {
+ return container.mRef->isDeleted() || !container.getRefData().isEnabled();
+ }))
+ setPtr(mPtr);
+
if (mUpdateNextFrame)
{
mItemView->update();
@@ -468,7 +576,9 @@ namespace MWGui
void ContainerWindow::onInventoryUpdate(const MWWorld::Ptr& ptr)
{
- if (ptr == mPtr)
+ // (or one of those opened with it)
+ if (ptr == mPtr
+ || std::find(mNearbyContainers.begin(), mNearbyContainers.end(), ptr) != mNearbyContainers.end())
mUpdateNextFrame = true;
}
@@ -477,6 +587,39 @@ namespace MWGui
return mDragAndDrop->mIsOnDragAndDrop;
}
+ void ContainerWindow::onContainerChanged(const MWWorld::Ptr& container)
+ {
+ // (not opened afresh on that one: it may be one of those opened with the window's own; the window may have lost
+ // its own, a body being disposed of)
+ setPtr(mPtr.isEmpty() ? container : mPtr);
+ }
+
+ std::vector<std::pair<MWWorld::Ptr, ContainerWindow::ItemCounts>> ContainerWindow::splitBySource(
+ const ItemCounts& items)
+ {
+ std::vector<std::pair<MWWorld::Ptr, ItemCounts>> bySource;
+ auto add = [&](const MWWorld::Ptr& container, const MWWorld::Ptr& item, int count) {
+ auto found = std::find_if(
+ bySource.begin(), bySource.end(), [&](const auto& entry) { return entry.first == container; });
+ if (found == bySource.end())
+ found = bySource.emplace(bySource.end(), container, ItemCounts());
+ found->second.emplace_back(item, count);
+ };
+
+ for (const auto& [item, count] : items)
+ {
+ if (mMergedModel == nullptr)
+ add(mPtr, item, count);
+ else
+ {
+ // (each container's own item: the server is told how many of it there are)
+ for (const ContainerItemModel::SourceItem& source : mMergedModel->getSourceItems(item, count))
+ add(source.mContainer, source.mItem, source.mCount);
+ }
+ }
+ return bySource;
+ }
+
bool ContainerWindow::dragItemByPtr(const MWWorld::Ptr& itemPtr, int dragCount)
{
for (size_t i = 0; i < mModel->getItemCount(); ++i)
diff --git a/apps/openmw/mwgui/container.hpp b/apps/openmw/mwgui/container.hpp
index 0f07a22..a1be676 100644
--- a/apps/openmw/mwgui/container.hpp
+++ b/apps/openmw/mwgui/container.hpp
@@ -1,6 +1,9 @@
#ifndef MGUI_CONTAINER_H
#define MGUI_CONTAINER_H
+#include <utility>
+#include <vector>
+
#include "itemmodel.hpp"
#include "referenceinterface.hpp"
#include "windowbase.hpp"
@@ -16,6 +19,7 @@ namespace MyGUI
namespace MWGui
{
class ContainerWindow;
+ class ContainerItemModel;
class ItemView;
class SortFilterItemModel;
class ItemTransfer;
@@ -56,14 +60,23 @@ namespace MWGui
/// Starts dragging this item, once the server has approved it (its index may have changed meanwhile).
bool dragItemByPtr(const MWWorld::Ptr& itemPtr, int dragCount);
ItemModel* getModel() { return mModel; }
+ /// The server changed this container, which the window shows: shows the window's containers afresh (the
+ /// one it was opened on, and those opened with it)
+ void onContainerChanged(const MWWorld::Ptr& container);
private:
+ using ItemCounts = std::vector<std::pair<MWWorld::Ptr, int>>;
+
Misc::NotNullPtr<DragAndDrop> mDragAndDrop;
Misc::NotNullPtr<ItemTransfer> mItemTransfer;
MWGui::ItemView* mItemView;
SortFilterItemModel* mSortModel;
ItemModel* mModel;
+ /// Isowind: containers right by the one opened that open with it, as one (see setPtr), and the model showing
+ /// them all (null when there are none)
+ std::vector<MWWorld::Ptr> mNearbyContainers;
+ ContainerItemModel* mMergedModel;
int mSelectedItem;
bool mUpdateNextFrame;
bool mTreatNextOpenAsLoot;
@@ -80,6 +93,9 @@ namespace MWGui
void onTakeAllButtonClicked(MyGUI::Widget* sender);
void onDisposeCorpseButtonClicked(MyGUI::Widget* sender);
+ /// Isowind multiplayer: items taken, by the container each comes out of
+ std::vector<std::pair<MWWorld::Ptr, ItemCounts>> splitBySource(const ItemCounts& items);
+
void onReferenceUnavailable() override;
};
}
diff --git a/apps/openmw/mwgui/containeritemmodel.cpp b/apps/openmw/mwgui/containeritemmodel.cpp
index b865702..2d77eff 100644
--- a/apps/openmw/mwgui/containeritemmodel.cpp
+++ b/apps/openmw/mwgui/containeritemmodel.cpp
@@ -64,6 +64,41 @@ namespace MWGui
mItemSources.emplace_back(source, store.resolveTemporarily());
}
+ ContainerItemModel::ContainerItemModel(const std::vector<MWWorld::Ptr>& itemSources)
+ : mTrading(false)
+ {
+ assert(!itemSources.empty());
+ // Each keeps its own resolution, so each is saved as changed only if it was
+ mItemSources.reserve(itemSources.size());
+ for (const MWWorld::Ptr& source : itemSources)
+ {
+ MWWorld::ContainerStore& store = source.getClass().getContainerStore(source);
+ mItemSources.emplace_back(source, store.resolveTemporarily());
+ }
+ }
+
+ std::vector<ContainerItemModel::SourceItem> ContainerItemModel::getSourceItems(const MWWorld::Ptr& item, int count)
+ {
+ std::vector<SourceItem> result;
+ for (auto& source : mItemSources)
+ {
+ MWWorld::ContainerStore& store = source.first.getClass().getContainerStore(source.first);
+ for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end() && count > 0; ++it)
+ {
+ if (!stacks(*it, item))
+ continue;
+ const int fromHere = std::min(count, it->getCellRef().getCount());
+ if (fromHere <= 0)
+ continue;
+ result.push_back({ source.first, *it, fromHere });
+ count -= fromHere;
+ }
+ if (count <= 0)
+ break;
+ }
+ return result;
+ }
+
bool ContainerItemModel::allowedToUseItems() const
{
if (mItemSources.empty())
@@ -74,6 +109,10 @@ namespace MWGui
// Check if the player is allowed to use items from opened container
MWBase::MechanicsManager* mm = MWBase::Environment::get().getMechanicsManager();
+ // Isowind: containers opened together (not a merchant's): only if allowed in all of them
+ if (!mTrading)
+ return std::all_of(mItemSources.begin(), mItemSources.end(),
+ [&](const auto& source) { return mm->isAllowedToUse(ptr, source.first, victim); });
return mm->isAllowedToUse(ptr, mItemSources[0].first, victim);
}
@@ -151,6 +190,9 @@ namespace MWGui
int quantity = it->mRef->mRef.getCount(false);
// Isowind multiplayer: the server removes it, for everyone, unless this is the container open
// here (whose items the server already agreed to hand over, see ContainerWindow)
+ // (Containers opened with it count as open too: they're only taken from here by a drag
+ // that starts once the server agreed, see ObjectList::editContainers. Any other way of
+ // taking from them in multiplayer would need to go through the server first.)
if (mwmp::hooks::isMultiplayer() && source.first.isInCell()
&& !mwmp::hooks::isCurrentContainer(source.first))
{
@@ -295,6 +337,24 @@ namespace MWGui
return true;
MWWorld::Ptr player = MWMechanics::getPlayer();
+
+ // Isowind: from containers opened together, taking from each is judged by whose it is
+ if (!mTrading && mItemSources.size() > 1)
+ {
+ std::vector<SourceItem> sources = getSourceItems(item, count);
+ for (auto it = sources.begin(); it != sources.end();)
+ {
+ // (one crime per container, however many stacks of it there)
+ int taken = 0;
+ auto next = it;
+ for (; next != sources.end() && next->mContainer == it->mContainer; ++next)
+ taken += next->mCount;
+ MWBase::Environment::get().getMechanicsManager()->itemTaken(player, it->mItem, it->mContainer, taken);
+ it = next;
+ }
+ return true;
+ }
+
MWBase::Environment::get().getMechanicsManager()->itemTaken(player, item, target, count);
return true;
diff --git a/apps/openmw/mwgui/containeritemmodel.hpp b/apps/openmw/mwgui/containeritemmodel.hpp
index 08ab7e7..71c7f6c 100644
--- a/apps/openmw/mwgui/containeritemmodel.hpp
+++ b/apps/openmw/mwgui/containeritemmodel.hpp
@@ -23,6 +23,21 @@ namespace MWGui
ContainerItemModel(const MWWorld::Ptr& source);
+ /// Isowind: containers opened together as one (crates stacked on the one opened, see ContainerWindow::setPtr).
+ /// The first is the one opened: items put in go to it, and items are taken from it first.
+ explicit ContainerItemModel(const std::vector<MWWorld::Ptr>& itemSources);
+
+ /// Isowind: one container's share of an item stack
+ struct SourceItem
+ {
+ MWWorld::Ptr mContainer;
+ MWWorld::Ptr mItem; ///< The container's own item (that stacks with the one asked about)
+ int mCount; ///< How many of those taken come from it
+ };
+
+ /// Isowind: where \a count of \a item come from when taken (in the order removeItem takes them)
+ std::vector<SourceItem> getSourceItems(const MWWorld::Ptr& item, int count);
+
bool allowedToUseItems() const override;
bool onDropItem(const MWWorld::Ptr& item, int count) override;
diff --git a/apps/openmw/mwmp/Hooks.cpp b/apps/openmw/mwmp/Hooks.cpp
index 2e040d5..67bd28b 100644
--- a/apps/openmw/mwmp/Hooks.cpp
+++ b/apps/openmw/mwmp/Hooks.cpp
@@ -6,6 +6,8 @@
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/world.hpp"
+#include "../mwgui/container.hpp"
+
#include "../mwworld/cellstore.hpp"
#include <components/misc/strings/algorithm.hpp>
@@ -258,8 +260,16 @@ namespace mwmp::hooks
if (!isMultiplayer() || container.isEmpty())
return false;
const CurrentContainer& current = Main::get().getLocalPlayer()->currentContainer;
- return current.refNum == container.getCellRef().getMpRefNum()
- && current.mpNum == container.getCellRef().getMpNum();
+ if (current.refNum == container.getCellRef().getMpRefNum()
+ && current.mpNum == container.getCellRef().getMpNum())
+ return true;
+ // Containers next to it that opened with it, in its window, count as open too (ContainerWindow::setPtr)
+ MWBase::WindowManager* windowManager = MWBase::Environment::get().getWindowManager();
+ if (!windowManager->containsMode(MWGui::GM_Container))
+ return false;
+ MWGui::ContainerWindow* containerWindow = windowManager->getContainerWindow();
+ MWGui::ItemModel* model = containerWindow ? containerWindow->getModel() : nullptr;
+ return model && model->usesContainer(container);
}
bool denyRemoteCellChange(const MWWorld::Ptr& ptr, const MWWorld::CellStore& newCell)
diff --git a/apps/openmw/mwmp/ObjectList.cpp b/apps/openmw/mwmp/ObjectList.cpp
index e06046a..907db51 100644
--- a/apps/openmw/mwmp/ObjectList.cpp
+++ b/apps/openmw/mwmp/ObjectList.cpp
@@ -206,16 +206,9 @@ void ObjectList::editContainers(MWWorld::CellStore* cellStore)
bool hasActorEquipment = ptrFound.getClass().isActor() && ptrFound.getClass().hasInventoryStore(ptrFound);
// If we are in a container, and it happens to be this container, keep track of that
+ // (Isowind: or one opened with it, shown in its window, see ContainerWindow::setPtr)
if (MWBase::Environment::get().getWindowManager()->containsMode(MWGui::GM_Container))
- {
- CurrentContainer *currentContainer = &mwmp::Main::get().getLocalPlayer()->currentContainer;
-
- if (currentContainer->refNum == ptrFound.getCellRef().getMpRefNum() &&
- currentContainer->mpNum == ptrFound.getCellRef().getMpNum())
- {
- isCurrentContainer = true;
- }
- }
+ isCurrentContainer = hooks::isCurrentContainer(ptrFound);
MWWorld::ContainerStore& containerStore = ptrFound.getClass().getContainerStore(ptrFound);
@@ -343,8 +336,9 @@ void ObjectList::editContainers(MWWorld::CellStore* cellStore)
}
else
{
+ // (Isowind: the window stays on the container it was opened on, which may not be this one)
MWGui::ContainerWindow* containerWindow = MWBase::Environment::get().getWindowManager()->getContainerWindow();
- containerWindow->setPtr(ptrFound);
+ containerWindow->onContainerChanged(ptrFound);
}
}
}
@@ -588,16 +582,12 @@ void ObjectList::deleteObjects(MWWorld::CellStore* cellStore)
ptrFound.getCellRef().getMpRefNum(), ptrFound.getCellRef().getMpNum());
// If we are in a container, and it happens to be this object, exit it
- if (MWBase::Environment::get().getWindowManager()->containsMode(MWGui::GM_Container))
+ // (Isowind: or one opened with it, shown in its window)
+ if (MWBase::Environment::get().getWindowManager()->containsMode(MWGui::GM_Container)
+ && hooks::isCurrentContainer(ptrFound))
{
- CurrentContainer *currentContainer = &mwmp::Main::get().getLocalPlayer()->currentContainer;
-
- if (currentContainer->refNum == ptrFound.getCellRef().getMpRefNum() &&
- currentContainer->mpNum == ptrFound.getCellRef().getMpNum())
- {
- MWBase::Environment::get().getWindowManager()->removeGuiMode(MWGui::GM_Container);
- MWBase::Environment::get().getWindowManager()->setDragDrop(false);
- }
+ MWBase::Environment::get().getWindowManager()->removeGuiMode(MWGui::GM_Container);
+ MWBase::Environment::get().getWindowManager()->setDragDrop(false);
}
// Is this a dying actor being deleted before its death animation has finished? If so,