Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. getShortestPath(SourceNode, TargetNode): ds_list
  3. Computes the shortest path between the two nodes.
  4. Returns a ds_list containing all the nodes in the path (first and last are included)
  5. If a path could not be found, noone is returned
  6. */
  7.  
  8. var Source, Target, OpenedList, ClosedList, PreviousNodes, TentativeDistances, CurrentNode, End, Connections;
  9. var tentativeDistance, Score, heuristicValue, lastConnection, currentScore;
  10.  
  11. Source = argument[0];
  12. Target = argument[1];
  13.  
  14. OpenedList = ds_priority_create(); //{Value: Node, Priority: Score}
  15. ClosedList = ds_list_create();
  16. PreviousNodes = ds_map_create();//{Key: Node, Value: PreviousNode}
  17. TentativeDistances = ds_map_create(); //{Key: Node, Value: Distance}
  18.  
  19. CurrentNode = noone;
  20. ds_priority_add(OpenedList, Source, 1);
  21. ds_map_add(TentativeDistances, Source, 0);
  22.  
  23. while (ds_priority_size(OpenedList) > 0) {
  24.     CurrentNode = ds_priority_find_min(OpenedList);
  25.  
  26.     if (CurrentNode == Target) {
  27.         break;
  28.     }
  29.  
  30.     ds_priority_delete_min(OpenedList); //Remove current node
  31.     ds_list_add(ClosedList, CurrentNode);
  32.    
  33.     Connections = __Nodes[CurrentNode, 1];
  34.     End = false;
  35.     lastConnection = ds_map_find_last(Connections);
  36.     for (var Neighbour = ds_map_find_first(Connections); End == false;
  37.         Neighbour = ds_map_find_next(Connections, Neighbour)) {
  38.         if (Neighbour == lastConnection) {
  39.             End = true;
  40.         }
  41.        
  42.         if (ds_list_find_index(ClosedList, Neighbour) != -1) {
  43.             continue;
  44.         }
  45.        
  46.         if (!filterPath(id, Neighbour)) {
  47.             continue;
  48.         }
  49.        
  50.         tentativeDistance = ds_map_find_value(Connections, Neighbour) + ds_map_find_value(TentativeDistances, CurrentNode);
  51.         heuristicValue = getHeuristic(Neighbour, Target);
  52.        
  53.         if (!ds_map_exists(TentativeDistances, Neighbour)) {
  54.             ds_map_add(TentativeDistances, Neighbour, tentativeDistance);
  55.         } else if (tentativeDistance < ds_map_find_value(TentativeDistances, Neighbour)) {
  56.             ds_map_replace(TentativeDistances, Neighbour, tentativeDistance);
  57.         }
  58.        
  59.         currentScore = ds_priority_find_priority(OpenedList, Neighbour);
  60.         Score = ds_map_find_value(TentativeDistances, Neighbour) + heuristicValue + 1;
  61.  
  62.         if (Score < currentScore || currentScore == 0 || is_undefined(currentScore)) {
  63.             if (currentScore == 0 || is_undefined(currentScore)) {
  64.                 ds_priority_add(OpenedList, Neighbour, Score);
  65.             } else {
  66.                 ds_priority_change_priority(OpenedList, Neighbour, Score);
  67.             }
  68.            
  69.             if (!ds_map_exists(PreviousNodes, Neighbour)) {
  70.                 ds_map_add(PreviousNodes, Neighbour, CurrentNode);
  71.             } else {
  72.                 ds_map_replace(PreviousNodes, Neighbour, CurrentNode);
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78. ds_priority_destroy(OpenedList);
  79. ds_list_destroy(ClosedList);
  80. ds_map_destroy(TentativeDistances);
  81.    
  82. if (CurrentNode != Target) {
  83.     ds_map_destroy(PreviousNodes);
  84.     return(noone);
  85. }
  86.  
  87.  
  88. var Path;
  89. Path = ds_list_create();
  90. while (true) {
  91.     ds_list_add(Path, CurrentNode);
  92.  
  93.     if (CurrentNode == Source) {
  94.         reversedPath = ds_list_create();
  95.         for (i = ds_list_size(Path) - 1; i >=0; i-=1) {
  96.             ds_list_add(reversedPath, ds_list_find_value(Path, i));
  97.         }
  98.        
  99.         ds_list_destroy(Path);
  100.         ds_map_destroy(PreviousNodes);
  101.         return(reversedPath);
  102.     }
  103.  
  104.     CurrentNode = ds_map_find_value(PreviousNodes, CurrentNode);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement