]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_entity.cpp
Tell on_punch to expect a return value
[dragonfireclient.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 core.registered_entities[name]
35         lua_getglobal(L, "core");
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(L, 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         // core.luaentities[id] = object
66         lua_getglobal(L, "core");
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         verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
82
83         int error_handler = PUSH_ERROR_HANDLER(L);
84
85         // Get core.luaentities[id]
86         luaentity_get(L, id);
87         int object = lua_gettop(L);
88
89         // Get on_activate function
90         lua_getfield(L, -1, "on_activate");
91         if (!lua_isnil(L, -1)) {
92                 luaL_checktype(L, -1, LUA_TFUNCTION);
93                 lua_pushvalue(L, object); // self
94                 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
95                 lua_pushinteger(L, dtime_s);
96
97                 setOriginFromTable(object);
98                 PCALL_RES(lua_pcall(L, 3, 0, error_handler));
99         } else {
100                 lua_pop(L, 1);
101         }
102         lua_pop(L, 2); // Pop object and error handler
103 }
104
105 void ScriptApiEntity::luaentity_Remove(u16 id)
106 {
107         SCRIPTAPI_PRECHECKHEADER
108
109         verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
110
111         // Get core.luaentities table
112         lua_getglobal(L, "core");
113         lua_getfield(L, -1, "luaentities");
114         luaL_checktype(L, -1, LUA_TTABLE);
115         int objectstable = lua_gettop(L);
116
117         // Set luaentities[id] = nil
118         lua_pushnumber(L, id); // Push id
119         lua_pushnil(L);
120         lua_settable(L, objectstable);
121
122         lua_pop(L, 2); // pop luaentities, core
123 }
124
125 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
126 {
127         SCRIPTAPI_PRECHECKHEADER
128
129         //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
130
131         int error_handler = PUSH_ERROR_HANDLER(L);
132
133         // Get core.luaentities[id]
134         luaentity_get(L, id);
135         int object = lua_gettop(L);
136
137         // Get get_staticdata function
138         lua_getfield(L, -1, "get_staticdata");
139         if (lua_isnil(L, -1)) {
140                 lua_pop(L, 2); // Pop entity and  get_staticdata
141                 return "";
142         }
143         luaL_checktype(L, -1, LUA_TFUNCTION);
144         lua_pushvalue(L, object); // self
145
146         setOriginFromTable(object);
147         PCALL_RES(lua_pcall(L, 1, 1, error_handler));
148
149         lua_remove(L, object);
150         lua_remove(L, error_handler);
151
152         size_t len = 0;
153         const char *s = lua_tolstring(L, -1, &len);
154         lua_pop(L, 1); // Pop static data
155         return std::string(s, len);
156 }
157
158 void ScriptApiEntity::luaentity_GetProperties(u16 id,
159                 ObjectProperties *prop)
160 {
161         SCRIPTAPI_PRECHECKHEADER
162
163         //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
164
165         // Get core.luaentities[id]
166         luaentity_get(L, id);
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         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
203
204         int error_handler = PUSH_ERROR_HANDLER(L);
205
206         // Get core.luaentities[id]
207         luaentity_get(L, id);
208         int object = lua_gettop(L);
209         // State: object is at top of stack
210         // Get step function
211         lua_getfield(L, -1, "on_step");
212         if (lua_isnil(L, -1)) {
213                 lua_pop(L, 2); // Pop on_step and entity
214                 return;
215         }
216         luaL_checktype(L, -1, LUA_TFUNCTION);
217         lua_pushvalue(L, object); // self
218         lua_pushnumber(L, dtime); // dtime
219
220         setOriginFromTable(object);
221         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
222
223         lua_pop(L, 2); // Pop object and error handler
224 }
225
226 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
227 //                       tool_capabilities, direction, damage)
228 bool ScriptApiEntity::luaentity_Punch(u16 id,
229                 ServerActiveObject *puncher, float time_from_last_punch,
230                 const ToolCapabilities *toolcap, v3f dir, s16 damage)
231 {
232         SCRIPTAPI_PRECHECKHEADER
233
234         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
235
236         int error_handler = PUSH_ERROR_HANDLER(L);
237
238         // Get core.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                 lua_pop(L, 2); // Pop on_punch and entity
246                 return false;
247         }
248         luaL_checktype(L, -1, LUA_TFUNCTION);
249         lua_pushvalue(L, object);  // self
250         objectrefGetOrCreate(L, puncher);  // Clicker reference
251         lua_pushnumber(L, time_from_last_punch);
252         push_tool_capabilities(L, *toolcap);
253         push_v3f(L, dir);
254         lua_pushnumber(L, damage);
255
256         setOriginFromTable(object);
257         PCALL_RES(lua_pcall(L, 6, 1, error_handler));
258
259         bool retval = lua_toboolean(L, -1);
260         lua_pop(L, 2); // Pop object and error handler
261         return retval;
262 }
263
264 // Calls entity:on_rightclick(ObjectRef clicker)
265 void ScriptApiEntity::luaentity_Rightclick(u16 id,
266                 ServerActiveObject *clicker)
267 {
268         SCRIPTAPI_PRECHECKHEADER
269
270         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
271
272         int error_handler = PUSH_ERROR_HANDLER(L);
273
274         // Get core.luaentities[id]
275         luaentity_get(L, id);
276         int object = lua_gettop(L);
277         // State: object is at top of stack
278         // Get function
279         lua_getfield(L, -1, "on_rightclick");
280         if (lua_isnil(L, -1)) {
281                 lua_pop(L, 2); // Pop on_rightclick and entity
282                 return;
283         }
284         luaL_checktype(L, -1, LUA_TFUNCTION);
285         lua_pushvalue(L, object); // self
286         objectrefGetOrCreate(L, clicker); // Clicker reference
287
288         setOriginFromTable(object);
289         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
290
291         lua_pop(L, 2); // Pop object and error handler
292 }
293