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);
172 getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
174 getfloatfield(L, -1, "weight", prop->weight);
176 lua_getfield(L, -1, "collisionbox");
177 if(lua_istable(L, -1))
178 prop->collisionbox = read_aabb3f(L, -1, 1.0);
181 getstringfield(L, -1, "visual", prop->visual);
183 getstringfield(L, -1, "mesh", prop->mesh);
185 // Deprecated: read object properties directly
186 read_object_properties(L, -1, prop);
188 // Read initial_properties
189 lua_getfield(L, -1, "initial_properties");
190 read_object_properties(L, -1, prop);
194 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
196 SCRIPTAPI_PRECHECKHEADER
198 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
200 // Get minetest.luaentities[id]
202 int object = lua_gettop(L);
203 // State: object is at top of stack
205 lua_getfield(L, -1, "on_step");
208 luaL_checktype(L, -1, LUA_TFUNCTION);
209 lua_pushvalue(L, object); // self
210 lua_pushnumber(L, dtime); // dtime
211 // Call with 2 arguments, 0 results
212 if(lua_pcall(L, 2, 0, 0))
213 scriptError("error running function 'on_step': %s\n", lua_tostring(L, -1));
216 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
217 // tool_capabilities, direction)
218 void ScriptApiEntity::luaentity_Punch(u16 id,
219 ServerActiveObject *puncher, float time_from_last_punch,
220 const ToolCapabilities *toolcap, v3f dir)
222 SCRIPTAPI_PRECHECKHEADER
224 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
226 // Get minetest.luaentities[id]
228 int object = lua_gettop(L);
229 // State: object is at top of stack
231 lua_getfield(L, -1, "on_punch");
234 luaL_checktype(L, -1, LUA_TFUNCTION);
235 lua_pushvalue(L, object); // self
236 objectrefGetOrCreate(puncher); // Clicker reference
237 lua_pushnumber(L, time_from_last_punch);
238 push_tool_capabilities(L, *toolcap);
240 // Call with 5 arguments, 0 results
241 if(lua_pcall(L, 5, 0, 0))
242 scriptError("error running function 'on_punch': %s\n", lua_tostring(L, -1));
245 // Calls entity:on_rightclick(ObjectRef clicker)
246 void ScriptApiEntity::luaentity_Rightclick(u16 id,
247 ServerActiveObject *clicker)
249 SCRIPTAPI_PRECHECKHEADER
251 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
253 // Get minetest.luaentities[id]
255 int object = lua_gettop(L);
256 // State: object is at top of stack
258 lua_getfield(L, -1, "on_rightclick");
261 luaL_checktype(L, -1, LUA_TFUNCTION);
262 lua_pushvalue(L, object); // self
263 objectrefGetOrCreate(clicker); // Clicker reference
264 // Call with 2 arguments, 0 results
265 if(lua_pcall(L, 2, 0, 0))
266 scriptError("error running function 'on_rightclick': %s\n", lua_tostring(L, -1));