info = { type = "image", title = "Claw machine", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Claw grabs plush toys, sometimes wins one", api = 33, } local tr = ({ sk = { ["Cabinet"] = "Automat", ["Win chance"] = "Šanca výhry", ["Pink"] = "Ružový", ["Aqua"] = "Tyrkysový", ["Purple"] = "Fialový", ["Orange"] = "Oranžový" }, cs = { ["Cabinet"] = "Automat", ["Win chance"] = "Šance na výhru", ["Pink"] = "Růžový", ["Aqua"] = "Tyrkysový", ["Purple"] = "Fialový", ["Orange"] = "Oranžový" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "body", label = T("Cabinet"), type = "int", default = "pink", opts = { keys = { "pink", "aqua", "purple", "orange" }, T("Pink"), T("Aqua"), T("Purple"), T("Orange") } }, { key = "chance", label = T("Win chance"), type = "int", min = 1, max = 10, default = 3, unit = "/10" }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sqrt = math.floor, math.sqrt local hash32, umod = math.hash32, math.umod local BODY = { pink = { { 236, 92, 150 }, { 255, 150, 190 }, { 150, 40, 96 } }, aqua = { { 52, 170, 200 }, { 130, 220, 236 }, { 24, 96, 130 } }, purple = { { 130, 90, 210 }, { 190, 160, 250 }, { 72, 44, 136 } }, orange = { { 240, 130, 50 }, { 255, 190, 110 }, { 160, 70, 20 } }, } local STEEL, STEEL_D, EYE = { 196, 202, 220 }, { 120, 126, 152 }, { 50, 30, 60 } local WALL0, WALL1 = { 84, 74, 136 }, { 54, 46, 98 } local L, SLIP, REL = 330, 160, 246 local XH, SY = 54, 29 local SLOT_X = { 11, 25, 39 } local G, BOUNCE = 0.3, 0.2 local function ss(t) if t <= 0 then return 0 elseif t >= 1 then return 1 end return t * t * (3 - 2 * t) end local function pose_at(u, rel) return u < 114 and 0 or u < 122 and 1 or u < rel and 2 or u < rel + 6 and 1 or 0 end local PT, QT, ET, POSEW, POSEF = {}, {}, {}, {}, {} do local q, v, w, z = 0, 0, 0.233, 0.15 for u = 0, L - 1 do local p = ss((u - 8) / 50) - ss((u - 176) / 50) v = v + (-w * w * (q - p) - 2 * z * w * v) q = q + v PT[u + 1], QT[u + 1] = p, q ET[u + 1] = ss((u - 74) / 30) - ss((u - 140) / 30) POSEW[u + 1], POSEF[u + 1] = pose_at(u, REL), pose_at(u, SLIP) end end local PRONG = { { 2, 3, 4, 5, 6, 6 }, { 2, 3, 4, 4, 4, 3 }, { 2, 3, 3, 3, 3, 2 } } local DIGIT = {} for d = 0, 5 do DIGIT[tostring(d)] = string.char(d) end local function spr(rows) return (table.concat(rows):gsub("%d", DIGIT)) end local SPR = { spr { "01100110", "11111111", "13111131", "11122111", "11123211", "01111110", "11111111", "01100110" }, spr { "01100110", "01100110", "11111111", "13111131", "11122111", "01111110", "11111111", "01100110" }, spr { "00111100", "01111110", "13111131", "11144111", "11111111", "11222211", "01222210", "00300300" }, spr { "10000001", "11000011", "11111111", "13111131", "11144111", "01111110", "11111111", "01100110" }, } local function mk(a, b, c) return { 0, 0, 0, a[1], a[2], a[3], b[1], b[2], b[3], EYE[1], EYE[2], EYE[3], c[1], c[2], c[3] } end local PALS = { mk({ 255, 140, 176 }, { 255, 204, 220 }, { 230, 70, 120 }), mk({ 255, 164, 72 }, { 255, 216, 150 }, { 220, 100, 40 }), mk({ 120, 214, 196 }, { 200, 244, 232 }, { 60, 160, 150 }), mk({ 240, 238, 248 }, { 255, 255, 255 }, { 250, 150, 170 }), mk({ 255, 224, 96 }, { 255, 244, 176 }, { 240, 150, 60 }), mk({ 176, 150, 236 }, { 224, 210, 250 }, { 120, 90, 200 }), } local function plush(x, y, kind, col, shade) if shade then blend_rect(x - 1, y + 2, 10, 7, 24, 12, 48, 0.4) end gfx.blit_indexed(SPR[kind], PALS[col], x, y, 8, 8, 8, 8, true) end local function pick(a, b) local h = hash32(a * 0x9E3779B1 + b * 0x85EBCA6B) return umod(h, 4) + 1, umod(h >> 4, 6) + 1, umod(h >> 8, 3) end local BG, PATCH, SK = nil, {}, {} local B_MAIN, B_LIGHT, B_DARK = BODY.pink[1], BODY.pink[2], BODY.pink[3] local CHANCE = 3 local function c3(c) return c[1], c[2], c[3] end local function build() rect(0, 0, 64, 64, c3(B_MAIN)) rect(1, 10, 1, 36, c3(B_LIGHT)); rect(62, 10, 1, 36, c3(B_DARK)) rect(4, 1, 56, 9, c3(B_DARK)) local top = gfx.text_ink("CLAW", { font = "5x7" }) gfx.text(4, 2 - top, "CLAW", 255, 232, 120, { font = "5x7", w = 56, align = "center" }) rect(3, 10, 58, 37, B_DARK[1] * 6 // 10, B_DARK[2] * 6 // 10, B_DARK[3] * 6 // 10) for y = 11, 45 do local t = (y - 11) / 34 rect(4, y, 56, 1, floor(WALL0[1] + (WALL1[1] - WALL0[1]) * t + 0.5), floor(WALL0[2] + (WALL1[2] - WALL0[2]) * t + 0.5), floor(WALL0[3] + (WALL1[3] - WALL0[3]) * t + 0.5)) end rect(4, 12, 56, 1, 128, 128, 160); rect(4, 13, 56, 1, 66, 60, 100) for k = 0, 5 do local kind, col, j = pick(k, 1) if k % 2 == 1 then local k2, c2 = pick(k, 9) plush(4 + 7 * k, SY + 3, k2, c2, true) SK[(k + 1) // 2] = { 4 + 7 * k, kind, col } else plush(4 + 7 * k, SY - 2 + j, kind, col, true) end end for k = 0, 4 do local kind, col, j = pick(k, 2) plush(7 + 7 * k + umod(hash32(k * 77 + 5), 3) - 1, 33 + j, kind, col, true) end for k = 0, 5 do local kind, col = pick(k, 3) plush(4 + 7 * k, 38, kind, col, true) end for i = 1, 3 do PATCH[i] = gfx.snapshot(SLOT_X[i] - 1, SY, 10, 9) end for i = 1, 3 do plush(SK[i][1], SY, SK[i][2], SK[i][3], true) end rect(0, 47, 64, 1, c3(B_LIGHT)) rect(4, 50, 34, 12, c3(B_DARK)) rect(11, 58, 6, 2, c3(STEEL_D)); rect(14, 55, 2, 3, c3(STEEL)) rect(41, 51, 20, 11, c3(B_DARK)); rect(42, 52, 18, 9, 18, 10, 34); rect(42, 52, 18, 3, 34, 24, 56) BG = gfx.snapshot(0, 0, 64, 64) end local function win(n) return n >= 0 and umod(hash32(n * 0x9E3779B1 + 7), 10) < CHANCE end local ORDER = { 2, 1, 3 } local function fall(y0, rest, t) local T0 = sqrt(2 * (rest - y0) / G) if t < T0 then return y0 + 0.5 * G * t * t, false end local v1 = BOUNCE * G * T0 local tt = t - T0 if tt < 2 * v1 / G then return rest - (v1 * tt - 0.5 * G * tt * tt), false end return rest, true end function on_settings() local b = BODY[cfg.body] or BODY.pink B_MAIN, B_LIGHT, B_DARK = b[1], b[2], b[3] CHANCE = cfg.chance or 3 BG = nil end local BULB_ON, BULB_OFF = { 255, 240, 150 }, { 150, 104, 60 } function render() if not BG then build() end gfx.blit_rgb(BG, 0, 0, 64, 64) local m, u = frame // L, frame % L local i = u + 1 local sl = ORDER[m % 3 + 1] local sc = SLOT_X[sl] + 4 local d = XH - sc local p, q = PT[i], QT[i] local cCar, cCl = XH - d * p, XH - d * q local ly = 16 + 10 * ET[i] local won = win(m) local pose = (won and POSEW or POSEF)[i] local ph = frame // 8 % 2 for k = 0, 13 do local c = (k + ph) % 2 == 0 and BULB_ON or BULB_OFF px(6 + 4 * k, 0, c3(c)) end local held = nil if u >= 140 then local px0 = SLOT_X[sl] local cl = floor(cCl + 0.5) - 4 if won then if u < REL then held = { cl, ly + 3 } else local ty = 19 + 0.5 * G * (u - REL) * (u - REL) if ty < 36 then held = { floor(XH + 0.5) - 4, ty } end end gfx.blit_rgb(PATCH[sl], px0 - 1, SY, 10, 9) elseif u < SLIP then held = { cl, ly + 3 } gfx.blit_rgb(PATCH[sl], px0 - 1, SY, 10, 9) else local cs = floor((XH - d * QT[SLIP + 1]) + 0.5) - 4 local ys = 16 + 10 * ET[SLIP + 1] + 3 local y, done = fall(ys, SY, u - SLIP) if not done then held = { cs + (px0 - cs) * math.min(1, (u - SLIP) / 8), y } gfx.blit_rgb(PATCH[sl], px0 - 1, SY, 10, 9) end end if held then plush(floor(held[1] + 0.5), floor(held[2] + 0.5), SK[sl][2], SK[sl][3]) end end local sp = ORDER[(m - 1) % 3 + 1] if m > 0 and win(m - 1) and sp ~= sl then local t = u - 4 local y, done = fall(3, SY, t < 0 and 0 or t) if not done then gfx.blit_rgb(PATCH[sp], SLOT_X[sp] - 1, SY, 10, 9) if t >= 0 then gfx.clip(4, 11, 56, 35) plush(SLOT_X[sp], floor(y + 0.5), SK[sp][2], SK[sp][3]) gfx.clip() end end end local xc, xl = floor(cCar + 0.5), floor(cCl + 0.5) rect(xc - 3, 12, 6, 3, c3(STEEL_D)); rect(xc - 3, 12, 6, 1, c3(STEEL)) local n = floor(ly) - 15 for k = 0, n - 1 do px(floor(xc + (xl - xc) * (k + 0.5) / n + 0.5), 15 + k, c3(STEEL)) end local y = floor(ly + 0.5) rect(xl - 3, y, 6, 2, c3(STEEL)); rect(xl - 2, y + 2, 4, 1, c3(STEEL_D)) local pr = PRONG[pose + 1] for k = 1, 6 do px(xl - 1 - pr[k], y + 2 + k, c3(STEEL)); px(xl + pr[k], y + 2 + k, c3(STEEL)) end rect(48, 36, 12, 10, c3(B_DARK)); rect(48, 36, 12, 1, c3(B_LIGHT)) local dp = (PT[i + 1] or 0) - p local lean = dp > 0.001 and -1 or dp < -0.001 and 1 or 0 rect(14 + lean, 55, 2, 1, c3(STEEL)); rect(13 + lean, 52, 4, 3, 240, 60, 70); px(13 + lean, 52, 255, 170, 170) local mv = lean ~= 0 rect(24, 54, 4, 4, mv and 255 or 120, mv and 230 or 100, mv and 90 or 40) rect(30, 54, 4, 4, 70, mv and 220 or 100, mv and 240 or 130) if won and u >= REL + 4 and u < REL + 60 and (u // 5) % 2 == 0 then rect(42, 49, 18, 1, 255, 240, 150) end end