Move diamond iteration to algorithms so the editor can (eventually) use it

This commit is contained in:
Eevee (Evelyn Woods) 2021-04-26 15:57:13 -06:00
parent 4077bd0de3
commit f2366be039
2 changed files with 28 additions and 22 deletions

View File

@ -118,3 +118,28 @@ export function find_matching_wire_tunnel(level, x, y, direction) {
} }
} }
} }
// TODO make this guy work generically for orange, red, brown buttons? others...?
export function find_implicit_connection() {
}
// Iterates over a grid in a diamond pattern, spreading out from the given start cell (but not
// including it). Only used for connecting orange buttons.
export function* iter_cells_in_diamond(levelish, x0, y0) {
let max_search_radius = Math.max(levelish.size_x, levelish.size_y) + 1;
for (let dist = 1; dist <= max_search_radius; dist++) {
// Start east and move counterclockwise
let sx = x0 + dist;
let sy = y0;
for (let direction of [[-1, -1], [-1, 1], [1, 1], [1, -1]]) {
for (let i = 0; i < dist; i++) {
let cell = levelish.cell(sx, sy);
if (cell) {
yield cell;
}
sx += direction[0];
sy += direction[1];
}
}
}
}

View File

@ -660,7 +660,9 @@ export class Level extends LevelInterface {
// Orange buttons do a really weird diamond search // Orange buttons do a really weird diamond search
if (connectable.type.connect_order === 'diamond') { if (connectable.type.connect_order === 'diamond') {
for (let cell of this.iter_cells_in_diamond(connectable.cell)) { for (let cell of algorithms.iter_cells_in_diamond(
this, connectable.cell.x, connectable.cell.y))
{
let target = null; let target = null;
for (let tile of cell) { for (let tile of cell) {
if (tile && goals.has(tile.type.name)) { if (tile && goals.has(tile.type.name)) {
@ -2355,27 +2357,6 @@ export class Level extends LevelInterface {
} }
} }
// Iterates over the grid in a diamond pattern, spreading out from the given start cell (but not
// including it). Only used for connecting orange buttons.
*iter_cells_in_diamond(start_cell) {
let max_search_radius = Math.max(this.size_x, this.size_y) + 1;
for (let dist = 1; dist <= max_search_radius; dist++) {
// Start east and move counterclockwise
let sx = start_cell.x + dist;
let sy = start_cell.y;
for (let direction of [[-1, -1], [-1, 1], [1, 1], [1, -1]]) {
for (let i = 0; i < dist; i++) {
let cell = this.cell(sx, sy);
if (cell) {
yield cell;
}
sx += direction[0];
sy += direction[1];
}
}
}
}
// FIXME require_stub should really just care whether we ourselves /can/ contain wire, and also // FIXME require_stub should really just care whether we ourselves /can/ contain wire, and also
// we should check that on our neighbor // we should check that on our neighbor
is_tile_wired(tile, require_stub = true) { is_tile_wired(tile, require_stub = true) {