1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-10 15:39:02 +00:00
OpenMW/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp

343 lines
12 KiB
C++
Raw Normal View History

#include "mechanicsmanagerimp.hpp"
2012-10-01 15:17:04 +00:00
#include "../mwworld/esmstore.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/player.hpp"
namespace MWMechanics
{
2010-09-15 11:31:26 +00:00
void MechanicsManager::buildPlayer()
{
MWWorld::Ptr ptr = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
2010-09-15 11:31:26 +00:00
MWMechanics::CreatureStats& creatureStats = MWWorld::Class::get (ptr).getCreatureStats (ptr);
MWMechanics::NpcStats& npcStats = MWWorld::Class::get (ptr).getNpcStats (ptr);
2010-09-15 11:31:26 +00:00
2012-11-05 12:07:59 +00:00
const ESM::NPC *player = ptr.get<ESM::NPC>()->mBase;
2010-09-15 11:31:26 +00:00
// reset
2012-09-17 07:37:50 +00:00
creatureStats.setLevel(player->mNpdt52.mLevel);
2012-07-22 14:29:54 +00:00
creatureStats.getSpells().clear();
creatureStats.setMagicEffects(MagicEffects());
2010-09-15 11:31:26 +00:00
for (int i=0; i<27; ++i)
2012-09-17 07:37:50 +00:00
npcStats.getSkill (i).setBase (player->mNpdt52.mSkills[i]);
2012-09-17 07:37:50 +00:00
creatureStats.getAttribute(0).setBase (player->mNpdt52.mStrength);
creatureStats.getAttribute(1).setBase (player->mNpdt52.mIntelligence);
creatureStats.getAttribute(2).setBase (player->mNpdt52.mWillpower);
creatureStats.getAttribute(3).setBase (player->mNpdt52.mAgility);
creatureStats.getAttribute(4).setBase (player->mNpdt52.mSpeed);
creatureStats.getAttribute(5).setBase (player->mNpdt52.mEndurance);
creatureStats.getAttribute(6).setBase (player->mNpdt52.mPersonality);
creatureStats.getAttribute(7).setBase (player->mNpdt52.mLuck);
2012-05-28 09:37:56 +00:00
2010-09-15 11:31:26 +00:00
// race
2010-09-26 08:01:30 +00:00
if (mRaceSelected)
{
const ESM::Race *race =
MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find (
MWBase::Environment::get().getWorld()->getPlayer().getRace()
);
bool male = MWBase::Environment::get().getWorld()->getPlayer().isMale();
2010-09-26 08:01:30 +00:00
for (int i=0; i<8; ++i)
{
2010-09-26 08:01:30 +00:00
const ESM::Race::MaleFemale *attribute = 0;
switch (i)
{
2012-09-17 07:37:50 +00:00
case 0: attribute = &race->mData.mStrength; break;
case 1: attribute = &race->mData.mIntelligence; break;
case 2: attribute = &race->mData.mWillpower; break;
case 3: attribute = &race->mData.mAgility; break;
case 4: attribute = &race->mData.mSpeed; break;
case 5: attribute = &race->mData.mEndurance; break;
case 6: attribute = &race->mData.mPersonality; break;
case 7: attribute = &race->mData.mLuck; break;
2010-09-26 08:01:30 +00:00
}
2012-07-22 14:29:54 +00:00
creatureStats.getAttribute(i).setBase (
2012-09-17 07:37:50 +00:00
static_cast<int> (male ? attribute->mMale : attribute->mFemale));
2010-09-26 08:01:30 +00:00
}
for (int i=0; i<27; ++i)
{
int bonus = 0;
for (int i2=0; i2<7; ++i2)
2012-09-17 07:37:50 +00:00
if (race->mData.mBonus[i2].mSkill==i)
{
2012-09-17 07:37:50 +00:00
bonus = race->mData.mBonus[i2].mBonus;
break;
}
npcStats.getSkill (i).setBase (5 + bonus);
}
2010-09-26 08:01:30 +00:00
2012-09-17 07:37:50 +00:00
for (std::vector<std::string>::const_iterator iter (race->mPowers.mList.begin());
iter!=race->mPowers.mList.end(); ++iter)
{
2012-07-22 14:29:54 +00:00
creatureStats.getSpells().add (*iter);
}
}
2010-09-15 11:31:26 +00:00
// birthsign
if (!MWBase::Environment::get().getWorld()->getPlayer().getBirthsign().empty())
2010-09-26 08:01:30 +00:00
{
const ESM::BirthSign *sign =
MWBase::Environment::get().getWorld()->getStore().get<ESM::BirthSign>().find (
MWBase::Environment::get().getWorld()->getPlayer().getBirthsign());
2012-09-17 07:37:50 +00:00
for (std::vector<std::string>::const_iterator iter (sign->mPowers.mList.begin());
iter!=sign->mPowers.mList.end(); ++iter)
{
2012-07-22 14:29:54 +00:00
creatureStats.getSpells().add (*iter);
}
2010-09-26 08:01:30 +00:00
}
2010-09-15 11:31:26 +00:00
// class
2010-09-26 08:01:30 +00:00
if (mClassSelected)
{
const ESM::Class& class_ = MWBase::Environment::get().getWorld()->getPlayer().getClass();
2010-09-26 08:01:30 +00:00
for (int i=0; i<2; ++i)
{
2012-09-17 07:37:50 +00:00
int attribute = class_.mData.mAttribute[i];
2010-09-26 08:01:30 +00:00
if (attribute>=0 && attribute<8)
{
2012-07-22 14:29:54 +00:00
creatureStats.getAttribute(attribute).setBase (
creatureStats.getAttribute(attribute).getBase() + 10);
2010-09-26 08:01:30 +00:00
}
}
2010-09-26 08:01:30 +00:00
for (int i=0; i<2; ++i)
{
2010-09-26 08:01:30 +00:00
int bonus = i==0 ? 10 : 25;
2010-09-26 08:01:30 +00:00
for (int i2=0; i2<5; ++i2)
{
2012-09-17 07:37:50 +00:00
int index = class_.mData.mSkills[i2][i];
2010-09-26 08:01:30 +00:00
if (index>=0 && index<27)
{
npcStats.getSkill (index).setBase (
npcStats.getSkill (index).getBase() + bonus);
2010-09-26 08:01:30 +00:00
}
}
}
2010-09-15 11:31:26 +00:00
const MWWorld::Store<ESM::Skill> &skills =
MWBase::Environment::get().getWorld()->getStore().get<ESM::Skill>();
MWWorld::Store<ESM::Skill>::iterator iter = skills.begin();
for (; iter != skills.end(); ++iter)
{
if (iter->mData.mSpecialization==class_.mData.mSpecialization)
{
int index = iter->mIndex;
2010-09-26 08:01:30 +00:00
if (index>=0 && index<27)
{
npcStats.getSkill (index).setBase (
npcStats.getSkill (index).getBase() + 5);
2010-09-26 08:01:30 +00:00
}
}
}
}
// forced update and current value adjustments
mActors.updateActor (ptr, 0);
for (int i=0; i<2; ++i)
{
DynamicStat<float> stat = creatureStats.getDynamic (i);
stat.setCurrent (stat.getModified());
creatureStats.setDynamic (i, stat);
}
2010-09-15 11:31:26 +00:00
}
MechanicsManager::MechanicsManager()
: mUpdatePlayer (true), mClassSelected (false),
mRaceSelected (false)
{
2010-09-15 11:31:26 +00:00
buildPlayer();
}
void MechanicsManager::addActor (const MWWorld::Ptr& ptr)
{
mActors.addActor (ptr);
}
void MechanicsManager::removeActor (const MWWorld::Ptr& ptr)
{
2011-02-10 09:38:45 +00:00
if (ptr==mWatched)
mWatched = MWWorld::Ptr();
mActors.removeActor (ptr);
}
void MechanicsManager::dropActors (const MWWorld::Ptr::CellStore *cellStore)
{
2011-02-10 09:38:45 +00:00
if (!mWatched.isEmpty() && mWatched.getCell()==cellStore)
mWatched = MWWorld::Ptr();
mActors.dropActors (cellStore);
}
void MechanicsManager::watchActor (const MWWorld::Ptr& ptr)
{
mWatched = ptr;
}
void MechanicsManager::update (std::vector<std::pair<std::string, Ogre::Vector3> >& movement,
float duration, bool paused)
{
if (!mWatched.isEmpty())
{
MWMechanics::CreatureStats& stats =
MWWorld::Class::get (mWatched).getCreatureStats (mWatched);
2010-09-21 15:42:07 +00:00
MWMechanics::NpcStats& npcStats =
MWWorld::Class::get (mWatched).getNpcStats (mWatched);
static const char *attributeNames[8] =
{
"AttribVal1", "AttribVal2", "AttribVal3", "AttribVal4", "AttribVal5",
"AttribVal6", "AttribVal7", "AttribVal8"
};
static const char *dynamicNames[3] =
{
"HBar", "MBar", "FBar"
};
for (int i=0; i<8; ++i)
{
2012-07-22 14:29:54 +00:00
if (stats.getAttribute(i)!=mWatchedCreature.getAttribute(i))
{
2012-07-22 14:29:54 +00:00
mWatchedCreature.setAttribute(i, stats.getAttribute(i));
2012-07-22 14:29:54 +00:00
MWBase::Environment::get().getWindowManager()->setValue (attributeNames[i], stats.getAttribute(i));
}
}
2012-07-22 14:29:54 +00:00
if (stats.getHealth() != mWatchedCreature.getHealth()) {
mWatchedCreature.setHealth(stats.getHealth());
MWBase::Environment::get().getWindowManager()->setValue(dynamicNames[0], stats.getHealth());
}
if (stats.getMagicka() != mWatchedCreature.getMagicka()) {
mWatchedCreature.setMagicka(stats.getMagicka());
MWBase::Environment::get().getWindowManager()->setValue(dynamicNames[1], stats.getMagicka());
}
if (stats.getFatigue() != mWatchedCreature.getFatigue()) {
mWatchedCreature.setFatigue(stats.getFatigue());
MWBase::Environment::get().getWindowManager()->setValue(dynamicNames[2], stats.getFatigue());
}
2010-09-15 13:32:35 +00:00
2010-09-21 15:42:07 +00:00
bool update = false;
//Loop over ESM::Skill::SkillEnum
for(int i = 0; i < 27; ++i)
2010-09-21 15:42:07 +00:00
{
if(npcStats.getSkill (i) != mWatchedNpc.getSkill (i))
2010-09-21 15:42:07 +00:00
{
update = true;
mWatchedNpc.getSkill (i) = npcStats.getSkill (i);
MWBase::Environment::get().getWindowManager()->setValue((ESM::Skill::SkillEnum)i, npcStats.getSkill (i));
2010-09-21 15:42:07 +00:00
}
}
if (update)
MWBase::Environment::get().getWindowManager()->updateSkillArea();
2010-09-21 15:42:07 +00:00
2012-07-22 14:29:54 +00:00
MWBase::Environment::get().getWindowManager()->setValue ("level", stats.getLevel());
}
if (mUpdatePlayer)
{
// basic player profile; should not change anymore after the creation phase is finished.
MWBase::WindowManager *winMgr =
MWBase::Environment::get().getWindowManager();
MWBase::World *world = MWBase::Environment::get().getWorld();
MWWorld::Player &player = world->getPlayer();
winMgr->setValue ("name", player.getName());
winMgr->setValue ("race", world->getStore().get<ESM::Race>().find (player.getRace())->mName);
winMgr->setValue ("class", player.getClass().mName);
mUpdatePlayer = false;
2010-09-21 15:42:07 +00:00
MWBase::WindowManager::SkillList majorSkills (5);
MWBase::WindowManager::SkillList minorSkills (5);
2010-09-21 15:42:07 +00:00
for (int i=0; i<5; ++i)
{
minorSkills[i] = player.getClass().mData.mSkills[i][0];
majorSkills[i] = player.getClass().mData.mSkills[i][1];
2010-09-21 15:42:07 +00:00
}
winMgr->configureSkills (majorSkills, minorSkills);
}
mActors.update (movement, duration, paused);
}
void MechanicsManager::restoreDynamicStats()
{
mActors.restoreDynamicStats ();
}
void MechanicsManager::setPlayerName (const std::string& name)
{
MWBase::Environment::get().getWorld()->getPlayer().setName (name);
mUpdatePlayer = true;
}
void MechanicsManager::setPlayerRace (const std::string& race, bool male)
{
MWBase::Environment::get().getWorld()->getPlayer().setGender (male);
MWBase::Environment::get().getWorld()->getPlayer().setRace (race);
2010-09-26 08:01:30 +00:00
mRaceSelected = true;
2010-09-15 11:31:26 +00:00
buildPlayer();
mUpdatePlayer = true;
}
void MechanicsManager::setPlayerBirthsign (const std::string& id)
{
MWBase::Environment::get().getWorld()->getPlayer().setBirthsign (id);
2010-09-15 11:31:26 +00:00
buildPlayer();
mUpdatePlayer = true;
}
void MechanicsManager::setPlayerClass (const std::string& id)
{
MWBase::Environment::get().getWorld()->getPlayer().setClass (
*MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>().find (id)
);
2010-09-26 08:01:30 +00:00
mClassSelected = true;
2010-09-15 11:31:26 +00:00
buildPlayer();
mUpdatePlayer = true;
}
void MechanicsManager::setPlayerClass (const ESM::Class& class_)
{
MWBase::Environment::get().getWorld()->getPlayer().setClass (class_);
2010-09-26 08:01:30 +00:00
mClassSelected = true;
2010-09-15 11:31:26 +00:00
buildPlayer();
mUpdatePlayer = true;
}
int MechanicsManager::countDeaths (const std::string& id) const
{
return mActors.countDeaths (id);
}
}