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

Marc Zinnschlag's avatar
Marc Zinnschlag committed
12
#include "../mwmechanics/creaturestats.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
13
#include "../mwmechanics/npcstats.hpp"
14 15
#include "../mwmechanics/movement.hpp"
#include "../mwmechanics/mechanicsmanager.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
16

17
#include "../mwworld/ptr.hpp"
18
#include "../mwworld/actiontalk.hpp"
19
#include "../mwworld/world.hpp"
20
#include "../mwworld/inventorystore.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
21
#include "../mwworld/customdata.hpp"
22

23 24
#include "../mwgui/window_manager.hpp"

25 26
#include "../mwbase/environment.hpp"

Marc Zinnschlag's avatar
Marc Zinnschlag committed
27
namespace
28 29 30
{
    const Ogre::Radian kOgrePi (Ogre::Math::PI);
    const Ogre::Radian kOgrePiOverTwo (Ogre::Math::PI / Ogre::Real(2.0));
31 32 33 34

    struct CustomData : public MWWorld::CustomData
    {
        MWMechanics::NpcStats mNpcStats;
35
        MWMechanics::CreatureStats mCreatureStats;
36
        MWMechanics::Movement mMovement;
37
        MWWorld::InventoryStore mInventoryStore;
38 39 40 41 42 43 44 45

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

    MWWorld::CustomData *CustomData::clone() const
    {
        return new CustomData (*this);
    }
46 47
}

48
namespace MWClass
49
{
50 51 52 53 54 55
    void Npc::ensureCustomData (const MWWorld::Ptr& ptr) const
    {
        if (!ptr.getRefData().getCustomData())
        {
            std::auto_ptr<CustomData> data (new CustomData);

56
            MWWorld::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref = ptr.get<ESM::NPC>();
57

58
            // NPC stats
59 60
            if (!ref->base->faction.empty())
            {
scrawl's avatar
scrawl committed
61 62
                std::string faction = ref->base->faction;
                boost::algorithm::to_lower(faction);
63 64
                if(ref->base->npdt52.gold != -10)
                {
scrawl's avatar
scrawl committed
65
                    data->mNpcStats.mFactionRank[faction] = (int)ref->base->npdt52.rank;
66 67 68
                }
                else
                {
scrawl's avatar
scrawl committed
69
                    data->mNpcStats.mFactionRank[faction] = (int)ref->base->npdt12.rank;
70
                }
71 72
            }

73
            // creature stats
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            if(ref->base->npdt52.gold != -10)
            {
                for (int i=0; i<27; ++i)
                    data->mNpcStats.mSkill[i].setBase (ref->base->npdt52.skills[i]);

                data->mCreatureStats.mAttributes[0].set (ref->base->npdt52.strength);
                data->mCreatureStats.mAttributes[1].set (ref->base->npdt52.intelligence);
                data->mCreatureStats.mAttributes[2].set (ref->base->npdt52.willpower);
                data->mCreatureStats.mAttributes[3].set (ref->base->npdt52.agility);
                data->mCreatureStats.mAttributes[4].set (ref->base->npdt52.speed);
                data->mCreatureStats.mAttributes[5].set (ref->base->npdt52.endurance);
                data->mCreatureStats.mAttributes[6].set (ref->base->npdt52.personality);
                data->mCreatureStats.mAttributes[7].set (ref->base->npdt52.luck);
                data->mCreatureStats.mDynamic[0].set (ref->base->npdt52.health);
                data->mCreatureStats.mDynamic[1].set (ref->base->npdt52.mana);
                data->mCreatureStats.mDynamic[2].set (ref->base->npdt52.fatigue);

                data->mCreatureStats.mLevel = ref->base->npdt52.level;
            }
            else
            {
Marc Zinnschlag's avatar
Marc Zinnschlag committed
95
                /// \todo do something with npdt12 maybe:p
96
            }
97

98 99 100 101 102
            data->mCreatureStats.mHello = ref->base->AI.hello;
            data->mCreatureStats.mFight = ref->base->AI.fight;
            data->mCreatureStats.mFlee = ref->base->AI.flee;
            data->mCreatureStats.mAlarm = ref->base->AI.alarm;

103
            // store
104 105 106 107
            ptr.getRefData().setCustomData (data.release());
        }
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
108 109
    std::string Npc::getId (const MWWorld::Ptr& ptr) const
    {
110
        MWWorld::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
111 112 113 114 115
            ptr.get<ESM::NPC>();

        return ref->base->mId;
    }

Jason Hooks's avatar
Jason Hooks committed
116
    void Npc::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
117
    {
Jason Hooks's avatar
Jason Hooks committed
118
        renderingInterface.getActors().insertNPC(ptr, getInventoryStore(ptr));
Jason Hooks's avatar
Jason Hooks committed
119
    }
Jason Hooks's avatar
Jason Hooks committed
120

121
    void Npc::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
Jason Hooks's avatar
Jason Hooks committed
122
    {
123
        MWWorld::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
Jason Hooks's avatar
Jason Hooks committed
124
            ptr.get<ESM::NPC>();
Marc Zinnschlag's avatar
Marc Zinnschlag committed
125

Jason Hooks's avatar
Jason Hooks committed
126
        assert (ref->base != NULL);
127 128 129
        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
130

131
        std::string smodel = "meshes\\base_anim.nif";
132 133 134
        if(beast)
            smodel = "meshes\\base_animkna.nif";
        physics.insertActorPhysics(ptr, smodel);
135

136
        MWBase::Environment::get().getMechanicsManager()->addActor (ptr);
137 138
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
139 140
    std::string Npc::getName (const MWWorld::Ptr& ptr) const
    {
141
        MWWorld::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
Marc Zinnschlag's avatar
Marc Zinnschlag committed
142 143 144 145 146
            ptr.get<ESM::NPC>();

        return ref->base->name;
    }

147
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
148
    {
149
        ensureCustomData (ptr);
150

151
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mCreatureStats;
152 153
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
154 155
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
156
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
157

158
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mNpcStats;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
159 160
    }

161
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
162
        const MWWorld::Ptr& actor) const
163 164
    {
        return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
165
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
166

167
    MWWorld::ContainerStore& Npc::getContainerStore (const MWWorld::Ptr& ptr)
168
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
169
    {
170
        ensureCustomData (ptr);
Marc Zinnschlag's avatar
Marc Zinnschlag committed
171

172 173 174 175 176 177 178 179 180
        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
181 182
    }

183 184
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
185
        MWWorld::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
186 187 188 189 190
            ptr.get<ESM::NPC>();

        return ref->base->script;
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    void Npc::setForceStance (const MWWorld::Ptr& ptr, Stance stance, bool force) const
    {
        MWMechanics::NpcStats& stats = getNpcStats (ptr);

        switch (stance)
        {
            case Run:

                stats.mForceRun = force;
                break;

            case Sneak:

                stats.mForceSneak = force;
                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:

221 222
                stats.mRun = set;
                break;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

            case Sneak:

                stats.mSneak = set;
                break;

            case Combat:

                stats.mCombat = set;
                break;
        }
    }

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

        switch (stance)
        {
            case Run:

244 245 246 247
                if (!ignoreForce && stats.mForceRun)
                    return true;

                return stats.mRun;
Marc Zinnschlag's avatar
Marc Zinnschlag committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

            case Sneak:

                if (!ignoreForce && stats.mForceSneak)
                    return true;

                return stats.mSneak;

            case Combat:

                return stats.mCombat;
        }

        return false;
    }

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

269 270
    MWMechanics::Movement& Npc::getMovementSettings (const MWWorld::Ptr& ptr) const
    {
271
        ensureCustomData (ptr);
272

273
        return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mMovement;
274 275 276 277 278 279
    }

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

Jason Hooks's avatar
Jason Hooks committed
280 281 282
        vector.x = - getMovementSettings (ptr).mLeftRight * 127;
        vector.y = getMovementSettings (ptr).mForwardBackward * 127;
		vector.z = getMovementSettings(ptr).mUpDown * 127;
283

Jason Hooks's avatar
Jason Hooks committed
284 285
        //if (getStance (ptr, Run, false))
        //    vector *= 2;
286 287 288 289

        return vector;
    }

290 291 292 293 294
    void Npc::registerSelf()
    {
        boost::shared_ptr<Class> instance (new Npc);
        registerClass (typeid (ESM::NPC).name(), instance);
    }
295 296 297 298 299 300 301 302

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

        return true;
    }

303
    MWGui::ToolTipInfo Npc::getToolTipInfo (const MWWorld::Ptr& ptr) const
304
    {
305
        MWWorld::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
306 307 308 309 310 311
            ptr.get<ESM::NPC>();

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

        std::string text;
312
        if (MWBase::Environment::get().getWindowManager()->getFullHelp())
313 314 315 316 317
            text += MWGui::ToolTips::getMiscString(ref->base->script, "Script");
        info.text = text;

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

scrawl's avatar
scrawl committed
319
    float Npc::getCapacity (const MWWorld::Ptr& ptr) const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
320 321 322 323
    {
        const MWMechanics::CreatureStats& stats = getCreatureStats (ptr);
        return stats.mAttributes[0].getModified()*5;
    }
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

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

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

        weight -= stats.mMagicEffects.get (MWMechanics::EffectKey (8)).mMagnitude; // feather

        weight += stats.mMagicEffects.get (MWMechanics::EffectKey (7)).mMagnitude; // burden

        if (weight<0)
            weight = 0;

        return weight;
    }
340
}