info = { type = "image", title = "Canal rower", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A rower glides along a canal of coloured houses", api = 33, } local tr = ({ sk = { ["Light"] = "Svetlo", ["Day"] = "Deň", ["Dusk"] = "Súmrak", ["Speed"] = "Rýchlosť" }, cs = { ["Light"] = "Světlo", ["Day"] = "Den", ["Dusk"] = "Soumrak", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "light", label = T("Light"), type = "int", default = "day", opts = { keys = { "day", "dusk" }, T("Day"), T("Dusk") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect, clip = gfx.rect, gfx.px, gfx.blend_rect, gfx.clip local blit_rgb, blit_indexed, blend_mask, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.blend_mask, gfx.snapshot local floor, sin, abs, max = math.floor, math.sin, math.abs, math.max local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local DUSK = false local function K(r, g, b) if DUSK then return floor(r * 0.5 + 36), floor(g * 0.42 + 20), floor(b * 0.6 + 56) end return r, g, b end local function mixc(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 AX = 63 local RT, RB = 33, 48 local WA, WB = { 36, 104, 168 }, { 22, 78, 148 } local HOUSES = { { 0, 8, 15, { 196, 84, 62 }, 1 }, { 8, 7, 11, { 226, 170, 76 }, 2 }, { 15, 9, 14, { 74, 152, 150 }, 1 }, { 24, 6, 10, { 224, 132, 142 }, 2 }, { 30, 8, 13, { 234, 218, 170 }, 1 }, { 38, 9, 16, { 126, 170, 108 }, 1 }, { 47, 8, 12, { 104, 126, 184 }, 2 }, { 55, 9, 14, { 208, 110, 70 }, 1 }, } local BASE = 30 local function houses(mirror) local function R(x, y, w, h, r, g, b) if mirror then r, g, b = mixc({ r, g, b }, WA, 0.42) rect(x, AX + 1 - y - h, w, h, r, g, b) else rect(x, y, w, h, r, g, b) end end for i = 1, #HOUSES do local x, w, top, c, roof = HOUSES[i][1], HOUSES[i][2], HOUSES[i][3], HOUSES[i][4], HOUSES[i][5] local r, g, b = K(c[1], c[2], c[3]) local rr, rg, rb = K(c[1] * 0.78, c[2] * 0.78, c[3] * 0.8) R(x, top, w, BASE - top + 1, r, g, b) R(x + w - 1, top, 1, BASE - top + 1, rr, rg, rb) local pr, pg, pb = K(roof == 1 and 112 or 84, roof == 1 and 58 or 70, roof == 1 and 56 or 96) if roof == 1 then R(x, top - 1, w, 1, pr, pg, pb); R(x + 1, top - 2, w - 2, 1, pr, pg, pb); R(x + 2, top - 3, w - 4, 1, pr, pg, pb) else R(x, top - 1, w, 1, pr, pg, pb); R(x + 1, top - 2, 2, 1, pr, pg, pb) end local ny = (BASE - 4 - top - 2) // 5 + 1 for row = 0, ny - 1 do local wy = top + 2 + row * 5 for col = 0, (w - 2) // 3 - 1 do local wx = x + 1 + col * 3 + (w == 6 and 1 or 0) local lit = umod(hash32(i * 977 + row * 131 + col * 31), 100) < (DUSK and 70 or 22) if lit then R(wx, wy, 2, 3, 255, 214, 120) else R(wx, wy, 2, 3, K(44, 56, 96)) end R(wx, wy + 3, 2, 1, K(236, 228, 210)) end end R(x + w // 2 - 1, BASE - 3, 2, 4, K(66, 42, 42)) end end local BG, REFL, STAGE = nil, {}, 1 local function build_bg() local sa, sb if DUSK then sa, sb = { 84, 66, 150 }, { 244, 150, 126 } else sa, sb = { 120, 190, 232 }, { 206, 232, 246 } end for y = 0, 15 do rect(0, y, W, 1, mixc(sa, sb, y / 15)) end for y = RT, H - 1 do local t = (y - RT) / (RB - 1 - RT) if t > 1 then t = 1 end rect(0, y, W, 1, K(mixc(WA, WB, t))) end local wr, wg, wb = K(WB[1], WB[2], WB[3]) for i = 1, 14 do local h = hash32(i * 0x9E3779B1) blend_rect(umod(h, 58), 50 + umod(h >> 8, 13), 3 + umod(h >> 16, 5), 1, 150, 200, 240, 0.16) end houses(false) rect(0, 31, W, 1, K(158, 146, 134)); rect(0, 32, W, 1, K(98, 90, 92)) for _, bx in ipairs({ 11, 31, 51 }) do rect(bx, 29, 2, 2, K(52, 48, 54)) end BG = snapshot(0, 0, W, H) end local function build_refl() blit_rgb(BG, 0, 0, W, H) clip(0, RT, W, RB - RT) houses(true) clip() for y = RT, RB - 1 do REFL[y - RT + 1] = snapshot(0, y, W, 1) end end local CW, CH = 30, 15 local BY = 46 local IDX = { r = 1, g = 2, k = 3, h = 4, s = 5, w = 6, n = 7, o = 8, b = 9, t = 10, f = 11, d = 12 } local POSES = {} local POSE = { { tx = 15, ex = 21, ey = 13, hx = 11, hy = 7 }, { tx = 16, ex = 15, ey = 14, hx = 13, hy = 7 }, { tx = 17, ex = 8, ey = 13, hx = 16, hy = 6 }, { tx = 16, ex = 14, ey = 9, hx = 14, hy = 6 }, } local PALF = {} local MASKK = {} local function init_pal() local wr, wg, wb = K(WB[1], WB[2], WB[3]) local cols = { { 190, 50, 42 }, { 232, 200, 150 }, { 110, 30, 34 }, { 230, 190, 90 }, { 240, 190, 150 }, { 240, 240, 236 }, { 40, 60, 120 }, { 200, 160, 100 }, { 236, 210, 160 }, { 96, 84, 76 }, } PALF = { 0, 0, 0 } for i = 1, 10 do local r, g, b = K(cols[i][1], cols[i][2], cols[i][3]) PALF[#PALF + 1], PALF[#PALF + 2], PALF[#PALF + 3] = r, g, b end local fr, fg, fb = mixc({ wr, wg, wb }, { 255, 255, 255 }, 0.5) local dr, dg, db = mixc({ wr, wg, wb }, { 6, 20, 50 }, 0.55) for _, v in ipairs({ fr, fg, fb, dr, dg, db }) do PALF[#PALF + 1] = v end local cmap, mk = { ["."] = "\0" }, {} for c, i in pairs(IDX) do cmap[c] = char(i) end POSES.cmap = cmap for k = 1, 12 do local m = { ["."] = "\0" } for c, i in pairs(IDX) do m[c] = i == k and "\255" or "\0" end MASKK[k] = m end POSES.W = { wr, wg, wb } end local function build_pose(p) local d = POSE[p] local g = {} for y = 1, CH do g[y] = {}; for x = 1, CW do g[y][x] = "." end end local function put(x, y, c) if x >= 0 and x < CW and y >= 0 and y < CH then g[y + 1][x + 1] = c end end local function hl(x0, x1, y, c) for x = x0, x1 do put(x, y, c) end end local function ln(x0, y0, x1, y1, c) local n = max(abs(x1 - x0), abs(y1 - y0)) if n == 0 then put(x0, y0, c) return end for i = 0, n do put(floor(x0 + (x1 - x0) * i / n + 0.5), floor(y0 + (y1 - y0) * i / n + 0.5), c) end end local tx = d.tx hl(tx, tx + 1, 0, "h"); hl(tx - 2, tx + 2, 1, "h") hl(tx - 1, tx + 1, 2, "s"); hl(tx - 1, tx + 1, 3, "s") for y = 4, 8 do hl(tx, tx + 2, y, y % 2 == 0 and "w" or "n") end hl(tx - 3, tx + 1, 8, "t"); put(tx - 3, 7, "t") hl(25, 26, 8, "g"); hl(3, 26, 9, "g"); hl(4, 26, 10, "r"); hl(5, 25, 11, "r"); hl(6, 24, 12, "k") hl(6, 23, 13, "f"); hl(8, 21, 14, "d") ln(d.hx, d.hy, d.ex, d.ey, "o") local bx, by = 14 + (d.ex - 14) * 0.6, 9 + (d.ey - 9) * 0.6 ln(floor(bx + 0.5), floor(by + 0.5), d.ex, d.ey, "b") put(d.ex, d.ey - 1, "b") ln(tx, 5, d.hx, d.hy, "s") put(d.hx, d.hy, "s") local rows = {} for y = 1, CH do rows[y] = concat(g[y]) end local s = concat(rows) local pz = { cells = s:gsub(".", POSES.cmap), mask = {} } for k = 1, 12 do if s:find(({ "r", "g", "k", "h", "s", "w", "n", "o", "b", "t", "f", "d" })[k], 1, true) then pz.mask[k] = s:gsub(".", MASKK[k]) end end local tm = {} for y = 0, CH - 1 do for x = 0, CW - 1 do local c = s:sub(y * CW + x + 1, y * CW + x + 1) local l = x > 0 and s:sub(y * CW + x, y * CW + x) or "." tm[#tm + 1] = (c ~= "." and l == ".") and "\255" or "\0" end end pz.trail = concat(tm) POSES[p] = pz end local PERIOD, TRAVEL = 270, 230 function on_settings() DUSK = cfg.light == "dusk" local sp = cfg.speed or 3 PERIOD = 480 - 70 * sp TRAVEL = PERIOD - 40 BG, STAGE = nil, 1 POSES = {} end local function span(x0, x1, y, r, g, b, a) local i0, i1 = floor(x0), floor(x1) if i0 == i1 then blend_rect(i0, y, 1, 1, r, g, b, a * (x1 - x0)) return end blend_rect(i0, y, 1, 1, r, g, b, a * (1 - (x0 - i0))) if i1 > i0 + 1 then blend_rect(i0 + 1, y, i1 - i0 - 1, 1, r, g, b, a) end if x1 > i1 then blend_rect(i1, y, 1, 1, r, g, b, a * (x1 - i1)) end end local function boat_x(fr) local c = fr % PERIOD if c >= TRAVEL then return nil end return -32 + 100 * c / TRAVEL end local GLINT = {} for i = 1, 12 do local h = hash32(i * 0x85EBCA6B) GLINT[i] = { 1 + umod(h, 60), 34 + umod(h >> 8, 29), umod(h >> 16, 628) / 100 } end function render() if STAGE == 1 then build_bg(); init_pal() elseif STAGE == 2 then build_refl() elseif STAGE <= 6 then build_pose(STAGE - 2) end STAGE = STAGE + 1 local t = frame / FPS if STAGE <= 3 then return end blit_rgb(BG, 0, 0, W, H) local cr, cg, cb = 255, 255, 255 if DUSK then cr, cg, cb = 255, 200, 190 end for i = 1, 2 do local cx = (i * 29 + t * (1.1 + i * 0.4)) % (W + 24) - 12 local cy = 1 + i * 3 span(cx + 2, cx + 9, cy, cr, cg, cb, 0.55); span(cx, cx + 11, cy + 1, cr, cg, cb, 0.45) end for r = 0, RB - RT - 1 do local dx = floor(1.1 * sin(r * 0.8 + t * 1.7) * (0.4 + r / 16) + 0.5) blit_rgb(REFL[r + 1], dx, RT + r, W, 1) end for i = 1, #GLINT do local q = GLINT[i] local v = sin(t * 1.6 + q[3]) if v > 0.2 then blend_rect(q[1], q[2], 2, 1, 255, 255, 255, (v - 0.2) * 0.45) end end if #POSES < 4 then return end local fr = frame local bx = boat_x(fr) local wr, wg, wb = mixc(POSES.W, { 255, 255, 255 }, 0.5) for j = 1, 7 do local age = j * 0.2 local bxj = boat_x(fr - floor(age * FPS + 0.5)) if bxj and bxj > -30 then local sx = bxj + 4 local a = 0.55 * (1 - j / 8) local len = 3 + j // 2 span(sx - len, sx, 60 + floor(age * 2.0), wr, wg, wb, a) span(sx - len, sx, 58 - floor(age * 3.0), wr, wg, wb, a * 0.8) end end if bx then local pz = POSES[(frame // 7) % 4 + 1] local ix = floor(bx) local f = bx - ix blit_indexed(pz.cells, PALF, ix, BY, CW, CH, CW, CH, true) if f > 0.03 then for k = 1, 12 do local m = pz.mask[k] if m then blend_mask(m, PALF[k * 3 + 1], PALF[k * 3 + 2], PALF[k * 3 + 3], ix + 1, BY, CW, CH, CW, CH, f) end end local w = POSES.W blend_mask(pz.trail, w[1], w[2], w[3], ix, BY, CW, CH, CW, CH, f) end end end