fork/m-overlay

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

blackops7799Added MiniMap panel for offscreen player display More work on PlayerPercent to make it show Nana's percentd16d89b

gui
4.7 KiB223 linesraw
1local floor = math.floor
2local random = math.random
3
4local byte = string.byte
5local char = string.char
6local find = string.find
7local format = string.format
8local gmatch = string.gmatch
9local gsub = string.gsub
10local match = string.match
11local reverse = string.reverse
12local sub = string.sub
13local upper = string.upper
14
15local concat = table.concat
16local insert = table.insert
17
18do
19	-- With special chars: 33-126
20	-- without: 48-57, 65-90, 97-122 (this is going to be a bit messy without continue)
21	local num = 0 --recycling these up here since they get called a lot
22	local function numroll()
23		num = random(48, 122)
24		if (num > 57 and num < 65) or (num > 90 and num < 97) then
25			return numroll()
26		else
27			return num
28		end
29	end
30
31	local tab = {}
32	function string.random(len, specialchars)
33		tab = {}
34		if specialchars then
35			for i=1, len do
36				insert(tab, char(random(33, 126)))
37			end
38			return concat(tab)
39		else
40			for i=1, len do
41				insert(tab, char(numroll()))
42			end
43			return concat(tab)
44		end
45	end
46end
47
48function string.toTable(str)
49	local tbl = {}
50	
51	for i=1, #str do
52		tbl[i] = sub(str, i, i)
53	end
54	
55	return tbl
56end
57
58function string.explode(str, sep, pat)
59	if sep == "" then return string.toTable(str) end
60	
61	local ret = {}
62	local i, pos = 1, 1
63	
64	if not pat then sep = gsub(sep, "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1") end
65	
66	for startpos, endpos in gmatch(str, "()"..sep.."()") do
67		ret[i] = sub(str, pos, startpos - 1)
68		i = i + 1
69		pos = endpos
70	end
71	
72	ret[i] = sub(str, pos)
73	return ret
74end
75
76-- remove trailing and leading whitespace from string.
77function string.trim(s)
78	return gsub(s, "^%s*(.-)%s*$", "%1")
79end
80
81local trim = string.trim
82
83-- remove leading whitespace from string.
84function string.ltrim(s)
85	return gsub(s, "^%s*", "")
86end
87
88-- remove trailing whitespace from string.
89function string.rtrim(s)
90	local n = #s
91	while n > 0 and find(s, "^%s", n) do n = n - 1 end
92	return sub(s, 1, n)
93end
94
95function string.parseArgs(line)
96	local cmd, val = match(line, "(%S-)%s-=%s+(.+)")
97	if cmd and val then
98		return {trim(cmd), trim(val)}
99	end
100	local quote = sub(line, 1,1) ~= '"'
101	local ret = {}
102	for chunk in gmatch(line, '[^"]+') do
103		quote = not quote
104		if quote then
105			insert(ret,chunk)
106		else
107			for chunk in gmatch(chunk, "%S+") do -- changed %w to %S to allow all characters except space
108				insert(ret, chunk)
109			end
110		end
111	end
112	return ret
113end
114
115local pattern_escape_replacements = {
116	["("] = "%(",
117	[")"] = "%)",
118	["."] = "%.",
119	["%"] = "%%",
120	["+"] = "%+",
121	["-"] = "%-",
122	["*"] = "%*",
123	["?"] = "%?",
124	["["] = "%[",
125	["]"] = "%]",
126	["^"] = "%^",
127	["$"] = "%$",
128	["\0"] = "%z"
129}
130
131function string.escape(str)
132	return gsub(str, ".", pattern_escape_replacements)
133end
134
135function string.toSize(size)
136	size = tonumber(size) or 0
137
138	if size <= 0 then return "0" end
139	if size < 1024 then return format("%.2f Bytes", size) end
140	if size < 1024 * 1024 then return format("%.2f KB", size / 1024) end
141	if size < 1024 * 1024 * 1024 then return format("%.2f MB", size / (1024 * 1024)) end
142
143	return format("%.2f GB", size / (1024 * 1024 * 1024))
144end
145
146function string.upperFirst(str)
147	return gsub(str, "^%l", upper)
148end
149
150function string.addCommas(str)
151	str = tostring(str)
152	return reverse(gsub(gsub(reverse(str), "(...)", "%1,"), ",$", ""))
153end
154
155function string.niceNumber(num)
156	return string.addCommas(format("%0.0f", num))
157end
158
159function string.singularOrMuliple(str, num)
160	local fmt = format("%i %s", num, str)
161	return num == 1 and fmt or (fmt .. "s")
162end
163
164function string.AOrAn(s)
165	return match(s, "^h?[AaEeIiOoUu]") and "an" or "a"
166end
167
168function string.secondsToHuman(sec, accuracy)
169
170	local accuracy = accuracy or 2
171
172	local years = sec/31536000
173	local yearsRemainder = sec%31536000
174	local days = yearsRemainder/86400
175	local daysRemainder = sec%86400
176	local hours = daysRemainder/3600
177	local hourRemainder = (sec - 86400)%3600
178	local min = hourRemainder/60
179	local sec = sec%60
180	
181	years = floor(years)
182	days = floor(days)
183	hours = floor(hours)
184	min = floor(min)
185	sec = floor(sec)
186	
187	local results = {}
188	
189	if years >= 1 then
190		insert(results, string.singularOrMuliple("year", years))
191	end
192	if days >= 1 then
193		insert(results, string.singularOrMuliple("day", days))
194	end
195	if hours >= 1 then
196		insert(results, string.singularOrMuliple("hour", hours))
197	end
198	if min >= 1 then
199		insert(results, string.singularOrMuliple("minute", min))
200	end
201	if sec >= 1 then
202		insert(results, string.singularOrMuliple("second", sec))
203	end
204	
205	local result = {}
206	for i=1,accuracy do
207		result[ i ] = results[ i ]
208	end
209	
210	return concat(result, ", ")
211end
212
213function string.escapeURL(url)
214	return gsub(url, "([^A-Za-z0-9_])", function(c)
215		return format("%%%02x", byte(c))
216	end)
217end
218
219function string.enescapeURL(url)
220	return gsub(url, "%%(%x%x)", function(hex)
221		return char(tonumber(hex, 16))
222	end)
223end