info = { type = "image", title = "Gem miner", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A lamp-lit miner digs a gem out of the rock", api = 44, } local tr = ({ sk = { ["Gem colour"] = "Farba drahokamu", ["Speed"] = "Rýchlosť" }, cs = { ["Gem colour"] = "Barva drahokamu", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "gem", label = T("Gem colour"), type = "color", default = "#46DC50" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, abs, min, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local sub = string.sub local W, H = gfx.W, gfx.H local B = { back = { 92, 72, 66 }, back2 = { 80, 62, 58 }, beam = { 150, 104, 66 }, beamL = { 188, 136, 88 }, post = { 132, 90, 58 }, rock1 = { 150, 122, 104 }, rock2 = { 136, 108, 94 }, rock3 = { 162, 134, 112 }, speck = { 112, 88, 76 }, ore = { 206, 170, 96 }, edge = { 86, 64, 56 }, floor = { 110, 88, 76 }, rail = { 170, 170, 184 }, sleeper = { 120, 82, 52 }, cav = { 72, 54, 48 }, cav2 = { 88, 66, 58 }, peb = { 150, 126, 108 }, } local function scaled(k) local p = {} for n, c in pairs(B) do p[n] = { floor(c[1] * k), floor(c[2] * k), floor(c[3] * k) + (k < 1 and 3 or 0) } end return p end local PALS = { B, scaled(0.55), scaled(0.28) } local function c3(c) return c[1], c[2], c[3] end local function scene(P) rect(0, 0, W, H, c3(P.floor)) rect(0, 6, 40, 44, c3(P.back)) for y = 13, 48, 7 do rect(0, y, 40, 1, c3(P.back2)) end rect(0, 0, 40, 6, c3(P.beam)) rect(0, 0, 40, 1, c3(P.beamL)) rect(0, 5, 40, 1, c3(P.post)) rect(4, 6, 4, 44, c3(P.post)) rect(4, 6, 1, 44, c3(P.beamL)) for i = 0, 6 do rect(8 + i, 12 - i, 1, 2, c3(P.post)) end rect(40, 0, 24, H, c3(P.rock1)) local y = 2 for i = 1, 12 do local h = 2 + umod(hash32(i * 0x9E3779B1), 4) rect(40, y, 24, h, c3(i % 2 == 0 and P.rock2 or P.rock3)) y = y + h + 2 + umod(hash32(i * 977), 4) end for i = 1, 46 do local h = hash32(i * 0x85EBCA6B) local x, yy = 42 + umod(h, 22), umod(h >> 8, H) if i % 4 == 0 then px(x, yy, c3(P.ore)) else rect(x, yy, 1 + (h >> 20 & 1), 1, c3(P.speck)) end end rect(40, 0, 1, 50, c3(P.edge)) rect(0, 50, 40, 1, c3(P.rail)) for x = 2, 38, 9 do rect(x, 51, 5, 2, c3(P.sleeper)) end rect(0, 53, 40, 1, c3(P.back2)) end local DARK, MIDR, LITR = nil, {}, {} local STAGE = 1 local function build() scene(PALS[4 - STAGE]) if STAGE == 1 then DARK = snapshot(0, 0, W, H) else local rows = STAGE == 2 and MIDR or LITR for y = 0, H - 1 do rows[y] = snapshot(0, y, W, 1) end end STAGE = STAGE + 1 end local function runs(rows, keys, flip) local out = {} for y, row in ipairs(rows) do if flip then row = row:reverse() end local x, last = 1, -1 while x <= #row do local c = row:sub(x, x) local e = x while row:sub(e + 1, e + 1) == c do e = e + 1 end local k = keys:find(c, 1, true) if k > 1 then out[#out + 1] = { x - 1, y - 1, e - x + 1, k - 1, last == x - 1 } end last, x = k > 1 and e or -1, e + 1 end end return out end local function draw(sp, pal, X, Y) local x0 = floor(X) local f = X - x0 if f > 0.88 then x0, f = x0 + 1, 0 end for i = #sp, 1, -1 do local s = sp[i] local r, g, b = c3(pal[s[4]]) local x, y, n = x0 + s[1], Y + s[2], s[3] if f < 0.12 then rect(x, y, n, 1, r, g, b) elseif s[5] then rect(x, y, n, 1, r, g, b) blend_rect(x + n, y, 1, 1, r, g, b, f) else blend_rect(x, y, 1, 1, r, g, b, 1 - f) if n > 1 then rect(x + 1, y, n - 1, 1, r, g, b) end blend_rect(x + n, y, 1, 1, r, g, b, f) end end end local BODY = { "...hhhhh...", "..hhhhhhhll", "..HHHHHHH..", "...sssse...", "...ssssss..", "..jjjjjjj..", "..jjjjjjj..", "..jjjjjjj..", "..ooooooo..", "..ooooooo..", "..ooooooo..", } local LEGS = { stand = { "...oo.oo...", "...oo.oo...", "...oo.oo...", "..kkk.kkk..", "..kkk.kkk.." }, stride = { "...oo.oo...", "..oo...oo..", ".oo.....oo.", ".kkk...kkk.", ".kkk...kkk." }, } local MKEYS = ".hlHsejok" local MPAL = { { 240, 190, 40 }, { 255, 248, 190 }, { 200, 146, 24 }, { 255, 214, 180 }, { 50, 40, 60 }, { 92, 128, 190 }, { 70, 98, 160 }, { 50, 40, 40 } } local GLOVE, WOOD, IRON = { 214, 168, 108 }, { 150, 100, 60 }, { 190, 196, 210 } local SETS = {} local function miner(leg, flip) local key = leg .. (flip and 1 or 0) if not SETS[key] then local rows = {} for _, r in ipairs(BODY) do rows[#rows + 1] = r end for _, r in ipairs(LEGS[leg]) do rows[#rows + 1] = r end SETS[key] = runs(rows, MKEYS, flip) end return SETS[key] end local GKEYS = ".dglw" local GEM = runs({ "..lllllll..", ".llwlgggdd.", "lllgggggddd", ".lgggggggd.", "..lgggggd..", "...lgggd...", "....ggd....", ".....g....." }, GKEYS) local MINI = runs({ ".lll.", "lwggd", ".ggd.", "..g.." }, GKEYS) local GPAL = { { 30, 150, 56 }, { 70, 220, 80 }, { 150, 255, 128 }, { 250, 255, 245 } } function on_settings() local g = cfg.gem or { r = 70, g = 220, b = 80 } GPAL[1] = { g.r * 45 // 100, g.g * 45 // 100, g.b * 45 // 100 } GPAL[2] = { g.r, g.g, g.b } GPAL[3] = { (g.r + 255 * 2) // 3 // 1, (g.g + 255 * 2) // 3, (g.b + 255 * 2) // 3 } end local LOOP, VX, FLOOR = 670, 60 / 140, 49 local SPD = { 0.5, 0.75, 1, 1.3, 1.7 } local DIG0, SWING = 140, 45 local START = 200 local IMX, IMY = 41, 35 local GX, GY = 44, 31 local function dot(x, y, c) local ix = floor(x) local f = x - ix if f < 0.12 then px(ix, y, c[1], c[2], c[3]) else blend(ix, y, c[1], c[2], c[3], 1 - f) blend(ix + 1, y, c[1], c[2], c[3], f) end end local function line(x0, y0, x1, y1, c) local n = max(abs(x1 - x0), abs(y1 - y0)) n = max(1, floor(n + 0.5)) for i = 0, n do local k = i / n dot(x0 + (x1 - x0) * k, floor(y0 + (y1 - y0) * k + 0.5), c) end end local function tool(hx, hy, th, dir) local a = th * 0.017453 local ux, uy = cos(a) * dir, -sin(a) line(hx - 2 * ux, hy - 2 * uy, hx + 7 * ux, hy + 7 * uy, WOOD) local tx, ty = hx + 7 * ux, hy + 7 * uy line(tx + 3 * uy, ty - 3 * ux, tx - 3 * uy, ty + 3 * ux, IRON) end local function ease(x) x = max(0, min(1, x)); return x * x * (3 - 2 * x) end local function swing_angle(q) if q < 20 then return 100 + 30 * ease(q / 20) end if q < 28 then return 130 - 81 * ease((q - 20) / 8) end return 49 + 51 * ease((q - 28) / 17) end local function bounds(lx, ly, r, y) local d = r * r - (y + 0.5 - ly) ^ 2 if d <= 0 then return nil end local h = sqrt(d) return floor(lx - h + 0.5), floor(lx + h + 0.5) end local function strips(rows, lx, ly, r) for y = max(0, ly - floor(r)), min(H - 1, ly + floor(r)) do local a, b = bounds(lx, ly, r, y) if a then a, b = max(0, a), min(W, b) if b > a then blit_rgb(sub(rows[y], a * 3 + 1, b * 3), a, y, b - a, 1) end end end end local function span(xa, xb, y, k, lx, ly, r1, r2) local function fill(a, b, lv) if b > a then local c = PALS[lv][k]; rect(a, y, b - a, 1, c[1], c[2], c[3]) end end fill(xa, xb, 3) local a, b = bounds(lx, ly, r2, y) if a then fill(max(xa, a), min(xb, b), 2) end a, b = bounds(lx, ly, r1, y) if a then fill(max(xa, a), min(xb, b), 1) end end local function cavity(stage, lx, ly, r1, r2) if stage < 1 then return end local k = min(stage, 5) / 5 local cx, rx, ry = 41 + 8 * k, 1.5 + 7 * k, 2 + 5.5 * k for y = floor(35 - ry), floor(35 + ry) do local dy = (y + 0.5 - 35) / ry if abs(dy) < 1 then local hw = rx * sqrt(1 - dy * dy) local j = hash32(y * 31 + 7) & 1 local xa, xb = max(40, floor(cx - hw + 0.5) + j), floor(cx + hw + 0.5) span(xa, xb, y, y < 35 and "cav" or "cav2", lx, ly, r1, r2) end end end local SP = {} for i = 1, 4 do local h = hash32(i * 0xC2B2AE35) SP[i] = { x = GX + 1 + umod(h, 9), y = GY + 1 + umod(h >> 6, 7), w = 0.2 + umod(h >> 12, 6) * 0.04, ph = umod(h >> 16, 628) / 100 } end local function sparkle(x, y, a) blend(x, y, 255, 255, 245, a) blend(x - 1, y, 255, 255, 245, a * 0.6); blend(x + 1, y, 255, 255, 245, a * 0.6) blend(x, y - 1, 255, 255, 245, a * 0.6); blend(x, y + 1, 255, 255, 245, a * 0.6) end function render() if STAGE < 4 then build() gfx.ready(false) return end local Tm = frame * SPD[cfg.speed or 3] + START local t = Tm % LOOP local X, dir, leg, hy = -34, 1, "stand", 0 local mode = "walk" if t < DIG0 then X = -34 + VX * t; leg = floor(t / 8) % 2 == 0 and "stride" or "stand" elseif t < 365 then X, mode = 26, "dig" elseif t < 420 then X, mode = 26, "rest" elseif t < 480 then X, mode = 26, "cheer"; hy = -floor(4 * abs(sin((t - 420) * 0.105)) + 0.5) elseif t < 518 then X, mode = 26, "rest" else X, dir = 26 - VX * (t - 518), -1; leg = floor(t / 8) % 2 == 0 and "stride" or "stand" end if mode ~= "walk" then leg = "stand" end local function mx(c) return dir > 0 and X + c or X + 11 - c end local lx, ly = floor(mx(10)), 35 + hy local r1, r2 = 15 + 0.7 * sin(Tm * 0.31), 23 + 0.7 * sin(Tm * 0.23 + 1) blit_rgb(DARK, 0, 0, W, H) strips(MIDR, lx, ly, r2) strips(LITR, lx, ly, r1) local stage = 0 for k = 0, 4 do if t >= DIG0 + SWING * k + 28 then stage = k + 1 end end cavity(stage, lx, ly, r1, r2) local gem_out = t >= 480 if stage >= 5 and t < 480 then draw(GEM, GPAL, GX, GY) end for k = 0, 4 do local age = t - (DIG0 + SWING * k + 28) if age >= 0 and age < 70 then if age < 6 then local g = floor(age) blend_rect(IMX - 1, IMY - 1 - g // 2, 2 + g // 2, 3 + g, 176, 150, 124, 0.35 * (1 - age / 6)) if age < 3 then px(IMX - 1, IMY - 2, 255, 240, 170); px(IMX - 2, IMY, 255, 240, 170); px(IMX, IMY + 2, 255, 240, 170) end end for j = 1, 4 do local h = hash32(k * 17 + j) local a = min(age, 26) local pxx = IMX - 1 + (-(0.3 + umod(h, 10) * 0.1)) * a local pyy = min(FLOOR, IMY + (-(0.8 + umod(h >> 4, 12) * 0.1)) * a + 0.06 * a * a) local lv = (pxx - lx) ^ 2 + (pyy - ly) ^ 2 local c = PALS[lv < r1 * r1 and 1 or (lv < r2 * r2 and 2 or 3)].peb rect(floor(pxx), floor(pyy), j == 1 and 2 or 1, j == 1 and 2 or 1, c[1], c[2], c[3]) end end end draw(miner(leg, dir < 0), MPAL, X, 34 + hy) local hx, hyy = mx(9), 42 + hy if mode == "dig" then local q = (t - DIG0) % SWING local k = floor((t - DIG0) / SWING) line(mx(7), 40, hx, hyy, GLOVE) tool(hx, hyy, swing_angle(k >= 5 and 100 or q), dir) elseif mode == "cheer" then line(mx(7), 40 + hy, mx(10), 33 + hy, GLOVE) line(mx(3), 40 + hy, mx(1), 33 + hy, GLOVE) tool(mx(10), 33 + hy, 75, dir) elseif mode == "rest" and not gem_out then line(mx(7), 40, hx, hyy, GLOVE) tool(hx, hyy, -60, dir) else line(mx(7), 40 + hy, hx, hyy, GLOVE) tool(hx, hyy, -75, dir) end if gem_out then local k = (t - 480) / 22 if t < 502 then local gx = GX + 3 + (mx(10) - GX - 3) * ease(k) local gy = GY + 2 + (30 - GY - 2) * ease(k) - 6 * sin(min(1, k) * 3.14159) draw(MINI, GPAL, floor(gx), floor(gy)) else draw(MINI, GPAL, dir > 0 and X + 8 or X - 2, 41 + hy) end end if stage >= 5 and t >= 348 and t < 480 then for i = 1, 4 do local s = SP[i] local v = sin(Tm * s.w + s.ph) if v > 0.55 then sparkle(s.x, s.y, min(1, (v - 0.55) * 3)) end end end if t >= 502 and t < 640 and sin(Tm * 0.2) > 0.6 then sparkle(floor(mx(10)) + (dir > 0 and 1 or -1), 42 + hy, 0.9) end end