3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "cpp_api/s_entity.h"
21 #include "cpp_api/s_internal.h"
23 #include "object_properties.h"
24 #include "common/c_converter.h"
25 #include "common/c_content.h"
28 bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
30 SCRIPTAPI_PRECHECKHEADER
32 verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
33 <<name<<"\""<<std::endl;
35 // Get core.registered_entities[name]
36 lua_getglobal(L, "core");
37 lua_getfield(L, -1, "registered_entities");
38 luaL_checktype(L, -1, LUA_TTABLE);
39 lua_pushstring(L, name);
41 // Should be a table, which we will use as a prototype
42 //luaL_checktype(L, -1, LUA_TTABLE);
43 if (lua_type(L, -1) != LUA_TTABLE){
44 errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
47 int prototype_table = lua_gettop(L);
48 //dump2(L, "prototype_table");
50 // Create entity object
52 int object = lua_gettop(L);
54 // Set object metatable
55 lua_pushvalue(L, prototype_table);
56 lua_setmetatable(L, -2);
58 // Add object reference
59 // This should be userdata with metatable ObjectRef
60 push_objectRef(L, id);
61 luaL_checktype(L, -1, LUA_TUSERDATA);
62 if (!luaL_checkudata(L, -1, "ObjectRef"))
63 luaL_typerror(L, -1, "ObjectRef");
64 lua_setfield(L, -2, "object");
66 // core.luaentities[id] = object
67 lua_getglobal(L, "core");
68 lua_getfield(L, -1, "luaentities");
69 luaL_checktype(L, -1, LUA_TTABLE);
70 lua_pushnumber(L, id); // Push id
71 lua_pushvalue(L, object); // Copy object to top of stack
77 void ScriptApiEntity::luaentity_Activate(u16 id,
78 const std::string &staticdata, u32 dtime_s)
80 SCRIPTAPI_PRECHECKHEADER
82 verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
84 int error_handler = PUSH_ERROR_HANDLER(L);
86 // Get core.luaentities[id]
88 int object = lua_gettop(L);
90 // Get on_activate function
91 lua_getfield(L, -1, "on_activate");
92 if (!lua_isnil(L, -1)) {
93 luaL_checktype(L, -1, LUA_TFUNCTION);
94 lua_pushvalue(L, object); // self
95 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
96 lua_pushinteger(L, dtime_s);
98 setOriginFromTable(object);
99 PCALL_RES(lua_pcall(L, 3, 0, error_handler));
103 lua_pop(L, 2); // Pop object and error handler
106 void ScriptApiEntity::luaentity_Remove(u16 id)
108 SCRIPTAPI_PRECHECKHEADER
110 verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
112 // Get core.luaentities table
113 lua_getglobal(L, "core");
114 lua_getfield(L, -1, "luaentities");
115 luaL_checktype(L, -1, LUA_TTABLE);
116 int objectstable = lua_gettop(L);
118 // Set luaentities[id] = nil
119 lua_pushnumber(L, id); // Push id
121 lua_settable(L, objectstable);
123 lua_pop(L, 2); // pop luaentities, core
126 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
128 SCRIPTAPI_PRECHECKHEADER
130 //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
132 int error_handler = PUSH_ERROR_HANDLER(L);
134 // Get core.luaentities[id]
135 luaentity_get(L, id);
136 int object = lua_gettop(L);
138 // Get get_staticdata function
139 lua_getfield(L, -1, "get_staticdata");
140 if (lua_isnil(L, -1)) {
141 lua_pop(L, 2); // Pop entity and get_staticdata
144 luaL_checktype(L, -1, LUA_TFUNCTION);
145 lua_pushvalue(L, object); // self
147 setOriginFromTable(object);
148 PCALL_RES(lua_pcall(L, 1, 1, error_handler));
150 lua_remove(L, object);
151 lua_remove(L, error_handler);
154 const char *s = lua_tolstring(L, -1, &len);
155 lua_pop(L, 1); // Pop static data
156 return std::string(s, len);
159 void ScriptApiEntity::luaentity_GetProperties(u16 id,
160 ServerActiveObject *self, ObjectProperties *prop)
162 SCRIPTAPI_PRECHECKHEADER
164 //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
166 // Get core.luaentities[id]
167 luaentity_get(L, id);
169 // Set default values that differ from ObjectProperties defaults
172 // Deprecated: read object properties directly
173 read_object_properties(L, -1, self, prop, getServer()->idef());
175 // Read initial_properties
176 lua_getfield(L, -1, "initial_properties");
177 read_object_properties(L, -1, self, prop, getServer()->idef());
181 void ScriptApiEntity::luaentity_Step(u16 id, float dtime,
182 const collisionMoveResult *moveresult)
184 SCRIPTAPI_PRECHECKHEADER
186 int error_handler = PUSH_ERROR_HANDLER(L);
188 // Get core.luaentities[id]
189 luaentity_get(L, id);
190 int object = lua_gettop(L);
191 // State: object is at top of stack
193 lua_getfield(L, -1, "on_step");
194 if (lua_isnil(L, -1)) {
195 lua_pop(L, 2); // Pop on_step and entity
198 luaL_checktype(L, -1, LUA_TFUNCTION);
199 lua_pushvalue(L, object); // self
200 lua_pushnumber(L, dtime); // dtime
203 push_collision_move_result(L, *moveresult);
207 setOriginFromTable(object);
208 PCALL_RES(lua_pcall(L, 3, 0, error_handler));
210 lua_pop(L, 2); // Pop object and error handler
213 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
214 // tool_capabilities, direction, damage)
215 bool ScriptApiEntity::luaentity_Punch(u16 id,
216 ServerActiveObject *puncher, float time_from_last_punch,
217 const ToolCapabilities *toolcap, v3f dir, s16 damage)
219 SCRIPTAPI_PRECHECKHEADER
221 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
223 int error_handler = PUSH_ERROR_HANDLER(L);
225 // Get core.luaentities[id]
227 int object = lua_gettop(L);
228 // State: object is at top of stack
230 lua_getfield(L, -1, "on_punch");
231 if (lua_isnil(L, -1)) {
232 lua_pop(L, 2); // Pop on_punch and entity
235 luaL_checktype(L, -1, LUA_TFUNCTION);
236 lua_pushvalue(L, object); // self
237 objectrefGetOrCreate(L, puncher); // Clicker reference
238 lua_pushnumber(L, time_from_last_punch);
239 push_tool_capabilities(L, *toolcap);
241 lua_pushnumber(L, damage);
243 setOriginFromTable(object);
244 PCALL_RES(lua_pcall(L, 6, 1, error_handler));
246 bool retval = readParam<bool>(L, -1);
247 lua_pop(L, 2); // Pop object and error handler
251 // Calls entity[field](ObjectRef self, ObjectRef sao)
252 bool ScriptApiEntity::luaentity_run_simple_callback(u16 id,
253 ServerActiveObject *sao, const char *field)
255 SCRIPTAPI_PRECHECKHEADER
257 int error_handler = PUSH_ERROR_HANDLER(L);
259 // Get core.luaentities[id]
260 luaentity_get(L, id);
261 int object = lua_gettop(L);
262 // State: object is at top of stack
264 lua_getfield(L, -1, field);
265 if (lua_isnil(L, -1)) {
266 lua_pop(L, 2); // Pop callback field and entity
269 luaL_checktype(L, -1, LUA_TFUNCTION);
270 lua_pushvalue(L, object); // self
271 objectrefGetOrCreate(L, sao); // killer reference
273 setOriginFromTable(object);
274 PCALL_RES(lua_pcall(L, 2, 1, error_handler));
276 bool retval = readParam<bool>(L, -1);
277 lua_pop(L, 2); // Pop object and error handler
281 bool ScriptApiEntity::luaentity_on_death(u16 id, ServerActiveObject *killer)
283 return luaentity_run_simple_callback(id, killer, "on_death");
286 // Calls entity:on_rightclick(ObjectRef clicker)
287 void ScriptApiEntity::luaentity_Rightclick(u16 id, ServerActiveObject *clicker)
289 luaentity_run_simple_callback(id, clicker, "on_rightclick");
292 void ScriptApiEntity::luaentity_on_attach_child(u16 id, ServerActiveObject *child)
294 luaentity_run_simple_callback(id, child, "on_attach_child");
297 void ScriptApiEntity::luaentity_on_detach_child(u16 id, ServerActiveObject *child)
299 luaentity_run_simple_callback(id, child, "on_detach_child");
302 void ScriptApiEntity::luaentity_on_detach(u16 id, ServerActiveObject *parent)
304 luaentity_run_simple_callback(id, parent, "on_detach");