info = { type = "image", title = "Marble run", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Glass marbles roll, jump and ring a bell", api = 44, } local tr = ({ sk = { ["Marbles"] = "Guľôčky", ["Glass"] = "Sklo", ["Sea glass"] = "Morské sklo", ["Amber"] = "Jantár", ["Berry"] = "Lesné ovocie" }, cs = { ["Marbles"] = "Kuličky", ["Glass"] = "Sklo", ["Sea glass"] = "Mořské sklo", ["Amber"] = "Jantar", ["Berry"] = "Lesní ovoce" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Marbles"), type = "int", min = 1, max = 4, default = 3 }, { key = "tint", label = T("Glass"), type = "int", default = "sea", opts = { keys = { "sea", "amber", "berry" }, T("Sea glass"), T("Amber"), T("Berry") } }, } local rect, blend_rect, blend_mask = gfx.rect, gfx.blend_rect, gfx.blend_mask local blit_rgb, snapshot, clip = gfx.blit_rgb, gfx.snapshot, gfx.clip local floor, sqrt = math.floor, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local K, LIFT, BELL, CUP_IN = 487, 103, 291, 343 local HEX = "191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191919191a1c1d1e1f202223242527282a2b2d2f3032343637393b3d3f424446484b4d4f5254575a5c5f6265676a6d7073767a7d808386898c9093979b9ea2a6aaaeb2b6babec3c7cbd0d4d8dde1e5eaeae9e7e6e4e2dfdddbd8d6d4d2cfcccac7c4c1bebbb8b5b2afaca9a5a29e9b9794908d8985817d7975716d6965605c57534f4a45413c383637383a3b3d3e3f414345484a4d4f525457595c5f6264676a6e7174777a7e8184888b8f93969a9ea2a6a9adb1b5b9bdc1c4c8cccfd2d6d9dcdcdbdad8d7d5d4d3d1d0cecccbc9c8c6c4c2c1bfbdbbb9b7b5b3b1afadaba9a7a5a2a09e9c99979492908d8b888583807e7b787573706d6a6764615e5b5855524f4c4845423f3b3834312e2a2723201c191a1b1d1e1f202122232425262728292a2a2b2c2d2d2e2f2f303131323233333334343435353535353636363636363636353535353534343433333332323131302f2f2e2e2d2c2b2a2a292827262524232221201f1e1d1c1b1a1919191919191919191919191919191919191919191919191919f5f3f1efedebe9e7e5e3e1dfdddbd9d7d5d3d1cfcdcbc9c7c5c3c1bfbdbbb9b7b5b3b1afadaba9a7a5a3a19f9d9b99979593918f8d8b89878583817f7d7b79777573716f6d6b69676563615f5d5b59575553514f4d4b49474543413f3d3b39373533312f2d2b292929292929292929292a2a2a2b2b2b2c2c2c2d2d2d2e2e2e2f2f303031313232333334343535363637373839393a3b3d3e404345464748494a4a4b4c4d4e4f4f50515253545557585a5d5f6265686c6f706f7070707172737575767677777879797a7b7b7c7d7d7e7f8080818283848485868788898a8b8b8c8d8e8f909192939597999b9da0a3a6aaadb1b6b6b6b6b6b6b7b8b9babcbcbdbdbebfbfc0c1c1c2c3c3c4c5c6c6c7c8c9c9cacbcccdcfd0d2d5d7dadde0e0dfdedddddddddfdfe0e0e0e0e0e0e1e1e1e1e1e2e2e2e2e2e3e3e3e3e3e4e4e4e4e5e5e5e5e6e6e6e6e7e7e7e8e8e8e8e9e9e9eaeaeaebebebecececedededeeeeeeefefeff0f0f0f1f1f2f2f2f3f3f4f4f4f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f4f4f4f4f4f4f4f4f4f4f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f4f4f4f4f4f4f4f4f4f4f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5" local WALL, PEG = { 72, 82, 110 }, { 52, 60, 86 } local WL, WM, WD = { 238, 196, 130 }, { 200, 150, 92 }, { 132, 92, 58 } local SHAFT = { 40, 34, 52 } local GOLD, GOLD_L, GOLD_D = { 250, 210, 90 }, { 255, 240, 170 }, { 190, 140, 40 } local R = 2.3 local TINTS = { sea = { { { 28, 116, 128 }, { 112, 226, 214 } }, { { 32, 60, 150 }, { 116, 156, 255 } }, { { 40, 128, 84 }, { 156, 238, 164 } }, { { 96, 128, 160 }, { 214, 236, 248 } } }, amber = { { { 150, 86, 24 }, { 252, 196, 96 } }, { { 136, 56, 30 }, { 240, 140, 80 } }, { { 160, 120, 30 }, { 250, 226, 120 } }, { { 110, 120, 130 }, { 226, 230, 236 } } }, berry = { { { 132, 36, 96 }, { 244, 130, 196 } }, { { 70, 48, 150 }, { 176, 150, 255 } }, { { 120, 40, 60 }, { 240, 120, 130 } }, { { 96, 128, 160 }, { 214, 236, 248 } } }, } local TINT, N = TINTS.sea, 3 local function c3(c) return c[1], c[2], c[3] end local RAMPS = { { 4, 12.5, 8, 12.5 }, { 8, 12.5, 28, 16.5 }, { 33, 19.5, 50, 23.2 }, { 19, 38.8, 61, 29.6 }, { 11.5, 47, 40, 53 }, { 9, 63.5, 61, 57.4 }, } local LEGS = { { 18, 17 }, { 42, 24 }, { 36, 36 }, { 52, 32 }, { 24, 51 }, { 34, 53 }, { 30, 62 }, { 48, 60 } } local function ramp(r) local x1, y1, x2, y2 = r[1], r[2], r[3], r[4] local lo, hi = floor(x1 + 0.5), floor(x2 + 0.5) - 1 if x1 > x2 then lo, hi = floor(x2 + 0.5), floor(x1 + 0.5) - 1 end for x = lo, hi do local y = floor(y1 + (y2 - y1) * (x + 0.5 - x1) / (x2 - x1) + 0.5) rect(x, y, 1, 1, c3(WL)) rect(x, y + 1, 1, 2, c3(WM)) rect(x, y + 3, 1, 1, c3(WD)) end end local XS, YS = {}, {} local function decode(a, b) for i = a, b do XS[i] = tonumber(HEX:sub(2 * i - 1, 2 * i), 16) * 0.25 YS[i] = tonumber(HEX:sub(2 * (K + i) - 1, 2 * (K + i)), 16) * 0.25 end end local MASKS = {} local DISC = { { 2.4, 0, 0 }, { 1.7, -0.25, -0.25 }, { 0.85, -0.8, -0.9 } } local function mask(kind, px_, py_) return MASKS[kind * 16 + py_ * 4 + px_] end local function make(key) local kind, px_, py_ = key // 16, key % 4, key % 16 // 4 local d = DISC[kind] local cx, cy = 3 + px_ * 0.25 + d[2], 3 + py_ * 0.25 + d[3] local rows, row = {}, {} for j = 0, 6 do for i = 0, 6 do local n = 0 for sj = 0, 2 do local dy = j + (sj + 0.5) / 3 - cy for si = 0, 2 do local dx = i + (si + 0.5) / 3 - cx if dx * dx + dy * dy <= d[1] * d[1] then n = n + 1 end end end row[i + 1] = floor(n * 255 / 9) end rows[j + 1] = string.char(table.unpack(row, 1, 7)) end MASKS[key] = table.concat(rows) end local BG, PREV, STAGE = nil, nil, 1 local STAGES = { function() rect(0, 0, W, H, c3(WALL)) for y = 4, H - 1, 8 do for x = 4, W - 1, 8 do rect(x, y, 1, 1, c3(PEG)) end end end, function() for i = 1, #LEGS do local l = LEGS[i] rect(l[1], l[2], 2, 8, c3(WD)) end for i = 1, 3 do ramp(RAMPS[i]) end end, function() for i = 4, #RAMPS do ramp(RAMPS[i]) end end, function() for k = 16, 23 do make(k) end end, function() for k = 24, 31 do make(k) end end, function() for k = 32, 39 do make(k) end end, function() for k = 40, 47 do make(k) end end, function() for k = 48, 55 do make(k) end end, function() for k = 56, 63 do make(k) end end, function() decode(1, K // 3) end, function() decode(K // 3 + 1, 2 * K // 3) end, function() decode(2 * K // 3 + 1, K) end, function() rect(3, 6, 8, 58, c3(SHAFT)) rect(2, 6, 2, 58, c3(WM)); rect(2, 6, 1, 58, c3(WL)) rect(11, 22, 2, 34, c3(WM)); rect(11, 22, 1, 34, c3(WL)) rect(1, 0, 12, 7, c3(WM)); rect(1, 6, 12, 1, c3(WD)); rect(1, 0, 12, 1, c3(WL)) rect(5, 2, 4, 3, c3(WD)); rect(6, 3, 2, 1, c3(WL)) rect(61, 16, 2, 16, c3(WM)); rect(61, 16, 1, 16, c3(WL)) rect(56, 47, 8, 2, c3(WM)); rect(56, 47, 8, 1, c3(WL)) rect(59, 49, 1, 2, c3(WD)) BG = snapshot(0, 0, W, H) end, } local function marble(x, y, t) local ix, iy = floor(x), floor(y) local qx, qy = floor((x - ix) * 4), floor((y - iy) * 4) local rim, body = t[1], t[2] blend_mask(mask(1, qx, qy), rim[1], rim[2], rim[3], ix - 3, iy - 3, 7, 7, 7, 7, 1) blend_mask(mask(2, qx, qy), body[1], body[2], body[3], ix - 3, iy - 3, 7, 7, 7, 7, 1) blend_mask(mask(3, qx, qy), 255, 255, 255, ix - 3, iy - 3, 7, 7, 7, 7, 0.9) end local function cup(y) local iy = floor(y + 0.5) rect(4, iy, 7, 1, c3(WL)) rect(4, iy + 1, 7, 2, c3(WM)) rect(3, iy - 2, 1, 3, c3(WL)) end local function cup_y(f) if f < LIFT + 45 then return 63.5 - 15 * f / 30 end if f >= CUP_IN then local k = f - CUP_IN return k < 15 and 63.5 + 8 * (1 - k / 15) or 63.5 end end local SWING = { 1, 1, 1, 1, 0, -1, -1, 0, 1, 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, -1, 0 } local function bell(dt) local s = SWING[dt + 1] or 0 local x, y = 58 + s, 51 rect(x + 1, y, 3, 1, c3(GOLD)) rect(x, y + 1, 5, 2, c3(GOLD)) rect(x, y + 3, 6, 1, c3(GOLD_D)) rect(x + 1, y + 1, 1, 2, c3(GOLD_L)) rect(x + 2, y + 4, 1, 1, c3(GOLD_D)) if dt < 4 then blend_rect(56, 50, 1, 1, 255, 244, 190, 0.9 - dt * 0.2) blend_rect(55, 53, 1, 1, 255, 244, 190, 0.7 - dt * 0.15) end end function on_settings() TINT = TINTS[cfg.tint] or TINTS.sea N = cfg.n or 3 end function render() if not BG then if STAGE > 1 then blit_rgb(PREV, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if not BG then PREV = snapshot(0, 0, W, H) end gfx.ready(false) return end blit_rgb(BG, 0, 0, W, H) local hit = 99 clip(0, 7, W, H - 7) for i = 0, N - 1 do local f = (frame + i * K // N) % K local cy = cup_y(f) if cy then cup(cy) end local dt = f - BELL if dt >= 0 and dt < hit then hit = dt end end clip() bell(hit) for i = 0, N - 1 do local f = (frame + i * K // N) % K marble(XS[f + 1], YS[f + 1], TINT[i % 4 + 1]) end end