npc.cpp 11.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 14
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"

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

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

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

29
#include "../mwgui/window_manager.hpp"
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
64 65
            if (!ref->base->faction.empty())
            {
scrawl's avatar
scrawl committed
66 67
                std::string faction = ref->base->faction;
                boost::algorithm::to_lower(faction);
68 69
                if(ref->base->npdt52.gold != -10)
                {
70
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->base->npdt52.rank;
71 72 73
                }
                else
                {
74
                    data->mNpcStats.getFactionRanks()[faction] = (int)ref->base->npdt12.rank;
75
                }
76 77
            }

78
            // creature stats
79 80 81
            if(ref->base->npdt52.gold != -10)
            {
                for (int i=0; i<27; ++i)
82
                    data->mNpcStats.getSkill (i).setBase (ref->base->npdt52.skills[i]);
83

greye's avatar
greye committed
84 85 86 87 88 89 90 91 92 93 94 95 96
                data->mCreatureStats.getAttribute(0).set (ref->base->npdt52.strength);
                data->mCreatureStats.getAttribute(1).set (ref->base->npdt52.intelligence);
                data->mCreatureStats.getAttribute(2).set (ref->base->npdt52.willpower);
                data->mCreatureStats.getAttribute(3).set (ref->base->npdt52.agility);
                data->mCreatureStats.getAttribute(4).set (ref->base->npdt52.speed);
                data->mCreatureStats.getAttribute(5).set (ref->base->npdt52.endurance);
                data->mCreatureStats.getAttribute(6).set (ref->base->npdt52.personality);
                data->mCreatureStats.getAttribute(7).set (ref->base->npdt52.luck);
                data->mCreatureStats.getHealth().set (ref->base->npdt52.health);
                data->mCreatureStats.getMagicka().set (ref->base->npdt52.mana);
                data->mCreatureStats.getFatigue().set (ref->base->npdt52.fatigue);

                data->mCreatureStats.setLevel(ref->base->npdt52.level);
97 98 99
            }
            else
            {
Marc Zinnschlag's avatar
Marc Zinnschlag committed
100
                /// \todo do something with npdt12 maybe:p
101
            }
102

greye's avatar
greye committed
103 104 105 106
            data->mCreatureStats.setHello(ref->base->AI.hello);
            data->mCreatureStats.setFight(ref->base->AI.fight);
            data->mCreatureStats.setFlee(ref->base->AI.flee);
            data->mCreatureStats.setAlarm(ref->base->AI.alarm);
107

108
            // store
109 110 111 112
            ptr.getRefData().setCustomData (data.release());
        }
    }

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

        return ref->base->mId;
    }

Jason Hooks's avatar
Jason Hooks committed
121
    void Npc::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
122
    {
Jason Hooks's avatar
Jason Hooks committed
123
        renderingInterface.getActors().insertNPC(ptr, getInventoryStore(ptr));
Jason Hooks's avatar
Jason Hooks committed
124
    }
Jason Hooks's avatar
Jason Hooks committed
125

126
    void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
Jason Hooks's avatar
Jason Hooks committed
127
    {
greye's avatar
greye committed
128 129 130
        physics.insertActorPhysics(ptr, getModel(ptr));
        MWBase::Environment::get().getMechanicsManager()->addActor(ptr);
    }
Jason Hooks's avatar
Jason Hooks committed
131

greye's avatar
greye committed
132 133
    std::string Npc::getModel(const MWWorld::Ptr &ptr) const
    {
134
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Jason Hooks's avatar
Jason Hooks committed
135
            ptr.get<ESM::NPC>();
greye's avatar
greye committed
136
        assert(ref->base != NULL);
137

138
        std::string headID = ref->base->head;
greye's avatar
greye committed
139 140 141 142 143 144 145 146 147 148 149 150
        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;
151 152
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
153 154
    std::string Npc::getName (const MWWorld::Ptr& ptr) const
    {
155
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
156 157 158 159 160
            ptr.get<ESM::NPC>();

        return ref->base->name;
    }

161
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
162
    {
163
        ensureCustomData (ptr);
164

165
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mCreatureStats;
166 167
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
168 169
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
170
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
171

172
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
173 174
    }

175
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
176
        const MWWorld::Ptr& actor) const
177 178
    {
        return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
179
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
180

181
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
182
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
183
    {
184
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
185

186 187 188 189 190 191 192 193 194
        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
195 196
    }

197 198
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
199
        MWWorld::LiveCellRef<ESM::NPC> *ref =
200 201 202 203 204
            ptr.get<ESM::NPC>();

        return ref->base->script;
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
205 206 207 208 209 210 211 212
    void Npc::setForceStance (const MWWorld::Ptr& ptr, Stance stance, bool force) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        switch (stance)
        {
            case Run:

213
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
214 215 216 217
                break;

            case Sneak:

218
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
                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:

235
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Run, set);
236
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
237 238 239

            case Sneak:

240
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_Sneak, set);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
241 242 243 244
                break;

            case Combat:

245 246
                // 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
247 248 249 250 251 252 253 254 255 256 257 258
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

259
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
260 261
                    return true;

262
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
263 264 265

            case Sneak:

266
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
267 268
                    return true;

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

            case Combat:

273
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
274 275 276 277 278 279 280 281 282 283
        }

        return false;
    }

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

284 285
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
286
        ensureCustomData (ptr);
287

288
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mMovement;
289 290 291 292 293 294
    }

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

Jason Hooks's avatar
Jason Hooks committed
295 296 297
        vector.x = - getMovementSettings (ptr).mLeftRight * 127;
        vector.y = getMovementSettings (ptr).mForwardBackward * 127;
		vector.z = getMovementSettings(ptr).mUpDown * 127;
298

Jason Hooks's avatar
Jason Hooks committed
299 300
        //if (getStance (ptr, Run, false))
        //    vector *= 2;
301 302 303 304

        return vector;
    }

305 306 307 308 309
    void Npc::registerSelf()
    {
        boost::shared_ptr<Class> instance (new Npc);
        registerClass (typeid (ESM::NPC).name(), instance);
    }
310 311 312 313 314 315 316 317

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

        return true;
    }

318
    MWGui::ToolTipInfo Npc::getToolTipInfo (const MWWorld::Ptr& ptr) const
319
    {
320
        MWWorld::LiveCellRef<ESM::NPC> *ref =
321 322 323 324 325 326
            ptr.get<ESM::NPC>();

        MWGui::ToolTipInfo info;
        info.caption = ref->base->name;

        std::string text;
327
        if (MWBase::Environment::get().getWindowManager()->getFullHelp())
328 329 330 331 332
            text += MWGui::ToolTips::getMiscString(ref->base->script, "Script");
        info.text = text;

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

scrawl's avatar
scrawl committed
334
    float Npc::getCapacity (const MWWorld::Ptr& ptr) const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
335 336
    {
        const MWMechanics::CreatureStats& stats = getCreatureStats (ptr);
greye's avatar
greye committed
337
        return stats.getAttribute(0).getModified()*5;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
338
    }
339 340 341 342 343 344 345

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

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

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

greye's avatar
greye committed
348
        weight += stats.getMagicEffects().get (MWMechanics::EffectKey (7)).mMagnitude; // burden
349 350 351 352 353 354

        if (weight<0)
            weight = 0;

        return weight;
    }
355 356 357 358 359 360 361 362

    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

greye's avatar
greye committed
363
        return stats.getActiveSpells().addSpell (id);
364 365 366 367 368 369 370 371 372 373 374 375 376
    }

    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 (
            ref->base->cls);

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

gugus's avatar
gugus committed
378 379 380 381 382
    void Npc::adjustRotation(const MWWorld::Ptr& ptr,float& x,float& y,float& z) const
    {
        y = 0;
        x = 0;
    }
383 384

    MWWorld::Ptr
385
    Npc::copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const
386 387 388 389 390 391
    {
        MWWorld::LiveCellRef<ESM::NPC> *ref =
            ptr.get<ESM::NPC>();

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