info = { type = "image", title = "Lantern on the pier", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Statue with a pulsing lantern by the night sea", api = 44, } local tr = ({ sk = { ["Pulse"] = "Pulz svetla", ["Fog"] = "Hmla", ["Boat"] = "Loďka" }, cs = { ["Pulse"] = "Puls světla", ["Fog"] = "Mlha", ["Boat"] = "Loďka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pulse", label = T("Pulse"), type = "int", min = 1, max = 5, default = 3 }, { key = "fog", label = T("Fog"), type = "int", min = 0, max = 5, default = 3 }, { key = "boat", label = T("Boat"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt, max, min = math.floor, math.sin, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HOR = 31 local LX, LY = 22, 29 local WARM = { 255, 184, 84 } local FOGC = { 156, 180, 190 } local BG, STAGE = nil, 0 local function lerp(a, b, t) return a + (b - a) * t end local function disc(cx, cy, r, a) for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, WARM[1], WARM[2], WARM[3], a) end end local function build_sky_sea() for y = 0, HOR - 1 do local t = y / (HOR - 1) rect(0, y, W, 1, floor(lerp(12, 36, t) + 0.5), floor(lerp(22, 62, t) + 0.5), floor(lerp(40, 84, t) + 0.5)) end for y = HOR, H - 1 do local t = (y - HOR) / (H - 1 - HOR) rect(0, y, W, 1, floor(lerp(32, 8, t) + 0.5), floor(lerp(60, 22, t) + 0.5), floor(lerp(76, 34, t) + 0.5)) end rect(0, HOR, W, 1, 70, 98, 112) for i = 1, 46 do local h = hash32(i * 0x9E3779B1) local y = HOR + 2 + umod(h, H - HOR - 3) local len = 3 + (y - HOR) // 4 + umod(h >> 8, 4) local x = umod(h >> 12, W) if i % 3 == 0 then blend_rect(x, y, len, 1, 150, 190, 205, 0.12) else blend_rect(x, y, len, 1, 0, 6, 16, 0.25) end end BG = gfx.snapshot(0, 0, W, H) end local function span(y) if y < 17 or y > 45 then return nil end if y <= 23 then local hw = ({ 2, 3, 3, 3, 3, 2, 1 })[y - 16] return 11 - hw, 10 + hw end if y == 24 then return 9, 12 end if y == 25 then return 7, 14 end if y == 26 then return 6, 15 end local hw = 5 + (y - 27) // 6 return 11 - hw, 10 + hw end local function bronze(x, y, t, tint) local dx, dy = x - LX, (y - LY) * 0.8 local k = max(0, 1 - sqrt(dx * dx + dy * dy) / 28) * (0.2 + 0.8 * t) if tint then return floor(30 + 120 * k), floor(28 + 112 * k), floor(26 + 92 * k) end return floor(26 + 214 * k), floor(22 + 160 * k), floor(18 + 66 * k) end local function build_pier() disc(LX, LY, 15, 0.065) disc(LX, LY, 8, 0.09) rect(0, 52, 44, 1, 100, 70, 42) rect(0, 53, 44, 4, 62, 44, 32) rect(44, 52, 1, 5, 40, 30, 26) for x = 6, 40, 7 do rect(x, 53, 1, 4, 46, 32, 26) end for _, x in ipairs({ 3, 13, 24, 35, 42 }) do rect(x, 57, 2, 7, 34, 26, 24) end for y = 46, 51 do for x = 3, 18 do px(x, y, bronze(x, y, (x - 3) / 15, true)) end end BG = gfx.snapshot(0, 0, W, H) end local function build_figure() for y = 17, 45 do local xl, xr = span(y) for x = xl, xr do local r, g, b = bronze(x, y, (x - xl) / max(1, xr - xl)) if y >= 32 and (x == 9 or x == 13) then r, g, b = r * 0.72, g * 0.72, b * 0.72 end if x == xr then r, g, b = min(255, r * 1.15), min(255, g * 1.1), b end px(x, y, floor(r), floor(g), floor(b)) end end rect(3, 19, 1, 33, 52, 40, 28); rect(2, 18, 3, 1, 90, 70, 40) for k = 0, 5 do px(15 + k, 26 - k // 3, bronze(15 + k, 26, 0.9)); px(15 + k, 27 - k // 3, bronze(15 + k, 27, 0.9)) end rect(20, 24, 2, 3, bronze(20, 25, 0.9)) rect(21, 26, 2, 1, 70, 52, 34) rect(20, 27, 5, 1, 82, 60, 38); rect(20, 31, 5, 1, 82, 60, 38) rect(20, 28, 1, 3, 82, 60, 38); rect(24, 28, 1, 3, 82, 60, 38) BG = gfx.snapshot(0, 0, W, H) end local function fseg(x, y, len, c, a) local x0 = floor(x) local xe = x + len local x1 = floor(xe) if x1 == x0 then blend_rect(x0, y, 1, 1, c[1], c[2], c[3], a * len) return end if x1 - x0 > 1 then blend_rect(x0 + 1, y, x1 - x0 - 1, 1, c[1], c[2], c[3], a) end blend_rect(x0, y, 1, 1, c[1], c[2], c[3], a * (1 - (x - x0))) local f1 = xe - x1 if f1 > 0.02 then blend_rect(x1, y, 1, 1, c[1], c[2], c[3], a * f1) end end local function draw_light(t, spd) local p = 0.5 + 0.5 * sin(t * 6.2832 * spd / 10.2) + 0.06 * sin(t * 9.1) if p > 1 then p = 1 end rect(21, 28, 3, 3, floor(lerp(250, 255, p)), floor(lerp(190, 244, p)), floor(lerp(60, 170, p))) disc(LX, LY, 5, 0.10 + 0.14 * p) for y = 27, 45 do local _, xr = span(y) blend_rect(xr - 2, y, 3, 1, WARM[1], WARM[2], WARM[3], 0.04 + 0.12 * p) end blend_rect(4, 52, 36, 1, WARM[1], WARM[2], WARM[3], 0.05 + 0.1 * p) for y = HOR + 1, 51 do local d = y - HOR local w = 2 + d * 0.16 local cx = LX + 1.4 * sin(y * 0.7 + t * 2.1) * (0.4 + d / 20) local a = (0.62 - d * 0.014) * (0.25 + 0.75 * p) * (0.55 + 0.45 * sin(y * 1.7 - t * 2.6)) if a > 0.03 then fseg(cx - w / 2, y, w, WARM, a) end end end local function draw_fog(t, fog) for i = 1, fog * 2 do local h = hash32(i * 0x85EBCA6B) local y = HOR + 1 + umod(h, 17) local len = 22 + umod(h >> 6, 18) local v = (1.2 + umod(h >> 10, 20) / 10) * (i % 2 == 1 and 1 or -1) local x = (umod(h >> 14, W + len) + t * v) % (W + len) - len local a = 0.05 + umod(h >> 20, 5) / 100 fseg(x + 3, y - 1, len - 6, FOGC, a * 0.6) fseg(x, y, len, FOGC, a) fseg(x + 5, y + 1, len - 10, FOGC, a * 0.6) end end local function draw_clouds(t) for i = 1, 3 do local len = 26 + i * 6 local x = (i * 21 + t * (0.9 + i * 0.35)) % (W + len) - len local y = 3 + i * 6 fseg(x + 4, y, len - 8, FOGC, 0.08) fseg(x, y + 1, len, FOGC, 0.10) fseg(x + 6, y + 2, len - 12, FOGC, 0.07) end end local function draw_boat(t, p) local x = (44 + t * 0.55) % (W + 24) + 8 if x > W + 6 then x = x - (W + 24) end fseg(x, HOR - 1, 5, { 10, 16, 24 }, 1) local xi = floor(x + 2) local f = x + 2 - xi blend(xi, HOR - 2, 10, 16, 24, 1) local a = 0.7 + 0.3 * p blend(xi, HOR - 3, 255, 214, 96, a * (1 - f)) blend(xi + 1, HOR - 3, 255, 214, 96, a * f) fseg(x - 1, HOR + 1, 7, WARM, 0.12 * a) end function render() if STAGE == 0 then build_sky_sea(); STAGE = 1 elseif STAGE == 1 then gfx.blit_rgb(BG, 0, 0, W, H) build_pier(); STAGE = 2 elseif STAGE == 2 then gfx.blit_rgb(BG, 0, 0, W, H) build_figure(); STAGE = 3 else gfx.blit_rgb(BG, 0, 0, W, H) end if STAGE < 3 then gfx.ready(false) end local t = frame / FPS draw_clouds(t) if STAGE == 3 then draw_light(t, cfg.pulse or 3) end draw_fog(t, cfg.fog or 3) if cfg.boat ~= false then draw_boat(t, 0.5 + 0.5 * sin(t * 1.7)) end end