Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_path(start_node, end_node)
- closed_list = []
- open_list = []
- closed_list.push(start_node)
- until closed_list.last.x == end_node.x and closed_list.last.y == end_node.y
- last_x = closed_list.last.x
- last_y = closed_list.last.y
- open_list = []
- open_list.push(closed_list.last)
- for dir in 1..8
- node = PATH::Node.new
- node.x = PATH.offset(last_x, last_y, dir)[0]
- node.y = PATH.offset(last_x, last_y, dir)[1]
- node.direction = dir
- next unless $game_map.passable?(node.x, node.y)
- flag = false
- for item in closed_list
- flag = true if item.x == node.x and item.y == node.y
- end
- next if flag
- for item in open_list
- open_list.delete(item) if item.x == node.x and item.y == node.y
- end
- node.g = PATH.g(dir)
- node.h = PATH.h(node.x, node.y, end_node.x, end_node.y)
- node.f = node.g + node.h
- open_list.push(node)
- end
- for node in open_list
- open_list.delete(node) if closed_list.include?(node)
- end
- open_list = open_list.sort {|a, b| a.f <=> b.f}
- closed_list.push(open_list.first)
- open_list.delete(open_list.first)
- end
- for node in closed_list
- adjacent_nodes = []
- for i in 0..closed_list.index(node) - 1
- prev_node = closed_list[i]
- adjacent_nodes.push(prev_node) if (node.x - prev_node.x).abs <= 1 and (node.y - prev_node.y).abs <= 1
- end
- if adjacent_nodes.size > 1
- for i in 1..adjacent_nodes.size - 1
- closed_list.delete(adjacent_nodes[i])
- end
- end
- next unless adjacent_nodes.first
- node.direction = find_direction(adjacent_nodes.first, node)
- end
- return closed_list
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement