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++
#include "containeritemmodel.hpp"
#include <algorithm>
#include "../mwmechanics/actorutil.hpp"
#include "../mwmechanics/creaturestats.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/mechanicsmanager.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/world.hpp"
#include "../mwmp/Hooks.hpp"
#include <components/openmw-mp/Base/BaseObject.hpp>
namespace
{
bool stacks(const MWWorld::Ptr& left, const MWWorld::Ptr& right)
{
if (left == right)
return true;
// If one of the items is in an inventory and currently equipped, we need to check stacking both ways to be sure
if (left.getContainerStore() && right.getContainerStore())
return left.getContainerStore()->stacks(left, right) && right.getContainerStore()->stacks(left, right);
if (left.getContainerStore())
return left.getContainerStore()->stacks(left, right);
if (right.getContainerStore())
return right.getContainerStore()->stacks(left, right);
MWWorld::ContainerStore store;
return store.stacks(left, right);
}
}
namespace MWGui
{
ContainerItemModel::ContainerItemModel(
const std::vector<MWWorld::Ptr>& itemSources, const std::vector<MWWorld::Ptr>& worldItems)
: mWorldItems(worldItems)
, mTrading(true)
{
assert(!itemSources.empty());
// Tie resolution lifetimes to the ItemModel
mItemSources.reserve(itemSources.size());
for (const MWWorld::Ptr& source : itemSources)
{
MWWorld::ContainerStore& store = source.getClass().getContainerStore(source);
mItemSources.emplace_back(source, store.resolveTemporarily());
}
}
ContainerItemModel::ContainerItemModel(const MWWorld::Ptr& source)
: mTrading(false)
{
MWWorld::ContainerStore& store = source.getClass().getContainerStore(source);
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 (size_t i = 0; i < mItemSources.size(); ++i)
{
if (!isShown(i))
continue;
auto& source = mItemSources[i];
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;
}
void ContainerItemModel::setShownSource(int index)
{
mShownSource = index >= 0 && static_cast<size_t>(index) < mItemSources.size() ? index : -1;
}
const MWWorld::Ptr& ContainerItemModel::getDropTarget() const
{
return mItemSources[mShownSource < 0 ? 0 : mShownSource].first;
}
bool ContainerItemModel::allowedToUseItems() const
{
if (mItemSources.empty())
return true;
MWWorld::Ptr ptr = MWMechanics::getPlayer();
MWWorld::Ptr victim;
// 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 shown
if (!mTrading)
{
for (size_t i = 0; i < mItemSources.size(); ++i)
{
if (isShown(i) && !mm->isAllowedToUse(ptr, mItemSources[i].first, victim))
return false;
}
return true;
}
return mm->isAllowedToUse(ptr, mItemSources[0].first, victim);
}
ItemStack ContainerItemModel::getItem(ModelIndex index)
{
if (index < 0)
throw std::runtime_error("Invalid index supplied");
if (mItems.size() <= static_cast<size_t>(index))
throw std::runtime_error("Item index out of range");
return mItems[index];
}
size_t ContainerItemModel::getItemCount()
{
return mItems.size();
}
ItemModel::ModelIndex ContainerItemModel::getIndex(const ItemStack& item)
{
ModelIndex i = 0;
for (ItemStack& itemStack : mItems)
{
if (itemStack == item)
return i;
++i;
}
return -1;
}
MWWorld::Ptr ContainerItemModel::addItem(const ItemStack& item, size_t count, bool allowAutoEquip)
{
// (Isowind: the container shown, of several)
auto& source = mItemSources[mShownSource < 0 ? 0 : mShownSource];
MWWorld::ContainerStore& store = source.first.getClass().getContainerStore(source.first);
if (item.mBase.getContainerStore() == &store)
throw std::runtime_error("Item to add needs to be from a different container!");
// Isowind multiplayer: the server adds it, for everyone
if (mwmp::hooks::isMultiplayer() && source.first.isInCell())
{
mwmp::hooks::sendContainerChange(source.first, mwmp::BaseObjectList::ADD, mwmp::BaseObjectList::NONE,
{ { item.mBase, static_cast<int>(count) } });
return item.mBase;
}
return *store.add(item.mBase, static_cast<int>(count), allowAutoEquip);
}
MWWorld::Ptr ContainerItemModel::copyItem(const ItemStack& item, size_t count, bool allowAutoEquip)
{
// (Isowind: the container shown, of several)
auto& source = mItemSources[mShownSource < 0 ? 0 : mShownSource];
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!");
// Isowind multiplayer: the server adds it, for everyone
if (mwmp::hooks::isMultiplayer() && source.first.isInCell())
{
mwmp::hooks::sendContainerChange(source.first, mwmp::BaseObjectList::ADD, mwmp::BaseObjectList::NONE,
{ { item.mBase, static_cast<int>(count) } });
return item.mBase;
}
MWWorld::ManualRef newRef(*MWBase::Environment::get().getESMStore(), item.mBase, static_cast<int>(count));
return *store.add(newRef.getPtr(), static_cast<int>(count), allowAutoEquip);
}
void ContainerItemModel::removeItem(const ItemStack& item, size_t count)
{
int toRemove = static_cast<int>(count);
for (size_t i = 0; i < mItemSources.size(); ++i)
{
// (Isowind: only from the container shown, of several)
if (!isShown(i))
continue;
auto& source = mItemSources[i];
MWWorld::ContainerStore& store = source.first.getClass().getContainerStore(source.first);
for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it)
{
if (stacks(*it, item.mBase))
{
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))
{
const int count = it->getCellRef().getCount();
mwmp::hooks::sendContainerChange(source.first, mwmp::BaseObjectList::REMOVE,
mwmp::BaseObjectList::NONE, { { *it, std::min(toRemove, count) } });
toRemove -= count;
}
// If this is a restocking quantity, just don't remove it
else if (quantity < 0 && mTrading)
toRemove += quantity;
else
toRemove -= store.remove(*it, toRemove);
if (toRemove <= 0)
return;
}
}
}
for (MWWorld::Ptr& source : mWorldItems)
{
if (stacks(source, item.mBase))
{
int refCount = source.getCellRef().getCount();
if (refCount - toRemove <= 0)
{
// Isowind multiplayer: bought from its owner, it is gone for everyone
mwmp::hooks::sendObjectDelete(source);
MWBase::Environment::get().getWorld()->deleteObject(source);
}
else
source.getCellRef().setCount(std::max(0, refCount - toRemove));
toRemove -= refCount;
if (toRemove <= 0)
return;
}
}
throw std::runtime_error("Not enough items to remove could be found");
}
void ContainerItemModel::update()
{
mItems.clear();
for (size_t i = 0; i < mItemSources.size(); ++i)
{
// (Isowind: only the container shown, of several)
if (!isShown(i))
continue;
auto& source = mItemSources[i];
MWWorld::ContainerStore& store = source.first.getClass().getContainerStore(source.first);
for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it)
{
if (!(*it).getClass().showsInInventory(*it))
continue;
bool found = false;
for (ItemStack& itemStack : mItems)
{
if (stacks(*it, itemStack.mBase))
{
// we already have an item stack of this kind, add to it
itemStack.mCount += it->getCellRef().getCount();
found = true;
break;
}
}
if (!found)
{
// no stack yet, create one
ItemStack newItem(*it, this, it->getCellRef().getCount());
mItems.push_back(newItem);
}
}
}
for (MWWorld::Ptr& source : mWorldItems)
{
bool found = false;
for (ItemStack& itemStack : mItems)
{
if (stacks(source, itemStack.mBase))
{
// we already have an item stack of this kind, add to it
itemStack.mCount += source.getCellRef().getCount();
found = true;
break;
}
}
if (!found)
{
// no stack yet, create one
ItemStack newItem(source, this, source.getCellRef().getCount());
mItems.push_back(newItem);
}
}
}
bool ContainerItemModel::onDropItem(const MWWorld::Ptr& item, int count)
{
if (mItemSources.empty())
return false;
MWWorld::Ptr target = getDropTarget();
if (target.getType() != ESM::Container::sRecordId)
return true;
// Check container organic flag
MWWorld::LiveCellRef<ESM::Container>* ref = target.get<ESM::Container>();
if (ref->mBase->mFlags & ESM::Container::Organic)
{
MWBase::Environment::get().getWindowManager()->messageBox("#{sContentsMessage2}");
return false;
}
// Check for container without capacity
float capacity = target.getClass().getCapacity(target);
if (capacity <= 0.0f)
{
MWBase::Environment::get().getWindowManager()->messageBox("#{sContentsMessage3}");
return false;
}
// Check the container capacity plus one increment so the expected total weight can
// fit in the container with floating-point imprecision
float newEncumbrance = target.getClass().getEncumbrance(target) + (item.getClass().getWeight(item) * count);
if (std::nextafterf(capacity, std::numeric_limits<float>::max()) < newEncumbrance)
{
MWBase::Environment::get().getWindowManager()->messageBox("#{sContentsMessage3}");
return false;
}
return true;
}
bool ContainerItemModel::onTakeItem(const MWWorld::Ptr& item, int count)
{
if (mItemSources.empty())
return false;
MWWorld::Ptr target = mItemSources[0].first;
// Looting a dead corpse is considered OK
if (target.getClass().isActor() && target.getClass().getCreatureStats(target).isDead())
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;
}
bool ContainerItemModel::usesContainer(const MWWorld::Ptr& container)
{
for (const auto& source : mItemSources)
{
if (source.first == container)
return true;
}
return false;
}
}