info = { type = "image", title = "Orchard in wind", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Apple and pear trees sway, fruit drops, rolls", api = 41, } local ltr = ({ sk = { ["Wind"] = "Vietor", ["Season"] = "Ročné obdobie", ["summer"] = "leto", ["autumn"] = "jeseň", ["Falling fruit"] = "Padajúce ovocie", ["Bird"] = "Vták" }, cs = { ["Wind"] = "Vítr", ["Season"] = "Roční období", ["summer"] = "léto", ["autumn"] = "podzim", ["Falling fruit"] = "Padající ovoce", ["Bird"] = "Pták" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "wind", label = LT("Wind"), type = "int", min = 0, max = 5, default = 2 }, { key = "season", label = LT("Season"), type = "int", default = 0, opts = { LT("summer"), LT("autumn") } }, { key = "drops", label = LT("Falling fruit"), type = "bool", default = true }, { key = "bird", label = LT("Bird"), type = "bool", default = true }, } local rect, px, blend, brect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, exp = math.floor, math.sin, math.cos, math.sqrt, math.exp local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function lerp(a, b, t) return a + (b - a) * t end local function span(xa, xb, y, r, g, b) if xb <= xa then return end local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end local s0 = ia if xa > ia then blend(ia, y, r, g, b, ia + 1 - xa); s0 = ia + 1 end if ib > s0 then rect(s0, y, ib - s0, 1, r, g, b) end if xb > ib then blend(ib, y, r, g, b, xb - ib) end end local function dotf(X, Y, r, g, b, a) local fx, fy = X - 0.5, Y - 0.5 local ix, iy = floor(fx), floor(fy) fx, fy = fx - ix, fy - iy local w = (1 - fx) * (1 - fy) * a; if w > 0.05 then blend(ix, iy, r, g, b, w) end w = fx * (1 - fy) * a; if w > 0.05 then blend(ix + 1, iy, r, g, b, w) end w = (1 - fx) * fy * a; if w > 0.05 then blend(ix, iy + 1, r, g, b, w) end w = fx * fy * a; if w > 0.05 then blend(ix + 1, iy + 1, r, g, b, w) end end local WIND, DROPS_ON, BIRD_ON, AUTUMN = 2, true, true, false local TREES = {} local BG = nil local function lawn_y(x) return 57 + 0.8 * sin(x * 0.2) end local function flat(t, list) local X, Y, Hh, S = {}, {}, {}, {} for i = 1, #list do local b = list[i] local r = b[3] for yy = -r, r do local y = floor(b[2]) + yy local q = 1 - (yy / (r + 0.5)) * (yy / (r + 0.5)) if q > 0 then local u = y < t.base and (t.base - y) * t.inv or 0 X[#X + 1], Y[#Y + 1], Hh[#Hh + 1], S[#S + 1] = b[1], y, (r + 1) * sqrt(q), u * sqrt(u) end end end return { X, Y, Hh, S, #X } end local function make_tree(cx, cy, rx, ry, base, seed, fruits, ftype) local t = { cx = cx, cy = cy, rx = rx, ry = ry, base = base, ph = seed * 1.7, fruits = fruits, ftype = ftype } t.y0, t.y1 = floor(cy - ry), floor(cy + ry) t.inv = 1 / (base - t.y0) t.hw, t.col, t.sk = {}, {}, {} for y = t.y0, t.y1 do local dy = y + 0.5 - cy local q = 1 - (dy / ry) * (dy / ry) local hw = q > 0 and rx * sqrt(q) or 0 hw = hw + (((hash32(y * 0x9E3779B1 + seed) >> 5) & 7) - 3.5) * 0.3 t.hw[y] = hw > 0.5 and hw or 0.5 t.col[y] = {} local u = y < base and (base - y) * t.inv or 0 t.sk[y] = u * sqrt(u) end t.bl, t.bd = {}, {} local i, got = 0, 0 while got < 8 and i < 80 do i = i + 1 local h = hash32((i + seed * 17) * 0x85EBCA6B) local dx = ((h & 255) / 255 - 0.5) * 1.5 * rx local dy = (((h >> 8) & 255) / 255 - 0.5) * 1.5 * ry if (dx / rx) * (dx / rx) + (dy / ry) * (dy / ry) < 0.55 then got = got + 1 local b = { cx + dx, cy + dy, 2 + ((h >> 16) & 1) } if dx + dy * 0.5 < 0 then t.bl[#t.bl + 1] = b else t.bd[#t.bd + 1] = b end end end t.fd, t.fl = flat(t, t.bd), flat(t, t.bl) return t end local APPLES = { { 8, 18 }, { 14, 14 }, { 21, 17 }, { 10, 25 }, { 18, 26 }, { 24, 23 }, { 5, 23 } } local PEARS = { { 43, 9 }, { 50, 13 }, { 44, 19 }, { 52, 22 }, { 46, 28 }, { 41, 24 } } local NF = #APPLES + #PEARS local FR = { apple = { 214, 40, 44 }, applehl = { 255, 130, 120 }, pear = { 206, 206, 72 }, pearhl = { 250, 250, 160 }, } function on_settings() WIND = cfg.wind or 2 AUTUMN = (cfg.season or 0) == 1 DROPS_ON = cfg.drops ~= false BIRD_ON = cfg.bird ~= false if #TREES == 0 then TREES[1] = make_tree(15, 22, 13, 13, 35, 3, APPLES, "apple") TREES[2] = make_tree(47, 20, 10, 16, 37, 7, PEARS, "pear") end local P = AUTUMN and { { { 214, 142, 42 }, { 168, 92, 36 }, { 250, 196, 72 }, { 140, 70, 30 } }, { { 190, 150, 48 }, { 148, 104, 40 }, { 232, 204, 90 }, { 118, 80, 34 } } } or { { { 76, 160, 64 }, { 44, 118, 50 }, { 116, 198, 84 }, { 36, 100, 44 } }, { { 94, 162, 66 }, { 56, 120, 54 }, { 142, 198, 86 }, { 46, 104, 52 } } } for k = 1, 2 do local t, p = TREES[k], P[k] for y = t.y0, t.y1 do local u = (y - t.y0) / (t.y1 - t.y0) t.col[y][1] = floor(lerp(p[1][1], p[2][1], u) + 0.5) t.col[y][2] = floor(lerp(p[1][2], p[2][2], u) + 0.5) t.col[y][3] = floor(lerp(p[1][3], p[2][3], u) + 0.5) end t.cl, t.cd = p[3], p[4] end end local function build_bg() for y = 0, 39 do local t = y / 39 rect(0, y, W, 1, floor(lerp(116, 232, t) + 0.5), floor(lerp(194, 244, t) + 0.5), floor(lerp(244, 226, t) + 0.5)) end for x = 0, W - 1 do local t1 = floor(36 + 3 * sin(x * 0.09 + 1) + 2 * sin(x * 0.21)) rect(x, t1, 1, H - t1, 156, 204, 156) local t2 = floor(43 + 2.5 * sin(x * 0.07 + 3) + 1.5 * sin(x * 0.19 + 1)) rect(x, t2, 1, H - t2, 112, 176, 100) local t3 = floor(50 + 1.4 * sin(x * 0.13)) rect(x, t3, 1, H - t3, 86, 160, 70) end rect(0, 56, W, 8, 70, 142, 60) for k = 1, 2 do local cx = ({ 15, 47 })[k] brect(cx - 10, 54, 20, 1, 20, 40, 20, 0.22); brect(cx - 12, 55, 24, 2, 20, 40, 20, 0.22) rect(cx - 1, 33, 3, 21, 112, 74, 40); rect(cx + 1, 33, 1, 21, 88, 56, 30) rect(cx - 2, 51, 5, 3, 112, 74, 40); rect(cx + 2, 51, 1, 3, 88, 56, 30) end local FL = { { 250, 250, 250 }, { 255, 226, 84 }, { 255, 150, 190 } } for k = 1, 22 do local h = hash32(k * 0x9E3779B1) local x, y = h & 63, 52 + ((h >> 8) % 11) local c = FL[(h >> 16) % 3 + 1] px(x, y, c[1], c[2], c[3]) px(x + 1, y + 1, 56, 126, 50) end BG = gfx.snapshot(0, 0, W, H) end local function draw_fruit(kind, x, y, a) local ix = floor(x) local f = x - ix local c, hl = FR[kind], FR[kind .. "hl"] for s = 0, 1 do local al = a * (s == 0 and 1 - f or f) if al > 0.04 then local xx = ix + s if kind == "apple" then brect(xx - 1, y, 3, 1, c[1], c[2], c[3], al); brect(xx, y - 1, 1, 3, c[1], c[2], c[3], al) brect(xx, y - 1, 1, 1, hl[1], hl[2], hl[3], al * 0.8) else brect(xx, y - 1, 1, 1, c[1], c[2], c[3], al) brect(xx - 1, y, 3, 2, c[1], c[2], c[3], al) brect(xx - 1, y, 1, 1, hl[1], hl[2], hl[3], al * 0.8) end end end end local function shf(t, y, ag) if y >= t.base then return 0 end local u = (t.base - y) * t.inv return ag * u * sqrt(u) * (1 + 0.0 * u) end local PF, GR = 7, 63 local function window(len, f) local c = data.time if c.ok then return c.unix // len, c.unix % len + c.ms / 1000 end return floor(f / len), f % len end local function pick(n) return (hash32(n * 0x9E3779B1 + 77) & 0x7FFF) % NF + 1 end local function fruit_xy(idx) if idx <= #APPLES then return 1, APPLES[idx] end return 2, PEARS[idx - #APPLES] end local function draw_trees(f) local amp = WIND * 0.5 local n, tau = -1, -1 if DROPS_ON then n, tau = window(PF, f) end local fk = n >= 0 and pick(n) or 0 for k = 1, 2 do local t = TREES[k] local g = (sin(f * 1.8 + t.ph) + 0.4 * sin(f * 3.93 + t.ph * 2.3)) * (0.6 + 0.4 * sin(f * 0.51 + t.ph * 1.1)) local ag = amp * g local hwt, colt, skt = t.hw, t.col, t.sk local cx = t.cx for y = t.y0, t.y1 do local s = ag * skt[y] local hw, c = hwt[y], colt[y] span(cx - hw + s, cx + hw + s, y, c[1], c[2], c[3]) end for pass = 1, 2 do local fl, col = pass == 1 and t.fd or t.fl, pass == 1 and t.cd or t.cl local X, Y, Hh, S = fl[1], fl[2], fl[3], fl[4] local c1, c2, c3_ = col[1], col[2], col[3] for i = 1, fl[5] do local s, hw, x = ag * S[i], Hh[i], X[i] span(x - hw + s, x + hw + s, Y[i], c1, c2, c3_) end end for i = 1, #t.fruits do local idx = (k == 1) and i or (#APPLES + i) local a = 1 if idx == fk then if tau < PF - 1 then a = 0 elseif tau < PF - 0.33 then a = (tau - (PF - 1)) / 0.67 end end if a > 0 then local p = t.fruits[i] draw_fruit(t.ftype, p[1] + shf(t, p[2], ag), p[2], a) end end end if fk > 0 and tau < PF - 0.33 then local ti, p = fruit_xy(fk) local t = TREES[ti] local frel = f - tau local g = (sin(frel * 1.8 + t.ph) + 0.4 * sin(frel * 3.93 + t.ph * 2.3)) * (0.6 + 0.4 * sin(frel * 0.51 + t.ph * 1.1)) local x0 = p[1] + shf(t, p[2], amp * g) local y0 = p[2] local hsh = hash32(n * 0x85EBCA6B + 5) local dir = ((hsh >> 3) & 1) == 1 and 1 or -1 if x0 < 14 then dir = 1 elseif x0 > 50 then dir = -1 end local vx = WIND * 0.75 local yF = lawn_y(x0) local h = yF - y0 local t1 = sqrt(2 * h / GR) local x, y, ang = x0 + vx * (tau < t1 and tau or t1), y0, 0 if tau < t1 then y = y0 + 0.5 * GR * tau * tau else local tt = tau - t1 local roll = dir * 12 * (1 - exp(-tt / 1.33)) x = x + roll ang = roll local v = GR * t1 y = yF for _ = 1, 3 do v = v * 0.38 local T = 2 * v / GR if tt < T then y = yF - (v * tt - 0.5 * GR * tt * tt); break end tt = tt - T end end local a = 1 if tau > PF - 1.67 then a = (PF - 0.33 - tau) / 1.33 end x = x < 2 and 2 or (x > 61 and 61 or x) local kind = t.ftype draw_fruit(kind, x, floor(y + 0.5), a) local hl = FR[kind .. "hl"] px(floor(x + 0.5 + cos(ang * 0.7) * 1.0), floor(y + 0.5 + sin(ang * 0.7) * 1.0), hl[1], hl[2], hl[3]) end end local PB = 20 local function draw_bird(f) local n, ph = window(PB, f) local dir = (n % 2 == 0) and 1 or -1 local x = dir == 1 and (-8 + 9.6 * (ph - 0.17)) or (72 - 9.6 * (ph - 0.17)) if x < -6 or x > 70 then return end local y = 13 + 3 * sin(f * 1.35) + (n % 3) * 2 local st = floor(f * 7.5) % 4 local wy = (st == 0 and -1) or (st == 2 and 1) or 0 dotf(x, y, 26, 28, 50, 1); dotf(x - dir, y, 26, 28, 50, 1); dotf(x + dir, y, 26, 28, 50, 1) dotf(x, y + 1, 224, 96, 70, 0.9) dotf(x + dir, y + 1, 26, 28, 50, 0.8) dotf(x - 2 * dir, y + wy * 0.5, 26, 28, 50, 0.85) dotf(x - 3 * dir, y + wy, 26, 28, 50, 0.7) dotf(x + 2 * dir, y + 0.5, 26, 28, 50, 0.8) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame / gfx.FPS draw_trees(f) if BIRD_ON then draw_bird(f) end end