info = { type = "image", title = "Sax corner", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Jazzman plays sax under a street lamp at night", api = 44, } local tr = ({ sk = { ["Tempo"] = "Tempo", ["Notes"] = "Noty", ["Cat"] = "Mačka" }, cs = { ["Tempo"] = "Tempo", ["Notes"] = "Noty", ["Cat"] = "Kočka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tempo", label = T("Tempo"), type = "int", min = 1, max = 5, default = 3 }, { key = "notes", label = T("Notes"), type = "int", min = 0, max = 6, default = 3 }, { key = "cat", label = T("Cat"), type = "bool", default = true }, } local rect, px, blend_rect, blend_mask = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend_mask local floor, sin, sqrt, abs, max, min = math.floor, math.sin, math.sqrt, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function c3(c) return c[1], c[2], c[3] end local CH = {} for k = 0, 255 do CH[k] = string.char(k) end local SKY_T, SKY_B = { 8, 10, 38 }, { 24, 32, 78 } local FAR, BRICK, MORTAR = { 14, 18, 52 }, { 32, 36, 80 }, { 24, 26, 64 } local WALL, WALL_CAP, WALL_M = { 54, 54, 110 }, { 88, 90, 148 }, { 40, 40, 90 } local WALK, CURB, ROAD = { 34, 36, 72 }, { 66, 70, 124 }, { 12, 14, 34 } local WARM = { 255, 196, 96 } local STARS = { { 4, 3 }, { 13, 8 }, { 22, 4 }, { 8, 15 }, { 27, 12 }, { 18, 2 }, { 1, 11 }, { 24, 19 } } local function stage_street() for y = 0, 51 do local t = y / 51 rect(0, y, W, 1, floor(SKY_T[1] + (SKY_B[1] - SKY_T[1]) * t + 0.5), floor(SKY_T[2] + (SKY_B[2] - SKY_T[2]) * t + 0.5), floor(SKY_T[3] + (SKY_B[3] - SKY_T[3]) * t + 0.5)) end for i = 1, #STARS do px(STARS[i][1], STARS[i][2], 150, 160, 220) end for _, b in ipairs({ { 0, 7, 32 }, { 7, 6, 27 }, { 13, 8, 35 }, { 21, 7, 29 }, { 28, 4, 37 } }) do rect(b[1], b[3], b[2], 52 - b[3], c3(FAR)) end for _, w in ipairs({ { 2, 36 }, { 9, 31 }, { 10, 36 }, { 16, 39 }, { 23, 34 }, { 17, 44 } }) do px(w[1], w[2], 118, 90, 52) end rect(28, 0, 36, 46, c3(BRICK)) for r = 0, 11 do local y = r * 4 + 3 rect(28, y, 36, 1, c3(MORTAR)) for x = 30 + (r % 2) * 3, 62, 6 do px(x, y - 2, c3(MORTAR)); px(x, y - 1, c3(MORTAR)) end end rect(28, 0, 2, 46, 46, 50, 104) rect(31, 12, 5, 10, 44, 48, 100); rect(32, 13, 3, 8, 12, 14, 34) rect(31, 22, 5, 1, 70, 74, 130) rect(39, 8, 16, 19, 88, 60, 44) for y = 9, 25 do local t = (y - 9) / 16 rect(40, y, 14, 1, 255, floor(228 - 34 * t), floor(150 - 46 * t)) end rect(46, 9, 2, 17, 88, 60, 44); rect(40, 16, 14, 2, 88, 60, 44) rect(40, 9, 4, 17, 176, 62, 68) rect(41, 9, 1, 17, 150, 46, 54); rect(43, 9, 1, 17, 150, 46, 54) rect(38, 27, 18, 2, 100, 92, 128); rect(38, 27, 18, 1, 132, 124, 164) rect(38, 29, 18, 1, 22, 24, 56) end local function stage_ground() rect(6, 46, 58, 6, c3(WALL)) rect(6, 46, 58, 1, c3(WALL_CAP)) rect(6, 49, 58, 1, c3(WALL_M)) for x = 8, 62, 7 do px(x, 47, c3(WALL_M)); px(x, 48, c3(WALL_M)); px(x + 3, 50, c3(WALL_M)); px(x + 3, 51, c3(WALL_M)) end rect(0, 52, W, 2, c3(CURB)) rect(0, 54, W, 7, c3(WALK)) for x = 4, 63, 9 do rect(x, 55, 1, 3, 28, 30, 62); rect(x + 4, 58, 1, 3, 28, 30, 62) end rect(0, 57, W, 1, 28, 30, 62) rect(0, 61, W, 3, c3(ROAD)) rect(3, 17, 2, 38, 24, 26, 52); rect(4, 17, 1, 38, 44, 46, 84) rect(2, 52, 4, 3, 24, 26, 52) rect(3, 14, 12, 2, 24, 26, 52); rect(4, 14, 10, 1, 44, 46, 84) rect(11, 15, 7, 3, 20, 20, 44); rect(11, 15, 7, 1, 56, 56, 100) rect(12, 18, 5, 2, 255, 224, 140); rect(13, 18, 3, 1, 255, 246, 210) end local CONE, WINL, HALO local CONE_X, CONE_Y, CONE_W, CONE_H = -5, 19, 38, 36 local WIN_X, WIN_Y, WIN_W, WIN_H = 14, 29, 49, 32 local ROWS = {} local function build_cone() for y = 0, CONE_H - 1 do local hw = 2 + y * 0.42 local fall = 1 - y / (CONE_H + 2) local row = {} for x = 0, CONE_W - 1 do local e = 1 - abs(x + CONE_X + 0.5 - 14) / hw row[x + 1] = CH[e > 0 and floor(255 * fall * min(1, e * 2.2) + 0.5) or 0] end ROWS[y + 1] = table.concat(row) end CONE = table.concat(ROWS) end local function build_halo() local rows = {} for y = -7, 7 do for x = -7, 7 do local d = sqrt(x * x + y * y) rows[#rows + 1] = CH[d < 7 and floor(255 * (1 - d / 7) + 0.5) or 0] end end HALO = table.concat(rows) end local function build_window_light(y0, y1) for y = y0, y1 do local xc = 47 - y * 0.35 local hw = 7 + y * 0.45 local fall = 1 - y / (WIN_H + 4) local row = {} for x = 0, WIN_W - 1 do local e = (hw - abs(x + WIN_X + 0.5 - xc)) / 3 row[x + 1] = CH[e > 0 and floor(255 * fall * min(1, e) + 0.5) or 0] end ROWS[y + 1] = table.concat(row) end if y1 == WIN_H - 1 then WINL = table.concat(ROWS) end end local GW, GH, OX, OY = 22, 30, 4, 25 local COL = { h = { 28, 26, 56 }, r = { 96, 88, 138 }, g = { 200, 150, 64 }, s = { 104, 64, 44 }, S = { 66, 40, 30 }, L = { 168, 108, 66 }, c = { 46, 64, 132 }, C = { 90, 114, 196 }, D = { 30, 42, 96 }, w = { 214, 210, 230 }, t = { 172, 48, 64 }, p = { 30, 36, 90 }, P = { 56, 64, 134 }, b = { 14, 12, 26 }, B = { 78, 66, 98 }, y = { 236, 166, 36 }, Y = { 255, 230, 128 }, Z = { 150, 96, 22 }, o = { 60, 34, 14 }, } local POSES = { { lean = 0, hy = 0, m = { 12, 7 }, n = { 15, 10 }, e = { 15, 19 }, bell = { 19, 21 } }, { lean = -1, hy = -1, m = { 11, 6 }, n = { 15, 8 }, e = { 17, 16 }, bell = { 21, 12 } }, { lean = 1, hy = 1, m = { 13, 8 }, n = { 15, 11 }, e = { 14, 22 }, bell = { 18, 25 } }, } local RGB = { ["."] = string.char(255, 0, 255) } for k, c in pairs(COL) do RGB[k] = string.char(c[1], c[2], c[3]) end local SEQ = { 1, 2, 1, 3, 1, 2, 3, 2 } local RASTERS = {} local function man(pi, tap) local key = pi * 2 + tap if RASTERS[key] then return RASTERS[key] end local P = POSES[pi] local g = {} local function set(x, y, c) if x >= 0 and x < GW and y >= 0 and y < GH then g[y * GW + x + 1] = c end end local function box(x, y, w, h, c) for j = y, y + h - 1 do for i = x, x + w - 1 do set(i, j, c) end end end local function line(x0, y0, x1, y1, c) local n = max(abs(x1 - x0), abs(y1 - y0)) for i = 0, n do local x, y = floor(x0 + (x1 - x0) * i / max(n, 1) + 0.5), floor(y0 + (y1 - y0) * i / max(n, 1) + 0.5) set(x, y, c); set(x + 1, y, c) end end box(6, 19, 6, 4, "p"); box(6, 23, 3, 5 - tap, "p"); box(9, 23, 3, 5 - tap, "p") box(9, 23, 1, 5 - tap, "D"); box(11, 19, 1, 9 - tap, "P") box(5, 28, 4, 2, "b") box(9, 28 - tap, 4, 2, "b"); set(12, 28 - tap, "B") for y = 10, 20 do local sh = floor(P.lean * (20 - y) / 10 + 0.5) local x0 = y == 10 and 5 or 4 box(x0 + sh, y, 13 - x0, 1, "c") set(x0 + sh, y, "D"); set(12 + sh, y, "C") if y == 10 then box(x0 + sh, y, 8, 1, "C") end if y >= 11 and y <= 14 then set(7 + sh, y, "w"); set(8 + sh, y, "w"); set(9 + sh, y, "w") end if y >= 12 and y <= 16 then set(8 + sh, y, "t") end end local hx, hy = P.lean, P.hy box(6 + hx, 5 + hy, 6, 5, "s"); box(5 + hx, 6 + hy, 1, 2, "S"); set(11 + hx, 5 + hy, "L"); set(11 + hx, 6 + hy, "L") box(6 + hx, 10 + hy, 4, 1, "S") box(6 + hx, 1 + hy, 6, 3, "h"); set(11 + hx, 1 + hy, "r"); box(6 + hx, 1 + hy, 6, 1, "r") box(6 + hx, 3 + hy, 6, 1, "g") box(4 + hx, 4 + hy, 10, 1, "h"); box(10 + hx, 4 + hy, 4, 1, "r") local m, n, e, b = P.m, P.n, P.e, P.bell m = { m[1] + hx, m[2] + hy } line(m[1], m[2], n[1], n[2], "y") line(n[1], n[2], e[1], e[2], "y") line(n[1] + 1, n[2] + 1, e[1] + 1, e[2] - 1, "Y") line(e[1], e[2], b[1] - 1, b[2], "y") box(b[1] - 1, b[2] - 2, 4, 3, "y"); box(b[1], b[2] - 2, 2, 1, "o"); box(b[1] + 2, b[2] - 2, 1, 1, "Y") set(m[1], m[2], "Z") line(10 + P.lean, 13, 14, 13 + P.hy // 1, "c") line(9 + P.lean, 17, 15, 16 + P.hy, "D") set(14, 13 + P.hy, "s"); set(15, 13 + P.hy, "s"); set(15, 17 + P.hy, "s"); set(16, 17 + P.hy, "s") local out = {} for i = 1, GW * GH do out[i] = RGB[g[i] or "."] end RASTERS[key] = table.concat(out) return RASTERS[key] end local CAT_BODY = { "#.......#.#.", "#.......###.", "#.......###*", ".#########..", ".#########..", ".########..." } local CAT_LEGS = { { ".##....##...", ".#.#..#..#.." }, { "..##...##...", "..##...##..." }, { "..##....##..", ".#..#...#..#" }, { "..##...##...", "..##...##..." }, } local CW, CHH = 13, 8 local CMASKS = {} local function cat_mask(p, q, eye) local key = (p * 4 + q) * 2 + eye if CMASKS[key] then return CMASKS[key] end local rows = {} for i = 1, 6 do rows[i] = CAT_BODY[i] end rows[7], rows[8] = CAT_LEGS[p + 1][1], CAT_LEGS[p + 1][2] local out = {} for y = 1, CHH do local r = rows[y] for x = 0, CW - 1 do local function at(xx) local ch = r:sub(xx + 1, xx + 1) if xx < 0 then return 0 end if eye == 1 then return ch == "*" and 4 or 0 end return (ch == "#" or ch == "*") and 4 or 0 end out[#out + 1] = CH[floor(255 * ((4 - q) * at(x) + q * at(x - 1)) / 16 + 0.5)] end end CMASKS[key] = table.concat(out) return CMASKS[key] end local NOTE_SHAPES = { { w = 3, "..#", "..#", "..#", "..#", "###", "##." }, { w = 4, "..##", "..#.", "..#.", "..#.", "###.", "##.." }, { w = 7, "..#####", "..#...#", "..#...#", "..#...#", "###.###", "##..##." }, } local NMASKS = {} local function note_mask(si, qx, qy) local key = (si * 4 + qx) * 4 + qy if NMASKS[key] then return NMASKS[key] end local S = NOTE_SHAPES[si] local w, h = S.w + 1, #S + 1 local out = {} for y = 0, h - 1 do for x = 0, w - 1 do local function at(xx, yy) if xx < 0 or yy < 0 or xx >= S.w or yy >= #S then return 0 end return S[yy + 1]:sub(xx + 1, xx + 1) == "#" and 1 or 0 end local v = (4 - qx) * (4 - qy) * at(x, y) + qx * (4 - qy) * at(x - 1, y) + (4 - qx) * qy * at(x, y - 1) + qx * qy * at(x - 1, y - 1) out[#out + 1] = CH[floor(255 * v / 16 + 0.5)] end end NMASKS[key] = { table.concat(out), w, h } return NMASKS[key] end local BG, STAGE = nil, 0 local STAGES = { stage_street, stage_ground, build_cone, build_halo, function() ROWS = {}; build_window_light(0, 15) end, function() build_window_light(16, WIN_H - 1) end } for pi = 1, #POSES do for tap = 0, 1 do STAGES[#STAGES + 1] = function() man(pi, tap) end end end function render() if STAGE < #STAGES then if STAGE > 0 then gfx.blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE + 1]() if STAGE < 2 then BG = gfx.snapshot(0, 0, W, H) end STAGE = STAGE + 1 gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local beat = 18 - 2 * (cfg.tempo or 3) local pose = SEQ[(frame // beat) % #SEQ + 1] local tap = (frame // (beat // 2)) % 2 do local flick = 1 + 0.04 * sin(t * 7) + 0.03 * sin(t * 17.3) blend_mask(CONE, WARM[1], WARM[2], WARM[3], CONE_X, CONE_Y, CONE_W, CONE_H, CONE_W, CONE_H, 0.5 * flick) blend_mask(HALO, 255, 214, 130, 14 - 7, 19 - 7, 15, 15, 15, 15, 0.5 * flick) blend_mask(WINL, WARM[1], WARM[2], WARM[3], WIN_X, WIN_Y, WIN_W, WIN_H, WIN_W, WIN_H, 0.34 * (1 + 0.05 * sin(t * 2.3))) end if cfg.cat ~= false then local x = 9 + (4 * t + 11) % 100 if x < W + 2 then local ix = floor(x) local q = floor((x - ix) * 4 + 0.5) if q == 4 then ix, q = ix + 1, 0 end local p = floor(t * 4) % 4 blend_mask(cat_mask(p, q, 0), 4, 4, 16, ix, 46 - CHH, CW, CHH, CW, CHH, 1) blend_mask(cat_mask(p, q, 1), 255, 210, 80, ix, 46 - CHH, CW, CHH, CW, CHH, 1) end end gfx.blit_rgb(man(pose, tap), OX, OY, GW, GH, 255, 0, 255) local n = cfg.notes or 3 if n > 0 then local span = beat * 2 for k = 0, n + 2 do local born = (frame // span - k) * span local age = frame - born local life = 110 if age >= 0 and age < life then local pb = POSES[SEQ[(born // beat) % #SEQ + 1]].bell local h = hash32((born // span) * 0x9E3779B1) local si = umod(h, 3) + 1 local x = OX + pb[1] + 0.12 * age + 4 * sin(age * 0.06 + umod(h >> 8, 6)) local y = OY + pb[2] - 6 - age * 0.32 local ix, iy = floor(x), floor(y) local qx, qy = floor((x - ix) * 4 + 0.5), floor((y - iy) * 4 + 0.5) if qx == 4 then ix, qx = ix + 1, 0 end if qy == 4 then iy, qy = iy + 1, 0 end local m = note_mask(si, qx, qy) local a = min(1, age / 10) * min(1, (life - age) / 30) blend_mask(m[1], 255, 210, 100, ix, iy, m[2], m[3], m[2], m[3], a) end end end end