info = { type = "image", title = "Sunset run", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Runners race along the beach at sunset", api = 33, } local ltr = ({ sk = { ["Runners"] = "Bežci", ["Birds"] = "Vtáky", ["Sun sets"] = "Slnko zapadá" }, cs = { ["Runners"] = "Běžci", ["Birds"] = "Ptáci", ["Sun sets"] = "Slunce zapadá" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "runners", label = LT("Runners"), type = "int", min = 1, max = 3, default = 2 }, { key = "birds", label = LT("Birds"), type = "int", min = 0, max = 6, default = 3 }, { key = "sink", label = LT("Sun sets"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect 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 STEP_HZ = 30 local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local function rgb2hsv(r, g, b) local mx, mn = math.max(r, g, b), math.min(r, g, b) local d = mx - mn local h = 0 if d > 0 then if mx == r then h = ((g - b) / d) % 6 elseif mx == g then h = (b - r) / d + 2 else h = (r - g) / d + 4 end h = h * 60 end return h, mx > 0 and d / mx or 0, mx end local function hsv2rgb(h, s, v) h = (h % 360) / 60 local i = floor(h) local f = h - i local p, q, u = v * (1 - s), v * (1 - s * f), v * (1 - s * (1 - f)) local r, g, b if i == 0 then r, g, b = v, u, p elseif i == 1 then r, g, b = q, v, p elseif i == 2 then r, g, b = p, v, u elseif i == 3 then r, g, b = p, q, v elseif i == 4 then r, g, b = u, p, v else r, g, b = v, p, q end return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function hmix(a, b, t) local h1, s1, v1 = rgb2hsv(a[1], a[2], a[3]) local h2, s2, v2 = rgb2hsv(b[1], b[2], b[3]) local dh = (h2 - h1 + 180) % 360 - 180 return hsv2rgb(h1 + dh * t, s1 + (s2 - s1) * t, v1 + (v2 - v1) * t) end local HOR, SHORE = 34, 45 local STOPS = { { 0, { 214, 128, 180 } }, { 18, { 252, 164, 130 } }, { 33, { 255, 212, 142 } } } local SKY = {} for y = 0, HOR - 1 do local k = (y > STOPS[2][1]) and 2 or 1 local a, b = STOPS[k], STOPS[k + 1] local r, g, bb = hmix(a[2], b[2], (y - a[1]) / (b[1] - a[1])) SKY[y] = { r, g, bb } end local CLOUD, CLOUD_D = { 238, 150, 172 }, { 214, 122, 150 } local SEA_T, SEA_B, SEA_L = { 160, 128, 186 }, { 104, 100, 168 }, { 186, 166, 214 } local WET, SAND, SAND_D, SAND_L = { 204, 140, 114 }, { 238, 178, 124 }, { 214, 154, 106 }, { 250, 198, 146 } local WATER, FOAM, GLINT = { 176, 172, 216 }, { 250, 238, 228 }, { 255, 220, 150 } local SUN = { 255, 242, 196 } local SUNX, SUNR = 16, 7.6 local INK, RIM, BIRD = { 44, 22, 34 }, { 178, 88, 62 }, { 82, 44, 60 } local SUNHW = {} for dy = -8, 8 do local r2 = SUNR * SUNR - dy * dy SUNHW[dy] = r2 >= 0 and floor(sqrt(r2) + 0.5) or -1 end local PW, PH = 14, 19 local function pose(k, sc, pony) local C = {} local function set(x, y) x, y = floor(x + 0.5), floor(y + 0.5) if x >= 0 and x < PW and y >= 0 and y < PH then C[y * PW + x + 1] = 1 end end local function line(x0, y0, x1, y1, th) local n = math.max(2, floor(math.max(math.abs(x1 - x0), math.abs(y1 - y0)) * 3)) for i = 0, n do local u = i / n local x, y = x0 + (x1 - x0) * u, y0 + (y1 - y0) * u set(x, y) if th > 1 then set(x + 1, y) end if th > 2 then set(x - 1, y) end end end local phi = TAU * k / 8 local Lt, Ls = 3.6 * sc, 3.8 * sc local hx = 6 local hy = (PH - 1) - (Lt + Ls) * 0.93 - ((math.abs(sin(phi)) > 0.7) and 1 or 0) for i = 0, 1 do local p = phi + i * 3.14159 local th = 0.8 * sin(p) local sh = th - (0.3 + 1.3 * math.max(0, -cos(p))) local kx, ky = hx + sin(th) * Lt, hy + cos(th) * Lt local fx, fy = kx + sin(sh) * Ls, math.min(PH - 1, ky + cos(sh) * Ls) line(hx, hy, kx, ky, sc > 0.9 and 2 or 1) line(kx, ky, fx, fy, sc > 0.9 and 2 or 1) set(fx + 1, fy) end local lean = 0.22 local T = 4.6 * sc local sx, sy = hx + sin(lean) * T, hy - cos(lean) * T line(hx, hy, sx, sy, sc > 0.9 and 3 or 2) for i = 0, 1 do local p = phi + i * 3.14159 local ua = -0.9 * sin(p) local fa = ua + 1.5 local ex, ey = sx + sin(ua) * 2.6 * sc, sy + cos(ua) * 2.6 * sc line(sx, sy, ex, ey, 1) line(ex, ey, ex + sin(fa) * 2.4 * sc, ey + cos(fa) * 2.4 * sc, 1) end local r = math.max(1.2, 1.5 * sc) local cx, cy = sx + 0.8, sy - 2.2 * sc for dy = -2, 2 do for dx = -2, 2 do if dx * dx + dy * dy <= r * r + 0.5 then set(cx + dx, cy + dy) end end end if pony then for j = 0, 3 do set(cx - r - j, cy - 0.5 + 0.9 * sin(2 * phi + j * 0.9) * j / 3) end end local rows = {} for y = 0, PH - 1 do local row = {} for x = 0, PW - 1 do local c = C[y * PW + x + 1] or 0 if c == 1 and y < PH - 2 and (x == 0 or not C[y * PW + x]) and C[y * PW + x + 2] and C[y * PW + x + 3] then c = 2 end row[x + 1] = c end rows[y + 1] = string.char(table.unpack(row, 1, PW)) end return table.concat(rows) end local RUNNER = { { 1.0, false, 0, 57, 0 }, { 0.93, true, 11, 55, 3 }, { 0.7, false, 21, 56, 5 } } local POSES = { {}, {}, {} } local PAL = { 0, 0, 0, INK[1], INK[2], INK[3], RIM[1], RIM[2], RIM[3] } local BG = nil local function build_bg() for y = 0, HOR - 1 do rect(0, y, W, 1, c3(SKY[y])) end rect(30, 8, 26, 1, c3(CLOUD)); rect(26, 9, 34, 1, c3(CLOUD)); rect(32, 10, 24, 1, c3(CLOUD_D)) rect(42, 15, 18, 1, c3(CLOUD)); rect(46, 16, 12, 1, c3(CLOUD_D)) rect(2, 4, 14, 1, c3(CLOUD)); rect(5, 5, 9, 1, c3(CLOUD_D)) for y = HOR, SHORE - 1 do local t = (y - HOR) / (SHORE - 1 - HOR) rect(0, y, W, 1, floor(SEA_T[1] + (SEA_B[1] - SEA_T[1]) * t + 0.5), floor(SEA_T[2] + (SEA_B[2] - SEA_T[2]) * t + 0.5), floor(SEA_T[3] + (SEA_B[3] - SEA_T[3]) * t + 0.5)) end rect(0, HOR, W, 1, c3(SEA_L)) rect(0, SHORE, W, 5, c3(WET)) rect(0, SHORE + 5, W, H - SHORE - 5, c3(SAND)) for i = 1, 60 do local h = hash32(i * 0x9E3779B1 + 1) local x, y = umod(h, W), SHORE + 6 + umod(h >> 8, H - SHORE - 6) px(x, y, c3(umod(h >> 16, 2) == 0 and SAND_D or SAND_L)) end BG = gfx.snapshot(0, 0, W, H) end local SUN_P = 60 * STEP_HZ local function sun_y(t) if cfg.sink == false then return 26, 1 end local p = (t + 3 * STEP_HZ) % SUN_P local down = 44 * STEP_HZ if p < down then return 18 + 26 * p / down, 1 end if p < 57 * STEP_HZ then return 99, 0 end return 18, (p - 57 * STEP_HZ) / (3 * STEP_HZ) end local function sun(y, a) if y > HOR + SUNR then return end gfx.clip(0, 0, W, HOR) local iy = floor(y) local f = y - iy if a >= 0.999 then for dy = -8, 8 do local hw = SUNHW[dy] if hw >= 0 then rect(SUNX - hw, iy + 1 + dy, 2 * hw + 1, 1, c3(SUN)) end end end local b = a >= 0.999 and 1 - f or a if b > 0.03 then for dy = -8, 8 do local hw = SUNHW[dy] if hw >= 0 then blend_rect(SUNX - hw, iy + dy, 2 * hw + 1, 1, SUN[1], SUN[2], SUN[3], b) end end end gfx.clip() end local function sea(t, sy, sa) local vis = sa * math.max(0, math.min(1, (HOR + SUNR - sy) / (2 * SUNR))) local q = t // 4 for y = HOR + 1, SHORE - 1 do local h = hash32(q * 0x9E3779B1 + y * 0x85EBCA6B) local w = floor((7 - (y - HOR) * 0.35) * vis + umod(h, 3) - 1 + 0.5) if w > 0 then rect(SUNX - w + umod(h >> 4, 3) - 1, y, 2 * w, 1, c3(GLINT)) end end for i = 1, 5 do local h = hash32(i * 0x27D4EB2F + (q // 3) * 0x165667B1) if umod(h, 3) > 0 then rect(28 + umod(h >> 4, 34), HOR + 2 + umod(h >> 12, SHORE - HOR - 3), 2 + umod(h >> 20, 4), 1, c3(SEA_L)) end end end local function shore(t) local w = SHORE - 0.5 + 4 * (0.5 + 0.5 * sin(t * TAU / (5 * STEP_HZ))) local iw = floor(w) local f = w - iw if iw > SHORE then rect(0, SHORE, W, iw - SHORE, c3(WATER)) end local q = floor(t / 6) for x = 0, W - 1, 8 do local h = hash32(x * 0x9E3779B1 + q) local l = 4 + umod(h, 5) rect(x, iw, l, 1, c3(FOAM)) if f > 0.04 then blend_rect(x, iw + 1, l, 1, FOAM[1], FOAM[2], FOAM[3], f) end end end local WUP = { -2, -1, -1, 0, 0, 0, 1, 0, 2, -1 } local WDN = { -2, 1, -1, 0, 0, 0, 1, 0, 2, 1 } local WMID = { -2, 0, -1, 0, 0, 0, 1, 0, 2, 0 } local function birds(t, n) for i = 1, n do local h = hash32(i * 0x85EBCA6B + 9) local v = (0.15 + umod(h, 20) / 100) local x = (umod(h >> 6, 80) - v * t) % (W + 12) - 6 local y = 6 + umod(h >> 12, 16) + 1.2 * sin(t * 0.05 + i) local per = 10 + umod(h >> 18, 6) local ph = (t + i * 5) % per local s = ph < per / 3 and WUP or (ph < 2 * per / 3 and WMID or WDN) local ix = floor(x) local f = x - ix local iy = floor(y + 0.5) for k = 1, #s, 2 do blend_rect(ix + s[k], iy + s[k + 1], 1, 1, BIRD[1], BIRD[2], BIRD[3], 1 - f) if f > 0.04 then blend_rect(ix + s[k] + 1, iy + s[k + 1], 1, 1, BIRD[1], BIRD[2], BIRD[3], f) end end end end local PASS = 120 local function runners(t, n) local lead = t % PASS - 20 for i = 1, n do local R = RUNNER[i] local x = lead - R[3] for m = 0, 12 do local fx = m * 12 - 20 + R[5] local d = x - fx if d > 3 and fx >= -1 and fx < W then local a = 0.55 - d / 140 if a > 0.03 then blend_rect(fx, R[4] + 1 + (m % 2), 2, 1, SAND_D[1] - 40, SAND_D[2] - 50, SAND_D[3] - 40, a) end end end end local built = false local order = { 2, 3, 1 } for k = 1, 3 do local i = order[k] if i <= n then local R = RUNNER[i] local x = lead - R[3] if x > -10 and x < W + 10 then local p = ((t // 3) + R[5]) % 8 local P = POSES[i][p] if not P and not built then P = pose(p, R[1], R[2]); POSES[i][p] = P; built = true end if not P then for d = 1, 7 do P = POSES[i][(p + 8 - d) % 8]; if P then break end end end if P then gfx.blit_indexed(P, PAL, x - 6, R[4] - PH + 1, PW, PH, PW, PH, true) end end end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local sy, sa = sun_y(t) sun(sy, sa) birds(t, cfg.birds or 3) sea(t, sy, sa) shore(t) runners(t, cfg.runners or 2) end