-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseful.lua
More file actions
110 lines (92 loc) · 3.05 KB
/
useful.lua
File metadata and controls
110 lines (92 loc) · 3.05 KB
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
--[[
just a collection of useful functions
either because they are general purpose or
for deubugging.
]]
Useful = {}
-- help with dev
function Useful.printTable(t, indent)
-- used to help with debugging to get a recursive view of tables within tables
indent = indent or ""
for k, v in pairs(t) do
if type(v) == "table" then
print(indent .. tostring(k) .. ":")
Useful.printTable(v, indent .. " ")
else
print(indent .. tostring(k) .. ": " .. tostring(v))
end
end
end
function Useful.value_not_in_table(tbl, val)
-- checks if a value is NOT in a table
for _, v in ipairs(tbl) do
if v == val then
return false -- value *is* in the table
end
end
return true -- value *is not* in the table
end
function Useful.RGB255ToRGB1(rgb)
-- love2d uses colours as 1 being max 0 being min so .5 would be half of a colour value
-- im used to using 255 colours and this is also how I identify colour picking so this function makes life easy
return { r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255 }
end
function Useful.IntToBool(val)
-- lua doesnt support 1 being equivalent to true or 0 being equivalent to false
-- the events table uses 1 and 0 for true and false
-- i mostly stick to checking for those values but using true and false can be useful sometimes
if val >= 1 then return true else return false end
end
function Useful.fisherYates(tbl)
-- a fisher yates shuffle
-- used quite a lot for adding elements to a table in a random order
-- and for shuffline keys to get unique additions
local n = #tbl
for i = n, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
-- Function to shuffle using weighted keys
function Useful.weightedShuffle(deck)
local shuffled = {}
for _, card in ipairs(deck) do
-- Generate a random number in (0,1)
local u = math.random()
while u == 0 do u = math.random() end -- avoid log(0)
-- Use Gumbel-Max trick to compute a key
local key = math.log(card.weight) - math.log(-math.log(u))
table.insert(shuffled, { card = card, key = key })
end
-- Sort by the key descending
table.sort(shuffled, function(a, b)
return a.key > b.key
end)
-- Extract the shuffled cards
local result = {}
for _, entry in ipairs(shuffled) do
table.insert(result, entry.card)
end
return result
end
function Useful.invertTable(myTable)
table.sort(idxToRemove, function(a, b) return a > b end)
end
function Useful.deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[Useful.deepcopy(orig_key)] = Useful.deepcopy(orig_value)
end
setmetatable(copy, Useful.deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function Useful.tableShallowCopy()
end
return Useful