fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

blackops7799I lost track of what I'm even doing lola7d6bcc

GUI
1.9 KiB109 linesraw
1function math.gcd(m, n)
2	-- greatest common divisor
3	while m ~= 0 do
4		m, n = n % m, m
5	end
6	return n
7end
8
9do
10	local min = math.min
11	local max = math.max
12
13	function math.clamp(num, min, max)
14		assert(min <= max, "invalid clamp range")
15		if num < min then
16			num = min
17		elseif num > max then
18			num = max
19		end
20		return num
21	end
22end
23
24do
25	local pow = math.pow
26	local floor = math.floor
27
28	function math.round(num)
29		return num + 0.5 - (num + 0.5) % 1
30	end
31
32	function math.roundplaces(num, palces)
33		local mult = pow(10, (palces or 0))
34		return floor(num * mult + 0.5) / mult
35	end
36end
37
38do
39	local random = math.random
40
41	function math.randomFloat(min, max)
42		return min + random() * (max - min)
43	end
44
45	require("extensions.table")
46
47	local sum = table.sum
48
49	function math.weightedRandom(tbl)
50		local threshold = random(0, sum(tbl))
51		local last_choice
52		for choice, weight in pairs(tbl) do
53			threshold = threshold - weight
54			if threshold <= 0 then return choice end
55			last_choice = choice
56		end
57		return last_choice
58	end
59end
60
61do
62	local sin = math.sin
63
64	function math.sinlerp(min, max, t)
65		local h = (max - min) / 2
66		return min + h + sin(t) * h
67	end
68
69	local cos = math.cos
70
71	function math.coslerp(min, max, t)
72		local h = (max - min) / 2
73		return min + h + cos(t) * h
74	end
75end
76
77function math.lerp(a,b,t)
78	return (1-t)*a + t*b
79end
80
81function math.timeunits(seconds)
82	return {
83		years = seconds/3.154e7,
84		months = seconds/2.628e6,
85		weeks = seconds/604800,
86		days = seconds/86400,
87		hours = seconds/3600,
88		minutes = seconds/60,
89		seconds = seconds,
90		milliseconds = seconds * 1000,
91	}
92end
93
94do
95	local format = string.format
96
97	function math.toMinutesSeconds(num, fmt)
98		local m = num / 60
99		local s = num % 60
100		return format(fmt or "%02d:%02d", m, s)
101	end
102
103	function math.toMinSecMS(num, fmt)
104		local m = num / 60
105		local s = num % 60
106		local ms = (num % 1) * 100
107		return format(fmt or "%02d:%02d:%02d", m, s, ms % 100)
108	end
109end