info = { type = "image", title = "Forest glade", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sunbeams, dust and a glittering river", api = 44, } local tr = ({ sk = { ["Season"] = "Ročné obdobie", ["Summer"] = "Leto", ["Autumn"] = "Jeseň" }, cs = { ["Season"] = "Roční období", ["Summer"] = "Léto", ["Autumn"] = "Podzim" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "season", label = T("Season"), type = "int", default = "summer", opts = { keys = { "summer", "autumn" }, T("Summer"), T("Autumn") } }, } local rect, blend_rect, blend = gfx.rect, gfx.blend_rect, gfx.blend local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32 = math.hash32 local SEASONS = { summer = { sky_t = { 138, 188, 230 }, sky_b = { 236, 242, 224 }, haze = { 240, 244, 226 }, far = { 120, 164, 140 }, mid = { 80, 128, 92 }, gfar = { 160, 200, 108 }, gnear = { 62, 112, 48 }, glit = { 196, 224, 124 }, gdark = { 30, 70, 36 }, lm = { { 38, 86, 40 }, { 52, 100, 44 } }, ld = { 20, 48, 26 }, ll = { 92, 150, 56 }, lh = { 176, 210, 88 }, ray = { 255, 246, 208 }, shade = { 24, 60, 34 }, spot = { { 255, 255, 255 }, { 250, 230, 90 }, { 240, 150, 200 } }, }, autumn = { sky_t = { 150, 188, 220 }, sky_b = { 244, 238, 214 }, haze = { 248, 238, 208 }, far = { 176, 148, 106 }, mid = { 128, 98, 66 }, gfar = { 192, 172, 96 }, gnear = { 108, 90, 40 }, glit = { 226, 196, 110 }, gdark = { 58, 46, 26 }, lm = { { 150, 66, 24 }, { 190, 110, 30 } }, ld = { 64, 26, 18 }, ll = { 220, 140, 36 }, lh = { 252, 206, 76 }, ray = { 255, 226, 160 }, shade = { 50, 38, 24 }, spot = { { 200, 60, 30 }, { 236, 150, 40 }, { 250, 210, 80 } }, }, } local P local function rr(i, s) return (hash32(i * 0x9E3779B1 + s * 0x85EBCA6B) & 0xFFFF) / 65536 end local function mix(a, b, t) 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 local function dab(x, y, w, h, c, a) blend_rect(floor(x), floor(y), w, h, c[1], c[2], c[3], a) end local RIVER = {} for x = 0, 63 do RIVER[x] = floor(36 + 2.5 * sin(x * 0.13 + 0.6) + 0.5) end local function river_y(x) return RIVER[x] end local STOPS = { { 0, 36, 28, 24 }, { 0.6, 70, 52, 38 }, { 0.9, 112, 86, 58 }, { 1, 170, 140, 96 } } local function trunk_col(f) for i = 2, 4 do local a, b = STOPS[i - 1], STOPS[i] if f <= b[1] then local t = (f - a[1]) / (b[1] - a[1]) return floor(a[2] + (b[2] - a[2]) * t), floor(a[3] + (b[3] - a[3]) * t), floor(a[4] + (b[4] - a[4]) * t) end end return 170, 140, 96 end local function trunk(x0, w, y0, y1, lit_right, seed, haze) for i = 0, w - 1 do local f = w == 1 and 0.5 or i / (w - 1) if not lit_right then f = 1 - f end local r, g, b = trunk_col(f) if haze then r, g, b = mix({ r, g, b }, P.haze, haze) end rect(x0 + i, y0, 1, y1 - y0, r, g, b) end for k = 1, w * 5 do local x, y = x0 + floor(rr(k, seed) * w), y0 + rr(k, seed + 1) * (y1 - y0 - 8) local len = 3 + floor(rr(k, seed + 2) * 7) local light = rr(k, seed + 3) > 0.6 blend_rect(x, floor(y), 1, len, light and 150 or 20, light and 118 or 16, light and 80 or 14, haze and 0.15 or 0.3) end end local function canopy(side, k0, p0, p1) local passes = { { P.ld, 110, 6, 4, 0.85, 99 }, { P.lm, 100, 5, 3, 0.7, 1.9 }, { P.ll, 60, 3, 2, 0.7, 1.5 }, { P.lh, 30, 2, 1, 0.85, 1.32 }, } local glints = {} for pi = p0, p1 do local ps = passes[pi] for i = 1, ps[2] do local h = hash32((i + k0) * 0x9E3779B1 + pi * 0x85EBCA6B) local y = (h & 0xFF) % 34 local gy = y / 26 local lo = y < 26 and 11 * sqrt(1 - gy * gy) or 0 local lim = 9 + (y - 6) / 1.5 if lim > lo then lo = lim end local hi = 32 local g = ps[6] - gy * gy if g > 0 and ps[6] < 50 then hi = 11 * sqrt(g) end if hi > lo + 0.5 then local adx = lo + ((h >> 8) & 0xFFFF) / 65536 * (hi - lo) if adx > 31 then adx = 31 end local x = floor(side == 0 and 32 - adx or 32 + adx) local c = ps[1][1] and type(ps[1][1]) == "table" and ps[1][(i % 2) + 1] or ps[1] dab(x, y, ps[3], ps[4], c, ps[5]) if pi == 4 and #glints < 6 then glints[#glints + 1] = { x, y } end end end end return glints end local function bush(cx, w) for i = 1, 90 do local h = hash32(i * 0x9E3779B1 + 41 * 0x85EBCA6B) local x, y = cx + ((h & 0xFF) / 256 - 0.5) * w, 52 + (((h >> 8) & 0xFF) / 256 - 0.2) * 14 local d = (x - cx) / (w / 2) if y < 63 - d * d * 4 then local k = ((h >> 16) & 0xFF) / 256 dab(x, y, 3, 2, k < 0.45 and P.ld or (k < 0.85 and P.lm[1] or P.ll), 0.8) end end end local BG, STAGE = nil, 0 local SITES, GLINTS = {}, {} local MASKS = {} local RAYS = { { 12, 38 }, { 22, 46 }, { 30, 54 } } local PROF = { 6, 30, 80, 140, 170, 140, 80, 30, 6 } local function ray_mask(us) local rows = {} for y = 0, 55 do local fade = 1 - y / 62 local parts, pos = {}, 0 for _, u in ipairs(us) do local xs = floor(u + 0.45 * y) - 4 local lo, hi = xs < pos and pos or xs, xs + 8 > 63 and 63 or xs + 8 if hi >= lo then local v = {} for i = lo, hi do v[#v + 1] = floor(PROF[i - xs + 1] * fade) end parts[#parts + 1] = string.rep("\0", lo - pos) .. string.char(table.unpack(v)) pos = hi + 1 end end parts[#parts + 1] = string.rep("\0", 64 - pos) rows[#rows + 1] = table.concat(parts) end return table.concat(rows) end local function grass_dabs(i0, i1) for i = i0, i1 do local h = hash32(i * 0x9E3779B1 + 11 * 0x85EBCA6B) local x, y = h & 63, 32 + ((h >> 6) & 0xFF) % 31 local d = y - RIVER[x] if d > 3 or d < -3 then local lit = (h >> 14) & 1 == 0 dab(x, y, 1 + (y - 32) // 14, 1, lit and P.glit or P.gdark, lit and 0.6 or 0.5) end end end local function glints_add(g) for _, v in ipairs(g) do GLINTS[#GLINTS + 1] = v end end local STAGES = { function() rect(0, 0, 64, 64, P.gfar[1], P.gfar[2], P.gfar[3]) for y = 0, 30 do rect(0, y, 64, 1, mix(P.sky_t, P.sky_b, y / 30)) end for k = 1, 3 do local rx, ry = 26 - k * 6, 14 - k * 3 for y = 0, ry do local half = floor(rx * sqrt(1 - (y / ry) ^ 2)) blend_rect(32 - half, y, 2 * half, 1, 255, 255, 245, 0.14) end end for x = 0, 63 do local top = floor(24 + 2.2 * sin(x * 0.35) + 1.6 * sin(x * 0.13 + 1)) rect(x, top, 1, 32 - top, P.far[1], P.far[2], P.far[3]) top = floor(27.5 + 1.5 * sin(x * 0.5 + 2) + sin(x * 0.21)) rect(x, top, 1, 32 - top, P.mid[1], P.mid[2], P.mid[3]) end end, function() for y = 31, 63 do rect(0, y, 64, 1, mix(P.gfar, P.gnear, (y - 31) / 32)) end for x = 0, 63 do local yc = river_y(x) rect(x, yc - 2, 1, 1, 88, 128, 120) rect(x, yc - 1, 1, 1, 210, 230, 232) rect(x, yc, 1, 1, 186, 214, 224) rect(x, yc + 1, 1, 1, 150, 190, 206) rect(x, yc + 2, 1, 1, 120, 160, 140) end for y = 42, 63 do local xc = 36 - (y - 41) * 0.15 + 3 * sin((y - 41) * 0.15) local w = floor(2 + (y - 41) * 0.22) rect(floor(xc - w / 2), y, w, 1, 176, 146, 98) end for i = 1, 14 do local x = 14 + floor(rr(i, 5) * 38) SITES[i] = { x, river_y(x) + floor(rr(i, 6) * 3) - 1, 0.04 + rr(i, 7) * 0.05, rr(i, 8) * 6.28 } end end, function() grass_dabs(1, 115) end, function() grass_dabs(116, 230) end, function() for i = 1, 16 do local x, y = floor(rr(i, 21) * 62) + 1, 46 + floor(rr(i, 22) * 16) local c = P.spot[i % 3 + 1] rect(x, y, 1, 1, c[1], c[2], c[3]) end for _, s in ipairs({ { 6, 5 }, { 15, 3 }, { 48, 3 }, { 58, 6 } }) do for y = 36, 63 do blend_rect(floor(s[1] + (y - 36) * (s[1] - 32) / 40), y, s[2], 1, P.shade[1], P.shade[2], P.shade[3], 0.3) end end end, function() trunk(21, 2, 0, 33, true, 31, 0.5); trunk(43, 2, 0, 33, false, 32, 0.5) trunk(13, 4, 0, 37, true, 33, 0.18); trunk(47, 3, 0, 37, false, 34, 0.18) end, function() GLINTS = canopy(0, 0, 1, 2) end, function() glints_add(canopy(0, 0, 3, 4)); bush(18, 22) end, function() canopy(1, 500, 1, 2) end, function() glints_add(canopy(1, 500, 3, 4)); bush(46, 18) end, function() trunk(0, 11, 0, 64, true, 51, nil) end, function() trunk(54, 10, 0, 64, false, 52, nil) for y = 52, 63 do blend_rect(0, y, 64, 1, 10, 24, 12, (y - 51) / 12 * 0.35) end end, function() MASKS[1] = ray_mask(RAYS[1]) end, function() MASKS[2] = ray_mask(RAYS[2]) end, function() MASKS[3] = ray_mask(RAYS[3]) end, } function on_settings() P = SEASONS[cfg.season] or SEASONS.summer STAGE, BG = 0, nil end local MOTES = {} for i = 1, 14 do MOTES[i] = { u = RAYS[i % 3 + 1][i % 2 + 1], off = (rr(i, 61) - 0.5) * 8, y0 = rr(i, 62) * 52, vy = 0.015 + rr(i, 63) * 0.03, wx = 0.01 + rr(i, 64) * 0.02, ph = rr(i, 65) * 6.28, wa = 0.02 + rr(i, 66) * 0.03 } end local WINGS = { { -2, -1, -1, 0, 0, 0, 1, 0, 2, -1 }, { -2, 0, -1, 0, 0, 0, 1, 0, 2, 0 }, { -2, 1, -1, 0, 0, 0, 1, 0, 2, 1 } } local function bilinear(x, y, r, g, b, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, r, g, b, a * (1 - fx) * (1 - fy)); blend(ix + 1, iy, r, g, b, a * fx * (1 - fy)) blend(ix, iy + 1, r, g, b, a * (1 - fx) * fy); blend(ix + 1, iy + 1, r, g, b, a * fx * fy) end function render() if STAGE < #STAGES then if STAGE > 0 then gfx.blit_rgb(BG, 0, 0, 64, 64) end STAGES[STAGE + 1]() STAGE = STAGE + 1 BG = gfx.snapshot(0, 0, 64, 64) gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, 64, 64) local f = frame local ray = P.ray local u, dirn = f % 330, (f // 330) % 2 if u < 100 then local bx = dirn == 0 and 46 - u * 0.27 or 20 + u * 0.27 local by = 9 + 2 * sin(u * 0.07) local w = WINGS[({ 1, 2, 3, 2 })[(u // 4) % 4 + 1]] gfx.clip(20, 0, 26, 22) for i = 1, 9, 2 do local x, fx = floor(bx + w[i]), (bx + w[i]) % 1 blend(x, floor(by) + w[i + 1], 40, 42, 48, 1 - fx); blend(x + 1, floor(by) + w[i + 1], 40, 42, 48, fx) end gfx.clip() end for k = 1, 3 do gfx.blend_mask(MASKS[k], ray[1], ray[2], ray[3], 0, 0, 64, 56, 64, 56, 0.8 * (0.75 + 0.25 * sin(f * 0.013 * (1 + k * 0.37) + k * 2.1))) end local haze = P.haze blend_rect(11, 27, 43, 7, haze[1], haze[2], haze[3], 0.07 + 0.04 * sin(f * 0.01)) for i = 1, #MOTES do local m = MOTES[i] local y = (m.y0 + m.vy * f) % 52 local a = (0.25 + 0.4 * (0.5 + 0.5 * sin(f * m.wa + m.ph))) * (1 - y / 58) bilinear(m.u + 0.45 * y + m.off + 2 * sin(f * m.wx + m.ph), y, 255, 246, 214, a) end for i = 1, #SITES do local s = SITES[i] local a = sin(f * s[3] + s[4]) if a > 0.6 then a = (a - 0.6) / 0.4 blend(s[1], s[2], 255, 255, 240, a * 0.95) blend(s[1] - 1, s[2], 255, 255, 240, a * 0.35); blend(s[1] + 1, s[2], 255, 255, 240, a * 0.35) end end local lh = P.lh for i = 1, #GLINTS do local g = GLINTS[i] blend(g[1], g[2], lh[1], lh[2], lh[3], 0.6 * (0.5 + 0.5 * sin(f * (0.03 + i * 0.004) + i * 1.7))) end if P == SEASONS.autumn then for i = 1, 6 do local y = (rr(i, 71) * 60 + f * (0.1 + rr(i, 72) * 0.06)) % 60 local c = P.spot[i % 3 + 1] bilinear(14 + rr(i, 73) * 36 + 3 * sin(f * 0.03 + i), y, c[1], c[2], c[3], 0.9) end end end