@pocketrts/grid 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @pocketrts/grid
2
2
 
3
- Grid algorithms for real-time strategy games on an 8-connected cell grid: flow fields shared by whole armies and computed under a fixed per-step budget, bounded local A*, float-cost A* within an area, 4-connected regions, straight-line reach, nearest free cell, distance fields, bucket grids for neighbour queries, field of view, polygons, 8-connected wall chains, line formations and a complete elastic-column march system. Pure logic over typed arrays, deterministic (only `@pocketrts/determinism` math), no DOM.
3
+ Grid algorithms for real-time strategy games on an 8-connected cell grid: flow fields shared by whole armies and computed under a fixed per-step budget, bounded local A*, float-cost A* within an area, 4-connected regions, straight-line reach, nearest free cell, distance fields, bucket grids for neighbour queries, field of view, fog of war per side, polygons, 8-connected wall chains, line formations and a complete elastic-column march system. Pure logic over typed arrays, deterministic (only `@pocketrts/determinism` math), no DOM.
4
4
 
5
5
  Try it: [`apps/grid-demo`](../../apps/grid-demo) (`pnpm --filter @pocketrts/grid-demo dev`).
6
6
 
@@ -33,6 +33,7 @@ ES module, TypeScript types included. Depends on `@pocketrts/determinism`.
33
33
  | `chamferDistance(src, w, h)` | Two-pass distance to the nearest source cell. |
34
34
  | `BucketGrid` | Counting-sort bucket grid rebuilt each step: `build(n, x, y, alive)`, `start` / `items` runs per cell, `cellAt`, `forEachNear(x, y, range, visit)`. |
35
35
  | `stampDisc(vis, stamped, w, h, cx, cy, r)` | Reveal a disc of cells; repeated stamps from one cell cost nothing. |
36
+ | `FogOfWar(w, h, sides, side?)` | Fog of war for sides 1 to `sides` (0 = the whole map): `update(vis, version)` takes each side's visible cells once per version and adds them to that side's explored cells; `watch(side)` switches the view; `visible` / `explored` are the viewed side's arrays (a different array after `watch`, so read them through the getters) and `isVisible` / `isExplored` ask about the viewed side; `visibleOf` / `exploredOf` give any side's arrays; `reset()` forgets everything. |
36
37
  | `inPolygon(points, x, y)`, `polygonDist(points, x, y)` | Even–odd containment and distance to the outline of a flat `[x0, y0, x1, y1, …]` polygon. |
37
38
  | `stepCells`, `ringChain`, `chainLinks`, `chainOutSides` | 8-connected chains (walls, fences): cells between two points, a closed chain through waypoints with loops and extra corner cells removed, which neighbours a chain cell joins, which side of each joined segment faces out. |
38
39
  | `lineFormation(units, cx, cy, dx, dy, positions?, options?)`, `FormationUnit`, `Formation`, `FormationOptions`, `DEFAULT_FORMATION_OPTIONS` | Ranks facing a direction: one rank class per rank (lower in front), frontage from the group's footprint (with `taper`, the ranks in front narrower: a wedge); with current positions, members keep their lateral order so paths do not cross. Returns spots, offsets, rank and place per member, and per-rank width, depth, count, radius and slowest speed. |
package/dist/vision.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
- * Field of view on a cell raster: each viewer reveals a disc of cells. A per-cell record of the largest radius already
3
- * stamped there lets many viewers standing in the same cell cost one stamp.
2
+ * Field of view on a cell raster: each viewer reveals a disc of cells, and a per-cell record of the largest radius
3
+ * already stamped there lets many viewers standing in the same cell cost one stamp. Fog of war keeps, per side, the
4
+ * cells seen now and the cells ever seen, and which side's view is shown.
4
5
  */
5
6
  /**
6
7
  * Mark the disc of radius `r` cells around cell (cx, cy) in `vis` (set to 1), clipped to the `w` × `h` raster.
@@ -9,3 +10,54 @@
9
10
  * `vis` and `stamped` together (fill with 0) before each recomputation.
10
11
  */
11
12
  export declare function stampDisc(vis: Uint8Array, stamped: Uint8Array | Uint16Array, w: number, h: number, cx: number, cy: number, r: number): void;
13
+ /**
14
+ * Fog of war for several sides (alliances, or any party with a shared view) on one cell raster: what each side sees now,
15
+ * what each side has ever seen, and the view being shown. Sides are numbered 1 to `sides`; side 0 is the whole-map view,
16
+ * where every cell is visible and explored. The game computes each side's visible cells (with `stampDisc`, say) and hands
17
+ * them in with a version that changes whenever they are recomputed; `update` copies and accumulates them only when the
18
+ * version changes, so calling it every frame is cheap and every reader of one frame sees the same cells.
19
+ */
20
+ export declare class FogOfWar {
21
+ readonly w: number;
22
+ readonly h: number;
23
+ readonly sides: number;
24
+ /** Cells the side sees now (1 = visible), per side; [0] is the whole map, all 1 */
25
+ private readonly visibleBy;
26
+ /** Cells the side has ever seen (1 = explored), per side; [0] is the whole map, all 1 */
27
+ private readonly exploredBy;
28
+ private viewSide;
29
+ private lastVersion;
30
+ /** All cells start unseen and unexplored, and the view starts on side `side` (default 0, the whole map) */
31
+ constructor(w: number, h: number, sides: number, side?: number);
32
+ /** The side being viewed; 0 = the whole map */
33
+ get side(): number;
34
+ /** The version of the last update, NaN before the first */
35
+ get version(): number;
36
+ /**
37
+ * The viewed side's cells visible now (1 = visible). The array belongs to the side, so `watch` makes this return
38
+ * another array: read it through this getter rather than keeping it. Do not write to it.
39
+ */
40
+ get visible(): Uint8Array;
41
+ /** The viewed side's explored cells (1 = explored); like `visible`, a different array after `watch`. Do not write to it */
42
+ get explored(): Uint8Array;
43
+ /** Side `side`'s cells visible now, whatever the view; do not write to it */
44
+ visibleOf(side: number): Uint8Array;
45
+ /** Side `side`'s explored cells, whatever the view; do not write to it */
46
+ exploredOf(side: number): Uint8Array;
47
+ /** Show side `side`'s view (0 = the whole map). Every side keeps its own history, so switching loses nothing */
48
+ watch(side: number): void;
49
+ /**
50
+ * Take each side's visible cells: `vis[s]` holds side s's cells (any nonzero = visible, `w` × `h`, row-major) for s
51
+ * from 1 to `sides`; `vis[0]` is ignored. Nothing happens when `version` equals the last update's. Otherwise every
52
+ * side's current cells are replaced and added to its explored cells, whichever side is viewed. Returns whether it
53
+ * took them.
54
+ */
55
+ update(vis: readonly ArrayLike<number>[], version: number): boolean;
56
+ /** Whether the viewed side sees cell (x, y) now; false outside the raster */
57
+ isVisible(x: number, y: number): boolean;
58
+ /** Whether the viewed side has explored cell (x, y); false outside the raster */
59
+ isExplored(x: number, y: number): boolean;
60
+ /** Forget everything every side has seen and wait for the next update, whatever its version; the view is kept */
61
+ reset(): void;
62
+ private check;
63
+ }
package/dist/vision.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
- * Field of view on a cell raster: each viewer reveals a disc of cells. A per-cell record of the largest radius already
3
- * stamped there lets many viewers standing in the same cell cost one stamp.
2
+ * Field of view on a cell raster: each viewer reveals a disc of cells, and a per-cell record of the largest radius
3
+ * already stamped there lets many viewers standing in the same cell cost one stamp. Fog of war keeps, per side, the
4
+ * cells seen now and the cells ever seen, and which side's view is shown.
4
5
  */
5
6
  /**
6
7
  * Mark the disc of radius `r` cells around cell (cx, cy) in `vis` (set to 1), clipped to the `w` × `h` raster.
@@ -28,3 +29,114 @@ export function stampDisc(vis, stamped, w, h, cx, cy, r) {
28
29
  vis[row + xx] = 1;
29
30
  }
30
31
  }
32
+ /**
33
+ * Fog of war for several sides (alliances, or any party with a shared view) on one cell raster: what each side sees now,
34
+ * what each side has ever seen, and the view being shown. Sides are numbered 1 to `sides`; side 0 is the whole-map view,
35
+ * where every cell is visible and explored. The game computes each side's visible cells (with `stampDisc`, say) and hands
36
+ * them in with a version that changes whenever they are recomputed; `update` copies and accumulates them only when the
37
+ * version changes, so calling it every frame is cheap and every reader of one frame sees the same cells.
38
+ */
39
+ export class FogOfWar {
40
+ w;
41
+ h;
42
+ sides;
43
+ /** Cells the side sees now (1 = visible), per side; [0] is the whole map, all 1 */
44
+ visibleBy;
45
+ /** Cells the side has ever seen (1 = explored), per side; [0] is the whole map, all 1 */
46
+ exploredBy;
47
+ viewSide = 0;
48
+ lastVersion = NaN;
49
+ /** All cells start unseen and unexplored, and the view starts on side `side` (default 0, the whole map) */
50
+ constructor(w, h, sides, side = 0) {
51
+ this.w = w;
52
+ this.h = h;
53
+ this.sides = sides;
54
+ if (!Number.isInteger(sides) || sides < 0)
55
+ throw new RangeError(`sides must be a non-negative integer, got ${sides}`);
56
+ const n = w * h, all = new Uint8Array(n).fill(1);
57
+ this.visibleBy = Array.from({ length: sides + 1 }, (_, s) => (s ? new Uint8Array(n) : all));
58
+ this.exploredBy = Array.from({ length: sides + 1 }, (_, s) => (s ? new Uint8Array(n) : all));
59
+ this.watch(side);
60
+ }
61
+ /** The side being viewed; 0 = the whole map */
62
+ get side() {
63
+ return this.viewSide;
64
+ }
65
+ /** The version of the last update, NaN before the first */
66
+ get version() {
67
+ return this.lastVersion;
68
+ }
69
+ /**
70
+ * The viewed side's cells visible now (1 = visible). The array belongs to the side, so `watch` makes this return
71
+ * another array: read it through this getter rather than keeping it. Do not write to it.
72
+ */
73
+ get visible() {
74
+ return this.visibleBy[this.viewSide];
75
+ }
76
+ /** The viewed side's explored cells (1 = explored); like `visible`, a different array after `watch`. Do not write to it */
77
+ get explored() {
78
+ return this.exploredBy[this.viewSide];
79
+ }
80
+ /** Side `side`'s cells visible now, whatever the view; do not write to it */
81
+ visibleOf(side) {
82
+ return this.visibleBy[this.check(side)];
83
+ }
84
+ /** Side `side`'s explored cells, whatever the view; do not write to it */
85
+ exploredOf(side) {
86
+ return this.exploredBy[this.check(side)];
87
+ }
88
+ /** Show side `side`'s view (0 = the whole map). Every side keeps its own history, so switching loses nothing */
89
+ watch(side) {
90
+ this.viewSide = this.check(side);
91
+ }
92
+ /**
93
+ * Take each side's visible cells: `vis[s]` holds side s's cells (any nonzero = visible, `w` × `h`, row-major) for s
94
+ * from 1 to `sides`; `vis[0]` is ignored. Nothing happens when `version` equals the last update's. Otherwise every
95
+ * side's current cells are replaced and added to its explored cells, whichever side is viewed. Returns whether it
96
+ * took them.
97
+ */
98
+ update(vis, version) {
99
+ if (version === this.lastVersion)
100
+ return false;
101
+ const n = this.w * this.h;
102
+ if (vis.length < this.sides + 1)
103
+ throw new RangeError(`expected visible cells for sides 1 to ${this.sides}`);
104
+ for (let s = 1; s <= this.sides; s++) {
105
+ if (vis[s].length !== n)
106
+ throw new RangeError(`side ${s}: expected ${n} cells, got ${vis[s].length}`);
107
+ }
108
+ for (let s = 1; s <= this.sides; s++) {
109
+ const src = vis[s], cur = this.visibleBy[s], ex = this.exploredBy[s];
110
+ for (let i = 0; i < n; i++) {
111
+ if (src[i])
112
+ cur[i] = ex[i] = 1;
113
+ else
114
+ cur[i] = 0;
115
+ }
116
+ }
117
+ this.lastVersion = version;
118
+ return true;
119
+ }
120
+ /** Whether the viewed side sees cell (x, y) now; false outside the raster */
121
+ isVisible(x, y) {
122
+ return x >= 0 && y >= 0 && x < this.w && y < this.h && this.visible[y * this.w + x] === 1;
123
+ }
124
+ /** Whether the viewed side has explored cell (x, y); false outside the raster */
125
+ isExplored(x, y) {
126
+ return x >= 0 && y >= 0 && x < this.w && y < this.h && this.explored[y * this.w + x] === 1;
127
+ }
128
+ /** Forget everything every side has seen and wait for the next update, whatever its version; the view is kept */
129
+ reset() {
130
+ for (let s = 1; s <= this.sides; s++) {
131
+ this.visibleBy[s].fill(0);
132
+ this.exploredBy[s].fill(0);
133
+ }
134
+ this.lastVersion = NaN;
135
+ }
136
+ check(side) {
137
+ if (!Number.isInteger(side) || side < 0 || side > this.sides) {
138
+ throw new RangeError(`side must be an integer from 0 to ${this.sides}, got ${side}`);
139
+ }
140
+ return side;
141
+ }
142
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pocketrts/grid",
3
- "version": "0.5.0",
4
- "description": "Grid algorithms for RTS games: budgeted flow fields, bounded local A*, float-cost A* within an area, 4-connected regions, line reach, distance fields, bucket grids, field of view, polygons, cell chains, line formations and column march",
3
+ "version": "0.6.0",
4
+ "description": "Grid algorithms for RTS games: budgeted flow fields, bounded local A*, float-cost A* within an area, 4-connected regions, line reach, distance fields, bucket grids, field of view, fog of war per side, polygons, cell chains, line formations and column march",
5
5
  "license": "MIT",
6
6
  "author": "Junjie Bai <bjj19851010@gmail.com>",
7
7
  "repository": {