Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function find_path(hexs, hexe, flying)
- {
- // hexs - id гекса, с которого мы начинаем
- // hexe - id гекса, до которого пытаемся дойти
- // flying - летает наш юнит или нет
- frontier = ds_priority_create()
- ds_priority_add(frontier, hexs, 0)
- came_from = ds_map_create()
- cost_so_far = ds_map_create()
- ds_map_add(came_from, hexs, noone)
- ds_map_add(cost_so_far, hexs, 0)
- while !ds_priority_empty(frontier)
- {
- current = ds_priority_delete_min(frontier)
- if current == hexe
- {
- break
- }
- vneighbors = neighbors(current)
- for (var next = 0; next < array_length(vneighbors); ++next)
- {
- if !flying
- {
- new_cost = ds_map_find_value(cost_so_far, current) + vneighbors[next].cell_cost
- }
- else
- {
- new_cost = ds_map_find_value(cost_so_far, current) + vneighbors[next].base_cost
- }
- if !ds_map_find_value(cost_so_far, vneighbors[next])
- or new_cost < ds_map_find_value(cost_so_far, vneighbors[next])
- {
- ds_map_add(cost_so_far, vneighbors[next], new_cost)
- //priority = new_cost
- ds_priority_add(frontier, vneighbors[next], new_cost)
- ds_map_add(came_from, vneighbors[next], current)
- }
- }
- }
- if current == hexe
- {
- current = hexe
- var path = ds_list_create()
- while current != hexs
- {
- // Добавляем ячейку в путь
- ds_list_add(path, current)
- current = ds_map_find_value(came_from, current)
- }
- /*
- for (var i = 0; i < ds_list_size(path); ++i)
- {
- inst = ds_list_find_value(path, i)
- }
- */
- return path
- }
- else
- {
- return noone
- }
- ds_map_destroy(came_from)
- ds_map_destroy(cost_so_far)
- ds_priority_destroy(frontier)
- ds_list_destroy(path)
- }
- function neighbors(start)
- {
- var neighbors_ar = array_create(0, 0)
- with oHex
- {
- /*
- q, r, s - это трёхмерные координаты гекса.
- stb - это столбец, str - это строка. По сути, x и y
- inst.q = stb
- inst.r = str - (stb - (stb & 1)) / 2
- inst.s = -inst.q - inst.r
- */
- if (s <= start.s + 1)
- and (start.s - 1 <= s)
- and (q <= start.q + 1)
- and (start.q - 1 <= q)
- and (r <= start.r + 1)
- and (start.r - 1 <= r)
- and cell_cost != 0
- and id != start
- {
- if !global.cur_unit.vars[15] and !instance_position(x, y, oUnit)
- {
- array_insert(neighbors_ar, array_length(neighbors_ar), id)
- }
- else if global.cur_unit.vars[15]
- {
- array_insert(neighbors_ar, array_length(neighbors_ar), id)
- }
- }
- }
- return neighbors_ar
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement