info = { type = "image", title = "Rainbow cocoa", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Steaming rainbow cocoa with marshmallows", api = 33, } local tr = ({ sk = { ["Rainbow speed"] = "Rýchlosť dúhy", ["Steam"] = "Para", ["Marshmallows"] = "Marshmallow" }, cs = { ["Rainbow speed"] = "Rychlost duhy", ["Steam"] = "Pára", ["Marshmallows"] = "Marshmallow" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "flow", label = T("Rainbow speed"), type = "int", min = 0, max = 5, default = 2 }, { key = "steam", label = T("Steam"), type = "bool", default = true }, { key = "marsh", label = T("Marshmallows"), type = "int", min = 0, max = 4, default = 3 }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, cos, sqrt, atan = math.floor, math.sin, math.cos, math.sqrt, math.atan local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local WALL_T, WALL_B = { 112, 92, 176 }, { 236, 156, 150 } local TABLE, TABLE_L = { 160, 98, 62 }, { 200, 136, 88 } local TABLE_G, TABLE_S = { 140, 84, 52 }, { 124, 72, 46 } local OUT = { 70, 38, 74 } local CREAM, CREAM_D = { 252, 238, 222 }, { 214, 192, 182 } local COCOA, COCOA_L = { 122, 66, 44 }, { 156, 94, 62 } local STICK, STICK_D = { 250, 248, 244 }, { 196, 190, 200 } local POP1, POP2 = { 255, 86, 150 }, { 255, 242, 246 } local POP_O = { 132, 30, 76 } local MW, MW_L, MW_D = { 250, 232, 240 }, { 255, 252, 252 }, { 222, 196, 214 } local MP = { 255, 196, 220 } local X0, X1 = 12, 41 local Y_RIM, Y0, Y1 = 22, 28, 51 local function wall(y) local t = y / 51 return floor(WALL_T[1] + (WALL_B[1] - WALL_T[1]) * t + 0.5), floor(WALL_T[2] + (WALL_B[2] - WALL_T[2]) * t + 0.5), floor(WALL_T[3] + (WALL_B[3] - WALL_T[3]) * t + 0.5) end local function hsv(h, s, v) h = (h % 360) / 60 local i = floor(h) local f = h - i local p, q, r = v * (1 - s), v * (1 - s * f), v * (1 - s * (1 - f)) local R, G, B if i == 0 then R, G, B = v, r, p elseif i == 1 then R, G, B = q, v, p elseif i == 2 then R, G, B = p, v, r elseif i == 3 then R, G, B = p, q, v elseif i == 4 then R, G, B = r, p, v else R, G, B = v, p, q end return floor(R * 255 + 0.5), floor(G * 255 + 0.5), floor(B * 255 + 0.5) end local BG = nil local function build_bg() for y = 0, 51 do rect(0, y, W, 1, wall(y)) end for y = 4, 48, 8 do for x = (y // 8 % 2) * 4 + 2, W - 1, 8 do local r, g, b = wall(y) px(x, y, r + 14, g + 12, b + 12) end end rect(0, 52, W, 1, c3(TABLE_L)) rect(0, 53, W, H - 53, c3(TABLE)) for i = 1, 14 do local h = hash32(i * 0x9E3779B1) rect(umod(h, W), 55 + umod(h >> 8, 8), 4 + umod(h >> 16, 8), 1, c3(TABLE_G)) end rect(10, 53, 44, 2, c3(TABLE_S)); rect(13, 55, 38, 1, c3(TABLE_S)) rect(42, 29, 10, 18, c3(OUT)) rect(43, 30, 8, 16, c3(CREAM)) rect(43, 43, 8, 3, c3(CREAM_D)) rect(45, 32, 4, 12, c3(OUT)) for y = 33, 42 do local r, g, b = wall(y); rect(46, y, 2, 1, r, g, b) end px(51, 29, wall(29)); px(51, 46, wall(46)) rect(X0 - 1, Y_RIM, X1 - X0 + 3, Y1 - Y_RIM + 1, c3(OUT)) rect(X0, Y1 + 1, X1 - X0 + 1, 1, c3(OUT)) rect(X0 + 2, Y1 + 2, X1 - X0 - 3, 1, c3(OUT)) px(X0 - 1, Y_RIM, wall(Y_RIM)); px(X1 + 1, Y_RIM, wall(Y_RIM)) rect(X0 + 1, Y_RIM + 1, X1 - X0 - 1, 1, c3(CREAM)) rect(X0, Y_RIM + 2, X1 - X0 + 1, 3, c3(COCOA)) rect(X0 + 3, Y_RIM + 2, 9, 1, c3(COCOA_L)); rect(X0 + 18, Y_RIM + 3, 7, 1, c3(COCOA_L)) rect(X0, Y_RIM + 5, X1 - X0 + 1, 1, c3(CREAM)) for k = 0, 13 do local x, y = 37 + k * 9 // 13, Y_RIM + 3 - k px(x, y, c3(STICK)); px(x + 1, y, c3(STICK_D)) end local cx, cy, r = 49, 8, 5.6 for y = cy - 7, cy + 7 do for x = cx - 7, cx + 7 do local dx, dy = x - cx, y - cy local d = sqrt(dx * dx + dy * dy) if d <= r then local a = atan(dy, dx) / TAU local band = floor((a + d / 4.2) * 3) % 2 px(x, y, c3(band == 0 and POP1 or POP2)) elseif d <= r + 1 then px(x, y, c3(POP_O)) end end end BG = gfx.snapshot(0, 0, W, H) end local MX = { 15, 21, 27, 32 } local MPH = { 0, 2.1, 4.0, 1.1 } local MCOL = { MW, MP, MW, MP } local function marsh(n, t) gfx.clip(X0, 0, X1 - X0 + 1, Y_RIM + 5) for i = 1, n do local y = Y_RIM - 0.4 + 0.6 * sin(t * 0.07 + MPH[i]) local iy = floor(y) local f = y - iy local x, c = MX[i], MCOL[i] gfx.blend_rect(x, iy, 4, 1, MW_L[1], MW_L[2], MW_L[3], 1 - f) rect(x, iy + 1, 4, 1, c3(MW_L)) rect(x, iy + 2, 4, Y_RIM + 5 - iy - 2, c3(c)) rect(x + 3, iy + 2, 1, Y_RIM + 5 - iy - 2, c3(MW_D)) end gfx.clip() end local SB = { 18, 25, 32 } local SPH = { 0, 2.3, 4.4 } local function steam(t) for s = 1, 3 do for j = 0, 17 do local h = (t * 0.33 + j * 1.45 + s * 3.1) % 26 local x = SB[s] + (0.8 + h * 0.12) * sin(h * 0.3 - t * 0.05 + SPH[s]) local y = Y_RIM - h local a = 0.42 * (1 - h / 26) if h < 3 then a = a * h / 3 end local ix, iy = floor(x + 0.5), floor(y) local f = y - iy blend(ix, iy, 255, 255, 255, a * (1 - f)) blend(ix, iy + 1, 255, 255, 255, a * f) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local off = t * (cfg.flow or 2) * 0.6 for y = Y0, Y1 do local hue = (y - Y0) * 13 - off local a, b = X0, X1 if y == Y1 then a, b = X0 + 1, X1 - 1 end rect(a, y, b - a + 1, 1, hsv(hue, 0.62, 1.0)) rect(b - 4, y, 4, 1, hsv(hue, 0.70, 0.80)) if y > Y0 + 1 and y < Y1 - 3 then rect(a + 2, y, 2, 1, hsv(hue, 0.25, 1.0)) end end local n = cfg.marsh or 3 if n > 0 then marsh(n, t) end if cfg.steam ~= false then steam(t) end end