info = { type = "image", title = "Dust devil mesa", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Dust devil and tumbleweed in a pastel desert", api = 33, } local ltr = ({ sk = { ["Whirlwind"] = "Prachový vír", ["Tumbleweed"] = "Kotúľavý krík", ["Clouds"] = "Oblaky", ["Wind"] = "Vietor" }, cs = { ["Whirlwind"] = "Prachový vír", ["Tumbleweed"] = "Kutálející se keř", ["Clouds"] = "Oblaka", ["Wind"] = "Vítr" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "whirl", label = LT("Whirlwind"), type = "int", min = 0, max = 5, default = 3 }, { key = "weed", label = LT("Tumbleweed"), type = "bool", default = true }, { key = "clouds", label = LT("Clouds"), type = "bool", default = true }, { key = "wind", label = LT("Wind"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local KEY = { 255, 0, 255 } 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 frect(x, y, w, h, r, g, b, a) a = a or 1 local x0, xe = floor(x), x + w local x1 = floor(xe) if x1 == x0 then blend_rect(x0, y, 1, h, r, g, b, w * a) return end local f = x - x0 if f > 0.02 then blend_rect(x0, y, 1, h, r, g, b, (1 - f) * a); x0 = x0 + 1 end if x1 > x0 then if a < 1 then blend_rect(x0, y, x1 - x0, h, r, g, b, a) else rect(x0, y, x1 - x0, h, r, g, b) end end if xe - x1 > 0.02 then blend_rect(x1, y, 1, h, r, g, b, (xe - x1) * a) end end local SKY_T, SKY_H = { 150, 226, 222 }, { 212, 250, 234 } local CAP, FACE, STRATA, SHADE = { 255, 226, 196 }, { 252, 170, 146 }, { 244, 152, 134 }, { 222, 124, 124 } local TAL, TAL_S = { 240, 158, 138 }, { 214, 116, 118 } local HORIZON = 44 local SKY, FG local function butte(x0, x1, top, base) local sh = top + floor((base - top) * 0.55) for y = top, base do local widen = y > sh and floor((y - sh) * 0.45) or 0 local h = hash32(y * 0x9E3779B1 + x0) local xl = x0 - widen - (y > top + 1 and umod(h, 2) or 0) local xr = x1 + widen + (y > top + 1 and umod(h >> 4, 2) or 0) local c = y < top + 2 and CAP or (y > sh and TAL or (y % 3 == 0 and STRATA or FACE)) rect(xl, y, xr - xl + 1, 1, c[1], c[2], c[3]) local d = y > sh and TAL_S or SHADE local sw = floor((xr - xl + 1) * 0.3) if y >= top + 2 then rect(xr - sw + 1, y, sw, 1, d[1], d[2], d[3]) end end end local function build() for y = 0, HORIZON - 1 do rect(0, y, W, 1, mix(SKY_T, SKY_H, (y / (HORIZON - 1)) ^ 1.1)) end SKY = snapshot(0, 0, W, H) rect(0, 0, W, H, KEY[1], KEY[2], KEY[3]) for x = 0, W - 1 do local top = 39 + floor(1.2 * sin(x * 0.3) + 1.4 * sin(x * 0.77 + 1) + 0.5) rect(x, top, 1, HORIZON - top, 255, 206, 184) end butte(26, 43, 34, HORIZON); butte(-3, 18, 26, HORIZON) butte(50, 55, 29, HORIZON); butte(58, 66, 37, HORIZON) for y = HORIZON, H - 1 do rect(0, y, W, 1, mix({ 252, 230, 188 }, { 246, 208, 154 }, (y - HORIZON) / 19)) end for k = 1, 22 do local h = hash32(k * 0x85EBCA6B) local x, y = umod(h, W), HORIZON + 2 + umod(h >> 8, 17) if k % 3 == 0 then px(x, y, 110, 200, 150); px(x + 1, y, 110, 200, 150) else px(x, y, 240, 140, 120) end end FG = snapshot(0, 0, W, H) end local CLOUDS = { { 11, 26, 2.2, 6 }, { 19, 16, 1.5, 40 }, { 5, 14, 1.0, 60 } } local function draw_clouds(t) for i = 1, #CLOUDS do local c = CLOUDS[i] local y, w = c[1], c[2] local x = (c[4] + t * c[3]) % (W + w + 10) - w - 5 frect(x + w * 0.3, y, w * 0.35, 1, 255, 255, 255) frect(x, y + 1, w, 2, 255, 255, 255) frect(x + 2, y + 3, w - 5, 1, 255, 238, 214) end end local function ellipse(cx, cy, a, b, al) for dy = -b, b do local q = 1 - (dy / (b + 0.5)) ^ 2 if q > 0 then local hw = a * sqrt(q); frect(cx - hw, cy + dy, 2 * hw, 1, 70, 110, 190, al) end end end local NP = 50 local HH, PH, OM = {}, {}, {} for i = 1, NP do local h = hash32(i * 0xC2B2AE35) HH[i] = (i * 0.618034) % 1 PH[i] = umod(h, 628) / 100 OM[i] = 4.6 - 2.4 * HH[i] + umod(h >> 10, 10) / 20 end local LIT, MID, DARK, TIP = { 255, 218, 184 }, { 230, 170, 142 }, { 196, 122, 114 }, { 255, 238, 214 } local function devil(t, tw, n) local u = (tw % 48) / 48 local cx = -14 + 100 * u + 3 * sin(t * 0.5) local gy = 54 + 2 * sin(TAU * u) local hd = 18 + 3 * (cfg.whirl or 3) for k = 0, 10 do local h = k / 10 local r = 1.8 + 6.2 * h frect(cx - r + 2.2 * h * sin(t * 0.9 + h * 3), floor(gy - hd * h + 0.5), 2 * r, 3, 238, 176, 148, 0.3) end for i = 1, 6 do local a = t * 3.4 + i * 1.05 local r = 4 + (i % 3) * 1.3 local c = cos(a) local k = c < -0.3 and LIT or (c > 0.4 and DARK or MID) px(floor(cx + r * c + 0.5), floor(gy + 0.3 * r * sin(a) + 0.5), k[1], k[2], k[3]) end for i = 1, n do local h = HH[i] local r = 1.8 + 6.2 * h local a = t * OM[i] + PH[i] local c = cos(a) local x = cx + r * c + 2.2 * h * sin(t * 0.9 + h * 3) local y = gy - hd * h + 0.32 * r * sin(a) local k = h > 0.85 and TIP or (c < -0.3 and LIT or (c > 0.4 and DARK or MID)) px(floor(x + 0.5), floor(y + 0.5), k[1], k[2], k[3]) end end local BNC = {} do local ts, A = 0, 10 while A > 1.2 do local D = sqrt(A / 10) BNC[#BNC + 1] = { ts, D, A } ts, A = ts + D, A * 0.68 end end local WA, WR = {}, {} for i = 1, 14 do local h = hash32(i * 0x27D4EB2F) WA[i] = umod(h, 628) / 100 WR[i] = i <= 9 and 2.3 + umod(h >> 10, 12) / 10 or 0.8 + umod(h >> 10, 12) / 10 end local GW = 60 local function weed(t) local tt = t % 24 local x = -8 + 9 * tt local hgt = 0 for k = 1, #BNC do local b = BNC[k] local u = (tt - b[1]) / b[2] if u >= 0 and u < 1 then hgt = 4 * b[3] * u * (1 - u) end end local cy = GW - 3.5 - hgt blend_rect(floor(x - 3 + hgt * 0.15), GW + 1, 7 - floor(hgt * 0.3), 1, 150, 110, 100, 0.4) local spin = x / 3.5 for i = 1, 14 do local a = WA[i] + spin local c = cos(a) px(floor(x + WR[i] * c + 0.5), floor(cy + WR[i] * sin(a) + 0.5), c < 0 and 210 or 156, c < 0 and 160 or 106, c < 0 and 110 or 74) end end function render() if not SKY then build() end local t = frame / FPS local wf = 0.7 + 0.3 * ((cfg.wind or 2) - 1) blit_rgb(SKY, 0, 0, W, H) if cfg.clouds ~= false then draw_clouds(t) end blit_rgb(FG, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) if cfg.clouds ~= false then local c = CLOUDS[1] local sx = (c[4] + t * c[3]) % (W + c[2] + 10) - c[2] - 5 + c[2] * 0.5 + 6 ellipse(sx, 53, 15, 4, 0.08) ellipse(sx, 53, 10, 3, 0.1) end local n = 10 * (cfg.whirl or 3) if n > 0 then devil(t, (t + 14) * wf, n) end if cfg.weed ~= false then weed((t + 1.5) * wf) end end