]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - builtin/misc_helpers.lua
Use engine.is_yes() in mainmenu
[dragonfireclient.git] / builtin / misc_helpers.lua
index 743a9cfd30768d4894a55c41ebd9b19572eaa6cc..3a325e0d30d981c2adce85712229a66fcf841de8 100644 (file)
@@ -1,5 +1,6 @@
 -- Minetest: builtin/misc_helpers.lua
 
+--------------------------------------------------------------------------------
 function basic_dump2(o)
        if type(o) == "number" then
                return tostring(o)
@@ -19,6 +20,7 @@ function basic_dump2(o)
        end
 end
 
+--------------------------------------------------------------------------------
 function dump2(o, name, dumped)
        name = name or "_"
        dumped = dumped or {}
@@ -44,6 +46,7 @@ function dump2(o, name, dumped)
        end
 end
 
+--------------------------------------------------------------------------------
 function dump(o, dumped)
        dumped = dumped or {}
        if type(o) == "number" then
@@ -74,6 +77,7 @@ function dump(o, dumped)
        end
 end
 
+--------------------------------------------------------------------------------
 function string:split(sep)
        local sep, fields = sep or ",", {}
        local pattern = string.format("([^%s]+)", sep)
@@ -81,14 +85,170 @@ function string:split(sep)
        return fields
 end
 
+--------------------------------------------------------------------------------
+function file_exists(filename)
+       local f = io.open(filename, "r")
+       if f==nil then
+               return false
+       else
+               f:close()
+               return true
+       end
+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 .. ")"
+--------------------------------------------------------------------------------
+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
+
+--------------------------------------------------------------------------------
+function explode_textlist_event(text)
+       
+       local retval = {}
+       retval.typ = "INV"
+       
+       local parts = text:split(":")
+                               
+       if #parts == 2 then
+               retval.typ = parts[1]:trim()
+               retval.index= tonumber(parts[2]:trim())
+               
+               if type(retval.index) ~= "number" then
+                       retval.typ = "INV"
+               end
+       end
+       
+       return retval
+end
+
+--------------------------------------------------------------------------------
+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
+
+local tbl = engine or minetest
+function tbl.formspec_escape(text)
+       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
 
+--------------------------------------------------------------------------------
+-- mainmenu only functions
+--------------------------------------------------------------------------------
+if engine ~= nil then
+       engine.get_game = function(index)
+               local games = game.get_games()
+               
+               if index > 0 and index <= #games then
+                       return games[index]
+               end
+               
+               return nil
+       end
+       
+       function fgettext(text, ...)
+               text = engine.gettext(text)
+               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
+               return engine.formspec_escape(text)
+       end
+end
+--------------------------------------------------------------------------------
+-- core only fct
+--------------------------------------------------------------------------------
+if minetest ~= nil then
+       --------------------------------------------------------------------------------
+       function minetest.pos_to_string(pos)
+               return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
+       end
+end