info = { type = "image", title = "Cosmos in the wind", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Purple cosmos sway, a butterfly drifts by", api = 41, } local tr = ({ sk = { ["Visitor"] = "Návštevník", ["butterfly"] = "motýľ", ["bee"] = "včela", ["none"] = "žiadne", ["Wind"] = "Vietor", ["Light shifts warm/cool"] = "Svetlo sa mení teplé/studené" }, cs = { ["Visitor"] = "Návštěvník", ["butterfly"] = "motýl", ["bee"] = "včela", ["none"] = "žádné", ["Wind"] = "Vítr", ["Light shifts warm/cool"] = "Světlo se mění teplé/studené" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "visitor", label = T("Visitor"), type = "int", default = 0, opts = { T("butterfly"), T("bee"), T("none") } }, { key = "wind", label = T("Wind"), type = "int", min = 1, max = 5, default = 3 }, { key = "light", label = T("Light shifts warm/cool"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt, abs, atan = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.atan local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TOP, BOT = { 46, 26, 62 }, { 104, 66, 116 } local BOKEH = { 208, 160, 230 } local STEM, LEAF = { 62, 104, 72 }, { 84, 132, 88 } local BG = nil local function build_bg() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(TOP[1] + (BOT[1] - TOP[1]) * t + 0.5), floor(TOP[2] + (BOT[2] - TOP[2]) * t + 0.5), floor(TOP[3] + (BOT[3] - TOP[3]) * t + 0.5)) end local discs = { { 12, 46, 6, 0.16 }, { 52, 52, 5, 0.12 }, { 36, 58, 4, 0.14 }, { 8, 14, 4, 0.10 }, { 58, 22, 5, 0.09 } } for k = 1, #discs do local cx, cy, r, a = discs[k][1], discs[k][2], discs[k][3], discs[k][4] for dy = -r, r do local h = floor(sqrt((r + 0.5) * (r + 0.5) - dy * dy)) blend_rect(cx - h, cy + dy, 2 * h + 1, 1, BOKEH[1], BOKEH[2], BOKEH[3], a) end end BG = gfx.snapshot(0, 0, W, H) end local function make_head(R, rot, k, bluish) local S = 2 * floor(R + 0.999) + 3 local c = (S - 1) / 2 local cells, mask = {}, {} for y = 0, S - 1 do for x = 0, S - 1 do local dx, dy = x - c, y - c local r = sqrt(dx * dx + dy * dy) local th = atan(dy, dx) - rot local lobe = abs(cos(4 * th)) local rr = R * (0.64 + 0.36 * lobe ^ 0.6) local v = 0 if r <= rr then if r < 1.9 then v = 4 elseif r < 2.9 then v = 5 elseif lobe < 0.3 and r > 3.4 then v = 3 elseif r > rr - 1.5 and lobe > 0.55 then v = 2 elseif (dx + dy) > R * 0.6 then v = 3 else v = 1 end end cells[#cells + 1] = string.char(v) mask[#mask + 1] = string.char(v > 0 and 255 or 0) end end local bl = bluish and 14 or 0 local function col(r, g, b) return floor(r * k), floor(g * k), floor((b + bl) * k) end local pal = { 0, 0, 0 } for _, c3 in ipairs({ { 172, 134, 222 }, { 214, 184, 246 }, { 120, 84, 172 }, { 244, 184, 60 }, { 196, 112, 44 } }) do local r, g, b = col(c3[1], c3[2], c3[3]) pal[#pal + 1] = r; pal[#pal + 1] = g; pal[#pal + 1] = b end return { cells = table.concat(cells), mask = table.concat(mask), pal = pal, S = S, c = c, edge = { pal[10], pal[11], pal[12] } } end local FL = { { x = 22, y = 21, bx = 32, ph = 0.0, w = 1.38, amp = 2.6, R = 8.6, rot = 0.2, k = 1.0 }, { x = 45, y = 34, bx = 41, ph = 2.1, w = 1.14, amp = 2.2, R = 7.0, rot = 0.55, k = 0.92 }, { x = 49, y = 11, bx = 52, ph = 4.0, w = 1.56, amp = 2.8, R = 6.0, rot = 0.0, k = 0.78, blue = true }, { x = 8, y = 40, bx = 12, ph = 1.1, w = 1.26, amp = 1.6, R = 4.6, rot = 0.4, k = 0.62, blue = true }, } local built = 0 local function build_heads() built = built + 1 local f = FL[built] f.s = make_head(f.R, f.rot, f.k, f.blue) end local NM = 7 local MX, MY0, MV, MP = {}, {}, {}, {} for i = 1, NM do local h = hash32(i * 0x9E3779B1) MX[i] = 4 + umod(h, 56) MY0[i] = umod(h >> 8, 64) MV[i] = 1.5 + umod(h >> 14, 8) * 0.3 MP[i] = umod(h >> 20, 628) / 100.0 end local function put(s, fx, fy) local ix, iy = floor(fx), floor(fy) local ax, ay = fx - ix, fy - iy local S = s.S if ax > 0.05 then gfx.blend_mask(s.mask, s.edge[1], s.edge[2], s.edge[3], ix + 1, iy, S, S, S, S, ax) end if ay > 0.05 then gfx.blend_mask(s.mask, s.edge[1], s.edge[2], s.edge[3], ix, iy + 1, S, S, S, S, ay) end gfx.blit_indexed(s.cells, s.pal, ix, iy, S, S, S, S, true) end local function soft(x, y, w, h, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy rect(ix + 1, iy + 1, w - 1, h - 1, r, g, b) blend_rect(ix, iy + 1, 1, h - 1, r, g, b, 1 - fx) blend_rect(ix + w, iy + 1, 1, h - 1, r, g, b, fx) blend_rect(ix + 1, iy, w - 1, 1, r, g, b, 1 - fy) blend_rect(ix + 1, iy + h, w - 1, 1, r, g, b, fy) blend_rect(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)) blend_rect(ix + w, iy, 1, 1, r, g, b, fx * (1 - fy)) blend_rect(ix, iy + h, 1, 1, r, g, b, (1 - fx) * fy) blend_rect(ix + w, iy + h, 1, 1, r, g, b, fx * fy) end local function stem(hx, hy, bx, k) local r, g, b = floor(STEM[1] * k), floor(STEM[2] * k), floor(STEM[3] * k) local y = hy + 4 while y < H do local u = (y + 2 - hy) / (H - hy) if u > 1 then u = 1 end local x = bx + (hx - bx) * (1 - u) ^ 1.7 local ix = floor(x) local f = x - ix local h = (H - y < 4) and (H - y) or 4 blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + 1, y, 1, h, r, g, b, f) y = y + 4 end end local function leafpair(hx, hy, bx, yy, k, side) local u = (yy - hy) / (H - hy) local x = floor(bx + (hx - bx) * (1 - u) ^ 1.7 + 0.5) local r, g, b = floor(LEAF[1] * k), floor(LEAF[2] * k), floor(LEAF[3] * k) px(x + side, yy - 1, r, g, b); px(x + 2 * side, yy - 2, r, g, b); px(x + 3 * side, yy - 2, r, g, b) px(x - side, yy + 2, r, g, b); px(x - 2 * side, yy + 3, r, g, b) end local CYC = 16 local function win(len, t) local c = data.time if c.ok then return c.unix % len + c.ms / 1000 end return t % len end local function butterfly(p, t) if p < 1.33 or p >= 9.33 then return end local u = (p - 1.33) / 8 local x = -8 + 80 * u local y = 30 + 12 * sin(u * 9.0) + 1.4 * sin(t * 9.3) local wi = 1 + floor(abs(sin(t * 25.5)) * 2.4 + 0.5) soft(x - wi, y - 1, wi, 2, 250, 160, 56) soft(x + 1, y - 1, wi, 2, 250, 160, 56) soft(x - wi + 1, y + 1, wi - 1 > 0 and wi - 1 or 1, 1, 196, 98, 40) soft(x + 1, y + 1, wi - 1 > 0 and wi - 1 or 1, 1, 196, 98, 40) rect(floor(x + 0.5), floor(y - 1.5), 1, 4, 40, 24, 36) end local function bee(p, t) if p < 1.33 or p >= 11.33 then return end local u = (p - 1.33) / 10 local env = sin(u * 3.14159) local a = u * 26 local x = 72 - 84 * u + env * 7 * sin(a) local y = 36 - 10 * u + env * 6 * cos(a) soft(x - 1, y, 3, 2, 252, 208, 56) rect(floor(x + 0.5), floor(y), 1, 2, 38, 28, 24) local up = floor(t * 15) % 2 == 0 and -1 or 0 blend_rect(floor(x - 1), floor(y) - 1 + up, 3, 1, 255, 255, 255, 0.7) end function render() if not BG then build_bg() end if built < #FL then build_heads() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / gfx.FPS local wind = (cfg.wind or 3) / 3 for i = 1, NM do local y = 64 - (MY0[i] + MV[i] * t) % 72 + 4 local a = 0.3 + 0.3 * sin(t * 2.4 + MP[i]) local iy = floor(y) local f = y - iy blend_rect(MX[i], iy, 1, 1, 255, 224, 168, a * (1 - f)) blend_rect(MX[i], iy + 1, 1, 1, 255, 224, 168, a * f) end for i = #FL, 1, -1 do local f = FL[i] local gust = 0.75 + 0.25 * sin(t * 0.33 + f.ph) local hx = f.x + f.amp * wind * gust * sin(t * f.w + f.ph) local hy = f.y + 0.5 * wind * sin(t * f.w * 2 + f.ph) stem(hx, hy, f.bx, f.k) leafpair(hx, hy, f.bx, hy + 20, f.k, i % 2 == 0 and 1 or -1) if f.R > 6 then leafpair(hx, hy, f.bx, hy + 29, f.k, i % 2 == 0 and -1 or 1) end if f.s then put(f.s, hx - f.s.c, hy - f.s.c) end end local v = cfg.visitor or 0 local p = win(CYC, t) if v == 0 then butterfly(p, t) elseif v == 1 then bee(p, t) end if cfg.light ~= false then local s = sin(t * 0.186) if s > 0 then blend_rect(0, 0, W, H, 255, 150, 90, 0.13 * s) else blend_rect(0, 0, W, H, 110, 140, 255, -0.11 * s) end end end