node.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. class Node(object):
  3. """
  4. basic node, saves X and Y coordinates on some grid and determine if
  5. it is walkable.
  6. """
  7. def __init__(self, x=0, y=0, walkable=True, weight=1):
  8. # Coordinates
  9. self.x = x
  10. self.y = y
  11. # Whether this node can be walked through.
  12. self.walkable = walkable
  13. # used for weighted algorithms
  14. self.weight = weight
  15. # values used in the finder
  16. self.cleanup()
  17. def __lt__(self, other):
  18. """
  19. nodes are sorted by f value (see a_star.py)
  20. :param other: compare Node
  21. :return:
  22. """
  23. return self.f < other.f
  24. def cleanup(self):
  25. """
  26. reset all calculated values, fresh start for pathfinding
  27. """
  28. # cost from this node to the goal
  29. self.h = 0.0
  30. # cost from the start node to this node
  31. self.g = 0.0
  32. # distance from start to this point (f = g + h )
  33. self.f = 0.0
  34. self.opened = 0
  35. self.closed = False
  36. # used for backtracking to the start point
  37. self.parent = None
  38. # used for recurion tracking of IDA*
  39. self.retain_count = 0
  40. # used for IDA* and Jump-Point-Search
  41. self.tested = False