info = { type = "image", title = "Worm section", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Earthworms crawl in a cross-section of soil", api = 44, } local tr = ({ sk = { ["Rose"] = "Ruža", ["Worms"] = "Dážďovky", ["Rain"] = "Dážď" }, cs = { ["Rose"] = "Růže", ["Worms"] = "Žížaly", ["Rain"] = "Déšť" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "rose", label = T("Rose"), type = "color", default = "#D8203C" }, { key = "worms", label = T("Worms"), type = "int", min = 1, max = 3, default = 3 }, { key = "rain", label = T("Rain"), 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 GY = 24 local CYC = 900 local function mkpath(p) local L = { 0 } for i = 3, #p, 2 do L[#L + 1] = L[#L] + sqrt((p[i] - p[i - 2]) ^ 2 + (p[i + 1] - p[i - 1]) ^ 2) end return { p = p, L = L, n = #L } end local function at(P, s) local L, p = P.L, P.p if s <= 0 then return p[1], p[2] end if s >= L[P.n] then return p[#p - 1], p[#p] end for i = 2, P.n do if s <= L[i] then local u, j = (s - L[i - 1]) / (L[i] - L[i - 1]), 2 * i - 3 return p[j] + (p[j + 2] - p[j]) * u, p[j + 1] + (p[j + 3] - p[j + 1]) * u end end end local P1 = mkpath({ 4, 38, 12, 41, 22, 38, 32, 43, 44, 41, 47, 36, 47, 28, 48, 23, 52, 21, 58, 21 }) local P2 = mkpath({ 2, 52, 10, 49, 20, 53, 30, 50, 40, 55, 52, 52, 62, 56 }) local P3 = mkpath({ 62, 33, 55, 37, 52, 45, 58, 50 }) local SHAFT_TOP = 70.0 do local s = 0 while s < P1.L[P1.n] do local _, y = at(P1, s) if y <= GY + 2 then break end s = s + 0.5 end SHAFT_TOP = s end local BG, PART = nil, nil local STAGE = 1 local RC, RD, RL = { 216, 32, 60 }, { 134, 18, 38 }, { 240, 100, 112 } local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function garden() for y = 0, GY - 1 do local t = y / (GY - 1) rect(0, y, W, 1, rgb(104 + 96 * t, 186 + 48 * t, 236 + 10 * t)) end for x = 0, W - 1 do local h1 = floor(14.5 + 2 * sin(x * 0.10 + 1) + 1.2 * sin(x * 0.27)) rect(x, h1, 1, GY - h1, 150, 208, 150) local h2 = floor(18.5 + 1.6 * sin(x * 0.16 + 2.2)) rect(x, h2, 1, GY - h2, 112, 188, 112) end local sx, sy = 51, 7 rect(sx - 2, sy - 4, 5, 9, 255, 232, 96); rect(sx - 3, sy - 3, 7, 7, 255, 232, 96); rect(sx - 4, sy - 2, 9, 5, 255, 232, 96) rect(sx - 2, sy - 2, 5, 5, 255, 246, 170) rect(20, 14, 2, GY - 14, 52, 140, 62) rect(14, 18, 6, 2, 62, 164, 70); rect(12, 17, 3, 1, 62, 164, 70); rect(22, 14, 6, 2, 62, 164, 70); rect(27, 13, 2, 1, 62, 164, 70) rect(15, 19, 4, 1, 40, 112, 52); rect(23, 15, 4, 1, 40, 112, 52) rect(16, 5, 10, 9, rgb(RC[1], RC[2], RC[3])); rect(15, 6, 12, 7, rgb(RC[1], RC[2], RC[3])) rect(17, 4, 8, 1, rgb(RD[1], RD[2], RD[3])); rect(17, 14, 8, 1, rgb(RD[1], RD[2], RD[3])) rect(19, 7, 4, 4, rgb(RD[1], RD[2], RD[3])); rect(20, 8, 2, 2, rgb(RC[1], RC[2], RC[3])) rect(16, 7, 2, 1, rgb(RL[1], RL[2], RL[3])); rect(24, 10, 2, 1, rgb(RL[1], RL[2], RL[3])); rect(18, 12, 3, 1, rgb(RL[1], RL[2], RL[3])) rect(17, 13, 1, 2, 52, 140, 62); rect(24, 13, 1, 2, 52, 140, 62) rect(0, GY, W, 3, 84, 168, 66); rect(0, GY, W, 1, 124, 200, 84) for x = 0, W - 1, 2 do local h = hash32(x * 0x9E3779B1) local bl = 1 + umod(h, 3) if x < 44 or x > 50 then px(x + umod(h >> 4, 2), GY - bl, 96, 180, 74) end end rect(44, GY, 5, 3, 44, 26, 18) rect(40, GY - 2, 1, 2, 250, 250, 240); rect(39, GY - 3, 3, 1, 250, 250, 240); rect(40, GY - 3, 1, 1, 250, 210, 60) end local function stamp(P, w, r, g, b) local s, S = 0, P.L[P.n] while s <= S do local x, y = at(P, s) if y > GY + 1 then rect(floor(x - w / 2 + 0.5), floor(y - w / 2 + 0.5), w, w, r, g, b) end s = s + 1 end end local function soil() local y0 = GY + 3 rect(0, y0, W, 8, 84, 50, 32) rect(0, y0 + 8, W, 16, 112, 70, 42) rect(0, y0 + 24, W, H - y0 - 24, 136, 90, 56) for i = 1, 150 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, W), y0 + umod(h >> 8, H - y0) if h & 0x10000 ~= 0 then px(x, y, 150, 100, 64) else px(x, y, 70, 42, 26) end end end local function underground() stamp(P1, 4, 40, 24, 18); stamp(P2, 4, 40, 24, 18); stamp(P3, 4, 40, 24, 18) local pebs = { { 14, 57, 5 }, { 38, 33, 4 }, { 27, 47, 4 }, { 56, 41, 3 }, { 6, 44, 3 }, { 47, 59, 5 } } for _, p in ipairs(pebs) do local x, y, w = p[1], p[2], p[3] rect(x, y, w, 3, 150, 150, 158); rect(x + 1, y - 1, w - 2, 1, 150, 150, 158); rect(x + 1, y + 3, w - 2, 1, 150, 150, 158) rect(x + 1, y - 1, w - 2, 1, 196, 196, 204); rect(x, y + 2, w, 1, 108, 108, 118); rect(x + 1, y + 3, w - 2, 1, 108, 108, 118) end local roots = { 20, 27, 20, 33, 18, 38, 15, 43, 13, 47, 20, 31, 24, 36, 27, 41, 19, 39, 22, 44, 21, 49 } local R = { mkpath({ table.unpack(roots, 1, 10) }), mkpath({ table.unpack(roots, 11, 16) }), mkpath({ table.unpack(roots, 17, 22) }) } for i, P in ipairs(R) do local s = 0 while s <= P.L[P.n] do local x, y = at(P, s) px(floor(x + 0.5), floor(y + 0.5), 204, 166, 112) if i == 1 and s < 8 then px(floor(x + 1.5), floor(y + 0.5), 180, 142, 96) end s = s + 1 end end end local STAGES = { garden, soil, underground } local function build() if STAGE > 1 then gfx.blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if STAGE <= #STAGES then PART = gfx.snapshot(0, 0, W, H) else BG = gfx.snapshot(0, 0, W, H) end end function on_settings() local c = cfg.rose or { r = 216, g = 32, b = 60 } RC = { c.r, c.g, c.b } RD = { c.r * 0.62, c.g * 0.62, c.b * 0.62 } RL = { c.r + (255 - c.r) * 0.4, c.g + (255 - c.g) * 0.4, c.b + (255 - c.b) * 0.4 } BG, STAGE = nil, 1 end local function link(x, y, r, g, b) local x0 = floor(x - 1) local f = x - 1 - x0 local y0 = floor(y - 1 + 0.5) blend_rect(x0, y0, 1, 2, r, g, b, 1 - f) blend_rect(x0 + 1, y0, 1, 2, r, g, b, 1) if f > 0.02 then blend_rect(x0 + 2, y0, 1, 2, r, g, b, f) end end local NL, GAP = 10, 2.0 local function worm(P, s) for k = NL - 1, 0, -1 do local x, y = at(P, s - k * GAP) if k == 0 then link(x, y, 248, 176, 182) elseif k % 2 == 0 then link(x, y, 240, 146, 158) else link(x, y, 212, 110, 128) end end end local function smooth(u) return u * u * (3 - 2 * u) end local function seg(c, a, b) if c <= a then return 0 elseif c >= b then return 1 end return (c - a) / (b - a) end local DROPX, DROPP = {}, {} for i = 1, 18 do local h = hash32(i * 0xC2B2AE35) DROPX[i], DROPP[i] = umod(h, W), umod(h >> 8, 40) end local function idle(c) return 30 + 10 * sin(c * 6.2832 / 300) end local function rain_level(c) if c < 300 or c > 430 then return 0 end return math.min(seg(c, 300, 322), 1 - seg(c, 408, 430)) end function render() if not BG then build() if not BG then gfx.ready(false) return end end gfx.blit_rgb(BG, 0, 0, W, H) local c = frame % CYC local rain = cfg.rain ~= false local a = rain and rain_level(c) or 0 local pu = 0.45 + 0.4 * sin(frame * 0.07) local sx, sy = 51, 7 local ra = pu * (1 - a) if ra > 0.02 then blend_rect(sx - 7, sy, 2, 1, 255, 240, 140, ra); blend_rect(sx + 6, sy, 2, 1, 255, 240, 140, ra) blend_rect(sx, sy - 7, 1, 2, 255, 240, 140, ra); blend_rect(sx, sy + 6, 1, 2, 255, 240, 140, ra) blend_rect(sx - 6, sy - 6, 1, 1, 255, 240, 140, ra); blend_rect(sx + 6, sy - 6, 1, 1, 255, 240, 140, ra) blend_rect(sx - 6, sy + 6, 1, 1, 255, 240, 140, ra); blend_rect(sx + 6, sy + 6, 1, 1, 255, 240, 140, ra) end if a > 0 then blend_rect(0, 0, W, GY, 112, 122, 142, 0.5 * a) blend_rect(36, 1, 26, 6, 176, 184, 200, 0.85 * a); blend_rect(40, 0, 18, 9, 176, 184, 200, 0.85 * a) gfx.clip(0, 0, W, GY) for i = 1, 18 do local y = umod(floor(frame * 2.4 + DROPP[i] * 1.0), 40) - 8 blend_rect(DROPX[i], y, 1, 3, 190, 214, 255, 0.85 * a) end gfx.clip() end if rain then local wet = c >= 300 and c < 700 and math.min(seg(c, 300, 340), 1 - seg(c, 520, 700)) or 0 if wet > 0 then blend_rect(0, GY + 3, W, 6, 24, 12, 8, 0.3 * wet) end end local n = cfg.worms or 3 local s1 = idle(c) if rain then if c >= 430 and c < 520 then s1 = idle(430) + (SHAFT_TOP + 11 - idle(430)) * smooth(seg(c, 430, 520)) elseif c >= 520 and c < 700 then s1 = SHAFT_TOP + 11 elseif c >= 700 and c < 800 then s1 = SHAFT_TOP + 11 + (idle(800) - SHAFT_TOP - 11) * smooth(seg(c, 700, 800)) end end worm(P1, s1) if n >= 2 then worm(P2, 24 + 16 * sin(c * 6.2832 / 450 + 1)) end if n >= 3 then worm(P3, 18 + 6 * sin(c * 6.2832 / 225 + 2)) end end