info = { type = "image", title = "Lily pad frog", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A frog on a lily pad snaps at flies", api = 33, } local ltr = ({ sk = { ["Flies"] = "Mušky", ["Appetite"] = "Hlad", ["Calm"] = "Pokojná", ["Normal"] = "Bežná", ["Hungry"] = "Hladná" }, cs = { ["Flies"] = "Mušky", ["Appetite"] = "Hlad", ["Calm"] = "Klidná", ["Normal"] = "Běžná", ["Hungry"] = "Hladová" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "flies", label = LT("Flies"), type = "int", min = 1, max = 5, default = 3 }, { key = "hunger", label = LT("Appetite"), type = "int", default = "normal", opts = { keys = { "calm", "normal", "hungry" }, LT("Calm"), LT("Normal"), LT("Hungry") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, ceil, sin, cos, sqrt, abs, min, max = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function rnd(v) return floor(v + 0.5) end local function ease(e) e = max(0, min(1, e)); return e * e * (3 - 2 * e) end local WATER, PADS = nil, nil local KEY = { 255, 0, 255 } local PAD_X, PAD_Y = 18, 54 local function pad(cx, cy, a, b) for dy = -b, b do local hw = rnd(a * sqrt(1 - ((dy + 0.5) / (b + 0.5)) ^ 2)) local t = (dy + b) / (2 * b) rect(cx - hw, cy + dy, 2 * hw, 1, rnd(112 - 70 * t), rnd(208 - 78 * t), rnd(100 - 28 * t)) end local hw = rnd(a * sqrt(1 - (b / (b + 0.5)) ^ 2)) + 2 rect(cx - hw, cy + b + 1, 2 * hw, 1, 24, 92, 56) end local function bake_water() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, rnd(88 - 60 * t), rnd(176 - 80 * t), rnd(200 - 64 * t)) end for i = 1, 26 do local h = hash32(i * 0x9E3779B1) blend_rect(umod(h, 60), umod(h >> 8, H), 3 + umod(h >> 16, 6), 1, 220, 248, 255, 0.18) end for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) blend_rect(umod(h, 58), umod(h >> 8, H), 4 + umod(h >> 16, 6), 1, 10, 60, 96, 0.2) end WATER = snapshot(0, 0, W, H) end local function bake_pads() gfx.clear(KEY[1], KEY[2], KEY[3]) for _, rd in ipairs({ { 59, 24, 30 }, { 62, 18, 34 }, { 56, 34, 20 } }) do rect(rd[1], rd[2], 1, rd[3], 52, 122, 62) rect(rd[1], rd[2] - 5, 2, 6, 122, 70, 40) px(rd[1] - 1, rd[2] + 8, 52, 122, 62); px(rd[1] - 2, rd[2] + 7, 52, 122, 62) end pad(8, 20, 5, 2) pad(50, 14, 7, 3) rect(48, 11, 5, 2, 250, 150, 190); rect(49, 10, 3, 1, 250, 150, 190) px(50, 11, 255, 226, 90); px(48, 11, 255, 206, 226); px(52, 11, 255, 206, 226) pad(52, 57, 9, 3) pad(PAD_X, PAD_Y, 19, 9) rect(PAD_X + 12, PAD_Y + 4, 12, 1, KEY[1], KEY[2], KEY[3]) rect(PAD_X + 9, PAD_Y + 5, 14, 1, KEY[1], KEY[2], KEY[3]) rect(PAD_X + 7, PAD_Y + 6, 16, 1, KEY[1], KEY[2], KEY[3]) for k = 0, 7 do local ang = 0.39 + k * 0.785 for r = 3, 15 do px(rnd(PAD_X + r * cos(ang)), rnd(PAD_Y + r * sin(ang) * 0.47), 34, 112, 62) end end PADS = snapshot(0, 0, W, H) end local function ring(cx, cy, a, b, al) for y = floor(cy - b), floor(cy + b) do local d = abs(y + 0.5 - cy) local q1, q2 = 1 - (max(0, d - 0.5) / b) ^ 2, 1 - ((d + 0.5) / b) ^ 2 local xl = q1 > 0 and a * sqrt(q1) or 0 local xs = q2 > 0 and a * sqrt(q2) or 0 local w = max(1, ceil(xl - xs)) blend_rect(floor(cx - xl), y, w, 1, 220, 248, 255, al) blend_rect(floor(cx + xs), y, w, 1, 220, 248, 255, al) end end local RIP = {} for k = 1, 3 do RIP[k] = { p = 90 + 17 * k, off = k * 31 } end local function ripples(t) for k = 1, 3 do local r = RIP[k] local a = (t + r.off) % r.p local n = (t + r.off) // r.p local h = hash32(n * 977 + k * 131) local rad = 1 + a * 0.13 local al = 0.5 * (1 - a / r.p) if a > 2 then ring(22 + umod(h, 36), 6 + umod(h >> 8, 34), rad, rad * 0.38, al) end end local a = t % 140 ring(PAD_X, PAD_Y + 1, 21 + a * 0.08, 10 + a * 0.04, 0.4 * (1 - a / 140)) end local NFL = 5 local FLY = {} for j = 1, NFL do local h = hash32(j * 0x9E3779B1) FLY[j] = { cx = 32 + umod(h, 14), cy = 10 + umod(h >> 6, 24), ax = 6 + umod(h >> 12, 5), ay = 4 + umod(h >> 16, 6), w1 = 0.03 + umod(h >> 20, 30) / 1000, w2 = 0.04 + umod(h >> 24, 30) / 1000, p1 = umod(h >> 4, 628) / 100, p2 = umod(h >> 8, 628) / 100, } end local CYCS = { calm = 300, normal = 210, hungry = 150 } local CYC = 210 local TS = 105 local FX, FY = 8, 37 local MX, MY = FX + 10, FY + 9 function on_settings() CYC = CYCS[cfg.hunger] or 210 TS = CYC // 2 end local function fly_at(j, t, nfl) local f = FLY[j] local x = f.cx + f.ax * sin(t * f.w1 + f.p1) + 0.7 * sin(t * 0.37 + f.p1 * 2) local y = f.cy + f.ay * sin(t * f.w2 + f.p2) + 0.5 * sin(t * 0.51 + f.p2) local n = t // CYC local tx, ty = 40 + 3 * sin(n * 1.3), 31 + 3 * cos(n * 0.9) if n % nfl ~= j - 1 then return x, y, 0, tx, ty end local tl = t - n * CYC if tl >= TS - 35 and tl <= TS then local s = ease((tl - TS + 35) / 35) return x + (tx - x) * s, y + (ty - y) * s, 0, tx, ty elseif tl > TS and tl < TS + 10 then return tx, ty, 1, tx, ty elseif tl < TS + 35 and tl >= TS + 10 then return x, y, 2, tx, ty elseif tl < TS + 65 and tl >= TS + 35 then local r = ease((tl - TS - 35) / 30) return x + (1 - r) * 40, y - (1 - r) * 8, 0, tx, ty end return x, y, 0, tx, ty end local function draw_fly(x, y, t) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 30, 28, 40, (1 - fx) * (1 - fy)) blend(ix + 1, iy, 30, 28, 40, fx * (1 - fy)) blend(ix, iy + 1, 30, 28, 40, (1 - fx) * fy) blend(ix + 1, iy + 1, 30, 28, 40, fx * fy) local rx, ry = rnd(x), rnd(y) local wy = (t // 2) % 2 == 0 and ry - 1 or ry blend(rx - 1, wy, 232, 246, 255, 0.75); blend(rx + 1, wy, 232, 246, 255, 0.75) blend(rx + 1, ry + 6, 8, 40, 70, 0.22) end local MID, DARK, LIGHT, BELLY, SCL = { 96, 188, 72 }, { 52, 134, 56 }, { 140, 214, 96 }, { 196, 234, 140 }, { 236, 244, 214 } local function eye(ex, ey, lx, ly, shut) if shut then rect(ex + 1, ey, 3, 1, MID[1], MID[2], MID[3]) rect(ex, ey + 1, 5, 4, MID[1], MID[2], MID[3]) rect(ex + 1, ey + 3, 3, 1, 36, 84, 44) return end rect(ex + 1, ey, 3, 1, MID[1], MID[2], MID[3]) rect(ex, ey + 1, 5, 3, SCL[1], SCL[2], SCL[3]) rect(ex + 1, ey + 4, 3, 1, DARK[1], DARK[2], DARK[3]) rect(ex + 2 + lx, ey + 1 + (ly > 0 and 1 or 0), 2, 2, 20, 24, 30) end local function frog(open, lx, ly, shut, flash) blend_rect(FX - 1, FY + 15, 22, 3, 16, 64, 44, 0.4) rect(FX, FY + 9, 5, 6, DARK[1], DARK[2], DARK[3]); rect(FX + 15, FY + 9, 5, 6, DARK[1], DARK[2], DARK[3]) rect(FX - 2, FY + 15, 6, 2, MID[1], MID[2], MID[3]); rect(FX + 16, FY + 15, 6, 2, MID[1], MID[2], MID[3]) rect(FX + 3, FY + 8, 14, 8, MID[1], MID[2], MID[3]) rect(FX + 6, FY + 11, 8, 5, BELLY[1], BELLY[2], BELLY[3]) if flash then rect(FX + 7, FY + 10, 6, 1, 236, 252, 176) end rect(FX + 5, FY + 13, 2, 3, MID[1], MID[2], MID[3]); rect(FX + 13, FY + 13, 2, 3, MID[1], MID[2], MID[3]) rect(FX + 4, FY + 16, 4, 1, LIGHT[1], LIGHT[2], LIGHT[3]); rect(FX + 12, FY + 16, 4, 1, LIGHT[1], LIGHT[2], LIGHT[3]) rect(FX + 3, FY + 3, 14, 1, MID[1], MID[2], MID[3]) rect(FX + 1, FY + 4, 18, 6, MID[1], MID[2], MID[3]) rect(FX + 4, FY + 4, 12, 1, LIGHT[1], LIGHT[2], LIGHT[3]) if open then rect(FX + 4, FY + 8, 12, 3, 96, 24, 40) rect(FX + 8, FY + 9, 4, 1, 214, 70, 100) else rect(FX + 3, FY + 8, 14, 1, 36, 84, 44) px(FX + 2, FY + 7, 36, 84, 44); px(FX + 17, FY + 7, 36, 84, 44) end px(FX + 8, FY + 5, 36, 100, 44); px(FX + 11, FY + 5, 36, 100, 44) px(FX + 5, FY + 11, DARK[1], DARK[2], DARK[3]); px(FX + 15, FY + 12, DARK[1], DARK[2], DARK[3]) eye(FX + 3, FY, lx, ly, shut) eye(FX + 12, FY, lx, ly, shut) end function render() if not WATER then bake_water(); bake_pads() end local t = frame local nfl = min(cfg.flies or 3, NFL) blit_rgb(WATER, 0, 0, W, H) ripples(t) blit_rgb(PADS, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) local n = t // CYC local tl = t - n * CYC local tj = n % nfl + 1 local tfx, tfy, _, px_, py_ = fly_at(tj, t, nfl) local lx = tfx - MX > 8 and 1 or (tfx - MX < -8 and -1 or 0) local ly = tfy < FY and -1 or 1 local gulp = tl >= TS + 10 and tl < TS + 26 local flash = t % 50 < 6 if gulp then flash = (tl // 4) % 2 == 0 end frog(tl >= TS - 6 and tl < TS + 11, lx, ly, gulp or t % 110 < 3, flash) for j = 1, nfl do local x, y, st = fly_at(j, t, nfl) if st == 0 then draw_fly(x, y, t) end end local e if tl >= TS - 5 and tl <= TS then e = (tl - TS + 5) / 5 elseif tl > TS and tl <= TS + 2 then e = 1 elseif tl > TS + 2 and tl < TS + 10 then e = 1 - (tl - TS - 2) / 8 end if e then local dx, dy = px_ - MX, py_ - MY local len = sqrt(dx * dx + dy * dy) for i = 0, floor(len * e) do local k = i / len px(rnd(MX + dx * k), rnd(MY + dy * k), 232, 84, 116) end local tx, ty = MX + dx * e, MY + dy * e rect(rnd(tx) - 1, rnd(ty) - 1, 2, 2, 255, 128, 156) if tl > TS then draw_fly(tx, ty - 1, t) end end end