SHARE
TWEET

Drakor Direction patch

a guest Oct 18th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let compass = ['North', 'East', 'South', 'West'];
  2. function calculateDirections(current, ending) {
  3.   let queue = [];
  4.   let reversePath = [];
  5.   let step = {};
  6.   step[current] = [];
  7.   queue.push(current);
  8.   while (queue.length > 0 && !pathFound) {
  9.     current = queue.shift();
  10.     for (let index in compass) {
  11.       let neighbour = mapNodes[current][compass[index]];
  12.       if (neighbour.length > 0 && !step[neighbour]) {
  13.         step[neighbour] = [compass[index], current];
  14.         if (neighbour == ending) {
  15.           pathFound = true;
  16.           break;
  17.         }
  18.         queue.push(neighbour);
  19.       }
  20.     }
  21.   }
  22.   if (pathFound) {
  23.     current = ending;
  24.     let currentStep;
  25.     while ((currentStep = step[current]) && currentStep.length > 0) {
  26.       let previous = currentStep[1];
  27.       currentStep[1] = current;
  28.       reversePath.push(currentStep);
  29.       current = previous;
  30.     }
  31.     while (reversePath.length > 0) {
  32.       currentPath.push(reversePath.pop());
  33.     }
  34.   }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top