]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_entity.cpp
Improve Script CPP API diagnostics
[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 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         // Get core.luaentities[id]
84         luaentity_get(L, id);
85         int object = lua_gettop(L);
86
87         // Get on_activate function
88         lua_getfield(L, -1, "on_activate");
89         if (!lua_isnil(L, -1)) {
90                 luaL_checktype(L, -1, LUA_TFUNCTION);
91                 lua_pushvalue(L, object); // self
92                 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
93                 lua_pushinteger(L, dtime_s);
94                 // Call with 3 arguments, 0 results
95                 PCALL_RES(lua_pcall(L, 3, 0, m_errorhandler));
96         } else {
97                 lua_pop(L, 1);
98         }
99         lua_pop(L, 1); // Pop object
100 }
101
102 void ScriptApiEntity::luaentity_Remove(u16 id)
103 {
104         SCRIPTAPI_PRECHECKHEADER
105
106         verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
107
108         // Get core.luaentities table
109         lua_getglobal(L, "core");
110         lua_getfield(L, -1, "luaentities");
111         luaL_checktype(L, -1, LUA_TTABLE);
112         int objectstable = lua_gettop(L);
113
114         // Set luaentities[id] = nil
115         lua_pushnumber(L, id); // Push id
116         lua_pushnil(L);
117         lua_settable(L, objectstable);
118
119         lua_pop(L, 2); // pop luaentities, core
120 }
121
122 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
123 {
124         SCRIPTAPI_PRECHECKHEADER
125
126         //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
127
128         // Get core.luaentities[id]
129         luaentity_get(L, id);
130         int object = lua_gettop(L);
131
132         // Get get_staticdata function
133         lua_getfield(L, -1, "get_staticdata");
134         if (lua_isnil(L, -1)) {
135                 lua_pop(L, 2); // Pop entity and  get_staticdata
136                 return "";
137         }
138
139         luaL_checktype(L, -1, LUA_TFUNCTION);
140         lua_pushvalue(L, object); // self
141         // Call with 1 arguments, 1 results
142         PCALL_RES(lua_pcall(L, 1, 1, m_errorhandler));
143         lua_remove(L, object); // Remove object
144
145         size_t len = 0;
146         const char *s = lua_tolstring(L, -1, &len);
147         lua_pop(L, 1); // Pop static data
148         return std::string(s, len);
149 }
150
151 void ScriptApiEntity::luaentity_GetProperties(u16 id,
152                 ObjectProperties *prop)
153 {
154         SCRIPTAPI_PRECHECKHEADER
155
156         //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
157
158         // Get core.luaentities[id]
159         luaentity_get(L, id);
160
161         // Set default values that differ from ObjectProperties defaults
162         prop->hp_max = 10;
163
164         /* Read stuff */
165
166         prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
167
168         getboolfield(L, -1, "physical", prop->physical);
169         getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
170
171         getfloatfield(L, -1, "weight", prop->weight);
172
173         lua_getfield(L, -1, "collisionbox");
174         if (lua_istable(L, -1))
175                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
176         lua_pop(L, 1);
177
178         getstringfield(L, -1, "visual", prop->visual);
179
180         getstringfield(L, -1, "mesh", prop->mesh);
181
182         // Deprecated: read object properties directly
183         read_object_properties(L, -1, prop);
184
185         // Read initial_properties
186         lua_getfield(L, -1, "initial_properties");
187         read_object_properties(L, -1, prop);
188         lua_pop(L, 1);
189 }
190
191 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
192 {
193         SCRIPTAPI_PRECHECKHEADER
194
195         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
196
197         // Get core.luaentities[id]
198         luaentity_get(L, id);
199         int object = lua_gettop(L);
200         // State: object is at top of stack
201         // Get step function
202         lua_getfield(L, -1, "on_step");
203         if (lua_isnil(L, -1)) {
204                 lua_pop(L, 2); // Pop on_step and entity
205                 return;
206         }
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         PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
212         lua_pop(L, 1); // Pop object
213 }
214
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)
220 {
221         SCRIPTAPI_PRECHECKHEADER
222
223         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
224
225         // Get core.luaentities[id]
226         luaentity_get(L,id);
227         int object = lua_gettop(L);
228         // State: object is at top of stack
229         // Get function
230         lua_getfield(L, -1, "on_punch");
231         if (lua_isnil(L, -1)) {
232                 lua_pop(L, 2); // Pop on_punch and entitu
233                 return;
234         }
235         luaL_checktype(L, -1, LUA_TFUNCTION);
236         lua_pushvalue(L, object);  // self
237         objectrefGetOrCreate(L, puncher);  // Clicker reference
238         lua_pushnumber(L, time_from_last_punch);
239         push_tool_capabilities(L, *toolcap);
240         push_v3f(L, dir);
241         // Call with 5 arguments, 0 results
242         PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
243         lua_pop(L, 1); // Pop object
244 }
245
246 // Calls entity:on_rightclick(ObjectRef clicker)
247 void ScriptApiEntity::luaentity_Rightclick(u16 id,
248                 ServerActiveObject *clicker)
249 {
250         SCRIPTAPI_PRECHECKHEADER
251
252         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
253
254         // Get core.luaentities[id]
255         luaentity_get(L, id);
256         int object = lua_gettop(L);
257         // State: object is at top of stack
258         // Get function
259         lua_getfield(L, -1, "on_rightclick");
260         if (lua_isnil(L, -1)) {
261                 lua_pop(L, 2); // Pop on_rightclick and entity
262                 return;
263         }
264         luaL_checktype(L, -1, LUA_TFUNCTION);
265         lua_pushvalue(L, object); // self
266         objectrefGetOrCreate(L, clicker); // Clicker reference
267         // Call with 2 arguments, 0 results
268         PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
269         lua_pop(L, 1); // Pop object
270 }
271