info = { type = "image", title = "Flamingo lagoon", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Flamingos wade a lagoon at sunset", api = 44, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Sunset"] = "Západ", ["Morning"] = "Ráno" }, cs = { ["Time of day"] = "Denní doba", ["Sunset"] = "Západ", ["Morning"] = "Ráno" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time of day"), type = "int", default = "sunset", opts = { keys = { "sunset", "morning" }, T("Sunset"), T("Morning") } }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local floor, ceil, sqrt, sin, cos, abs = math.floor, math.ceil, math.sqrt, math.sin, math.cos, math.abs local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local RIP, RT, GA, MIR = 0, 0, 1, nil local function span(xa, xb, y, r, g, b, a) if MIR then y = MIR - y end if RIP ~= 0 then local o = RIP * sin(y * 0.9 + RT) xa, xb = xa + o, xb + o end if xb <= xa then return end a = (a or 1) * GA local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, (xb - xa) * a) return end local s, c = ia + 1, ia + 1 - xa if c > 0.96 and a == 1 then s = ia else blend(ia, y, r, g, b, c * a) end local f = xb - ib if f > 0.04 then blend(ib, y, r, g, b, f * a) end if ib > s then if a < 1 then blend_rect(s, y, ib - s, 1, r, g, b, a) else rect(s, y, ib - s, 1, r, g, b) end end end local function oval(cx, cy, rx, ry, r, g, b, a) for y = floor(cy - ry), ceil(cy + ry) - 1 do local d = (y + 0.5 - cy) / ry if d > -1 and d < 1 then local hw = rx * sqrt(1 - d * d) span(cx - hw, cx + hw, y, r, g, b, a) end end end local function lin(a, b, lo, hi) if a > -1e-9 and a < 1e-9 then if b >= lo and b <= hi then return -1e9, 1e9 end return 1e9, -1e9 end local x1, x2 = (lo - b) / a, (hi - b) / a if x1 > x2 then return x2, x1 end return x1, x2 end local function capsule(x0, y0, x1, y1, rad, r, g, b, a) local dx, dy = x1 - x0, y1 - y0 local L2 = dx * dx + dy * dy local L = sqrt(L2) local ya, yb = y0, y1 if ya > yb then ya, yb = yb, ya end local r2 = rad * rad local ady = dy < 0 and -dy or dy local steep = ady > 0.35 * L local ihw, k = 0, 0 if steep then ihw, k = rad * L / ady, dx / dy end local xlo, xhi = (x0 < x1 and x0 or x1) - rad, (x0 > x1 and x0 or x1) + rad for y = floor(ya - rad), ceil(yb + rad) - 1 do local yc = y + 0.5 local lo, hi if steep and yc >= ya and yc <= yb then local xc = x0 + k * (yc - y0) lo, hi = xc - ihw, xc + ihw if lo < xlo then lo = xlo end if hi > xhi then hi = xhi end else lo, hi = 1e9, -1e9 local e = r2 - (yc - y0) * (yc - y0) if e > 0 then e = sqrt(e); lo, hi = x0 - e, x0 + e end e = r2 - (yc - y1) * (yc - y1) if e > 0 then e = sqrt(e) if x1 - e < lo then lo = x1 - e end if x1 + e > hi then hi = x1 + e end end if not steep and L > 0.01 then local pa, pb = lin(dy, -x0 * dy - (yc - y0) * dx, -rad * L, rad * L) local qa, qb = lin(dx, -x0 * dx + (yc - y0) * dy, 0, L2) if qa > pa then pa = qa end if qb < pb then pb = qb end if pb > pa then if pa < lo then lo = pa end if pb > hi then hi = pb end end end end span(lo, hi, y, r, g, b, a) end end local function lerp3(a, b, f) return floor(a[1] + (b[1] - a[1]) * f + 0.5), floor(a[2] + (b[2] - a[2]) * f + 0.5), floor(a[3] + (b[3] - a[3]) * f + 0.5) end local PS = { sky0 = { 92, 68, 150 }, sky1 = { 255, 176, 96 }, w0 = { 250, 170, 112 }, w1 = { 182, 108, 148 }, w2 = { 70, 84, 152 }, sun = { 255, 238, 180 }, land = { 104, 56, 100 }, cloud = { 255, 160, 130 }, glint = { 255, 214, 150 }, sx = 42, sr = 4.2, ld = 1, } local PM = { sky0 = { 112, 156, 222 }, sky1 = { 255, 222, 170 }, w0 = { 236, 214, 190 }, w1 = { 120, 190, 204 }, w2 = { 40, 112, 164 }, sun = { 255, 250, 224 }, land = { 70, 118, 136 }, cloud = { 255, 200, 190 }, glint = { 255, 246, 214 }, sx = 22, sr = 3.6, ld = -1, } local PINK, PINK_D, PINK_L = { 252, 122, 142 }, { 196, 66, 108 }, { 255, 182, 150 } local BEAK, TIP, LEG = { 255, 224, 206 }, { 52, 38, 60 }, { 228, 104, 134 } local HZ = 27 local P, BG, STAGE = PS, nil, 0 function on_settings() P = cfg.time == "morning" and PM or PS BG, STAGE = nil, 0 end local function stage1() for y = 0, HZ - 1 do rect(0, y, W, 1, lerp3(P.sky0, P.sky1, (y / (HZ - 1)) ^ 1.4)) end for i = 1, 5 do local h = hash32(i * 0x9E3779B1) local y, x, w = 4 + umod(h, 14), umod(h >> 8, 56) - 8, 16 + umod(h >> 16, 20) blend_rect(x, y, w, 1, P.cloud[1], P.cloud[2], P.cloud[3], 0.55) blend_rect(x + 3, y + 1, w - 7, 1, P.cloud[1], P.cloud[2], P.cloud[3], 0.3) blend_rect(x + 2, y - 1, w - 5, 1, P.sky0[1], P.sky0[2], P.sky0[3], 0.25) end local sx, sy = P.sx, HZ - 1.5 for k = 1, 3 do oval(sx, sy, 8 + 6 * k, 8 + 6 * k, P.sun[1], P.sun[2], P.sun[3], 0.08) end oval(sx, sy, 6.5, 6.5, P.sun[1], P.sun[2], P.sun[3], 0.2) oval(sx, sy, P.sr, P.sr, P.sun[1], P.sun[2], P.sun[3]) end local function stage2() for y = HZ, H - 1 do local f = (y - HZ) / (H - 1 - HZ) if f < 0.45 then rect(0, y, W, 1, lerp3(P.w0, P.w1, f / 0.45)) else rect(0, y, W, 1, lerp3(P.w1, P.w2, (f - 0.45) / 0.55)) end end local L = P.land for x = 0, W - 1 do local d = abs(x - P.sx) local m = d < 14 and 0 or (d < 22 and (d - 14) / 8 or 1) local h = floor(m * (1.5 + 1.2 * sin(x * 0.7) + 0.8 * sin(x * 1.9)) + 0.5) if h > 0 then rect(x, HZ - h, 1, h, L[1], L[2], L[3]) end end for _, p in ipairs({ { 4, 6 }, { 11, 4 }, { 52, 5 }, { 59, 7 } }) do local x, h = p[1], p[2] if abs(x - P.sx) > 16 then rect(x, HZ - h, 1, h, L[1], L[2], L[3]) rect(x - 2, HZ - h, 5, 1, L[1], L[2], L[3]) rect(x - 3, HZ - h + 1, 7, 1, L[1], L[2], L[3]) blend(x - 3, HZ - h + 2, L[1], L[2], L[3], 0.8); blend(x + 3, HZ - h + 2, L[1], L[2], L[3], 0.8) end end end local function stage3() for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) local y, x, w = HZ + 3 + umod(h, 34), umod(h >> 8, 64) - 10, 14 + umod(h >> 16, 24) blend_rect(x, y, w, 1, P.w2[1], P.w2[2], P.w2[3], 0.1) blend_rect(x + 2, y + 1, w - 4, 1, P.w2[1], P.w2[2], P.w2[3], 0.06) end BG = gfx.snapshot(0, 0, W, H) end local UP = { 1.2, -9.6, 3.2, -3.2, -2.4, 3.4, 0.15 } local DIP = { 4.6, 8.4, 3.8, -2.6, 0.4, -4.2, 1.15 } local TUCK = { -2.2, -1.6, 3.4, -5.2, -4.4, -4.4, 2.6 } local POSE = {} local function mixpose(a, b, m, out) for i = 1, 7 do out[i] = a[i] + (b[i] - a[i]) * m end end local function smooth(w) w = w < 0 and 0 or (w > 1 and 1 or w) return w * w * (3 - 2 * w) end local function leg(hx, hy, fx, fy, kx, ky, s, dir, det) local lr, lg, lb = LEG[1], LEG[2], LEG[3] if det == 0 then capsule(hx, hy, fx, fy, 0.5 * s + 0.1, lr, lg, lb) return end capsule(hx, hy, kx, ky, 0.5 * s + 0.1, lr, lg, lb) capsule(kx, ky, fx, fy, 0.45 * s + 0.1, lr, lg, lb) if fy > hy + 3 * s then capsule(fx, fy, fx + dir * 1.8 * s, fy + 0.2, 0.4 * s, lr, lg, lb) end end local function bird(cx, fy, s, dir, kind, t, ph, det) local ld = P.ld local hipy = fy - 9.2 * s local cy = hipy - 1.3 * s local pose = POSE local bob = 0 local walk = kind == 2 if kind == 1 then mixpose(UP, TUCK, smooth(1.4 * sin(6.2832 * (t + ph) / 760) + 0.9), pose) elseif kind == 3 then mixpose(UP, DIP, smooth(1.5 * sin(6.2832 * (t + ph) / 300) + 0.5), pose) elseif kind == 2 then mixpose(UP, UP, 0, pose) pose[1] = pose[1] + 0.7 * sin(6.2832 * t / 60) bob = 0.25 * s * sin(6.2832 * t / 30) else mixpose(UP, TUCK, smooth(1.2 * sin(6.2832 * (t + ph) / 540) + 0.2) * 0.9, pose) end cy = cy + bob local hx1, hx2 = cx - 0.9 * s, cx + 1.0 * s if kind == 1 then leg(hx1, hipy + bob, hx1 + 0.2 * s, fy, hx1 - dir * 0.9 * s, fy - 4.6 * s, s, dir, det) if det > 0 then capsule(hx2, hipy + bob, hx2 + dir * 2.4 * s, hipy + 3.2 * s, 0.5 * s + 0.1, LEG[1], LEG[2], LEG[3]) capsule(hx2 + dir * 2.4 * s, hipy + 3.2 * s, hx2 - dir * 0.6 * s, hipy + 4.4 * s, 0.45 * s + 0.1, LEG[1], LEG[2], LEG[3]) end elseif walk then for i = 0, 1 do local q = (t / 60 + i * 0.5) % 1 local rel, lift if q < 0.6 then rel, lift = 1.26 * (1 - 2 * q / 0.6), 0 else local u = (q - 0.6) / 0.4; rel, lift = -1.26 + 2.52 * u, 2.2 * s * sin(3.1416 * u) end local hx = i == 0 and hx1 or hx2 local fx, fyy = hx + dir * rel, fy - lift leg(hx, hipy + bob, fx, fyy, (hx + fx) / 2 - dir * 1.1 * s, (hipy + fyy) / 2, s, dir, det) end else leg(hx1, hipy, hx1 + 0.2 * s, fy, hx1 - dir * 0.9 * s, fy - 4.6 * s, s, dir, det) leg(hx2, hipy, hx2 + 0.2 * s, fy, hx2 - dir * 0.9 * s, fy - 4.6 * s, s, dir, det) end local rx, ry = 4.4 * s, 2.8 * s if det > 0 then capsule(cx - dir * 4.6 * s, cy + 0.3 * s, cx - dir * 7.4 * s, cy + 1.4 * s, 0.9 * s, PINK_D[1], PINK_D[2], PINK_D[3]) oval(cx, cy, rx, ry, PINK_D[1], PINK_D[2], PINK_D[3]) oval(cx - 0.2 * s * ld, cy - 0.45 * s, rx * 0.93, ry * 0.8, PINK[1], PINK[2], PINK[3]) else oval(cx, cy, rx, ry, PINK[1], PINK[2], PINK[3]) end if det > 1 then oval(cx - dir * 1.0 * s, cy + 0.5 * s, 2.9 * s, 1.35 * s, PINK_D[1], PINK_D[2], PINK_D[3]) oval(cx + ld * 1.5 * s, cy - 1.0 * s, 2.0 * s, 0.9 * s, PINK_L[1], PINK_L[2], PINK_L[3]) capsule(cx - dir * 5.6 * s, cy + 0.6 * s, cx - dir * 7.4 * s, cy + 1.4 * s, 0.5 * s, TIP[1], TIP[2], TIP[3]) end local bx, by = cx + dir * 3.3 * s, cy - 1.0 * s local hx, hy = bx + dir * pose[1] * s, by + pose[2] * s local c1x, c1y = bx + dir * pose[3] * s, by + pose[4] * s local c2x, c2y = hx + dir * pose[5] * s, hy + pose[6] * s local px_, py_ = bx, by local nseg = det > 0 and 3 or 2 for k = 1, nseg do local u = k / nseg local v = 1 - u local nx = v * v * v * bx + 3 * v * v * u * c1x + 3 * v * u * u * c2x + u * u * u * hx local ny = v * v * v * by + 3 * v * v * u * c1y + 3 * v * u * u * c2y + u * u * u * hy local rad = (1.0 - 0.18 * k * 3 / nseg) * s + 0.1 capsule(px_, py_, nx, ny, rad, PINK[1], PINK[2], PINK[3]) if det > 1 and rad > 0.7 then capsule(px_ + ld * 0.45 * s, py_, nx + ld * 0.45 * s, ny, 0.3 * s, PINK_L[1], PINK_L[2], PINK_L[3]) end px_, py_ = nx, ny end oval(hx, hy, 1.25 * s + 0.1, 1.1 * s + 0.1, PINK[1], PINK[2], PINK[3]) if det > 0 then local a = pose[7] local ex, ey = hx + dir * cos(a) * 1.7 * s, hy + sin(a) * 1.7 * s capsule(hx, hy, ex, ey, 0.55 * s, BEAK[1], BEAK[2], BEAK[3]) capsule(ex, ey, ex + dir * cos(a + 0.9) * 1.5 * s, ey + sin(a + 0.9) * 1.5 * s, 0.5 * s, TIP[1], TIP[2], TIP[3]) end end local FL = { { 43.5, 0.5, 1, 4, 100, 57 }, { 44.5, 0.55, -1, 4, 330, 25 }, { 50.5, 0.72, -1, 3, 0, 45 }, { 54.5, 0.8, 1, 1, 0, 14 }, { 58, 0.9, 1, 2, 0, 0 }, } local function draw_birds(t) for i = 1, 5 do local f = FL[i] local fy, s, dir, kind, ph, cx = f[1], f[2], f[3], f[4], f[5], f[6] if kind == 2 then cx = -12 + (t * 0.07 + 46) % 96 end RIP, RT, GA, MIR = 0.7, t * 0.09 + i, 0.38, floor(2 * fy) - 1 gfx.clip(0, ceil(fy), W, H) bird(cx, fy, s, dir, kind, t, ph, 0) gfx.clip() RIP, GA, MIR = 0, 1, nil bird(cx, fy, s, dir, kind, t, ph, s > 0.78 and 2 or 1) span(cx - 5 * s, cx + 5 * s, floor(fy), P.glint[1], P.glint[2], P.glint[3], 0.25) end end local function draw_water(t) local g = P.glint for y = HZ + 1, 52 do local w = 1 + (y - HZ) * 0.24 + 1.5 * sin(y * 2.1 + t * 0.11) local off = 1.2 * sin(y * 0.8 + t * 0.05) local a = 0.55 - (y - HZ) * 0.016 span(P.sx - w / 2 + off, P.sx + w / 2 + off, y, g[1], g[2], g[3], a + 0.2 * sin(y * 1.7 + t * 0.13)) end for i = 1, 12 do local h = hash32(i * 0xC2B2AE35) local y = HZ + 2 + umod(h, 33) local x = (umod(h >> 8, 80) + t * (0.05 + umod(h >> 16, 6) * 0.01)) % 84 - 10 blend_rect(floor(x), y, 3 + umod(h >> 20, 6), 1, g[1], g[2], g[3], 0.16) end end function render() if STAGE == 0 then stage1() STAGE = 1 gfx.ready(false) return end if STAGE == 1 then stage2() STAGE = 2 gfx.ready(false) return end if STAGE == 2 then stage3() STAGE = 3 end if frame % 3 ~= 2 then return end gfx.blit_rgb(BG, 0, 0, W, H) draw_water(frame) draw_birds(frame) end