info = { type = "image", title = "Painter by the lake", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Painter copies a lake view onto a canvas", api = 33, } local tr = ({ sk = { ["Landscape"] = "Krajina", ["day"] = "deň", ["sunset"] = "západ" }, cs = { ["Landscape"] = "Krajina", ["day"] = "den", ["sunset"] = "západ" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Landscape"), type = "int", default = "day", opts = { keys = { "day", "sunset" }, T("day"), T("sunset") } }, } local rect, px, blend_rect, blit_rgb, clip = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb, gfx.clip local floor, sin, sqrt, abs, min, max = math.floor, math.sin, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local PALS = { day = { sky = { { 96, 158, 224 }, { 150, 196, 236 }, { 200, 228, 245 } }, far = { 124, 150, 190 }, near = { 66, 98, 130 }, snow = { 238, 244, 250 }, water = { { 100, 166, 200 }, { 70, 130, 176 }, { 44, 100, 150 } }, refl = { 54, 84, 114 }, ripple = { 214, 236, 248 }, bank = { 44, 84, 52 }, reed = { 30, 62, 40 }, smock = { 178, 92, 56 }, smock_d = { 128, 66, 46 }, rim = { 232, 156, 104 }, pants = { 52, 58, 88 }, brush = { { 0, 0, 0 }, { 150, 196, 236 }, { 66, 98, 130 }, { 70, 130, 176 }, { 214, 236, 248 } }, cloud = { 250, 252, 255, 0.8 }, }, sunset = { sky = { { 60, 54, 124 }, { 196, 96, 132 }, { 252, 172, 100 } }, far = { 130, 74, 122 }, near = { 56, 38, 86 }, snow = { 250, 190, 150 }, water = { { 226, 130, 108 }, { 150, 82, 112 }, { 70, 58, 112 } }, refl = { 62, 44, 92 }, ripple = { 255, 200, 140 }, bank = { 34, 42, 56 }, reed = { 22, 28, 40 }, smock = { 40, 94, 108 }, smock_d = { 28, 66, 82 }, rim = { 246, 170, 120 }, pants = { 46, 40, 70 }, brush = { { 0, 0, 0 }, { 196, 96, 132 }, { 56, 38, 86 }, { 150, 82, 112 }, { 255, 200, 140 } }, sun = { 255, 228, 156 }, cloud = { 250, 150, 130, 0.55 }, }, } local P local function lerp(a, b, t) return a + (b - a) * t end local function grad(stops, t) local n = #stops - 1 local k = min(n - 1, floor(t * n)) local a, b, f = stops[k + 1], stops[k + 2], t * n - k return floor(lerp(a[1], b[1], f) + 0.5), floor(lerp(a[2], b[2], f) + 0.5), floor(lerp(a[3], b[3], f) + 0.5) end local function mix(a, b, t) return floor(lerp(a[1], b[1], t) + 0.5), floor(lerp(a[2], b[2], t) + 0.5), floor(lerp(a[3], b[3], t) + 0.5) end local function tri(v) return 1 - abs((v % 1) * 2 - 1) end local function far_p(u) return 0.55 + 0.3 * sin(6.283 * (1.1 * u + 0.1)) + 0.15 * sin(6.283 * (2.7 * u + 0.6)) end local function near_p(u) return 0.25 + 0.5 * tri(u * 2.3 + 0.35) + 0.25 * tri(u * 5.3) end local function scene(x0, y0, w, h, hz, mh, upto, sunx, sunr, suny) if upto >= 1 then for r = 0, hz - 1 do rect(x0, y0 + r, w, 1, grad(P.sky, r / (hz - 1))) end if P.sun then local c = P.sun for dy = -sunr, sunr do local hw = floor(sqrt(sunr * sunr + 0.6 - dy * dy) + 0.5) rect(x0 + sunx - hw, y0 + hz - suny + dy, 2 * hw + 1, 1, c[1], c[2], c[3]) end end end if upto >= 2 then for x = 0, w - 1 do local u = (x + 0.5) / w local hf = floor(mh * far_p(u) + 0.5) local hn = floor(mh * 0.62 * near_p(u) + 0.5) rect(x0 + x, y0 + hz - hf, 1, hf, P.far[1], P.far[2], P.far[3]) if mh > 10 and hf >= mh * 0.88 then rect(x0 + x, y0 + hz - hf, 1, 2, P.snow[1], P.snow[2], P.snow[3]) end rect(x0 + x, y0 + hz - hn, 1, hn, P.near[1], P.near[2], P.near[3]) end end if upto >= 3 then for r = hz, h - 1 do rect(x0, y0 + r, w, 1, grad(P.water, (r - hz) / (h - hz - 1))) end end if upto >= 4 then for x = 0, w - 1 do local u = (x + 0.5) / w local hf = floor(mh * 0.5 * far_p(u) + 0.5) local hn = floor(mh * 0.44 * near_p(u) + 0.5) local c = P.refl rect(x0 + x, y0 + hz, 1, hf, mix(c, P.water[1], 0.5)) rect(x0 + x, y0 + hz, 1, hn, c[1], c[2], c[3]) end local rp = P.ripple for r = hz + 2, h - 1, 3 do blend_rect(x0, y0 + r, w, 1, rp[1], rp[2], rp[3], 0.2) end if P.sun then for r = hz + 1, h - 1, 2 do local hw = 1 + (r - hz) * sunr // (hz // 2 + 1) // 2 blend_rect(x0 + sunx - hw, y0 + r, 2 * hw + 1, 1, P.sun[1], P.sun[2], P.sun[3], 0.55) end end end end local CX, CY, CW, CH = 39, 15, 22, 22 local CHZ, CMH = 12, 7 local LAY = { { 0, CHZ - 1 }, { CHZ - CMH, CHZ - 1 }, { CHZ, CH - 1 }, { CHZ, CHZ + 7 } } local HZ, WH = 33, 54 local SXJ, SYJ, L1, BL = 26, 38.5, 17, 8 local REST_X, REST_Y = 48, 45 local BG, S, ITEMS, CYCLE = nil, nil, nil, 0 local function painter_body() local sm, sd, rm = P.smock, P.smock_d, P.rim local rows = { [38] = { 9, 25 }, [39] = { 7, 27 } } for y = 40, 47 do rows[y] = { 6, 28 } end for y = 48, 56 do rows[y] = { 7, 27 } end for y = 38, 56 do local a, b = rows[y][1], rows[y][2] rect(a, y, b - a + 1, 1, sm[1], sm[2], sm[3]) px(a, y, sd[1], sd[2], sd[3]) px(b, y, rm[1], rm[2], rm[3]) end rect(16, 44, 1, 11, sd[1], sd[2], sd[3]) rect(20, 46, 1, 9, sd[1], sd[2], sd[3]) rect(7, 56, 21, 1, sd[1], sd[2], sd[3]) local pc = P.pants rect(9, 57, 8, 7, pc[1], pc[2], pc[3]) rect(18, 57, 8, 7, pc[1], pc[2], pc[3]) rect(3, 41, 3, 10, sd[1], sd[2], sd[3]) rect(3, 51, 2, 2, 226, 170, 138) local rowsw = { 6, 9, 10, 10, 9, 6 } for i = 1, 6 do rect(0, 49 + i, rowsw[i], 1, 198, 152, 100) end px(2, 52, 200, 50, 46); px(4, 52, 60, 100, 190); px(6, 53, 236, 200, 70); px(3, 54, 240, 240, 236) end local function easel() local wd, wl = { 112, 78, 48 }, { 148, 106, 68 } rect(CX + 4, CY - 4, CW - 8, 2, wl[1], wl[2], wl[3]) rect(CX - 1, CY - 2, CW + 2, CH + 4, wd[1], wd[2], wd[3]) rect(CX, CY, CW, CH, 238, 236, 228) rect(CX - 3, CY + CH + 2, CW + 6, 2, wl[1], wl[2], wl[3]) for y = 41, 60 do local k = (y - 41) // 5 rect(38 - k, y, 2, 1, wd[1], wd[2], wd[3]) rect(60 + k, y, 2, 1, wd[1], wd[2], wd[3]) end rect(49, 41, 2, 16, wd[1] - 30, wd[2] - 24, wd[3] - 16) end local function bank() local b, rd = P.bank, P.reed rect(0, WH, 64, 10, b[1], b[2], b[3]) for x = 0, 63 do local h = hash32(x * 0x9E3779B1) if umod(h, 3) == 0 then rect(x, WH - 1 - umod(h >> 8, 3), 1, 2 + umod(h >> 8, 3), rd[1], rd[2], rd[3]) end end end local function build_bg() scene(0, 0, 64, WH, HZ, 15, 4, 26, 5, 7) bank() easel() painter_body() BG = gfx.snapshot(0, 0, 64, 64) end local function build_states() S = {} for k = 0, 4 do rect(CX, CY, CW, CH, 238, 236, 228) if k > 0 then scene(CX, CY, CW, CH, CHZ, CMH, k, 16, 2, 3) end S[k] = gfx.snapshot(CX, CY, CW, CH) end end local function build_timeline() ITEMS = {} local t = 0 local function add(kind, l, ya, n, dur, p) ITEMS[#ITEMS + 1] = { k = kind, t = t, d = dur, l = l, ya = ya, n = n, p = p } t = t + dur end add("rest", 0, 0, 0, 30) for l = 1, 4 do local y, p = LAY[l][1], 0 while y <= LAY[l][2] do local n = min(3, LAY[l][2] - y + 1) add("stroke", l, y, n, 22, p) y, p = y + n, p + 1 end if l < 4 then add("rest", l, 0, 0, 28) end end add("hold", 4, 0, 0, 190) add("wipe", 4, 0, 0, 24) CYCLE = t end function on_settings() P = PALS[cfg.time] or PALS.day BG, S = nil, nil if not ITEMS then build_timeline() end end local function ss(u) return u * u * (3 - 2 * u) end local function stroke_xy(it, u) local uu = 0.5 * u + 0.5 * ss(u) local fx = (it.p % 2 == 0) and uu or 1 - uu return CX + fx * (CW - 1), CY + it.ya + it.n * 0.5 + 1.3 * sin(u * 9.4248) end local function tip(i, u, f) local it = ITEMS[i] if it.k == "stroke" then local x, y = stroke_xy(it, u) local pv = ITEMS[i - 1] if pv.k == "stroke" and f < 6 then local _, py = stroke_xy(pv, 1) y = lerp(py, y, ss(f / 6)) end return x, y elseif it.k == "rest" then local ax, ay = REST_X, REST_Y local pv, nx = ITEMS[i - 1], ITEMS[i + 1] if pv and pv.k == "stroke" and u < 0.5 then local px0, py0 = stroke_xy(pv, 1) local e = ss(u * 2) return lerp(px0, ax, e), lerp(py0, ay, e) elseif nx and nx.k == "stroke" and u >= 0.5 then local nx0, ny0 = stroke_xy(nx, 0) local e = ss(u * 2 - 1) return lerp(ax, nx0, e), lerp(ay, ny0, e) end return ax, ay end return REST_X, REST_Y + 1.5 * sin(u * 6.283) end local function seg(x1, y1, x2, y2, c) local n = max(abs(x2 - x1), abs(y2 - y1)) n = floor(n + 1) for i = 0, n do local t = i / n rect(floor(lerp(x1, x2, t) + 0.5) - 1, floor(lerp(y1, y2, t) + 0.5) - 1, 3, 3, c[1], c[2], c[3]) end end local function arm(tx, ty, bc) local dx, dy = tx - SXJ, ty - SYJ local d = sqrt(dx * dx + dy * dy) local ux, uy = dx / d, dy / d local rr = d - BL if rr > 2 * L1 - 0.5 then rr = 2 * L1 - 0.5 end if rr < 6 then rr = 6 end local hx, hy = SXJ + ux * rr, SYJ + uy * rr local a = rr / 2 local hg = sqrt(max(0, L1 * L1 - a * a)) local ex, ey = SXJ + ux * a - uy * hg, SYJ + uy * a + ux * hg seg(SXJ, SYJ, ex, ey, P.smock_d) seg(ex, ey, hx, hy, P.smock) rect(floor(hx + 0.5) - 1, floor(hy + 0.5) - 1, 2, 2, 226, 170, 138) local bx, by = hx + ux * BL, hy + uy * BL local n = BL for i = 1, n do local t = i / n px(floor(lerp(hx, bx, t) + 0.5), floor(lerp(hy, by, t) + 0.5), 112, 76, 44) end rect(floor(bx + 0.5) - 1, floor(by + 0.5), 2, 1, bc[1], bc[2], bc[3]) end local function head(hx) local x = hx rect(14 + x, 36, 3, 2, 226, 170, 138) local hr = { 6, 8, 10, 10, 10, 10, 10, 8, 6 } for i = 1, 9 do rect(15 + x - hr[i] // 2, 28 + i, hr[i], 1, 56, 38, 30) end rect(21 + x - 1, 32, 1, 2, 226, 170, 138) local br = { 6, 10, 12, 10 } for i = 1, 4 do rect(15 + x - br[i] // 2, 24 + i, br[i], 1, 200, 60, 56) end px(15 + x, 24, 200, 60, 56) px(13 + x, 26, 236, 120, 110) end local function soft_bar(x, y, w, h, r, g, b, a) local ix = floor(x) local f = x - ix blend_rect(ix + 1, y, w - 1, h, r, g, b, a) blend_rect(ix, y, 1, h, r, g, b, a * (1 - f)) blend_rect(ix + w, y, 1, h, r, g, b, a * f) end local GLINT = { { 31, 38 }, { 35, 41 }, { 30, 45 }, { 34, 48 }, { 36, 36 }, { 62, 44 }, { 62, 49 }, { 31, 51 } } local function draw_sky_life() local c = P.cloud for k = 0, 1 do local x = (frame * (0.06 + 0.03 * k) + 24 + 40 * k) % 96 - 24 local y = 5 + 5 * k soft_bar(x + 3, y, 9, 1, c[1], c[2], c[3], c[4]) soft_bar(x, y + 1, 15, 1, c[1], c[2], c[3], c[4]) soft_bar(x + 4, y + 2, 9, 1, c[1], c[2], c[3], c[4] * 0.5) end local rp = P.ripple for i = 1, #GLINT do local a = sin(frame * 0.07 + i * 2.3) if a > 0.2 then blend_rect(GLINT[i][1], GLINT[i][2], 2, 1, rp[1], rp[2], rp[3], a * 0.8) end end end local function draw_canvas(i, u, f) local it = ITEMS[i] local l = it.l if it.k == "stroke" then blit_rgb(S[l - 1], CX, CY, CW, CH) local top = LAY[l][1] if it.ya > top then clip(CX, CY + top, CW, it.ya - top) blit_rgb(S[l], CX, CY, CW, CH) end local uu = 0.5 * u + 0.5 * ss(u) local w = floor(uu * CW + 0.5) clip(it.p % 2 == 0 and CX or CX + CW - w, CY + it.ya, w, it.n) blit_rgb(S[l], CX, CY, CW, CH) clip() elseif it.k == "wipe" then blit_rgb(S[4], CX, CY, CW, CH) local w = floor(ss(u) * CW + 0.5) clip(CX, CY, w, CH) blit_rgb(S[0], CX, CY, CW, CH) clip() else blit_rgb(S[l], CX, CY, CW, CH) end end function render() if not BG then if not P then P = PALS.day end build_bg() build_states() gfx.clear(0, 0, 0) end blit_rgb(BG, 0, 0, 64, 64) draw_sky_life() local tt = (frame + 250) % CYCLE local i = 1 while i < #ITEMS and ITEMS[i + 1].t <= tt do i = i + 1 end local it = ITEMS[i] local f = tt - it.t local u = f / it.d draw_canvas(i, u, f) local tx, ty = tip(i, u, f) local bc = P.brush[(it.k == "rest" and u >= 0.5 and ITEMS[i + 1] and ITEMS[i + 1].l or it.l) + 1] or P.brush[2] head(tx > 52 and 1 or 0) arm(tx, ty, bc) end