info = { type = "image", title = "Ice bear dusk", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A polar bear on an ice floe at dusk", api = 44, } local tr = ({ sk = { ["Sky"] = "Obloha", ["Dusk"] = "Súmrak", ["Aurora night"] = "Noc s polárnou žiarou" }, cs = { ["Sky"] = "Obloha", ["Dusk"] = "Soumrak", ["Aurora night"] = "Noc s polární září" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sky", label = T("Sky"), type = "int", default = "dusk", opts = { keys = { "dusk", "night" }, T("Dusk"), T("Aurora night") } }, } local rect, px, blend, blend_rect, blit_rgb, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_rgb, gfx.blit_indexed local floor, ceil, sin, sqrt, min, max = math.floor, math.ceil, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ = 31 local RN = 16 local NIGHT = false local PAL, BG = nil, nil local function stop(S, t) for i = 2, #S do local a, b = S[i - 1], S[i] if t <= b[1] then local u = (t - a[1]) / (b[1] - a[1]) return floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5), floor(a[4] + (b[4] - a[4]) * u + 0.5) end end local z = S[#S] return z[2], z[3], z[4] end function on_settings() NIGHT = cfg.sky == "night" local fur = NIGHT and { { 0, 30, 40, 76 }, { 0.5, 120, 146, 190 }, { 0.8, 190, 226, 232 }, { 1, 196, 255, 220 } } or { { 0, 54, 66, 110 }, { 0.5, 168, 184, 222 }, { 0.8, 236, 226, 238 }, { 1, 255, 214, 176 } } local ice = NIGHT and { { 0, 24, 56, 92 }, { 0.5, 104, 160, 196 }, { 0.85, 190, 230, 240 }, { 1, 200, 255, 230 } } or { { 0, 48, 92, 136 }, { 0.5, 150, 196, 226 }, { 0.85, 226, 238, 248 }, { 1, 255, 226, 200 } } PAL = { 0, 0, 0 } for _, S in ipairs({ fur, ice }) do for i = 0, RN - 1 do local r, g, b = stop(S, i / (RN - 1)) PAL[#PAL + 1] = r; PAL[#PAL + 1] = g; PAL[#PAL + 1] = b end end local w = NIGHT and { 24, 70, 90 } or { 60, 116, 140 } for _, v in ipairs({ w[1], w[2], w[3], 10, 12, 22, 12, 14, 28 }) do PAL[#PAL + 1] = v end BG = nil end local CV = nil local function put(x, y, v) local c = CV if x >= c.x0 and x < c.x0 + c.w and y >= c.y0 and y < c.y0 + c.h then c.g[(y - c.y0) * c.w + x - c.x0 + 1] = v end end local FUR, ICE = 1, 17 local BP, BR = 3, 4.5 local function blob(parts, amb, k, solid) local c, dy = CV, CV.dy local pw, ph = c.w + 2 * BP, c.h + 2 * BP local X0, Y0 = c.x0 - BP, c.y0 - BP local IN, D = {}, {} for i = 1, pw * ph do IN[i] = false; D[i] = 0 end local function mark(list, own) for _, e in ipairs(list) do local cx, cy, rx, ry = e[1], e[2] + dy, e[3], e[4] for y = max(Y0, ceil(cy - ry - 0.5)), min(Y0 + ph - 1, floor(cy + ry - 0.5)) do local ny = (y + 0.5 - cy) / ry local a = 1 - ny * ny if a > 0 then local hw, row = rx * sqrt(a), (y - Y0) * pw - X0 + 1 for x = max(X0, ceil(cx - hw - 0.5)), min(X0 + pw - 1, floor(cx + hw - 0.5)) do D[row + x] = BR if own then IN[row + x] = true end end end end end end mark(parts, true) if solid then mark(solid, false) end for j = 1, ph - 2 do for i = 1, pw - 2 do local id = j * pw + i + 1 local v = D[id] if v > 0 then local t = D[id - 1] + 1; if t < v then v = t end t = D[id - pw] + 1; if t < v then v = t end t = D[id - pw - 1] + 1.41; if t < v then v = t end t = D[id - pw + 1] + 1.41; if t < v then v = t end D[id] = v end end end for j = ph - 2, 1, -1 do for i = pw - 2, 1, -1 do local id = j * pw + i + 1 local v = D[id] if v > 0 then local t = D[id + 1] + 1; if t < v then v = t end t = D[id + pw] + 1; if t < v then v = t end t = D[id + pw + 1] + 1.41; if t < v then v = t end t = D[id + pw - 1] + 1.41; if t < v then v = t end D[id] = v end end end for i = 1, pw * ph do local v = D[i]; if v > 0 then D[i] = sqrt(v / BR) end end for j = BP, BP + c.h - 1 do local y = Y0 + j local gy = 0.1 - 0.25 * (y + 0.5 - 30 - dy) / 20 for i = BP, BP + c.w - 1 do local id = j * pw + i + 1 if IN[id] then local nx = (D[id - 1] - D[id + 1]) * 1.5 local ny = (D[id - pw] - D[id + pw]) * 1.5 local d = (-0.62 * nx - 0.3 * ny + 0.72) / sqrt(nx * nx + ny * ny + 1) + gy local s = amb + (1 - amb) * (d < 0 and 0 or d > 1 and 1 or d) put(X0 + i, y, FUR + floor(s * k * (RN - 1) + 0.5)) end end end end local function flat(cx, cy, rx, ry, v) cy = cy + CV.dy for y = ceil(cy - ry - 0.5), floor(cy + ry - 0.5) do local ny = (y + 0.5 - cy) / ry local a = 1 - ny * ny if a > 0 then local hw = rx * sqrt(a) for x = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) do put(x, y, v) end end end end local function draw_floe(xa, xb) for x = xa, xb do local nx = (x + 0.5 - 34) / 28.5 local a = 1 - nx * nx if a > 0 then local e = 5.6 * sqrt(a) local y0, y1 = ceil(47 - e - 0.5 + CV.dy), floor(47 + e - 0.5 + CV.dy) for y = y0, y1 do local s = 0.74 - 0.2 * nx - 0.12 * (y + 0.5 - 47 - CV.dy) / 5.6 put(x, y, ICE + floor((s < 0 and 0 or s > 1 and 1 or s) * (RN - 1) + 0.5)) end local th = floor(4 * (1 - 0.5 * nx * nx) + 0.5) for v = 1, th do local s = 0.56 - 0.3 * nx - 0.3 * v / th put(x, y1 + v, ICE + floor((s < 0 and 0 or s > 1 and 1 or s) * (RN - 1) + 0.5)) end put(x, y1 + th + 1, 33); put(x, y1 + th + 2, 33) end end end local BODY = { { 40, 38, 12.5, 8 }, { 48, 39, 7, 7.5 }, { 31, 37, 7, 8 }, { 30, 45, 3, 4.5 }, { 47, 45.5, 3.5, 4 }, { 54.5, 36.5, 1.6, 1.6 } } local function draw_far() blob({ { 34, 45.5, 2.8, 4 }, { 52, 45, 3, 3.8 } }, 0.1, 0.8) end local function draw_body() blob(BODY, 0.18, 1) end local HEAD = { { { 20.5, 32.5, 5.8, 5.2 }, { 14.5, 34.8, 4.6, 3.1 }, { 23.5, 27.8, 2.1, 2.1 }, { 26, 35, 6, 6.5 }, { 10.2, 33.8, 1.7, 1.4 } }, { { 20, 35.5, 5.8, 5.2 }, { 14.5, 39.5, 4.4, 3 }, { 23.3, 30.5, 2.1, 2.1 }, { 26, 37, 6, 6 }, { 10.4, 40.5, 1.7, 1.4 } }, { { 19.5, 39.5, 5.8, 5 }, { 14.2, 44.6, 3.2, 4.2 }, { 23, 35, 2.1, 2.1 }, { 26.5, 39, 6, 6.5 }, { 13.4, 48.3, 1.6, 1.3 } }, } local EYE = { { 17, 31 }, { 17, 34 }, { 17, 39 } } local function draw_head(n) local h = HEAD[n] blob({ h[4], h[1], h[3], h[2] }, 0.18, 1, BODY) flat(h[5][1], h[5][2], h[5][3], h[5][4], 34) end local JOBS = {} for v = 0, 1 do JOBS[#JOBS + 1] = { "f" .. v, 4, 40, 60, 20, v * 0.5, function() draw_floe(5, 34) end, 1 } JOBS[#JOBS + 1] = { "f" .. v, 4, 40, 60, 20, v * 0.5, function() draw_floe(35, 63) end, 2 } JOBS[#JOBS + 1] = { "b" .. v, 24, 28, 34, 24, v * 0.5, draw_far, 1 } JOBS[#JOBS + 1] = { "b" .. v, 24, 28, 34, 24, v * 0.5, draw_body } JOBS[#JOBS + 1] = { "h1" .. v, 6, 24, 24, 28, v * 0.5, function() draw_head(1) end } end for n = 2, 3 do for v = 0, 1 do JOBS[#JOBS + 1] = { "h" .. n .. v, 6, 24, 24, 28, v * 0.5, function() draw_head(n) end } end end local GRID = {} local nj, cur = 1, nil local function build_step() local j = JOBS[nj] if not j then return end nj = nj + 1 if not cur then local g = {} for i = 1, j[4] * j[5] do g[i] = 0 end cur = { slot = j[1], x0 = j[2], y0 = j[3], w = j[4], h = j[5], dy = j[6], g = g } end CV = cur j[7]() CV = nil if j[8] ~= 1 then local rows, g = {}, cur.g for y = 0, cur.h - 1 do rows[y + 1] = string.char(table.unpack(g, y * cur.w + 1, y * cur.w + cur.w)) end GRID[cur.slot] = table.concat(rows) cur = nil end end local function berg(x, y, w, h, body, lit) for r = 0, h - 1 do local ww = floor(w * (0.4 + 0.6 * (r + 1) / h)) local xx = x + floor((w - ww) * 0.4) rect(xx, y + r, ww, 1, body[1], body[2], body[3]) rect(xx, y + r, 1, 1, lit[1], lit[2], lit[3]) end end local STAR = {} for i = 1, 16 do local h = hash32(i * 0x9E3779B1) STAR[i] = { umod(h, W), umod(h >> 8, 24), umod(h >> 16, 628) / 100 } end local STREAK = {} for i = 1, 10 do local h = hash32(i * 0x85EBCA6B) STREAK[i] = { umod(h, 80), 34 + (i - 1) * 3 + umod(h >> 8, 2), 5 + umod(h >> 12, 10), (0.04 + umod(h >> 16, 12) / 100) * (i % 2 == 0 and 1 or -1) } end local function build_bg() local sky = NIGHT and { { 0, 4, 6, 20 }, { 30, 22, 38, 72 } } or { { 0, 24, 28, 76 }, { 14, 88, 58, 128 }, { 26, 214, 120, 120 }, { 30, 246, 164, 118 } } local sea = NIGHT and { { 31, 30, 60, 92 }, { 63, 6, 14, 36 } } or { { 31, 168, 104, 112 }, { 36, 118, 80, 118 }, { 48, 52, 52, 98 }, { 63, 18, 26, 62 } } for y = 0, HZ - 1 do rect(0, y, W, 1, stop(sky, y)) end for y = HZ, 63 do rect(0, y, W, 1, stop(sea, y)) end if not NIGHT then for _, r in ipairs({ { 30, 5 }, { 29, 5 }, { 28, 4 }, { 27, 3 }, { 26, 1 } }) do rect(10 - r[2], r[1], 2 * r[2] + 1, 1, 255, 232, 176) end for y = 33, 49, 2 do blend_rect(10 - (50 - y) // 6, y, 2 * ((50 - y) // 6) + 1, 1, 255, 190, 140, 0.4) end blend_rect(14, 8, 22, 1, 255, 170, 130, 0.3); blend_rect(30, 12, 26, 1, 255, 170, 130, 0.3) blend_rect(6, 16, 18, 1, 255, 150, 130, 0.3); blend_rect(36, 19, 20, 1, 255, 150, 130, 0.25) end local body = NIGHT and { 50, 86, 124 } or { 110, 120, 172 } local lit = NIGHT and { 110, 170, 190 } or { 232, 170, 160 } berg(22, 26, 17, 5, body, lit); berg(46, 27, 11, 4, body, lit); berg(60, 28, 6, 3, body, lit) BG = gfx.snapshot(0, 0, W, H) end local function streak(xf, y, len, r, g, b, a) local x = floor(xf) local f = xf - x blend_rect(x + 1, y, len - 1, 1, r, g, b, a) blend(x, y, r, g, b, a * (1 - f)) blend(x + len, y, r, g, b, a * f) end local function aurora(ts) for i = 0, 15 do local x0 = i * 4 local h = 9 + 7 * sin(i * 0.7 + ts * 0.5) + 3 * sin(i * 1.9 - ts * 0.8) local yb = 22 + 3 * sin(i * 0.4 + ts * 0.3) local a = 0.12 + 0.07 * sin(i * 2.3 + ts * 0.6) blend_rect(x0, floor(yb - h), 4, floor(h), 150, 90, 230, a * 0.8) blend_rect(x0, floor(yb - h * 0.6), 4, floor(h * 0.6), 80, 255, 170, a * 1.4) blend_rect(x0, floor(yb - h * 0.25), 4, floor(h * 0.25) + 1, 150, 255, 190, a * 1.6) end end function render() if not PAL then on_settings() end if not BG then build_bg() end build_step() blit_rgb(BG, 0, 0, W, H) local ts = frame / FPS if NIGHT then for i = 1, 16 do local s = STAR[i] blend(s[1], s[2], 220, 230, 255, 0.35 + 0.35 * sin(ts * 1.7 + s[3])) end aurora(ts) else for i = 1, 8 do local h = hash32(i * 0xC2B2AE35) local a = sin(ts * 2.1 + i * 1.7) * 0.5 + 0.05 if a > 0 then blend(4 + umod(h, 14), 33 + umod(h >> 8, 14), 255, 220, 170, a) end end end for i = 1, 10 do local s = STREAK[i] local x = (s[1] + ts * s[4] * FPS) % 90 - 12 if NIGHT then streak(x, s[2], s[3], 120, 220, 200, 0.12) else streak(x, s[2], s[3], 230, 150, 150, 0.2) end end for i = 0, 3 do blend_rect(8 + i * 5, 59 + i, 52 - i * 10, 1, 6, 8, 24, 0.28 - i * 0.05) end local q = floor((0.5 + 0.5 * sin(ts * 1.14)) * 2 + 0.5) / 2 local v, oy = floor(q * 2) % 2, q == 1 and 1 or 0 local t = ts % 14 local hp = (t >= 5 and t < 5.4 or t >= 8.6 and t < 9) and 2 or (t >= 5.4 and t < 8.6) and 3 or 1 if GRID["f" .. v] then blit(GRID["f" .. v], PAL, 4, 40 + oy, 60, 20, 60, 20, true) end if GRID["b" .. v] then blit(GRID["b" .. v], PAL, 24, 28 + oy, 34, 24, 34, 24, true) end local hg = GRID["h" .. hp .. v] if not (GRID["f" .. v] and GRID["b" .. v] and hg) then gfx.ready(false) end if hg then blit(hg, PAL, 6, 24 + oy, 24, 28, 24, 28, true) local e = EYE[hp] if (ts + 1.3) % 4.1 > 0.14 then rect(e[1], e[2] + oy, 1, 2, 12, 14, 28) end end if hp == 3 then local r = floor((ts * 3) % 3) blend_rect(7 - r, 52, 2 + 2 * r, 1, 200, 190, 220, 0.35 - r * 0.1) end end