info = { type = "image", title = "Pottery wheel", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A vase takes shape on a spinning wheel", api = 44, } local tr = ({ sk = { ["Clay"] = "Hlina", ["Terracotta"] = "Terakota", ["Gray"] = "Sivá", ["White"] = "Biela" }, cs = { ["Clay"] = "Hlína", ["Terracotta"] = "Terakota", ["Gray"] = "Šedá", ["White"] = "Bílá" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "clay", label = T("Clay"), type = "int", default = "terracotta", opts = { keys = { "terracotta", "gray", "white" }, T("Terracotta"), T("Gray"), T("White") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_indexed, blit_rgb, blend_mask = gfx.blit_indexed, gfx.blit_rgb, gfx.blend_mask local floor, sin, cos, sqrt, max, min = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min local hash32 = math.hash32 local char, byte, unpack = string.char, string.byte, table.unpack local W, H = gfx.W, gfx.H local TAU, PI = 6.28318, 3.14159 local CLAYS = { terracotta = { 198, 106, 68 }, gray = { 140, 140, 150 }, white = { 234, 228, 216 } } local METAL = { 150, 152, 162 } local WY = 45 local SKIN, SKIN_L, SKIN_D = { 232, 176, 146 }, { 246, 206, 178 }, { 178, 120, 98 } local function lerp(a, b, t) return a + (b - a) * t end local function clamp(v, lo, hi) return v < lo and lo or (v > hi and hi or v) end local function ss(u) u = clamp(u, 0, 1) return u * u * (3 - 2 * u) end local function ramp(base) local p = {} for k = 0, 63 do local u = k / 63 * 1.25 local m = min(1.25, u * 1.15) local w = clamp((u - 0.95) * 2.2, 0, 1) for c = 1, 3 do local v = base[c] * m p[k * 3 + c] = floor(clamp(v * (1 - w) + 255 * 0.92 * w, 0, 255) + 0.5) end end return p end local PALC, PALM, CLAY = nil, nil, CLAYS.terracotta local STRIPS = {} local BUDGET, PENDING = 0, false local function strip(r, tilt) local key = r * 8 + tilt + 3 local s = STRIPS[key] if s then return s end if BUDGET <= 0 then PENDING = true; return string.rep("\34", 2 * r) end BUDGET = BUDGET - 1 local t = {} local ny = tilt * 0.25 local nyz = sqrt(1 - ny * ny) for i = 0, 2 * r - 1 do local nx = (i + 0.5 - r) / r local nz = sqrt(max(0, 1 - nx * nx)) * nyz local d = nx * nyz * -0.62 + ny * 0.48 + nz * 0.62 local v = 0.16 + 0.82 * max(d, 0) if nx > 0.4 then v = v + 0.12 * (nx - 0.4) / 0.6 end local sd = max(nx * nyz * -0.344 + ny * 0.267 + nz * 0.9, 0) sd = sd * sd; sd = sd * sd; sd = sd * sd; sd = sd * sd * sd * sd v = v + sd * 0.4 t[i + 1] = clamp(floor(v / 1.25 * 63 + 0.5), 0, 63) end s = char(unpack(t, 1, 2 * r)) STRIPS[key] = s return s end local VV = { 0, 0.12, 0.38, 0.62, 0.8, 0.9, 0.97, 1 } local VR = { 7.2, 11.5, 14.2, 11.5, 6.3, 5.0, 5.8, 6.6 } local VM = {} for i = 1, #VV do local a, b = VV[i > 1 and i - 1 or i], VV[i < #VV and i + 1 or i] local ra, rb = VR[i > 1 and i - 1 or i], VR[i < #VV and i + 1 or i] VM[i] = (rb - ra) / (b - a) end local function vase_r(v) local i = 1 while i < #VV - 1 and v > VV[i + 1] do i = i + 1 end local dv = VV[i + 1] - VV[i] local t = clamp((v - VV[i]) / dv, 0, 1) local t2 = t * t local t3 = t2 * t return (2 * t3 - 3 * t2 + 1) * VR[i] + (t3 - 2 * t2 + t) * dv * VM[i] + (-2 * t3 + 3 * t2) * VR[i + 1] + (t3 - t2) * dv * VM[i + 1] end local function radius(v, m1, m2) local d = 11.5 * sqrt(max(0, 1 - v * v)) local c = 9.4 + 2.2 * (1 - v) ^ 5 local a = d + (c - d) * m1 if m2 > 0 then a = a + (vase_r(v) - a) * m2 end return floor(a * 4 + 0.5) / 4 end local BG, STAGE = nil, 1 local function row_disc(y, hw, r, g, b) local n = floor(hw) if n > 0 then rect(32 - n, y, 2 * n, 1, r, g, b) end end local STAGES = { function() for y = 0, 53 do rect(0, y, W, 1, floor(lerp(60, 42, y / 53) + 0.5), floor(lerp(44, 31, y / 53) + 0.5), floor(lerp(36, 27, y / 53) + 0.5)) end local m = {} for j = 0, 31 do for i = 0, 31 do local d = sqrt((i - 13) ^ 2 + (j - 15) ^ 2) / 24 local a = clamp(1 - d, 0, 1) m[j * 32 + i + 1] = floor(a ^ 1.2 * 255 + 0.5) end end blend_mask(char(unpack(m, 1, 1024)), 168, 118, 80, 0, 0, W, 54, 32, 32, 0.7) end, function() for y = 52, H - 1 do rect(0, y, W, 1, floor(lerp(104, 58, (y - 52) / 12) + 0.5), floor(lerp(72, 40, (y - 52) / 12) + 0.5), floor(lerp(50, 30, (y - 52) / 12) + 0.5)) end rect(0, 52, W, 1, 132, 94, 64) for x = 9, W - 1, 17 do rect(x, 53, 1, 11, 54, 36, 28) end end, function() BUDGET = 40 local s = strip(11, -1) blit_indexed(s, PALM, 21, 56, 22, 5, 22, 1) blend_rect(21, 56, 22, 5, 0, 0, 0, 0.45) blend_rect(14, 61, 36, 3, 0, 0, 0, 0.35) local sw = strip(21, 0) blit_indexed(sw, PALM, 11, WY, 42, 7, 42, 1) for y = 52, 55 do local hw = 21 * sqrt(1 - ((y - 51.5) / 4.6) ^ 2) local n = floor(hw) if n > 0 then blit_indexed(strip(n, -1), PALM, 32 - n, y, 2 * n, 1, 2 * n, 1) end end for dy = -4, 4 do local hw = 21 * sqrt(1 - (dy / 4.6) ^ 2) local sh = dy < 0 and 1.12 or (dy > 2 and 0.82 or 1) row_disc(WY + dy, hw, floor(172 * sh), floor(172 * sh), floor(182 * sh)) local h2 = 16 * sqrt(1 - (dy / 3.6) ^ 2) if dy >= -3 and dy <= 3 then row_disc(WY + dy, h2, 128, 128, 140) end end BG = gfx.snapshot(0, 0, W, H) end, } function on_settings() CLAY = CLAYS[cfg.clay] or CLAYS.terracotta PALC, PALM = ramp(CLAY), ramp(METAL) STRIPS = {} BG, STAGE = nil, 1 end local RR = {} local function flush(ys, cnt, n, fr4, tilt) if n < 1 then return end local s = strip(n, tilt) blit_indexed(s, PALC, 32 - n, ys, 2 * n, cnt, 2 * n, 1) if fr4 > 0 then local a = fr4 / 4 local i0, i1 = byte(s, 1) * 3, byte(s, 2 * n) * 3 blend_rect(31 - n, ys, 1, cnt, PALC[i0 + 1], PALC[i0 + 2], PALC[i0 + 3], a) blend_rect(32 + n, ys, 1, cnt, PALC[i1 + 1], PALC[i1 + 2], PALC[i1 + 3], a) end end local function draw_form(h, m1, m2, yoff) local ye = WY + yoff local y0, y1 = floor(ye - h), floor(ye) for y = y0 - 1, y1 + 1 do local v = clamp((ye - (y + 0.5)) / h, 0, 1) RR[y + 100] = radius(v, m1, m2) end local rn, rfr, rt, ys = -1, 0, 0, y0 for y = y0, y1 do local r = RR[y + 100] local n = floor(r) local fr4 = floor((r - n) * 4 + 0.5) if fr4 == 4 then n, fr4 = n + 1, 0 end local tilt = clamp(floor((RR[y + 101] - RR[y + 99]) * 1.3 + 0.5), -2, 2) if y >= y1 - 1 then tilt = -2 end if n ~= rn or fr4 ~= rfr or tilt ~= rt then if y > ys then flush(ys, y - ys, rn, rfr, rt) end ys, rn, rfr, rt = y, n, fr4, tilt end end flush(ys, y1 + 1 - ys, rn, rfr, rt) local r0 = RR[y1 + 100] local ryb = max(1, floor(r0 * 0.23 + 0.5)) for dy = 1, ryb do local n = floor(r0 * sqrt(1 - (dy / (ryb + 0.6)) ^ 2)) flush(y1 + dy, 1, n, 0, -2) end if m1 > 0.25 then local rt_ = RR[y0 + 100] local ryt = max(1, floor(rt_ * 0.24 + 0.5)) local k1 = 1.1 for dy = -ryt, ryt do local hw = rt_ * sqrt(1 - (dy / (ryt + 0.4)) ^ 2) row_disc(y0 + dy, hw, floor(min(255, CLAY[1] * k1)), floor(min(255, CLAY[2] * k1)), floor(min(255, CLAY[3] * k1))) if m1 > 0.5 and ryt >= 2 and dy > -ryt and dy < ryt then local f = dy == -ryt + 1 and 0.55 or 0.28 row_disc(y0 + dy, hw - 1.7, floor(CLAY[1] * f), floor(CLAY[2] * f), floor(CLAY[3] * f)) end end end return y0, y1 end local FY, FT = {}, {} for i = 1, 10 do local hh = hash32(i * 0x9E3779B1) FY[i] = 0.08 + (hh & 255) / 255 * 0.8 FT[i] = ((hh >> 8) & 1023) / 1023 * TAU end local function surface_marks(h, yoff, rot, strength) local ye = WY + yoff local dr, dg, db = floor(CLAY[1] * 0.5), floor(CLAY[2] * 0.5), floor(CLAY[3] * 0.5) local y0, y1 = floor(ye - h), floor(ye) for y = y0 + 2, y1 - 1, 3 do local ph = rot + 0.45 * y local c = cos(ph) if c > 0.05 then local x = 31.5 + RR[y + 100] * sin(ph) * 0.97 local ix = floor(x) local f = x - ix local a = 0.3 * c * strength blend(ix, y, dr, dg, db, a * (1 - f)) blend(ix + 1, y, dr, dg, db, a * f) end end for i = 1, 10 do local th = FT[i] + rot if cos(th) > 0.2 then local y = floor(ye - FY[i] * h) local r = RR[y + 100] if r then local x = floor(31.5 + r * sin(th) * 0.88) if i % 2 == 0 then px(x, y, dr, dg, db) else px(x, y, floor(min(255, CLAY[1] * 1.2)), floor(min(255, CLAY[2] * 1.2)), floor(min(255, CLAY[3] * 1.2))) end end end end end local function vrect(x, w, y, h, r, g, b) local y0, y1 = floor(y), floor(y + h) if w < 1 or y1 <= y0 then return end if y1 - y0 > 1 then rect(x, y0 + 1, w, y1 - y0 - 1, r, g, b) end blend_rect(x, y0, w, 1, r, g, b, 1 - (y - y0)) blend_rect(x, y1, w, 1, r, g, b, y + h - y1) end local function hand(side, yc, rr, slide) local xin = floor(31.5 + side * (rr + 0.3 + slide) + 0.5) local xp = side < 0 and xin - 5 or xin local x0, x1 = side < 0 and 0 or xin + 5, side < 0 and xin - 5 or W if x1 > x0 then local xm = (x0 + x1) // 2 vrect(x0, xm - x0, yc - (side < 0 and 3.6 or 2.6), side < 0 and 6.4 or 5.4, SKIN_D[1] + 24, SKIN_D[2] + 24, SKIN_D[3] + 24) vrect(xm, x1 - xm, yc - (side < 0 and 2.6 or 3.6), side < 0 and 5.4 or 6.4, SKIN_D[1] + 24, SKIN_D[2] + 24, SKIN_D[3] + 24) vrect(x0, x1 - x0, yc - 3, 1.4, SKIN[1], SKIN[2], SKIN[3]) end vrect(xp, 5, yc - 3.9, 7.8, SKIN[1], SKIN[2], SKIN[3]) vrect(xp, 5, yc - 3.9, 1.4, SKIN_L[1], SKIN_L[2], SKIN_L[3]) vrect(side < 0 and xin - 1 or xin, 1, yc - 3.1, 6.2, CLAY[1], CLAY[2], CLAY[3]) end local LOOP = 700 local SHOT, SHOT_T = nil, -1 local function wheel_dots(rot) for i = 0, 5 do local a = rot * 0.8 + i * (TAU / 6) local x = 31.5 + 18 * cos(a) local y = WY + 3 * sin(a) local ix = floor(x) local f = x - ix blend(ix, floor(y), 214, 216, 224, 1 - f) blend(ix + 1, floor(y), 214, 216, 224, f) end end function render() if not BG then STAGES[STAGE]() STAGE = STAGE + 1 if not BG then gfx.ready(false) return end end BUDGET, PENDING = 6, false local t = frame local tc = (t + 440) % LOOP local rot = t * 0.0838 local h, m1, m2, yoff = 10, 0, 0, 0 local show, hv, slide, wire = true, nil, 0, nil local hold = false if tc < 10 or tc >= 690 then show = false elseif tc < 32 then local u = (tc - 10) / 22 yoff = floor(-66 * (1 - u * u) - 0.5) elseif tc < 40 then yoff = -floor(3 * sin(PI * (tc - 32) / 8) + 0.5) elseif tc < 80 then if tc >= 50 then hv, slide = 0.35, (1 - ss((tc - 50) / 30)) * 26 end elseif tc < 250 then local u = (tc - 80) / 170 m1 = ss(u); h = lerp(10, 27, m1) hv = 0.5 - 0.4 * cos(3 * PI * u) elseif tc < 400 then local u = (tc - 250) / 150 m1, m2 = 1, ss(u); h = lerp(27, 31, m2) hv = lerp(0.25, 0.8, ss(u)) elseif tc < 430 then m1, m2, h = 1, 1, 31 hv, slide = 0.8, ss((tc - 400) / 30) * 26 elseif tc < 570 then m1, m2, h, hold = 1, 1, 31, true elseif tc < 625 then m1, m2, h = 1, 1, 31 hv = 0 if tc < 590 then slide = (1 - ss((tc - 570) / 20)) * 26 end if tc >= 590 then wire = -3 + 6 * ss((tc - 590) / 25) end else m1, m2, h = 1, 1, 31 local u = (tc - 625) / 65 yoff = -floor(80 * u * u) hv = 0 end if tc < SHOT_T then SHOT = nil end SHOT_T = tc if hold and SHOT then blit_rgb(SHOT, 0, 0, W, H) else blit_rgb(BG, 0, 0, W, H) if show then local ye = WY + yoff if yoff > -8 then local r0 = max(4, RR[floor(ye) + 100] or 9) blend_rect(31 - r0 - 2, WY, 2 * r0 + 6, 1, 0, 0, 0, 0.32) blend_rect(30 - r0, WY + 1, 2 * r0 + 4, 2, 0, 0, 0, 0.32) blend_rect(33 - r0, WY + 3, 2 * r0 - 2, 1, 0, 0, 0, 0.25) end draw_form(h, m1, m2, yoff) if PENDING then gfx.ready(false) elseif hold then SHOT = gfx.snapshot(0, 0, W, H) end end end wheel_dots(rot) if not show then return end surface_marks(h, yoff, rot, 1) if hv then local ye = WY + yoff local yc = ye - hv * h - (hv == 0 and 1 or 0) local rr = RR[floor(yc) + 100] or 8 if wire then local f = wire - floor(wire) local yw = ye + wire local xa, xb = floor(31.5 - rr - slide - 1), floor(31.5 + rr + slide + 1) blend_rect(xa, floor(yw), xb - xa, 1, 214, 220, 232, 1 - f) blend_rect(xa, floor(yw) + 1, xb - xa, 1, 214, 220, 232, f) end hand(-1, yc, rr, slide) hand(1, yc, rr, slide) end end