info = { type = "image", title = "Arch sunset", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sunset sea seen through a mossy rock arch", api = 44, } local tr = ({ sk = { ["Gulls"] = "Čajky", ["Waves"] = "Vlny" }, cs = { ["Gulls"] = "Racci", ["Waves"] = "Vlny" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "gulls", label = T("Gulls"), type = "int", min = 0, max = 5, default = 3 }, { key = "waves", label = T("Waves"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect, blend_mask = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend_mask local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, SHORE = 31, 46 local SX, SY = 32, 28 local KEY = { 255, 0, 255 } local SKY_T, SKY_M, SKY_H = { 104, 58, 118 }, { 226, 104, 100 }, { 255, 176, 100 } local SEA_0, SEA_1, SEA_2 = { 246, 150, 104 }, { 118, 84, 140 }, { 88, 88, 134 } local SAND_0, SAND_1 = { 196, 128, 104 }, { 124, 86, 92 } local FOAM, DEEP, WET = { 255, 232, 220 }, { 84, 64, 122 }, { 96, 62, 74 } local ROCK, ROCK_L = { 44, 32, 54 }, { 68, 50, 76 } local RIM1, RIM2 = { 176, 98, 76 }, { 100, 60, 70 } local LEAF_D, LEAF_M, LEAF_L = { 28, 66, 44 }, { 48, 100, 58 }, { 84, 138, 74 } local GULL = { 62, 36, 68 } local function c3(c) return c[1], c[2], c[3] end 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 hw(y) if y < 12 then return 0 end local w if y < 40 then local e = (40 - y) / 28 w = 19 * sqrt(1 - e * e) else w = 19 + (y - 40) * 0.12 end return w + 0.7 * sin(y * 1.9) + 0.5 * sin(y * 0.7 + 1) end local BACK, FRONT, PART, STAGE = nil, nil, nil, 1 local function st_back() for y = 0, HZ - 1 do local t = y / (HZ - 1) if t < 0.6 then rect(0, y, W, 1, mix(SKY_T, SKY_M, t / 0.6)) else rect(0, y, W, 1, mix(SKY_M, SKY_H, (t - 0.6) / 0.4)) end end for y = HZ, SHORE - 1 do local t = (y - HZ) / (SHORE - HZ) if t < 0.5 then rect(0, y, W, 1, mix(SEA_0, SEA_1, t * 2)) else rect(0, y, W, 1, mix(SEA_1, SEA_2, (t - 0.5) * 2)) end end for y = SHORE, H - 1 do rect(0, y, W, 1, mix(SAND_0, SAND_1, (y - SHORE) / 12 > 1 and 1 or (y - SHORE) / 12)) end rect(17, HZ - 1, 9, 1, 96, 62, 112); rect(19, HZ - 2, 5, 1, 96, 62, 112) end local function st_sun() local R = 17 local n = 2 * R + 1 local rows, row = {}, {} for gy = 0, n - 1 do for gx = 0, n - 1 do local d = sqrt((gx - R) ^ 2 + (gy - R) ^ 2) row[gx + 1] = d < R and floor(255 * (1 - d / R)) or 0 end rows[gy + 1] = string.char(table.unpack(row, 1, n)) end blend_mask(table.concat(rows), 255, 168, 88, SX - R, SY + 1 - R, n, n, n, n, 0.62) for dy = -4, 3 do local y = SY + dy if y < HZ then local w = floor(sqrt(16.5 - dy * dy) + 0.5) rect(SX - w, y, 2 * w, 1, 255, 240 - (dy + 4) * 3, 178 - (dy + 4) * 4) end end for y = HZ, SHORE - 1 do local w = 2 + floor((y - HZ) * 0.8) blend_rect(SX - w // 2, y, w, 1, 255, 214, 150, 0.4) end BACK = snapshot(0, 0, W, H) end local function st_rock() rect(0, 0, W, H, c3(KEY)) for y = 0, 55 do local w = hw(y) local l, r = floor(SX - w), floor(SX + w + 0.5) if w == 0 then l, r = SX, SX end rect(0, y, l, 1, c3(ROCK)) rect(r, y, W - r, 1, c3(ROCK)) if y >= 24 and w > 0 then rect(l - 1, y, 1, 1, c3(y > 31 and RIM1 or RIM2)) rect(l - 2, y, 1, 1, c3(RIM2)) rect(r, y, 1, 1, c3(y > 31 and RIM1 or RIM2)) rect(r + 1, y, 1, 1, c3(RIM2)) end end for i = 1, 46 do local h = hash32(i * 0x9E3779B1) local y, x, len = 8 + umod(h, 46), umod(h >> 8, W), 3 + umod(h >> 16, 5) local w = hw(y) if x < SX - w - 4 or x > SX + w + 3 then rect(x, y, len, 1, c3(ROCK_L)) end end for x = 0, W - 1 do local h = hash32(x * 0x85EBCA6B) local yb = 55 + floor(1.6 * sin(x * 0.45) + 0.5) + umod(h, 2) rect(x, yb, 1, H - yb, c3(ROCK)) rect(x, yb, 1, 1, c3(ROCK_L)) end end local function st_green() for x = 0, W - 1 do local yb = 7 + floor(3 * sin(x * 0.6) + 2 * sin(x * 1.9 + 2) + 0.5) rect(x, 0, 1, yb, c3(LEAF_D)) local h = hash32(x * 0xC2B2AE35) if h & 1 == 1 then px(x, umod(h >> 4, yb), c3(LEAF_M)) end if (h >> 3) & 3 == 0 then px(x, umod(h >> 8, yb), c3(LEAF_L)) end px(x, yb, c3(LEAF_M)) end for x = 20, 46, 3 do local d = x - SX local top = floor(40 - 28 * sqrt(1 - (d / 19) ^ 2) + 0.5) if (d / 19) ^ 2 < 1 then local h = hash32(x * 0x27D4EB2F) local len = 2 + umod(h, 6) rect(x, top - 1, 1, len + 1, c3(LEAF_D)) px(x, top + len, c3(LEAF_M)) px(x + 1, top + len - 1, c3(LEAF_L)) end end FRONT = snapshot(0, 0, W, H) end local STAGES = { st_back, st_sun, st_rock, st_green } local CONTINUES = { false, true, false, true } local function band(xl, xr, y, h, r, g, b, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, h, r, g, b, a) blend_rect(il, y, 1, h, r, g, b, a * (1 - (xl - il))) blend_rect(ir, y, 1, h, r, g, b, a * (xr - ir)) end local CLOUDS = { { 17, 30, 1.7, 8 }, { 23, 22, -1.1, 46 }, { 13, 18, 1.2, 24 } } local function clouds(t) for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[2] local x = (c[4] + t * c[3]) % (W + L) - L band(x + 4, x + L - 4, c[1], 1, 190, 96, 112, 0.42) band(x, x + L, c[1] + 1, 1, 190, 96, 112, 0.34) band(x + 6, x + L - 8, c[1] + 2, 1, 150, 70, 110, 0.3) end end local function glints(t) for y = HZ + 1, SHORE - 1 do local k = y - HZ local cx = SX + 1.6 * sin(t * 1.3 + y) local w = 3 + k * 0.7 local a = 0.32 + 0.26 * sin(t * 2.1 + y * 1.7) band(cx - w, cx - 0.8, y, 1, 255, 230, 170, a) band(cx + 0.8, cx + w, y, 1, 255, 230, 170, 0.58 - a) end end local function wave(u, n) local h = hash32(n * 0x9E3779B1) local y, a, run = 0, 0, 0 if u < 0.72 then local s = u / 0.72 y = HZ + 1 + 14.4 * s ^ 1.5 a = 0.2 + 0.6 * s else local v = (u - 0.72) / 0.28 if v < 0.4 then y = SHORE + 2.4 * (v / 0.4) else y = SHORE + 2.4 - 4.4 * ((v - 0.4) / 0.6) end a = 0.75 * (1 - v) run = 1 end local iy = floor(y) local f = y - iy local x0 = 12 + umod(h, 5) local seg = 3 for k = 0, seg - 1 do local xa = x0 + k * 13 + umod(h >> (3 * k + 4), 3) local xb = xa + 7 + umod(h >> (3 * k + 12), 5) band(xa, xb, iy, 1, FOAM[1], FOAM[2], FOAM[3], a * (1 - f)) band(xa, xb, iy + 1, 1, FOAM[1], FOAM[2], FOAM[3], a * f) if run == 0 and u > 0.25 then band(xa, xb, iy - 1, 1, DEEP[1], DEEP[2], DEEP[3], 0.22) end end if run == 1 then local top = floor(y + 0.5) local d = (u - 0.72) / 0.28 local hh = top - SHORE blend_rect(12, SHORE, 40, 2 + (hh > 0 and hh or 0), WET[1], WET[2], WET[3], 0.28 * (1 - d)) blend_rect(12, SHORE - 1, 40, 1, FOAM[1], FOAM[2], FOAM[3], 0.18 * (1 - d)) end end local GUL = { { { -2, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 }, { 2, -1 } }, { { -2, 0 }, { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } }, { { -2, 1 }, { -1, 0 }, { 0, -1 }, { 1, 0 }, { 2, 1 } }, } local GSEQ = { 1, 2, 3, 2 } local function gull(k, t) local v = 6 + 1.7 * k local span = W + 28 local d = (t * v + k * 23) % span - 14 local x = (k % 2 == 0) and d or (W - d) local y = floor(15 + 3.4 * k + 1.3 * sin(t * 1.5 + k) + 0.5) local pose = GUL[GSEQ[(frame // 5 + k) % 4 + 1]] local ix = floor(x) local f = x - ix local r, g, b = c3(GULL) for i = 1, 5 do local p = pose[i] if k % 2 == 1 and (i == 1 or i == 5) then goto skip end blend_rect(ix + p[1], y + p[2], 1, 1, r, g, b, 1 - f) blend_rect(ix + p[1] + 1, y + p[2], 1, 1, r, g, b, f) ::skip:: end end function render() if STAGE <= #STAGES then if CONTINUES[STAGE] then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() PART = snapshot(0, 0, W, H) STAGE = STAGE + 1 if BACK then blit_rgb(BACK, 0, 0, W, H) if STAGE > 3 then blit_rgb(PART, 0, 0, W, H, c3(KEY)) end end if STAGE <= #STAGES then gfx.ready(false) end return end local t = frame / FPS blit_rgb(BACK, 0, 0, W, H) clouds(t) glints(t) local per = 7.6 - 1.2 * (cfg.waves or 3) for k = 0, 2 do local c = t / per + k / 3 wave(c % 1, floor(c) * 3 + k) end for k = 0, (cfg.gulls or 3) - 1 do gull(k, t) end blit_rgb(FRONT, 0, 0, W, H, c3(KEY)) end