info = { type = "image", title = "Knight on guard", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A tiny knight breathes and twirls his sword", api = 33, } local tr = ({ sk = { ["Armour colour"] = "Farba brnenia", ["Cape colour"] = "Farba plášťa", ["Look around and sword twirl"] = "Obzeranie a točenie mečom" }, cs = { ["Armour colour"] = "Barva brnění", ["Cape colour"] = "Barva pláště", ["Look around and sword twirl"] = "Rozhlížení a točení mečem" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "armor", label = T("Armour colour"), type = "color", default = "#7C8EBE" }, { key = "cape", label = T("Cape colour"), type = "color", default = "#D6403C" }, { key = "tricks", label = T("Look around and sword twirl"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI, TAU = 3.14159, 6.28318 local function mix(a, b, t) return { floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) } end local function c3(c) return c[1], c[2], c[3] end local WHITE = { 255, 255, 255 } local WALL, WALL_M, WALL_L = { 104, 100, 150 }, { 78, 74, 122 }, { 126, 122, 170 } local FLOOR, FLOOR_L, FLOOR_M = { 140, 132, 168 }, { 164, 156, 192 }, { 106, 100, 142 } local IRON = { 70, 64, 84 } local GOLD, GOLD_D = { 255, 206, 72 }, { 204, 146, 40 } local BELT, LEATHER, VISOR = { 150, 98, 58 }, { 118, 82, 68 }, { 34, 28, 56 } local BLADE, BLADE_D = { 240, 246, 255 }, { 150, 164, 204 } local EYE = { 255, 230, 110 } local HEAD = { "...hhhhhh...", "..hhhhhhHH..", ".hhhhhhhHHd.", ".hHgggggHHd.", ".hHvvvvvvHd.", ".hHvvvvvvHd.", ".hhHHHHHHdd.", "..hHHHHHHd..", "..hHHHHHHd..", "...dddddd...", } local BODY = { ".ghhhhhhhhHHHHg.", "hhhhhhhhhHHHHHdd", "hHHhhhhggHHHdHdd", "hHHhhhhggHHHdHdd", "hHdhhhhhhHHHdHdd", "hHdHhhhhhHHHdHdd", ".HdbbbbbbbbbbdH.", "..dHhhhhHHHHd...", "..dddddddddddd..", } local LEGS = { ".hhHH..hHHd.", ".hhHH..hHHd.", ".hHHd..hHHd.", ".hHHd..hHHd.", ".hHHd..hHHd.", "lllll..lllll", "lllll..lllll", "ddddd..ddddd", } local MAP = { ["."] = 0, h = 1, H = 2, d = 3, g = 4, v = 5, b = 6, l = 7 } local function grid(rows) local out = {} for i = 1, #rows do local r = rows[i] local t = {} for k = 1, #r do t[k] = string.char(MAP[r:sub(k, k)]) end out[i] = table.concat(t) end return table.concat(out), #rows[1], #rows end local G_HEAD, HW, HH = grid(HEAD) local G_BODY, BW, BH = grid(BODY) local G_LEGS, LW, LH = grid(LEGS) local PAL, CAPE, CAPE_L local BG = nil function on_settings() local a = cfg.armor or { r = 124, g = 142, b = 190 } local m = { a.r, a.g, a.b } local l, d = mix(m, WHITE, 0.45), mix(m, { 30, 30, 80 }, 0.45) PAL = { 0, 0, 0, l[1], l[2], l[3], m[1], m[2], m[3], d[1], d[2], d[3], GOLD[1], GOLD[2], GOLD[3], VISOR[1], VISOR[2], VISOR[3], BELT[1], BELT[2], BELT[3], LEATHER[1], LEATHER[2], LEATHER[3] } local c = cfg.cape or { r = 214, g = 64, b = 60 } CAPE = { c.r, c.g, c.b } CAPE_L = mix(CAPE, WHITE, 0.3) end local function build_bg() rect(0, 0, W, 54, c3(WALL)) for y = 0, 53, 4 do rect(0, y + 3, W, 1, c3(WALL_M)) local off = ((y // 4) % 2) * 5 for x = off, W, 10 do rect(x, y, 1, 3, c3(WALL_M)) end rect(0, y, W, 1, c3(WALL_L)) end for y = 3, 17 do local half = (y < 7) and (3 + (y - 3)) or 6 local c = mix({ 250, 170, 140 }, { 90, 90, 196 }, (y - 3) / 14) rect(32 - half, y, 2 * half, 1, c3(c)) end rect(25, 3, 1, 15, c3(IRON)); rect(38, 7, 1, 11, c3(IRON)); rect(25, 17, 14, 1, c3(IRON)) rect(30, 3, 4, 1, c3(IRON)) rect(34, 7, 3, 3, 255, 248, 214); px(35, 7, 255, 255, 255) for _, tx in ipairs({ 9, 55 }) do rect(tx - 1, 26, 3, 5, c3(IRON)); rect(tx - 2, 25, 5, 1, c3(IRON)); rect(tx, 31, 1, 6, c3(IRON)) end rect(0, 54, W, 10, c3(FLOOR)) rect(0, 54, W, 1, c3(FLOOR_L)) rect(0, 58, W, 1, c3(FLOOR_M)); rect(0, 62, W, 1, c3(FLOOR_M)) for x = 6, W, 14 do rect(x, 55, 1, 3, c3(FLOOR_M)) end for x = 13, W, 14 do rect(x, 59, 1, 3, c3(FLOOR_M)) end rect(21, 53, 22, 2, 86, 82, 126) BG = gfx.snapshot(0, 0, W, H) end local function torch(tx, t, ph) local fl = sin(t * 0.37 + ph) > 0.1 and 1 or 0 local a = 0.08 + 0.02 * sin(t * 0.21 + ph) for k = -8, 8 do local w = floor(sqrt(72 - k * k) + 0.5) blend_rect(tx - w, 21 + k, 2 * w + 1, 1, 255, 190, 110, a) end for k = -5, 5 do local w = floor(sqrt(30 - k * k) + 0.5) blend_rect(tx - w, 21 + k, 2 * w + 1, 1, 255, 200, 120, a + 0.05) end rect(tx - 1, 22 - fl, 3, 3 + fl, 255, 140, 40) rect(tx, 21 - fl, 1, 2 + fl, 255, 140, 40) rect(tx, 23 - fl, 1, 2 + fl, 255, 230, 120) end local function span(xl, y, w, c, a) local xi = floor(xl) local fx = xl - xi rect(xi + 1, y, w - 1, 1, c3(c)) blend_rect(xi, y, 1, 1, c[1], c[2], c[3], 1 - fx) if fx > 0.02 then blend_rect(xi + w, y, 1, 1, c[1], c[2], c[3], fx) end end local function cape(t) local gust = 0.6 + 0.4 * sin(t * 0.045) for i = 0, 14 do local k = i / 14 local hw = 9 + 3.5 * k local sway = k * (1.2 + 1.6 * gust) + 1.1 * k * sin(t * 0.11 - i * 0.5) local w = floor(2 * hw + 0.5) local c = (i >= 13) and CAPE_L or CAPE span(32 + sway - hw, 38 + i, w, c) end end local function blade_line(hx, hy, ang, len, col, a) local ca, sa = cos(ang), sin(ang) for k = 1, len do local x, y = floor(hx + ca * k + 0.5), floor(hy + sa * k + 0.5) if a >= 1 then px(x, y, c3(col)) else blend_rect(x, y, 1, 1, col[1], col[2], col[3], a) end end end local function sword_twirl(hx, hy, ang, dir) for j = 3, 1, -1 do blade_line(hx, hy, ang - dir * j * 0.2, 17, BLADE_D, 0.18 + 0.1 * (4 - j)) end local ca, sa = cos(ang), sin(ang) blade_line(hx, hy, ang, 18, BLADE, 1) for k = 2, 16, 2 do px(floor(hx + ca * k + 0.5) + ((sa * sa > 0.5) and 1 or 0), floor(hy + sa * k + 0.5) + ((sa * sa > 0.5) and 0 or 1), c3(BLADE_D)) end local gx, gy = hx + ca * 3, hy + sa * 3 px(floor(gx - sa * 2 + 0.5), floor(gy + ca * 2 + 0.5), c3(GOLD)) px(floor(gx + 0.5), floor(gy + 0.5), c3(GOLD)) px(floor(gx + sa * 2 + 0.5), floor(gy - ca * 2 + 0.5), c3(GOLD)) px(floor(hx - ca * 2 + 0.5), floor(hy - sa * 2 + 0.5), c3(GOLD_D)) end local function sword_rest(hx, hy, glint) rect(hx, hy - 19, 1, 18, c3(BLADE)) rect(hx + 1, hy - 19, 1, 18, c3(BLADE_D)) px(hx, hy - 20, c3(BLADE)) rect(hx - 2, hy - 1, 5, 1, c3(GOLD)) rect(hx, hy, 1, 2, c3(BELT)); px(hx, hy + 2, c3(GOLD_D)) if glint then local gy = hy - 2 - floor(glint * 16 + 0.5) rect(hx - 1, gy, 3, 1, 255, 255, 255); px(hx, gy - 1, 255, 255, 255); px(hx, gy + 1, 255, 255, 255) if glint > 0.9 then px(hx - 2, gy, 220, 240, 255); px(hx + 2, gy, 220, 240, 255) px(hx, gy - 2, 220, 240, 255) end end end local CYC = 360 function render() if not BG then build_bg() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) torch(9, t, 0.0); torch(55, t, 2.3) cape(t) local lam = 0.5 - 0.5 * cos(t * TAU / (2.6 * STEP_HZ)) local tt = t % CYC local tricks = cfg.tricks ~= false local look = 0 if tricks and tt >= 150 and tt < 240 then local v = (tt - 150) / 90 look = -sin(TAU * v) look = look * look * look * 0.5 + look * 0.5 end local u = -1 if tricks and tt >= 36 and tt < 78 then u = (tt - 36) / 42 end gfx.blit_indexed(G_LEGS, PAL, 26, 46, LW, LH, LW, LH, true) gfx.blit_indexed(G_BODY, PAL, 24, 37, BW, BH, BW, BH, true) local ab = 0.55 * lam if ab > 0.03 then blend_rect(26, 36, 8, 1, PAL[4], PAL[5], PAL[6], ab) blend_rect(34, 36, 4, 1, PAL[7], PAL[8], PAL[9], ab) end local hx, hy = 39, 42 if u >= 0 then local e = u * u * (3 - 2 * u) local lift = 5 * sin(PI * u) local ang = -PI / 2 + TAU * e rect(hx - 1, floor(hy - lift) - 1, 3, 3, PAL[7], PAL[8], PAL[9]) sword_twirl(hx, floor(hy - lift + 0.5), ang, 1) else local glint = nil if tricks and tt >= 270 and tt < 320 then glint = (tt - 270) / 50 end sword_rest(hx, hy, glint) rect(hx - 1, hy - 1, 3, 3, PAL[7], PAL[8], PAL[9]) end local sh = floor(look * 1.2 + (look >= 0 and 0.5 or -0.5)) local hx0 = 26 + sh gfx.blit_indexed(G_HEAD, PAL, hx0, 28, HW, HH, HW, HH, true) local ah = 0.9 * lam if ah > 0.03 then blend_rect(hx0 + 3, 27, 6, 1, PAL[4], PAL[5], PAL[6], ah) end local blink = (t % 111) < 4 if not blink then local es = floor(look * 1.6 + (look >= 0 and 0.5 or -0.5)) px(hx0 + 4 + es, 33, c3(EYE)); px(hx0 + 7 + es, 33, c3(EYE)) end local px0 = hx0 + 5 for k = 0, 6 do local x = px0 + floor(k * 0.9 + 0.5) local y = 27 - floor(2.5 * sin(PI * k / 7) + 0.5) + floor(1.1 * (k / 6) * sin(t * 0.15 - k * 0.7) + 0.5) + floor(k / 4) rect(x, y, 2, 1, c3(CAPE)) if k < 3 then px(x + 1, y - 1, c3(CAPE_L)) end end end