veikk0
2016-03-30 2258adb2a9b2ec223ea3614a39ecbd59d2d6c948
technic/helpers.lua
@@ -1,27 +1,38 @@
--load config
local sepchar, baresepchar = nil, nil
local digit_sep_esc
do
   local sepcode = technic.config:get("thousand_separator")
   --default is SI style
   sepchar = sepcode and string.char(sepcode) or " "
   baresepchar = sepchar
   --handling if sepchar is magic...
   for magic in string.gmatch("().%+-*?[^$", ".") do
      if sepchar == magic then sepchar = "%"..sepchar end
   local sep = technic.config:get("digit_separator")
   sep = tonumber(sep) and string.char(sep) or sep or " "
   -- Escape for gsub
   for magic in ("().%+-*?[^$"):gmatch(".") do
      if sep == magic then
         sep = "%"..sep
      end
   end
   digit_sep_esc = sep
end
-- Only changes name, keeps other params
function technic.pretty_num(num)
   local str, k = tostring(num), nil
   repeat
      str, k = str:gsub("^(-?%d+)(%d%d%d)", "%1"..digit_sep_esc.."%2")
   until k == 0
   return str
end
--- Same as minetest.swap_node, but only changes name
-- and doesn't re-set if already set.
function technic.swap_node(pos, name)
   local node = minetest.get_node(pos)
   if node.name ~= name then
      node.name = name
      minetest.swap_node(pos, node)
   end
   return node.name
end
-- Fully charge RE chargeable item.
--- Fully charge RE chargeable item.
-- Must be defined early to reference in item definitions.
function technic.refill_RE_charge(stack)
   local max_charge = technic.power_tools[stack:get_name()]
@@ -33,63 +44,83 @@
   return stack
end
local function resolve_name(function_name)
   local a = _G
   for key in string.gmatch(function_name, "([^%.]+)(%.?)") do
      if a[key] then
         a = a[key]
      else
         return nil
      end
   end
   return a
end
function technic.function_exists(function_name)
   return type(resolve_name(function_name)) == 'function'
end
-- if the node is loaded, returns it. If it isn't loaded, load it and return nil.
-- If the node is loaded, returns it.  If it isn't loaded, load it and return nil.
function technic.get_or_load_node(pos)
   local node_or_nil = minetest.get_node_or_nil(pos)
   if node_or_nil then return node_or_nil end
   local node = minetest.get_node_or_nil(pos)
   if node then return node end
   local vm = VoxelManip()
   local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
   return nil
end
function technic.format(str, ...)
   local arg={...}
   local param = nil
   local percent = false
   local res = ""
   local i = 1
   for c in str:gmatch"." do
      if percent then
         assert(c ~= "%") --syntax error
         if c == "e" then
            -- use enhanced number formatting
            -- only works for unsigned numbers
            local numstr = tostring(math.abs(arg[i]))
            local a, b, body, frac = numstr:find("^(%d+)([.]?.-)$")
            a = 1
            body = body..baresepchar
            while a ~= 0 do
               body, a = body:gsub("(%d)(%d%d%d)"..sepchar, "%1"..sepchar.."%2"..sepchar, 1)
            end
            body = body:gsub(sepchar.."$", "")
            res = res .. body .. frac
         else
            --use traditional string:format
            res = res .. (string.format(("%"..c), arg[i]))
         end
         i = i + 1
         percent = false
      elseif c == "%" then
         percent = true
      else
         res = res .. c
      end
technic.tube_inject_item = pipeworks.tube_inject_item or function(pos, start_pos, velocity, item)
   local tubed = pipeworks.tube_item(vector.new(pos), item)
   tubed:get_luaentity().start_pos = vector.new(start_pos)
   tubed:setvelocity(velocity)
   tubed:setacceleration(vector.new(0, 0, 0))
end
-- Based on code by Uberi: https://gist.github.com/Uberi/3125280
function technic.trace_node_ray(pos, dir, range)
   local p = vector.round(pos)
   local x_step,      y_step,      z_step      = 0, 0, 0
   local x_component, y_component, z_component = 0, 0, 0
   local x_intersect, y_intersect, z_intersect = 0, 0, 0
   if dir.x == 0 then
      x_intersect = math.huge
   elseif dir.x > 0 then
      x_step = 1
      x_component = 1 / dir.x
      x_intersect = x_component
   else
      x_step = -1
      x_component = 1 / -dir.x
   end
   return res
end
   if dir.y == 0 then
      y_intersect = math.huge
   elseif dir.y > 0 then
      y_step = 1
      y_component = 1 / dir.y
      y_intersect = y_component
   else
      y_step = -1
      y_component = 1 / -dir.y
   end
   if dir.z == 0 then
      z_intersect = math.huge
   elseif dir.z > 0 then
      z_step = 1
      z_component = 1 / dir.z
      z_intersect = z_component
   else
      z_step = -1
      z_component = 1 / -dir.z
   end
   return function()
      if x_intersect < y_intersect then
         if x_intersect < z_intersect then
            p.x = p.x + x_step
            x_intersect = x_intersect + x_component
         else
            p.z = p.z + z_step
            z_intersect = z_intersect + z_component
         end
      elseif y_intersect < z_intersect then
         p.y = p.y + y_step
         y_intersect = y_intersect + y_component
      else
         p.z = p.z + z_step
         z_intersect = z_intersect + z_component
      end
      if vector.distance(pos, p) > range then
         return nil
      end
      return p
   end
end