-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.lua
160 lines (143 loc) · 2.88 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
anim_stage = 0
anim_timer = 0
function dump(o)
if type(o) == 'table' then
local s = '{'
local cn = 1
if #o ~= 0 then
for _,v in ipairs(o) do
if cn > 1 then s = s .. ',' end
s = s .. dump(v)
cn = cn + 1
end
else
for k,v in pairs(o) do
if cn > 1 then s = s .. ',' end
s = s .. tostring(k) .. ' = ' .. dump(v)
cn = cn + 1
end
end
return s .. '}'
elseif type(o) == 'string' then
return '"' .. o .. '"'
else
return tostring(o)
end
end
function removeFromTable(t, obj)
if not t then
return
end
for i,v in ipairs(t) do
if v == obj then
table.remove(t, i)
return
end
end
end
function copyTable(t, l_)
local l = l_ or 0
local new_table = {}
for k,v in pairs(t) do
if type(v) == "table" and l > 0 then
new_table[k] = copyTable(v, l - 1)
else
new_table[k] = v
end
end
return new_table
end
function deepCopy(o)
if type(o) == "table" then
local new_table = {}
for k,v in pairs(o) do
new_table[k] = deepCopy(v)
end
return new_table
else
return o
end
end
function lerp(a,b,t) return (1-t)*a + t*b end
function eq(a,b)
if type(a) == "table" or type(b) == "table" then
if type(a) ~= "table" or type(b) ~= "table" then
return false
end
local result = true
if #a == #b then
for i,v in pairs(a) do
if v ~= b[i] then
result = false
break
end
end
else
result = false
end
return result
else
return a == b
end
end
function string.starts(str, start)
return str:sub(1, #start) == start
end
function string.ends(str, ending)
return ending == "" or str:sub(-#ending) == ending
end
function table.has_value(tab, val)
for _, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
function table.random(tab)
return tab[love.math.random(1,#tab)]
end
function mergeTable(t, other)
if other ~= nil then
for k,v in pairs(other) do
if type(k) == "number" then
if not table.has_value(t, v) then
table.insert(t, v)
end
else
if t[k] ~= nil then
if type(t[k]) == "table" and type(v) == "table" then
mergeTable(t[k], v)
end
else
t[k] = v
end
end
end
end
end
function sign(x)
if (x > 0) then
return 1
elseif (x < 0) then
return -1
end
return 0
end
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
function prime(a)
for b=2,math.floor(math.sqrt(a)) do
if a%b == 0 then
return false
end
end
return true
end
function pair(a, b)
if a >= 0 then a = a * 2 else a = a * -2 + 1 end
if b >= 0 then b = b * 2 else b = b * -2 + 1 end
return (((a + b) * (a + b + 1)) / 2) + b
end