fork/m-overlay

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

BkacjiosAdded support for Scooby-Doo: Night of 100 Frights (#139)94c74ee

v2.1.1
5.6 KiB276 linesraw
1function table.merge(a,b)
2	for k,v in pairs(b) do
3		if not a[k] then a[k]=v end
4	end
5	return a
6end
7
8function table.clone(a)
9	local ret = {}
10	for k,v in pairs(a) do
11		ret[k] = type(v) == "table" and table.clone(v) or v
12	end
13	return ret
14end
15
16local function reversedipairsiter(t, i)
17    i = i - 1
18    if i ~= 0 then
19        return i, t[i]
20    end
21end
22
23function reversedipairs(t)
24    return reversedipairsiter, t, #t + 1
25end
26
27function table.hasValue(tbl, val)
28	for key, value in pairs(tbl) do
29		if (value == val) then return true, key end
30	end
31	return false
32end
33
34do
35	local recursiveTableString
36	local stack = {}
37	local cache = {}
38
39	local KEY_TYPE_DOT = 1
40	local KEY_TYPE_BRACKET = 2
41
42	local function val_to_str(v, name)
43		if type(v) == "string" then
44			v = string.gsub(v, "\n", "\\n" )
45			if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then
46				return "'" .. v .. "'"
47			end
48			return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
49		elseif type(v) == "table" then
50			return recursiveTableString(v, name)
51		else
52			return tostring(v)
53		end
54	end
55
56	local function key_type(k)
57		if type(k) == "string" and string.match(k, "^[_%a][_%a%d]*$") then
58			return KEY_TYPE_DOT
59		else
60			return KEY_TYPE_BRACKET
61		end
62	end
63
64	local function key_to_str(k, name)
65		if key_type(k) == KEY_TYPE_DOT then
66			return k
67		else
68			return "[" .. val_to_str(k, name) .. "]"
69		end
70	end
71
72	local function getStackName()
73		local result = ""
74		for pos, name in ipairs(stack) do
75			if key_type(name) == KEY_TYPE_DOT and pos > 1 then
76				result = result .. "." .. key_to_str(name)
77			else
78				result = result .. key_to_str(name)
79			end
80		end
81		return result
82	end
83
84	local function getTabs()
85		return string.rep("\t", #stack)
86	end
87
88	recursiveTableString = function (tbl, name)
89		name = name or "self"
90
91		local result = "{\n"
92
93		if cache[tbl] then
94			return cache[tbl]
95		end
96
97		table.insert(stack, name)
98		cache[tbl] = getStackName()
99
100		for k, v in pairs(tbl) do
101			result = result .. getTabs() .. key_to_str(k, k) .. " = " .. val_to_str(v, k) .. "\n"
102		end
103		table.remove(stack, #stack)
104
105		return result .. getTabs(stack) .. "}"
106	end
107
108	function table.tostring(tbl)
109		stack = {}
110		cache = {}
111		return recursiveTableString(tbl)
112	end
113
114	function table.print(tbl)
115		print(table.tostring(tbl))
116	end
117end
118
119--[[local test = {
120	1, 2, 5,
121	["Kappa 1 2 3 \n lul"] = true,
122	data = {
123		1337.84239,
124		test = {
125			7, 8, "NINE"
126		}
127	}
128}
129
130table.insert(test.data, test)
131test.data.test[3] = test
132test[58] = test.data.test
133
134print(table.tostring(test))]]
135
136function table.copy(object)
137	local lookup_table = {}
138	local function _copy(object)
139		if type(object) ~= "table" then
140			return object
141		elseif lookup_table[object] then
142			return lookup_table[object]
143		end
144		local new_table = {}
145		lookup_table[object] = new_table
146		for index, value in pairs(object) do
147			new_table[_copy(index)] = _copy(value)
148		end
149		return setmetatable(new_table, getmetatable(object))
150	end
151	return _copy(object)
152end
153
154function table.concatNumberedList(tbl, sep, oxford)
155	if type(tbl) ~= "table" then
156		return tostring(tbl)
157	end
158
159	local str = ""
160	local num = #tbl
161	for i=1,num do
162		if i < num then
163			-- If we aren't at the end of the list, use commas
164			-- If we're one before the end of the list, use an oxford comma if we want it
165			str = str .. i .. ". " .. tbl[i] .. ((oxford or i < num - 1) and ", " or " ")
166		elseif i > 1 then
167			-- If we're the last item and we have more than 1 item already..
168			-- Seperate using our defined seperator word
169			str = str .. (sep or "and") .. " " .. i .. ". " .. tbl[i]
170		else
171			-- Single item..
172			str = i .. ". " .. tbl[i]
173		end
174	end
175	return str
176end
177
178function table.concatList(tbl, sep, oxford)
179	if type(tbl) ~= "table" then
180		return tostring(tbl)
181	end
182
183	local str = ""
184	local num = #tbl
185	for i=1,num do
186		if i < num then
187			-- If we aren't at the end of the list, use commas
188			-- If we're one before the end of the list, use an oxford comma if we want it
189			str = str .. tbl[i] .. ((oxford or i < num - 1) and ", " or " ")
190		elseif i > 1 then
191			-- If we're the last item and we have more than 1 item already..
192			-- Seperate using our defined seperator word
193			str = str .. (sep or "and") .. " " .. tbl[i]
194		else
195			-- Single item..
196			-- Don't even bother concating the string
197			str = tbl[i]
198		end
199	end
200	return str
201end
202
203function table.max(tbl)
204	local max_key = nil
205	local max_value = -math.huge
206	for key, value in pairs(tbl) do
207		value = assert(tonumber(value), "expected a table full of numbers")
208		if value > max_value then
209			max_key = key
210			max_value = value
211		end
212	end
213	return max_key, max_value
214end
215
216function table.min(tbl)
217	local min_key = nil
218	local min_value = math.huge
219	for key, value in pairs(tbl) do
220		value = assert(tonumber(value), "expected a table full of numbers")
221		if value < min_value then
222			min_key = key
223			min_value = value
224		end
225	end
226	return min_key, min_value
227end
228
229function table.avg(tbl)
230	local num = 0
231	local total = 0
232	for key, value in pairs(tbl) do
233		value = assert(tonumber(value), "expected a table full of numbers")
234		total = total + value
235		num = num + 1
236	end
237	return total / num
238end
239
240function table.sum(tbl)
241	local total = 0
242	for key, value in pairs(tbl) do
243		value = assert(tonumber(value), "expected a table full of numbers")
244		total = total + value
245	end
246	return total
247end
248
249function table.keys(tbl)
250	local keys = {}
251	for k,v in pairs(tbl) do
252		table.insert(keys, k)
253	end
254	return keys
255end
256
257function table.random(tbl)
258	return tbl[math.random(1, #tbl)]
259end
260
261function table.randomkey(tbl)
262	local keys = table.keys(tbl)
263	return keys[math.random(1, #keys)]
264end
265
266function table.randomkeyvalue(tbl)
267	local key = table.randomkey(tbl)
268	return key, tbl[key]
269end
270
271function table.shuffle(tbl)
272	for i = #tbl, 2, -1 do
273		local j = math.random(i)
274		tbl[i], tbl[j] = tbl[j], tbl[i]
275	end
276end