2012-04-01 05:37:41 -04:00
|
|
|
-- Minetest: builtin/misc_helpers.lua
|
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2014-05-08 23:56:36 -04:00
|
|
|
function basic_dump(o)
|
|
|
|
local tp = type(o)
|
|
|
|
if tp == "number" then
|
2012-04-01 05:37:41 -04:00
|
|
|
return tostring(o)
|
2014-05-08 23:56:36 -04:00
|
|
|
elseif tp == "string" then
|
2012-04-01 05:37:41 -04:00
|
|
|
return string.format("%q", o)
|
2014-05-08 23:56:36 -04:00
|
|
|
elseif tp == "boolean" then
|
2012-04-01 05:37:41 -04:00
|
|
|
return tostring(o)
|
2014-05-08 23:56:36 -04:00
|
|
|
elseif tp == "nil" then
|
2012-04-01 05:37:41 -04:00
|
|
|
return "nil"
|
2014-05-08 23:56:36 -04:00
|
|
|
-- Uncomment for full function dumping support.
|
|
|
|
-- Not currently enabled because bytecode isn't very human-readable and
|
|
|
|
-- dump's output is intended for humans.
|
|
|
|
--elseif tp == "function" then
|
|
|
|
-- return string.format("loadstring(%q)", string.dump(o))
|
2012-04-01 05:37:41 -04:00
|
|
|
else
|
2014-05-08 23:56:36 -04:00
|
|
|
return string.format("<%s>", tp)
|
2012-04-01 05:37:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2014-05-08 23:56:36 -04:00
|
|
|
-- Dumps values in a line-per-value format.
|
|
|
|
-- For example, {test = {"Testing..."}} becomes:
|
|
|
|
-- _["test"] = {}
|
|
|
|
-- _["test"][1] = "Testing..."
|
|
|
|
-- This handles tables as keys and circular references properly.
|
|
|
|
-- It also handles multiple references well, writing the table only once.
|
|
|
|
-- The dumped argument is internal-only.
|
|
|
|
|
2012-04-01 05:37:41 -04:00
|
|
|
function dump2(o, name, dumped)
|
|
|
|
name = name or "_"
|
2014-05-08 23:56:36 -04:00
|
|
|
-- "dumped" is used to keep track of serialized tables to handle
|
|
|
|
-- multiple references and circular tables properly.
|
|
|
|
-- It only contains tables as keys. The value is the name that
|
|
|
|
-- the table has in the dump, eg:
|
|
|
|
-- {x = {"y"}} -> dumped[{"y"}] = '_["x"]'
|
2012-04-01 05:37:41 -04:00
|
|
|
dumped = dumped or {}
|
2014-05-08 23:56:36 -04:00
|
|
|
if type(o) ~= "table" then
|
|
|
|
return string.format("%s = %s\n", name, basic_dump(o))
|
|
|
|
end
|
|
|
|
if dumped[o] then
|
|
|
|
return string.format("%s = %s\n", name, dumped[o])
|
|
|
|
end
|
|
|
|
dumped[o] = name
|
|
|
|
-- This contains a list of strings to be concatenated later (because
|
|
|
|
-- Lua is slow at individual concatenation).
|
|
|
|
local t = {}
|
|
|
|
for k, v in pairs(o) do
|
|
|
|
local keyStr
|
|
|
|
if type(k) == "table" then
|
|
|
|
if dumped[k] then
|
|
|
|
keyStr = dumped[k]
|
|
|
|
else
|
|
|
|
-- Key tables don't have a name, so use one of
|
|
|
|
-- the form _G["table: 0xFFFFFFF"]
|
|
|
|
keyStr = string.format("_G[%q]", tostring(k))
|
|
|
|
-- Dump key table
|
|
|
|
table.insert(t, dump2(k, keyStr, dumped))
|
2012-04-01 05:37:41 -04:00
|
|
|
end
|
2014-05-08 23:56:36 -04:00
|
|
|
else
|
|
|
|
keyStr = basic_dump(k)
|
2012-04-01 05:37:41 -04:00
|
|
|
end
|
2014-05-08 23:56:36 -04:00
|
|
|
local vname = string.format("%s[%s]", name, keyStr)
|
|
|
|
table.insert(t, dump2(v, vname, dumped))
|
2012-04-01 05:37:41 -04:00
|
|
|
end
|
2014-05-08 23:56:36 -04:00
|
|
|
return string.format("%s = {}\n%s", name, table.concat(t))
|
2012-04-01 05:37:41 -04:00
|
|
|
end
|
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2014-05-08 23:56:36 -04:00
|
|
|
-- This dumps values in a one-line format, like serialize().
|
|
|
|
-- For example, {test = {"Testing..."}} becomes {["test"] = {[1] = "Testing..."}}
|
|
|
|
-- This supports tables as keys, but not circular references.
|
|
|
|
-- It performs poorly with multiple references as it writes out the full
|
|
|
|
-- table each time.
|
|
|
|
-- The dumped argument is internal-only.
|
|
|
|
|
2012-04-01 05:37:41 -04:00
|
|
|
function dump(o, dumped)
|
2014-05-08 23:56:36 -04:00
|
|
|
-- Same as "dumped" in dump2. The difference is that here it can only
|
|
|
|
-- contain boolean (and nil) values since multiple references aren't
|
|
|
|
-- handled properly.
|
2012-04-01 05:37:41 -04:00
|
|
|
dumped = dumped or {}
|
2014-05-08 23:56:36 -04:00
|
|
|
if type(o) == "table" then
|
2012-04-01 05:37:41 -04:00
|
|
|
if dumped[o] then
|
|
|
|
return "<circular reference>"
|
|
|
|
end
|
|
|
|
dumped[o] = true
|
|
|
|
local t = {}
|
2014-05-08 23:56:36 -04:00
|
|
|
for k, v in pairs(o) do
|
|
|
|
k = dump(k, dumped)
|
|
|
|
v = dump(v, dumped)
|
|
|
|
table.insert(t, string.format("[%s] = %s", k, v))
|
2012-04-01 05:37:41 -04:00
|
|
|
end
|
2014-05-08 23:56:36 -04:00
|
|
|
return string.format("{%s}", table.concat(t, ", "))
|
2012-04-01 05:37:41 -04:00
|
|
|
else
|
2014-05-08 23:56:36 -04:00
|
|
|
return basic_dump(o)
|
2012-04-01 05:37:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2012-04-01 05:37:41 -04:00
|
|
|
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
|
|
|
|
|
2013-08-14 13:22:23 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
function file_exists(filename)
|
|
|
|
local f = io.open(filename, "r")
|
|
|
|
if f==nil then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
f:close()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2012-04-01 05:37:41 -04:00
|
|
|
function string:trim()
|
|
|
|
return (self:gsub("^%s*(.-)%s*$", "%1"))
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
|
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2013-07-07 12:02:11 -04: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 05:37:41 -04:00
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
function get_last_folder(text,count)
|
|
|
|
local parts = text:split(DIR_DELIM)
|
|
|
|
|
|
|
|
if count == nil then
|
|
|
|
return parts[#parts]
|
|
|
|
end
|
|
|
|
|
|
|
|
local retval = ""
|
|
|
|
for i=1,count,1 do
|
|
|
|
retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
|
|
|
|
end
|
|
|
|
|
|
|
|
return retval
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
function cleanup_path(temppath)
|
|
|
|
|
|
|
|
local parts = temppath:split("-")
|
|
|
|
temppath = ""
|
|
|
|
for i=1,#parts,1 do
|
|
|
|
if temppath ~= "" then
|
|
|
|
temppath = temppath .. "_"
|
|
|
|
end
|
|
|
|
temppath = temppath .. parts[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
parts = temppath:split(".")
|
|
|
|
temppath = ""
|
|
|
|
for i=1,#parts,1 do
|
|
|
|
if temppath ~= "" then
|
|
|
|
temppath = temppath .. "_"
|
|
|
|
end
|
|
|
|
temppath = temppath .. parts[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
parts = temppath:split("'")
|
|
|
|
temppath = ""
|
|
|
|
for i=1,#parts,1 do
|
|
|
|
if temppath ~= "" then
|
|
|
|
temppath = temppath .. ""
|
|
|
|
end
|
|
|
|
temppath = temppath .. parts[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
parts = temppath:split(" ")
|
|
|
|
temppath = ""
|
|
|
|
for i=1,#parts,1 do
|
|
|
|
if temppath ~= "" then
|
|
|
|
temppath = temppath
|
|
|
|
end
|
|
|
|
temppath = temppath .. parts[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
return temppath
|
|
|
|
end
|
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
function core.formspec_escape(text)
|
2013-08-14 14:21:39 -04:00
|
|
|
if text ~= nil then
|
|
|
|
text = string.gsub(text,"\\","\\\\")
|
|
|
|
text = string.gsub(text,"%]","\\]")
|
|
|
|
text = string.gsub(text,"%[","\\[")
|
|
|
|
text = string.gsub(text,";","\\;")
|
|
|
|
text = string.gsub(text,",","\\,")
|
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2013-11-10 13:37:45 -05:00
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
function core.splittext(text,charlimit)
|
2013-11-10 13:37:45 -05:00
|
|
|
local retval = {}
|
|
|
|
|
|
|
|
local current_idx = 1
|
|
|
|
|
|
|
|
local start,stop = string.find(text," ",current_idx)
|
|
|
|
local nl_start,nl_stop = string.find(text,"\n",current_idx)
|
|
|
|
local gotnewline = false
|
|
|
|
if nl_start ~= nil and (start == nil or nl_start < start) then
|
|
|
|
start = nl_start
|
|
|
|
stop = nl_stop
|
|
|
|
gotnewline = true
|
|
|
|
end
|
|
|
|
local last_line = ""
|
|
|
|
while start ~= nil do
|
|
|
|
if string.len(last_line) + (stop-start) > charlimit then
|
|
|
|
table.insert(retval,last_line)
|
|
|
|
last_line = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
if last_line ~= "" then
|
|
|
|
last_line = last_line .. " "
|
|
|
|
end
|
|
|
|
|
|
|
|
last_line = last_line .. string.sub(text,current_idx,stop -1)
|
|
|
|
|
|
|
|
if gotnewline then
|
|
|
|
table.insert(retval,last_line)
|
|
|
|
last_line = ""
|
|
|
|
gotnewline = false
|
|
|
|
end
|
|
|
|
current_idx = stop+1
|
|
|
|
|
|
|
|
start,stop = string.find(text," ",current_idx)
|
|
|
|
nl_start,nl_stop = string.find(text,"\n",current_idx)
|
|
|
|
|
|
|
|
if nl_start ~= nil and (start == nil or nl_start < start) then
|
|
|
|
start = nl_start
|
|
|
|
stop = nl_stop
|
|
|
|
gotnewline = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--add last part of text
|
|
|
|
if string.len(last_line) + (string.len(text) - current_idx) > charlimit then
|
|
|
|
table.insert(retval,last_line)
|
|
|
|
table.insert(retval,string.sub(text,current_idx))
|
|
|
|
else
|
|
|
|
last_line = last_line .. " " .. string.sub(text,current_idx)
|
|
|
|
table.insert(retval,last_line)
|
|
|
|
end
|
|
|
|
|
|
|
|
return retval
|
|
|
|
end
|
|
|
|
|
2013-11-05 02:11:35 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
if INIT == "game" then
|
2014-01-06 20:31:49 -05:00
|
|
|
local dirs1 = {9, 18, 7, 12}
|
|
|
|
local dirs2 = {20, 23, 22, 21}
|
2013-11-05 02:11:35 -05:00
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
function core.rotate_and_place(itemstack, placer, pointed_thing,
|
2014-01-06 20:31:49 -05:00
|
|
|
infinitestacks, orient_flags)
|
2013-11-06 22:09:06 -05:00
|
|
|
orient_flags = orient_flags or {}
|
2013-11-05 02:11:35 -05:00
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
local unode = core.get_node_or_nil(pointed_thing.under)
|
2014-01-06 20:31:49 -05:00
|
|
|
if not unode then
|
|
|
|
return
|
|
|
|
end
|
2014-04-27 21:02:48 -04:00
|
|
|
local undef = core.registered_nodes[unode.name]
|
2014-01-06 20:31:49 -05:00
|
|
|
if undef and undef.on_rightclick then
|
2014-01-19 17:16:22 -05:00
|
|
|
undef.on_rightclick(pointed_thing.under, unode, placer,
|
2014-01-11 11:23:28 -05:00
|
|
|
itemstack, pointed_thing)
|
2014-01-06 20:31:49 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local pitch = placer:get_look_pitch()
|
2014-04-27 21:02:48 -04:00
|
|
|
local fdir = core.dir_to_facedir(placer:get_look_dir())
|
2014-01-06 20:31:49 -05:00
|
|
|
local wield_name = itemstack:get_name()
|
|
|
|
|
|
|
|
local above = pointed_thing.above
|
|
|
|
local under = pointed_thing.under
|
|
|
|
local iswall = (above.y == under.y)
|
|
|
|
local isceiling = not iswall and (above.y < under.y)
|
2014-04-27 21:02:48 -04:00
|
|
|
local anode = core.get_node_or_nil(above)
|
2014-01-06 20:31:49 -05:00
|
|
|
if not anode then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local pos = pointed_thing.above
|
|
|
|
local node = anode
|
2013-11-05 02:11:35 -05:00
|
|
|
|
2014-01-06 20:31:49 -05:00
|
|
|
if undef and undef.buildable_to then
|
|
|
|
pos = pointed_thing.under
|
|
|
|
node = unode
|
|
|
|
iswall = false
|
|
|
|
end
|
2013-11-05 02:11:35 -05:00
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
if core.is_protected(pos, placer:get_player_name()) then
|
|
|
|
core.record_protection_violation(pos,
|
2014-01-06 20:40:59 -05:00
|
|
|
placer:get_player_name())
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
local ndef = core.registered_nodes[node.name]
|
2014-01-06 20:31:49 -05:00
|
|
|
if not ndef or not ndef.buildable_to then
|
|
|
|
return
|
|
|
|
end
|
2013-11-05 02:11:35 -05:00
|
|
|
|
2014-01-06 20:31:49 -05:00
|
|
|
if orient_flags.force_floor then
|
|
|
|
iswall = false
|
|
|
|
isceiling = false
|
|
|
|
elseif orient_flags.force_ceiling then
|
|
|
|
iswall = false
|
|
|
|
isceiling = true
|
|
|
|
elseif orient_flags.force_wall then
|
|
|
|
iswall = true
|
|
|
|
isceiling = false
|
|
|
|
elseif orient_flags.invert_wall then
|
|
|
|
iswall = not iswall
|
|
|
|
end
|
2013-11-05 02:11:35 -05:00
|
|
|
|
2014-01-06 20:31:49 -05:00
|
|
|
if iswall then
|
2014-04-27 21:02:48 -04:00
|
|
|
core.set_node(pos, {name = wield_name,
|
2014-01-06 20:31:49 -05:00
|
|
|
param2 = dirs1[fdir+1]})
|
|
|
|
elseif isceiling then
|
|
|
|
if orient_flags.force_facedir then
|
2014-04-27 21:02:48 -04:00
|
|
|
core.set_node(pos, {name = wield_name,
|
2014-01-06 20:31:49 -05:00
|
|
|
param2 = 20})
|
|
|
|
else
|
2014-04-27 21:02:48 -04:00
|
|
|
core.set_node(pos, {name = wield_name,
|
2014-01-06 20:31:49 -05:00
|
|
|
param2 = dirs2[fdir+1]})
|
2013-11-05 02:11:35 -05:00
|
|
|
end
|
2014-01-06 20:31:49 -05:00
|
|
|
else -- place right side up
|
|
|
|
if orient_flags.force_facedir then
|
2014-04-27 21:02:48 -04:00
|
|
|
core.set_node(pos, {name = wield_name,
|
2014-01-06 20:31:49 -05:00
|
|
|
param2 = 0})
|
|
|
|
else
|
2014-04-27 21:02:48 -04:00
|
|
|
core.set_node(pos, {name = wield_name,
|
2014-01-06 20:31:49 -05:00
|
|
|
param2 = fdir})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not infinitestacks then
|
|
|
|
itemstack:take_item()
|
|
|
|
return itemstack
|
2013-11-05 02:11:35 -05:00
|
|
|
end
|
|
|
|
end
|
2013-11-07 16:20:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
--Wrapper for rotate_and_place() to check for sneak and assume Creative mode
|
|
|
|
--implies infinite stacks when performing a 6d rotation.
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
core.rotate_node = function(itemstack, placer, pointed_thing)
|
|
|
|
core.rotate_and_place(itemstack, placer, pointed_thing,
|
|
|
|
core.setting_getbool("creative_mode"),
|
2014-01-06 20:31:49 -05:00
|
|
|
{invert_wall = placer:get_player_control().sneak})
|
2013-11-07 16:20:04 -05:00
|
|
|
return itemstack
|
|
|
|
end
|
2013-11-05 02:11:35 -05:00
|
|
|
end
|
|
|
|
|
2013-08-23 06:24:11 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2014-04-27 21:02:48 -04:00
|
|
|
function core.explode_table_event(evt)
|
2013-08-23 06:24:11 -04:00
|
|
|
if evt ~= nil then
|
|
|
|
local parts = evt:split(":")
|
|
|
|
if #parts == 3 then
|
|
|
|
local t = parts[1]:trim()
|
|
|
|
local r = tonumber(parts[2]:trim())
|
|
|
|
local c = tonumber(parts[3]:trim())
|
|
|
|
if type(r) == "number" and type(c) == "number" and t ~= "INV" then
|
|
|
|
return {type=t, row=r, column=c}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return {type="INV", row=0, column=0}
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2014-04-27 21:02:48 -04:00
|
|
|
function core.explode_textlist_event(evt)
|
2013-08-23 06:24:11 -04:00
|
|
|
if evt ~= nil then
|
|
|
|
local parts = evt:split(":")
|
|
|
|
if #parts == 2 then
|
|
|
|
local t = parts[1]:trim()
|
|
|
|
local r = tonumber(parts[2]:trim())
|
|
|
|
if type(r) == "number" and t ~= "INV" then
|
|
|
|
return {type=t, index=r}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return {type="INV", index=0}
|
|
|
|
end
|
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
function core.pos_to_string(pos)
|
|
|
|
return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
|
|
|
|
end
|
|
|
|
|
2013-07-16 15:03:57 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- mainmenu only functions
|
|
|
|
--------------------------------------------------------------------------------
|
2014-04-27 21:02:48 -04:00
|
|
|
if INIT == "mainmenu" then
|
|
|
|
function core.get_game(index)
|
2013-07-16 15:03:57 -04:00
|
|
|
local games = game.get_games()
|
|
|
|
|
|
|
|
if index > 0 and index <= #games then
|
|
|
|
return games[index]
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
2013-08-14 13:22:23 -04:00
|
|
|
|
|
|
|
function fgettext(text, ...)
|
2014-04-27 21:02:48 -04:00
|
|
|
text = core.gettext(text)
|
2013-08-14 13:22:23 -04:00
|
|
|
local arg = {n=select('#', ...), ...}
|
|
|
|
if arg.n >= 1 then
|
|
|
|
-- Insert positional parameters ($1, $2, ...)
|
|
|
|
result = ''
|
|
|
|
pos = 1
|
|
|
|
while pos <= text:len() do
|
|
|
|
newpos = text:find('[$]', pos)
|
|
|
|
if newpos == nil then
|
|
|
|
result = result .. text:sub(pos)
|
|
|
|
pos = text:len() + 1
|
|
|
|
else
|
|
|
|
paramindex = tonumber(text:sub(newpos+1, newpos+1))
|
|
|
|
result = result .. text:sub(pos, newpos-1) .. tostring(arg[paramindex])
|
|
|
|
pos = newpos + 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
text = result
|
|
|
|
end
|
2014-04-27 21:02:48 -04:00
|
|
|
return core.formspec_escape(text)
|
2013-07-16 15:03:57 -04:00
|
|
|
end
|
|
|
|
end
|
2013-08-14 14:21:39 -04:00
|
|
|
|