diff options
Diffstat (limited to 'util.lua')
| -rw-r--r-- | util.lua | 116 |
1 files changed, 71 insertions, 45 deletions
@@ -1,49 +1,51 @@ +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 +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 + for k, v in ipairs(a) do if pred(v) then filtered[k] = v end @@ -53,9 +55,33 @@ 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 "NA" + 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
\ No newline at end of file |
