Implemented levelled items

actorid
scrawl 12 years ago
parent a0d34c5e8d
commit 2d2196b0d6

@ -14,6 +14,8 @@
#include "../mwbase/world.hpp"
#include "../mwbase/scriptmanager.hpp"
#include "../mwmechanics/creaturestats.hpp"
#include "manualref.hpp"
#include "refdata.hpp"
#include "class.hpp"
@ -180,19 +182,70 @@ void MWWorld::ContainerStore::fill (const ESM::InventoryList& items, const MWWor
for (std::vector<ESM::ContItem>::const_iterator iter (items.mList.begin()); iter!=items.mList.end();
++iter)
{
ManualRef ref (store, iter->mItem.toString());
std::string id = iter->mItem.toString();
addInitialItem(id, iter->mCount);
}
flagAsModified();
}
void MWWorld::ContainerStore::addInitialItem (const std::string& id, int count, unsigned char failChance, bool topLevel)
{
count = std::abs(count); /// \todo implement item restocking (indicated by negative count)
ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), id);
if (ref.getPtr().getTypeName()==typeid (ESM::ItemLevList).name())
{
const ESM::ItemLevList* levItem = ref.getPtr().get<ESM::ItemLevList>()->mBase;
const std::vector<ESM::LeveledListBase::LevelItem>& items = levItem->mList;
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
int playerLevel = MWWorld::Class::get(player).getCreatureStats(player).getLevel();
failChance += levItem->mChanceNone;
if (ref.getPtr().getTypeName()==typeid (ESM::ItemLevList).name())
if (topLevel && count > 1 && levItem->mFlags & ESM::ItemLevList::Each)
{
/// \todo implement leveled item lists
continue;
for (int i=0; i<count; ++i)
addInitialItem(id, 1, failChance, false);
return;
}
ref.getPtr().getRefData().setCount (std::abs(iter->mCount)); /// \todo implement item restocking (indicated by negative count)
float random = static_cast<float> (std::rand()) / RAND_MAX;
if (random >= failChance/100.f)
{
std::vector<std::string> candidates;
int highestLevel = 0;
for (std::vector<ESM::LeveledListBase::LevelItem>::const_iterator it = items.begin(); it != items.end(); ++it)
{
if (it->mLevel > highestLevel)
highestLevel = it->mLevel;
}
std::pair<int, std::string> highest = std::make_pair(-1, "");
for (std::vector<ESM::LeveledListBase::LevelItem>::const_iterator it = items.begin(); it != items.end(); ++it)
{
if (playerLevel >= it->mLevel
&& (levItem->mFlags & ESM::ItemLevList::AllLevels || it->mLevel == highestLevel))
{
candidates.push_back(it->mId);
if (it->mLevel >= highest.first)
highest = std::make_pair(it->mLevel, it->mId);
}
}
if (!candidates.size())
return;
std::string item = candidates[std::rand()%candidates.size()];
addInitialItem(item, count, failChance, false);
}
}
else
{
ref.getPtr().getRefData().setCount (count);
addImp (ref.getPtr());
}
flagAsModified();
}
void MWWorld::ContainerStore::clear()

@ -53,6 +53,7 @@ namespace MWWorld
mutable float mCachedWeight;
mutable bool mWeightUpToDate;
ContainerStoreIterator addImp (const Ptr& ptr);
void addInitialItem (const std::string& id, int count, unsigned char failChance=0, bool topLevel=true);
public:

@ -22,17 +22,20 @@ struct LeveledListBase
{
enum Flags
{
AllLevels = 0x01, // Calculate from all levels <= player
// level, not just the closest below
// player.
Each = 0x02 // Select a new item each time this
Each = 0x01, // Select a new item each time this
// list is instantiated, instead of
// giving several identical items
}; // (used when a container has more
// (used when a container has more
// than one instance of one leveled
// list.)
AllLevels = 0x02 // Calculate from all levels <= player
// level, not just the closest below
// player.
};
int mFlags;
unsigned char mChanceNone; // Chance that none are selected (0-255?)
unsigned char mChanceNone; // Chance that none are selected (0-100)
std::string mId;
// Record name used to read references. Must be set before load() is

Loading…
Cancel
Save