-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
78 lines (67 loc) · 1.63 KB
/
init.lua
File metadata and controls
78 lines (67 loc) · 1.63 KB
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
function object(fn)
local __ENV = getfenv()
return function(...)
local _ENV = {}
for k,v in pairs(__ENV) do
_ENV[k] = v
end
local interface = {}
setmetatable(_ENV, {
__index = function(t, k)
return interface[k]
end,
__newindex = function(t, k, v)
interface[k] = v
end,
})
setfenv(fn, _ENV)
local obj = fn(...)
setmetatable(obj, {
__index = function(t, k)
if k=="interface" then
return interface
else
return interface[k]
end
end,
__newindex = function(t, k, v)
interface[k] = v
end,
})
return obj
end
end
function math.randomReal(min, max)
return min+math.random()*(max-min)
end
function math.round(val)
return math.floor(val+0.5)
end
function math.dice(dice,side)
local x = 0
for i=1,dice do
x = x + math.random(1,side)
end
return x
end
--if 'probabilityPower' is above 1, lower values will be more common than higher values
--if it's between 0 to 1, higher values will be more common than lower values.
--If it's 1, the results will be in a general randomness.
--Probability distribution function
function math.randomP(min, max, probabilityPower)
return math.floor(min + (max + 1 - min) * (math.pow(math.random(), probabilityPower)))
end
function math.chance(chance)
local x = math.random()
if x <= chance then
return true
else
return false
end
end
function math.clamp(val, lower, upper)
--assert(val and lower and upper, "not very useful error message here")
if lower > upper then lower, upper = upper, lower end -- swap if boundaries supplied the wrong way
return math.max(lower, math.min(upper, val))
end
math.randomseed(os.time())