info = { type = "image", title = "Paper boat", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A paper boat sails a rain gutter at dusk", api = 33, } local tr = ({ sk = { ["Boat"] = "Loďka", ["White"] = "Biela", ["Yellow"] = "Žltá", ["Red"] = "Červená" }, cs = { ["Boat"] = "Loďka", ["White"] = "Bílá", ["Yellow"] = "Žlutá", ["Red"] = "Červená" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "boat", label = T("Boat"), type = "int", default = 0, opts = { T("White"), T("Yellow"), T("Red") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, abs, max, min, sqrt = math.floor, math.sin, math.cos, math.abs, math.max, math.min, math.sqrt local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function lerp(a, b, t) return a + (b - a) * t end local function rnd(v) return floor(v + 0.5) end local function hs(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local WATER_Y0, WATER_Y1 = 44, 55 local LAMP_X = 41 local function spans(cells, w, h) local lo, hi = {}, {} for r = 0, h - 1 do local a, b = 99, -1 for i = 0, w - 1 do if cells[r * w + i + 1] ~= 0 then if i < a then a = i end; b = i end end lo[r], hi[r] = a, b end lo[-1], hi[-1], lo[h], hi[h] = 99, -1, 99, -1 cells.lo, cells.hi = lo, hi end local function draw_cells(cells, w, h, x, y, PR, PG, PB, amul) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local w00, w10, w01, w11 = (1 - fx) * (1 - fy), fx * (1 - fy), (1 - fx) * fy, fx * fy local lo, hi = cells.lo, cells.hi amul = amul or 1 for oy = 0, h do local a0, a1 = lo[oy], lo[oy - 1] local b0, b1 = hi[oy], hi[oy - 1] local from = (a0 < a1 and a0 or a1) local to = (b0 > b1 and b0 or b1) + 1 for ox = from, to do local a, r, g, b = 0, 0, 0, 0 local v if ox < w and oy < h and w00 > 0 then v = cells[oy * w + ox + 1] if v ~= 0 then a = a + w00; r = r + PR[v] * w00; g = g + PG[v] * w00; b = b + PB[v] * w00 end end if ox > 0 and oy < h and w10 > 0 then v = cells[oy * w + ox] if v ~= 0 then a = a + w10; r = r + PR[v] * w10; g = g + PG[v] * w10; b = b + PB[v] * w10 end end if ox < w and oy > 0 and w01 > 0 then v = cells[(oy - 1) * w + ox + 1] if v ~= 0 then a = a + w01; r = r + PR[v] * w01; g = g + PG[v] * w01; b = b + PB[v] * w01 end end if ox > 0 and oy > 0 and w11 > 0 then v = cells[(oy - 1) * w + ox] if v ~= 0 then a = a + w11; r = r + PR[v] * w11; g = g + PG[v] * w11; b = b + PB[v] * w11 end end if a > 0.02 then blend(ix + ox, iy + oy, floor(r / a + 0.5), floor(g / a + 0.5), floor(b / a + 0.5), min(a * amul, 1)) end end end end local BW, BH = 17, 12 local POSES = 12 local BOAT, REFL = {}, {} local function boat_cells(yaw) local c, s = cos(yaw), sin(yaw) local ac, as = abs(c), abs(s) local cells = {} for r = 0, BH - 1 do local half if r <= 5 then half = max(1.0, 6.4 * (r + 0.5) / 6 * ac) else half = (7.5 - (r - 6) * 0.9) * ac + (3.4 - (r - 6) * 0.35) * as end for i = 0, BW - 1 do local dx = i - 8 local v = 0 if abs(dx) <= half then if r == 6 then v = 3 elseif r > 6 then v = (dx < 0) and 2 or 3 elseif c >= 0 then v = (dx < 0) and 1 or 2 else v = (dx < -half / 2) and 2 or 3 end end cells[r * BW + i + 1] = v end end local rc = {} for r = 0, 3 do for i = 0, BW - 1 do rc[r * BW + i + 1] = cells[(11 - r) * BW + i + 1] end end spans(cells, BW, BH); spans(rc, BW, 4) return cells, rc end local function boat_pose(k) if not BOAT[k] then BOAT[k], REFL[k] = boat_cells(k * TAU / POSES) end return BOAT[k], REFL[k] end local BOAT_PAL = { [0] = { { 240, 240, 252 }, { 176, 174, 218 }, { 118, 114, 170 } }, [1] = { { 255, 226, 98 }, { 224, 174, 62 }, { 162, 112, 42 } }, [2] = { { 238, 82, 76 }, { 192, 52, 62 }, { 132, 34, 54 } }, } local PR, PG, PB = {}, {}, {} local LEAF_PR = { 226, 168 } local LEAF_PG = { 128, 70 } local LEAF_PB = { 44, 34 } local LEAF = { 0, 1, 1, 0, 1, 2, 1, 1 } spans(LEAF, 4, 2) function on_settings() local p = BOAT_PAL[cfg.boat or 0] or BOAT_PAL[0] for i = 1, 3 do PR[i], PG[i], PB[i] = p[i][1], p[i][2], p[i][3] end BOAT, REFL = {}, {} end local BG local FAR = { { 0, 9, 22 }, { 9, 7, 18 }, { 16, 10, 24 }, { 26, 8, 16 }, { 34, 12, 21 }, { 46, 9, 19 }, { 55, 9, 23 } } local NEAR = { { -2, 14, 26 }, { 14, 8, 30 }, { 22, 10, 23 }, { 32, 12, 28 }, { 44, 8, 25 }, { 52, 14, 21 } } local GROUND = 33 local function build_bg() for y = 0, GROUND - 1 do local t = y / (GROUND - 1) local r, g, b if t < 0.5 then local u = t / 0.5; r, g, b = lerp(44, 150, u), lerp(36, 84, u), lerp(98, 140, u) else local u = (t - 0.5) / 0.5; r, g, b = lerp(150, 244, u), lerp(84, 164, u), lerp(140, 120, u) end rect(0, y, W, 1, rnd(r), rnd(g), rnd(b)) end blend_rect(4, 7, 22, 1, 255, 176, 150, 0.22) blend_rect(30, 12, 28, 1, 255, 170, 150, 0.2) blend_rect(8, 17, 30, 1, 255, 190, 140, 0.18) for _, b in ipairs(FAR) do rect(b[1], b[3], b[2], GROUND - b[3], 122, 76, 126) end for _, b in ipairs(NEAR) do rect(b[1], b[3], b[2], GROUND - b[3], 46, 34, 74) end for _, b in ipairs(NEAR) do for wy = b[3] + 3, GROUND - 3, 4 do for wx = b[1] + 2, b[1] + b[2] - 3, 3 do local h = hs(wx, wy) if wx >= 0 and wx < W - 1 and h % 100 < 22 then if h % 7 == 0 then rect(wx, wy, 1, 2, 190, 214, 252) else rect(wx, wy, 1, 2, 255, 196, 104) end end end end end rect(LAMP_X + 3, 9, 1, 31, 28, 20, 46) rect(LAMP_X - 1, 8, 5, 1, 28, 20, 46) rect(LAMP_X - 2, 9, 3, 2, 255, 232, 168) blend_rect(LAMP_X - 4, 7, 7, 6, 255, 196, 120, 0.12) blend_rect(LAMP_X - 3, 8, 5, 4, 255, 206, 128, 0.2) rect(0, GROUND, W, 7, 64, 56, 98) rect(0, GROUND, W, 1, 96, 78, 116) rect(0, 40, W, 1, 176, 150, 176) rect(0, 41, W, 3, 98, 86, 128) for y = WATER_Y0, WATER_Y1 do local t = (y - WATER_Y0) / (WATER_Y1 - WATER_Y0) rect(0, y, W, 1, rnd(lerp(150, 58, t)), rnd(lerp(100, 66, t)), rnd(lerp(146, 126, t))) end rect(0, 56, W, 8, 56, 50, 90) rect(0, 56, W, 1, 78, 70, 112) for x = 8, W - 1, 18 do rect(x, 57, 1, 7, 70, 64, 104) end for _, p in ipairs({ { 6, 59, 12 }, { 26, 61, 14 }, { 50, 58, 11 } }) do rect(p[1], p[2], p[3], 2, 92, 96, 152) rect(p[1] + 1, p[2] - 1, p[3] - 2, 1, 92, 96, 152) rect(p[1] + 1, p[2] + 2, p[3] - 2, 1, 92, 96, 152) end BG = gfx.snapshot(0, 0, W, H) end local RING = {} for r = 1, 6 do local seen, pts = {}, {} for k = 0, 19 do local a = TAU * k / 20 local dx, dy = rnd(r * cos(a)), rnd(r * 0.4 * sin(a)) local key = dx * 100 + dy if not seen[key] then seen[key] = true; pts[#pts + 1] = dx; pts[#pts + 1] = dy end end RING[r] = pts end local SPOTS = { { 10, 59 }, { 32, 61 }, { 55, 59 }, { 18, 50 }, { 38, 53 }, { 57, 47 }, { 6, 47 }, { 26, 46 }, { 46, 51 }, { 14, 62 } } local ND, TF, RF = 5, 20, 18 local DP = { 41, 47, 53, 59, 67, 73 } local DASH = {} for i = 1, 12 do DASH[i] = { y = WATER_Y0 + 1 + (i * 5) % 11, len = 3 + hs(i, 1) % 4, v = 4 + hs(i, 2) % 40 / 10, off = hs(i, 3) % 90 } end local LP = 24 * FPS local LEAVES = { { off = 0, xl = 9, yw = 51, v = 3.6 }, { off = 8 * FPS, xl = 20, yw = 53, v = 3.0 }, { off = 16 * FPS, xl = 14, yw = 49, v = 3.4 }, } local PERIOD = 24 * FPS local ECC = 0.6 function render() if not BG then build_bg() else gfx.blit_rgb(BG, 0, 0, W, H) end local t = frame / FPS for i = 1, 10 do local d = DASH[i] local xa = (d.off + d.v * t) % (W + d.len + 4) - d.len - 2 local ia, ib = floor(xa), floor(xa + d.len) local fa, fb = xa - ia, xa + d.len - ib local al = 0.38 blend_rect(ia + 1, d.y, ib - ia - 1, 1, 214, 190, 226, al) blend(ia, d.y, 214, 190, 226, al * (1 - fa)) blend(ib, d.y, 214, 190, 226, al * fb) end for y = WATER_Y0 + 3, WATER_Y1 do local k = y - WATER_Y0 local sv = sin(k * 1.9 + t * 3.3) if sv > -0.35 then local w = 1 + floor(1.4 + 1.2 * sv) local ox = rnd(1.2 * sin(k * 0.8 - t * 2.1)) blend_rect(LAMP_X - 1 - w + ox, y, 2 * w + 2, 1, 255, 226, 160, 0.7 - k * 0.04) end end local u = (frame % PERIOD) / PERIOD local bx = -10 + 86 * (u + ECC / TAU * sin(TAU * u)) local sw = max(0, min(1, (u - 0.40) / 0.22)) local yaw = TAU * sw * sw * (3 - 2 * sw) bx = bx + 2.2 * sin(yaw) * (sw > 0 and sw < 1 and 1 or 0) local k = floor(yaw / (TAU / POSES) + 0.5) % POSES local ex, ey = 33, 50 rect(ex - 1, ey, 3, 1, 40, 44, 92) for ring = 1, 2 do local rx, ry, n = (ring == 1) and 7.5 or 4.6, (ring == 1) and 2.4 or 1.5, (ring == 1) and 16 or 10 for j = 0, n - 1 do local a = TAU * j / n local al = 0.5 * max(0, sin(a * 1 - t * (ring == 1 and 2.4 or -3.2))) if al > 0.05 then blend(rnd(ex + rx * cos(a)), rnd(ey + ry * sin(a)), 220, 214, 240, al) end end end if bx > -12 and bx < W + 12 then local bob = 0.55 * sin(t * 2.4 + 1) local cells, rc = boat_pose(k) local by = 36 + rnd(bob) draw_cells(rc, BW, 4, bx - 8, by + 12, PR, PG, PB, 0.3) draw_cells(cells, BW, BH, bx - 8, by, PR, PG, PB) blend_rect(rnd(bx) - 8, by + 11, BW, 1, 96, 88, 150, 0.45) end for i = 1, 3 do local L = LEAVES[i] local tc = (frame + L.off) % LP / FPS local x, y if tc < 5 then x = L.xl - 4 + 4 * tc / 5 + 3 * sin(TAU * tc / 2.2 + i) y = -3 + (L.yw + 3) * tc / 5 else x = L.xl + L.v * (tc - 5) y = L.yw + 0.5 * sin(t * 1.8 + i * 2) end if x < W + 2 then draw_cells(LEAF, 4, 2, x, y, LEAF_PR, LEAF_PG, LEAF_PB) end end for i = 1, ND do local P = DP[i] local fr = frame + i * 13 local n, kk = fr // P, fr % P local s = SPOTS[hs(i, n) % #SPOTS + 1] local sx, sy = s[1], s[2] if kk < TF then local y = kk * (sy + 3) / TF - 2 local iy = floor(y) blend(sx, iy, 208, 214, 248, 0.7) blend(sx, iy - 1, 208, 214, 248, 0.4) blend(sx, iy - 2, 208, 214, 248, 0.2) elseif kk < TF + RF then local q = (kk - TF) / RF local r = 1 + floor(q * 5) local al = 0.7 * (1 - q) local pts = RING[r] for j = 1, #pts, 2 do blend(sx + pts[j], sy + pts[j + 1], 214, 218, 250, al) end if r > 2 then pts = RING[r - 2] for j = 1, #pts, 2 do blend(sx + pts[j], sy + pts[j + 1], 214, 218, 250, al * 0.6) end end end end end