info = { type = "image", title = "Tree on the hill", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A lone tree sways in the wind at sunset", api = 44, } local tr = ({ sk = { ["Wind"] = "Vietor", ["Falling leaves"] = "Padajúce lístie", ["Clouds"] = "Oblaky" }, cs = { ["Wind"] = "Vítr", ["Falling leaves"] = "Padající listí", ["Clouds"] = "Mraky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "wind", label = T("Wind"), type = "int", min = 1, max = 5, default = 3 }, { key = "leaves", label = T("Falling leaves"), type = "int", min = 0, max = 10, default = 5 }, { key = "clouds", label = T("Clouds"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local function rgb2hsv(r, g, b) local mx, mn = math.max(r, g, b), math.min(r, g, b) local d = mx - mn local h = 0 if d > 0 then if mx == r then h = ((g - b) / d) % 6 elseif mx == g then h = (b - r) / d + 2 else h = (r - g) / d + 4 end h = h * 60 end return h, mx > 0 and d / mx or 0, mx end local function hsv2rgb(h, s, v) h = (h % 360) / 60 local i = floor(h) local f = h - i local p, q, u = v * (1 - s), v * (1 - s * f), v * (1 - s * (1 - f)) local r, g, b if i == 0 then r, g, b = v, u, p elseif i == 1 then r, g, b = q, v, p elseif i == 2 then r, g, b = p, v, u elseif i == 3 then r, g, b = p, q, v elseif i == 4 then r, g, b = u, p, v else r, g, b = v, p, q end return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function hmix(a, b, t) local h1, s1, v1 = rgb2hsv(a[1], a[2], a[3]) local h2, s2, v2 = rgb2hsv(b[1], b[2], b[3]) local dh = (h2 - h1 + 180) % 360 - 180 return hsv2rgb(h1 + dh * t, s1 + (s2 - s1) * t, v1 + (v2 - v1) * t) end local STOPS = { { 0, { 92, 74, 164 } }, { 15, { 198, 96, 152 } }, { 29, { 248, 140, 100 } }, { 44, { 255, 206, 122 } } } local SKY_B = 50 local SKY = {} for y = 0, SKY_B - 1 do local k = 1 while k < #STOPS - 1 and y > STOPS[k + 1][1] do k = k + 1 end local a, b = STOPS[k], STOPS[k + 1] local t = math.min(1, math.max(0, (y - a[1]) / (b[1] - a[1]))) local r, g, bb = hmix(a[2], b[2], t) SKY[y] = { r, g, bb } end local SUN, SUN_R = { 255, 248, 214 }, { 255, 228, 156 } local SX, SY, SR = 47, 41, 7 local FAR, FAR_L = { 156, 86, 124 }, { 196, 116, 132 } local HILL, HILL_D, HILL_L = { 80, 124, 62 }, { 62, 100, 50 }, { 100, 142, 66 } local RIM, RIM_D = { 196, 186, 92 }, { 128, 146, 70 } local FH, HY = {}, {} for x = 0, W - 1 do FH[x] = floor(41 + 2.5 * sin(x * 0.09 + 0.5) + 1.5 * sin(x * 0.23) + 0.5) HY[x] = floor(42 + 0.006 * (x - 22) * (x - 22) + 0.8 * sin(x * 0.4) + 0.5) end local PAL = { 0, 0, 0, 44, 94, 58, 66, 126, 62, 118, 162, 70, 200, 192, 98, 84, 54, 42, 112, 72, 52, 152, 104, 70 } local BLOBS = { { 22, 19, 9.5 }, { 14, 23, 6 }, { 30, 22, 6.5 }, { 19, 11, 6 }, { 27, 13, 5.5 } } local TX0, TY0, TX1, TY1 = 6, 3, 39, 43 local RIGID = 37 local ROWS, C, BEST, BN = nil, nil, nil, nil local function tree_a() C, BEST, BN = {}, {}, {} local function set(x, y, c) C[y * 64 + x] = c end for y = 26, TY1 do local x0 = y >= 36 and 20 or 21 for x = x0, 23 do set(x, y, x == x0 and 5 or (x == 23 and 7 or 6)) end end set(19, 43, 5); set(24, 43, 6); set(19, 42, 5) for k = 0, 4 do set(20 - k, 28 - k, 5); set(24 + k, 27 - k, 6) end local LX, LY = 0.82, -0.57 for i = 1, #BLOBS do local b = BLOBS[i] local bx, by, r = b[1], b[2], b[3] for y = math.ceil(by - r), floor(by + r) do for x = math.ceil(bx - r), floor(bx + r) do local dx, dy = x - bx, y - by local d2 = dx * dx + dy * dy if d2 <= r * r then local k = y * 64 + x local depth = 1 - sqrt(d2) / r if depth > (BEST[k] or -1) then BEST[k], BN[k] = depth, (dx * LX + dy * LY) / r end end end end end end local function tree_b() ROWS = {} for k, best in pairs(BEST) do local x, y = k % 64, k // 64 local bn = BN[k] local nz = umod(hash32((x // 2) * 0x9E3779B1 + (y // 2) * 0x85EBCA6B), 100) / 100 - 0.5 local v = bn + nz * 0.7 local c = 2 if v > 0.38 then c = 3 elseif v < -0.3 then c = 1 end if best < 0.16 and bn > 0.45 then c = 4 end C[k] = c end for y = TY0, TY1 do local L, R = nil, nil for x = TX0, TX1 do if (C[y * 64 + x] or 0) > 0 then L = L or x; R = x end end if L then local s = {} for x = L, R do s[#s + 1] = C[y * 64 + x] or 0 end ROWS[y] = { L, R, string.char(table.unpack(s)), R - L + 1, C[y * 64 + R] } end end C, BEST, BN = nil, nil, nil end local BG = nil local function build_bg() for y = 0, SKY_B - 1 do rect(0, y, W, 1, c3(SKY[y])) end for y = SY - SR - 2, SY + SR + 2 do local dy = y - SY local r2 = (SR + 1.6) * (SR + 1.6) - dy * dy if r2 >= 0 then local hw = floor(sqrt(r2) + 0.5) rect(SX - hw, y, 2 * hw + 1, 1, c3(SUN_R)) end local r1 = (SR + 0.4) * (SR + 0.4) - dy * dy if r1 >= 0 then local hw = floor(sqrt(r1) + 0.5) rect(SX - hw, y, 2 * hw + 1, 1, c3(SUN)) end end for x = 0, W - 1 do rect(x, FH[x], 1, H - FH[x], c3(FAR)) px(x, FH[x], c3(FAR_L)) end for x = 0, W - 1 do local y = HY[x] rect(x, y, 1, H - y, c3(HILL)) px(x, y, c3(x > 20 and RIM or RIM_D)) end for i = 1, 70 do local h = hash32(i * 0x9E3779B1 + 5) local x = umod(h, W) local y = HY[x] + 2 + umod(h >> 8, H - HY[x]) local c = umod(h >> 16, 3) == 0 and HILL_L or HILL_D rect(x, y, 1 + umod(h >> 20, 2), 1, c3(c)) if umod(h >> 24, 2) == 0 then px(x, y - 1, c3(c)) end end for y = RIGID, TY1 do local r = ROWS[y] if r then gfx.blit_indexed(r[3], PAL, r[1], y, r[4], 1, r[4], 1, true) end end BG = gfx.snapshot(0, 0, W, H) end local W1, W2, WG = TAU / (3.2 * FPS), TAU / (1.7 * FPS), TAU / (11 * FPS) local function sway(t, amp) local g = 0.7 + 0.3 * sin(WG * t) return amp * (0.4 + g * (0.65 * sin(W1 * t) + 0.35 * sin(W2 * t + 1.3))) end local KY = {} for y = TY0, RIGID - 1 do local k = (RIGID - y) / (RIGID - TY0) KY[y] = k * sqrt(k) end local function tree(t, amp) local S = sway(t, amp) for y = TY0, RIGID - 1 do local r = ROWS[y] if r then local dx = S * KY[y] local ix = floor(dx) local f = dx - ix local L = r[1] + ix gfx.blit_indexed(r[3], PAL, L, y, r[4], 1, r[4], 1, true) if f > 0.04 then local s = SKY[y] blend_rect(L, y, 1, 1, s[1], s[2], s[3], f) local c = r[5] * 3 blend_rect(r[2] + ix + 1, y, 1, 1, PAL[c + 1], PAL[c + 2], PAL[c + 3], f) end end end return S end local CLOUDS = { { 5, 18, 1.4, 6 }, { 13, 12, 0.9, 40 }, { 24, 16, 1.1, 22 } } local CTOP, CMID, CBOT = { 232, 148, 176 }, { 246, 176, 168 }, { 255, 212, 160 } local CROW = { { 0, 3, -7, CTOP }, { 1, 0, 0, CMID }, { 2, 2, -4, CBOT } } local function clouds(t) for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[2] local x = (c[4] + t * c[3] / FPS) % (W + L + 8) - L - 4 local ix = floor(x) local f = x - ix for k = 1, 3 do local R = CROW[k] local y, x0, w, col = c[1] + R[1], ix + R[2], L + R[3], R[4] rect(x0, y, w, 1, c3(col)) if f > 0.04 then local s = SKY[y] blend_rect(x0, y, 1, 1, s[1], s[2], s[3], f) blend_rect(x0 + w, y, 1, 1, col[1], col[2], col[3], f) end end end end local LEAFC = { { 186, 180, 70 }, { 232, 150, 62 }, { 210, 196, 84 } } local function leaves(t, n, S) for i = 1, n do local h = hash32(i * 0x85EBCA6B + 3) local P = (5 + umod(h, 5)) * FPS local s = (t + umod(h >> 4, P)) % P local x0 = 12 + umod(h >> 8, 22) local y0 = 12 + umod(h >> 12, 16) local x = x0 + 1.8 * s / FPS + 2.2 * sin(2.7 * s / FPS + i) local y = y0 + 6.6 * s / FPS local yl = HY[floor(math.min(W - 1, math.max(0, x)))] local a = 1 if y >= yl - 1 then local sl = (yl - 1 - y0) / 6.6 * FPS x = x0 + 1.8 * sl / FPS + 2.2 * sin(2.7 * sl / FPS + i) y = yl - 1 a = math.min(1, (P - s) / FPS) end if x < W then local c = LEAFC[1 + umod(h >> 16, 3)] local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend_rect(ix, iy, 1, 1, c[1], c[2], c[3], a * (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, 1, 1, c[1], c[2], c[3], a * fx * (1 - fy)) blend_rect(ix, iy + 1, 1, 1, c[1], c[2], c[3], a * (1 - fx) * fy) blend_rect(ix + 1, iy + 1, 1, 1, c[1], c[2], c[3], a * fx * fy) end end end function render() if not ROWS then if not C then tree_a() else tree_b() end for y = 0, SKY_B - 1 do rect(0, y, W, 1, c3(SKY[y])) end rect(0, SKY_B, W, H - SKY_B, c3(HILL)) gfx.ready(false) return end if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame if cfg.clouds ~= false then clouds(t) end local amp = 0.4 + 0.35 * (cfg.wind or 3) local S = tree(t, amp) leaves(t, cfg.leaves or 5, S) end