2020-12-18 23:21:10 +01:00
|
|
|
#ifndef MWLUA_OBJECT_H
|
|
|
|
#define MWLUA_OBJECT_H
|
|
|
|
|
2022-05-16 17:04:41 +02:00
|
|
|
#include <map>
|
2021-01-29 01:54:54 +01:00
|
|
|
#include <typeindex>
|
|
|
|
|
2022-02-15 19:38:47 +01:00
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
2022-01-22 15:58:41 +01:00
|
|
|
#include <components/esm3/cellref.hpp>
|
2020-12-18 23:21:10 +01:00
|
|
|
|
2022-12-23 19:57:12 +01:00
|
|
|
#include "../mwbase/environment.hpp"
|
2020-12-18 23:21:10 +01:00
|
|
|
#include "../mwworld/ptr.hpp"
|
2022-12-23 19:57:12 +01:00
|
|
|
#include "../mwworld/worldmodel.hpp"
|
2020-12-18 23:21:10 +01:00
|
|
|
|
|
|
|
namespace MWLua
|
|
|
|
{
|
|
|
|
// ObjectId is a unique identifier of a game object.
|
|
|
|
// It can change only if the order of content files was change.
|
|
|
|
using ObjectId = ESM::RefNum;
|
|
|
|
inline const ObjectId& getId(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
return ptr.getCellRef().getRefNum();
|
|
|
|
}
|
|
|
|
std::string idToString(const ObjectId& id);
|
2021-01-29 01:54:54 +01:00
|
|
|
std::string ptrToString(const MWWorld::Ptr& ptr);
|
2021-04-17 11:50:20 +02:00
|
|
|
bool isMarker(const MWWorld::Ptr& ptr);
|
2020-12-18 23:21:10 +01:00
|
|
|
|
|
|
|
// Lua scripts can't use MWWorld::Ptr directly, because lifetime of a script can be longer than lifetime of Ptr.
|
|
|
|
// `GObject` and `LObject` are intended to be passed to Lua as a userdata.
|
|
|
|
// It automatically updates the underlying Ptr when needed.
|
|
|
|
class Object
|
|
|
|
{
|
|
|
|
public:
|
2022-12-23 19:57:12 +01:00
|
|
|
Object(ObjectId id)
|
2020-12-18 23:21:10 +01:00
|
|
|
: mId(id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~Object() {}
|
|
|
|
ObjectId id() const { return mId; }
|
|
|
|
|
|
|
|
std::string toString() const;
|
|
|
|
|
|
|
|
// Updates and returns the underlying Ptr. Throws an exception if object is not available.
|
|
|
|
const MWWorld::Ptr& ptr() const;
|
|
|
|
|
|
|
|
// Returns `true` if calling `ptr()` is safe.
|
|
|
|
bool isValid() const;
|
|
|
|
|
2022-02-15 19:38:47 +01:00
|
|
|
virtual sol::object getObject(lua_State* lua, ObjectId id) const = 0; // returns LObject or GOBject
|
|
|
|
virtual sol::object getCell(lua_State* lua, MWWorld::CellStore* store) const = 0; // returns LCell or GCell
|
|
|
|
|
2020-12-18 23:21:10 +01:00
|
|
|
protected:
|
|
|
|
const ObjectId mId;
|
|
|
|
|
|
|
|
mutable MWWorld::Ptr mPtr;
|
2022-12-23 19:57:12 +01:00
|
|
|
mutable size_t mLastUpdate = 0;
|
2020-12-18 23:21:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Used only in local scripts
|
2022-02-15 19:38:47 +01:00
|
|
|
struct LCell
|
|
|
|
{
|
|
|
|
MWWorld::CellStore* mStore;
|
|
|
|
};
|
2020-12-18 23:21:10 +01:00
|
|
|
class LObject : public Object
|
|
|
|
{
|
|
|
|
using Object::Object;
|
2022-12-23 19:57:12 +01:00
|
|
|
sol::object getObject(lua_State* lua, ObjectId id) const final { return sol::make_object<LObject>(lua, id); }
|
2022-02-15 19:38:47 +01:00
|
|
|
sol::object getCell(lua_State* lua, MWWorld::CellStore* store) const final
|
|
|
|
{
|
|
|
|
return sol::make_object(lua, LCell{ store });
|
|
|
|
}
|
2020-12-18 23:21:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Used only in global scripts
|
2022-02-15 19:38:47 +01:00
|
|
|
struct GCell
|
|
|
|
{
|
|
|
|
MWWorld::CellStore* mStore;
|
|
|
|
};
|
2020-12-18 23:21:10 +01:00
|
|
|
class GObject : public Object
|
|
|
|
{
|
|
|
|
using Object::Object;
|
2022-12-23 19:57:12 +01:00
|
|
|
sol::object getObject(lua_State* lua, ObjectId id) const final { return sol::make_object<GObject>(lua, id); }
|
2022-02-15 19:38:47 +01:00
|
|
|
sol::object getCell(lua_State* lua, MWWorld::CellStore* store) const final
|
|
|
|
{
|
|
|
|
return sol::make_object(lua, GCell{ store });
|
|
|
|
}
|
2020-12-18 23:21:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using ObjectIdList = std::shared_ptr<std::vector<ObjectId>>;
|
|
|
|
template <typename Obj>
|
|
|
|
struct ObjectList
|
|
|
|
{
|
|
|
|
ObjectIdList mIds;
|
|
|
|
};
|
|
|
|
using GObjectList = ObjectList<GObject>;
|
|
|
|
using LObjectList = ObjectList<LObject>;
|
|
|
|
|
2022-03-10 00:31:44 +01:00
|
|
|
template <typename Obj>
|
|
|
|
struct Inventory
|
|
|
|
{
|
|
|
|
Obj mObj;
|
|
|
|
};
|
|
|
|
|
2020-12-18 23:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MWLUA_OBJECT_H
|