creaturelevlist.cpp 3.1 KB
Newer Older
1 2 3 4

#include "creaturelevlist.hpp"

#include <components/esm/loadlevlist.hpp>
5
#include <components/esm/creaturelevliststate.hpp>
6

7 8 9 10 11 12
#include "../mwmechanics/levelledlist.hpp"

#include "../mwworld/customdata.hpp"

namespace
{
13
    struct CreatureLevListCustomData : public MWWorld::CustomData
14
    {
15 16 17
        // actorId of the creature we spawned
        int mSpawnActorId;

18 19 20
        virtual MWWorld::CustomData *clone() const;
    };

21
    MWWorld::CustomData *CreatureLevListCustomData::clone() const
22
    {
23
        return new CreatureLevListCustomData (*this);
24 25 26
    }
}

27 28
namespace MWClass
{
Marc Zinnschlag's avatar
Marc Zinnschlag committed
29 30 31 32 33
    std::string CreatureLevList::getName (const MWWorld::Ptr& ptr) const
    {
        return "";
    }

34 35 36 37 38 39
    void CreatureLevList::registerSelf()
    {
        boost::shared_ptr<Class> instance (new CreatureLevList);

        registerClass (typeid (ESM::CreatureLevList).name(), instance);
    }
40 41 42 43

    void CreatureLevList::insertObjectRendering(const MWWorld::Ptr &ptr, MWRender::RenderingInterface &renderingInterface) const
    {
        ensureCustomData(ptr);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

        CreatureLevListCustomData& customData = dynamic_cast<CreatureLevListCustomData&> (*ptr.getRefData().getCustomData());
        if (customData.mSpawnActorId != -1)
            return;  // TODO: handle respawning


        MWWorld::LiveCellRef<ESM::CreatureLevList> *ref =
            ptr.get<ESM::CreatureLevList>();

        std::string id = MWMechanics::getLevelledItem(ref->mBase, true);

        if (!id.empty())
        {
            const MWWorld::ESMStore& store = MWBase::Environment::get().getWorld()->getStore();
            MWWorld::ManualRef ref(store, id);
            ref.getPtr().getCellRef().mPos = ptr.getCellRef().mPos;
            MWWorld::Ptr placed = MWBase::Environment::get().getWorld()->safePlaceObject(ref.getPtr(), ptr.getCell() , ptr.getCellRef().mPos);
            customData.mSpawnActorId = placed.getClass().getCreatureStats(placed).getActorId();
        }
63 64 65 66 67 68
    }

    void CreatureLevList::ensureCustomData(const MWWorld::Ptr &ptr) const
    {
        if (!ptr.getRefData().getCustomData())
        {
69
            std::auto_ptr<CreatureLevListCustomData> data (new CreatureLevListCustomData);
70
            data->mSpawnActorId = -1;
71

72 73 74
            ptr.getRefData().setCustomData(data.release());
        }
    }
75

76 77 78 79
    void CreatureLevList::readAdditionalState (const MWWorld::Ptr& ptr, const ESM::ObjectState& state)
        const
    {
        const ESM::CreatureLevListState& state2 = dynamic_cast<const ESM::CreatureLevListState&> (state);
80

81 82 83 84
        ensureCustomData(ptr);
        CreatureLevListCustomData& customData = dynamic_cast<CreatureLevListCustomData&> (*ptr.getRefData().getCustomData());
        customData.mSpawnActorId = state2.mSpawnActorId;
    }
85

86 87 88 89 90 91 92 93
    void CreatureLevList::writeAdditionalState (const MWWorld::Ptr& ptr, ESM::ObjectState& state)
        const
    {
        ESM::CreatureLevListState& state2 = dynamic_cast<ESM::CreatureLevListState&> (state);

        ensureCustomData(ptr);
        CreatureLevListCustomData& customData = dynamic_cast<CreatureLevListCustomData&> (*ptr.getRefData().getCustomData());
        state2.mSpawnActorId = customData.mSpawnActorId;
94
    }
95
}