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