info = { type = "image", title = "Maze run", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "A maze digs itself and a dot runs through", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Wall colour"] = "Farba steny", ["Trail colour"] = "Farba stopy", ["Speed"] = "Rýchlosť" }, cs = { ["Wall colour"] = "Barva stěny", ["Trail colour"] = "Barva stopy", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "wall", label = T("Wall colour"), type = "color", default = "#1CC47C" }, { key = "trail", label = T("Trail colour"), type = "color", default = "#FF6E1E" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 4, default = 2 }, } local rect, blit = gfx.rect, gfx.blit_indexed local floor, abs = math.floor, math.abs local hash32 = math.hash32 local W, H = gfx.W, gfx.H local NU, NC = 31, 15 local NSTEP = NC * NC - 1 local RUNPAD, HOLD = 10, 50 local COR_C, HEAD_C = { 32, 24, 10 }, { 220, 255, 190 } local PAL = nil local WALL = { 28, 196, 124 } local TRAIL = { 255, 110, 30 } local RATE, RUNF, GEN, CYC = 3, 150, 80, 280 function on_settings() local w, t = cfg.wall or { r = 28, g = 196, b = 124 }, cfg.trail or { r = 255, g = 110, b = 30 } WALL, TRAIL = { w.r, w.g, w.b }, { t.r, t.g, t.b } PAL = { 0, 0, 0, COR_C[1], COR_C[2], COR_C[3], HEAD_C[1], HEAD_C[2], HEAD_C[3], t.r, t.g, t.b } local sp = cfg.speed or 2 RATE = 1 + sp RUNF = 450 // (sp + 1) GEN = (NSTEP + RATE - 1) // RATE + 4 CYC = GEN + RUNPAD + RUNF + HOLD end local function h(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local G = {} for i = 1, NU * NU do G[i] = 0 end local cyc, steps, last_f = -1, 0, -1 local visited, par, stack, sp_, head_u local path, plen, marked = nil, 0, 0 local function uidx(cx, cy) return (2 * cy + 1) * NU + (2 * cx + 1) + 1 end local function reset(n) for i = 1, NU * NU do G[i] = 0 end visited, par, stack = {}, {}, { 0 } sp_, steps, head_u, path, plen, marked = 1, 0, nil, nil, 0, 0 visited[0] = true G[uidx(0, 0)] = 1 cyc = n end local nb = {} local function carve() while sp_ > 0 do local c = stack[sp_] local ci, cj = c % NC, c // NC local n = 0 if ci > 0 and not visited[c - 1] then n = n + 1; nb[n] = c - 1 end if ci < NC - 1 and not visited[c + 1] then n = n + 1; nb[n] = c + 1 end if cj > 0 and not visited[c - NC] then n = n + 1; nb[n] = c - NC end if cj < NC - 1 and not visited[c + NC] then n = n + 1; nb[n] = c + NC end if n > 0 then local d = nb[h(cyc, steps) % n + 1] steps = steps + 1 local di, dj = d % NC, d // NC visited[d] = true par[d] = c G[(cj + dj + 1) * NU + (ci + di + 1) + 1] = 1 G[uidx(di, dj)] = 1 sp_ = sp_ + 1 stack[sp_] = d return d end sp_ = sp_ - 1 end end local function make_path() local cells, c = {}, NC * NC - 1 while c do cells[#cells + 1] = c; c = par[c] end path = {} local n = 0 for k = #cells, 1, -1 do local d = cells[k] local di, dj = d % NC, d // NC if k < #cells then local p = cells[k + 1] local pi, pj = p % NC, p // NC n = n + 1; path[n] = { pi + di + 1, pj + dj + 1 } end n = n + 1; path[n] = { 2 * di + 1, 2 * dj + 1 } end plen = n end function render() FR = frame * STEP_HZ // gfx.FPS if not PAL then on_settings() end local f = FR % CYC local n = FR // CYC if n ~= cyc or f < last_f then reset(n) end last_f = f if head_u then G[head_u] = 1 end local want = f < GEN and (f + 1) * RATE or NSTEP if want > NSTEP then want = NSTEP end local d while steps < want do local r = carve() if not r then break end d = r end if f < GEN then if d then head_u = uidx(d % NC, d // NC); G[head_u] = 2 end elseif not path then head_u = nil make_path() end local rx, ry local pos = 0 if path then local r = (f - GEN - RUNPAD) / RUNF if r < 0 then r = 0 elseif r > 1 then r = 1 end pos = r * (plen - 1) local k = floor(pos) for i = marked + 1, k + 1 do local p = path[i] G[p[2] * NU + p[1] + 1] = 3 end if k + 1 > marked then marked = k + 1 end local a, b = path[k + 1], path[k + 2] or path[k + 1] local fr = pos - k rx = 1 + 2 * (a[1] + (b[1] - a[1]) * fr) ry = 1 + 2 * (a[2] + (b[2] - a[2]) * fr) end gfx.clear(WALL[1], WALL[2], WALL[3]) blit(G, PAL, 1, 1, 2 * NU, 2 * NU, NU, NU, true) if path then local gx = 1 + 2 * (2 * NC - 1) if f < GEN + RUNPAD + RUNF then if (f // 5) % 2 == 0 then rect(gx, gx, 2, 2, 255, 90, 170) else rect(gx, gx, 2, 2, 255, 190, 220) end else rect(gx, gx, 2, 2, 255, 240, 180) end if f < GEN + RUNPAD + RUNF then gfx.blend_rect(floor(rx + 0.5) - 1, floor(ry + 0.5) - 1, 4, 4, 255, 240, 160, 0.35) rect(floor(rx + 0.5), floor(ry + 0.5), 2, 2, 255, 248, 190) end end end