info = { type = "image", title = "Watermelon snack", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "A melon slice eaten bite by bite, then regrown", api = 33, } local STEP_HZ = 30 local FSTEP = 0 local ltr = ({ sk = { ["Eating speed"] = "Rýchlosť jedenia" }, cs = { ["Eating speed"] = "Rychlost jedení" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "speed", label = LT("Eating speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sqrt, sin, cos, exp, abs = math.floor, math.ceil, math.sqrt, math.sin, math.cos, math.exp, math.abs local hash32 = math.hash32 local W, H = gfx.W, gfx.H local SR, SG, SB = 140, 180, 245 local DR, DG, DB = 34, 122, 58 local LR, LG, LB = 132, 204, 80 local PR, PG, PB = 236, 244, 196 local FR, FG, FB = 238, 52, 72 local ER, EG, EB = 255, 134, 146 local KR, KG, KB = 40, 22, 26 local JR, JG, JB = 255, 150, 164 local GING = { 255, 246, 214, 250, 222, 120, 234, 182, 44 } local CX0, CY = 32, 27 local RO, RL, RP, RF = 25, 22, 20, 19 local TABLE = 52 local function ov(x, a, b) local lo = a > x and a or x local hi = b < x + 1 and b or x + 1 return hi > lo and hi - lo or 0 end local function part(x0, x1, y, a1, b1, a2, b2, r, g, b) local rs, rq = x0, 0 for x = x0, x1 do local c = ov(x, a1, b1) if a2 then c = c + ov(x, a2, b2) end local q = floor(c * 4 + 0.5) if q ~= rq then if rq >= 8 then rect(rs, y, x - rs, 1, r, g, b) elseif rq > 0 then blend_rect(rs, y, x - rs, 1, r, g, b, rq / 8) end rs, rq = x, q end end if rq >= 8 then rect(rs, y, x1 + 1 - rs, 1, r, g, b) elseif rq > 0 then blend_rect(rs, y, x1 + 1 - rs, 1, r, g, b, rq / 8) end end local function aarow(y, a1, b1, a2, b2, r, g, b) if not a1 then a1, b1, a2, b2 = a2, b2, nil, nil end if not a1 then return end local lo, hi, L, R if a2 then lo, hi = floor(a1 < a2 and a1 or a2), ceil(b1 > b2 and b1 or b2) - 1 L, R = ceil(a1 > a2 and a1 or a2), floor(b1 < b2 and b1 or b2) if R > L then rect(L, y, R - L, 1, r, g, b) end else lo, hi, L, R = floor(a1), ceil(b1) - 1, ceil(a1), floor(b1) if R > L then blend_rect(L, y, R - L, 1, r, g, b, 0.5) end end if R > L then if L > lo then part(lo, L - 1, y, a1, b1, a2, b2, r, g, b) end if hi >= R then part(R, hi, y, a1, b1, a2, b2, r, g, b) end else part(lo, hi, y, a1, b1, a2, b2, r, g, b) end end local function hspan(cx, yc, r, s, c) local dy = yc - CY local q = r * r - dy * dy if q <= 0 then return nil end q = sqrt(q) local lo, hi = cx - q, cx + q if s > 1e-4 then local m = cx + dy * c / s if m < hi then hi = m end elseif s < -1e-4 then local m = cx + dy * c / s if m > lo then lo = m end elseif dy < 0 then return nil end if hi <= lo then return nil end return lo, hi end local function hrow(lo, hi, y, r, g, b) local c0, c1 = ceil(lo - 0.5), ceil(hi - 0.5) - 1 if c1 >= c0 then rect(c0, y, c1 - c0 + 1, 1, r, g, b) end end local function draw_rind(cx, th) local s, c = sin(th), cos(th) for y = floor(CY - RO * abs(s)) - 1, CY + RO - 1 do local a1, b1 = hspan(cx, y + 0.25, RO, s, c) local a2, b2 = hspan(cx, y + 0.75, RO, s, c) aarow(y, a1, b1, a2, b2, DR, DG, DB) local yc = y + 0.5 local lo, hi = hspan(cx, yc, RL, s, c) if lo then hrow(lo, hi, y, LR, LG, LB) lo, hi = hspan(cx, yc, RP, s, c) if lo then hrow(lo, hi, y, PR, PG, PB) lo, hi = hspan(cx, yc, RF, s, c) if lo then hrow(lo, hi, y, SR, SG, SB) end end end end local sx = floor(cx + 0.5) blend_rect(sx - 10, TABLE, 20, 1, 150, 100, 20, 0.45) blend_rect(sx - 7, TABLE + 1, 14, 1, 150, 100, 20, 0.25) end local FC0, FC1 = {}, {} for y = CY, CY + RF - 1 do local dy = y + 0.5 - CY local a = sqrt(RF * RF - dy * dy) FC0[y], FC1[y] = ceil(CX0 - a - 0.5), ceil(CX0 + a - 0.5) - 1 end local KX, KY, NK = {}, {}, 0 do local function seed(r, a) NK = NK + 1 KX[NK], KY[NK] = floor(CX0 + r * cos(a)), floor(CY + r * sin(a)) end for k = 1, 7 do seed(13.5, k * 3.14159 / 8) end for k = 1, 5 do seed(16.5, k * 3.14159 / 6) end for k = 1, 3 do seed(7, k * 3.14159 / 4) end end local BX = { 14, 23, 32, 41, 50, 48, 40, 32, 24, 16, 22, 32, 42 } local BY = { 27, 27, 27, 27, 27, 34, 34, 34, 34, 34, 41, 41, 41 } local NB = #BX local SDX, SDY, NS = {}, {}, 0 do local BR, T, TR = 6.5, 11, 1.7 local tx, ty = {}, {} for j = 1, T do local a = j * 6.28318 / T tx[j], ty[j] = BR * cos(a), BR * sin(a) end for dy = -9, 9 do for dx = -9, 9 do local d2 = dx * dx + dy * dy local inside = d2 < BR * BR if not inside and d2 < (BR + TR) * (BR + TR) then for j = 1, T do local ex, ey = dx - tx[j], dy - ty[j] if ex * ex + ey * ey < TR * TR then inside = true; break end end end if inside then NS = NS + 1; SDX[NS], SDY[NS] = dx, dy end end end end local E = {} local function clear_mask() for i = 1, 2 * H do E[i] = 0 end end clear_mask() local function eaten(x, y) return (E[2 * y + 1 + (x >> 5)] >> (x & 31)) & 1 == 1 end local function stamp(i) local bx, by = BX[i], BY[i] for k = 1, NS do local x, y = bx + SDX[k], by + SDY[k] local j = 2 * y + 1 + (x >> 5) E[j] = E[j] | (1 << (x & 31)) end end local function draw_flesh(mask) for y = CY, CY + RF - 1 do local c0, c1 = FC0[y], FC1[y] if mask then local run = nil for x = c0, c1 do if eaten(x, y) then if run then rect(run, y, x - run, 1, FR, FG, FB); run = nil end elseif not run then run = x end end if run then rect(run, y, c1 + 1 - run, 1, FR, FG, FB) end else rect(c0, y, c1 - c0 + 1, 1, FR, FG, FB) end end if mask then for y = CY, CY + RF - 1 do for x = FC0[y], FC1[y] do if not eaten(x, y) and (eaten(x - 1, y) or eaten(x + 1, y) or eaten(x, y - 1) or eaten(x, y + 1)) then px(x, y, ER, EG, EB) end end end end for k = 1, NK do if not mask or not (eaten(KX[k], KY[k]) or eaten(KX[k], KY[k] + 1)) then rect(KX[k], KY[k], 1, 2, KR, KG, KB) end end end local GRAV = 0.16 local function piece(i, j, u, cyc) local h = hash32(i * 0x9E3779B1 + j * 0x7F4A7C15 + cyc * 0x2545F491) local r1 = (h & 0xFF) / 255 local r2 = ((h >> 8) & 0xFF) / 255 local r3 = ((h >> 16) & 0xFF) / 255 local side = BX[i] < CX0 and -1 or 1 if BX[i] == CX0 then side = (j % 2 == 0) and 1 or -1 end local vx = side * (0.3 + r1 * 0.8) local vy = -(1.0 + r2 * 1.2) local x0, y0 = BX[i] + (r3 - 0.5) * 6, BY[i] + 2 local yl = 54 + ((h >> 24) & 7) local t1 = (-vy + sqrt(vy * vy + 2 * GRAV * (yl - y0))) / GRAV local juice = j == 4 or (j == 3 and r3 < 0.35) local x, y, a if u < t1 then x, y, a = x0 + vx * u, y0 + vy * u + 0.5 * GRAV * u * u, 1 elseif juice then return else local vb = -0.3 * (vy + GRAV * t1) local t2 = -2 * vb / GRAV local x1, w = x0 + vx * t1, u - t1 if w < t2 then x, y, a = x1 + 0.5 * vx * w, yl + vb * w + 0.5 * GRAV * w * w, 1 else x, y = x1 + 0.5 * vx * t2, yl a = 1 - (w - t2 - 45) / 20 if a <= 0 then return end if a > 1 then a = 1 end end end local ix, iy = floor(x + 0.5), floor(y + 0.5) local cw, ch, r, g, b if j == 1 then cw, ch, r, g, b = 2, 2, FR, FG, FB elseif j == 2 then cw, ch, r, g, b = 2, 1, FR, FG, FB elseif juice then cw, ch, r, g, b = 1, 1, JR, JG, JB else cw, ch, r, g, b = 1, 2, KR, KG, KB end if a >= 1 then rect(ix, iy, cw, ch, r, g, b) else blend_rect(ix, iy, cw, ch, r, g, b, a) end end local EMPTY, CUR = nil, nil local CURK, CURC = -1, -1 local function cloud(x, y, w) rect(x, y, w, 3, 248, 250, 255) rect(x + 2, y - 2, w - 6, 2, 248, 250, 255) rect(x + 4, y - 3, w // 3, 1, 248, 250, 255) rect(x + 1, y + 3, w - 2, 1, 214, 226, 252) end local function draw_bg() rect(0, 0, W, TABLE, SR, SG, SB) cloud(5, 9, 17) cloud(41, 5, 13) for j = 0, 3 do for i = 0, 10 do local c = 3 * ((i % 2) + (j % 2)) rect(i * 6, TABLE + j * 3, 6, 3, GING[c + 1], GING[c + 2], GING[c + 3]) end end end local FX0, FY0, FW, FH = CX0 - RF, CY, 2 * RF + 1, RF local function build() draw_bg() draw_rind(CX0, 0) EMPTY = gfx.snapshot(0, 0, W, H) end function on_settings() CURK, CURC = -1, -1 end local BIS = { 44, 32, 24, 18, 13 } local T0, ROCK, GROW = 14, 84, 45 local AMP, TAU, OM = 0.2, 24, 6.28318 / 28 function render() FSTEP = frame * STEP_HZ // gfx.FPS if not EMPTY then build() end local BI = BIS[cfg.speed or 3] or 24 local tl = T0 + (NB - 1) * BI local L = tl + ROCK + GROW local t, cyc = FSTEP % L, FSTEP // L if t < tl then local k = 0 if t >= T0 then k = (t - T0) // BI + 1 end if k ~= CURK or cyc ~= CURC then if cyc == CURC and k == CURK + 1 then stamp(k) else clear_mask() for i = 1, k do stamp(i) end end gfx.blit_rgb(EMPTY, 0, 0, W, H) draw_flesh(k > 0) CUR = gfx.snapshot(FX0, FY0, FW, FH) CURK, CURC = k, cyc else gfx.blit_rgb(EMPTY, 0, 0, W, H) gfx.blit_rgb(CUR, FX0, FY0, FW, FH) end elseif t < tl + ROCK then local u = t - tl local th = AMP * exp(-u / TAU) * sin(u * OM) draw_bg() draw_rind(CX0 + RO * th, th) else local p = (t - tl - ROCK) / GROW local e = 1 - (1 - p) * (1 - p) local lv = CY + RF - RF * e gfx.blit_rgb(EMPTY, 0, 0, W, H) for y = CY, CY + RF - 1 do local cov = y + 1 - lv if cov >= 1 then rect(FC0[y], y, FC1[y] - FC0[y] + 1, 1, FR, FG, FB) elseif cov > 0.04 then blend_rect(FC0[y], y, FC1[y] - FC0[y] + 1, 1, FR, FG, FB, cov) end end for k = 1, NK do if KY[k] >= lv then rect(KX[k], KY[k], 1, 2, KR, KG, KB) end end end for i = 1, NB do local u = t - (T0 + (i - 1) * BI) if u >= 0 and u < 150 then for j = 1, 4 do piece(i, j, u, cyc) end end end end