3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "lua_api/l_base.h"
23 #include "serverenvironment.h"
26 class ModApiEnvMod : public ModApiBase {
28 // set_node(pos, node)
29 // pos = {x=num, y=num, z=num}
30 static int l_set_node(lua_State *L);
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);
36 static int l_add_node(lua_State *L);
39 // pos = {x=num, y=num, z=num}
40 static int l_remove_node(lua_State *L);
42 // swap_node(pos, node)
43 // pos = {x=num, y=num, z=num}
44 static int l_swap_node(lua_State *L);
47 // pos = {x=num, y=num, z=num}
48 static int l_get_node(lua_State *L);
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);
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);
59 // place_node(pos, node)
60 // pos = {x=num, y=num, z=num}
61 static int l_place_node(lua_State *L);
64 // pos = {x=num, y=num, z=num}
65 static int l_dig_node(lua_State *L);
68 // pos = {x=num, y=num, z=num}
69 static int l_punch_node(lua_State *L);
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);
75 // get_node_level(pos)
76 // pos = {x=num, y=num, z=num}
77 static int l_get_node_level(lua_State *L);
79 // set_node_level(pos)
80 // pos = {x=num, y=num, z=num}
81 static int l_set_node_level(lua_State *L);
83 // add_node_level(pos)
84 // pos = {x=num, y=num, z=num}
85 static int l_add_node_level(lua_State *L);
87 // find_nodes_with_meta(pos1, pos2)
88 static int l_find_nodes_with_meta(lua_State *L);
91 static int l_get_meta(lua_State *L);
93 // get_node_timer(pos)
94 static int l_get_node_timer(lua_State *L);
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);
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);
104 // get_player_by_name(name)
105 static int l_get_player_by_name(lua_State *L);
107 // get_objects_inside_radius(pos, radius)
108 static int l_get_objects_inside_radius(lua_State *L);
110 // set_timeofday(val)
112 static int l_set_timeofday(lua_State *L);
114 // get_timeofday() -> 0...1
115 static int l_get_timeofday(lua_State *L);
118 static int l_get_gametime(lua_State *L);
120 // get_day_count() -> int
121 static int l_get_day_count(lua_State *L);
123 // find_node_near(pos, radius, nodenames, search_center) -> pos or nil
124 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
125 static int l_find_node_near(lua_State *L);
127 // find_nodes_in_area(minp, maxp, nodenames) -> list of positions
128 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
129 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);
135 // fix_light(p1, p2) -> true/false
136 static int l_fix_light(lua_State *L);
138 // emerge_area(p1, p2)
139 static int l_emerge_area(lua_State *L);
141 // delete_area(p1, p2) -> true/false
142 static int l_delete_area(lua_State *L);
144 // get_perlin(seeddiff, octaves, persistence, scale)
145 // returns world-specific PerlinNoise
146 static int l_get_perlin(lua_State *L);
148 // get_perlin_map(noiseparams, size)
149 // returns world-specific PerlinNoiseMap
150 static int l_get_perlin_map(lua_State *L);
153 // returns world-specific voxel manipulator
154 static int l_get_voxel_manip(lua_State *L);
157 // clear all objects in the environment
158 static int l_clear_objects(lua_State *L);
160 // spawn_tree(pos, treedef)
161 static int l_spawn_tree(lua_State *L);
163 // line_of_sight(pos1, pos2) -> true/false
164 static int l_line_of_sight(lua_State *L);
166 // raycast(pos1, pos2, objects, liquids) -> Raycast
167 static int l_raycast(lua_State *L);
169 // find_path(pos1, pos2, searchdistance,
170 // max_jump, max_drop, algorithm) -> table containing path
171 static int l_find_path(lua_State *L);
173 // transforming_liquid_add(pos)
174 static int l_transforming_liquid_add(lua_State *L);
176 // forceload_block(blockpos)
177 // forceloads a block
178 static int l_forceload_block(lua_State *L);
180 // forceload_free_block(blockpos)
181 // stops forceloading a position
182 static int l_forceload_free_block(lua_State *L);
185 static void Initialize(lua_State *L, int top);
186 static void InitializeClient(lua_State *L, int top);
188 static struct EnumString es_ClearObjectsMode[];
191 class LuaABM : public ActiveBlockModifier {
195 std::vector<std::string> m_trigger_contents;
196 std::vector<std::string> m_required_neighbors;
197 float m_trigger_interval;
198 u32 m_trigger_chance;
199 bool m_simple_catch_up;
201 LuaABM(lua_State *L, int id,
202 const std::vector<std::string> &trigger_contents,
203 const std::vector<std::string> &required_neighbors,
204 float trigger_interval, u32 trigger_chance, bool simple_catch_up):
206 m_trigger_contents(trigger_contents),
207 m_required_neighbors(required_neighbors),
208 m_trigger_interval(trigger_interval),
209 m_trigger_chance(trigger_chance),
210 m_simple_catch_up(simple_catch_up)
213 virtual const std::vector<std::string> &getTriggerContents() const
215 return m_trigger_contents;
217 virtual const std::vector<std::string> &getRequiredNeighbors() const
219 return m_required_neighbors;
221 virtual float getTriggerInterval()
223 return m_trigger_interval;
225 virtual u32 getTriggerChance()
227 return m_trigger_chance;
229 virtual bool getSimpleCatchUp()
231 return m_simple_catch_up;
233 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
234 u32 active_object_count, u32 active_object_count_wider);
237 class LuaLBM : public LoadingBlockModifierDef
242 LuaLBM(lua_State *L, int id,
243 const std::set<std::string> &trigger_contents,
244 const std::string &name,
245 bool run_at_every_load):
248 this->run_at_every_load = run_at_every_load;
249 this->trigger_contents = trigger_contents;
252 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n);
255 //! Lua wrapper for RaycastState objects
256 class LuaRaycast : public ModApiBase
259 static const char className[];
260 static const luaL_Reg methods[];
264 // Exported functions
267 static int gc_object(lua_State *L);
270 * Raycast:next() -> pointed_thing
271 * Returns the next pointed thing on the ray.
273 static int l_next(lua_State *L);
275 //! Constructor with the same arguments as RaycastState.
277 const core::line3d<f32> &shootline,
278 bool objects_pointable,
279 bool liquids_pointable) :
280 state(shootline, objects_pointable, liquids_pointable)
283 //! Creates a LuaRaycast and leaves it on top of the stack.
284 static int create_object(lua_State *L);
287 * Returns the Raycast from the stack or throws an error.
288 * @param narg location of the RaycastState in the stack
290 static LuaRaycast *checkobject(lua_State *L, int narg);
292 //! Registers Raycast as a Lua userdata type.
293 static void Register(lua_State *L);
296 struct ScriptCallbackState {
297 ServerScripting *script;
300 unsigned int refcount;