diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index b8784dc81..bf188b963 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -96,12 +96,16 @@ set(GAMECLASS mwclass/activator.cpp mwclass/creature.cpp mwclass/npc.cpp + mwclass/weapon.cpp + mwclass/armor.cpp ) set(GAMECLASS_HEADER mwclass/classes.hpp mwclass/activator.hpp mwclass/creature.hpp mwclass/npc.hpp + mwclass/weapon.hpp + mwclass/armor.hpp ) source_group(apps\\openmw\\mwclass FILES ${GAMECLASS} ${GAMECLASS_HEADER}) diff --git a/apps/openmw/mwclass/armor.cpp b/apps/openmw/mwclass/armor.cpp new file mode 100644 index 000000000..cdeeb06e0 --- /dev/null +++ b/apps/openmw/mwclass/armor.cpp @@ -0,0 +1,31 @@ + +#include "armor.hpp" + +#include + +#include + +#include "../mwworld/ptr.hpp" + +namespace MWClass +{ + bool Armor::hasItemHealth (const MWWorld::Ptr& ptr) const + { + return true; + } + + int Armor::getItemMaxHealth (const MWWorld::Ptr& ptr) const + { + ESMS::LiveCellRef *ref = + ptr.get(); + + return ref->base->data.health; + } + + void Armor::registerSelf() + { + boost::shared_ptr instance (new Armor); + + registerClass (typeid (ESM::Armor).name(), instance); + } +} diff --git a/apps/openmw/mwclass/armor.hpp b/apps/openmw/mwclass/armor.hpp new file mode 100644 index 000000000..0bc3856ce --- /dev/null +++ b/apps/openmw/mwclass/armor.hpp @@ -0,0 +1,22 @@ +#ifndef GAME_MWCLASS_ARMOR_H +#define GAME_MWCLASS_ARMOR_H + +#include "../mwworld/class.hpp" + +namespace MWClass +{ + class Armor : public MWWorld::Class + { + public: + + virtual bool hasItemHealth (const MWWorld::Ptr& ptr) const; + ///< \return Item health data available? + + virtual int getItemMaxHealth (const MWWorld::Ptr& ptr) const; + ///< Return item max health or throw an exception, if class does not have item health + + static void registerSelf(); + }; +} + +#endif diff --git a/apps/openmw/mwclass/classes.cpp b/apps/openmw/mwclass/classes.cpp index 16f3fab4d..655159e35 100644 --- a/apps/openmw/mwclass/classes.cpp +++ b/apps/openmw/mwclass/classes.cpp @@ -4,6 +4,8 @@ #include "activator.hpp" #include "creature.hpp" #include "npc.hpp" +#include "weapon.hpp" +#include "armor.hpp" namespace MWClass { @@ -12,5 +14,7 @@ namespace MWClass Activator::registerSelf(); Creature::registerSelf(); Npc::registerSelf(); + Weapon::registerSelf(); + Armor::registerSelf(); } } diff --git a/apps/openmw/mwclass/weapon.cpp b/apps/openmw/mwclass/weapon.cpp new file mode 100644 index 000000000..de45582ef --- /dev/null +++ b/apps/openmw/mwclass/weapon.cpp @@ -0,0 +1,31 @@ + +#include "weapon.hpp" + +#include + +#include + +#include "../mwworld/ptr.hpp" + +namespace MWClass +{ + bool Weapon::hasItemHealth (const MWWorld::Ptr& ptr) const + { + return true; + } + + int Weapon::getItemMaxHealth (const MWWorld::Ptr& ptr) const + { + ESMS::LiveCellRef *ref = + ptr.get(); + + return ref->base->data.health; + } + + void Weapon::registerSelf() + { + boost::shared_ptr instance (new Weapon); + + registerClass (typeid (ESM::Weapon).name(), instance); + } +} diff --git a/apps/openmw/mwclass/weapon.hpp b/apps/openmw/mwclass/weapon.hpp new file mode 100644 index 000000000..e14750bca --- /dev/null +++ b/apps/openmw/mwclass/weapon.hpp @@ -0,0 +1,22 @@ +#ifndef GAME_MWCLASS_WEAPON_H +#define GAME_MWCLASS_WEAPON_H + +#include "../mwworld/class.hpp" + +namespace MWClass +{ + class Weapon : public MWWorld::Class + { + public: + + virtual bool hasItemHealth (const MWWorld::Ptr& ptr) const; + ///< \return Item health data available? + + virtual int getItemMaxHealth (const MWWorld::Ptr& ptr) const; + ///< Return item max health or throw an exception, if class does not have item health + + static void registerSelf(); + }; +} + +#endif diff --git a/apps/openmw/mwscript/statsextensions.cpp b/apps/openmw/mwscript/statsextensions.cpp index 106f1b72e..e28b5593b 100644 --- a/apps/openmw/mwscript/statsextensions.cpp +++ b/apps/openmw/mwscript/statsextensions.cpp @@ -186,31 +186,14 @@ namespace MWScript MWWorld::Ptr ptr = context.getReference(); - if (mIndex==0) + if (mIndex==0 && MWWorld::Class::get (ptr).hasItemHealth (ptr)) { // health is a special case - if (context.getReference().getType()== - typeid (ESMS::LiveCellRef *)) - { - ESMS::LiveCellRef *ref = - context.getReference().get(); + Interpreter::Type_Integer value = + MWWorld::Class::get (ptr).getItemMaxHealth (ptr); + runtime.push (value); - Interpreter::Type_Integer value = ref->base->data.health; - runtime.push (value); - - return; - } - else if (context.getReference().getType()== - typeid (ESMS::LiveCellRef *)) - { - ESMS::LiveCellRef *ref = - context.getReference().get(); - - Interpreter::Type_Integer value = ref->base->data.health; - runtime.push (value); - - return; - } + return; } Interpreter::Type_Integer value = @@ -239,31 +222,14 @@ namespace MWScript MWWorld::Ptr ptr = context.getWorld().getPtr (id, false); - if (mIndex==0) + if (mIndex==0 && MWWorld::Class::get (ptr).hasItemHealth (ptr)) { // health is a special case - if (context.getWorld().getPtr (id, false).getType()== - typeid (ESMS::LiveCellRef *)) - { - ESMS::LiveCellRef *ref = - context.getWorld().getPtr (id, false).get(); + Interpreter::Type_Integer value = + MWWorld::Class::get (ptr).getItemMaxHealth (ptr); + runtime.push (value); - Interpreter::Type_Integer value = ref->base->data.health; - runtime.push (value); - - return; - } - else if (context.getWorld().getPtr (id, false).getType()== - typeid (ESMS::LiveCellRef *)) - { - ESMS::LiveCellRef *ref = - context.getWorld().getPtr (id, false).get(); - - Interpreter::Type_Integer value = ref->base->data.health; - runtime.push (value); - - return; - } + return; } Interpreter::Type_Integer value = diff --git a/apps/openmw/mwworld/class.cpp b/apps/openmw/mwworld/class.cpp index 9a2020337..e5840b96d 100644 --- a/apps/openmw/mwworld/class.cpp +++ b/apps/openmw/mwworld/class.cpp @@ -18,6 +18,16 @@ namespace MWWorld throw std::runtime_error ("class does not have creature stats"); } + bool Class::hasItemHealth (const Ptr& ptr) const + { + return false; + } + + int Class::getItemMaxHealth (const Ptr& ptr) const + { + throw std::runtime_error ("class does not have item health"); + } + const Class& Class::get (const std::string& key) { std::map >::const_iterator iter = sClasses.find (key); diff --git a/apps/openmw/mwworld/class.hpp b/apps/openmw/mwworld/class.hpp index 3144a7108..3d9eee7be 100644 --- a/apps/openmw/mwworld/class.hpp +++ b/apps/openmw/mwworld/class.hpp @@ -36,6 +36,12 @@ namespace MWWorld ///< Return creature stats or throw an exception, if class does not have creature stats /// (default implementation: throw an exceoption) + virtual bool hasItemHealth (const Ptr& ptr) const; + ///< \return Item health data available? (default implementation: false) + + virtual int getItemMaxHealth (const Ptr& ptr) const; + ///< Return item max health or throw an exception, if class does not have item health + /// (default implementation: throw an exceoption) static const Class& get (const std::string& key); ///< If there is no class for this \a key, an exception is thrown.