@xtia/grid 0.0.7 → 0.0.8

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
  # `Grid`
2
2
 
3
- **Alpha**: Some API details may change
3
+ **Alpha**: Pre-0.1.0 API details may change
4
4
 
5
5
  ## Summary
6
6
 
@@ -48,7 +48,7 @@ const grid = Grid.from(source);
48
48
 
49
49
  ### Wrapping existing structures
50
50
 
51
- Use Grid's interface over any read/write 2D structure:
51
+ Use Grid's interface and features over any read/write 2D structure:
52
52
 
53
53
  ```ts
54
54
  const gameMap = [
package/lib/grid.d.ts CHANGED
@@ -13,7 +13,7 @@ type PathFindOptions<T> = {
13
13
  cell: Cell<T>;
14
14
  cost: number;
15
15
  }) => void;
16
- shortcutMap?: Source2D<Cell<T>[] | undefined>;
16
+ shortcutMap?: Source2D<Cell<T>[] | undefined> | GetXYFunc<Cell<T>[]>;
17
17
  };
18
18
  type CostMapOptions<T> = PathFindOptions<T> & {
19
19
  stopAt?: LocationSpec<T>;
@@ -43,9 +43,9 @@ export declare class Cell<T> {
43
43
  private getSiblingXY;
44
44
  getNeighbours(includeDiagonals?: boolean): Cell<T>[];
45
45
  look(xDelta: number, yDelta: number): Cell<T> | undefined;
46
- findPath(target: LocationSpec<T>, getCost: CostFunc<T>, options?: PathFindOptions<T>): Cell<T>[] | null;
47
- getPathMap(getCost: CostFunc<T>, options?: PathFindOptions<T>): Pipe2D<Cell<T>[] | null>;
48
- getCostMap(getCost: CostFunc<T>, options?: CostMapOptions<T>): Pipe2D<number>;
46
+ findPath(target: LocationSpec<T>, getCost: CostFunc<T> | Map<T, number>, options?: PathFindOptions<T>): Cell<T>[] | null;
47
+ getPathMap(getCost: CostFunc<T> | Map<T, number>, options?: PathFindOptions<T>): Pipe2D<Cell<T>[] | null>;
48
+ getCostMap(getCost: CostFunc<T> | Map<T, number>, options?: CostMapOptions<T>): Pipe2D<number>;
49
49
  private calculateCosts;
50
50
  getLineTo(target: LocationSpec<T>): IterableIterator<Cell<T>>;
51
51
  getLineTo(x: number, y: number): IterableIterator<Cell<T>>;
package/lib/grid.js CHANGED
@@ -72,15 +72,18 @@ export class Cell {
72
72
  const pathInfo = asPathMap
73
73
  ? new Map()
74
74
  : null;
75
+ const getCostFn = getCost instanceof Map
76
+ ? (cell) => { var _a; return (_a = getCost.get(cell.value)) !== null && _a !== void 0 ? _a : Infinity; }
77
+ : getCost;
75
78
  costs.set(this, 0);
76
79
  const queue = new OrderedQueue(cell => { var _a; return (_a = costs.get(cell)) !== null && _a !== void 0 ? _a : Infinity; }, this);
77
80
  const stopAtCell = options.stopAt && this.getSiblingCell(options.stopAt);
78
81
  while (queue.length > 0) {
79
82
  const current = queue.take();
80
- (_a = options.onVisit) === null || _a === void 0 ? void 0 : _a.call(options, { cell: current, cost: costs.get(current) });
81
83
  if (visited.has(current))
82
84
  continue;
83
85
  visited.add(current);
86
+ (_a = options.onVisit) === null || _a === void 0 ? void 0 : _a.call(options, { cell: current, cost: costs.get(current) });
84
87
  if (stopAtCell && current === stopAtCell) {
85
88
  break;
86
89
  }
@@ -91,19 +94,21 @@ export class Cell {
91
94
  : TraversalType.diagonal,
92
95
  cell
93
96
  }));
94
- if (options.shortcutMap) {
95
- const shortcuts = (_b = options.shortcutMap.get(current.x, current.y)) === null || _b === void 0 ? void 0 : _b.map(cell => ({
97
+ const shortcutCells = typeof options.shortcutMap == "function"
98
+ ? options.shortcutMap(current.x, current.y)
99
+ : (_b = options.shortcutMap) === null || _b === void 0 ? void 0 : _b.get(current.x, current.y);
100
+ if (shortcutCells) {
101
+ const shortcuts = shortcutCells.map(cell => ({
96
102
  traversalType: TraversalType.shortcut,
97
103
  cell
98
104
  }));
99
- if (shortcuts)
100
- neighbours.push(...shortcuts);
105
+ neighbours.push(...shortcuts);
101
106
  }
102
107
  neighbours.forEach(n => {
103
108
  var _a;
104
109
  if (visited.has(n.cell))
105
110
  return;
106
- const moveCost = getCost(n.cell, {
111
+ const moveCost = getCostFn(n.cell, {
107
112
  traversalType: n.traversalType,
108
113
  from: current,
109
114
  });
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.7",
2
+ "version": "0.0.8",
3
3
  "description": "2D store and utilities, with zero-copy regions and transform layers, pathfinding, Pipe2D interop",
4
4
  "name": "@xtia/grid",
5
5
  "exports": {