info = { type = "image", title = "Milk pour", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Milk carton pours a glass on a kitchen table", api = 44, } local ltr = ({ sk = { ["Drink"] = "Nápoj", ["Milk"] = "Mlieko", ["Cocoa"] = "Kakao", ["Speed"] = "Rýchlosť", ["Slow"] = "Pomaly", ["Normal"] = "Normálne", ["Fast"] = "Rýchlo" }, cs = { ["Drink"] = "Nápoj", ["Milk"] = "Mléko", ["Cocoa"] = "Kakao", ["Speed"] = "Rychlost", ["Slow"] = "Pomalu", ["Normal"] = "Normálně", ["Fast"] = "Rychle" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "drink", label = LT("Drink"), type = "int", default = "milk", opts = { keys = { "milk", "cocoa" }, LT("Milk"), LT("Cocoa") } }, { key = "speed", label = LT("Speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, LT("Slow"), LT("Normal"), LT("Fast") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, ceil, sin, cos, min, max, rad = math.floor, math.ceil, math.sin, math.cos, math.min, math.max, math.rad local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TOP, BASE = 50, 55 local PIV = 25 local GX0, GXC, GT = 45, 51, 37 local KEY = { 255, 0, 255 } local DRINKS = { milk = { face = { { 92, 152, 232 }, { 66, 122, 208 } }, panel = { 36, 72, 156 }, line = { 150, 198, 244 }, side = { 44, 90, 172 }, roof = { 156, 204, 246 }, roofS = { 112, 164, 222 }, fin = { 210, 234, 252 }, icon = { 244, 250, 255 }, liq = { 250, 250, 244 }, top = { 255, 255, 255 }, sh = { 214, 220, 228 }, stream = { 250, 250, 246 }, drop = { "...#...", "...#...", "..###..", "..###..", ".#####.", "#######", "#######", "#######", ".#####.", "..###.." }, }, cocoa = { face = { { 178, 106, 68 }, { 144, 82, 52 } }, panel = { 98, 54, 36 }, line = { 228, 172, 128 }, side = { 114, 66, 42 }, roof = { 232, 196, 156 }, roofS = { 190, 150, 116 }, fin = { 248, 224, 190 }, icon = { 238, 198, 150 }, liq = { 128, 74, 48 }, top = { 214, 172, 128 }, sh = { 98, 56, 36 }, stream = { 150, 88, 54 }, drop = { "..###..", ".#####.", "###.###", "####.##", "###.###", "##.####", "###.###", "####.##", ".#####.", "..###.." }, }, } local D = DRINKS.milk local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function c3(c) return c[1], c[2], c[3] end local function wall(y) return mix({ 28, 76, 126 }, { 46, 106, 158 }, y / TOP) end local FACE = {} local function carton(u, v) if v >= 0 and v < 24 then if u < 0 or u >= 22 then return end if u >= 17 then return mix(D.side, D.panel, v / 40) end if u == 0 or v == 0 then return c3(D.line) end if u >= 3 and u <= 13 and v >= 5 and v <= 18 then if u == 3 or u == 13 or v == 5 or v == 18 then return c3(D.line) end local row = D.drop[v - 6] if row and u >= 5 and u <= 11 and row:byte(u - 4) == 35 then return c3(D.icon) end return c3(D.panel) end local f = FACE[u] return f[1], f[2], f[3] elseif v < 0 and v >= -6 then local r = -v if u >= r // 2 and u <= 21 - r // 2 then return c3(u >= 17 and D.roofS or D.roof) end elseif v == -7 or v == -8 then if u >= 3 and u <= 19 then return c3(D.fin) end if u >= 20 + (v == -8 and 1 or 0) and u <= 22 then return c3(D.roof) end end end local POSES = { { a = 0, lift = 0 }, { a = 24, lift = -2 }, { a = 50, lift = -6 } } local POSE_AT = { { 0, 30, 1 }, { 30, 40, 2 }, { 40, 142, 3 }, { 142, 150, 2 } } local SHADOW = { { 3, 24, 0.3 }, { 10, 20, 0.24 }, { 18, 12, 0.18 } } local SPX, SPY local function setup_poses() for i = 1, #POSES do local P = POSES[i] P.cs, P.sn = cos(rad(P.a)), sin(rad(P.a)) local x0, y0, x1, y1 = 99, 99, -99, -99 for _, c in ipairs({ { 0, -9 }, { 23, -9 }, { 23, 24 }, { 0, 24 } }) do local dx, dy = c[1] - 22, c[2] - 24 local x, y = PIV + dx * P.cs - dy * P.sn, BASE + P.lift + dx * P.sn + dy * P.cs x0, y0, x1, y1 = min(x0, x), min(y0, y), max(x1, x), max(y1, y) end P.x, P.y = max(0, floor(x0)), max(0, floor(y0)) P.w, P.h = min(W, ceil(x1) + 1) - P.x, min(H, ceil(y1) + 1) - P.y end local P = POSES[3] SPX = PIV + (23 - 22) * P.cs - (-8 - 24) * P.sn SPY = BASE + P.lift + (23 - 22) * P.sn + (-8 - 24) * P.cs end local function pose_rows(P, ya, yb) for Y = ya, yb do for X = P.x, P.x + P.w - 1 do local dx, dy = X + 0.5 - PIV, Y + 0.5 - (BASE + P.lift) local r, g, b = carton(floor(22 + dx * P.cs + dy * P.sn), floor(24 - dx * P.sn + dy * P.cs)) if r then px(X, Y, r, g, b) end end end end local function gl(i) return GX0 + floor(i * 2 / 17 + 0.5) end local function gr(i) return GX0 + 13 - floor(i * 2 / 17 + 0.5) end local function glass_back() for i = 0, 15 do local y = GT + i local wr, wg, wb = wall(y) rect(gl(i) + 1, y, gr(i) - gl(i) - 1, 1, floor(wr + (210 - wr) * 0.14), floor(wg + (236 - wg) * 0.14), floor(wb + (255 - wb) * 0.14)) end end local function glass_front() for i = 0, 17 do local y, l, r = GT + i, gl(i), gr(i) if i >= 16 then rect(l, y, r - l + 1, 1, 150, 196, 236) elseif i == 0 then rect(l, y, r - l + 1, 1, 206, 234, 255) else px(l, y, 176, 216, 246) px(r, y, 150, 194, 232) if i >= 2 and i <= 10 then px(l + 1, y, 226, 242, 255) end end end rect(gl(16), GT + 17, gr(16) - gl(16) + 1, 1, 118, 166, 214) end local function room() for y = 0, TOP - 1 do rect(0, y, W, 1, wall(y)) end for r = 0, 3 do local y = 12 + r * 12 rect(0, y, W, 1, mix({ wall(y) }, { 120, 170, 220 }, 0.12)) for x = (r % 2) * 7, W - 1, 14 do rect(x, y - 11, 1, 11, mix({ wall(y) }, { 120, 170, 220 }, 0.12)) end end rect(0, 48, W, 1, mix({ wall(48) }, { 120, 170, 220 }, 0.12)) for y = TOP, 57 do rect(0, y, W, 1, mix({ 210, 142, 82 }, { 184, 116, 62 }, (y - TOP) / 7)) end rect(0, TOP, W, 1, 228, 170, 104) for i = 1, 30 do local h = hash32(i * 0x9E3779B1) local len = 3 + umod(h >> 8, 8) blend_rect(umod(h, W), TOP + 1 + umod(h >> 16, 7), len, 1, 150, 92, 48, 0.4) end rect(0, 54, W, 1, 170, 106, 56) for y = 58, H - 1 do rect(0, y, W, 1, mix({ 150, 94, 54 }, { 108, 66, 38 }, (y - 58) / 5)) end rect(0, 58, W, 1, 180, 122, 72) blend_rect(GX0 - 1, BASE + 1, 16, 2, 70, 36, 20, 0.3) end local BG, GB, GF, POSE, PART = nil, nil, nil, {}, nil local STAGES, STAGE = {}, 1 local function cover() if BG then blit_rgb(BG, 0, 0, W, H) end end local function plan() STAGES = { function() room(); BG = snapshot(0, 0, W, H) end, function() rect(GX0, GT, 14, 18, c3(KEY)); glass_back(); GB = snapshot(GX0, GT, 14, 18) end, function() rect(GX0, GT, 14, 18, c3(KEY)); glass_front(); GF = snapshot(GX0, GT, 14, 18) end, } for i = 1, #POSES do local P = POSES[i] local mid = P.y + P.h // 2 STAGES[#STAGES + 1] = function() rect(P.x, P.y, P.w, P.h, c3(KEY)) pose_rows(P, P.y, mid - 1) PART = snapshot(P.x, P.y, P.w, P.h) end STAGES[#STAGES + 1] = function() blit_rgb(PART, P.x, P.y, P.w, P.h) pose_rows(P, mid, P.y + P.h - 1) POSE[i] = snapshot(P.x, P.y, P.w, P.h) end end end function on_settings() D = DRINKS[cfg.drink] or DRINKS.milk for u = 0, 16 do FACE[u] = { mix(D.face[1], D.face[2], u / 16) } end setup_poses() plan() BG, GB, GF, STAGE = nil, nil, nil, 1 end local CYC = 300 local SPEED = { slow = 0.7, normal = 1, fast = 1.45 } local FULL = 13 local function clamp01(v) return v < 0 and 0 or (v > 1 and 1 or v) end local function smooth(s) s = clamp01(s) return s * s * (3 - 2 * s) end local SPARK = {} for i = 1, 8 do local h = hash32(i * 0x85EBCA6B) SPARK[i] = { x = 4 + umod(h, 56), y = 4 + umod(h >> 8, 40), ph = umod(h >> 16, 628) / 100, w = 0.16 + umod(h >> 24, 12) / 100 } end local function level_at(t) if t < 46 then return 0 end if t < 128 then return FULL * (t - 46) / 82 end if t < 210 then return FULL end if t < 222 then return FULL - FULL * 0.55 * smooth((t - 210) / 12) end if t < 235 then return FULL * 0.45 end if t < 247 then return FULL * 0.45 * (1 - smooth((t - 235) / 12)) end return 0 end function render() if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 if BG then blit_rgb(BG, 0, 0, W, H) end gfx.ready(false) return end local t = (frame * (SPEED[cfg.speed] or 1)) % CYC blit_rgb(BG, 0, 0, W, H) local pi = 1 for k = 1, #POSE_AT do local s = POSE_AT[k] if t >= s[1] and t < s[2] then pi = s[3] end end local P = POSES[pi] local sh = SHADOW[pi] blend_rect(sh[1], BASE + 1, sh[2], 2, 70, 36, 20, sh[3]) blit_rgb(POSE[pi], P.x, P.y, P.w, P.h, 255, 0, 255) local lift = 0 if t >= 210 and t < 222 then lift = 4 * sin((t - 210) / 12 * 3.14159) elseif t >= 235 and t < 247 then lift = 4 * sin((t - 235) / 12 * 3.14159) end lift = floor(lift + 0.5) local lv = level_at(t) blit_rgb(GB, GX0, GT - lift, 14, 18, 255, 0, 255) local ys = GT + 16 - lv if lv > 0.05 then for y = floor(ys), GT + 15 do local i = y - GT local cov = clamp01(y + 1 - ys) local l, r = gl(i) + 1, gr(i) - 1 if cov >= 1 then rect(l, y - lift, r - l, 1, c3(y == floor(ys) + 1 and D.top or D.liq)) px(r, y - lift, c3(D.sh)) else blend_rect(l, y - lift, r - l + 1, 1, D.top[1], D.top[2], D.top[3], cov) end end end local uh, ut = clamp01((t - 46) / 5) * 2, clamp01((t - 126) / 5) * 2 if uh > ut and t < 142 then local xm, ym = GXC, GT - 1 local iy = floor(ys) local s = D.stream local u0, u1 = ut, min(uh, 1) if u1 > u0 then for cx = floor(SPX + (xm - SPX) * u0), floor(SPX + (xm - SPX) * u1) do local ua, ub = (cx - SPX) / (xm - SPX), (cx + 1 - SPX) / (xm - SPX) local ya, yb = SPY + (ym - SPY) * ua * ua, SPY + (ym - SPY) * min(1, ub) ^ 2 rect(cx, floor(ya), 1, max(2, floor(yb) - floor(ya) + 1), c3(s)) end end if uh > 1 then local ya = ym + (max(ut, 1) - 1) * (iy - ym) local yb = ym + (uh - 1) * (iy - ym) rect(xm, floor(ya), 2, max(1, floor(yb) - floor(ya) + 1), c3(s)) end if uh >= 2 then for k = 0, 3 do local ss = (t * 0.5 + k * 2.5) % 10 local h = (1.8 + 0.15 * k) * ss - 0.18 * ss * ss blend(floor(xm + 1 + (k % 2 == 0 and 1 or -1) * (0.3 + 0.1 * k) * ss + 0.5), floor(iy - h), D.top[1], D.top[2], D.top[3], 0.9) end end end blit_rgb(GF, GX0, GT - lift, 14, 18, 255, 0, 255) for i = 1, #SPARK do local k = SPARK[i] local a = max(0, sin(t * k.w + k.ph)) ^ 3 if a > 0.05 then blend(k.x, k.y, 255, 246, 206, a) blend(k.x - 1, k.y, 255, 246, 206, a * 0.5) blend(k.x + 1, k.y, 255, 246, 206, a * 0.5) blend(k.x, k.y - 1, 255, 246, 206, a * 0.5) blend(k.x, k.y + 1, 255, 246, 206, a * 0.5) end end end