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

reorganised imports for FOV module #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 20 additions & 20 deletions src/fov/discrete-shadowcasting.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import FOV, { VisibilityCallback } from "./fov.js";
import { FOV, VisibilityCallback } from "./fov";

/**
* @class Discrete shadowcasting algorithm. Obsoleted by Precise shadowcasting.
* @augments ROT.FOV
*/
export default class DiscreteShadowcasting extends FOV {
export class DiscreteShadowcasting extends FOV {
compute(x: number, y: number, R: number, callback: VisibilityCallback) {
/* this place is always visible */
callback(x, y, 0, 1);

/* standing in a dark place. FIXME is this a good idea? */
if (!this._lightPasses(x, y)) { return; }

/* start and end angles */
let DATA: number[] = [];

let A, B, cx, cy, blocks;

/* analyze surrounding cells in concentric rings, starting from the center */
Expand All @@ -27,10 +27,10 @@ export default class DiscreteShadowcasting extends FOV {
cy = neighbors[i][1];
A = angle * (i - 0.5);
B = A + angle;

blocks = !this._lightPasses(cx, cy);
if (this._visibleCoords(Math.floor(A), Math.ceil(B), blocks, DATA)) { callback(cx, cy, r, 1); }

if (DATA.length == 2 && DATA[0] == 0 && DATA[1] == 360) { return; } /* cutoff? */

} /* for all cells in this ring */
Expand All @@ -44,57 +44,57 @@ export default class DiscreteShadowcasting extends FOV {
* @param {int[][]} DATA shadowed angle pairs
*/
_visibleCoords(A: number, B: number, blocks: boolean, DATA: number[]): boolean {
if (A < 0) {
if (A < 0) {
let v1 = this._visibleCoords(0, B, blocks, DATA);
let v2 = this._visibleCoords(360+A, 360, blocks, DATA);
return v1 || v2;
}

let index = 0;
while (index < DATA.length && DATA[index] < A) { index++; }

if (index == DATA.length) { /* completely new shadow */
if (blocks) { DATA.push(A, B); }
if (blocks) { DATA.push(A, B); }
return true;
}

let count = 0;

if (index % 2) { /* this shadow starts in an existing shadow, or within its ending boundary */
while (index < DATA.length && DATA[index] < B) {
index++;
count++;
}

if (count == 0) { return false; }
if (blocks) {

if (blocks) {
if (count % 2) {
DATA.splice(index-count, count, B);
} else {
DATA.splice(index-count, count);
}
}

return true;

} else { /* this shadow starts outside an existing shadow, or within a starting boundary */
while (index < DATA.length && DATA[index] < B) {
index++;
count++;
}

/* visible when outside an existing shadow, or when overlapping */
if (A == DATA[index-count] && count == 1) { return false; }
if (blocks) {

if (blocks) {
if (count % 2) {
DATA.splice(index-count, count, A);
} else {
DATA.splice(index-count, count, A, B);
}
}

return true;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/fov/fov.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DIRS } from "../constants.js";
import { DIRS } from "../constants";

export interface LightPassesCallback { (x: number, y: number): boolean };

Expand All @@ -8,9 +8,9 @@ export interface Options {
topology: 4 | 6 | 8
}

export default abstract class FOV {
_lightPasses: LightPassesCallback;
_options: Options;
export abstract class FOV {
protected _lightPasses: LightPassesCallback;
protected _options: Options;

/**
* @class Abstract FOV algorithm
Expand Down
12 changes: 8 additions & 4 deletions src/fov/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import DiscreteShadowcasting from "./discrete-shadowcasting.js";
import PreciseShadowcasting from "./precise-shadowcasting.js";
import RecursiveShadowcasting from "./recursive-shadowcasting.js";
import { DiscreteShadowcasting } from "./discrete-shadowcasting";
import { PreciseShadowcasting } from "./precise-shadowcasting";
import { RecursiveShadowcasting } from "./recursive-shadowcasting";

export default { DiscreteShadowcasting, PreciseShadowcasting, RecursiveShadowcasting };
export const FOV = {
DiscreteShadowcasting,
PreciseShadowcasting,
RecursiveShadowcasting
};
18 changes: 9 additions & 9 deletions src/fov/precise-shadowcasting.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import FOV, { VisibilityCallback } from "./fov.js";
import { FOV, VisibilityCallback } from "./fov";

type Arc = [number, number];

/**
* @class Precise shadowcasting algorithm
* @augments ROT.FOV
*/
export default class PreciseShadowcasting extends FOV {
export class PreciseShadowcasting extends FOV {
compute(x: number, y: number, R: number, callback: VisibilityCallback) {
/* this place is always visible */
callback(x, y, 0, 1);

/* standing in a dark place. FIXME is this a good idea? */
if (!this._lightPasses(x, y)) { return; }

/* list of all shadows */
let SHADOWS: Arc[] = [];

let cx, cy, blocks, A1, A2, visibility;

/* analyze surrounding cells in concentric rings, starting from the center */
Expand All @@ -29,8 +29,8 @@ export default class PreciseShadowcasting extends FOV {
cy = neighbors[i][1];
/* shift half-an-angle backwards to maintain consistency of 0-th cells */
A1 = [i ? 2*i-1 : 2*neighborCount-1, 2*neighborCount];
A2 = [2*i+1, 2*neighborCount];
A2 = [2*i+1, 2*neighborCount];

blocks = !this._lightPasses(cx, cy);
visibility = this._checkVisibility(A1 as Arc, A2 as Arc, blocks, SHADOWS);
if (visibility) { callback(cx, cy, r, visibility); }
Expand Down Expand Up @@ -79,15 +79,15 @@ export default class PreciseShadowcasting extends FOV {

let visible = true;
if (index1 == index2 && (edge1 || edge2)) { /* subset of existing shadow, one of the edges match */
visible = false;
visible = false;
} else if (edge1 && edge2 && index1+1==index2 && (index2 % 2)) { /* completely equivalent with existing shadow */
visible = false;
} else if (index1 > index2 && (index1 % 2)) { /* subset of existing shadow, not touching */
visible = false;
}

if (!visible) { return 0; } /* fast case: not visible */

let visibleLength;

/* compute the length of visible arc, adjust list of shadows (if blocking) */
Expand Down
22 changes: 11 additions & 11 deletions src/fov/recursive-shadowcasting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FOV, { VisibilityCallback } from "./fov.js";
import { FOV, VisibilityCallback } from "./fov";

/** Octants used for translating recursive shadowcasting offsets */
const OCTANTS = [
Expand All @@ -18,7 +18,7 @@ const OCTANTS = [
* Based on Peter Harkins' implementation of Björn Bergström's algorithm described here: http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting
* @augments ROT.FOV
*/
export default class RecursiveShadowcasting extends FOV {
export class RecursiveShadowcasting extends FOV {
/**
* Compute visibility for a 360-degree circle
* @param {int} x
Expand Down Expand Up @@ -91,10 +91,10 @@ export default class RecursiveShadowcasting extends FOV {
* @param {float} visSlopeStart The slope to start at
* @param {float} visSlopeEnd The slope to end at
* @param {int} radius The radius to reach out to
* @param {int} xx
* @param {int} xy
* @param {int} yx
* @param {int} yy
* @param {int} xx
* @param {int} xy
* @param {int} yx
* @param {int} yy
* @param {function} callback The callback to use when we hit a block that is visible
*/
_castVisibility(startX: number, startY: number, row: number, visSlopeStart: number, visSlopeEnd: number, radius: number, xx: number, xy: number, yx: number, yy: number, callback: VisibilityCallback) {
Expand All @@ -116,18 +116,18 @@ export default class RecursiveShadowcasting extends FOV {
//Range of the row
let slopeStart = (dx - 0.5) / (dy + 0.5);
let slopeEnd = (dx + 0.5) / (dy - 0.5);

//Ignore if not yet at left edge of Octant
if (slopeEnd > visSlopeStart) { continue; }

//Done if past right edge
if (slopeStart < visSlopeEnd) { break; }

//If it's in range, it's visible
if ((dx * dx + dy * dy) < (radius * radius)) {
callback(mapX, mapY, i, 1);
}

if (!blocked) {
//If tile is a blocking tile, cast around it
if (!this._lightPasses(mapX, mapY) && i < radius) {
Expand All @@ -141,7 +141,7 @@ export default class RecursiveShadowcasting extends FOV {
newStart = slopeEnd;
continue;
}

//Block has ended
blocked = false;
visSlopeStart = newStart;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export {default as Display} from "./display/display.js";
export {default as StringGenerator} from "./stringgenerator.js";
export {default as EventQueue} from "./eventqueue.js";
export {default as Scheduler, SpeedActor} from "./scheduler/index.js";
export {default as FOV} from "./fov/index.js";
export { FOV } from "./fov/index";
export {default as Map} from "./map/index.js";
export {default as Noise} from "./noise/index.js";
export {default as Path} from "./path/index.js";
Expand Down
2 changes: 1 addition & 1 deletion src/lighting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FOV from "./fov/fov.js";
import { FOV } from "./fov/fov";
import * as Color from "./color.js";

type LightColor = [number, number, number];
Expand Down