2014-10-30 03:47:08 +01:00
|
|
|
--- Lua code execution functions.
|
|
|
|
-- @module worldedit.code
|
2013-01-13 00:20:41 +01:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Executes `code` as a Lua chunk in the global namespace.
|
|
|
|
-- @return An error message if the code fails, or nil on success.
|
2024-03-25 15:44:34 +01:00
|
|
|
function worldedit.lua(code, name)
|
2024-03-28 17:02:13 +01:00
|
|
|
if string.sub(code,1,1)=="=" then
|
|
|
|
code="return "..string.sub(code,2)
|
|
|
|
end
|
2024-03-25 15:44:34 +01:00
|
|
|
local factory, err = loadstring("return function(p) " .. code .. " end")
|
|
|
|
if not factory then -- Syntax error
|
|
|
|
return false, err
|
2013-01-13 00:20:41 +01:00
|
|
|
end
|
2024-03-25 15:44:34 +01:00
|
|
|
local func=factory()
|
|
|
|
local player=minetest.get_player_by_name(name)
|
|
|
|
local p={name=name, player=player}
|
|
|
|
if player then
|
|
|
|
p["pos"]=vector.round(player:get_pos())
|
2013-01-13 00:20:41 +01:00
|
|
|
end
|
2024-03-25 15:44:34 +01:00
|
|
|
local good, err = pcall(func, p)
|
|
|
|
if good then
|
|
|
|
err=dump(err)
|
|
|
|
end
|
|
|
|
return good, err
|
2013-01-13 00:20:41 +01:00
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
|
|
|
|
--- Executes `code` as a Lua chunk in the global namespace with the variable
|
2014-07-20 19:42:57 +02:00
|
|
|
-- pos available, for each node in a region defined by positions `pos1` and
|
2014-10-30 03:47:08 +01:00
|
|
|
-- `pos2`.
|
|
|
|
-- @return An error message if the code fails, or nil on success.
|
|
|
|
function worldedit.luatransform(pos1, pos2, code)
|
2014-07-20 19:42:57 +02:00
|
|
|
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
2013-01-13 00:20:41 +01:00
|
|
|
|
2014-07-20 19:42:57 +02:00
|
|
|
local factory, err = loadstring("return function(pos) " .. code .. " end")
|
|
|
|
if not factory then -- Syntax error
|
|
|
|
return err
|
2013-01-13 00:20:41 +01:00
|
|
|
end
|
2014-07-20 19:42:57 +02:00
|
|
|
local func = factory()
|
2013-01-13 00:20:41 +01:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
worldedit.keep_loaded(pos1, pos2)
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2023-06-09 14:28:29 +02:00
|
|
|
local pos = vector.new(pos1.x, 0, 0)
|
2013-01-13 00:20:41 +01:00
|
|
|
while pos.x <= pos2.x do
|
|
|
|
pos.y = pos1.y
|
|
|
|
while pos.y <= pos2.y do
|
|
|
|
pos.z = pos1.z
|
|
|
|
while pos.z <= pos2.z do
|
2014-07-20 19:42:57 +02:00
|
|
|
local good, err = pcall(func, pos)
|
|
|
|
if not good then -- Runtime error
|
|
|
|
return err
|
2013-01-13 00:20:41 +01:00
|
|
|
end
|
|
|
|
pos.z = pos.z + 1
|
|
|
|
end
|
|
|
|
pos.y = pos.y + 1
|
|
|
|
end
|
|
|
|
pos.x = pos.x + 1
|
|
|
|
end
|
|
|
|
return nil
|
2014-07-20 19:42:57 +02:00
|
|
|
end
|
|
|
|
|