info = { type = "image", title = "Three kings", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "Silhouette caravan crosses a starlit desert", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Sand"] = "Piesok", ["Star rays"] = "Lúče hviezdy" }, cs = { ["Speed"] = "Rychlost", ["Sand"] = "Písek", ["Star rays"] = "Paprsky hvězdy" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "sand", label = T("Sand"), type = "int", min = 0, max = 6, default = 3 }, { key = "rays", label = T("Star rays"), type = "bool", default = true }, } local rect, px, blend_rect, blend_mask = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend_mask local floor, sin = math.floor, math.sin local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SKY_T, SKY_B = { 6, 10, 40 }, { 40, 56, 128 } local GLOW1, GLOW2 = { 26, 36, 96 }, { 44, 56, 122 } local FAR, FAR_RIM = { 26, 34, 92 }, { 150, 118, 52 } local PLAIN = { 20, 26, 70 } local NEAR, NEAR_RIM = { 8, 10, 32 }, { 98, 76, 36 } local SIL, GOLD = { 3, 4, 16 }, { 232, 178, 58 } local SX, SY = 46, 13 local HORIZON, FEET = 44, 50 local function c3(c) return c[1], c[2], c[3] end local GW, GH = 19, 18 local function fill(g, spec, dy) for y, list in spec:gmatch("(%d+):([%d,%-]+)") do for a0, b0 in list:gmatch("(%d+)%-?(%d*)") do local a = tonumber(a0) local b = b0 == "" and a or tonumber(b0) for x = a, b do g[(tonumber(y) + dy) * GW + x + 1] = 1 end end end end local RIDER_D = "2:5-7;3:5-7;4:4-8;5:4-8;6:4-8;7:3-9" local CAMEL_D = "3:15-16;4:14-17;5:13-16;6:9-10,12-14;7:3-14;8:1-14;9:0-13;10:1-13" local GOLD_C = { "0:4,6,8;1:4-8;8:4-8;9:4,8", "0:6;1:5-7;8:4-8;9:4,8;5:1-2;6:1-3", "0:5-7;1:4-8;8:4-8;9:4,8;6:1-3;7:0-2", } local LEG_X = { 2, 4, 10, 12 } local WALK = { { { 2, 1 }, { -1, 0 }, { -1, 0 }, { 2, 1 }, 1 }, { { 1, 0 }, { 0, 1 }, { 0, 1 }, { 1, 0 }, 0 }, { { -1, 0 }, { 2, 1 }, { 2, 1 }, { -1, 0 }, 1 }, { { 0, 1 }, { 1, 0 }, { 1, 0 }, { 0, 1 }, 0 }, } local CH = {} for k = 0, 4 do CH[k] = string.char(floor(255 * k / 4 + 0.5)) end local function shifted(g, q, y0, y1) local out, n = {}, 0 for y = y0, y1 do for x = 0, GW - 1 do n = n + 1 out[n] = CH[(4 - q) * (g[y * GW + x + 1] or 0) + q * (x > 0 and g[y * GW + x] or 0)] end end return table.concat(out) end local DMASK, CMASK = {}, {} local function dark(p, q) local key = p * 4 + q if DMASK[key] then return DMASK[key] end local pose = WALK[p + 1] local off = pose[5] local d = {} fill(d, RIDER_D, off); fill(d, CAMEL_D, off) for k = 1, 4 do local leg = pose[k] local top = 11 + off local n = GH - top for i = 0, n - leg[2] - 1 do d[(top + i) * GW + LEG_X[k] + floor(leg[1] * i / (n - 1) + 0.5) + 1] = 1 end end DMASK[key] = shifted(d, q, 0, GH - 1) return DMASK[key] end local C_ROWS = 11 local function gold(v, p, q) local key = (v * 4 + p) * 4 + q if CMASK[key] then return CMASK[key] end local c = {} fill(c, GOLD_C[v], WALK[p + 1][5]) CMASK[key] = shifted(c, q, 0, C_ROWS - 1) return CMASK[key] end local BG, STAGE = nil, 0 local STARS = {} do local k, i = 0, 0 while k < 18 and i < 300 do i = i + 1 local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, HORIZON - 10) local dx, dy = x - SX, y - SY if dx * dx + dy * dy > 100 then k = k + 1 STARS[k] = { x, y, umod(h >> 16, 628) / 100.0 } end end end local function sky_stage() for y = 0, HORIZON - 1 do local t = y / (HORIZON - 1) rect(0, y, W, 1, floor(SKY_T[1] + (SKY_B[1] - SKY_T[1]) * t + 0.5), floor(SKY_T[2] + (SKY_B[2] - SKY_T[2]) * t + 0.5), floor(SKY_T[3] + (SKY_B[3] - SKY_T[3]) * t + 0.5)) end rect(0, HORIZON, W, H - HORIZON, c3(SKY_B)) for y = SY - 9, SY + 9 do local dy = y - SY local w1 = floor(math.sqrt(81 - dy * dy) + 0.5) rect(SX - w1, y, 2 * w1 + 1, 1, c3(GLOW1)) if dy * dy <= 25 then local w2 = floor(math.sqrt(25 - dy * dy) + 0.5) rect(SX - w2, y, 2 * w2 + 1, 1, c3(GLOW2)) end end BG = gfx.snapshot(0, 0, W, H) end local function dune_stage() gfx.blit_rgb(BG, 0, 0, W, H) local FY = {} for x = -1, W do FY[x] = floor(44 + 3 * sin(x * 0.07 + 1) + 1.5 * sin(x * 0.19 + 2) + 0.5) end for x = 0, W - 1 do rect(x, FY[x], 1, FEET + 1 - FY[x], c3(FAR)) if FY[x + 1] > FY[x] then px(x, FY[x], c3(FAR_RIM)) end end rect(0, FEET + 1, W, 5, c3(PLAIN)) for x = 0, W - 1 do local y = floor(57 + 2 * sin(x * 0.09 + 0.3) + sin(x * 0.23) + 0.5) local y1 = floor(57 + 2 * sin((x + 1) * 0.09 + 0.3) + sin((x + 1) * 0.23) + 0.5) rect(x, y, 1, H - y, c3(NEAR)) if y1 > y then px(x, y, c3(NEAR_RIM)) end end BG = gfx.snapshot(0, 0, W, H) end local SAND = {} for i = 1, 18 do local h = hash32(i * 0x85EBCA6B) SAND[i] = { x = umod(h, W + 8), y = 46 + umod(h >> 6, 14), v = 5 + umod(h >> 10, 10), a = 1.2 + umod(h >> 14, 14) / 10, w = 0.5 + umod(h >> 18, 8) / 10, ph = umod(h >> 22, 628) / 100, } end local C1, C2, C3 = { 255, 226, 132 }, { 236, 206, 150 }, { 164, 178, 236 } local function draw_star(t, rays) rect(SX - 1, SY - 1, 3, 3, 255, 226, 140) px(SX, SY, 255, 252, 226) if not rays then return end local tw = 0.8 + 0.2 * sin(t * 2.1) local LV, LH = floor(12 * tw + 0.5), floor(8 * tw + 0.5) for s = -1, 1, 2 do for k = 2, LV do local c, a = k <= 4 and C1 or (k <= 7 and C2 or C3), k <= 4 and 0.9 or (k <= 7 and 0.55 or 0.3) blend_rect(SX, SY + s * k, 1, 1, c[1], c[2], c[3], a) if k <= LH then blend_rect(SX + s * k, SY, 1, 1, c[1], c[2], c[3], a) end end for k = 2, 4 do local c = k <= 2 and C1 or C2 blend_rect(SX + s * k, SY - k, 1, 1, c[1], c[2], c[3], 0.5) blend_rect(SX + s * k, SY + k, 1, 1, c[1], c[2], c[3], 0.5) end end end local SPACING, CAM_W = 21, 18 local PHASE = { 0, 0.3, 0.65 } local LAST = {} function render() if STAGE == 0 then sky_stage(); STAGE = 1; return end if STAGE == 1 then dune_stage(); STAGE = 2; return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS for i = 1, #STARS do local s = STARS[i] local bright = sin(t * 1.7 + s[3] * 3) > -0.2 px(s[1], s[2], bright and 214 or 96, bright and 220 or 108, bright and 255 or 170) end draw_star(t, cfg.rays ~= false) local v = 2 * (cfg.speed or 3) local cyc = 160 local lead = (v * t + 50) % cyc - CAM_W local budget = 1 for i = 0, 2 do local x = lead - SPACING * i if x > -GW and x < W then local ix = floor(x) local q = floor((x - ix) * 4 + 0.5) if q == 4 then ix, q = ix + 1, 0 end local p = floor(4 * (v * t / 8 + PHASE[i + 1])) % 4 local kd, kc = p * 4 + q, ((i + 1) * 4 + p) * 4 + q local d, c = DMASK[kd], CMASK[kc] if not d and budget > 0 then d = dark(p, q); budget = budget - 1 end if not c and budget > 0 then c = gold(i + 1, p, q); budget = budget - 1 end if d and c then LAST[i] = { d, c } else d, c = nil, nil end local m = LAST[i] if m then blend_rect(ix + 1, FEET + 1, CAM_W - 2, 1, 0, 0, 10, 0.55) blend_mask(m[1], SIL[1], SIL[2], SIL[3], ix, FEET - GH + 1, GW, GH, GW, GH, 1) blend_mask(m[2], GOLD[1], GOLD[2], GOLD[3], ix, FEET - GH + 1, GW, C_ROWS, GW, C_ROWS, 1) end end end local n = (cfg.sand or 3) * 3 for i = 1, n do local s = SAND[i] local x = (s.x + s.v * t * 1.5) % (W + 8) - 4 local y = floor(s.y + s.a * sin(t * s.w + s.ph) + 0.5) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, 1, 214, 178, 96, 0.3 * (1 - f)) blend_rect(ix + 1, y, 1, 1, 214, 178, 96, 0.3) blend_rect(ix + 2, y, 1, 1, 214, 178, 96, 0.3 * f) end end