info = { type = "image", title = "Cherries", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Two cherries sway under a leafy twig", api = 33, } local tr = ({ sk = { ["Fruit"] = "Ovocie", ["Cherries"] = "Čerešne", ["Sour cherries"] = "Višne", ["Golden"] = "Zlatá", ["Wind"] = "Vietor", ["Calm"] = "Pokojná", ["Breeze"] = "Vánok", ["Windy"] = "Veterno" }, cs = { ["Fruit"] = "Ovoce", ["Cherries"] = "Třešně", ["Sour cherries"] = "Višně", ["Golden"] = "Zlatá", ["Wind"] = "Vítr", ["Calm"] = "Klidná", ["Breeze"] = "Vánek", ["Windy"] = "Větrno" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fruit", label = T("Fruit"), type = "int", default = 0, opts = { T("Cherries"), T("Sour cherries"), T("Golden") } }, { key = "wind", label = T("Wind"), type = "int", default = 1, opts = { T("Calm"), T("Breeze"), T("Windy") } }, } local rect, px, blend, blit = gfx.rect, gfx.px, gfx.blend, gfx.blit_indexed local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local BGC = { 22, 32, 76 } local BARK, BARK_L, BARK_D = { 112, 70, 42 }, { 166, 112, 66 }, { 76, 46, 32 } local SR, SG, SB = 116, 204, 80 local KNOB = { 78, 160, 60 } local LEAF = { { 50, 148, 66 }, { 100, 200, 88 }, { 30, 106, 50 } } local FRUITS = { [0] = { { 150, 18, 48 }, { 214, 40, 74 }, { 252, 108, 132 }, { 104, 12, 36 }, { 255, 224, 230 } }, [1] = { { 106, 12, 42 }, { 158, 24, 62 }, { 204, 64, 100 }, { 76, 8, 32 }, { 255, 200, 214 } }, [2] = { { 204, 118, 30 }, { 242, 178, 52 }, { 255, 224, 126 }, { 160, 84, 24 }, { 255, 252, 228 } }, } local RUNS = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 25, 26, 27, 28, 29, 46, 47 }, { 1, 1, 2, 3 }, { 2, 1, 2, 3 }, { 3, 1, 2, 3 }, { 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, { 5, 1, 2 }, { 6, 1, 2 }, { 7, 1 }, { 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, { 11, 1 }, { 12, 1, 2, 3, 4, 5, 6, 7, 8 }, { 13, 1, 2 }, { 14, 1, 2, 3 }, { 15, 1, 2, 3 }, { 16, 1, 2, 3, 4 } } local UNZ = { ["\n"] = "" } do local c = 33 for _, r in ipairs(RUNS) do local v = string.char(r[1]) for k = 2, #r do if c == 93 then c = 94 end UNZ[string.char(c)] = v:rep(r[k]) c = c + 1 end end end local function unz(s) return (s:gsub(".", UNZ)) end local CR, SZ, OFF, Q = 8.5, 20, 10, 4 local SPRZ = { [[ 2KifJ,JNPpON7)LapQ:'LcT:%JdT@7$NdT@=#JeTA7"JdUA7"LObUB:"LP_WB:"JZC7"JZC7#NXC=$7WD7%:TE:':I:)7=E=7,8; 82]], [[ 3JjJ-LPpON:)JapQ='JcT=&dT@:$LdTA$NdTA:#OcUA:"JObVA="JP_WB=#[B:#NYC:#LXD%WD:%7TE='7I=*:E=:-7<72]], [[ 3JLiJ-LPqO:)JNaoQ=7'\bT=&NdS@=$JdTA7#LdTA:#NcUA=#ObVB#Q_VC#NZB=#LYC:#JXD7$NVD=&NTD='7=H=7):F:-7<72]], [[ 3JLiJ-LNPpOL*NaoR7'NcS@7%LdT@%eT@:#LdU@=#LdTB#NObUB7"NP_WB7"LZC#LYC=$YC:$:VE&=TE7'=I7):=E:-7<72]], [[ 4g/LNOpN:*JapQ7'JcS@7&dT@%LdT@:$eTA#JdUA7"LObUB:"LP`VB:"L[B:"JZC7#NXC=$:WD:%=TE='=@PF=):G:,;>;2]], [[ 4Jg.LNOpOL7)JN`pQ:'J\bT:&NcT@7$LdT@=$NdTA7#OcUA:"JObVA="JP`VB="J[B=#ZC:#LXD7#7WD:%:UD=':@PF=7(7=F:,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E=7':H=7)7:D=: .9(]] } local SPR = {} local function spr(q) local s = SPR[q] if not s then s = unz(SPRZ[q]); SPR[q] = s end return s end local PAL, GL = { 0, 0, 0 }, FRUITS[0][5] local function build_pal(F) PAL = { 0, 0, 0 } for s = 1, 4 do local c = F[s] for lvl = 1, 4 do local a = lvl / 4 for k = 1, 3 do PAL[#PAL + 1] = floor(BGC[k] + (c[k] - BGC[k]) * a + 0.5) end end end GL = F[5] end build_pal(FRUITS[0]) local function leaf_runs(bx, by, ang, L, Wd) local dx, dy = cos(ang), sin(ang) local litpos = 0.6 * dy - 0.8 * dx > 0 local out = {} local r = floor(L) + 2 for y = floor(by) - r, floor(by) + r do local cur, start = 0, 0 for x = floor(bx) - r, floor(bx) + r + 1 do local c = 0 local qx, qy = x + 0.5 - bx, y + 0.5 - by local u = qx * dx + qy * dy if u > 0 and u < L then local v = -qx * dy + qy * dx local w = Wd * sin(3.14159 * u / L) if v <= w and v >= -w then if v > -0.5 and v < 0.5 and u < L - 2 then c = 3 elseif (v > 0) == litpos then c = 2 else c = 1 end end end if c ~= cur then if cur ~= 0 then out[#out + 1] = { start, y, x - start, cur } end cur, start = c, x end end end return out end local function draw_runs(runs) for k = 1, #runs do local r = runs[k] local c = LEAF[r[4]] rect(r[1], r[2], r[3], 1, c[1], c[2], c[3]) end end local byte = string.byte local function draw_packed(s) for k = 1, #s, 4 do local x, y, w, c = byte(s, k, k + 3) local col = LEAF[c] rect(x, y, w, 1, col[1], col[2], col[3]) end end local LEAF_STEP = 0.09 local LEAFV = { [0] = "\37\4\3\2\35\5\8\2\34\6\10\2\33\7\6\2\39\7\4\3\43\7\2\1\32\8\7\3\39\8\5\1\33\9\10\1\34\10\8\1\36\11\4\1", "\36\5\5\2\34\6\9\2\33\7\11\2\32\8\11\3\43\8\2\2\32\9\12\1\34\10\9\1\35\11\7\1", "\38\5\1\2\35\6\7\2\33\7\10\2\32\8\2\3\34\8\10\2\32\9\2\1\34\9\9\3\43\9\2\2\33\10\11\1\34\11\9\1\36\12\5\1", "\36\6\4\2\34\7\8\2\32\8\1\3\33\8\10\2\32\9\1\1\33\9\6\3\39\9\5\2\33\10\6\1\39\10\4\3\43\10\2\2\34\11\11\1\35\12\8\1\37\13\3\1", "\34\7\7\2\32\8\10\2\32\9\4\3\36\9\7\2\33\10\3\1\36\10\4\3\40\10\4\2\34\11\6\1\40\11\3\3\43\11\2\2\34\12\11\1\36\13\6\1", "\35\7\4\2\32\8\9\2\32\9\3\3\35\9\7\2\33\10\2\1\35\10\3\3\38\10\5\2\33\11\5\1\38\11\3\3\41\11\3\2\34\12\6\1\40\12\3\3\43\12\1\2\35\13\10\1\36\14\6\1", "\33\8\7\2\32\9\2\3\34\9\7\2\32\10\2\1\34\10\2\3\36\10\6\2\33\11\3\1\36\11\3\3\39\11\4\2\33\12\5\1\38\12\3\3\41\12\2\2\34\13\7\1\41\13\2\3\43\13\1\2\35\14\9\1\37\15\4\1", "\33\8\5\2\32\9\2\3\34\9\6\2\32\10\1\1\33\10\2\3\35\10\6\2\33\11\2\1\35\11\2\3\37\11\5\2\33\12\4\1\37\12\2\3\39\12\3\2\34\13\5\1\39\13\2\3\41\13\2\2\34\14\6\1\40\14\2\3\42\14\1\2\36\15\7\1\43\15\1\2", "\32\9\1\3\33\9\6\2\32\10\1\1\33\10\2\3\35\10\5\2\32\11\2\1\34\11\2\3\36\11\5\2\33\12\3\1\36\12\2\3\38\12\4\2\33\13\4\1\37\13\2\3\39\13\3\2\34\14\5\1\39\14\2\3\41\14\1\2\35\15\5\1\40\15\1\3\41\15\2\2\36\16\7\1" } local FL0, FLT, FLN = 4.27, 6.5, 1.2 local BG = nil local function build_bg() rect(0, 0, W, H, BGC[1], BGC[2], BGC[3]) for x = 0, 57 do local y = floor(9.0 - x * 0.07 + 0.5) local th = x <= 48 and 3 or (x <= 54 and 2 or 1) rect(x, y - 1, 1, th, BARK[1], BARK[2], BARK[3]) px(x, y - 1, BARK_L[1], BARK_L[2], BARK_L[3]) if th == 3 then px(x, y + 1, BARK_D[1], BARK_D[2], BARK_D[3]) end end draw_runs(leaf_runs(13, 9.5, 2.45, 11, 3.2)) draw_runs(leaf_runs(50, 6.5, 0.9, 9, 2.6)) BG = gfx.snapshot(0, 0, W, H) end local P0X, P0Y = 30.5, 9.0 local ST = { { beta = -0.34, len = 30.5, ph = 0.0, bow = -0.14 }, { beta = 0.29, len = 34.0, ph = 0.7, bow = 0.11 }, } local WINDS = { [0] = { 0.05, 3.4 }, [1] = { 0.10, 2.8 }, [2] = { 0.16, 2.2 } } local WA, WW, WIND = 0.10, TAU / 2.8, 1 function on_settings() build_pal(FRUITS[cfg.fruit or 0] or FRUITS[0]) WIND = cfg.wind or 1 local wd = WINDS[WIND] or WINDS[1] WA, WW = wd[1], TAU / wd[2] end local function angle(s, t) local env = 0.8 + 0.2 * sin(t * 0.21) return s.beta + WA * env * (sin(WW * t + s.ph) + 0.3 * sin(1.63 * WW * t + 2 * s.ph + 1.1)) end local function stem(x0, y0, qx, x1, y1) local dy = y1 - y0 for y = floor(y0), floor(y1) do local t = (y + 0.5 - y0) / dy if t < 0 then t = 0 elseif t > 1 then t = 1 end local u = 1 - t local x = u * u * x0 + 2 * u * t * qx + t * t * x1 - 0.5 local xi = floor(x) local f = x - xi blend(xi, y, SR, SG, SB, 1 - f) if f > 0.02 then blend(xi + 1, y, SR, SG, SB, f) end end end local function glint(cx, cy, sw) local gx = cx - 3.6 - sw * 7 local gy = floor(cy - 4.6) local xi = floor(gx) local f = gx - xi blend(xi, gy, GL[1], GL[2], GL[3], 1 - f) px(xi + 1, gy, GL[1], GL[2], GL[3]) blend(xi + 2, gy, GL[1], GL[2], GL[3], f) blend(xi, gy + 1, GL[1], GL[2], GL[3], 0.55 * (1 - f)) blend(xi + 1, gy + 1, GL[1], GL[2], GL[3], 0.55 * f) end local CXs, CYs, SWs, QXs = { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS for i = 1, 2 do local s = ST[i] local a = angle(s, t) local al = angle(s, t - 0.22) CXs[i] = P0X + s.len * sin(a) CYs[i] = P0Y + s.len * cos(a) SWs[i] = a - s.beta QXs[i] = P0X + 0.5 * s.len * sin(al + s.bow) end for i = 1, 2 do local cx, cy = CXs[i], CYs[i] local ix, iy = floor(cx), floor(cy) local q = floor((cy - iy) * Q) * Q + floor((cx - ix) * Q) + 1 blit(spr(q), PAL, ix - OFF, iy - OFF, SZ, SZ, SZ, SZ, true) glint(cx, cy, SWs[i]) end for i = 1, 2 do stem(P0X, P0Y, QXs[i], CXs[i] + 0.3, CYs[i] - CR + 1.4) end rect(29, 8, 3, 2, KNOB[1], KNOB[2], KNOB[3]) local k = (t - FL0) % FLT local off = 0.03 * (WIND + 1) * sin(t * 1.3) if k < FLN then off = off + 0.34 * sin(k * 28.5) * (1 - k / FLN) end local li = floor(off / LEAF_STEP + 4.5) if li < 0 then li = 0 elseif li > 8 then li = 8 end draw_packed(LEAFV[li]) end