info = { type = "image", title = "Knight patrol", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A knight swats a bat in a torchlit hall", api = 33, } local tr = ({ sk = { ["Hall"] = "Sála", ["Bat"] = "Netopier", ["Stone"] = "Kameň", ["Moss"] = "Machová", ["Ember"] = "Žeravá" }, cs = { ["Hall"] = "Síň", ["Bat"] = "Netopýr", ["Stone"] = "Kámen", ["Moss"] = "Mechová", ["Ember"] = "Žhavá" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "hall", label = T("Hall"), type = "int", default = "stone", opts = { keys = { "stone", "moss", "ember" }, T("Stone"), T("Moss"), T("Ember") } }, { key = "bat", label = T("Bat"), type = "bool", default = true }, } local rect, px, blend_rect, blit_idx = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_indexed local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local char, concat, rep = string.char, table.concat, string.rep local W, H, FPS = gfx.W, gfx.H, gfx.FPS local THEMES = { stone = { { 30, 30, 44 }, { 70, 72, 96 }, { 96, 98, 124 }, { 124, 126, 152 }, { 156, 158, 184 }, { 92, 76, 64 }, { 70, 58, 50 }, { 40, 36, 48 } }, moss = { { 24, 34, 30 }, { 58, 76, 66 }, { 80, 102, 86 }, { 106, 130, 110 }, { 138, 160, 138 }, { 84, 80, 62 }, { 64, 62, 50 }, { 34, 42, 34 } }, ember = { { 40, 20, 24 }, { 96, 50, 52 }, { 128, 70, 68 }, { 158, 92, 84 }, { 190, 120, 104 }, { 96, 70, 60 }, { 72, 50, 46 }, { 52, 28, 32 } }, } local PAL local function C(i) return PAL[i * 3 + 1], PAL[i * 3 + 2], PAL[i * 3 + 3] end local STEEL, STEEL_D, STEEL_L = { 168, 172, 192 }, { 96, 100, 124 }, { 226, 230, 244 } local RED, GOLD, BROWN, BLACK = { 206, 38, 42 }, { 252, 204, 44 }, { 124, 74, 38 }, { 8, 8, 16 } local function set(c, x, y, w, h) rect(x, y, w, h, c[1], c[2], c[3]) end local function band(bi) local tones = {} for n = 0, 8 do tones[n] = 3 + (hash32(bi * 31 + n * 7) & 3) // 3 end local rows = {} for r = 0, 4 do local segs = {} for n = 0, 8 do local t = tones[n] + (r == 0 and 1 or (r == 4 and -1 or 0)) segs[n + 1] = rep(char(t), 7) .. char(1) end local s = concat(segs) local off = (bi % 2) * 4 rows[r + 1] = s:sub(off + 1, off + 64) end rows[6] = rep(char(1), 64) return concat(rows) end local FLOOR_Y = 50 local function floor_rows() local rows = {} for y = 0, 13 do local g, r = y // 7, y % 7 local s if y >= 6 and y <= 12 then s = char(y == 6 and 10 or (y == 12 and 10 or 9)):rep(64) elseif r == 0 then s = rep(char(8), 64) else local a, b = char(g == 0 and 6 or 7), char(g == 0 and 7 or 6) s = (rep(a, 8) .. rep(b, 8)):rep(4) end rows[#rows + 1] = s end return concat(rows) end local ARCH = { { 3, 4 }, { 2, 6 }, { 1, 8 }, { 0, 10 } } local function arch_window(x, moon) for k = 0, 24 do local y = 8 + k local o, w = 0, 10 if k < 4 then o, w = ARCH[k + 1][1], ARCH[k + 1][2] end rect(x + o - 1, y, w + 2, 1, C(5)) if k == 0 then rect(x + 2, 8, 6, 1, C(5)) end end for k = 1, 24 do local y = 8 + k local o, w = 0, 10 if k <= 4 then o, w = ARCH[k][1], ARCH[k][2] end local t = k / 24 rect(x + o, y, w, 1, floor(14 + 14 * t), floor(20 + 20 * t), floor(64 + 36 * t)) end rect(x + 4, 13, 1, 20, C(5)) rect(x, 20, 10, 1, C(5)) rect(x - 1, 33, 12, 2, C(5)) rect(x - 1, 35, 12, 1, C(2)) if moon then rect(x + 5, 15, 4, 4, 236, 236, 204) rect(x + 6, 14, 2, 1, 236, 236, 204); rect(x + 6, 19, 2, 1, 236, 236, 204) px(x + 6, 16, 190, 190, 164) end for i = 1, 3 do px(x + 1 + umod(hash32(x * 13 + i), 8), 15 + umod(hash32(x * 7 + i * 3), 12), 200, 210, 255) end end local function banner(x, y, len) set(GOLD, x - 1, y - 1, 8, 1) set(BLACK, x - 1, y, 8, len - 1) set(RED, x, y, 6, len - 2) set(RED, x, y + len - 2, 2, 2) set(RED, x + 4, y + len - 2, 2, 2) set(GOLD, x + 2, y + 3, 2, 5) set(GOLD, x + 1, y + 5, 4, 1) end local function glow(x, y) for k = -6, 6 do local hw = floor(9 * sqrt(1 - (k / 6.6) ^ 2)) blend_rect(x + 1 - hw, y - 3 + k, 2 * hw, 1, 255, 150, 40, 0.07) local h2 = floor(5.5 * sqrt(1 - (k / 6.6) ^ 2)) if k > -4 and k < 4 then blend_rect(x + 1 - h2, y - 3 + k, 2 * h2, 1, 255, 170, 60, 0.09) end end end local TORCHES = { { 12, 24 }, { 50, 24 } } local BG local function build_bg() local th = THEMES[cfg.hall] or THEMES.stone PAL = { 0, 0, 0 } for i = 1, 8 do PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = th[i][1], th[i][2], th[i][3] end for _, c in ipairs({ { 150, 24, 32 }, { 230, 180, 40 } }) do PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = c[1], c[2], c[3] end for bi = 0, 7 do blit_idx(band(bi), PAL, 0, bi * 6, 64, 6, 64, 6) end rect(0, 48, W, 2, C(8)) blit_idx(floor_rows(), PAL, 0, FLOOR_Y, 64, 14, 64, 14) blend_rect(0, 0, W, 4, 0, 0, 20, 0.45) blend_rect(0, 4, W, 3, 0, 0, 20, 0.22) arch_window(17, true) arch_window(37, false) banner(30, 8, 20); banner(3, 9, 18); banner(56, 9, 18) for _, t in ipairs(TORCHES) do glow(t[1], t[2]) set(BLACK, t[1] - 1, t[2] + 1, 4, 8) set(BROWN, t[1], t[2] + 2, 2, 6) set({ 110, 114, 136 }, t[1] - 1, t[2], 4, 2) end BG = gfx.snapshot(0, 0, W, H) end function on_settings() BG = nil end local FEET = 60 local function leg(x, lift, bob) local top = 54 + bob set(BLACK, x - 1, top - 1, 5, 8 - lift - bob) set(STEEL_D, x, top, 3, 4) set(BROWN, x, top + 4, 3, 3 - lift - bob) px(x, top, STEEL[1], STEEL[2], STEEL[3]) end local function sword(hx, hy, deg) local a = deg * 3.14159 / 180 local ca, sa = cos(a), sin(a) local wide = (ca * ca > sa * sa) for i = -2, 11 do local x, y = floor(hx + ca * i + 0.5), floor(hy + sa * i + 0.5) if i < 0 then px(x, y, BROWN[1], BROWN[2], BROWN[3]) elseif i == 1 then set(GOLD, x - (wide and 0 or 1), y - (wide and 1 or 0), wide and 1 or 3, wide and 3 or 1) elseif i > 1 then px(x, y, STEEL_L[1], STEEL_L[2], STEEL_L[3]) if wide then px(x, y + 1, STEEL[1], STEEL[2], STEEL[3]) else px(x + 1, y, STEEL[1], STEEL[2], STEEL[3]) end end end end local function knight(cx, p, bob, hx, hy, deg) set(BLACK, cx - 10, 45 + bob, 6, 12) set(RED, cx - 9, 46 + bob, 4, 10) set(GOLD, cx - 8, 47 + bob, 1, 8) set(GOLD, cx - 9, 46 + bob, 4, 1) leg(cx - 3, p == 1 and 2 or 0, bob) leg(cx + 1, p == 3 and 2 or 0, bob) set(BLACK, cx - 5, 44 + bob, 11, 10) set(STEEL, cx - 4, 45 + bob, 9, 8) set(RED, cx - 2, 45 + bob, 5, 8) set(GOLD, cx, 46 + bob, 1, 5) set(GOLD, cx - 1, 47 + bob, 3, 1) set(BROWN, cx - 4, 51 + bob, 9, 1) set(STEEL_L, cx + 4, 45 + bob, 2, 2) set(STEEL_D, cx + 5, 47 + bob, 2, 4) set(BLACK, cx - 4, 37 + bob, 9, 8) set(STEEL, cx - 3, 38 + bob, 7, 6) set(STEEL_L, cx - 3, 38 + bob, 1, 4) set(BLACK, cx - 2, 41 + bob, 5, 1) set(RED, cx - 1, 35 + bob, 3, 3) set(RED, cx - 3 - (p % 2), 36 + bob, 2, 2) sword(hx, hy + bob, deg) set(STEEL_D, hx - 1, hy + bob - 1, 2, 3) end local function bat(x, y, wing) local c = { 74, 34, 112 } set(c, x - 1, y, 3, 2) px(x - 1, y - 1, 8, 8, 16); px(x + 1, y - 1, 8, 8, 16) px(x - 1, y, 255, 60, 60); px(x + 1, y, 255, 60, 60) if wing == 0 then set(c, x - 4, y - 2, 3, 1); set(c, x + 2, y - 2, 3, 1) px(x - 5, y - 1, c[1], c[2], c[3]); px(x + 5, y - 1, c[1], c[2], c[3]) else set(c, x - 4, y + 1, 3, 1); set(c, x + 2, y + 1, 3, 1) px(x - 5, y, c[1], c[2], c[3]); px(x + 5, y, c[1], c[2], c[3]) end end local CYCLE, STOP_X = 8 * FPS, 22 local BAT_ENTER, HIT = 20, 64 local function bat_at(t) if t < BAT_ENTER then return nil end if t < HIT then local u = (t - BAT_ENTER) / (HIT - BAT_ENTER) local e = u * u * (3 - 2 * u) return floor(70 - 34 * u + 0.5), floor(14 + 32 * e + 3 * sin(t * 0.45) * (1 - u) + 0.5), (t // 3) % 2 end local tau = t - HIT local x, y = floor(36 + 1.5 * tau + 0.5), floor(46 - 2 * tau + 0.02 * tau * tau + 0.5) if x > 70 then return nil end return x, y, (t // 2) % 2 end local function draw_flames(t) for i, tc in ipairs(TORCHES) do local f = (t // 3 + i) % 3 local x, y = tc[1], tc[2] local a = 0.1 + 0.03 * f blend_rect(x - 1, y - 5, 4, 7, 255, 190, 80, a) set({ 210, 40, 20 }, x - 1, y - 2, 4, 3) set({ 250, 124, 20 }, x, y - 4 - (f == 1 and 1 or 0), 2, 3) set({ 255, 230, 90 }, x, y - 2, 2, 2) if f == 2 then px(x + 1, y - 6, 255, 190, 60) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame % CYCLE draw_flames(t) local withbat = cfg.bat ~= false local cx, p, bob, hx, hy, deg = nil, 0, 0, 0, 0, -75 local swoosh = false if not withbat or t < 34 or t >= 78 then local u = t cx = -12 + t if withbat and t >= 78 then u = t - 78; cx = STOP_X + u end p = (u // 6) % 4 bob = (p % 2 == 1) and -1 or 0 hx, hy = cx + 7, 50 else cx = STOP_X hx, hy, deg = cx + 7, 50, -75 if t >= 52 and t < 61 then hx, hy, deg = cx + 6, 47, -130 elseif t >= 61 and t < 64 then hx, hy, deg, swoosh = cx + 8, 48, -35, true elseif t >= 64 and t < 68 then hx, hy, deg, swoosh = cx + 9, 50, 25, true elseif t >= 68 then hx, hy, deg = cx + 8, 52, 55 end end blend_rect(cx - 7, 61, 15, 1, 0, 0, 0, 0.4) if cx < W + 12 then knight(cx, p, bob, hx, hy, deg) end if withbat then local bx, by, bw = bat_at(t) if bx then bat(bx, by, bw) end if t >= HIT - 1 and t < HIT + 4 then local y = 46 set({ 255, 240, 120 }, 35, y, 3, 1); set({ 255, 240, 120 }, 36, y - 1, 1, 3) px(36, y, 255, 255, 255) end end if swoosh then local sx, sy = cx + 5, 47 for k = 0, 6 do local a = (-100 + k * 20) * 3.14159 / 180 px(floor(sx + 13 * cos(a) + 0.5), floor(sy + 13 * sin(a) + 0.5), 240, 244, 255) end end end