npc.cpp 11.2 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
    {
Jason Hooks's avatar
Jason Hooks committed
128

129
        MWWorld::LiveCellRef<ESM::NPC> *ref =
Jason Hooks's avatar
Jason Hooks committed
130
            ptr.get<ESM::NPC>();
Marc Zinnschlag's avatar
Marc Zinnschlag committed
131

Jason Hooks's avatar
Jason Hooks committed
132
        assert (ref->base != NULL);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
133

134

135

136 137 138
        std::string headID = ref->base->head;
        std::string bodyRaceID = headID.substr(0, headID.find_last_of("head_") - 4);
        bool beast = bodyRaceID == "b_n_khajiit_m_" || bodyRaceID == "b_n_khajiit_f_" || bodyRaceID == "b_n_argonian_m_" || bodyRaceID == "b_n_argonian_f_";
Marc Zinnschlag's avatar
Marc Zinnschlag committed
139

140
        std::string smodel = "meshes\\base_anim.nif";
141 142
        if(beast)
            smodel = "meshes\\base_animkna.nif";
Jason Hooks's avatar
Jason Hooks committed
143
         physics.insertActorPhysics(ptr, smodel);
144

145

146
        MWBase::Environment::get().getMechanicsManager()->addActor (ptr);
147 148
    }

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

        return ref->base->name;
    }

157
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
158
    {
159
        ensureCustomData (ptr);
160

161
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mCreatureStats;
162 163
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
164 165
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
166
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
167

168
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
169 170
    }

171
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
172
        const MWWorld::Ptr& actor) const
173 174
    {
        return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
175
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
176

177
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
178
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
179
    {
180
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
181

182 183 184 185 186 187 188 189 190
        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
191 192
    }

193 194
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
195
        MWWorld::LiveCellRef<ESM::NPC> *ref =
196 197 198 199 200
            ptr.get<ESM::NPC>();

        return ref->base->script;
    }

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

        switch (stance)
        {
            case Run:

209
                stats.setMovementFlag (MWMechanics::NpcStats::Flag_ForceRun, force);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
210 211 212 213
                break;

            case Sneak:

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

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

            case Sneak:

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

            case Combat:

241 242
                // 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
243 244 245 246 247 248 249 250 251 252 253 254
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

255
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceRun))
256 257
                    return true;

258
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Run);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
259 260 261

            case Sneak:

262
                if (!ignoreForce && stats.getMovementFlag (MWMechanics::NpcStats::Flag_ForceSneak))
Marc Zinnschlag's avatar
Marc Zinnschlag committed
263 264
                    return true;

265
                return stats.getMovementFlag (MWMechanics::NpcStats::Flag_Sneak);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
266 267 268

            case Combat:

269
                return false;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
270 271 272 273 274 275 276 277 278 279
        }

        return false;
    }

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

280 281
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
282
        ensureCustomData (ptr);
283

284
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mMovement;
285 286 287 288 289 290
    }

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

Jason Hooks's avatar
Jason Hooks committed
291 292 293
        vector.x = - getMovementSettings (ptr).mLeftRight * 127;
        vector.y = getMovementSettings (ptr).mForwardBackward * 127;
		vector.z = getMovementSettings(ptr).mUpDown * 127;
294

Jason Hooks's avatar
Jason Hooks committed
295 296
        //if (getStance (ptr, Run, false))
        //    vector *= 2;
297 298 299 300

        return vector;
    }

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

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

        return true;
    }

314
    MWGui::ToolTipInfo Npc::getToolTipInfo (const MWWorld::Ptr& ptr) const
315
    {
316
        MWWorld::LiveCellRef<ESM::NPC> *ref =
317 318 319 320 321 322
            ptr.get<ESM::NPC>();

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

        std::string text;
323
        if (MWBase::Environment::get().getWindowManager()->getFullHelp())
324 325 326 327 328
            text += MWGui::ToolTips::getMiscString(ref->base->script, "Script");
        info.text = text;

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

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

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

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

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

greye's avatar
greye committed
344
        weight += stats.getMagicEffects().get (MWMechanics::EffectKey (7)).mMagnitude; // burden
345 346 347 348 349 350

        if (weight<0)
            weight = 0;

        return weight;
    }
351 352 353 354 355 356 357 358

    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
359
        return stats.getActiveSpells().addSpell (id);
360 361 362 363 364 365 366 367 368 369 370 371 372
    }

    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);
    }
373

gugus's avatar
gugus committed
374 375 376 377 378
    void Npc::adjustRotation(const MWWorld::Ptr& ptr,float& x,float& y,float& z) const
    {
        y = 0;
        x = 0;
    }
379
}