3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "cpp_api/s_entity.h"
21 #include "cpp_api/s_internal.h"
23 #include "object_properties.h"
24 #include "common/c_converter.h"
25 #include "common/c_content.h"
27 bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
29 SCRIPTAPI_PRECHECKHEADER
31 verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
32 <<name<<"\""<<std::endl;
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);
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;
46 int prototype_table = lua_gettop(L);
47 //dump2(L, "prototype_table");
49 // Create entity object
51 int object = lua_gettop(L);
53 // Set object metatable
54 lua_pushvalue(L, prototype_table);
55 lua_setmetatable(L, -2);
57 // Add object reference
58 // This should be userdata with metatable ObjectRef
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");
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
76 void ScriptApiEntity::luaentity_Activate(u16 id,
77 const std::string &staticdata, u32 dtime_s)
79 SCRIPTAPI_PRECHECKHEADER
81 verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
83 // Get core.luaentities[id]
85 int object = lua_gettop(L);
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);
95 setOriginFromTable(object);
96 PCALL_RES(lua_pcall(L, 3, 0, m_errorhandler));
100 lua_pop(L, 1); // Pop object
103 void ScriptApiEntity::luaentity_Remove(u16 id)
105 SCRIPTAPI_PRECHECKHEADER
107 verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
109 // Get core.luaentities table
110 lua_getglobal(L, "core");
111 lua_getfield(L, -1, "luaentities");
112 luaL_checktype(L, -1, LUA_TTABLE);
113 int objectstable = lua_gettop(L);
115 // Set luaentities[id] = nil
116 lua_pushnumber(L, id); // Push id
118 lua_settable(L, objectstable);
120 lua_pop(L, 2); // pop luaentities, core
123 std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
125 SCRIPTAPI_PRECHECKHEADER
127 //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
129 // Get core.luaentities[id]
130 luaentity_get(L, id);
131 int object = lua_gettop(L);
133 // Get get_staticdata function
134 lua_getfield(L, -1, "get_staticdata");
135 if (lua_isnil(L, -1)) {
136 lua_pop(L, 2); // Pop entity and get_staticdata
139 luaL_checktype(L, -1, LUA_TFUNCTION);
140 lua_pushvalue(L, object); // self
142 setOriginFromTable(object);
143 PCALL_RES(lua_pcall(L, 1, 1, m_errorhandler));
145 lua_remove(L, object); // Remove object
148 const char *s = lua_tolstring(L, -1, &len);
149 lua_pop(L, 1); // Pop static data
150 return std::string(s, len);
153 void ScriptApiEntity::luaentity_GetProperties(u16 id,
154 ObjectProperties *prop)
156 SCRIPTAPI_PRECHECKHEADER
158 //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
160 // Get core.luaentities[id]
161 luaentity_get(L, id);
163 // Set default values that differ from ObjectProperties defaults
168 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
170 getboolfield(L, -1, "physical", prop->physical);
171 getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
173 getfloatfield(L, -1, "weight", prop->weight);
175 lua_getfield(L, -1, "collisionbox");
176 if (lua_istable(L, -1))
177 prop->collisionbox = read_aabb3f(L, -1, 1.0);
180 getstringfield(L, -1, "visual", prop->visual);
182 getstringfield(L, -1, "mesh", prop->mesh);
184 // Deprecated: read object properties directly
185 read_object_properties(L, -1, prop);
187 // Read initial_properties
188 lua_getfield(L, -1, "initial_properties");
189 read_object_properties(L, -1, prop);
193 void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
195 SCRIPTAPI_PRECHECKHEADER
197 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
199 // Get core.luaentities[id]
200 luaentity_get(L, id);
201 int object = lua_gettop(L);
202 // State: object is at top of stack
204 lua_getfield(L, -1, "on_step");
205 if (lua_isnil(L, -1)) {
206 lua_pop(L, 2); // Pop on_step and entity
209 luaL_checktype(L, -1, LUA_TFUNCTION);
210 lua_pushvalue(L, object); // self
211 lua_pushnumber(L, dtime); // dtime
213 setOriginFromTable(object);
214 PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
216 lua_pop(L, 1); // Pop object
219 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
220 // tool_capabilities, direction)
221 void ScriptApiEntity::luaentity_Punch(u16 id,
222 ServerActiveObject *puncher, float time_from_last_punch,
223 const ToolCapabilities *toolcap, v3f dir)
225 SCRIPTAPI_PRECHECKHEADER
227 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
229 // Get core.luaentities[id]
231 int object = lua_gettop(L);
232 // State: object is at top of stack
234 lua_getfield(L, -1, "on_punch");
235 if (lua_isnil(L, -1)) {
236 lua_pop(L, 2); // Pop on_punch and entitu
239 luaL_checktype(L, -1, LUA_TFUNCTION);
240 lua_pushvalue(L, object); // self
241 objectrefGetOrCreate(L, puncher); // Clicker reference
242 lua_pushnumber(L, time_from_last_punch);
243 push_tool_capabilities(L, *toolcap);
246 setOriginFromTable(object);
247 PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
249 lua_pop(L, 1); // Pop object
252 // Calls entity:on_rightclick(ObjectRef clicker)
253 void ScriptApiEntity::luaentity_Rightclick(u16 id,
254 ServerActiveObject *clicker)
256 SCRIPTAPI_PRECHECKHEADER
258 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
260 // Get core.luaentities[id]
261 luaentity_get(L, id);
262 int object = lua_gettop(L);
263 // State: object is at top of stack
265 lua_getfield(L, -1, "on_rightclick");
266 if (lua_isnil(L, -1)) {
267 lua_pop(L, 2); // Pop on_rightclick and entity
270 luaL_checktype(L, -1, LUA_TFUNCTION);
271 lua_pushvalue(L, object); // self
272 objectrefGetOrCreate(L, clicker); // Clicker reference
274 setOriginFromTable(object);
275 PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
277 lua_pop(L, 1); // Pop object