From cee794580c10ac1cb2948e7519a0cbab91ccd9db Mon Sep 17 00:00:00 2001 From: etsune Date: Fri, 21 Jul 2023 23:59:37 +0300 Subject: [PATCH 1/3] Fix sight line bug --- .../dungeon/movement/MovementAlgorithm.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/components/dungeon/movement/MovementAlgorithm.ts b/src/components/dungeon/movement/MovementAlgorithm.ts index 8d9b4a9..8020d01 100644 --- a/src/components/dungeon/movement/MovementAlgorithm.ts +++ b/src/components/dungeon/movement/MovementAlgorithm.ts @@ -55,10 +55,7 @@ export function isRecentDirection(actor: Actor, relPoint: Point): boolean { // Calculates whether point 0 can see point 1 // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm -function arePointsOnSameLine(p0: Point, p1: Point, obstacles: Point[]) { - if (obstacles.length === 0) { - return true; - } +function arePointsOnSameLine(p0: Point, p1: Point, pb: PlayBoard) { let x0 = p0.x; let y0 = p0.y; const x1 = p1.x; @@ -71,8 +68,9 @@ function arePointsOnSameLine(p0: Point, p1: Point, obstacles: Point[]) { const sy = (y0 < y1) ? 1 : -1; let diff = Δx + Δy; + let curPoint = p0; - while (x0 !== x1 || y0 !== y1) { + while (!isEqualPoint(curPoint, p1)) { const e2 = 2 * diff; if (e2 < Δx) { @@ -83,9 +81,9 @@ function arePointsOnSameLine(p0: Point, p1: Point, obstacles: Point[]) { diff += Δy; x0 += sx; } - const curPoint = new Point(x0, y0); + curPoint = new Point(x0, y0); - if (obstacles.some((e) => isEqualPoint(curPoint, e))) { + if (!pb[x0][y0]?.isGround && !isEqualPoint(curPoint, p1)) { return false; } } @@ -103,14 +101,14 @@ export function basicBfs(pb: PlayBoard, root: Point, depth: number): Point[] { queue.push(root); while (queue.length > 0) { - const current = queue.pop()!; + const current = queue.shift()!; if (isGroundCell(current, pb)) { const neighbors = getDirectionsForPoint(current) .filter((e) => doesPointExist(e, pb)) .filter((e) => !explored.some((c) => isEqualPoint(c, e))) .filter((e) => getDistance(e, root) <= depth) - .filter((e) => arePointsOnSameLine(root, e, obstacles)); + .filter((e) => arePointsOnSameLine(root, e, pb)); queue.unshift(...neighbors); } else { From 7f0ad878feb59ce179e3e00e9d8b825f09bfbdf9 Mon Sep 17 00:00:00 2001 From: etsune Date: Sat, 22 Jul 2023 13:48:16 +0300 Subject: [PATCH 2/3] Requested fixes --- src/components/dungeon/movement/MovementAlgorithm.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/components/dungeon/movement/MovementAlgorithm.ts b/src/components/dungeon/movement/MovementAlgorithm.ts index 8020d01..bcbcae6 100644 --- a/src/components/dungeon/movement/MovementAlgorithm.ts +++ b/src/components/dungeon/movement/MovementAlgorithm.ts @@ -96,7 +96,6 @@ function arePointsOnSameLine(p0: Point, p1: Point, pb: PlayBoard) { export function basicBfs(pb: PlayBoard, root: Point, depth: number): Point[] { const queue: Point[] = []; const explored: Point[] = []; - const obstacles: Point[] = []; queue.push(root); @@ -111,8 +110,6 @@ export function basicBfs(pb: PlayBoard, root: Point, depth: number): Point[] { .filter((e) => arePointsOnSameLine(root, e, pb)); queue.unshift(...neighbors); - } else { - obstacles.push(current); } explored.push(current); } From d01a577926a2a637953f3b1b37909f5950f150e9 Mon Sep 17 00:00:00 2001 From: etsune Date: Sat, 22 Jul 2023 14:13:08 +0300 Subject: [PATCH 3/3] Requested changes --- src/components/dungeon/movement/MovementAlgorithm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/dungeon/movement/MovementAlgorithm.ts b/src/components/dungeon/movement/MovementAlgorithm.ts index bcbcae6..982dbce 100644 --- a/src/components/dungeon/movement/MovementAlgorithm.ts +++ b/src/components/dungeon/movement/MovementAlgorithm.ts @@ -83,7 +83,7 @@ function arePointsOnSameLine(p0: Point, p1: Point, pb: PlayBoard) { } curPoint = new Point(x0, y0); - if (!pb[x0][y0]?.isGround && !isEqualPoint(curPoint, p1)) { + if (!isGroundCell(curPoint, pb) && !isEqualPoint(curPoint, p1)) { return false; } }