info = { type = "image", title = "Hedge fountain", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fountain in a dithered retro hedge garden", api = 33, } local tr = ({ sk = { ["Sky"] = "Obloha", ["Dusk"] = "Súmrak", ["Noon"] = "Poludnie", ["Night"] = "Noc", ["Water flow"] = "Tok vody" }, cs = { ["Sky"] = "Obloha", ["Dusk"] = "Soumrak", ["Noon"] = "Poledne", ["Night"] = "Noc", ["Water flow"] = "Tok vody" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sky", label = T("Sky"), type = "int", default = "dusk", opts = { keys = { "dusk", "noon", "night" }, T("Dusk"), T("Noon"), T("Night") } }, { key = "flow", label = T("Water flow"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend, blend_rect, blit_idx = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local K, HD, HM, HL, HT, GD, GL, SL, SS, WD, WM, WL, WH, GRS, GRD = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 local S1, S2, S3, S4 = 16, 17, 18, 19 local PAL_BASE = { 0, 0, 0, 6, 8, 18, 10, 58, 34, 22, 112, 58, 70, 170, 80, 130, 210, 110, 70, 64, 80, 150, 142, 150, 190, 186, 196, 96, 92, 112, 24, 60, 170, 60, 130, 230, 150, 230, 250, 250, 252, 255, 30, 100, 40, 16, 70, 36, } local SKIES = { dusk = { 58, 36, 120, 150, 60, 140, 244, 110, 100, 255, 190, 110 }, noon = { 60, 120, 230, 100, 170, 240, 150, 210, 250, 210, 236, 252 }, night = { 8, 10, 40, 16, 22, 70, 28, 44, 110, 52, 76, 150 }, } local PAL, SKY_KEY local BG local PATS = {} local function pat(a, b, ph) local k = (a * 32 + b) * 2 + ph local s = PATS[k] if not s then local pair = ph == 0 and string.char(a, b) or string.char(b, a) s = pair:rep(32) PATS[k] = s end return s end local function hrow(y, x0, x1, a, b) local w = x1 - x0 + 1 if w <= 0 then return end blit_idx(pat(a, b, (x0 + y) % 2):sub(1, w), PAL, x0, y, w, 1, w, 1) end local function ell(cx, cy, rx, ry, c, a) local q = ry * ry + 0.5 for dy = -ry, ry do local hw = floor(rx * sqrt(1 - dy * dy / q) + 0.5) local r, g, b = PAL[c * 3 + 1], PAL[c * 3 + 2], PAL[c * 3 + 3] if a then blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, r, g, b, a) else rect(cx - hw, cy + dy, 2 * hw + 1, 1, r, g, b) end end end local function col(c, x, y, w, h) rect(x, y, w, h, PAL[c * 3 + 1], PAL[c * 3 + 2], PAL[c * 3 + 3]) end local function dot(c, x, y) px(x, y, PAL[c * 3 + 1], PAL[c * 3 + 2], PAL[c * 3 + 3]) end local function wall_x(y) if y < 10 or y > 47 then return -1 end return floor(min(20, 2 * (y - 10), (47 - y) / 0.65)) end local BANDS = { { 0, 6, HL, HM }, { 7, 13, HM, HM }, { 14, 20, HM, HD } } local function hedge_row(y, xm, right) local clump = y % 8 == 3 for i = 1, 3 do local b = BANDS[i] local lo, hi = b[1], min(xm, b[2]) if hi >= lo then local x0, x1 = lo, hi if right then x0, x1 = 63 - hi, 63 - lo end if clump then hrow(y, x0, x1, b[4], b[4]) else hrow(y, x0, x1, b[3], b[4]) end end end end local function build_bg() local sk = SKIES[cfg.sky or "dusk"] or SKIES.dusk PAL = {} for i = 1, #PAL_BASE do PAL[i] = PAL_BASE[i] end for i = 1, 12 do PAL[#PAL + 1] = sk[i] end for y = 0, 19 do if y < 6 then hrow(y, 0, 63, S1, S1) elseif y < 9 then hrow(y, 0, 63, S1, S2) elseif y < 13 then hrow(y, 0, 63, S2, S2) elseif y < 15 then hrow(y, 0, 63, S2, S3) elseif y < 18 then hrow(y, 0, 63, S3, S3) else hrow(y, 0, 63, S3, S4) end end if cfg.sky == "night" then for i = 1, 10 do local h = hash32(i * 0x9E3779B1) dot(WH, 12 + umod(h, 40), 1 + umod(h >> 8, 9)) end for dy = -3, 3 do local hw = floor(sqrt(max(0, 10 - dy * dy)) + 0.5) col(WL, 44 - hw, 6 + dy, 2 * hw + 1, 1) end col(S2, 45, 5, 3, 3) elseif cfg.sky == "noon" then col(WH, 36, 4, 9, 2); col(WH, 34, 6, 14, 2); col(WH, 10, 9, 8, 1); col(WH, 8, 10, 12, 1) end for y = 20, 34 do hrow(y, 20, 43, HM, HD) end hrow(20, 20, 43, HL, HM) for y = 10, 47 do local xm = wall_x(y) if xm >= 0 then hedge_row(y, xm, false) hedge_row(y, xm, true) end end for x = 0, 20 do local ty, by = floor(10 + 0.5 * x), floor(47 - 0.65 * x) dot(HT, x, ty); dot(HT, 63 - x, ty) dot(HD, x, by); dot(HD, 63 - x, by) end for y = 35, 63 do local xl = max(-1, floor((47 - y) / 0.65)) local pl = floor(20 - 0.6 * (y - 34)) local pr = 63 - pl if pl > xl + 1 then hrow(y, xl + 1, pl - 1, GRS, GRD) hrow(y, pr + 1, 62 - xl, GRS, GRD) end if y < 44 then hrow(y, max(pl, 0), min(pr, 63), GD, GL) else hrow(y, max(pl, 0), min(pr, 63), GL, SL) end end ell(34, 58, 22, 4, K, 0.4) ell(32, 54, 17, 6, SS) ell(32, 52, 17, 6, SL) ell(32, 52, 15, 5, SS) ell(32, 53, 14, 4, WD) ell(32, 54, 12, 2, WM) col(SS, 30, 38, 4, 14); col(SL, 30, 38, 1, 14); col(SL, 31, 38, 1, 14) col(SS, 30, 28, 4, 10) ell(32, 40, 9, 3, SS); ell(32, 38, 9, 3, SL); ell(32, 38, 7, 2, SS); ell(32, 38, 6, 1, WM) col(SL, 31, 29, 1, 9) ell(32, 30, 5, 2, SS); ell(32, 29, 5, 2, SL); ell(32, 29, 3, 1, WM) col(SS, 31, 22, 2, 6); col(SL, 31, 22, 1, 6) ell(32, 21, 2, 2, SL); dot(WH, 31, 20) for _, tx in ipairs({ 8, 56 }) do col(SS, tx - 3, 59, 7, 4); col(SL, tx - 3, 59, 7, 1); col(SL, tx - 3, 59, 1, 4) ell(tx, 55, 4, 4, HM); ell(tx - 1, 54, 2, 2, HL); dot(HT, tx - 2, 53) col(HD, tx + 1, 55, 3, 3) end for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) local y = 14 + umod(h, 30) local xm = wall_x(y) if xm > 3 then local x = 1 + umod(h >> 8, xm - 2) dot(umod(h >> 16, 2) == 0 and WH or WL, x, y) dot(umod(h >> 16, 2) == 0 and WH or WL, 63 - x, y) end end BG = gfx.snapshot(0, 0, W, H) end function on_settings() BG = nil end local FALLS_HI = { { 28, 30, 38, -1, 0.0 }, { 30, 30, 38, 0, 0.45 }, { 34, 30, 38, 0, 0.2 }, { 36, 30, 38, 1, 0.7 } } local FALLS_LO = { { 24, 40, 51, -1, 0.1 }, { 26, 40, 51, -1, 0.6 }, { 28, 41, 51, 0, 0.3 }, { 31, 41, 51, 0, 0.8 }, { 34, 41, 51, 0, 0.5 }, { 37, 41, 51, 0, 0.05 }, { 39, 40, 51, 1, 0.4 }, { 41, 40, 51, 1, 0.85 } } local function falls(list, t, n, speed) for i = 1, n do local f = list[i] local s = (t * speed + f[5]) % 1 local len = f[3] - f[2] local y = f[2] + floor(len * (0.25 * s + 0.75 * s * s)) local x = f[1] + floor(f[4] * s * 2) rect(x, y, 1, 1 + floor(2 * s), PAL[WH * 3 + 1], PAL[WH * 3 + 2], PAL[WH * 3 + 3]) local s2 = (s + 0.5) % 1 local y2 = f[2] + floor(len * (0.25 * s2 + 0.75 * s2 * s2)) rect(f[1] + floor(f[4] * s2 * 2), y2, 1, 1 + floor(2 * s2), PAL[WL * 3 + 1], PAL[WL * 3 + 2], PAL[WL * 3 + 3]) end end local function ring(cx, cy, rx, ry, c, a) local r, g, b = PAL[c * 3 + 1], PAL[c * 3 + 2], PAL[c * 3 + 3] local q = ry * ry + 0.5 local function hw(d) return floor(rx * sqrt(1 - d * d / q) + 0.5) end for dy = 0, ry do local h = hw(dy) for s = 1, (dy == 0 and 1 or 2) do local yy = s == 1 and cy + dy or cy - dy if dy == ry then blend_rect(cx - h, yy, 2 * h + 1, 1, r, g, b, a) else local nx = hw(dy + 1) blend_rect(cx - h, yy, h - nx + 1, 1, r, g, b, a) blend_rect(cx + nx, yy, h - nx + 1, 1, r, g, b, a) end end end end local function ripples(t, n) for k = 0, 2 do local u = ((t + k * 36) % 108) / 108 local a = (1 - u) * 0.85 ring(32, 52, 3 + floor(11 * u), 1 + floor(3 * u), u < 0.5 and WH or WL, a) end local u = (t % 40) / 40 ring(32, 38, 1 + floor(5 * u), 1, WL, 0.8 * (1 - u)) end local function jet(t, n) local h = 4 + n + floor(1.5 * sin(t * 0.2) + 0.5) rect(32, 20 - h, 1, h, 250, 252, 255) blend_rect(31, 21 - h, 3, 2, 150, 230, 250, 0.8) px(31, 20 - h + 3, 150, 230, 250); px(33, 20 - h + 3, 150, 230, 250) px(30, 20 - h + 6, 150, 230, 250); px(34, 20 - h + 6, 150, 230, 250) end local function sparkle(t) for k = 0, 5 do local h = hash32((t // 4) * 7 + k * 131) local a = 1 - ((t % 4) / 4) blend(20 + umod(h, 25), 50 + umod(h >> 8, 5), 255, 255, 255, 0.8 * a) end end local function butterfly(t) local x = 32 + 20 * sin(t * 0.021) + 6 * sin(t * 0.07) local y = 46 + 8 * sin(t * 0.033 + 1) + 3 * sin(t * 0.11) local xi, yi = floor(x + 0.5), floor(y + 0.5) if (t // 3) % 2 == 0 then rect(xi - 1, yi, 3, 1, 255, 220, 80) px(xi, yi + 1, 60, 40, 20) else rect(xi, yi - 1, 1, 2, 255, 220, 80) px(xi, yi + 1, 60, 40, 20) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local n = cfg.flow or 3 jet(t, n) falls(FALLS_HI, t / 24, min(4, 1 + n // 2 + 1), 1) falls(FALLS_LO, t / 30, min(8, 2 + n), 1) ripples(t, n) sparkle(t) butterfly(t) end