/* Minetest Copyright (C) 2013 celeron55, Perttu Ahola 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 "cpp_api/s_entity.h" #include "cpp_api/s_internal.h" #include "log.h" #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) { SCRIPTAPI_PRECHECKHEADER verbosestream<<"scriptapi_luaentity_add: id="<idef()); lua_pop(L, 1); } void ScriptApiEntity::luaentity_Step(u16 id, float dtime, const collisionMoveResult *moveresult) { SCRIPTAPI_PRECHECKHEADER 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 step function lua_getfield(L, -1, "on_step"); if (lua_isnil(L, -1)) { lua_pop(L, 2); // Pop on_step and entity return; } 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, 3, 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, damage) bool ScriptApiEntity::luaentity_Punch(u16 id, ServerActiveObject *puncher, float time_from_last_punch, const ToolCapabilities *toolcap, v3f dir, s16 damage) { SCRIPTAPI_PRECHECKHEADER //infostream<<"scriptapi_luaentity_step: id="<(L, -1); lua_pop(L, 2); // Pop object and error handler return retval; } // Calls entity[field](ObjectRef self, ObjectRef sao) bool ScriptApiEntity::luaentity_run_simple_callback(u16 id, ServerActiveObject *sao, const char *field) { SCRIPTAPI_PRECHECKHEADER 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, field); if (lua_isnil(L, -1)) { lua_pop(L, 2); // Pop callback field and entity return false; } luaL_checktype(L, -1, LUA_TFUNCTION); lua_pushvalue(L, object); // self objectrefGetOrCreate(L, sao); // killer reference setOriginFromTable(object); PCALL_RES(lua_pcall(L, 2, 1, error_handler)); bool retval = readParam(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"); }