info = { type = "image", title = "Egg spring", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "An egg cracks open and a chick trots away", api = 44, } local tr = ({ sk = { ["Chick"] = "Kuriatko", ["Egg"] = "Vajce", ["Yellow"] = "Žlté", ["Orange"] = "Oranžové", ["Dark"] = "Tmavé", ["White"] = "Biele", ["Brown"] = "Hnedé", ["Blue"] = "Modré" }, cs = { ["Chick"] = "Kuřátko", ["Egg"] = "Vejce", ["Yellow"] = "Žluté", ["Orange"] = "Oranžové", ["Dark"] = "Tmavé", ["White"] = "Bílé", ["Brown"] = "Hnědé", ["Blue"] = "Modré" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "chick", label = T("Chick"), type = "int", default = 0, opts = { T("Yellow"), T("Orange"), T("Dark") } }, { key = "egg", label = T("Egg"), type = "int", default = 0, opts = { T("White"), T("Brown"), T("Blue") } }, } local rect, blend_rect, blit, snapshot, blit_rgb = gfx.rect, gfx.blend_rect, gfx.blit_indexed, gfx.snapshot, gfx.blit_rgb local floor, sin, abs, sqrt = math.floor, math.sin, math.abs, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local schar = string.char local CYC = 14.5 local T0 = 3.2 local EX, EY = 12, 40 local GRAV = 364 local M = {} local function grid(w, h) local c = { w = w, h = h } for i = 1, w * h do c[i] = false end return c end local function part(c, inside, ch, hi, sh, ol) local w, h = c.w, c.h for i = 1, w * h do M[i] = false end for y = 0, h - 1 do for x = 0, w - 1 do local s = inside(x + 0.5, y + 0.5) if s then M[y * w + x + 1] = s end end end for y = 0, h - 1 do for x = 0, w - 1 do local i = y * w + x + 1 local s = M[i] if s then local edge = ol and (x == 0 or y == 0 or x == w - 1 or y == h - 1 or not M[i - 1] or not M[i + 1] or not M[i - w] or not M[i + w]) c[i] = edge and ol or (hi and s < -0.4 and hi) or (sh and s > 0.4 and sh) or ch end end end end local function ell(cx, cy, rx, ry) return function(x, y) local dx, dy = (x - cx) / rx, (y - cy) / ry if dx * dx + dy * dy <= 1 then return dx * 0.6 + dy * 0.8 end end end local function dots(c, ch, ...) local a = { ... } for k = 1, #a, 2 do c[a[k + 1] * c.w + a[k] + 1] = ch end end local function seal(c, keys) local t = {} for i = 1, c.w * c.h do local ch = c[i] t[i] = schar(ch and keys:find(ch, 1, true) - 1 or 0) end return table.concat(t) end local function plist(keys, col) local l = {} for i = 1, #keys do local c = col[keys:sub(i, i)] l[i * 3 - 2], l[i * 3 - 1], l[i * 3] = c[1], c[2], c[3] end return l end local function put(s, w, h, pal, x, y) blit(s, pal, x, y, w, h, w, h, true) end local KEYS = "?wxspkcyzuqa" local CHICKS = { { y = { 255, 214, 52 }, z = { 255, 240, 150 }, u = { 232, 166, 28 }, q = { 188, 118, 18 } }, { y = { 250, 172, 92 }, z = { 255, 216, 152 }, u = { 214, 120, 52 }, q = { 158, 82, 30 } }, { y = { 98, 90, 104 }, z = { 150, 142, 158 }, u = { 62, 56, 68 }, q = { 34, 30, 40 } }, } local EGGS = { { w = { 244, 242, 234 }, x = { 255, 255, 250 }, s = { 204, 200, 194 }, p = { 150, 144, 138 }, c = { 255, 232, 190 } }, { w = { 214, 168, 122 }, x = { 238, 204, 160 }, s = { 172, 126, 86 }, p = { 112, 78, 52 }, c = { 250, 226, 182 } }, { w = { 176, 222, 224 }, x = { 224, 248, 246 }, s = { 128, 176, 184 }, p = { 82, 122, 134 }, c = { 240, 244, 220 } }, } local EYE, CRACK, BEAK = { 28, 22, 24 }, { 104, 88, 78 }, { 250, 128, 38 } local NKEYS = "?SHDO" local NEST = { S = { 190, 140, 70 }, H = { 226, 180, 100 }, D = { 134, 92, 44 }, O = { 88, 58, 30 } } local EGG, CAP, SHELL, BODY, WING, WINGUP, NESTF = {}, nil, nil, nil, nil, nil, nil local SPR_PAL local function zig(x) local m = x % 4; return m == 1 and -1 or m == 3 and 1 or 0 end local function build_egg() local e = grid(12, 15) part(e, ell(6, 7.7, 5.5, 7.2), "w", "x", "s", "p") local function cracks(c, list) dots(c, "k", table.unpack(list)) end local g1 = { 9, 3, 8, 4, 9, 5, 8, 6 } local g2 = { 7, 7, 6, 8, 7, 9, 4, 10, 3, 9 } local g3 = {} for x = 1, 10 do g3[#g3 + 1] = x; g3[#g3 + 1] = 7 + zig(x) end local function copy(c) local d = { w = c.w, h = c.h }; for i = 1, c.w * c.h do d[i] = c[i] end return d end EGG[0] = seal(e, KEYS) local c1 = copy(e); cracks(c1, g1); EGG[1] = seal(c1, KEYS) local c2 = copy(c1); cracks(c2, g2); EGG[2] = seal(c2, KEYS) local c3 = copy(c2); cracks(c3, g3); EGG[3] = seal(c3, KEYS) local cap, bot = grid(12, 15), grid(12, 15) for y = 0, 14 do for x = 0, 11 do local i = y * 12 + x + 1 local cut = 7 + zig(x) if e[i] then if y < cut then cap[i] = e[i] elseif y == cut then bot[i] = "c" else bot[i] = e[i] end end end end CAP, SHELL = seal(cap, KEYS), seal(bot, KEYS) end local function build_chick() local b = grid(15, 14) part(b, ell(6.6, 7, 6.1, 5.6), "y", "z", "u", "q") dots(b, "y", 5, 0, 6, 0, 4, 1) dots(b, "a", 13, 6, 14, 6, 13, 7) BODY = seal(b, KEYS) local wg = grid(15, 14) part(wg, ell(4.4, 8.4, 2.6, 2.0), "u", nil, nil, "q") WING = seal(wg, KEYS) local wu = grid(15, 14) part(wu, ell(3.2, 4.4, 2.2, 3.2), "u", nil, nil, "q") WINGUP = seal(wu, KEYS) local nf = grid(26, 9) part(nf, function(x, y) if y < 2.2 then return nil end local dx, dy = (x - 13) / 12.8, (y - 1.4) / 7.2 if dx * dx + dy * dy <= 1 then return dx * 0.5 + dy * 0.9 end end, "S", "H", "D", "O") for y = 0, 8 do for x = 0, 25 do local i = y * 26 + x + 1 if nf[i] == "S" and (x + y * 2) % 5 == 0 then nf[i] = "D" end end end NESTF = seal(nf, NKEYS) end local PAL_NEST local function set_pal() local ci, ei = CHICKS[(cfg.chick or 0) + 1], EGGS[(cfg.egg or 0) + 1] local col = { ["?"] = { 0, 0, 0 }, k = CRACK, a = BEAK } for k, v in pairs(ci) do col[k] = v end for k, v in pairs(ei) do col[k] = v end SPR_PAL = plist(KEYS, col) PAL_NEST = plist(NKEYS, { ["?"] = { 0, 0, 0 }, S = NEST.S, H = NEST.H, D = NEST.D, O = NEST.O }) end function on_settings() set_pal() end local BG = nil local FLOWERS = { { 5, 55, 1 }, { 38, 53, 2 }, { 46, 59, 3 }, { 57, 54, 1 }, { 26, 60, 2 }, { 12, 61, 3 }, { 60, 61, 2 }, { 33, 57, 1 } } local FCOL = { { 236, 70, 80 }, { 250, 240, 250 }, { 255, 196, 60 } } local BG1 = nil local function build_bg() if BG1 then blit_rgb(BG1, 0, 0, W, H) else for y = 0, 43 do local k = y / 43 rect(0, y, W, 1, floor(136 + 70 * k + 0.5), floor(198 + 34 * k + 0.5), floor(236 + 4 * k + 0.5)) end for y = -9, 9 do local r2 = 81 - y * y local hw = floor(sqrt(r2 > 0 and r2 or 0) + 0.5) if hw > 0 then rect(52 - hw, 10 + y, 2 * hw, 1, 255, 240, 176) end end for y = -6, 6 do local hw = floor(sqrt(36.5 - y * y) + 0.5) rect(52 - hw, 10 + y, 2 * hw, 1, 255, 226, 100) end for x = 0, W - 1 do local y1 = floor(39 + 3 * sin(x * 0.11 + 0.6) + 0.5) rect(x, y1, 1, H - y1, 150, 206, 122) local y2 = floor(45 + 2 * sin(x * 0.19 + 2) + 0.5) rect(x, y2, 1, H - y2, 112, 192, 86) end BG1 = snapshot(0, 0, W, H) return end rect(0, 50, W, 14, 92, 176, 62) rect(0, 59, W, 5, 74, 156, 52) for i = 1, 46 do local h = math.hash32(i * 0x9E3779B1) local x, y = (h & 0x7FFF) % W, 50 + ((h >> 8) & 0x7FFF) % 14 if (h >> 20) & 1 == 1 then rect(x, y, 1, 2, 130, 204, 92) else rect(x, y, 1, 2, 60, 134, 44) end end for i = 1, #FLOWERS do local f = FLOWERS[i] local c = FCOL[f[3]] rect(f[1] + 1, f[2] + 2, 1, 3, 54, 130, 44) rect(f[1], f[2], 3, 2, c[1], c[2], c[3]) rect(f[1] + 1, f[2], 1, 1, 255, 236, 140) end for y = 0, 6 do local hw = floor(12.4 * sqrt(1 - ((y - 3) / 4.6) ^ 2) + 0.5) rect(18 - hw, 49 + y, 2 * hw, 1, 112, 76, 40) end rect(7, 49, 22, 1, 150, 106, 54) BG = snapshot(0, 0, W, H) BG1 = nil end local function ease(a, b, t) t = t < 0 and 0 or t > 1 and 1 or t; return a + (b - a) * t * t * (3 - 2 * t) end local function egg_y(t) local T1 = 0.55 if t < T1 then return EY - 0.5 * GRAV * (T1 - t) ^ 2 end local u, v = t - T1, 70 if u < 2 * v / GRAV then return EY - (v * u - 0.5 * GRAV * u * u) end return EY end local SHAKES = { { 2.0, 2.5 }, { 3.4, 3.9 }, { 4.8, 5.5 } } local POP = 6.2 local function shake(t) for i = 1, #SHAKES do local s = SHAKES[i] if t >= s[1] and t < s[2] then return (frame // 2) % 2 == 0 and -1 or 1 end end return 0 end local function chick_top(t) if t < 6.9 then return ease(47, 43, (t - POP) / 0.7) end if t < 7.5 then return 43 end return ease(43, 41, (t - 7.5) / 0.8) end local function legs(x, y, ph) local fx1, fy1, fx2, fy2 = 4, 13, 9, 13 if ph == 1 then fx1, fx2, fy2 = 6, 7, 12 elseif ph == 2 then fx1, fy1, fx2 = 3, 12, 10 end rect(x + fx1, y + fy1, 2, 1, 250, 128, 38) rect(x + fx2, y + fy2, 2, 1, 250, 128, 38) end local function eyes(x, y, open) if open then rect(x + 10, y + 4, 1, 2, EYE[1], EYE[2], EYE[3]) else rect(x + 9, y + 5, 2, 1, EYE[1], EYE[2], EYE[3]) end end local function chick(x, y, wing, ph, open) put(BODY, 15, 14, SPR_PAL, x, y) put(wing, 15, 14, SPR_PAL, x, y) if ph then legs(x, y, ph) end eyes(x, y, open) end local function clouds(t) local x = (4 + t * 0.9) % 100 - 20 for k = 0, 1 do local cx = (x + k * 46) % 92 - 22 local ix = floor(cx) local f = cx - ix local y = 8 + k * 9 local rows = { { 3, 15, y }, { 1, 19, y + 1 }, { 0, 21, y + 2 } } for r = 1, 3 do local a = rows[r] rect(ix + a[1] + 1, a[3], a[2] - 1, 1, 252, 252, 255) blend_rect(ix + a[1], a[3], 1, 1, 252, 252, 255, 1 - f) blend_rect(ix + a[1] + a[2], a[3], 1, 1, 252, 252, 255, f) end end end local function butterfly(t) local x = 42 + 14 * sin(t * 0.55) local y = 31 + 5 * sin(t * 1.3) + 2 * sin(t * 2.9) local ix, iy = floor(x), floor(y) local up = (frame // 4) % 2 == 0 rect(ix, iy + 1, 1, 2, 70, 50, 80) if up then rect(ix - 2, iy, 2, 2, 250, 130, 170); rect(ix + 1, iy, 2, 2, 250, 130, 170) else rect(ix - 1, iy + 1, 1, 2, 250, 130, 170); rect(ix + 1, iy + 1, 1, 2, 250, 130, 170) end end function render() if not EGG[0] then build_egg(); set_pal(); gfx.clear(150, 200, 236); gfx.ready(false) return end if not BODY then build_chick(); gfx.clear(150, 200, 236); gfx.ready(false) return end if not BG then build_bg() end if not BG then gfx.ready(false) return end blit_rgb(BG, 0, 0, W, H) local t0 = T0 + frame / FPS local t = t0 % CYC clouds(t0) butterfly(t0) if t < POP then local stage = t >= 5.5 and 3 or t >= 3.9 and 2 or t >= 2.5 and 1 or 0 if t < 0.55 then put(SHELL, 12, 15, SPR_PAL, EX, EY) end put(EGG[stage], 12, 15, SPR_PAL, EX + shake(t), floor(egg_y(t) + 0.5)) elseif t < 13.4 then local top = chick_top(t) if t < 9.4 then local dx = (t > 8.6 and t < 9.3) and ((frame // 2) % 2 == 0 and -1 or 1) or 0 local blink = (t > 7.1 and t < 7.25) or (t > 8.45 and t < 8.6) chick(11 + dx, floor(top + 0.5), WING, nil, not blink) end put(SHELL, 12, 15, SPR_PAL, EX, EY) else put(SHELL, 12, 15, SPR_PAL, EX, EY) end put(NESTF, 26, 9, PAL_NEST, 5, 49) if t >= POP then local u = t - POP local cx, cy = EX + 60 * u, EY - 200 * u + 0.5 * GRAV * u * u if cx < W then put(CAP, 12, 15, SPR_PAL, floor(cx + 0.5), floor(cy + 0.5)) end if t >= 9.4 and t < 10.2 then local k = (t - 9.4) / 0.8 chick(11 + 17 * k, floor(ease(41, 44, k) - 36 * k * (1 - k) + 0.5), WINGUP, nil, true) elseif t >= 10.2 then local x = 28 + 16 * (t - 10.2) if x < W then local ph = (frame // 4) % 2 == 0 and 1 or 2 chick(floor(x), 44 - (ph == 1 and 0 or 1), WING, ph, true) end end end end