import { grid, cols, rows } from './state.js'; import { config } from './config.js'; import { move, swap } from './actions.js'; import { explode } from './explosive.js'; import { createCell } from './physics.js'; const dxs = [0, 0, -1, 1]; const dys = [1, -1, 0, 0]; export function updateAcid(x, y, cell) { // 1. Corrosive reactions with neighbors for (let i = 0; i < 4; i++) { const nx = x + dxs[i]; const ny = y + dys[i]; if (nx >= 0 && nx < cols && ny >= 0 && ny < rows && grid[nx][ny]) { const t = grid[nx][ny].type; if (t === 'rock' || t === 'wood' || t === 'dirt' || t === 'sand' || t === 'plant' || t === 'grass' || t === 'ice' || t === 'oil') { if (config.acid.dissolve[t] && Math.random() < config.acid.dissolveChance) { grid[nx][ny] = createCell('smoke'); // Dissolves into toxic smoke grid[x][y] = null; // Acid is consumed return; } } else if (t === 'explosive') { if (config.acid.explodeTNT) { explode(nx, ny); grid[x][y] = null; return; } } else if (t === 'lava') { grid[x][y] = createCell('steam'); grid[nx][ny] = createCell('rock'); return; } else if (t === 'water') { if (config.acid.dissolve.water && Math.random() < 0.02) { grid[nx][ny] = createCell('acid'); // Invert/infect water into acid } } } } // 2. Straight fall const below = y + 1; if (below >= rows) return; const belowCell = grid[x][below]; if (!belowCell) { move(x, y, x, below); return; } // Acid is dense: sinks through water & oil if not dissolving them if (belowCell.type === 'water' || belowCell.type === 'oil') { if (!config.acid.dissolve[belowCell.type] && Math.random() < config.acid.sinkChance) { swap(x, y, x, below); return; } } // 3. Diagonal fall const canDownLeft = x > 0 && (!grid[x - 1][below] || isPermeableFluid(grid[x - 1][below])); const canDownRight = x < cols - 1 && (!grid[x + 1][below] || isPermeableFluid(grid[x + 1][below])); if (canDownLeft && canDownRight) { const targetX = Math.random() < 0.5 ? x - 1 : x + 1; executeAcidStep(x, y, targetX, below); return; } else if (canDownLeft) { executeAcidStep(x, y, x - 1, below); return; } else if (canDownRight) { executeAcidStep(x, y, x + 1, below); return; } // 4. Fast horizontal fluid dispersion const maxDisp = config.acid.dispersion || 4; const dir = Math.random() < 0.5 ? 1 : -1; for (const d of [dir, -dir]) { let targetX = x; for (let step = 1; step <= maxDisp; step++) { const nx = x + d * step; if (nx < 0 || nx >= cols) break; const c = grid[nx][y]; if (!c) { targetX = nx; if (y + 1 < rows && !grid[nx][y + 1]) { targetX = nx; break; } } else if ((c.type === 'water' || c.type === 'oil') && !config.acid.dissolve[c.type]) { if (Math.random() < config.acid.sinkChance) { targetX = nx; } break; } else { break; } } if (targetX !== x) { executeAcidStep(x, y, targetX, y); return; } } } function isPermeableFluid(cell) { return cell && (cell.type === 'water' || cell.type === 'oil') && !config.acid.dissolve[cell.type]; } function executeAcidStep(x1, y1, x2, y2) { if (grid[x2][y2]) { swap(x1, y1, x2, y2); } else { move(x1, y1, x2, y2); } }