info = { type = "image", title = "Foggy shore", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cloaked figure on misty rocks by the sea", api = 44, } local tr = ({ sk = { ["Fog"] = "Hmla", ["Surf"] = "Príboj" }, cs = { ["Fog"] = "Mlha", ["Surf"] = "Příboj" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fog", label = T("Fog"), type = "int", min = 1, max = 5, default = 3 }, { key = "surf", label = T("Surf"), type = "int", min = 1, max = 5, default = 2 }, } 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, max, min, pi = math.floor, math.sin, math.sqrt, math.max, math.min, math.pi local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SKY_T, SKY_H = { 168, 198, 196 }, { 228, 236, 228 } local SEA_H, SEA_N = { 190, 214, 208 }, { 80, 122, 132 } local ROCK_T, ROCK_M, ROCK_L = { 120, 128, 128 }, { 74, 82, 88 }, { 52, 62, 70 } local SAND_W, SAND_D = { 208, 200, 180 }, { 186, 152, 106 } local FOAM = { 240, 246, 242 } local FX, FY = 22, 33 local SUN_X, SUN_Y, SUN_R = 12, 9, 24 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 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 YT, XR = {}, {} for x = 0, W - 1 do local y if x < 14 then y = 38 - floor(x * 5 / 14) elseif x <= 30 then y = FY elseif x <= 46 then y = FY + (x - 30) else y = 99 end if (x < 17 or x > 27) and y < 99 then y = y + umod(hash32(x * 0x9E3779B1), 2) end YT[x] = y end for y = 30, 63 do local m = -1 for x = 0, W - 1 do if YT[x] <= y then m = x end end XR[y] = m end local BG, PART, STAGE = nil, nil, 1 local MASK, GLOW = {}, nil local function st_sky_sea() for y = 0, 28 do rect(0, y, W, 1, mix(SKY_T, SKY_H, (y / 28) ^ 1.3)) end rect(46, 25, 6, 5, 148, 168, 172); rect(47, 23, 4, 2, 148, 168, 172); rect(0, 27, 30, 3, 178, 198, 198) rect(0, 26, 12, 1, 178, 198, 198) for y = 29, 52 do rect(0, y, W, 1, mix(SEA_H, SEA_N, ((y - 29) / 23) ^ 0.8)) end for i = 1, 22 do local h = hash32(i * 0xC2B2AE35) local y = 30 + umod(h, 20) blend_rect(umod(h >> 8, 50), y, 6 + umod(h >> 16, 9), 1, 240, 246, 242, 0.22) end end local function st_rock() blit_rgb(PART, 0, 0, W, H) for y = 31, 57 do local t = (y - 31) / 26 local r, g, b if t < 0.4 then r, g, b = mix(ROCK_T, ROCK_M, t / 0.4) else r, g, b = mix(ROCK_M, ROCK_L, (t - 0.4) / 0.6) end local x0 = nil for x = 0, W do local on = x < W and YT[x] <= y if on and not x0 then x0 = x end if not on and x0 then rect(x0, y, x - x0, 1, r, g, b); x0 = nil end end end for x = 0, 46 do rect(x, YT[x], 1, 1, 156, 166, 164) rect(x, YT[x] + 1, 1, 1, 104, 114, 116) end for i = 1, 34 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, 44), 34 + umod(h >> 8, 18) if YT[x] < y - 2 and YT[min(x + 3, 46)] < y - 2 then rect(x, y, 2 + umod(h >> 16, 3), 1, 100, 110, 112) rect(x + 1, y + 1, 1, 1, 100, 110, 112) end end for i = 1, 56 do local h = hash32(i * 0x27D4EB2F) local x, y = umod(h, 44), 35 + umod(h >> 8, 19) if YT[x] < y - 1 then px(x, y, 38, 46, 54); if h & 0x10000 == 0 then px(x + 1, y + 1, 38, 46, 54) end end end end local function st_shore() blit_rgb(PART, 0, 0, W, H) for y = 50, H - 1 do local r, g, b = mix(SAND_W, SAND_D, ((y - 50) / 13) ^ 0.9) rect(0, y, W, 1, r, g, b) end for x = 0, 34 do local ys = 50 + floor((34 - x) * 0.12) if ys > 50 then for y = 50, ys - 1 do local t = (y - 31) / 26 local r, g, b if t < 0.4 then r, g, b = mix(ROCK_T, ROCK_M, t / 0.4) else r, g, b = mix(ROCK_M, ROCK_L, (t - 0.4) / 0.6) end if YT[x] <= y then px(x, y, r, g, b) end end end end for i = 1, 40 do local h = hash32(i * 0x9E3779B1) px(umod(h, W), 52 + umod(h >> 8, 11), 150, 122, 88) end for i = 1, 12 do local h = hash32(i * 0x7FEB352D) px(umod(h, W), 51 + umod(h >> 8, 10), 226, 214, 190) end for x = 0, 13 do local y = 57 + x // 3 rect(x, y, 1, H - y, 40, 46, 52); px(x, y, 84, 92, 96) end for x = 54, 63 do local y = 60 + (x - 54) // 4 rect(x, y, 1, H - y, 44, 50, 56); px(x, y, 88, 96, 100) end local rows = { { 18, 1 }, { 19, 1 }, { 20, 1 }, { 21, 2 }, { 22, 2 }, { 23, 2 }, { 24, 2 }, { 25, 2 }, { 26, 2 }, { 27, 3 }, { 28, 3 }, { 29, 3 }, { 30, 3 }, { 31, 3 }, { 32, 3 } } for i = 1, #rows do local y, hw = rows[i][1], rows[i][2] rect(FX - hw, y, 2 * hw + 1, 1, 34, 76, 58) rect(FX - hw, y, 1, 1, 58, 112, 86) rect(FX + hw, y, 1, 1, 22, 52, 42) if hw >= 2 then px(FX + 1, y, 26, 60, 46) end end rect(FX - 1, 19, 2, 2, 176, 158, 136) px(FX - 1, 19, 34, 38, 44) rect(FX + 5, 25, 1, 8, 92, 70, 50) px(FX + 4, 25, 34, 76, 58) rect(FX - 5, FY, 11, 1, 50, 58, 64) BG = snapshot(0, 0, W, H) end local LAYERS = { { y = 26, h = 14, v = 1.3, peak = 0.9, ph = 0.0, col = { 236, 240, 236 } }, { y = 36, h = 16, v = 2.4, peak = 1.0, ph = 0.37, col = { 228, 236, 232 } }, { y = 48, h = 15, v = 3.8, peak = 0.8, ph = 0.71, col = { 232, 238, 230 } }, } local function st_fog(k) return function() local L = LAYERS[k] local rows, row = {}, {} for yy = 0, L.h - 1 do local p = sin(pi * (yy + 0.5) / L.h) for x = 0, 63 do local n = 0.5 + 0.28 * sin(2 * pi * (x * 2 / 64 + L.ph + yy * 0.07)) + 0.22 * sin(2 * pi * (x * 3 / 64 + L.ph * 2 - yy * 0.05)) row[x + 1] = floor(255 * p * n * L.peak) end local s = string.char(table.unpack(row, 1, 64)) rows[yy + 1] = s .. s end MASK[k] = table.concat(rows) end end local function st_glow() local n = 2 * SUN_R + 1 local rows, row = {}, {} for gy = 0, n - 1 do for gx = 0, n - 1 do local d = sqrt((gx - SUN_R) ^ 2 + (gy - SUN_R) ^ 2) row[gx + 1] = d < SUN_R and floor(255 * (1 - d / SUN_R)) or 0 end rows[gy + 1] = string.char(table.unpack(row, 1, n)) end GLOW = table.concat(rows) end local STAGES = { st_sky_sea, st_rock, st_shore, st_fog(1), st_fog(2), st_fog(3), st_glow } local function front(u, n, a0) local h = hash32(n * 0x9E3779B1) local y, a if u < 0.8 then local s = u / 0.8 y = 34 + 15.4 * s ^ 1.3 a = a0 * (0.3 + 0.7 * s) else local v = (u - 0.8) / 0.2 y = 49.4 + 1.6 * math.sin(v * pi * 0.5) - 0.8 * v * v a = a0 * (1 - v) end local iy = floor(y) local f = y - iy local xs = XR[iy] + 2 if xs > 60 then return end local mid = xs + 8 + umod(h, 12) for k = 0, 1 do local xa = k == 0 and xs or mid + 3 + umod(h >> 6, 4) local xb = k == 0 and mid or W 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) end if u >= 0.8 then blend_rect(XR[50] + 2, 50, W - XR[50] - 2, 1 + floor((y - 49.4) + 0.5), 120, 100, 80, 0.16 * (1 - (u - 0.8) / 0.2)) end end function render() if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 if BG then blit_rgb(BG, 0, 0, W, H) else PART = snapshot(0, 0, W, H) end gfx.ready(false) return end local t = frame / FPS local fog = cfg.fog or 3 blit_rgb(BG, 0, 0, W, H) blend_mask(GLOW, 252, 248, 230, SUN_X - SUN_R, SUN_Y - SUN_R, 2 * SUN_R + 1, 2 * SUN_R + 1, 2 * SUN_R + 1, 2 * SUN_R + 1, 0.5 + 0.05 * sin(t * 0.3)) local per = 16 - 2.4 * (cfg.surf or 2) for k = 0, 2 do local c = t / per + k / 3 front(c % 1, floor(c) * 3 + k, 0.9) end local sc = 0.2 + 0.12 * fog for k = 1, 3 do local L = LAYERS[k] local c = L.col blend_mask(MASK[k], c[1], c[2], c[3], -(floor(t * L.v) % 64), L.y, 128, L.h, 128, L.h, sc) end end