Ulabael

A_Star

Jul 15th, 2022 (edited)
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function heuristic(a,b)
  2. {
  3.     // a, b - массивы, которые хранят x и y выбранных клеток
  4.     a = global.grid_array[a]
  5.     b = global.grid_array[b]
  6.     return abs(a[0] - b[0]) + abs(a[1] - b[1])
  7. }
  8.  
  9. function find_path(cell_start, cell_end)
  10. {
  11.     var frontier = ds_priority_create()
  12.     var came_from = ds_map_create()
  13.     var cost_so_far = ds_map_create()
  14.    
  15.     var cell_x = global.grid_array[cell_end][0]
  16.     var cell_y = global.grid_array[cell_end][1]
  17.     var need_neighbors = ds_grid_get(global.grid, cell_x, cell_y) == 0
  18.  
  19.     if need_neighbors
  20.     {
  21.         var end_neighbors = neighbors(cell_end)
  22.     }
  23.     var btb = false
  24.  
  25.     ds_priority_add(frontier, cell_start, 0)
  26.     ds_map_add(came_from, cell_start, noone)
  27.     ds_map_add(cost_so_far, cell_start, 0)
  28.    
  29.     while !ds_priority_empty(frontier)
  30.     {
  31.         current = ds_priority_delete_min(frontier)
  32.        
  33.         if need_neighbors
  34.         {
  35.             var close = scrFindInArray(end_neighbors, current)
  36.         }
  37.         // Если клетка, которую мы хотим достичь, непроходима и мы являемся соседом с ней
  38.         // То выходим из цикла - путь найден
  39.         if current == cell_end
  40.         or need_neighbors
  41.         and close != noone
  42.         {
  43.             btb = true
  44.             break
  45.         }
  46.        
  47.         vneighbors = neighbors(current)
  48.         for (var i = 0; i < array_length(vneighbors); ++i)
  49.         {
  50.             new_cost = ds_map_find_value(cost_so_far, current) + ds_grid_get(global.grid, global.grid_array[vneighbors[i]][0], global.grid_array[vneighbors[i]][1])
  51.             if is_undefined(ds_map_find_value(cost_so_far, vneighbors[i]))
  52.             or new_cost < ds_map_find_value(cost_so_far, vneighbors[i])
  53.             {
  54.                 priority = new_cost + heuristic(cell_end, vneighbors[i])
  55.                 ds_map_add(cost_so_far, vneighbors[i], new_cost)
  56.                 ds_priority_add(frontier, vneighbors[i], priority)
  57.                 ds_map_add(came_from, vneighbors[i], current)
  58.             }
  59.         }
  60.     }
  61.        
  62.     // Если путь найден
  63.     if current == cell_end
  64.     or btb
  65.     {
  66.         var path = []
  67.         while current != cell_start
  68.         {
  69.             array_push(path, current)
  70.             current = ds_map_find_value(came_from, current)
  71.         }
  72.         array_push(path, cell_start)
  73.         ds_map_destroy(came_from)
  74.         ds_map_destroy(cost_so_far)
  75.         ds_priority_destroy(frontier)
  76.         return path
  77.     }
  78.     else
  79.     {  
  80.         // Пути нет - возвращаем noone
  81.         ds_map_destroy(came_from)
  82.         ds_map_destroy(cost_so_far)
  83.         ds_priority_destroy(frontier)
  84.         return noone   
  85.     }
  86. }
Add Comment
Please, Sign In to add comment