info = { type = "image", title = "Hot air drift", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A hot air balloon rises and dips among clouds", api = 33, } local tr = ({ sk = { ["Balloon colours"] = "Farby balónov", ["Sunset"] = "Západ slnka", ["Ocean"] = "Oceán", ["Candy"] = "Cukrík", ["Clouds"] = "Oblaky" }, cs = { ["Balloon colours"] = "Barvy balónů", ["Sunset"] = "Západ slunce", ["Ocean"] = "Oceán", ["Candy"] = "Bonbon", ["Clouds"] = "Mraky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pal", label = T("Balloon colours"), type = "int", default = "sunset", opts = { keys = { "sunset", "ocean", "candy" }, T("Sunset"), T("Ocean"), T("Candy") } }, { key = "clouds", label = T("Clouds"), type = "int", min = 0, max = 6, default = 4 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local byte, char, concat, unpack = string.byte, string.char, table.concat, table.unpack local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local SKY, HAZE = { 126, 190, 236 }, { 240, 218, 198 } local SUN, SUN_R = { 255, 246, 200 }, { 196, 222, 244 } local FAR, FAR_HI = { 146, 184, 178 }, { 178, 208, 196 } local NEAR, NEAR_HI = { 92, 156, 92 }, { 132, 190, 110 } local FIELD = { 118, 178, 88 } local TREE, TRUNK = { 54, 116, 66 }, { 110, 78, 54 } local CLOUD, CLOUD_S = { 255, 255, 255 }, { 222, 230, 246 } local FCLOUD = { 250, 244, 242 } local ROPE = { 96, 70, 48 } local BASKET, RIM, WEAVE = { 182, 122, 64 }, { 120, 76, 42 }, { 150, 98, 52 } local FLAME_O, FLAME_Y = { 255, 140, 40 }, { 255, 238, 150 } local BURNER = { 104, 108, 120 } local SKY_END = 38 local function c3(c) return c[1], c[2], c[3] end local BW, BH = 21, 22 local CELLS do local t = {} for r = 0, BH - 1 do local y = r + 0.5 local hw if y <= 13.5 then local d = y - 11 hw = sqrt(math.max(0, 112.36 - d * d)) else hw = 10.3 - (y - 13.5) * 0.85 end for c = 0, BW - 1 do local x = c + 0.5 - BW / 2 local v = 0 if x * x <= hw * hw then local u = x / hw local gi = floor((u + 1) * 3.5) if gi > 6 then gi = 6 end local dark, light = u > 0.55, u < -0.55 and r < 12 if r >= 20 then v = dark and 10 or 9 elseif r == 14 or r == 15 then v = dark and 8 or 7 elseif r >= 3 and r <= 6 and u > -0.66 and u < -0.36 then v = 11 else v = (gi % 2 == 0) and 1 or 2 if dark then v = v + 2 elseif light then v = v + 4 end end end t[#t + 1] = v end end CELLS = char(unpack(t)) end local SETS = { sunset = { { 232, 64, 56 }, { 252, 198, 64 }, { 250, 244, 230 } }, ocean = { { 36, 112, 214 }, { 244, 246, 252 }, { 250, 196, 60 } }, candy = { { 238, 108, 168 }, { 140, 214, 236 }, { 255, 240, 150 } }, } local PAL = {} local function mix(c, k, w) if w then return floor(c[1] + (255 - c[1]) * k + 0.5), floor(c[2] + (255 - c[2]) * k + 0.5), floor(c[3] + (255 - c[3]) * k + 0.5) end return floor(c[1] * k + 0.5), floor(c[2] * k + 0.5), floor(c[3] * k + 0.5) end local KR, KG, KB = 255, 0, 255 local NP = 4 local RAS, built = {}, false local function shift_y(f) local out, row = {}, {} local g = 1 - f local br, bg, bb = c3(SKY) for y = 0, BH do local k = 0 for x = 1, BW do local a = (y >= 1 and f > 0) and byte(CELLS, (y - 1) * BW + x) or 0 local b = (y < BH and g > 0) and byte(CELLS, y * BW + x) or 0 local r1, g1, b1 = KR, KG, KB if a ~= 0 or b ~= 0 then local ar, ag, ab, cr, cg, cb = br, bg, bb, br, bg, bb if a ~= 0 then ar, ag, ab = PAL[3 * a - 2], PAL[3 * a - 1], PAL[3 * a] end if b ~= 0 then cr, cg, cb = PAL[3 * b - 2], PAL[3 * b - 1], PAL[3 * b] end r1 = floor(ar * f + cr * g + 0.5) g1 = floor(ag * f + cg * g + 0.5) b1 = floor(ab * f + cb * g + 0.5) end row[k + 1], row[k + 2], row[k + 3] = r1, g1, b1 k = k + 3 end out[y + 1] = char(unpack(row, 1, k)) end return concat(out) end local CL = { { 12, 18, -12.6, 40, true }, { 36, 9, -3.3, 10, false }, { 26, 14, -10.8, 0, true }, { 39, 12, -4.2, 44, false }, { 5, 12, -14.4, 22, true }, { 33, 7, -2.7, 30, false }, } local function span(xl, xr, y, h, r, g, b, a) local il, ir = floor(xl), floor(xr) if il == ir then local c = (xr - xl) * a if c > 0.02 then blend_rect(il, y, 1, h, r, g, b, c) end return end if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, h, r, g, b, a) end local cl, cr = (1 - (xl - il)) * a, (xr - ir) * a if cl > 0.02 then blend_rect(il, y, 1, h, r, g, b, cl) end if cr > 0.02 then blend_rect(ir, y, 1, h, r, g, b, cr) end end local function box(x0, y0, w, h, r, g, b) local x1, y1 = x0 + w, y0 + h local ix0, iy0, ix1, iy1 = floor(x0), floor(y0), floor(x1), floor(y1) local fx0, fy0, fx1, fy1 = 1 - (x0 - ix0), 1 - (y0 - iy0), x1 - ix1, y1 - iy1 local iw, ih = ix1 - ix0 - 1, iy1 - iy0 - 1 if iw > 0 and ih > 0 then rect(ix0 + 1, iy0 + 1, iw, ih, r, g, b) end if iw > 0 then blend_rect(ix0 + 1, iy0, iw, 1, r, g, b, fy0) if fy1 > 0.02 then blend_rect(ix0 + 1, iy1, iw, 1, r, g, b, fy1) end end if ih > 0 then blend_rect(ix0, iy0 + 1, 1, ih, r, g, b, fx0) if fx1 > 0.02 then blend_rect(ix1, iy0 + 1, 1, ih, r, g, b, fx1) end end blend(ix0, iy0, r, g, b, fx0 * fy0) if fx1 > 0.02 then blend(ix1, iy0, r, g, b, fx1 * fy0) end if fy1 > 0.02 then blend(ix0, iy1, r, g, b, fx0 * fy1) end if fx1 * fy1 > 0.02 then blend(ix1, iy1, r, g, b, fx1 * fy1) end end local function draw_cloud(c, t) local L, v = c[2], c[3] local x = (c[4] + t * v / FPS) % (W + L + 4) - L - 2 local y = c[1] if c[5] then local cr, cg, cb = c3(CLOUD) span(x + L * 0.3, x + L * 0.62, y, 1, cr, cg, cb, 0.92) span(x + L * 0.12, x + L * 0.86, y + 1, 1, cr, cg, cb, 0.92) span(x, x + L, y + 2, 2, cr, cg, cb, 0.92) span(x + L * 0.08, x + L * 0.92, y + 4, 1, CLOUD_S[1], CLOUD_S[2], CLOUD_S[3], 0.92) else local cr, cg, cb = c3(FCLOUD) span(x + L * 0.25, x + L * 0.7, y, 1, cr, cg, cb, 0.7) span(x, x + L, y + 1, 1, cr, cg, cb, 0.7) end end local BG = nil local function build_bg() rect(0, 0, W, SKY_END, c3(SKY)) for y = SKY_END, 49 do local t = (y - SKY_END + 1) / (49 - SKY_END + 2) rect(0, y, W, 1, floor(SKY[1] + (HAZE[1] - SKY[1]) * t + 0.5), floor(SKY[2] + (HAZE[2] - SKY[2]) * t + 0.5), floor(SKY[3] + (HAZE[3] - SKY[3]) * t + 0.5)) end rect(0, 50, W, H - 50, c3(HAZE)) local SXc, SYc = 53, 9 for y = SYc - 7, SYc + 7 do for x = SXc - 7, SXc + 7 do local dx, dy = x - SXc, y - SYc local d = dx * dx + dy * dy if d <= 20 then px(x, y, c3(SUN)) elseif d <= 42 then px(x, y, c3(SUN_R)) end end end for x = 0, W - 1 do local f = floor(45 + 2.5 * sin(x * 0.09 + 1) + 1.5 * sin(x * 0.23) + 0.5) rect(x, f, 1, H - f, c3(FAR)); px(x, f, c3(FAR_HI)) end for x = 0, W - 1 do local n = floor(53 + 3 * sin(x * 0.07 + 2.2) + 1.2 * sin(x * 0.19) + 0.5) rect(x, n, 1, H - n, c3(NEAR)); px(x, n, c3(NEAR_HI)) end rect(0, 60, W, 4, c3(FIELD)) for k = 0, 5 do local x = 4 + k * 11 + (k % 2) * 3 local n = floor(53 + 3 * sin(x * 0.07 + 2.2) + 1.2 * sin(x * 0.19) + 0.5) rect(x, n - 1, 1, 2, c3(TRUNK)) rect(x - 1, n - 4, 3, 3, c3(TREE)); px(x, n - 5, c3(TREE)) end BG = gfx.snapshot(0, 0, W, H) end local NCL = 4 function on_settings() local s = SETS[cfg.pal] or SETS.sunset local A, B, S = s[1], s[2], s[3] local p = {} local function add(r, g, b) p[#p + 1] = r; p[#p + 1] = g; p[#p + 1] = b end add(c3(A)); add(c3(B)) add(mix(A, 0.76)); add(mix(B, 0.78)) add(mix(A, 0.3, true)); add(mix(B, 0.3, true)) add(c3(S)); add(mix(S, 0.8)) add(150, 98, 58); add(112, 72, 42) add(255, 252, 246) PAL = p RAS, built = {}, false NCL = cfg.clouds or 4 end local BX = 19 local function draw_balloon(t) local ph = t * TAU / (9 * FPS) local sy = sin(ph) local Y = 9 + 5 * sy local q = floor(Y * NP + 0.5) local iy, p = q // NP, q % NP local Yq = q / NP if not RAS[0] then RAS[0] = shift_y(0); built = true end if not RAS[p] and not built then RAS[p] = shift_y(p / NP); built = true end local R = RAS[p] if not R then R = RAS[0]; iy = floor(Y + 0.5); Yq = iy end gfx.blit_rgb(R, BX, iy, BW, BH + 1, KR, KG, KB) local s = (sy - 0.15) / 0.5 if s < 0 then s = 0 elseif s > 1 then s = 1 end local cx = BX + BW / 2 local amp = 0.16 + 0.1 * sin(t * TAU / (11 * FPS)) local th = amp * sin(t * TAU / (1.9 * FPS)) local bx = cx + 6.5 * th local ty = Yq + BH local by = ty + 5 for k = 0, 4 do local y = floor(ty) + k local u = (k + 0.5) / 5 for side = -1, 1, 2 do local x = cx + side * 3 + (bx + side * 2.5 - cx - side * 3) * u - 0.5 local ix = floor(x) local f = x - ix blend(ix, y, ROPE[1], ROPE[2], ROPE[3], 1 - f) blend(ix + 1, y, ROPE[1], ROPE[2], ROPE[3], f) end end local bxi = floor(bx + 0.5) rect(bxi - 1, floor(by) - 1, 2, 1, c3(BURNER)) if s > 0 then local h = hash32(floor(t * 15 / FPS) * 0x9E3779B1) local fh = floor(1 + 2.5 * s + umod(h, 2) + 0.5) local fy = floor(by) - 1 - fh rect(bxi - 1, fy, 2, fh, c3(FLAME_O)) rect(bxi - 1 + umod(h >> 3, 2), fy + 1, 1, fh - 1, c3(FLAME_Y)) blend_rect(floor(cx) - 3, iy + BH - 2, 7, 2, 255, 176, 90, 0.55 * s) end box(bx - 3, by, 6, 4, c3(BASKET)) box(bx - 3, by + 2, 6, 1, c3(WEAVE)) box(bx - 3.5, by, 7, 1, c3(RIM)) end function render() built = false if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame for i = 1, NCL do if not CL[i][5] then draw_cloud(CL[i], t) end end draw_balloon(t) for i = 1, NCL do if CL[i][5] then draw_cloud(CL[i], t) end end end