info = { type = "image", title = "Shishi-odoshi garden", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bamboo fountain tips and clacks by a pond", api = 33, } local tr = ({ sk = { ["Season"] = "Ročné obdobie", ["Autumn"] = "Jeseň", ["Spring"] = "Jar" }, cs = { ["Season"] = "Roční období", ["Autumn"] = "Podzim", ["Spring"] = "Jaro" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "season", label = T("Season"), type = "int", default = "autumn", opts = { keys = { "autumn", "spring" }, T("Autumn"), T("Spring") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, max, min = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min local W, H = gfx.W, gfx.H local TAU, RAD = 6.28318, 0.0174533 local PX, PY = 38, 33 local BX0, BY0, BS = PX - 15, PY - 15, 24 local TROUGH_Y = 50 local BAMBOO = { 0,0,0, 150,172,70, 110,136,52, 70,92,40, 188,170,88, 40,56,44 } local WATER, WATER_HI = { 78, 156, 196 }, { 160, 214, 232 } local FILL_F, TIP_F, HOLD_F, RET_F, BNC_F = 219, 30, 18, 15, 9 local CYC = FILL_F + TIP_F + HOLD_F + RET_F + BNC_F local TH_REST, TH_TIP = 40, -50 local DRAIN_F = 18 local A12 = FILL_F + floor(TIP_F * sqrt((TH_REST - 12) / (TH_REST - TH_TIP)) + 0.5) local function c3(c) return c[1], c[2], c[3] end local function ell(cx, cy, rx, ry, c) for dy = -ry, ry do local hw = floor(rx * sqrt(1 - (dy / ry) * (dy / ry)) + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, c3(c)) end end local function spx(x, y, r, g, b, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local w = a * (1 - fx) * (1 - fy); if w > 0.04 then blend(ix, iy, r, g, b, w) end w = a * fx * (1 - fy); if w > 0.04 then blend(ix + 1, iy, r, g, b, w) end w = a * (1 - fx) * fy; if w > 0.04 then blend(ix, iy + 1, r, g, b, w) end w = a * fx * fy; if w > 0.04 then blend(ix + 1, iy + 1, r, g, b, w) end end local POSES = {} local function pose(k) local p = POSES[k] if p then return p end local a = k * 3 * RAD local c, s = cos(a), sin(a) local cells, i0, i1 = {}, {}, {} for j = 0, BS - 1 do for i = 0, BS - 1 do local dx, dy = BX0 + i + 0.5 - PX, BY0 + j + 0.5 - PY local u, v = -dx * c - dy * s, dx * s - dy * c local cell = 0 if u > -5.5 and u < 12.5 and v > -2.6 and v < 2.6 then local inner = v > -1.5 and v < 1.5 and u > -3.8 if inner then cell = 5 if not i0[i] or j < i0[i] then i0[i] = j end if not i1[i] or j > i1[i] then i1[i] = j end elseif u > 3.4 and u < 4.6 or u < -4.3 then cell = 4 elseif v >= 1.5 then cell = 1 elseif v > -1.5 then cell = 2 else cell = 3 end end cells[j * BS + i + 1] = cell end end local rows, row = {}, {} for j = 0, BS - 1 do for i = 1, BS do row[i] = cells[j * BS + i] end rows[j + 1] = string.char(table.unpack(row, 1, BS)) end local lo, hi = 99, -1 for i = 0, BS - 1 do if i0[i] then lo = min(lo, i0[i]); hi = max(hi, i1[i]) end end p = { cells = table.concat(rows), i0 = i0, i1 = i1, lo = lo, hi = hi, mx = PX - 12 * c, my = PY - 12 * s } POSES[k] = p return p end local BG = nil local SEASON = { autumn = { { 172, 40, 40 }, { 218, 84, 48 }, { 116, 30, 42 } }, spring = { { 226, 142, 172 }, { 250, 204, 218 }, { 180, 104, 146 } } } local LEAFC = SEASON.autumn local function build_bg() LEAFC = SEASON[cfg.season] or SEASON.autumn for y = 0, 21 do rect(0, y, W, 1, 16 + y // 3, 20 + y // 2, 46 + y) end rect(0, 22, W, 32, 28, 40, 70) for y = 1, 9 do for x = 44, 54 do local d1 = (x - 49) ^ 2 + (y - 5) ^ 2 local d2 = (x - 50.4) ^ 2 + (y - 4.2) ^ 2 if d1 <= 11 and d2 > 9.5 then px(x, y, 232, 226, 186) end end end ell(18, 38, 20, 14, { 20, 46, 54 }) ell(48, 42, 22, 12, { 22, 54, 58 }) ell(30, 32, 11, 7, { 24, 62, 62 }) ell(8, 30, 7, 5, { 26, 68, 64 }) ell(46, 30, 8, 5, { 26, 68, 64 }) rect(0, 44, W, 10, 22, 50, 56) rect(0, 54, W, 10, 40, 46, 76) for k = 0, 11 do local h = math.hash32(k * 0x9E3779B1) local x, y = math.umod(h, W), 55 + math.umod(h >> 8, 8) ell(x, y, 2 + math.umod(h >> 16, 3), 1, { 58, 66, 104 }) end ell(8, 61, 5, 2, { 36, 78, 60 }); ell(52, 60, 6, 2, { 36, 78, 60 }) ell(43, 43, 6, 5, { 70, 80, 120 }) rect(40, 40, 6, 1, 104, 116, 156) rect(38, 47, 11, 3, 52, 60, 96) rect(37, 33, 2, 21, 96, 66, 44) rect(37, 33, 1, 21, 126, 90, 58) rect(34, 32, 8, 2, 126, 90, 58) rect(34, 33, 8, 1, 74, 50, 38) rect(37, 54, 3, 1, 66, 44, 34) rect(11, 49, 26, 10, 66, 76, 112) rect(12, 48, 24, 1, 104, 116, 156) rect(11, 49, 26, 1, 90, 102, 142) rect(10, 50, 1, 8, 66, 76, 112); rect(37, 50, 1, 8, 66, 76, 112) rect(12, 56, 24, 3, 44, 52, 86) rect(13, 50, 22, 6, 24, 62, 90) rect(13, 50, 22, 1, 36, 84, 112) rect(24, 52, 6, 1, 44, 100, 130) rect(3, 54, 6, 2, 66, 76, 112); rect(4, 40, 3, 14, 80, 92, 134); rect(4, 40, 1, 14, 104, 116, 156) rect(1, 40, 9, 2, 104, 116, 156) rect(2, 34, 7, 6, 70, 80, 120); rect(2, 34, 1, 6, 90, 102, 142) rect(0, 31, 11, 3, 80, 92, 134); rect(0, 31, 11, 1, 110, 122, 164) rect(3, 29, 5, 2, 104, 116, 156); px(5, 28, 130, 142, 184) for x = 29, 57 do local yc = 17 - floor((x - 29) * 8 / 28 + 0.5) rect(x, yc - 1, 1, 1, 150, 172, 70) rect(x, yc, 1, 2, 110, 136, 52) rect(x, yc + 2, 1, 1, 70, 92, 40) if x == 36 or x == 46 or x == 55 then rect(x, yc - 1, 1, 4, 188, 170, 88) end end rect(29, 16, 1, 3, 40, 56, 44) for _, st in ipairs({ { 56, 4 }, { 61, 3 } }) do local x0, w = st[1], st[2] rect(x0, 0, 1, 54, 150, 172, 70) rect(x0 + 1, 0, w - 2, 54, 110, 136, 52) rect(x0 + w - 1, 0, 1, 54, 70, 92, 40) for y = (x0 == 56) and 12 or 22, 52, 14 do rect(x0 - 1, y, w + 2, 1, 188, 170, 88) end end for k = 0, 4 do local x, y = 52 + (k * 5) % 9, 4 + k * 9 rect(x, y, 4, 1, 60, 120, 56); px(x - 1, y + 1, 60, 120, 56); px(x + 4, y - 1, 90, 156, 70) end for x = 0, 25 do rect(x, 4 + x // 5, 1, 2, 84, 56, 40) end ell(6, 5, 7, 3, LEAFC[1]); ell(19, 9, 6, 3, LEAFC[1]); ell(13, 3, 4, 2, LEAFC[3]) ell(5, 3, 3, 1, LEAFC[2]); ell(18, 7, 3, 1, LEAFC[2]); ell(23, 11, 2, 1, LEAFC[3]) for k = 0, 8 do local h = math.hash32(k * 0x85EBCA6B) px(2 + math.umod(h, 22), 6 + math.umod(h >> 8, 6), c3(LEAFC[1 + k % 3])) end BG = gfx.snapshot(0, 0, W, H) end function on_settings() BG = nil end local STARS = { { 6, 2 }, { 30, 3 }, { 40, 7 }, { 12, 14 }, { 34, 12 }, { 53, 14 }, { 24, 1 } } local FIRE = { { 14, 40, 5, 3, 0.0 }, { 24, 28, 4, 5, 2.1 }, { 48, 36, 6, 4, 4.0 }, { 52, 46, 3, 3, 1.0 } } local function ring(cx, cy, age, life, r, g, b) if age < 0 or age >= life then return end local rad = 1 + age * 0.2 local al = 0.75 * (1 - age / life) local n = 10 + 2 * floor(rad) for k = 0, n - 1 do local a = k * TAU / n blend(floor(cx + rad * cos(a) + 0.5), floor(cy + rad * 0.36 * sin(a) + 0.5), r, g, b, al) end end local LEAF_F, LEAF_P = 300, 420 function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame local a = f % CYC for k = 1, #STARS do local st = STARS[k] local on = (f // 20 + k * 3) % 5 ~= 0 px(st[1], st[2], on and 220 or 120, on and 224 or 130, on and 255 or 180) end for k = 1, #FIRE do local q = FIRE[k] local t = f / 30 spx(q[1] + q[3] * sin(t * 0.7 + q[5]), q[2] + q[4] * sin(t * 0.9 + q[5] * 2), 190, 232, 110, 0.35 + 0.35 * sin(t * 2.3 + q[5])) end local fl = (f // 3 + (f // 7) * 5) % 4 rect(3, 36, 3, 3, 255 - fl * 4, 186 - fl * 10, 72 - fl * 3) local th, fill, phase = TH_REST, 0, 0 local shake, spark = 0, false if a < FILL_F then fill = a / FILL_F else local b = a - FILL_F if b < TIP_F then local s = b / TIP_F th = TH_REST - (TH_REST - TH_TIP) * s * s phase = 1 elseif b < TIP_F + HOLD_F then th = TH_TIP phase = 2 elseif b < TIP_F + HOLD_F + RET_F then local s = (b - TIP_F - HOLD_F) / RET_F th = TH_TIP + (TH_REST - TH_TIP) * s * s phase = 3 else local s = (b - TIP_F - HOLD_F - RET_F) / BNC_F th = TH_REST - 6 * sin(3.1416 * s) phase = 4 shake = (s < 0.25) and 1 or 0 spark = s < 0.3 end fill = 1 if phase == 1 or phase == 2 then if th < 12 then fill = max(0, 1 - (a - A12) / DRAIN_F) end elseif phase == 3 or phase == 4 then fill = 0 end end local k = floor(th / 3 + 0.5) local p = pose(k) local pouring = (phase == 1 or phase == 2) and fill > 0.02 and th < 14 local sy = (phase == 1 or phase == 2) and TROUGH_Y or 26 rect(28, 19, 2, sy - 19, 150, 206, 228) for q = 0, 3 do local yy = 19 + (f * 2 + q * 7) % 28 if yy < sy then px(28 + (q & 1), yy, 232, 244, 252) end end gfx.clip(13, 50, 22, 6) local rc = { 150, 196, 220 } for r = 0, 4 do ring(30, 53, a - (A12 + r * 7), 44, c3(rc)) end local lf = f % LEAF_P ring(23, 53, lf - LEAF_F, 50, c3(rc)) ring(23, 53, lf - LEAF_F - 14, 50, c3(rc)) gfx.clip() do local lx, ly, al if lf < LEAF_F then local s = lf / LEAF_F lx, ly, al = 15 + 8 * s + 3 * sin(s * 14), 11 + 41 * s, 1 else lx, ly = 23 + (lf - LEAF_F) * 0.01, 52 + 0.3 * sin(lf * 0.1) al = 1 - (lf - LEAF_F) / (LEAF_P - LEAF_F) end local c1, c2 = LEAFC[2], LEAFC[1] spx(lx, ly, c1[1], c1[2], c1[3], al) spx(lx - 1, ly, c2[1], c2[2], c2[3], al); spx(lx + 1, ly, c2[1], c2[2], c2[3], al) spx(lx, ly - 1, c2[1], c2[2], c2[3], al); spx(lx, ly + 1, c2[1], c2[2], c2[3], al) end gfx.blit_indexed(p.cells, BAMBOO, BX0, BY0 + shake, BS, BS, BS, BS, true) if fill > 0.02 then local yw = p.hi + 1 - fill * (p.hi + 1 - p.lo) for i = 0, BS - 1 do local t0, t1 = p.i0[i], p.i1[i] if t0 then local top = max(t0, floor(yw + 0.5)) if top <= t1 then rect(BX0 + i, BY0 + top + shake, 1, t1 - top + 1, c3(WATER)) px(BX0 + i, BY0 + top + shake, c3(WATER_HI)) end end end end if pouring then local mx = floor(p.mx + 0.5) local my = floor(p.my + 2) rect(mx, my, 2, TROUGH_Y - my, 150, 206, 228) px(mx, my + (f * 2) % max(1, TROUGH_Y - my), 232, 244, 252) for i = 1, 6 do local t = (f + i * 3) % 9 local dir = i <= 3 and -1 or 1 local x = mx + 1 + dir * (0.6 + 0.3 * (i % 3)) * t local y = TROUGH_Y + 0.5 - (1.8 + 0.3 * (i % 3)) * t + 0.4 * t * t if y < TROUGH_Y + 0.5 then blend(floor(x), floor(y), 190, 226, 240, 0.8) end end end if spark then px(40, 38, 236, 230, 190); px(39, 37, 236, 230, 190); px(41, 37, 200, 196, 150) end end