bought/sold items now use a separate ContainerStore, this fixes a stacking issue among other things

This commit is contained in:
scrawl 2012-05-18 20:53:24 +02:00
parent fb47681fbd
commit db49e2c046
4 changed files with 56 additions and 58 deletions

View file

@ -182,19 +182,15 @@ void ContainerBase::onSelectedItem(MyGUI::Widget* _sender)
void ContainerBase::sellAlreadyBoughtItem(MyGUI::Widget* _sender, int count)
{
MWWorld::Ptr object = *mSelectedItem->getUserData<MWWorld::Ptr>();
assert( std::find(mBoughtItems.begin(), mBoughtItems.end(), object) != mBoughtItems.end() );
if (count == object.getRefData().getCount())
mBoughtItems.erase( std::find(mBoughtItems.begin(), mBoughtItems.end(), object) );
if (isInventory())
{
MWBase::Environment::get().getWindowManager()->getTradeWindow()->readdBarteredItem(*mSelectedItem->getUserData<MWWorld::Ptr>(), count);
MWBase::Environment::get().getWindowManager()->getTradeWindow()->addItem(object, count);
MWBase::Environment::get().getWindowManager()->getTradeWindow()->drawItems();
}
else
{
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->readdBarteredItem(*mSelectedItem->getUserData<MWWorld::Ptr>(), count);
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->addItem(object, count);
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->drawItems();
}
@ -203,19 +199,14 @@ void ContainerBase::sellAlreadyBoughtItem(MyGUI::Widget* _sender, int count)
void ContainerBase::sellItem(MyGUI::Widget* _sender, int count)
{
MWWorld::Ptr newPtr;
if (isInventory())
{
newPtr = MWBase::Environment::get().getWindowManager()->getTradeWindow()->addBarteredItem(*mSelectedItem->getUserData<MWWorld::Ptr>(), count);
if (std::find(mSoldItems.begin(), mSoldItems.end(), newPtr) == mSoldItems.end())
mSoldItems.push_back(newPtr);
MWBase::Environment::get().getWindowManager()->getTradeWindow()->addBarteredItem(*mSelectedItem->getUserData<MWWorld::Ptr>(), count);
MWBase::Environment::get().getWindowManager()->getTradeWindow()->drawItems();
}
else
{
newPtr = MWBase::Environment::get().getWindowManager()->getInventoryWindow()->addBarteredItem(*mSelectedItem->getUserData<MWWorld::Ptr>(), count);
if (std::find(mSoldItems.begin(), mSoldItems.end(), newPtr) == mSoldItems.end())
mSoldItems.push_back(newPtr);
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->addBarteredItem(*mSelectedItem->getUserData<MWWorld::Ptr>(), count);
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->drawItems();
}
@ -385,10 +376,16 @@ void ContainerBase::drawItems()
std::vector<MWWorld::Ptr> equippedItems = getEquippedItems();
// add bartered items (always at the beginning)
std::sort(mBoughtItems.begin(), mBoughtItems.end(), sortItems);
for (std::vector<MWWorld::Ptr>::iterator it=mBoughtItems.begin();
it != mBoughtItems.end(); ++it)
// add bought items (always at the beginning)
std::vector<MWWorld::Ptr> boughtItems;
for (MWWorld::ContainerStoreIterator it (mBoughtItems.begin()); it!=mBoughtItems.end(); ++it)
{
boughtItems.push_back(*it);
}
std::sort(boughtItems.begin(), boughtItems.end(), sortItems);
for (std::vector<MWWorld::Ptr>::iterator it=boughtItems.begin();
it != boughtItems.end(); ++it)
{
items.push_back( std::make_pair(*it, ItemState_Barter) );
}
@ -529,49 +526,50 @@ std::string ContainerBase::getCountString(const int count)
return boost::lexical_cast<std::string>(count);
}
MWWorld::Ptr ContainerBase::readdBarteredItem(MWWorld::Ptr item, int count)
void ContainerBase::addBarteredItem(MWWorld::Ptr item, int count)
{
MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer);
int origCount = item.getRefData().getCount();
item.getRefData().setCount(count);
MWWorld::ContainerStoreIterator it = containerStore.add(item);
MWWorld::ContainerStoreIterator it = mBoughtItems.add(item);
item.getRefData().setCount(origCount - count);
if (origCount - count == 0)
mSoldItems.erase( std::find(mSoldItems.begin(), mSoldItems.end(), item) );
return *it;
}
MWWorld::Ptr ContainerBase::addBarteredItem(MWWorld::Ptr item, int count)
void ContainerBase::addItem(MWWorld::Ptr item, int count)
{
MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer);
int origCount = item.getRefData().getCount();
int origCount = item.getRefData().getCount();
item.getRefData().setCount(count);
MWWorld::ContainerStoreIterator it = containerStore.add(item);
item.getRefData().setCount(origCount - count);
if (std::find(mBoughtItems.begin(), mBoughtItems.end(), *it) == mBoughtItems.end())
mBoughtItems.push_back(*it);
return *it;
}
void ContainerBase::removeBarteredItem(MWWorld::Ptr item, int count)
void ContainerBase::transferBoughtItems()
{
MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer);
for (MWWorld::ContainerStoreIterator it(containerStore.begin()); it != containerStore.end(); ++it)
for (MWWorld::ContainerStoreIterator it(mBoughtItems.begin()); it != mBoughtItems.end(); ++it)
{
if (*it == item)
{
item.getRefData().setCount(item.getRefData().getCount() - count);
}
containerStore.add(*it);
}
}
void ContainerBase::returnBoughtItems(MWWorld::ContainerStore& store)
{
for (MWWorld::ContainerStoreIterator it(mBoughtItems.begin()); it != mBoughtItems.end(); ++it)
{
store.add(*it);
}
}
MWWorld::ContainerStore& ContainerBase::getContainerStore()
{
MWWorld::ContainerStore& store = MWWorld::Class::get(mContainer).getContainerStore(mContainer);
return store;
}
// ------------------------------------------------------------------------------------------------
ContainerWindow::ContainerWindow(WindowManager& parWindowManager,DragAndDrop* dragAndDrop)

View file

@ -67,9 +67,13 @@ namespace MWGui
void setWidgets(MyGUI::Widget* containerWidget, MyGUI::ScrollView* itemView); ///< only call once
MWWorld::Ptr addBarteredItem(MWWorld::Ptr item, int count);
MWWorld::Ptr readdBarteredItem(MWWorld::Ptr item, int count);
void removeBarteredItem(MWWorld::Ptr item, int count);
void addBarteredItem(MWWorld::Ptr item, int count);
void addItem(MWWorld::Ptr item, int count);
void transferBoughtItems(); ///< transfer bought items into the inventory
void returnBoughtItems(MWWorld::ContainerStore& store); ///< return bought items into the specified ContainerStore
MWWorld::ContainerStore& getContainerStore();
void openContainer(MWWorld::Ptr container);
void setFilter(Filter filter); ///< set category filter
@ -86,8 +90,8 @@ namespace MWGui
Filter mFilter;
std::vector<MWWorld::Ptr> mBoughtItems;
std::vector<MWWorld::Ptr> mSoldItems;
// bought items are put in a separate ContainerStore so that they don't stack with other (not bought) items.
MWWorld::ContainerStore mBoughtItems;
void onSelectedItem(MyGUI::Widget* _sender);
void onContainerClicked(MyGUI::Widget* _sender);

View file

@ -97,7 +97,6 @@ namespace MWGui
mTrading = false;
mBoughtItems.clear();
mSoldItems.clear();
onWindowResize(static_cast<MyGUI::Window*>(mMainWidget));
}

View file

@ -110,7 +110,6 @@ namespace MWGui
mWindowManager.getInventoryWindow()->startTrade();
mBoughtItems.clear();
mSoldItems.clear();
ContainerBase::openContainer(actor);
@ -146,10 +145,19 @@ namespace MWGui
void TradeWindow::onOfferButtonClicked(MyGUI::Widget* _sender)
{
transferBoughtItems();
mWindowManager.getInventoryWindow()->transferBoughtItems();
mWindowManager.setGuiMode(GM_Game);
}
void TradeWindow::onCancelButtonClicked(MyGUI::Widget* _sender)
{
// i give you back your stuff!
returnBoughtItems(mWindowManager.getInventoryWindow()->getContainerStore());
// now gimme back my stuff!
mWindowManager.getInventoryWindow()->returnBoughtItems(MWWorld::Class::get(mContainer).getContainerStore(mContainer));
mWindowManager.setGuiMode(GM_Game);
}
@ -169,16 +177,6 @@ namespace MWGui
mTotalBalance->setCaption(boost::lexical_cast<std::string>(-mCurrentBalance));
}
// retrieve merchant gold
int gold = 0;
MWWorld::InventoryStore& invStore = static_cast<MWWorld::InventoryStore&>(MWWorld::Class::get(mContainer).getContainerStore(mContainer));
for (MWWorld::ContainerStoreIterator it = invStore.begin();
it != invStore.end(); ++it)
{
if (MWWorld::Class::get(*it).getName(*it) == MWBase::Environment::get().getWorld()->getStore().gameSettings.search("sGold")->str)
gold = it->getRefData().getCount();
}
int merchantgold;
if (mContainer.getTypeName() == typeid(ESM::NPC).name())
{
@ -193,10 +191,9 @@ namespace MWGui
ESMS::LiveCellRef<ESM::Creature, MWWorld::RefData>* ref = mContainer.get<ESM::Creature>();
merchantgold = ref->base->data.gold;
}
gold += merchantgold;
mMerchantGold->setCaption(MWBase::Environment::get().getWorld()->getStore().gameSettings.search("sSellerGold")->str
+ " " + boost::lexical_cast<std::string>(gold));
+ " " + boost::lexical_cast<std::string>(merchantgold));
}
std::vector<MWWorld::Ptr> TradeWindow::getEquippedItems()