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

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

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

28
#include "../mwrender/actors.hpp"
29
#include "../mwrender/renderinginterface.hpp"
30

31
#include "../mwgui/tooltips.hpp"
32

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

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

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

    MWWorld::CustomData *CustomData::clone() const
    {
        return new CustomData (*this);
    }
52 53
}

54
namespace MWClass
55
{
56 57 58 59 60 61
    void Npc::ensureCustomData (const MWWorld::Ptr& ptr) const
    {
        if (!ptr.getRefData().getCustomData())
        {
            std::auto_ptr<CustomData> data (new CustomData);

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

64
            // NPC stats
greye's avatar
greye committed
65
            if (!ref->mBase->mFaction.empty())
66
            {
greye's avatar
greye committed
67
                std::string faction = ref->mBase->mFaction;
eduard's avatar
eduard committed
68
                Misc::StringUtils::toLower(faction);
greye's avatar
greye committed
69
                if(ref->mBase->mNpdt52.mGold != -10)
70
                {
greye's avatar
greye committed
71
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->mBase->mNpdt52.mRank;
72 73 74
                }
                else
                {
greye's avatar
greye committed
75
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->mBase->mNpdt12.mRank;
76
                }
77 78
            }

79
            // creature stats
greye's avatar
greye committed
80
            if(ref->mBase->mNpdt52.mGold != -10)
81 82
            {
                for (int i=0; i<27; ++i)
greye's avatar
greye committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                    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
98
                data->mNpcStats.setBaseDisposition(ref->mBase->mNpdt52.mDisposition);
99
                data->mNpcStats.setReputation(ref->mBase->mNpdt52.mReputation);
100 101 102
            }
            else
            {
103 104 105 106 107 108
                for (int i=0; i<8; ++i)
                    data->mCreatureStats.getAttribute (i).set (10);

                for (int i=0; i<3; ++i)
                    data->mCreatureStats.setDynamic (i, 10);

109 110 111
                data->mCreatureStats.setLevel(ref->mBase->mNpdt12.mLevel);
                data->mNpcStats.setBaseDisposition(ref->mBase->mNpdt12.mDisposition);
                data->mNpcStats.setReputation(ref->mBase->mNpdt12.mReputation);
112
            }
113

114 115 116 117
            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);
118

119
            // spells
greye's avatar
greye committed
120 121
            for (std::vector<std::string>::const_iterator iter (ref->mBase->mSpells.mList.begin());
                iter!=ref->mBase->mSpells.mList.end(); ++iter)
122 123
                data->mCreatureStats.getSpells().add (*iter);

124
            // store
125 126 127 128
            ptr.getRefData().setCustomData (data.release());
        }
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
129 130
    std::string Npc::getId (const MWWorld::Ptr& ptr) const
    {
131
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
132 133
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
134
        return ref->mBase->mId;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
135 136
    }

Jason Hooks's avatar
Jason Hooks committed
137
    void Npc::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
138
    {
Jason Hooks's avatar
Jason Hooks committed
139
        renderingInterface.getActors().insertNPC(ptr, getInventoryStore(ptr));
Jason Hooks's avatar
Jason Hooks committed
140
    }
Jason Hooks's avatar
Jason Hooks committed
141

142
    void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
Jason Hooks's avatar
Jason Hooks committed
143
    {
144
        physics.addActor(ptr);
145
        MWBase::Environment::get().getMechanicsManager()->add(ptr);
greye's avatar
greye committed
146
    }
Jason Hooks's avatar
Jason Hooks committed
147

greye's avatar
greye committed
148 149
    std::string Npc::getModel(const MWWorld::Ptr &ptr) const
    {
150
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Jason Hooks's avatar
Jason Hooks committed
151
            ptr.get<ESM::NPC>();
greye's avatar
greye committed
152
        assert(ref->mBase != NULL);
153

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

greye's avatar
greye committed
156 157 158 159 160 161 162 163 164 165 166 167
        int end = headID.find_last_of("head_") - 4;
        std::string bodyRaceID = headID.substr(0, end);

        std::string model = "meshes\\base_anim.nif";
        if (bodyRaceID == "b_n_khajiit_m_" ||
            bodyRaceID == "b_n_khajiit_f_" ||
            bodyRaceID == "b_n_argonian_m_" ||
            bodyRaceID == "b_n_argonian_f_")
        {
            model = "meshes\\base_animkna.nif";
        }
        return model;
168 169 170

    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
171 172
    std::string Npc::getName (const MWWorld::Ptr& ptr) const
    {
173
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
174 175
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
176
        return ref->mBase->mName;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
177 178
    }

179
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
180
    {
181
        ensureCustomData (ptr);
182

183
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mCreatureStats;
184 185
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
186 187
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
188
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
189

190
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
191 192
    }

193
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
194
        const MWWorld::Ptr& actor) const
195
    {
scrawl's avatar
scrawl committed
196 197 198 199
        if (MWWorld::Class::get (ptr).getCreatureStats (ptr).isDead())
            return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionOpen(ptr));
        else
            return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
200
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
201

202
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
203
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
204
    {
205
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
206

207 208 209 210 211 212 213 214 215
        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
216 217
    }

218 219
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
220
        MWWorld::LiveCellRef<ESM::NPC> *ref =
221 222
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
223
        return ref->mBase->mScript;
224 225
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
226 227 228 229 230 231 232 233
    void Npc::setForceStance (const MWWorld::Ptr& ptr, Stance stance, bool force) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        switch (stance)
        {
            case Run:

234
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
235 236 237 238
                break;

            case Sneak:

239
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
                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:

256
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
257
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
258 259 260

            case Sneak:

261
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Sneak, set);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
262 263 264 265
                break;

            case Combat:

266 267
                // 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
268 269 270 271 272 273 274 275 276 277 278 279
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

280
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
281 282
                    return true;

283
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
284 285 286

            case Sneak:

287
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
288 289
                    return true;

290
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Sneak);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
291 292 293

            case Combat:

294
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
295 296 297 298 299 300 301 302 303 304
        }

        return false;
    }

    float Npc::getSpeed (const MWWorld::Ptr& ptr) const
    {
        return getStance (ptr, Run) ? 600 : 300; // TODO calculate these values from stats
    }

305 306
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
307
        ensureCustomData (ptr);
308

309
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mMovement;
310 311 312 313
    }

    Ogre::Vector3 Npc::getMovementVector (const MWWorld::Ptr& ptr) const
    {
314 315 316 317
        Ogre::Vector3 vector;
        vector.x = getMovementSettings(ptr).mLeftRight;
        vector.y = getMovementSettings(ptr).mForwardBackward;
        vector.z = getMovementSettings(ptr).mUpDown;
318 319 320

        return vector;
    }
321 322 323 324 325
    
    bool Npc::isEssential (const MWWorld::Ptr& ptr) const
    {
        MWWorld::LiveCellRef<ESM::NPC> *ref =
            ptr.get<ESM::NPC>();
326

greye's avatar
greye committed
327
        return ref->mBase->mFlags & ESM::NPC::Essential;
328 329
    }
    
330 331 332 333 334
    void Npc::registerSelf()
    {
        boost::shared_ptr<Class> instance (new Npc);
        registerClass (typeid (ESM::NPC).name(), instance);
    }
335 336 337 338 339 340 341 342

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

        return true;
    }

343
    MWGui::ToolTipInfo Npc::getToolTipInfo (const MWWorld::Ptr& ptr) const
344
    {
345
        MWWorld::LiveCellRef<ESM::NPC> *ref =
346 347 348
            ptr.get<ESM::NPC>();

        MWGui::ToolTipInfo info;
greye's avatar
greye committed
349
        info.caption = ref->mBase->mName;
350 351

        std::string text;
352
        if (MWBase::Environment::get().getWindowManager()->getFullHelp())
greye's avatar
greye committed
353
            text += MWGui::ToolTips::getMiscString(ref->mBase->mScript, "Script");
354 355 356 357
        info.text = text;

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

scrawl's avatar
scrawl committed
359
    float Npc::getCapacity (const MWWorld::Ptr& ptr) const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
360 361
    {
        const MWMechanics::CreatureStats& stats = getCreatureStats (ptr);
greye's avatar
greye committed
362
        return stats.getAttribute(0).getModified()*5;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
363
    }
364 365 366 367 368 369 370

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

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

greye's avatar
greye committed
371
        weight -= stats.getMagicEffects().get (MWMechanics::EffectKey (8)).mMagnitude; // feather
372

greye's avatar
greye committed
373
        weight += stats.getMagicEffects().get (MWMechanics::EffectKey (7)).mMagnitude; // burden
374 375 376 377 378 379

        if (weight<0)
            weight = 0;

        return weight;
    }
380 381 382 383 384 385 386 387

    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

388
        return stats.getActiveSpells().addSpell (id, actor);
389 390 391 392 393 394 395 396
    }

    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>();

397 398 399 400
        const ESM::Class *class_ =
            MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>().find (
                ref->mBase->mClass
            );
401 402 403

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

gugus's avatar
gugus committed
405 406 407 408 409
    void Npc::adjustRotation(const MWWorld::Ptr& ptr,float& x,float& y,float& z) const
    {
        y = 0;
        x = 0;
    }
410 411

    MWWorld::Ptr
412
    Npc::copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const
413 414 415 416
    {
        MWWorld::LiveCellRef<ESM::NPC> *ref =
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
417
        return MWWorld::Ptr(&cell.mNpcs.insert(*ref), &cell);
418
    }
419
}