info = { type = "image", title = "Fern pond", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Rain rings on a forest pond between ferns", api = 44, } local tr = ({ sk = { ["Rain"] = "Dážď", ["Mist"] = "Hmla" }, cs = { ["Rain"] = "Déšť", ["Mist"] = "Mlha" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "rain", label = T("Rain"), type = "int", min = 1, max = 5, default = 3 }, { key = "mist", label = T("Mist"), type = "int", min = 0, max = 3, default = 2 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local W, H = gfx.W, gfx.H local function rn(a, b) return math.hash32(a * 7919 + b * 104729) & 0x7FFF end local WX, WY, WW, WH = 7, 25, 50, 39 local BG, FG = nil, nil local TIPX, TIPY local STOPS = { { 25, 26, 50, 46 }, { 34, 66, 98, 108 }, { 44, 60, 90, 104 }, { 63, 34, 58, 76 } } local function water(y) for i = 1, #STOPS - 1 do local a, b = STOPS[i], STOPS[i + 1] if y <= b[1] then local t = (y - a[1]) / (b[1] - a[1]) return floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5), floor(a[4] + (b[4] - a[4]) * t + 0.5) end end local s = STOPS[#STOPS] return s[2], s[3], s[4] end local function build_bg() rect(0, 0, W, H, 14, 24, 22) for y = 0, 24 do rect(0, y, W, 1, 12 + y // 3, 32 + y // 2, 26 + y // 3) end for i = 1, 10 do rect(4 + rn(i, 1) % 52, rn(i, 2) % 16, 3 + rn(i, 3) % 5, 2 + rn(i, 4) % 3, 54, 104, 62) end for y = WY - 1, H - 1 do local r, g, b = water(y) local xl = 6 + floor(1.5 * sin(y * 0.4) + 0.5) rect(xl, y, 58 - floor(1.5 * sin(y * 0.3 + 1) + 0.5) - xl, 1, r, g, b) end for i = 1, 16 do blend_rect(10 + rn(i, 5) % 38, 30 + rn(i, 6) % 28, 3 + rn(i, 7) % 5, 1, 200, 226, 232, 0.16) end for i = 1, 7 do blend_rect(8 + rn(i, 8) % 40, 26 + rn(i, 9) % 8, 6 + rn(i, 10) % 8, 1, 10, 36, 28, 0.35) end BG = gfx.snapshot(0, 0, W, H) end local GR = { { 16, 50, 36 }, { 30, 88, 54 }, { 54, 134, 74 }, { 110, 184, 108 } } local function frond(x, y, a, len, curl, ll) for i = 0, len do local f = i / len local ang = math.rad(a + curl * f * f) local dx, dy = cos(ang), sin(ang) x, y = x + dx, y + dy local c = GR[1 + floor(f * 2.2)] px(floor(x + 0.5), floor(y + 0.5), c[1], c[2], c[3]) if i >= 3 and i % 2 == 0 then local l = floor((ll + 2) * sin(3.14159 * f) ^ 0.8 + 1) for side = -1, 1, 2 do local vx, vy = dx * 0.75 - dy * side * 0.65, dy * 0.75 + dx * side * 0.65 for k = 1, l do local ax, ay = x + vx * k, y + vy * k + k * k * 0.12 local idx = 1 + floor(f * 2.0 + (side > 0 and 0.8 or 0) + (k == l and 0.7 or 0)) local g = GR[idx > 4 and 4 or idx] px(floor(ax + 0.5), floor(ay + 0.5), g[1], g[2], g[3]) end end end end return x, y end local ROCK = { { 0, 92, 138, 72 }, { 0.3, 70, 110, 66 }, { 0.62, 56, 82, 70 }, { 1, 28, 42, 48 } } local function rock(cx, cy, rx, ry, seed) for row = 0, 2 * ry do local t = row / (2 * ry) local y = cy - ry + row local hw = floor(rx * sqrt(1 - ((row - ry) / ry) ^ 2) + 0.5) local c1, c2 = ROCK[1], ROCK[2] for i = 2, #ROCK do c2 = ROCK[i] if t <= c2[1] then break end end for i = 1, #ROCK - 1 do if t >= ROCK[i][1] then c1 = ROCK[i] end end local u = (t - c1[1]) / (c2[1] - c1[1] + 0.0001) if u > 1 then u = 1 elseif u < 0 then u = 0 end rect(cx - hw, y, 2 * hw, 1, floor(c1[2] + (c2[2] - c1[2]) * u), floor(c1[3] + (c2[3] - c1[3]) * u), floor(c1[4] + (c2[4] - c1[4]) * u)) end for i = 1, 40 do local x, y = cx - rx + rn(seed, i) % (2 * rx), cy - ry + rn(seed, i + 60) % (2 * ry) local dx, dy = (x - cx) / rx, (y - cy) / ry if dx * dx + dy * dy < 0.8 then local g = GR[2 + rn(seed, i + 120) % 3] px(x, y, g[1], g[2], g[3]) end end end local function blade(x0, y0, x1, y1, arch, wmax) for x = x0, x1 do local f = (x - x0) / (x1 - x0) local yc = floor(y0 + (y1 - y0) * f - arch * sin(3.14159 * f) + 0.5) local t = floor(wmax * sin(3.14159 * f) ^ 0.7 + 0.5) + 1 rect(x, yc - t // 2, 1, t // 2, 54, 134, 74) rect(x, yc, 1, t - t // 2, 30, 88, 54) px(x, yc, 110, 184, 108) end end local function build_fg() gfx.clear(255, 0, 255) if FG then gfx.blit_rgb(FG, 0, 0, W, H) end if not FG then rock(8, 21, 16, 10, 1) rock(55, 58, 10, 7, 2) blade(-3, 66, 28, 50, 9, 8) blade(-3, 62, 15, 47, 5, 5) frond(63, 32, -100, 28, -70, 5) frond(61, 36, -125, 22, -30, 5) frond(66, 64, -165, 22, 55, 4) frond(64, 64, -125, 20, -30, 4) else frond(4, -1, 80, 18, 40, 4) frond(16, -1, 100, 16, -35, 4) frond(30, -1, 85, 14, 30, 4) frond(52, -1, 100, 17, -40, 4) frond(60, -2, 95, 14, -30, 4) TIPX, TIPY = frond(43, -1, 80, 25, 25, 4) TIPX, TIPY = floor(TIPX + 0.5), floor(TIPY + 0.5) end FG = gfx.snapshot(0, 0, W, H) end local function ring(cx, cy, r, a) local ry = r * 0.42 local nr = floor(ry + 0.5) for dy = 0, nr do local v0, v1 = dy - 0.5, dy + 0.5 if v0 < 0 then v0 = 0 end if v1 > ry then v1 = ry end local x1 = floor(r * sqrt(1 - (v0 / ry) ^ 2) + 0.5) local x0 = floor(r * sqrt(1 - (v1 / ry) ^ 2) + 0.5) for s = 0, (dy > 0 and 1 or 0) do local y = s == 0 and cy + dy or cy - dy if x0 == 0 then blend_rect(cx - x1, y, 2 * x1 + 1, 1, 188, 216, 224, a) else blend_rect(cx - x1, y, x1 - x0 + 1, 1, 188, 216, 224, a) blend_rect(cx + x0, y, x1 - x0 + 1, 1, 188, 216, 224, a) end end end end local LIFE = 46 local function drop_ring(x, y, u) local r = 0.8 + u * 0.17 local a = 0.5 * (1 - u / LIFE) ring(x, y, r, a) if u > 7 then ring(x, y, r * 0.6, a * 0.55) end end local function raindrop(i) local P = 52 + rn(i, 0) % 30 local t = frame + rn(i, 99) local k, u = t // P, t % P local x, y = 12 + rn(i, k * 2 + 1) % 40, 29 + rn(i, k * 2 + 2) % 27 if u < 3 then blend_rect(x, y - 14 + u * 6, 1, 3, 200, 226, 232, 0.5) else u = u - 3 if u < 4 then local a = 0.8 - 0.15 * u local dy = floor(-2 * u + 0.7 * u * u) blend(x, y, 226, 240, 244, a) blend(x - 1 - u, y + dy, 226, 240, 244, a) blend(x + 1 + u, y + dy, 226, 240, 244, a) end if u < LIFE then drop_ring(x, y, u) end end end local DRIP_P, SWELL, GRAV = 96, 28, 0.3 local function drip_ring() local wy = TIPY + 16 local u = frame % DRIP_P - SWELL if u < 0 then return end local ti = sqrt(2 * (wy - TIPY - 1) / GRAV) if u >= ti then drop_ring(TIPX, wy, u - ti) end end local function drip_drop() local u = frame % DRIP_P if u < SWELL then local a = 0.3 + 0.6 * u / SWELL blend(TIPX, TIPY + 1, 214, 238, 242, a) if u > SWELL - 8 then blend(TIPX, TIPY + 2, 214, 238, 242, a * 0.7) end else local t = u - SWELL local y = TIPY + 1 + 0.5 * GRAV * t * t if y < TIPY + 16 then local iy = floor(y) local f = y - iy blend(TIPX, iy, 214, 238, 242, 1 - f) blend(TIPX, iy + 1, 214, 238, 242, 0.9 * f + 0.1) blend(TIPX, iy - 1, 214, 238, 242, 0.35) end end end local function soft(x, y, w, h, a) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, h, 200, 226, 228, a * (1 - f)) blend_rect(ix + 1, y, w - 1, h, 200, 226, 228, a) blend_rect(ix + w, y, 1, h, 200, 226, 228, a * f) end local MY, ML, MV, MO = { 30, 38, 45, 51 }, { 34, 28, 36, 30 }, { 0.07, -0.05, 0.04, -0.08 }, { 5, 31, 52, 17 } local function mist(m) local a = 0.045 * m for i = 1, 4 do local L = ML[i] local x = (frame * MV[i] + MO[i]) % (W + L) - L local y = MY[i] soft(x + 6, y, L - 12, 2, a) soft(x, y + 2, L, 3, a) soft(x + 5, y + 5, L - 10, 2, a) end end function render() if not BG then build_bg() gfx.ready(false) elseif not TIPX then build_fg() end gfx.blit_rgb(BG, 0, 0, W, H) local rain, m = cfg.rain or 3, cfg.mist or 2 gfx.clip(WX, WY, WW, WH) for i = 1, 2 * rain + 1 do raindrop(i) end if TIPX then drip_ring() end if m > 0 then mist(m) end gfx.clip() if FG then gfx.blit_rgb(FG, 0, 0, W, H, 255, 0, 255) if TIPX then drip_drop() end end for j = 1, rain * 3 do blend_rect(rn(frame, j) % W, rn(frame, j + 50) % 66 - 2, 1, 3, 200, 224, 230, 0.2) end end