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 verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
83 // Get minetest.luaentities[id]
85 int object = lua_gettop(L);
87 // Get on_activate function
88 lua_pushvalue(L, object);
89 lua_getfield(L, -1, "on_activate");
90 if(!lua_isnil(L, -1)){
91 luaL_checktype(L, -1, LUA_TFUNCTION);
92 lua_pushvalue(L, object); // self
93 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
94 lua_pushinteger(L, dtime_s);
95 // Call with 3 arguments, 0 results
96 if(lua_pcall(L, 3, 0, 0))
97 scriptError("error running function on_activate: %s\n",
102 void ScriptApiEntity::luaentity_Remove(u16 id)
104 SCRIPTAPI_PRECHECKHEADER
106 verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
108 // Get minetest.luaentities table
109 lua_getglobal(L, "minetest");
110 lua_getfield(L, -1, "luaentities");
111 luaL_checktype(L, -1, LUA_TTABLE);
112 int objectstable = lua_gettop(L);
114 // Set luaentities[id] = nil
115 lua_pushnumber(L, id); // Push id
117 lua_settable(L, objectstable);
119 lua_pop(L, 2); // pop luaentities, minetest
122 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
124 SCRIPTAPI_PRECHECKHEADER
126 //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
128 // Get minetest.luaentities[id]
130 int object = lua_gettop(L);
132 // Get get_staticdata function
133 lua_pushvalue(L, object);
134 lua_getfield(L, -1, "get_staticdata");
138 luaL_checktype(L, -1, LUA_TFUNCTION);
139 lua_pushvalue(L, object); // self
140 // Call with 1 arguments, 1 results
141 if(lua_pcall(L, 1, 1, 0))
142 scriptError("error running function get_staticdata: %s\n",
143 lua_tostring(L, -1));
146 const char *s = lua_tolstring(L, -1, &len);
147 return std::string(s, len);
150 void ScriptApiEntity::luaentity_GetProperties(u16 id,
151 ObjectProperties *prop)
153 SCRIPTAPI_PRECHECKHEADER
155 //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
157 // Get minetest.luaentities[id]
159 //int object = lua_gettop(L);
161 // Set default values that differ from ObjectProperties defaults
166 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
168 getboolfield(L, -1, "physical", prop->physical);
169 getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
171 getfloatfield(L, -1, "weight", prop->weight);
173 lua_getfield(L, -1, "collisionbox");
174 if(lua_istable(L, -1))
175 prop->collisionbox = read_aabb3f(L, -1, 1.0);
178 getstringfield(L, -1, "visual", prop->visual);
180 getstringfield(L, -1, "mesh", prop->mesh);
182 // Deprecated: read object properties directly
183 read_object_properties(L, -1, prop);
185 // Read initial_properties
186 lua_getfield(L, -1, "initial_properties");
187 read_object_properties(L, -1, prop);
191 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
193 SCRIPTAPI_PRECHECKHEADER
195 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
197 // Get minetest.luaentities[id]
199 int object = lua_gettop(L);
200 // State: object is at top of stack
202 lua_getfield(L, -1, "on_step");
205 luaL_checktype(L, -1, LUA_TFUNCTION);
206 lua_pushvalue(L, object); // self
207 lua_pushnumber(L, dtime); // dtime
208 // Call with 2 arguments, 0 results
209 if(lua_pcall(L, 2, 0, 0))
210 scriptError("error running function 'on_step': %s\n", lua_tostring(L, -1));
213 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
214 // tool_capabilities, direction)
215 void ScriptApiEntity::luaentity_Punch(u16 id,
216 ServerActiveObject *puncher, float time_from_last_punch,
217 const ToolCapabilities *toolcap, v3f dir)
219 SCRIPTAPI_PRECHECKHEADER
221 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
223 // Get minetest.luaentities[id]
225 int object = lua_gettop(L);
226 // State: object is at top of stack
228 lua_getfield(L, -1, "on_punch");
231 luaL_checktype(L, -1, LUA_TFUNCTION);
232 lua_pushvalue(L, object); // self
233 objectrefGetOrCreate(puncher); // Clicker reference
234 lua_pushnumber(L, time_from_last_punch);
235 push_tool_capabilities(L, *toolcap);
237 // Call with 5 arguments, 0 results
238 if(lua_pcall(L, 5, 0, 0))
239 scriptError("error running function 'on_punch': %s\n", lua_tostring(L, -1));
242 // Calls entity:on_rightclick(ObjectRef clicker)
243 void ScriptApiEntity::luaentity_Rightclick(u16 id,
244 ServerActiveObject *clicker)
246 SCRIPTAPI_PRECHECKHEADER
248 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
250 // Get minetest.luaentities[id]
252 int object = lua_gettop(L);
253 // State: object is at top of stack
255 lua_getfield(L, -1, "on_rightclick");
258 luaL_checktype(L, -1, LUA_TFUNCTION);
259 lua_pushvalue(L, object); // self
260 objectrefGetOrCreate(clicker); // Clicker reference
261 // Call with 2 arguments, 0 results
262 if(lua_pcall(L, 2, 0, 0))
263 scriptError("error running function 'on_rightclick': %s\n", lua_tostring(L, -1));