2023-10-12 09:07:27 +00:00
|
|
|
function blockgame.get_keys (tab)
|
|
|
|
local keyset = {}
|
|
|
|
local n = 0
|
|
|
|
for key, value in pairs(tab) do
|
|
|
|
n = n + 1
|
|
|
|
keyset[n] = key
|
|
|
|
end
|
|
|
|
return keyset
|
|
|
|
end
|
|
|
|
|
|
|
|
function blockgame.shallow_copy_table (tab)
|
|
|
|
local result = {}
|
|
|
|
for key, value in pairs(tab) do
|
|
|
|
result[key] = value
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
2023-10-13 11:03:20 +00:00
|
|
|
|
2023-10-21 09:41:33 +00:00
|
|
|
function blockgame.naive_deep_copy (tab)
|
|
|
|
local result = {}
|
|
|
|
for key, value in pairs(tab) do
|
|
|
|
if type(value) == "table" then
|
|
|
|
result[key] = blockgame.naive_deep_copy(value)
|
|
|
|
else
|
|
|
|
result[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2023-10-13 11:03:20 +00:00
|
|
|
function blockgame.shuffle (tab)
|
|
|
|
local keys = blockgame.get_keys(tab)
|
|
|
|
local result = {}
|
|
|
|
while #keys > 0 do
|
|
|
|
local index = math.random(1, #keys)
|
|
|
|
local key = table.remove(keys, index)
|
|
|
|
table.insert(result, tab[key])
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
2023-10-17 09:19:55 +00:00
|
|
|
|
|
|
|
function blockgame.any (tab, check)
|
|
|
|
for key, value in pairs(tab) do
|
|
|
|
if check(value, key, tab) then return true end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function blockgame.every (tab, check)
|
|
|
|
for key, value in pairs(tab) do
|
|
|
|
if not check(value, key, tab) then return false end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function blockgame.underride (tab, template)
|
|
|
|
local tab = tab or {}
|
2023-10-21 09:41:33 +00:00
|
|
|
local result = blockgame.naive_deep_copy(template)
|
2023-10-17 09:19:55 +00:00
|
|
|
for key, value in pairs(tab) do
|
2023-10-21 09:41:33 +00:00
|
|
|
if type(value) == "table" then
|
|
|
|
result[key] = blockgame.underride(value, template[key] or {})
|
|
|
|
else
|
|
|
|
result[key] = value
|
|
|
|
end
|
2023-10-17 09:19:55 +00:00
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|