info = { type = "image", title = "Moss frog", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A mossy frog breathes among fireflies", api = 33, } local tr = ({ sk = { ["Fireflies"] = "Svetlušky", ["Frog colour"] = "Farba žaby" }, cs = { ["Fireflies"] = "Světlušky", ["Frog colour"] = "Barva žáby" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Fireflies"), type = "int", min = 0, max = 12, default = 7 }, { key = "frog", label = T("Frog colour"), type = "color", default = "#7AB23C" }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, ceil, sin, sqrt = math.floor, math.ceil, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU = 6.2831853 local SKY, SKY_LO, HALO = { 16, 24, 68 }, { 30, 44, 100 }, { 34, 46, 112 } local MOON, MOON_D = { 240, 236, 196 }, { 200, 194, 150 } local STAR, STAR_D = { 232, 236, 255 }, { 104, 114, 176 } local BANK, REED, CATTAIL = { 12, 30, 36 }, { 22, 62, 50 }, { 88, 54, 36 } local WATER, RIPPLE, REFL = { 14, 38, 86 }, { 40, 76, 132 }, { 170, 166, 128 } local PAD, PAD_D, PAD_V = { 36, 104, 70 }, { 22, 70, 50 }, { 62, 136, 86 } local OUT = { 20, 44, 26 } local IRIS, PUPIL = { 238, 198, 64 }, { 18, 18, 22 } local LEAF, STEM = { 134, 216, 86 }, { 86, 160, 62 } local FLY, GLOW = { 236, 255, 124 }, { 160, 230, 70 } local C, CD, CL, BELLY, THROAT = {}, {}, {}, {}, {} local function frog_colours(c) C = { c.r, c.g, c.b } CD = { c.r * 5 // 8, c.g * 5 // 8, c.b * 5 // 8 } CL = { c.r + (240 - c.r) * 2 // 5, c.g + (250 - c.g) * 2 // 5, c.b + (170 - c.b) * 2 // 5 } BELLY = { c.r + (236 - c.r) * 3 // 5, c.g + (236 - c.g) * 3 // 5, c.b + (176 - c.b) * 3 // 5 } THROAT = { c.r + (252 - c.r) * 4 // 5, c.g + (250 - c.g) * 4 // 5, c.b + (210 - c.b) * 4 // 5 } end frog_colours({ r = 122, g = 178, b = 60 }) local BG = nil local NF = 7 function on_settings() frog_colours(cfg.frog or { r = 122, g = 178, b = 60 }) NF = cfg.n or 7 BG = nil end local function c3(c) return c[1], c[2], c[3] end local function ell(cx, cy, a, b, c) for y = floor(cy - b), ceil(cy + b) do local dy = (y + 0.5 - cy) / b if dy * dy < 1 then local hw = a * sqrt(1 - dy * dy) local x0, x1 = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, c3(c)) end end end end local FROG = { { 32, 43.5, 15, 8.5 }, { 32, 32.5, 13.5, 7.5 }, { 24.5, 25, 4.5, 4.5 }, { 39.5, 25, 4.5, 4.5 }, { 19, 47, 6, 4.5 }, { 45, 47, 6, 4.5 }, } local EYES = { 24.5, 39.5 } local function build() rect(0, 0, W, 34, c3(SKY)) rect(0, 34, W, 10, c3(SKY_LO)) for x = 0, W - 1, 2 do px(x, 33, c3(SKY_LO)) end ell(12, 11, 7.5, 7.5, HALO) ell(12, 11, 5.5, 5.5, MOON) ell(14.5, 9.5, 4.6, 4.6, HALO) px(9, 12, c3(MOON_D)); px(10, 14, c3(MOON_D)) for x = 0, W - 1 do local top = floor(41 + 1.5 * sin(x * 0.21) + sin(x * 0.53 + 1) + 0.5) rect(x, top, 1, 45 - top, c3(BANK)) end rect(0, 45, W, 19, c3(WATER)) local RD = { 1, 30, 3, 26, 5, 33, 54, 27, 56, 22, 58, 29, 60, 24, 62, 31 } for i = 1, #RD, 2 do local x, y = RD[i], RD[i + 1] rect(x, y, 1, 44 - y, c3(REED)) if i % 4 == 1 then rect(x, y + 2, 2, 5, c3(CATTAIL)) end end px(7, 31, c3(REED)); px(8, 30, c3(REED)); px(52, 30, c3(REED)); px(51, 29, c3(REED)) for i = 1, 11 do local h = hash32(i * 0x9E3779B1) local y = 46 + umod(h, 17) local x = umod(h >> 8, 60) rect(x, y, 3 + umod(h >> 16, 5), 1, c3(RIPPLE)) end ell(32, 55, 26, 6.5, PAD_D) ell(32, 54.5, 25, 5.5, PAD) for y = 55, 61 do local k = y - 55 rect(floor(44 + k * 1.4), y, 2 + floor(k * 1.3), 1, c3(WATER)) end local VN = { 14, 53, 18, 52, 20, 57, 48, 52, 52, 54, 26, 58, 40, 58 } for i = 1, #VN, 2 do px(VN[i], VN[i + 1], c3(PAD_V)) end for i = 1, #FROG do local f = FROG[i]; ell(f[1], f[2], f[3] + 1, f[4] + 1, OUT) end rect(9, 49, 10, 4, c3(OUT)); rect(45, 49, 10, 4, c3(OUT)) for i = 1, #FROG do local f = FROG[i]; ell(f[1], f[2], f[3], f[4], C) end rect(10, 50, 8, 2, c3(C)); rect(46, 50, 8, 2, c3(C)) px(12, 51, c3(OUT)); px(15, 51, c3(OUT)); px(48, 51, c3(OUT)); px(51, 51, c3(OUT)) ell(32, 45.5, 8, 5, BELLY) rect(23, 43, 5, 7, c3(OUT)); rect(36, 43, 5, 7, c3(OUT)) rect(21, 49, 9, 3, c3(OUT)); rect(34, 49, 9, 3, c3(OUT)) rect(24, 43, 3, 7, c3(C)); rect(37, 43, 3, 7, c3(C)) rect(22, 50, 7, 1, c3(C)); rect(35, 50, 7, 1, c3(C)) px(24, 50, c3(OUT)); px(26, 50, c3(OUT)); px(37, 50, c3(OUT)); px(39, 50, c3(OUT)) ell(28, 28.5, 6, 2, CL) for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) local x, y = 17 + umod(h, 30), 27 + umod(h >> 8, 20) local dx, dy = (x - 32) / 13, (y - 40) / 11 if dx * dx + dy * dy < 1 and not (y >= 34 and y <= 49 and x >= 23 and x <= 41) then rect(x, y, 2, 1, c3(((h >> 16) & 3) == 0 and CL or CD)) end end rect(23, 35, 18, 1, c3(OUT)); px(22, 34, c3(OUT)); px(41, 34, c3(OUT)) px(29, 31, c3(OUT)); px(34, 31, c3(OUT)) for i = 1, 2 do ell(EYES[i], 25, 2.6, 2.6, IRIS) px(floor(EYES[i]) - 2, 23, 255, 255, 255) end BG = gfx.snapshot(0, 0, W, H) end local FF = {} for i = 1, 12 do local h, g = hash32(i * 0x9E3779B1), hash32(i * 0x27D4EB2F) FF[i] = { cx = 6 + umod(h, 52), cy = 6 + umod(h >> 8, 34), ax = 4 + umod(h >> 16, 8), ay = 2 + umod(h >> 20, 5), px = 180 + umod(g, 8) * 30, py = 150 + umod(g >> 4, 8) * 25, pp = 60 + umod(g >> 8, 6) * 10, p1 = umod(g >> 12, 628) / 100, p2 = umod(g >> 20, 628) / 100, } end local TS = 0 local function firefly(f) local x = f.cx + f.ax * sin((TS % f.px) * TAU / f.px + f.p1) local y = f.cy + f.ay * sin((TS % f.py) * TAU / f.py + f.p2) local k = 0.5 + 0.5 * sin((TS % f.pp) * TAU / f.pp + f.p2) local b = k * 1.6 - 0.6 if b < 0 then b = 0 end local xi, yi = floor(x), floor(y) local fx, fy = x - xi, y - yi local w00, w10, w01, w11 = (1 - fx) * (1 - fy), fx * (1 - fy), (1 - fx) * fy, fx * fy local s = (0.45 + 0.55 * b) / math.max(w00, w10, w01, w11) local r, g, bl = c3(FLY) blend(xi, yi, r, g, bl, w00 * s) blend(xi + 1, yi, r, g, bl, w10 * s) blend(xi, yi + 1, r, g, bl, w01 * s) blend(xi + 1, yi + 1, r, g, bl, w11 * s) if b > 0.2 then local a = 0.4 * b blend(xi - 1, yi, GLOW[1], GLOW[2], GLOW[3], a); blend(xi + 2, yi + 1, GLOW[1], GLOW[2], GLOW[3], a) blend(xi + 1, yi - 1, GLOW[1], GLOW[2], GLOW[3], a); blend(xi, yi + 2, GLOW[1], GLOW[2], GLOW[3], a) end return x, y end local function hrun(x, y, w, c) local xi = floor(x); local f = x - xi local r, g, b = c[1], c[2], c[3] if f < 0.04 then rect(xi, y, w, 1, r, g, b) return end if w > 1 then rect(xi + 1, y, w - 1, 1, r, g, b) end blend(xi, y, r, g, b, 1 - f) blend(xi + w, y, r, g, b, f) end local LEAVES = { { -1, 28, 3 }, { -2, 26, 4 }, { -3, 25, 3 }, { -1, 32, 2 }, { -2, 32, 4 }, { -3, 34, 4 }, { -4, 36, 2 } } local STARS = {} for i = 1, 12 do local h = hash32(i * 0x632BE5AB) local x, y = umod(h, W), umod(h >> 8, 30) if (x - 12) * (x - 12) + (y - 11) * (y - 11) > 100 then STARS[#STARS + 1] = { x, y, umod(h >> 16, 90) } end end local LID = { 2, 4, 5, 5, 3, 1 } local STEP_HZ = 30 function render() TS = frame * STEP_HZ // gfx.FPS if not BG then build() end gfx.blit_rgb(BG, 0, 0, W, H) for i = 1, #STARS do local s = STARS[i] local c = ((TS + s[3]) % 90) < 60 and STAR or STAR_D px(s[1], s[2], c3(c)) end local sh = (TS // 9) % 3 rect(6 - sh, 46, 5 + sh, 1, c3(REFL)) rect(7 + sh % 2, 47, 3, 1, c3(REFL)) local br = sin((TS % 60) * TAU / 60) local a, b = 6 + 0.6 * br, 2.2 + 0.7 * br local tc = TS % 240 local e = 0 if tc < 40 then e = sin(3.14159 * tc / 40); a, b = a + 3.2 * e, b + 3.6 * e end for y = 36, ceil(36 + b) do local dy = (y + 0.5 - 36) / b if dy < 1 then local hw = a * sqrt(1 - dy * dy) local xl, xr = 32 - hw, 32 + hw local xa, xb = floor(xl), floor(xr) if xb - xa > 1 then rect(xa + 1, y, xb - xa - 1, 1, c3(THROAT)) end blend(xa, y, THROAT[1], THROAT[2], THROAT[3], xa + 1 - xl) blend(xb, y, THROAT[1], THROAT[2], THROAT[3], xr - xb) end end if e > 0.35 then px(28, 37, 255, 255, 255); px(29, 37, 255, 255, 255) end local lx = 32 for i = 1, NF do local x = firefly(FF[i]) if i == 1 then lx = x end end local bt = (TS + 45) % 130 local lid = (bt < 6) and LID[bt + 1] or 0 if tc >= 8 and tc < 30 then lid = 3 end for i = 1, 2 do local ex = floor(EYES[i]) local d = lx < ex - 6 and -1 or (lx > ex + 6 and 1 or 0) if lid < 3 then rect(ex - 1 + d, 25, 3, 1, c3(PUPIL)) end if lid > 0 then rect(ex - 3, 22, 7, lid, c3(C)) rect(ex - 3, 22 + lid, 7, 1, c3(CD)) end end local s = 1.4 * sin((TS % 96) * TAU / 96) + 0.5 * sin((TS % 41) * TAU / 41) + 1.2 * e for k = 1, 6 do local x = 31 + s * k / 6 local xi = floor(x); local f = x - xi blend(xi, 25 - k, STEM[1], STEM[2], STEM[3], 1 - f) blend(xi + 1, 25 - k, STEM[1], STEM[2], STEM[3], f) end for i = 1, #LEAVES do local l = LEAVES[i] hrun(l[2] + s, 19 + l[1], l[3], LEAF) end end