info = { type = "image", title = "Treasure map", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Dashed route over an old map to a chest", api = 44, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Slow"] = "Pomaly", ["Normal"] = "Normálne", ["Fast"] = "Rýchlo", ["Paper"] = "Pergamen", ["Light"] = "Svetlý", ["Dark"] = "Tmavý" }, cs = { ["Speed"] = "Rychlost", ["Slow"] = "Pomalu", ["Normal"] = "Normálně", ["Fast"] = "Rychle", ["Paper"] = "Pergamen", ["Light"] = "Světlý", ["Dark"] = "Tmavý" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, { key = "paper", label = T("Paper"), type = "int", default = "light", opts = { keys = { "light", "dark" }, T("Light"), T("Dark") } }, } local rect, px, blend_rect, blit_rgb, snapshot = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32 = math.hash32 local W, H = gfx.W, gfx.H local WW = 192 local PAPERS = { light = { ground = { 230, 212, 170 }, speck = { 212, 190, 144 }, edge = { 170, 134, 92 }, ink = { 78, 50, 30 }, sea = { 176, 204, 208 }, wave = { 120, 156, 170 }, land = { 184, 196, 138 }, hill = { 150, 168, 108 }, palm = { 92, 124, 72 }, x = { 168, 44, 28 }, cream = { 248, 240, 216 } }, dark = { ground = { 188, 152, 102 }, speck = { 166, 130, 82 }, edge = { 118, 84, 50 }, ink = { 40, 24, 12 }, sea = { 134, 160, 150 }, wave = { 88, 118, 116 }, land = { 148, 152, 96 }, hill = { 114, 130, 76 }, palm = { 66, 92, 50 }, x = { 150, 36, 22 }, cream = { 238, 228, 198 } }, } local P = PAPERS.light local function C(c) return c[1], c[2], c[3] end local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise(x, seed) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, seed) * (1 - f) + vn(i + 1, seed) * f end local ISL = { { cx = 122, cy = 32, rx = 28, ry = 18, s = 11 }, { cx = 6, cy = 36, rx = 25, ry = 26, s = 23 }, { cx = 188, cy = 40, rx = 21, ry = 18, s = 37 }, } local XA, YA = 134, 27 local XB, YB = 180, 40 local function scan(isl, ya, yb) local runs = isl.runs or {} isl.runs = runs local xa, xb = max(4, isl.cx - isl.rx - 5), min(WW - 5, isl.cx + isl.rx + 5) if not isl.nx then isl.nx = {} for x = xa, xb do isl.nx[x] = noise(x / 4.5, isl.s) - 1 end end local nx = isl.nx for y = ya, yb do local r, ny = {}, noise(y / 4.5, isl.s + 1) local a local dy2 = ((y - isl.cy) / isl.ry) ^ 2 for x = xa, xb do local dx = (x - isl.cx) / isl.rx local inside = dx * dx + dy2 < 1 + 0.5 * (nx[x] + ny) if inside and not a then a = x elseif not inside and a then r[#r + 1] = a; r[#r + 1] = x - 1; a = nil end end if a then r[#r + 1] = a; r[#r + 1] = xb end runs[y] = r end end local function is_land(x, y) for k = 1, #ISL do local r = ISL[k].runs and ISL[k].runs[y] if r then for i = 1, #r, 2 do if x >= r[i] and x <= r[i + 1] then return true end end end end return false end local function route(wp, landw) local X, Y, n, li = {}, {}, 0, 0 for i = 1, #wp - 2, 2 do if (i + 1) // 2 == landw then li = n + 1 end local x0, y0, x1, y1 = wp[i], wp[i + 1], wp[i + 2], wp[i + 3] local len = floor(sqrt((x1 - x0) ^ 2 + (y1 - y0) ^ 2)) for k = 0, len - 1 do n = n + 1 X[n] = floor(x0 + (x1 - x0) * k / len + 0.5) Y[n] = floor(y0 + (y1 - y0) * k / len + sin(n * 0.37) * 1.2 + 0.5) end end n = n + 1 X[n], Y[n] = wp[#wp - 1], wp[#wp] local D = {} for i = 1, n do if i < li then if i % 5 == 0 then local dx, dy = (X[min(n, i + 2)] - X[max(1, i - 2)]), (Y[min(n, i + 2)] - Y[max(1, i - 2)]) if math.abs(dx) >= math.abs(dy) then D[#D + 1] = { X[i], Y[i], 3, 1, i } else D[#D + 1] = { X[i], Y[i], 1, 3, i } end end elseif i % 6 == 0 then D[#D + 1] = { X[i] + ((i // 6) % 2) * 2, Y[i], 1, 2, i } end end return { X = X, Y = Y, n = n, li = li, D = D } end local RA = route({ 32, 44, 48, 47, 62, 41, 78, 45, 94, 38, 106, 30, 118, 33, 126, 27, XA, YA }, 5) local RB = route({ 32, 44, 42, 52, 64, 55, 90, 53, 112, 57, 140, 57, 154, 55, 166, 46, 174, 41, XB, YB }, 9) RA.xx, RA.xy, RB.xx, RB.xy = XA, YA, XB, YB local function trace_x(x, y, ox) local c = P.x for k = -2, 2 do px(x + k - ox, y + k, C(c)); px(x + k - ox, y - k, C(c)) end px(x - 1 - ox, y, C(c)); px(x + 1 - ox, y, C(c)) end local function ground(ox) rect(0, 0, W, H, C(P.ground)) for i = 1, 70 do local h = hash32(i * 0x85EBCA6B) local x, y = (h & 0x7FFF) % WW - ox, ((h >> 15) & 0x7FF) % 64 if x >= 0 and x < W then rect(x, y, 1 + (h >> 26) % 2, 1, C(P.speck)) end end rect(3 - ox, 4, 186, 56, C(P.sea)) end local function waves(ox) for i = 1, 150 do local h = hash32(i * 0xC2B2AE35) local wx, y = 6 + (h & 0x7FFF) % 180, 6 + ((h >> 15) & 0x7FF) % 52 local x = wx - ox if x >= -5 and x < W and not is_land(wx, y) and not is_land(wx + 4, y) then rect(x, y, 2, 1, C(P.wave)); rect(x + 3, y - 1, 2, 1, C(P.wave)) end end end local function land(ox) for k = 1, #ISL do local isl = ISL[k] for y, r in pairs(isl.runs) do for i = 1, #r, 2 do rect(r[i] - 1 - ox, y - 1, r[i + 1] - r[i] + 3, 3, C(P.ink)) end end for y, r in pairs(isl.runs) do for i = 1, #r, 2 do rect(r[i] - ox, y, r[i + 1] - r[i] + 1, 1, C(P.land)) end end end end local function hills(ox) for k = 1, 3 do local isl = ISL[k] local placed = 0 for i = 1, 60 do if placed >= (k == 1 and 14 or 8) then break end local h = hash32(i * 0x27D4EB2F + k * 977) local x = isl.cx - isl.rx + (h & 0x7FFF) % (2 * isl.rx) local y = isl.cy - isl.ry + ((h >> 15) & 0x7FF) % (2 * isl.ry) local e = ((x - isl.cx) / isl.rx) ^ 2 + ((y - isl.cy) / isl.ry) ^ 2 local nearx = math.abs(x - XA) < 9 and math.abs(y - YA) < 8 or math.abs(x - XB) < 9 and math.abs(y - YB) < 8 if e < 0.5 and not nearx and is_land(x - 3, y) and is_land(x + 3, y) and is_land(x, y - 5) then placed = placed + 1 local sx = x - ox if placed % 3 == 0 then rect(sx, y - 1, 1, 3, C(P.ink)) rect(sx - 2, y - 3, 5, 1, C(P.palm)); px(sx - 1, y - 4, C(P.palm)); px(sx + 1, y - 4, C(P.palm)) px(sx - 2, y - 2, C(P.palm)); px(sx + 2, y - 2, C(P.palm)) else rect(sx - 2, y + 1, 5, 1, C(P.hill)); rect(sx - 1, y, 3, 1, C(P.hill)); px(sx, y - 1, C(P.hill)) px(sx, y - 2, C(P.ink)); px(sx - 1, y - 1, C(P.ink)); px(sx + 1, y - 1, C(P.ink)) px(sx - 2, y, C(P.ink)); px(sx + 2, y, C(P.ink)) end end end end end local function port(ox) local houses = { { 12, 33 }, { 18, 38 }, { 11, 41 } } for _, h in ipairs(houses) do rect(h[1] - ox, h[2], 5, 3, C(P.cream)); rect(h[1] - 1 - ox, h[2] - 1, 7, 1, 150, 62, 40) px(h[1] + 2 - ox, h[2] + 2, C(P.ink)) end rect(27 - ox, 44, 6, 1, 112, 78, 46) px(30 - ox, 45, 112, 78, 46); px(32 - ox, 45, 112, 78, 46) trace_x(XA, YA, ox); trace_x(XB, YB, ox) local cx, cy = 84 - ox, 24 rect(cx - 7, cy, 15, 1, C(P.ink)); rect(cx, cy - 7, 1, 15, C(P.ink)) for k = 1, 3 do rect(cx - k, cy - 7 + k, 2 * k + 1, 1, C(P.cream)); rect(cx - k, cy + 7 - k, 2 * k + 1, 1, C(P.cream)) rect(cx - 7 + k, cy - k, 1, 2 * k + 1, C(P.cream)); rect(cx + 7 - k, cy - k, 1, 2 * k + 1, C(P.cream)) end rect(cx - 7, cy, 15, 1, C(P.ink)); rect(cx, cy - 7, 1, 15, C(P.ink)) for k = 2, 4 do px(cx - k, cy - k, C(P.ink)); px(cx + k, cy - k, C(P.ink)); px(cx - k, cy + k, C(P.ink)); px(cx + k, cy + k, C(P.ink)) end px(cx, cy, C(P.x)) gfx.text(cx - 2, 7, "N", P.x[1], P.x[2], P.x[3], { font = "5x7" }) end local function frame_edge(ox) rect(0 - ox, 0, 3, 64, C(P.edge)); rect(189 - ox, 0, 3, 64, C(P.edge)) rect(0, 0, W, 3, C(P.edge)); rect(0, 61, W, 3, C(P.edge)) rect(3 - ox, 3, 186, 1, C(P.ink)); rect(3 - ox, 60, 186, 1, C(P.ink)) rect(3 - ox, 3, 1, 58, C(P.ink)); rect(188 - ox, 3, 1, 58, C(P.ink)) for i = 1, 40 do local h = hash32(i * 0x9E3779B1 + 5) local x, y = (h & 0x7FFF) % WW - ox, ((h >> 15) & 0x7FF) % 64 if x >= 0 and x < W then px(x, (h >> 28) % 2 == 0 and y % 3 or 63 - y % 3, C(P.ground)) end end end local TILE, PART = {}, nil local JOBS, NJ = {}, 0 for k = 1, #ISL do JOBS[#JOBS + 1] = function() scan(ISL[k], 4, 31) end JOBS[#JOBS + 1] = function() scan(ISL[k], 32, 59) end end for t = 0, 2 do local ox = t * 64 local stages = { function() ground(ox) end, function() waves(ox) end, function() land(ox) end, function() hills(ox) end, function() port(ox); frame_edge(ox) end } for s = 1, 5 do JOBS[#JOBS + 1] = function() if s > 1 then blit_rgb(PART, 0, 0, W, H) end stages[s]() PART = snapshot(0, 0, W, H) if s == 5 then TILE[t] = PART end end end end local SPEEDS = { slow = 0.6, normal = 1, fast = 1.5 } local SPD = 1 local DRAW, HOLD, PAN = 330, 70, 100 local CYC = DRAW + HOLD + PAN local FADE = 40 function on_settings() P = PAPERS[cfg.paper] or PAPERS.light SPD = SPEEDS[cfg.speed] or 1 TILE, NJ = {}, 0 end local function smooth(x) x = max(0, min(1, x)); return x * x * (3 - 2 * x) end local function ship(x, y, rock) rect(x - 3, y - 2, 7, 1, C(P.ink)); rect(x - 2, y - 1, 5, 1, C(P.ink)) rect(x, y - 8, 1, 6, C(P.ink)) rect(x + 1, y - 7 + rock, 1, 1, C(P.cream)); rect(x + 1, y - 6, 2, 1, C(P.cream)) rect(x + 1, y - 5, 3, 1, C(P.cream)); rect(x + 1, y - 4, 4, 1, C(P.cream)) rect(x - 2, y - 4, 2, 1, C(P.cream)); px(x - 1, y - 5, C(P.cream)) end local TAILA = { "......#..", ".....###.", "....##.#.", "....##...", "...###...", "...##....", "..###....", "..##.....", "..##.....", ".###.....", ".###.....", ".##......", } local function cells(rows, flip) local out = {} for i, r in ipairs(rows) do if flip then r = r:reverse() end out[i] = r:gsub("#", "\1"):gsub("%.", "\0") end return table.concat(out) end local TAIL = { cells(TAILA, false), cells(TAILA, true) } local PAL_TAIL = { 0, 0, 0, 52, 84, 84 } local function monster(u, sx, wl, pose) if u <= 0 or u >= 1 then return end local rise = floor(12 * sin(u * 3.14159) ^ 0.6 + 0.5) gfx.clip(0, 0, W, wl) gfx.blit_indexed(TAIL[pose], PAL_TAIL, sx, wl - rise, 9, 12, 9, 12, true) gfx.clip() local w = floor(3 + u * 7) local r, g, b = C(P.ink) blend_rect(sx + 4 - w, wl, 2 * w, 1, r, g, b, 0.5 * (1 - u)) blend_rect(sx + 1 - w, wl + 2, 2 * w + 6, 1, r, g, b, 0.3 * (1 - u)) end local function chest(sx, sy, a, t) blend_rect(sx - 2, sy - 3, 5, 1, 126, 76, 36, a) blend_rect(sx - 2, sy - 2, 5, 3, 98, 58, 28, a) blend_rect(sx, sy - 2, 1, 3, 236, 190, 60, a) local g = sin(t * 0.25) if g > 0 then blend_rect(sx + 2, sy - 6, 1, 3, 255, 252, 230, a * g); blend_rect(sx + 1, sy - 5, 3, 1, 255, 252, 230, a * g) end end local function draw_route(R, u, cam, ft) local n = min(R.n, floor(R.n * u / DRAW) + 1) local a = 1 if u > DRAW + HOLD then a = 1 - smooth((u - DRAW - HOLD) / FADE) end if a <= 0 then return end local ir, ig, ib = C(P.ink) local D = R.D for i = 1, #D do local d = D[i] if d[5] > n then break end local x = d[1] - cam if x > -4 and x < W then if a >= 1 then rect(x, d[2], d[3], d[4], ir, ig, ib) else blend_rect(x, d[2], d[3], d[4], ir, ig, ib, a) end end end if a >= 1 then local li = min(n, max(R.li - 1, 1)) local sx = R.X[li] - cam if n < R.li then ship(sx, R.Y[n] + 2 + floor(sin(ft * 0.09) + 0.5), (ft // 20) % 2) else ship(sx, R.Y[li] + 3, (ft // 20) % 2) if n < R.n then local ex, ey = R.X[n] - cam, R.Y[n] px(ex, ey - 2, 236, 184, 140); rect(ex, ey - 1, 1, 2, 150, 40, 30) end end if u >= DRAW then chest(R.xx - cam, R.xy, 1, ft) end else chest(R.xx - cam, R.xy, a, ft) end end function render() NJ = NJ + 1 if JOBS[NJ] then JOBS[NJ]() end gfx.ready(JOBS[NJ + 1] == nil) if not TILE[2] then return end local ft = floor(frame * SPD) local ci, u = ft // CYC, ft % CYC local R = ci % 2 == 0 and RA or RB local n = min(R.n, floor(R.n * u / DRAW) + 1) local top = max(0, min(WW - W, R.X[n] - 32)) local cam if u < DRAW + HOLD then cam = top else local fin = max(0, min(WW - W, R.X[R.n] - 32)) cam = floor(fin * (1 - smooth((u - DRAW - HOLD) / PAN)) + 0.5) end if u < DRAW + HOLD then cam = floor(top) end local k = cam // 64 blit_rgb(TILE[k], k * 64 - cam, 0, W, H) if cam % 64 > 0 and TILE[k + 1] then blit_rgb(TILE[k + 1], (k + 1) * 64 - cam, 0, W, H) end local nx = 84 - cam local sw = floor(sin(ft * 0.05) * 1.4 + 0.5) px(nx + sw, 17, C(P.x)); px(nx + floor(sw / 2), 19, C(P.x)) draw_route(R, u, cam, ft) if R == RB then local tu = (ft % CYC - (DRAW * 0.68)) / 90 monster(tu, 100 - cam, 58, (ft // 12) % 2 + 1) end end