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
|
update_hooks = {}
-- https://pico-8.fandom.com/wiki/Qsort
function qsort(a, c, l, r)
c, l, r = c or function(a, b) return a < b end, l or 1, r or #a
if l < r then
if c(a[r], a[l]) then
a[l], a[r] = a[r], a[l]
end
local lp, k, rp, p, q = l + 1, l + 1, r - 1, a[l], a[r]
while k <= rp do
local swaplp = c(a[k], p)
-- "if a or b then else"
-- saves a token versus
-- "if not (a or b) then"
if swaplp or c(a[k], q) then
else
while c(q, a[rp]) and k < rp do
rp -= 1
end
a[k], a[rp], swaplp = a[rp], a[k], c(a[rp], p)
rp -= 1
end
if swaplp then
a[k], a[lp] = a[lp], a[k]
lp += 1
end
k += 1
end
lp -= 1
rp += 1
-- sometimes lp==rp, so
-- these two lines *must*
-- occur in sequence;
-- don't combine them to
-- save a token!
a[l], a[lp] = a[lp], a[l]
a[r], a[rp] = a[rp], a[r]
qsort(a, c, l, lp - 1)
qsort(a, c, lp + 1, rp - 1)
qsort(a, c, rp + 1, r)
end
return a
end
function filter(a, pred)
filtered = {}
for k, v in ipairs(a) do
if pred(v) then
filtered[k] = v
end
end
return filtered
end
function fmt(x)
t = type(x)
if t == "number" then
return "" .. x
elseif t == "string" then
return x
elseif t == "boolean" and x then
return "true"
elseif t == "boolean" and not x then
return "false"
end
return "_unknown_"
end
_button_states = {}
function _update_button_states(_dt)
for button = 0, 20 do
if btn(button) then
if _button_states[button] == "up" then
_button_states[button] = "just_pressed"
else
_button_states[button] = "held"
end
else
_button_states[button] = "up"
end
end
end
update_hooks[1] = _update_button_states
function button_just_pressed(id)
return _button_states[id] == "just_pressed"
end
|