info = { type = "image", title = "Chick song", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A little green chick sings, notes float up", api = 33, } local tr = ({ sk = { ["Chick colour"] = "Farba kuriatok" }, cs = { ["Chick colour"] = "Barva kuřat" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tempo", label = T("Tempo"), type = "int", min = 60, max = 150, unit = " BPM", default = 96 }, { key = "chick", label = T("Chick colour"), type = "color", default = "#A8D86A" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local byte, char, concat, unpack = string.byte, string.char, table.concat, table.unpack local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159 local WALL, STRIPE = { 236, 188, 194 }, { 228, 176, 186 } local DISC, RING = { 252, 230, 216 }, { 246, 212, 206 } local STAGE, EDGE = { 190, 132, 92 }, { 214, 160, 114 } local PLANK, FRONT = { 168, 112, 76 }, { 150, 98, 66 } local SHADOW = { 170, 116, 82 } local FOOT = { 248, 150, 40 } local BEAK, BEAK_D = { 250, 172, 40 }, { 222, 128, 30 } local MOUTH = { 176, 58, 64 } local EYE = { 44, 40, 36 } local CHEEK = { 250, 140, 150 } local NOTES = { { 226, 70, 112 }, { 76, 120, 232 }, { 236, 140, 20 }, { 140, 76, 210 } } local DCX, DCY, DR = 24, 35, 23 local X0, Y0 = 13, 28 local function c3(c) return c[1], c[2], c[3] end local CW, CH = 22, 24 local CELLS do local t = {} for r = 0, CH - 1 do local y = r + 0.5 local ry = y < 13 and 13 or 11 local d = (y - 13) / ry local hw = d * d < 1 and 10.6 * sqrt(1 - d * d) or 0 for c = 0, CW - 1 do local x = c + 0.5 - CW / 2 local v = 0 if x * x <= hw * hw then local u = x / hw local bx, by = x / 6.8, (y - 17.5) / 6.2 if bx * bx + by * by <= 1 then v = u > 0.42 and 5 or 4 elseif u > 0.62 then v = 2 elseif r >= 3 and r <= 7 and u > -0.72 and u < -0.38 then v = 3 else v = 1 end end t[#t + 1] = v end end CELLS = char(unpack(t)) end local PAL = {} local KR, KG, KB = 255, 0, 255 local NP = 4 local RAS, built = {}, false local function shift_x(f) local out, row = {}, {} local g = 1 - f local br, bg, bb = c3(DISC) for y = 0, CH - 1 do local base, k = y * CW, 0 for x = 0, CW do local a = (x >= 1 and f > 0) and byte(CELLS, base + x) or 0 local b = (x < CW and g > 0) and byte(CELLS, base + x + 1) or 0 local r1, g1, b1 = KR, KG, KB if a ~= 0 or b ~= 0 then local ar, ag, ab, cr, cg, cb = br, bg, bb, br, bg, bb if a ~= 0 then ar, ag, ab = PAL[3 * a - 2], PAL[3 * a - 1], PAL[3 * a] end if b ~= 0 then cr, cg, cb = PAL[3 * b - 2], PAL[3 * b - 1], PAL[3 * b] end r1 = floor(ar * f + cr * g + 0.5) g1 = floor(ag * f + cg * g + 0.5) b1 = floor(ab * f + cb * g + 0.5) end row[k + 1], row[k + 2], row[k + 3] = r1, g1, b1 k = k + 3 end out[y + 1] = char(unpack(row, 1, k)) end return concat(out) end local function vspan(x, w, yt, yb, r, g, b, a) local it, ib = floor(yt), floor(yb) if it == ib then blend_rect(x, it, w, 1, r, g, b, (yb - yt) * a) return end if ib - it > 1 then blend_rect(x, it + 1, w, ib - it - 1, r, g, b, a) end local ct, cb = (1 - (yt - it)) * a, (yb - ib) * a if ct > 0.02 then blend_rect(x, it, w, 1, r, g, b, ct) end if cb > 0.02 then blend_rect(x, ib, w, 1, r, g, b, cb) end end local BG = nil local function build_bg() rect(0, 0, W, 54, c3(WALL)) for x = 2, W - 1, 6 do rect(x, 0, 2, 54, c3(STRIPE)) end for y = DCY - DR - 2, 53 do local dy = y + 0.5 - DCY local r2 = (DR + 1.5) * (DR + 1.5) - dy * dy if r2 > 0 then local hw = sqrt(r2) rect(floor(DCX - hw + 0.5), y, floor(2 * hw + 0.5), 1, c3(RING)) end r2 = DR * DR - dy * dy if r2 > 0 then local hw = sqrt(r2) rect(floor(DCX - hw + 0.5), y, floor(2 * hw + 0.5), 1, c3(DISC)) end end rect(0, 54, W, H - 54, c3(STAGE)) rect(0, 54, W, 1, c3(EDGE)) for x = 5, W - 1, 11 do rect(x, 55, 1, 5, c3(PLANK)) end rect(0, 60, W, H - 60, c3(FRONT)) rect(16, 55, 17, 1, c3(SHADOW)); rect(19, 56, 11, 1, c3(SHADOW)) rect(20, 52, 3, 2, c3(FOOT)); rect(26, 52, 3, 2, c3(FOOT)) BG = gfx.snapshot(0, 0, W, H) end local BPM = 96 local GS, GL = { 136, 186, 80 }, { 200, 236, 160 } function on_settings() BPM = cfg.tempo or 96 local c = cfg.chick or { r = 168, g = 216, b = 106 } GS = { floor(c.r * 0.78), floor(c.g * 0.84), floor(c.b * 0.76) } GL = { floor(c.r + (255 - c.r) * 0.45), floor(c.g + (255 - c.g) * 0.45), floor(c.b + (255 - c.b) * 0.45) } PAL = { c.r, c.g, c.b, GS[1], GS[2], GS[3], GL[1], GL[2], GL[3], 252, 248, 228, 232, 224, 194 } RAS, built = {}, false end local LIFE, RISE = 3.2, 13.5 local function draw_note(kind, x, y, c, a) local r, g, b = c[1], c[2], c[3] if kind == 1 then vspan(x, 2, y + 4, y + 6, r, g, b, a) vspan(x + 1, 1, y, y + 4, r, g, b, a) vspan(x + 2, 1, y + 1, y + 3, r, g, b, a) else vspan(x, 2, y + 4, y + 6, r, g, b, a) vspan(x + 4, 2, y + 4, y + 6, r, g, b, a) vspan(x + 1, 1, y + 1, y + 4, r, g, b, a) vspan(x + 5, 1, y + 1, y + 4, r, g, b, a) vspan(x + 1, 5, y, y + 1, r, g, b, a) end end local function draw_notes(beats) local fpb = 60 / BPM local B = floor(beats) for b = B - floor(LIFE / fpb) - 1, B do local pb = b % 8 if b >= 0 and pb <= 6 then local age = (beats - b) * fpb if age >= 0 and age < LIFE then local h = hash32(b * 0x9E3779B1 + 5) local x = 39 + umod(h, 13) local y = 33 - age * RISE local a = age < 0.2 and age / 0.2 or 1 if age > LIFE - 1.2 then a = (LIFE - age) / 1.2 end draw_note((pb == 3 or pb == 6) and 2 or 1, x, y, NOTES[umod(h >> 8, 4) + 1], a) end end end end function render() built = false if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local beats = frame * BPM / (60 * FPS) local B = floor(beats) local bp = beats - B local pb = B % 8 local sung = pb <= 6 local long = pb == 3 or pb == 6 local open = sung and (long or bp < 0.55) local happy = pb >= 2 and pb <= 6 local up = sung and bp < 0.35 local dip = bp < 0.15 and 1 or 0 local x = X0 + 1.6 * sin(PI * beats) local q = floor(x * NP + 0.5) local p = q % NP if not RAS[0] then RAS[0] = shift_x(0); built = true end if not RAS[p] and not built then RAS[p] = shift_x(p / NP); built = true end local R = RAS[p] if not R then R = RAS[0]; q = floor(x + 0.5) * NP end local by = Y0 + dip gfx.blit_rgb(R, q // NP, by, CW + 1, CH, KR, KG, KB) local bx = floor(q / NP + 0.5) local g1, g2, g3 = PAL[1], PAL[2], PAL[3] px(bx + 10, by - 1, g1, g2, g3); px(bx + 11, by - 1, g1, g2, g3) px(bx + 9, by - 2, g1, g2, g3); px(bx + 12, by - 2, g1, g2, g3) if up then rect(bx - 2, by + 10, 2, 4, c3(GS)); px(bx - 3, by + 9, c3(GS)) rect(bx + 22, by + 10, 2, 4, c3(GS)); px(bx + 24, by + 9, c3(GS)) else rect(bx - 1, by + 12, 2, 5, c3(GS)) rect(bx + 21, by + 12, 2, 5, c3(GS)) end if happy then px(bx + 6, by + 9, c3(EYE)); px(bx + 7, by + 8, c3(EYE)); px(bx + 8, by + 9, c3(EYE)) px(bx + 13, by + 9, c3(EYE)); px(bx + 14, by + 8, c3(EYE)); px(bx + 15, by + 9, c3(EYE)) else rect(bx + 6, by + 8, 2, 2, c3(EYE)); px(bx + 7, by + 8, 255, 255, 255) rect(bx + 14, by + 8, 2, 2, c3(EYE)); px(bx + 15, by + 8, 255, 255, 255) end blend_rect(bx + 3, by + 11, 2, 1, CHEEK[1], CHEEK[2], CHEEK[3], 0.7) blend_rect(bx + 17, by + 11, 2, 1, CHEEK[1], CHEEK[2], CHEEK[3], 0.7) if open then rect(bx + 9, by + 10, 4, 1, c3(BEAK)) rect(bx + 9, by + 11, 4, 2, c3(MOUTH)) rect(bx + 10, by + 13, 2, 1, c3(BEAK_D)) else rect(bx + 9, by + 11, 4, 1, c3(BEAK)) rect(bx + 10, by + 12, 2, 1, c3(BEAK_D)) end draw_notes(beats) end