1 worldedit = worldedit or {}
\r
2 local minetest = minetest -- local copy of global
\r
4 -- Copies and modifies positions `pos1` and `pos2` so that each component of
\r
5 -- `pos1` is less than or equal to the corresponding component of `pos2`.
\r
6 -- Returns the new positions.
\r
7 worldedit.sort_pos = function(pos1, pos2)
\r
8 pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
\r
9 pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
\r
10 if pos1.x > pos2.x then
\r
11 pos2.x, pos1.x = pos1.x, pos2.x
\r
13 if pos1.y > pos2.y then
\r
14 pos2.y, pos1.y = pos1.y, pos2.y
\r
16 if pos1.z > pos2.z then
\r
17 pos2.z, pos1.z = pos1.z, pos2.z
\r
22 -- Executes `code` as a Lua chunk in the global namespace,
\r
23 -- returning an error if the code fails, or nil otherwise.
\r
24 worldedit.lua = function(code)
\r
25 local func, err = loadstring(code)
\r
26 if not func then -- Syntax error
\r
29 local good, err = pcall(operation)
\r
30 if not good then -- Runtime error
\r
36 -- Executes `code` as a Lua chunk in the global namespace with the variable
\r
37 -- pos available, for each node in a region defined by positions `pos1` and
\r
38 -- `pos2`, returning an error if the code fails, or nil otherwise
\r
39 worldedit.luatransform = function(pos1, pos2, code)
\r
40 pos1, pos2 = worldedit.sort_pos(pos1, pos2)
\r
42 local factory, err = loadstring("return function(pos) " .. code .. " end")
\r
43 if not factory then -- Syntax error
\r
46 local func = factory()
\r
49 local manip = minetest.get_voxel_manip()
\r
50 manip:read_from_map(pos1, pos2)
\r
52 local pos = {x=pos1.x, y=0, z=0}
\r
53 while pos.x <= pos2.x do
\r
55 while pos.y <= pos2.y do
\r
57 while pos.z <= pos2.z do
\r
58 local good, err = pcall(func, pos)
\r
59 if not good then -- Runtime error
\r