import { grid, cols, rows } from './state.js'; import { move, swap } from './actions.js'; export function updateDirt(x, y, cell) { const below = y + 1; if (below >= rows) return; const belowCell = grid[x][below]; if (!belowCell) { move(x, y, x, below); return; } // Sinks through liquids (water, acid, oil) if (belowCell.type === 'water' || belowCell.type === 'acid' || belowCell.type === 'oil') { if (Math.random() < 0.35) { swap(x, y, x, below); } return; } // Slide diagonally down left or right const canDownLeft = x > 0 && (!grid[x - 1][below] || isDisplaceableLiquid(grid[x - 1][below])); const canDownRight = x < cols - 1 && (!grid[x + 1][below] || isDisplaceableLiquid(grid[x + 1][below])); if (canDownLeft && canDownRight) { const targetX = Math.random() < 0.5 ? x - 1 : x + 1; executeMove(x, y, targetX, below); } else if (canDownLeft) { executeMove(x, y, x - 1, below); } else if (canDownRight) { executeMove(x, y, x + 1, below); } } function isDisplaceableLiquid(target) { return target && (target.type === 'water' || target.type === 'acid' || target.type === 'oil'); } function executeMove(x1, y1, x2, y2) { if (grid[x2][y2]) { swap(x1, y1, x2, y2); } else { move(x1, y1, x2, y2); } }