1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00
OpenMW/apps/openmw/mwgui/tradeitemmodel.cpp
fteppe 125b21de20 Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type
The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID

Slowly going through all the changes to make, still hundreds of errors

a lot of functions/structures use std::string or stringview to designate an ID. So it takes time

Continues slowly replacing ids. There are technically more and more compilation errors

I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type

Continue moving forward, changes to the stores

slowly moving along

Starting to see the fruit of those changes.

still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type.

More replacements. Things are starting to get easier

I can see more and more often the issue is that the function is awaiting a RefId, but is given a string
there is less need to go down functions and to fix a long list of them.

Still moving forward, and for the first time error count is going down!

Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably

Cells are back to using string for the name, haven't fixed everything yet. Many other changes

Under the bar of 400 compilation errors.

more good progress <100 compile errors!

More progress

Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string

some more progress on other fronts

Mostly game settings clean

one error opened a lot of other errors. Down to 18, but more will prbably appear

only link errors left??

Fixed link errors

OpenMW compiles, and launches, with some issues, but still!
2022-12-27 19:15:54 +01:00

215 lines
6.6 KiB
C++

#include "tradeitemmodel.hpp"
#include <components/misc/strings/algorithm.hpp>
#include <components/settings/settings.hpp>
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwworld/inventorystore.hpp"
namespace MWGui
{
TradeItemModel::TradeItemModel(std::unique_ptr<ItemModel> sourceModel, const MWWorld::Ptr& merchant)
: mMerchant(merchant)
{
mSourceModel = std::move(sourceModel);
}
bool TradeItemModel::allowedToUseItems() const
{
return true;
}
ItemStack TradeItemModel::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 TradeItemModel::getItemCount()
{
return mItems.size();
}
void TradeItemModel::borrowImpl(const ItemStack& item, std::vector<ItemStack>& out)
{
bool found = false;
for (ItemStack& itemStack : out)
{
if (itemStack.mBase == item.mBase)
{
itemStack.mCount += item.mCount;
found = true;
break;
}
}
if (!found)
out.push_back(item);
}
void TradeItemModel::unborrowImpl(const ItemStack& item, size_t count, std::vector<ItemStack>& out)
{
std::vector<ItemStack>::iterator it = out.begin();
bool found = false;
for (; it != out.end(); ++it)
{
if (it->mBase == item.mBase)
{
if (it->mCount < count)
throw std::runtime_error("Not enough borrowed items to return");
it->mCount -= count;
if (it->mCount == 0)
out.erase(it);
found = true;
break;
}
}
if (!found)
throw std::runtime_error("Can't find borrowed item to return");
}
void TradeItemModel::borrowItemFromUs(ModelIndex itemIndex, size_t count)
{
ItemStack item = getItem(itemIndex);
item.mCount = count;
borrowImpl(item, mBorrowedFromUs);
}
void TradeItemModel::borrowItemToUs(ModelIndex itemIndex, ItemModel* source, size_t count)
{
ItemStack item = source->getItem(itemIndex);
item.mCount = count;
borrowImpl(item, mBorrowedToUs);
}
void TradeItemModel::returnItemBorrowedToUs(ModelIndex itemIndex, size_t count)
{
ItemStack item = getItem(itemIndex);
unborrowImpl(item, count, mBorrowedToUs);
}
void TradeItemModel::returnItemBorrowedFromUs(ModelIndex itemIndex, ItemModel* source, size_t count)
{
ItemStack item = source->getItem(itemIndex);
unborrowImpl(item, count, mBorrowedFromUs);
}
void TradeItemModel::adjustEncumbrance(float& encumbrance)
{
for (ItemStack& itemStack : mBorrowedToUs)
{
MWWorld::Ptr& item = itemStack.mBase;
encumbrance += item.getClass().getWeight(item) * itemStack.mCount;
}
for (ItemStack& itemStack : mBorrowedFromUs)
{
MWWorld::Ptr& item = itemStack.mBase;
encumbrance -= item.getClass().getWeight(item) * itemStack.mCount;
}
encumbrance = std::max(0.f, encumbrance);
}
void TradeItemModel::abort()
{
mBorrowedFromUs.clear();
mBorrowedToUs.clear();
}
const std::vector<ItemStack> TradeItemModel::getItemsBorrowedToUs() const
{
return mBorrowedToUs;
}
void TradeItemModel::transferItems()
{
for (ItemStack& itemStack : mBorrowedToUs)
{
// get index in the source model
ItemModel* sourceModel = itemStack.mCreator;
size_t i = 0;
for (; i < sourceModel->getItemCount(); ++i)
{
if (itemStack.mBase == sourceModel->getItem(i).mBase)
break;
}
if (i == sourceModel->getItemCount())
throw std::runtime_error("The borrowed item disappeared");
const ItemStack& item = sourceModel->getItem(i);
static const bool prevent = Settings::Manager::getBool("prevent merchant equipping", "Game");
// copy the borrowed items to our model
copyItem(item, itemStack.mCount, !prevent);
// then remove them from the source model
sourceModel->removeItem(item, itemStack.mCount);
}
mBorrowedToUs.clear();
mBorrowedFromUs.clear();
}
void TradeItemModel::update()
{
mSourceModel->update();
int services = 0;
if (!mMerchant.isEmpty())
services = mMerchant.getClass().getServices(mMerchant);
mItems.clear();
// add regular items
for (size_t i = 0; i < mSourceModel->getItemCount(); ++i)
{
ItemStack item = mSourceModel->getItem(i);
if (!mMerchant.isEmpty())
{
MWWorld::Ptr base = item.mBase;
if (ESM::RefId::ciEqual(base.getCellRef().getRefId(), MWWorld::ContainerStore::sGoldId))
continue;
if (!base.getClass().showsInInventory(base))
return;
if (!base.getClass().canSell(base, services))
continue;
// Bound items may not be bought
if (item.mFlags & ItemStack::Flag_Bound)
continue;
// don't show equipped items
if (mMerchant.getClass().hasInventoryStore(mMerchant))
{
MWWorld::InventoryStore& store = mMerchant.getClass().getInventoryStore(mMerchant);
if (store.isEquipped(base))
continue;
}
}
// don't show items that we borrowed to someone else
for (ItemStack& itemStack : mBorrowedFromUs)
{
if (itemStack.mBase == item.mBase)
{
if (item.mCount < itemStack.mCount)
throw std::runtime_error("Lent more items than present");
item.mCount -= itemStack.mCount;
}
}
if (item.mCount > 0)
mItems.push_back(item);
}
// add items borrowed to us
for (ItemStack& itemStack : mBorrowedToUs)
{
itemStack.mType = ItemStack::Type_Barter;
mItems.push_back(itemStack);
}
}
}