mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-18 13:12:50 +00:00
Fixing bug for merchant
This commit is contained in:
parent
46e07e4b19
commit
a662a00c62
@ -442,9 +442,11 @@ void MWWorld::ContainerStore::addInitialItem (const std::string& id, const std::
|
||||
// For a restocking levelled item, remember what we spawned so we can delete it later when the merchant restocks
|
||||
if (!levItem.empty() && count < 0)
|
||||
{
|
||||
if (mLevelledItemMap.find(id) == mLevelledItemMap.end())
|
||||
mLevelledItemMap[id] = 0;
|
||||
mLevelledItemMap[id] += std::abs(count);
|
||||
//If there is no item in map, insert it
|
||||
std::map<std::string, std::pair<int, std::string> >::iterator itemInMap =
|
||||
mLevelledItemMap.insert(std::make_pair(id, std::make_pair(0, levItem))).first;
|
||||
//Update spawned count
|
||||
itemInMap->second.first += std::abs(count);
|
||||
}
|
||||
count = std::abs(count);
|
||||
|
||||
@ -461,30 +463,56 @@ 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)
|
||||
{
|
||||
// Remove the items already spawned by levelled items that will restock
|
||||
for (std::map<std::string, int>::iterator it = mLevelledItemMap.begin(); it != mLevelledItemMap.end(); ++it)
|
||||
{
|
||||
if (count(it->first) >= it->second)
|
||||
remove(it->first, it->second, ptr);
|
||||
}
|
||||
mLevelledItemMap.clear();
|
||||
//allowedForReplace - Holds information about how many items from the list were sold;
|
||||
// Hence, tells us how many items we need to restock.
|
||||
//allowedForReplace[list] <- How many items we should generate(how many of these were sold)
|
||||
std::map<std::string, int> allowedForReplace;
|
||||
|
||||
//Check which lists need restocking:
|
||||
for (std::map<std::string, std::pair<int, std::string> >::iterator it = mLevelledItemMap.begin(); it != mLevelledItemMap.end(); ++it)
|
||||
{
|
||||
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)
|
||||
{
|
||||
//Create entry if it does not exist yet
|
||||
std::map<std::string, int>::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;
|
||||
}
|
||||
}
|
||||
|
||||
//Restock:
|
||||
//For every item that NPC could have
|
||||
for (std::vector<ESM::ContItem>::const_iterator it = items.mList.begin(); it != items.mList.end(); ++it)
|
||||
{
|
||||
//If he shouldn't have it restocked, don't restock it.
|
||||
if (it->mCount >= 0)
|
||||
continue;
|
||||
|
||||
std::string item = Misc::StringUtils::lowerCase(it->mItem.toString());
|
||||
std::string itemOrList = Misc::StringUtils::lowerCase(it->mItem.toString());
|
||||
|
||||
//If it's levelled list, restock if there's need to do so.
|
||||
if (MWBase::Environment::get().getWorld()->getStore().get<ESM::ItemLevList>().search(it->mItem.toString()))
|
||||
{
|
||||
addInitialItem(item, owner, it->mCount, true);
|
||||
std::map<std::string, int>::iterator listInMap = allowedForReplace.find(itemOrList);
|
||||
if(listInMap != allowedForReplace.end())
|
||||
addInitialItem(itemOrList, owner, listInMap->second, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
int currentCount = count(item);
|
||||
//Restocking static item - just restock to the max count
|
||||
int currentCount = count(itemOrList);
|
||||
if (currentCount < std::abs(it->mCount))
|
||||
addInitialItem(item, owner, std::abs(it->mCount) - currentCount, true);
|
||||
addInitialItem(itemOrList, owner, std::abs(it->mCount) - currentCount, true);
|
||||
}
|
||||
}
|
||||
flagAsModified();
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
|
||||
#include <components/esm/loadalch.hpp>
|
||||
#include <components/esm/loadappa.hpp>
|
||||
@ -68,9 +69,9 @@ namespace MWWorld
|
||||
MWWorld::CellRefList<ESM::Repair> repairs;
|
||||
MWWorld::CellRefList<ESM::Weapon> weapons;
|
||||
|
||||
std::map<std::string, int> mLevelledItemMap;
|
||||
///< Stores result of levelled item spawns. <refId, count>
|
||||
/// This is used to remove the spawned item(s) if the levelled item is restocked.
|
||||
std::map<std::string, std::pair<int, std::string> > mLevelledItemMap;
|
||||
///< Stores result of levelled item spawns. <refId, pair(count, spawningGroup)>
|
||||
/// This is used to restock levelled items(s) if the old item was sold.
|
||||
|
||||
mutable float mCachedWeight;
|
||||
mutable bool mWeightUpToDate;
|
||||
|
@ -36,6 +36,10 @@ void ESM::InventoryState::load (ESMReader &esm)
|
||||
{
|
||||
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();
|
||||
esm.getHNT (count, "COUN");
|
||||
mLevelledItemMap[id] = count;
|
||||
}
|
||||
@ -79,10 +83,11 @@ void ESM::InventoryState::save (ESMWriter &esm) const
|
||||
iter->save (esm, true);
|
||||
}
|
||||
|
||||
for (std::map<std::string, int>::const_iterator it = mLevelledItemMap.begin(); it != mLevelledItemMap.end(); ++it)
|
||||
for (std::map<std::string, std::pair<int, std::string> >::const_iterator it = mLevelledItemMap.begin(); it != mLevelledItemMap.end(); ++it)
|
||||
{
|
||||
esm.writeHNString ("LEVM", it->first);
|
||||
esm.writeHNT ("COUN", it->second);
|
||||
esm.writeHNT ("COUN", it->second.first);
|
||||
esm.writeHNString("LLST", it->second.second)
|
||||
}
|
||||
|
||||
for (TEffectMagnitudes::const_iterator it = mPermanentMagicEffectMagnitudes.begin(); it != mPermanentMagicEffectMagnitudes.end(); ++it)
|
||||
|
@ -20,7 +20,7 @@ namespace ESM
|
||||
// <Index in mItems, equipment slot>
|
||||
std::map<int, int> mEquipmentSlots;
|
||||
|
||||
std::map<std::string, int> mLevelledItemMap;
|
||||
std::map<std::string, std::pair<int, std::string> > mLevelledItemMap;
|
||||
|
||||
typedef std::map<std::string, std::vector<std::pair<float, float> > > TEffectMagnitudes;
|
||||
TEffectMagnitudes mPermanentMagicEffectMagnitudes;
|
||||
|
Loading…
x
Reference in New Issue
Block a user