info = { type = "image", title = "Monster walk", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A monster and a ghost stroll through a forest", api = 33, } local tr = ({ sk = { ["Walk speed"] = "Rýchlosť chôdze", ["Slow"] = "Pomaly", ["Normal"] = "Bežne", ["Brisk"] = "Svižne", ["Ghost"] = "Duch", ["Season"] = "Ročné obdobie", ["Summer"] = "Leto", ["Autumn"] = "Jeseň" }, cs = { ["Walk speed"] = "Rychlost chůze", ["Slow"] = "Pomalu", ["Normal"] = "Běžně", ["Brisk"] = "Svižně", ["Ghost"] = "Duch", ["Season"] = "Roční období", ["Summer"] = "Léto", ["Autumn"] = "Podzim" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Walk speed"), type = "int", default = 1, opts = { T("Slow"), T("Normal"), T("Brisk") } }, { key = "ghost", label = T("Ghost"), type = "bool", default = true }, { key = "season", label = T("Season"), type = "int", default = 0, opts = { T("Summer"), T("Autumn") } }, } local rect, blend, blend_rect, blit_rgb, blit_idx, blend_mask = gfx.rect, gfx.blend, gfx.blend_rect, gfx.blit_rgb, gfx.blit_indexed, gfx.blend_mask local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local W, H = gfx.W, gfx.H local TAU = 6.2832 local SEASONS = { { far = { 14, 76, 62 }, near = { 34, 140, 54 }, hi = { 74, 188, 70 }, edge = { 6, 44, 28 }, ftr = { 14, 58, 58 } }, { far = { 100, 50, 40 }, near = { 200, 96, 28 }, hi = { 244, 176, 52 }, edge = { 70, 28, 18 }, ftr = { 66, 36, 40 } }, } local SE = SEASONS[1] local NTR, NTR_L, NTR_D = { 96, 60, 34 }, { 150, 98, 54 }, { 30, 16, 10 } local TUFT = { 6, 40, 24 } local BG local function lerp(a, b, t) return a + (b - a) * t end local function build() for y = 0, 9 do local t = y / 9 rect(0, y, W, 1, floor(lerp(14, 26, t)), floor(lerp(18, 36, t)), floor(lerp(92, 130, t))) end for y = 10, 50 do local t = (y - 10) / 40 rect(0, y, W, 1, floor(lerp(6, 10, t)), floor(lerp(32, 56, t)), floor(lerp(34, 46, t))) end for y = 51, H - 1 do local t = (y - 51) / (H - 52) rect(0, y, W, 1, floor(lerp(18, 10, t)), floor(lerp(88, 56, t)), floor(lerp(46, 30, t))) end rect(0, 53, W, 7, 52, 112, 60) rect(0, 53, W, 1, 70, 134, 72) BG = gfx.snapshot(0, 0, W, H) end local function hs(x0, x1, y, h, c) if x1 <= x0 or x1 < -1 or x0 > W + 1 then return end local r, g, b = c[1], c[2], c[3] local i0, i1 = floor(x0), floor(x1) if i0 == i1 then blend_rect(i0, y, 1, h, r, g, b, x1 - x0); return end local s, e = i0 + 1, i1 local cl, cr = i0 + 1 - x0, x1 - i1 if cl > 0.94 then s = i0 elseif cl > 0.06 then blend_rect(i0, y, 1, h, r, g, b, cl) end if cr > 0.94 then e = i1 + 1 elseif cr > 0.06 then blend_rect(i1, y, 1, h, r, g, b, cr) end if e > s then rect(s, y, e - s, h, r, g, b) end end local CL = { { P = 128, h = 22, t0 = 2, ta = 1.2, b0 = 14, ba = 3, hi = false }, { P = 96, h = 30, t0 = 3, ta = 1.5, b0 = 22, ba = 3.5, hi = true }, } local CROWN, CPAL, NEXT = {}, {}, 1 local JOB local function build_crown() local j = JOB if not j.yt then local ly = CL[j.L] local P = ly.P local w = TAU / P local k = P // 32 local yt, ib, ik, hh = {}, {}, {}, {} for x = 1, P do local u = x - 0.5 + j.p / 4 local t = ly.t0 + ly.ta * sin(u * w * (k // 4 * 2 + 1) + 1) local b = ly.b0 + ly.ba * sin(u * w * k) + ly.ba * 0.6 * sin(u * w * (2 * k + 1) + 2) yt[x] = t ib[x] = floor(b) local q = floor((b - ib[x]) * 4 + 0.5) ik[x] = q >= 4 and 1 or (q == 0 and 3 or 3 + q) hh[x] = ly.hi and floor(t + 3 + 2.5 * (sin(u * w * 3 * k + 0.5) + 1)) or -1 end j.yt, j.ib, j.ik, j.hh, j.y = yt, ib, ik, hh, 0 return end local ly = CL[j.L] local P = ly.P local yt, ib, ik, hh, row = j.yt, j.ib, j.ik, j.hh, j.row for y = j.y, math.min(j.y + 11, ly.h - 1) do local Y = y + 0.5 for x = 1, P do local v = 0 if Y >= yt[x] then local e = ib[x] if y < e then v = y < hh[x] and 2 or 1 elseif y == e then v = ik[x] elseif y == e + 1 then v = 3 end end row[x] = v end j.rows[y + 1] = string.char(table.unpack(row, 1, P)) end j.y = j.y + 12 if j.y >= ly.h then CROWN[(j.L - 1) * 4 + j.p + 1] = table.concat(j.rows) JOB = nil NEXT = NEXT + 1 end end local function mix(a, b, t) return a + (b - a) * t end local function crown_pal(c, hi, e) local pal = { 0, 0, 0, c[1], c[2], c[3], hi[1], hi[2], hi[3], e[1], e[2], e[3] } for q = 1, 3 do for i = 1, 3 do pal[#pal + 1] = floor(mix(e[i], c[i], q / 4) + 0.5) end end return pal end local function draw_crown(L, c) local P = CL[L].P local off = floor(c) local s = CROWN[(L - 1) * 4 + floor((c - off) * 4) + 1] or CROWN[(L - 1) * 4 + 1] if not s then return end local h = CL[L].h blit_idx(s, CPAL[L], -off, 0, P, h, P, h, true) if off > P - W then blit_idx(s, CPAL[L], P - off, 0, P, h, P, h, true) end end local FAR_TR = { 12, 41, 74, 103 } local NEAR_TR = { 8, 38, 70 } local function far_trunks(cam) for k = 0, 1 do for i = 1, #FAR_TR do local x = FAR_TR[i] - cam + k * 128 if x > -6 and x < W + 2 then hs(x, x + 4, 14, 39, SE.ftr) if i == 2 then local open = frame % 210 > 7 if open then blend(floor(x + 0.6), 30, 255, 214, 60, 0.9); blend(floor(x + 2.6), 30, 255, 214, 60, 0.9) end end end end end end local function near_trunks(cam) for k = 0, 1 do for i = 1, #NEAR_TR do local x = NEAR_TR[i] - cam + k * 96 if x > -9 and x < W + 4 then hs(x - 1, x + 7, 18, 36, NTR_D) hs(x, x + 6, 18, 36, NTR) hs(x + 0.8, x + 2.4, 18, 36, NTR_L) hs(x - 3, x + 9, 50, 4, NTR_D) hs(x - 2, x + 8, 51, 3, NTR) end end end end local HEAD = { "..kkkkk..", ".kkkkkkk.", ".kgggggg.", ".ggggegeg", "bggggggg.", ".gggggmmg", "..GGGGG.." } local BODY = { ".jjjjjjj.", "JjjjjjjjJ", "JjjjjjjjJ", "gjjjjjjjg", ".ttttttt." } local LEGS = { { ".tt..ttt.", ".tt....tt", "kkk...kkk" }, { "..tttt...", "..tt.tt..", "..kk.kk.." }, { ".ttt..tt.", "tt....tt.", "kkk..kkk." }, { "..tttt...", "..tt.tt..", "..kk.kk.." }, } local IDX = { o = 1, k = 2, g = 3, G = 4, e = 5, m = 6, b = 7, j = 8, J = 9, t = 10 } local MPAL = { 0, 0, 0, 4, 12, 12, 22, 18, 26, 104, 208, 64, 62, 146, 52, 20, 20, 24, 150, 40, 44, 176, 176, 198, 152, 92, 44, 102, 60, 32, 94, 98, 132 } local MW, MH = 11, 17 local POSE = {} local function pose_of(p) if POSE[p] then return POSE[p] end local rows = {} for _, r in ipairs(HEAD) do rows[#rows + 1] = r end for _, r in ipairs(BODY) do rows[#rows + 1] = r end for _, r in ipairs(LEGS[p]) do rows[#rows + 1] = r end local g = {} for y = 1, MH do g[y] = {} for x = 1, MW do local r = rows[y - 1] local ch = r and x >= 2 and x <= 10 and r:sub(x - 1, x - 1) or "." g[y][x] = IDX[ch] or 0 end end local out = {} for y = 1, MH do for x = 1, MW do local v = g[y][x] if v == 0 and ((g[y - 1] and g[y - 1][x] or 0) > 1 or (g[y + 1] and g[y + 1][x] or 0) > 1 or (g[y][x - 1] or 0) > 1 or (g[y][x + 1] or 0) > 1) then v = 1 end out[#out + 1] = v end end POSE[p] = string.char(table.unpack(out)) return POSE[p] end local GW, GH = 12, 14 local function inside(u, v, m) if v < m then return false end local r = 5 - m if v <= 5 then return u * u + (v - 5) * (v - 5) <= r * r end if abs(u) > r - (v - 5) * 0.08 then return false end return v < 9.5 + 1.2 * cos(u * 1.2) - m + (u > 0 and 0.4 or 0) end local OUTL, BODYM, EYEM = {}, {}, {} local function bake(k) local qx, qy = k % 4 / 4, k // 4 / 4 local o, b, e, n = {}, {}, {}, 0 for y = 0, GH - 1 do for x = 0, GW - 1 do local co, ci, ce = 0, 0, 0 for sy = 0.25, 0.75, 0.5 do for sx = 0.25, 0.75, 0.5 do local u, v = x + sx - 5.5 - qx, y + sy - qy if inside(u, v, 0) then co = co + 1 if inside(u, v, 1) then ci = ci + 1 end end local a, d = (abs(u - 0.8) - 1.9), (v - 4.6) / 1.3 if a * a + d * d <= 0.7 then ce = ce + 1 end end end n = n + 1 o[n], b[n], e[n] = (co - ci) * 63, ci * 63, ce * 63 end end OUTL[k], BODYM[k], EYEM[k] = string.char(table.unpack(o)), string.char(table.unpack(b)), string.char(table.unpack(e)) end local LASTK local function draw_ghost() local x = 38 + 1.6 * sin(frame * 0.031) local y = 29 + 2.6 * sin(frame * 0.0806) local X, Y = x - 5.5, y local ix, iy = floor(X), floor(Y) local k = floor((Y - iy) * 4) * 4 + floor((X - ix) * 4) if not BODYM[k] and not JOB and NEXT > 8 then bake(k) end if not BODYM[k] then k = LASTK end if not k then return end LASTK = k local sw = floor(9 - (53 - y) * 0.08) blend_rect(floor(x) - sw // 2, 57, sw, 1, 0, 90, 70, 0.55) blend_rect(floor(x) - sw // 2 + 2, 58, sw - 4, 1, 0, 90, 70, 0.4) blend_mask(BODYM[k], 232, 252, 255, ix, iy, GW, GH, GW, GH, 0.85) blend_mask(OUTL[k], 70, 225, 255, ix, iy, GW, GH, GW, GH, 1) blend_mask(EYEM[k], 12, 20, 40, ix, iy, GW, GH, GW, GH, 1) end local STONES = { { 6, 55 }, { 22, 58 }, { 41, 56 }, { 55, 59 } } local TUFTS = { { 5, 4 }, { 17, 3 }, { 29, 5 }, { 40, 3 }, { 52, 4 }, { 60, 3 } } local SPEEDS = { 0.6, 1, 1.5 } function on_settings() SE = SEASONS[(cfg.season or 0) + 1] or SEASONS[1] CPAL[1] = crown_pal(SE.far, SE.far, SE.edge) CPAL[2] = crown_pal(SE.near, SE.hi, SE.edge) end function render() if not BG then build() end if not JOB and NEXT <= 8 then JOB = { L = (NEXT - 1) // 4 + 1, p = (NEXT - 1) % 4, rows = {}, row = {} } end if JOB then build_crown() end blit_rgb(BG, 0, 0, W, H) local m = SPEEDS[(cfg.speed or 1) + 1] or 1 local t = frame * m for i = 0, 5 do blend(5 + i * 11, 1 + (i * 5) % 3, 255, 250, 230, 0.35 + 0.35 * sin(frame * 0.06 + i * 2)) end local farc = (t * 0.1) % 128 far_trunks(farc) draw_crown(1, farc) near_trunks((t * 0.22) % 96) draw_crown(2, (t * 0.22) % 96) local gc = (t * 0.45) % 64 for k = 0, 1 do for i = 1, #STONES do local s = STONES[i] local x = s[1] - gc + k * 64 hs(x, x + 3, s[2], 1, { 100, 156, 96 }) hs(x + 0.5, x + 2.5, s[2] + 1, 1, { 34, 80, 46 }) end end local p = floor(t / 6) % 4 + 1 blit_idx(pose_of(p), MPAL, 13, 41, MW, MH, MW, MH, true) if cfg.ghost ~= false then draw_ghost() end local fc = (t * 0.8) % 64 for k = 0, 1 do for i = 1, #TUFTS do local f = TUFTS[i] local x = f[1] - fc + k * 64 hs(x, x + 1.6, 63 - f[2], f[2], TUFT) hs(x + 2.4, x + 3.6, 62 - f[2], f[2] + 1, TUFT) end end end