util.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ----------------
  2. -- Class
  3. ----------------
  4. function new(x, ...)
  5. local t = extend(x)
  6. if t.init then
  7. t:init(...)
  8. end
  9. return t
  10. end
  11. function extend(x)
  12. local t = {}
  13. setmetatable(t, {__index = x, __call = new})
  14. return t
  15. end
  16. function class() return extend() end
  17. ----------------
  18. -- Math
  19. ----------------
  20. function math.sign(x) return x > 0 and 1 or x < 0 and -1 or 0 end
  21. function math.round(x) return math.sign(x) >= 0 and math.floor(x + .5) or math.ceil(x - .5) end
  22. function math.clamp(x, l, h) return math.min(math.max(x, l), h) end
  23. function math.lerp(x1, x2, z) return x1 + (x2 - x1) * z end
  24. function math.anglerp(d1, d2, z) return d1 + (math.anglediff(d1, d2) * z) end
  25. function math.dx(len, dir) return len * math.cos(dir) end
  26. function math.dy(len, dir) return len * math.sin(dir) end
  27. function math.distance(x1, y1, x2, y2) return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ .5 end
  28. function math.direction(x1, y1, x2, y2) return math.atan2(y2 - y1, x2 - x1) end
  29. function math.vector(...) return math.distance(...), math.direction(...) end
  30. function math.inside(px, py, rx, ry, rw, rh) return px >= rx and px <= rx + rw and py >= ry and py <= ry + rh end
  31. function math.anglediff(d1, d2) return math.rad((((math.deg(d2) - math.deg(d1) % 360) + 540) % 360) - 180) end
  32. ----------------
  33. -- Table
  34. ----------------
  35. all = pairs
  36. function table.eq(t1, t2)
  37. if type(t1) ~= type(t2) then return false end
  38. if type(t1) ~= 'table' then return t1 == t2 end
  39. if #t1 ~= #t2 then return false end
  40. for k, _ in pairs(t1) do
  41. if not table.eq(t1[k], t2[k]) then return false end
  42. end
  43. return true
  44. end
  45. function table.copy(x)
  46. local t = type(x)
  47. if t ~= 'table' then return x end
  48. local y = {}
  49. for k, v in next, x, nil do y[k] = table.copy(v) end
  50. setmetatable(y, getmetatable(x))
  51. return y
  52. end
  53. function table.has(t, x, deep)
  54. local f = deep and table.eq or rawequal
  55. for _, v in pairs(t) do if f(v, x) then return true end end
  56. return false
  57. end
  58. function table.only(t, ks)
  59. local res = {}
  60. for _, k in pairs(ks) do res[k] = t[k] end
  61. return res
  62. end
  63. function table.except(t, ks)
  64. local res = table.copy(t)
  65. for _, k in pairs(ks) do res[k] = nil end
  66. return res
  67. end
  68. function table.keys(t)
  69. local res = {}
  70. table.each(t, function(_, k) table.insert(res, k) end)
  71. return res
  72. end
  73. function table.values(t)
  74. local res = {}
  75. table.each(t, function(v) table.insert(res, v) end)
  76. return res
  77. end
  78. function table.take(t, n)
  79. local res = {}
  80. for i = 1, n do res[i] = t[i] end
  81. return res
  82. end
  83. function table.drop(t, n)
  84. local res = table.copy(t)
  85. for i = 1, n do table.remove(t, 1) end
  86. return res
  87. end
  88. function table.each(t, f)
  89. if not t then return end
  90. for k, v in pairs(t) do if f(v, k) then break end end
  91. end
  92. function table.with(t, k, ...)
  93. return table.each(t, f.egoexe(k, ...))
  94. end
  95. function table.map(t, f)
  96. if not t then return end
  97. local res = {}
  98. table.each(t, function(v, k) res[k] = f(v, k) end)
  99. return res
  100. end
  101. function table.filter(t, f)
  102. return table.map(t, function(v, k) return f(v, k) and v or nil end)
  103. end
  104. function table.clear(t, v)
  105. table.each(t, function(_, k) t[k] = v end)
  106. end
  107. function table.merge(t1, t2, shallow)
  108. t1, t2 = t1 or {}, t2 or {}
  109. for k, v in pairs(t1) do t2[k] = shallow and v or table.copy(v) end
  110. return t2
  111. end
  112. function table.deltas(t1, t2)
  113. local res = {}
  114. for k, v in pairs(t1) do
  115. if type(t1[k]) ~= type(t2[k]) then
  116. res[k] = type(t2[k]) == 'table' and table.copy(t2[k]) or t2[k]
  117. elseif type(t2[k]) == 'table' then
  118. res[k] = table.deltas(t1[k], t2[k])
  119. elseif t1[k] ~= t2[k] then
  120. res[k] = t2[k]
  121. end
  122. end
  123. return res
  124. end
  125. function table.interpolate(t1, t2, z)
  126. local interp = table.copy(t1)
  127. for k, v in pairs(interp) do
  128. if t2[k] then
  129. if type(v) == 'table' then interp[k] = table.interpolate(t1[k], t2[k], z)
  130. elseif type(v) == 'number' then
  131. if k == 'angle' then interp[k] = math.anglerp(t1[k], t2[k], z)
  132. else interp[k] = math.lerp(t1[k], t2[k], z) end
  133. end
  134. end
  135. end
  136. return interp
  137. end
  138. function table.shuffle(t)
  139. for i = 1, #t do
  140. local a, b = math.random(#t), math.random(#t)
  141. t[a], t[b] = t[b], t[a]
  142. end
  143. end
  144. function table.count(t)
  145. local ct = 0
  146. table.each(t, function() ct = ct + 1 end)
  147. return ct
  148. end
  149. function table.print(t, n)
  150. n = n or 0
  151. if n > 10 then return end
  152. if t == nil then print('nil') end
  153. if type(t) ~= 'table' then io.write(tostring(t)) io.write('\n')
  154. else
  155. local empty = true
  156. for k, v in pairs(t) do
  157. empty = false
  158. io.write(string.rep('\t', n))
  159. io.write(k)
  160. if type(v) == 'table' then io.write('\n')
  161. else io.write('\t') end
  162. table.print(v, n + 1)
  163. end
  164. if empty then io.write(string.rep('\t', n) .. '{}\n') end
  165. end
  166. end
  167. ----------------
  168. -- Functions
  169. ----------------
  170. f = {}
  171. f.empty = function() end
  172. f.exe = function(x, ...) if type(x) == 'function' then return x(...) end return x end
  173. f.ego = function(f, ...) local a = {...} return function(x) x[f](x, unpack(a)) end end
  174. f.egoexe = function(f, ...) local a = {...} return function(x) if x[f] then x[f](x, unpack(a)) end end end
  175. f.val = function(x) return type(x) == 'function' and x or function() return x end end
  176. f.cur = function(fn, x) return function(y) return fn(x, y) end end
  177. f.wrap = function(fn, ...) local a = {...} return function() fn(unpack(a)) end end
  178. ----------------
  179. -- String
  180. ----------------
  181. string.capitalize = function(s) s = ' ' .. s return s:gsub('(%s%l)', string.upper):sub(2) end