info = { type = "image", title = "Dune caravan", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Camels cross pastel dunes in blowing sand", api = 44, } local ltr = ({ sk = { ["Tone"] = "Odtieň", ["Peach"] = "Broskyňa", ["Rose"] = "Ruža", ["Camels"] = "Ťavy", ["Blowing sand"] = "Vietor s pieskom" }, cs = { ["Tone"] = "Odstín", ["Peach"] = "Broskev", ["Rose"] = "Růže", ["Camels"] = "Velbloudi", ["Blowing sand"] = "Vítr s pískem" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "tone", label = LT("Tone"), type = "int", default = "peach", opts = { keys = { "peach", "rose" }, LT("Peach"), LT("Rose") } }, { key = "camels", label = LT("Camels"), type = "int", min = 1, max = 4, default = 3 }, { key = "sand", label = LT("Blowing sand"), type = "int", min = 0, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, pi = math.floor, math.sin, math.sqrt, math.pi local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TONES = { peach = { skyT = { 244, 140, 128 }, skyH = { 255, 222, 170 }, sun = { 255, 246, 212 }, far = { 238, 166, 142 }, mL = { 255, 196, 148 }, mS = { 226, 132, 114 }, nL = { 255, 210, 160 }, nS = { 214, 116, 100 }, cam = { 110, 56, 88 }, sand = { 255, 232, 188 } }, rose = { skyT = { 226, 130, 170 }, skyH = { 255, 214, 196 }, sun = { 255, 240, 226 }, far = { 226, 150, 170 }, mL = { 255, 190, 176 }, mS = { 206, 112, 128 }, nL = { 255, 206, 186 }, nS = { 190, 100, 118 }, cam = { 84, 44, 78 }, sand = { 255, 228, 214 } }, } local P = TONES.peach local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function frect(xl, xr, y, h, r, g, b, a) a = a or 1 local il, ir = floor(xl), floor(xr) if ir == il then blend_rect(il, y, 1, h, r, g, b, (xr - xl) * a) return end if ir > il + 1 then if a >= 1 then rect(il + 1, y, ir - il - 1, h, r, g, b) else blend_rect(il + 1, y, ir - il - 1, h, r, g, b, a) end end local fl, fr = (1 - (xl - il)) * a, (xr - ir) * a if fl > 0.02 then blend_rect(il, y, 1, h, r, g, b, fl) end if fr > 0.02 then blend_rect(ir, y, 1, h, r, g, b, fr) end end local YF, YM, YN = {}, {}, {} for x = -1, W do YF[x] = floor(36 + 2.5 * sin(x * 0.09 + 1) + 1.5 * sin(x * 0.23) + 0.5) YM[x] = floor(44 + 2 * sin(x * 0.07 + 0.5) + sin(x * 0.19) + 0.5) YN[x] = floor(55 + 3 * sin(x * 0.06 + 2) + 1.2 * sin(x * 0.2) + 0.5) end local HZ0, HZ1 = 29, 40 local HR, BG = {}, nil local function st_sky() local T, B, S = P.skyT, P.skyH, P.sun for y = 0, 43 do local t = y / 43 rect(0, y, W, 1, rgb(T[1] + (B[1] - T[1]) * t, T[2] + (B[2] - T[2]) * t, T[3] + (B[3] - T[3]) * t)) end for y = 8, 24 do local dy = y - 16 local R2 = 64 - dy * dy if R2 > 0 then local hw = floor(sqrt(R2) + 0.5) local t = y / 43 local sr, sg, sb = T[1] + (B[1] - T[1]) * t, T[2] + (B[2] - T[2]) * t, T[3] + (B[3] - T[3]) * t rect(46 - hw, y, 2 * hw, 1, rgb(sr + (S[1] - sr) * 0.3, sg + (S[2] - sg) * 0.3, sb + (S[3] - sb) * 0.3)) end local R1 = 25 - dy * dy if R1 > 0 then local hw = floor(sqrt(R1) + 0.5) rect(46 - hw, y, 2 * hw, 1, S[1], S[2], S[3]) end end for x = 0, W - 1 do rect(x, YF[x], 1, 50 - YF[x], P.far[1], P.far[2], P.far[3]) end end local function dune(Y, L, S, y1) for x = 0, W - 1 do local y = Y[x] rect(x, y, 1, y1 - y, S[1], S[2], S[3]) if Y[x + 1] - Y[x - 1] > 0 then rect(x, y, 1, 3, L[1], L[2], L[3]) end end end local function st_dunes() dune(YM, P.mL, P.mS, 60) dune(YN, P.nL, P.nS, H) local L = P.nL for y = 58, 62, 2 do for i = 0, 6 do local h = hash32(y * 131 + i * 977) local x = umod(h, 58) if y > YN[x] + 1 then blend_rect(x, y, 2 + umod(h >> 8, 4), 1, L[1], L[2], L[3], 0.35) end end end end local function st_snap() for y = HZ0, HZ1 do HR[y] = snapshot(0, y, W, 1) end BG = snapshot(0, 0, W, H) end local STAGES = { st_sky, st_dunes, st_snap } local STAGE, PART = 1, nil local function build() if PART then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if STAGE <= #STAGES then PART = snapshot(0, 0, W, H) end end function on_settings() P = TONES[cfg.tone] or TONES.peach BG, STAGE, PART = nil, 1, nil end local BODY = { { 0, 4, 6 }, { 1, 3, 7 }, { 1, 10, 13 }, { 2, 1, 11 }, { 3, 0, 10 }, { 4, 1, 8 } } local LEGX = { 1, 3, 5, 7 } local STEP_DX = { 1, 0, -1, 0 } local STEP_UP = { 0, 0, 0, 1 } local SPD, GAP, CW = 4.0, 17, 13 local function camel(x, yg, dist, rider) local c = P.cam local r, g, b = c[1], c[2], c[3] local top = yg - 8 for i = 1, #BODY do local s = BODY[i] frect(x + s[2], x + s[3], top + s[1], 1, r, g, b) end local p = floor(dist) % 4 for i = 1, 4 do local k = (p + (i % 2) * 2) % 4 local lx = x + LEGX[i] + STEP_DX[k + 1] frect(lx, lx + 1, top + 5, 4 - STEP_UP[k + 1], r, g, b) end if rider then frect(x + 4, x + 6, top - 2, 2, r, g, b) frect(x + 4.5, x + 5.5, top - 3, 1, r, g, b) end end local NS = 25 local SA = {} for i = 1, NS do local h = hash32(i * 0x9E3779B1) SA[i] = { x0 = 3 + umod(h, 54), len = 8 + umod(h >> 8, 14), per = 3 + umod(h >> 12, 40) / 10, ph = umod(h >> 16, 100) / 100, rise = 2 + umod(h >> 20, 4), mid = i % 3 == 0 } end local CL = { { 6, 20, 1.4, 5 }, { 14, 14, 0.9, 40 }, { 23, 18, 1.8, 22 } } function render() if not BG then build() gfx.ready(false) return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) for y = HZ0, HZ1 do local dx = floor((0.6 + (y - HZ0) * 0.08) * sin(y * 0.9 - t * 1.6) + 0.5) if dx ~= 0 then blit_rgb(HR[y], dx, y, W, 1) end end local cl = P.sun for i = 1, #CL do local c = CL[i] local L = c[2] local x = (c[4] + t * c[3]) % (W + L) - L frect(x + 2, x + L - 2, c[1], 1, cl[1], cl[2], cl[3], 0.5) frect(x, x + L * 0.7, c[1] + 1, 1, cl[1], cl[2], cl[3], 0.35) end local n = (cfg.sand or 3) * 5 local sd = P.sand for i = 1, n do local s = SA[i] local u = (t / s.per + s.ph) % 1 local x = s.x0 + u * s.len local ix = floor(x) local Y = s.mid and YM or YN local y = Y[ix < 0 and 0 or ix > W - 1 and W - 1 or ix] - 1 - floor(u * s.rise) local a = sin(pi * u) * 0.85 frect(x, x + 1.6, y, 1, sd[1], sd[2], sd[3], a) end local nc = cfg.camels or 3 local cyc = W + CW + nc * GAP + 22 local xh = (t * SPD + 40) % cyc - CW for i = nc - 1, 0, -1 do local x = xh - i * GAP if x > -CW and x < W then local xi = floor(x + 6) xi = xi < 0 and 0 or xi > W - 1 and W - 1 or xi camel(x, YM[xi] + 2, t * SPD + i * 1.3, i == 1) end end end