From aa721fe1f662e9e9781b9522c26f2f4dd2994303 Mon Sep 17 00:00:00 2001 From: MatthewRock Date: Tue, 8 Dec 2015 22:38:26 +0100 Subject: [PATCH] Fix bug 2952 with merchant and levelled items --- AUTHORS.md | 1 + apps/openmw/mwworld/containerstore.cpp | 55 ++++++++++++++++++++------ components/esm/inventorystate.cpp | 17 ++++---- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index e968d1d01..077878ea2 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -74,6 +74,7 @@ Programmers Marco Melletti (mellotanica) Marco Schulze Mateusz Kołaczek (PL_kolek) + Mateusz Malisz (malice) megaton Michael Hogan (Xethik) Michael Mc Donnell diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 78a50b4e7..1130ec604 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -415,6 +415,7 @@ void MWWorld::ContainerStore::fill (const ESM::InventoryList& items, const std:: void MWWorld::ContainerStore::addInitialItem (const std::string& id, const std::string& owner, int count, bool topLevel, const std::string& levItem) { + if (count == 0) return; //Don't restock with nothing. try { ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), id, count); @@ -463,8 +464,8 @@ void MWWorld::ContainerStore::addInitialItem (const std::string& id, const std:: void MWWorld::ContainerStore::restock (const ESM::InventoryList& items, const MWWorld::Ptr& ptr, const std::string& owner) { - //allowedForReplace - Holds information about how many items from the list were sold; - // Hence, tells us how many items we need to restock. + //allowedForReplace - Holds information about how many items from the list were not sold; + // Hence, tells us how many items we don't need to restock. //allowedForReplace[list] <- How many items we should generate(how many of these were sold) std::map allowedForReplace; @@ -473,20 +474,42 @@ void MWWorld::ContainerStore::restock (const ESM::InventoryList& items, const MW { int spawnedCount = it->second.first; //How many items should be in shop originally int itemCount = count(it->first); //How many items are there in shop now - //If anything was sold - if(itemCount < spawnedCount) + //If something was not sold + if(itemCount >= spawnedCount) + { + const std::string& ancestor = it->second.second; + // Security check for old saves: + //If item is imported from old save(doesn't have an ancestor) and wasn't sold + if(ancestor == "") + { + //Remove it, from shop, + remove(it->first, itemCount, ptr);//ptr is the NPC + //And remove it from map, so that when we restock, the new item will have proper ancestor. + mLevelledItemMap.erase(it); + continue; + + } + //Create the entry if it does not exist yet + std::map::iterator listInMap = allowedForReplace.insert( + std::make_pair(it->second.second, 0)).first; + //And signal that we don't need to restock item from this list + listInMap->second += std::abs(itemCount); + } + //If every of the item was sold + else if (itemCount == 0) + { + mLevelledItemMap.erase(it); + } + //If some was sold, but some remain + else { //Create entry if it does not exist yet std::map::iterator listInMap = allowedForReplace.insert( std::make_pair(it->second.second, 0)).first; - //And signal that we need to restock item from this list - listInMap->second += std::abs(spawnedCount - itemCount); - //Also, remove the record if item no longer figures in the shop - if(!itemCount) - mLevelledItemMap.erase(it->first); - //If there's still item in the shop, change its spawnedCount to current count. - else - it->second.first -= itemCount; + //And signal that we don't need to restock all items from this list + listInMap->second += std::abs(itemCount); + //And update itemCount so we don't mistake it next time. + it->second.first = itemCount; } } @@ -504,8 +527,14 @@ void MWWorld::ContainerStore::restock (const ESM::InventoryList& items, const MW if (MWBase::Environment::get().getWorld()->getStore().get().search(it->mItem.toString())) { std::map::iterator listInMap = allowedForReplace.find(itemOrList); + + int restockNum = it-> mCount; + //If we know we must restock less, take it into account if(listInMap != allowedForReplace.end()) - addInitialItem(itemOrList, owner, listInMap->second, true); + restockNum += listInMap->second;//We add, because list items have negative count + //restock + addInitialItem(itemOrList, owner, restockNum, true); + } else { diff --git a/components/esm/inventorystate.cpp b/components/esm/inventorystate.cpp index 1864b6e8d..ad9e70af1 100644 --- a/components/esm/inventorystate.cpp +++ b/components/esm/inventorystate.cpp @@ -31,17 +31,20 @@ void ESM::InventoryState::load (ESMReader &esm) ++index; } - + //Next item is Levelled item while (esm.isNextSub("LEVM")) { + //Get its name std::string id = esm.getHString(); int count; - std::string parentList; - //TODO: How should I handle old saves? - if(esm.isNextSub("LLST")) - std::string parentList = esm.getHString(); + std::string parentGroup = ""; + //Then get its count esm.getHNT (count, "COUN"); - mLevelledItemMap[id] = count; + //Old save formats don't have information about parent group; check for that + if(esm.isNextSub("LGRP")) + //Newest saves contain parent group + parentGroup = esm.getHString(); + mLevelledItemMap[id] = std::make_pair(count, parentGroup); } while (esm.isNextSub("MAGI")) @@ -87,7 +90,7 @@ void ESM::InventoryState::save (ESMWriter &esm) const { esm.writeHNString ("LEVM", it->first); esm.writeHNT ("COUN", it->second.first); - esm.writeHNString("LLST", it->second.second) + esm.writeHNString("LGRP", it->second.second); } for (TEffectMagnitudes::const_iterator it = mPermanentMagicEffectMagnitudes.begin(); it != mPermanentMagicEffectMagnitudes.end(); ++it)