Skip to content

Commit 0f0153a

Browse files
commit changes
1 parent edd0646 commit 0f0153a

File tree

11 files changed

+97
-50
lines changed

11 files changed

+97
-50
lines changed

Diff for: .github/workflows/learn-github-actions.yml

-13
This file was deleted.

Diff for: .github/workflows/publish-package-to-npmjs.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Publish Package to npmjs
2+
run-name: ${{ github.actor }} published a new Package to npmjs
3+
on:
4+
release:
5+
types: [published]
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
# Setup .npmrc file to publish to npm
12+
- uses: actions/setup-node@v3
13+
with:
14+
node-version: '16.x'
15+
registry-url: 'https://registry.npmjs.org'
16+
- run: npm ci
17+
- run: npm publish
18+
env:
19+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

Diff for: CHANGELOG.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1414

1515
---
1616

17-
## [1.X.X] - 2023-XX-XX
17+
## [1.2.7] - 2023-04-26
18+
19+
### Added
20+
21+
- Implement `Get as close as possible` option when the path is blocked ([Issue #10, @sefabaser](https://github.com/digitsensitive/astar-typescript/issues/10))
1822

1923
### Changed
2024

2125
- Update most dependencies in `./` and `./example`
22-
- Update Copyright to 2013 in different locations
23-
- Small optimizations in `grid.ts`
26+
- Update Copyright to 2023 in different locations
27+
- Small optimizations in `grid.ts` and `util.ts`
28+
- Add `CopyPlugin` and `HtmlWebpackPlugin` to webpack
2429

2530
---
2631

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ let myPathway = this.aStarInstance.findPath(startPos, goalPos);
131131

132132
Additional parameters may be passed to adapt your finder.
133133

134+
### Diagonal movements
135+
134136
If you want to disable `diagonal movements`:
135137

136138
```ts
@@ -143,6 +145,8 @@ this.aStarInstance = new AStarFinder({
143145
});
144146
```
145147

148+
### Heuristic function
149+
146150
Set the `heuristic function` (Manhattan, Euclidean, Chebyshev or Octile):
147151

148152
```ts
@@ -172,6 +176,8 @@ this.aStarInstance = new AStarFinder({
172176
});
173177
```
174178

179+
### Start and End Node
180+
175181
Include or Exclude the `start and end node`:
176182

177183
```ts
@@ -185,6 +191,18 @@ this.aStarInstance = new AStarFinder({
185191
});
186192
```
187193

194+
### Allow path as close as possible
195+
196+
```ts
197+
this.aStarInstance = new AStarFinder({
198+
grid: {
199+
width: 8,
200+
height: 8
201+
},
202+
allowPathAsCloseAsPossible: true
203+
});
204+
```
205+
188206
## Prettier
189207

190208
This library uses [Prettier](https://github.com/prettier/prettier).

Diff for: example/src/scenes/main-scene.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @license {@link https://opensource.org/licenses/MIT|MIT License}
66
*/
77

8-
import { AStarFinder } from '../../../lib/astar';
8+
import { AStarFinder } from '../../../dist/astar';
99
import { DatGuiService } from '../services/dat-gui.service';
1010
import { GameObject } from '../objects/gameobject';
1111

Diff for: lib/core/grid.ts

+25-21
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ export class Grid {
123123

124124
/**
125125
* Get surrounding nodes.
126-
* @param currentXPos [x-position on the grid]
127-
* @param currentYPos [y-position on the grid]
126+
* @param currentPosition [IPoint on the grid]
128127
* @param diagnonalMovementAllowed [is diagnonal movement allowed?]
129128
*/
130129
public getSurroundingNodes(
@@ -140,20 +139,24 @@ export class Grid {
140139

141140
for (let y = minY; y <= maxY; y++) {
142141
for (let x = minX; x <= maxX; x++) {
143-
if (this.isOnTheGrid({ x, y })) {
144-
if (this.isWalkableAt({ x, y })) {
145-
if (diagnonalMovementAllowed) {
146-
surroundingNodes.push(this.getNodeAt({ x, y }));
147-
} else {
148-
if (x == currentPosition.x || y == currentPosition.y) {
149-
surroundingNodes.push(this.getNodeAt({ x, y }));
150-
}
142+
// Evaluate if NOT current position
143+
if (x !== currentPosition.x || y !== currentPosition.y) {
144+
// Evaluate if current position is on the grid AND walkable
145+
if (
146+
this.isOnTheGrid({ x: x, y: y }) &&
147+
this.isWalkableAt({ x: x, y: y })
148+
) {
149+
// Add node
150+
// if diagonal movement is allowed OR
151+
// if the node lies on the cross through the center node
152+
if (
153+
diagnonalMovementAllowed ||
154+
x == currentPosition.x ||
155+
y == currentPosition.y
156+
) {
157+
surroundingNodes.push(this.getNodeAt({ x: x, y: y }));
151158
}
152-
} else {
153-
continue;
154159
}
155-
} else {
156-
continue;
157160
}
158161
}
159162
}
@@ -190,21 +193,22 @@ export class Grid {
190193
* Get a clone of the grid
191194
*/
192195
public clone(): Node[][] {
193-
const cloneGrid: Node[][] = [];
194-
let id: number = 0;
196+
const clonedGrid: Node[][] = [];
197+
let nodeId: number = 0;
195198

196199
for (let y = 0; y < this.height; y++) {
197-
cloneGrid[y] = [];
200+
clonedGrid[y] = [];
198201
for (let x = 0; x < this.width; x++) {
199-
cloneGrid[y][x] = new Node({
200-
id: id,
202+
clonedGrid[y][x] = new Node({
203+
id: nodeId,
201204
position: { x: x, y: y },
202205
walkable: this.gridNodes[y][x].getIsWalkable()
203206
});
204207

205-
id++;
208+
nodeId++;
206209
}
207210
}
208-
return cloneGrid;
211+
212+
return clonedGrid;
209213
}
210214
}

Diff for: lib/core/heuristic.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* https://github.com/riscy/a_star_on_grids#heuristics
99
*/
1010

11+
import { IPoint } from '../interfaces/astar.interfaces';
1112
import { Heuristic } from '../types/astar.types';
1213

1314
/**
@@ -19,9 +20,9 @@ import { Heuristic } from '../types/astar.types';
1920
*/
2021
export function calculateHeuristic(
2122
heuristicFunction: Heuristic,
22-
pos0,
23-
pos1,
24-
weight
23+
pos0: IPoint,
24+
pos1: IPoint,
25+
weight: number
2526
): number {
2627
const dx = Math.abs(pos1.x - pos0.x);
2728
const dy = Math.abs(pos1.y - pos0.y);

Diff for: lib/core/util.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ export function backtrace(
1313
// Init empty path
1414
const path: number[][] = [];
1515

16-
let currentNode: Node;
17-
if (includeEndNode) {
18-
// Attach the end node to be the current node
19-
currentNode = node;
20-
} else {
21-
currentNode = node.getParent();
22-
}
16+
// If `includeEndNode` is enabled, attach the end node to be the current node
17+
let currentNode: Node = includeEndNode ? node : node.getParent();
2318

2419
// Loop as long the current node has a parent
2520
while (currentNode.getParent()) {

Diff for: lib/finders/astar-finder.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class AStarFinder {
1919
private openList: Node[];
2020

2121
// Pathway variables
22+
private allowPathAsCloseAsPossible: boolean;
2223
readonly diagonalAllowed: boolean;
2324
private heuristic: Heuristic;
2425
readonly includeStartNode: boolean;
@@ -53,6 +54,10 @@ export class AStarFinder {
5354
this.includeEndNode =
5455
aParams.includeEndNode !== undefined ? aParams.includeEndNode : true;
5556

57+
// Default path as close as possible not allowed
58+
this.allowPathAsCloseAsPossible =
59+
aParams.allowPathAsCloseAsPossible || false;
60+
5661
// Set weight
5762
this.weight = aParams.weight || 1;
5863
}
@@ -170,7 +175,19 @@ export class AStarFinder {
170175
}
171176
}
172177
}
173-
// Path could not be created
178+
179+
// At this point the path to the end position could NOT be created
180+
181+
// Return path as close as possible if enabled
182+
if (this.allowPathAsCloseAsPossible) {
183+
return backtrace(
184+
this.closedList[this.closedList.length - 1],
185+
this.includeStartNode,
186+
false
187+
);
188+
}
189+
190+
// Return empty path, because could NOT be created
174191
return [];
175192
}
176193

Diff for: lib/interfaces/astar.interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface IAStarFinderConstructor {
77
weight?: number;
88
includeStartNode?: boolean;
99
includeEndNode?: boolean;
10+
allowPathAsCloseAsPossible?: boolean;
1011
}
1112

1213
export interface IGridConstructor {

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "astar-typescript",
3-
"version": "1.2.6",
3+
"version": "1.2.7",
44
"description": "A*(a-star) pathfinding in TypeScript",
55
"keywords": [
66
"A-star",

0 commit comments

Comments
 (0)