info = { type = "image", title = "Tea window", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Steaming cup on a wooden table by a window", api = 44, } local tr = ({ sk = { ["Drink"] = "Nápoj", ["Tea"] = "Čaj", ["Coffee"] = "Káva", ["Green tea"] = "Zelený čaj", ["Steam"] = "Para", ["Light"] = "Slabá", ["Normal"] = "Bežná", ["Rich"] = "Silná" }, cs = { ["Drink"] = "Nápoj", ["Tea"] = "Čaj", ["Coffee"] = "Káva", ["Green tea"] = "Zelený čaj", ["Steam"] = "Pára", ["Light"] = "Slabá", ["Normal"] = "Běžná", ["Rich"] = "Silná" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "drink", label = T("Drink"), type = "int", default = "tea", opts = { keys = { "tea", "coffee", "green" }, T("Tea"), T("Coffee"), T("Green tea") } }, { key = "steam", label = T("Steam"), type = "int", default = "normal", opts = { keys = { "light", "normal", "rich" }, T("Light"), T("Normal"), T("Rich") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local CX, CY, RX, RY, DEPTH = 22, 44, 12, 3.2, 11 local SX, SY, SRX, SRY = 22, 57, 17, 4 local DRINKS = { tea = { 178, 98, 30 }, coffee = { 66, 38, 26 }, green = { 166, 158, 66 } } local TEA, STEAM = DRINKS.tea, 1 local function clamp(v) return v < 0 and 0 or (v > 255 and 255 or floor(v + 0.5)) end local function span(y, a, b, r, g, bl, al, h) h = h or 1 local fa, fb = floor(a), floor(b) if fa == fb then if b > a then blend_rect(fa, y, 1, h, r, g, bl, (b - a) * al) end return end if a > fa then blend_rect(fa, y, 1, h, r, g, bl, (1 - a + fa) * al); fa = fa + 1 end if fb > fa then blend_rect(fa, y, fb - fa, h, r, g, bl, al) end if b > fb then blend_rect(fb, y, 1, h, r, g, bl, (b - fb) * al) end end local function step_room() for x = 0, W - 1 do local u = max(0, min(1, (x - 8) / 40)) u = u * u * (3 - 2 * u) rect(x, 0, 1, 35, 54 + floor(50 * u), 62 + floor(52 * u), 66 + floor(44 * u)) end for y = 0, 34 do blend_rect(0, y, W, 1, 0, 0, 0, 0.3 * (1 - y / 34)) end local panes = { { 37, 3, 12, 10, 232, 244, 218 }, { 52, 3, 11, 10, 250, 248, 236 }, { 37, 16, 12, 10, 250, 246, 230 }, { 52, 16, 11, 10, 252, 248, 236 } } rect(33, 0, 31, 30, 136, 146, 138) blend_rect(33, 0, 31, 30, 60, 66, 62, 0.35) for i = 1, 4 do local p = panes[i] blend_rect(p[1] - 2, p[2] - 2, p[3] + 4, p[4] + 4, p[5], p[6], p[7], 0.3) blend_rect(p[1] - 1, p[2] - 1, p[3] + 2, p[4] + 2, p[5], p[6], p[7], 0.5) rect(p[1], p[2], p[3], p[4], p[5], p[6], p[7]) end rect(30, 27, 34, 3, 190, 184, 168) blend_rect(30, 27, 34, 1, 255, 255, 240, 0.4) for _, d in ipairs({ { 31, 9, 3 }, { 33, 22, 4 }, { 59, 31, 3 } }) do for j = -d[3], d[3] do local hw = floor(sqrt(d[3] * d[3] - j * j) + 0.5) blend_rect(d[1] - hw, d[2] + j, 2 * hw + 1, 1, 255, 244, 200, 0.1) end end for y = 35, H - 1 do local t = (y - 35) / 28 local n = umod(hash32(y * 0x9E3779B1), 9) - 4 rect(0, y, W, 1, 158 - floor(36 * t) + n, 108 - floor(28 * t) + n, 64 - floor(16 * t) + n) end for i = 1, 70 do local k = hash32(i * 0x85EBCA6B) local y, x, len = 36 + umod(k, 27), umod(k >> 8, W), 6 + umod(k >> 16, 20) if i % 2 == 0 then blend_rect(x, y, len, 1, 255, 220, 170, 0.07) else blend_rect(x, y, len, 1, 30, 14, 4, 0.12) end end for _, y in ipairs({ 43, 52, 61 }) do blend_rect(0, y, W, 1, 28, 14, 6, 0.28) end rect(0, 35, W, 1, 62, 42, 30) for y = 38, 58 do local x0 = 34 + (y - 38) * 0.45 local edge = (y < 40 or y > 56) and 0.5 or 1 span(y, x0 - 2, 70, 255, 232, 180, 0.06 * edge) span(y, x0, 70, 255, 232, 180, 0.08 * edge) span(y, x0 + 2, 70, 255, 232, 180, 0.08 * edge) local xm = 50 + (y - 38) * 0.4 span(y, xm - 1.5, xm + 1.5, 40, 20, 8, 0.13 * edge) end blend_rect(36, 47, 28, 2, 40, 20, 8, 0.12) end local function step_saucer() for j = -4, 4 do local hw = sqrt(1 - (j / 4.5) ^ 2) * 19 span(SY + 2 + j, SX - 8 - hw, SX + 14 + hw * 0.4, 18, 8, 2, 0.34) end for y = SY - SRY, SY + SRY + 1 do for x = SX - SRX, SX + SRX do local dx, dy = (x - SX) / SRX, (y - SY) / SRY local under = (x - SX) / SRX if dx * dx + dy * dy <= 1 then local well = ((x - SX) / 11) ^ 2 + ((y - SY + 0.4) / 2.5) ^ 2 local s = 0.66 + 0.26 * dx - 0.06 * dy - (well <= 1 and 0.05 or 0) if well > 1 and well < 1.35 then s = s + 0.1 end px(x, y, clamp(238 * s), clamp(240 * s), clamp(242 * s)) elseif under * under + ((y - 1 - SY) / SRY) ^ 2 <= 1 then px(x, y, 126, 132, 142) end end end for i = 0, 9 do px(30 + i, 58 - floor(i * 0.3), 206, 208, 214) end px(29, 58, 232, 232, 236); px(30, 59, 160, 164, 172) end local function porcelain(nx, ny, base) local nz = sqrt(max(0, 1 - nx * nx - ny * ny)) local d = nx * 0.55 - ny * 0.45 + nz * 0.7 local s = 0.62 + 0.42 * max(0, d) local r, g, b = base[1] * s, base[2] * s, base[3] * s if d > 0.9 then local k = (d - 0.9) * 260; r, g, b = r + k, g + k, b + k end if ny > 0.45 then r, g, b = r + 22 * (ny - 0.45), g + 8 * (ny - 0.45), b end return clamp(r), clamp(g), clamp(b) end local function step_cup() local WHITE, BLUE = { 236, 240, 244 }, { 98, 138, 190 } for y = 44, 55 do for x = 32, 40 do local d = sqrt(((x - 35.5) / 3.6) ^ 2 + ((y - 49.5) / 4.6) ^ 2) if d <= 1 and d >= 0.5 then local s = 0.8 + 0.2 * (1 - d) px(x, y, clamp(238 * s), clamp(242 * s), clamp(244 * s)) end end end for y = CY, CY + DEPTH do local ny = (y - CY) / DEPTH for x = CX - RX, CX + RX do local nx = (x - CX) / RX if y <= CY + DEPTH * sqrt(max(0, 1 - nx * nx)) then local band = (y >= CY + 4 and y <= CY + 5) and BLUE or WHITE px(x, y, porcelain(nx, ny, band)) end end end for y = floor(CY - RY), CY + 4 do for x = CX - RX, CX + RX do local nx, ny = (x - CX) / RX, (y - CY) / RY local d = nx * nx + ny * ny if d <= 1 then if d > 0.62 or (y > CY and d > 0.5) then px(x, y, clamp(244 * (0.84 + 0.14 * nx)), clamp(246 * (0.84 + 0.14 * nx)), clamp(248 * (0.84 + 0.14 * nx))) else local s = 0.8 + 0.16 * nx - 0.1 * ny px(x, y, clamp(226 * s), clamp(218 * s), clamp(200 * s)) end end end end end local STEPS = { step_room, step_saucer, step_cup } local SNAP, STAGE = nil, 0 local WISPS = { { x = 20.5, ph = 0.0, drift = 0.10, amp = 2.2 }, { x = 23.5, ph = 2.1, drift = 0.14, amp = 2.8 }, { x = 26, ph = 4.0, drift = 0.18, amp = 2.0 } } local HMAX = 37 local function steam(t) for i = 1, #WISPS do local w = WISPS[i] for h = 1, HMAX, 2 do local k = h / HMAX local c = w.x + w.drift * h + w.amp * sin(0.2 * h - 0.09 * t + w.ph) * min(1, h / 9) local hw = 0.9 + 0.1 * h local a = STEAM * 0.34 * (1 - k) * (0.6 + 0.4 * sin(0.27 * h - 0.16 * t + w.ph * 2)) * min(1, h / 3) local y = CY - 1 - h span(y, c - hw, c + hw, 255, 252, 244, a * 0.5, 2) span(y, c - hw * 0.45, c + hw * 0.45, 255, 252, 244, a * 0.6, 2) end end end local function tea(t) local rx, ry = RX - 2.4, RY - 1.5 for j = floor(-ry), floor(ry) + 1 do local hw = rx * sqrt(max(0, 1 - ((j - 0.2) / (ry + 0.4)) ^ 2)) if hw > 0.5 then local y = CY + j local f = 1.08 - 0.07 * (j + ry) / (2 * ry) span(y, CX - hw, CX + hw, clamp(TEA[1] * f), clamp(TEA[2] * f), clamp(TEA[3] * f), 1) end end local gx = CX + 3.5 * sin(t * 0.04 + 0.5) local ga = 0.25 + 0.3 * sin(t * 0.09) ^ 2 span(CY - 1, gx - 1.5, gx + 1.5, 255, 250, 235, ga) span(CY, gx - 3, gx + 3.5, 255, 250, 235, ga * 0.4) blend(CX + 6, CY, 255, 250, 235, 0.15 + 0.2 * sin(t * 0.07 + 1) ^ 2) end function on_settings() TEA = DRINKS[cfg.drink] or DRINKS.tea STEAM = ({ light = 0.7, normal = 1, rich = 1.45 })[cfg.steam] or 1 end function render() if STAGE < #STEPS then if SNAP then gfx.blit_rgb(SNAP, 0, 0, W, H) end STAGE = STAGE + 1 STEPS[STAGE]() SNAP = gfx.snapshot(0, 0, W, H) if STAGE < #STEPS then gfx.ready(false) end else gfx.blit_rgb(SNAP, 0, 0, W, H) end if STAGE == #STEPS then tea(frame) steam(frame) end end