pico8-0hh1/utils.lua

62 lines
1.1 KiB
Lua
Raw Normal View History

local oldprint = print
2023-10-07 19:12:53 +02:00
function print(t, x, y, col1, col2)
if col2 then
for i = -1, 1 do
for j = -1, 1 do
oldprint(t, x + i, y + j, col2)
end
end
end
return oldprint(t, x, y, col1)
end
function str_width(str)
2023-10-07 19:12:53 +02:00
return print(str, 0, -8)
end
2023-10-07 19:12:53 +02:00
function print_shade(t, x, y, col1, col2)
print(t, x, y + 1, col2)
print(t, x + 1, y + 1, col2)
print(t, x, y, col1)
end
function set_state(state, ...)
2023-10-07 19:12:53 +02:00
local args = { ... }
if gs and gs._leave then gs._leave() end
gs = state
if gs and gs._enter then gs._enter(unpack(args)) end
2022-06-03 23:31:26 +02:00
end
--
-- Returns the indices of found occurences of `o` within `tbl`
--
function find(tbl, o)
2023-10-07 19:12:53 +02:00
local indices = {}
for k, v in ipairs(tbl) do
if v == o then add(indices, k) end
end
return indices
2022-06-03 23:31:26 +02:00
end
2022-07-01 19:50:08 +02:00
function contains(tbl, o)
2023-10-07 19:12:53 +02:00
for v in all(tbl) do
if v == o then return true end
end
return false
2022-07-01 19:50:08 +02:00
end
2022-06-03 23:31:26 +02:00
--
-- Makes a shallow table copy
--
function copy(tbl)
2023-10-07 19:12:53 +02:00
return map(tbl, function(o) return o end)
2022-06-03 23:31:26 +02:00
end
--
-- Table equality - shallow comparison
--
function equal(tbl1, tbl2)
2023-10-07 19:12:53 +02:00
for k, _ in ipairs(tbl1) do
if tbl1[k] ~= tbl2[k] then return false end
end
return true
end