info = { type = "image", title = "Bonsai through the seasons", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bonsai by a window, a year in time-lapse", api = 44, } local tr = ({ sk = { ["Tree"] = "Strom", ["Maple"] = "Javor", ["Pine"] = "Borovica", ["Time-lapse speed"] = "Rýchlosť časozberu", ["Slow"] = "Pomalý", ["Normal"] = "Normálny", ["Fast"] = "Rýchly" }, cs = { ["Tree"] = "Strom", ["Maple"] = "Javor", ["Pine"] = "Borovice", ["Time-lapse speed"] = "Rychlost časosběru", ["Slow"] = "Pomalý", ["Normal"] = "Normální", ["Fast"] = "Rychlý" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "kind", label = T("Tree"), type = "int", default = "maple", opts = { keys = { "maple", "pine" }, T("Maple"), T("Pine") } }, { key = "pace", label = T("Time-lapse speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32 = math.hash32 local W, H = gfx.W, gfx.H local KEY = { 1, 0, 1 } local G = 6 local CX0, CY0, CW, CH = 4, 3, 56, 34 local OX, OY, OW, OH = 6, 4, 52, 38 local function cl(v) v = floor(v + 0.5); return v < 0 and 0 or v > 255 and 255 or v end local function mixc(a, b, t) return { a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t } end local function c3(c) return cl(c[1]), cl(c[2]), cl(c[3]) end local function smooth(t) if t < 0 then t = 0 elseif t > 1 then t = 1 end return t * t * (3 - 2 * t) end local OUT = { { { 150, 196, 228 }, { 214, 232, 236 }, { 150, 184, 140 }, { 140, 126, 122 }, { 150, 196, 110 }, { 236, 170, 190 } }, { { 110, 170, 226 }, { 190, 222, 240 }, { 104, 156, 100 }, { 126, 110, 108 }, { 96, 160, 70 }, { 70, 130, 66 } }, { { 170, 186, 206 }, { 232, 214, 186 }, { 190, 150, 96 }, { 130, 108, 100 }, { 160, 132, 74 }, { 220, 120, 50 } }, { { 176, 188, 204 }, { 226, 230, 238 }, { 214, 220, 230 }, { 236, 240, 246 }, { 236, 240, 246 }, { 224, 228, 236 } }, } local RAMPS = { maple = { { { { 34, 66, 38 }, { 82, 138, 54 }, { 150, 196, 84 } }, { { 150, 76, 96 }, { 226, 140, 164 }, { 255, 208, 222 } } }, { { { 16, 46, 28 }, { 42, 98, 44 }, { 96, 152, 62 } }, { { 62, 116, 48 }, { 108, 164, 66 }, { 158, 198, 92 } } }, { { { 96, 40, 18 }, { 190, 98, 32 }, { 240, 166, 62 } }, { { 96, 20, 20 }, { 180, 40, 30 }, { 232, 96, 54 } } }, }, pine = { { { { 22, 56, 34 }, { 44, 100, 56 }, { 92, 150, 78 } }, { { 140, 190, 90 }, { 190, 226, 120 }, { 228, 248, 160 } } }, { { { 16, 46, 30 }, { 36, 88, 50 }, { 76, 134, 70 } }, { { 96, 160, 84 }, { 130, 190, 100 }, { 170, 216, 130 } } }, { { { 22, 50, 30 }, { 52, 96, 48 }, { 104, 140, 64 } }, { { 150, 150, 70 }, { 190, 176, 84 }, { 224, 204, 110 } } }, { { { 18, 44, 36 }, { 36, 84, 60 }, { 72, 124, 92 } }, { { 150, 164, 190 }, { 200, 212, 232 }, { 244, 248, 255 } } }, }, } local RAMP = {} local MAPLE = false local TS = 270 local PAL, GRID, PKEY = {}, {}, {} local function ramp6(r) local out = {} for k = 1, 6 do local t = (k - 1) / 5 out[k] = t < 0.5 and mixc(r[1], r[2], t * 2) or mixc(r[2], r[3], (t - 0.5) * 2) end return out end local stage = 0 function on_settings() MAPLE = cfg.kind ~= "pine" TS = ({ slow = 480, normal = 270, fast = 150 })[cfg.pace] or 270 local src = MAPLE and RAMPS.maple or RAMPS.pine RAMP = {} for s = 1, 4 do local d = src[s] or src[3] local a, b = ramp6(d[1]), ramp6(d[2]) RAMP[s] = {} for k = 1, 6 do RAMP[s][k] = a[k]; RAMP[s][k + 6] = b[k] end end if MAPLE then RAMP[4] = RAMP[1] end for g = 1, G do PAL[g] = {}; PKEY[g] = -1; for k = 1, 39 do PAL[g][k] = 0 end end stage = 0 end local STATIC, SNOW local SNX, SNY, SNW, SNH = 6, 4, 52, 42 local TONE, GRP = {}, {} local function r(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end local WOODD, WOOD, WOODL = { 40, 26, 18 }, { 70, 46, 30 }, { 120, 84, 56 } local TRUNK = { { 34, 22, 16 }, { 58, 38, 26 }, { 90, 62, 40 }, { 128, 90, 58 } } local function build_room() r(0, 0, W, H, { 62, 54, 48 }) r(0, 0, 4, H, { 46, 40, 36 }); r(60, 0, 4, H, { 46, 40, 36 }) r(4, 2, 56, 43, { 96, 70, 48 }) r(6, 4, OW, OH, KEY) r(31, 4, 2, OH, { 96, 70, 48 }); r(31, 4, 1, OH, { 140, 104, 72 }) r(4, 2, 56, 1, { 150, 112, 78 }); r(4, 2, 1, 43, { 130, 96, 66 }); r(59, 2, 1, 43, { 60, 42, 30 }) r(2, 42, 60, 3, { 170, 150, 126 }); r(2, 45, 60, 1, { 78, 66, 56 }) r(0, 46, W, 4, { 146, 100, 62 }) for y = 46, 49 do local x = 0 while x < W do local h = hash32(y * 977 + x) & 0x7FFF local len = 6 + h % 14 local k = 0.86 + (h >> 4) % 9 / 40 r(x, y, len, 1, { 146 * k, 100 * k, 62 * k }) x = x + len end end r(0, 50, W, 4, { 104, 70, 44 }); r(0, 50, W, 1, { 84, 56, 36 }); r(0, 53, W, 1, { 70, 46, 30 }) r(0, 54, W, 10, { 22, 16, 14 }); r(5, 54, 3, 10, { 46, 32, 24 }); r(56, 54, 3, 10, { 46, 32, 24 }) r(22, 42, 20, 7, { 52, 68, 88 }) r(22, 42, 20, 1, { 96, 118, 138 }); r(22, 43, 4, 6, { 78, 98, 120 }); r(38, 43, 4, 6, { 34, 46, 62 }) r(22, 47, 20, 1, { 40, 54, 72 }); r(23, 49, 3, 1, { 40, 54, 72 }); r(38, 49, 3, 1, { 40, 54, 72 }) r(21, 49, 22, 1, { 84, 58, 38 }) r(23, 41, 18, 1, { 48, 36, 26 }) for i = 0, 7 do px(24 + (hash32(i * 31) & 0x7FFF) % 16, 41, 74, 104, 52) end end local function bark(cx, y, hw) local x0 = floor(cx - hw + 0.5) local w = floor(2 * hw + 0.5) if w < 3 then w = 3 end local h = hash32(y * 131) & 0x7FFF r(x0, y, w, 1, TRUNK[3]) r(x0, y, 1, 1, TRUNK[3]); r(x0 + 1, y, w > 4 and 2 or 1, 1, TRUNK[4]) r(x0 + w - 1, y, 1, 1, TRUNK[1]); r(x0 + w - 2, y, 1, 1, TRUNK[2]) if h % 3 == 0 then r(x0 + 2 + h % (w - 2), y, 1, 1, TRUNK[2]) end end local SNOWSET = {} local function branch(x0, y0, x1, y1, x2, y2, t0, t1) local n = floor(sqrt((x2 - x0) ^ 2 + (y2 - y0) ^ 2) * 1.3) + 2 for i = 0, n do local t = i / n local s = 1 - t local x = s * s * x0 + 2 * s * t * x1 + t * t * x2 local y = s * s * y0 + 2 * s * t * y1 + t * t * y2 local th = floor(t0 + (t1 - t0) * t + 0.5) if th < 1 then th = 1 end local ix, iy = floor(x + 0.5), floor(y + 0.5) r(ix, iy, th, th, TRUNK[3]) r(ix, iy, th, 1, TRUNK[4]) if th > 1 then r(ix, iy + th - 1, th, 1, TRUNK[2]) end SNOWSET[iy * 64 + ix] = true if th > 1 then SNOWSET[iy * 64 + ix + 1] = true end end end local PADS local function pads_for(maple) if maple then return { { 30, 9, 10, 5.5 }, { 18, 14, 8, 5 }, { 43, 13, 9, 5 }, { 30, 17, 11, 5.5 }, { 12, 21, 6.5, 4.5 }, { 49, 21, 7.5, 4.5 }, { 21, 24, 7, 4 }, { 39, 24, 8, 4 } } end return { { 28, 9, 11, 4 }, { 45, 14, 12, 4 }, { 19, 18, 12, 4.5 }, { 37, 23, 13, 4 }, { 13, 27, 9, 3.5 }, { 49, 28, 9, 3.5 } } end local function build_trunk() local cx0 = 32 if MAPLE then for y = 41, 26, -1 do local k = 41 - y bark(cx0 + 3 * sin(k * 0.19), y, 2.6 + (k < 4 and (4 - k) * 0.6 or 0) - k * 0.06) end r(26, 40, 4, 1, TRUNK[2]); r(35, 40, 4, 1, TRUNK[1]); r(27, 41, 2, 1, TRUNK[1]) else for y = 41, 20, -1 do local k = 41 - y bark(31 + 6 * sin(k * 0.17 + 0.4), y, 3.4 + (k < 5 and (5 - k) * 0.5 or 0) - k * 0.05) end r(25, 40, 5, 1, TRUNK[2]); r(35, 40, 5, 1, TRUNK[1]) end end local function build_branches() if MAPLE then branch(32, 33, 25, 29, 16, 20, 3, 1) branch(33, 31, 40, 27, 44, 17, 3, 1) branch(32, 31, 29, 25, 30, 15, 2, 1) branch(31, 35, 24, 31, 21, 26, 2, 1) branch(34, 34, 41, 32, 40, 26, 2, 1) branch(30, 29, 22, 24, 12, 23, 2, 1) branch(35, 28, 44, 25, 50, 22, 2, 1) else branch(34, 32, 40, 28, 40, 24, 2, 2) branch(32, 26, 22, 24, 19, 19, 2, 1) branch(36, 30, 46, 30, 49, 29, 2, 1) branch(28, 28, 20, 30, 13, 28, 2, 1) branch(35, 20, 42, 19, 45, 15, 2, 1) branch(30, 18, 28, 14, 28, 10, 2, 1) end end local function build_snow_mask(h) local t = SNOW or {} local y0 = h == 1 and SNY or SNY + SNH // 2 local y1 = h == 1 and SNY + SNH // 2 - 1 or SNY + SNH - 1 for y = y0, y1 do local row = {} for x = 0, SNW - 1 do local k = y * 64 + SNX + x row[x + 1] = (SNOWSET[k] and not SNOWSET[k - 64]) and 255 or 0 end t[#t + 1] = string.char(table.unpack(row, 1, SNW)) end SNOW = t if h == 2 then SNOW = table.concat(t); SNOWSET = {} end end local function noise(x, y, k) return hash32(x * 7919 + y * 104729 + k * 15485863) & 0x7FFF end local function shade_pad(p) local cx, cy, rx, ry = p[1], p[2], p[3], p[4] for y = floor(cy - ry - 1), floor(cy + ry + 1) do for x = floor(cx - rx - 1), floor(cx + rx + 1) do local nx, ny = (x + 0.5 - cx) / rx, (y + 0.5 - cy) / ry local nz2 = noise(x, y, 1) % 100 local lim = 1 + (MAPLE and 0.5 or 0.2) * (nz2 - 50) / 100 local r2 = nx * nx + ny * ny if r2 <= lim then local i = (y - CY0) * CW + (x - CX0) + 1 local nz = sqrt(r2 < 1 and 1 - r2 or 0) local lum = -0.45 * nx - 0.55 * ny + 0.7 * nz local tone = floor((lum + 0.7) / 1.9 * 6) + 1 local nn = noise(x, y, 2) % 9 if nn == 0 then tone = tone - 1 elseif nn == 1 then tone = tone + 1 end if TONE[i] and TONE[i] ~= 0 and ny < -0.55 then tone = 1 end if tone < 1 then tone = 1 elseif tone > 6 then tone = 6 end local accent if MAPLE then accent = noise(x, y, 3) % 6 == 0 else accent = ny < -0.25 and tone >= 3 end TONE[i] = accent and tone + 6 or tone GRP[i] = noise(x // 2, y // 2, 4) % G + 1 end end end end local function build_group(g) local rows = {} for j = 0, CH - 1 do local row = {} for i = 1, CW do local k = j * CW + i row[i] = GRP[k] == g and TONE[k] or 0 end rows[j + 1] = string.char(table.unpack(row, 1, CW)) end GRID[g] = table.concat(rows) end local CUR = { {}, {}, {}, {}, {}, {}, {} } local function season(yph) local i = floor(yph) local u = yph - i return i % 4 + 1, u end local function underlay(i, f) local A, B = OUT[i], OUT[i % 4 + 1] local s = {} for k = 1, 6 do local a, b = A[k], B[k] s[k] = { a[1] + (b[1] - a[1]) * f, a[2] + (b[2] - a[2]) * f, a[3] + (b[3] - a[3]) * f } end local top, hor, hill, roof, lawn, blob = s[1], s[2], s[3], s[4], s[5], s[6] for y = OY, 27 do local t = (y - OY) / 23 rect(OX, y, OW, 1, cl(top[1] + (hor[1] - top[1]) * t), cl(top[2] + (hor[2] - top[2]) * t), cl(top[3] + (hor[3] - top[3]) * t)) end rect(OX, 25, 18, 4, c3(hill)); rect(24, 23, 16, 6, c3(hill)); rect(38, 25, 20, 4, c3(hill)) local hd = { hill[1] * 0.92, hill[2] * 0.92, hill[3] * 0.92 } rect(OX, 27, OW, 6, c3(hd)) rect(9, 28, 18, 6, 224, 218, 206); rect(9, 28, 18, 1, 196, 190, 180) for k = 0, 6 do rect(18 - 3 - k * 2 - 2, 21 + k, 10 + k * 4 - 2 + 2, 1, c3(roof)) end rect(15, 20, 6, 1, c3(roof)) rect(15, 30, 3, 4, 120, 96, 76) for _, b in ipairs({ { 47, 23, 5 }, { 53, 25, 4 }, { 40, 26, 3 } }) do rect(b[1] - 1, b[2] + b[3] - 1, 2, 5 - b[3] + 6, 92, 70, 52) rect(b[1] - b[3] // 2, b[2], b[3], b[3], c3(blob)) rect(b[1] - b[3] // 2 - 1, b[2] + 1, b[3] + 2, b[3] - 2, c3(blob)) end rect(OX, 33, OW, 9, c3(lawn)) rect(OX, 38, OW, 4, lawn[1] * 0.9, lawn[2] * 0.9, lawn[3] * 0.9) return s end local function splat(x, y, r_, g, b, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend_rect(ix, iy, 1, 1, r_, g, b, a * (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, 1, 1, r_, g, b, a * fx * (1 - fy)) blend_rect(ix, iy + 1, 1, 1, r_, g, b, a * (1 - fx) * fy) blend_rect(ix + 1, iy + 1, 1, 1, r_, g, b, a * fx * fy) end local function draw_falling(t, wp, wl, ws) for s = 1, 12 do local h = hash32(s * 977 + 5) local P = 300 + (h & 0xFF) local q = ((t + ((h >> 8) & 0x1FF) * 3) % P) / P local x0 = 12 + ((h >> 12) & 0x7FFF) % 40 local y0 = 12 + ((h >> 3) & 0x7FFF) % 14 local x = x0 + 5 * sin(q * 9 + s) + q * 7 local y = y0 + q * (47 - y0) local a = (q < 0.07 and q / 0.07 or (q > 0.92 and (1 - q) / 0.08 or 1)) if wp > 0.1 then splat(x, y, 255, 206, 220, a * wp * 0.9) elseif wl > 0.1 then local c = (s % 2 == 0) and 224 or 240 splat(x, y, c, 112 + s * 6, 36, a * wl * 0.9); splat(x + 1, y + 0.4, c, 112 + s * 6, 36, a * wl * 0.7) elseif ws > 0.1 then splat(x, y, 250, 252, 255, a * ws * 0.9) end end end local function draw_outside_snow(t, w) for s = 1, 14 do local h = hash32(s * 131 + 9) local P = 330 + (h & 0x7F) local q = ((t + ((h >> 8) & 0x1FF) * 2) % P) / P local x = OX + ((h >> 12) & 0x7FFF) % (OW - 4) + 2 * sin(q * 7 + s) local y = OY + q * (OH - 4) splat(x, y, 252, 252, 255, 0.85 * w) end end local function wsnow(i, f) return i == 3 and f or (i == 4 and 1 - f or 0) end local STEPS = {} local function make_steps() PADS = pads_for(MAPLE) STEPS = { build_room, build_trunk, build_branches, function() build_snow_mask(1) end, function() build_snow_mask(2); STATIC = gfx.snapshot(0, 0, W, H) end, function() TONE, GRP = {}, {} for k = 1, CW * CH do TONE[k] = 0; GRP[k] = 0 end end, } for k = 1, #PADS do STEPS[#STEPS + 1] = function() shade_pad(PADS[k]) end end for g = 1, G do STEPS[#STEPS + 1] = function() build_group(g) end end STEPS[#STEPS + 1] = function() TONE, GRP = nil, nil end end function render() if stage < #STEPS or stage == 0 then if stage == 0 then SNOW = nil; make_steps() end stage = stage + 1 STEPS[stage]() if stage > 4 then gfx.clear(0, 0, 0) end if stage > 5 then gfx.blit_rgb(STATIC, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) end gfx.ready(false) return end local yph = frame / TS + 0.22 local i, u = season(yph) local f = smooth((u - 0.55) / 0.45) local s = underlay(i, f) local ws = wsnow(i, f) if ws > 0.05 then draw_outside_snow(frame, ws) end blend_rect(8, 46, 48, 3, s[2][1], s[2][2], s[2][3], 0.28) gfx.blit_rgb(STATIC, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) if MAPLE and ws > 0.02 then gfx.blend_mask(SNOW, 244, 248, 255, SNX, SNY, SNW, SNH, SNW, SNH, ws) end local from, to = RAMP[i], RAMP[i % 4 + 1] local s0, sp = 0.5, 0.35 if i == 3 then s0, sp = 0.28, 0.5 elseif i == 4 then s0, sp = 0.62, 0.22 end if MAPLE and i == 3 then to = RAMP[3] end for g = 1, G do local rg = (g - 1) / G local fg = smooth((u - (s0 + sp * rg)) / 0.14) local show = true if MAPLE then if i == 3 then show = fg < 0.999 elseif i == 4 then show = fg > 0.001 end end if show then local p = PAL[g] local key = i * 100 + floor(fg * 40) if PKEY[g] ~= key then PKEY[g] = key for k = 1, 12 do local a, b = from[k], to[k] local o = k * 3 p[o + 1] = cl(a[1] + (b[1] - a[1]) * fg) p[o + 2] = cl(a[2] + (b[2] - a[2]) * fg) p[o + 3] = cl(a[3] + (b[3] - a[3]) * fg) end end gfx.blit_indexed(GRID[g], p, CX0, CY0, CW, CH, CW, CH, true) end end local wp = i == 1 and 1 - f or (i == 4 and f or 0) local wl = i == 3 and 1 - f or (i == 2 and f or 0) local wf = i == 4 and 1 - f or (i == 3 and f or 0) if MAPLE then draw_falling(frame, wp, wl, wf) else draw_falling(frame, 0, 0, wf) end end