added item health interface

This commit is contained in:
Marc Zinnschlag 2010-08-03 14:14:04 +02:00
parent 93754b2851
commit bfc282779b
9 changed files with 140 additions and 44 deletions

View file

@ -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})

View file

@ -0,0 +1,31 @@
#include "armor.hpp"
#include <components/esm/loadarmo.hpp>
#include <components/esm_store/cell_store.hpp>
#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<ESM::Armor, MWWorld::RefData> *ref =
ptr.get<ESM::Armor>();
return ref->base->data.health;
}
void Armor::registerSelf()
{
boost::shared_ptr<Class> instance (new Armor);
registerClass (typeid (ESM::Armor).name(), instance);
}
}

View file

@ -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

View file

@ -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();
}
}

View file

@ -0,0 +1,31 @@
#include "weapon.hpp"
#include <components/esm/loadweap.hpp>
#include <components/esm_store/cell_store.hpp>
#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<ESM::Weapon, MWWorld::RefData> *ref =
ptr.get<ESM::Weapon>();
return ref->base->data.health;
}
void Weapon::registerSelf()
{
boost::shared_ptr<Class> instance (new Weapon);
registerClass (typeid (ESM::Weapon).name(), instance);
}
}

View file

@ -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

View file

@ -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<ESM::Weapon, MWWorld::RefData> *))
{
ESMS::LiveCellRef<ESM::Weapon, MWWorld::RefData> *ref =
context.getReference().get<ESM::Weapon>();
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<ESM::Armor, MWWorld::RefData> *))
{
ESMS::LiveCellRef<ESM::Armor, MWWorld::RefData> *ref =
context.getReference().get<ESM::Armor>();
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<ESM::Weapon, MWWorld::RefData> *))
{
ESMS::LiveCellRef<ESM::Weapon, MWWorld::RefData> *ref =
context.getWorld().getPtr (id, false).get<ESM::Weapon>();
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<ESM::Armor, MWWorld::RefData> *))
{
ESMS::LiveCellRef<ESM::Armor, MWWorld::RefData> *ref =
context.getWorld().getPtr (id, false).get<ESM::Armor>();
Interpreter::Type_Integer value = ref->base->data.health;
runtime.push (value);
return;
}
return;
}
Interpreter::Type_Integer value =

View file

@ -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<std::string, boost::shared_ptr<Class> >::const_iterator iter = sClasses.find (key);

View file

@ -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.