2012-04-01 11:37:41 +02:00
|
|
|
-- Minetest: builtin/misc_helpers.lua
|
|
|
|
|
|
|
|
function basic_dump2(o)
|
|
|
|
if type(o) == "number" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "string" then
|
|
|
|
return string.format("%q", o)
|
|
|
|
elseif type(o) == "boolean" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "function" then
|
|
|
|
return "<function>"
|
|
|
|
elseif type(o) == "userdata" then
|
|
|
|
return "<userdata>"
|
|
|
|
elseif type(o) == "nil" then
|
|
|
|
return "nil"
|
|
|
|
else
|
|
|
|
error("cannot dump a " .. type(o))
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function dump2(o, name, dumped)
|
|
|
|
name = name or "_"
|
|
|
|
dumped = dumped or {}
|
|
|
|
io.write(name, " = ")
|
|
|
|
if type(o) == "number" or type(o) == "string" or type(o) == "boolean"
|
|
|
|
or type(o) == "function" or type(o) == "nil"
|
|
|
|
or type(o) == "userdata" then
|
|
|
|
io.write(basic_dump2(o), "\n")
|
|
|
|
elseif type(o) == "table" then
|
|
|
|
if dumped[o] then
|
|
|
|
io.write(dumped[o], "\n")
|
|
|
|
else
|
|
|
|
dumped[o] = name
|
|
|
|
io.write("{}\n") -- new table
|
|
|
|
for k,v in pairs(o) do
|
|
|
|
local fieldname = string.format("%s[%s]", name, basic_dump2(k))
|
|
|
|
dump2(v, fieldname, dumped)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error("cannot dump a " .. type(o))
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function dump(o, dumped)
|
|
|
|
dumped = dumped or {}
|
|
|
|
if type(o) == "number" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "string" then
|
|
|
|
return string.format("%q", o)
|
|
|
|
elseif type(o) == "table" then
|
|
|
|
if dumped[o] then
|
|
|
|
return "<circular reference>"
|
|
|
|
end
|
|
|
|
dumped[o] = true
|
|
|
|
local t = {}
|
|
|
|
for k,v in pairs(o) do
|
2013-06-16 08:06:41 +02:00
|
|
|
t[#t+1] = "[" .. dump(k, dumped) .. "] = " .. dump(v, dumped)
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
return "{" .. table.concat(t, ", ") .. "}"
|
|
|
|
elseif type(o) == "boolean" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "function" then
|
|
|
|
return "<function>"
|
|
|
|
elseif type(o) == "userdata" then
|
|
|
|
return "<userdata>"
|
|
|
|
elseif type(o) == "nil" then
|
|
|
|
return "nil"
|
|
|
|
else
|
|
|
|
error("cannot dump a " .. type(o))
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function string:split(sep)
|
|
|
|
local sep, fields = sep or ",", {}
|
|
|
|
local pattern = string.format("([^%s]+)", sep)
|
|
|
|
self:gsub(pattern, function(c) fields[#fields+1] = c end)
|
|
|
|
return fields
|
|
|
|
end
|
|
|
|
|
|
|
|
function string:trim()
|
|
|
|
return (self:gsub("^%s*(.-)%s*$", "%1"))
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
|
|
|
|
|
|
|
|
function minetest.pos_to_string(pos)
|
|
|
|
return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
|
|
|
|
end
|
|
|
|
|
2013-07-07 18:02:11 +02:00
|
|
|
function math.hypot(x, y)
|
|
|
|
local t
|
|
|
|
x = math.abs(x)
|
|
|
|
y = math.abs(y)
|
|
|
|
t = math.min(x, y)
|
|
|
|
x = math.max(x, y)
|
|
|
|
if x == 0 then return 0 end
|
|
|
|
t = t / x
|
|
|
|
return x * math.sqrt(1 + t * t)
|
|
|
|
end
|
2012-04-01 11:37:41 +02:00
|
|
|
|