DirectionPheromone.py 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from intelligine.cst import PHEROMON_DIRECTION, PHEROMON_POSITIONS
  2. from intelligine.core.exceptions import NoPheromone
  3. from random import shuffle
  4. from synergine_xyz.geometry import get_degree_from_north
  5. from intelligine.synergy.event.move.direction import get_direction_for_degrees
  6. class DirectionPheromone():
  7. @staticmethod
  8. def appose(context, point, pheromone):
  9. context.pheromones().increment_with_pheromone(point, pheromone)
  10. context.metas.list.add(PHEROMON_POSITIONS, PHEROMON_POSITIONS, point, assert_not_in=False)
  11. @classmethod
  12. def get_direction_for_point(cls, context, point, pheromone_type):
  13. flavour = context.pheromones().get_flavour(point)
  14. pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  15. distance = pheromone.get_distance()
  16. around_pheromone_filter = lambda around_pheromone: around_pheromone.get_distance() < distance
  17. around_pheromones_points = cls._get_around_pheromones(context, point, pheromone_type,
  18. pheromone_filter=around_pheromone_filter)
  19. if not around_pheromones_points:
  20. raise NoPheromone()
  21. shuffle(around_pheromones_points)
  22. around_pheromones_sorted = sorted(around_pheromones_points, key=lambda x: x[1].get_intensity(), reverse=True)
  23. max_intensity = around_pheromones_sorted[0][1].get_intensity()
  24. around_pheromones_max = []
  25. for around_pheromone_sorted in around_pheromones_sorted:
  26. if around_pheromone_sorted[1].get_intensity() == max_intensity:
  27. around_pheromones_max.append(around_pheromone_sorted)
  28. around_pheromones_sorted_by_distance = sorted(around_pheromones_max,
  29. key=lambda x: x[1].get_distance(),
  30. reverse=False)
  31. go_to_point = around_pheromones_sorted_by_distance[0][0]
  32. direction_degrees = get_degree_from_north(point, go_to_point)
  33. direction = get_direction_for_degrees(direction_degrees)
  34. return direction
  35. @staticmethod
  36. def _get_around_pheromones(context, reference_point, pheromone_type,
  37. pheromone_filter=lambda around_pheromone: True):
  38. around_points = context.get_around_points_of(reference_point)
  39. around_pheromones_points = []
  40. for around_point in around_points:
  41. flavour = context.pheromones().get_flavour(around_point)
  42. try:
  43. around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  44. if pheromone_filter(around_pheromone):
  45. around_pheromones_points.append((around_point, around_pheromone))
  46. except NoPheromone:
  47. pass # No pheromone, ok continue to sniff around
  48. return around_pheromones_points
  49. @staticmethod
  50. def get_best_pheromone_direction_in(context, reference_point, points, pheromone_type):
  51. around_pheromones_points = []
  52. for around_point in points:
  53. flavour = context.pheromones().get_flavour(around_point)
  54. try:
  55. around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  56. around_pheromones_points.append((around_point, around_pheromone))
  57. except NoPheromone:
  58. pass # Ok, no pheromone, continue to sniff around
  59. if not around_pheromones_points:
  60. raise NoPheromone()
  61. shuffle(around_pheromones_points)
  62. around_pheromones_sorted = sorted(around_pheromones_points, key=lambda x: x[1].get_intensity(), reverse=True)
  63. go_to_point = around_pheromones_sorted[0][0]
  64. direction_degrees = get_degree_from_north(reference_point, go_to_point)
  65. direction = get_direction_for_degrees(direction_degrees)
  66. return direction