npc.cpp 12.4 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"
23
#include "../mwworld/inventorystore.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
24
#include "../mwworld/customdata.hpp"
25
#include "../mwworld/physicssystem.hpp"
26

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

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

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

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

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

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

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

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

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

78
            // creature stats
greye's avatar
greye committed
79
            if(ref->base->mNpdt52.mGold != -10)
80 81
            {
                for (int i=0; i<27; ++i)
greye's avatar
greye committed
82 83 84 85 86 87 88 89 90 91
                    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);
92 93 94
                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
95 96

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

greye's avatar
greye committed
110 111 112 113
            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);
114

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

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

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

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

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

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

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

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

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

    }

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

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

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

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

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

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

189
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
190
        const MWWorld::Ptr& actor) const
191 192
    {
        return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
193
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
194

195
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
196
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
197
    {
198
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
199

200 201 202 203 204 205 206 207 208
        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
209 210
    }

211 212
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
213
        MWWorld::LiveCellRef<ESM::NPC> *ref =
214 215
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
216
        return ref->base->mScript;
217 218
    }

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

        switch (stance)
        {
            case Run:

227
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
228 229 230 231
                break;

            case Sneak:

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

249
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
250
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
251 252 253

            case Sneak:

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

            case Combat:

259 260
                // 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
261 262 263 264 265 266 267 268 269 270 271 272
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

273
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
274 275
                    return true;

276
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
277 278 279

            case Sneak:

280
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
281 282
                    return true;

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

            case Combat:

287
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
288 289 290 291 292 293 294 295 296 297
        }

        return false;
    }

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

298 299
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
300
        ensureCustomData (ptr);
301

302
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mMovement;
303 304 305 306 307 308
    }

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

309
        vector.x = getMovementSettings (ptr).mLeftRight * 127;
Jason Hooks's avatar
Jason Hooks committed
310
        vector.y = getMovementSettings (ptr).mForwardBackward * 127;
greye's avatar
greye committed
311
        vector.z = getMovementSettings(ptr).mUpDown * 127;
312

Jason Hooks's avatar
Jason Hooks committed
313 314
        //if (getStance (ptr, Run, false))
        //    vector *= 2;
315 316 317

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

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

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

        return true;
    }

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

        MWGui::ToolTipInfo info;
greye's avatar
greye committed
346
        info.caption = ref->base->mName;
347 348

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

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

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

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

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

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

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

        if (weight<0)
            weight = 0;

        return weight;
    }
377 378 379 380 381 382 383 384

    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

385
        return stats.getActiveSpells().addSpell (id, actor);
386 387 388 389 390 391 392 393 394
    }

    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
395
            ref->base->mClass);
396 397 398

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

gugus's avatar
gugus committed
400 401 402 403 404
    void Npc::adjustRotation(const MWWorld::Ptr& ptr,float& x,float& y,float& z) const
    {
        y = 0;
        x = 0;
    }
405 406

    MWWorld::Ptr
407
    Npc::copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const
408 409 410 411 412 413
    {
        MWWorld::LiveCellRef<ESM::NPC> *ref =
            ptr.get<ESM::NPC>();

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