From b86173b7a6fe1a2859b10a6429b1c4a318bf0240 Mon Sep 17 00:00:00 2001 From: Mister Hat Date: Thu, 2 Jan 2020 14:06:33 -0600 Subject: [PATCH 1/5] add .setParallel(limit) when many varying path lengths are used, distributes the path finding per iterationsPerCalculation on up to limit paths. when calculate() is called, switch to a solving a different path in the queue instead of solving them sequentially. --- README.md | 15 ++++++++------ bin/easystar-0.4.3.js | 42 ++++++++++++++++++++++++++++++++++---- bin/easystar-0.4.3.min.js | 2 +- src/easystar.js | 43 +++++++++++++++++++++++++++++++++++---- 4 files changed, 87 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 074d82b..98216ba 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ * **Phaser:** see [Phaser Plugin](https://github.com/appsbu-de/phaser_plugin_pathfinding) * **Bower:** `bower install easystarjs` -## Description -easystar.js is an asynchronous A* pathfinding API written in Javascript for use in your HTML5 games and interactive projects. The goal of this project is to make it easy and fast to implement performance conscious pathfinding. +## Description +easystar.js is an asynchronous A\* pathfinding API written in Javascript for use in your HTML5 games and interactive projects. The goal of this project is to make it easy and fast to implement performance conscious pathfinding. ## Features @@ -73,6 +73,9 @@ var instanceId = easystar.findPath(startX, startY, endX, endY, callback); // ... easystar.cancelPath(instanceId); ``` +```javascript +easystar.setParallelLimit(limit); +``` ## Usage @@ -96,7 +99,7 @@ var grid = [[0,0,1,0,0], ``` Set our grid. -```javascript +```javascript easystar.setGrid(grid); ``` Set tiles which are "walkable". @@ -115,14 +118,14 @@ easystar.findPath(0, 0, 4, 0, function( path ) { }); ``` -EasyStar will not yet start calculating my path. +EasyStar will not yet start calculating my path. In order for EasyStar to actually start calculating, I must call the calculate() method. You should call `easystar.calculate()` on a ticker, or setInterval. -If you have a large grid, then it is possible that these calculations could slow down the browser. -For this reason, it might be a good idea to give EasyStar a smaller `iterationsPerCalculation` value via +If you have a large grid, then it is possible that these calculations could slow down the browser. +For this reason, it might be a good idea to give EasyStar a smaller `iterationsPerCalculation` value via ```javascript easystar.setIterationsPerCalculation(1000); diff --git a/bin/easystar-0.4.3.js b/bin/easystar-0.4.3.js index 5ccabb4..1919016 100644 --- a/bin/easystar-0.4.3.js +++ b/bin/easystar-0.4.3.js @@ -78,9 +78,11 @@ var EasyStar = var iterationsSoFar; var instances = {}; var instanceQueue = []; + var instanceQueueIndex = 0; var iterationsPerCalculation = Number.MAX_VALUE; var acceptableTiles; var diagonalsEnabled = false; + var parallelLimit; /** * Sets the collision grid that EasyStar uses. @@ -225,6 +227,19 @@ var EasyStar = iterationsPerCalculation = iterations; }; + /** + * Sets the limit of the path queue instance index, which is decremented per + * calculation if enabled. If multiple calls to .findPath() are made, this + * will distribute the load by running iterationsPerCalculation before + * moving onto the next path in the queue. + * + * @param {Number} limit The maximum number of iterations before resetting + * on calculate() call. Use -1 to run all the paths at once. + **/ + this.setParallelLimit = function (limit) { + parallelLimit = limit; + }; + /** * Avoid a particular point on the grid, * regardless of whether or not it is an acceptable tile. @@ -349,6 +364,7 @@ var EasyStar = var instanceId = nextInstanceId++; instances[instanceId] = instance; instanceQueue.push(instanceId); + return instanceId; }; @@ -378,6 +394,7 @@ var EasyStar = if (instanceQueue.length === 0 || collisionGrid === undefined || acceptableTiles === undefined) { return; } + for (iterationsSoFar = 0; iterationsSoFar < iterationsPerCalculation; iterationsSoFar++) { if (instanceQueue.length === 0) { return; @@ -388,11 +405,12 @@ var EasyStar = iterationsSoFar = 0; } - var instanceId = instanceQueue[0]; + var instanceId = instanceQueue[instanceQueueIndex]; var instance = instances[instanceId]; if (typeof instance == 'undefined') { // This instance was cancelled - instanceQueue.shift(); + instanceQueue.splice(instanceQueueIndex, 1); + updateQueueIndex(); continue; } @@ -400,7 +418,8 @@ var EasyStar = if (instance.openList.size() === 0) { instance.callback(null); delete instances[instanceId]; - instanceQueue.shift(); + instanceQueue.splice(instanceQueueIndex, 1); + updateQueueIndex(); continue; } @@ -419,7 +438,8 @@ var EasyStar = var ip = path; instance.callback(ip); delete instances[instanceId]; - instanceQueue.shift(); + instanceQueue.splice(instanceQueueIndex, 1); + updateQueueIndex(); continue; } @@ -468,6 +488,8 @@ var EasyStar = } } } + + updateQueueIndex(); }; // Private methods follow @@ -561,6 +583,18 @@ var EasyStar = return dx + dy; } }; + + var updateQueueIndex = function () { + if (!parallelLimit || !instanceQueue.length) { + return; + } + + instanceQueueIndex--; + + if (instanceQueueIndex < 0 || instanceQueueIndex > instanceQueue.length) { + instanceQueueIndex = (instanceQueue.length - 1) % (parallelLimit === -1 ? instanceQueue.length : parallelLimit); + } + }; }; EasyStar.TOP = 'TOP'; diff --git a/bin/easystar-0.4.3.min.js b/bin/easystar-0.4.3.min.js index 164e752..d95e61c 100644 --- a/bin/easystar-0.4.3.min.js +++ b/bin/easystar-0.4.3.min.js @@ -1 +1 @@ -var EasyStar=function(t){function n(o){if(e[o])return e[o].exports;var r=e[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,n),r.loaded=!0,r.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){var o={},r=e(1),i=e(2),s=e(3);const u=0,a=1;t.exports=o;var l=1;o.js=function(){var t,n,e,c=1,f=1.4,h=!1,p={},d={},v={},y={},T=!0,g={},x=[],O=Number.MAX_VALUE,b=!1;this.setAcceptableTiles=function(t){t instanceof Array?e=t:!isNaN(parseFloat(t))&&isFinite(t)&&(e=[t])},this.enableSync=function(){h=!0},this.disableSync=function(){h=!1},this.enableDiagonals=function(){b=!0},this.disableDiagonals=function(){b=!1},this.setGrid=function(n){t=n;for(var e=0;et[0].length-1||o>t.length-1||i>t[0].length-1||u>t.length-1)throw new Error("Your start or end point is outside the scope of your grid.");if(n===i&&o===u)return void f([]);for(var p=t[u][i],d=!1,v=0;v0&&m(r,i,0,-1,c*F(i.x,i.y-1)),i.x0&&m(r,i,-1,0,c*F(i.x-1,i.y)),b&&(i.x>0&&i.y>0&&(T||A(t,e,i.x,i.y-1,i)&&A(t,e,i.x-1,i.y,i))&&m(r,i,-1,-1,f*F(i.x-1,i.y-1)),i.x0&&(T||A(t,e,i.x,i.y-1,i)&&A(t,e,i.x+1,i.y,i))&&m(r,i,1,-1,f*F(i.x+1,i.y-1)),i.x>0&&i.yn?1:0},p=function(t,n,e,o,r){var i;if(null==e&&(e=0),null==r&&(r=s),e<0)throw new Error("lo must be non-negative");for(null==o&&(o=t.length);ee;0<=e?n++:n--)l.push(n);return l}.apply(this).reverse(),a=[],o=0,r=i.length;oy;r=0<=y?++f:--f)T.push(l(t,e));return T},g=function(t,n,e,o){var r,i,u;for(null==o&&(o=s),r=t[e];e>n&&(u=e-1>>1,i=t[u],o(r,i)<0);)t[e]=i,e=u;return t[e]=r},x=function(t,n,e){var o,r,i,u,a;for(null==e&&(e=s),r=t.length,a=n,i=t[n],o=2*n+1;ot[0].length-1||o>t.length-1||r>t[0].length-1||u>t.length-1)throw new Error("Your start or end point is outside the scope of your grid.");if(n===r&&o===u)return void c([]);for(var f=t[u][r],d=!1,v=0;v0&&A(i,r,0,-1,h*L(r.x,r.y-1)),r.x0&&A(i,r,-1,0,h*L(r.x-1,r.y)),P&&(r.x>0&&r.y>0&&(g||E(t,e,r.x,r.y-1,r)&&E(t,e,r.x-1,r.y,r))&&A(i,r,-1,-1,f*L(r.x-1,r.y-1)),r.x0&&(g||E(t,e,r.x,r.y-1,r)&&E(t,e,r.x+1,r.y,r))&&A(i,r,1,-1,f*L(r.x+1,r.y-1)),r.x>0&&r.yO.length)&&(m=(O.length-1)%(c===-1?O.length:c)))}},o.TOP="TOP",o.TOP_RIGHT="TOP_RIGHT",o.RIGHT="RIGHT",o.BOTTOM_RIGHT="BOTTOM_RIGHT",o.BOTTOM="BOTTOM",o.BOTTOM_LEFT="BOTTOM_LEFT",o.LEFT="LEFT",o.TOP_LEFT="TOP_LEFT"},function(t,n){t.exports=function(){this.pointsToAvoid={},this.startX,this.callback,this.startY,this.endX,this.endY,this.nodeHash={},this.openList}},function(t,n){t.exports=function(t,n,e,o,i){this.parent=t,this.x=n,this.y=e,this.costSoFar=o,this.simpleDistanceToTarget=i,this.bestGuessDistance=function(){return this.costSoFar+this.simpleDistanceToTarget}}},function(t,n,e){t.exports=e(4)},function(t,n,e){var o,i,r;(function(){var e,s,u,l,a,c,h,f,p,d,v,y,T,g,x;u=Math.floor,d=Math.min,s=function(t,n){return tn?1:0},p=function(t,n,e,o,i){var r;if(null==e&&(e=0),null==i&&(i=s),e<0)throw new Error("lo must be non-negative");for(null==o&&(o=t.length);ee;0<=e?n++:n--)a.push(n);return a}.apply(this).reverse(),l=[],o=0,i=r.length;oy;i=0<=y?++h:--h)T.push(a(t,e));return T},g=function(t,n,e,o){var i,r,u;for(null==o&&(o=s),i=t[e];e>n&&(u=e-1>>1,r=t[u],o(i,r)<0);)t[e]=r,e=u;return t[e]=i},x=function(t,n,e){var o,i,r,u,l;for(null==e&&(e=s),i=t.length,l=n,r=t[n],o=2*n+1;o instanceQueue.length) { + instanceQueueIndex = (instanceQueue.length - 1) % + (parallelLimit === -1 ? instanceQueue.length : parallelLimit); + } + }; } EasyStar.TOP = 'TOP' From d87a2521ad89b13d23710a7c71e59cd5c7643523 Mon Sep 17 00:00:00 2001 From: Mister Hat Date: Thu, 2 Jan 2020 14:21:25 -0600 Subject: [PATCH 2/5] update typescript bindings --- index.d.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/index.d.ts b/index.d.ts index a31b48e..9ead2ef 100644 --- a/index.d.ts +++ b/index.d.ts @@ -90,6 +90,17 @@ export class js { */ setIterationsPerCalculation(iterations: number): void + /** + * Sets the limit of the path queue instance index, which is decremented per + * calculation if enabled. If multiple calls to .findPath() are made, this + * will distribute the load by running iterationsPerCalculation before + * moving onto the next path in the queue. + * + * @param {Number} limit The maximum number of iterations before resetting + * on calculate() call. Use -1 to run all the paths at once. + */ + setParallelLimit(limit: number): void + /** * Avoid a particular point on the grid, * regardless of whether or not it is an acceptable tile. From 6192e9001720ec4e53b71fd0f86529947fe25781 Mon Sep 17 00:00:00 2001 From: Mister Hat Date: Sun, 5 Jan 2020 13:48:16 -0600 Subject: [PATCH 3/5] replace original bin files --- bin/easystar-0.4.3.js | 42 ++++----------------------------------- bin/easystar-0.4.3.min.js | 2 +- 2 files changed, 5 insertions(+), 39 deletions(-) diff --git a/bin/easystar-0.4.3.js b/bin/easystar-0.4.3.js index 1919016..5ccabb4 100644 --- a/bin/easystar-0.4.3.js +++ b/bin/easystar-0.4.3.js @@ -78,11 +78,9 @@ var EasyStar = var iterationsSoFar; var instances = {}; var instanceQueue = []; - var instanceQueueIndex = 0; var iterationsPerCalculation = Number.MAX_VALUE; var acceptableTiles; var diagonalsEnabled = false; - var parallelLimit; /** * Sets the collision grid that EasyStar uses. @@ -227,19 +225,6 @@ var EasyStar = iterationsPerCalculation = iterations; }; - /** - * Sets the limit of the path queue instance index, which is decremented per - * calculation if enabled. If multiple calls to .findPath() are made, this - * will distribute the load by running iterationsPerCalculation before - * moving onto the next path in the queue. - * - * @param {Number} limit The maximum number of iterations before resetting - * on calculate() call. Use -1 to run all the paths at once. - **/ - this.setParallelLimit = function (limit) { - parallelLimit = limit; - }; - /** * Avoid a particular point on the grid, * regardless of whether or not it is an acceptable tile. @@ -364,7 +349,6 @@ var EasyStar = var instanceId = nextInstanceId++; instances[instanceId] = instance; instanceQueue.push(instanceId); - return instanceId; }; @@ -394,7 +378,6 @@ var EasyStar = if (instanceQueue.length === 0 || collisionGrid === undefined || acceptableTiles === undefined) { return; } - for (iterationsSoFar = 0; iterationsSoFar < iterationsPerCalculation; iterationsSoFar++) { if (instanceQueue.length === 0) { return; @@ -405,12 +388,11 @@ var EasyStar = iterationsSoFar = 0; } - var instanceId = instanceQueue[instanceQueueIndex]; + var instanceId = instanceQueue[0]; var instance = instances[instanceId]; if (typeof instance == 'undefined') { // This instance was cancelled - instanceQueue.splice(instanceQueueIndex, 1); - updateQueueIndex(); + instanceQueue.shift(); continue; } @@ -418,8 +400,7 @@ var EasyStar = if (instance.openList.size() === 0) { instance.callback(null); delete instances[instanceId]; - instanceQueue.splice(instanceQueueIndex, 1); - updateQueueIndex(); + instanceQueue.shift(); continue; } @@ -438,8 +419,7 @@ var EasyStar = var ip = path; instance.callback(ip); delete instances[instanceId]; - instanceQueue.splice(instanceQueueIndex, 1); - updateQueueIndex(); + instanceQueue.shift(); continue; } @@ -488,8 +468,6 @@ var EasyStar = } } } - - updateQueueIndex(); }; // Private methods follow @@ -583,18 +561,6 @@ var EasyStar = return dx + dy; } }; - - var updateQueueIndex = function () { - if (!parallelLimit || !instanceQueue.length) { - return; - } - - instanceQueueIndex--; - - if (instanceQueueIndex < 0 || instanceQueueIndex > instanceQueue.length) { - instanceQueueIndex = (instanceQueue.length - 1) % (parallelLimit === -1 ? instanceQueue.length : parallelLimit); - } - }; }; EasyStar.TOP = 'TOP'; diff --git a/bin/easystar-0.4.3.min.js b/bin/easystar-0.4.3.min.js index d95e61c..164e752 100644 --- a/bin/easystar-0.4.3.min.js +++ b/bin/easystar-0.4.3.min.js @@ -1 +1 @@ -var EasyStar=function(t){function n(o){if(e[o])return e[o].exports;var i=e[o]={exports:{},id:o,loaded:!1};return t[o].call(i.exports,i,i.exports,n),i.loaded=!0,i.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){var o={},i=e(1),r=e(2),s=e(3);const u=0,l=1;t.exports=o;var a=1;o.js=function(){var t,n,e,c,h=1,f=1.4,p=!1,d={},v={},y={},T={},g=!0,x={},O=[],m=0,b=Number.MAX_VALUE,P=!1;this.setAcceptableTiles=function(t){t instanceof Array?e=t:!isNaN(parseFloat(t))&&isFinite(t)&&(e=[t])},this.enableSync=function(){p=!0},this.disableSync=function(){p=!1},this.enableDiagonals=function(){P=!0},this.disableDiagonals=function(){P=!1},this.setGrid=function(n){t=n;for(var e=0;et[0].length-1||o>t.length-1||r>t[0].length-1||u>t.length-1)throw new Error("Your start or end point is outside the scope of your grid.");if(n===r&&o===u)return void c([]);for(var f=t[u][r],d=!1,v=0;v0&&A(i,r,0,-1,h*L(r.x,r.y-1)),r.x0&&A(i,r,-1,0,h*L(r.x-1,r.y)),P&&(r.x>0&&r.y>0&&(g||E(t,e,r.x,r.y-1,r)&&E(t,e,r.x-1,r.y,r))&&A(i,r,-1,-1,f*L(r.x-1,r.y-1)),r.x0&&(g||E(t,e,r.x,r.y-1,r)&&E(t,e,r.x+1,r.y,r))&&A(i,r,1,-1,f*L(r.x+1,r.y-1)),r.x>0&&r.yO.length)&&(m=(O.length-1)%(c===-1?O.length:c)))}},o.TOP="TOP",o.TOP_RIGHT="TOP_RIGHT",o.RIGHT="RIGHT",o.BOTTOM_RIGHT="BOTTOM_RIGHT",o.BOTTOM="BOTTOM",o.BOTTOM_LEFT="BOTTOM_LEFT",o.LEFT="LEFT",o.TOP_LEFT="TOP_LEFT"},function(t,n){t.exports=function(){this.pointsToAvoid={},this.startX,this.callback,this.startY,this.endX,this.endY,this.nodeHash={},this.openList}},function(t,n){t.exports=function(t,n,e,o,i){this.parent=t,this.x=n,this.y=e,this.costSoFar=o,this.simpleDistanceToTarget=i,this.bestGuessDistance=function(){return this.costSoFar+this.simpleDistanceToTarget}}},function(t,n,e){t.exports=e(4)},function(t,n,e){var o,i,r;(function(){var e,s,u,l,a,c,h,f,p,d,v,y,T,g,x;u=Math.floor,d=Math.min,s=function(t,n){return tn?1:0},p=function(t,n,e,o,i){var r;if(null==e&&(e=0),null==i&&(i=s),e<0)throw new Error("lo must be non-negative");for(null==o&&(o=t.length);ee;0<=e?n++:n--)a.push(n);return a}.apply(this).reverse(),l=[],o=0,i=r.length;oy;i=0<=y?++h:--h)T.push(a(t,e));return T},g=function(t,n,e,o){var i,r,u;for(null==o&&(o=s),i=t[e];e>n&&(u=e-1>>1,r=t[u],o(i,r)<0);)t[e]=r,e=u;return t[e]=i},x=function(t,n,e){var o,i,r,u,l;for(null==e&&(e=s),i=t.length,l=n,r=t[n],o=2*n+1;ot[0].length-1||o>t.length-1||i>t[0].length-1||u>t.length-1)throw new Error("Your start or end point is outside the scope of your grid.");if(n===i&&o===u)return void f([]);for(var p=t[u][i],d=!1,v=0;v0&&m(r,i,0,-1,c*F(i.x,i.y-1)),i.x0&&m(r,i,-1,0,c*F(i.x-1,i.y)),b&&(i.x>0&&i.y>0&&(T||A(t,e,i.x,i.y-1,i)&&A(t,e,i.x-1,i.y,i))&&m(r,i,-1,-1,f*F(i.x-1,i.y-1)),i.x0&&(T||A(t,e,i.x,i.y-1,i)&&A(t,e,i.x+1,i.y,i))&&m(r,i,1,-1,f*F(i.x+1,i.y-1)),i.x>0&&i.yn?1:0},p=function(t,n,e,o,r){var i;if(null==e&&(e=0),null==r&&(r=s),e<0)throw new Error("lo must be non-negative");for(null==o&&(o=t.length);ee;0<=e?n++:n--)l.push(n);return l}.apply(this).reverse(),a=[],o=0,r=i.length;oy;r=0<=y?++f:--f)T.push(l(t,e));return T},g=function(t,n,e,o){var r,i,u;for(null==o&&(o=s),r=t[e];e>n&&(u=e-1>>1,i=t[u],o(r,i)<0);)t[e]=i,e=u;return t[e]=r},x=function(t,n,e){var o,r,i,u,a;for(null==e&&(e=s),r=t.length,a=n,i=t[n],o=2*n+1;o Date: Mon, 6 Jan 2020 09:55:10 -0600 Subject: [PATCH 4/5] fix issues in PR review * remove parallelLimit and replace it with binary parallel compute option * added unit tests for serial and parallel path computing --- index.d.ts | 25 ++++++++------ src/easystar.js | 40 ++++++++++++---------- test/easystartest.js | 80 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 29 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9ead2ef..4efa09f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -41,6 +41,20 @@ export class js { */ disableDiagonals(): void + /** + * Enables parallel path computing. + * + * If multiple calls to .findPath() are made, this + * will distribute the load by running iterationsPerCalculation before + * moving onto the next path in the queue. + **/ + enableParallelCompute(): void + + /** + * Disables parallel path computing. + */ + disableParallelCompute(): void + /** * Sets the collision grid that EasyStar uses. * @@ -90,17 +104,6 @@ export class js { */ setIterationsPerCalculation(iterations: number): void - /** - * Sets the limit of the path queue instance index, which is decremented per - * calculation if enabled. If multiple calls to .findPath() are made, this - * will distribute the load by running iterationsPerCalculation before - * moving onto the next path in the queue. - * - * @param {Number} limit The maximum number of iterations before resetting - * on calculate() call. Use -1 to run all the paths at once. - */ - setParallelLimit(limit: number): void - /** * Avoid a particular point on the grid, * regardless of whether or not it is an acceptable tile. diff --git a/src/easystar.js b/src/easystar.js index 1cc3368..dd7eff6 100755 --- a/src/easystar.js +++ b/src/easystar.js @@ -35,7 +35,7 @@ EasyStar.js = function() { var iterationsPerCalculation = Number.MAX_VALUE; var acceptableTiles; var diagonalsEnabled = false; - var parallelLimit; + var parallelEnabled = false; /** * Sets the collision grid that EasyStar uses. @@ -69,6 +69,24 @@ EasyStar.js = function() { syncEnabled = false; }; + /** + * Enables parallel path computing. + * + * If multiple calls to .findPath() are made, this + * will distribute the load by running iterationsPerCalculation before + * moving onto the next path in the queue. + **/ + this.enableParallelCompute = function() { + parallelEnabled = true; + }; + + /** + * Disables parallel path computing. + **/ + this.disableParallelCompute = function() { + parallelEnabled = false; + }; + /** * Enable diagonal pathfinding. */ @@ -180,19 +198,6 @@ EasyStar.js = function() { iterationsPerCalculation = iterations; }; - /** - * Sets the limit of the path queue instance index, which is decremented per - * calculation if enabled. If multiple calls to .findPath() are made, this - * will distribute the load by running iterationsPerCalculation before - * moving onto the next path in the queue. - * - * @param {Number} limit The maximum number of iterations before resetting - * on calculate() call. Use -1 to run all the paths at once. - **/ - this.setParallelLimit = function(limit) { - parallelLimit = limit; - }; - /** * Avoid a particular point on the grid, * regardless of whether or not it is an acceptable tile. @@ -568,15 +573,14 @@ EasyStar.js = function() { }; var updateQueueIndex = function () { - if (!parallelLimit || !instanceQueue.length) { + if (!parallelEnabled || !instanceQueue.length) { return; } instanceQueueIndex--; - if (instanceQueueIndex < 0 || instanceQueueIndex > instanceQueue.length) { - instanceQueueIndex = (instanceQueue.length - 1) % - (parallelLimit === -1 ? instanceQueue.length : parallelLimit); + if (instanceQueueIndex < 0) { + instanceQueueIndex = instanceQueue.length - 1; } }; } diff --git a/test/easystartest.js b/test/easystartest.js index f932812..29f73e0 100644 --- a/test/easystartest.js +++ b/test/easystartest.js @@ -346,4 +346,84 @@ describe("EasyStar.js", function() { easyStar.calculate(); }) + + it("It should handle computing paths in serial", function (done) { + var easyStar = new EasyStar.js(); + var map = [[1,1,0,1,1], + [1,1,0,1,1], + [1,1,0,1,1], + [1,1,1,1,1], + [1,1,1,1,1]]; + + easyStar.setGrid(map); + easyStar.setAcceptableTiles([1]); + easyStar.setIterationsPerCalculation(2); + easyStar.disableParallelCompute(); + + var iteration = 0; + // iteration each path was found on + var firstIteration = -1; + var secondIteration = -1; + var maxIterations = 8; + + easyStar.findPath(1, 2, 3, 2, () => { + firstIteration = iteration; + }); + + easyStar.findPath(1, 2, 3, 2, () => { + secondIteration = iteration; + expect(firstIteration).toBeLessThan(secondIteration); + done(); + }); + + function iterate() { + if (iteration < maxIterations) { + easyStar.calculate(); + iteration += 1; + setTimeout(iterate, 1); + } + } + + iterate(); + }) + + it("It should handle computing paths in parallel", function (done) { + var easyStar = new EasyStar.js(); + var map = [[1,1,0,1,1], + [1,1,0,1,1], + [1,1,0,1,1], + [1,1,1,1,1], + [1,1,1,1,1]]; + + easyStar.setGrid(map); + easyStar.setAcceptableTiles([1]); + easyStar.setIterationsPerCalculation(2); + easyStar.enableParallelCompute(); + + var iteration = 0; + // iteration each path was found on + var firstIteration = -1; + var secondIteration = -1; + var maxIterations = 8; + + easyStar.findPath(1, 2, 3, 2, () => { + firstIteration = iteration; + }); + + easyStar.findPath(1, 2, 3, 2, () => { + secondIteration = iteration; + expect(firstIteration).toEqual(secondIteration); + done(); + }); + + function iterate() { + if (iteration < maxIterations) { + easyStar.calculate(); + iteration += 1; + setTimeout(iterate, 1); + } + } + + iterate(); + }) }); From aac92711c6a9431c7cb2e82f4e690449bd778434 Mon Sep 17 00:00:00 2001 From: Mister Hat Date: Mon, 6 Jan 2020 10:02:57 -0600 Subject: [PATCH 5/5] update readme api --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 98216ba..ca4ca5a 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ var instanceId = easystar.findPath(startX, startY, endX, endY, callback); easystar.cancelPath(instanceId); ``` ```javascript -easystar.setParallelLimit(limit); +easystar.enableParallelCompute(); ``` ## Usage