Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sight line bug #10

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/components/dungeon/movement/MovementAlgorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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)) {
etsune marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}
Expand All @@ -98,23 +96,20 @@ function arePointsOnSameLine(p0: Point, p1: Point, obstacles: Point[]) {
export function basicBfs(pb: PlayBoard, root: Point, depth: number): Point[] {
const queue: Point[] = [];
const explored: Point[] = [];
const obstacles: 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));

etsune marked this conversation as resolved.
Show resolved Hide resolved
queue.unshift(...neighbors);
} else {
obstacles.push(current);
}
explored.push(current);
}
Expand Down