]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_entity.cpp
Expose collision information to LuaEntity on_step
[dragonfireclient.git] / src / script / cpp_api / s_entity.cpp
index 0d159846a0b6662ce1422d6dac425a1dce0a9ed2..ea9320051e55cc075ddd25e4264c3ee354dc74e1 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)
 {
@@ -56,7 +57,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
 
        // Add object reference
        // This should be userdata with metatable ObjectRef
-       objectrefGet(L, id);
+       push_objectRef(L, id);
        luaL_checktype(L, -1, LUA_TUSERDATA);
        if (!luaL_checkudata(L, -1, "ObjectRef"))
                luaL_typerror(L, -1, "ObjectRef");
@@ -80,6 +81,8 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
 
        verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Get core.luaentities[id]
        luaentity_get(L, id);
        int object = lua_gettop(L);
@@ -93,11 +96,11 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
                lua_pushinteger(L, dtime_s);
 
                setOriginFromTable(object);
-               PCALL_RES(lua_pcall(L, 3, 0, m_errorhandler));
+               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)
@@ -126,6 +129,8 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
 
        //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Get core.luaentities[id]
        luaentity_get(L, id);
        int object = lua_gettop(L);
@@ -140,9 +145,10 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
        lua_pushvalue(L, object); // self
 
        setOriginFromTable(object);
-       PCALL_RES(lua_pcall(L, 1, 1, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 1, 1, error_handler));
 
-       lua_remove(L, object); // Remove object
+       lua_remove(L, object);
+       lua_remove(L, error_handler);
 
        size_t len = 0;
        const char *s = lua_tolstring(L, -1, &len);
@@ -151,7 +157,7 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
 }
 
 void ScriptApiEntity::luaentity_GetProperties(u16 id,
-               ObjectProperties *prop)
+               ServerActiveObject *self, ObjectProperties *prop)
 {
        SCRIPTAPI_PRECHECKHEADER
 
@@ -163,38 +169,21 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
        // Set default values that differ from ObjectProperties defaults
        prop->hp_max = 10;
 
-       /* Read stuff */
-
-       prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
-
-       getboolfield(L, -1, "physical", prop->physical);
-       getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
-
-       getfloatfield(L, -1, "weight", prop->weight);
-
-       lua_getfield(L, -1, "collisionbox");
-       if (lua_istable(L, -1))
-               prop->collisionbox = read_aabb3f(L, -1, 1.0);
-       lua_pop(L, 1);
-
-       getstringfield(L, -1, "visual", prop->visual);
-
-       getstringfield(L, -1, "mesh", prop->mesh);
-
        // Deprecated: read object properties directly
-       read_object_properties(L, -1, prop);
+       read_object_properties(L, -1, self, prop, getServer()->idef());
 
        // Read initial_properties
        lua_getfield(L, -1, "initial_properties");
-       read_object_properties(L, -1, prop);
+       read_object_properties(L, -1, self, prop, getServer()->idef());
        lua_pop(L, 1);
 }
 
-void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
+void ScriptApiEntity::luaentity_Step(u16 id, float dtime,
+       const collisionMoveResult *moveresult)
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
+       int error_handler = PUSH_ERROR_HANDLER(L);
 
        // Get core.luaentities[id]
        luaentity_get(L, id);
@@ -209,23 +198,30 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_pushvalue(L, object); // self
        lua_pushnumber(L, dtime); // dtime
+       /* moveresult */
+       if (moveresult)
+               push_collision_move_result(L, *moveresult);
+       else
+               lua_pushnil(L);
 
        setOriginFromTable(object);
-       PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 3, 0, error_handler));
 
-       lua_pop(L, 1); // Pop object
+       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;
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Get core.luaentities[id]
        luaentity_get(L,id);
        int object = lua_gettop(L);
@@ -233,8 +229,8 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
        // 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
@@ -242,38 +238,68 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
        lua_pushnumber(L, time_from_last_punch);
        push_tool_capabilities(L, *toolcap);
        push_v3f(L, dir);
+       lua_pushnumber(L, damage);
 
        setOriginFromTable(object);
-       PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 6, 1, error_handler));
 
-       lua_pop(L, 1); // Pop object
+       bool retval = readParam<bool>(L, -1);
+       lua_pop(L, 2); // Pop object and error handler
+       return retval;
 }
 
-// Calls entity:on_rightclick(ObjectRef clicker)
-void ScriptApiEntity::luaentity_Rightclick(u16 id,
-               ServerActiveObject *clicker)
+// Calls entity[field](ObjectRef self, ObjectRef sao)
+bool ScriptApiEntity::luaentity_run_simple_callback(u16 id,
+       ServerActiveObject *sao, const char *field)
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
+       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_rightclick");
+       lua_getfield(L, -1, field);
        if (lua_isnil(L, -1)) {
-               lua_pop(L, 2); // Pop on_rightclick and entity
-               return;
+               lua_pop(L, 2); // Pop callback field and entity
+               return false;
        }
        luaL_checktype(L, -1, LUA_TFUNCTION);
-       lua_pushvalue(L, object); // self
-       objectrefGetOrCreate(L, clicker); // Clicker reference
+       lua_pushvalue(L, object);  // self
+       objectrefGetOrCreate(L, sao);  // killer reference
 
        setOriginFromTable(object);
-       PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 2, 1, error_handler));
 
-       lua_pop(L, 1); // Pop object
+       bool retval = readParam<bool>(L, -1);
+       lua_pop(L, 2); // Pop object and error handler
+       return retval;
+}
+
+bool ScriptApiEntity::luaentity_on_death(u16 id, ServerActiveObject *killer)
+{
+       return luaentity_run_simple_callback(id, killer, "on_death");
+}
+
+// Calls entity:on_rightclick(ObjectRef clicker)
+void ScriptApiEntity::luaentity_Rightclick(u16 id, ServerActiveObject *clicker)
+{
+       luaentity_run_simple_callback(id, clicker, "on_rightclick");
 }
 
+void ScriptApiEntity::luaentity_on_attach_child(u16 id, ServerActiveObject *child)
+{
+       luaentity_run_simple_callback(id, child, "on_attach_child");
+}
+
+void ScriptApiEntity::luaentity_on_detach_child(u16 id, ServerActiveObject *child)
+{
+       luaentity_run_simple_callback(id, child, "on_detach_child");
+}
+
+void ScriptApiEntity::luaentity_on_detach(u16 id, ServerActiveObject *parent)
+{
+       luaentity_run_simple_callback(id, parent, "on_detach");
+}