info = { type = "image", title = "Giraffe savanna", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A giraffe browses an acacia at sunset", api = 33, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Sunset"] = "Západ", ["Noon"] = "Poludnie" }, cs = { ["Time of day"] = "Denní doba", ["Sunset"] = "Západ", ["Noon"] = "Poledne" }, })[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", "noon" }, T("Sunset"), T("Noon") } }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, sqrt, hash32, umod = math.floor, math.sin, math.sqrt, math.hash32, math.umod local W, H = gfx.W, gfx.H local HZ = 45 local KR, KG, KB = 255, 0, 255 local SE = { sunset = { sky = { { 0, 190, 82, 52 }, { 22, 248, 140, 64 }, { 36, 255, 196, 100 }, { 45, 255, 224, 142 } }, sun = { 22, 38, 9 }, sunc = { { 255, 238, 176 }, { 255, 247, 210 }, { 255, 253, 236 } }, glow = { 255, 214, 128, 26, 16 }, hill = { 226, 128, 66 }, gr0 = { 150, 74, 30 }, gr1 = { 66, 30, 18 }, sheen = { 255, 190, 100 }, blade = { 38, 20, 12 }, tip = { 255, 176, 84 }, tree = { 22, 14, 12 }, coat = { { 238, 170, 92 }, { 216, 146, 72 }, { 196, 124, 58 }, { 172, 104, 48 }, { 148, 86, 40 }, { 124, 70, 34 } }, rim = { 255, 206, 120 }, spot = { 108, 56, 26 }, zeb = { { 40, 26, 26 }, { 236, 196, 150 } }, }, noon = { sky = { { 0, 72, 132, 216 }, { 26, 118, 172, 232 }, { 45, 206, 226, 240 } }, sun = { 50, 9, 4 }, sunc = { { 255, 250, 224 }, { 255, 255, 244 }, { 255, 255, 252 } }, glow = { 255, 252, 230, 12, 8 }, hill = { 150, 168, 190 }, gr0 = { 190, 146, 74 }, gr1 = { 112, 78, 36 }, blade = { 90, 66, 28 }, tip = { 236, 208, 120 }, tree = { 30, 40, 24 }, coat = { { 240, 184, 100 }, { 222, 160, 78 }, { 204, 136, 62 }, { 182, 114, 52 }, { 158, 96, 44 }, { 134, 80, 38 } }, rim = { 250, 204, 128 }, spot = { 112, 60, 28 }, zeb = { { 46, 40, 44 }, { 238, 232, 224 } }, }, } local S local function sky_col(c, y) for i = 2, #c do if y <= c[i][1] then local a, b = c[i - 1], c[i] local t = (y - a[1]) / (b[1] - a[1]) return floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5), floor(a[4] + (b[4] - a[4]) * t + 0.5) end end local l = c[#c] return l[2], l[3], l[4] end local PAL = { K = { 40, 24, 18 }, d = { 74, 42, 24 }, e = { 10, 8, 8 }, n = { 62, 36, 28 }, z = { 40, 26, 26 }, y = { 236, 196, 150 }, } local function parse(art, pal) local runs, w = {}, #art[1] for y = 1, #art do local row, x = art[y], 1 while x <= w do local ch, x2 = row:sub(x, x), x while row:sub(x2 + 1, x2 + 1) == ch do x2 = x2 + 1 end local c = pal[ch] if c then runs[#runs + 1] = { y - 1, x - 1, x2 - x + 1, c[1], c[2], c[3], x == 1 or not pal[row:sub(x - 1, x - 1)] } end x = x2 + 1 end end return runs end local function draw(runs, x, y) local ix = floor(x) local f = x - ix for i = 1, #runs do local r = runs[i] local a0, yy, n = ix + r[2], y + r[1], r[3] if r[7] then blend(a0, yy, r[4], r[5], r[6], 1 - f) if n > 1 then rect(a0 + 1, yy, n - 1, 1, r[4], r[5], r[6]) end else rect(a0, yy, n, 1, r[4], r[5], r[6]) end blend(a0 + n, yy, r[4], r[5], r[6], f) end end local BG, HAZE = nil, {} local function disc(cx, cy, r, c, a) for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) if a then blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3], a) else rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3]) end end end local function build_bg() for y = 0, HZ - 1 do rect(0, y, W, 1, sky_col(S.sky, y)) end gfx.clip(0, 0, W, HZ) local sx, sy, sr = S.sun[1], S.sun[2], S.sun[3] local g = S.glow for k = 0, g[5] - 1 do disc(sx, sy, g[4] - floor(k * (g[4] - sr) / g[5]), g, 0.045) end disc(sx, sy, sr, S.sunc[1]) disc(sx, sy, sr * 2 // 3, S.sunc[2]) disc(sx, sy, sr // 3, S.sunc[3]) gfx.clip() local h = S.hill for x = 0, W - 1 do local top = HZ - 2 - floor(1.6 * sin(x * 0.13 + 1) + 1.4 * sin(x * 0.31) + 1.5) rect(x, top, 1, HZ - top, h[1], h[2], h[3]) end for y = HZ, H - 1 do local t = (y - HZ) / (H - 1 - HZ) rect(0, y, W, 1, floor(S.gr0[1] + (S.gr1[1] - S.gr0[1]) * t + 0.5), floor(S.gr0[2] + (S.gr1[2] - S.gr0[2]) * t + 0.5), floor(S.gr0[3] + (S.gr1[3] - S.gr0[3]) * t + 0.5)) end for k = 0, S.sheen and 7 or -1 do local w = S.glow[4] - k * 2 blend_rect(sx - w // 2, HZ + k, w, 1, S.sheen[1], S.sheen[2], S.sheen[3], 0.30 - k * 0.032) end local t = S.tree rect(14, 19, 3, HZ - 19, t[1], t[2], t[3]) for k = 0, 9 do rect(15 - k, 18 - k // 2, 2, 1, t[1], t[2], t[3]) rect(16 + k, 18 - k // 2, 2, 1, t[1], t[2], t[3]) end for _, l in ipairs({ { 14, 11, 13, 3 }, { 29, 12, 11, 3 }, { 6, 13, 8, 2 }, { 39, 13, 8, 2 }, { 22, 9, 12, 2 }, { 20, 15, 18, 1 }, { 4, 10, 5, 1 }, { 33, 9, 8, 1 } }) do for dy = -l[4], l[4] do local hw = floor(l[3] * sqrt(1 - (dy / (l[4] + 0.6)) ^ 2) + 0.5) rect(l[1] - hw, l[2] + dy, 2 * hw + 1, 1, t[1], t[2], t[3]) end end for i = 1, 26 do local hh = hash32(i * 0x27D4EB2F) local x, y = 3 + umod(hh, 40), 8 + umod(hh >> 8, 8) rect(x, y, 1 + umod(hh >> 16, 2), 1, sky_col(S.sky, y)) end for i = 1, 14 do local hh = hash32(i * 0x165667B1) rect(2 + umod(hh, 42), 15 + umod(hh >> 8, 2), 1 + umod(hh >> 12, 2), 1 + umod(hh >> 16, 2), t[1], t[2], t[3]) end BG = gfx.snapshot(0, 0, W, H) for k = 0, 5 do HAZE[k] = gfx.snapshot(0, 39 + k, W, 1) end end local BODY, POSE = nil, {} local HPAL local BX, BY, BW, BH = 36, 22, 26, 26 local NX, NY, NW, NH = 20, 9, 26, 19 local function c3(c) return c[1], c[2], c[3] end local ROWS = { { 39, 47 }, { 38, 52 }, { 38, 55 }, { 38, 57 }, { 38, 58 }, { 38, 58 }, { 39, 58 }, { 40, 57 }, { 42, 55 }, { 45, 52 }, } local SPOTS = { { 40, 26, 2, 2 }, { 44, 25, 3, 2 }, { 49, 27, 3, 3 }, { 53, 28, 2, 2 }, { 41, 29, 3, 2 }, { 46, 30, 3, 2 }, { 51, 31, 3, 2 }, { 55, 31, 2, 2 }, { 43, 32, 2, 1 }, { 56, 26, 2, 2 } } local function bake_body() rect(BX, BY, BW, BH, KR, KG, KB) local C = S.coat local sp = S.spot rect(43, 32, 2, 14, c3(C[5])); rect(43, 44, 2, 2, 40, 26, 20) rect(56, 32, 2, 14, c3(C[5])); rect(56, 44, 2, 2, 40, 26, 20) for i = 1, #ROWS do local r, y = ROWS[i], 23 + i rect(r[1], y, r[2] - r[1] + 1, 1, c3(C[math.min(1 + (i - 1) * 5 // 9, 6)])) rect(r[1], y, 1, 1, c3(i < 8 and S.rim or C[3])) if i <= 2 then rect(r[1] + 1, y, r[2] - r[1] - 1, 1, c3(S.rim)) end end for i = 1, #SPOTS do local p = SPOTS[i] rect(p[1], p[2], p[3], p[4], c3(sp)) end for _, lx in ipairs({ 39, 54 }) do rect(lx, 32, 2, 12, c3(C[3])); rect(lx, 32, 1, 12, c3(C[2])) rect(lx, 40, 2, 4, c3(C[4])); rect(lx, 44, 2, 2, 40, 26, 20) rect(lx, 35, 2, 2, c3(sp)) end BODY = gfx.snapshot(BX, BY, BW, BH) end local HEAD = { "....K.K.....", "....d.d.cc..", "LLLLcccccc..", "nLccccecccc.", "nncccccccc..", ".nncccc.....", } local HEAD_B = { HEAD[1], HEAD[2], HEAD[3], HEAD[4], HEAD[5], "..nncccc...." } local function bake_pose(k, hx, hy, jaw) rect(NX, NY, NW, NH, KR, KG, KB) local C = S.coat local tx, ty, sx, sy = hx + 8, hy + 4, 42.5, 26 for y = ty, sy do local t = (y - ty) / (sy - ty) local x0 = floor(tx + (sx - tx) * t - 2 + 0.5) local w = 5 + floor(3 * t + 0.5) rect(x0, y, 1, 1, c3(S.rim)) rect(x0 + 1, y, 1, 1, c3(C[1])) rect(x0 + 2, y, w - 4, 1, c3(C[3])) rect(x0 + w - 2, y, 1, 1, c3(C[5])) rect(x0 + w - 1, y, 1, 1, 56, 32, 20) if (y - ty) % 4 == 2 then rect(x0 + 2, y, 2, 2, c3(S.spot)) end end draw(parse(jaw and HEAD_B or HEAD, HPAL), hx, hy) POSE[k] = gfx.snapshot(NX, NY, NW, NH) end local function head_pal() local p = { c = S.coat[3], L = S.rim } for k, v in pairs(PAL) do p[k] = v end return p end local POSES = { { 26, 17 }, { 25, 16 }, { 24, 15 }, { 24, 14, false }, { 24, 14, true }, { 28, 18 }, { 29, 20 }, } local SEQ = { { 1, 60 }, { 2, 10 }, { 3, 10 }, { 4, 14 }, { 5, 14 }, { 4, 14 }, { 5, 14 }, { 4, 14 }, { 5, 14 }, { 3, 10 }, { 2, 10 }, { 1, 70 }, { 6, 30 }, { 7, 50 }, { 6, 30 }, { 1, 40 }, } local SEQLEN = 0 for i = 1, #SEQ do SEQLEN = SEQLEN + SEQ[i][2] end local ZEBRA_A = { "zz......", "zzzzzzzz", ".zyzzyzz", ".z.z.z.z", ".z.z.z.z" } local ZEBRA_B = { "zz......", "zzzzzzzz", ".zyzzyzz", "..z.z.z.", "..z.z.z." } local ZR = {} local BLADES = {} for i = 1, 22 do local h = hash32(i * 0x85EBCA6B) BLADES[i] = { x = umod(h, W), h = 3 + umod(h >> 6, 5), ph = umod(h >> 12, 628) / 100 } end local BIRDS = { { 8, 20, 0.09, 0 }, { 14, 25, 0.075, 2.2 }, { 40, 17, 0.06, 4 } } local stage = 0 function on_settings() S = SE[cfg.time] or SE.sunset HPAL = head_pal() BG, BODY, POSE, stage = nil, nil, {}, 0 local zc = S.zeb local zp = { z = zc[1], y = zc[2] } ZR[1], ZR[2] = parse(ZEBRA_A, zp), parse(ZEBRA_B, zp) end local function dot3(x, y, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 40, 20, 14, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, 40, 20, 14, a * fx * (1 - fy)) blend(ix, iy + 1, 40, 20, 14, a * (1 - fx) * fy) blend(ix + 1, iy + 1, 40, 20, 14, a * fx * fy) end function render() if not S then on_settings() end if stage == 0 then build_bg(); stage = 1 elseif stage == 1 then bake_body(); stage = 2 elseif stage <= 8 then local p = POSES[stage - 1] bake_pose(stage - 1, p[1], p[2], p[3] == true) stage = stage + 1 end if not BG then gfx.clear(0, 0, 0) return end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame for k = 0, 5 do local dx = floor(sin(f * 0.11 + k * 1.3) * 1.2 + 0.5) if dx ~= 0 then gfx.blit_rgb(HAZE[k], dx, 39 + k, W, 1) end end for i = 1, #BIRDS do local b = BIRDS[i] local x = (b[1] + f * b[3]) % (W + 12) - 6 local fl = (f // 8 + i) % 2 dot3(x, b[2] + fl, 0.8); dot3(x - 2, b[2] + 1 - fl, 0.7); dot3(x + 2, b[2] + 1 - fl, 0.7) end for i = 1, 5 do local x = (W + 14) - ((f * 0.11 + i * 17) % (W + 28)) draw(ZR[(f // 10 + i) % 2 + 1], x - 8, 43 + i % 2) end if BODY then blend_rect(37, 45, 24, 2, 0, 0, 0, 0.3) gfx.blit_rgb(BODY, BX, BY, BW, BH, KR, KG, KB) local t, acc = f % SEQLEN, 0 local pose = 1 for i = 1, #SEQ do acc = acc + SEQ[i][2] if t < acc then pose = SEQ[i][1]; break end end if POSE[pose] then gfx.blit_rgb(POSE[pose], NX, NY, NW, NH, KR, KG, KB) end local sw = (f // 22) % 5 == 0 and 1 or 0 rect(59, 28, 1, 5, 110, 64, 34) rect(59 + sw, 33, 1, 2, 110, 64, 34) rect(59 + sw, 35, 1, 2, 40, 24, 18) end local bc, tc = S.blade, S.tip for i = 1, #BLADES do local b = BLADES[i] local lean = floor(sin(f * 0.05 + b.ph + b.x * 0.2) * 1.2 + 0.5) rect(b.x, H - b.h, 1, b.h, bc[1], bc[2], bc[3]) rect(b.x + lean, H - b.h - 1, 1, 2, bc[1], bc[2], bc[3]) blend(b.x + lean, H - b.h - 2, tc[1], tc[2], tc[3], 0.6) end end