l_env.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #pragma once
  17. #include "lua_api/l_base.h"
  18. #include "serverenvironment.h"
  19. #include "raycast.h"
  20. // base class containing helpers
  21. class ModApiEnvBase : public ModApiBase {
  22. protected:
  23. static void collectNodeIds(lua_State *L, int idx,
  24. const NodeDefManager *ndef, std::vector<content_t> &filter);
  25. static void checkArea(v3s16 &minp, v3s16 &maxp);
  26. // F must be (v3s16 pos) -> MapNode
  27. template <typename F>
  28. static int findNodeNear(lua_State *L, v3s16 pos, int radius,
  29. const std::vector<content_t> &filter, int start_radius, F &&getNode);
  30. // F must be (G callback) -> void
  31. // with G being (v3s16 p, MapNode n) -> bool
  32. // and behave like Map::forEachNodeInArea
  33. template <typename F>
  34. static int findNodesInArea(lua_State *L, const NodeDefManager *ndef,
  35. const std::vector<content_t> &filter, bool grouped, F &&iterate);
  36. // F must be (v3s16 pos) -> MapNode
  37. template <typename F>
  38. static int findNodesInAreaUnderAir(lua_State *L, v3s16 minp, v3s16 maxp,
  39. const std::vector<content_t> &filter, F &&getNode);
  40. static const EnumString es_ClearObjectsMode[];
  41. static const EnumString es_BlockStatusType[];
  42. };
  43. class ModApiEnv : public ModApiEnvBase {
  44. private:
  45. // set_node(pos, node)
  46. // pos = {x=num, y=num, z=num}
  47. static int l_set_node(lua_State *L);
  48. // bulk_set_node([pos1, pos2, ...], node)
  49. // pos = {x=num, y=num, z=num}
  50. static int l_bulk_set_node(lua_State *L);
  51. static int l_add_node(lua_State *L);
  52. // remove_node(pos)
  53. // pos = {x=num, y=num, z=num}
  54. static int l_remove_node(lua_State *L);
  55. // swap_node(pos, node)
  56. // pos = {x=num, y=num, z=num}
  57. static int l_swap_node(lua_State *L);
  58. // get_node_raw(x, y, z) -> content, param1, param2, pos_ok
  59. // Used to implement get_node and get_node_or_nil in lua.
  60. // This is still faster than doing it from C++ even with optimized pushnode.
  61. static int l_get_node_raw(lua_State *L);
  62. // get_node_light(pos, timeofday)
  63. // pos = {x=num, y=num, z=num}
  64. // timeofday: nil = current time, 0 = night, 0.5 = day
  65. static int l_get_node_light(lua_State *L);
  66. // get_natural_light(pos, timeofday)
  67. // pos = {x=num, y=num, z=num}
  68. // timeofday: nil = current time, 0 = night, 0.5 = day
  69. static int l_get_natural_light(lua_State *L);
  70. // place_node(pos, node)
  71. // pos = {x=num, y=num, z=num}
  72. static int l_place_node(lua_State *L);
  73. // dig_node(pos)
  74. // pos = {x=num, y=num, z=num}
  75. static int l_dig_node(lua_State *L);
  76. // punch_node(pos)
  77. // pos = {x=num, y=num, z=num}
  78. static int l_punch_node(lua_State *L);
  79. // get_node_max_level(pos)
  80. // pos = {x=num, y=num, z=num}
  81. static int l_get_node_max_level(lua_State *L);
  82. // get_node_level(pos)
  83. // pos = {x=num, y=num, z=num}
  84. static int l_get_node_level(lua_State *L);
  85. // set_node_level(pos)
  86. // pos = {x=num, y=num, z=num}
  87. static int l_set_node_level(lua_State *L);
  88. // add_node_level(pos)
  89. // pos = {x=num, y=num, z=num}
  90. static int l_add_node_level(lua_State *L);
  91. // get_node_boxes(box_type, pos, [node]) -> table
  92. // box_type = string
  93. // pos = {x=num, y=num, z=num}
  94. // node = {name=string, param1=num, param2=num} or nil
  95. static int l_get_node_boxes(lua_State *L);
  96. // find_nodes_with_meta(pos1, pos2)
  97. static int l_find_nodes_with_meta(lua_State *L);
  98. // get_meta(pos)
  99. static int l_get_meta(lua_State *L);
  100. // get_node_timer(pos)
  101. static int l_get_node_timer(lua_State *L);
  102. // add_entity(pos, entityname) -> ObjectRef or nil
  103. // pos = {x=num, y=num, z=num}
  104. static int l_add_entity(lua_State *L);
  105. // add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
  106. // pos = {x=num, y=num, z=num}
  107. static int l_add_item(lua_State *L);
  108. // get_connected_players()
  109. static int l_get_connected_players(lua_State *L);
  110. // get_player_by_name(name)
  111. static int l_get_player_by_name(lua_State *L);
  112. // get_objects_inside_radius(pos, radius)
  113. static int l_get_objects_inside_radius(lua_State *L);
  114. // get_objects_in_area(pos, minp, maxp)
  115. static int l_get_objects_in_area(lua_State *L);
  116. // set_timeofday(val)
  117. // val = 0...1
  118. static int l_set_timeofday(lua_State *L);
  119. // get_timeofday() -> 0...1
  120. static int l_get_timeofday(lua_State *L);
  121. // get_gametime()
  122. static int l_get_gametime(lua_State *L);
  123. // get_day_count() -> int
  124. static int l_get_day_count(lua_State *L);
  125. // find_node_near(pos, radius, nodenames, search_center) -> pos or nil
  126. // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
  127. static int l_find_node_near(lua_State *L);
  128. // find_nodes_in_area(minp, maxp, nodenames) -> list of positions
  129. // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
  130. static int l_find_nodes_in_area(lua_State *L);
  131. // find_surface_nodes_in_area(minp, maxp, nodenames) -> list of positions
  132. // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
  133. static int l_find_nodes_in_area_under_air(lua_State *L);
  134. // fix_light(p1, p2) -> true/false
  135. static int l_fix_light(lua_State *L);
  136. // load_area(p1)
  137. static int l_load_area(lua_State *L);
  138. // emerge_area(p1, p2)
  139. static int l_emerge_area(lua_State *L);
  140. // delete_area(p1, p2) -> true/false
  141. static int l_delete_area(lua_State *L);
  142. // get_perlin(seeddiff, octaves, persistence, scale)
  143. // returns world-specific PerlinNoise
  144. static int l_get_perlin(lua_State *L);
  145. // get_perlin_map(noiseparams, size)
  146. // returns world-specific PerlinNoiseMap
  147. static int l_get_perlin_map(lua_State *L);
  148. // get_voxel_manip()
  149. // returns world-specific voxel manipulator
  150. static int l_get_voxel_manip(lua_State *L);
  151. // clear_objects()
  152. // clear all objects in the environment
  153. static int l_clear_objects(lua_State *L);
  154. // spawn_tree(pos, treedef)
  155. static int l_spawn_tree(lua_State *L);
  156. // line_of_sight(pos1, pos2) -> true/false
  157. static int l_line_of_sight(lua_State *L);
  158. // raycast(pos1, pos2, objects, liquids) -> Raycast
  159. static int l_raycast(lua_State *L);
  160. // find_path(pos1, pos2, searchdistance,
  161. // max_jump, max_drop, algorithm) -> table containing path
  162. static int l_find_path(lua_State *L);
  163. // transforming_liquid_add(pos)
  164. static int l_transforming_liquid_add(lua_State *L);
  165. // forceload_block(blockpos)
  166. // forceloads a block
  167. static int l_forceload_block(lua_State *L);
  168. // forceload_free_block(blockpos)
  169. // stops forceloading a position
  170. static int l_forceload_free_block(lua_State *L);
  171. // compare_block_status(nodepos)
  172. static int l_compare_block_status(lua_State *L);
  173. // get_translated_string(lang_code, string)
  174. static int l_get_translated_string(lua_State * L);
  175. public:
  176. static void Initialize(lua_State *L, int top);
  177. static void InitializeClient(lua_State *L, int top);
  178. };
  179. /*
  180. * Duplicates of certain env APIs that operate not on the global
  181. * map but on a VoxelManipulator. This is for emerge scripting.
  182. */
  183. class ModApiEnvVM : public ModApiEnvBase {
  184. private:
  185. // get_node_or_nil(pos)
  186. static int l_get_node_or_nil(lua_State *L);
  187. // get_node_max_level(pos)
  188. static int l_get_node_max_level(lua_State *L);
  189. // get_node_level(pos)
  190. static int l_get_node_level(lua_State *L);
  191. // set_node_level(pos)
  192. static int l_set_node_level(lua_State *L);
  193. // add_node_level(pos)
  194. static int l_add_node_level(lua_State *L);
  195. // find_node_near(pos, radius, nodenames, [search_center])
  196. static int l_find_node_near(lua_State *L);
  197. // find_nodes_in_area(minp, maxp, nodenames, [grouped])
  198. static int l_find_nodes_in_area(lua_State *L);
  199. // find_surface_nodes_in_area(minp, maxp, nodenames)
  200. static int l_find_nodes_in_area_under_air(lua_State *L);
  201. // spawn_tree(pos, treedef)
  202. static int l_spawn_tree(lua_State *L);
  203. // Helper: get the vmanip we're operating on
  204. static MMVManip *getVManip(lua_State *L);
  205. public:
  206. static void InitializeEmerge(lua_State *L, int top);
  207. };
  208. class LuaABM : public ActiveBlockModifier {
  209. private:
  210. int m_id;
  211. std::vector<std::string> m_trigger_contents;
  212. std::vector<std::string> m_required_neighbors;
  213. float m_trigger_interval;
  214. u32 m_trigger_chance;
  215. bool m_simple_catch_up;
  216. s16 m_min_y;
  217. s16 m_max_y;
  218. public:
  219. LuaABM(lua_State *L, int id,
  220. const std::vector<std::string> &trigger_contents,
  221. const std::vector<std::string> &required_neighbors,
  222. float trigger_interval, u32 trigger_chance, bool simple_catch_up, s16 min_y, s16 max_y):
  223. m_id(id),
  224. m_trigger_contents(trigger_contents),
  225. m_required_neighbors(required_neighbors),
  226. m_trigger_interval(trigger_interval),
  227. m_trigger_chance(trigger_chance),
  228. m_simple_catch_up(simple_catch_up),
  229. m_min_y(min_y),
  230. m_max_y(max_y)
  231. {
  232. }
  233. virtual const std::vector<std::string> &getTriggerContents() const
  234. {
  235. return m_trigger_contents;
  236. }
  237. virtual const std::vector<std::string> &getRequiredNeighbors() const
  238. {
  239. return m_required_neighbors;
  240. }
  241. virtual float getTriggerInterval()
  242. {
  243. return m_trigger_interval;
  244. }
  245. virtual u32 getTriggerChance()
  246. {
  247. return m_trigger_chance;
  248. }
  249. virtual bool getSimpleCatchUp()
  250. {
  251. return m_simple_catch_up;
  252. }
  253. virtual s16 getMinY()
  254. {
  255. return m_min_y;
  256. }
  257. virtual s16 getMaxY()
  258. {
  259. return m_max_y;
  260. }
  261. virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
  262. u32 active_object_count, u32 active_object_count_wider);
  263. };
  264. class LuaLBM : public LoadingBlockModifierDef
  265. {
  266. private:
  267. int m_id;
  268. public:
  269. LuaLBM(lua_State *L, int id,
  270. const std::set<std::string> &trigger_contents,
  271. const std::string &name,
  272. bool run_at_every_load):
  273. m_id(id)
  274. {
  275. this->run_at_every_load = run_at_every_load;
  276. this->trigger_contents = trigger_contents;
  277. this->name = name;
  278. }
  279. virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, float dtime_s);
  280. };
  281. //! Lua wrapper for RaycastState objects
  282. class LuaRaycast : public ModApiBase
  283. {
  284. private:
  285. static const luaL_Reg methods[];
  286. //! Inner state
  287. RaycastState state;
  288. // Exported functions
  289. // garbage collector
  290. static int gc_object(lua_State *L);
  291. /*!
  292. * Raycast:next() -> pointed_thing
  293. * Returns the next pointed thing on the ray.
  294. */
  295. static int l_next(lua_State *L);
  296. public:
  297. //! Constructor with the same arguments as RaycastState.
  298. LuaRaycast(
  299. const core::line3d<f32> &shootline,
  300. bool objects_pointable,
  301. bool liquids_pointable,
  302. const std::optional<Pointabilities> &pointabilities) :
  303. state(shootline, objects_pointable, liquids_pointable, pointabilities)
  304. {}
  305. //! Creates a LuaRaycast and leaves it on top of the stack.
  306. static int create_object(lua_State *L);
  307. //! Registers Raycast as a Lua userdata type.
  308. static void Register(lua_State *L);
  309. static const char className[];
  310. };
  311. struct ScriptCallbackState {
  312. ServerScripting *script;
  313. int callback_ref;
  314. int args_ref;
  315. unsigned int refcount;
  316. std::string origin;
  317. };