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"
27 bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
29 SCRIPTAPI_PRECHECKHEADER
31 verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
32 <<name<<"\""<<std::endl;
34 // Get minetest.registered_entities[name]
35 lua_getglobal(L, "minetest");
36 lua_getfield(L, -1, "registered_entities");
37 luaL_checktype(L, -1, LUA_TTABLE);
38 lua_pushstring(L, name);
40 // Should be a table, which we will use as a prototype
41 //luaL_checktype(L, -1, LUA_TTABLE);
42 if(lua_type(L, -1) != LUA_TTABLE){
43 errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
46 int prototype_table = lua_gettop(L);
47 //dump2(L, "prototype_table");
49 // Create entity object
51 int object = lua_gettop(L);
53 // Set object metatable
54 lua_pushvalue(L, prototype_table);
55 lua_setmetatable(L, -2);
57 // Add object reference
58 // This should be userdata with metatable ObjectRef
60 luaL_checktype(L, -1, LUA_TUSERDATA);
61 if(!luaL_checkudata(L, -1, "ObjectRef"))
62 luaL_typerror(L, -1, "ObjectRef");
63 lua_setfield(L, -2, "object");
65 // minetest.luaentities[id] = object
66 lua_getglobal(L, "minetest");
67 lua_getfield(L, -1, "luaentities");
68 luaL_checktype(L, -1, LUA_TTABLE);
69 lua_pushnumber(L, id); // Push id
70 lua_pushvalue(L, object); // Copy object to top of stack
76 void ScriptApiEntity::luaentity_Activate(u16 id,
77 const std::string &staticdata, u32 dtime_s)
79 SCRIPTAPI_PRECHECKHEADER
81 lua_pushcfunction(L, script_error_handler);
82 int errorhandler = lua_gettop(L);
84 verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
86 // Get minetest.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);
97 // Call with 3 arguments, 0 results
98 if(lua_pcall(L, 3, 0, errorhandler))
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 minetest.luaentities table
113 lua_getglobal(L, "minetest");
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, minetest
126 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
128 SCRIPTAPI_PRECHECKHEADER
130 lua_pushcfunction(L, script_error_handler);
131 int errorhandler = lua_gettop(L);
133 //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
135 // Get minetest.luaentities[id]
136 luaentity_get(L, id);
137 int object = lua_gettop(L);
139 // Get get_staticdata function
140 lua_getfield(L, -1, "get_staticdata");
144 luaL_checktype(L, -1, LUA_TFUNCTION);
145 lua_pushvalue(L, object); // self
146 // Call with 1 arguments, 1 results
147 if(lua_pcall(L, 1, 1, errorhandler))
149 lua_remove(L, object); // Remove object
150 lua_remove(L, errorhandler); // Remove error handler
153 const char *s = lua_tolstring(L, -1, &len);
154 return std::string(s, len);
157 void ScriptApiEntity::luaentity_GetProperties(u16 id,
158 ObjectProperties *prop)
160 SCRIPTAPI_PRECHECKHEADER
162 //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
164 // Get minetest.luaentities[id]
166 //int object = lua_gettop(L);
168 // Set default values that differ from ObjectProperties defaults
173 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
175 getboolfield(L, -1, "physical", prop->physical);
176 getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
178 getfloatfield(L, -1, "weight", prop->weight);
180 lua_getfield(L, -1, "collisionbox");
181 if(lua_istable(L, -1))
182 prop->collisionbox = read_aabb3f(L, -1, 1.0);
185 getstringfield(L, -1, "visual", prop->visual);
187 getstringfield(L, -1, "mesh", prop->mesh);
189 // Deprecated: read object properties directly
190 read_object_properties(L, -1, prop);
192 // Read initial_properties
193 lua_getfield(L, -1, "initial_properties");
194 read_object_properties(L, -1, prop);
198 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
200 SCRIPTAPI_PRECHECKHEADER
202 lua_pushcfunction(L, script_error_handler);
203 int errorhandler = lua_gettop(L);
205 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
207 // Get minetest.luaentities[id]
208 luaentity_get(L, id);
209 int object = lua_gettop(L);
210 // State: object is at top of stack
212 lua_getfield(L, -1, "on_step");
215 luaL_checktype(L, -1, LUA_TFUNCTION);
216 lua_pushvalue(L, object); // self
217 lua_pushnumber(L, dtime); // dtime
218 // Call with 2 arguments, 0 results
219 if(lua_pcall(L, 2, 0, errorhandler))
221 lua_remove(L, object); // Remove object
222 lua_remove(L, errorhandler); // Remove error handler
225 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
226 // tool_capabilities, direction)
227 void ScriptApiEntity::luaentity_Punch(u16 id,
228 ServerActiveObject *puncher, float time_from_last_punch,
229 const ToolCapabilities *toolcap, v3f dir)
231 SCRIPTAPI_PRECHECKHEADER
233 lua_pushcfunction(L, script_error_handler);
234 int errorhandler = lua_gettop(L);
236 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
238 // Get minetest.luaentities[id]
240 int object = lua_gettop(L);
241 // State: object is at top of stack
243 lua_getfield(L, -1, "on_punch");
246 luaL_checktype(L, -1, LUA_TFUNCTION);
247 lua_pushvalue(L, object); // self
248 objectrefGetOrCreate(puncher); // Clicker reference
249 lua_pushnumber(L, time_from_last_punch);
250 push_tool_capabilities(L, *toolcap);
252 // Call with 5 arguments, 0 results
253 if(lua_pcall(L, 5, 0, errorhandler))
255 lua_remove(L, object); // Remove object
256 lua_remove(L, errorhandler); // Remove error handler
259 // Calls entity:on_rightclick(ObjectRef clicker)
260 void ScriptApiEntity::luaentity_Rightclick(u16 id,
261 ServerActiveObject *clicker)
263 SCRIPTAPI_PRECHECKHEADER
265 lua_pushcfunction(L, script_error_handler);
266 int errorhandler = lua_gettop(L);
268 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
270 // Get minetest.luaentities[id]
272 int object = lua_gettop(L);
273 // State: object is at top of stack
275 lua_getfield(L, -1, "on_rightclick");
278 luaL_checktype(L, -1, LUA_TFUNCTION);
279 lua_pushvalue(L, object); // self
280 objectrefGetOrCreate(clicker); // Clicker reference
281 // Call with 2 arguments, 0 results
282 if(lua_pcall(L, 2, 0, errorhandler))
284 lua_remove(L, object); // Remove object
285 lua_remove(L, errorhandler); // Remove error handler