1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 07:49:56 +00:00
openmw-tes3mp/apps/openmw/mwclass/npc.cpp

277 lines
7.7 KiB
C++
Raw Normal View History

2010-08-03 11:03:08 +00:00
#include "npc.hpp"
#include <components/esm/loadnpc.hpp>
2010-08-03 11:32:37 +00:00
#include "../mwmechanics/creaturestats.hpp"
2010-08-19 10:49:13 +00:00
#include "../mwmechanics/npcstats.hpp"
2010-08-03 11:32:37 +00:00
#include "../mwworld/ptr.hpp"
#include "../mwworld/actiontalk.hpp"
#include "../mwworld/environment.hpp"
#include "../mwworld/world.hpp"
#include "../mwmechanics/mechanicsmanager.hpp"
2010-11-25 23:36:25 +00:00
#include <OgreSceneNode.h>
2010-08-03 11:03:08 +00:00
2011-01-18 09:45:29 +00:00
namespace
{
const Ogre::Radian kOgrePi (Ogre::Math::PI);
const Ogre::Radian kOgrePiOverTwo (Ogre::Math::PI / Ogre::Real(2.0));
}
namespace MWClass
2010-08-03 11:03:08 +00:00
{
2010-08-08 12:28:35 +00:00
std::string Npc::getId (const MWWorld::Ptr& ptr) const
{
ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
ptr.get<ESM::NPC>();
return ref->base->mId;
}
2011-11-12 04:01:12 +00:00
void Npc::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
{
2011-12-09 05:08:30 +00:00
renderingInterface.getActors().insertNPC(ptr);
2011-11-12 04:01:12 +00:00
}
2010-12-22 02:45:54 +00:00
2011-11-12 04:01:12 +00:00
void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics, MWWorld::Environment& environment) const
{
2011-11-17 22:10:27 +00:00
2011-12-25 02:53:12 +00:00
2011-11-12 04:01:12 +00:00
ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
ptr.get<ESM::NPC>();
2011-01-18 09:45:29 +00:00
2010-11-25 23:36:25 +00:00
2011-11-12 04:01:12 +00:00
const std::string &model = ref->base->model;
assert (ref->base != NULL);
std::string headID = ref->base->head;
std::string bodyRaceID = headID.substr(0, headID.find_last_of("head_") - 4);
bool beast = bodyRaceID == "b_n_khajiit_m_" || bodyRaceID == "b_n_khajiit_f_" || bodyRaceID == "b_n_argonian_m_" || bodyRaceID == "b_n_argonian_f_";
std::string smodel = "meshes\\base_anim.nif";
if(beast)
smodel = "meshes\\base_animkna.nif";
physics.insertActorPhysics(ptr, smodel);
2011-01-05 21:18:21 +00:00
}
void Npc::enable (const MWWorld::Ptr& ptr, MWWorld::Environment& environment) const
{
environment.mMechanicsManager->addActor (ptr);
}
void Npc::disable (const MWWorld::Ptr& ptr, MWWorld::Environment& environment) const
{
environment.mMechanicsManager->removeActor (ptr);
}
2010-08-03 15:11:41 +00:00
std::string Npc::getName (const MWWorld::Ptr& ptr) const
{
ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
ptr.get<ESM::NPC>();
return ref->base->name;
}
MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
2010-08-03 11:03:08 +00:00
{
if (!ptr.getRefData().getCreatureStats().get())
{
boost::shared_ptr<MWMechanics::CreatureStats> stats (
new MWMechanics::CreatureStats);
ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref = ptr.get<ESM::NPC>();
2010-08-03 11:03:08 +00:00
stats->mAttributes[0].set (ref->base->npdt52.strength);
stats->mAttributes[1].set (ref->base->npdt52.intelligence);
stats->mAttributes[2].set (ref->base->npdt52.willpower);
stats->mAttributes[3].set (ref->base->npdt52.agility);
stats->mAttributes[4].set (ref->base->npdt52.speed);
stats->mAttributes[5].set (ref->base->npdt52.endurance);
stats->mAttributes[6].set (ref->base->npdt52.personality);
stats->mAttributes[7].set (ref->base->npdt52.luck);
stats->mDynamic[0].set (ref->base->npdt52.health);
stats->mDynamic[1].set (ref->base->npdt52.mana);
stats->mDynamic[2].set (ref->base->npdt52.fatigue);
2010-09-15 13:32:35 +00:00
stats->mLevel = ref->base->npdt52.level;
2010-08-03 11:03:08 +00:00
ptr.getRefData().getCreatureStats() = stats;
}
return *ptr.getRefData().getCreatureStats();
}
2010-08-19 10:49:13 +00:00
MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
{
if (!ptr.getRefData().getNpcStats().get())
{
boost::shared_ptr<MWMechanics::NpcStats> stats (
new MWMechanics::NpcStats);
ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref = ptr.get<ESM::NPC>();
if (!ref->base->faction.empty())
{
// TODO research how initial rank is stored. The information in loadnpc.hpp are at
// best very unclear.
stats->mFactionRank[ref->base->faction] = 0;
}
for (int i=0; i<27; ++i)
stats->mSkill[i].setBase (ref->base->npdt52.skills[i]);
2010-08-19 10:49:13 +00:00
ptr.getRefData().getNpcStats() = stats;
}
return *ptr.getRefData().getNpcStats();
}
boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{
return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
}
MWWorld::ContainerStore<MWWorld::RefData>& Npc::getContainerStore (const MWWorld::Ptr& ptr)
const
2010-08-04 12:37:23 +00:00
{
if (!ptr.getRefData().getContainerStore().get())
{
boost::shared_ptr<MWWorld::ContainerStore<MWWorld::RefData> > store (
new MWWorld::ContainerStore<MWWorld::RefData>);
// TODO add initial content
ptr.getRefData().getContainerStore() = store;
}
return *ptr.getRefData().getContainerStore();
}
std::string Npc::getScript (const MWWorld::Ptr& ptr) const
{
ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
ptr.get<ESM::NPC>();
return ref->base->script;
}
2011-01-18 09:45:29 +00:00
void Npc::setForceStance (const MWWorld::Ptr& ptr, Stance stance, bool force) const
{
MWMechanics::NpcStats& stats = getNpcStats (ptr);
switch (stance)
{
case Run:
stats.mForceRun = force;
break;
case Sneak:
stats.mForceSneak = force;
break;
case Combat:
throw std::runtime_error ("combat stance not enforcable for NPCs");
}
}
void Npc::setStance (const MWWorld::Ptr& ptr, Stance stance, bool set) const
{
MWMechanics::NpcStats& stats = getNpcStats (ptr);
switch (stance)
{
case Run:
2011-02-10 10:08:25 +00:00
stats.mRun = set;
break;
2011-01-18 09:45:29 +00:00
case Sneak:
stats.mSneak = set;
break;
case Combat:
stats.mCombat = set;
break;
}
}
bool Npc::getStance (const MWWorld::Ptr& ptr, Stance stance, bool ignoreForce) const
{
MWMechanics::NpcStats& stats = getNpcStats (ptr);
switch (stance)
{
case Run:
2011-02-10 10:08:25 +00:00
if (!ignoreForce && stats.mForceRun)
return true;
return stats.mRun;
2011-01-18 09:45:29 +00:00
case Sneak:
if (!ignoreForce && stats.mForceSneak)
return true;
return stats.mSneak;
case Combat:
return stats.mCombat;
}
return false;
}
float Npc::getSpeed (const MWWorld::Ptr& ptr) const
{
return getStance (ptr, Run) ? 600 : 300; // TODO calculate these values from stats
}
MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
{
if (!ptr.getRefData().getMovement().get())
{
boost::shared_ptr<MWMechanics::Movement> movement (
new MWMechanics::Movement);
2011-02-03 11:16:20 +00:00
ptr.getRefData().getMovement() = movement;
}
return *ptr.getRefData().getMovement();
}
Ogre::Vector3 Npc::getMovementVector (const MWWorld::Ptr& ptr) const
{
Ogre::Vector3 vector (0, 0, 0);
if (ptr.getRefData().getMovement().get())
{
vector.x = - ptr.getRefData().getMovement()->mLeftRight * 200;
2011-03-11 18:39:45 +00:00
vector.y = ptr.getRefData().getMovement()->mForwardBackward * 200;
if (getStance (ptr, Run, false))
vector *= 2;
}
return vector;
}
2010-08-03 11:03:08 +00:00
void Npc::registerSelf()
{
boost::shared_ptr<Class> instance (new Npc);
registerClass (typeid (ESM::NPC).name(), instance);
}
}