]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_entity.cpp
Expose getPointedThing to Lua
[dragonfireclient.git] / src / script / cpp_api / s_entity.cpp
index 8e79f88386ed3a29b656b8dda1f6e9ee3f5492a1..4c1e296d4925e63fa2aba18af9a0ac99fc24737f 100644 (file)
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "object_properties.h"
 #include "common/c_converter.h"
 #include "common/c_content.h"
+#include "server.h"
 
 bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
 {
@@ -31,8 +32,8 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
        verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
                        <<name<<"\""<<std::endl;
 
-       // Get minetest.registered_entities[name]
-       lua_getglobal(L, "minetest");
+       // Get core.registered_entities[name]
+       lua_getglobal(L, "core");
        lua_getfield(L, -1, "registered_entities");
        luaL_checktype(L, -1, LUA_TTABLE);
        lua_pushstring(L, name);
@@ -56,14 +57,14 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
 
        // Add object reference
        // This should be userdata with metatable ObjectRef
-       objectrefGet(id);
+       push_objectRef(L, id);
        luaL_checktype(L, -1, LUA_TUSERDATA);
        if (!luaL_checkudata(L, -1, "ObjectRef"))
                luaL_typerror(L, -1, "ObjectRef");
        lua_setfield(L, -2, "object");
 
-       // minetest.luaentities[id] = object
-       lua_getglobal(L, "minetest");
+       // core.luaentities[id] = object
+       lua_getglobal(L, "core");
        lua_getfield(L, -1, "luaentities");
        luaL_checktype(L, -1, LUA_TTABLE);
        lua_pushnumber(L, id); // Push id
@@ -80,7 +81,9 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
 
        verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
 
-       // Get minetest.luaentities[id]
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       // Get core.luaentities[id]
        luaentity_get(L, id);
        int object = lua_gettop(L);
 
@@ -91,13 +94,13 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
                lua_pushvalue(L, object); // self
                lua_pushlstring(L, staticdata.c_str(), staticdata.size());
                lua_pushinteger(L, dtime_s);
-               // Call with 3 arguments, 0 results
-               if (lua_pcall(L, 3, 0, m_errorhandler))
-                       scriptError();
+
+               setOriginFromTable(object);
+               PCALL_RES(lua_pcall(L, 3, 0, error_handler));
        } else {
                lua_pop(L, 1);
        }
-       lua_pop(L, 1); // Pop object
+       lua_pop(L, 2); // Pop object and error handler
 }
 
 void ScriptApiEntity::luaentity_Remove(u16 id)
@@ -106,8 +109,8 @@ void ScriptApiEntity::luaentity_Remove(u16 id)
 
        verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
 
-       // Get minetest.luaentities table
-       lua_getglobal(L, "minetest");
+       // Get core.luaentities table
+       lua_getglobal(L, "core");
        lua_getfield(L, -1, "luaentities");
        luaL_checktype(L, -1, LUA_TTABLE);
        int objectstable = lua_gettop(L);
@@ -117,7 +120,7 @@ void ScriptApiEntity::luaentity_Remove(u16 id)
        lua_pushnil(L);
        lua_settable(L, objectstable);
 
-       lua_pop(L, 2); // pop luaentities, minetest
+       lua_pop(L, 2); // pop luaentities, core
 }
 
 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
@@ -126,7 +129,9 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
 
        //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
 
-       // Get minetest.luaentities[id]
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       // Get core.luaentities[id]
        luaentity_get(L, id);
        int object = lua_gettop(L);
 
@@ -136,13 +141,14 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
                lua_pop(L, 2); // Pop entity and  get_staticdata
                return "";
        }
-
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_pushvalue(L, object); // self
-       // Call with 1 arguments, 1 results
-       if (lua_pcall(L, 1, 1, m_errorhandler))
-               scriptError();
-       lua_remove(L, object); // Remove object
+
+       setOriginFromTable(object);
+       PCALL_RES(lua_pcall(L, 1, 1, error_handler));
+
+       lua_remove(L, object);
+       lua_remove(L, error_handler);
 
        size_t len = 0;
        const char *s = lua_tolstring(L, -1, &len);
@@ -157,7 +163,7 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
 
        //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
 
-       // Get minetest.luaentities[id]
+       // Get core.luaentities[id]
        luaentity_get(L, id);
 
        // Set default values that differ from ObjectProperties defaults
@@ -182,11 +188,11 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
        getstringfield(L, -1, "mesh", prop->mesh);
 
        // Deprecated: read object properties directly
-       read_object_properties(L, -1, prop);
+       read_object_properties(L, -1, prop, getServer()->idef());
 
        // Read initial_properties
        lua_getfield(L, -1, "initial_properties");
-       read_object_properties(L, -1, prop);
+       read_object_properties(L, -1, prop, getServer()->idef());
        lua_pop(L, 1);
 }
 
@@ -196,7 +202,9 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
 
        //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
 
-       // Get minetest.luaentities[id]
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       // Get core.luaentities[id]
        luaentity_get(L, id);
        int object = lua_gettop(L);
        // State: object is at top of stack
@@ -209,42 +217,49 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_pushvalue(L, object); // self
        lua_pushnumber(L, dtime); // dtime
-       // Call with 2 arguments, 0 results
-       if (lua_pcall(L, 2, 0, m_errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop object
+
+       setOriginFromTable(object);
+       PCALL_RES(lua_pcall(L, 2, 0, error_handler));
+
+       lua_pop(L, 2); // Pop object and error handler
 }
 
 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
-//                       tool_capabilities, direction)
-void ScriptApiEntity::luaentity_Punch(u16 id,
+//                       tool_capabilities, direction, damage)
+bool ScriptApiEntity::luaentity_Punch(u16 id,
                ServerActiveObject *puncher, float time_from_last_punch,
-               const ToolCapabilities *toolcap, v3f dir)
+               const ToolCapabilities *toolcap, v3f dir, s16 damage)
 {
        SCRIPTAPI_PRECHECKHEADER
 
        //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
 
-       // Get minetest.luaentities[id]
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       // Get core.luaentities[id]
        luaentity_get(L,id);
        int object = lua_gettop(L);
        // State: object is at top of stack
        // Get function
        lua_getfield(L, -1, "on_punch");
        if (lua_isnil(L, -1)) {
-               lua_pop(L, 2); // Pop on_punch and entitu
-               return;
+               lua_pop(L, 2); // Pop on_punch and entity
+               return false;
        }
        luaL_checktype(L, -1, LUA_TFUNCTION);
-       lua_pushvalue(L, object);                // self
-       objectrefGetOrCreate(puncher);           // Clicker reference
+       lua_pushvalue(L, object);  // self
+       objectrefGetOrCreate(L, puncher);  // Clicker reference
        lua_pushnumber(L, time_from_last_punch);
        push_tool_capabilities(L, *toolcap);
        push_v3f(L, dir);
-       // Call with 5 arguments, 0 results
-       if (lua_pcall(L, 5, 0, m_errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop object
+       lua_pushnumber(L, damage);
+
+       setOriginFromTable(object);
+       PCALL_RES(lua_pcall(L, 6, 1, error_handler));
+
+       bool retval = lua_toboolean(L, -1);
+       lua_pop(L, 2); // Pop object and error handler
+       return retval;
 }
 
 // Calls entity:on_rightclick(ObjectRef clicker)
@@ -255,7 +270,9 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
 
        //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
 
-       // Get minetest.luaentities[id]
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       // Get core.luaentities[id]
        luaentity_get(L, id);
        int object = lua_gettop(L);
        // State: object is at top of stack
@@ -267,10 +284,11 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
        }
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_pushvalue(L, object); // self
-       objectrefGetOrCreate(clicker); // Clicker reference
-       // Call with 2 arguments, 0 results
-       if (lua_pcall(L, 2, 0, m_errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop object
+       objectrefGetOrCreate(L, clicker); // Clicker reference
+
+       setOriginFromTable(object);
+       PCALL_RES(lua_pcall(L, 2, 0, error_handler));
+
+       lua_pop(L, 2); // Pop object and error handler
 }