info = { type = "image", title = "Penguins", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Chubby penguins waddle on the snowy shore", api = 33, } local tr = ({ sk = { ["Penguins"] = "Tučniaky", ["Snowfall"] = "Sneženie" }, cs = { ["Penguins"] = "Tučňáci", ["Snowfall"] = "Sněžení" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Penguins"), type = "int", min = 1, max = 3, default = 3 }, { key = "snow", label = T("Snowfall"), type = "int", min = 0, max = 10, default = 5 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin = math.floor, math.sin local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local SKY = { { 96, 92, 190 }, { 168, 130, 210 }, { 236, 160, 190 } } local SEA, WAVE = { 40, 96, 170 }, { 120, 178, 228 } local BERG, BERG_S = { 236, 232, 252 }, { 176, 168, 226 } local SNOW, DRIFT = { 176, 184, 232 }, { 138, 142, 206 } local EDGE = { 120, 200, 240 } local BODY, BELLY = { 30, 34, 64 }, { 246, 248, 255 } local BEAK, CHEEK = { 255, 150, 40 }, { 255, 140, 170 } local EYE = { 16, 16, 28 } local FLAKE = { 255, 255, 255 } local SKY_H = H * 26 // 64 local SHORE = SKY_H + 10 local function c3(c) return c[1], c[2], c[3] end local BG = nil local function build_bg() local bh = SKY_H // 3 for i = 1, 3 do rect(0, (i - 1) * bh, W, (i < 3) and bh or SKY_H - 2 * bh, c3(SKY[i])) end for x = 0, W - 1, 2 do px(x, bh - 1, c3(SKY[2])); px(x + 1, 2 * bh - 1, c3(SKY[3])) end rect(0, SKY_H, W, SHORE - SKY_H, c3(SEA)) local KR = { 3, 5, 6, 7, 7, 8, 8 } for j = 1, #KR do local y = SKY_H - #KR + j - 1 rect(13 - KR[j], y, KR[j], 1, c3(BERG)) rect(13, y, KR[j] - 2 + (j // 2), 1, c3(BERG_S)) end for x = 0, W - 1 do local e = SHORE - 1 + umod(hash32(x // 3 * 0x9E3779B1), 2) rect(x, e, 1, H - e, c3(SNOW)) px(x, e, c3(EDGE)) end local D = { { 2, H - 8, 18, 2 }, { 6, H - 6, 12, 2 }, { 38, H - 12, 22, 2 }, { 44, H - 10, 14, 2 } } for i = 1, #D do local d = D[i] rect(d[1], d[2], d[3], d[4], c3(DRIFT)) end BG = gfx.snapshot(0, 0, W, H) end local BIG = { dark = { 2, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4, 3 }, wy = 3, white = { 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 4, 2 }, eye = 5, edx = 2, beak = 7, cheek = 8, cdx = 4, fl = 8, fdx = 3, } local SMALL = { dark = { 1, 3, 4, 4, 5, 5, 5, 5, 5, 4, 3, 2 }, wy = 2, white = { 2, 3, 3, 3, 3, 4, 4, 3, 2 }, eye = 4, edx = 2, beak = 5, cheek = 6, cdx = 3, fl = 6, fdx = 2, } local function penguin(k, x, fy, lean, look, lift, shut, wing) local d = k.dark local n = #d local top = fy - n local half = n // 2 for r = 1, n do local hw = d[r] local s = r <= half and lean or 0 rect(x - hw + s, top + r - 1, 2 * hw + 1, 1, c3(BODY)) end local w = k.white for r = 1, #w do local y = top + k.wy + r - 1 local s = (k.wy + r) <= half and lean or 0 rect(x - w[r] + s + look, y, 2 * w[r] + 1, 1, c3(BELLY)) end local s = lean + look px(x + s, top + k.wy, c3(BODY)) local ey = top + k.eye if shut then px(x - k.edx + s, ey + 1, c3(EYE)); px(x + k.edx + s, ey + 1, c3(EYE)) else rect(x - k.edx + s, ey, 1, 2, c3(EYE)); rect(x + k.edx + s, ey, 1, 2, c3(EYE)) end local by = top + k.beak rect(x - 1 + s + look, by, 3, 1, c3(BEAK)) px(x + s + look, by + 1, c3(BEAK)) local cy = top + k.cheek local sc = (k.cheek < half) and s or look px(x - k.cdx + sc, cy, c3(CHEEK)); px(x + k.cdx + sc, cy, c3(CHEEK)) local fl = top + k.fl local ww = d[k.fl + 1] + 1 if wing then rect(x - ww - 1, fl - 2, 1, 2, c3(BODY)); px(x - ww, fl, c3(BODY)) rect(x + ww + 1, fl - 2, 1, 2, c3(BODY)); px(x + ww, fl, c3(BODY)) else rect(x - ww, fl, 1, 4, c3(BODY)); rect(x + ww, fl, 1, 4, c3(BODY)) end local fd = k.fdx rect(x - fd - 1, fy - (lift == -1 and 1 or 0), 2, 1, c3(BEAK)) rect(x + fd, fy - (lift == 1 and 1 or 0), 2, 1, c3(BEAK)) end local LAYOUT = { { { BIG, 10, W - 11, H - 9 } }, { { BIG, 30, W - 11, H - 9 }, { SMALL, 14, 14, H - 7 } }, { { BIG, 26, 38, H - 9 }, { SMALL, 12, 12, H - 7 }, { SMALL, W - 11, W - 11, H - 6 } }, } local CAST, NFLAKE = LAYOUT[3], 15 local STEP = STEP_HZ // 3 function on_settings() CAST = LAYOUT[cfg.n or 3] or LAYOUT[3] NFLAKE = 3 * (cfg.snow or 5) end local function walker(p, t) local L = p[3] - p[2] local s = t // STEP local pos = s % (2 * L) local x, dir = p[2] + pos, 1 if pos >= L then x, dir = p[3] - (pos - L), -1 end local ph = s % 2 == 0 and 1 or -1 local blink = (t + 40) % (4 * STEP_HZ) < 3 penguin(p[1], x, p[4], ph, dir, -ph, blink, false) end local function stander(p, t, i) local seed = hash32(i * 0x85EBCA6B) local ofs = umod(seed, 7 * STEP_HZ) local tt = t + ofs local lean = umod(tt // STEP_HZ, 4) == 1 and 1 or (umod(tt // STEP_HZ, 4) == 3 and -1 or 0) local lk = umod(hash32((tt // (3 * STEP_HZ)) * 0x9E3779B1 + seed), 3) - 1 local blink = tt % (5 * STEP_HZ) < 3 local hop = tt % (7 * STEP_HZ) local dy, wing = 0, false if hop < 12 then local u = hop / 11 dy = -floor(4 * 4 * u * (1 - u) + 0.5) wing = true end penguin(p[1], p[2], p[4] + dy, lean, lk, 0, blink, wing) end local WV = {} for i = 1, 6 do local h = hash32(i * 0x27D4EB2F) WV[i] = { y = SKY_H + 1 + umod(h, SHORE - SKY_H - 3), x0 = umod(h >> 8, W), len = 3 + umod(h >> 16, 4), v = (umod(h >> 20, 2) * 2 - 1) * (0.08 + umod(h >> 24, 8) / 100.0) } end local function waves(t) for i = 1, #WV do local w = WV[i] local x = (w.x0 + t * w.v) % (W + 8) - 4 local xi = floor(x) local f = x - xi rect(xi + 1, w.y, w.len - 1, 1, c3(WAVE)) blend(xi, w.y, WAVE[1], WAVE[2], WAVE[3], 1 - f) blend(xi + w.len, w.y, WAVE[1], WAVE[2], WAVE[3], f) end end local FX, FV, FO, FA = {}, {}, {}, {} for i = 1, 30 do local h = hash32(i * 0x9E3779B1) FX[i], FV[i], FO[i], FA[i] = umod(h, W), 0.25 + umod(h >> 8, 30) / 100.0, umod(h >> 16, 997), umod(h >> 24, 628) / 100.0 end local function snow(t) for i = 1, NFLAKE do local yf = (FO[i] + t * FV[i]) % (H + 2) - 1 local y = floor(yf) local f = yf - y local x = floor(FX[i] + 1.5 * sin(t * 0.04 + FA[i]) + 0.5) % W blend(x, y, FLAKE[1], FLAKE[2], FLAKE[3], 1 - f) blend(x, y + 1, FLAKE[1], FLAKE[2], FLAKE[3], f) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS waves(t) for i = 2, #CAST do stander(CAST[i], t, i) end walker(CAST[1], t) snow(t) end