Skip to content

Instantly share code, notes, and snippets.

@JacobNinja
Created December 12, 2012 05:50
Show Gist options
  • Save JacobNinja/4265168 to your computer and use it in GitHub Desktop.
Save JacobNinja/4265168 to your computer and use it in GitHub Desktop.
tile pathfinder
def route_to(start_x, start_y, end_x, end_y, path=[], last_routes=[])
possible_routes = sorted_routes(start_x, start_y, end_x, end_y)
if r = possible_routes.find {|route| route == [end_x, end_y]}
return path + [[*r, new_item_height(*r)]]
else
possible_routes.each do |(possible_x, possible_y)|
next if last_routes.include? [possible_x, possible_y]
new_path = path + [[possible_x, possible_y, new_item_height(possible_x, possible_y)]]
used_routes = (last_routes + possible_routes).uniq
new_route = route_to(possible_x, possible_y, end_x, end_y, new_path, used_routes)
return new_route if new_route
end
end
nil
end
def sorted_routes(start_x, start_y, end_x, end_y)
neighbors(start_x, start_y).sort_by do |(x, y)|
(end_x - x).abs + (end_y - y).abs
end
end
def neighbors(x, y)
surrounding(x, y).reject do |(x, y)|
within_grid?(x, y) || blocked?(x, y)
end
end
def surrounding(x, y)
[[x - 1, y - 1],
[x - 1, y],
[x - 1, y +1],
[x, y - 1],
[x, y + 1],
[x + 1, y - 1],
[x + 1, y],
[x + 1, y + 1]]
end
def within_grid?(x, y)
row = heightmap_data[y]
x < 0 || y < 0 || row.nil? || row[x].nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment