(Re)spawn players within 'mapgen_limit'
[oweals/minetest.git] / builtin / common / misc_helpers.lua
index e4653d41dda1c53642f809a68594ffb0fde239f3..f9b572d9f88dd61789bdab4bfe06e556e72217a8 100644 (file)
@@ -197,16 +197,17 @@ assert(table.indexof({"foo", "bar"}, "foo") == 1)
 assert(table.indexof({"foo", "bar"}, "baz") == -1)
 
 --------------------------------------------------------------------------------
-function file_exists(filename)
-       local f = io.open(filename, "r")
-       if f == nil then
-               return false
-       else
-               f:close()
-               return true
+if INIT ~= "client" then
+       function file_exists(filename)
+               local f = io.open(filename, "r")
+               if f == nil then
+                       return false
+               else
+                       f:close()
+                       return true
+               end
        end
 end
-
 --------------------------------------------------------------------------------
 function string:trim()
        return (self:gsub("^%s*(.-)%s*$", "%1"))
@@ -307,7 +308,7 @@ function core.formspec_escape(text)
 end
 
 
-function core.splittext(text,charlimit)
+function core.wrap_text(text, charlimit)
        local retval = {}
 
        local current_idx = 1
@@ -382,7 +383,6 @@ if INIT == "game" then
                                        itemstack, pointed_thing)
                        return
                end
-               local pitch = placer:get_look_pitch()
                local fdir = core.dir_to_facedir(placer:get_look_dir())
                local wield_name = itemstack:get_name()
 
@@ -463,7 +463,7 @@ if INIT == "game" then
 
        core.rotate_node = function(itemstack, placer, pointed_thing)
                core.rotate_and_place(itemstack, placer, pointed_thing,
-                               core.setting_getbool("creative_mode"),
+                               core.settings:get_bool("creative_mode"),
                                {invert_wall = placer:get_player_control().sneak})
                return itemstack
        end
@@ -607,7 +607,9 @@ if INIT == "mainmenu" then
 
                return nil
        end
+end
 
+if INIT == "client" or INIT == "mainmenu" then
        function fgettext_ne(text, ...)
                text = core.gettext(text)
                local arg = {n=select('#', ...), ...}
@@ -638,3 +640,93 @@ if INIT == "mainmenu" then
        end
 end
 
+local ESCAPE_CHAR = string.char(0x1b)
+
+function core.get_color_escape_sequence(color)
+       return ESCAPE_CHAR .. "(c@" .. color .. ")"
+end
+
+function core.get_background_escape_sequence(color)
+       return ESCAPE_CHAR .. "(b@" .. color .. ")"
+end
+
+function core.colorize(color, message)
+       local lines = tostring(message):split("\n", true)
+       local color_code = core.get_color_escape_sequence(color)
+
+       for i, line in ipairs(lines) do
+               lines[i] = color_code .. line
+       end
+
+       return table.concat(lines, "\n") .. core.get_color_escape_sequence("#ffffff")
+end
+
+
+function core.strip_foreground_colors(str)
+       return (str:gsub(ESCAPE_CHAR .. "%(c@[^)]+%)", ""))
+end
+
+function core.strip_background_colors(str)
+       return (str:gsub(ESCAPE_CHAR .. "%(b@[^)]+%)", ""))
+end
+
+function core.strip_colors(str)
+       return (str:gsub(ESCAPE_CHAR .. "%([bc]@[^)]+%)", ""))
+end
+
+--------------------------------------------------------------------------------
+-- Returns the exact coordinate of a pointed surface
+--------------------------------------------------------------------------------
+function core.pointed_thing_to_face_pos(placer, pointed_thing)
+       local eye_offset_first = placer:get_eye_offset()
+       local node_pos = pointed_thing.under
+       local camera_pos = placer:get_pos()
+       local pos_off = vector.multiply(
+                       vector.subtract(pointed_thing.above, node_pos), 0.5)
+       local look_dir = placer:get_look_dir()
+       local offset, nc
+       local oc = {}
+
+       for c, v in pairs(pos_off) do
+               if nc or v == 0 then
+                       oc[#oc + 1] = c
+               else
+                       offset = v
+                       nc = c
+               end
+       end
+
+       local fine_pos = {[nc] = node_pos[nc] + offset}
+       camera_pos.y = camera_pos.y + 1.625 + eye_offset_first.y / 10
+       local f = (node_pos[nc] + offset - camera_pos[nc]) / look_dir[nc]
+
+       for i = 1, #oc do
+               fine_pos[oc[i]] = camera_pos[oc[i]] + look_dir[oc[i]] * f
+       end
+       return fine_pos
+end
+
+function core.string_to_privs(str, delim)
+       assert(type(str) == "string")
+       delim = delim or ','
+       local privs = {}
+       for _, priv in pairs(string.split(str, delim)) do
+               privs[priv:trim()] = true
+       end
+       return privs
+end
+
+function core.privs_to_string(privs, delim)
+       assert(type(privs) == "table")
+       delim = delim or ','
+       local list = {}
+       for priv, bool in pairs(privs) do
+               if bool then
+                       list[#list + 1] = priv
+               end
+       end
+       return table.concat(list, delim)
+end
+
+assert(core.string_to_privs("a,b").b == true)
+assert(core.privs_to_string({a=true,b=true}) == "a,b")