info = { type = "image", title = "Night harbour", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Boats rock by a lamp-lit pier at night", api = 33, } local tr = ({ sk = { ["Boats"] = "Lode", ["Visitor boat"] = "Návštevná loďka" }, cs = { ["Boats"] = "Lodě", ["Visitor boat"] = "Návštěvní loďka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "ships", label = T("Boats"), type = "int", min = 1, max = 3, default = 3 }, { key = "visit", label = T("Visitor boat"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, min, max = math.floor, math.sin, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local SHORE = 31 local WATER0 = 33 local DECK = 46 local LAMPS = { 24, 40, 56 } local LOOP = 600 local WARM = { 255, 208, 112 } local function c3(c) return c[1], c[2], c[3] end local function fill(x, y, w, h, c, a) if a >= 1 then rect(x, y, w, h, c3(c)) else blend_rect(x, y, w, h, c[1], c[2], c[3], a) end end local function hr(x, y, w, h, c, a) a = a or 1 local ix = floor(x) local f = x - ix if f < 0.04 then fill(ix, y, w, h, c, a) return end if w > 1 then fill(ix + 1, y, w - 1, h, c, a) end blend_rect(ix, y, 1, h, c[1], c[2], c[3], (1 - f) * a) blend_rect(ix + w, y, 1, h, c[1], c[2], c[3], f * a) end local function vr(x, y, w, h, c) local iy = floor(y) local f = y - iy if f < 0.04 then rect(x, iy, w, h, c3(c)) return end if h > 1 then rect(x, iy + 1, w, h - 1, c3(c)) end blend_rect(x, iy, w, 1, c[1], c[2], c[3], 1 - f) blend_rect(x, iy + h, w, 1, c[1], c[2], c[3], f) end local function sr(x, y, w, h, c) local iy = floor(y) local f = y - iy if f < 0.04 then hr(x, iy, w, h, c) return end if h > 1 then hr(x, iy + 1, w, h - 1, c) end hr(x, iy, w, 1, c, 1 - f) hr(x, iy + h, w, 1, c, f) end local BG local QL = {} local WINS = {} local function halo(cx, cy) for _, r in ipairs({ { 8, 0.07 }, { 5, 0.12 }, { 3, 0.2 } }) do for dy = -r[1], r[1] do local dx = math.floor(math.sqrt(r[1] * r[1] - dy * dy) + 0.5) blend_rect(cx - dx, cy + dy, 2 * dx + 1, 1, WARM[1], WARM[2], WARM[3], r[2]) end end end local function build() for y = 0, SHORE do local t = y / SHORE rect(0, y, W, 1, floor(6 + 14 * t + 0.5), floor(12 + 24 * t + 0.5), floor(58 + 56 * t + 0.5)) end for y = WATER0, H - 1 do local t = (y - WATER0) / (H - 1 - WATER0) rect(0, y, W, 1, floor(24 - 14 * t + 0.5), floor(46 - 24 * t + 0.5), floor(116 - 42 * t + 0.5)) end local x, k, nq, nw = 0, 0, 0, 0 while x < W do local h = hash32(k * 0x9E3779B1 + 7) local w, ht = 3 + umod(h, 5), 5 + umod(h >> 8, 8) local top = SHORE - ht local tone = (k % 2 == 0) and { 16, 24, 68 } or { 22, 30, 80 } rect(x, top, w, ht, c3(tone)) for wy = top + 2, SHORE - 2, 2 do for wx = x + 1, x + w - 2, 2 do local r = umod(hash32(wx * 131 + wy * 7919), 100) if r < 34 then px(wx, wy, 250, 196, 104) elseif r < 42 and nw < 8 then nw = nw + 1; WINS[nw] = { wx, wy, nw * 17 } end end end k = k + 1 x = x + w end rect(0, SHORE, W, 2, 44, 58, 122) for qx = 2, W - 1, 5 + 0 do local jx = qx + umod(hash32(qx * 977), 3) if jx < W then px(jx, SHORE, 255, 214, 128); nq = nq + 1; QL[nq] = jx end end for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) px(umod(h, W), umod(h >> 8, 20), 120, 130, 190) end for i = 1, #LAMPS do halo(LAMPS[i], 37) end rect(20, DECK, W - 20, 1, 130, 92, 56) rect(20, DECK + 1, W - 20, 1, 88, 60, 40) rect(20, DECK + 2, W - 20, 1, 44, 32, 34) for px0 = 24, 62, 8 do rect(px0, DECK + 3, 2, 4, 52, 38, 34) end rect(62, DECK + 3, 2, 4, 52, 38, 34) for i = 1, #LAMPS do local lx = LAMPS[i] rect(lx, 38, 1, DECK - 38, 70, 72, 96) rect(lx - 1, 36, 3, 2, c3(WARM)) px(lx, 35, 90, 92, 120) end BG = gfx.snapshot(0, 0, W, H) end local function boat_a(by) local TRIM, HULL, SAIL, MAST = { 196, 148, 100 }, { 128, 84, 60 }, { 226, 222, 204 }, { 150, 152, 176 } vr(6, 41 + by, 17, 1, TRIM); vr(7, 42 + by, 15, 1, HULL); vr(9, 43 + by, 11, 1, HULL) vr(14, 27 + by, 1, 14, MAST) vr(15, 29 + by, 2, 3, SAIL); vr(15, 32 + by, 4, 3, SAIL); vr(15, 35 + by, 6, 3, SAIL); vr(15, 38 + by, 8, 2, SAIL) vr(13, 31 + by, 1, 3, SAIL); vr(12, 34 + by, 2, 3, SAIL); vr(10, 37 + by, 4, 3, SAIL) vr(14, 26 + by, 1, 1, { 255, 90, 80 }) end local function boat_b(by) local HULL, TRIM = { 70, 96, 148 }, { 118, 148, 200 } vr(27, 41 + by, 11, 1, TRIM); vr(28, 42 + by, 10, 1, HULL); vr(29, 43 + by, 8, 1, HULL) vr(30, 37 + by, 5, 4, { 212, 206, 190 }); vr(29, 36 + by, 7, 1, { 196, 62, 58 }) vr(31, 38 + by, 3, 1, WARM) vr(35, 33 + by, 2, 4, { 214, 72, 62 }); vr(35, 34 + by, 2, 1, { 240, 232, 204 }) vr(31, 31 + by, 1, 5, { 150, 152, 176 }); vr(31, 30 + by, 1, 1, WARM) end local function boat_c(by) local HULL, TRIM = { 150, 66, 60 }, { 214, 128, 98 } vr(43, 41 + by, 11, 1, TRIM); vr(44, 42 + by, 9, 1, HULL); vr(45, 43 + by, 7, 1, HULL) vr(47, 38 + by, 4, 3, { 232, 204, 150 }); vr(48, 39 + by, 2, 1, { 120, 84, 60 }) vr(52, 31 + by, 1, 10, { 150, 152, 176 }); vr(52, 30 + by, 1, 1, WARM) vr(44, 39 + by, 3, 2, { 120, 84, 60 }) end local BOATS = { { fn = boat_a, amp = 0.9, per = 3.6, ph = 0.0, x = 6, w = 17, c = { 128, 84, 60 } }, { fn = boat_b, amp = 0.7, per = 3.0, ph = 1.3, x = 27, w = 11, c = { 70, 96, 148 } }, { fn = boat_c, amp = 0.8, per = 4.2, ph = 2.6, x = 43, w = 11, c = { 150, 66, 60 } }, } local function streak(x, y0, rows, w0, a0, c, ph, t) for i = 0, rows - 1 do local y = y0 + i local dx = floor(1.6 * sin(y * 0.8 - t * 5 + ph) + 0.5) local w = w0 + i // 4 blend_rect(x - w // 2 + dx, y, w, 1, c[1], c[2], c[3], a0 * (1 - i / (rows + 1))) end end local JACKET, SKIN, HULLT, HULLB, POLE = { 70, 112, 196 }, { 232, 192, 152 }, { 190, 140, 96 }, { 120, 80, 56 }, { 150, 152, 176 } local function dinghy(x, y) sr(x, y, 11, 1, HULLT) sr(x + 1, y + 1, 9, 1, HULLB) sr(x + 2, y + 2, 7, 1, HULLB) sr(x, y - 1, 1, 1, HULLT); sr(x + 10, y - 1, 1, 1, HULLT) sr(x + 4, y - 3, 2, 3, JACKET) sr(x + 4, y - 5, 2, 2, SKIN) sr(x + 8, y - 4, 1, 4, POLE) sr(x + 8, y - 6, 1, 2, WARM) end local function visitor_x(t) if t < 230 then local s = t / 230; return -14 + 44 * (1 - (1 - s) * (1 - s)) end if t < 400 then return 30 end if t < 520 then local s = (t - 400) / 120; return 30 + 50 * s * s end return nil end function render() if not BG then build() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / gfx.FPS local tf = frame % LOOP for i = 1, #WINS do local w = WINS[i] if (frame // (20 + w[3])) % 3 == 0 then px(w[1], w[2], 250, 196, 104) end end for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) if sin(frame * 0.06 + i * 1.7) > 0.5 then px(umod(h, W), umod(h >> 8, 20), 200, 208, 255) end end local nb = cfg.ships or 3 for i = 1, nb do local b = BOATS[i] local by = b.amp * sin(t * 6.2832 / b.per + b.ph) b.fn(by) local dx = floor(sin(t * 4 + i) + 0.5) blend_rect(b.x + 2 + dx, 44, b.w - 4, 1, b.c[1], b.c[2], b.c[3], 0.5) blend_rect(b.x + 3 - dx, 45, b.w - 6, 1, b.c[1], b.c[2], b.c[3], 0.3) end if nb >= 2 then for i = 0, 3 do local age = (frame * 0.5 + i * 20) % 80 hr(36 + age * 0.12, 33 - floor(age * 0.12), 2, 1, { 150, 160, 196 }, 0.4 * (1 - age / 80)) end end for i = 1, #LAMPS do streak(LAMPS[i], DECK + 7, 11, 2, 0.6, WARM, i * 2, t) end for i = 1, #QL, 2 do streak(QL[i], WATER0 + 2, 6, 1, 0.45, WARM, i, t) end if cfg.visit then local vx = visitor_x(tf) if vx then local bob = 0.55 * sin(t * 6.2832 / 2.8) local y = 54 + bob local speed = tf < 230 and (1 - tf / 230) * 0.38 or (tf >= 400 and (tf - 400) / 120 * 0.83 or 0) if speed > 0.05 then local wk = speed / 0.38 blend_rect(floor(vx) - 5, 57, 4, 1, 200, 224, 255, min(0.4, 0.3 * wk)) blend_rect(floor(vx) - 10, 58, 4, 1, 200, 224, 255, min(0.3, 0.2 * wk)) end streak(floor(vx) + 8, 58, 5, 1, 0.5, WARM, 1, t) dinghy(vx, y) end end end