/
hamster75
/
Life
Обзор
Документация
Войти
/
hamster75
/
Life
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
6
CI/CD
Аналитика
Безопасность
master
js/mc-parser.js
73 строки
3 KB
Dmitry Smirnov
large experiment
25 май 2026, 14:59
25 май 2026, 14:59
9ad2a72
Код
Авторство
О чём код?
import { join, emptyTree, DEAD, LIVE } from './algorithms/hashlife.js'; // ── Build level-3 node from 8×8 cell array ─────────────────────────────────── function l1(cells, ox, oy) { const c = (x, y) => (cells[y] && cells[y][x]) ? LIVE : DEAD; return join(c(ox, oy), c(ox+1, oy), c(ox, oy+1), c(ox+1, oy+1)); } function l2(cells, ox, oy) { return join(l1(cells, ox, oy), l1(cells, ox+2, oy), l1(cells, ox, oy+2), l1(cells, ox+2, oy+2)); } function leafToNode(cells) { return join(l2(cells, 0, 0), l2(cells, 4, 0), l2(cells, 0, 4), l2(cells, 4, 4)); } // ── Parse one leaf RLE line into 8×8 cell array ────────────────────────────── // Rows separated by '$', '*' = alive, '.' = dead, trailing empties omitted. function parseLeaf(line) { const rows = line.split('$'); const cells = []; for (let y = 0; y < 8; y++) { cells.push([]); const row = rows[y] || ''; for (let x = 0; x < 8; x++) { cells[y].push(row[x] === '*' ? 1 : 0); } } return leafToNode(cells); } // ── Main parser ────────────────────────────────────────────────────────────── export function parseMC(text) { const lines = text.split('\n'); // nodes[0] = null sentinel (empty, resolved per-context); nodes[1..] = real nodes const nodes = [null]; let rootLevel = 3; // updated as we see non-leaf lines for (const rawLine of lines) { const line = rawLine.trim(); if (!line) continue; if (line.startsWith('[')) continue; // [M2] header if (line.startsWith('#')) continue; // comment if (/^[0-9]/.test(line)) { // Non-leaf node: "level nw ne sw se" const parts = line.split(/\s+/); const L = parseInt(parts[0], 10); const childLvl = L - 1; const empty = emptyTree(childLvl); const resolve = idx => (parseInt(idx, 10) === 0 ? empty : nodes[parseInt(idx, 10)]); const nw = resolve(parts[1]); const ne = resolve(parts[2]); const sw = resolve(parts[3]); const se = resolve(parts[4]); nodes.push(join(nw, ne, sw, se)); rootLevel = L; } else { // Leaf node: 8×8 RLE block (level 3 = 8×8 cells) nodes.push(parseLeaf(line)); rootLevel = 3; } } const root = nodes[nodes.length - 1]; // Center offset: tree covers [0, 2^level), world origin = tree center const half = 1 << (root.level - 1); return { root, offsetX: half, offsetY: half }; }