info = { type = "image", title = "Greenhouse window", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Snow, fogged glass and swaying plants", api = 33, } local tr = ({ sk = { ["Snowfall"] = "Sneženie", ["Fogged glass"] = "Zahmlené sklo", ["Fairy lights"] = "Svetelná reťaz" }, cs = { ["Snowfall"] = "Sněžení", ["Fogged glass"] = "Zamlžené sklo", ["Fairy lights"] = "Světelný řetěz" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "snow", label = T("Snowfall"), type = "int", min = 1, max = 10, default = 5 }, { key = "fog", label = T("Fogged glass"), type = "bool", default = true }, { key = "lights", label = T("Fairy lights"), 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 = gfx.W, gfx.H local WOOD, WOOD_L, WOOD_D = { 104, 76, 52 }, { 150, 118, 82 }, { 72, 52, 38 } local SKY0, SKY1 = { 108, 140, 164 }, { 176, 206, 218 } local HILL, PINE = { 224, 236, 246 }, { 70, 98, 108 } local PANEL, PANEL_D = { 44, 66, 62 }, { 34, 52, 50 } local POT, POT_D = { 184, 96, 62 }, { 138, 68, 46 } local LEAF_D, LEAF, LEAF_L = { 38, 96, 68 }, { 66, 136, 84 }, { 112, 172, 108 } local FOG = { 214, 228, 234 } local DROP = { 238, 246, 252 } local BULB = { 255, 214, 120 } local STRING = { 56, 64, 56 } local P1, P2, PW = 3, 33, 28 local PY0, PY1 = 3, 43 local SOIL = 39 local function c3(c) return c[1], c[2], c[3] end local BG = nil local function build_bg() rect(0, 0, W, H, c3(WOOD)) for y = PY0, PY1 do local t = (y - PY0) / (PY1 - PY0) local r, g, b = floor(SKY0[1] + (SKY1[1] - SKY0[1]) * t + 0.5), floor(SKY0[2] + (SKY1[2] - SKY0[2]) * t + 0.5), floor(SKY0[3] + (SKY1[3] - SKY0[3]) * t + 0.5) rect(P1, y, PW, 1, r, g, b) rect(P2, y, PW, 1, r, g, b) end rect(P1, 39, 58, 5, c3(HILL)); rect(P1, 38, 22, 1, c3(HILL)); rect(40, 37, 20, 1, c3(HILL)) local pines = { { 8, 9 }, { 16, 6 }, { 24, 8 }, { 38, 8 }, { 47, 11 }, { 55, 6 } } for k = 1, #pines do local x, h = pines[k][1], pines[k][2] for i = 0, h - 1 do local hw = (i * 3) // (h + 1) + (i > 0 and 1 or 0) rect(x - hw, 39 - h + i, 2 * hw + 1, 1, c3(PINE)) end end rect(P1 + PW, 0, 2, 44, c3(WOOD)) rect(P1 - 1, 2, PW + 2, 1, c3(WOOD_L)); rect(P2 - 1, 2, PW + 2, 1, c3(WOOD_L)) rect(2, 3, 1, 41, c3(WOOD_D)); rect(P2 - 1, 3, 1, 41, c3(WOOD_D)) rect(P1 + PW, 3, 1, 41, c3(WOOD_L)) rect(0, 44, W, 1, c3(WOOD_L)); rect(0, 45, W, 2, c3(WOOD)) rect(0, 47, W, H - 47, c3(PANEL)) for x = 3, W - 1, 8 do rect(x, 47, 1, H - 47, c3(PANEL_D)) end rect(0, 55, W, 1, c3(WOOD_L)); rect(0, 56, W, 2, c3(WOOD)); rect(0, 58, W, 1, c3(WOOD_D)) for k = 0, 10 do blend_rect(8 + k, 6 + k * 2, 1, 2, 255, 255, 255, 0.12) blend_rect(40 + k, 6 + k * 2, 1, 2, 255, 255, 255, 0.10) end for x = P1, 60 do local a, b = P1, 31 if x >= 32 then a, b = 32, 60 end local y = 4 + floor(3 * sin(3.14159 * (x - a) / (b - a))) px(x, y, c3(STRING)) end local function pot(x, y, w, h) rect(x, y, w, h, c3(POT)); rect(x - 1, y, w + 2, 1, c3(POT)); rect(x, y + h - 1, w, 1, c3(POT_D)) rect(x + w - 2, y + 1, 2, h - 1, c3(POT_D)) end pot(6, 52, 5, 3); pot(19, 52, 5, 3); pot(40, 52, 5, 3); pot(53, 52, 5, 3) BG = gfx.snapshot(0, 0, W, H) end local NF = 56 local FX, FV, FP, FY0, FPN = {}, {}, {}, {}, {} for i = 1, NF do local h = hash32(i * 0x9E3779B1) FX[i] = umod(h, PW) FV[i] = 4.2 + umod(h >> 6, 26) * 0.3 FP[i] = umod(h >> 12, 628) / 100.0 FY0[i] = umod(h >> 18, 48) FPN[i] = (i % 2 == 0) and P1 or P2 end local function big_pots() rect(6, 40, 8, 4, c3(POT)); rect(5, 40, 10, 1, c3(POT)); rect(6, 43, 8, 1, c3(POT_D)); rect(12, 41, 2, 2, c3(POT_D)) rect(27, 40, 9, 4, c3(POT)); rect(26, 40, 11, 1, c3(POT)); rect(27, 43, 9, 1, c3(POT_D)); rect(34, 41, 2, 2, c3(POT_D)) rect(46, 40, 8, 4, c3(POT)); rect(45, 40, 10, 1, c3(POT)); rect(46, 43, 8, 1, c3(POT_D)); rect(52, 41, 2, 2, c3(POT_D)) end local function ramp(p, a, b) if p <= a then return 0 elseif p >= b then return 1 end local t = (p - a) / (b - a) return t * t * (3 - 2 * t) end local function leaf(x, y, w, h, c) local ix = floor(x) local fx = x - ix local r, g, b = c[1], c[2], c[3] if w > 1 then rect(ix + 1, y, w - 1, h, r, g, b) end blend_rect(ix, y, 1, h, r, g, b, 1 - fx) blend_rect(ix + w, y, 1, h, r, g, b, fx) end local function bend(t, ph, dy, amp) return amp * (-dy) * sin(t * 1.35 + ph - dy * 0.06) end local function fern(cx, t) local ph = 0.4 for i = 0, 4 do local dy, w = -2 - 3 * i, 5 - i local bx = bend(t, ph, dy, 0.045) leaf(cx - w + bx, SOIL + dy, w, 1, i % 2 == 0 and LEAF or LEAF_L) leaf(cx + 1 + bx, SOIL + dy, w, 1, i % 2 == 0 and LEAF_L or LEAF) leaf(cx - w + 1 + bx, SOIL + dy + 1, w - 1, 1, LEAF_D) leaf(cx + 1 + bx, SOIL + dy + 1, w - 1, 1, LEAF_D) end leaf(cx + bend(t, ph, -14, 0.045), SOIL - 14, 1, 11, LEAF_D) end local function rubber(cx, t) local ph = 1.9 leaf(cx + 0.5 * bend(t, ph, -12, 0.04), SOIL - 24, 1, 24, LEAF_D) for i = 0, 4 do local dy = -6 - 4 * i local bx = bend(t, ph, dy, 0.04) + 0.4 * sin(t * 3.3 + i) leaf(cx - 5 + bx, SOIL + dy, 5, 3, LEAF) leaf(cx - 5 + bx, SOIL + dy, 5, 1, LEAF_L) leaf(cx + 1 + bx, SOIL + dy - 2, 5, 3, LEAF_D) leaf(cx + 1 + bx, SOIL + dy - 2, 5, 1, LEAF) end end local function succulent(cx, t) local ph = 3.1 for i = 0, 3 do local dy = -2 - i local bx = bend(t, ph, dy, 0.035) leaf(cx - 5 + i + bx, SOIL + dy, 3, 1, i % 2 == 0 and LEAF or LEAF_L) leaf(cx + 2 - i + bx, SOIL + dy, 3, 1, i % 2 == 0 and LEAF_L or LEAF) end leaf(cx - 1 + bend(t, ph, -6, 0.035), SOIL - 6, 3, 3, LEAF_L) for k = 0, 7 do local bx = 1.4 * (k / 7) * sin(t * 1.5 + 0.7 - k * 0.2) leaf(cx + 4 + bx, 41 + k, 1, 1, LEAF_D) if k % 2 == 1 then leaf(cx + 5 + bx, 41 + k, 2, 1, LEAF) end end end local function sprouts(t) local xs = { 8, 21, 42, 55 } for k = 1, 4 do local cx = xs[k] local bx = 0.6 * sin(t * 2.1 + k * 1.3) leaf(cx + bx * 0.5, 49, 1, 3, LEAF_D) leaf(cx - 2 + bx, 48, 2, 2, LEAF) leaf(cx + 1 + bx, 47, 2, 2, LEAF_L) end end local CYC = 16 local function fog_pane(x0, off, t, nk) local p = (t + off) % CYC local fa = 0.6 * ramp(p, 0.33, 3.67) * (1 - ramp(p, 9, 13.33)) if fa < 0.01 then return end local yb = PY1 + 1 - 42 * ramp(p, 0.33, 4) local dx, dy = -1, -1 local ds = 4 if p >= ds and p < 9 then local u = (p - ds) / 4.33 if u > 1 then u = 1 end dx = x0 + 4 + umod(hash32(floor((t + off) / CYC) * 0x9E3779B1 + x0 * 977), PW - 8) dy = 5 + 36 * (u + 0.05 * sin(u * 20)) end for y = PY0, PY1, 2 do local e = (y - yb) / 5 if e > 1 then e = 1 end if e > 0 then local a = fa * e * (0.55 + 0.45 * (y - PY0) / (PY1 - PY0)) if dy >= y and dx >= 0 then blend_rect(x0, y, dx - x0, 2, FOG[1], FOG[2], FOG[3], a) blend_rect(dx + 1, y, x0 + PW - dx - 1, 2, FOG[1], FOG[2], FOG[3], a) else blend_rect(x0, y, PW, 2, FOG[1], FOG[2], FOG[3], a) end end end if dx >= 0 then local iy = floor(dy) local f = dy - iy blend_rect(dx, iy, 1, 1, DROP[1], DROP[2], DROP[3], 1 - f) blend_rect(dx, iy + 1, 1, 1, DROP[1], DROP[2], DROP[3], 1) blend_rect(dx, iy + 2, 1, 1, DROP[1], DROP[2], DROP[3], f) end end local BX = { 8, 17, 26, 36, 46, 56 } local BY = { 7, 8, 7, 7, 8, 7 } function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / gfx.FPS local n = (cfg.snow or 5) * 5 + 6 if n > NF then n = NF end for i = 1, n do local y = (FY0[i] + FV[i] * t) % 50 - 3 local x = FPN[i] + (floor(FX[i] + 2.2 * sin(t * 1.05 + FP[i])) % PW) if y >= PY0 and y < PY1 - 1 then local iy = floor(y) local f = y - iy blend_rect(x, iy, 1, 1, 255, 255, 255, 0.85 * (1 - f)) blend_rect(x, iy + 1, 1, 1, 255, 255, 255, 0.85 * f) end end if cfg.fog ~= false then fog_pane(P1, 0, t) fog_pane(P2, 8, t) end big_pots() fern(10, t) rubber(31, t) succulent(50, t) sprouts(t) if cfg.lights ~= false then for k = 1, #BX do local a = 0.55 + 0.45 * sin(t * 1.8 + k * 1.7) blend_rect(BX[k] - 1, BY[k] - 1, 3, 3, BULB[1], BULB[2], BULB[3], 0.2 * a) blend_rect(BX[k], BY[k], 1, 1, BULB[1], BULB[2], BULB[3], 0.5 + 0.5 * a) end end end