info = { type = "image", title = "Butterfly metamorphosis", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A caterpillar becomes a butterfly and flies off", api = 44, } local tr = ({ sk = { ["Butterfly"] = "Motýľ", ["Peacock"] = "Babôčka", ["Blue"] = "Modráčik", ["Swallowtail"] = "Lastovičiar" }, cs = { ["Butterfly"] = "Motýl", ["Peacock"] = "Babočka", ["Blue"] = "Modrásek", ["Swallowtail"] = "Otakárek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sp", label = T("Butterfly"), type = "int", default = 0, opts = { T("Peacock"), T("Blue"), T("Swallowtail") } }, } local rect, blend_rect, blend_mask, blit_rgb, blit_ix = gfx.rect, gfx.blend_rect, gfx.blend_mask, gfx.blit_rgb, gfx.blit_indexed local floor, ceil, sqrt, sin, cos, abs = math.floor, math.ceil, math.sqrt, math.sin, math.cos, math.abs local schar, unpack, concat = string.char, table.unpack, table.concat local W, H = gfx.W, gfx.H local CYCLE, OFF = 900, 665 local T_EAT, T_CRAWL, T_PUPA, T_CRACK, T_OPEN, T_REST, T_FLY, T_GONE = 40, 280, 380, 560, 575, 700, 770, 860 local KEY = { 1, 254, 1 } local SPECIES = { { main = { 214, 54, 40 }, dark = { 48, 28, 32 }, acc = { 250, 232, 160 }, acc2 = { 72, 104, 214 }, kind = 1 }, { main = { 92, 146, 244 }, dark = { 38, 44, 100 }, acc = { 244, 156, 52 }, acc2 = { 250, 250, 255 }, kind = 2 }, { main = { 250, 214, 72 }, dark = { 30, 26, 32 }, acc = { 84, 134, 232 }, acc2 = { 242, 134, 44 }, kind = 3 }, } local BG = nil local function ellipse(cx, cy, rx, ry, c) for y = floor(cy - ry), ceil(cy + ry) do local dy = (y + 0.5 - cy) / ry if dy * dy < 1 then local hw = rx * sqrt(1 - dy * dy) local x0, x1 = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, c[1], c[2], c[3]) end end end end local STAGE = 0 local STAGES = { function() for y = 0, H - 1 do local k = y / (H - 1) rect(0, y, W, 1, floor(124 + 56 * k), floor(186 + 36 * k), floor(238 + 8 * k)) end end, function() for x = 0, 51 do local h = floor(12 + 5 * sin(x * 0.25) + 3 * sin(x * 0.6 + 1)) rect(x, 0, 1, h, 38, 92, 48) end end, function() for k = 0, 13 do local cx = 2 + k * 3.8 local cy = 12 + 5 * sin(cx * 0.25) + 3 * sin(cx * 0.6 + 1) ellipse(cx, cy - 1, 4.6, 3, { 56, 122, 58 }) ellipse(cx - 1, cy - 3, 3, 1.6, { 86, 156, 78 }) end end, function() rect(52, 0, W - 52, H, 92, 64, 42) rect(53, 0, 1, H, 124, 92, 60); rect(55, 0, 1, H, 68, 46, 30); rect(60, 0, 1, H, 68, 46, 30); rect(58, 0, 1, H, 112, 80, 52) for k = 0, 5 do rect(53 + (k * 3) % 9, 6 + k * 10, 2, 3, 62, 42, 28) end for x = 18, 52 do local y = 9 + floor((52 - x) * 0.03) rect(x, y, 1, 3, 104, 74, 48); rect(x, y, 1, 1, 136, 102, 66); rect(x, y + 2, 1, 1, 74, 52, 34) end ellipse(26, 14, 4.5, 2, { 54, 122, 58 }); ellipse(46, 14, 4, 2, { 54, 122, 58 }); ellipse(30, 7, 4, 2, { 70, 140, 66 }) end, function() rect(0, 38, 53, 2, 104, 74, 48); rect(0, 38, 53, 1, 136, 102, 66) for k = 0, 11 do ellipse(2 + k * 5.5, 62 + (k % 3), 4, 3, k % 2 == 0 and { 62, 132, 66 } or { 88, 164, 82 }) end BG = gfx.snapshot(0, 0, W, H) end, } local built_now = false local function may_build() if built_now then return false end built_now = true return true end local function any(t) for _, v in pairs(t) do return v end end local LPAL = { 0, 0, 0, 94, 174, 76, 66, 134, 60, 124, 200, 100, 40, 96, 50, 54, 112, 56 } local BITE_X = { 11, 15.4, 19.8, 24.2, 28.6, 33 } local LX0, LY0, LW, LH = 7, 33, 32, 14 local LEAVES = {} local BITE_Y, BITE_R2 = {}, {} local function rim_y(x) local d = (x - 22) / 15 if d * d >= 1 then return 38 end return 40 - 5.2 * sqrt(1 - d * d) end for b = 1, 6 do BITE_Y[b] = rim_y(BITE_X[b]) + 0.8; BITE_R2[b] = (2 + 0.25 * (b - 1)) ^ 2 end local function leaf(nb) local s = LEAVES[nb] if s then return s end if not may_build() then return any(LEAVES) end local inside = {} for j = 0, LH - 1 do inside[j] = {} local y = LY0 + j + 0.5 local ey = (y - 40) / 5.2 for i = 0, LW - 1 do local x = LX0 + i + 0.5 local ex = (x - 22) / 15 local e = ex * ex + ey * ey <= 1 if e then for b = 1, nb do local dx, dy = x - BITE_X[b], y - BITE_Y[b] if dx * dx + dy * dy < BITE_R2[b] then e = false; break end end end inside[j][i] = e end end local rows = {} for j = 0, LH - 1 do local row = {} for i = 0, LW - 1 do local v = 0 if inside[j][i] then local y = LY0 + j + 0.5 v = y < 40 and 1 or 2 if j == 0 or not inside[j - 1][i] or not (inside[j + 1] and inside[j + 1][i]) or i == 0 or not inside[j][i - 1] or not inside[j][i + 1] then v = 4 end if v ~= 4 then if abs(y - 40.5) < 0.6 then v = 3 elseif ((LX0 + i) - 2 * floor(abs(y - 40.5))) % 5 == 0 then v = 5 end end end row[i + 1] = v end rows[j + 1] = schar(unpack(row)) end s = concat(rows) LEAVES[nb] = s return s end local DISC = {} local function disc_mask(qx, qy) local k = qy * 4 + qx local m = DISC[k] if m then return m end if not may_build() then return nil end local rows = {} for j = 0, 5 do local row = {} for i = 0, 5 do local d = sqrt((i + 0.5 - 3 - qx / 4) ^ 2 + (j + 0.5 - 3 - qy / 4) ^ 2) local c = 1.95 + 0.5 - d row[i + 1] = floor((c < 0 and 0 or (c > 1 and 1 or c)) * 255) end rows[j + 1] = schar(unpack(row)) end m = concat(rows) DISC[k] = m return m end local function seg(x, y, r, g, b) local ix, iy = floor(x), floor(y) local m = disc_mask(floor((x - ix) * 4), floor((y - iy) * 4)) if not m then for k = 0, 15 do m = m or DISC[k] end end if m then blend_mask(m, r, g, b, ix - 3, iy - 3, 6, 6, 6, 6, 1) end end local function smooth(u) u = u < 0 and 0 or (u > 1 and 1 or u); return u * u * (3 - 2 * u) end local function head(t) if t < T_EAT then return -10 + 19 * smooth(t / T_EAT), 0, false end if t < T_CRAWL then local k = t - T_EAT local j = k // 40 local u = k % 40 local px = j == 0 and 9 or BITE_X[j] if u < 20 then return px + (BITE_X[j + 1] - px) * smooth(u / 20), u >= 24 and j + 1 or j, true end return BITE_X[j + 1], u >= 24 and j + 1 or j, false end return 33 + 22 * smooth((t - T_CRAWL) / 60), 6, true end local function draw_caterpillar(t) local hx, nb, moving = head(t) local wave = t * 0.4 for i = 7, 0, -1 do local x = hx - 1.9 * i + (moving and 0.35 * sin(wave - i) or 0) local gy = (x >= 9 and x <= 37) and rim_y(x) or 38 local bump = moving and 1.1 * (0.5 + 0.5 * sin(wave * 1.5 - i * 0.9)) or 0 local y = gy - 1.9 - bump + ((not moving and i == 0) and 0.6 or 0) if i == 0 then seg(x, y, 78, 128, 44) blend_rect(floor(x + 0.6), floor(y - 0.5), 1, 1, 14, 18, 14, 1) else local c = i % 2 == 0 and { 150, 214, 74 } or { 118, 188, 62 } seg(x, y, c[1], c[2], c[3]) if i % 2 == 0 then blend_rect(floor(x), floor(y - 1.4), 1, 1, 246, 220, 80, 0.9) end end end end local PUPA = {} local PW = { [3] = 1.8, [4] = 3.4, [5] = 4.8, [6] = 5.2, [7] = 5.2, [8] = 4.8, [9] = 4.0, [10] = 3.0, [11] = 2.0, [12] = 1.2, [13] = 0.6 } local function pupa(p) local s = PUPA[p] if s then return s end if not may_build() then return any(PUPA) end local a = p * 0.055 local ca, sa = cos(a), sin(a) local rows = {} for j = 0, 14 do local row = {} for i = 0, 10 do local ox, oy = i + 0.5 - 5.5, j + 0.5 - 1.0 local u, v = ox * ca + oy * sa, -ox * sa + oy * ca local idx = 0 if v >= 0 and v < 3 then if abs(u) < 0.5 then idx = 6 end elseif v >= 3 and v < 14 then local f = floor(v) local w0, w1 = PW[f] or 0.4, PW[f + 1] or 0.2 local hw = (w0 + (w1 - w0) * (v - f)) / 2 if abs(u) <= hw then if abs(u) > hw - 0.8 then idx = 4 elseif u < -0.15 * hw then idx = 1 elseif u > 0.45 * hw then idx = 3 else idx = 2 end if (v > 6 and v < 7.2 and abs(u - 1.0) < 0.7) or (v > 8.6 and v < 9.6 and abs(u + 0.8) < 0.7) then idx = 5 end end end row[i + 1] = idx end rows[j + 1] = schar(unpack(row)) end s = concat(rows) PUPA[p] = s return s end local function lerp3(a, b, f) return a[1] + (b[1] - a[1]) * f, a[2] + (b[2] - a[2]) * f, a[3] + (b[3] - a[3]) * f end local function draw_pupa(t, S, husk) local pal = { 0, 0, 0 } if husk then for _, c in ipairs({ { 214, 228, 204 }, { 182, 202, 172 }, { 140, 160, 132 }, { 98, 118, 96 }, { 176, 190, 150 }, { 176, 190, 160 } }) do pal[#pal + 1], pal[#pal + 2], pal[#pal + 3] = c[1], c[2], c[3] end else local f = (t - T_PUPA) / (T_CRACK - T_PUPA) f = f * f local ripe = { lerp3(S.main, S.dark, 0.25) } local G = { { 172, 228, 122 }, { 122, 198, 92 }, { 72, 142, 66 }, { 40, 88, 48 }, { 240, 212, 84 }, { 190, 204, 150 } } local R = { ripe, { lerp3(S.main, S.dark, 0.5) }, { S.dark[1] * 1.2 + 8, S.dark[2] * 1.2 + 8, S.dark[3] * 1.2 + 8 }, { 34, 24, 30 }, S.acc, { 120, 120, 100 } } for i = 1, 6 do local r, g, b = lerp3(G[i], R[i], f) pal[#pal + 1], pal[#pal + 2], pal[#pal + 3] = floor(r), floor(g), floor(b) end end local A = 0.15 * (0.65 + 0.35 * sin(t / 130)) local p = floor(A * sin(t * 6.2832 / 110) / 0.055 + 0.5) p = p < -3 and -3 or (p > 3 and 3 or p) local ps = pupa(p) if ps then blit_ix(ps, pal, 31, 11, 11, 15, 11, 15, true) end if not husk and t >= T_CRACK - 12 then local a = (t - (T_CRACK - 12)) / 12 blend_rect(35 + floor(p * -0.5), 17, 3, 1, 255, 252, 224, a > 1 and 1 or a) if t > T_CRACK - 6 then blend_rect(36 + floor(p * -0.5), 18, 1, 2, 255, 252, 224, 0.9) end end if husk then rect(35, 21, 3, 3, 52, 46, 44) end end local POSES = { { 0.45, 0.3 }, { 0.6, 0.4 }, { 0.75, 0.5 }, { 0.9, 0.6 }, { 1, 0.7 }, { 1, 0.85 }, { 1, 1 }, { 1, 0.4 } } local BFLY = {} local function wing_px(S, ax, y) local fa, fy = (ax - 4.6) / 5.4, (y + 2.2) / 4.2 local fq = fa * fa + fy * fy local ha, hy = (ax - 3.6) / 3.9, (y - 3.4) / 3.7 local hq = ha * ha + hy * hy local q if ax > 0.7 and fq <= 1 then q = fq elseif ax > 0.7 and hq <= 1 then q = hq end local kind = S.kind if not q then if kind == 3 and ax > 3.1 and ax < 4.5 and y > 6.6 and y < 9.4 then return S.dark end return nil end local c = S.main if kind == 3 then if ((ax * 0.9 + (y + 2) * 0.35) % 2.2) < 0.55 or q > 0.66 then c = S.dark end if hq <= 1 then local da, db = ax - 3.4, y - 5.2 if da * da + db * db < 1 then c = S.acc end da, db = ax - 4.5, y - 4.4 if da * da + db * db < 0.5 then c = S.acc2 end end elseif kind == 1 then if q > 0.64 then c = S.dark end local da, db = ax - 6.2, y + 3.4 local e = da * da + db * db if e < 1.9 then c = S.acc2 end if e < 0.7 then c = S.acc end da, db = ax - 4.0, y - 4.2 if da * da + db * db < 1.3 then c = S.acc2 end else if q > 0.7 then c = S.dark end if q > 0.93 then c = S.acc2 end if hq <= 1 then local da, db = ax - 3.4, y - 5.4 local da2, db2 = ax - 2.4, y - 5.1 if da * da + db * db < 0.8 or da2 * da2 + db2 * db2 < 0.5 then c = S.acc end end end return c end local BODY = { 24, 18, 24 } local function butterfly(S, pi) local key = S.kind * 10 + pi local s = BFLY[key] if s then return s end if not may_build() then return any(BFLY) end local sc, k = POSES[pi][1], POSES[pi][2] local inv_x, inv_y = 1 / (sc * k), 1 / sc local rows = {} for j = 0, 21 do local y = j + 0.5 - 11 local half = {} for i = 0, 11 do local x = i + 0.5 local c = wing_px(S, x * inv_x, y * inv_y) if i == 0 and y > -4.6 and y < 6 then c = BODY end if i == 0 and y < -5 and y > -6.2 then c = BODY end if y < -6 and y > -8.5 and abs(x - (-5.8 - y) * 0.55) < 0.6 then c = BODY end half[i + 1] = c end local row, n = {}, 0 for i = 24, 1, -1 do local c = half[i > 12 and i - 12 or 13 - i] or KEY row[n + 1], row[n + 2], row[n + 3] = c[1], c[2], c[3] n = n + 3 end rows[j + 1] = schar(unpack(row, 1, n)) end s = concat(rows) BFLY[key] = s return s end local function draw_butterfly(S, pi, cx, cy) local bs = butterfly(S, pi) if bs then blit_rgb(bs, floor(cx + 0.5) - 12, floor(cy + 0.5) - 11, 24, 22, KEY[1], KEY[2], KEY[3]) end end function on_settings() BFLY = {} end function render() if not BG then STAGE = STAGE + 1; STAGES[STAGE](); if not BG then gfx.ready(false) return end end built_now = false local S = SPECIES[(cfg.sp or 0) + 1] or SPECIES[1] local t = (frame + OFF) % CYCLE blit_rgb(BG, 0, 0, W, H) local _, nb = head(t) local ls = leaf(nb) if ls then blit_ix(ls, LPAL, LX0, LY0, LW, LH, LW, LH, true) end if t < T_PUPA - 20 then gfx.clip(0, 0, 52, H) draw_caterpillar(t) gfx.clip() end if t >= T_PUPA and t < T_OPEN then draw_pupa(t, S, false) elseif t >= T_OPEN and t < T_GONE + 20 then draw_pupa(t, S, true) end if t >= T_OPEN and t < T_FLY + 95 then local pi, cx, cy if t < T_REST then pi, cx, cy = 1 + floor((t - T_OPEN) / 25), 38, 26 if pi > 5 then pi = 5 end elseif t < T_FLY then local m = (t - T_REST) % 50 pi = m < 12 and 5 or (m < 20 and 6 or (m < 34 and 7 or (m < 42 and 6 or 5))) cx, cy = 38, 26 else local p = (t - T_FLY) / 90 pi = ({ 7, 6, 8, 6 })[(t // 2) % 4 + 1] cx, cy = 38 + 36 * p ^ 1.2 + 2 * sin(p * 14), 26 - 38 * p ^ 1.1 + 3 * sin(p * 11) end draw_butterfly(S, pi, cx, cy) end end