All pastes #219729 Raw Edit

Unnamed

public text v1 · immutable
#219729 ·published 2006-10-24 20:50 UTC
rendered paste body
class Game_Map
  def find_path(source, x, y, depth = 0)
    return if [source.x, source.y] == [x, y]
    open = {} # [x, y] @ [parent_x, parent_y]
    closed = []
    closed << [source.x, source.y]
    loop do
      for x_pos in (closed.last[0] - 1)..(closed.last[0] + 1)
        for y_pos in (closed.last[1] - 1)..(closed.last[1] + 1)
          next unless $game_map.passable?(x_pos, y_pos)
          next if closed.last[0] == x_pos and closed.last[1] == y_pos
          open[x_pos, y_pos] = [closed.last[0], closed.last[1]]
        end
      end
      value = {}
      for open_coords in open
        value[open_coords[0]] = (find_h(open_coords[0], x, y) + 
                                 find_g(open_coords[0], open_coords[1]))
      end
      value.sort {|a,b| a[1]<=>b[1]}
      closed << value.shift[0]
      break if (closed.last == [x, y])
    end
  end
  def find_h(coords, end_x, end_y)
    start_x = coords[0]
    start_y = coords[1]
    return (end_x - start_x).abs + (end_y - start_y).abs
  end
  def find_g(tile_coords, parent_coords)
    if (tile_coords[0] == parent_coords[0] or tile_coords[1] == parent_coords[1])
      return 10
    else
      return 14
    end
  end
end