fork/m-overlay

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

BkacjiosMore stuff to allow the use of the in-game volume slider to adjust M'Overlays volume!57e394e

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