summaryrefslogtreecommitdiff
path: root/apps/openmw/mwworld/class.cpp
blob: 24cd09ad37829bad1c9d7a449b2c006453078849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#include "class.hpp"

#include <stdexcept>

#include <components/esm/defs.hpp>

#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/world.hpp"
#include "../mwworld/esmstore.hpp"

#include "ptr.hpp"
#include "refdata.hpp"
#include "nullaction.hpp"
#include "failedaction.hpp"
#include "actiontake.hpp"
#include "containerstore.hpp"

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

#include "../mwmechanics/creaturestats.hpp"
#include "../mwmechanics/npcstats.hpp"

namespace MWWorld
{
    std::map<unsigned int, std::shared_ptr<Class> > Class::sClasses;

    void Class::insertObjectRendering (const Ptr& ptr, const std::string& mesh, MWRender::RenderingInterface& renderingInterface) const
    {

    }

    void Class::insertObject(const Ptr& ptr, const std::string& mesh, const osg::Quat& rotation, MWPhysics::PhysicsSystem& physics) const
    {

    }

    void Class::insertObjectPhysics(const Ptr& ptr, const std::string& mesh, const osg::Quat& rotation, MWPhysics::PhysicsSystem& physics) const
    {}

    bool Class::apply (const MWWorld::Ptr& ptr, const std::string& id,  const MWWorld::Ptr& actor) const
    {
        return false;
    }

    void Class::skillUsageSucceeded (const MWWorld::Ptr& ptr, int skill, int usageType, float extraFactor) const
    {
        throw std::runtime_error ("class does not represent an actor");
    }

    bool Class::canSell (const MWWorld::ConstPtr& item, int npcServices) const
    {
        return false;
    }

    int Class::getServices(const ConstPtr &actor) const
    {
        throw std::runtime_error ("class does not have services");
    }

    MWMechanics::CreatureStats& Class::getCreatureStats (const Ptr& ptr) const
    {
        throw std::runtime_error ("class does not have creature stats");
    }

    MWMechanics::NpcStats& Class::getNpcStats (const Ptr& ptr) const
    {
        throw std::runtime_error ("class does not have NPC stats");
    }

    bool Class::hasItemHealth (const ConstPtr& ptr) const
    {
        return false;
    }

    int Class::getItemHealth(const ConstPtr &ptr) const
    {
        if (ptr.getCellRef().getCharge() == -1)
            return getItemMaxHealth(ptr);
        else
            return ptr.getCellRef().getCharge();
    }

    float Class::getItemNormalizedHealth (const ConstPtr& ptr) const
    {
        if (getItemMaxHealth(ptr) == 0)
        {
            return 0.f;
        }
        else
        {
            return getItemHealth(ptr) / static_cast<float>(getItemMaxHealth(ptr));
        }
    }

    int Class::getItemMaxHealth (const ConstPtr& ptr) const
    {
        throw std::runtime_error ("class does not have item health");
    }

    void Class::hit(const Ptr& ptr, float attackStrength, int type) const
    {
        throw std::runtime_error("class cannot hit");
    }

    void Class::block(const Ptr &ptr) const
    {
        throw std::runtime_error("class cannot block");
    }

    void Class::onHit(const Ptr& ptr, float damage, bool ishealth, const Ptr& object, const Ptr& attacker, const osg::Vec3f& hitPosition, bool successful) const
    {
        throw std::runtime_error("class cannot be hit");
    }

    std::shared_ptr<Action> Class::activate (const Ptr& ptr, const Ptr& actor) const
    {
        return std::shared_ptr<Action> (new NullAction);
    }

    std::shared_ptr<Action> Class::use (const Ptr& ptr, bool force) const
    {
        return std::shared_ptr<Action> (new NullAction);
    }

    ContainerStore& Class::getContainerStore (const Ptr& ptr) const
    {
        throw std::runtime_error ("class does not have a container store");
    }

    InventoryStore& Class::getInventoryStore (const Ptr& ptr) const
    {
        throw std::runtime_error ("class does not have an inventory store");
    }

    bool Class::hasInventoryStore(const Ptr &ptr) const
    {
        return false;
    }

    bool Class::canLock(const ConstPtr &ptr) const
    {
        return false;
    }

    void Class::setRemainingUsageTime (const Ptr& ptr, float duration) const
    {
        throw std::runtime_error ("class does not support time-based uses");
    }

    float Class::getRemainingUsageTime (const ConstPtr& ptr) const
    {
        return -1;
    }

    std::string Class::getScript (const ConstPtr& ptr) const
    {
        return "";
    }

    float Class::getMaxSpeed (const Ptr& ptr) const
    {
        return 0;
    }
    
    float Class::getCurrentSpeed (const Ptr& ptr) const
    {
        return 0;
    }

    float Class::getJump (const Ptr& ptr) const
    {
        return 0;
    }

    int Class::getEnchantmentPoints (const MWWorld::ConstPtr& ptr) const
    {
        throw std::runtime_error ("class does not support enchanting");
    }

    MWMechanics::Movement& Class::getMovementSettings (const Ptr& ptr) const
    {
        throw std::runtime_error ("movement settings not supported by class");
    }

    osg::Vec3f Class::getRotationVector (const Ptr& ptr) const
    {
        return osg::Vec3f (0, 0, 0);
    }

    std::pair<std::vector<int>, bool> Class::getEquipmentSlots (const ConstPtr& ptr) const
    {
        return std::make_pair (std::vector<int>(), false);
    }

    int Class::getEquipmentSkill (const ConstPtr& ptr) const
    {
        return -1;
    }

    int Class::getValue (const ConstPtr& ptr) const
    {
        throw std::logic_error ("value not supported by this class");
    }

    float Class::getCapacity (const MWWorld::Ptr& ptr) const
    {
        throw std::runtime_error ("capacity not supported by this class");
    }

    float Class::getWeight(const ConstPtr &ptr) const
    {
        throw std::runtime_error ("weight not supported by this class");
    }

    float Class::getEncumbrance (const MWWorld::Ptr& ptr) const
    {
        throw std::runtime_error ("encumbrance not supported by class");
    }

    bool Class::isEssential (const MWWorld::ConstPtr& ptr) const
    {
        return false;
    }

    float Class::getArmorRating (const MWWorld::Ptr& ptr) const
    {
        throw std::runtime_error("Class does not support armor rating");
    }

    const Class& Class::get (unsigned int key)
    {
        auto iter = sClasses.find (key);

        if (iter==sClasses.end())
            throw std::logic_error ("Class::get(): unknown class key: " + std::to_string(key));

        return *iter->second;
    }

    bool Class::isPersistent(const ConstPtr &ptr) const
    {
        throw std::runtime_error ("class does not support persistence");
    }

    void Class::registerClass(unsigned int key, std::shared_ptr<Class> instance)
    {
        instance->mType = key;
        sClasses.insert(std::make_pair(key, instance));
    }

    std::string Class::getUpSoundId (const ConstPtr& ptr) const
    {
        throw std::runtime_error ("class does not have an up sound");
    }

    std::string Class::getDownSoundId (const ConstPtr& ptr) const
    {
        throw std::runtime_error ("class does not have an down sound");
    }

    std::string Class::getSoundIdFromSndGen(const Ptr &ptr, const std::string &type) const
    {
        throw std::runtime_error("class does not support soundgen look up");
    }

    std::string Class::getInventoryIcon (const MWWorld::ConstPtr& ptr) const
    {
        throw std::runtime_error ("class does not have any inventory icon");
    }

    MWGui::ToolTipInfo Class::getToolTipInfo (const ConstPtr& ptr, int count) const
    {
        throw std::runtime_error ("class does not have a tool tip");
    }

    bool Class::showsInInventory (const ConstPtr& ptr) const
    {
        // NOTE: Don't show WerewolfRobe objects in the inventory, or allow them to be taken.
        // Vanilla likely uses a hack like this since there's no other way to prevent it from
        // being shown or taken.
        return (ptr.getCellRef().getRefId() != "werewolfrobe");
    }

    bool Class::hasToolTip (const ConstPtr& ptr) const
    {
        return true;
    }

    std::string Class::getEnchantment (const ConstPtr& ptr) const
    {
        return "";
    }

    void Class::adjustScale(const MWWorld::ConstPtr& ptr, osg::Vec3f& scale, bool rendering) const
    {
    }

    std::string Class::getModel(const MWWorld::ConstPtr &ptr) const
    {
        return "";
    }

    bool Class::useAnim() const
    {
        return false;
    }

    void Class::getModelsToPreload(const Ptr &ptr, std::vector<std::string> &models) const
    {
        std::string model = getModel(ptr);
        if (!model.empty())
            models.push_back(model);
    }

    std::string Class::applyEnchantment(const MWWorld::ConstPtr &ptr, const std::string& enchId, int enchCharge, const std::string& newName) const
    {
        throw std::runtime_error ("class can't be enchanted");
    }

    std::pair<int, std::string> Class::canBeEquipped(const MWWorld::ConstPtr &ptr, const MWWorld::Ptr &npc) const
    {
        return std::make_pair (1, "");
    }

    void Class::adjustPosition(const MWWorld::Ptr& ptr, bool force) const
    {
    }

    std::shared_ptr<Action> Class::defaultItemActivate(const Ptr &ptr, const Ptr &actor) const
    {
        if(!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory))
            return std::shared_ptr<Action>(new NullAction());

        if(actor.getClass().isNpc() && actor.getClass().getNpcStats(actor).isWerewolf())
        {
            const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
            const ESM::Sound *sound = store.get<ESM::Sound>().searchRandom("WolfItem");

            std::shared_ptr<MWWorld::Action> action(new MWWorld::FailedAction("#{sWerewolfRefusal}"));
            if(sound) action->setSound(sound->mId);

            return action;
        }

        std::shared_ptr<MWWorld::Action> action(new ActionTake(ptr));
        action->setSound(getUpSoundId(ptr));

        return action;
    }

    MWWorld::Ptr
    Class::copyToCellImpl(const ConstPtr &ptr, CellStore &cell) const
    {
        throw std::runtime_error("unable to copy class to cell");
    }

    MWWorld::Ptr
    Class::copyToCell(const ConstPtr &ptr, CellStore &cell, int count) const
    {
        Ptr newPtr = copyToCellImpl(ptr, cell);
        newPtr.getCellRef().unsetRefNum(); // This RefNum is only valid within the original cell of the reference
        newPtr.getRefData().setCount(count);
        return newPtr;
    }

    MWWorld::Ptr
    Class::copyToCell(const ConstPtr &ptr, CellStore &cell, const ESM::Position &pos, int count) const
    {
        Ptr newPtr = copyToCell(ptr, cell, count);
        newPtr.getRefData().setPosition(pos);

        return newPtr;
    }

    bool Class::isBipedal(const ConstPtr &ptr) const
    {
        return false;
    }

    bool Class::canFly(const ConstPtr &ptr) const
    {
        return false;
    }

    bool Class::canSwim(const ConstPtr &ptr) const
    {
        return false;
    }

    bool Class::canWalk(const ConstPtr &ptr) const
    {
        return false;
    }

    bool Class::isPureWaterCreature(const ConstPtr& ptr) const
    {
        return canSwim(ptr)
                && !isBipedal(ptr)
                && !canFly(ptr)
                && !canWalk(ptr);
    }

    bool Class::isPureFlyingCreature(const ConstPtr& ptr) const
    {
        return canFly(ptr)
                && !isBipedal(ptr)
                && !canSwim(ptr)
                && !canWalk(ptr);
    }

    bool Class::isPureLandCreature(const Ptr& ptr) const
    {
        return canWalk(ptr)
                && !isBipedal(ptr)
                && !canFly(ptr)
                && !canSwim(ptr);
    }

    bool Class::isMobile(const MWWorld::Ptr& ptr) const
    {
        return canSwim(ptr) || canWalk(ptr) || canFly(ptr);
    }

    float Class::getSkill(const MWWorld::Ptr& ptr, int skill) const
    {
        throw std::runtime_error("class does not support skills");
    }

    int Class::getBloodTexture (const MWWorld::ConstPtr& ptr) const
    {
        throw std::runtime_error("class does not support gore");
    }

    void Class::readAdditionalState (const MWWorld::Ptr& ptr, const ESM::ObjectState& state) const {}

    void Class::writeAdditionalState (const MWWorld::ConstPtr& ptr, ESM::ObjectState& state) const {}

    int Class::getBaseGold(const MWWorld::ConstPtr& ptr) const
    {
        throw std::runtime_error("class does not support base gold");
    }

    bool Class::isClass(const MWWorld::ConstPtr& ptr, const std::string &className) const
    {
        return false;
    }

    MWWorld::DoorState Class::getDoorState (const MWWorld::ConstPtr &ptr) const
    {
        throw std::runtime_error("this is not a door");
    }

    void Class::setDoorState (const MWWorld::Ptr &ptr, MWWorld::DoorState state) const
    {
        throw std::runtime_error("this is not a door");
    }

    float Class::getNormalizedEncumbrance(const Ptr &ptr) const
    {
        float capacity = getCapacity(ptr);
        float encumbrance = getEncumbrance(ptr);

        if (encumbrance == 0)
            return 0.f;

        if (capacity == 0)
            return 1.f;

        return encumbrance / capacity;
    }

    std::string Class::getSound(const MWWorld::ConstPtr&) const
    {
      return std::string();
    }

    int Class::getBaseFightRating(const ConstPtr &ptr) const
    {
        throw std::runtime_error("class does not support fight rating");
    }

    std::string Class::getPrimaryFaction (const MWWorld::ConstPtr& ptr) const
    {
        return std::string();
    }
    int Class::getPrimaryFactionRank (const MWWorld::ConstPtr& ptr) const
    {
        return -1;
    }

    float Class::getEffectiveArmorRating(const ConstPtr &armor, const Ptr &actor) const
    {
        throw std::runtime_error("class does not support armor ratings");
    }

    osg::Vec4f Class::getEnchantmentColor(const MWWorld::ConstPtr& item) const
    {
        osg::Vec4f result(1,1,1,1);
        std::string enchantmentName = item.getClass().getEnchantment(item);
        if (enchantmentName.empty())
            return result;

        const ESM::Enchantment* enchantment = MWBase::Environment::get().getWorld()->getStore().get<ESM::Enchantment>().search(enchantmentName);
        if (!enchantment)
            return result;

        assert (enchantment->mEffects.mList.size());

        const ESM::MagicEffect* magicEffect = MWBase::Environment::get().getWorld()->getStore().get<ESM::MagicEffect>().search(
                enchantment->mEffects.mList.front().mEffectID);
        if (!magicEffect)
            return result;

        result.x() = magicEffect->mData.mRed / 255.f;
        result.y() = magicEffect->mData.mGreen / 255.f;
        result.z() = magicEffect->mData.mBlue / 255.f;
        return result;
    }

    void Class::setBaseAISetting(const std::string& id, MWMechanics::CreatureStats::AiSetting setting, int value) const
    {
        throw std::runtime_error ("class does not have creature stats");
    }

    void Class::modifyBaseInventory(const std::string& actorId, const std::string& itemId, int amount) const
    {
        throw std::runtime_error ("class does not have an inventory store");
    }

    float Class::getWalkSpeed(const Ptr& /*ptr*/) const
    {
        return 0;
    }

    float Class::getRunSpeed(const Ptr& /*ptr*/) const
    {
        return 0;
    }

    float Class::getSwimSpeed(const Ptr& /*ptr*/) const
    {
        return 0;
    }
}