Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.82 KB | None | 0 0
  1.   def create_path(start_node, end_node)
  2.     closed_list = []
  3.     open_list = []
  4.     closed_list.push(start_node)
  5.     until closed_list.last.x == end_node.x and closed_list.last.y == end_node.y
  6.       last_x = closed_list.last.x
  7.       last_y = closed_list.last.y
  8.       open_list = []
  9.       open_list.push(closed_list.last)
  10.       for dir in 1..8
  11.         node = PATH::Node.new
  12.         node.x = PATH.offset(last_x, last_y, dir)[0]
  13.         node.y = PATH.offset(last_x, last_y, dir)[1]
  14.         node.direction = dir
  15.         next unless $game_map.passable?(node.x, node.y)
  16.         flag = false
  17.         for item in closed_list
  18.           flag = true if item.x == node.x and item.y == node.y
  19.         end
  20.         next if flag
  21.         for item in open_list
  22.           open_list.delete(item) if item.x == node.x and item.y == node.y
  23.         end
  24.         node.g = PATH.g(dir)
  25.         node.h = PATH.h(node.x, node.y, end_node.x, end_node.y)
  26.         node.f = node.g + node.h
  27.         open_list.push(node)
  28.       end
  29.      
  30.       for node in open_list
  31.         open_list.delete(node) if closed_list.include?(node)
  32.       end
  33.      
  34.       open_list = open_list.sort {|a, b| a.f <=> b.f}
  35.      
  36.       closed_list.push(open_list.first)
  37.       open_list.delete(open_list.first)
  38.     end
  39.    
  40.     for node in closed_list
  41.       adjacent_nodes = []
  42.       for i in 0..closed_list.index(node) - 1
  43.         prev_node = closed_list[i]
  44.         adjacent_nodes.push(prev_node) if (node.x - prev_node.x).abs <= 1 and (node.y - prev_node.y).abs <= 1
  45.       end
  46.       if adjacent_nodes.size > 1
  47.         for i in 1..adjacent_nodes.size - 1
  48.           closed_list.delete(adjacent_nodes[i])
  49.         end
  50.       end
  51.       next unless adjacent_nodes.first
  52.       node.direction = find_direction(adjacent_nodes.first, node)
  53.     end
  54.    
  55.     return closed_list
  56.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement