Advertisement
Ulabael

find_path in hexes, A*

Jul 5th, 2022
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. function find_path(hexs, hexe, flying)
  2. {
  3. // hexs - id гекса, с которого мы начинаем
  4. // hexe - id гекса, до которого пытаемся дойти
  5. // flying - летает наш юнит или нет
  6. frontier = ds_priority_create()
  7. ds_priority_add(frontier, hexs, 0)
  8. came_from = ds_map_create()
  9. cost_so_far = ds_map_create()
  10. ds_map_add(came_from, hexs, noone)
  11. ds_map_add(cost_so_far, hexs, 0)
  12.  
  13. while !ds_priority_empty(frontier)
  14. {
  15. current = ds_priority_delete_min(frontier)
  16.  
  17. if current == hexe
  18. {
  19. break
  20. }
  21. vneighbors = neighbors(current)
  22. for (var next = 0; next < array_length(vneighbors); ++next)
  23. {
  24. if !flying
  25. {
  26. new_cost = ds_map_find_value(cost_so_far, current) + vneighbors[next].cell_cost
  27. }
  28. else
  29. {
  30. new_cost = ds_map_find_value(cost_so_far, current) + vneighbors[next].base_cost
  31. }
  32.  
  33. if !ds_map_find_value(cost_so_far, vneighbors[next])
  34. or new_cost < ds_map_find_value(cost_so_far, vneighbors[next])
  35. {
  36. ds_map_add(cost_so_far, vneighbors[next], new_cost)
  37. //priority = new_cost
  38. ds_priority_add(frontier, vneighbors[next], new_cost)
  39. ds_map_add(came_from, vneighbors[next], current)
  40. }
  41. }
  42. }
  43.  
  44. if current == hexe
  45. {
  46. current = hexe
  47. var path = ds_list_create()
  48. while current != hexs
  49. {
  50. // Добавляем ячейку в путь
  51. ds_list_add(path, current)
  52. current = ds_map_find_value(came_from, current)
  53. }
  54. /*
  55. for (var i = 0; i < ds_list_size(path); ++i)
  56. {
  57. inst = ds_list_find_value(path, i)
  58. }
  59. */
  60.  
  61. return path
  62. }
  63. else
  64. {
  65. return noone
  66. }
  67.  
  68. ds_map_destroy(came_from)
  69. ds_map_destroy(cost_so_far)
  70. ds_priority_destroy(frontier)
  71. ds_list_destroy(path)
  72. }
  73.  
  74.  
  75. function neighbors(start)
  76. {
  77. var neighbors_ar = array_create(0, 0)
  78. with oHex
  79. {
  80. /*
  81. q, r, s - это трёхмерные координаты гекса.
  82.  
  83. stb - это столбец, str - это строка. По сути, x и y
  84. inst.q = stb
  85. inst.r = str - (stb - (stb & 1)) / 2
  86. inst.s = -inst.q - inst.r
  87. */
  88. if (s <= start.s + 1)
  89. and (start.s - 1 <= s)
  90. and (q <= start.q + 1)
  91. and (start.q - 1 <= q)
  92. and (r <= start.r + 1)
  93. and (start.r - 1 <= r)
  94. and cell_cost != 0
  95. and id != start
  96. {
  97. if !global.cur_unit.vars[15] and !instance_position(x, y, oUnit)
  98. {
  99. array_insert(neighbors_ar, array_length(neighbors_ar), id)
  100. }
  101. else if global.cur_unit.vars[15]
  102. {
  103. array_insert(neighbors_ar, array_length(neighbors_ar), id)
  104. }
  105. }
  106. }
  107. return neighbors_ar
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement