info = { type = "image", title = "Laughing flower", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A flower sways, laughs and gets a visitor", api = 41, } local tr = ({ sk = { ["Petal colour"] = "Farba lupeňov", ["Wind"] = "Vietor", ["Visitors"] = "Návštevníci" }, cs = { ["Petal colour"] = "Barva okvětních lístků", ["Wind"] = "Vítr", ["Visitors"] = "Návštěvníci" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "petals", label = T("Petal colour"), type = "color", default = "#FF7AA8" }, { key = "wind", label = T("Wind"), type = "int", min = 1, max = 3, default = 2 }, { key = "guest", label = T("Visitors"), type = "bool", default = true }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function c3(c) return c[1], c[2], c[3] end local function ell(cx, cy, rx, ry, c) for y = floor(cy - ry + 0.5), floor(cy + ry - 0.5) do local dy = (y + 0.5 - cy) / ry local k = 1 - dy * dy if k > 0 then local hw = rx * sqrt(k) local x0 = floor(cx - hw + 0.5) rect(x0, y, floor(cx + hw + 0.5) - x0, 1, c[1], c[2], c[3]) end end end local function rell(cx, cy, a, b, phi, c) local cs, sn = cos(phi), sin(phi) local a2, b2 = a * a, b * b local A = cs * cs / a2 + sn * sn / b2 local k1 = 2 * (cs * sn / a2 - sn * cs / b2) local k2 = sn * sn / a2 + cs * cs / b2 local hh = sqrt(a2 * sn * sn + b2 * cs * cs) for y = floor(cy - hh), floor(cy + hh) do local dy = y + 0.5 - cy local B, C = k1 * dy, k2 * dy * dy - 1 local disc = B * B - 4 * A * C if disc > 0 then local sq = sqrt(disc) local x0 = floor(cx + (-B - sq) / (2 * A) + 0.5) local x1 = floor(cx + (-B + sq) / (2 * A) + 0.5) if x1 > x0 then rect(x0, y, x1 - x0, 1, c[1], c[2], c[3]) end end end end local SKY1, SKY2 = { 150, 210, 255 }, { 196, 236, 255 } local HILL, HILL2, GRASS = { 120, 202, 100 }, { 84, 170, 84 }, { 56, 140, 70 } local GRASS_L = { 150, 226, 110 } local STEM, STEM_L, LEAF, LEAF_D = { 40, 150, 70 }, { 90, 200, 100 }, { 70, 180, 80 }, { 40, 128, 66 } local SKIN, EYE, CHEEK = { 255, 220, 150 }, { 40, 24, 40 }, { 255, 130, 130 } local MOUTH, TONGUE = { 150, 30, 56 }, { 255, 120, 150 } local PET, PET_L = { 255, 122, 168 }, { 255, 190, 214 } local HX0, HY0 = 32, 23 local BASE = 61 local BG = nil local function build_bg() rect(0, 0, W, 30, c3(SKY1)); rect(0, 30, W, 14, c3(SKY2)) for x = 0, W - 1, 2 do px(x, 29, c3(SKY2)) end ell(9.5, 8.5, 6, 6, { 255, 246, 190 }); ell(9.5, 8.5, 4, 4, { 255, 224, 100 }) ell(14, 52, 34, 14, HILL); ell(58, 50, 28, 12, HILL2) rect(0, 52, W, 12, c3(HILL2)); rect(0, 56, W, 8, c3(GRASS)) for x = 0, W - 1, 2 do px(x, 55, c3(GRASS)) end local cols = { { 255, 255, 255 }, { 255, 190, 60 }, { 255, 110, 130 } } for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), 44 + umod(h >> 8, 8) local c = cols[1 + i % 3] px(x, y, c3(c)) end for i = 1, 46 do local h = hash32(i * 0x85EBCA6B) local x = umod(h, W) local hh = 2 + umod(h >> 8, 4) rect(x, H - hh, 1, hh, c3(GRASS_L)); rect(x + 1, H - hh + 1, 1, hh - 1, c3(GRASS)) end ell(32, BASE + 1, 7, 2.5, { 150, 104, 70 }) BG = gfx.snapshot(0, 0, W, H) end function on_settings() local c = cfg.petals or { r = 255, g = 122, b = 168 } PET = { c.r, c.g, c.b } PET_L = { c.r + (255 - c.r) // 2, c.g + (255 - c.g) // 2, c.b + (255 - c.b) // 2 } end local function draw_cloud(f) local x = (floor(f * 3) + 30) % (W + 36) - 18 ell(x + 8, 9, 9, 3, { 255, 255, 255 }); ell(x + 5, 6.5, 5, 3, { 255, 255, 255 }); ell(x + 12, 7, 4.5, 2.5, { 255, 255, 255 }) end local VCYC = 18 local function guest_pos(u) if u < 1.67 then return -6 + u * 33, 14 + 4 * sin(u * 6) elseif u < 4.67 then return 49 + 3 * sin(u * 5.1), 14 + 3 * sin(u * 8.7) elseif u < 6 then return 49 + (u - 4.67) * 30, 14 - (u - 4.67) * 10.5 end return nil end local function win(len, f) local c = data.time if c.ok then return c.unix % len + c.ms / 1000 end return f % len end local function butterfly(x, y, f) local open = floor(f * 15) % 2 == 0 local wx = open and 3.2 or 1.6 ell(x - wx * 0.6 - 0.5, y - 1, wx, 2.6, { 255, 150, 60 }) ell(x + wx * 0.6 + 1.5, y - 1, wx, 2.6, { 255, 150, 60 }) ell(x - wx * 0.5 - 0.3, y + 1.8, wx * 0.7, 1.7, { 90, 120, 255 }) ell(x + wx * 0.5 + 1.3, y + 1.8, wx * 0.7, 1.7, { 90, 120, 255 }) rect(floor(x + 0.5), floor(y - 1.5), 1, 4, 30, 20, 40) end local function bee(x, y, f) local ix, iy = floor(x), floor(y) local up = floor(f * 30) % 2 == 0 and 3 or 2 rect(ix - 2, iy - up, 3, 2, 230, 244, 255); rect(ix + 1, iy - up, 2, 2, 230, 244, 255) ell(ix + 1.5, iy + 1.5, 4, 2.5, { 255, 208, 40 }) rect(ix, iy, 1, 3, 40, 28, 30); rect(ix + 3, iy, 1, 3, 40, 28, 30) px(ix - 3, iy + 1, 40, 28, 30) px(ix + 4, iy, 40, 28, 30) end local function head(cx, cy, f, tilt, laugh, blink) local rot = f * 0.36 + tilt for k = 0, 7 do local a = rot + k * 0.7854 local c = (k % 2 == 0) and PET or PET_L rell(cx + 8.5 * cos(a), cy + 8.5 * sin(a), 5.2, 2.9, a, c) end ell(cx, cy, 8, 8, SKIN) local ey = blink and 1 or 3 rect(cx - 5, cy - 3 + (3 - ey), 2, ey, c3(EYE)); rect(cx + 3, cy - 3 + (3 - ey), 2, ey, c3(EYE)) if not blink then px(cx - 5, cy - 3, 255, 255, 255); px(cx + 3, cy - 3, 255, 255, 255) end rect(cx - 6, cy + 1, 2, 1, c3(CHEEK)); rect(cx + 4, cy + 1, 2, 1, c3(CHEEK)) if laugh > 0 then local oh = laugh ell(cx, cy + 3.5 + oh * 0.2, 4, 0.8 + oh * 0.8, MOUTH) if oh >= 2 then rect(cx - 2, cy + 3 + oh, 4, 1, c3(TONGUE)) end rect(cx - 3, cy + 2, 6, 1, 255, 255, 255) else rect(cx - 3, cy + 4, 6, 1, c3(MOUTH)) px(cx - 4, cy + 3, c3(MOUTH)); px(cx + 3, cy + 3, c3(MOUTH)) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame / gfx.FPS draw_cloud(f) local wind = cfg.wind or 2 local amp = (2 + wind * 1.5) * (0.6 + 0.4 * sin(f * 0.33) ^ 2 * 2) local off = floor(amp * sin(f * 1.65) + 0.5) local lp = f % 4 local laugh, shake = 0, 0 if lp < 1.87 then laugh = 1 + floor(lp * 7.5) % 3 shake = floor(lp * 15) % 2 == 0 and 0 or 1 end local blink = f % 3.23 < 0.1 local top = HY0 + 4 for y = BASE, top, -2 do local s = (BASE - y) / (BASE - top) local x = HX0 + floor(off * s * s + 0.5) rect(x - 1, y, 2, 2, c3(STEM)); rect(x - 1, y, 1, 2, c3(STEM_L)) end local function stem_x(s) return HX0 + floor(off * s * s + 0.5) end local s1, s2 = 0.28, 0.5 rell(stem_x(s1) - 7, BASE - s1 * (BASE - top) - 3 + off * 0.1, 7, 2.8, -0.45 + off * 0.03, LEAF) rell(stem_x(s2) + 7, BASE - s2 * (BASE - top) - 2 + off * 0.1, 7, 2.8, 0.45 + off * 0.03, LEAF_D) head(HX0 + off + shake, HY0 + ((lp < 1.87 and floor(lp * 7.5) % 2 == 0) and 1 or 0), f, off * 0.04, laugh, blink) if cfg.guest ~= false then local u = win(VCYC, f) local vi = u // 9 local gu = u - vi * 9 if vi <= 1 then local x, y = guest_pos(gu) if x then if vi == 0 then butterfly(x, y, f) else bee(x, y, f) end end end end end