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 // 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);
64 // place_node(pos, node)
65 // pos = {x=num, y=num, z=num}
66 static int l_place_node(lua_State *L);
69 // pos = {x=num, y=num, z=num}
70 static int l_dig_node(lua_State *L);
73 // pos = {x=num, y=num, z=num}
74 static int l_punch_node(lua_State *L);
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);
80 // get_node_level(pos)
81 // pos = {x=num, y=num, z=num}
82 static int l_get_node_level(lua_State *L);
84 // set_node_level(pos)
85 // pos = {x=num, y=num, z=num}
86 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);
92 // find_nodes_with_meta(pos1, pos2)
93 static int l_find_nodes_with_meta(lua_State *L);
96 static int l_get_meta(lua_State *L);
98 // get_node_timer(pos)
99 static int l_get_node_timer(lua_State *L);
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);
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);
109 // get_connected_players()
110 static int l_get_connected_players(lua_State *L);
112 // get_player_by_name(name)
113 static int l_get_player_by_name(lua_State *L);
115 // get_objects_inside_radius(pos, radius)
116 static int l_get_objects_inside_radius(lua_State *L);
118 // set_timeofday(val)
120 static int l_set_timeofday(lua_State *L);
122 // get_timeofday() -> 0...1
123 static int l_get_timeofday(lua_State *L);
126 static int l_get_gametime(lua_State *L);
128 // get_day_count() -> int
129 static int l_get_day_count(lua_State *L);
131 // find_node_near(pos, radius, nodenames, search_center) -> pos or nil
132 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
133 static int l_find_node_near(lua_State *L);
135 // find_nodes_in_area(minp, maxp, nodenames) -> list of positions
136 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
137 static int l_find_nodes_in_area(lua_State *L);
139 // find_surface_nodes_in_area(minp, maxp, nodenames) -> list of positions
140 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
141 static int l_find_nodes_in_area_under_air(lua_State *L);
143 // fix_light(p1, p2) -> true/false
144 static int l_fix_light(lua_State *L);
147 static int l_load_area(lua_State *L);
149 // emerge_area(p1, p2)
150 static int l_emerge_area(lua_State *L);
152 // delete_area(p1, p2) -> true/false
153 static int l_delete_area(lua_State *L);
155 // get_perlin(seeddiff, octaves, persistence, scale)
156 // returns world-specific PerlinNoise
157 static int l_get_perlin(lua_State *L);
159 // get_perlin_map(noiseparams, size)
160 // returns world-specific PerlinNoiseMap
161 static int l_get_perlin_map(lua_State *L);
164 // returns world-specific voxel manipulator
165 static int l_get_voxel_manip(lua_State *L);
168 // clear all objects in the environment
169 static int l_clear_objects(lua_State *L);
171 // spawn_tree(pos, treedef)
172 static int l_spawn_tree(lua_State *L);
174 // line_of_sight(pos1, pos2) -> true/false
175 static int l_line_of_sight(lua_State *L);
177 // raycast(pos1, pos2, objects, liquids) -> Raycast
178 static int l_raycast(lua_State *L);
180 // find_path(pos1, pos2, searchdistance,
181 // max_jump, max_drop, algorithm) -> table containing path
182 static int l_find_path(lua_State *L);
184 // transforming_liquid_add(pos)
185 static int l_transforming_liquid_add(lua_State *L);
187 // forceload_block(blockpos)
188 // forceloads a block
189 static int l_forceload_block(lua_State *L);
191 // forceload_free_block(blockpos)
192 // stops forceloading a position
193 static int l_forceload_free_block(lua_State *L);
195 // Get a string translated server side
196 static int l_get_translated_string(lua_State * L);
200 static void collectNodeIds(lua_State *L, int idx,
201 const NodeDefManager *ndef, std::vector<content_t> &filter);
204 static void Initialize(lua_State *L, int top);
205 static void InitializeClient(lua_State *L, int top);
207 static struct EnumString es_ClearObjectsMode[];
210 class LuaABM : public ActiveBlockModifier {
214 std::vector<std::string> m_trigger_contents;
215 std::vector<std::string> m_required_neighbors;
216 float m_trigger_interval;
217 u32 m_trigger_chance;
218 bool m_simple_catch_up;
220 LuaABM(lua_State *L, int id,
221 const std::vector<std::string> &trigger_contents,
222 const std::vector<std::string> &required_neighbors,
223 float trigger_interval, u32 trigger_chance, bool simple_catch_up):
225 m_trigger_contents(trigger_contents),
226 m_required_neighbors(required_neighbors),
227 m_trigger_interval(trigger_interval),
228 m_trigger_chance(trigger_chance),
229 m_simple_catch_up(simple_catch_up)
232 virtual const std::vector<std::string> &getTriggerContents() const
234 return m_trigger_contents;
236 virtual const std::vector<std::string> &getRequiredNeighbors() const
238 return m_required_neighbors;
240 virtual float getTriggerInterval()
242 return m_trigger_interval;
244 virtual u32 getTriggerChance()
246 return m_trigger_chance;
248 virtual bool getSimpleCatchUp()
250 return m_simple_catch_up;
252 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
253 u32 active_object_count, u32 active_object_count_wider);
256 class LuaLBM : public LoadingBlockModifierDef
261 LuaLBM(lua_State *L, int id,
262 const std::set<std::string> &trigger_contents,
263 const std::string &name,
264 bool run_at_every_load):
267 this->run_at_every_load = run_at_every_load;
268 this->trigger_contents = trigger_contents;
271 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n);
274 //! Lua wrapper for RaycastState objects
275 class LuaRaycast : public ModApiBase
278 static const char className[];
279 static const luaL_Reg methods[];
283 // Exported functions
286 static int gc_object(lua_State *L);
289 * Raycast:next() -> pointed_thing
290 * Returns the next pointed thing on the ray.
292 static int l_next(lua_State *L);
294 //! Constructor with the same arguments as RaycastState.
296 const core::line3d<f32> &shootline,
297 bool objects_pointable,
298 bool liquids_pointable) :
299 state(shootline, objects_pointable, liquids_pointable)
302 //! Creates a LuaRaycast and leaves it on top of the stack.
303 static int create_object(lua_State *L);
306 * Returns the Raycast from the stack or throws an error.
307 * @param narg location of the RaycastState in the stack
309 static LuaRaycast *checkobject(lua_State *L, int narg);
311 //! Registers Raycast as a Lua userdata type.
312 static void Register(lua_State *L);
315 struct ScriptCallbackState {
316 ServerScripting *script;
319 unsigned int refcount;