info = { type = "image", title = "Woodblock sail", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A sailboat crosses an indigo woodblock sea", api = 44, } local tr = ({ sk = { ["Sun"] = "Slnko", ["Speed"] = "Rýchlosť", ["Terracotta"] = "Terakota", ["Gold"] = "Zlaté", ["Indigo"] = "Indigo" }, cs = { ["Sun"] = "Slunce", ["Speed"] = "Rychlost", ["Terracotta"] = "Terakota", ["Gold"] = "Zlaté", ["Indigo"] = "Indigo" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sun", label = T("Sun"), type = "int", default = 0, opts = { T("Terracotta"), T("Gold"), T("Indigo") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect, blend_rect, blit, snapshot, blit_rgb = gfx.rect, gfx.blend_rect, gfx.blit_indexed, gfx.snapshot, gfx.blit_rgb local floor, sin, sqrt = math.floor, math.sin, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local unpack, schar, byte = table.unpack, string.char, string.byte local T0 = 13 local PAPER, GRAIN = { 238, 224, 186 }, { 226, 208, 166 } local INK_TOP = { 50, 68, 120 } local SEA = { { 98, 134, 174 }, { 72, 108, 154 }, { 58, 92, 140 } } local ROCK, ROCK_L = { 34, 44, 86 }, { 62, 78, 124 } local CREAM, TERRA = { 242, 230, 198 }, { 200, 86, 52 } local SUNS = { { 204, 88, 54 }, { 234, 174, 52 }, { 40, 52, 100 } } local HORIZON = 30 local function variant(sp, fx) local w, h, pal, bg = sp.w, sp.h, sp.pal, sp.bg local list, map = sp.list, sp.map local rows, row = {}, {} local ow = sp.wrap and w or w + 1 for j = 0, h - 1 do for i = 0, ow - 1 do local a, r, g, b = 0, 0, 0, 0 for dx = 0, 1 do local sx, wt = i - dx, dx == 0 and 4 - fx or fx if sp.wrap then sx = sx % w end local c = wt > 0 and sx >= 0 and sx < w and pal[byte(sp.s, j * w + sx + 1)] if c then a = a + wt * 4; r = r + wt * 4 * c[1]; g = g + wt * 4 * c[2]; b = b + wt * 4 * c[3] end end local v = 0 if a >= 3 then local k = 16 - a local R, G, B = floor((r + k * bg[1]) / 128 + 0.5) * 8, floor((g + k * bg[2]) / 128 + 0.5) * 8, floor((b + k * bg[3]) / 128 + 0.5) * 8 local key = R * 65536 + G * 256 + B v = map[key] if not v then v = #list // 3 if v < 255 then map[key] = v; list[v * 3 + 1], list[v * 3 + 2], list[v * 3 + 3] = R, G, B end end end row[i + 1] = v end rows[j + 1] = schar(unpack(row, 1, ow)) end return table.concat(rows), ow end local function mk(w, h, s, pal, bg, wrap) return { w = w, h = h, s = s, pal = pal, bg = bg, wrap = wrap, v = {}, list = { 0, 0, 0 }, map = {} } end local budget = 1 local function draw(sp, x, y, span) local ix = floor(x) local fx = floor((x - ix) * 4) local v = sp.v[fx] if not v then if budget > 0 then budget = budget - 1; v = variant(sp, fx); sp.v[fx] = v else local _; _, v = next(sp.v); if not v then return end end end local ow = sp.wrap and sp.w or sp.w + 1 if span then for k = 0, span // sp.w do blit(v, sp.list, ix + k * sp.w, y, ow, sp.h, ow, sp.h, false) end else blit(v, sp.list, ix, y, ow, sp.h, ow, sp.h, true) end end local BOAT local function build_boat() local w, h = 26, 18 local c = {} for i = 1, w * h do c[i] = "." end local function fill(x, y, ww, hh, ch) for yy = y, y + hh - 1 do for xx = x, x + ww - 1 do c[yy * w + xx + 1] = ch end end end fill(11, 0, 1, 14, "m") fill(3, 1, 17, 1, "m") fill(5, 2, 13, 11, "s") fill(15, 2, 3, 11, "d") fill(8, 2, 1, 11, "t"); fill(14, 2, 1, 11, "t") fill(1, 13, 23, 1, "h"); fill(2, 14, 21, 1, "t"); fill(3, 15, 18, 1, "h"); fill(5, 16, 13, 1, "h") fill(23, 11, 2, 2, "h") local pal = { [byte("m")] = { 58, 40, 36 }, [byte("s")] = CREAM, [byte("d")] = { 214, 196, 156 }, [byte("t")] = TERRA, [byte("h")] = ROCK, } BOAT = mk(w, h, table.concat(c), pal, SEA[3]) end local BANDS = {} local function build_bands() local defs = { { 20, 1, 3.4, SEA[3], { 46, 76, 128 }, { 58, 92, 144 }, { 240, 228, 196 } }, { 16, -1, 4.8, { 46, 76, 128 }, { 36, 60, 112 }, { 48, 78, 132 }, { 228, 214, 180 } }, { 24, 1, 6.6, { 36, 60, 112 }, { 28, 46, 92 }, { 40, 64, 118 }, { 214, 200, 166 } }, } for n = 1, 3 do local d = defs[n] local P = d[1] local top, hi = {}, {} for x = 0, P - 1 do local ph = x / P local f = ph < 0.72 and (ph / 0.72) ^ 1.4 or (1 - ph) / 0.28 top[x] = 3 - floor(3.2 * f + 0.5) hi[x] = f > 0.6 end local rows = {} for y = 0, 5 do local r = {} for x = 0, P - 1 do local yp, ch = top[x], "a" local nxt = top[(x + 1) % P] if y == yp or (y == yp + 1 and hi[x]) then ch = "f" elseif y > yp and y <= nxt - 0 and nxt - yp >= 2 and y < nxt then ch = "f" elseif y > yp then ch = (y == yp + 2 and (x * 7) % 5 < 3) and "l" or "b" end r[x + 1] = ch end rows[y + 1] = table.concat(r) end local pal = { [byte("a")] = d[4], [byte("b")] = d[5], [byte("l")] = d[6], [byte("f")] = d[7] } BANDS[n] = { sp = mk(P, 6, table.concat(rows), pal, d[5], true), P = P, dir = d[2], v = d[3] } end end local BG, BG_SUN = nil, nil local function build_bg() for y = 0, 9 do local k = y / 10 rect(0, y, W, 1, floor(INK_TOP[1] + (PAPER[1] - INK_TOP[1]) * k + 0.5), floor(INK_TOP[2] + (PAPER[2] - INK_TOP[2]) * k + 0.5), floor(INK_TOP[3] + (PAPER[3] - INK_TOP[3]) * k + 0.5)) end rect(0, 10, W, HORIZON - 10, PAPER[1], PAPER[2], PAPER[3]) for i = 1, 26 do local h = math.hash32(i * 0x9E3779B1) local x, y, l = (h & 0x7FFF) % W, 10 + ((h >> 8) & 0x7FFF) % (HORIZON - 10), 5 + ((h >> 20) & 15) rect(x, y, l, 1, GRAIN[1], GRAIN[2], GRAIN[3]) end local sc = SUNS[(cfg.sun or 0) + 1] local cx, cy, r = 44, 21, 8 for y = -r, r do local hw = floor(sqrt(r * r + 0.5 - y * y) + 0.5) rect(cx - hw, cy + y, 2 * hw, 1, sc[1], sc[2], sc[3]) end for x = 0, 25 do local hgt = floor(12 - x * 0.34 + 1.6 * sin(x * 0.9) + 1.2 * sin(x * 0.37 + 1) + 0.5) if hgt > 1 then rect(x, HORIZON - hgt, 1, hgt, ROCK[1], ROCK[2], ROCK[3]) if x % 3 ~= 0 then for ly = HORIZON - hgt + 2, HORIZON - 1, 3 do rect(x, ly, 1, 1, ROCK_L[1], ROCK_L[2], ROCK_L[3]) end end end end for x = 55, 63 do local d = x - 59 local hgt = 5 - (d < 0 and -d or d) if hgt > 0 then rect(x, HORIZON - hgt, 1, hgt, ROCK[1], ROCK[2], ROCK[3]) end end rect(0, HORIZON, W, 4, SEA[1][1], SEA[1][2], SEA[1][3]) rect(0, HORIZON + 4, W, 6, SEA[2][1], SEA[2][2], SEA[2][3]) rect(0, HORIZON + 10, W, 16, SEA[3][1], SEA[3][2], SEA[3][3]) for i = 1, 40 do local h = math.hash32(i * 0x85EBCA6B) local x, y, l = (h & 0x7FFF) % W, HORIZON + ((h >> 8) & 0x7FFF) % 16, 4 + ((h >> 20) & 7) local b = y < HORIZON + 4 and SEA[1] or y < HORIZON + 10 and SEA[2] or SEA[3] rect(x, y, l, 1, b[1] + 14, b[2] + 16, b[3] + 18) end BG = snapshot(0, 0, W, H) end local function mist(t) local bars = { { 7, 24, 0.9, 6 }, { 15, 18, 0.7, 40 }, { 25, 28, 0.6, 20 } } for i = 1, #bars do local b = bars[i] local cx = (b[4] - t * b[3]) % (W + b[2]) - b[2] local ix = floor(cx) local f = cx - ix for r = 0, 1 do rect(ix + 1, b[1] + r, b[2] - 1, 1, 246, 238, 208) blend_rect(ix, b[1] + r, 1, 1, 246, 238, 208, 1 - f) blend_rect(ix + b[2], b[1] + r, 1, 1, 246, 238, 208, f) end end end local function glints(t) local sc = SUNS[(cfg.sun or 0) + 1] for i = 0, 4 do local l = 10 - i * 2 local cx = 44 + 2.2 * sin(t * 0.9 + i * 1.3) - l / 2 local ix = floor(cx) local f = cx - ix local y = HORIZON + 2 + i * 3 rect(ix + 1, y, l - 1, 1, sc[1], sc[2], sc[3]) blend_rect(ix, y, 1, 1, sc[1], sc[2], sc[3], 1 - f) blend_rect(ix + l, y, 1, 1, sc[1], sc[2], sc[3], f) end end function on_settings() BG = nil end function render() if not BOAT then build_boat(); build_bands(); gfx.clear(PAPER[1], PAPER[2], PAPER[3]); gfx.ready(false) return end if not BG then build_bg() end budget = 1 blit_rgb(BG, 0, 0, W, H) local t = T0 + frame / FPS mist(t) glints(t) local v = ({ 1.6, 2.4, 3.4 })[cfg.speed or 2] or 2.4 local x = (t * v) % 98 - 30 draw(BOAT, x, 29 + floor(0.5 + 0.7 * sin(t * TAU / 4.5))) for n = 1, 3 do local b = BANDS[n] local s = (b.dir * t * b.v) % b.P draw(b.sp, s - b.P, 46 + (n - 1) * 6, W + b.P) if not next(b.sp.v) then gfx.ready(false) end end end