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
|
-- Define useful global stuff.
local utillib = {}
function utillib.tableClone( org )
return { table.unpack( org ) }
end
function utillib.deepCopy( orig )
local copy
if type( orig ) == "table" then
copy = {}
for origKey, origValue in next, orig, nil do
-- If object has clone method use that. Mainly for vector libraries.
if type( origValue ) == "table" and type( origValue.clone ) == "function" then
copy[ utillib.deepCopy( origKey ) ] = orig_value:clone()
else
copy[ utillib.deepCopy( origKey ) ] = utillib.deepCopy( origValue )
end
end
setmetatable( copy, utillib.deepCopy( getmetatable( orig ) ) )
else -- number, string, boolean, etc.
copy = orig
end
return copy
end
function utillib.sign( v )
if 0 <= v then
return 1
elseif v < 0 then
return -1
end
end
function utillib.clamp( val, min, max )
return math.max( min, math.min( val, max ) )
end
-- Returns changed value ( value to be changed, index, bit )
function utillib.setBit( v, i, b )
if b then
return v | 1 << i
else
return v & ~( 1 << i )
end
end
function utillib.toggleBit( v, i )
return v ~ ( 1 << i )
end
function utillib.getBit( v, i )
if v == nil then
return false
end
return v & ( 1 << i ) > 0
end
function utillib.utf8Sub( s, i, j )
i = i or 1
j = j or -1
if i < 1 or j < 1 then
local n = utf8.len(s)
if not n then return nil end
if i < 0 then i = n + 1 + i end
if j < 0 then j = n + 1 + j end
if i < 0 then i = 1 elseif i > n then i = n end
if j < 0 then j = 1 elseif j > n then j = n end
end
if j < i then return "" end
i = utf8.offset( s, i )
j = utf8.offset( s, j + 1 )
if i and j then
return s:sub( i, j - 1 )
elseif i then
return s:sub( i )
else
return ""
end
end
function utillib.round( v )
return math.tointeger( v + 0.5 - ( v + 0.5 ) % 1 )
end
function utillib.tableLen( t )
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
function utillib.split( str, sep )
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch( str, "([^"..sep.."]+)" ) do
table.insert( t, str )
end
return t
end
function utillib.wrapAngleDeg( angle )
if angle < 0 then
return math.fmod( angle, 360.0 ) + 360.0
else
return math.fmod( angle, 360.0 )
end
end
function utillib.wrapAngleRad( angle )
if angle < 0 then
return math.fmod( angle, PI * 2 ) + PI * 2
else
return math.fmod( angle, PI * 2 )
end
end
function utillib.lerp( a, b, f )
return ( a * ( 1.0 - f ) ) + ( b * f )
end
function utillib.toBoolean( v )
if type( v ) == "string" then
if v == "1" or string.lower( v ) == "true" then
return true
elseif v == "0" or string.lower( v ) == "false" then
return false
end
elseif type( v ) == "number" then
return 0 < v
end
return false
end
return utillib
|