2020-12-18 23:21:10 +01:00
|
|
|
#include "luabindings.hpp"
|
|
|
|
|
|
|
|
#include <components/lua/luastate.hpp>
|
2021-02-17 22:56:14 +01:00
|
|
|
#include <components/queries/luabindings.hpp>
|
2020-12-18 23:21:10 +01:00
|
|
|
|
|
|
|
#include "eventqueue.hpp"
|
|
|
|
#include "worldview.hpp"
|
|
|
|
|
|
|
|
namespace MWLua
|
|
|
|
{
|
|
|
|
|
2021-01-29 01:54:54 +01:00
|
|
|
static sol::table definitionList(LuaUtil::LuaState& lua, std::initializer_list<std::string> values)
|
|
|
|
{
|
|
|
|
sol::table res(lua.sol(), sol::create);
|
|
|
|
for (const std::string& v : values)
|
|
|
|
res[v] = v;
|
|
|
|
return lua.makeReadOnly(res);
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:21:10 +01:00
|
|
|
sol::table initCorePackage(const Context& context)
|
|
|
|
{
|
|
|
|
sol::table api(context.mLua->sol(), sol::create);
|
|
|
|
api["sendGlobalEvent"] = [context](std::string eventName, const sol::object& eventData)
|
|
|
|
{
|
|
|
|
context.mGlobalEventQueue->push_back({std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer)});
|
|
|
|
};
|
2021-03-12 18:29:51 +01:00
|
|
|
api["getGameTimeInSeconds"] = [world=context.mWorldView]() { return world->getGameTimeInSeconds(); };
|
|
|
|
api["getGameTimeInHours"] = [world=context.mWorldView]() { return world->getGameTimeInHours(); };
|
2021-01-29 01:54:54 +01:00
|
|
|
api["OBJECT_TYPE"] = definitionList(*context.mLua,
|
|
|
|
{
|
|
|
|
"Activator", "Armor", "Book", "Clothing", "Creature", "Door", "Ingredient",
|
|
|
|
"Light", "Miscellaneous", "NPC", "Player", "Potion", "Static", "Weapon"
|
|
|
|
});
|
2020-12-18 23:21:10 +01:00
|
|
|
return context.mLua->makeReadOnly(api);
|
|
|
|
}
|
|
|
|
|
|
|
|
sol::table initWorldPackage(const Context& context)
|
|
|
|
{
|
|
|
|
sol::table api(context.mLua->sol(), sol::create);
|
|
|
|
WorldView* worldView = context.mWorldView;
|
|
|
|
api["activeActors"] = GObjectList{worldView->getActorsInScene()};
|
2021-02-17 22:56:14 +01:00
|
|
|
api["selectObjects"] = [context](const Queries::Query& query)
|
|
|
|
{
|
|
|
|
ObjectIdList list;
|
|
|
|
WorldView* worldView = context.mWorldView;
|
|
|
|
if (query.mQueryType == "activators")
|
|
|
|
list = worldView->getActivatorsInScene();
|
|
|
|
else if (query.mQueryType == "actors")
|
|
|
|
list = worldView->getActorsInScene();
|
|
|
|
else if (query.mQueryType == "containers")
|
|
|
|
list = worldView->getContainersInScene();
|
|
|
|
else if (query.mQueryType == "doors")
|
|
|
|
list = worldView->getDoorsInScene();
|
|
|
|
else if (query.mQueryType == "items")
|
|
|
|
list = worldView->getItemsInScene();
|
|
|
|
return GObjectList{selectObjectsFromList(query, list, context)};
|
|
|
|
// TODO: Use sqlite to search objects that are not in the scene
|
|
|
|
// return GObjectList{worldView->selectObjects(query, false)};
|
|
|
|
};
|
2020-12-18 23:21:10 +01:00
|
|
|
return context.mLua->makeReadOnly(api);
|
|
|
|
}
|
|
|
|
|
|
|
|
sol::table initNearbyPackage(const Context& context)
|
|
|
|
{
|
|
|
|
sol::table api(context.mLua->sol(), sol::create);
|
|
|
|
WorldView* worldView = context.mWorldView;
|
2021-01-29 01:54:54 +01:00
|
|
|
api["activators"] = LObjectList{worldView->getActivatorsInScene()};
|
2020-12-18 23:21:10 +01:00
|
|
|
api["actors"] = LObjectList{worldView->getActorsInScene()};
|
2021-01-29 01:54:54 +01:00
|
|
|
api["containers"] = LObjectList{worldView->getContainersInScene()};
|
|
|
|
api["doors"] = LObjectList{worldView->getDoorsInScene()};
|
2020-12-18 23:21:10 +01:00
|
|
|
api["items"] = LObjectList{worldView->getItemsInScene()};
|
2021-02-17 22:56:14 +01:00
|
|
|
api["selectObjects"] = [context](const Queries::Query& query)
|
|
|
|
{
|
|
|
|
ObjectIdList list;
|
|
|
|
WorldView* worldView = context.mWorldView;
|
|
|
|
if (query.mQueryType == "activators")
|
|
|
|
list = worldView->getActivatorsInScene();
|
|
|
|
else if (query.mQueryType == "actors")
|
|
|
|
list = worldView->getActorsInScene();
|
|
|
|
else if (query.mQueryType == "containers")
|
|
|
|
list = worldView->getContainersInScene();
|
|
|
|
else if (query.mQueryType == "doors")
|
|
|
|
list = worldView->getDoorsInScene();
|
|
|
|
else if (query.mQueryType == "items")
|
|
|
|
list = worldView->getItemsInScene();
|
|
|
|
return LObjectList{selectObjectsFromList(query, list, context)};
|
|
|
|
// TODO: Maybe use sqlite
|
|
|
|
// return LObjectList{worldView->selectObjects(query, true)};
|
|
|
|
};
|
2020-12-18 23:21:10 +01:00
|
|
|
return context.mLua->makeReadOnly(api);
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:56:14 +01:00
|
|
|
sol::table initQueryPackage(const Context& context)
|
|
|
|
{
|
|
|
|
Queries::registerQueryBindings(context.mLua->sol());
|
|
|
|
sol::table query(context.mLua->sol(), sol::create);
|
|
|
|
for (std::string_view t : ObjectQueryTypes::types)
|
|
|
|
query[t] = Queries::Query(std::string(t));
|
|
|
|
for (const QueryFieldGroup& group : getBasicQueryFieldGroups())
|
|
|
|
query[group.mName] = initFieldGroup(context, group);
|
|
|
|
return query; // makeReadonly is applied by LuaState::addCommonPackage
|
|
|
|
}
|
|
|
|
|
|
|
|
sol::table initFieldGroup(const Context& context, const QueryFieldGroup& group)
|
|
|
|
{
|
|
|
|
sol::table res(context.mLua->sol(), sol::create);
|
|
|
|
for (const Queries::Field* field : group.mFields)
|
|
|
|
{
|
|
|
|
sol::table subgroup = res;
|
|
|
|
for (int i = 0; i < static_cast<int>(field->path().size()) - 1; ++i)
|
|
|
|
{
|
|
|
|
const std::string& name = field->path()[i];
|
|
|
|
if (subgroup[name] == sol::nil)
|
|
|
|
subgroup[name] = context.mLua->makeReadOnly(context.mLua->newTable());
|
|
|
|
subgroup = context.mLua->getMutableFromReadOnly(subgroup[name]);
|
|
|
|
}
|
|
|
|
subgroup[field->path().back()] = field;
|
|
|
|
}
|
|
|
|
return context.mLua->makeReadOnly(res);
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:21:10 +01:00
|
|
|
}
|
|
|
|
|