Ramaraunt1

Untitled

Nov 8th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. ///movement_range(origin_gridspacer, movement_range, unit);
  2.  
  3. wipe_nodes();
  4.  
  5. //declare relevant variables from arguments
  6. start = argument0;
  7. range = argument1;
  8. unit = argument2;
  9.  
  10. //create data structures
  11. open = ds_priority_create();
  12. closed = ds_list_create();
  13. neighbors = ds_list_create();
  14.  
  15. //add starting node to the open list
  16. ds_priority_add(open,start,0);
  17.  
  18. //while open queue is NOT empty...
  19. //repeat the following until ALL nodes have been looked at
  20.  
  21. while!(ds_priority_empty(open))
  22. {
  23. //remove node with lowest G score from open
  24. current_gridspacer = ds_priority_delete_min(open);
  25.  
  26. //add current_gridspacer to closed list
  27. ds_list_add(closed, current_gridspacer);
  28.  
  29. //add neighbors to ds_list neighbors
  30. if unit.walkable[current_gridspacer.n+1,current_gridspacer.m]
  31. {
  32. ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n + 1],global.handler.gridspace_y[current_gridspacer.m],obj_gridspacer));
  33. }
  34. if unit.walkable[current_gridspacer.n-1,current_gridspacer.m]
  35. {
  36. ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n - 1],global.handler.gridspace_y[current_gridspacer.m],obj_gridspacer));
  37. }
  38. if unit.walkable[current_gridspacer.n,current_gridspacer.m-1]
  39. {
  40. ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n],global.handler.gridspace_y[current_gridspacer.m-1],obj_gridspacer));
  41. }
  42. if unit.walkable[current_gridspacer.n,current_gridspacer.m+1]
  43. {
  44. ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n],global.handler.gridspace_y[current_gridspacer.m+1],obj_gridspacer));
  45. }
  46.  
  47. //step through all of current_gridspacer's neighbors
  48. for (ii = 0; ii < ds_list_size(neighbors); ii ++)
  49. {
  50. //store current neighbor in neighbor variable
  51. neighbor = ds_list_find_value(neighbors, ii);
  52.  
  53. //add neighbor to open list
  54. //note: must not be ALREADY on closed list
  55. if (ds_list_find_index(closed,neighbor) < 0)
  56. {
  57. costMod = 1;
  58. //only calculate a new g score if it hasnt already been calculated
  59. if (ds_priority_find_priority(open, neighbor) == 0 or ds_priority_find_priority(open, neighbor) == undefined)
  60. {
  61. //give neighbor the appropriate parent
  62. neighbor.parent = current_gridspacer;
  63.  
  64. //calculate G store of nieghbor using costmod
  65. neighbor.G = current_gridspacer.G + (neighbor.cost * costMod);
  66.  
  67. //add neighbor to the open list so it can be checked out too!
  68. ds_priority_add(open, neighbor, neighbor.G);
  69. }
  70. //ELSE!
  71. //If neighbor's score has ALREADY been calculated for the open list!
  72. else
  73. {
  74. //figure outif the neighbor's score would be LOWER if found from the current node
  75. costMod = 1;
  76.  
  77. tempG = current_gridspacer.G + (nieghbor.cost * costMod);
  78.  
  79. //check if G score would be lower
  80. if (tempG < neighbor.G)
  81. {
  82. neighbor.parent = current_gridspacer;
  83. neighbor.G = tempG;
  84. ds_priority_change_priority(open, neighbor, neighbor.G);
  85. }
  86. }
  87. }
  88. }
  89.  
  90. //empty the neighbors list
  91. ds_list_clear(neighbors);
  92. }
  93.  
  94. //round down all G scores!
  95. with(obj_gridspacer)
  96. {
  97. G = floor(G);
  98. }
  99.  
  100. //destroy the neighbors list
  101. ds_list_destroy(neighbors);
  102.  
  103. //destroy the open list SUPER IMPORTANT!!! No leaks here!
  104. ds_priority_destroy(open);
  105.  
  106. //destroy the closed list.
  107. ds_list_destroy(closed);
Advertisement
Add Comment
Please, Sign In to add comment