info = { type = "image", title = "Moon island", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Moonlit island, rippling sea and shooting stars", api = 41, } local tr = ({ sk = { ["Lighthouse"] = "Maják" }, cs = { ["Lighthouse"] = "Maják" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "beam", label = T("Lighthouse"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt, abs, PI = math.floor, math.sin, math.sqrt, math.abs, math.pi local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ = 42 local MX, MY, MR = 50, 12, 6 local SKY, SKY_LO = { 12, 20, 62 }, { 26, 38, 98 } local HALO1, HALO2 = { 58, 74, 148 }, { 38, 52, 118 } local MOON, MOON_D = { 252, 242, 204 }, { 214, 200, 156 } local LIT, RIM = { 132, 196, 104 }, { 84, 152, 82 } local MOSS, ROCK = { 50, 116, 66 }, { 24, 70, 50 } local WATER, WAVE = { 14, 34, 96 }, { 34, 64, 136 } local HLINE = { 52, 86, 162 } local REFL_G = { 22, 62, 60 } local REF1, REF2 = { 252, 236, 168 }, { 206, 184, 112 } local STAR, STAR_D = { 240, 240, 255 }, { 110, 120, 188 } local TOWER, TRED, ROOF = { 230, 234, 240 }, { 212, 52, 52 }, { 44, 38, 48 } local LAMP, LAMP_B = { 236, 196, 84 }, { 255, 240, 150 } local function c3(c) return c[1], c[2], c[3] end local function span(xa, xb, y, r, g, b) if xb <= xa then return end local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end local s0 = ia if xa > ia then blend(ia, y, r, g, b, ia + 1 - xa); s0 = ia + 1 end if ib > s0 then rect(s0, y, ib - s0, 1, r, g, b) end if xb > ib then blend(ib, y, r, g, b, xb - ib) end end local TOP = {} for x = 0, W - 1 do local dA, dB = abs(x - 13) / 22, abs(x - 31) / 15 local hA = dA < 1 and 29 * (1 - dA ^ 1.5) or 0 local hB = dB < 1 and 18 * (1 - dB ^ 1.3) or 0 local h = hA > hB and hA or hB if h > 0 then h = h + 1.3 * sin(x * 1.3) + 0.8 * sin(x * 2.9 + 1) end TOP[x] = h > 0.5 and floor(HZ - h + 0.5) or HZ end TOP[W] = HZ local LX = 40 local LB = TOP[LX] local LY = LB - 9 local SX, SY, SW, SP = {}, {}, {}, {} do local k, i = 0, 0 while k < 24 and i < 400 do i = i + 1 local h = hash32(i * 0x9E3779B1 + 77) local x, y = umod(h, W), umod(h >> 8, HZ - 4) local dx, dy = x - MX, y - MY if y < TOP[x] - 2 and dx * dx + dy * dy > (MR + 6) * (MR + 6) and abs(x - LX) > 2 then k = k + 1 SX[k], SY[k] = x, y SW[k] = 1.2 + umod(h >> 16, 60) * 0.03 SP[k] = umod(h >> 20, 628) / 100 end end end local RY, RX, RL, RV = {}, {}, {}, {} for i = 1, 7 do local h = hash32(i * 0x85EBCA6B + 9) RY[i] = HZ + 3 + umod(h, H - HZ - 4) RX[i] = umod(h >> 8, W) RL[i] = 3 + umod(h >> 16, 4) RV[i] = ((h >> 20) & 1 == 1 and 1 or -1) * (1.8 + umod(h >> 21, 8) * 0.3) end local BG = nil local function pine(x) local y = TOP[x] px(x, y - 5, c3(ROCK)) rect(x - 1, y - 4, 3, 2, c3(ROCK)) rect(x - 2, y - 2, 5, 2, c3(ROCK)) end local function build_bg() rect(0, 0, W, HZ, c3(SKY)) rect(0, 28, W, HZ - 28, c3(SKY_LO)) for x = 0, W - 1, 2 do px(x, 27, c3(SKY_LO)) end local R1, R2 = MR + 2.5, MR + 4.5 for y = MY - MR - 5, MY + MR + 5 do for x = MX - MR - 5, MX + MR + 5 do local dx, dy = x - MX, y - MY local d = sqrt(dx * dx + dy * dy) if d <= MR + 0.5 then px(x, y, c3(MOON)) elseif d <= R1 then px(x, y, c3(HALO1)) elseif d <= R2 then px(x, y, c3(HALO2)) end end end px(MX - 2, MY - 3, c3(MOON_D)); px(MX - 3, MY - 2, c3(MOON_D)); px(MX + 2, MY + 1, c3(MOON_D)) px(MX + 3, MY + 2, c3(MOON_D)); px(MX - 1, MY + 3, c3(MOON_D)); px(MX + 3, MY - 3, c3(MOON_D)) rect(0, HZ, W, H - HZ, c3(WATER)) rect(0, HZ, W, 1, c3(HLINE)) for i = 1, 36 do local h = hash32(i * 0x27D4EB2F + 3) local y = HZ + 3 + umod(h, H - HZ - 3) rect(umod(h >> 8, W), y, 2 + umod(h >> 16, 4), 1, c3(WAVE)) end for x = 0, W - 1 do local top = TOP[x] if top < HZ then rect(x, top, 1, HZ - top, c3(MOSS)) if TOP[x + 1] > top then rect(x, top, 1, 3, c3(LIT)) else rect(x, top + 1, 1, 4, c3(ROCK)); px(x, top, c3(RIM)) end rect(x, HZ - 2, 1, 2, c3(ROCK)) local rh = floor((HZ - top) * 0.3 + 0.5) if rh > 0 then rect(x, HZ + 1, 1, rh, c3(REFL_G)) end end end for r = 2, 12, 3 do rect(0, HZ + 1 + r, 47, 1, c3(WATER)) end for k = 1, 5 do local b = 2 + k * 6 for x = 0, 46 do local y = floor(b + 0.55 * x + 1.2 * sin(x * 0.7 + k) + 0.5) if y > TOP[x] + 4 and y < HZ - 2 then px(x, y, c3(ROCK)) end end end pine(24); pine(29); pine(34) for r = 0, 7 do local c = (r // 2) % 2 == 0 and TOWER or TRED rect(LX - 1, LB - 8 + r, 3, 1, c3(c)) end rect(LX - 1, LB - 10, 3, 1, c3(ROOF)); px(LX, LB - 11, c3(ROOF)) BG = gfx.snapshot(0, 0, W, H) end local BEAM = true function on_settings() BEAM = cfg.beam ~= false end local function draw_stars(t) for k = 1, #SX do local v = sin(t * SW[k] + SP[k]) if v > 0.55 then px(SX[k], SY[k], c3(STAR)) elseif v > -0.5 then px(SX[k], SY[k], c3(STAR_D)) end end local v = sin(t * 1.5) px(6, 6, c3(STAR)); px(30, 4, c3(STAR)) if v > 0.3 then px(5, 6, c3(STAR_D)); px(7, 6, c3(STAR_D)); px(6, 5, c3(STAR_D)); px(6, 7, c3(STAR_D)) else px(29, 4, c3(STAR_D)); px(31, 4, c3(STAR_D)); px(30, 3, c3(STAR_D)); px(30, 5, c3(STAR_D)) end end local function draw_reflection(t) for y = HZ + 1, H - 1 do local d = y - HZ local hw = 0.8 + d * 0.12 if (y * 3 + floor(t * 3.33)) % 5 == 0 then hw = hw * 0.4 end local off = 1.4 * sin(t * 2.1 + y * 0.9) + 0.7 * sin(t * 1.29 - y * 0.37) local c = MX + 0.5 + off local col = d < 9 and REF1 or REF2 span(c - hw, c + hw, y, col[1], col[2], col[3]) end local wr, wg, wb = WAVE[1], WAVE[2], WAVE[3] for i = 1, #RY do local L = RL[i] local x = (RX[i] + t * RV[i]) % (W + L) - L span(x, x + L, RY[i], wr, wg, wb) end end local function draw_lamp(t) local p = (t % 4) / 4 local lr, lg, lb = LAMP_B[1], LAMP_B[2], LAMP_B[3] if (p >= 0.3 and p < 0.36) or (p >= 0.8 and p < 0.86) then rect(LX - 1, LY, 3, 1, lr, lg, lb) px(LX - 2, LY, lr, lg, lb); px(LX + 2, LY, lr, lg, lb) px(LX, LY - 1, lr, lg, lb) else rect(LX - 1, LY, 3, 1, c3(LAMP)) end if p < 0.3 then local f = sin(p / 0.3 * PI) blend_rect(LX + 2, LY, 6, 1, lr, lg, lb, 0.5 * f) blend_rect(LX + 8, LY, 8, 1, lr, lg, lb, 0.25 * f) elseif p >= 0.5 and p < 0.8 then local f = sin((p - 0.5) / 0.3 * PI) blend_rect(LX - 7, LY, 6, 1, lr, lg, lb, 0.5 * f) blend_rect(LX - 15, LY, 8, 1, lr, lg, lb, 0.25 * f) end end local TAIL = { 0.75, 0.55, 0.4, 0.28, 0.18 } local function window(len, t) local c = data.time if c.ok then return c.unix // len, c.unix % len + c.ms / 1000 end return floor(t / len), t % len end local function draw_meteor(t) local n, le = window(6, t) if le >= 0.53 then return end local h = hash32(n * 0x9E3779B1 + 0x3C) if umod(h, 3) == 0 then return end local dir = ((h >> 12) & 1) == 1 and 1 or -1 local x0 = dir < 0 and 34 + umod(h >> 4, 26) or 2 + umod(h >> 4, 26) local y0 = 2 + umod(h >> 9, 8) local vx, vy = 60 * dir, 30 local fade = le < 0.1 and le / 0.1 or (le > 0.4 and (0.53 - le) / 0.13 or 1) local sr, sg, sb = STAR[1], STAR[2], STAR[3] for j = 0, #TAIL do local x = floor(x0 + vx * (le - j * 0.02) + 0.5) local y = floor(y0 + vy * (le - j * 0.02) + 0.5) if x >= 0 and x < W and y >= 0 and y < TOP[x] - 1 then local a = (j == 0 and 1 or TAIL[j]) * fade blend(x, y, sr, sg, sb, a) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS draw_stars(t) draw_meteor(t) draw_reflection(t) if BEAM then draw_lamp(t) else rect(LX - 1, LY, 3, 1, c3(LAMP)) end end