info = { type = "image", title = "Dewy web spider", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Spider swings on a thread by a dewy web", api = 44, } local tr = ({ sk = { ["Spider"] = "Pavúk", ["Dew glints"] = "Trblietanie", ["Dew drops"] = "Kvapky rosy" }, cs = { ["Spider"] = "Pavouk", ["Dew glints"] = "Třpyt", ["Dew drops"] = "Kapky rosy" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "spider", label = T("Spider"), type = "bool", default = true }, { key = "glint", label = T("Dew glints"), type = "int", min = 0, max = 5, default = 3 }, { key = "dew", label = T("Dew drops"), type = "int", min = 0, max = 16, default = 12 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot, clip = gfx.blit_rgb, gfx.snapshot, gfx.clip local floor, sin, cos, sqrt, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SKY_T, SKY_B = { 44, 84, 150 }, { 150, 196, 228 } local TREES = { 58, 94, 118 } local FRAME, SILL = { 26, 36, 54 }, { 40, 54, 76 } local EDGE = { 84, 112, 146 } local AX, AY = 44, 3 local CX, CY = 4, 4 local N = 480 local SX, SY, VIS, MV = {}, {}, {}, {} local SIMMED, SIMF, TH, OM = false, 0, 0, 0 local function len_at(t) if t < 1.2 then return -5 end if t < 5.0 then local u = (t - 1.2) / 3.8; return -5 + 33 * u * u * (3 - 2 * u) end if t < 11 then return 28 end if t < 15 then local u = (t - 11) / 4; return 28 - 33 * u * u * (3 - 2 * u) end return -5 end local function simulate() local G, dt = 97, 1 / 60 local th, om = TH, OM for f = SIMF, SIMF + N // 2 - 1 do for s = 0, 1 do local t = (f + s / 2) / FPS if f == 39 and s == 0 then om = om + 0.12 end if f == 150 and s == 0 then om = om + 0.55 end local L, L2 = len_at(t), len_at(t + dt) local Lp = max(L, 6) om = om + (-(G / Lp) * sin(th) - 2 * ((L2 - L) / dt) / Lp * om - 0.25 * om) * dt th = max(-1, min(1, th + om * dt)) end local L = len_at(f / FPS) SX[f] = AX + max(L, 0) * sin(th) SY[f] = AY + 0.5 + L * cos(th) VIS[f] = L > -4.5 MV[f] = abs(len_at((f + 1) / FPS) - L) * FPS > 1.5 end TH, OM, SIMF = th, om, SIMF + N // 2 SIMMED = SIMF >= N end local LEGS = { { { { -2, -1 }, { -3, -2 }, { -4, -3 }, { -5, -2 }, { -5, -1 } }, { { -2, 0 }, { -3, -1 }, { -4, -1 }, { -5, 0 }, { -6, 1 } }, { { -2, 1 }, { -3, 1 }, { -4, 1 }, { -5, 2 }, { -6, 3 } }, { { -2, 2 }, { -3, 3 }, { -4, 4 }, { -4, 5 } } }, { { { -2, -1 }, { -3, -2 }, { -4, -2 }, { -5, -1 }, { -6, 0 } }, { { -2, 0 }, { -3, 0 }, { -4, 0 }, { -5, 1 }, { -6, 2 } }, { { -2, 1 }, { -3, 2 }, { -4, 2 }, { -5, 3 }, { -6, 4 } }, { { -2, 2 }, { -3, 3 }, { -3, 4 }, { -4, 5 }, { -4, 6 } } }, } local LEG_C = { 206, 226, 244 } local ABD = { { -1, 1 }, { -2, 2 }, { -2, 2 }, { -2, 2 }, { -1, 1 } } local POSE = {} for p = 1, 2 do local S = {} for _, leg in ipairs(LEGS[p]) do for _, q in ipairs(leg) do S[#S + 1] = { q[1], q[2], LEG_C[1], LEG_C[2], LEG_C[3] } S[#S + 1] = { -q[1], q[2], LEG_C[1], LEG_C[2], LEG_C[3] } end end for r = 1, #ABD do local y = r - 2 for x = ABD[r][1], ABD[r][2] do local s = x + y local c = s <= -1 and { 218, 234, 250 } or s <= 0 and { 178, 208, 238 } or s <= 2 and { 128, 172, 220 } or { 84, 126, 184 } if x == 0 and y == 1 then c = { 60, 140, 232 } end S[#S + 1] = { x, y, c[1], c[2], c[3] } end end for x = -1, 1 do local eye = x ~= 0 S[#S + 1] = { x, -2, eye and 26 or 204, eye and 40 or 226, eye and 80 or 246 } end POSE[p] = S end local function splat(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local gx, gy = 1 - fx, 1 - fy blend(ix, iy, r, g, b, gx * gy) if fx > 0.04 then blend(ix + 1, iy, r, g, b, fx * gy) end if fy > 0.04 then blend(ix, iy + 1, r, g, b, gx * fy) end if fx > 0.04 and fy > 0.04 then blend(ix + 1, iy + 1, r, g, b, fx * fy) end end local function draw_spider(f) if not VIS[f] then return end local bx, by = SX[f], SY[f] clip(0, 4, W, H - 10) local y1 = by - 3 local span = y1 - AY if span > 1 then for y = 4, floor(y1) do local x = AX + (bx - AX) * (y - AY) / span local ix = floor(x) local fx = x - ix blend(ix, y, 206, 228, 246, 0.85 * (1 - fx)) blend(ix + 1, y, 206, 228, 246, 0.85 * fx) end end local pose = MV[f] and ((f // 4) % 2 + 1) or ((f % 70) < 6 and 2 or 1) local S = POSE[pose] for i = 1, #S do local q = S[i] splat(bx + q[1], by + q[2], q[3], q[4], q[5]) end clip() end local BG, ND = nil, 0 local DROPS = {} local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local function line(x0, y0, x1, y1, r, g, b, a) local dx, dy = x1 - x0, y1 - y0 local n = max(abs(dx), abs(dy)) if n < 1 then blend(floor(x0 + 0.5), floor(y0 + 0.5), r, g, b, a) return end for i = 0, floor(n) do local t = i / n blend(floor(x0 + dx * t + 0.5), floor(y0 + dy * t + 0.5), r, g, b, a) end end local function drop(x, y, big) if big then rect(x, y, 3, 3, 150, 196, 232) px(x, y, 255, 255, 255); px(x + 1, y, 222, 240, 252); px(x, y + 1, 214, 236, 250) px(x + 2, y + 1, 76, 120, 172); px(x + 1, y + 2, 76, 120, 172); px(x + 2, y + 2, 54, 94, 148) else px(x, y, 255, 255, 255); px(x + 1, y, 200, 228, 246); px(x, y + 1, 160, 204, 236); px(x + 1, y + 1, 84, 130, 182) end end local ANG = { 0, 11, 23, 35, 47, 59, 71, 83, 90 } local RLEN = { 99, 32, 36, 38, 38, 37, 34, 30, 99 } local RING = { 6, 11, 16, 21, 26, 31 } local function build() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, lerp(SKY_T[1], SKY_B[1], t), lerp(SKY_T[2], SKY_B[2], t), lerp(SKY_T[3], SKY_B[3], t)) end for x = 4, W - 5 do local top = floor(47 + 2.5 * sin(x * 0.31) + 1.5 * sin(x * 0.87 + 1)) rect(x, top, 1, H - 6 - top, TREES[1], TREES[2], TREES[3]) blend_rect(x, top, 1, 3, 150, 196, 228, 0.35) end rect(0, 0, W, 4, FRAME[1], FRAME[2], FRAME[3]); rect(0, 0, 4, H, FRAME[1], FRAME[2], FRAME[3]) rect(W - 4, 0, 4, H, FRAME[1], FRAME[2], FRAME[3]); rect(0, H - 6, W, 6, SILL[1], SILL[2], SILL[3]) rect(4, 3, W - 8, 1, EDGE[1], EDGE[2], EDGE[3]); rect(3, 4, 1, H - 10, EDGE[1], EDGE[2], EDGE[3]) rect(0, H - 6, W, 1, 96, 120, 150) for j = 2, 8 do local a = math.rad(ANG[j]) local L = RLEN[j] line(CX, CY, CX + L * cos(a), CY + L * sin(a), 128, 170, 210, 1) end local cand = {} for k = 1, #RING do local r = RING[k] for j = 1, 8 do if r <= min(RLEN[j], RLEN[j + 1]) then local a1, a2 = math.rad(ANG[j]), math.rad(ANG[j + 1]) local px1, py1 = CX + r * cos(a1), CY + r * sin(a1) local px2, py2 = CX + r * cos(a2), CY + r * sin(a2) local mx, my = (px1 + px2) / 2, (py1 + py2) / 2 mx, my = mx + (CX - mx) * 0.07, my + (CY - my) * 0.07 line(px1, py1, mx, my, 196, 222, 240, 0.95) line(mx, my, px2, py2, 196, 222, 240, 0.95) if k >= 2 and j >= 2 and j <= 7 then cand[#cand + 1] = { floor(mx + 0.5), floor(my + 0.5) } end end end end DROPS, ND = {}, 0 local used, want = {}, min(cfg.dew or 12, #cand) for i = 1, 200 do if ND >= want then break end local h = hash32(i * 0x9E3779B1) local c = umod(h, #cand) + 1 if not used[c] then used[c] = true ND = ND + 1 local x, y = cand[c][1] - 1, cand[c][2] - 1 local big = umod(h >> 8, 3) == 0 drop(x, y, big) DROPS[ND] = { x = x, y = y, off = umod(h >> 12, 200), per = 150 - 18 * (cfg.glint or 3) + umod(h >> 20, 60) } end end BG = snapshot(0, 0, W, H) end function on_settings() BG = nil end local function glints() local g = cfg.glint or 3 if g == 0 then return end for i = 1, (ND * g + 4) // 5 do local d = DROPS[i] local ph = (frame + d.off) % d.per if ph < 14 then local a = 1 - abs(ph - 7) / 7 blend(d.x, d.y, 255, 255, 255, a) if a > 0.3 then blend(d.x - 1, d.y, 255, 255, 255, a * 0.6); blend(d.x + 1, d.y, 255, 255, 255, a * 0.6) blend(d.x, d.y - 1, 255, 255, 255, a * 0.6); blend(d.x, d.y + 1, 255, 255, 255, a * 0.6) end if a > 0.75 then blend(d.x - 2, d.y, 255, 255, 255, 0.35); blend(d.x + 2, d.y, 255, 255, 255, 0.35) blend(d.x, d.y - 2, 255, 255, 255, 0.35); blend(d.x, d.y + 2, 255, 255, 255, 0.35) end end end end function render() if not SIMMED then simulate(); gfx.ready(false) return end if not BG then build() end blit_rgb(BG, 0, 0, W, H) glints() if cfg.spider ~= false then draw_spider(frame % N) end end