npc.cpp 39.6 KB
Newer Older
1 2 3

#include "npc.hpp"

4 5
#include <memory>

scrawl's avatar
scrawl committed
6 7
#include <boost/algorithm/string.hpp>

8 9
#include <OgreSceneNode.h>

10 11
#include <components/esm/loadnpc.hpp>

12 13
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
14
#include "../mwbase/mechanicsmanager.hpp"
15
#include "../mwbase/windowmanager.hpp"
16
#include "../mwbase/dialoguemanager.hpp"
17
#include "../mwbase/soundmanager.hpp"
18

Marc Zinnschlag's avatar
Marc Zinnschlag committed
19
#include "../mwmechanics/creaturestats.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
20
#include "../mwmechanics/npcstats.hpp"
21
#include "../mwmechanics/movement.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
22

23
#include "../mwworld/ptr.hpp"
24
#include "../mwworld/actiontalk.hpp"
scrawl's avatar
scrawl committed
25
#include "../mwworld/actionopen.hpp"
26
#include "../mwworld/inventorystore.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
27
#include "../mwworld/customdata.hpp"
28
#include "../mwworld/physicssystem.hpp"
29

30
#include "../mwrender/actors.hpp"
31
#include "../mwrender/renderinginterface.hpp"
32

33
#include "../mwgui/tooltips.hpp"
34

Marc Zinnschlag's avatar
Marc Zinnschlag committed
35
namespace
36 37 38
{
    const Ogre::Radian kOgrePi (Ogre::Math::PI);
    const Ogre::Radian kOgrePiOverTwo (Ogre::Math::PI / Ogre::Real(2.0));
39 40 41 42

    struct CustomData : public MWWorld::CustomData
    {
        MWMechanics::NpcStats mNpcStats;
43
        MWMechanics::CreatureStats mCreatureStats;
44
        MWMechanics::Movement mMovement;
45
        MWWorld::InventoryStore mInventoryStore;
46 47 48 49 50 51 52 53

        virtual MWWorld::CustomData *clone() const;
    };

    MWWorld::CustomData *CustomData::clone() const
    {
        return new CustomData (*this);
    }
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    void autoCalculateAttributes (const ESM::NPC* npc, MWMechanics::CreatureStats& creatureStats)
    {
        // race bonus
        const ESM::Race *race =
            MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc->mRace);

        bool male = (npc->mFlags & ESM::NPC::Female) == 0;

        int level = creatureStats.getLevel();

        for (int i=0; i<ESM::Attribute::Length; ++i)
        {
            const ESM::Race::MaleFemale& attribute = race->mData.mAttributeValues[i];
            creatureStats.getAttribute(i).setBase (male ? attribute.mMale : attribute.mFemale);
        }

        // class bonus
        const ESM::Class *class_ =
            MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>().find(npc->mClass);

        for (int i=0; i<2; ++i)
        {
            int attribute = class_->mData.mAttribute[i];
            if (attribute>=0 && attribute<8)
            {
                creatureStats.getAttribute(attribute).setBase (
                    creatureStats.getAttribute(attribute).getBase() + 10);
            }
        }

        // skill bonus
        for (int attribute=0; attribute<ESM::Attribute::Length; ++attribute)
        {
            float modifierSum = 0;

            for (int j=0; j<ESM::Skill::Length; ++j)
            {
                const ESM::Skill* skill = MWBase::Environment::get().getWorld()->getStore().get<ESM::Skill>().find(j);

                if (skill->mData.mAttribute != attribute)
                    continue;

                // is this a minor or major skill?
                float add=0.2;
                for (int k=0; k<5; ++k)
                {
                    if (class_->mData.mSkills[k][0] == j)
                        add=0.5;
                }
                for (int k=0; k<5; ++k)
                {
                    if (class_->mData.mSkills[k][1] == j)
                        add=1.0;
                }
                modifierSum += add;
            }
            creatureStats.getAttribute(attribute).setBase ( std::min(creatureStats.getAttribute(attribute).getBase()
                + static_cast<int>((level-1) * modifierSum+0.5), 100) );
        }
114

115
        // initial health
116 117
        int strength = creatureStats.getAttribute(ESM::Attribute::Strength).getBase();
        int endurance = creatureStats.getAttribute(ESM::Attribute::Endurance).getBase();
118 119 120 121 122 123 124 125 126 127 128 129 130

        int multiplier = 3;

        if (class_->mData.mSpecialization == ESM::Class::Combat)
            multiplier += 2;
        else if (class_->mData.mSpecialization == ESM::Class::Stealth)
            multiplier += 1;

        if (class_->mData.mAttribute[0] == ESM::Attribute::Endurance
            || class_->mData.mAttribute[1] == ESM::Attribute::Endurance)
            multiplier += 1;

        creatureStats.setHealth(static_cast<int> (0.5 * (strength + endurance)) + multiplier * (creatureStats.getLevel() - 1));
131
    }
132 133
}

134
namespace MWClass
135
{
136 137
    void Npc::ensureCustomData (const MWWorld::Ptr& ptr) const
    {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        static bool inited = false;
        if(!inited)
        {
            const MWBase::World *world = MWBase::Environment::get().getWorld();
            const MWWorld::Store<ESM::GameSetting> &gmst = world->getStore().get<ESM::GameSetting>();

            fMinWalkSpeed = gmst.find("fMinWalkSpeed");
            fMaxWalkSpeed = gmst.find("fMaxWalkSpeed");
            fEncumberedMoveEffect = gmst.find("fEncumberedMoveEffect");
            fSneakSpeedMultiplier = gmst.find("fSneakSpeedMultiplier");
            fAthleticsRunBonus = gmst.find("fAthleticsRunBonus");
            fBaseRunMultiplier = gmst.find("fBaseRunMultiplier");
            fMinFlySpeed = gmst.find("fMinFlySpeed");
            fMaxFlySpeed = gmst.find("fMaxFlySpeed");
            fSwimRunBase = gmst.find("fSwimRunBase");
            fSwimRunAthleticsMult = gmst.find("fSwimRunAthleticsMult");
154 155 156 157 158
            fJumpEncumbranceBase = gmst.find("fJumpEncumbranceBase");
            fJumpEncumbranceMultiplier = gmst.find("fJumpEncumbranceMultiplier");
            fJumpAcrobaticsBase = gmst.find("fJumpAcrobaticsBase");
            fJumpAcroMultiplier = gmst.find("fJumpAcroMultiplier");
            fJumpRunMultiplier = gmst.find("fJumpRunMultiplier");
159
            fWereWolfRunMult = gmst.find("fWereWolfRunMult");
160 161 162

            inited = true;
        }
163 164
        if (!ptr.getRefData().getCustomData())
        {
165
            std::auto_ptr<CustomData> data(new CustomData);
166

167
            MWWorld::LiveCellRef<ESM::NPC> *ref = ptr.get<ESM::NPC>();
168

169
            // NPC stats
greye's avatar
greye committed
170
            if (!ref->mBase->mFaction.empty())
171
            {
greye's avatar
greye committed
172
                std::string faction = ref->mBase->mFaction;
eduard's avatar
eduard committed
173
                Misc::StringUtils::toLower(faction);
greye's avatar
greye committed
174
                if(ref->mBase->mNpdt52.mGold != -10)
175
                {
greye's avatar
greye committed
176
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->mBase->mNpdt52.mRank;
177 178 179
                }
                else
                {
greye's avatar
greye committed
180
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->mBase->mNpdt12.mRank;
181
                }
182 183
            }

184
            // creature stats
greye's avatar
greye committed
185
            if(ref->mBase->mNpdt52.mGold != -10)
186 187
            {
                for (int i=0; i<27; ++i)
greye's avatar
greye committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                    data->mNpcStats.getSkill (i).setBase (ref->mBase->mNpdt52.mSkills[i]);

                data->mCreatureStats.getAttribute(0).set (ref->mBase->mNpdt52.mStrength);
                data->mCreatureStats.getAttribute(1).set (ref->mBase->mNpdt52.mIntelligence);
                data->mCreatureStats.getAttribute(2).set (ref->mBase->mNpdt52.mWillpower);
                data->mCreatureStats.getAttribute(3).set (ref->mBase->mNpdt52.mAgility);
                data->mCreatureStats.getAttribute(4).set (ref->mBase->mNpdt52.mSpeed);
                data->mCreatureStats.getAttribute(5).set (ref->mBase->mNpdt52.mEndurance);
                data->mCreatureStats.getAttribute(6).set (ref->mBase->mNpdt52.mPersonality);
                data->mCreatureStats.getAttribute(7).set (ref->mBase->mNpdt52.mLuck);
                data->mCreatureStats.setHealth (ref->mBase->mNpdt52.mHealth);
                data->mCreatureStats.setMagicka (ref->mBase->mNpdt52.mMana);
                data->mCreatureStats.setFatigue (ref->mBase->mNpdt52.mFatigue);

                data->mCreatureStats.setLevel(ref->mBase->mNpdt52.mLevel);
scrawl's avatar
scrawl committed
203
                data->mNpcStats.setBaseDisposition(ref->mBase->mNpdt52.mDisposition);
204
                data->mNpcStats.setReputation(ref->mBase->mNpdt52.mReputation);
205 206 207
            }
            else
            {
208 209 210
                for (int i=0; i<3; ++i)
                    data->mCreatureStats.setDynamic (i, 10);

211 212 213
                data->mCreatureStats.setLevel(ref->mBase->mNpdt12.mLevel);
                data->mNpcStats.setBaseDisposition(ref->mBase->mNpdt12.mDisposition);
                data->mNpcStats.setReputation(ref->mBase->mNpdt12.mReputation);
214 215

                autoCalculateAttributes(ref->mBase, data->mCreatureStats);
216
            }
217

218 219
            data->mCreatureStats.getAiSequence().fill(ref->mBase->mAiPackage);

220 221 222 223
            data->mCreatureStats.setAiSetting (0, ref->mBase->mAiData.mHello);
            data->mCreatureStats.setAiSetting (1, ref->mBase->mAiData.mFight);
            data->mCreatureStats.setAiSetting (2, ref->mBase->mAiData.mFlee);
            data->mCreatureStats.setAiSetting (3, ref->mBase->mAiData.mAlarm);
224

225
            // spells
greye's avatar
greye committed
226 227
            for (std::vector<std::string>::const_iterator iter (ref->mBase->mSpells.mList.begin());
                iter!=ref->mBase->mSpells.mList.end(); ++iter)
228 229
                data->mCreatureStats.getSpells().add (*iter);

230 231 232 233
            // inventory
            data->mInventoryStore.fill(ref->mBase->mInventory, getId(ptr),
                                       MWBase::Environment::get().getWorld()->getStore());

234
            // store
235 236 237 238
            ptr.getRefData().setCustomData (data.release());
        }
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
239 240
    std::string Npc::getId (const MWWorld::Ptr& ptr) const
    {
241
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
242 243
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
244
        return ref->mBase->mId;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
245 246
    }

247 248 249 250 251
    void Npc::adjustPosition(const MWWorld::Ptr& ptr) const
    {
        MWBase::Environment::get().getWorld()->adjustPosition(ptr);
    }

Jason Hooks's avatar
Jason Hooks committed
252
    void Npc::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
253
    {
Jason Hooks's avatar
Jason Hooks committed
254
        renderingInterface.getActors().insertNPC(ptr, getInventoryStore(ptr));
Jason Hooks's avatar
Jason Hooks committed
255
    }
Jason Hooks's avatar
Jason Hooks committed
256

257
    void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
Jason Hooks's avatar
Jason Hooks committed
258
    {
259
        physics.addActor(ptr);
260
        MWBase::Environment::get().getMechanicsManager()->add(ptr);
greye's avatar
greye committed
261
    }
Jason Hooks's avatar
Jason Hooks committed
262

scrawl's avatar
scrawl committed
263 264 265 266 267 268
    bool Npc::isPersistent(const MWWorld::Ptr &actor) const
    {
        MWWorld::LiveCellRef<ESM::NPC>* ref = actor.get<ESM::NPC>();
        return ref->mBase->mPersistent;
    }

greye's avatar
greye committed
269 270
    std::string Npc::getModel(const MWWorld::Ptr &ptr) const
    {
271
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Jason Hooks's avatar
Jason Hooks committed
272
            ptr.get<ESM::NPC>();
greye's avatar
greye committed
273
        assert(ref->mBase != NULL);
274

greye's avatar
greye committed
275
        std::string headID = ref->mBase->mHead;
276

greye's avatar
greye committed
277 278 279 280
        int end = headID.find_last_of("head_") - 4;
        std::string bodyRaceID = headID.substr(0, end);

        std::string model = "meshes\\base_anim.nif";
281 282
        const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(ref->mBase->mRace);
        if(race->mData.mFlags & ESM::Race::Beast)
greye's avatar
greye committed
283
            model = "meshes\\base_animkna.nif";
284

greye's avatar
greye committed
285
        return model;
286 287 288

    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
289 290
    std::string Npc::getName (const MWWorld::Ptr& ptr) const
    {
291
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
292 293
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
294
        return ref->mBase->mName;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
295 296
    }

297
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
298
    {
299
        ensureCustomData (ptr);
300

301
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mCreatureStats;
302 303
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
304 305
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
306
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
307

308
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
309 310
    }

Chris Robinson's avatar
Chris Robinson committed
311

312
    void Npc::hit(const MWWorld::Ptr& ptr, int type) const
Chris Robinson's avatar
Chris Robinson committed
313
    {
314 315 316
        MWBase::World *world = MWBase::Environment::get().getWorld();
        const MWWorld::Store<ESM::GameSetting> &gmst = world->getStore().get<ESM::GameSetting>();

317 318
        // Get the weapon used (if hand-to-hand, weapon = inv.end())
        MWWorld::InventoryStore &inv = getInventoryStore(ptr);
319 320 321 322
        MWWorld::ContainerStoreIterator weaponslot = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
        MWWorld::Ptr weapon = ((weaponslot != inv.end()) ? *weaponslot : MWWorld::Ptr());
        if(!weapon.isEmpty() && weapon.getTypeName() != typeid(ESM::Weapon).name())
            weapon = MWWorld::Ptr();
323

324 325
        float dist = 100.0f * (!weapon.isEmpty() ?
                               weapon.get<ESM::Weapon>()->mBase->mData.mReach :
326 327
                               gmst.find("fHandToHandReach")->getFloat());
        MWWorld::Ptr victim = world->getFacedObject(ptr, dist);
Chris Robinson's avatar
Chris Robinson committed
328 329 330 331 332 333 334 335 336 337
        if(victim.isEmpty()) // Didn't hit anything
            return;

        const MWWorld::Class &othercls = MWWorld::Class::get(victim);
        if(!othercls.isActor() || othercls.getCreatureStats(victim).isDead())
        {
            // Can't hit non-actors, or dead actors
            return;
        }

scrawl's avatar
scrawl committed
338
        if(ptr.getRefData().getHandle() == "player")
scrawl's avatar
scrawl committed
339
            MWBase::Environment::get().getWindowManager()->setEnemy(victim);
scrawl's avatar
scrawl committed
340

341
        int weapskill = ESM::Skill::HandToHand;
342 343
        if(!weapon.isEmpty())
            weapskill = MWWorld::Class::get(weapon).getEquipmentSkill(weapon);
344 345 346 347 348 349 350 351 352 353 354 355

        MWMechanics::CreatureStats &crstats = getCreatureStats(ptr);
        MWMechanics::NpcStats &npcstats = getNpcStats(ptr);
        const MWMechanics::MagicEffects &mageffects = crstats.getMagicEffects();
        float hitchance = npcstats.getSkill(weapskill).getModified() +
                          (crstats.getAttribute(ESM::Attribute::Agility).getModified() / 5.0f) +
                          (crstats.getAttribute(ESM::Attribute::Luck).getModified() / 10.0f);
        hitchance *= crstats.getFatigueTerm();
        hitchance += mageffects.get(MWMechanics::EffectKey(ESM::MagicEffect::FortifyAttack)).mMagnitude -
                     mageffects.get(MWMechanics::EffectKey(ESM::MagicEffect::Blind)).mMagnitude;
        hitchance -= othercls.getEvasion(victim);

Chris Robinson's avatar
Chris Robinson committed
356
        if((::rand()/(RAND_MAX+1.0)) > hitchance/100.0f)
357
        {
358
            othercls.onHit(victim, 0.0f, false, weapon, ptr, false);
359 360
            return;
        }
Chris Robinson's avatar
Chris Robinson committed
361

362
        bool healthdmg;
363 364
        float damage = 0.0f;
        if(!weapon.isEmpty())
Chris Robinson's avatar
Chris Robinson committed
365
        {
366
            const bool weaphashealth = get(weapon).hasItemHealth(weapon);
367
            const unsigned char *attack = NULL;
Chris Robinson's avatar
Chris Robinson committed
368
            if(type == MWMechanics::CreatureStats::AT_Chop)
369
                attack = weapon.get<ESM::Weapon>()->mBase->mData.mChop;
Chris Robinson's avatar
Chris Robinson committed
370
            else if(type == MWMechanics::CreatureStats::AT_Slash)
371
                attack = weapon.get<ESM::Weapon>()->mBase->mData.mSlash;
Chris Robinson's avatar
Chris Robinson committed
372
            else if(type == MWMechanics::CreatureStats::AT_Thrust)
373
                attack = weapon.get<ESM::Weapon>()->mBase->mData.mThrust;
374
            if(attack)
375
            {
376
                damage  = attack[0] + ((attack[1]-attack[0])*npcstats.getAttackStrength());
377
                damage *= 0.5f + (crstats.getAttribute(ESM::Attribute::Luck).getModified() / 100.0f);
378 379 380 381 382 383 384
                if(weaphashealth)
                {
                    int weapmaxhealth = weapon.get<ESM::Weapon>()->mBase->mData.mHealth;
                    if(weapon.getCellRef().mCharge == -1)
                        weapon.getCellRef().mCharge = weapmaxhealth;
                    damage *= float(weapon.getCellRef().mCharge) / weapmaxhealth;
                }
385 386 387 388
                if(!othercls.hasDetected(victim, ptr))
                {
                    damage *= gmst.find("fCombatCriticalStrikeMult")->getFloat();
                    MWBase::Environment::get().getWindowManager()->messageBox("#{sTargetCriticalStrike}");
389
                    MWBase::Environment::get().getSoundManager()->playSound3D(victim, "critical damage", 1.0f, 1.0f);
390
                }
391 392 393
                weapon.getCellRef().mCharge -= std::min(std::max(1,
                                                                 (int)(damage * gmst.find("fWeaponDamageMult")->getFloat())),
                                                        weapon.getCellRef().mCharge);
394
            }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
            healthdmg = true;
        }
        else
        {
            // Note: MCP contains an option to include Strength in hand-to-hand damage
            // calculations. Some mods recommend using it, so we may want to include am
            // option for it.
            float minstrike = gmst.find("fMinHandToHandMult")->getFloat();
            float maxstrike = gmst.find("fMaxHandToHandMult")->getFloat();
            damage  = npcstats.getSkill(weapskill).getModified();
            damage *= minstrike + ((maxstrike-minstrike)*npcstats.getAttackStrength());
            if(!othercls.hasDetected(victim, ptr))
            {
                damage *= gmst.find("fCombatCriticalStrikeMult")->getFloat();
                MWBase::Environment::get().getWindowManager()->messageBox("#{sTargetCriticalStrike}");
                MWBase::Environment::get().getSoundManager()->playSound3D(victim, "critical damage", 1.0f, 1.0f);
            }

            healthdmg = (othercls.getCreatureStats(victim).getFatigue().getCurrent() < 1.0f ||
                         npcstats.isWerewolf());
            if(healthdmg)
416
                damage *= gmst.find("fHandtoHandHealthPer")->getFloat();
Chris Robinson's avatar
Chris Robinson committed
417
        }
418 419
        if(ptr.getRefData().getHandle() == "player")
            skillUsageSucceeded(ptr, weapskill, 0);
420

421
        othercls.onHit(victim, damage, healthdmg, weapon, ptr, true);
Chris Robinson's avatar
Chris Robinson committed
422 423
    }

424
    void Npc::onHit(const MWWorld::Ptr &ptr, float damage, bool ishealth, const MWWorld::Ptr &object, const MWWorld::Ptr &attacker, bool successful) const
Chris Robinson's avatar
Chris Robinson committed
425
    {
426 427
        MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager();

428 429 430 431 432 433 434
        // NOTE: 'object' and/or 'attacker' may be empty.

        if(!successful)
        {
            // TODO: Handle HitAttemptOnMe script function

            // Missed
435
            sndMgr->playSound3D(ptr, "miss", 1.0f, 1.0f);
436 437
            return;
        }
Chris Robinson's avatar
Chris Robinson committed
438

439
        if(!object.isEmpty())
440
            getCreatureStats(ptr).setLastHitObject(get(object).getId(object));
441 442 443 444 445 446 447 448

        if(!attacker.isEmpty() && attacker.getRefData().getHandle() == "player")
        {
            const std::string &script = ptr.get<ESM::NPC>()->mBase->mScript;
            /* Set the OnPCHitMe script variable. The script is responsible for clearing it. */
            if(!script.empty())
                ptr.getRefData().getLocals().setVarByInt(script, "onpchitme", 1);
        }
449 450

        if(damage > 0.0f)
Chris Robinson's avatar
Chris Robinson committed
451 452 453
        {
            // 'ptr' is losing health. Play a 'hit' voiced dialog entry if not already saying
            // something, alert the character controller, scripts, etc.
454 455

            MWBase::Environment::get().getDialogueManager()->say(ptr, "hit");
456

457
            if(object.isEmpty())
458 459
            {
                if(ishealth)
460
                    damage /= std::min(1.0f + getArmorRating(ptr)/std::max(1.0f, damage), 4.0f);
461
                sndMgr->playSound3D(ptr, "Hand To Hand Hit", 1.0f, 1.0f);
462 463
            }
            else if(ishealth)
464
            {
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
                // Hit percentages:
                // cuirass = 30%
                // shield, helmet, greaves, boots, pauldrons = 10% each
                // guantlets = 5% each
                static const int hitslots[20] = {
                    MWWorld::InventoryStore::Slot_Cuirass, MWWorld::InventoryStore::Slot_Cuirass,
                    MWWorld::InventoryStore::Slot_Cuirass, MWWorld::InventoryStore::Slot_Cuirass,
                    MWWorld::InventoryStore::Slot_Cuirass, MWWorld::InventoryStore::Slot_Cuirass,
                    MWWorld::InventoryStore::Slot_CarriedLeft, MWWorld::InventoryStore::Slot_CarriedLeft,
                    MWWorld::InventoryStore::Slot_Helmet, MWWorld::InventoryStore::Slot_Helmet,
                    MWWorld::InventoryStore::Slot_Greaves, MWWorld::InventoryStore::Slot_Greaves,
                    MWWorld::InventoryStore::Slot_Boots, MWWorld::InventoryStore::Slot_Boots,
                    MWWorld::InventoryStore::Slot_LeftPauldron, MWWorld::InventoryStore::Slot_LeftPauldron,
                    MWWorld::InventoryStore::Slot_RightPauldron, MWWorld::InventoryStore::Slot_RightPauldron,
                    MWWorld::InventoryStore::Slot_LeftGauntlet, MWWorld::InventoryStore::Slot_RightGauntlet
                };
                int hitslot = hitslots[(int)(::rand()/(RAND_MAX+1.0)*20.0)];

483 484 485 486
                float damagediff = damage;
                damage /= std::min(1.0f + getArmorRating(ptr)/std::max(1.0f, damage), 4.0f);
                damagediff -= damage;

487 488 489 490
                MWWorld::InventoryStore &inv = getInventoryStore(ptr);
                MWWorld::ContainerStoreIterator armorslot = inv.getSlot(hitslot);
                MWWorld::Ptr armor = ((armorslot != inv.end()) ? *armorslot : MWWorld::Ptr());
                if(!armor.isEmpty() && armor.getTypeName() == typeid(ESM::Armor).name())
491
                {
492 493 494 495 496
                    ESM::CellRef &armorref = armor.getCellRef();
                    if(armorref.mCharge == -1)
                        armorref.mCharge = armor.get<ESM::Armor>()->mBase->mData.mHealth;
                    armorref.mCharge -= std::min(std::max(1, (int)damagediff),
                                                 armorref.mCharge);
497 498 499 500 501 502 503 504 505 506 507 508
                    switch(get(armor).getEquipmentSkill(armor))
                    {
                        case ESM::Skill::LightArmor:
                            sndMgr->playSound3D(ptr, "Light Armor Hit", 1.0f, 1.0f);
                            break;
                        case ESM::Skill::MediumArmor:
                            sndMgr->playSound3D(ptr, "Medium Armor Hit", 1.0f, 1.0f);
                            break;
                        case ESM::Skill::HeavyArmor:
                            sndMgr->playSound3D(ptr, "Heavy Armor Hit", 1.0f, 1.0f);
                            break;
                    }
509 510
                }
            }
Chris Robinson's avatar
Chris Robinson committed
511 512
        }

513 514 515 516 517 518 519 520 521 522 523 524 525
        if(ishealth)
        {
            if(damage > 0.0f)
                sndMgr->playSound3D(ptr, "Health Damage", 1.0f, 1.0f);
            float health = getCreatureStats(ptr).getHealth().getCurrent() - damage;
            setActorHealth(ptr, health, attacker);
        }
        else
        {
            MWMechanics::DynamicStat<float> fatigue(getCreatureStats(ptr).getFatigue());
            fatigue.setCurrent(fatigue.getCurrent() - damage);
            getCreatureStats(ptr).setFatigue(fatigue);
        }
526 527 528 529 530
    }

    void Npc::setActorHealth(const MWWorld::Ptr& ptr, float health, const MWWorld::Ptr& attacker) const
    {
        MWMechanics::CreatureStats &crstats = getCreatureStats(ptr);
Chris Robinson's avatar
Chris Robinson committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
        bool wasDead = crstats.isDead();

        MWMechanics::DynamicStat<float> stat(crstats.getHealth());
        stat.setCurrent(health);
        crstats.setHealth(stat);

        if(!wasDead && crstats.isDead())
        {
            // actor was just killed
        }
        else if(wasDead && !crstats.isDead())
        {
            // actor was just resurrected
        }
    }


548
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
549
        const MWWorld::Ptr& actor) const
550
    {
scrawl's avatar
scrawl committed
551
        if (MWWorld::Class::get (ptr).getCreatureStats (ptr).isDead())
552 553 554
            return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionOpen(ptr, true));
        else if (MWWorld::Class::get(actor).getStance(actor, MWWorld::Class::Sneak))
            return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionOpen(ptr)); // stealing
scrawl's avatar
scrawl committed
555 556
        else
            return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
557
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
558

559
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
560
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
561
    {
562
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
563

564 565 566 567 568 569 570 571 572
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mInventoryStore;
    }

    MWWorld::InventoryStore& Npc::getInventoryStore (const MWWorld::Ptr& ptr)
        const
    {
        ensureCustomData (ptr);

        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mInventoryStore;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
573 574
    }

575 576
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
577
        MWWorld::LiveCellRef<ESM::NPC> *ref =
578 579
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
580
        return ref->mBase->mScript;
581 582
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
583 584 585 586 587 588 589 590
    void Npc::setForceStance (const MWWorld::Ptr& ptr, Stance stance, bool force) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        switch (stance)
        {
            case Run:

591
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
592 593 594 595
                break;

            case Sneak:

596
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
                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:

613
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
614
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
615 616 617

            case Sneak:

618
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Sneak, set);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
619 620 621 622
                break;

            case Combat:

623 624
                // Combat stance ignored for now; need to be determined based on draw state instead of
                // being maunally set.
Marc Zinnschlag's avatar
Marc Zinnschlag committed
625 626 627 628 629 630 631 632 633 634 635 636
                break;
        }
    }

    bool Npc::getStance (const MWWorld::Ptr& ptr, Stance stance, bool ignoreForce) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        switch (stance)
        {
            case Run:

637
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
638 639
                    return true;

640
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
641 642 643

            case Sneak:

644
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
645 646
                    return true;

647
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Sneak);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
648 649 650

            case Combat:

651
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
652 653 654 655 656
        }

        return false;
    }

657
    float Npc::getSpeed(const MWWorld::Ptr& ptr) const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
658
    {
659 660
        const MWBase::World *world = MWBase::Environment::get().getWorld();
        const CustomData *npcdata = static_cast<const CustomData*>(ptr.getRefData().getCustomData());
661 662
        const MWMechanics::MagicEffects &mageffects = npcdata->mCreatureStats.getMagicEffects();

663 664 665 666 667 668 669 670
        const float normalizedEncumbrance = Npc::getEncumbrance(ptr) / Npc::getCapacity(ptr);

        float walkSpeed = fMinWalkSpeed->getFloat() + 0.01f*npcdata->mCreatureStats.getAttribute(ESM::Attribute::Speed).getModified()*
                                                      (fMaxWalkSpeed->getFloat() - fMinWalkSpeed->getFloat());
        walkSpeed *= 1.0f - fEncumberedMoveEffect->getFloat()*normalizedEncumbrance;
        walkSpeed = std::max(0.0f, walkSpeed);
        if(Npc::getStance(ptr, Sneak, false))
            walkSpeed *= fSneakSpeedMultiplier->getFloat();
671

672 673
        float runSpeed = walkSpeed*(0.01f * npcdata->mNpcStats.getSkill(ESM::Skill::Athletics).getModified() *
                                    fAthleticsRunBonus->getFloat() + fBaseRunMultiplier->getFloat());
674 675
        if(npcdata->mNpcStats.isWerewolf())
            runSpeed *= fWereWolfRunMult->getFloat();
676 677

        float moveSpeed;
678
        if(normalizedEncumbrance >= 1.0f)
679
            moveSpeed = 0.0f;
680
        else if(mageffects.get(MWMechanics::EffectKey(10/*levitate*/)).mMagnitude > 0)
681 682
        {
            float flySpeed = 0.01f*(npcdata->mCreatureStats.getAttribute(ESM::Attribute::Speed).getModified() +
683
                                    mageffects.get(MWMechanics::EffectKey(10/*levitate*/)).mMagnitude);
684 685 686 687 688 689 690 691 692 693
            flySpeed = fMinFlySpeed->getFloat() + flySpeed*(fMaxFlySpeed->getFloat() - fMinFlySpeed->getFloat());
            flySpeed *= 1.0f - fEncumberedMoveEffect->getFloat() * normalizedEncumbrance;
            flySpeed = std::max(0.0f, flySpeed);
            moveSpeed = flySpeed;
        }
        else if(world->isSwimming(ptr))
        {
            float swimSpeed = walkSpeed;
            if(Npc::getStance(ptr, Run, false))
                swimSpeed = runSpeed;
694
            swimSpeed *= 1.0f + 0.01f * mageffects.get(MWMechanics::EffectKey(1/*swift swim*/)).mMagnitude;
695 696 697 698
            swimSpeed *= fSwimRunBase->getFloat() + 0.01f*npcdata->mNpcStats.getSkill(ESM::Skill::Athletics).getModified()*
                                                    fSwimRunAthleticsMult->getFloat();
            moveSpeed = swimSpeed;
        }
scrawl's avatar
scrawl committed
699
        else if(Npc::getStance(ptr, Run, false) && !Npc::getStance(ptr, Sneak, false))
700 701 702
            moveSpeed = runSpeed;
        else
            moveSpeed = walkSpeed;
703
        if(getMovementSettings(ptr).mPosition[0] != 0 && getMovementSettings(ptr).mPosition[1] == 0)
704 705 706
            moveSpeed *= 0.75f;

        return moveSpeed;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
707 708
    }

709 710 711 712 713 714
    float Npc::getJump(const MWWorld::Ptr &ptr) const
    {
        const CustomData *npcdata = static_cast<const CustomData*>(ptr.getRefData().getCustomData());
        const MWMechanics::MagicEffects &mageffects = npcdata->mCreatureStats.getMagicEffects();
        const float encumbranceTerm = fJumpEncumbranceBase->getFloat() +
                                          fJumpEncumbranceMultiplier->getFloat() *
715
                                          (1.0f - Npc::getEncumbrance(ptr)/Npc::getCapacity(ptr));
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

        float a = npcdata->mNpcStats.getSkill(ESM::Skill::Acrobatics).getModified();
        float b = 0.0f;
        if(a > 50.0f)
        {
            b = a - 50.0f;
            a = 50.0f;
        }

        float x = fJumpAcrobaticsBase->getFloat() +
                  std::pow(a / 15.0f, fJumpAcroMultiplier->getFloat());
        x += 3 * b * fJumpAcroMultiplier->getFloat();
        x += mageffects.get(MWMechanics::EffectKey(9/*jump*/)).mMagnitude * 64;
        x *= encumbranceTerm;

        if(Npc::getStance(ptr, Run, false))
            x *= fJumpRunMultiplier->getFloat();
        x *= 1.25f;//fatigueTerm;
        x -= -627.2/*gravity constant*/;
        x /= 3;

        return x;
    }

740 741
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
742
        ensureCustomData (ptr);
743

744
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mMovement;
745 746 747 748
    }

    Ogre::Vector3 Npc::getMovementVector (const MWWorld::Ptr& ptr) const
    {
749 750 751 752 753 754
        MWMechanics::Movement &movement = getMovementSettings(ptr);
        Ogre::Vector3 vec(movement.mPosition);
        movement.mPosition[0] = 0.0f;
        movement.mPosition[1] = 0.0f;
        movement.mPosition[2] = 0.0f;
        return vec;
755
    }
756 757 758

    Ogre::Vector3 Npc::getRotationVector (const MWWorld::Ptr& ptr) const
    {
759 760 761 762 763 764
        MWMechanics::Movement &movement = getMovementSettings(ptr);
        Ogre::Vector3 vec(movement.mRotation);
        movement.mRotation[0] = 0.0f;
        movement.mRotation[1] = 0.0f;
        movement.mRotation[2] = 0.0f;
        return vec;
765 766
    }

767 768 769 770
    bool Npc::isEssential (const MWWorld::Ptr& ptr) const
    {
        MWWorld::LiveCellRef<ESM::NPC> *ref =
            ptr.get<ESM::NPC>();
771

greye's avatar
greye committed
772
        return ref->mBase->mFlags & ESM::NPC::Essential;
773 774
    }
    
775 776 777 778 779
    void Npc::registerSelf()
    {
        boost::shared_ptr<Class> instance (new Npc);
        registerClass (typeid (ESM::NPC).name(), instance);
    }
780 781 782 783 784 785 786 787

    bool Npc::hasToolTip (const MWWorld::Ptr& ptr) const
    {
        /// \todo We don't want tooltips for NPCs in combat mode.

        return true;
    }

788
    MWGui::ToolTipInfo Npc::getToolTipInfo (const MWWorld::Ptr& ptr) const
789
    {
790
        MWWorld::LiveCellRef<ESM::NPC> *ref =
791 792 793
            ptr.get<ESM::NPC>();

        MWGui::ToolTipInfo info;
greye's avatar
greye committed
794
        info.caption = ref->mBase->mName;
795 796

        std::string text;
797
        if (MWBase::Environment::get().getWindowManager()->getFullHelp())
greye's avatar
greye committed
798
            text += MWGui::ToolTips::getMiscString(ref->mBase->mScript, "Script");
799 800 801 802
        info.text = text;

        return info;
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
803

scrawl's avatar
scrawl committed
804
    float Npc::getCapacity (const MWWorld::Ptr& ptr) const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
805 806
    {
        const MWMechanics::CreatureStats& stats = getCreatureStats (ptr);
greye's avatar
greye committed
807
        return stats.getAttribute(0).getModified()*5;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
808
    }
809 810 811 812 813 814 815

    float Npc::getEncumbrance (const MWWorld::Ptr& ptr) const
    {
        float weight = getContainerStore (ptr).getWeight();

        const MWMechanics::CreatureStats& stats = getCreatureStats (ptr);

816
        weight -= stats.getMagicEffects().get (MWMechanics::EffectKey (ESM::MagicEffect::Feather)).mMagnitude;
817

818
        weight += stats.getMagicEffects().get (MWMechanics::EffectKey (ESM::MagicEffect::Burden)).mMagnitude;
819 820 821 822 823 824

        if (weight<0)
            weight = 0;

        return weight;
    }
825 826 827 828 829 830 831 832

    bool Npc::apply (const MWWorld::Ptr& ptr, const std::string& id,
        const MWWorld::Ptr& actor) const
    {
        MWMechanics::CreatureStats& stats = getCreatureStats (ptr);

        /// \todo consider instant effects

833
        return stats.getActiveSpells().addSpell (id, actor);
834 835 836 837 838 839 840 841
    }

    void Npc::skillUsageSucceeded (const MWWorld::Ptr& ptr, int skill, int usageType) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        MWWorld::LiveCellRef<ESM::NPC> *ref = ptr.get<ESM::NPC>();

842 843 844 845
        const ESM::Class *class_ =
            MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>().find (
                ref->mBase->mClass
            );
846 847 848

        stats.useSkill (skill, *class_, usageType);
    }
849

850 851
    float Npc::getArmorRating (const MWWorld::Ptr& ptr) const
    {
852 853
        const MWBase::World *world = MWBase::Environment::get().getWorld();
        const MWWorld::Store<ESM::GameSetting> &gmst = world->getStore().get<ESM::GameSetting>();
854

855 856
        MWMechanics::NpcStats &stats = getNpcStats(ptr);
        MWWorld::InventoryStore &invStore = getInventoryStore(ptr);
857 858 859 860

        int iBaseArmorSkill = gmst.find("iBaseArmorSkill")->getInt();
        float fUnarmoredBase1 = gmst.find("fUnarmoredBase1")->getFloat();
        float fUnarmoredBase2 = gmst.find("fUnarmoredBase2")->getFloat();
861
        int unarmoredSkill = stats.getSkill(ESM::Skill::Unarmored).getModified();
862

863 864
        int ratings[MWWorld::InventoryStore::Slots];
        for(int i = 0;i < MWWorld::InventoryStore::Slots;i++)
865 866 867 868 869 870 871 872 873
        {
            MWWorld::ContainerStoreIterator it = invStore.getSlot(i);
            if (it == invStore.end() || it->getTypeName() != typeid(ESM::Armor).name())
            {
                // unarmored
                ratings[i] = (fUnarmoredBase1 * unarmoredSkill) * (fUnarmoredBase2 * unarmoredSkill);
            }
            else
            {
874
                MWWorld::LiveCellRef<ESM::Armor> *ref = it->get<ESM::Armor>();
875 876

                int armorSkillType = MWWorld::Class::get(*it).getEquipmentSkill(*it);
877
                int armorSkill = stats.getSkill(armorSkillType).getModified();
878

879
                if(ref->mBase->mData.mWeight == 0)
880 881 882 883 884 885
                    ratings[i] = ref->mBase->mData.mArmor;
                else
                    ratings[i] = ref->mBase->mData.mArmor * armorSkill / iBaseArmorSkill;
            }
        }

886
        float shield = getCreatureStats(ptr).getMagicEffects().get(ESM::MagicEffect::Shield).mMagnitude;
887

888
        return ratings[MWWorld::InventoryStore::Slot_Cuirass] * 0.3f
889 890 891
                + (ratings[MWWorld::InventoryStore::Slot_CarriedLeft] + ratings[MWWorld::InventoryStore::Slot_Helmet]
                    + ratings[MWWorld::InventoryStore::Slot_Greaves] + ratings[MWWorld::InventoryStore::Slot_Boots]
                    + ratings[MWWorld::InventoryStore::Slot_LeftPauldron] + ratings[MWWorld::InventoryStore::Slot_RightPauldron]