Advertisement
skypop

utils

Sep 8th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.19 KB | None | 0 0
  1. -- API utils
  2. -- by SukaiPoppuGo
  3.  
  4. -- shortcuts
  5. local _abs  = math.abs
  6. local _sqrt = math.sqrt
  7. local _pow  = math.pow
  8. local _max  = math.max
  9. local _min  = math.min
  10. local _floor = math.floor
  11. local _rand = math.random
  12. local _sub  = string.sub
  13. local _len  = string.len
  14. local _rep  = string.rep
  15. local _char = string.char
  16.  
  17. --Extensions
  18. math.round = function(n, m)
  19.     m = m and 10 ^ m or 1
  20.     return _floor((n * m) + 0.5) / m
  21. end
  22.  
  23. -- Formate un nombre de secondes en duree lisible
  24. function timeStr(sec)
  25.     local d = _floor(sec/86400) --24*60*60 sec
  26.     local h = _floor(sec/3600)%24 -- 60x60 sec
  27.     local m = _floor(sec/60)%60 -- 60sec
  28.     local s = _floor(sec%60) --Reste
  29.     local str = ""
  30.     if d>0 then str = d>1 and d.." days " or "1 day " end
  31.     if h>0 then str = str..h.."h" end
  32.     if m>0 then str = str..(m>9 and m or "0"..m) end
  33.     if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
  34.     return str
  35. end
  36.  
  37. --
  38. function var_dump(v,pad)
  39.     pad = pad or ""
  40.     local _t = type(v)
  41.     if _t == "table" then
  42.         local _k,_v
  43.         local _r = ""
  44.         for _k,_v in pairs(v) do
  45.             _r = _r..pad.."  ".._k.."="..var_dump(_v,pad.."  ")..",\n"
  46.         end
  47.         if string.len(_r)>0 then
  48.             return "{\n".._r..pad.."}"
  49.         else
  50.             return "{}"
  51.         end
  52.     elseif _t == "function" then
  53.         return "function()"
  54.     elseif _t == "string" then
  55.         return "\""..v.."\""
  56.     elseif _t == "nil" or _t == "number" or _t == "boolean" then
  57.         return tostring(v)
  58.     else
  59.         return _t
  60.     end
  61. end
  62.  
  63. --
  64. local _loadedFiles = {}
  65. function include(filename)
  66.     if fs.exists(filename) then
  67.         dofile(filename)
  68.         _loadedFiles[filename] = true
  69.         return true
  70.     else
  71.         print(string.format("Include file \"%s\" not found", filename))
  72.         return false
  73.     end
  74. end
  75. function require(filename, error_level)
  76.     error_level = error_level or 1
  77.     if fs.exists(filename) then
  78.         dofile(filename)
  79.         _loadedFiles[filename] = true
  80.         return true
  81.     else
  82.         error(string.format("Required file \"%s\" not found", filename), error_level + 1)
  83.         return false
  84.     end
  85. end
  86. function include_once(filename)
  87.     if not _loadedFiles[filename] then
  88.         return include(filename)
  89.     end
  90.     return _loadedFiles[filename]
  91. end
  92. function require_once(filename)
  93.     if not _loadedFiles[filename] then
  94.         return require(filename, 2)
  95.     end
  96. end
  97.  
  98. --
  99. function file_get_contents(filename)
  100.     if not fs.exists(filename) then
  101.         error(string.format("File \"%s\" not found", filename), 2)
  102.         return false
  103.     end
  104.     local handler = fs.open(filename, "r")
  105.     local contents = handler.readAll()
  106.     handler.close()
  107.     return contents
  108. end
  109.  
  110. -- POO
  111. function isInstanceOf(o, sType)
  112.     return type(o) == "table" and o.type and o.type == sType
  113. end
  114.  
  115. -- Parse MC coords format
  116. -- absolute / ~relative
  117. function parseCoords(v, r)
  118.     local s = tostring(v)
  119.     if _sub(s, 1, 1) == "~" then
  120.         if _len(s) == 1 then
  121.             return r
  122.         else
  123.             return r + tonumber(_sub(s, 2))
  124.         end
  125.     else
  126.         return v
  127.     end
  128. end
  129.  
  130. -- Constraint value
  131. function constraint(v, a, b)
  132.     return _max(a > b and b or a, _min(a > b and a or b, v))
  133. end
  134.  
  135. function coordInside(A, B, C)
  136.     return A.x >= _min(B.x, C.x) and A.x <= _max(B.x, C.x) and A.y >= _min(B.y, C.y) and A.y <= _max(B.y, C.y) and A.z >= _min(B.z, C.z) and A.z <= _max(B.z, C.z)
  137. end
  138.  
  139. --Taxicab geometry
  140. --https://en.wikipedia.org/wiki/Taxicab_geometry
  141. -- d(A,B) = |B.x - A.x| + |B.y - A.y|
  142. function fuelCost(A, B)
  143.     return _abs(B.x - A.x) + _abs(B.y - A.y) + _abs(B.z - A.z)
  144. end
  145.  
  146. --https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space
  147. function distance(A, B)
  148.     return _sqrt(_pow(B.x - A.x, 2) + _pow(B.y - A.y, 2) + _pow(B.z - A.z, 2))
  149. end
  150.  
  151. --Randomize table
  152. function shuffle( t )
  153.     local it = #t
  154.     local j
  155.     for i = it, 2, -1 do
  156.         j = _rand(i)
  157.         t[i], t[j] = t[j], t[i]
  158.     end
  159.     return t
  160. end
  161.  
  162. function keySort( t )
  163.     local tkeys = {}
  164.     for k in pairs( t ) do table.insert(tkeys, k) end -- populate the table that holds the keys
  165.     table.sort( tkeys ) -- sort the keys
  166.     local temp = {}
  167.     for _, k in ipairs( tkeys ) do table.insert( temp, t[k] ) end -- use the keys to retrieve the values in the sorted order
  168.     return temp
  169. end
  170.  
  171. --Copy table
  172. -- if more than one merge them
  173. -- in case of overwriting values last one gets its way
  174. function copyTable(...)
  175.     tArgs = {...}
  176.     local B = {}
  177.         for _, A in pairs(tArgs) do
  178.             if A and type(A) == "table" then
  179.                 for i, k in pairs(A) do
  180.                     if type(k) == "table" then B[i] = copyT( B[i] or {}, k)
  181.                     else B[i] = k end
  182.                 end
  183.             end
  184.         end
  185.     return B
  186. end
  187.  
  188. -- compareData
  189. -- Compare every data from _2 to data from _1
  190. -- @param   _1  (mixed) data to compare
  191. -- @param   _2  (mixed) reference
  192. -- @param   strict  (bool)  Compare every data from _2 and every data from _1
  193. -- @return  (bool)
  194. function compareData(_1,_2, strict)
  195.     strict = strict or false
  196.     if type(_1) == type(_2) then
  197.         if type(_1) == "table" then
  198.             local test, total, k, v = 0,0
  199.             for k,v in pairs(_2) do
  200.                 total = total+1
  201.                 if compareData(_1[k], v, strict) then
  202.                     test = test+1
  203.                 end
  204.             end
  205.             if strict then
  206.                 for k,v in pairs(_1) do
  207.                     total = total+1
  208.                     if compareData(_2[k], v, true) then
  209.                         test = test+1
  210.                     end
  211.                 end
  212.             end
  213.             return test == total
  214.         elseif type(_1) == "string"
  215.         or type(_1) == "number"
  216.         or type(_1) == "boolean" then
  217.             return _1 == _2
  218.         else
  219.             error("Cant compare "..type(_1),2)
  220.         end
  221.     end
  222.     return false
  223. end
  224. -- Compare every data from B to data from A
  225. -- function compareData(A, B)
  226. --  if type(A) == type(B) then
  227. --      if type(A) == "table" then
  228. --          local test, total, k, v = 0, 0
  229. --          for k,v in pairs(B) do
  230. --              total = total + 1
  231. --              if compareData(A[k], v) then
  232. --                  test = test + 1
  233. --              end
  234. --          end
  235. --          return test == total
  236. --      elseif type(A) == "string"
  237. --      or type(A) == "number"
  238. --      or type(A) == "boolean" then
  239. --          return A == B
  240. --      else
  241. --          error("Cant compare "..type(A),2)
  242. --      end
  243. --  end
  244. --  return false
  245. -- end
  246.  
  247.  
  248. -- Run a function after X update iteration
  249. -- CountdownFunction = {}
  250. -- CountdownFunction.__index = CountdownFunction
  251. -- CountdownFunction.new = function(self, count, func)
  252. --    local o = {}
  253. --    setmetatable(o, CountdownFunction)
  254. --    o.count = count
  255. --    o.func = func
  256. --    return o
  257. -- end
  258. -- CountdownFunction.update = function(self, ...)
  259. --  if self.count > 0 then
  260. --      self.count = self.count - 1
  261. --  elseif self.count == 0 then
  262. --      slef.count = -1
  263. --      return self.func(...)
  264. --  end
  265. -- end
  266.  
  267. -- Colors references
  268. local _colors = {
  269.     [    1] = "0", -- white
  270.     [    2] = "1", -- orange
  271.     [    4] = "2", -- magenta
  272.     [    8] = "3", -- lightBlue
  273.     [   16] = "4", -- yellow
  274.     [   32] = "5", -- lime
  275.     [   64] = "6", -- pink
  276.     [  128] = "7", -- gray
  277.     [  256] = "8", -- lightGray
  278.     [  512] = "9", -- cyan
  279.     [ 1024] = "a", -- purple
  280.     [ 2048] = "b", -- blue
  281.     [ 4096] = "c", -- brown
  282.     [ 8192] = "d", -- green
  283.     [16384] = "e", -- red
  284.     [32768] = "f", -- black
  285. }
  286.  
  287. -------------------
  288. -- colorsGetChar
  289. -- param:   number  color       As color API
  290. -- return:  string  colorLetter Letter stands for color code in paint or blit function
  291. function colorsGetChar(value)
  292.     assert(_colors[value], "Unknown color: \""..(tostring(value) or type(value)).."\"")
  293.     return _colors[value]
  294. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement