info = { type = "image", title = "Dew snail", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A snail crawls along a leaf covered in dew", api = 44, } local tr = ({ sk = { ["Shell"] = "Ulita", ["Amber"] = "Jantárová", ["Plum"] = "Slivková", ["Olive"] = "Olivová" }, cs = { ["Shell"] = "Ulita", ["Amber"] = "Jantarová", ["Plum"] = "Švestková", ["Olive"] = "Olivová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "shell", label = T("Shell"), type = "int", default = "amber", opts = { keys = { "amber", "plum", "olive" }, T("Amber"), T("Plum"), T("Olive") } }, } local rect, px, blend, blend_rect, blend_mask = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blend_mask local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local atan, log = math.atan, math.log local schar, concat = string.char, table.concat local W, H = gfx.W, gfx.H local TAU = 6.2831853 local SHELLS = { amber = { { 218, 164, 80 }, { 104, 60, 30 } }, plum = { { 176, 148, 198 }, { 74, 54, 96 } }, olive = { { 172, 182, 96 }, { 72, 86, 44 } }, } local SH_LIGHT, SH_DARK = SHELLS.amber[1], SHELLS.amber[2] local SW, SHT = 46, 28 local OY = 22 local SPEED = 0.03 local KEY = { 255, 0, 255 } local function leaf(y) local t = (y - 22) / 34 if t < 0 then t = 0 elseif t > 1 then t = 1 end return floor(112 - 62 * t + 0.5), floor(188 - 72 * t + 0.5), floor(42 - 12 * t + 0.5) end local EY = {} for x = 0, 63 do EY[x] = floor(22 + 3 * math.sin(x * 0.09 + 0.6) + 0.5) end local function edge_y(x) return EY[x] end local BG local DROPS = { { 8, 30, 2 }, { 17, 36, 3 }, { 27, 28, 2 }, { 31, 39, 1 }, { 41, 31, 3 }, { 48, 37, 2 }, { 55, 27, 2 }, { 60, 40, 3 }, { 6, 54, 2 }, { 18, 57, 3 }, { 30, 53, 1 }, { 39, 58, 2 }, { 51, 55, 3 }, { 59, 60, 2 }, { 25, 61, 1 }, } local SPEC = {} local function disc_mask(r) local n = 2 * r + 1 local t = {} for y = 0, n - 1 do local row = {} for x = 0, n - 1 do local d = sqrt((x - r) ^ 2 + (y - r) ^ 2) local a = 0 if d < r + 0.5 then a = 120 + 110 * max(0, (d - (r - 1.6)) / 1.6) if d > r - 0.5 then a = a * (r + 0.5 - d) end end row[x + 1] = schar(min(255, floor(a))) end t[y + 1] = concat(row) end return concat(t), n end local STEPS = { function() for y = 0, H - 1 do local r, g, b if y <= 56 then r, g, b = leaf(y) else local t = (y - 56) / 7 r, g, b = floor(50 - 26 * t + 0.5), floor(116 - 58 * t + 0.5), floor(30 - 8 * t + 0.5) end rect(0, y, W, 1, r, g, b) end end, function() for y = 0, 27 do local t = y / 27 local r, g, b = floor(46 + 48 * t + 0.5), floor(100 + 58 * t + 0.5), floor(36 + 14 * t + 0.5) local x = 0 while x < W do if edge_y(x) > y then local x1 = x while x1 + 1 < W and edge_y(x1 + 1) > y do x1 = x1 + 1 end rect(x, y, x1 - x + 1, 1, r, g, b) x = x1 + 1 else x = x + 1 end end end end, function() local D = { { 6, 5, 6 }, { 19, 9, 4 }, { 30, 4, 7 }, { 43, 10, 5 }, { 54, 5, 6 }, { 12, 13, 4 }, { 36, 14, 4 }, { 60, 14, 4 } } local masks = {} for i = 1, #D do local r = D[i][3] if not masks[r] then masks[r] = { disc_mask(r) } end local m = masks[r] local k = (i % 2 == 0) and 0.36 or 0.5 blend_mask(m[1], 196, 226, 120, D[i][1] - r, D[i][2] - r, m[2], m[2], m[2], m[2], k) end for x = 0, W - 1 do local y = edge_y(x) px(x, y, 206, 234, 132) blend(x, y + 1, 190, 226, 120, 0.4) end end, function() blend_rect(0, 43, W, 1, 200, 230, 130, 0.45) rect(0, 44, W, 1, 178, 214, 108) blend_rect(0, 45, W, 1, 30, 70, 20, 0.35) for x0 = 4, 64, 14 do for s = 0, 18 do local x, y = x0 + s * 0.8, 42 - s blend(floor(x + 0.5), floor(y + 0.5), 196, 228, 124, 0.34) blend(floor(x + 0.5) - 1, floor(y + 0.5) + 1, 30, 70, 20, 0.2) end for s = 0, 14 do local x, y = x0 + 6 + s * 0.8, 47 + s blend(floor(x + 0.5), floor(y + 0.5), 188, 222, 118, 0.3) blend(floor(x + 0.5) - 1, floor(y + 0.5) + 1, 30, 70, 20, 0.2) end end end, function() for i = 1, #DROPS do local d = DROPS[i] local cx, cy, r = d[1], d[2], d[3] if r == 1 then px(cx, cy, 214, 244, 190); px(cx + 1, cy + 1, 28, 76, 24) else for dy = -r, r do for dx = -r, r do local q = sqrt(dx * dx + dy * dy) if q <= r + 0.3 then local f = (dx * 0.6 + dy * 0.8) / r * 0.5 + 0.5 if q > r - 0.7 and f < 0.5 then px(cx + dx, cy + dy, 24, 62, 20) else px(cx + dx, cy + dy, floor(40 + 170 * f), floor(84 + 156 * f), floor(26 + 110 * f)) end end end end local sx, sy = cx - floor(r * 0.4 + 0.5), cy - floor(r * 0.4 + 0.5) px(sx, sy, 226, 250, 214) SPEC[#SPEC + 1] = { sx, sy, i } end end end, function() blend_rect(0, 56, W, 8, 12, 40, 14, 0.3) blend_rect(0, 0, 2, H, 10, 30, 12, 0.3) blend_rect(W - 2, 0, 2, H, 10, 30, 12, 0.3) BG = gfx.snapshot(0, 0, W, H) end, } local LX, LY, LZ = -0.38, -0.62, 0.69 local HX, HY, HZ = -0.206, -0.337, 0.918 local function body_r(x) if x < 0.5 then return 0 end local r if x < 14 then local t = x / 14; r = 0.6 + 3.6 * t * t * (3 - 2 * t) elseif x < 30 then r = 4.2 else local t = min(1, (x - 30) / 6); r = 4.2 + 0.5 * t * t * (3 - 2 * t) end if x > 38 then local q = (x - 38) / 4.3 if q >= 1 then return 0 end r = r * sqrt(1 - q * q) end return r end local function clamp255(v) if v < 0 then return 0 elseif v > 255 then return 255 end return floor(v) end local function pixel(u0, vv, lr, lg, lb) local under_r, under_g, under_b = lr, lg, lb local shadow = 0 if vv > 23.5 then local su = min(1, max(0, (u0 - 1) / 5), max(0, (42 - u0) / 6)) shadow = 0.5 * max(0, 1 - (vv - 23.5) / 3.6) * su if shadow > 0 then under_r, under_g, under_b = lr * (1 - shadow), lg * (1 - shadow), lb * (1 - shadow) end end local out_r, out_g, out_b = under_r, under_g, under_b local solid = shadow > 0.04 local r = body_r(u0) if r > 0.05 then local top = 24 - 2 * r local ba = min(max(vv - top + 0.5, 0), 1, max(24.5 - vv, 0)) if ba > 0.04 then local c = 24 - r local d = (vv - c) / r if d > 1 then d = 1 elseif d < -1 then d = -1 end local nx = u0 > 38 and (u0 - 38) / 4.3 or 0 local nz = sqrt(max(0, 1 - d * d - nx * nx)) local lit = max(0, nx * LX + d * LY + nz * LZ) local k = 0.26 + 0.8 * lit local wr = 0.93 + 0.07 * sin(u0 * 1.9 + d * 2.3) k = k * wr local sp = max(0, nx * HX + d * HY + nz * HZ) ^ 18 * 0.5 * 255 local bounce = 0.2 * max(0, d) local br = 170 * k + sp + lr * bounce local bg = 138 * k + sp + lg * bounce local bb = 110 * k + sp + lb * bounce out_r, out_g, out_b = br * ba + under_r * (1 - ba), bg * ba + under_g * (1 - ba), bb * ba + under_b * (1 - ba) solid = true end end local dx, dy = u0 - 16, vv - 12 local dist = sqrt(dx * dx + dy * dy) local a = min(max(10 - dist + 0.5, 0), 1) if a > 0.03 then local nx, ny = dx / 10, dy / 10 local nz = sqrt(max(0, 1 - nx * nx - ny * ny)) local lit = max(0, nx * LX + ny * LY + nz * LZ) local ph = (atan(dy, dx) / TAU + 2.2 * log(dist + 1)) % 1 local m = 0.5 + 0.5 * sin(ph * TAU) m = m ^ 0.7 local k = 0.3 + 0.78 * lit local sp = max(0, nx * HX + ny * HY + nz * HZ) ^ 26 * 0.9 * 255 local bounce = 0.18 * max(0, ny) local sr = (SH_DARK[1] + (SH_LIGHT[1] - SH_DARK[1]) * m) * k + sp + lr * bounce local sg = (SH_DARK[2] + (SH_LIGHT[2] - SH_DARK[2]) * m) * k + sp + lg * bounce local sb = (SH_DARK[3] + (SH_LIGHT[3] - SH_DARK[3]) * m) * k + sp + lb * bounce out_r, out_g, out_b = sr * a + out_r * (1 - a), sg * a + out_g * (1 - a), sb * a + out_b * (1 - a) solid = true end if not solid then return KEY[1], KEY[2], KEY[3] end return clamp255(out_r), clamp255(out_g), clamp255(out_b) end local SPR, ROWS, PK, PV = {}, {}, 0, 0 local function bake_rows(n) while n > 0 and PK < 4 do local row = {} local vv = PV + 0.5 local lr, lg, lb = leaf(OY + PV) for u = 0, SW - 1 do local r, g, b = pixel(u + 0.5 - PK * 0.25, vv, lr, lg, lb) row[u + 1] = schar(r, g, b) end ROWS[PV + 1] = concat(row) PV = PV + 1 if PV == SHT then SPR[PK + 1] = concat(ROWS) ROWS, PV, PK = {}, 0, PK + 1 end n = n - 1 end end local STEP = 1 function on_settings() local s = SHELLS[cfg.shell] or SHELLS.amber SH_LIGHT, SH_DARK = s[1], s[2] SPR, ROWS, PK, PV = {}, {}, 0, 0 end local STALK = { 140, 110, 92 } local function bulb(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local w = (1 - fx) * (1 - fy) if w > 0.05 then blend(ix, iy, r, g, b, min(1, w * 1.5)) end w = fx * (1 - fy) if w > 0.05 then blend(ix + 1, iy, r, g, b, min(1, w * 1.5)) end w = (1 - fx) * fy if w > 0.05 then blend(ix, iy + 1, r, g, b, min(1, w * 1.5)) end w = fx * fy if w > 0.05 then blend(ix + 1, iy + 1, r, g, b, min(1, w * 1.5)) end end local function stalk(bx, by, dx, dy) local tx, ty = bx + dx, by + dy local cx, cy = bx + dx * 0.25, by + dy * 0.8 for i = 1, 8 do local u = i / 8 local a, b, c = (1 - u) * (1 - u), 2 * u * (1 - u), u * u bulb(a * bx + b * cx + c * tx, a * by + b * cy + c * ty, STALK[1], STALK[2], STALK[3]) end bulb(tx, ty - 0.2, 30, 24, 22) bulb(tx + 0.8, ty - 0.4, 30, 24, 22) end function render() if STEP <= #STEPS then STEPS[STEP]() STEP = STEP + 1 if STEP <= #STEPS then gfx.ready(false) end return end gfx.blit_rgb(BG, 0, 0, W, H) for i = 1, #SPEC do local s = SPEC[i] local k = 0.6 + 0.4 * sin(frame * 0.05 + s[3]) px(s[1], s[2], floor(150 + 105 * k), floor(190 + 65 * k), floor(150 + 95 * k)) end if PK < 4 then bake_rows(2) end local nph = PK if PK == 0 and PV == 0 then return end local xs = -SW + (frame * SPEED + 53) % (SW + W) local xi = floor(xs) local ph = floor((xs - xi) * 4) local spr = SPR[min(ph, nph - 1) + 1] or SPR[1] if not spr then return end gfx.blit_rgb(spr, xi, OY, SW, SHT, KEY[1], KEY[2], KEY[3]) local bx, by = xs + 37.0, OY + 16.0 local f = frame stalk(bx, by, 3.8 + 0.9 * sin(f * 0.031), -8.0 + 0.7 * sin(f * 0.043 + 1)) stalk(bx + 2.8, by, 5.0 + 0.9 * sin(f * 0.037 + 2), -7.0 + 0.7 * sin(f * 0.05)) end