]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_entity.cpp
Add an option to disable object <-> object collision for Lua entities
[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 "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         getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
173
174         getfloatfield(L, -1, "weight", prop->weight);
175
176         lua_getfield(L, -1, "collisionbox");
177         if(lua_istable(L, -1))
178                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
179         lua_pop(L, 1);
180
181         getstringfield(L, -1, "visual", prop->visual);
182
183         getstringfield(L, -1, "mesh", prop->mesh);
184
185         // Deprecated: read object properties directly
186         read_object_properties(L, -1, prop);
187
188         // Read initial_properties
189         lua_getfield(L, -1, "initial_properties");
190         read_object_properties(L, -1, prop);
191         lua_pop(L, 1);
192 }
193
194 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
195 {
196         SCRIPTAPI_PRECHECKHEADER
197
198         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
199
200         // Get minetest.luaentities[id]
201         luaentity_get(L,id);
202         int object = lua_gettop(L);
203         // State: object is at top of stack
204         // Get step function
205         lua_getfield(L, -1, "on_step");
206         if(lua_isnil(L, -1))
207                 return;
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));
214 }
215
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)
221 {
222         SCRIPTAPI_PRECHECKHEADER
223
224         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
225
226         // Get minetest.luaentities[id]
227         luaentity_get(L,id);
228         int object = lua_gettop(L);
229         // State: object is at top of stack
230         // Get function
231         lua_getfield(L, -1, "on_punch");
232         if(lua_isnil(L, -1))
233                 return;
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);
239         push_v3f(L, dir);
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));
243 }
244
245 // Calls entity:on_rightclick(ObjectRef clicker)
246 void ScriptApiEntity::luaentity_Rightclick(u16 id,
247                 ServerActiveObject *clicker)
248 {
249         SCRIPTAPI_PRECHECKHEADER
250
251         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
252
253         // Get minetest.luaentities[id]
254         luaentity_get(L,id);
255         int object = lua_gettop(L);
256         // State: object is at top of stack
257         // Get function
258         lua_getfield(L, -1, "on_rightclick");
259         if(lua_isnil(L, -1))
260                 return;
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));
267 }
268