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

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->base->mFaction.empty())
66
            {
greye's avatar
greye committed
67
                std::string faction = ref->base->mFaction;
scrawl's avatar
scrawl committed
68
                boost::algorithm::to_lower(faction);
greye's avatar
greye committed
69
                if(ref->base->mNpdt52.mGold != -10)
70
                {
greye's avatar
greye committed
71
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->base->mNpdt52.mRank;
72 73 74
                }
                else
                {
greye's avatar
greye committed
75
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->base->mNpdt12.mRank;
76
                }
77 78
            }

79
            // creature stats
greye's avatar
greye committed
80
            if(ref->base->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
                    data->mNpcStats.getSkill (i).setBase (ref->base->mNpdt52.mSkills[i]);

                data->mCreatureStats.getAttribute(0).set (ref->base->mNpdt52.mStrength);
                data->mCreatureStats.getAttribute(1).set (ref->base->mNpdt52.mIntelligence);
                data->mCreatureStats.getAttribute(2).set (ref->base->mNpdt52.mWillpower);
                data->mCreatureStats.getAttribute(3).set (ref->base->mNpdt52.mAgility);
                data->mCreatureStats.getAttribute(4).set (ref->base->mNpdt52.mSpeed);
                data->mCreatureStats.getAttribute(5).set (ref->base->mNpdt52.mEndurance);
                data->mCreatureStats.getAttribute(6).set (ref->base->mNpdt52.mPersonality);
                data->mCreatureStats.getAttribute(7).set (ref->base->mNpdt52.mLuck);
93 94 95
                data->mCreatureStats.setHealth (ref->base->mNpdt52.mHealth);
                data->mCreatureStats.setMagicka (ref->base->mNpdt52.mMana);
                data->mCreatureStats.setFatigue (ref->base->mNpdt52.mFatigue);
greye's avatar
greye committed
96 97

                data->mCreatureStats.setLevel(ref->base->mNpdt52.mLevel);
98 99 100
            }
            else
            {
greye's avatar
greye committed
101
                /// \todo do something with mNpdt12 maybe:p
102 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);

                data->mCreatureStats.setLevel (1);
109
            }
110

greye's avatar
greye committed
111 112 113 114
            data->mCreatureStats.setHello(ref->base->mAiData.mHello);
            data->mCreatureStats.setFight(ref->base->mAiData.mFight);
            data->mCreatureStats.setFlee(ref->base->mAiData.mFlee);
            data->mCreatureStats.setAlarm(ref->base->mAiData.mAlarm);
115

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

121
            // store
122 123 124 125
            ptr.getRefData().setCustomData (data.release());
        }
    }

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

131
        return ref->base->mId;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
132 133
    }

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

139
    void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
Jason Hooks's avatar
Jason Hooks committed
140
    {
greye's avatar
greye committed
141 142 143
        physics.insertActorPhysics(ptr, getModel(ptr));
        MWBase::Environment::get().getMechanicsManager()->addActor(ptr);
    }
Jason Hooks's avatar
Jason Hooks committed
144

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

greye's avatar
greye committed
151
        std::string headID = ref->base->mHead;
152

greye's avatar
greye committed
153 154 155 156 157 158 159 160 161 162 163 164
        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;
165 166 167

    }

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

greye's avatar
greye committed
173
        return ref->base->mName;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
174 175
    }

176
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
177
    {
178
        ensureCustomData (ptr);
179

180
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mCreatureStats;
181 182
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
183 184
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
185
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
186

187
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
188 189
    }

190
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
191
        const MWWorld::Ptr& actor) const
192
    {
scrawl's avatar
scrawl committed
193 194 195 196
        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));
197
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
198

199
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
200
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
201
    {
202
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
203

204 205 206 207 208 209 210 211 212
        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
213 214
    }

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

greye's avatar
greye committed
220
        return ref->base->mScript;
221 222
    }

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

        switch (stance)
        {
            case Run:

231
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
232 233 234 235
                break;

            case Sneak:

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

253
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
254
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
255 256 257

            case Sneak:

258
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Sneak, set);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
259 260 261 262
                break;

            case Combat:

263 264
                // 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
265 266 267 268 269 270 271 272 273 274 275 276
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

277
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
278 279
                    return true;

280
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
281 282 283

            case Sneak:

284
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
285 286
                    return true;

287
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Sneak);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
288 289 290

            case Combat:

291
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
292 293 294 295 296 297 298 299 300 301
        }

        return false;
    }

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

302 303
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
304
        ensureCustomData (ptr);
305

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

    Ogre::Vector3 Npc::getMovementVector (const MWWorld::Ptr& ptr) const
    {
        Ogre::Vector3 vector (0, 0, 0);

313
        vector.x = getMovementSettings (ptr).mLeftRight * 127;
Jason Hooks's avatar
Jason Hooks committed
314
        vector.y = getMovementSettings (ptr).mForwardBackward * 127;
greye's avatar
greye committed
315
        vector.z = getMovementSettings(ptr).mUpDown * 127;
316

Jason Hooks's avatar
Jason Hooks committed
317 318
        //if (getStance (ptr, Run, false))
        //    vector *= 2;
319 320 321

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

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

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

        return true;
    }

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

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

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

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

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

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

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

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

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

        if (weight<0)
            weight = 0;

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

    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

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

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

        const ESM::Class *class_ = MWBase::Environment::get().getWorld()->getStore().classes.find (
greye's avatar
greye committed
399
            ref->base->mClass);
400 401 402

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

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

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

        return MWWorld::Ptr(&cell.npcs.insert(*ref), &cell);
    }
418
}