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"
22 #include "object_properties.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
30 bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
32 SCRIPTAPI_PRECHECKHEADER
34 verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
35 <<name<<"\""<<std::endl;
37 // Get minetest.registered_entities[name]
38 lua_getglobal(L, "minetest");
39 lua_getfield(L, -1, "registered_entities");
40 luaL_checktype(L, -1, LUA_TTABLE);
41 lua_pushstring(L, name);
43 // Should be a table, which we will use as a prototype
44 //luaL_checktype(L, -1, LUA_TTABLE);
45 if(lua_type(L, -1) != LUA_TTABLE){
46 errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
49 int prototype_table = lua_gettop(L);
50 //dump2(L, "prototype_table");
52 // Create entity object
54 int object = lua_gettop(L);
56 // Set object metatable
57 lua_pushvalue(L, prototype_table);
58 lua_setmetatable(L, -2);
60 // Add object reference
61 // This should be userdata with metatable ObjectRef
63 luaL_checktype(L, -1, LUA_TUSERDATA);
64 if(!luaL_checkudata(L, -1, "ObjectRef"))
65 luaL_typerror(L, -1, "ObjectRef");
66 lua_setfield(L, -2, "object");
68 // minetest.luaentities[id] = object
69 lua_getglobal(L, "minetest");
70 lua_getfield(L, -1, "luaentities");
71 luaL_checktype(L, -1, LUA_TTABLE);
72 lua_pushnumber(L, id); // Push id
73 lua_pushvalue(L, object); // Copy object to top of stack
79 void ScriptApiEntity::luaentity_Activate(u16 id,
80 const std::string &staticdata, u32 dtime_s)
82 SCRIPTAPI_PRECHECKHEADER
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_pushvalue(L, object);
92 lua_getfield(L, -1, "on_activate");
93 if(!lua_isnil(L, -1)){
94 luaL_checktype(L, -1, LUA_TFUNCTION);
95 lua_pushvalue(L, object); // self
96 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
97 lua_pushinteger(L, dtime_s);
98 // Call with 3 arguments, 0 results
99 if(lua_pcall(L, 3, 0, 0))
100 scriptError("error running function on_activate: %s\n",
101 lua_tostring(L, -1));
105 void ScriptApiEntity::luaentity_Remove(u16 id)
107 SCRIPTAPI_PRECHECKHEADER
109 verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
111 // Get minetest.luaentities table
112 lua_getglobal(L, "minetest");
113 lua_getfield(L, -1, "luaentities");
114 luaL_checktype(L, -1, LUA_TTABLE);
115 int objectstable = lua_gettop(L);
117 // Set luaentities[id] = nil
118 lua_pushnumber(L, id); // Push id
120 lua_settable(L, objectstable);
122 lua_pop(L, 2); // pop luaentities, minetest
125 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
127 SCRIPTAPI_PRECHECKHEADER
129 //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
131 // Get minetest.luaentities[id]
133 int object = lua_gettop(L);
135 // Get get_staticdata function
136 lua_pushvalue(L, object);
137 lua_getfield(L, -1, "get_staticdata");
141 luaL_checktype(L, -1, LUA_TFUNCTION);
142 lua_pushvalue(L, object); // self
143 // Call with 1 arguments, 1 results
144 if(lua_pcall(L, 1, 1, 0))
145 scriptError("error running function get_staticdata: %s\n",
146 lua_tostring(L, -1));
149 const char *s = lua_tolstring(L, -1, &len);
150 return std::string(s, len);
153 void ScriptApiEntity::luaentity_GetProperties(u16 id,
154 ObjectProperties *prop)
156 SCRIPTAPI_PRECHECKHEADER
158 //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
160 // Get minetest.luaentities[id]
162 //int object = lua_gettop(L);
164 // Set default values that differ from ObjectProperties defaults
169 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
171 getboolfield(L, -1, "physical", prop->physical);
173 getfloatfield(L, -1, "weight", prop->weight);
175 lua_getfield(L, -1, "collisionbox");
176 if(lua_istable(L, -1))
177 prop->collisionbox = read_aabb3f(L, -1, 1.0);
180 getstringfield(L, -1, "visual", prop->visual);
182 getstringfield(L, -1, "mesh", prop->mesh);
184 // Deprecated: read object properties directly
185 read_object_properties(L, -1, prop);
187 // Read initial_properties
188 lua_getfield(L, -1, "initial_properties");
189 read_object_properties(L, -1, prop);
193 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
195 SCRIPTAPI_PRECHECKHEADER
197 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
199 // Get minetest.luaentities[id]
201 int object = lua_gettop(L);
202 // State: object is at top of stack
204 lua_getfield(L, -1, "on_step");
207 luaL_checktype(L, -1, LUA_TFUNCTION);
208 lua_pushvalue(L, object); // self
209 lua_pushnumber(L, dtime); // dtime
210 // Call with 2 arguments, 0 results
211 if(lua_pcall(L, 2, 0, 0))
212 scriptError("error running function 'on_step': %s\n", lua_tostring(L, -1));
215 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
216 // tool_capabilities, direction)
217 void ScriptApiEntity::luaentity_Punch(u16 id,
218 ServerActiveObject *puncher, float time_from_last_punch,
219 const ToolCapabilities *toolcap, v3f dir)
221 SCRIPTAPI_PRECHECKHEADER
223 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
225 // Get minetest.luaentities[id]
227 int object = lua_gettop(L);
228 // State: object is at top of stack
230 lua_getfield(L, -1, "on_punch");
233 luaL_checktype(L, -1, LUA_TFUNCTION);
234 lua_pushvalue(L, object); // self
235 objectrefGetOrCreate(puncher); // Clicker reference
236 lua_pushnumber(L, time_from_last_punch);
237 push_tool_capabilities(L, *toolcap);
239 // Call with 5 arguments, 0 results
240 if(lua_pcall(L, 5, 0, 0))
241 scriptError("error running function 'on_punch': %s\n", lua_tostring(L, -1));
244 // Calls entity:on_rightclick(ObjectRef clicker)
245 void ScriptApiEntity::luaentity_Rightclick(u16 id,
246 ServerActiveObject *clicker)
248 SCRIPTAPI_PRECHECKHEADER
250 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
252 // Get minetest.luaentities[id]
254 int object = lua_gettop(L);
255 // State: object is at top of stack
257 lua_getfield(L, -1, "on_rightclick");
260 luaL_checktype(L, -1, LUA_TFUNCTION);
261 lua_pushvalue(L, object); // self
262 objectrefGetOrCreate(clicker); // Clicker reference
263 // Call with 2 arguments, 0 results
264 if(lua_pcall(L, 2, 0, 0))
265 scriptError("error running function 'on_rightclick': %s\n", lua_tostring(L, -1));