]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_entity.cpp
Pass a errfunc to lua_pcall to get a traceback
[minetest.git] / src / script / cpp_api / s_entity.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "cpp_api/s_entity.h"
21 #include "cpp_api/s_internal.h"
22 #include "log.h"
23 #include "object_properties.h"
24 #include "common/c_converter.h"
25 #include "common/c_content.h"
26
27 bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
28 {
29         SCRIPTAPI_PRECHECKHEADER
30
31         verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
32                         <<name<<"\""<<std::endl;
33
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);
39         lua_gettable(L, -2);
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;
44                 return false;
45         }
46         int prototype_table = lua_gettop(L);
47         //dump2(L, "prototype_table");
48
49         // Create entity object
50         lua_newtable(L);
51         int object = lua_gettop(L);
52
53         // Set object metatable
54         lua_pushvalue(L, prototype_table);
55         lua_setmetatable(L, -2);
56
57         // Add object reference
58         // This should be userdata with metatable ObjectRef
59         objectrefGet(id);
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");
64
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
71         lua_settable(L, -3);
72
73         return true;
74 }
75
76 void ScriptApiEntity::luaentity_Activate(u16 id,
77                 const std::string &staticdata, u32 dtime_s)
78 {
79         SCRIPTAPI_PRECHECKHEADER
80
81         lua_pushcfunction(L, script_error_handler);
82         int errorhandler = lua_gettop(L);
83
84         verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
85
86         // Get minetest.luaentities[id]
87         luaentity_get(L, id);
88         int object = lua_gettop(L);
89
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))
99                         scriptError();
100         } else {
101                 lua_pop(L, 1);
102         }
103         lua_pop(L, 2); // Pop object and error handler
104 }
105
106 void ScriptApiEntity::luaentity_Remove(u16 id)
107 {
108         SCRIPTAPI_PRECHECKHEADER
109
110         verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
111
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);
117
118         // Set luaentities[id] = nil
119         lua_pushnumber(L, id); // Push id
120         lua_pushnil(L);
121         lua_settable(L, objectstable);
122
123         lua_pop(L, 2); // pop luaentities, minetest
124 }
125
126 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
127 {
128         SCRIPTAPI_PRECHECKHEADER
129
130         lua_pushcfunction(L, script_error_handler);
131         int errorhandler = lua_gettop(L);
132
133         //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
134
135         // Get minetest.luaentities[id]
136         luaentity_get(L, id);
137         int object = lua_gettop(L);
138
139         // Get get_staticdata function
140         lua_getfield(L, -1, "get_staticdata");
141         if(lua_isnil(L, -1))
142                 return "";
143
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))
148                 scriptError();
149         lua_remove(L, object); // Remove object
150         lua_remove(L, errorhandler); // Remove error handler
151
152         size_t len = 0;
153         const char *s = lua_tolstring(L, -1, &len);
154         return std::string(s, len);
155 }
156
157 void ScriptApiEntity::luaentity_GetProperties(u16 id,
158                 ObjectProperties *prop)
159 {
160         SCRIPTAPI_PRECHECKHEADER
161
162         //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
163
164         // Get minetest.luaentities[id]
165         luaentity_get(L,id);
166         //int object = lua_gettop(L);
167
168         // Set default values that differ from ObjectProperties defaults
169         prop->hp_max = 10;
170
171         /* Read stuff */
172
173         prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
174
175         getboolfield(L, -1, "physical", prop->physical);
176         getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
177
178         getfloatfield(L, -1, "weight", prop->weight);
179
180         lua_getfield(L, -1, "collisionbox");
181         if(lua_istable(L, -1))
182                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
183         lua_pop(L, 1);
184
185         getstringfield(L, -1, "visual", prop->visual);
186
187         getstringfield(L, -1, "mesh", prop->mesh);
188
189         // Deprecated: read object properties directly
190         read_object_properties(L, -1, prop);
191
192         // Read initial_properties
193         lua_getfield(L, -1, "initial_properties");
194         read_object_properties(L, -1, prop);
195         lua_pop(L, 1);
196 }
197
198 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
199 {
200         SCRIPTAPI_PRECHECKHEADER
201
202         lua_pushcfunction(L, script_error_handler);
203         int errorhandler = lua_gettop(L);
204
205         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
206
207         // Get minetest.luaentities[id]
208         luaentity_get(L, id);
209         int object = lua_gettop(L);
210         // State: object is at top of stack
211         // Get step function
212         lua_getfield(L, -1, "on_step");
213         if(lua_isnil(L, -1))
214                 return;
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))
220                 scriptError();
221         lua_remove(L, object); // Remove object
222         lua_remove(L, errorhandler); // Remove error handler
223 }
224
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)
230 {
231         SCRIPTAPI_PRECHECKHEADER
232
233         lua_pushcfunction(L, script_error_handler);
234         int errorhandler = lua_gettop(L);
235
236         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
237
238         // Get minetest.luaentities[id]
239         luaentity_get(L,id);
240         int object = lua_gettop(L);
241         // State: object is at top of stack
242         // Get function
243         lua_getfield(L, -1, "on_punch");
244         if(lua_isnil(L, -1))
245                 return;
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);
251         push_v3f(L, dir);
252         // Call with 5 arguments, 0 results
253         if(lua_pcall(L, 5, 0, errorhandler))
254                 scriptError();
255         lua_remove(L, object); // Remove object
256         lua_remove(L, errorhandler); // Remove error handler
257 }
258
259 // Calls entity:on_rightclick(ObjectRef clicker)
260 void ScriptApiEntity::luaentity_Rightclick(u16 id,
261                 ServerActiveObject *clicker)
262 {
263         SCRIPTAPI_PRECHECKHEADER
264
265         lua_pushcfunction(L, script_error_handler);
266         int errorhandler = lua_gettop(L);
267
268         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
269
270         // Get minetest.luaentities[id]
271         luaentity_get(L,id);
272         int object = lua_gettop(L);
273         // State: object is at top of stack
274         // Get function
275         lua_getfield(L, -1, "on_rightclick");
276         if(lua_isnil(L, -1))
277                 return;
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))
283                 scriptError();
284         lua_remove(L, object); // Remove object
285         lua_remove(L, errorhandler); // Remove error handler
286 }
287