npc.cpp 11.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"
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

greye's avatar
greye committed
103 104 105 106
            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);
107

108
            // spells
greye's avatar
greye committed
109 110
            for (std::vector<std::string>::const_iterator iter (ref->base->mSpells.mList.begin());
                iter!=ref->base->mSpells.mList.end(); ++iter)
111 112
                data->mCreatureStats.getSpells().add (*iter);

113
            // store
114 115 116 117
            ptr.getRefData().setCustomData (data.release());
        }
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
118 119
    std::string Npc::getId (const MWWorld::Ptr& ptr) const
    {
120
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
121 122
            ptr.get<ESM::NPC>();

123
        return ref->base->mId;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
124 125
    }

Jason Hooks's avatar
Jason Hooks committed
126
    void Npc::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
127
    {
Jason Hooks's avatar
Jason Hooks committed
128
        renderingInterface.getActors().insertNPC(ptr, getInventoryStore(ptr));
Jason Hooks's avatar
Jason Hooks committed
129
    }
Jason Hooks's avatar
Jason Hooks committed
130

131
    void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
Jason Hooks's avatar
Jason Hooks committed
132
    {
greye's avatar
greye committed
133 134 135
        physics.insertActorPhysics(ptr, getModel(ptr));
        MWBase::Environment::get().getMechanicsManager()->addActor(ptr);
    }
Jason Hooks's avatar
Jason Hooks committed
136

greye's avatar
greye committed
137 138
    std::string Npc::getModel(const MWWorld::Ptr &ptr) const
    {
139
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Jason Hooks's avatar
Jason Hooks committed
140
            ptr.get<ESM::NPC>();
greye's avatar
greye committed
141
        assert(ref->base != NULL);
142

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

greye's avatar
greye committed
145 146 147 148 149 150 151 152 153 154 155 156
        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;
157 158 159

    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
160 161
    std::string Npc::getName (const MWWorld::Ptr& ptr) const
    {
162
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
163 164
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
165
        return ref->base->mName;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
166 167
    }

168
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
169
    {
170
        ensureCustomData (ptr);
171

172
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mCreatureStats;
173 174
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
175 176
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
177
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
178

179
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
180 181
    }

182
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
183
        const MWWorld::Ptr& actor) const
184 185
    {
        return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
186
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
187

188
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
189
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
190
    {
191
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
192

193 194 195 196 197 198 199 200 201
        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
202 203
    }

204 205
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
206
        MWWorld::LiveCellRef<ESM::NPC> *ref =
207 208
            ptr.get<ESM::NPC>();

greye's avatar
greye committed
209
        return ref->base->mScript;
210 211
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
212 213 214 215 216 217 218 219
    void Npc::setForceStance (const MWWorld::Ptr& ptr, Stance stance, bool force) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        switch (stance)
        {
            case Run:

220
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
221 222 223 224
                break;

            case Sneak:

225
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                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:

242
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
243
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
244 245 246

            case Sneak:

247
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Sneak, set);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
248 249 250 251
                break;

            case Combat:

252 253
                // 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
254 255 256 257 258 259 260 261 262 263 264 265
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

266
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
267 268
                    return true;

269
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
270 271 272

            case Sneak:

273
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
274 275
                    return true;

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

            case Combat:

280
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
281 282 283 284 285 286 287 288 289 290
        }

        return false;
    }

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

291 292
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
293
        ensureCustomData (ptr);
294

295
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mMovement;
296 297 298 299 300 301
    }

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

302
        vector.x = getMovementSettings (ptr).mLeftRight * 127;
Jason Hooks's avatar
Jason Hooks committed
303
        vector.y = getMovementSettings (ptr).mForwardBackward * 127;
greye's avatar
greye committed
304
        vector.z = getMovementSettings(ptr).mUpDown * 127;
305

Jason Hooks's avatar
Jason Hooks committed
306 307
        //if (getStance (ptr, Run, false))
        //    vector *= 2;
308 309 310 311

        return vector;
    }

312 313 314 315 316
    void Npc::registerSelf()
    {
        boost::shared_ptr<Class> instance (new Npc);
        registerClass (typeid (ESM::NPC).name(), instance);
    }
317 318 319 320 321 322 323 324

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

        return true;
    }

325
    MWGui::ToolTipInfo Npc::getToolTipInfo (const MWWorld::Ptr& ptr) const
326
    {
327
        MWWorld::LiveCellRef<ESM::NPC> *ref =
328 329 330
            ptr.get<ESM::NPC>();

        MWGui::ToolTipInfo info;
greye's avatar
greye committed
331
        info.caption = ref->base->mName;
332 333

        std::string text;
334
        if (MWBase::Environment::get().getWindowManager()->getFullHelp())
greye's avatar
greye committed
335
            text += MWGui::ToolTips::getMiscString(ref->base->mScript, "Script");
336 337 338 339
        info.text = text;

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

scrawl's avatar
scrawl committed
341
    float Npc::getCapacity (const MWWorld::Ptr& ptr) const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
342 343
    {
        const MWMechanics::CreatureStats& stats = getCreatureStats (ptr);
greye's avatar
greye committed
344
        return stats.getAttribute(0).getModified()*5;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
345
    }
346 347 348 349 350 351 352

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

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

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

greye's avatar
greye committed
355
        weight += stats.getMagicEffects().get (MWMechanics::EffectKey (7)).mMagnitude; // burden
356 357 358 359 360 361

        if (weight<0)
            weight = 0;

        return weight;
    }
362 363 364 365 366 367 368 369

    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

370
        return stats.getActiveSpells().addSpell (id, actor);
371 372 373 374 375 376 377 378 379
    }

    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
380
            ref->base->mClass);
381 382 383

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

gugus's avatar
gugus committed
385 386 387 388 389
    void Npc::adjustRotation(const MWWorld::Ptr& ptr,float& x,float& y,float& z) const
    {
        y = 0;
        x = 0;
    }
390 391

    MWWorld::Ptr
392
    Npc::copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const
393 394 395 396 397 398
    {
        MWWorld::LiveCellRef<ESM::NPC> *ref =
            ptr.get<ESM::NPC>();

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