info = { type = "image", title = "Juice on the beach", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Glass of juice with ice, surf behind it", api = 44, } local tr = ({ sk = { ["Drink"] = "Nápoj", ["Orange"] = "Pomaranč", ["Cherry"] = "Čerešňa", ["Lime"] = "Limetka", ["Surf"] = "Príboj", ["Bubbles"] = "Bublinky", ["Condensation"] = "Kondenzát" }, cs = { ["Drink"] = "Nápoj", ["Orange"] = "Pomeranč", ["Cherry"] = "Třešeň", ["Lime"] = "Limetka", ["Surf"] = "Příboj", ["Bubbles"] = "Bublinky", ["Condensation"] = "Kondenzát" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "drink", label = T("Drink"), type = "int", default = "orange", opts = { keys = { "orange", "cherry", "lime" }, T("Orange"), T("Cherry"), T("Lime") } }, { key = "surf", label = T("Surf"), type = "int", min = 1, max = 5, default = 3 }, { key = "bubbles", label = T("Bubbles"), type = "int", min = 0, max = 8, default = 5 }, { key = "drop", label = T("Condensation"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot, clip = gfx.blit_rgb, gfx.snapshot, gfx.clip local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local CX = 32 local LEVEL, BOT = 23, 52 local SHORE = 40 local DRINKS = { orange = { top = { 255, 190, 70 }, bot = { 224, 84, 34 }, surf = { 255, 214, 130 }, straw = { 206, 40, 78 } }, cherry = { top = { 255, 96, 130 }, bot = { 168, 22, 68 }, surf = { 255, 170, 190 }, straw = { 250, 240, 230 } }, lime = { top = { 206, 244, 96 }, bot = { 60, 168, 56 }, surf = { 232, 252, 150 }, straw = { 255, 160, 60 } }, } local D = DRINKS.orange local function lerp(a, b, t) return a + (b - a) * t end local function mix(c1, c2, t) return lerp(c1[1], c2[1], t), lerp(c1[2], c2[2], t), lerp(c1[3], c2[3], t) end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function hw(y) return 11 - 2 * (y - 14) / 40 end local SEA_TOP, SEA_BOT = { 30, 160, 232 }, { 126, 236, 224 } local WET, DRY = { 212, 182, 114 }, { 248, 228, 164 } local function sea_rows(ya, yb) for y = ya, yb do local r, g, b = mix(SEA_TOP, SEA_BOT, ((y - 24) / 22) ^ 0.9) rect(0, y, W, 1, rgb(r, g, b)) end end local function sand_rows(ya, yb) for y = ya, yb do local r, g, b = mix(WET, DRY, min(1, (y - 36) / 12) ^ 0.8) rect(0, y, W, 1, rgb(r, g, b)) end end local SEAC, SANDC = {}, {} local function zone_x(f) for x = 0, 18, 2 do f(x) end for x = 46, 62, 2 do f(x) end end local function backdrop() for y = 0, 23 do local t = y / 23 rect(0, y, W, 1, 255, floor(lerp(222, 244, t) + 0.5), floor(lerp(128, 196, t) + 0.5)) end sea_rows(24, 47) for i = 1, 26 do local h = hash32(i * 0x9E3779B1) local y = 25 + umod(h, 14) local len = 4 + (y - 24) // 2 + umod(h >> 8, 6) if umod(h >> 14, 2) == 0 then blend_rect(umod(h >> 16, W), y, len, 1, 255, 255, 255, 0.16) else blend_rect(umod(h >> 16, W), y, len, 1, 20, 120, 210, 0.18) end end end local function shore() sand_rows(36, 47) zone_x(function(x) SANDC[x] = snapshot(x, 36, 2, 12) end) sea_rows(36, 47) zone_x(function(x) SEAC[x] = snapshot(x, 36, 2, 12) end) sea_rows(36, SHORE - 1) sand_rows(SHORE, 47) end local function table_top() sand_rows(48, 51) for y = 52, 63 do local t = (y - 52) / 11 rect(0, y, W, 1, rgb(lerp(204, 158, t), lerp(140, 98, t), lerp(70, 46, t))) end rect(0, 52, W, 1, 236, 182, 100) blend_rect(0, 53, W, 1, 90, 50, 20, 0.35) blend_rect(0, 58, W, 1, 90, 50, 20, 0.3) for i = 1, 16 do local h = hash32(i * 0x85EBCA6B) blend_rect(umod(h, W), 54 + umod(h >> 8, 9), 6 + umod(h >> 14, 9), 1, 255, 226, 160, 0.14) end for dy = -3, 3 do local k = sqrt(1 - dy * dy / 12.25) blend_rect(floor(CX + 6 - 14 * k), 56 + dy, floor(28 * k), 1, 70, 36, 14, 0.42) end for dy = -2, 2 do local k = sqrt(1 - dy * dy / 6.25) blend_rect(floor(CX + 10 - 8 * k), 56 + dy, floor(16 * k), 1, D.top[1], floor(D.top[2] * 0.7), floor(D.top[3] * 0.5), 0.4) end end local function colour(x, y) local ty = (y - LEVEL) / 25 local r, g, b = mix(D.top, D.bot, ty) local k = 1 - 0.16 * (x - CX) / 10 - 0.05 + 0.04 * sin(y * 0.9 + x * 0.35) return rgb(r * k, g * k, b * k) end local function juice(ya, yb) for y = ya, yb do local w = hw(y) - 1.3 if y > BOT then w = w * sqrt(max(0, 1 - ((y - BOT) / 2.6) ^ 2)) end for x = CX - floor(w), CX + floor(w) do px(x, y, colour(x, y)) end end end local function surface() local rx = hw(LEVEL) - 1.3 for dy = -2, 2 do local k = sqrt(1 - dy * dy / 7) local half = floor(rx * k) rect(CX - half, LEVEL + dy, 2 * half + 1, 1, rgb(mix(D.surf, D.top, 0.25 + 0.15 * (dy + 2)))) end blend_rect(CX + 2, LEVEL - 1, 5, 1, 255, 255, 255, 0.5) end local function glass() for y = 15, 53 do local w = hw(y) local xl, xr = floor(CX - w + 0.5), floor(CX + w + 0.5) blend_rect(xl, y, 1, 1, 255, 255, 255, 0.9) blend_rect(xl + 1, y, 1, 1, 255, 255, 255, 0.22) blend_rect(xr, y, 1, 1, 220, 245, 250, 0.6) if y < LEVEL then blend_rect(xl + 1, y, xr - xl - 1, 1, 255, 255, 255, 0.07) end end for i = -11, 11 do local s = sqrt(1 - (i / 11) ^ 2) blend_rect(CX + i, 14 - floor(2.4 * s + 0.5), 1, 1, 255, 255, 255, 0.55) blend_rect(CX + i, 14 + floor(2.4 * s + 0.5), 1, 1, 255, 255, 255, 0.95) end for i = -9, 9 do local s = sqrt(1 - (i / 9.5) ^ 2) blend_rect(CX + i, BOT + 1 + floor(2 * s + 0.5), 1, 1, 255, 255, 255, 0.85) blend_rect(CX + i, BOT - 1, 1, 2, 210, 240, 245, 0.18) end local xl = floor(CX - hw(30) + 0.5) blend_rect(xl + 2, 17, 1, 28, 255, 255, 255, 0.55) blend_rect(xl + 2, 47, 1, 4, 255, 255, 255, 0.4) blend_rect(xl + 4, 20, 1, 8, 255, 255, 255, 0.28) blend_rect(floor(CX + hw(30) - 3), 19, 1, 12, 255, 255, 255, 0.35) end local BG, STAGE = nil, 1 local STAGES = { backdrop, shore, table_top, function() juice(LEVEL, 37) end, function() juice(38, BOT + 2) end, surface, glass, function() BG = snapshot(0, 0, W, H) end } local PART function on_settings() D = DRINKS[cfg.drink] or DRINKS.orange BG, PART, STAGE = nil, nil, 1 end local BUB = {} for i = 1, 8 do local h = hash32(i * 0xC2B2AE35) BUB[i] = { x = CX - 7 + umod(h, 13), v = 5 + umod(h >> 8, 5), off = umod(h >> 12, 100) / 100 * 24, big = umod(h >> 20, 3) == 0, ph = umod(h >> 22, 628) / 100 } end local GLINT = {} for i = 1, 9 do local h = hash32(i * 0x27D4EB2F) GLINT[i] = { x = umod(h, W), y = 25 + umod(h >> 8, 11), w = 1.2 + umod(h >> 14, 8) * 0.3, ph = umod(h >> 18, 628) / 100 } end local function prof(u) if u < 0.28 then return sin(u / 0.28 * 1.5708) end return cos((u - 0.28) / 0.72 * 1.5708) end local function waves(t) local sp = 0.55 + 0.15 * (cfg.surf or 3) for z = 0, 1 do for x = z == 0 and 0 or 46, z == 0 and 18 or 62, 2 do local u = (t * sp / 7 + x * 0.012) % 1 local ys = 37.5 + 7 * prof(u) local row = floor(ys) if row > SHORE then clip(x, SHORE, 2, row - SHORE); blit_rgb(SEAC[x], x, 36, 2, 12) else clip(x, row, 2, SHORE - row); blit_rgb(SANDC[x], x, 36, 2, 12) end clip() blend_rect(x, row - 1, 2, 1, 255, 255, 255, 0.8) blend_rect(x, row, 2, 1, 255, 255, 255, 0.8 * (ys - row)) blend_rect(x, row - 3, 2, 1, 255, 255, 255, 0.28 * (0.5 + 0.5 * sin(t * 1.3 + x))) end end end local CLOUDS = { { 4, 22, 3.0, 8 }, { 7, 15, 2.2, 38 } } local function span(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, 255, 255, 255, a) blend_rect(il, y, 1, 1, 255, 255, 255, a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, 255, 255, 255, a * (xr - ir)) end local function clouds(t) for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[2] local x = (c[4] + t * c[3]) % (W + 2 * L) - L span(x + L * 0.25, x + L * 0.75, c[1], 0.9) span(x, x + L, c[1] + 1, 0.9) span(x + L * 0.05, x + L * 0.95, c[1] + 2, 0.55) end end local function cube(X, Y, k) blend_rect(X, Y + 2, 7, 5, 235, 250, 255, 0.46 * k) blend_rect(X + 1, Y, 7, 2, 245, 252, 255, 0.62 * k) blend_rect(X + 2, Y, 5, 1, 255, 255, 255, 0.7 * k) blend_rect(X, Y + 2, 1, 5, 255, 255, 255, 0.6 * k) blend_rect(X + 6, Y + 3, 1, 4, 150, 60, 20, 0.2 * k) blend_rect(X + 1, Y + 6, 6, 1, 150, 60, 20, 0.2 * k) blend_rect(X + 2, Y + 3, 2, 1, 255, 255, 255, 0.7 * k) end local function straw() local c = D.straw local function seg(x, y0, y1, a) blend_rect(x, y0, 1, y1 - y0 + 1, min(255, c[1] + 70), min(255, c[2] + 70), min(255, c[3] + 70), a) blend_rect(x + 1, y0, 1, y1 - y0 + 1, c[1], c[2], c[3], a) blend_rect(x + 2, y0, 1, y1 - y0 + 1, floor(c[1] * 0.55), floor(c[2] * 0.55), floor(c[3] * 0.55), a) end blend_rect(38, 3, 6, 2, c[1], c[2], c[3], 1) blend_rect(38, 3, 6, 1, min(255, c[1] + 70), min(255, c[2] + 70), min(255, c[3] + 70), 1) seg(38, 5, LEVEL - 1, 1) seg(38, LEVEL, BOT - 1, 0.78) end local function drop(t) local s = t % 12 local x = CX + 5 if s < 3 then local g = min(1, s / 2) blend_rect(x, 33, 1, 1, 255, 255, 255, 0.5 * g) if s > 1.5 then blend_rect(x + 1, 33, 1, 1, 255, 255, 255, 0.3) end elseif s < 10 then local u = min(1, (s - 3) / 6) local y = 33 + 17 * u * u * (3 - 2 * u) local iy = floor(y) local fade = s < 9 and 1 or 10 - s blend_rect(x, 33, 1, max(0, iy - 33), 255, 255, 255, 0.14 * fade) blend_rect(x, iy, 2, 2, 255, 255, 255, 0.45 * fade) blend_rect(x, iy + 2, 2, 1, 90, 30, 10, 0.25 * fade * (y - iy < 0.5 and 1 or 0.6)) blend_rect(x, iy, 1, 1, 255, 255, 255, 0.5 * fade) end end function render() if not BG then if PART then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if not BG then PART = snapshot(0, 0, W, H) else PART = nil end if not BG then gfx.ready(false) end return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) waves(t) for i = 1, #GLINT do local g = GLINT[i] local v = sin(t * g.w + g.ph) if v > 0.4 then blend_rect(g.x, g.y, 2, 1, 255, 255, 255, (v - 0.4) * 1.2) end end clouds(t) for i = 1, cfg.bubbles or 5 do local b = BUB[i] local trav = BOT - 1 - LEVEL local y = BOT - 1 - (t * b.v + b.off) % trav local x = floor(b.x + 0.6 * sin(t * 1.7 + b.ph) + 0.5) local a = min(1, (y - LEVEL) / 3) * 0.7 local iy = floor(y) local f = y - iy if b.big then blend_rect(x, iy, 2, 2, 255, 244, 190, a * (1 - f) * 0.5) blend_rect(x, iy + 1, 2, 2, 255, 244, 190, a * f * 0.5) blend_rect(x, iy, 1, 1, 255, 255, 255, a * 0.8) else blend_rect(x, iy, 1, 1, 255, 246, 200, a * (1 - f)) blend_rect(x, iy + 1, 1, 1, 255, 246, 200, a * f) end end local cy = LEVEL - 3.8 + 0.8 * sin(t * 1.1) local iy = floor(cy) cube(25, iy, 1 - (cy - iy)); cube(25, iy + 1, cy - iy) straw() if cfg.drop ~= false then drop(t) end end