prettymuchbryce / easystarjs

An asynchronous A* pathfinding API written in Javascript.
MIT License
1.9k stars 165 forks source link

Adds option to 'find nearest' when target tile is unacceptable #95

Open Jiminibob opened 3 years ago

Jiminibob commented 3 years ago

Not sure if this is the most elegant approach, but is my attempt at 'Moving within range of target'

https://github.com/prettymuchbryce/easystarjs/issues/75

aJamDonut commented 3 years ago

Not sure if this is the most elegant approach, but is my attempt at 'Moving within range of target'

75

you legend, i was literally just about to write this... testing now

aJamDonut commented 3 years ago

Sadly I find several performance issues with the solution, due to the fact it essentially loops over the entire node list multiple times during its calculation. It would be better if this could calculate the check during the first calculate and return early or something similar.

image

This sort fuction and the following loop are causing the issue.

`instance.storeList.sort(function(a, b){ if( a.simpleDistanceToTarget === b.simpleDistanceToTarget) return 0; return a.simpleDistanceToTarget < b.simpleDistanceToTarget ? -1 :1 }) // loop through and find best result var closestDist = instance.storeList[0].simpleDistanceToTarget; var targetNode, targetCost; for(var i = 0; i < instance.storeList.length; i++){

                    if(instance.storeList[i].simpleDistanceToTarget > closestDist ) break;

                    if(!targetNode || targetCost > instance.storeList[i].costSoFar){
                        targetNode = instance.storeList[i];
                        targetCost = instance.storeList[i].costSoFar;
                    }
                }

`

Jiminibob commented 3 years ago

yah, that's not pretty :/

Updated to check 'nearest' during the main look up and cache it ( kills a few of those loops ). See if that feels better

nithiz commented 3 years ago

@Jiminibob Great stuff! Sadly the execution still takes about 500ms. Have you been able to make progress on the performance?

aJamDonut commented 11 months ago

sorry for the old response, i wrote it later down the line and forgot about this.

commit here, if anyone wants it i might make a pr

https://github.com/aJamDonut/CustomStar/commit/ae4e8fdb7e23bd39049a06df1092da55ee4ce3ad

aJamDonut commented 11 months ago

it's been in use for a while now no issues