diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 60c5b0e1a..54908deec 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -2,6 +2,8 @@ #include "containerstore.hpp" #include +#include +#include MWWorld::ContainerStoreIterator MWWorld::ContainerStore::begin (int mask) { @@ -10,12 +12,78 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::begin (int mask) MWWorld::ContainerStoreIterator MWWorld::ContainerStore::end() { - return ContainerStoreIterator(); + return ContainerStoreIterator (this); } +void MWWorld::ContainerStore::add (const Ptr& ptr) +{ + /// \todo implement item stocking + + switch (getType (ptr)) + { + case Type_Potion: potions.list.push_back (*ptr.get()); break; + case Type_Apparatus: appas.list.push_back (*ptr.get()); break; + case Type_Armor: armors.list.push_back (*ptr.get()); break; + case Type_Book: books.list.push_back (*ptr.get()); break; + case Type_Clothing: clothes.list.push_back (*ptr.get()); break; + case Type_Ingredient: ingreds.list.push_back (*ptr.get()); break; + case Type_Light: lights.list.push_back (*ptr.get()); break; + case Type_Lockpick: lockpicks.list.push_back (*ptr.get()); break; + case Type_Miscellaneous: miscItems.list.push_back (*ptr.get()); break; + case Type_Probe: probes.list.push_back (*ptr.get()); break; + case Type_Repair: repairs.list.push_back (*ptr.get()); break; + case Type_Weapon: weapons.list.push_back (*ptr.get()); break; + } +} + +int MWWorld::ContainerStore::getType (const Ptr& ptr) +{ + if (ptr.isEmpty()) + throw std::runtime_error ("can't put a non-existent object into a container"); + + if (ptr.getTypeName()==typeid (ESM::Potion).name()) + return Type_Potion; + + if (ptr.getTypeName()==typeid (ESM::Apparatus).name()) + return Type_Apparatus; + + if (ptr.getTypeName()==typeid (ESM::Armor).name()) + return Type_Armor; + + if (ptr.getTypeName()==typeid (ESM::Book).name()) + return Type_Book; + + if (ptr.getTypeName()==typeid (ESM::Clothing).name()) + return Type_Clothing; + + if (ptr.getTypeName()==typeid (ESM::Ingredient).name()) + return Type_Ingredient; + + if (ptr.getTypeName()==typeid (ESM::Light).name()) + return Type_Light; + + if (ptr.getTypeName()==typeid (ESM::Tool).name()) + return Type_Lockpick; + + if (ptr.getTypeName()==typeid (ESM::Miscellaneous).name()) + return Type_Miscellaneous; + + if (ptr.getTypeName()==typeid (ESM::Probe).name()) + return Type_Probe; + + if (ptr.getTypeName()==typeid (ESM::Repair).name()) + return Type_Repair; + + if (ptr.getTypeName()==typeid (ESM::Weapon).name()) + return Type_Weapon; + + throw std::runtime_error ( + "Object of type " + ptr.getTypeName() + " can not be placed into a container"); +} -MWWorld::ContainerStoreIterator::ContainerStoreIterator() : mType (-1), mMask (0), mContainer (0) +MWWorld::ContainerStoreIterator::ContainerStoreIterator (ContainerStore *container) +: mType (-1), mMask (0), mContainer (container) {} MWWorld::ContainerStoreIterator::ContainerStoreIterator (int mask, ContainerStore *container) @@ -209,7 +277,7 @@ MWWorld::Ptr MWWorld::ContainerStoreIterator::operator*() const case ContainerStore::Type_Weapon: return MWWorld::Ptr (&*mWeapon, 0); } - return MWWorld::Ptr(); + throw std::runtime_error ("invalid pointer"); } MWWorld::ContainerStoreIterator& MWWorld::ContainerStoreIterator::operator++() @@ -252,6 +320,7 @@ bool MWWorld::ContainerStoreIterator::isEqual (const ContainerStoreIterator& ite case ContainerStore::Type_Probe: return mProbe==iter.mProbe; case ContainerStore::Type_Repair: return mRepair==iter.mRepair; case ContainerStore::Type_Weapon: return mWeapon==iter.mWeapon; + case -1: return true; } return false; diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index 9fcdb99c6..99091ff67 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -31,6 +31,8 @@ namespace MWWorld static const int Type_All = 0xffff; +// private: + ESMS::CellRefList potions; ESMS::CellRefList appas; ESMS::CellRefList armors; @@ -44,10 +46,24 @@ namespace MWWorld ESMS::CellRefList repairs; ESMS::CellRefList weapons; + public: + ContainerStoreIterator begin (int mask = Type_All); ContainerStoreIterator end(); + void add (const Ptr& ptr); + ///< Add the item pointed to by \a ptr to this container. + /// + /// \note The item pointed to is not required to exist beyond this function call. + /// + /// \attention Do not add items to an existing stack by increasing the count instead of + /// calling this function! + + static int getType (const Ptr& ptr); + ///< This function throws an exception, if ptr does not point to an object, that can be + /// put into a container. + friend class ContainerStoreIterator; }; @@ -76,7 +92,7 @@ namespace MWWorld private: - ContainerStoreIterator(); + ContainerStoreIterator (ContainerStore *container); ///< End-iterator ContainerStoreIterator (int mask, ContainerStore *container);