info = { type = "image", title = "Apple time-lapse", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A blossom becomes an apple, ripens and falls", api = 44, } local tr = ({ sk = { ["Apple"] = "Jablko", ["Blossom"] = "Kvet", ["Red"] = "Červené", ["Golden"] = "Zlaté", ["Green"] = "Zelené", ["White"] = "Biely", ["Pink"] = "Ružový" }, cs = { ["Apple"] = "Jablko", ["Blossom"] = "Květ", ["Red"] = "Červené", ["Golden"] = "Zlaté", ["Green"] = "Zelené", ["White"] = "Bílý", ["Pink"] = "Růžový" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fruit", label = T("Apple"), type = "int", default = 0, opts = { T("Red"), T("Golden"), T("Green") } }, { key = "bloom", label = T("Blossom"), type = "int", default = 0, opts = { T("White"), T("Pink") } }, } local rect, blend_rect, blit, snapshot, blit_rgb = gfx.rect, gfx.blend_rect, gfx.blit_indexed, gfx.snapshot, gfx.blit_rgb local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local schar = string.char local CYC = 22 local T0 = 2 local TX, TY = 44, 25 local REST = 45 local GRAV = 180 local M = {} local function grid(w, h) local c = { w = w, h = h } for i = 1, w * h do c[i] = false end return c end local function part(c, inside, ch, hi, sh, ol) local w, h = c.w, c.h for i = 1, w * h do M[i] = false end for y = 0, h - 1 do for x = 0, w - 1 do local s = inside(x + 0.5, y + 0.5) if s then M[y * w + x + 1] = s end end end for y = 0, h - 1 do for x = 0, w - 1 do local i = y * w + x + 1 local s = M[i] if s then local edge = ol and (x == 0 or y == 0 or x == w - 1 or y == h - 1 or not M[i - 1] or not M[i + 1] or not M[i - w] or not M[i + w]) c[i] = edge and ol or (hi and s < -0.4 and hi) or (sh and s > 0.4 and sh) or ch end end end end local function ell(cx, cy, rx, ry) return function(x, y) local dx, dy = (x - cx) / rx, (y - cy) / ry if dx * dx + dy * dy <= 1 then return dx * 0.6 + dy * 0.8 end end end local function dots(c, ch, ...) local a = { ... } for k = 1, #a, 2 do c[a[k + 1] * c.w + a[k] + 1] = ch end end local function seal(c, keys) local t = {} for i = 1, c.w * c.h do local ch = c[i] t[i] = schar(ch and keys:find(ch, 1, true) - 1 or 0) end return table.concat(t) end local function put(sp, x, y, pal) blit(sp.s, pal, x, y, sp.w, sp.h, sp.w, sp.h, true) end local FKEYS, BKEYS = "?abcde", "?wxsyg" local function hsv(h, s, v) h = (h % 360) / 60 local i = floor(h) local f = h - i local p, q, t = v * (1 - s), v * (1 - s * f), v * (1 - s * (1 - f)) local r, g, b if i == 0 then r, g, b = v, t, p elseif i == 1 then r, g, b = q, v, p elseif i == 2 then r, g, b = p, v, t elseif i == 3 then r, g, b = p, q, v elseif i == 4 then r, g, b = t, p, v else r, g, b = v, p, q end return floor(r * 255 + 0.5), floor(g * 255 + 0.5), floor(b * 255 + 0.5) end local RIPE = { { 2, 0.86, 0.88 }, { 46, 0.82, 0.96 }, { 98, 0.72, 0.78 } } local FPAL = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 76, 40, 84, 170, 60 } local BPAL = { 0, 0, 0, 255, 248, 250, 255, 255, 255, 236, 170, 190, 255, 206, 60, 70, 150, 56 } local PETAL = { 255, 248, 250 } local function fruit_colour(k) local r = RIPE[(cfg.fruit or 0) + 1] local h, s, v = 100 + (r[1] - 100) * k, 0.72 + (r[2] - 0.72) * k, 0.78 + (r[3] - 0.78) * k FPAL[4], FPAL[5], FPAL[6] = hsv(h, s, v) FPAL[7], FPAL[8], FPAL[9] = hsv(h - 5, s * 0.55, v * 1.2 > 1 and 1 or v * 1.2) FPAL[10], FPAL[11], FPAL[12] = hsv(h + 4, s, v * 0.6) end function on_settings() local pink = (cfg.bloom or 0) == 1 if pink then BPAL[4], BPAL[5], BPAL[6], BPAL[7], BPAL[8], BPAL[9], BPAL[10], BPAL[11], BPAL[12] = 250, 190, 212, 255, 226, 236, 226, 118, 158 PETAL = { 250, 190, 212 } else BPAL[4], BPAL[5], BPAL[6], BPAL[7], BPAL[8], BPAL[9], BPAL[10], BPAL[11], BPAL[12] = 255, 248, 250, 255, 255, 255, 236, 170, 190 PETAL = { 255, 248, 250 } end end local FR, BL = {}, {} local function sp(c, keys) return { s = seal(c, keys), w = c.w, h = c.h } end local function build_fruit() for k = 1, 4 do local w = 2 * k + 1 local c = grid(w, w + 1) part(c, ell(w / 2, w / 2 + 1, w / 2, w / 2), "a", "b", "c") dots(c, "d", k, 0) if k >= 3 then dots(c, "d", k, 1) end FR[k] = sp(c, FKEYS) end local c = grid(11, 13) part(c, ell(5.5, 7.8, 5.4, 4.9), "a", "b", "c") for _, p in ipairs({ { 4, 3 }, { 5, 3 }, { 6, 3 }, { 5, 4 } }) do c[p[2] * 11 + p[1] + 1] = false end dots(c, "d", 5, 1, 5, 2, 5, 3) part(c, ell(8.2, 1.7, 2.6, 1.3), "e") FR[5] = sp(c, FKEYS) end local function build_bloom() local b = grid(5, 8) part(b, ell(2.5, 4.6, 2.2, 3.3), "w", "x", "s", "s") part(b, ell(2.5, 1.5, 1.9, 1.5), "g") BL[1] = sp(b, BKEYS) local h = grid(9, 9) part(h, ell(2.4, 5.2, 2.3, 3.2), "w", "x", "s", "s") part(h, ell(6.6, 5.2, 2.3, 3.2), "w", "x", "s", "s") part(h, ell(4.5, 6.4, 2.5, 2.4), "w", "x", "s", "s") part(h, ell(4.5, 1.6, 2.2, 1.5), "g") BL[2] = sp(h, BKEYS) local o = grid(13, 13) for k = 0, 4 do local a = (k * 72 - 90) / 360 * TAU part(o, ell(6.5 + 3.9 * cos(a), 6.8 + 3.9 * sin(a), 3.1, 3.1), "w", "x", "s", "s") end part(o, ell(6.5, 6.8, 2.2, 2.2), "y") BL[3] = sp(o, BKEYS) end local BG = nil local function leaf(x, y, dx) local rows = { { 1, 2 }, { 0, 4 }, { 0, 5 }, { 0, 4 }, { 1, 2 } } for r = 1, 5 do local a = rows[r] local sx = x + a[1] + floor((r - 3) * dx / 2) rect(sx, y + r - 1, a[2], 1, r <= 2 and 112 or 66, r <= 2 and 192 or 152, r <= 2 and 84 or 62) end rect(x + 1 + floor(-dx / 2), y + 2, 3, 1, 42, 112, 52) end local BG1 = nil local function build_bg() if BG1 then blit_rgb(BG1, 0, 0, W, H) else for y = 0, 45 do local k = y / 45 rect(0, y, W, 1, floor(92 + 100 * k + 0.5), floor(160 + 68 * k + 0.5), floor(220 + 20 * k + 0.5)) end for x = 0, W - 1 do local y = floor(40 + 2.2 * sin(x * 0.45) + 1.6 * sin(x * 0.17 + 1) + 0.5) rect(x, y, 1, 10, 70, 138, 84) end rect(0, 46, W, 18, 96, 178, 60) for i = 0, 8 do rect(0, 46 + i * 2 + 1, W, 1, 104, 186, 66) end rect(0, 60, W, 4, 76, 152, 50) for i = 1, 70 do local hh = math.hash32(i * 0x9E3779B1) local x, y = (hh & 0x7FFF) % W, 47 + ((hh >> 8) & 0x7FFF) % 17 if (hh >> 20) & 1 == 1 then rect(x, y, 1, 2, 138, 208, 92) else rect(x, y, 1, 2, 64, 138, 46) end end BG1 = snapshot(0, 0, W, H) return end for x = 0, 47 do local yc = 9 + x * 0.19 local th = floor(6 - x * 0.07 + 0.5) local y0 = floor(yc - th / 2 + 0.5) rect(x, y0, 1, th, 108, 70, 42) rect(x, y0, 1, 1, 150, 104, 62) rect(x, y0 + th - 1, 1, 1, 70, 44, 30) end rect(TX - 1, 16, 2, TY - 16, 100, 66, 40) rect(TX - 1, 16, 1, TY - 16, 140, 96, 58) local L = { { 4, 2, -1 }, { 12, 3, 1 }, { 18, 15, -1 }, { 26, 5, 1 }, { 30, 14, -1 }, { 37, 8, 1 }, { 40, 17, -1 }, { 46, 11, 1 }, { 50, 15, 1 }, { 8, 13, 1 }, { 22, 9, -1 }, { 34, 3, -1 }, { 47, 4, 1 }, { 2, 17, -1 } } for i = 1, #L do leaf(L[i][1], L[i][2], L[i][3]) end BG = snapshot(0, 0, W, H) BG1 = nil end local function ease(a, b, t) t = t < 0 and 0 or t > 1 and 1 or t; return a + (b - a) * t * t * (3 - 2 * t) end local T_HALF, T_OPEN, T_PETALS = 1.5, 3.0, 5.2 local T_S = { 5.2, 6.4, 7.8, 9.3, 11.0 } local T_RIPE0, T_RIPE1, T_DROP = 11.6, 15.6, 17.4 local function fall(u) local T1 = sqrt(2 * (REST - TY) / GRAV) local x = TX - 5 + 6 * u if u < T1 then return x, TY + 0.5 * GRAV * u * u end local v, w = GRAV * T1 * 0.5, u - T1 local tb = 2 * v / GRAV if w < tb then return x, REST - (v * w - 0.5 * GRAV * w * w) end w = w - tb v = v * 0.5 tb = 2 * v / GRAV if w < tb then return x, REST - (v * w - 0.5 * GRAV * w * w) end return x, REST end local function roll_x(u) local T1 = sqrt(2 * (REST - TY) / GRAV) local ub = T1 + (GRAV * T1 * 0.5) / GRAV * 2 * 1.5 if u < ub then return 0 end local w = u - ub return 6 * w + 3 * w * w end local function clouds(t) for k = 0, 1 do local cx = (6 + t * 1.1 + k * 44) % 92 - 22 local ix = floor(cx) local f = cx - ix local y = 4 + k * 12 local rows = { { 3, 14, y }, { 1, 18, y + 1 }, { 0, 20, y + 2 } } for r = 1, 3 do local a = rows[r] rect(ix + a[1] + 1, a[3], a[2] - 1, 1, 252, 252, 255) blend_rect(ix + a[1], a[3], 1, 1, 252, 252, 255, 1 - f) blend_rect(ix + a[1] + a[2], a[3], 1, 1, 252, 252, 255, f) end end end local function bee(t) local u = (t - 2.4) / 3.2 if u < 0 or u > 1 then return end local x, y if u < 0.35 then local k = ease(0, 1, u / 0.35); x, y = 72 - 21 * k, 12 + 13 * k elseif u < 0.7 then x, y = 51 + 1.5 * sin(t * 6), 25 + 1.5 * sin(t * 4.1) else local k = ease(0, 1, (u - 0.7) / 0.3); x, y = 51 + 22 * k, 25 - 16 * k end local ix, iy = floor(x), floor(y) rect(ix, iy + 1, 3, 2, 250, 200, 40) rect(ix + 1, iy + 1, 1, 2, 40, 30, 30) if (frame // 2) % 2 == 0 then rect(ix, iy - 1, 2, 1, 230, 240, 255) else rect(ix + 1, iy, 2, 1, 230, 240, 255) end end local function petals(t) for i = 0, 4 do local u = t - (T_PETALS + 0.12 * i) if u > 0 then local a = (i * 72 - 90) / 360 * TAU local x = TX + 4 * cos(a) + (9 + 2.5 * i) * u local y = TY + 6.8 + 4 * sin(a) + (5 + 1.2 * (i % 3)) * u + 1.5 * sin(u * 4 + i) if x < W then if (frame // 6 + i) % 2 == 0 then rect(floor(x), floor(y), 2, 1, PETAL[1], PETAL[2], PETAL[3]) else rect(floor(x), floor(y), 1, 2, PETAL[1], PETAL[2], PETAL[3]) end end end end end function render() if not FR[1] then build_fruit(); on_settings(); gfx.clear(150, 200, 236); gfx.ready(false) return end if not BL[1] then build_bloom(); gfx.clear(150, 200, 236); gfx.ready(false) return end if not BG then build_bg() end if not BG then gfx.ready(false) return end blit_rgb(BG, 0, 0, W, H) local t0 = T0 + frame / FPS local t = t0 % CYC clouds(t0) if t < T_PETALS then local s = t >= T_OPEN and BL[3] or t >= T_HALF and BL[2] or BL[1] put(s, TX - (s.w - 1) // 2, TY, BPAL) bee(t) else petals(t) end if t >= T_S[1] and t < T_DROP then local st = 1 for k = 2, 5 do if t >= T_S[k] then st = k end end local k = (t - T_RIPE0) / (T_RIPE1 - T_RIPE0) fruit_colour(k < 0 and 0 or k > 1 and 1 or k) local s = FR[st] local dx = 0 if t > T_DROP - 0.7 then dx = (frame // 3) % 2 == 0 and -1 or 1 end put(s, TX - (s.w - 1) // 2 + dx, TY, FPAL) elseif t >= T_DROP then local u = t - T_DROP local x, y = fall(u) x = x + roll_x(u) if x < W then fruit_colour(1) put(FR[5], floor(x + 0.5), floor(y + 0.5), FPAL) end end end