2015-09-01 17:15:24 +02:00
|
|
|
--
|
|
|
|
-- Utility functions
|
|
|
|
--
|
|
|
|
|
|
|
|
util = {}
|
|
|
|
|
|
|
|
function util.sort_pos(pos1, pos2)
|
|
|
|
-- function taken from worldedit
|
|
|
|
-- ensure that pos2 has greater coords than pos1
|
|
|
|
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
|
|
|
|
pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
|
|
|
|
if pos1.x > pos2.x then
|
|
|
|
pos2.x, pos1.x = pos1.x, pos2.x
|
|
|
|
end
|
|
|
|
if pos1.y > pos2.y then
|
|
|
|
pos2.y, pos1.y = pos1.y, pos2.y
|
|
|
|
end
|
|
|
|
if pos1.z > pos2.z then
|
|
|
|
pos2.z, pos1.z = pos1.z, pos2.z
|
|
|
|
end
|
|
|
|
return pos1, pos2
|
|
|
|
end
|
|
|
|
|
|
|
|
function util.fixlight(pos1, pos2)
|
|
|
|
-- function taken from worldedit
|
|
|
|
-- repair most lighting in a block
|
|
|
|
local pos1, pos2 = util.sort_pos(pos1, pos2)
|
|
|
|
|
|
|
|
--make area stay loaded
|
|
|
|
|
2017-05-12 01:45:53 +02:00
|
|
|
local manip = core.get_voxel_manip()
|
2015-09-01 17:15:24 +02:00
|
|
|
manip:read_from_map(pos1, pos2)
|
|
|
|
|
2017-05-12 01:45:53 +02:00
|
|
|
local nodes = core.find_nodes_in_area(pos1, pos2, "air")
|
|
|
|
local dig_node = core.dig_node
|
2015-09-01 17:15:24 +02:00
|
|
|
for _, pos in ipairs(nodes) do
|
|
|
|
dig_node(pos)
|
|
|
|
end
|
|
|
|
|
2015-10-20 23:10:44 +02:00
|
|
|
manip:write_to_map()
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
return #nodes
|
|
|
|
end
|
2017-05-12 01:45:53 +02:00
|
|
|
if core.setting_getbool("fixlight_command_enable") then
|
|
|
|
core.register_privilege("fixlight", "Can use /fixlight command")
|
2015-10-20 21:44:42 +02:00
|
|
|
|
2017-05-12 01:45:53 +02:00
|
|
|
core.register_chatcommand(
|
2015-10-20 20:44:38 +02:00
|
|
|
"fixlight",
|
|
|
|
{
|
|
|
|
params = "[radius 1-20]",
|
|
|
|
description = "Fix light in a radius around you",
|
2015-10-20 21:44:42 +02:00
|
|
|
privs = {fixlight = true},
|
2015-10-20 20:44:38 +02:00
|
|
|
func = function(name, param)
|
|
|
|
local rad = tonumber(param)
|
|
|
|
|
|
|
|
if rad == nil or (rad < 1 or rad > 20) then
|
|
|
|
return false, "Bad param for /fixlight; type /help fixlight"
|
|
|
|
end
|
|
|
|
|
2017-05-12 01:45:53 +02:00
|
|
|
local player = core.get_player_by_name(name)
|
2015-10-20 20:44:38 +02:00
|
|
|
|
|
|
|
local pos = player:getpos()
|
|
|
|
pos.x = math.floor(pos.x + 0.5)
|
|
|
|
pos.y = math.floor(pos.y + 0.5)
|
|
|
|
pos.z = math.floor(pos.z + 0.5)
|
|
|
|
|
|
|
|
local minp = {x = pos.x - rad, y = pos.y - rad, z = pos.z - rad}
|
|
|
|
local maxp = {x = pos.x + rad, y = pos.y + rad, z = pos.z + rad}
|
|
|
|
|
|
|
|
util.fixlight(minp, maxp)
|
|
|
|
|
2017-05-12 01:45:53 +02:00
|
|
|
core.chat_send_all("*** " .. name .. " has fixed light in a " .. rad .. "m radius at (" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")")
|
2015-10-20 20:44:38 +02:00
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
function util.nodefunc(pos1, pos2, name, func, nomanip)
|
2015-09-01 17:15:24 +02:00
|
|
|
-- function based off fixlight
|
|
|
|
-- call a function for every node of a single type
|
|
|
|
local pos1, pos2 = util.sort_pos(pos1, pos2)
|
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
if not nomanip then
|
2017-05-12 01:45:53 +02:00
|
|
|
local manip = core.get_voxel_manip()
|
2015-10-23 01:27:35 +02:00
|
|
|
manip:read_from_map(pos1, pos2)
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-12 01:45:53 +02:00
|
|
|
local nodes = core.find_nodes_in_area(pos1, pos2, name)
|
2015-09-01 17:15:24 +02:00
|
|
|
for _, pos in ipairs(nodes) do
|
|
|
|
func(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function util.getvoxelmanip(pos1, pos2)
|
|
|
|
-- function based off fixlight
|
|
|
|
-- return a voxel manipulator
|
|
|
|
local pos1, pos2 = util.sort_pos(pos1, pos2)
|
|
|
|
|
2017-05-12 01:45:53 +02:00
|
|
|
local manip = core.get_voxel_manip()
|
2015-09-01 17:15:24 +02:00
|
|
|
manip:read_from_map(pos1, pos2)
|
|
|
|
|
|
|
|
return manip
|
|
|
|
end
|
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
function util.remove_area(pos1, pos2, nomanip)
|
2015-09-01 17:15:24 +02:00
|
|
|
-- function based off fixlight
|
|
|
|
-- call a function for every node of a single type
|
|
|
|
local pos1, pos2 = util.sort_pos(pos1, pos2)
|
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
if not nomanip then
|
2017-05-12 01:45:53 +02:00
|
|
|
local manip = core.get_voxel_manip()
|
2015-10-23 01:27:35 +02:00
|
|
|
manip:read_from_map(pos1, pos2)
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
for i = pos1.x, pos2.x-1 do
|
|
|
|
for j = pos1.y, pos2.y-1 do
|
|
|
|
for k = pos1.z, pos2.z-1 do
|
2017-05-12 01:45:53 +02:00
|
|
|
core.remove_node({x = i, y = j, z = k})
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-02 22:58:37 +02:00
|
|
|
|
|
|
|
manip:write_to_map()
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
function util.areafunc(pos1, pos2, func, nomanip)
|
2015-09-01 17:15:24 +02:00
|
|
|
-- function based off fixlight
|
|
|
|
-- call a function for every node of a single type
|
|
|
|
local pos1, pos2 = util.sort_pos(pos1, pos2)
|
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
if not nomanip then
|
2017-05-12 01:45:53 +02:00
|
|
|
local manip = core.get_voxel_manip()
|
2015-10-23 01:27:35 +02:00
|
|
|
manip:read_from_map(pos1, pos2)
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
for i = pos1.x, pos2.x-1 do
|
|
|
|
for j = pos1.y, pos2.y-1 do
|
|
|
|
for k = pos1.z, pos2.z-1 do
|
|
|
|
func(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
function util.reconstruct(pos1, pos2, nomanip)
|
2015-09-01 17:15:24 +02:00
|
|
|
-- function based off fixlight
|
|
|
|
-- force a re-construction of the nodes in an area, for fixing missing metadata in schematics
|
|
|
|
local pos1, pos2 = util.sort_pos(pos1, pos2)
|
|
|
|
|
2015-10-23 01:27:35 +02:00
|
|
|
if not nomanip then
|
2017-05-12 01:45:53 +02:00
|
|
|
local manip = core.get_voxel_manip()
|
2015-10-23 01:27:35 +02:00
|
|
|
manip:read_from_map(pos1, pos2)
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
-- fix chests
|
2017-05-12 01:45:53 +02:00
|
|
|
local nodes = core.find_nodes_in_area(pos1, pos2, "default:chest")
|
|
|
|
local node = core.registered_nodes["default:chest"]
|
2015-09-01 17:15:24 +02:00
|
|
|
for _, pos in ipairs(nodes) do
|
|
|
|
node.on_construct(pos)
|
|
|
|
end
|
2015-10-02 22:58:37 +02:00
|
|
|
|
|
|
|
-- fix music players
|
2017-05-12 01:45:53 +02:00
|
|
|
nodes = core.find_nodes_in_area(pos1, pos2, "music:player")
|
|
|
|
node = core.registered_nodes["music:player"]
|
2015-10-02 22:58:37 +02:00
|
|
|
for _, pos in ipairs(nodes) do
|
2015-09-01 17:15:24 +02:00
|
|
|
node.on_construct(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- fix furnaces
|
2017-05-12 01:45:53 +02:00
|
|
|
nodes = core.find_nodes_in_area(pos1, pos2, "default:furnace")
|
|
|
|
node = core.registered_nodes["default:furnace"]
|
2015-10-02 22:58:37 +02:00
|
|
|
for _, pos in ipairs(nodes) do
|
2015-09-01 17:15:24 +02:00
|
|
|
node.on_construct(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function util.choice(tab, pr)
|
|
|
|
-- return a random index of the given table
|
|
|
|
|
|
|
|
local choices = {}
|
|
|
|
|
|
|
|
for n, _ in pairs(tab) do
|
|
|
|
table.insert(choices, n)
|
|
|
|
end
|
|
|
|
|
|
|
|
if #choices <= 0 then return end
|
|
|
|
|
2015-09-02 19:44:07 +02:00
|
|
|
if pr then
|
|
|
|
return choices[pr:next(1, #choices)]
|
|
|
|
else
|
|
|
|
return choices[math.random(1, #choices)]
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function util.choice_element(tab, pr)
|
|
|
|
-- return a random element of the given table
|
|
|
|
|
|
|
|
local choices = {}
|
|
|
|
|
|
|
|
for _,n in pairs(tab) do
|
|
|
|
table.insert(choices, n)
|
|
|
|
end
|
|
|
|
|
|
|
|
if #choices <= 0 then return end
|
|
|
|
|
2015-09-02 19:44:07 +02:00
|
|
|
if pr then
|
|
|
|
return choices[pr:next(1, #choices)]
|
|
|
|
else
|
|
|
|
return choices[math.random(1, #choices)]
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
2015-10-17 01:36:39 +02:00
|
|
|
-- util.split function taken from a StackOverflow answer.
|
|
|
|
-- http://stackoverflow.com/questions/12709205/split-a-string-and-store-in-an-array-in-lua
|
|
|
|
function util.split(str, tok)
|
|
|
|
-- Source: http://lua-users.org/wiki/MakingLuaLikePhp
|
|
|
|
-- Credit: http://richard.warburton.it/
|
|
|
|
|
|
|
|
if not tok then return {} end
|
|
|
|
|
|
|
|
local pos = 0
|
|
|
|
local arr = {}
|
|
|
|
|
|
|
|
for st, sp in function() return string.find(str, tok, pos, true) end do
|
|
|
|
table.insert(arr, string.sub(str, pos, st - 1))
|
|
|
|
pos = sp + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(arr, string.sub(str, pos))
|
|
|
|
|
|
|
|
return arr
|
|
|
|
end
|