aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/lua_api')
-rw-r--r--src/script/lua_api/CMakeLists.txt1
-rw-r--r--src/script/lua_api/l_client.cpp22
-rw-r--r--src/script/lua_api/l_client.h7
-rw-r--r--src/script/lua_api/l_clientobject.cpp199
-rw-r--r--src/script/lua_api/l_clientobject.h77
-rw-r--r--src/script/lua_api/l_localplayer.cpp13
-rw-r--r--src/script/lua_api/l_localplayer.h3
-rw-r--r--src/script/lua_api/l_util.cpp2
8 files changed, 322 insertions, 2 deletions
diff --git a/src/script/lua_api/CMakeLists.txt b/src/script/lua_api/CMakeLists.txt
index 32f6a2793..ee94737d6 100644
--- a/src/script/lua_api/CMakeLists.txt
+++ b/src/script/lua_api/CMakeLists.txt
@@ -28,6 +28,7 @@ set(common_SCRIPT_LUA_API_SRCS
set(client_SCRIPT_LUA_API_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/l_camera.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_client.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_clientobject.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_localplayer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_mainmenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_minimap.cpp
diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp
index 56ce6471c..9961471ff 100644
--- a/src/script/lua_api/l_client.cpp
+++ b/src/script/lua_api/l_client.cpp
@@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_base.h"
#include "gettext.h"
#include "l_internal.h"
+#include "l_clientobject.h"
#include "lua_api/l_nodemeta.h"
#include "gui/mainmenumanager.h"
#include "map.h"
@@ -503,6 +504,26 @@ int ModApiClient::l_drop_selected_item(lua_State *L)
return 0;
}
+// get_objects_inside_radius(pos, radius)
+int ModApiClient::l_get_objects_inside_radius(lua_State *L)
+{
+ ClientEnvironment &env = getClient(L)->getEnv();
+
+ v3f pos = checkFloatPos(L, 1);
+ float radius = readParam<float>(L, 2) * BS;
+
+ std::vector<DistanceSortedActiveObject> objs;
+ env.getActiveObjects(pos, radius, objs);
+
+ int i = 0;
+ lua_createtable(L, objs.size(), 0);
+ for (const auto obj : objs) {
+ ClientObjectRef::create(L, obj.obj); // TODO: getObjectRefOrCreate
+ lua_rawseti(L, -2, ++i);
+ }
+ return 1;
+}
+
void ModApiClient::Initialize(lua_State *L, int top)
{
API_FCT(get_current_modname);
@@ -536,4 +557,5 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(get_inventory);
API_FCT(set_keypress);
API_FCT(drop_selected_item);
+ API_FCT(get_objects_inside_radius);
}
diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h
index 4e49ad60e..5863e5717 100644
--- a/src/script/lua_api/l_client.h
+++ b/src/script/lua_api/l_client.h
@@ -119,10 +119,13 @@ private:
// set_keypress(key_setting, pressed)
static int l_set_keypress(lua_State *L);
-
+
// drop_selected_item()
static int l_drop_selected_item(lua_State *L);
-public:
+ // get_objects_inside_radius(pos, radius)
+ static int l_get_objects_inside_radius(lua_State *L);
+
+public:
static void Initialize(lua_State *L, int top);
};
diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp
new file mode 100644
index 000000000..2ae614ea0
--- /dev/null
+++ b/src/script/lua_api/l_clientobject.cpp
@@ -0,0 +1,199 @@
+/*
+Dragonfire
+Copyright (C) 2020 system32
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "lua_api/l_clientobject.h"
+#include "l_internal.h"
+#include "common/c_converter.h"
+#include "client/client.h"
+#include "object_properties.h"
+
+// should prob do some more NULL checking
+
+
+ClientObjectRef *ClientObjectRef::checkobject(lua_State *L, int narg)
+{
+ luaL_checktype(L, narg, LUA_TUSERDATA);
+ void *userdata = luaL_checkudata(L, narg, className);
+ if (!userdata)
+ luaL_typerror(L, narg, className);
+ return *(ClientObjectRef**)userdata;
+}
+
+ClientActiveObject *ClientObjectRef::get_cao(ClientObjectRef *ref)
+{
+ ClientActiveObject *obj = ref->m_object;
+ return obj;
+}
+
+GenericCAO *ClientObjectRef::get_generic_cao(ClientObjectRef *ref, lua_State *L)
+{
+ ClientActiveObject *obj = get_cao(ref);
+ ClientEnvironment &env = getClient(L)->getEnv();
+ GenericCAO *gcao = env.getGenericCAO(obj->getId());
+ return gcao;
+}
+
+int ClientObjectRef::l_get_pos(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ ClientActiveObject *cao = get_cao(ref);
+ push_v3f(L, cao->getPosition() / BS);
+ return 1;
+}
+
+int ClientObjectRef::l_get_velocity(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ push_v3f(L, gcao->getVelocity() / BS);
+ return 1;
+}
+
+int ClientObjectRef::l_get_acceleration(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ push_v3f(L, gcao->getAcceleration() / BS);
+ return 1;
+}
+
+int ClientObjectRef::l_get_rotation(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ push_v3f(L, gcao->getRotation());
+ return 1;
+}
+
+int ClientObjectRef::l_is_player(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ lua_pushboolean(L, gcao->isPlayer());
+ return 1;
+}
+
+int ClientObjectRef::l_get_name(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ lua_pushstring(L, gcao->getName().c_str());
+ return 1;
+}
+
+int ClientObjectRef::l_get_attach(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ create(L, gcao->getParent());
+ return 1;
+}
+
+int ClientObjectRef::l_get_nametag(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ ObjectProperties *props = gcao->getProperties();
+ lua_pushstring(L, props->nametag.c_str());
+ return 1;
+}
+
+int ClientObjectRef::l_get_item_textures(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ ObjectProperties *props = gcao->getProperties();
+ lua_newtable(L);
+
+ for (std::string &texture : props->textures) {
+ lua_pushstring(L, texture.c_str());
+ }
+ return 1;
+}
+
+int ClientObjectRef::l_get_max_hp(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ ObjectProperties *props = gcao->getProperties();
+ lua_pushnumber(L, props->hp_max);
+ return 1;
+}
+
+ClientObjectRef::ClientObjectRef(ClientActiveObject *object):
+ m_object(object)
+{
+}
+
+void ClientObjectRef::create(lua_State *L, ClientActiveObject *object)
+{
+ if (object) {
+ ClientObjectRef *o = new ClientObjectRef(object);
+ *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
+ luaL_getmetatable(L, className);
+ lua_setmetatable(L, -2);
+ }
+}
+
+int ClientObjectRef::gc_object(lua_State *L) {
+ ClientObjectRef *obj = *(ClientObjectRef **)(lua_touserdata(L, 1));
+ delete obj;
+ return 0;
+}
+
+// taken from LuaLocalPlayer
+void ClientObjectRef::Register(lua_State *L)
+{
+ lua_newtable(L);
+ int methodtable = lua_gettop(L);
+ luaL_newmetatable(L, className);
+ int metatable = lua_gettop(L);
+
+ lua_pushliteral(L, "__metatable");
+ lua_pushvalue(L, methodtable);
+ lua_settable(L, metatable); // hide metatable from lua getmetatable()
+
+ lua_pushliteral(L, "__index");
+ lua_pushvalue(L, methodtable);
+ lua_settable(L, metatable);
+
+ lua_pushliteral(L, "__gc");
+ lua_pushcfunction(L, gc_object);
+ lua_settable(L, metatable);
+
+ lua_pop(L, 1); // Drop metatable
+
+ luaL_openlib(L, 0, methods, 0); // fill methodtable
+ lua_pop(L, 1); // Drop methodtable
+}
+
+const char ClientObjectRef::className[] = "ClientObjectRef";
+luaL_Reg ClientObjectRef::methods[] = {
+ luamethod(ClientObjectRef, get_pos),
+ luamethod(ClientObjectRef, get_velocity),
+ luamethod(ClientObjectRef, get_acceleration),
+ luamethod(ClientObjectRef, get_rotation),
+ luamethod(ClientObjectRef, is_player),
+ luamethod(ClientObjectRef, get_name),
+ luamethod(ClientObjectRef, get_attach),
+ luamethod(ClientObjectRef, get_nametag),
+ luamethod(ClientObjectRef, get_item_textures),
+ luamethod(ClientObjectRef, get_max_hp),
+ {0, 0}
+};
diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h
new file mode 100644
index 000000000..c538c339e
--- /dev/null
+++ b/src/script/lua_api/l_clientobject.h
@@ -0,0 +1,77 @@
+/*
+Dragonfire
+Copyright (C) 2020 system32
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "lua_api/l_base.h"
+#include "client/clientobject.h"
+#include "client/content_cao.h"
+
+class ClientObjectRef : public ModApiBase
+{
+public:
+ ClientObjectRef(ClientActiveObject *object);
+
+ ~ClientObjectRef() = default;
+
+ static void Register(lua_State *L);
+
+ static void create(lua_State *L, ClientActiveObject *object);
+
+ static ClientObjectRef *checkobject(lua_State *L, int narg);
+
+private:
+ ClientActiveObject *m_object = nullptr;
+ static const char className[];
+ static luaL_Reg methods[];
+
+ static ClientActiveObject *get_cao(ClientObjectRef *ref);
+ static GenericCAO *get_generic_cao(ClientObjectRef *ref, lua_State *L);
+
+ static int gc_object(lua_State *L);
+
+ // get_pos(self)
+ // returns: {x=num, y=num, z=num}
+ static int l_get_pos(lua_State *L);
+
+ // get_velocity(self)
+ static int l_get_velocity(lua_State *L);
+
+ // get_acceleration(self)
+ static int l_get_acceleration(lua_State *L);
+
+ // get_rotation(self)
+ static int l_get_rotation(lua_State *L);
+
+ // is_player(self)
+ static int l_is_player(lua_State *L);
+
+ // get_name(self)
+ static int l_get_name(lua_State *L);
+
+ // get_attach(self)
+ static int l_get_attach(lua_State *L);
+
+ // get_nametag(self)
+ static int l_get_nametag(lua_State *L);
+
+ // get_textures(self)
+ static int l_get_item_textures(lua_State *L);
+
+ // get_hp(self)
+ static int l_get_max_hp(lua_State *L);
+};
diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp
index 8e743c3ab..8057802a4 100644
--- a/src/script/lua_api/l_localplayer.cpp
+++ b/src/script/lua_api/l_localplayer.cpp
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "l_clientobject.h"
#include "l_localplayer.h"
#include "l_internal.h"
#include "lua_api/l_item.h"
@@ -452,6 +453,17 @@ int LuaLocalPlayer::l_hud_get(lua_State *L)
return 1;
}
+int LuaLocalPlayer::l_get_object(lua_State *L)
+{
+ LocalPlayer *player = getobject(L, 1);
+ ClientEnvironment &env = getClient(L)->getEnv();
+ ClientActiveObject *obj = env.getGenericCAO(player->getCAO()->getId());
+
+ ClientObjectRef::create(L, obj);
+
+ return 1;
+}
+
LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
@@ -546,6 +558,7 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, hud_remove),
luamethod(LuaLocalPlayer, hud_change),
luamethod(LuaLocalPlayer, hud_get),
+ luamethod(LuaLocalPlayer, get_object),
{0, 0}
};
diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h
index f43eb2a4d..8daa901e0 100644
--- a/src/script/lua_api/l_localplayer.h
+++ b/src/script/lua_api/l_localplayer.h
@@ -111,6 +111,9 @@ private:
// hud_get(self, id)
static int l_hud_get(lua_State *L);
+ // get_object(self)
+ static int l_get_object(lua_State *L);
+
LocalPlayer *m_localplayer = nullptr;
public:
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index 99a006ec1..4595dc1c1 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -531,6 +531,8 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
API_FCT(compress);
API_FCT(decompress);
+ API_FCT(request_insecure_environment);
+
API_FCT(encode_base64);
API_FCT(decode_base64);