DirectionPheromone.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from intelligine.cst import PHEROMON_DIRECTION, PHEROMON_POSITIONS
  2. from intelligine.core.exceptions import NoPheromone
  3. from random import shuffle
  4. from xyzworld.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. @staticmethod
  12. def get_direction_for_point(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_points = context.get_around_points_of(point)
  17. # TODO: Cet algo around a mettre ailleurs
  18. around_pheromones_points = []
  19. for around_point in around_points:
  20. flavour = context.pheromones().get_flavour(around_point)
  21. try:
  22. around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  23. if around_pheromone.get_distance() < distance:
  24. around_pheromones_points.append((around_point, around_pheromone))
  25. except NoPheromone:
  26. pass # No pheromone, ok continue to sniff around
  27. if not around_pheromones_points:
  28. raise NoPheromone()
  29. shuffle(around_pheromones_points)
  30. around_pheromones_sorted = sorted(around_pheromones_points, key=lambda x: x[1].get_intensity(), reverse=True)
  31. max_intensity = around_pheromones_sorted[0][1].get_intensity()
  32. around_pheromones_max = []
  33. for around_pheromone_sorted in around_pheromones_sorted:
  34. if around_pheromone_sorted[1].get_intensity() == max_intensity:
  35. around_pheromones_max.append(around_pheromone_sorted)
  36. around_pheromones_sorted_by_distance = sorted(around_pheromones_max,
  37. key=lambda x: x[1].get_distance(),
  38. reverse=False)
  39. go_to_point = around_pheromones_sorted_by_distance[0][0]
  40. direction_degrees = get_degree_from_north(point, go_to_point)
  41. direction = get_direction_for_degrees(direction_degrees)
  42. return direction
  43. @staticmethod
  44. def get_best_pheromone_direction_in(context, reference_point, points, pheromone_type):
  45. around_pheromones_points = []
  46. for around_point in points:
  47. flavour = context.pheromones().get_flavour(around_point)
  48. try:
  49. around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  50. around_pheromones_points.append((around_point, around_pheromone))
  51. except NoPheromone:
  52. pass # Ok, no pheromone, continue to sniff around
  53. if not around_pheromones_points:
  54. raise NoPheromone()
  55. shuffle(around_pheromones_points)
  56. around_pheromones_sorted = sorted(around_pheromones_points, key=lambda x: x[1].get_intensity(), reverse=True)
  57. go_to_point = around_pheromones_sorted[0][0]
  58. direction_degrees = get_degree_from_north(reference_point, go_to_point)
  59. direction = get_direction_for_degrees(direction_degrees)
  60. return direction