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.
 
 
 
 
 
 

228 lines
6.1 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  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. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "s_client.h"
  18. #include "s_internal.h"
  19. #include "client.h"
  20. #include "common/c_converter.h"
  21. #include "common/c_content.h"
  22. #include "s_item.h"
  23. void ScriptApiClient::on_mods_loaded()
  24. {
  25. SCRIPTAPI_PRECHECKHEADER
  26. // Get registered shutdown hooks
  27. lua_getglobal(L, "core");
  28. lua_getfield(L, -1, "registered_on_mods_loaded");
  29. // Call callbacks
  30. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  31. }
  32. void ScriptApiClient::on_shutdown()
  33. {
  34. SCRIPTAPI_PRECHECKHEADER
  35. // Get registered shutdown hooks
  36. lua_getglobal(L, "core");
  37. lua_getfield(L, -1, "registered_on_shutdown");
  38. // Call callbacks
  39. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  40. }
  41. bool ScriptApiClient::on_sending_message(const std::string &message)
  42. {
  43. SCRIPTAPI_PRECHECKHEADER
  44. // Get core.registered_on_chat_messages
  45. lua_getglobal(L, "core");
  46. lua_getfield(L, -1, "registered_on_sending_chat_message");
  47. // Call callbacks
  48. lua_pushstring(L, message.c_str());
  49. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  50. return readParam<bool>(L, -1);
  51. }
  52. bool ScriptApiClient::on_receiving_message(const std::string &message)
  53. {
  54. SCRIPTAPI_PRECHECKHEADER
  55. // Get core.registered_on_chat_messages
  56. lua_getglobal(L, "core");
  57. lua_getfield(L, -1, "registered_on_receiving_chat_message");
  58. // Call callbacks
  59. lua_pushstring(L, message.c_str());
  60. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  61. return readParam<bool>(L, -1);
  62. }
  63. void ScriptApiClient::on_damage_taken(int32_t damage_amount)
  64. {
  65. SCRIPTAPI_PRECHECKHEADER
  66. // Get core.registered_on_chat_messages
  67. lua_getglobal(L, "core");
  68. lua_getfield(L, -1, "registered_on_damage_taken");
  69. // Call callbacks
  70. lua_pushinteger(L, damage_amount);
  71. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  72. }
  73. void ScriptApiClient::on_hp_modification(int32_t newhp)
  74. {
  75. SCRIPTAPI_PRECHECKHEADER
  76. // Get core.registered_on_chat_messages
  77. lua_getglobal(L, "core");
  78. lua_getfield(L, -1, "registered_on_hp_modification");
  79. // Call callbacks
  80. lua_pushinteger(L, newhp);
  81. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  82. }
  83. void ScriptApiClient::on_death()
  84. {
  85. SCRIPTAPI_PRECHECKHEADER
  86. // Get registered shutdown hooks
  87. lua_getglobal(L, "core");
  88. lua_getfield(L, -1, "registered_on_death");
  89. // Call callbacks
  90. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  91. }
  92. void ScriptApiClient::environment_step(float dtime)
  93. {
  94. SCRIPTAPI_PRECHECKHEADER
  95. // Get core.registered_globalsteps
  96. lua_getglobal(L, "core");
  97. lua_getfield(L, -1, "registered_globalsteps");
  98. // Call callbacks
  99. lua_pushnumber(L, dtime);
  100. try {
  101. runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
  102. } catch (LuaError &e) {
  103. getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
  104. + script_get_backtrace(L));
  105. }
  106. }
  107. void ScriptApiClient::on_formspec_input(const std::string &formname,
  108. const StringMap &fields)
  109. {
  110. SCRIPTAPI_PRECHECKHEADER
  111. // Get core.registered_on_chat_messages
  112. lua_getglobal(L, "core");
  113. lua_getfield(L, -1, "registered_on_formspec_input");
  114. // Call callbacks
  115. // param 1
  116. lua_pushstring(L, formname.c_str());
  117. // param 2
  118. lua_newtable(L);
  119. StringMap::const_iterator it;
  120. for (it = fields.begin(); it != fields.end(); ++it) {
  121. const std::string &name = it->first;
  122. const std::string &value = it->second;
  123. lua_pushstring(L, name.c_str());
  124. lua_pushlstring(L, value.c_str(), value.size());
  125. lua_settable(L, -3);
  126. }
  127. runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
  128. }
  129. bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
  130. {
  131. SCRIPTAPI_PRECHECKHEADER
  132. INodeDefManager *ndef = getClient()->ndef();
  133. // Get core.registered_on_dignode
  134. lua_getglobal(L, "core");
  135. lua_getfield(L, -1, "registered_on_dignode");
  136. // Push data
  137. push_v3s16(L, p);
  138. pushnode(L, node, ndef);
  139. // Call functions
  140. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  141. return lua_toboolean(L, -1);
  142. }
  143. bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
  144. {
  145. SCRIPTAPI_PRECHECKHEADER
  146. INodeDefManager *ndef = getClient()->ndef();
  147. // Get core.registered_on_punchgnode
  148. lua_getglobal(L, "core");
  149. lua_getfield(L, -1, "registered_on_punchnode");
  150. // Push data
  151. push_v3s16(L, p);
  152. pushnode(L, node, ndef);
  153. // Call functions
  154. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  155. return readParam<bool>(L, -1);
  156. }
  157. bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
  158. {
  159. SCRIPTAPI_PRECHECKHEADER
  160. // Get core.registered_on_placenode
  161. lua_getglobal(L, "core");
  162. lua_getfield(L, -1, "registered_on_placenode");
  163. // Push data
  164. push_pointed_thing(L, pointed, true);
  165. push_item_definition(L, item);
  166. // Call functions
  167. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  168. return readParam<bool>(L, -1);
  169. }
  170. bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
  171. {
  172. SCRIPTAPI_PRECHECKHEADER
  173. // Get core.registered_on_item_use
  174. lua_getglobal(L, "core");
  175. lua_getfield(L, -1, "registered_on_item_use");
  176. // Push data
  177. LuaItemStack::create(L, item);
  178. push_pointed_thing(L, pointed, true);
  179. // Call functions
  180. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  181. return readParam<bool>(L, -1);
  182. }
  183. void ScriptApiClient::setEnv(ClientEnvironment *env)
  184. {
  185. ScriptApiBase::setEnv(env);
  186. }