]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_env.h
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / script / lua_api / l_env.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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
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.
14
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.
18 */
19
20 #pragma once
21
22 #include "lua_api/l_base.h"
23 #include "serverenvironment.h"
24 #include "raycast.h"
25
26 class ModApiEnvMod : public ModApiBase {
27 private:
28         // set_node(pos, node)
29         // pos = {x=num, y=num, z=num}
30         static int l_set_node(lua_State *L);
31
32         // bulk_set_node([pos1, pos2, ...], node)
33         // pos = {x=num, y=num, z=num}
34         static int l_bulk_set_node(lua_State *L);
35
36         static int l_add_node(lua_State *L);
37
38         // remove_node(pos)
39         // pos = {x=num, y=num, z=num}
40         static int l_remove_node(lua_State *L);
41
42         // swap_node(pos, node)
43         // pos = {x=num, y=num, z=num}
44         static int l_swap_node(lua_State *L);
45
46         // get_node(pos)
47         // pos = {x=num, y=num, z=num}
48         static int l_get_node(lua_State *L);
49
50         // get_node_or_nil(pos)
51         // pos = {x=num, y=num, z=num}
52         static int l_get_node_or_nil(lua_State *L);
53
54         // get_node_light(pos, timeofday)
55         // pos = {x=num, y=num, z=num}
56         // timeofday: nil = current time, 0 = night, 0.5 = day
57         static int l_get_node_light(lua_State *L);
58
59         // get_natural_light(pos, timeofday)
60         // pos = {x=num, y=num, z=num}
61         // timeofday: nil = current time, 0 = night, 0.5 = day
62         static int l_get_natural_light(lua_State *L);
63
64         // place_node(pos, node)
65         // pos = {x=num, y=num, z=num}
66         static int l_place_node(lua_State *L);
67
68         // dig_node(pos)
69         // pos = {x=num, y=num, z=num}
70         static int l_dig_node(lua_State *L);
71
72         // punch_node(pos)
73         // pos = {x=num, y=num, z=num}
74         static int l_punch_node(lua_State *L);
75
76         // get_node_max_level(pos)
77         // pos = {x=num, y=num, z=num}
78         static int l_get_node_max_level(lua_State *L);
79
80         // get_node_level(pos)
81         // pos = {x=num, y=num, z=num}
82         static int l_get_node_level(lua_State *L);
83
84         // set_node_level(pos)
85         // pos = {x=num, y=num, z=num}
86         static int l_set_node_level(lua_State *L);
87
88         // add_node_level(pos)
89         // pos = {x=num, y=num, z=num}
90         static int l_add_node_level(lua_State *L);
91
92         // find_nodes_with_meta(pos1, pos2)
93         static int l_find_nodes_with_meta(lua_State *L);
94
95         // get_meta(pos)
96         static int l_get_meta(lua_State *L);
97
98         // get_node_timer(pos)
99         static int l_get_node_timer(lua_State *L);
100
101         // add_entity(pos, entityname) -> ObjectRef or nil
102         // pos = {x=num, y=num, z=num}
103         static int l_add_entity(lua_State *L);
104
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
109         // get_connected_players()
110         static int l_get_connected_players(lua_State *L);
111
112         // get_player_by_name(name)
113         static int l_get_player_by_name(lua_State *L);
114
115         // get_objects_inside_radius(pos, radius)
116         static int l_get_objects_inside_radius(lua_State *L);
117
118         // get_objects_in_area(pos, minp, maxp)
119         static int l_get_objects_in_area(lua_State *L);
120
121         // set_timeofday(val)
122         // val = 0...1
123         static int l_set_timeofday(lua_State *L);
124
125         // get_timeofday() -> 0...1
126         static int l_get_timeofday(lua_State *L);
127
128         // get_gametime()
129         static int l_get_gametime(lua_State *L);
130
131         // get_day_count() -> int
132         static int l_get_day_count(lua_State *L);
133
134         // find_node_near(pos, radius, nodenames, search_center) -> pos or nil
135         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
136         static int l_find_node_near(lua_State *L);
137         
138         // find_nodes_near(pos, radius, nodenames, search_center) -> list of positions
139         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
140         static int l_find_nodes_near(lua_State *L);
141         
142         // find_nodes_near_under_air(pos, radius, nodenames, search_center) -> list of positions
143         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
144         static int l_find_nodes_near_under_air(lua_State *L);
145         
146         // find_nodes_near_under_air(pos, radius, nodenames, search_center) -> list of positions
147         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
148         static int l_find_nodes_near_under_air_except(lua_State *L);
149
150         // find_nodes_in_area(minp, maxp, nodenames) -> list of positions
151         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
152         static int l_find_nodes_in_area(lua_State *L);
153
154         // find_surface_nodes_in_area(minp, maxp, nodenames) -> list of positions
155         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
156         static int l_find_nodes_in_area_under_air(lua_State *L);
157
158         // fix_light(p1, p2) -> true/false
159         static int l_fix_light(lua_State *L);
160
161         // load_area(p1)
162         static int l_load_area(lua_State *L);
163
164         // emerge_area(p1, p2)
165         static int l_emerge_area(lua_State *L);
166
167         // delete_area(p1, p2) -> true/false
168         static int l_delete_area(lua_State *L);
169
170         // get_perlin(seeddiff, octaves, persistence, scale)
171         // returns world-specific PerlinNoise
172         static int l_get_perlin(lua_State *L);
173
174         // get_perlin_map(noiseparams, size)
175         // returns world-specific PerlinNoiseMap
176         static int l_get_perlin_map(lua_State *L);
177
178         // get_voxel_manip()
179         // returns world-specific voxel manipulator
180         static int l_get_voxel_manip(lua_State *L);
181
182         // clear_objects()
183         // clear all objects in the environment
184         static int l_clear_objects(lua_State *L);
185
186         // spawn_tree(pos, treedef)
187         static int l_spawn_tree(lua_State *L);
188
189         // line_of_sight(pos1, pos2) -> true/false
190         static int l_line_of_sight(lua_State *L);
191
192         // raycast(pos1, pos2, objects, liquids) -> Raycast
193         static int l_raycast(lua_State *L);
194
195         // find_path(pos1, pos2, searchdistance,
196         //     max_jump, max_drop, algorithm) -> table containing path
197         static int l_find_path(lua_State *L);
198
199         // transforming_liquid_add(pos)
200         static int l_transforming_liquid_add(lua_State *L);
201
202         // forceload_block(blockpos)
203         // forceloads a block
204         static int l_forceload_block(lua_State *L);
205
206         // forceload_free_block(blockpos)
207         // stops forceloading a position
208         static int l_forceload_free_block(lua_State *L);
209
210         // compare_block_status(nodepos)
211         static int l_compare_block_status(lua_State *L);
212
213         // Get a string translated server side
214         static int l_get_translated_string(lua_State * L);
215
216         /* Helpers */
217
218         static void collectNodeIds(lua_State *L, int idx,
219                 const NodeDefManager *ndef, std::vector<content_t> &filter);
220
221 public:
222         static void Initialize(lua_State *L, int top);
223         static void InitializeClient(lua_State *L, int top);
224
225         static const EnumString es_ClearObjectsMode[];
226         static const EnumString es_BlockStatusType[];
227 };
228
229 class LuaABM : public ActiveBlockModifier {
230 private:
231         int m_id;
232
233         std::vector<std::string> m_trigger_contents;
234         std::vector<std::string> m_required_neighbors;
235         float m_trigger_interval;
236         u32 m_trigger_chance;
237         bool m_simple_catch_up;
238         s16 m_min_y;
239         s16 m_max_y;
240 public:
241         LuaABM(lua_State *L, int id,
242                         const std::vector<std::string> &trigger_contents,
243                         const std::vector<std::string> &required_neighbors,
244                         float trigger_interval, u32 trigger_chance, bool simple_catch_up, s16 min_y, s16 max_y):
245                 m_id(id),
246                 m_trigger_contents(trigger_contents),
247                 m_required_neighbors(required_neighbors),
248                 m_trigger_interval(trigger_interval),
249                 m_trigger_chance(trigger_chance),
250                 m_simple_catch_up(simple_catch_up),
251                 m_min_y(min_y),
252                 m_max_y(max_y)
253         {
254         }
255         virtual const std::vector<std::string> &getTriggerContents() const
256         {
257                 return m_trigger_contents;
258         }
259         virtual const std::vector<std::string> &getRequiredNeighbors() const
260         {
261                 return m_required_neighbors;
262         }
263         virtual float getTriggerInterval()
264         {
265                 return m_trigger_interval;
266         }
267         virtual u32 getTriggerChance()
268         {
269                 return m_trigger_chance;
270         }
271         virtual bool getSimpleCatchUp()
272         {
273                 return m_simple_catch_up;
274         }
275         virtual s16 getMinY()
276         {
277                 return m_min_y;
278         }
279         virtual s16 getMaxY()
280         {
281                 return m_max_y;
282         }
283         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
284                         u32 active_object_count, u32 active_object_count_wider);
285 };
286
287 class LuaLBM : public LoadingBlockModifierDef
288 {
289 private:
290         int m_id;
291 public:
292         LuaLBM(lua_State *L, int id,
293                         const std::set<std::string> &trigger_contents,
294                         const std::string &name,
295                         bool run_at_every_load):
296                 m_id(id)
297         {
298                 this->run_at_every_load = run_at_every_load;
299                 this->trigger_contents = trigger_contents;
300                 this->name = name;
301         }
302         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n);
303 };
304
305 //! Lua wrapper for RaycastState objects
306 class LuaRaycast : public ModApiBase
307 {
308 private:
309         static const char className[];
310         static const luaL_Reg methods[];
311         //! Inner state
312         RaycastState state;
313
314         // Exported functions
315
316         // garbage collector
317         static int gc_object(lua_State *L);
318
319         /*!
320          * Raycast:next() -> pointed_thing
321          * Returns the next pointed thing on the ray.
322          */
323         static int l_next(lua_State *L);
324 public:
325         //! Constructor with the same arguments as RaycastState.
326         LuaRaycast(
327                 const core::line3d<f32> &shootline,
328                 bool objects_pointable,
329                 bool liquids_pointable) :
330                 state(shootline, objects_pointable, liquids_pointable)
331         {}
332
333         //! Creates a LuaRaycast and leaves it on top of the stack.
334         static int create_object(lua_State *L);
335
336         /*!
337          * Returns the Raycast from the stack or throws an error.
338          * @param narg location of the RaycastState in the stack
339          */
340         static LuaRaycast *checkobject(lua_State *L, int narg);
341
342         //! Registers Raycast as a Lua userdata type.
343         static void Register(lua_State *L);
344 };
345
346 struct ScriptCallbackState {
347         ServerScripting *script;
348         int callback_ref;
349         int args_ref;
350         unsigned int refcount;
351         std::string origin;
352 };