info = { type = "image", title = "Turtle dash", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Hatchlings crawl over moonlit sand to the sea", api = 33, } local tr = ({ sk = { ["Hatchlings"] = "Mláďatá", ["Full moon"] = "Spln" }, cs = { ["Hatchlings"] = "Mláďata", ["Full moon"] = "Úplněk" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Hatchlings"), type = "int", min = 1, max = 8, default = 5 }, { key = "moon", label = T("Full moon"), type = "bool", default = true }, } local rect, px, blend, blend_rect, clip = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.clip local floor, ceil, sqrt, sin, cos = math.floor, math.ceil, math.sqrt, math.sin, math.cos local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function span(xa, xb, y, r, g, b, a) if xb <= xa then return end a = a or 1 local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, (xb - xa) * a) return end local s, c = ia + 1, ia + 1 - xa if c > 0.96 then s = ia else blend(ia, y, r, g, b, c * a) end local f = xb - ib if f > 0.04 then blend(ib, y, r, g, b, f * a) end if ib > s then if a < 1 then blend_rect(s, y, ib - s, 1, r, g, b, a) else rect(s, y, ib - s, 1, r, g, b) end end end local function oval(cx, cy, rx, ry, r, g, b, a) for y = floor(cy - ry), ceil(cy + ry) - 1 do local d = (y + 0.5 - cy) / ry if d > -1 and d < 1 then local hw = rx * sqrt(1 - d * d) span(cx - hw, cx + hw, y, r, g, b, a) end end end local function lin(a, b, lo, hi) if a > -1e-9 and a < 1e-9 then if b >= lo and b <= hi then return -1e9, 1e9 end return 1e9, -1e9 end local x1, x2 = (lo - b) / a, (hi - b) / a if x1 > x2 then return x2, x1 end return x1, x2 end local function capsule(x0, y0, x1, y1, rad, r, g, b, a) local dx, dy = x1 - x0, y1 - y0 local L2 = dx * dx + dy * dy local L = sqrt(L2) local ya, yb = y0, y1 if ya > yb then ya, yb = yb, ya end local r2 = rad * rad local ady = dy < 0 and -dy or dy local steep = ady > 0.35 * L local ihw, k = 0, 0 if steep then ihw, k = rad * L / ady, dx / dy end local xlo, xhi = (x0 < x1 and x0 or x1) - rad, (x0 > x1 and x0 or x1) + rad for y = floor(ya - rad), ceil(yb + rad) - 1 do local yc = y + 0.5 local lo, hi if steep and yc >= ya and yc <= yb then local xc = x0 + k * (yc - y0) lo, hi = xc - ihw, xc + ihw if lo < xlo then lo = xlo end if hi > xhi then hi = xhi end else lo, hi = 1e9, -1e9 local e = r2 - (yc - y0) * (yc - y0) if e > 0 then e = sqrt(e); lo, hi = x0 - e, x0 + e end e = r2 - (yc - y1) * (yc - y1) if e > 0 then e = sqrt(e) if x1 - e < lo then lo = x1 - e end if x1 + e > hi then hi = x1 + e end end if not steep and L > 0.01 then local pa, pb = lin(dy, -x0 * dy - (yc - y0) * dx, -rad * L, rad * L) local qa, qb = lin(dx, -x0 * dx + (yc - y0) * dy, 0, L2) if qa > pa then pa = qa end if qb < pb then pb = qb end if pb > pa then if pa < lo then lo = pa end if pb > hi then hi = pb end end end end span(lo, hi, y, r, g, b, a) end end local function lerp3(a, b, f) return floor(a[1] + (b[1] - a[1]) * f + 0.5), floor(a[2] + (b[2] - a[2]) * f + 0.5), floor(a[3] + (b[3] - a[3]) * f + 0.5) end local PM = { sky0 = { 16, 24, 76 }, sky1 = { 64, 92, 160 }, sea0 = { 52, 80, 150 }, sea1 = { 22, 48, 112 }, wet = { 84, 82, 104 }, sand0 = { 134, 126, 130 }, sand1 = { 112, 104, 112 }, water = { 40, 76, 146 }, foam = { 214, 228, 244 }, star = 0.55, } local PD = { sky0 = { 8, 12, 46 }, sky1 = { 36, 54, 112 }, sea0 = { 30, 52, 108 }, sea1 = { 14, 32, 86 }, wet = { 66, 66, 88 }, sand0 = { 104, 98, 112 }, sand1 = { 86, 80, 96 }, water = { 24, 46, 98 }, foam = { 176, 194, 224 }, star = 1, } local SHELL, SHELL_L, FLIP, SKIN = { 40, 52, 44 }, { 88, 104, 80 }, { 60, 76, 58 }, { 72, 86, 64 } local MOONC = { 252, 240, 200 } local HZ, YB, SW, TW = 22, 38, 5, 150 local MX, MY = 46, 10 local N, MOON, P = 5, true, PM local SL, BG = {}, nil function on_settings() N = cfg.n or 5 MOON = cfg.moon ~= false P = MOON and PM or PD BG = nil for i = 1, N do local h = hash32(i * 0x9E3779B1) local dly = umod(h >> 8, 120) local uc = ceil((dly + 95 - 22) / TW) * TW + 22 local xs = floor(6 + (i - 0.5) * 52 / N + umod(h, 5) - 2) local xe = xs + umod(h >> 16, 15) - 7 local P = ceil((uc + 95) / TW) * TW local sel, u0 = (i - 1) % 5, 60 if sel == 0 or sel == 2 then u0 = uc > 200 and 210 or 60 elseif sel == 3 then u0 = uc > 200 and 360 or 210 end SL[i] = { xs = xs, ys = 53 + umod(h >> 4, 9), dly = dly, uc = uc, xe = xe < 6 and 6 or (xe > 57 and 57 or xe), ye = 40.2 + umod(h >> 20, 12) / 10, P = P, off = (u0 - 60) % P, ph = umod(h >> 24, 628) / 100, } end end local function build_bg() for y = 0, HZ - 1 do rect(0, y, W, 1, lerp3(P.sky0, P.sky1, y / (HZ - 1))) end for y = HZ, YB - 1 do rect(0, y, W, 1, lerp3(P.sea0, P.sea1, (y - HZ) / (YB - 1 - HZ))) end rect(0, YB, W, SW + 1, P.wet[1], P.wet[2], P.wet[3]) for y = YB + SW + 1, H - 1 do rect(0, y, W, 1, lerp3(P.sand0, P.sand1, (y - YB - SW - 1) / (H - YB - SW - 2))) end for i = 1, 70 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, W), YB + SW + 2 + umod(h >> 8, H - YB - SW - 2) if (h >> 16) & 1 == 1 then blend(x, y, 255, 250, 240, 0.12) else blend(x, y, 20, 20, 40, 0.18) end end for i = 1, MOON and 14 or 26 do local h = hash32(i * 0xC2B2AE35) local x, y = umod(h, W), umod(h >> 8, HZ - 3) local dx, dy = x - MX, y - MY if not MOON or dx * dx + dy * dy > 100 then blend(x, y, 236, 240, 255, P.star) end end if MOON then oval(MX, MY, 9, 9, MOONC[1], MOONC[2], MOONC[3], 0.14) oval(MX, MY, 6.8, 6.8, MOONC[1], MOONC[2], MOONC[3], 0.22) oval(MX, MY, 4.6, 4.6, MOONC[1], MOONC[2], MOONC[3]) px(MX - 2, MY - 1, 226, 212, 168); px(MX + 1, MY + 1, 226, 212, 168); px(MX - 1, MY + 2, 226, 212, 168) end for i = 1, N do local s = SL[i] oval(s.xs, s.ys + 0.8, 4.6, 2.2, 150, 142, 146, 0.5) oval(s.xs, s.ys, 3, 1.4, 66, 60, 70) end BG = gfx.snapshot(0, 0, W, H) end local function hatchling(x, y, s, ph, k) local W3 = P.water local sr, sg, sb = lerp3(SHELL, W3, k) local lr, lg, lb = lerp3(SHELL_L, W3, k) local fr, fg, fb = lerp3(FLIP, W3, k) local kr, kg, kb = lerp3(SKIN, W3, k) local sw = 0.75 * sin(ph) for side = -1, 1, 2 do local th = 0.15 + side * sw capsule(x + side * 1.6 * s, y + 2.4 * s, x + side * 2.3 * s, y + 3.4 * s, 0.6 * s, fr, fg, fb) capsule(x + side * 2.0 * s, y - 1.4 * s, x + side * (2.0 + 2.8 * cos(th)) * s, y - (1.4 + 2.8 * sin(th)) * s, 0.75 * s, fr, fg, fb) end oval(x, y, 2.6 * s, 3.1 * s, sr, sg, sb) oval(x, y - 0.2 * s, 1.2 * s, 1.9 * s, lr, lg, lb) oval(x, y - 3.5 * s, 1.35 * s, 1.35 * s, kr, kg, kb) end local function swash(tw) if tw < 45 then local u = tw / 45; return 1 - (1 - u) * (1 - u) end if tw < 60 then return 1 end local u = (tw - 60) / 90 return 1 - u * u end local function draw_slot(s, t) local u = (t + s.off) % s.P local dly, uc = s.dly, s.uc if u < dly then if u > dly - 18 then px(s.xs + (u // 3) % 2, s.ys - 1, 150, 142, 146) end return end local k, x, y, sc, ph = 0, s.xs, s.ys, 1, t * 0.8 local c0 = dly + 25 local clipped = u < c0 + 22 if u < c0 then y = s.ys + 5 * (1 - (u - dly) / 25) local tau = (u - dly) / 30 if tau < 0.5 then local ay = floor(s.ys - (14 * tau - 28 * tau * tau)) px(floor(s.xs - 5 * tau), ay, 160, 152, 154) px(floor(s.xs + 3 * tau), ay - 1, 160, 152, 154) px(floor(s.xs + 7 * tau), ay, 160, 152, 154) end elseif u < uc - 12 then local w = (u - c0) / (uc - 12 - c0) local q = w - 0.8 * sin(w * 31.416) / 31.416 x = s.xs + (s.xe - s.xs) * q + 1.1 * sin(q * 9 + s.ph) y = s.ys + (s.ye - s.ys) * q elseif u < uc then x, y, ph = s.xe, s.ye, t * 0.35 elseif u < uc + 90 then local w = (u - uc) / 90 x, y = s.xe + 1.5 * w, s.ye - 11 * (1 - (1 - w) * (1 - w)) sc, k, ph = 1 - 0.4 * w, 0.15 + 0.77 * w, t * 0.8 else return end if clipped then clip(0, 0, W, s.ys + 1) end hatchling(x, y, sc, ph, k) if clipped then clip() end end local function draw_waves(t) local f = swash(t % TW) local wr, wg, wb = P.water[1], P.water[2], P.water[3] local fr, fg, fb = P.foam[1], P.foam[2], P.foam[3] for x = 0, W - 1, 2 do local yf = YB + SW * f + 0.9 * sin(x * 0.45 + t * 0.06) - 0.4 local iy = floor(yf) if iy > YB then blend_rect(x, YB, 2, iy - YB, wr, wg, wb, 0.6) end blend_rect(x, iy, 2, 1, fr, fg, fb, 0.5 + 0.4 * (yf - iy)) blend_rect(x, iy - 1, 2, 1, fr, fg, fb, 0.3) end end local function draw_moonpath(t) for y = HZ + 1, YB - 1 do local d = y - HZ local w = 2 + d * 0.5 + 1.5 * sin(y * 2.3 + t * 0.07) local off = 1.4 * sin(y * 0.9 + t * 0.05) span(MX - w / 2 + off, MX + w / 2 + off, y, MOONC[1], MOONC[2], MOONC[3], 0.2 + 0.14 * sin(y * 1.9 + t * 0.12)) span(MX - w / 4 + off, MX + w / 4 + off, y, MOONC[1], MOONC[2], MOONC[3], 0.25) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame if MOON then draw_moonpath(t) end for i = 1, N do draw_slot(SL[i], t) end draw_waves(t) end