Issue #324: Finished turning NpcStats into a proper class

actorid
Marc Zinnschlag 13 years ago
parent 771863e73b
commit d30ba14a17

@ -67,11 +67,11 @@ namespace MWClass
boost::algorithm::to_lower(faction);
if(ref->base->npdt52.gold != -10)
{
data->mNpcStats.mFactionRank[faction] = (int)ref->base->npdt52.rank;
data->mNpcStats.getFactionRanks()[faction] = (int)ref->base->npdt52.rank;
}
else
{
data->mNpcStats.mFactionRank[faction] = (int)ref->base->npdt12.rank;
data->mNpcStats.getFactionRanks()[faction] = (int)ref->base->npdt12.rank;
}
}
@ -79,7 +79,7 @@ namespace MWClass
if(ref->base->npdt52.gold != -10)
{
for (int i=0; i<27; ++i)
data->mNpcStats.mSkill[i].setBase (ref->base->npdt52.skills[i]);
data->mNpcStats.getSkill (i).setBase (ref->base->npdt52.skills[i]);
data->mCreatureStats.mAttributes[0].set (ref->base->npdt52.strength);
data->mCreatureStats.mAttributes[1].set (ref->base->npdt52.intelligence);
@ -201,12 +201,12 @@ namespace MWClass
{
case Run:
stats.mForceRun = force;
stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
break;
case Sneak:
stats.mForceSneak = force;
stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak, force);
break;
case Combat:
@ -223,12 +223,12 @@ namespace MWClass
{
case Run:
stats.mRun = set;
stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
break;
case Sneak:
stats.mSneak = set;
stats.setMovementFlag (MWMechanics::NpcStats::Flag_Sneak, set);
break;
case Combat:
@ -247,17 +247,17 @@ namespace MWClass
{
case Run:
if (!ignoreForce && stats.mForceRun)
if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
return true;
return stats.mRun;
return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
case Sneak:
if (!ignoreForce && stats.mForceSneak)
if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
return true;
return stats.mSneak;
return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Sneak);
case Combat:

@ -203,10 +203,10 @@ namespace MWDialogue
MWMechanics::NpcStats PCstats = MWWorld::Class::get(MWBase::Environment::get().getWorld()->getPlayer().getPlayer()).getNpcStats(MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
MWMechanics::NpcStats NPCstats = MWWorld::Class::get(actor).getNpcStats(actor);
int sameFaction = 0;
if(!NPCstats.mFactionRank.empty())
if(!NPCstats.getFactionRanks().empty())
{
std::string NPCFaction = NPCstats.mFactionRank.begin()->first;
if(PCstats.mFactionRank.find(toLower(NPCFaction)) != PCstats.mFactionRank.end()) sameFaction = 1;
std::string NPCFaction = NPCstats.getFactionRanks().begin()->first;
if(PCstats.getFactionRanks().find(toLower(NPCFaction)) != PCstats.getFactionRanks().end()) sameFaction = 1;
}
if(!selectCompare<int,int>(comp,sameFaction,select.i)) return false;
}
@ -525,8 +525,8 @@ namespace MWDialogue
//MWWorld::Class npcClass = MWWorld::Class::get(actor);
MWMechanics::NpcStats stats = MWWorld::Class::get(actor).getNpcStats(actor);
std::map<std::string,int>::iterator it = stats.mFactionRank.find(toLower(info.npcFaction));
if(it!=stats.mFactionRank.end())
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(toLower(info.npcFaction));
if(it!=stats.getFactionRanks().end())
{
//check rank
if(it->second < (int)info.data.rank) return false;
@ -542,8 +542,8 @@ namespace MWDialogue
if(!info.pcFaction.empty())
{
MWMechanics::NpcStats stats = MWWorld::Class::get(MWBase::Environment::get().getWorld()->getPlayer().getPlayer()).getNpcStats(MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
std::map<std::string,int>::iterator it = stats.mFactionRank.find(toLower(info.pcFaction));
if(it!=stats.mFactionRank.end())
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(toLower(info.pcFaction));
if(it!=stats.getFactionRanks().end())
{
//check rank
if(it->second < (int)info.data.PCrank) return false;
@ -903,13 +903,13 @@ namespace MWDialogue
std::string factionID("");
MWMechanics::NpcStats stats = MWWorld::Class::get(mActor).getNpcStats(mActor);
if(stats.mFactionRank.empty())
if(stats.getFactionRanks().empty())
{
std::cout << "No faction for this actor!";
}
else
{
factionID = stats.mFactionRank.begin()->first;
factionID = stats.getFactionRanks().begin()->first;
}
return factionID;
}

@ -270,7 +270,7 @@ void StatsWindow::onFrame ()
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
MWMechanics::NpcStats PCstats = MWWorld::Class::get(player).getNpcStats(player);
setFactions(PCstats.mFactionRank);
setFactions(PCstats.getFactionRanks());
setBirthSign(MWBase::Environment::get().getWorld()->getPlayer().getBirthsign());

@ -28,7 +28,7 @@ namespace MWMechanics
creatureStats.mMagicEffects = MagicEffects();
for (int i=0; i<27; ++i)
npcStats.mSkill[i].setBase (player->npdt52.skills[i]);
npcStats.getSkill (i).setBase (player->npdt52.skills[i]);
creatureStats.mAttributes[0].setBase (player->npdt52.strength);
creatureStats.mAttributes[1].setBase (player->npdt52.intelligence);
@ -73,8 +73,8 @@ namespace MWMechanics
if (index>=0 && index<27)
{
npcStats.mSkill[index].setBase (
npcStats.mSkill[index].getBase() + race->data.bonus[i].bonus);
npcStats.getSkill (index).setBase (
npcStats.getSkill (index).getBase() + race->data.bonus[i].bonus);
}
}
@ -124,8 +124,8 @@ namespace MWMechanics
if (index>=0 && index<27)
{
npcStats.mSkill[index].setBase (
npcStats.mSkill[index].getBase() + bonus);
npcStats.getSkill (index).setBase (
npcStats.getSkill (index).getBase() + bonus);
}
}
}
@ -141,8 +141,8 @@ namespace MWMechanics
if (index>=0 && index<27)
{
npcStats.mSkill[index].setBase (
npcStats.mSkill[index].getBase() + 5);
npcStats.getSkill (index).setBase (
npcStats.getSkill (index).getBase() + 5);
}
}
}
@ -236,11 +236,11 @@ namespace MWMechanics
//Loop over ESM::Skill::SkillEnum
for(int i = 0; i < 27; ++i)
{
if(npcStats.mSkill[i] != mWatchedNpc.mSkill[i])
if(npcStats.getSkill (i) != mWatchedNpc.getSkill (i))
{
update = true;
mWatchedNpc.mSkill[i] = npcStats.mSkill[i];
MWBase::Environment::get().getWindowManager()->setValue((ESM::Skill::SkillEnum)i, npcStats.mSkill[i]);
mWatchedNpc.getSkill (i) = npcStats.getSkill (i);
MWBase::Environment::get().getWindowManager()->setValue((ESM::Skill::SkillEnum)i, npcStats.getSkill (i));
}
}

@ -1,9 +1,10 @@
#include "npcstats.hpp"
#include <stdexcept>
MWMechanics::NpcStats::NpcStats()
: mForceRun (false), mForceSneak (false), mRun (false), mSneak (false),
mDrawState (DrawState_Nothing)
: mMovementFlags (0), mDrawState (DrawState_Nothing)
{}
MWMechanics::DrawState MWMechanics::NpcStats::getDrawState() const
@ -15,3 +16,42 @@ void MWMechanics::NpcStats::setDrawState (DrawState state)
{
mDrawState = state;
}
bool MWMechanics::NpcStats::getMovementFlag (Flag flag) const
{
return mMovementFlags & flag;
}
void MWMechanics::NpcStats::setMovementFlag (Flag flag, bool state)
{
if (state)
mMovementFlags |= flag;
else
mMovementFlags &= ~flag;
}
const MWMechanics::Stat<float>& MWMechanics::NpcStats::getSkill (int index) const
{
if (index<0 || index>=27)
throw std::runtime_error ("skill index out of range");
return mSkill[index];
}
MWMechanics::Stat<float>& MWMechanics::NpcStats::getSkill (int index)
{
if (index<0 || index>=27)
throw std::runtime_error ("skill index out of range");
return mSkill[index];
}
std::map<std::string, int>& MWMechanics::NpcStats::getFactionRanks()
{
return mFactionRank;
}
const std::map<std::string, int>& MWMechanics::NpcStats::getFactionRanks() const
{
return mFactionRank;
}

@ -19,27 +19,46 @@ namespace MWMechanics
class NpcStats
{
DrawState mDrawState;
public:
/// NPCs other than the player can only have one faction. But for the sake of consistency
/// we use the same data structure for the PC and the NPCs.
/// \note the faction key must be in lowercase
std::map<std::string, int> mFactionRank;
enum Flag
{
Flag_ForceRun = 1,
Flag_ForceSneak = 2,
Flag_Run = 4,
Flag_Sneak = 8
};
private:
Stat<float> mSkill[27];
/// NPCs other than the player can only have one faction. But for the sake of consistency
/// we use the same data structure for the PC and the NPCs.
/// \note the faction key must be in lowercase
std::map<std::string, int> mFactionRank;
bool mForceRun;
bool mForceSneak;
bool mRun;
bool mSneak;
DrawState mDrawState;
unsigned int mMovementFlags;
Stat<float> mSkill[27];
public:
NpcStats();
DrawState getDrawState() const;
void setDrawState (DrawState state);
bool getMovementFlag (Flag flag) const;
void setMovementFlag (Flag flag, bool state);
const Stat<float>& getSkill (int index) const;
Stat<float>& getSkill (int index);
std::map<std::string, int>& getFactionRanks();
const std::map<std::string, int>& getFactionRanks() const;
};
}

@ -41,7 +41,7 @@ namespace MWMechanics
{
const ESM::MagicEffect* effect = MWBase::Environment::get().getWorld()->getStore().magicEffects.find(it->effectID);
int _school = effect->data.school;
int _skillLevel = stats.mSkill[spellSchoolToSkill(_school)].getModified();
int _skillLevel = stats.getSkill (spellSchoolToSkill(_school)).getModified();
if (school == -1)
{
@ -78,7 +78,7 @@ namespace MWMechanics
NpcStats& stats = MWWorld::Class::get(actor).getNpcStats(actor);
CreatureStats& creatureStats = MWWorld::Class::get(actor).getCreatureStats(actor);
int skillLevel = stats.mSkill[getSpellSchool(spellId, actor)].getModified();
int skillLevel = stats.getSkill (getSpellSchool(spellId, actor)).getModified();
// Sound magic effect (reduces spell casting chance)
int soundMagnitude = creatureStats.mMagicEffects.get (MWMechanics::EffectKey (48)).mMagnitude;

@ -59,54 +59,36 @@ namespace MWScript
};
template<class R>
class OpClearForceRun : public Interpreter::Opcode0
class OpClearMovementFlag : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = R()(runtime);
MWMechanics::NpcStats::Flag mFlag;
MWWorld::Class::get (ptr).getNpcStats (ptr).mForceRun = false;
}
};
template<class R>
class OpForceRun : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = R()(runtime);
MWWorld::Class::get (ptr).getNpcStats (ptr).mForceRun = true;
}
};
template<class R>
class OpClearForceSneak : public Interpreter::Opcode0
{
public:
OpClearMovementFlag (MWMechanics::NpcStats::Flag flag) : mFlag (flag) {}
virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = R()(runtime);
MWWorld::Class::get (ptr).getNpcStats (ptr).mForceSneak = false;
MWWorld::Class::get (ptr).getNpcStats (ptr).setMovementFlag (mFlag, false);
}
};
template<class R>
class OpForceSneak : public Interpreter::Opcode0
class OpSetMovementFlag : public Interpreter::Opcode0
{
MWMechanics::NpcStats::Flag mFlag;
public:
OpSetMovementFlag (MWMechanics::NpcStats::Flag flag) : mFlag (flag) {}
virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = R()(runtime);
MWWorld::Class::get (ptr).getNpcStats (ptr).mForceSneak = true;
MWWorld::Class::get (ptr).getNpcStats (ptr).setMovementFlag (mFlag, true);
}
};
@ -165,19 +147,23 @@ namespace MWScript
interpreter.installSegment5 (opcodeToggleCollision, new OpToggleCollision);
interpreter.installSegment5 (opcodeClearForceRun, new OpClearForceRun<ImplicitRef>);
interpreter.installSegment5 (opcodeForceRun, new OpForceRun<ImplicitRef>);
interpreter.installSegment5 (opcodeClearForceSneak, new OpClearForceSneak<ImplicitRef>);
interpreter.installSegment5 (opcodeForceSneak, new OpForceSneak<ImplicitRef>);
interpreter.installSegment5 (opcodeClearForceRun,
new OpClearMovementFlag<ImplicitRef> (MWMechanics::NpcStats::Flag_ForceRun));
interpreter.installSegment5 (opcodeForceRun,
new OpSetMovementFlag<ImplicitRef> (MWMechanics::NpcStats::Flag_ForceRun));
interpreter.installSegment5 (opcodeClearForceSneak,
new OpClearMovementFlag<ImplicitRef> (MWMechanics::NpcStats::Flag_ForceSneak));
interpreter.installSegment5 (opcodeForceSneak,
new OpSetMovementFlag<ImplicitRef> (MWMechanics::NpcStats::Flag_ForceSneak));
interpreter.installSegment5 (opcodeClearForceRunExplicit,
new OpClearForceRun<ExplicitRef>);
new OpClearMovementFlag<ExplicitRef> (MWMechanics::NpcStats::Flag_ForceRun));
interpreter.installSegment5 (opcodeForceRunExplicit,
new OpForceRun<ExplicitRef>);
new OpSetMovementFlag<ExplicitRef> (MWMechanics::NpcStats::Flag_ForceRun));
interpreter.installSegment5 (opcodeClearForceSneakExplicit,
new OpClearForceSneak<ExplicitRef>);
new OpClearMovementFlag<ExplicitRef> (MWMechanics::NpcStats::Flag_ForceSneak));
interpreter.installSegment5 (opcodeForceSneakExplicit,
new OpForceSneak<ExplicitRef>);
new OpSetMovementFlag<ExplicitRef> (MWMechanics::NpcStats::Flag_ForceSneak));
}
}
}

@ -237,7 +237,7 @@ namespace MWScript
MWWorld::Ptr ptr = R()(runtime);
Interpreter::Type_Integer value =
MWWorld::Class::get (ptr).getNpcStats (ptr).mSkill[mIndex].
MWWorld::Class::get (ptr).getNpcStats (ptr).getSkill (mIndex).
getModified();
runtime.push (value);
@ -260,7 +260,7 @@ namespace MWScript
Interpreter::Type_Integer value = runtime[0].mInteger;
runtime.pop();
MWWorld::Class::get (ptr).getNpcStats (ptr).mSkill[mIndex].
MWWorld::Class::get (ptr).getNpcStats (ptr).getSkill (mIndex).
setModified (value, 0);
}
};
@ -281,10 +281,10 @@ namespace MWScript
Interpreter::Type_Integer value = runtime[0].mInteger;
runtime.pop();
value += MWWorld::Class::get (ptr).getNpcStats (ptr).mSkill[mIndex].
value += MWWorld::Class::get (ptr).getNpcStats (ptr).getSkill (mIndex).
getModified();
MWWorld::Class::get (ptr).getNpcStats (ptr).mSkill[mIndex].
MWWorld::Class::get (ptr).getNpcStats (ptr).getSkill (mIndex).
setModified (value, 0, 100);
}
};
@ -373,9 +373,9 @@ namespace MWScript
if(factionID != "")
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
if(MWWorld::Class::get(player).getNpcStats(player).mFactionRank.find(factionID) == MWWorld::Class::get(player).getNpcStats(player).mFactionRank.end())
if(MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().find(factionID) == MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().end())
{
MWWorld::Class::get(player).getNpcStats(player).mFactionRank[factionID] = 0;
MWWorld::Class::get(player).getNpcStats(player).getFactionRanks()[factionID] = 0;
}
}
}
@ -402,13 +402,13 @@ namespace MWScript
if(factionID != "")
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
if(MWWorld::Class::get(player).getNpcStats(player).mFactionRank.find(factionID) == MWWorld::Class::get(player).getNpcStats(player).mFactionRank.end())
if(MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().find(factionID) == MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().end())
{
MWWorld::Class::get(player).getNpcStats(player).mFactionRank[factionID] = 0;
MWWorld::Class::get(player).getNpcStats(player).getFactionRanks()[factionID] = 0;
}
else
{
MWWorld::Class::get(player).getNpcStats(player).mFactionRank[factionID] = MWWorld::Class::get(player).getNpcStats(player).mFactionRank[factionID] +1;
MWWorld::Class::get(player).getNpcStats(player).getFactionRanks()[factionID] = MWWorld::Class::get(player).getNpcStats(player).getFactionRanks()[factionID] +1;
}
}
}
@ -435,9 +435,9 @@ namespace MWScript
if(factionID != "")
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
if(MWWorld::Class::get(player).getNpcStats(player).mFactionRank.find(factionID) != MWWorld::Class::get(player).getNpcStats(player).mFactionRank.end())
if(MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().find(factionID) != MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().end())
{
MWWorld::Class::get(player).getNpcStats(player).mFactionRank[factionID] = MWWorld::Class::get(player).getNpcStats(player).mFactionRank[factionID] -1;
MWWorld::Class::get(player).getNpcStats(player).getFactionRanks()[factionID] = MWWorld::Class::get(player).getNpcStats(player).getFactionRanks()[factionID] -1;
}
}
}
@ -460,22 +460,22 @@ namespace MWScript
}
else
{
if(MWWorld::Class::get(ptr).getNpcStats(ptr).mFactionRank.empty())
if(MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().empty())
{
//throw exception?
}
else
{
factionID = MWWorld::Class::get(ptr).getNpcStats(ptr).mFactionRank.begin()->first;
factionID = MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().begin()->first;
}
}
boost::algorithm::to_lower(factionID);
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
if(factionID!="")
{
if(MWWorld::Class::get(player).getNpcStats(player).mFactionRank.find(factionID) != MWWorld::Class::get(player).getNpcStats(player).mFactionRank.end())
if(MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().find(factionID) != MWWorld::Class::get(player).getNpcStats(player).getFactionRanks().end())
{
runtime.push(MWWorld::Class::get(player).getNpcStats(player).mFactionRank[factionID]);
runtime.push(MWWorld::Class::get(player).getNpcStats(player).getFactionRanks()[factionID]);
}
else
{

@ -161,10 +161,10 @@ void MWWorld::InventoryStore::autoEquip (const MWMechanics::NpcStats& stats)
if (testSkill!=-1 || oldSkill!=-1 || testSkill!=oldSkill)
{
if (stats.mSkill[oldSkill].getModified()>stats.mSkill[testSkill].getModified())
if (stats.getSkill (oldSkill).getModified()>stats.getSkill (testSkill).getModified())
continue; // rejected, because old item better matched the NPC's skills.
if (stats.mSkill[oldSkill].getModified()<stats.mSkill[testSkill].getModified())
if (stats.getSkill (oldSkill).getModified()<stats.getSkill (testSkill).getModified())
use = true;
}
}

Loading…
Cancel
Save