-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield-of-view.ts
154 lines (129 loc) · 3.68 KB
/
field-of-view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { XY } from "@jhuggett/terminal/xy";
function range(start: number, end: number) {
const result = [];
for (let i = start; i <= end; i++) {
result.push(i);
}
return result;
}
class Fraction {
constructor(public numerator: number, public denominator: number) {}
value() {
return this.numerator / this.denominator;
}
multiply(other: Fraction) {
return new Fraction(
this.numerator * other.numerator,
this.denominator * other.denominator
);
}
}
export const calculateFOV = (
origin: XY,
isBlocked: (point: XY) => boolean,
markAsVisible: (point: XY) => void,
maxDepth = 10
) => {
markAsVisible(origin);
for (const i of [0, 1, 2, 3]) {
const quadrant = new Quadrant(i, origin);
const reveal = ({ column, row }: Tile) => {
markAsVisible(quadrant.transform(row, column));
};
const isWall = ({ column, row }: Tile) => {
return isBlocked(quadrant.transform(row, column));
};
const isFloor = ({ column, row }: Tile) => {
return !isBlocked(quadrant.transform(row, column));
};
const scan = (row: Row) => {
if (row.depth > maxDepth) {
return;
}
let previousTile: Tile | undefined;
for (const tile of row.tiles()) {
if (isWall(tile) || isSymmetric(row, tile)) {
reveal(tile);
}
if (previousTile && isWall(previousTile) && isFloor(tile)) {
row.startingSlope = slopeAt(tile);
}
if (previousTile && isFloor(previousTile) && isWall(tile)) {
const nextRow = row.next();
nextRow.endingSlope = slopeAt(tile);
scan(nextRow);
}
previousTile = tile;
}
if (previousTile && isFloor(previousTile)) {
scan(row.next());
}
};
const firstRow = new Row(1, new Fraction(-1, 1), new Fraction(1, 1));
scan(firstRow);
}
};
type Tile = {
row: number;
column: number;
};
enum QuadrantCardinal {
North = 0,
East = 1,
South = 2,
West = 3,
}
class Quadrant {
constructor(public cardinal: QuadrantCardinal, public origin: XY) {}
transform(row: number, column: number) {
switch (this.cardinal) {
case QuadrantCardinal.North:
return { x: this.origin.x + column, y: this.origin.y - row };
case QuadrantCardinal.East:
return { x: this.origin.x + row, y: this.origin.y + column };
case QuadrantCardinal.South:
return { x: this.origin.x + column, y: this.origin.y + row };
case QuadrantCardinal.West:
return { x: this.origin.x - row, y: this.origin.y + column };
}
throw new Error(`Invalid cardinal: ${this.cardinal}`);
}
}
class Row {
constructor(
public depth: number,
public startingSlope: Fraction,
public endingSlope: Fraction
) {}
tiles(): Tile[] {
const minColumn = roundTiesUp(
this.startingSlope.multiply(new Fraction(this.depth, 1))
);
const maxColumn = roundTiesDown(
this.endingSlope.multiply(new Fraction(this.depth, 1))
);
return range(minColumn, maxColumn + 1).map((column) => ({
row: this.depth,
column,
}));
}
next() {
return new Row(this.depth + 1, this.startingSlope, this.endingSlope);
}
}
const roundTiesUp = (fraction: Fraction) => {
return Math.floor(fraction.value() + 0.5);
};
const roundTiesDown = (fraction: Fraction) => {
return Math.ceil(fraction.value() - 0.5);
};
const slopeAt = ({ column, row }: Tile) => {
return new Fraction(2 * column - 1, 2 * row);
};
const isSymmetric = (row: Row, tile: Tile) => {
return (
tile.column >=
row.startingSlope.multiply(new Fraction(row.depth, 1)).value() &&
tile.column <= row.endingSlope.multiply(new Fraction(row.depth, 1)).value()
);
};