info = { type = "image", title = "Fawn ride", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fawn carries a squirrel through blossoms", api = 33, } local tr = ({ sk = { ["Walk"] = "Chôdza", ["Slow"] = "Pomalá", ["Steady"] = "Stála", ["Brisk"] = "Svižná", ["Petals"] = "Lupene", ["Blossom"] = "Kvet", ["Pink"] = "Ružový", ["White"] = "Biely", ["Lilac"] = "Orgovánový" }, cs = { ["Walk"] = "Chůze", ["Slow"] = "Pomalá", ["Steady"] = "Stálá", ["Brisk"] = "Svižná", ["Petals"] = "Lístky", ["Blossom"] = "Květ", ["Pink"] = "Růžový", ["White"] = "Bílý", ["Lilac"] = "Šeříkový" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Walk"), type = "int", default = "steady", opts = { keys = { "slow", "steady", "brisk" }, T("Slow"), T("Steady"), T("Brisk") } }, { key = "petals", label = T("Petals"), type = "int", min = 0, max = 24, default = 12 }, { key = "bloom", label = T("Blossom"), type = "int", default = "pink", opts = { keys = { "pink", "white", "lilac" }, T("Pink"), T("White"), T("Lilac") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit, blit_rgb, snapshot = gfx.blit_indexed, gfx.blit_rgb, gfx.snapshot local floor, ceil, sin, sqrt, cos, min, max, abs = math.floor, math.ceil, math.sin, math.sqrt, math.cos, math.min, math.max, math.abs local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H = gfx.W, gfx.H local TAU = 6.28318 local BASE = { { 238, 146, 60 }, { 255, 188, 106 }, { 170, 82, 38 }, { 255, 232, 200 }, { 226, 184, 146 }, { 255, 146, 152 }, { 52, 26, 30 }, { 255, 250, 240 }, { 200, 108, 46 }, { 100, 56, 44 }, { 86, 40, 50 }, { 178, 92, 54 }, { 224, 146, 92 }, { 206, 122, 68 }, { 150, 96, 52 }, { 200, 146, 84 }, { 104, 54, 34 }, { 56, 140, 52 }, { 92, 188, 62 }, { 150, 218, 86 }, { 255, 140, 190 }, { 255, 226, 90 }, { 176, 120, 230 }, } local PAL, NPAL = { 0, 0, 0 }, #BASE for i = 1, #BASE do local c = BASE[i]; PAL[3 * i + 1], PAL[3 * i + 2], PAL[3 * i + 3] = c[1], c[2], c[3] end local MIXC = {} local function mix(a, b) if a == b then return a end if a > b then a, b = b, a end local k = a * 256 + b local m = MIXC[k] if m then return m end if NPAL >= 255 then return a end NPAL = NPAL + 1 MIXC[k] = NPAL for j = 1, 3 do PAL[3 * NPAL + j] = (PAL[3 * a + j] + PAL[3 * b + j]) // 2 end return NPAL end local function newgrid(w, h) local g = {} for i = 1, w * h do g[i] = 0 end return g end local function fell(g, w, h, cx, cy, rx, ry, c, only) for y = max(0, floor(cy - ry)), min(h - 1, ceil(cy + ry)) do for x = max(0, floor(cx - rx)), min(w - 1, ceil(cx + rx)) do local dx, dy = (x + 0.5 - cx) / rx, (y + 0.5 - cy) / ry if dx * dx + dy * dy <= 1 then local i = y * w + x + 1 if not only or g[i] == (only == -1 and 0 or only) then g[i] = c end end end end end local function edge(g, w, h, map) local o = {} for y = 0, h - 1 do for x = 0, w - 1 do local i = y * w + x + 1 local m = map[g[i]] if m and (x == 0 or y == 0 or x == w - 1 or y == h - 1 or g[i - 1] == 0 or g[i + 1] == 0 or g[i - w] == 0 or g[i + w] == 0) then o[i] = m end end end for i, c in pairs(o) do g[i] = c end end local function pack(g, w, h) local t = {} for y = 0, h - 1 do t[y + 1] = char(unpack(g, y * w + 1, y * w + w)) end return concat(t) end local function half(g, w, h) local nw = w + 1 local o = newgrid(nw, h) for y = 0, h - 1 do for x = 0, nw - 1 do local c = x < w and g[y * w + x + 1] or 0 local p = x > 0 and g[y * w + x] or 0 o[y * nw + x + 1] = c == 0 and p or (p == 0 and c or mix(p, c)) end end return o, nw end local DW, DH = 30, 26 local DEER_EDGE = { [1] = 3, [2] = 1, [4] = 5 } local SQ_EDGE = { [12] = 17, [13] = 12, [14] = 12, [15] = 17, [16] = 15, [4] = 5 } local UPPER = {} local function upper(o) local g = UPPER[o] if g then return g end g = newgrid(DW, DH) fell(g, DW, DH, 11.5, o + 11, 9.5, 5.2, 1) fell(g, DW, DH, 10.5, o + 9.4, 7.5, 3, 2, 1) fell(g, DW, DH, 12, o + 14.6, 7.5, 2.4, 4, 1) fell(g, DW, DH, 1.8, o + 8.5, 1.9, 1.6, 4) fell(g, DW, DH, 19.5, o + 8, 3.4, 5, 1) fell(g, DW, DH, 23, o + 5.5, 4.6, 4.2, 1) fell(g, DW, DH, 22, o + 4, 3, 2.2, 2, 1) fell(g, DW, DH, 26.4, o + 7.6, 2.6, 2.1, 4) fell(g, DW, DH, 24.6, o - 0.3, 1.5, 2.4, 1) fell(g, DW, DH, 19.6, o + 0.8, 1.7, 2.6, 1) edge(g, DW, DH, DEER_EDGE) fell(g, DW, DH, 19.6, o + 1.2, 0.8, 1.6, 6) for _, p in ipairs({ { 7, 8 }, { 11, 7 }, { 15, 9 }, { 9, 11 }, { 13, 11 } }) do g[(o + p[2]) * DW + p[1] + 1] = 8 end for y = o + 4, o + 6 do g[y * DW + 25] = 7; g[y * DW + 26] = 7 end g[(o + 4) * DW + 25] = 8 g[(o + 8) * DW + 23] = 6; g[(o + 8) * DW + 24] = 6 g[(o + 7) * DW + 29] = 11; g[(o + 8) * DW + 29] = 11 UPPER[o] = g return g end local function deer_grid(pose) local th = pose * TAU / 4 local o = 2 + (pose % 2 == 1 and -1 or 0) local u = upper(o) local g = {} for i = 1, DW * DH do g[i] = u[i] end local function leg(hx, a, c1, c2, under) local top = o + 14 local fx, fy = hx + 2.5 * sin(a), 24 - 2 * max(0, cos(a)) local n = floor(fy) - top for y = top, floor(fy) do local x = floor(hx + (fx - hx) * (y - top) / max(1, n) + 0.5) local hoof = y >= floor(fy) - 1 local i = y * DW + x + 1 if not under or g[i] == 0 then g[i] = hoof and 10 or c1 end if not under or g[i + 1] == 0 then g[i + 1] = hoof and 10 or c2 end end end leg(4, th + 3.14, 9, 3, true); leg(14, th + 3.14, 9, 3, true) leg(7, th, 1, 3); leg(17, th + 3.14, 1, 3) return g, DW, DH end local SW, SH = 12, 15 local function sq_grid(pose) local g = newgrid(SW, SH) if pose == 0 then fell(g, SW, SH, 2.6, 8, 2.3, 5.5, 14) else fell(g, SW, SH, 2.3, 9.5, 2.2, 4.4, 14) fell(g, SW, SH, 1.6, 4, 1.7, 2.6, 14) end fell(g, SW, SH, 6.6, 10.5, 3.6, 4, 12) fell(g, SW, SH, 7.6, 11.5, 2, 2.8, 4) fell(g, SW, SH, 7.4, 6, 3.4, 3, 12) fell(g, SW, SH, 5.4, 3.3, 1, 1.5, 12); fell(g, SW, SH, 9.6, 3.3, 1, 1.5, 12) fell(g, SW, SH, 7.4, 3, 3.8, 1.8, 15) fell(g, SW, SH, 7, 2.3, 2.6, 0.9, 16) fell(g, SW, SH, 6.2, 13.8, 2.2, 1.2, 12) edge(g, SW, SH, SQ_EDGE) g[6 * SW + 9] = 7; g[7 * SW + 9] = 7 g[7 * SW + 12] = 11 g[8 * SW + 10] = 6 g[0 * SW + 8] = 15 return g, SW, SH end local SPR = {} local function spr(kind, pose, hf) local key = kind * 100 + pose * 2 + hf local s = SPR[key] if s then return s end local g, w, h = (kind == 1 and deer_grid or sq_grid)(pose) if hf == 1 then g, w = half(g, w, h) end s = { pack(g, w, h), w, h } SPR[key] = s return s end local BLOOM = { pink = { { 255, 200, 218 }, { 246, 150, 186 }, { 214, 100, 152 }, { 255, 238, 246 } }, white = { { 255, 250, 252 }, { 240, 216, 228 }, { 206, 172, 196 }, { 255, 255, 255 } }, lilac = { { 236, 218, 255 }, { 198, 166, 242 }, { 152, 118, 208 }, { 250, 242, 255 } }, } local LEAF = { { 156, 218, 112 }, { 98, 178, 78 }, { 60, 132, 62 }, { 232, 255, 200 } } local PETAL = { 255, 200, 220 } local GROUND, EAVE = 50, 40 local function blob(cx, cy, rx, ry, c) for y = floor(cy - ry), ceil(cy + ry) - 1 do local v = (y + 0.5 - cy) / ry if v > -1 and v < 1 then local hw = rx * sqrt(1 - v * v) local x0 = floor(cx - hw + 0.5) rect(x0, y, floor(cx + hw + 0.5) - x0, 1, c[1], c[2], c[3]) end end end local function tree(cx, cy, rx, ry, pal, tw, ground) rect(cx - tw // 2, cy + ry - 4, tw, ground - (cy + ry - 4), 92, 66, 64) blob(cx, cy, rx, ry, pal[3]) blob(cx - rx * 0.15, cy - ry * 0.12, rx * 0.82, ry * 0.82, pal[2]) blob(cx - rx * 0.3, cy - ry * 0.3, rx * 0.5, ry * 0.5, pal[1]) local sp = pal[4] for k = 1, 6 do local h = hash32(cx * 131 + k * 977 + cy) px(floor(cx + (umod(h, 100) - 50) * rx * 0.012), floor(cy + (umod(h >> 8, 100) - 50) * ry * 0.012), sp[1], sp[2], sp[3]) end end local function range(yb, hgt, per, off, lc, dc, sl, sd) for x = 0, W - 1 do local u = ((x + off) / per) % 1 local tri = 1 - abs(2 * u - 1) local y = floor(yb - hgt * tri ^ 0.85 + 0.5) local c = u < 0.5 and lc or dc rect(x, y, 1, 40 - y, c[1], c[2], c[3]) if tri > 0.74 then local s = u < 0.5 and sl or sd rect(x, y, 1, floor((tri - 0.7) * 11), s[1], s[2], s[3]) end end end local STAGES = { function() for y = 0, 33 do local t = y / 33 rect(0, y, W, 1, floor(108 + 106 * t), floor(184 + 54 * t), floor(244 + 8 * t)) end range(31, 21, 40, 8, { 188, 162, 216 }, { 162, 138, 196 }, { 255, 246, 252 }, { 230, 214, 240 }) range(38, 13, 26, 0, { 150, 196, 168 }, { 118, 168, 146 }, { 200, 230, 200 }, { 172, 206, 178 }) blend_rect(0, 30, W, 10, 255, 244, 250, 0.3) for y = EAVE, H - 1 do local t = (y - EAVE) / (H - 1 - EAVE) rect(0, y, W, 1, floor(132 - 46 * t), floor(204 - 52 * t), floor(96 - 32 * t)) end end, function() local B = BLOOM[cfg.bloom] or BLOOM.pink for i = 0, 3 do local h = hash32(i * 0x9E3779B1) tree(i * 9 + 3 + umod(h, 3), 29 + i % 2 * 3, 7 + umod(h >> 4, 3), 8, i == 2 and LEAF or B, 2, EAVE + 5) end end, function() local B = BLOOM[cfg.bloom] or BLOOM.pink for i = 4, 7 do local h = hash32(i * 0x9E3779B1) tree(i * 9 + 3 + umod(h, 3), 29 + i % 2 * 3, 7 + umod(h >> 4, 3), 8, i == 5 and LEAF or B, 2, EAVE + 5) end blend_rect(0, 20, W, 24, 236, 232, 250, 0.22) end, function() local B = BLOOM[cfg.bloom] or BLOOM.pink tree(8, 25, 12, 13, B, 3, GROUND) tree(35, 22, 13, 13, LEAF, 3, GROUND) tree(59, 26, 11, 12, B, 3, GROUND) for k = 1, 20 do local h = hash32(k * 0x85EBCA6B) px(umod(h, W), GROUND + 1 + umod(h >> 8, 8), PETAL[1], PETAL[2], PETAL[3]) end end, } local SNAP, STAGE = nil, 1 local FG = nil local function build_fg() local g = newgrid(W, 8) for x = 0, W - 1 do local h = hash32(x * 0x9E3779B1) local len = 2 + umod(h, 4) for k = 0, len - 1 do g[(7 - k) * W + x + 1] = k >= len - 1 and 20 or (k >= 2 and 19 or 18) end if umod(h >> 8, 7) == 0 and len < 6 then local c = 21 + umod(h >> 12, 3) g[(7 - len) * W + x + 1] = c if len < 5 then g[(6 - len) * W + x + 1] = c end end end FG = pack(g, W, 8) end function on_settings() STAGE, SNAP = 1, nil PETAL = (BLOOM[cfg.bloom] or BLOOM.pink)[1] end local function frect(x, y, w, r, g, b, a) local x0 = floor(x) local f = x - x0 blend_rect(x0 + 1, y, w - 1, 1, r, g, b, a) blend(x0, y, r, g, b, a * (1 - f)) blend(x0 + w, y, r, g, b, a * f) end local function splat(x, y, r, g, b, a) local x0, y0 = floor(x), floor(y) local fx, fy = x - x0, y - y0 blend(x0, y0, r, g, b, a * (1 - fx) * (1 - fy)) blend(x0 + 1, y0, r, g, b, a * fx * (1 - fy)) blend(x0, y0 + 1, r, g, b, a * (1 - fx) * fy) blend(x0 + 1, y0 + 1, r, g, b, a * fx * fy) end local CLOUDS = { { 5, 20, 0.05, 6 }, { 11, 14, -0.035, 40 } } local function draw_clouds(t) for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[2] local x = (c[4] + t * c[3]) % (W + L) - L frect(x + 3, c[1], L - 6, 255, 252, 255, 0.85) frect(x, c[1] + 1, L, 255, 252, 255, 0.85) frect(x + 2, c[1] + 2, L - 4, 232, 238, 252, 0.8) end end local PETALS = {} for i = 1, 24 do local h = hash32(i * 0xC2B2AE35) PETALS[i] = { x = umod(h, 90) - 12, y = umod(h >> 8, 80), v = (5 + umod(h >> 12, 5)) / 30, w = 0.03 + umod(h >> 16, 4) / 100, ph = umod(h >> 18, 628) / 100, big = umod(h >> 22, 3) == 0 } end local PRELIST, PRE = { { 0, 2 }, { 0, 1 } }, 1 for pose = 0, 3 do for hf = 0, 1 do PRELIST[#PRELIST + 1] = { 1, pose, hf } if pose < 2 then PRELIST[#PRELIST + 1] = { 2, pose, hf } end end end local SPEEDS = { slow = 0.3, steady = 0.46, brisk = 0.66 } function render() if STAGE <= #STAGES then if SNAP then blit_rgb(SNAP, 0, 0, W, H) end STAGES[STAGE]() SNAP = snapshot(0, 0, W, H) STAGE = STAGE + 1 return end if not FG then blit_rgb(SNAP, 0, 0, W, H); build_fg() return end blit_rgb(SNAP, 0, 0, W, H) draw_clouds(frame) local v = SPEEDS[cfg.speed] or 0.46 local run = floor((W + 2 * DW) / v) local f = frame % (run + 45) local x = -DW - 2 + v * f if PRE <= #PRELIST then local k = PRELIST[PRE] if k[1] == 0 then upper(k[2]) else spr(k[1], k[2], k[3]) end PRE = PRE + 1 elseif f < run and x > -DW + 1 then local ix = floor(x) local hf = x - ix >= 0.5 and 1 or 0 local pose = floor(x / 3) % 4 local d = spr(1, pose, hf) blend_rect(ix + 3, 57, 24, 1, 40, 90, 40, 0.25) blit(d[1], PAL, ix, 33, d[2], d[3], d[2], d[3], true) local sw = floor(0.75 * sin(x * TAU / 12 - 1.0) * 2 + 0.5) / 2 local sx = x + 4 + sw local six = floor(sx) local s = spr(2, floor(x / 6) % 2, sx - six >= 0.5 and 1 or 0) blit(s[1], PAL, six, 33 + 9 - 14 + (pose % 2 == 0 and -1 or 0), s[2], s[3], s[2], s[3], true) end blit(FG, PAL, 0, 56, W, 8, W, 8, true) for i = 1, cfg.petals or 12 do local p = PETALS[i] local y = (p.y + p.v * frame) % (H + 12) - 6 local x = p.x + 4 * sin(frame * p.w + p.ph) + 0.12 * y splat(x, y, PETAL[1], PETAL[2], PETAL[3], 0.95) if p.big then splat(x + 1, y, PETAL[1], PETAL[2], PETAL[3], 0.55) end end end