slot system

This commit is contained in:
Marc Zinnschlag 2012-03-13 13:31:11 +01:00
parent 222b5b741e
commit 635b7ec6cb
3 changed files with 112 additions and 0 deletions

View file

@ -1,6 +1,8 @@
#ifndef GAME_MWWORLD_CONTAINERSTORE_H
#define GAME_MWWORLD_CONTAINERSTORE_H
#include <iterator>
#include <components/esm_store/cell_store.hpp>
#include "refdata.hpp"
@ -84,6 +86,7 @@ namespace MWWorld
///
/// \note The iterator will automatically skip over deleted objects.
class ContainerStoreIterator
: public std::iterator<std::forward_iterator_tag, Ptr, std::ptrdiff_t, Ptr *, Ptr&>
{
int mType;
int mMask;

View file

@ -1,2 +1,70 @@
#include "inventorystore.hpp"
#include <iterator>
void MWWorld::InventoryStore::copySlots (const InventoryStore& store)
{
// some const-trickery, required because of a flaw in the handling of MW-references and the
// resulting workarounds
for (std::vector<ContainerStoreIterator>::const_iterator iter (
const_cast<InventoryStore&> (store).mSlots.begin());
iter!=const_cast<InventoryStore&> (store).mSlots.end(); ++iter)
{
std::size_t distance = std::distance (const_cast<InventoryStore&> (store).begin(), *iter);
ContainerStoreIterator slot = begin();
std::advance (slot, distance);
mSlots.push_back (slot);
}
}
MWWorld::InventoryStore::InventoryStore()
{
for (int i=0; i<Slots; ++i)
mSlots.push_back (end());
}
MWWorld::InventoryStore::InventoryStore (const InventoryStore& store)
: ContainerStore (store)
{
copySlots (store);
}
MWWorld::InventoryStore& MWWorld::InventoryStore::operator= (const InventoryStore& store)
{
ContainerStore::operator= (store);
mSlots.clear();
copySlots (store);
return *this;
}
void MWWorld::InventoryStore::equip (int slot, const ContainerStoreIterator& iterator)
{
if (slot<0 || slot>=static_cast<int> (mSlots.size()))
throw std::runtime_error ("slot number out of range");
/// \todo verify slot
mSlots[slot] = iterator;
}
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot)
{
if (slot<0 || slot>=static_cast<int> (mSlots.size()))
throw std::runtime_error ("slot number out of range");
if (mSlots[slot]==end())
return end();
if (mSlots[slot]->getRefData().getCount()<1)
{
// object has been deleted
mSlots[slot] = end();
return end();
}
return mSlots[slot];
}

View file

@ -8,8 +8,49 @@ namespace MWWorld
///< \brief Variant of the ContainerStore for NPCs
class InventoryStore : public ContainerStore
{
public:
static const int Slot_Helmet = 0;
static const int Slot_Cuirass = 1;
static const int Slot_Greaves = 2;
static const int Slot_LeftPauldron = 3;
static const int Slot_RightPauldron = 4;
static const int Slot_LeftGauntlet = 5;
static const int Slot_RightGauntlet = 6;
static const int Slot_Boots = 7;
static const int Slot_Shirt = 8;
static const int Slot_Pants = 9;
static const int Slot_Skirt = 10;
static const int Slot_Robe = 11;
static const int Slot_LeftRing = 12;
static const int Slot_RightRing = 13;
static const int Slot_Amulet = 14;
static const int Slot_Belt = 15;
static const int Slot_CarriedRight = 16;
static const int Slot_CarriedLeft = 17;
static const int Slot_Ammunition = 18;
static const int Slots = 19;
static const int Slot_NoSlot = -1;
private:
mutable std::vector<ContainerStoreIterator> mSlots;
void copySlots (const InventoryStore& store);
public:
InventoryStore();
InventoryStore (const InventoryStore& store);
InventoryStore& operator= (const InventoryStore& store);
void equip (int slot, const ContainerStoreIterator& iterator);
ContainerStoreIterator getSlot (int slot);
};
}