info = { type = "image", title = "Beacon coast", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Night bay with a sweeping lighthouse beam", api = 44, } local tr = ({ sk = { ["Beam speed"] = "Rýchlosť lúča", ["Haze"] = "Opar", ["Town lights"] = "Svetlá mesta" }, cs = { ["Beam speed"] = "Rychlost paprsku", ["Haze"] = "Opar", ["Town lights"] = "Světla města" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Beam speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "haze", label = T("Haze"), type = "int", min = 0, max = 5, default = 3 }, { key = "town", label = T("Town lights"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, abs, min, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, SHY = 36, 51 local LX, LY = 13, 22 local SKY = { 23, 36, 95 } local HILL, ROCK, ROCK_L = { 18, 26, 58 }, { 10, 16, 36 }, { 44, 58, 108 } local WATER = { 8, 16, 46 } local TOWER, TOWER_R, TOWER_S = { 168, 166, 178 }, { 140, 56, 62 }, { 78, 80, 104 } local LAMP, BEAM = { 255, 232, 150 }, { 255, 244, 206 } local SAND_N, SAND_F = { 18, 26, 54 }, { 44, 62, 112 } local FOAM = { 150, 178, 226 } local function mix(a, b, t) return a + (b - a) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function vn(i, s) return (hash32(i * 0x9E3779B1 + s) & 0x7FFF) / 32767 end local function h2(x, y, s) return (hash32(x * 0x9E3779B1 + y * 0x85EBCA6B + s) & 0x7FFF) / 32767 end local function noise(x, s) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, s) * (1 - f) + vn(i + 1, s) * f end local function hill_top(x) return 36 - floor((max(0, x - 22) / 41) ^ 1.1 * 6 + noise(x / 4, 12) * 2.2 + 0.5) end local TL = {} for x = 23, W - 1 do local dens = noise(x / 5, 4) local top = hill_top(x) if dens > 0.4 and vn(x, 10) > 0.25 and top < 35 then TL[#TL + 1] = { x = x, y = 34, c = umod(hash32(x * 0x27D4EB2F), 5) } if vn(x, 11) < dens - 0.2 then TL[#TL + 1] = { x = x, y = 33 - floor(vn(x, 13) * 2), c = umod(hash32(x * 3 + 1), 5) } end end if vn(x, 14) > 0.88 and top < 33 then TL[#TL + 1] = { x = x, y = top + 2 + floor(vn(x, 15) * 3), c = 2 } end end local TLC = { { 255, 218, 140 }, { 255, 188, 96 }, { 255, 232, 180 }, { 214, 226, 255 }, { 255, 160, 80 } } local function sky() rect(0, 0, W, HZ, SKY[1], SKY[2], SKY[3]) for i = 1, 16 do local h = hash32(i * 0x9E3779B1) local y = umod(h >> 8, 22) local v = 120 + umod(h >> 16, 90) px(umod(h, W), y, v, v, min(255, v + 30)) end end local function shore(on) for x = 30, W - 1 do local yt = 36 - floor((x - 30) / 33 * 10 + noise(x / 3.5, 31) * 2.4) rect(x, yt, 1, HZ - yt, 24, 34, 80) end for x = 22, W - 1 do local yt = hill_top(x) rect(x, yt, 1, HZ - yt, HILL[1], HILL[2], HILL[3]) end if on then blend_rect(30, 31, W - 30, 2, 255, 170, 90, 0.1) blend_rect(28, 33, W - 28, 3, 255, 170, 90, 0.12) for i = 1, #TL do local l = TL[i] local c = TLC[l.c + 1] px(l.x, l.y, c[1], c[2], c[3]) end end end local function cape() for x = 0, 27 do local yt = 34 + floor(max(0, x - 11) * 0.62 + noise(x / 2.5, 21) * 1.6) rect(x, yt, 1, 52 - yt, ROCK[1], ROCK[2], ROCK[3]) px(x, yt, ROCK_L[1], ROCK_L[2], ROCK_L[3]) end for y = 24, 35 do local hw = y < 29 and 1 or 2 local band = ((y - 24) // 3) % 2 == 0 local c = band and TOWER or TOWER_R rect(LX - hw, y, 2 * hw + 1, 1, c[1], c[2], c[3]) px(LX + hw, y, TOWER_S[1], TOWER_S[2], TOWER_S[3]) end rect(LX - 3, 23, 7, 1, ROCK_L[1], ROCK_L[2], ROCK_L[3]) rect(LX - 1, 21, 3, 2, LAMP[1], LAMP[2], LAMP[3]) rect(LX - 2, 20, 5, 1, ROCK[1], ROCK[2], ROCK[3]) px(LX, 19, ROCK[1], ROCK[2], ROCK[3]) rect(4, 32, 6, 4, 40, 40, 68) rect(3, 31, 8, 1, 62, 36, 50) px(6, 33, 255, 200, 110); px(7, 33, 255, 200, 110) end local XL, XR = 1, W - 2 local function water() for y = HZ, SHY - 1 do blit_rgb(snapshot(0, 2 * HZ - 1 - y, W, 1), 0, y, W, 1) blend_rect(0, y, W, 1, WATER[1], WATER[2], WATER[3], 0.6) end for i = 1, #TL do local l = TL[i] local c = TLC[l.c + 1] if l.y >= 33 then local len = 3 + umod(hash32(i * 0x85EBCA6B), 5) for j = 1, len do local y = HZ + j if y < SHY - 2 and hash32(i * 0x27D4EB2F + j) & 3 ~= 0 then blend_rect(l.x, y, 1, 1, c[1], c[2], c[3], 0.4 * (1 - j / (len + 1))) end end end end end local function beach(ya, yb) for y = ya, yb do local t = (y - SHY) / (H - 1 - SHY) local wet = max(0, 1 - t * 1.6) for x = 0, W - 1 do local g = 0.88 + 0.24 * h2(x // (y > 57 and 2 or 1), y, 8) local r = mix(SAND_N[1], SAND_F[1], wet) * g local gg = mix(SAND_N[2], SAND_F[2], wet) * g local b = mix(SAND_N[3], SAND_F[3], wet) * g px(x, y, rgb(r, gg, b)) end end end local BG, WROW, PART, STAGE = nil, {}, nil, 1 local TOWN = true local STAGES local function finish() for y = HZ + 1, SHY - 3 do WROW[y] = snapshot(XL, y, XR - XL + 1, 1) end BG = snapshot(0, 0, W, H) end STAGES = { sky, function() shore(TOWN) end, water, cape, function() beach(SHY - 2, SHY + 5) end, function() beach(SHY + 6, SHY + 13) end, finish, } local function build() if STAGE > 1 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 local SPEED, HAZE = 3, 3 function on_settings() SPEED, HAZE = cfg.speed or 3, cfg.haze or 3 local town = cfg.town ~= false if town ~= TOWN or not BG then TOWN = town BG, STAGE = nil, 1 end end local function beam(phi) local c, s = cos(phi), sin(phi) local L = 46 * abs(c) ^ 0.8 if L < 6 then return end local dir = c > 0 and 1 or -1 local ye = LY - s * L * 0.3 local n = floor(L / 4) local a0 = 0.05 + 0.05 * HAZE for i = 0, n - 1 do local f = (i + 0.5) / n local x = dir > 0 and LX + i * 4 or LX - i * 4 - 3 local cy = LY + (ye - LY) * f local w = 1.2 + 4 * f local a = a0 * (1 - f * 0.7) blend_rect(x, floor(cy - w * 1.1 + 0.5), 4, floor(w * 2.2 + 0.5), BEAM[1], BEAM[2], BEAM[3], a * 0.6) blend_rect(x, floor(cy - w * 0.6 + 0.5), 4, floor(w * 1.2 + 0.5), BEAM[1], BEAM[2], BEAM[3], a * 0.8) end end local function flash(k) blend_rect(LX - 1, LY - 1, 3, 3, 255, 250, 220, min(1, k * 1.2)) blend_rect(LX - 2, LY - 2, 5, 5, 255, 244, 200, 0.55 * k) blend_rect(LX - 4, LY - 4, 9, 9, 255, 238, 190, 0.3 * k) blend_rect(LX - 8, LY - 1, 17, 3, 255, 238, 190, 0.3 * k) blend_rect(LX - 1, LY - 8, 3, 17, 255, 238, 190, 0.2 * k) end local NW, LIFE, TW = 2, 10, 5 local function waves(t) for k = 0, NW - 1 do local pos = t / LIFE + k / NW local q = pos % 1 local n = floor(pos) local yf = HZ + 8 + (SHY - HZ - 8) * q ^ 1.8 local y = floor(yf) local f = yf - y local a = 0.12 + 0.3 * q local seg = 4 + floor(q * 6) local x, i = -umod(n * 7 + k * 5, seg), 0 while x < W do local h = hash32(n * 0x9E3779B1 + k * 0x27D4EB2F + i * 0x85EBCA6B) local len = seg + umod(h >> 4, seg) if umod(h >> 10, 8) > 1 then blend_rect(x, y, len, 1, FOAM[1], FOAM[2], FOAM[3], a * (1 - f)) blend_rect(x, y + 1, len, 1, FOAM[1], FOAM[2], FOAM[3], a * f) end x = x + len + umod(h >> 14, 3) i = i + 1 end end local u = (t % TW) / TW local s if u < 0.28 then s = 5 * (1 - (1 - u / 0.28) ^ 2) else s = 5 * (1 - ((u - 0.28) / 0.72) ^ 2) end local yf = SHY + s if yf - SHY >= 1 then blend_rect(0, SHY - 1, W, floor(yf) - SHY + 1, 120, 150, 210, 0.2) end for xi = 0, 15 do local x = xi * 4 local yv = yf + (hash32(xi * 0x9E3779B1 + floor(t / TW) * 0x85EBCA6B) & 7) / 7 * 2 - 1 blend_rect(x, floor(yv), 4, 1, FOAM[1], FOAM[2], FOAM[3], 0.5) end end function render() if not BG then build() if not BG then gfx.ready(false) end return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) local sp = 0.55 + 0.15 * 3 local p1, p2 = t * 6 * sp, t * 4.5 * sp for y = HZ + 1, SHY - 3 do local d = (y - HZ) / (SHY - 1 - HZ) local dx = max(-1, min(1, floor((0.5 + 0.9 * d) * (sin(y * 0.95 - p1) + 0.7 * sin(y * 1.9 + p2 + 1.3)) + 0.5))) if dx ~= 0 then blit_rgb(WROW[y], XL + dx, y, XR - XL + 1, 1) end end if TOWN then for i = 1, #TL, 3 do local l = TL[i] if sin(t * (0.9 + (i % 7) * 0.12) + i) > 0.55 then px(l.x, l.y, 90, 70, 60) end end end waves(t) local per = 20 / (1 + SPEED * 0.5) local w = 6.28318 / per beam(t * w) beam(t * w + 3.14159) local k = 0 for b = 0, 1 do local s = sin(t * w + b * 3.14159) if s > 0.55 then k = max(k, ((s - 0.55) / 0.45) ^ 2) end end blend_rect(LX - 1, LY - 1, 3, 3, 255, 236, 170, 0.5) if k > 0.05 then flash(k) end end