milan2018_v3 #28

Closed
milan wants to merge 66 commits from milan2018_v3 into master
Showing only changes of commit 28e0dac6f0 - Show all commits

View file

@ -1,52 +1,59 @@
--- Lua code execution functions. --- Lua code execution functions.
-- @module worldedit.code -- @module worldedit.code
--- Executes `code` as a Lua chunk in the global namespace. --- Executes `code` as a Lua chunk in the global namespace.
-- @return An error message if the code fails, or nil on success. -- @return An error message if the code fails, or nil on success.
function worldedit.lua(code) function worldedit.lua(code)
local func, err = loadstring(code) local func, err = loadstring(code)
if not func then -- Syntax error if not func then -- Syntax error
return err return err
end end
local good, err = pcall(func) local good, err = pcall(func)
if not good then -- Runtime error if not good then -- Runtime error
return err return err
end end
return nil return nil
end end
--- Executes `code` as a Lua chunk in the global namespace with the variable --- Executes `code` as a Lua chunk in the global namespace with the variable
-- pos available, for each node in a region defined by positions `pos1` and -- pos available, for each node in a region defined by positions `pos1` and
-- `pos2`. -- `pos2`.
-- @return An error message if the code fails, or nil on success. -- @return An error message if the code fails, or nil on success.
function worldedit.luatransform(pos1, pos2, code) function worldedit.luatransform(pos1, pos2, code)
pos1, pos2 = worldedit.sort_pos(pos1, pos2) pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local factory, err = loadstring("return function(pos) " .. code .. " end") local factory, err = loadstring("return function(pos) " .. code .. " end")
if not factory then -- Syntax error if not factory then -- Syntax error
return err return err
end end
local func = factory() local func = factory()
worldedit.keep_loaded(pos1, pos2) worldedit.keep_loaded(pos1, pos2)
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local good, err = pcall(func, pos) local good, err = pcall(func, pos)
if not good then -- Runtime error if not good then -- Runtime error
return err return err
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
end end
pos.y = pos.y + 1 pos.y = pos.y + 1
end end
pos.x = pos.x + 1 pos.x = pos.x + 1
end end
return nil return nil
end end
local input = io.open(minetest.get_worldpath().."/init.lua", "r")
if input then
local code = input:read("*a")
input:close()
worldedit.lua(code)
end