npc.cpp 48.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>

Emanuel Guevel's avatar
Emanuel Guevel committed
10
#include <components/esm/loadmgef.hpp>
11 12
#include <components/esm/loadnpc.hpp>

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

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

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

33
#include "../mwrender/actors.hpp"
34
#include "../mwrender/renderinginterface.hpp"
35

36
#include "../mwgui/tooltips.hpp"
37

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

    struct CustomData : public MWWorld::CustomData
    {
        MWMechanics::NpcStats mNpcStats;
46
        MWMechanics::Movement mMovement;
47
        MWWorld::InventoryStore mInventoryStore;
48 49 50 51 52 53 54 55

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

    MWWorld::CustomData *CustomData::clone() const
    {
        return new CustomData (*this);
    }
56 57 58 59 60 61 62 63 64 65 66 67 68

    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];
69
            creatureStats.setAttribute(i, male ? attribute.mMale : attribute.mFemale);
70 71 72 73 74 75 76 77 78 79 80
        }

        // 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)
            {
81
                creatureStats.setAttribute(attribute,
82 83 84 85 86
                    creatureStats.getAttribute(attribute).getBase() + 10);
            }
        }

        // skill bonus
87
        for (int attribute=0; attribute < ESM::Attribute::Length; ++attribute)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        {
            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;
            }
112
            creatureStats.setAttribute(attribute, std::min(creatureStats.getAttribute(attribute).getBase()
113 114
                + static_cast<int>((level-1) * modifierSum+0.5), 100) );
        }
115

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

        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));
132
    }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    /**
     * @brief autoCalculateSkills
     *
     * Skills are calculated with following formulae ( http://www.uesp.net/wiki/Morrowind:NPCs#Skills ):
     *
     * Skills: (Level - 1) × (Majority Multiplier + Specialization Multiplier)
     *
     *         The Majority Multiplier is 1.0 for a Major or Minor Skill, or 0.1 for a Miscellaneous Skill.
     *
     *         The Specialization Multiplier is 0.5 for a Skill in the same Specialization as the class,
     *         zero for other Skills.
     *
     * and by adding class, race, specialization bonus.
     */
    void autoCalculateSkills(const ESM::NPC* npc, MWMechanics::NpcStats& npcStats)
    {
        const ESM::Class *class_ =
            MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>().find(npc->mClass);

        unsigned int level = npcStats.getLevel();

        const ESM::Race *race = MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc->mRace);


        for (int i = 0; i < 2; ++i)
        {
            int bonus = (i==0) ? 10 : 25;

            for (int i2 = 0; i2 < 5; ++i2)
            {
                int index = class_->mData.mSkills[i2][i];
                if (index >= 0 && index < ESM::Skill::Length)
                {
                    npcStats.getSkill(index).setBase (npcStats.getSkill(index).getBase() + bonus);
                }
            }
        }

        for (int skillIndex = 0; skillIndex < ESM::Skill::Length; ++skillIndex)
        {
            float majorMultiplier = 0.1f;
            float specMultiplier = 0.0f;

            int raceBonus = 0;
            int specBonus = 0;

            for (int raceSkillIndex = 0; raceSkillIndex < 7; ++raceSkillIndex)
            {
              if (race->mData.mBonus[raceSkillIndex].mSkill == skillIndex)
              {
                  raceBonus = race->mData.mBonus[raceSkillIndex].mBonus;
                  break;
              }
            }

            for (int k = 0; k < 5; ++k)
            {
              // is this a minor or major skill?
              if ((class_->mData.mSkills[k][0] == skillIndex) || (class_->mData.mSkills[k][1] == skillIndex))
              {
                majorMultiplier = 1.0f;
                break;
              }
            }

            // is this skill in the same Specialization as the class?
            const ESM::Skill* skill = MWBase::Environment::get().getWorld()->getStore().get<ESM::Skill>().find(skillIndex);
            if (skill->mData.mSpecialization == class_->mData.mSpecialization)
            {
              specMultiplier = 0.5f;
              specBonus = 5;
            }

            npcStats.getSkill(skillIndex).setBase(
                  std::min(
                    npcStats.getSkill(skillIndex).getBase()
                    + 5
                    + raceBonus
                    + specBonus
                    + static_cast<int>((level-1) * (majorMultiplier + specMultiplier)), 100.0f));
        }
    }
216 217
}

218
namespace MWClass
219
{
220 221
    void Npc::ensureCustomData (const MWWorld::Ptr& ptr) const
    {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        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");
238 239 240 241 242
            fJumpEncumbranceBase = gmst.find("fJumpEncumbranceBase");
            fJumpEncumbranceMultiplier = gmst.find("fJumpEncumbranceMultiplier");
            fJumpAcrobaticsBase = gmst.find("fJumpAcrobaticsBase");
            fJumpAcroMultiplier = gmst.find("fJumpAcroMultiplier");
            fJumpRunMultiplier = gmst.find("fJumpRunMultiplier");
243
            fWereWolfRunMult = gmst.find("fWereWolfRunMult");
244 245 246

            inited = true;
        }
247 248
        if (!ptr.getRefData().getCustomData())
        {
249
            std::auto_ptr<CustomData> data(new CustomData);
250

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

253
            // NPC stats
greye's avatar
greye committed
254
            if (!ref->mBase->mFaction.empty())
255
            {
greye's avatar
greye committed
256
                std::string faction = ref->mBase->mFaction;
eduard's avatar
eduard committed
257
                Misc::StringUtils::toLower(faction);
258
                if(ref->mBase->mNpdtType != ESM::NPC::NPC_WITH_AUTOCALCULATED_STATS)
259
                {
greye's avatar
greye committed
260
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->mBase->mNpdt52.mRank;
261 262 263
                }
                else
                {
greye's avatar
greye committed
264
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->mBase->mNpdt12.mRank;
265
                }
266 267
            }

268
            // creature stats
269
            int gold=0;
270
            if(ref->mBase->mNpdtType != ESM::NPC::NPC_WITH_AUTOCALCULATED_STATS)
271
            {
272 273
                gold = ref->mBase->mNpdt52.mGold;

274
                for (unsigned int i=0; i< ESM::Skill::Length; ++i)
greye's avatar
greye committed
275 276
                    data->mNpcStats.getSkill (i).setBase (ref->mBase->mNpdt52.mSkills[i]);

277 278 279 280 281 282 283 284 285
                data->mNpcStats.setAttribute(ESM::Attribute::Strength, ref->mBase->mNpdt52.mStrength);
                data->mNpcStats.setAttribute(ESM::Attribute::Intelligence, ref->mBase->mNpdt52.mIntelligence);
                data->mNpcStats.setAttribute(ESM::Attribute::Willpower, ref->mBase->mNpdt52.mWillpower);
                data->mNpcStats.setAttribute(ESM::Attribute::Agility, ref->mBase->mNpdt52.mAgility);
                data->mNpcStats.setAttribute(ESM::Attribute::Speed, ref->mBase->mNpdt52.mSpeed);
                data->mNpcStats.setAttribute(ESM::Attribute::Endurance, ref->mBase->mNpdt52.mEndurance);
                data->mNpcStats.setAttribute(ESM::Attribute::Personality, ref->mBase->mNpdt52.mPersonality);
                data->mNpcStats.setAttribute(ESM::Attribute::Luck, ref->mBase->mNpdt52.mLuck);

286 287 288 289 290
                data->mNpcStats.setHealth (ref->mBase->mNpdt52.mHealth);
                data->mNpcStats.setMagicka (ref->mBase->mNpdt52.mMana);
                data->mNpcStats.setFatigue (ref->mBase->mNpdt52.mFatigue);

                data->mNpcStats.setLevel(ref->mBase->mNpdt52.mLevel);
scrawl's avatar
scrawl committed
291
                data->mNpcStats.setBaseDisposition(ref->mBase->mNpdt52.mDisposition);
292
                data->mNpcStats.setReputation(ref->mBase->mNpdt52.mReputation);
293 294 295
            }
            else
            {
296 297
                gold = ref->mBase->mNpdt12.mGold;

298
                for (int i=0; i<3; ++i)
299
                    data->mNpcStats.setDynamic (i, 10);
300

301
                data->mNpcStats.setLevel(ref->mBase->mNpdt12.mLevel);
302 303
                data->mNpcStats.setBaseDisposition(ref->mBase->mNpdt12.mDisposition);
                data->mNpcStats.setReputation(ref->mBase->mNpdt12.mReputation);
304

305
                autoCalculateAttributes(ref->mBase, data->mNpcStats);
306
                autoCalculateSkills(ref->mBase, data->mNpcStats);
307
            }
308

309
            data->mNpcStats.getAiSequence().fill(ref->mBase->mAiPackage);
310

311 312 313 314
            data->mNpcStats.setAiSetting (0, ref->mBase->mAiData.mHello);
            data->mNpcStats.setAiSetting (1, ref->mBase->mAiData.mFight);
            data->mNpcStats.setAiSetting (2, ref->mBase->mAiData.mFlee);
            data->mNpcStats.setAiSetting (3, ref->mBase->mAiData.mAlarm);
315

316
            // spells
greye's avatar
greye committed
317 318
            for (std::vector<std::string>::const_iterator iter (ref->mBase->mSpells.mList.begin());
                iter!=ref->mBase->mSpells.mList.end(); ++iter)
319
                data->mNpcStats.getSpells().add (*iter);
320

321 322 323 324
            // inventory
            data->mInventoryStore.fill(ref->mBase->mInventory, getId(ptr),
                                       MWBase::Environment::get().getWorld()->getStore());

325
            // store
326
            ptr.getRefData().setCustomData (data.release());
327

328 329
            getContainerStore(ptr).add("gold_001", gold, ptr);

330
            getInventoryStore(ptr).autoEquip(ptr);
331 332 333
        }
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
334 335
    std::string Npc::getId (const MWWorld::Ptr& ptr) const
    {
336
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
337 338
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
339
        return ref->mBase->mId;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
340 341
    }

342 343 344 345 346
    void Npc::adjustPosition(const MWWorld::Ptr& ptr) const
    {
        MWBase::Environment::get().getWorld()->adjustPosition(ptr);
    }

Jason Hooks's avatar
Jason Hooks committed
347
    void Npc::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
348
    {
349
        renderingInterface.getActors().insertNPC(ptr);
Jason Hooks's avatar
Jason Hooks committed
350
    }
Jason Hooks's avatar
Jason Hooks committed
351

352
    void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
Jason Hooks's avatar
Jason Hooks committed
353
    {
354
        physics.addActor(ptr);
355
        MWBase::Environment::get().getMechanicsManager()->add(ptr);
greye's avatar
greye committed
356
    }
Jason Hooks's avatar
Jason Hooks committed
357

scrawl's avatar
scrawl committed
358 359 360 361 362 363
    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
364 365
    std::string Npc::getModel(const MWWorld::Ptr &ptr) const
    {
366
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Jason Hooks's avatar
Jason Hooks committed
367
            ptr.get<ESM::NPC>();
greye's avatar
greye committed
368
        assert(ref->mBase != NULL);
369

scrawl's avatar
scrawl committed
370
        //std::string headID = ref->mBase->mHead;
371

scrawl's avatar
scrawl committed
372 373
        //int end = headID.find_last_of("head_") - 4;
        //std::string bodyRaceID = headID.substr(0, end);
greye's avatar
greye committed
374 375

        std::string model = "meshes\\base_anim.nif";
376 377
        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
378
            model = "meshes\\base_animkna.nif";
379

greye's avatar
greye committed
380
        return model;
381 382 383

    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
384 385
    std::string Npc::getName (const MWWorld::Ptr& ptr) const
    {
386 387 388 389 390 391 392
        if(getNpcStats(ptr).isWerewolf())
        {
            const MWBase::World *world = MWBase::Environment::get().getWorld();
            const MWWorld::Store<ESM::GameSetting> &gmst = world->getStore().get<ESM::GameSetting>();

            return gmst.find("sWerewolfPopup")->getString();
        }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
393

394
        MWWorld::LiveCellRef<ESM::NPC> *ref = ptr.get<ESM::NPC>();
greye's avatar
greye committed
395
        return ref->mBase->mName;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
396 397
    }

398
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
399
    {
400
        ensureCustomData (ptr);
401

402
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
403 404
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
405 406
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
407
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
408

409
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
410 411
    }

Chris Robinson's avatar
Chris Robinson committed
412

413
    void Npc::hit(const MWWorld::Ptr& ptr, int type) const
Chris Robinson's avatar
Chris Robinson committed
414
    {
415 416 417
        MWBase::World *world = MWBase::Environment::get().getWorld();
        const MWWorld::Store<ESM::GameSetting> &gmst = world->getStore().get<ESM::GameSetting>();

418 419
        // Get the weapon used (if hand-to-hand, weapon = inv.end())
        MWWorld::InventoryStore &inv = getInventoryStore(ptr);
420 421 422 423
        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();
424

425 426
        float dist = 100.0f * (!weapon.isEmpty() ?
                               weapon.get<ESM::Weapon>()->mBase->mData.mReach :
427
                               gmst.find("fHandToHandReach")->getFloat());
428 429
        // TODO: Use second to work out the hit angle and where to spawn the blood effect
        MWWorld::Ptr victim = world->getHitContact(ptr, dist).first;
Chris Robinson's avatar
Chris Robinson committed
430 431 432 433
        if(victim.isEmpty()) // Didn't hit anything
            return;

        const MWWorld::Class &othercls = MWWorld::Class::get(victim);
434 435
        if(!othercls.isActor()) // Can't hit non-actors
            return;
436
        MWMechanics::CreatureStats &otherstats = victim.getClass().getCreatureStats(victim);
437
        if(otherstats.isDead()) // Can't hit dead actors
Chris Robinson's avatar
Chris Robinson committed
438 439
            return;

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

443
        int weapskill = ESM::Skill::HandToHand;
444
        if(!weapon.isEmpty())
445
            weapskill = get(weapon).getEquipmentSkill(weapon);
446

447 448 449 450 451 452
        MWMechanics::NpcStats &stats = getNpcStats(ptr);
        const MWMechanics::MagicEffects &mageffects = stats.getMagicEffects();
        float hitchance = stats.getSkill(weapskill).getModified() +
                          (stats.getAttribute(ESM::Attribute::Agility).getModified() / 5.0f) +
                          (stats.getAttribute(ESM::Attribute::Luck).getModified() / 10.0f);
        hitchance *= stats.getFatigueTerm();
scrawl's avatar
scrawl committed
453 454
        hitchance += mageffects.get(ESM::MagicEffect::FortifyAttack).mMagnitude -
                     mageffects.get(ESM::MagicEffect::Blind).mMagnitude;
455
        hitchance -= otherstats.getEvasion();
456

Chris Robinson's avatar
Chris Robinson committed
457
        if((::rand()/(RAND_MAX+1.0)) > hitchance/100.0f)
458
        {
459
            othercls.onHit(victim, 0.0f, false, weapon, ptr, false);
460 461
            return;
        }
Chris Robinson's avatar
Chris Robinson committed
462

463
        bool healthdmg;
464 465
        float damage = 0.0f;
        if(!weapon.isEmpty())
Chris Robinson's avatar
Chris Robinson committed
466
        {
467
            const bool weaphashealth = get(weapon).hasItemHealth(weapon);
468
            const unsigned char *attack = NULL;
Chris Robinson's avatar
Chris Robinson committed
469
            if(type == MWMechanics::CreatureStats::AT_Chop)
470
                attack = weapon.get<ESM::Weapon>()->mBase->mData.mChop;
Chris Robinson's avatar
Chris Robinson committed
471
            else if(type == MWMechanics::CreatureStats::AT_Slash)
472
                attack = weapon.get<ESM::Weapon>()->mBase->mData.mSlash;
Chris Robinson's avatar
Chris Robinson committed
473
            else if(type == MWMechanics::CreatureStats::AT_Thrust)
474
                attack = weapon.get<ESM::Weapon>()->mBase->mData.mThrust;
475
            if(attack)
476
            {
477 478
                damage  = attack[0] + ((attack[1]-attack[0])*stats.getAttackStrength());
                damage *= 0.5f + (stats.getAttribute(ESM::Attribute::Luck).getModified() / 100.0f);
479 480 481 482 483 484 485
                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;
                }
486 487 488 489
                if(!othercls.hasDetected(victim, ptr))
                {
                    damage *= gmst.find("fCombatCriticalStrikeMult")->getFloat();
                    MWBase::Environment::get().getWindowManager()->messageBox("#{sTargetCriticalStrike}");
490
                    MWBase::Environment::get().getSoundManager()->playSound3D(victim, "critical damage", 1.0f, 1.0f);
491
                }
492 493 494 495
                
                if (!MWBase::Environment::get().getWorld()->getGodModeState())
                    weapon.getCellRef().mCharge -= std::min(std::max(1,
                        (int)(damage * gmst.find("fWeaponDamageMult")->getFloat())), weapon.getCellRef().mCharge);
496 497 498 499 500

                // Weapon broken? unequip it
                if (weapon.getCellRef().mCharge == 0)
                    weapon = *inv.unequipItem(weapon, ptr);

501
            }
502 503 504 505 506 507 508 509 510
            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();
511 512
            damage  = stats.getSkill(weapskill).getModified();
            damage *= minstrike + ((maxstrike-minstrike)*stats.getAttackStrength());
513 514 515 516 517 518 519
            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);
            }

520
            healthdmg = (otherstats.getFatigue().getCurrent() < 1.0f);
521
            if(stats.isWerewolf())
522 523 524 525 526 527
            {
                healthdmg = true;
                // GLOB instead of GMST because it gets updated during a quest
                const MWWorld::Store<ESM::Global> &glob = world->getStore().get<ESM::Global>();
                damage *= glob.find("WerewolfClawMult")->mValue.getFloat();
            }
528
            if(healthdmg)
529
                damage *= gmst.find("fHandtoHandHealthPer")->getFloat();
530 531 532 533

            MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager();
            if(stats.isWerewolf())
            {
534 535 536
                const ESM::Sound *sound = world->getStore().get<ESM::Sound>().searchRandom("WolfHit");
                if(sound)
                    sndMgr->playSound3D(victim, sound->mId, 1.0f, 1.0f);
537 538 539
            }
            else
                sndMgr->playSound3D(victim, "Hand To Hand Hit", 1.0f, 1.0f);
Chris Robinson's avatar
Chris Robinson committed
540
        }
541 542
        if(ptr.getRefData().getHandle() == "player")
            skillUsageSucceeded(ptr, weapskill, 0);
543

544
        // Apply "On hit" enchanted weapons
545
        std::string enchantmentName = !weapon.isEmpty() ? weapon.getClass().getEnchantment(weapon) : "";
546 547 548 549 550 551 552 553 554
        if (!enchantmentName.empty())
        {
            const ESM::Enchantment* enchantment = MWBase::Environment::get().getWorld()->getStore().get<ESM::Enchantment>().find(
                        enchantmentName);
            if (enchantment->mData.mType == ESM::Enchantment::WhenStrikes)
            {
                // Check if we have enough charges
                const float enchantCost = enchantment->mData.mCost;
                int eSkill = stats.getSkill(ESM::Skill::Enchant).getModified();
555
                const int castCost = std::max(1.f, enchantCost - (enchantCost / 100) * (eSkill - 10));
556 557 558 559 560 561 562 563 564 565 566

                if (weapon.getCellRef().mEnchantmentCharge == -1)
                    weapon.getCellRef().mEnchantmentCharge = enchantment->mData.mCharge;
                if (weapon.getCellRef().mEnchantmentCharge < castCost)
                {
                    if (ptr.getRefData().getHandle() == "player")
                        MWBase::Environment::get().getWindowManager()->messageBox("#{sMagicInsufficientCharge}");
                }
                else
                {
                    weapon.getCellRef().mEnchantmentCharge -= castCost;
567 568 569

                    MWMechanics::CastSpell cast(ptr, victim);
                    cast.cast(weapon);
570 571 572

                    if (ptr.getRefData().getHandle() == "player")
                        skillUsageSucceeded (ptr, ESM::Skill::Enchant, 3);
573 574 575 576
                }
            }
        }

577
        othercls.onHit(victim, damage, healthdmg, weapon, ptr, true);
Chris Robinson's avatar
Chris Robinson committed
578 579
    }

580
    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
581
    {
582 583
        MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager();

584 585 586 587 588 589 590
        // NOTE: 'object' and/or 'attacker' may be empty.

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

            // Missed
591
            sndMgr->playSound3D(ptr, "miss", 1.0f, 1.0f);
592 593
            return;
        }
Chris Robinson's avatar
Chris Robinson committed
594

595
        if(!object.isEmpty())
596
            getCreatureStats(ptr).setLastHitObject(get(object).getId(object));
597 598 599 600 601 602 603 604

        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);
        }
605 606

        if(damage > 0.0f)
Chris Robinson's avatar
Chris Robinson committed
607 608 609
        {
            // 'ptr' is losing health. Play a 'hit' voiced dialog entry if not already saying
            // something, alert the character controller, scripts, etc.
610 611

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

613
            if(object.isEmpty())
614 615
            {
                if(ishealth)
616
                    damage /= std::min(1.0f + getArmorRating(ptr)/std::max(1.0f, damage), 4.0f);
617 618
            }
            else if(ishealth)
619
            {
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
                // 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)];

638 639 640 641
                float damagediff = damage;
                damage /= std::min(1.0f + getArmorRating(ptr)/std::max(1.0f, damage), 4.0f);
                damagediff -= damage;

642 643 644 645
                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())
646
                {
647 648 649 650 651
                    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);
652 653 654 655 656

                    // Armor broken? unequip it
                    if (armorref.mCharge == 0)
                        inv.unequipItem(armor, ptr);

657 658 659 660 661 662 663 664 665 666 667 668
                    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;
                    }
669 670
                }
            }
Chris Robinson's avatar
Chris Robinson committed
671 672
        }

673 674 675 676 677 678 679 680 681 682
        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());
683
            fatigue.setCurrent(fatigue.getCurrent() - damage, true);
684 685
            getCreatureStats(ptr).setFatigue(fatigue);
        }
686 687 688 689 690
    }

    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
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
        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
        }
    }


708
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
709
        const MWWorld::Ptr& actor) const
710
    {
711 712
        if(get(actor).isNpc() && get(actor).getNpcStats(actor).isWerewolf())
        {
713 714 715
            const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
            const ESM::Sound *sound = store.get<ESM::Sound>().searchRandom("WolfNPC");

716
            boost::shared_ptr<MWWorld::Action> action(new MWWorld::FailedAction("#{sWerewolfRefusal}"));
717 718
            if(sound) action->setSound(sound->mId);

719 720 721 722 723 724
            return action;
        }
        if(getCreatureStats(ptr).isDead())
            return boost::shared_ptr<MWWorld::Action>(new MWWorld::ActionOpen(ptr, true));
        if(get(actor).getStance(actor, MWWorld::Class::Sneak))
            return boost::shared_ptr<MWWorld::Action>(new MWWorld::ActionOpen(ptr)); // stealing
725 726
        if(get(ptr).getCreatureStats(ptr).isHostile())
            return boost::shared_ptr<MWWorld::Action>(new MWWorld::FailedAction("#{sActorInCombat}"));
727
        return boost::shared_ptr<MWWorld::Action>(new MWWorld::ActionTalk(ptr));
728
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
729

730
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
731
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
732
    {
733
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
734

735 736 737 738 739 740 741 742 743
        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
744 745
    }

746 747
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
748
        MWWorld::LiveCellRef<ESM::NPC> *ref =
749 750
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
751
        return ref->mBase->mScript;
752 753
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
754 755 756 757 758 759 760 761
    void Npc::setForceStance (const MWWorld::Ptr& ptr, Stance stance, bool force) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        switch (stance)
        {
            case Run:

762
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
763 764 765 766
                break;

            case Sneak:

767
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
                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:

784
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
785
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
786 787 788

            case Sneak:

789
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Sneak, set);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
790 791 792 793
                break;

            case Combat:

794 795
                // 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
796 797 798 799 800 801 802 803 804 805 806 807
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

808
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
809 810
                    return true;

811
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
812 813 814

            case Sneak:

815
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
816 817
                    return true;

818
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Sneak);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
819 820 821

            case Combat:

822
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
823 824 825 826 827
        }

        return false;
    }

828
    float Npc::getSpeed(const MWWorld::Ptr& ptr) const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
829
    {
830 831
        const MWBase::World *world = MWBase::Environment::get().getWorld();
        const CustomData *npcdata = static_cast<const CustomData*>(ptr.getRefData().getCustomData());
832
        const MWMechanics::MagicEffects &mageffects = npcdata->mNpcStats.getMagicEffects();
833

834 835
        const float normalizedEncumbrance = Npc::getEncumbrance(ptr) / Npc::getCapacity(ptr);

836
        float walkSpeed = fMinWalkSpeed->getFloat() + 0.01f*npcdata->mNpcStats.getAttribute(ESM::Attribute::Speed).getModified()*
837 838 839 840 841
                                                      (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();
842

843 844
        float runSpeed = walkSpeed*(0.01f * npcdata->mNpcStats.getSkill(ESM::Skill::Athletics).getModified() *
                                    fAthleticsRunBonus->getFloat() + fBaseRunMultiplier->getFloat());
845 846
        if(npcdata->mNpcStats.isWerewolf())
            runSpeed *= fWereWolfRunMult->getFloat();
847 848

        float moveSpeed;
849
        if(normalizedEncumbrance >= 1.0f)
850
            moveSpeed = 0.0f;
scrawl's avatar
scrawl committed
851
        else if(mageffects.get(ESM::MagicEffect::Levitate).mMagnitude > 0 &&
852
                world->isLevitationEnabled())
853
        {
854
            float flySpeed = 0.01f*(npcdata->mNpcStats.getAttribute(ESM::Attribute::Speed).getModified() +
scrawl's avatar
scrawl committed
855
                                    mageffects.get(ESM::MagicEffect::Levitate).mMagnitude);
856 857 858 859 860 861 862 863 864 865
            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;
scrawl's avatar
scrawl committed
866
            swimSpeed *= 1.0f + 0.01f * mageffects.get(ESM::MagicEffect::SwiftSwim).mMagnitude;
867 868 869 870
            swimSpeed *= fSwimRunBase->getFloat() + 0.01f*npcdata->mNpcStats.getSkill(ESM::Skill::Athletics).getModified()*
                                                    fSwimRunAthleticsMult->getFloat();
            moveSpeed = swimSpeed;
        }
scrawl's avatar
scrawl committed
871
        else if(Npc::getStance(ptr, Run, false) && !Npc::getStance(ptr, Sneak, false))
872 873 874
            moveSpeed = runSpeed;
        else
            moveSpeed = walkSpeed;
875
        if(getMovementSettings(ptr).mPosition[0] != 0 && getMovementSettings(ptr).mPosition[1] == 0)
876 877 878
            moveSpeed *= 0.75f;

        return moveSpeed;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
879 880
    }

Chris Robinson's avatar