Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. /// @description Returns an array of positions
  2. /// @param target_x
  3. /// @param target_y
  4. global._astar_end_x = argument0;
  5. global._astar_end_y = argument1;
  6. var _time = get_timer();
  7. //Create intial cell
  8. var _first_cell = array_create(astar_struct_size);
  9. _first_cell[@ astar_x] = global._astar_start_x;
  10. _first_cell[@ astar_y] = global._astar_start_y;
  11. _first_cell[@ astar_h] = 9999;
  12.  
  13. _first_cell[@ astar_open] = true;
  14.  
  15. //Add initial cell to open queue
  16. global._astar_value_grid[# global._astar_start_x, global._astar_start_y] = _first_cell;
  17. ds_priority_add(global._astar_open,_first_cell,_first_cell[@ astar_f]);
  18.  
  19. while(ds_priority_size(global._astar_open)){
  20. //Get first celland remove from open list
  21. var _cell = ds_priority_delete_min(global._astar_open);
  22. _cell[@ astar_closed] = true;
  23. _cell[@ astar_open] = false;
  24.  
  25. //Mark cell as closed
  26. global._astar_value_grid[# _cell[astar_x],_cell[astar_y]] = _cell;
  27.  
  28. if(_cell[astar_x] == global._astar_end_x && _cell[astar_y] == global._astar_end_y){
  29. //Path has been found - return path
  30.  
  31. var _path = ds_create(ds_type_list);
  32. var _origin = vector2(global._astar_start_x,global._astar_start_y);
  33. var _child_position = vector2(_cell[astar_x],_cell[astar_y]);
  34.  
  35.  
  36. while(!vector2_equals(_child_position,_origin)){
  37. var _child = global._astar_value_grid[# _child_position[v2x], _child_position[v2y]];
  38. ds_list_add(_path,_child_position);
  39. _child_position = vector2(_child[astar_parent_x],_child[astar_parent_y]);
  40. }
  41.  
  42. if(!ds_list_size(_path)){
  43. return _path;
  44. }
  45.  
  46. ds_list_add(_path,_origin);
  47.  
  48. ds_list_flip(_path);
  49. return _path;
  50. }
  51.  
  52. //Check adjacent
  53. var _i=-1;
  54. repeat(array_length_1d(global._astar_adjacent)){
  55. ++_i;
  56. var _adjacent_data = global._astar_adjacent[_i],
  57. _position = vector2_add(vector2(_cell[astar_x],_cell[astar_y]),_adjacent_data);
  58.  
  59. //Supress out of bound errors
  60. if(!ds_grid_within_bounds(global._astar_grid, _position[v2x], _position[v2y])){
  61. continue;
  62. }
  63.  
  64. //Look out for non passable cells
  65. var _passable_value = global._astar_grid[# _position[v2x], _position[v2y]],
  66. _can_pass = (global._astar_custom_passable_script) ?
  67. script_execute(global._astar_passable_script,_passable_value) :
  68. astar_is_passable(_passable_value);
  69.  
  70. //Ignore if cannot pass
  71. if(!_can_pass){
  72. continue;
  73. }
  74.  
  75. var _adjacent_cell = global._astar_value_grid[# _position[v2x],_position[v2y]],
  76. _update = false,
  77. _create = !is_array(_adjacent_cell),
  78. _g = _cell[astar_g]+_adjacent_data[2];
  79.  
  80. if(_create){
  81. _adjacent_cell = array_create(astar_struct_size);
  82. _adjacent_cell[astar_x] = _position[v2x];
  83. _adjacent_cell[astar_y] = _position[v2y];
  84. _update = true;
  85.  
  86. }else{
  87. //Don't check closed cells
  88. if(_adjacent_cell[astar_closed]){
  89. continue;
  90. }
  91. _update = (_g < _adjacent_cell[astar_g]);
  92. }
  93.  
  94. if(_update){
  95. //Update cell values
  96. _adjacent_cell[@ astar_h] = abs(_position[v2x] - global._astar_end_x) + abs(_position[v2y] - abs(global._astar_end_y));
  97. _adjacent_cell[@ astar_g] = _g;
  98. _adjacent_cell[@ astar_f] = _adjacent_cell[astar_h] + _adjacent_cell[astar_g];
  99. _adjacent_cell[@ astar_parent_x] = _cell[astar_x];
  100. _adjacent_cell[@ astar_parent_y] = _cell[astar_y];
  101.  
  102. if(_create){
  103. //Add to open queue
  104. _adjacent_cell[@ astar_open] = true;
  105. ds_priority_add(global._astar_open,_adjacent_cell,_adjacent_cell[astar_f]);
  106. global._astar_value_grid[# _position[v2x],_position[v2y]] = _adjacent_cell;
  107. }
  108. }
  109. }
  110. }
  111.  
  112. return undefined;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement