info = { type = "image", title = "Frog pond", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A frog on a lily pad catches a fly", api = 33, } local tr = ({ sk = { ["Frog colour"] = "Farba žaby", ["Croak"] = "Kvákanie", ["Dragonfly"] = "Vážka" }, cs = { ["Frog colour"] = "Barva žáby", ["Croak"] = "Kvákání", ["Dragonfly"] = "Vážka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "frog", label = T("Frog colour"), type = "color", default = "#38AA44" }, { key = "ribbit", label = T("Croak"), type = "bool", default = true }, { key = "dragon", label = T("Dragonfly"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, sqrt, abs = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local SKY, CLOUD = { 188, 228, 250 }, { 255, 255, 255 } local TREES, TREES_L = { 46, 118, 76 }, { 66, 146, 92 } local WATER1, WATER2 = { 48, 140, 168 }, { 62, 158, 186 } local GLINT, RING1, RING2 = { 168, 226, 240 }, { 190, 238, 248 }, { 120, 196, 218 } local PAD, PAD_D, PAD_L = { 60, 170, 78 }, { 34, 116, 58 }, { 98, 196, 104 } local REED, CATT = { 64, 134, 58 }, { 126, 82, 48 } local PETAL, PETAL_D = { 252, 164, 200 }, { 232, 112, 166 } local MOUTH, TONGUE, TONGUE_T = { 24, 70, 40 }, { 246, 128, 156 }, { 232, 70, 112 } local IRIS, BLACK = { 250, 214, 64 }, { 22, 18, 24 } local FLY, WING = { 34, 28, 30 }, { 236, 248, 255 } local FROG, DARK, BELLY, SAC, SACE = { 56, 170, 68 }, { 30, 112, 52 }, { 180, 234, 116 }, { 230, 246, 160 }, { 100, 172, 74 } local MX, MY = 32, 38 local FX0, FY0 = 52, 24 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), ceil(cy + ry) - 1 do local dy = (y + 0.5 - cy) / ry local q = 1 - dy * dy if q > 0 then local hw = rx * sqrt(q) local xa, xb = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if xb >= xa then rect(xa, y, xb - xa + 1, 1, c3(c)) end end end end local BG, BGKEY = nil, nil local function build() rect(0, 0, W, 18, c3(SKY)) rect(14, 3, 9, 2, c3(CLOUD)); rect(12, 5, 14, 2, c3(CLOUD)); rect(38, 6, 8, 1, c3(CLOUD)); rect(36, 7, 12, 1, c3(CLOUD)) for x = 0, W - 1 do local top = floor(12 + 2.2 * sin(x * 0.23) + 1.6 * sin(x * 0.61 + 1) + 0.5) rect(x, top, 1, 18 - top, c3(TREES)) if (x + floor(top)) % 5 == 0 then px(x, top, c3(TREES_L)) end end local ys = { 17, 20, 24, 29, 35, 42, 50, 64 } for i = 1, #ys - 1 do local c = (i % 2 == 1) and WATER1 or WATER2 rect(0, ys[i], W, ys[i + 1] - ys[i], c3(c)) end local function reed(x, top, h) rect(x, top, 1, h, c3(REED)); rect(x - 1, top - 5, 3, 6, c3(CATT)); px(x, top - 6, c3(CATT)) px(x + 1, top + 5, c3(REED)); px(x + 2, top + 4, c3(REED)); px(x - 1, top + 8, c3(REED)); px(x - 2, top + 7, c3(REED)) end reed(3, 9, 12); reed(7, 12, 10); reed(60, 8, 13); reed(56, 12, 9) ell(8, 59, 8, 3.2, PAD_D); ell(8, 59, 6.6, 2.4, PAD) ell(57, 60, 7, 3, PAD_D); ell(57, 60, 5.6, 2.2, PAD) rect(55, 57, 4, 1, c3(PETAL)); rect(54, 58, 6, 1, c3(PETAL_D)); rect(56, 56, 2, 1, c3(PETAL)) px(57, 55, c3(PETAL)); rect(57, 58, 1, 1, 255, 236, 140) ell(32, 54, 24, 8, PAD_D); ell(32, 54, 22.4, 6.8, PAD) for i = 1, 5 do local x = 14 + i * 6 rect(x, 52 + (i % 2) * 2, 4, 1, c3(PAD_L)) end ell(20, 47, 6.5, 6, DARK); ell(44, 47, 6.5, 6, DARK) rect(11, 52, 8, 2, c3(FROG)); rect(45, 52, 8, 2, c3(FROG)) for i = 0, 2 do px(11 + i * 3, 54, c3(FROG)); px(45 + i * 3 + 1, 54, c3(FROG)) end ell(32, 44, 14.5, 10.5, FROG) ell(32, 34.5, 15.5, 7, FROG) ell(32, 48, 9, 7, BELLY) rect(22, 46, 3, 8, c3(FROG)); rect(39, 46, 3, 8, c3(FROG)) rect(19, 53, 8, 2, c3(FROG)); rect(37, 53, 8, 2, c3(FROG)) for i = 0, 3 do px(19 + i * 2, 55, c3(FROG)); px(37 + i * 2 + 1, 55, c3(FROG)) end ell(24, 28, 5.2, 5, FROG); ell(40, 28, 5.2, 5, FROG) rect(23, 25, 2, 1, 120, 210, 120); rect(39, 25, 2, 1, 120, 210, 120) px(28, 32, c3(DARK)); px(36, 32, c3(DARK)); px(31, 30, c3(DARK)); px(32, 30, c3(DARK)) px(26, 34, c3(DARK)); px(38, 34, c3(DARK)); px(33, 35, c3(DARK)) rect(20, 38, 25, 1, c3(MOUTH)); px(19, 37, c3(MOUTH)); px(45, 37, c3(MOUTH)) BG = gfx.snapshot(0, 0, W, H) end local function eyes(t, gx, gy, shut) for e = 0, 1 do local cx = e == 0 and 24 or 40 if shut then rect(cx - 3, 28, 7, 1, c3(DARK)); rect(cx - 2, 27, 5, 1, c3(FROG)) else ell(cx, 28, 3.6, 3.5, IRIS) rect(cx - 2 + gx, 27 + gy, 4, 2, c3(BLACK)) px(cx - 3 + gx, 25, 255, 255, 220) end end end local function sac(r) if r < 1.2 then return end local cy = 40.5 for pass = 1, 2 do local rr = pass == 1 and r + 1 or r local c = pass == 1 and SACE or SAC for y = 39, floor(cy + rr) do local dy = (y + 0.5 - cy) / rr local q = 1 - dy * dy if q > 0 then local hw = rr * 1.15 * sqrt(q) local xa, xb = ceil(32 - hw - 0.5), floor(32 + hw - 0.5) if xb >= xa then rect(xa, y, xb - xa + 1, 1, c3(c)) end end end end end local RP = { { 7, 30 }, { 57, 27 }, { 6, 44 }, { 58, 43 }, { 13, 23 }, { 51, 22 }, { 10, 36 }, { 55, 35 } } local function blocked(x, y) if x >= 14 and x <= 50 and y >= 22 and y <= 56 then return true end local dx, dy = (x - 32) / 24, (y - 54) / 8 return dx * dx + dy * dy <= 1 end local function ring(cx, cy, r, c) local ry = r * 0.4 local n = floor(ry + 0.5) for dy = -n, n do local q = 1 - (dy / (ry + 0.01)) * (dy / (ry + 0.01)) if q >= 0 then local dx = floor(r * sqrt(q) + 0.5) local y = cy + dy if y >= 18 and y < 62 then if dy == -n or dy == n then local a = dx > 2 and dx - 2 or 0 for x = cx - a, cx + a do if not blocked(x, y) then px(x, y, c3(c)) end end else if not blocked(cx - dx, y) then px(cx - dx, y, c3(c)) end if not blocked(cx + dx, y) then px(cx + dx, y, c3(c)) end end end end end end local function fly_pos(t) if t < 1.0 or t >= 4.9 then return nil end if t < 2.4 then local u = (t - 1.0) / 1.4 local e = 1 - (1 - u) * (1 - u) return 70 + (FX0 - 70) * e + 2 * sin(t * 9), 8 + (FY0 - 8) * e + 2 * sin(t * 7) elseif t < 4.4 then local u = (t - 2.4) / 2.0 local r = 7 * (1 - u * 0.82) local a = (t - 2.4) * 2 * PI * 1.3 return FX0 + r * cos(a), FY0 + 0.65 * r * sin(a) end return FX0 + 1.0 * sin(t * 40), FY0 + 0.8 * sin(t * 33) end local function draw_fly(x, y, k) local xi, yi = floor(x + 0.5), floor(y + 0.5) px(xi, yi, c3(FLY)); px(xi + 1, yi, c3(FLY)) if k % 2 == 0 then px(xi, yi - 1, c3(WING)); px(xi + 1, yi - 1, c3(WING)) else px(xi - 1, yi - 1, c3(WING)); px(xi + 2, yi - 1, c3(WING)) end end local function draw_dragon(td, k) if td > 2.4 then return end local x = floor(-10 + 84 * td / 2.4) local y = floor(9 + 2.5 * sin(td * 4.5) + 0.5) rect(x, y, 6, 1, 70, 130, 232); px(x + 6, y, 40, 190, 120); px(x + 7, y, 40, 190, 120) px(x - 1, y, 60, 110, 200) if k % 2 == 0 then rect(x + 2, y - 2, 3, 1, c3(WING)); rect(x + 2, y + 1, 3, 1, c3(WING)) else rect(x + 1, y - 1, 5, 1, c3(WING)); rect(x + 3, y + 1, 1, 1, c3(WING)) end end local function draw_tongue(ex, ey, ext) local x0, y0 = MX, MY + 1 local dx, dy = ex - x0, ey - y0 local n = floor(sqrt(dx * dx + dy * dy) * ext + 0.5) if n < 1 then return end for i = 0, n do local f = i / (n > 0 and n or 1) * ext local x, y = floor(x0 + dx * f + 0.5), floor(y0 + dy * f + 0.5) local c = i > n - 2 and TONGUE_T or TONGUE px(x, y, c3(c)); px(x + 1, y, c3(c)) if abs(dx) < abs(dy) then px(x, y + 1, c3(c)) end end end local function waves(k) for j = 0, 2 do local u = k - j * 5 if u >= 0 and u < 9 then local x = 18 + 5 * j + u // 3 local h = 1 + j local c = u < 6 and 255 or 190 for s = -1, 1, 2 do local xx = s < 0 and (31 - x) or (32 + x) rect(xx, 36 - h, 1, 2 * h + 1, c, c, 255) px(xx - s, 36 - h - 1, c, c, 255); px(xx - s, 36 + h + 1, c, c, 255) end end end end local CYC = 12.0 local STEP_HZ = 30 local function shimmer(f) local SPX = { 5, 18, 47, 60, 12, 52 } local SPY = { 22, 27, 25, 33, 40, 48 } for i = 1, 6 do if (f + i * 3) % 7 < 2 then rect(SPX[i], SPY[i], 3, 1, c3(GLINT)) end end end function render() local fc = cfg.frog or { r = 56, g = 170, b = 68 } local key = (fc.r * 256 + fc.g) * 256 + fc.b if key ~= BGKEY then BGKEY = key FROG = { fc.r, fc.g, fc.b } DARK = { fc.r * 55 // 100, fc.g * 66 // 100, fc.b * 76 // 100 } BELLY = { fc.r + (255 - fc.r) * 55 // 100, fc.g + (255 - fc.g) * 60 // 100, fc.b + (255 - fc.b) * 40 // 100 } SAC = { fc.r + (255 - fc.r) * 80 // 100, fc.g + (255 - fc.g) * 80 // 100, fc.b + (255 - fc.b) * 60 // 100 } SACE = { fc.r * 80 // 100 + 20, fc.g * 80 // 100 + 30, fc.b * 80 // 100 + 10 } build() end gfx.blit_rgb(BG, 0, 0, W, H) local ta = frame / FPS local t = ta % CYC local k = floor(ta * 15) for i = 0, 2 do local tt = ta + i * 0.63 local n = floor(tt / 1.9) local u = tt - n * 1.9 local p = RP[umod(hash32(n * 0x9E3779B1 + i * 0x85EBCA6B), #RP) + 1] ring(p[1], p[2], 1 + u * 4.6, u < 1.0 and RING1 or RING2) end shimmer(floor(ta * 6)) local fx, fy = fly_pos(t) local gx, gy = 0, 0 if fx then gx = fx < 36 and -1 or 1; gy = fy < 24 and -1 or 0 end local shut = (t % 4.2) < 0.13 or (t > 5.1 and t < 5.4) local sr = 2.3 + 0.9 * sin(ta * 3.4) local ribbit = cfg.ribbit ~= false local kv = -1 if t > 5.1 and t < 5.5 then sr = 3 + 3.5 * sin((t - 5.1) / 0.4 * PI) end if ribbit and t >= 6.2 and t < 8.6 then local s = t - 6.2 if s < 0.4 then sr = 2.5 + 7 * (s / 0.4) elseif s < 1.7 then sr = 9.5 + (floor(s * 5) % 2 == 0 and 0.8 or -0.8) kv = floor((s - 0.4) * STEP_HZ) % 20 if kv < 0 then kv = -1 end else sr = 9.5 - 7 * ((s - 1.7) / 0.7) end end sac(sr) eyes(ta, gx, gy, shut) local open = (t >= 4.5 and t < 5.15) or kv >= 0 if open then rect(25, 37, 14, 3, c3(MOUTH)) if kv >= 0 then rect(28, 37, 8, 1, c3(MOUTH)) end end if fx then if t >= 4.6 then local ext if t < 4.8 then ext = (t - 4.6) / 0.2 elseif t < 4.9 then ext = 1 else ext = 1 end draw_tongue(fx, fy, ext) draw_fly(fx, fy, k) else draw_fly(fx, fy, k) end elseif t >= 4.9 and t < 5.15 then local ext = 1 - (t - 4.9) / 0.25 local fxh, fyh = FX0 + 1.0 * sin(4.9 * 40), FY0 + 0.8 * sin(4.9 * 33) draw_tongue(fxh, fyh, ext) local tx, ty = MX + (fxh - MX) * ext, MY + 1 + (fyh - MY - 1) * ext if ext > 0.15 then draw_fly(tx, ty, k) end end if kv >= 0 then waves(kv) end if cfg.dragon ~= false then draw_dragon((ta + 3.0) % 12.0, k) end end