-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.lua
115 lines (94 loc) · 2.35 KB
/
util.lua
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
--Veins of the Earth
--From ToME git
function util.squareApply(x, y, w, h, fct)
for i = x, x + w do for j = y, y + h do
fct(i, j)
end end
end
--From Qi Daozei
--- Rounds a number, rounding .5 away from 0. See http://lua-users.org/wiki/SimpleRound
function math.round_simple(num)
if num >= 0 then return math.floor(num+.5)
else return math.ceil(num-.5) end
end
--From ToME2 port
-- Reverse of T-Engine's string.capitalize().
function string.uncapitalize(s)
if #s > 1 then
return s:sub(1, 1):lower() .. s:sub(2)
elseif #s == 1 then
return s:lower()
else
return s
end
end
--From StackOverflow via StarKeep
function util.remove_duplicates(t)
local hash = {}
local res = {}
for _,v in ipairs(t) do
if (not hash[v]) then
res[#res+1] = v
hash[v] = true
end
end
game.log("Called the filter")
return res
end
--From Reddit
function table.filter_duplicates(list, what)
assert(what~=nil, "filter key cannot be nil")
local dupes = {}
local result = {}
for i=1,#list do
local item = list[i]
-- game.log(item)
if not item[what] then game.log("No what in item") end
if not dupes[item[what]] then
dupes[item[what]] = true
result[#result+1] = item
end
end
return result
end
function table.filter_list(list, what)
for i, t in ipairs(list) do
local dupes = {}
local result = {}
for i, what in ipairs(t) do
game.log(t.what)
if not t.what then game.log("No what in item") return end
local item = t
local v = t.what
if not dupes[v] then
dupes[v] = true
result[#result+1] = t
end
-- if not dupes[item[what]] then
-- dupes[item[what]] = true
--[[ result[#result+1] = item
end]]
end
if not dupes.t then
result[#result+1] = t
end
end
return result
end
function table.remove_duplicates(list, what)
if _G.type(what) ~= "string" then game.log("What is not a string") end
for i, t in ipairs(list) do
local hash = {}
local res = {}
for _, what in ipairs(t) do
if not hash[what] then
res[#res+1] = what
hash[what] = true
list[#list+1] = res
end
end
end
local out = list
return out
-- return res
end