You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

256 lines
8.2 KiB

  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_env.h"
  17. #include "cpp_api/s_internal.h"
  18. #include "common/c_converter.h"
  19. #include "log.h"
  20. #include "environment.h"
  21. #include "mapgen.h"
  22. #include "lua_api/l_env.h"
  23. #include "server.h"
  24. void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
  25. u32 blockseed)
  26. {
  27. SCRIPTAPI_PRECHECKHEADER
  28. // Get core.registered_on_generateds
  29. lua_getglobal(L, "core");
  30. lua_getfield(L, -1, "registered_on_generateds");
  31. // Call callbacks
  32. push_v3s16(L, minp);
  33. push_v3s16(L, maxp);
  34. lua_pushnumber(L, blockseed);
  35. runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
  36. }
  37. void ScriptApiEnv::environment_Step(float dtime)
  38. {
  39. SCRIPTAPI_PRECHECKHEADER
  40. //infostream << "scriptapi_environment_step" << std::endl;
  41. // Get core.registered_globalsteps
  42. lua_getglobal(L, "core");
  43. lua_getfield(L, -1, "registered_globalsteps");
  44. // Call callbacks
  45. lua_pushnumber(L, dtime);
  46. try {
  47. runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
  48. } catch (LuaError &e) {
  49. getServer()->setAsyncFatalError(
  50. std::string("environment_Step: ") + e.what() + "\n"
  51. + script_get_backtrace(L));
  52. }
  53. }
  54. void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
  55. {
  56. SCRIPTAPI_PRECHECKHEADER
  57. if (player == NULL)
  58. return;
  59. // Get minetest.registered_playerevents
  60. lua_getglobal(L, "minetest");
  61. lua_getfield(L, -1, "registered_playerevents");
  62. // Call callbacks
  63. objectrefGetOrCreate(L, player); // player
  64. lua_pushstring(L,type.c_str()); // event type
  65. try {
  66. runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
  67. } catch (LuaError &e) {
  68. getServer()->setAsyncFatalError(
  69. std::string("player_event: ") + e.what() + "\n"
  70. + script_get_backtrace(L) );
  71. }
  72. }
  73. void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
  74. {
  75. SCRIPTAPI_PRECHECKHEADER
  76. verbosestream << "scriptapi_add_environment" << std::endl;
  77. setEnv(env);
  78. /*
  79. Add {Loading,Active}BlockModifiers to environment
  80. */
  81. // Get core.registered_abms
  82. lua_getglobal(L, "core");
  83. lua_getfield(L, -1, "registered_abms");
  84. int registered_abms = lua_gettop(L);
  85. if (!lua_istable(L, registered_abms)) {
  86. lua_pop(L, 1);
  87. throw LuaError("core.registered_abms was not a lua table, as expected.");
  88. }
  89. lua_pushnil(L);
  90. while (lua_next(L, registered_abms)) {
  91. // key at index -2 and value at index -1
  92. int id = lua_tonumber(L, -2);
  93. int current_abm = lua_gettop(L);
  94. std::set<std::string> trigger_contents;
  95. lua_getfield(L, current_abm, "nodenames");
  96. if (lua_istable(L, -1)) {
  97. int table = lua_gettop(L);
  98. lua_pushnil(L);
  99. while (lua_next(L, table)) {
  100. // key at index -2 and value at index -1
  101. luaL_checktype(L, -1, LUA_TSTRING);
  102. trigger_contents.insert(readParam<std::string>(L, -1));
  103. // removes value, keeps key for next iteration
  104. lua_pop(L, 1);
  105. }
  106. } else if (lua_isstring(L, -1)) {
  107. trigger_contents.insert(readParam<std::string>(L, -1));
  108. }
  109. lua_pop(L, 1);
  110. std::set<std::string> required_neighbors;
  111. lua_getfield(L, current_abm, "neighbors");
  112. if (lua_istable(L, -1)) {
  113. int table = lua_gettop(L);
  114. lua_pushnil(L);
  115. while (lua_next(L, table)) {
  116. // key at index -2 and value at index -1
  117. luaL_checktype(L, -1, LUA_TSTRING);
  118. required_neighbors.insert(readParam<std::string>(L, -1));
  119. // removes value, keeps key for next iteration
  120. lua_pop(L, 1);
  121. }
  122. } else if (lua_isstring(L, -1)) {
  123. required_neighbors.insert(readParam<std::string>(L, -1));
  124. }
  125. lua_pop(L, 1);
  126. float trigger_interval = 10.0;
  127. getfloatfield(L, current_abm, "interval", trigger_interval);
  128. int trigger_chance = 50;
  129. getintfield(L, current_abm, "chance", trigger_chance);
  130. bool simple_catch_up = true;
  131. getboolfield(L, current_abm, "catch_up", simple_catch_up);
  132. LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
  133. trigger_interval, trigger_chance, simple_catch_up);
  134. env->addActiveBlockModifier(abm);
  135. // removes value, keeps key for next iteration
  136. lua_pop(L, 1);
  137. }
  138. lua_pop(L, 1);
  139. // Get core.registered_lbms
  140. lua_getglobal(L, "core");
  141. lua_getfield(L, -1, "registered_lbms");
  142. int registered_lbms = lua_gettop(L);
  143. if (!lua_istable(L, registered_lbms)) {
  144. lua_pop(L, 1);
  145. throw LuaError("core.registered_lbms was not a lua table, as expected.");
  146. }
  147. lua_pushnil(L);
  148. while (lua_next(L, registered_lbms)) {
  149. // key at index -2 and value at index -1
  150. int id = lua_tonumber(L, -2);
  151. int current_lbm = lua_gettop(L);
  152. std::set<std::string> trigger_contents;
  153. lua_getfield(L, current_lbm, "nodenames");
  154. if (lua_istable(L, -1)) {
  155. int table = lua_gettop(L);
  156. lua_pushnil(L);
  157. while (lua_next(L, table)) {
  158. // key at index -2 and value at index -1
  159. luaL_checktype(L, -1, LUA_TSTRING);
  160. trigger_contents.insert(readParam<std::string>(L, -1));
  161. // removes value, keeps key for next iteration
  162. lua_pop(L, 1);
  163. }
  164. } else if (lua_isstring(L, -1)) {
  165. trigger_contents.insert(readParam<std::string>(L, -1));
  166. }
  167. lua_pop(L, 1);
  168. std::string name;
  169. getstringfield(L, current_lbm, "name", name);
  170. bool run_at_every_load = getboolfield_default(L, current_lbm,
  171. "run_at_every_load", false);
  172. LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
  173. run_at_every_load);
  174. env->addLoadingBlockModifierDef(lbm);
  175. // removes value, keeps key for next iteration
  176. lua_pop(L, 1);
  177. }
  178. lua_pop(L, 1);
  179. }
  180. void ScriptApiEnv::on_emerge_area_completion(
  181. v3s16 blockpos, int action, ScriptCallbackState *state)
  182. {
  183. Server *server = getServer();
  184. // This function should be executed with envlock held.
  185. // The caller (LuaEmergeAreaCallback in src/script/lua_api/l_env.cpp)
  186. // should have obtained the lock.
  187. // Note that the order of these locks is important! Envlock must *ALWAYS*
  188. // be acquired before attempting to acquire scriptlock, or else ServerThread
  189. // will try to acquire scriptlock after it already owns envlock, thus
  190. // deadlocking EmergeThread and ServerThread
  191. SCRIPTAPI_PRECHECKHEADER
  192. int error_handler = PUSH_ERROR_HANDLER(L);
  193. lua_rawgeti(L, LUA_REGISTRYINDEX, state->callback_ref);
  194. luaL_checktype(L, -1, LUA_TFUNCTION);
  195. push_v3s16(L, blockpos);
  196. lua_pushinteger(L, action);
  197. lua_pushinteger(L, state->refcount);
  198. lua_rawgeti(L, LUA_REGISTRYINDEX, state->args_ref);
  199. setOriginDirect(state->origin.c_str());
  200. try {
  201. PCALL_RES(lua_pcall(L, 4, 0, error_handler));
  202. } catch (LuaError &e) {
  203. server->setAsyncFatalError(
  204. std::string("on_emerge_area_completion: ") + e.what() + "\n"
  205. + script_get_backtrace(L));
  206. }
  207. lua_pop(L, 1); // Pop error handler
  208. if (state->refcount == 0) {
  209. luaL_unref(L, LUA_REGISTRYINDEX, state->callback_ref);
  210. luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);
  211. }
  212. }