-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.lua
107 lines (94 loc) · 1.95 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
local forbidden = {"os", "io", "debug"}
local time = os.time
local getenv = os.getenv
local getinfo = debug.getinfo
os = nil
io = nil
debug = nil
os = {}
debug = {}
debug.getinfo = getinfo
os.time = time
os.getenv = getenv
local _require = require
function require(name)
if not table.containsValue(forbidden, name) then
return _require(name)
else
error("\nTrying to require a forbidden package?\nMistake or made on purpose?\nCaught in 4k.")
end
end
function getfenv(f)
error("\nTrying to get environment for something?\nMistake or made on purpose?\nCaught in 4k.")
end
function setfenv(f, t)
error("\nTrying to set environment for something?\nMistake or made on purpose?\nCaught in 4k.")
end
function string:split(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function table.indexof(t, e)
for i,v in pairs(t) do
if (v == e) then
return i
end
end
return 0
end
function table.containsValue(t, e)
for _,v in pairs(t) do
if (v == e) then
return true
end
end
return false
end
function table.containsKey(t, e)
for k in pairs(t) do
if (k == e) then
return true
end
end
return false
end
function table.clear(t)
for k in pairs(t) do
t[k] = nil
end
end
function math.clamp(v, min, max, recur)
if recur then
return (v > max) and min or (v < min and max or v)
end
return math.max(min, math.min(v, max))
end
function math.lerp(a, b, t)
return (1-t)*a + t*b
end
function regularVertices(r, x, y, n)
local theta = math.rad(360 / n)
local a = {}
for i=1,n*2,2 do
local angle = theta * math.floor(i / 2)
a[i] = x + r * math.cos(angle)
a[i+1] = y + r * math.sin(angle)
end
return a
end
function createSpecialTable(t, onaccess)
local proxy = t
t = {}
setmetatable(t, {
__index = function(t, k)
return proxy[k]
end,
__newindex = function(t, k, v)
proxy[k] = v
onaccess(proxy, k, v, t)
end
})
return t
end