2020-12-18 23:21:10 +01:00
|
|
|
#include "object.hpp"
|
|
|
|
|
2021-01-29 01:54:54 +01:00
|
|
|
#include "../mwclass/activator.hpp"
|
|
|
|
#include "../mwclass/armor.hpp"
|
|
|
|
#include "../mwclass/book.hpp"
|
|
|
|
#include "../mwclass/clothing.hpp"
|
|
|
|
#include "../mwclass/container.hpp"
|
|
|
|
#include "../mwclass/creature.hpp"
|
|
|
|
#include "../mwclass/door.hpp"
|
|
|
|
#include "../mwclass/ingredient.hpp"
|
|
|
|
#include "../mwclass/light.hpp"
|
|
|
|
#include "../mwclass/misc.hpp"
|
|
|
|
#include "../mwclass/npc.hpp"
|
|
|
|
#include "../mwclass/potion.hpp"
|
|
|
|
#include "../mwclass/static.hpp"
|
|
|
|
#include "../mwclass/weapon.hpp"
|
2020-12-18 23:21:10 +01:00
|
|
|
|
|
|
|
namespace MWLua
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string idToString(const ObjectId& id)
|
|
|
|
{
|
|
|
|
return std::to_string(id.mIndex) + "_" + std::to_string(id.mContentFile);
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:54:54 +01:00
|
|
|
const static std::map<std::type_index, std::string_view> classNames = {
|
|
|
|
{typeid(MWClass::Activator), "Activator"},
|
|
|
|
{typeid(MWClass::Armor), "Armor"},
|
|
|
|
{typeid(MWClass::Book), "Book"},
|
|
|
|
{typeid(MWClass::Clothing), "Clothing"},
|
|
|
|
{typeid(MWClass::Container), "Container"},
|
|
|
|
{typeid(MWClass::Creature), "Creature"},
|
|
|
|
{typeid(MWClass::Door), "Door"},
|
|
|
|
{typeid(MWClass::Ingredient), "Ingredient"},
|
|
|
|
{typeid(MWClass::Light), "Light"},
|
|
|
|
{typeid(MWClass::Miscellaneous), "Miscellaneous"},
|
|
|
|
{typeid(MWClass::Npc), "NPC"},
|
|
|
|
{typeid(MWClass::Potion), "Potion"},
|
|
|
|
{typeid(MWClass::Static), "Static"},
|
|
|
|
{typeid(MWClass::Weapon), "Weapon"},
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string_view getMWClassName(const std::type_index& cls_type, std::string_view fallback)
|
2020-12-18 23:21:10 +01:00
|
|
|
{
|
2021-01-29 01:54:54 +01:00
|
|
|
auto it = classNames.find(cls_type);
|
|
|
|
if (it != classNames.end())
|
|
|
|
return it->second;
|
2020-12-18 23:21:10 +01:00
|
|
|
else
|
2021-01-29 01:54:54 +01:00
|
|
|
return fallback;
|
2020-12-18 23:21:10 +01:00
|
|
|
}
|
|
|
|
|
2021-04-17 11:50:20 +02:00
|
|
|
bool isMarker(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
std::string_view id = *ptr.getCellRef().getRefIdPtr();
|
|
|
|
return id == "prisonmarker" || id == "divinemarker" || id == "templemarker" || id == "northmarker";
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:54:54 +01:00
|
|
|
std::string_view getMWClassName(const MWWorld::Ptr& ptr)
|
2020-12-18 23:21:10 +01:00
|
|
|
{
|
2021-01-29 01:54:54 +01:00
|
|
|
if (*ptr.getCellRef().getRefIdPtr() == "player")
|
2020-12-18 23:21:10 +01:00
|
|
|
return "Player";
|
2021-04-17 11:50:20 +02:00
|
|
|
if (isMarker(ptr))
|
|
|
|
return "Marker";
|
|
|
|
return getMWClassName(typeid(ptr.getClass()), ptr.getTypeName());
|
2021-01-29 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ptrToString(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
std::string res = "object";
|
|
|
|
res.append(idToString(getId(ptr)));
|
|
|
|
res.append(" (");
|
|
|
|
res.append(getMWClassName(ptr));
|
|
|
|
res.append(", ");
|
|
|
|
res.append(*ptr.getCellRef().getRefIdPtr());
|
|
|
|
res.append(")");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Object::toString() const
|
|
|
|
{
|
|
|
|
if (isValid())
|
|
|
|
return ptrToString(ptr());
|
|
|
|
else
|
|
|
|
return "object" + idToString(mId) + " (not found)";
|
2020-12-18 23:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Object::isValid() const
|
|
|
|
{
|
|
|
|
if (mLastUpdate < mObjectRegistry->mUpdateCounter)
|
|
|
|
{
|
|
|
|
updatePtr();
|
|
|
|
mLastUpdate = mObjectRegistry->mUpdateCounter;
|
|
|
|
}
|
|
|
|
return !mPtr.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
const MWWorld::Ptr& Object::ptr() const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
throw std::runtime_error("Object is not available: " + idToString(mId));
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectRegistry::update()
|
|
|
|
{
|
|
|
|
if (mChanged)
|
|
|
|
{
|
|
|
|
mUpdateCounter++;
|
|
|
|
mChanged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectRegistry::clear()
|
|
|
|
{
|
|
|
|
mObjectMapping.clear();
|
|
|
|
mChanged = false;
|
|
|
|
mUpdateCounter = 0;
|
|
|
|
mLastAssignedId.unset();
|
|
|
|
}
|
|
|
|
|
|
|
|
MWWorld::Ptr ObjectRegistry::getPtr(ObjectId id, bool onlyActive)
|
|
|
|
{
|
|
|
|
MWWorld::Ptr ptr;
|
|
|
|
auto it = mObjectMapping.find(id);
|
|
|
|
if (it != mObjectMapping.end())
|
|
|
|
ptr = it->second;
|
|
|
|
if (onlyActive)
|
|
|
|
{
|
|
|
|
// TODO: add flag `isActive` to LiveCellRefBase. Return empty Ptr if the flag is not set.
|
|
|
|
// Needed because in multiplayer mode inactive objects will not be synchronized, so will likely be out of date.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: If Ptr is empty then try to load the object from esp/esm.
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectId ObjectRegistry::registerPtr(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
ObjectId id = ptr.getCellRef().getOrAssignRefNum(mLastAssignedId);
|
|
|
|
mChanged = true;
|
|
|
|
mObjectMapping[id] = ptr;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectId ObjectRegistry::deregisterPtr(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
ObjectId id = getId(ptr);
|
|
|
|
mChanged = true;
|
|
|
|
mObjectMapping.erase(id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|