// Undo (Ctrl+Z) for building, and the bot building ghosts. // ---------- Undo ---------- // Building actions are recorded while undoRec is open (the game wraps each click, drag, cut or // paste in one record). Ctrl+Z plays the last record backwards: what was placed is taken back // and refunded, what was taken back is built again (paid for again), ghosts are cleared. const UNDO_MAX = 80; let undoStack = [], undoRec = null; function undoNote(op) { if (undoRec) undoRec.ops.push(op); } function undoPush(rec) { if (rec && rec.ops.length) { undoStack.push(rec); if (undoStack.length > UNDO_MAX) undoStack.shift(); } } // label: text for the message when it is undone, or a function of the record that makes it function undoRecord(label, fn) { const prev = undoRec, rec = { label: '', ops: [] }; undoRec = rec; try { return fn(); } finally { undoRec = prev; rec.label = typeof label === 'function' ? label(rec) : label; undoPush(rec); } } function undoLast() { const rec = undoStack.pop(); if (!rec) return 'Nothing to undo.'; const prev = undoRec; undoRec = null; let fails = 0; const fail = () => { fails++; }; for (let k = rec.ops.length - 1; k >= 0; k--) { const o = rec.ops[k]; if (o.t === 'block+') { if (blockP[o.by * BW + o.bx] === o.code) removeBlock(o.bx, o.by); } else if (o.t === 'block-') { const r = placeBlock(o.code, isFilter(o.code) ? o.v & 128 : o.v, o.bx, o.by, false, o.extra); if (r) fail(r); } else if (o.t === 'filter') { const b = o.by * BW + o.bx; if (isFilter(blockP[b])) { blockV[b] = o.v & 128; setFilter(b, o.extra); filtersDirty = true; } } else if (o.t === 'mach+') { const mm = machines.find(m => m && m.type === o.type && m.x === o.bx * BS && m.y === o.by * BS); if (mm && siloFull(mm)) fail('empty the silo first'); else if (mm) removeMachine(mm.id, true, false); } else if (o.t === 'mach-') { const r = placeMachineAt(o.type, o.dir, o.bx, o.by); if (r) fail(r); else { const mm = machines.find(m => m && m.type === o.type && m.x === o.bx * BS && m.y === o.by * BS); if (mm) mm.on = o.on !== false; } } else if (o.t === 'mold+') { const mo = molds.find(m => m.kind === o.kind && m.x === o.bx * BS && m.y === o.by * BS); if (mo) removeMold(mo); } else if (o.t === 'mold-') { const r = placeMoldAt(o.kind, o.bx, o.by, o.wall); if (r) fail(r); } else if (o.t === 'paste') { // ghosts still waiting are cleared; anything already built from them is taken back for (const [bx, by, code] of o.blocks) { const b = by * BW + bx; if (ghostBlocks.has(b)) { ghostBlocks.delete(b); ghostBlocked.delete(b); } else if (blockP[b] === code) removeBlock(bx, by); } for (const g of o.machines) { const k = ghostMachines.indexOf(g); if (k >= 0) ghostMachines.splice(k, 1); else { const mm = machines.find(m => m && m.type === g.type && m.x === g.bx * BS && m.y === g.by * BS); if (mm) removeMachine(mm.id, true, false); } } for (const g of o.molds) { const k = ghostMolds.indexOf(g); if (k >= 0) ghostMolds.splice(k, 1); else { const mo = molds.find(m => m.kind === g.kind && m.x === g.bx * BS && m.y === g.by * BS); if (mo) removeMold(mo); } } } } undoRec = prev; powerDirty = true; const n = rec.ops.length; return fails ? `Undo incomplete: ${fails} of ${n} changes could not be put back.` : `Undid ${rec.label}.`; } function clearAllGhosts() { ghostBlocks.clear(); ghostMachines = []; ghostMolds = []; ghostBlocked.clear(); } let ghostMissing = 0, ghostIter = null; function ghostTick() { if (!ghostBlocks.size && !ghostMachines.length && !ghostMolds.length) return; const keepRec = undoRec; undoRec = null; try { ghostTick0(); } finally { undoRec = keepRec; } } function ghostTick0() { let budget = 2, checks = 200; ghostMissing = 0; if (!ghostIter) ghostIter = ghostBlocks.entries(); while (budget > 0 && checks-- > 0 && ghostBlocks.size) { let nx = ghostIter.next(); if (nx.done) { ghostIter = ghostBlocks.entries(); nx = ghostIter.next(); if (nx.done) break; } const [b, [code, v, e]] = nx.value; const bx = b % BW, by = (b / BW) | 0; if (blockP[b] === code && blockV[b] === (isFilter(code) ? v & 128 : v)) { if (isFilter(code) && e && !sameFilter(filterInfo(b), cleanFilterCfg(e))) setFilter(b, e); // same piece, new settings ghostBlocks.delete(b); ghostBlocked.delete(b); continue; } if (!blockInReach(bx, by)) continue; const err = canPlaceBlock(bx, by, code); if (err) { if (err.startsWith('Needs')) ghostBlocked.add(b); continue; } if (!canAfford(pieceCost(code, v), 1)) { ghostMissing++; continue; } placeBlock(code, v, bx, by, false, e); ghostBlocks.delete(b); ghostBlocked.delete(b); budget--; emit({ t: 'sfx', s: 'build' }); } for (let k = ghostMachines.length - 1; k >= 0 && budget > 0; k--) { const g = ghostMachines[k], P = PIECE_BY_ID[g.type]; if (!inReach(g.bx * BS + P.w / 2, g.by * BS + P.h / 2)) continue; if (canPlaceMachine(g.type, g.bx, g.by)) continue; if (!canAfford(P.cost, 1)) { ghostMissing++; continue; } placeMachineAt(g.type, g.dir, g.bx, g.by); ghostMachines.splice(k, 1); budget--; emit({ t: 'sfx', s: 'build' }); } for (let k = ghostMolds.length - 1; k >= 0 && budget > 0; k--) { const g = ghostMolds[k], md = MOLDS[g.kind]; if (!inReach(g.bx * BS + md.w / 2, g.by * BS + md.h / 2)) continue; if (canPlaceMold(g.kind, g.bx, g.by)) continue; if (!canAfford(wallCost(g.wall, md.walls), 1)) { ghostMissing++; continue; } addMold(g.kind, g.bx, g.by, g.wall, false); ghostMolds.splice(k, 1); budget--; emit({ t: 'sfx', s: 'build' }); } }