2026-10-22 14:05:51 +00:00
|
|
|
import { grid, cols, rows } from './state.js';
|
2026-10-22 14:45:19 +00:00
|
|
|
import { move, swap } from './actions.js';
|
2026-10-22 14:05:51 +00:00
|
|
|
|
|
|
|
|
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);
|
2026-10-22 14:45:19 +00:00
|
|
|
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);
|
2026-10-22 14:05:51 +00:00
|
|
|
}
|
2026-10-22 14:45:19 +00:00
|
|
|
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);
|
2026-10-22 14:05:51 +00:00
|
|
|
} else {
|
2026-10-22 14:45:19 +00:00
|
|
|
move(x1, y1, x2, y2);
|
2026-10-22 14:05:51 +00:00
|
|
|
}
|
|
|
|
|
}
|