info = { type = "image", title = "Smith at the forge", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Blacksmith hammers glowing iron, sparks fly", api = 44, } local tr = ({ sk = { ["Strike speed"] = "Rýchlosť úderov" }, cs = { ["Strike speed"] = "Rychlost úderů" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "spd", label = T("Strike speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, exp = math.floor, math.sin, math.sqrt, math.exp local hash32 = math.hash32 local W, H = gfx.W, gfx.H local NS = 8 local TS_BY_SPEED = { 40, 32, 26, 20, 15 } local Ts = 26 local function cl(v) v = floor(v + 0.5); return v < 0 and 0 or v > 255 and 255 or v end local function grad(st, t) if t < 0 then t = 0 elseif t > 1 then t = 1 end for i = 2, #st do local a, b = st[i - 1], st[i] if t <= b[1] then local k = (t - a[1]) / (b[1] - a[1]) return cl(a[2] + (b[2] - a[2]) * k), cl(a[3] + (b[3] - a[3]) * k), cl(a[4] + (b[4] - a[4]) * k) end end local l = st[#st] return l[2], l[3], l[4] end local function c3(c) return c[1], c[2], c[3] end local HEAT = { { 0, 56, 56, 62 }, { 0.12, 110, 28, 14 }, { 0.3, 226, 64, 18 }, { 0.5, 255, 130, 32 }, { 0.72, 255, 200, 90 }, { 1, 255, 244, 214 } } local SPARK = { { 0, 255, 240, 190 }, { 0.35, 255, 170, 50 }, { 0.7, 220, 60, 16 }, { 1, 90, 24, 10 } } local WALL, WALL2 = { 18, 12, 10 }, { 26, 17, 14 } local BRICK, BRICK2, FLUE = { 46, 31, 26 }, { 58, 40, 32 }, { 30, 22, 20 } local STONE, FLOOR, FLOOR2 = { 72, 58, 52 }, { 34, 25, 22 }, { 42, 31, 27 } local STEEL, STEEL_HI, STEEL_D = { 60, 62, 72 }, { 112, 116, 128 }, { 38, 40, 48 } local WOOD, WOOD2 = { 60, 40, 28 }, { 82, 56, 38 } local SKIN, SKIN_L = { 96, 58, 42 }, { 168, 92, 52 } local SHIRT, APRON, PANTS, BOOT = { 54, 40, 38 }, { 78, 52, 34 }, { 40, 34, 40 }, { 26, 20, 20 } local HAIR, RIM = { 24, 16, 14 }, { 150, 70, 30 } local SEG, ML = {}, 0 local OFF = 0 local function seg(kind, dur, c) SEG[#SEG + 1] = { kind, dur, ML, c }; ML = ML + dur end function on_settings() Ts = TS_BY_SPEED[cfg.spd or 3] or 26 SEG, ML = {}, 0 for c = 0, 2 do seg("F", 60, c); seg("out", 24, c); seg("hit", NS * Ts, c) if c == 2 then seg("toq", 22, c); seg("dunk", 44, c); seg("fromq", 34, c) else seg("back", 24, c) end end OFF = 24 + floor(2.5 * Ts) end local BG, GM, GB local stage = 0 local GX0, GY0, GCW, GCH = 0, 14, 32, 25 local function build_masks() local t = {} for j = 0, GCH - 1 do local row = {} for i = 0, GCW - 1 do local dx, dy = (i * 2 + 1 - 9) / 46, (j * 2 + 1 + GY0 - 42) / 24 local d = sqrt(dx * dx + dy * dy) row[i + 1] = d >= 1 and 0 or floor(255 * (1 - d)) end t[j + 1] = string.char(table.unpack(row, 1, GCW)) end GM = table.concat(t) t = {} for j = 0, 10 do local row = {} for i = 0, 16 do local d = sqrt(((i - 8) / 9) ^ 2 + ((j - 5) / 6) ^ 2) row[i + 1] = d >= 1 and 0 or floor(255 * (1 - d)) end t[j + 1] = string.char(table.unpack(row, 1, 17)) end GB = table.concat(t) end local function build_scene() rect(0, 0, W, H, c3(WALL)) for y = 0, 56, 4 do rect(0, y, W, 1, c3(WALL2)) for x = ((y // 4) % 2) * 6, W - 1, 12 do rect(x, y, 1, 4, c3(WALL2)) end end rect(0, 57, W, 7, c3(FLOOR)) for x = 0, W - 1, 9 do rect(x, 57, 1, 7, c3(FLOOR2)) end rect(0, 60, W, 1, c3(FLOOR2)) rect(58, 18, 1, 20, c3(STEEL_D)); rect(56, 18, 5, 3, c3(STEEL_D)); rect(60, 28, 1, 14, c3(STEEL_D)); rect(59, 41, 3, 2, c3(STEEL_D)) rect(2, 0, 12, 17, c3(FLUE)) rect(0, 16, 17, 41, c3(BRICK)) for y = 18, 54, 4 do rect(0, y, 17, 1, c3(BRICK2)) for x = ((y // 4) % 2) * 4 + 1, 16, 8 do rect(x, y, 1, 4, c3(BRICK2)) end end rect(0, 16, 17, 2, c3(BRICK2)) rect(5, 32, 7, 1, 10, 6, 6); rect(4, 33, 9, 1, 10, 6, 6); rect(3, 34, 11, 14, 10, 6, 6) rect(1, 48, 15, 4, c3(STONE)); rect(1, 48, 15, 1, 96, 80, 72) rect(21, 52, 10, 5, c3(WOOD)); rect(21, 52, 1, 5, c3(WOOD2)); rect(21, 52, 10, 1, c3(WOOD2)) rect(23, 46, 6, 6, c3(STEEL)); rect(19, 43, 17, 3, c3(STEEL)); rect(19, 43, 17, 1, c3(STEEL_HI)) rect(16, 43, 3, 2, c3(STEEL)); rect(16, 43, 3, 1, c3(STEEL_HI)); rect(17, 45, 2, 1, c3(STEEL_D)) rect(22, 50, 8, 2, c3(STEEL_D)) rect(44, 46, 5, 11, c3(PANTS)); rect(50, 46, 5, 11, c3(PANTS)) rect(43, 55, 6, 2, c3(BOOT)); rect(49, 55, 6, 2, c3(BOOT)) rect(44, 26, 12, 21, c3(SHIRT)); rect(43, 27, 14, 3, c3(SHIRT)) rect(44, 32, 8, 15, c3(APRON)); rect(44, 40, 12, 1, c3(BOOT)); rect(47, 30, 1, 3, c3(BOOT)) rect(48, 23, 4, 3, c3(SKIN)) rect(47, 15, 8, 8, c3(SKIN)); rect(47, 16, 2, 6, c3(SKIN_L)); rect(46, 18, 1, 2, c3(SKIN_L)) rect(46, 13, 10, 3, c3(HAIR)); rect(53, 15, 3, 7, c3(HAIR)); rect(47, 21, 5, 3, c3(HAIR)) rect(48, 18, 1, 1, 255, 214, 150) rect(44, 28, 1, 14, c3(RIM)); rect(44, 46, 1, 9, c3(RIM)) BG = gfx.snapshot(0, 0, W, H) end local IX, IY = 28, 40 local function draw_sparks(lt, c, hot) local j1 = floor((lt - 0.82 * Ts) / Ts) for j = j1 - 3, j1 do local age = lt - (j * Ts + 0.82 * Ts) if j >= 0 and j < NS and age >= 0 and age < 46 then local n = 2 + floor(5 * (0.95 - 0.7 * (j / NS))) local a = age / 46 for s = 1, n do local h = hash32((c * 17 + j) * 977 + s * 131) local vx = ((h & 0xFF) / 255 - 0.4) * 2.6 local vy = -(0.9 + ((h >> 8) & 0xFF) / 255 * 1.7) local x = IX + vx * age local y = IY + vy * age + 0.5 * 0.15 * age * age if y < 56 and x >= 0 and x < W and a < 0.92 then px(floor(x), floor(y), grad(SPARK, a)) end end if age < 3 then blend_rect(IX - 2, IY - 1, 5, 3, 255, 214, 130, 0.5 - age * 0.15) end end end end local function draw_steam(qt) for k = 0, 12 do local te = 6 + k * 4 local age = qt - te if age >= 0 and age < 64 then local x = 38 + 3 * sin(te * 0.9 + age * 0.1) + age * 0.03 local y = 48 - age * 0.4 local r = 1 + age // 14 blend_rect(floor(x - r), floor(y - r), 2 * r + 1, 2 * r + 1, 150, 150, 156, 0.42 * (1 - age / 64)) end end end local function thick(x0, y0, x1, y1, w, r, g, b) local dx, dy = x1 - x0, y1 - y0 local n = floor(sqrt(dx * dx + dy * dy) + 0.5) if n < 1 then n = 1 end for i = 0, n do rect(floor(x0 + dx * i / n + 0.5), floor(y0 + dy * i / n + 0.5), w, w, r, g, b) end end local function arm(sx, sy, hx, hy, w) local l = 7.5 local dx, dy = hx - sx, hy - sy local d = sqrt(dx * dx + dy * dy) if d > 2 * l - 0.3 then local k = (2 * l - 0.3) / d hx, hy = sx + dx * k, sy + dy * k dx, dy, d = hx - sx, hy - sy, 2 * l - 0.3 end local h = sqrt(l * l - d * d / 4) local nx, ny = -dy / d, dx / d if ny < 0 then nx, ny = -nx, -ny end local ex, ey = (sx + hx) / 2 + nx * h, (sy + hy) / 2 + ny * h thick(sx, sy, ex, ey, w, c3(SHIRT)); thick(ex, ey, hx, hy, w, c3(SHIRT)) rect(floor(hx), floor(hy), 2, 2, c3(SKIN)) end local POSE = { A = { 36, 36, 28, 39 }, B = { 38, 34, 33, 36 }, C = { 41, 22, 37, 15 }, D = { 38, 28, 30, 32 }, } local function hammer(p) local hx, hy, cx, cy = p[1], p[2], p[3], p[4] arm(51, 28, hx, hy, 2) thick(hx, hy, cx, cy, 1, c3(WOOD2)) rect(cx - 3, cy - 1, 7, 3, c3(STEEL_HI)); rect(cx - 3, cy + 1, 7, 1, c3(STEEL)) end local function pose_at(ph) if ph < 0.1 or ph >= 0.82 then return POSE.A end if ph < 0.28 then return POSE.B end if ph < 0.7 then return POSE.C end return POSE.D end local function smoothstep(t) if t < 0 then t = 0 elseif t > 1 then t = 1 end return t * t * (3 - 2 * t) end local function draw_fire(I, t) rect(4, 45, 9, 3, 120 * I // 1 + 30, 26 + 10 * I // 1, 10) for x = 4, 12 do local h = 4 + 5 * I * (0.55 + 0.45 * sin(t * 0.37 + x * 1.9)) + 2 * sin(t * 0.9 + x * 3.1) if h > 12 then h = 12 end h = floor(h) rect(x, 45 - h, 1, h, 200, 50, 12) local h2 = floor(h * 0.7) rect(x, 45 - h2, 1, h2, 255, 122, 22) local h3 = floor(h * 0.4) rect(x, 45 - h3, 1, h3, 255, 212, 112) end end function render() if stage < 2 then stage = stage + 1 if stage == 1 then build_masks() else build_scene() end gfx.clear(0, 0, 0) gfx.ready(false) return end local u = (frame + OFF) % ML local S for i = 1, #SEG do if u >= SEG[i][3] and u < SEG[i][3] + SEG[i][2] then S = SEG[i]; break end end local kind, dur, c = S[1], S[2], S[4] local lt = u - S[3] local p = lt / dur local heat, tx, ty, vert = 0, 32, 42, false local pulse = 0 if kind == "F" then local h0 = c == 0 and 0 or 0.15 heat = h0 + (1 - h0) * smoothstep(lt / 45) tx, ty = 15, 46 pulse = lt % 20 < 10 and sin(lt % 20 * 0.314) or 0 elseif kind == "out" then local e = smoothstep(p) heat = 1 - 0.05 * p tx, ty = 15 + 17 * e, 46 - 4 * e elseif kind == "hit" then heat = 0.95 - 0.7 * p tx, ty = 32, 42 elseif kind == "back" then local e = smoothstep(p) heat = 0.25 - 0.1 * p tx, ty = 32 - 17 * e, 42 + 4 * e elseif kind == "toq" then local e = smoothstep(p) heat = 0.25 tx, ty = 32 + 6 * e, 42 - 1 * e elseif kind == "dunk" then heat = 0.25 * (1 - lt / 16) if heat < 0 then heat = 0 end tx, ty, vert = 38, 40, true else local e = smoothstep(p) tx, ty = 38 - 23 * e, 41 + 5 * e end tx, ty = floor(tx + 0.5), floor(ty + 0.5) local I = 0.55 + 0.1 * sin(frame * 0.21) + (kind == "F" and 0.3 * pulse + 0.1 or 0) gfx.blit_rgb(BG, 0, 0, W, H) gfx.blend_mask(GM, 255, 110, 34, GX0, GY0, 64, 50, GCW, GCH, 0.1 + 0.38 * I) draw_fire(I, frame) local br, bg, bb = grad(HEAT, heat) if kind ~= "F" and heat > 0.05 then gfx.blend_mask(GB, br, bg, bb, tx - (vert and 8 or 17), ty - 5, 17, 11, 17, 11, heat * 0.55) end if vert then rect(tx - 1, ty, 2, 12, br, bg, bb) else rect(tx - 12, ty - 1, 12, 2, br, bg, bb) end thick(39, 37, tx, ty, 1, 52, 54, 62) arm(47, 29, 39, 37, 2) rect(tx - 1, ty - 1, 2, 3, 52, 54, 62) local ph = kind == "hit" and (lt % Ts) / Ts or 0.1 hammer(kind == "hit" and pose_at(ph) or POSE.B) if kind == "hit" then draw_sparks(lt, c, heat) end rect(35, 50, 7, 7, c3(WOOD)); rect(35, 50, 1, 7, c3(WOOD2)); rect(35, 53, 7, 1, c3(STEEL_D)); rect(35, 56, 7, 1, c3(STEEL_D)) if kind == "dunk" or kind == "fromq" then draw_steam(kind == "dunk" and lt or 44 + lt) end end