From f2366be0398622a8bc157852cabbe0568c3cc4d8 Mon Sep 17 00:00:00 2001 From: "Eevee (Evelyn Woods)" Date: Mon, 26 Apr 2021 15:57:13 -0600 Subject: [PATCH] Move diamond iteration to algorithms so the editor can (eventually) use it --- js/algorithms.js | 25 +++++++++++++++++++++++++ js/game.js | 25 +++---------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/js/algorithms.js b/js/algorithms.js index 2736a11..6ff8f04 100644 --- a/js/algorithms.js +++ b/js/algorithms.js @@ -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]; + } + } + } +} diff --git a/js/game.js b/js/game.js index 73d2b55..7009aa5 100644 --- a/js/game.js +++ b/js/game.js @@ -660,7 +660,9 @@ export class Level extends LevelInterface { // Orange buttons do a really weird diamond search 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; for (let tile of cell) { 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 // we should check that on our neighbor is_tile_wired(tile, require_stub = true) {