info = { type = "image", title = "Soaring eagle", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A bald eagle flaps and glides over hills", api = 33, } local ltr = ({ sk = { ["Speed"] = "Rýchlosť", ["Flaps in burst"] = "Mávnutí v dávke", ["Sky"] = "Obloha", ["day"] = "deň", ["sunset"] = "západ slnka", ["dawn"] = "svitanie" }, cs = { ["Speed"] = "Rychlost", ["Flaps in burst"] = "Mávnutí v dávce", ["Sky"] = "Obloha", ["day"] = "den", ["sunset"] = "západ slunce", ["dawn"] = "svítání" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "flaps", label = LT("Flaps in burst"), type = "int", min = 1, max = 6, default = 3 }, { key = "sky", label = LT("Sky"), type = "int", default = 0, opts = { LT("day"), LT("sunset"), LT("dawn") } }, } 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 = math.hash32 local W, H = gfx.W, gfx.H local PI = 3.14159265 local SKIES = { { { 64, 132, 230 }, { 186, 226, 250 }, { 255, 250, 226 }, { 255, 238, 176 } }, { { 84, 70, 170 }, { 255, 170, 96 }, { 255, 238, 176 }, { 255, 204, 124 } }, { { 104, 124, 208 }, { 255, 206, 196 }, { 255, 246, 218 }, { 255, 226, 176 } }, } local FARC, HILLC, WOODC = { 142, 150, 202 }, { 86, 112, 156 }, { 40, 76, 72 } local CLOUD, CLOUD_S = { 250, 250, 252 }, { 204, 216, 238 } local function ell(cx, cy, rx, ry, a, r, g, b) local c, s = cos(a), sin(a) local ix, iy = 1 / (rx * rx), 1 / (ry * ry) local A = c * c * ix + s * s * iy local Bk = 2 * c * s * (ix - iy) local Ck = s * s * ix + c * c * iy local hh = sqrt(rx * rx * s * s + ry * ry * c * c) local top, bot = cy - hh, cy + hh for y = floor(top), ceil(bot) - 1 do local y0 = y > top and y or top local y1 = (y + 1 < bot) and (y + 1) or bot local cov = y1 - y0 if cov > 0.12 then local dy = (y0 + y1) * 0.5 - cy local B = Bk * dy local disc = B * B - 4 * A * (Ck * dy * dy - 1) if disc > 0 then local sq = sqrt(disc) / (2 * A) local m = -B / (2 * A) local xs, xe = floor(cx + m - sq + 0.5), floor(cx + m + sq + 0.5) if xe > xs then if cov > 0.97 then rect(xs, y, xe - xs, 1, r, g, b) else blend_rect(xs, y, xe - xs, 1, r, g, b, cov) end end end end end end local function tri(x1, y1, x2, y2, x3, y3, r, g, b) local ymin = y1 < y2 and y1 or y2; if y3 < ymin then ymin = y3 end local ymax = y1 > y2 and y1 or y2; if y3 > ymax then ymax = y3 end for y = floor(ymin), ceil(ymax) - 1 do local yc = y + 0.5 local lo, hi = 1e9, -1e9 if (y1 <= yc) ~= (y2 <= yc) then local x = x1 + (yc - y1) * (x2 - x1) / (y2 - y1) if x < lo then lo = x end; if x > hi then hi = x end end if (y2 <= yc) ~= (y3 <= yc) then local x = x2 + (yc - y2) * (x3 - x2) / (y3 - y2) if x < lo then lo = x end; if x > hi then hi = x end end if (y3 <= yc) ~= (y1 <= yc) then local x = x3 + (yc - y3) * (x1 - x3) / (y1 - y3) if x < lo then lo = x end; if x > hi then hi = x end end if hi > lo then local xs, xe = floor(lo + 0.5), floor(hi + 0.5) if xe > xs then rect(xs, y, xe - xs, 1, r, g, b) end end end end local function frect(x, y, w, h, r, g, b) local x2 = x + w if x2 <= 0 or x >= W then return end local ix, ix2 = floor(x), floor(x2) if ix2 == ix then blend_rect(ix, y, 1, h, r, g, b, w) return end local cl = ix + 1 - x if cl > 0.04 then blend_rect(ix, y, 1, h, r, g, b, cl) end if ix2 - ix > 1 then rect(ix + 1, y, ix2 - ix - 1, h, r, g, b) end local cr = x2 - ix2 if cr > 0.04 then blend_rect(ix2, y, 1, h, r, g, b, cr) end end local FAR_P, HILL_P, WOOD_P = 128, 96, 80 local FAR_T, HILL_T, WOOD_T = {}, {}, {} do local px_, ph = {}, {} for j = 1, 5 do local h = hash32(j * 0x9E3779B1) & 0x7FFF px_[j] = (j - 1) * 25 + 6 + h % 14 ph[j] = 13 + (h >> 5) % 9 end for x = 0, FAR_P - 1 do local m = 4 for j = 1, 5 do local d = abs(x - px_[j]); if d > FAR_P / 2 then d = FAR_P - d end local v = ph[j] - 0.95 * d if v > m then m = v end end FAR_T[x] = 45 - m end for x = 0, HILL_P - 1 do local a = 2 * PI * x / HILL_P HILL_T[x] = 50 + 3 * sin(a + 0.6) + 2 * sin(3 * a + 1) + 0.8 * sin(7 * a) end local ht = {} for k = 0, WOOD_P // 5 do ht[k] = (hash32((k + 3) * 0x85EBCA6B) & 0x7FFF) % 4 end for x = 0, WOOD_P - 1 do local k = (x + 2) // 5 local d = abs(x - 5 * k) WOOD_T[x] = 54 - ht[k % (WOOD_P // 5)] + d * 0.9 end end local function layer(T, P, off, aa, r, g, b) local run0, runy = 0, nil for x = 0, W - 1 do local pos = (x + off) % P local i = floor(pos) local y = T[i] if aa then local nx = T[(i + 1) % P] y = y + (nx - y) * (pos - i) end local iy = floor(y) if iy ~= runy then if runy then rect(run0, runy + 1, x - run0, H - runy - 1, r, g, b) end run0, runy = x, iy end if aa then local cov = 1 - (y - iy) if cov > 0.06 and cov < 0.97 then blend_rect(x, iy, 1, 1, r, g, b, cov) end if cov >= 0.97 then rect(x, iy, 1, 1, r, g, b) end end end rect(run0, runy + 1, W - run0, H - runy - 1, r, g, b) end local BG, SKYI = nil, 0 local function build_bg() local s = SKIES[SKYI + 1] or SKIES[1] for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(s[1][1] + (s[2][1] - s[1][1]) * t), floor(s[1][2] + (s[2][2] - s[1][2]) * t), floor(s[1][3] + (s[2][3] - s[1][3]) * t)) end ell(53, 13, 8, 8, 0, s[4][1], s[4][2], s[4][3]) ell(53, 13, 5.5, 5.5, 0, s[3][1], s[3][2], s[3][3]) BG = gfx.snapshot(0, 0, W, H) end local K, FL = 1.0, 3 function on_settings() K = (cfg.speed or 3) / 3 FL = cfg.flaps or 3 local s = cfg.sky or 0 if s ~= SKYI then SKYI = s; BG = nil end end local CLOUDS = { { 8, 24, 0.22, 10 }, { 20, 18, 0.38, 50 }, { 30, 14, 0.55, 90 } } local function draw_clouds(frame) for i = 1, #CLOUDS do local c = CLOUDS[i] local w, y = c[2], c[1] local L = W + w + 24 local x = (c[4] - frame * c[3] * K) % L - w frect(x + w * 0.3, y, w * 0.4, 2, CLOUD[1], CLOUD[2], CLOUD[3]) frect(x + w * 0.1, y + 2, w * 0.8, 2, CLOUD[1], CLOUD[2], CLOUD[3]) frect(x, y + 4, w, 1, CLOUD[1], CLOUD[2], CLOUD[3]) frect(x + 1, y + 5, w - 2, 1, CLOUD_S[1], CLOUD_S[2], CLOUD_S[3]) end end local function line(x0, y0, x1, y1, r, g, b) local n = floor(math.max(abs(x1 - x0), abs(y1 - y0)) + 0.5) if n < 1 then n = 1 end for i = 0, n do local t = i / n px(floor(x0 + (x1 - x0) * t + 0.5), floor(y0 + (y1 - y0) * t + 0.5), r, g, b) end end local FING = { { 0.0, 0.0, 4 }, { 0.7, 0.30, 4 }, { 0.35, 0.55, 3 } } local function fingers(tx, ty, ax, ay, bx, by, r, g, b, qx, qy) local len = sqrt(ax * ax + ay * ay) if len < 1 then return end local ux, uy = ax / len, ay / len for i = 1, qx and 3 or 2 do local f = FING[i] local ox, oy = tx, ty if qx and f[1] > 0 then ox, oy = qx + (tx - qx) * f[1], qy + (ty - qy) * f[1] end local c, s = cos(f[2]), sin(f[2]) local dx, dy = ux * c + bx * s, uy * c + by * s line(ox, oy, ox + dx * f[3], oy + dy * f[3], r, g, b) end end local STEP_HZ = 30 local PFLAP, GLIDE = 24, 108 local function eagle_pose(c, F) local alt, d, bob local dg = -1 + 1.5 * sin(2 * PI * c / 90) if c < F then local u = c / F alt = 8 * u * u * (3 - 2 * u) local th = 2 * PI * c / PFLAP local w = c / 6 if (F - c) / 6 < w then w = (F - c) / 6 end if w > 1 then w = 1 end d = dg + (1 - 11 * cos(th) - dg) * w bob = sin(th) * w else alt = 4 + 4 * cos(PI * (c - F) / GLIDE) d, bob = dg, 0 end return alt, d, bob end local function draw_eagle(frame) local F = FL * PFLAP local c = (frame + (F + GLIDE) * 2 - 120) % (F + GLIDE) local alt, d, bob = eagle_pose(c, F) local cx = 33 local cy = 34 - alt - bob local fx, fy = cx - 14, cy + 14 + 0.8 * d tri(cx + 3, cy + 2, cx - 10, cy + 4, fx, fy, 38, 26, 26) fingers(fx, fy, fx - cx + 3, fy - cy - 3, -1, 0, 38, 26, 26) ell(cx - 13, cy + 2, 6, 2.6, 0.12 + 0.05 * sin(frame * 0.2), 244, 244, 248) ell(cx - 11, cy + 3.4, 4, 1.1, 0.12, 206, 208, 222) ell(cx, cy + 1, 9.5, 3.9, -0.1, 70, 46, 34) ell(cx + 1, cy + 2.6, 7, 1.6, -0.1, 52, 34, 28) local icx, icy = floor(cx + 0.5), floor(cy + 0.5) rect(icx + 1, icy + 5, 2, 1, 250, 196, 40) rect(icx - 2, icy + 5, 2, 1, 250, 196, 40) local sx, sy = cx + 4, cy - 2 local tx, ty = cx - 9 - 0.15 * d, cy - 16 + d local ax, ay = tx - sx, ty - sy local len = sqrt(ax * ax + ay * ay) local nx, ny = -ay / len, ax / len if nx < 0 then nx, ny = -nx, -ny end local wx, wy = sx + ax * 0.45 + nx * 2.5, sy + ay * 0.45 + ny * 2.5 local rx_, ry_ = cx - 9, cy + 1 local qx, qy = rx_ + (tx - rx_) * 0.55 - nx * 2, ry_ + (ty - ry_) * 0.55 - ny * 2 tri(sx, sy, wx, wy, rx_, ry_, 66, 44, 32) tri(wx, wy, qx, qy, rx_, ry_, 66, 44, 32) tri(wx, wy, tx, ty, qx, qy, 66, 44, 32) fingers(tx, ty, ax, ay, -nx, -ny, 66, 44, 32, qx, qy) line(sx, sy, wx, wy, 112, 80, 54) line(wx, wy, tx, ty, 112, 80, 54) ell(cx + 9.5, cy - 2.2, 3.9, 3.4, 0, 246, 246, 250) ell(cx + 9.8, cy - 0.4, 3, 1.2, 0, 214, 214, 228) rect(icx + 12, icy - 3, 4, 2, 255, 198, 32) rect(icx + 15, icy - 2, 1, 3, 255, 198, 32) px(icx + 12, icy - 1, 214, 150, 24) rect(icx + 10, icy - 5, 2, 1, 40, 28, 14) px(icx + 11, icy - 4, 12, 10, 8) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local fr = frame * STEP_HZ // gfx.FPS local f = fr * K draw_clouds(fr) layer(FAR_T, FAR_P, (f * 0.1) % FAR_P, true, FARC[1], FARC[2], FARC[3]) layer(HILL_T, HILL_P, (f * 0.4) % HILL_P, true, HILLC[1], HILLC[2], HILLC[3]) draw_eagle(fr) layer(WOOD_T, WOOD_P, floor(f * 0.9) % WOOD_P, false, WOODC[1], WOODC[2], WOODC[3]) end