Advertisement
Guest User

scr_findPath

a guest
Feb 12th, 2018
1,457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. ///scr_findPath(startNode, endNode, ds_list<obj_node>, max range)
  2.  
  3. var startNode = argument0;
  4. var endNode = argument1;
  5. var pathNodes = argument2;
  6. var maxRange = argument3;
  7.  
  8. var openSet = ds_list_create(); //create open set
  9. var closedSet = ds_list_create(); //create closed set
  10.  
  11. ds_list_add(openSet,startNode); //add starting node to open set
  12.  
  13. //while there are nodes in the open set, do the algorithm
  14. //otherwise return false (path not found)
  15.  
  16. while(ds_list_size(openSet) > 0)
  17. {
  18. //here we get the node with the lowest F cost (G cost + H cost)
  19. //from all the nodes in the open set
  20. //and remove it from the open set and
  21. //add it to the closed set
  22. var currentNode = ds_list_find_value(openSet,0);
  23. var index = 0;
  24. for (var i=0;i<ds_list_size(openSet);++i)
  25. {
  26. var tmpNode = ds_list_find_value(openSet,i);
  27. if (tmpNode.fCost < currentNode.fCost)
  28. {
  29. currentNode = tmpNode;
  30. index = i;
  31. }
  32. }
  33. ds_list_delete(openSet,index);
  34. ds_list_add(closedSet,currentNode);
  35.  
  36. //if our current node (from the open set) is our end node
  37. //return true (path found) and retrace path from end to start
  38. //and add nodes to the list
  39. if (currentNode == endNode)
  40. {
  41. ds_list_destroy(openSet);
  42. ds_list_destroy(closedSet);
  43.  
  44. var currentNode = endNode;
  45.  
  46. while (currentNode != startNode)
  47. {
  48. ds_list_insert(pathNodes,0,currentNode);
  49. currentNode = currentNode.parent;
  50. }
  51. ds_list_insert(pathNodes,0,startNode);
  52. return true;
  53. }
  54.  
  55. //get neighbour nodes from current node
  56. var neighbours = scr_getNeighbours(currentNode,maxRange);
  57.  
  58. //for each neighbour
  59. for (var i=0;i<ds_list_size(neighbours);++i)
  60. {
  61. var neighbourNode = ds_list_find_value(neighbours,i);
  62.  
  63. //if the neighbour nodes is in the closed set
  64. //set the boolean closedSetContains to true, else false
  65. var closedSetContains = false;
  66. for (var j=0;j<ds_list_size(closedSet);++j)
  67. {
  68. var tmpNode = ds_list_find_value(closedSet,j);
  69. if (tmpNode == neighbourNode)
  70. {
  71. closedSetContains = true;
  72. break;
  73. }
  74. }
  75.  
  76. //if the neighbour node isn't in the closed set
  77. if (!closedSetContains)
  78. {
  79. //calculates new distance for the node
  80. var newMovementCost = currentNode.gCost + scr_getDistance(currentNode,neighbourNode);
  81.  
  82. //if the neighbour nodes is in the open set
  83. //set the boolean openSetContains to true, else false
  84. var openSetContains = false;
  85. for (var j=0;j<ds_list_size(openSet);++j)
  86. {
  87. var tmpNode = ds_list_find_value(openSet,j);
  88. if (tmpNode == neighbourNode)
  89. {
  90. openSetContains = true;
  91. break;
  92. }
  93. }
  94.  
  95. //if our new calculated movement cost is smaller than
  96. //the neighbour node G cost, OR the neighbour node is not
  97. //in the open set, then
  98. if (newMovementCost < neighbourNode.gCost || !openSetContains)
  99. {
  100. //recalculate all the costs
  101. neighbourNode.gCost = newMovementCost;
  102. neighbourNode.hCost = scr_getDistance(neighbourNode,endNode);
  103. neighbourNode.fCost = neighbourNode.gCost + neighbourNode.hCost;
  104.  
  105. //set the parent to our current node (so we can retrace later)
  106. neighbourNode.parent = currentNode;
  107.  
  108. //and finally, if the neighbour node is not in the open set
  109. //add it to the open set
  110. if (!openSetContains)
  111. ds_list_add(openSet,neighbourNode);
  112. }
  113. }
  114. }
  115.  
  116. //destroying temporary ds_list otherwise we will get a memory leak
  117. ds_list_destroy(neighbours);
  118. }
  119.  
  120. //destroying temporary ds_lists otherwise we will get a memory leak
  121. ds_list_destroy(openSet);
  122. ds_list_destroy(closedSet);
  123.  
  124. //if our code doesn't return in the while statement,
  125. //a path has not been found - return false;
  126. return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement