info = { type = "image", title = "Lavender rows", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Lavender field swaying in the evening light", api = 44, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Noon"] = "Poludnie", ["Sunset"] = "Západ" }, cs = { ["Time of day"] = "Denní doba", ["Noon"] = "Poledne", ["Sunset"] = "Západ" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tod", label = T("Time of day"), type = "int", default = "sunset", opts = { keys = { "noon", "sunset" }, T("Noon"), T("Sunset") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local char, concat, unpack = string.char, table.concat, table.unpack local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local function clamp(v, a, b) return v < a and a or v > b and b or v end local P = { sunset = { sky = { { 0, { 70, 62, 148 } }, { 12, { 132, 86, 160 } }, { 20, { 226, 124, 128 } }, { 27, { 255, 186, 106 } } }, light = { 226, 160, 235 }, base = { 150, 100, 200 }, dark = { 84, 56, 140 }, fur = { 104, 100, 58 }, furd = { 62, 66, 40 }, haze = { 214, 142, 150 }, hill1 = { 168, 104, 148 }, hill2 = { 116, 76, 122 }, tree = { 60, 50, 72 }, rim = { 244, 160, 104 }, cloud = { 255, 176, 150 }, sunx = 44, suny = 22, }, noon = { sky = { { 0, { 64, 132, 226 } }, { 27, { 178, 216, 246 } } }, light = { 196, 154, 242 }, base = { 132, 92, 202 }, dark = { 80, 56, 150 }, fur = { 96, 136, 62 }, furd = { 56, 92, 40 }, haze = { 186, 206, 236 }, hill1 = { 150, 180, 216 }, hill2 = { 112, 152, 168 }, tree = { 52, 96, 60 }, rim = { 150, 200, 110 }, cloud = { 255, 255, 255 }, sunx = 50, suny = 8, }, } local K = P.sunset local function sky_at(y) local s = K.sky for i = 2, #s do if y <= s[i][1] then local t = (y - s[i - 1][1]) / (s[i][1] - s[i - 1][1]) local a, b = s[i - 1][2], s[i][2] return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end end local l = s[#s][2] return l[1], l[2], l[3] end local HY, VPX, PERIOD = 27, 36, 26 local FIELD = {} local function mix(a, b, t) return a + (b - a) * t end local function field_rows(ya, yb) for y = ya, yb do local z = (y - HY) / (63 - HY) local row = {} local blk = z > 0.55 and 2 or 1 local haze = (1 - z) ^ 3 for x = 0, 63 do local xb = z > 0.02 and VPX + (x - VPX) / z or 9999 local u = xb / PERIOD local idx = floor(u) local fr = u - idx local r, g, b if fr >= 0.1 and fr <= 0.92 then local sh = (fr - 0.1) / 0.82 local t = 0.1 + 0.8 * sh local hs = hash32(idx * 7919 + (x // blk) * 104729 + (y // blk) * 15485863) & 15 local c1, c2 = K.dark, K.base if t > 0.5 then c1, c2, t = K.base, K.light, (t - 0.5) * 2 else t = t * 2 end r, g, b = mix(c1[1], c2[1], t), mix(c1[2], c2[2], t), mix(c1[3], c2[3], t) if hs < 3 then r, g, b = mix(r, K.light[1], 0.6), mix(g, K.light[2], 0.6), mix(b, K.light[3], 0.6) elseif hs > 12 then r, g, b = mix(r, K.dark[1], 0.6), mix(g, K.dark[2], 0.6), mix(b, K.dark[3], 0.6) end else local c = fr < 0.1 and K.furd or K.fur r, g, b = c[1], c[2], c[3] end local hz = clamp(haze * 1.1, 0, 0.9) r, g, b = mix(r, K.haze[1], hz), mix(g, K.haze[2], hz), mix(b, K.haze[3], hz) local k = x * 3 row[k + 1], row[k + 2], row[k + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end FIELD[y - HY] = char(unpack(row, 1, 192)) end end local function disc(cx, cy, r, cr, cg, cb, a) for dy = -r, r do local w = floor(sqrt(r * r - dy * dy) + 0.5) if a >= 1 then rect(cx - w, cy + dy, 2 * w + 1, 1, cr, cg, cb) else blend_rect(cx - w, cy + dy, 2 * w + 1, 1, cr, cg, cb, a) end end end local function hill_y(x, a, b, c) return floor(a + b * sin(x * 0.09 + c) + 0.5 * sin(x * 0.23 + c * 2)) end local function tree(x, y, s) local c = K.tree rect(x, y - 4 * s, 1, 4 * s, c3(c)) local rx, ry = 4.5 * s, 3.5 * s for dy = -floor(ry), floor(ry) do local w = floor(rx * sqrt(1 - (dy / (ry + 0.5)) ^ 2) + 0.5) local yy = y - 4 * s - 2 * s + dy rect(x - w, yy, 2 * w + 1, 1, c3(c)) px(x + w, yy, c3(K.rim)) end end local BG, stage = nil, 0 local function compose() for y = 0, 27 do rect(0, y, 64, 1, sky_at(y)) end local sx, sy = K.sunx, K.suny if K == P.sunset then for k = 16, 6, -1 do disc(sx, sy, k, 255, 200, 120, 0.07) end disc(sx, sy, 5, 255, 232, 160, 1); disc(sx, sy, 4, 255, 248, 214, 1) else for k = 8, 4, -2 do disc(sx, sy, k, 255, 255, 255, 0.12) end disc(sx, sy, 3, 255, 255, 238, 1) end for x = 0, 63 do local t = hill_y(x, 25, 1.8, 1) rect(x, t, 1, 4, c3(K.hill1)) local t2 = hill_y(x, 27, 1.2, 2) rect(x, t2, 1, 2, c3(K.hill2)) end tree(13, hill_y(13, 27, 1.2, 2) + 1, 1) tree(55, hill_y(55, 27, 1.2, 2) + 1, 1) gfx.blit_rgb(concat(FIELD), 0, HY + 1, 64, 36) BG = gfx.snapshot(0, 0, 64, 64) FIELD = {} end local function build_step() stage = stage + 1 if stage <= 9 then field_rows(HY + 1 + (stage - 1) * 4, HY + stage * 4) else compose() end end local SP = {} for i = 1, 20 do local h = hash32(i * 0x9E3779B1) local y = 44 + umod(h, 19) local z = (y - HY) / (63 - HY) local k = umod(h >> 6, 5) - 1 SP[i] = { x = VPX + (k + 0.15 + umod(h >> 10, 70) / 100) * PERIOD * z - 2, y = y, hgt = 3 + floor(z * 6), ph = umod(h >> 16, 628) / 100 } end function on_settings() K = P[cfg.tod] or P.sunset BG, stage, FIELD = nil, 0, {} end local function hspan(x, y, w, h, c, a) local ix = floor(x) local f = x - ix blend_rect(ix + 1, y, w - 1, h, c[1], c[2], c[3], a) blend_rect(ix, y, 1, h, c[1], c[2], c[3], a * (1 - f)) blend_rect(ix + w, y, 1, h, c[1], c[2], c[3], a * f) end local CL = { { 6, 16, 0.08, 10 }, { 13, 12, -0.05, 40 }, { 4, 20, 0.12, 70 } } function render() if not BG then build_step() if not BG then gfx.clear(0, 0, 0); gfx.ready(false) return end end gfx.blit_rgb(BG, 0, 0, 64, 64) local t = frame for i = 1, #CL do local c = CL[i] local x = (c[4] + t * c[3]) % (64 + c[2]) - c[2] hspan(x + 3, c[1], c[2] - 8, 1, K.cloud, 0.55) hspan(x, c[1] + 1, c[2], 2, K.cloud, 0.5) hspan(x + 5, c[1] + 3, c[2] - 10, 1, K.cloud, 0.3) end for i = 1, #SP do local s = SP[i] local w = sin(t * TAU / 160 - s.x * 0.09 + s.ph * 0.2) local off = floor(w * 1.6 + 0.5) local x, y, h = floor(s.x), s.y, s.hgt rect(x, y - h // 2, 1, h // 2 + 1, 70, 104, 56) rect(x + off // 2, y - h, 1, h - h // 2, 80, 116, 62) local hd = 2 + (h > 6 and 1 or 0) rect(x + off - 1, y - h - hd, 3, hd, c3(K.base)) rect(x + off - 1, y - h - hd, 1, hd, c3(K.light)) px(x + off, y - h - hd - 1, c3(K.light)) end local u = t % 700 if u < 240 then local bx = -6 + u * 0.32 local by = 50 + 5 * sin(u * TAU / 55) + 4 * sin(u * TAU / 130) local ix, iy = floor(bx), floor(by) rect(ix, iy, 3, 2, 250, 200, 30); px(ix + 1, iy, 40, 30, 20); px(ix + 2, iy + 1, 40, 30, 20) px(ix + 3, iy, 30, 24, 20) local up = (t // 2) % 2 == 0 blend_rect(ix, iy - (up and 2 or 1), 2, 1, 255, 255, 255, 0.8) end end