s_entity.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "cpp_api/s_entity.h"
  17. #include "cpp_api/s_internal.h"
  18. #include "log.h"
  19. #include "object_properties.h"
  20. #include "common/c_converter.h"
  21. #include "common/c_content.h"
  22. #include "server.h"
  23. bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
  24. {
  25. SCRIPTAPI_PRECHECKHEADER
  26. verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
  27. <<name<<"\""<<std::endl;
  28. // Get core.registered_entities[name]
  29. lua_getglobal(L, "core");
  30. lua_getfield(L, -1, "registered_entities");
  31. luaL_checktype(L, -1, LUA_TTABLE);
  32. lua_pushstring(L, name);
  33. lua_gettable(L, -2);
  34. // Should be a table, which we will use as a prototype
  35. //luaL_checktype(L, -1, LUA_TTABLE);
  36. if (lua_type(L, -1) != LUA_TTABLE){
  37. errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
  38. return false;
  39. }
  40. int prototype_table = lua_gettop(L);
  41. //dump2(L, "prototype_table");
  42. // Create entity object
  43. lua_newtable(L);
  44. int object = lua_gettop(L);
  45. // Set object metatable
  46. lua_pushvalue(L, prototype_table);
  47. lua_setmetatable(L, -2);
  48. // Add object reference
  49. // This should be userdata with metatable ObjectRef
  50. push_objectRef(L, id);
  51. luaL_checktype(L, -1, LUA_TUSERDATA);
  52. if (!luaL_checkudata(L, -1, "ObjectRef"))
  53. luaL_typerror(L, -1, "ObjectRef");
  54. lua_setfield(L, -2, "object");
  55. // core.luaentities[id] = object
  56. lua_getglobal(L, "core");
  57. lua_getfield(L, -1, "luaentities");
  58. luaL_checktype(L, -1, LUA_TTABLE);
  59. lua_pushnumber(L, id); // Push id
  60. lua_pushvalue(L, object); // Copy object to top of stack
  61. lua_settable(L, -3);
  62. return true;
  63. }
  64. void ScriptApiEntity::luaentity_Activate(u16 id,
  65. const std::string &staticdata, u32 dtime_s)
  66. {
  67. SCRIPTAPI_PRECHECKHEADER
  68. verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
  69. int error_handler = PUSH_ERROR_HANDLER(L);
  70. // Get core.luaentities[id]
  71. luaentity_get(L, id);
  72. int object = lua_gettop(L);
  73. // Get on_activate function
  74. lua_getfield(L, -1, "on_activate");
  75. if (!lua_isnil(L, -1)) {
  76. luaL_checktype(L, -1, LUA_TFUNCTION);
  77. lua_pushvalue(L, object); // self
  78. lua_pushlstring(L, staticdata.c_str(), staticdata.size());
  79. lua_pushinteger(L, dtime_s);
  80. setOriginFromTable(object);
  81. PCALL_RES(lua_pcall(L, 3, 0, error_handler));
  82. } else {
  83. lua_pop(L, 1);
  84. }
  85. lua_pop(L, 2); // Pop object and error handler
  86. }
  87. void ScriptApiEntity::luaentity_Remove(u16 id)
  88. {
  89. SCRIPTAPI_PRECHECKHEADER
  90. verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
  91. // Get core.luaentities table
  92. lua_getglobal(L, "core");
  93. lua_getfield(L, -1, "luaentities");
  94. luaL_checktype(L, -1, LUA_TTABLE);
  95. int objectstable = lua_gettop(L);
  96. // Set luaentities[id] = nil
  97. lua_pushnumber(L, id); // Push id
  98. lua_pushnil(L);
  99. lua_settable(L, objectstable);
  100. lua_pop(L, 2); // pop luaentities, core
  101. }
  102. std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
  103. {
  104. SCRIPTAPI_PRECHECKHEADER
  105. //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
  106. int error_handler = PUSH_ERROR_HANDLER(L);
  107. // Get core.luaentities[id]
  108. luaentity_get(L, id);
  109. int object = lua_gettop(L);
  110. // Get get_staticdata function
  111. lua_getfield(L, -1, "get_staticdata");
  112. if (lua_isnil(L, -1)) {
  113. lua_pop(L, 2); // Pop entity and get_staticdata
  114. return "";
  115. }
  116. luaL_checktype(L, -1, LUA_TFUNCTION);
  117. lua_pushvalue(L, object); // self
  118. setOriginFromTable(object);
  119. PCALL_RES(lua_pcall(L, 1, 1, error_handler));
  120. lua_remove(L, object);
  121. lua_remove(L, error_handler);
  122. size_t len = 0;
  123. const char *s = lua_tolstring(L, -1, &len);
  124. lua_pop(L, 1); // Pop static data
  125. return std::string(s, len);
  126. }
  127. void ScriptApiEntity::luaentity_GetProperties(u16 id,
  128. ObjectProperties *prop)
  129. {
  130. SCRIPTAPI_PRECHECKHEADER
  131. //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
  132. // Get core.luaentities[id]
  133. luaentity_get(L, id);
  134. // Set default values that differ from ObjectProperties defaults
  135. prop->hp_max = 10;
  136. /* Read stuff */
  137. prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
  138. getboolfield(L, -1, "physical", prop->physical);
  139. getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
  140. getfloatfield(L, -1, "weight", prop->weight);
  141. lua_getfield(L, -1, "collisionbox");
  142. if (lua_istable(L, -1))
  143. prop->collisionbox = read_aabb3f(L, -1, 1.0);
  144. lua_pop(L, 1);
  145. getstringfield(L, -1, "visual", prop->visual);
  146. getstringfield(L, -1, "mesh", prop->mesh);
  147. // Deprecated: read object properties directly
  148. read_object_properties(L, -1, prop, getServer()->idef());
  149. // Read initial_properties
  150. lua_getfield(L, -1, "initial_properties");
  151. read_object_properties(L, -1, prop, getServer()->idef());
  152. lua_pop(L, 1);
  153. }
  154. void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
  155. {
  156. SCRIPTAPI_PRECHECKHEADER
  157. //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
  158. int error_handler = PUSH_ERROR_HANDLER(L);
  159. // Get core.luaentities[id]
  160. luaentity_get(L, id);
  161. int object = lua_gettop(L);
  162. // State: object is at top of stack
  163. // Get step function
  164. lua_getfield(L, -1, "on_step");
  165. if (lua_isnil(L, -1)) {
  166. lua_pop(L, 2); // Pop on_step and entity
  167. return;
  168. }
  169. luaL_checktype(L, -1, LUA_TFUNCTION);
  170. lua_pushvalue(L, object); // self
  171. lua_pushnumber(L, dtime); // dtime
  172. setOriginFromTable(object);
  173. PCALL_RES(lua_pcall(L, 2, 0, error_handler));
  174. lua_pop(L, 2); // Pop object and error handler
  175. }
  176. // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
  177. // tool_capabilities, direction, damage)
  178. bool ScriptApiEntity::luaentity_Punch(u16 id,
  179. ServerActiveObject *puncher, float time_from_last_punch,
  180. const ToolCapabilities *toolcap, v3f dir, s16 damage)
  181. {
  182. SCRIPTAPI_PRECHECKHEADER
  183. //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
  184. int error_handler = PUSH_ERROR_HANDLER(L);
  185. // Get core.luaentities[id]
  186. luaentity_get(L,id);
  187. int object = lua_gettop(L);
  188. // State: object is at top of stack
  189. // Get function
  190. lua_getfield(L, -1, "on_punch");
  191. if (lua_isnil(L, -1)) {
  192. lua_pop(L, 2); // Pop on_punch and entity
  193. return false;
  194. }
  195. luaL_checktype(L, -1, LUA_TFUNCTION);
  196. lua_pushvalue(L, object); // self
  197. objectrefGetOrCreate(L, puncher); // Clicker reference
  198. lua_pushnumber(L, time_from_last_punch);
  199. push_tool_capabilities(L, *toolcap);
  200. push_v3f(L, dir);
  201. lua_pushnumber(L, damage);
  202. setOriginFromTable(object);
  203. PCALL_RES(lua_pcall(L, 6, 1, error_handler));
  204. bool retval = lua_toboolean(L, -1);
  205. lua_pop(L, 2); // Pop object and error handler
  206. return retval;
  207. }
  208. // Calls entity[field](ObjectRef self, ObjectRef sao)
  209. bool ScriptApiEntity::luaentity_run_simple_callback(u16 id,
  210. ServerActiveObject *sao, const char *field)
  211. {
  212. SCRIPTAPI_PRECHECKHEADER
  213. int error_handler = PUSH_ERROR_HANDLER(L);
  214. // Get core.luaentities[id]
  215. luaentity_get(L, id);
  216. int object = lua_gettop(L);
  217. // State: object is at top of stack
  218. // Get function
  219. lua_getfield(L, -1, field);
  220. if (lua_isnil(L, -1)) {
  221. lua_pop(L, 2); // Pop callback field and entity
  222. return false;
  223. }
  224. luaL_checktype(L, -1, LUA_TFUNCTION);
  225. lua_pushvalue(L, object); // self
  226. objectrefGetOrCreate(L, sao); // killer reference
  227. setOriginFromTable(object);
  228. PCALL_RES(lua_pcall(L, 2, 1, error_handler));
  229. bool retval = lua_toboolean(L, -1);
  230. lua_pop(L, 2); // Pop object and error handler
  231. return retval;
  232. }
  233. bool ScriptApiEntity::luaentity_on_death(u16 id, ServerActiveObject *killer)
  234. {
  235. return luaentity_run_simple_callback(id, killer, "on_death");
  236. }
  237. // Calls entity:on_rightclick(ObjectRef clicker)
  238. void ScriptApiEntity::luaentity_Rightclick(u16 id, ServerActiveObject *clicker)
  239. {
  240. luaentity_run_simple_callback(id, clicker, "on_rightclick");
  241. }
  242. void ScriptApiEntity::luaentity_on_attach_child(u16 id, ServerActiveObject *child)
  243. {
  244. luaentity_run_simple_callback(id, child, "on_attach_child");
  245. }
  246. void ScriptApiEntity::luaentity_on_detach_child(u16 id, ServerActiveObject *child)
  247. {
  248. luaentity_run_simple_callback(id, child, "on_detach_child");
  249. }
  250. void ScriptApiEntity::luaentity_on_detach(u16 id, ServerActiveObject *parent)
  251. {
  252. luaentity_run_simple_callback(id, parent, "on_detach");
  253. }