info = { type = "image", title = "Pen and paper plane", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A pen sketches a paper plane that flies off", api = 33, } local tr = ({ sk = { ["Pen colour"] = "Farba pera" }, cs = { ["Pen colour"] = "Barva pera" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "ink", label = T("Pen colour"), type = "color", default = "#2F5BE0" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, atan, sqrt, min, max = math.floor, math.sin, math.cos, math.atan, math.sqrt, math.min, math.max local W, H = gfx.W, gfx.H local PI2 = 6.2831853 local PAGE local function build_page() rect(0, 0, W, H, 248, 244, 226) for i = 1, 36 do local h = math.hash32(i * 0x9E3779B1) rect(math.umod(h, W), math.umod(h >> 8, H), 1, 1, 236, 230, 208) end for y = 5, H - 1, 6 do rect(0, y, W, 1, 150, 178, 216) end rect(11, 0, 1, H, 236, 120, 124) rect(0, 0, 1, H, 226, 220, 198) for _, y in ipairs({ 10, 32, 54 }) do rect(3, y + 1, 4, 2, 60, 58, 70); rect(4, y, 2, 4, 60, 58, 70) rect(4, y + 3, 2, 1, 120, 116, 124) end PAGE = gfx.snapshot(0, 0, W, H) end local N, L, B, R = { 13, 0 }, { -10, -9 }, { -5, 0 }, { -10, 9 } local K, F1, F2 = { 6, 0 }, { -8, -6.5 }, { -8, 6.5 } local SEG = { { N, L }, { L, B }, { B, R }, { R, N }, { N, B }, { K, F1 }, { K, F2 } } local SLEN = {} for i, s in ipairs(SEG) do local dx, dy = s[2][1] - s[1][1], s[2][2] - s[1][2] SLEN[i] = sqrt(dx * dx + dy * dy) end local INK = { 47, 91, 224 } local PENPAL local PEN function on_settings() local c = cfg.ink or { r = 47, g = 91, b = 224 } INK = { c.r, c.g, c.b } PENPAL = { 0, 0, 0, 170, 172, 184, 70, 72, 86, c.r, c.g, c.b, min(255, c.r + 60), min(255, c.g + 60), min(255, c.b + 60), c.r * 6 // 10, c.g * 6 // 10, c.b * 6 // 10, 210, 210, 220 } end do local g = {} for i = 1, 18 * 13 do g[i] = 0 end for i = 0, 17 do local yc = 12 - floor(i * 0.62 + 0.5) local function put(y, v) g[y * 18 + i + 1] = v end if i < 5 then put(yc, 1); if i >= 2 then put(yc + 1, 1) end elseif i < 7 then for d = -1, 1 do put(yc + d, 2) end elseif i < 16 then put(yc - 1, 4); put(yc, 3); put(yc + 1, 5) else for d = -1, 1 do put(yc + d, 6) end end end local t = {} for i = 1, #g do t[i] = string.char(g[i]) end PEN = table.concat(t) end local C0X, C0Y = 24, 38 local EV, T_INK_END = {}, 0 do local t, cx, cy = 0, 84, 6 local function go(x, y, ink, seg, pause) t = t + (pause or 0) local d = sqrt((x - cx) ^ 2 + (y - cy) ^ 2) local dur = max(1, d / (ink and 1.0 or 2.2)) EV[#EV + 1] = { t, t + dur, cx, cy, x, y, seg } t, cx, cy = t + dur, x, y end local function at(p) return C0X + p[1], C0Y + p[2] end local x, y = at(N); go(x, y, false) x, y = at(L); go(x, y, true, 1, 3) x, y = at(B); go(x, y, true, 2) x, y = at(R); go(x, y, true, 3) x, y = at(N); go(x, y, true, 4) x, y = at(B); go(x, y, true, 5) x, y = at(K); go(x, y, false) x, y = at(F1); go(x, y, true, 6, 3) x, y = at(K); go(x, y, false) x, y = at(F2); go(x, y, true, 7, 3) T_INK_END = t go(90, -14, false, nil, 4) end local FLY0 = floor(EV[#EV][2]) + 4 local FLY1 = FLY0 + 190 local FLIP0 = FLY1 + 90 local CYC = FLIP0 + 30 local function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function hermite(x0, y0, mx0, my0, x1, y1, mx1, my1, u) local u2, u3 = u * u, u * u * u local a, b, c, d = 2 * u3 - 3 * u2 + 1, u3 - 2 * u2 + u, -2 * u3 + 3 * u2, u3 - u2 local da, db, dc, dd = 6 * u2 - 6 * u, 3 * u2 - 4 * u + 1, 6 * u - 6 * u2, 3 * u2 - 2 * u return a * x0 + b * mx0 + c * x1 + d * mx1, a * y0 + b * my0 + c * y1 + d * my1, da * x0 + db * mx0 + dc * x1 + dd * mx1, da * y0 + db * my0 + dc * y1 + dd * my1 end local LCX, LCY, LR = 40, 18.5, 6.5 local LAND_X, LAND_Y = 46, 53 local function pose(p) local q = p - FLY0 if q < 0 then return C0X, C0Y, 0, 0 end if q < 50 then local t = q / 50 local x, y, dx, dy = hermite(C0X, C0Y, 16, 0, 40, 25, 16, 0, t ^ 1.6) return x, y, atan(dy, dx), min(1, q / 25) elseif q < 110 then local a = (q - 50) / 60 * PI2 return LCX + LR * sin(a), LCY + LR * cos(a), -a, 1 elseif q < 190 then local t = (q - 110) / 80 local x, y, dx, dy = hermite(40, 25, 18, 0, LAND_X, LAND_Y, 12, 0, 1 - (1 - t) ^ 1.5) return x, y, atan(dy, dx), t < 0.8 and 1 or (1 - t) / 0.2 end return LAND_X, LAND_Y, 0, 0 end local function wu(x0, y0, x1, y1, r, g, b, a) local steep = math.abs(y1 - y0) > math.abs(x1 - x0) if steep then x0, y0, x1, y1 = y0, x0, y1, x1 end if x0 > x1 then x0, y0, x1, y1 = x1, y1, x0, y0 end local grad = x1 == x0 and 0 or (y1 - y0) / (x1 - x0) for x = floor(x0 + 0.5), floor(x1 + 0.5) do local y = y0 + grad * (x - x0) local yi = floor(y) local f = y - yi if steep then blend(yi, x, r, g, b, min(1, 1.5 * (1 - f)) * a); blend(yi + 1, x, r, g, b, min(1, 1.5 * f) * a) else blend(x, yi, r, g, b, min(1, 1.5 * (1 - f)) * a); blend(x, yi + 1, r, g, b, min(1, 1.5 * f) * a) end end end local function fill_tri(ax, ay, bx, by, cx, cy, r, g, b, a) local y0, y1 = floor(min(ay, by, cy) + 0.5), floor(max(ay, by, cy) - 0.5) for y = y0, y1 do local yc = y + 0.5 local xa, xb local function edge(px_, py_, qx, qy) if (py_ <= yc and qy > yc) or (qy <= yc and py_ > yc) then local x = px_ + (yc - py_) * (qx - px_) / (qy - py_) if not xa then xa = x elseif x < xa then xb = xa; xa = x else xb = x end end end edge(ax, ay, bx, by); edge(bx, by, cx, cy); edge(cx, cy, ax, ay) if xb then local x0, x1 = floor(xa + 0.5), floor(xb - 0.5) if x1 >= x0 then blend_rect(x0, y, x1 - x0 + 1, 1, r, g, b, a) end end end end local function plane(cx, cy, th, lift, fill, ink) local ct, st = cos(th), sin(th) local function X(p) return cx + p[1] * ct - p[2] * st end local function Y(p) return cy + p[1] * st + p[2] * ct end if fill > 0 then if lift > 0 then local ox, oy = 3 * lift, 4 * lift fill_tri(X(N) + ox, Y(N) + oy, X(L) + ox, Y(L) + oy, X(B) + ox, Y(B) + oy, 90, 84, 70, 0.2 * fill) fill_tri(X(N) + ox, Y(N) + oy, X(B) + ox, Y(B) + oy, X(R) + ox, Y(R) + oy, 90, 84, 70, 0.2 * fill) end fill_tri(X(N), Y(N), X(L), Y(L), X(B), Y(B), 252, 250, 244, fill) fill_tri(X(N), Y(N), X(B), Y(B), X(R), Y(R), 226, 222, 208, fill) end for i = 1, #SEG do if ink <= 0 then break end local a, b = SEG[i][1], SEG[i][2] local f = min(1, ink / SLEN[i]) local ax, ay = X(a), Y(a) wu(ax, ay, ax + (X(b) - ax) * f, ay + (Y(b) - ay) * f, INK[1], INK[2], INK[3], i >= 6 and 0.7 or 1) ink = ink - SLEN[i] end end function render() if not PAGE then build_page() end local p = frame % CYC local ox = p >= FLIP0 and -W * smooth((p - FLIP0) / 30) or 0 gfx.blit_rgb(PAGE, floor(ox), 0, W, H) if ox < 0 then gfx.blit_rgb(PAGE, floor(ox) + W, 0, W, H) blend_rect(floor(ox) + W, 0, 3, H, 60, 50, 30, 0.12) end if p < FLY0 then local ink, tx, ty = 0, EV[1][3], EV[1][4] for i = 1, #EV do local e = EV[i] if p >= e[2] then tx, ty = e[5], e[6] if e[7] then ink = ink + SLEN[e[7]] end elseif p > e[1] then local f = (p - e[1]) / (e[2] - e[1]) tx, ty = e[3] + (e[5] - e[3]) * f, e[4] + (e[6] - e[4]) * f if e[7] then ink = ink + SLEN[e[7]] * f end break else break end end plane(C0X, C0Y, 0, 0, smooth((p - T_INK_END) / 14), ink) if tx < W + 14 and ty > -14 then local x, y = floor(tx + 0.5), floor(ty + 0.5) gfx.blit_indexed(PEN, PENPAL, x, y - 12, 18, 13, 18, 13, true) end else local x, y, th, lift = pose(p) plane(x + ox, y, th, lift, 1, 1e9) end end