]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_entity.cpp
Move scriptapi to separate folder (by sapier)
[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 "log.h"
22 #include "object_properties.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
25
26 extern "C" {
27 #include "lauxlib.h"
28 }
29
30 bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
31 {
32         SCRIPTAPI_PRECHECKHEADER
33
34         verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
35                         <<name<<"\""<<std::endl;
36
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);
42         lua_gettable(L, -2);
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;
47                 return false;
48         }
49         int prototype_table = lua_gettop(L);
50         //dump2(L, "prototype_table");
51
52         // Create entity object
53         lua_newtable(L);
54         int object = lua_gettop(L);
55
56         // Set object metatable
57         lua_pushvalue(L, prototype_table);
58         lua_setmetatable(L, -2);
59
60         // Add object reference
61         // This should be userdata with metatable ObjectRef
62         objectrefGet(id);
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");
67
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
74         lua_settable(L, -3);
75
76         return true;
77 }
78
79 void ScriptApiEntity::luaentity_Activate(u16 id,
80                 const std::string &staticdata, u32 dtime_s)
81 {
82         SCRIPTAPI_PRECHECKHEADER
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_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));
102         }
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 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);
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, minetest
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         // Get minetest.luaentities[id]
132         luaentity_get(L,id);
133         int object = lua_gettop(L);
134
135         // Get get_staticdata function
136         lua_pushvalue(L, object);
137         lua_getfield(L, -1, "get_staticdata");
138         if(lua_isnil(L, -1))
139                 return "";
140
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));
147
148         size_t len=0;
149         const char *s = lua_tolstring(L, -1, &len);
150         return std::string(s, len);
151 }
152
153 void ScriptApiEntity::luaentity_GetProperties(u16 id,
154                 ObjectProperties *prop)
155 {
156         SCRIPTAPI_PRECHECKHEADER
157
158         //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
159
160         // Get minetest.luaentities[id]
161         luaentity_get(L,id);
162         //int object = lua_gettop(L);
163
164         // Set default values that differ from ObjectProperties defaults
165         prop->hp_max = 10;
166
167         /* Read stuff */
168
169         prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
170
171         getboolfield(L, -1, "physical", prop->physical);
172
173         getfloatfield(L, -1, "weight", prop->weight);
174
175         lua_getfield(L, -1, "collisionbox");
176         if(lua_istable(L, -1))
177                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
178         lua_pop(L, 1);
179
180         getstringfield(L, -1, "visual", prop->visual);
181
182         getstringfield(L, -1, "mesh", prop->mesh);
183
184         // Deprecated: read object properties directly
185         read_object_properties(L, -1, prop);
186
187         // Read initial_properties
188         lua_getfield(L, -1, "initial_properties");
189         read_object_properties(L, -1, prop);
190         lua_pop(L, 1);
191 }
192
193 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
194 {
195         SCRIPTAPI_PRECHECKHEADER
196
197         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
198
199         // Get minetest.luaentities[id]
200         luaentity_get(L,id);
201         int object = lua_gettop(L);
202         // State: object is at top of stack
203         // Get step function
204         lua_getfield(L, -1, "on_step");
205         if(lua_isnil(L, -1))
206                 return;
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));
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 minetest.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                 return;
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);
238         push_v3f(L, dir);
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));
242 }
243
244 // Calls entity:on_rightclick(ObjectRef clicker)
245 void ScriptApiEntity::luaentity_Rightclick(u16 id,
246                 ServerActiveObject *clicker)
247 {
248         SCRIPTAPI_PRECHECKHEADER
249
250         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
251
252         // Get minetest.luaentities[id]
253         luaentity_get(L,id);
254         int object = lua_gettop(L);
255         // State: object is at top of stack
256         // Get function
257         lua_getfield(L, -1, "on_rightclick");
258         if(lua_isnil(L, -1))
259                 return;
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));
266 }
267