DirectionMolecule.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from intelligine.cst import POINTS_SMELL, MOLECULES, MOLECULES_DIRECTION
  2. from intelligine.core.exceptions import NoMolecule
  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 DirectionMolecule():
  7. _positions_key = None
  8. @classmethod
  9. def appose(cls, context, point, molecule):
  10. context.molecules().increment_with_molecule(point, molecule, context.get_cycle())
  11. context.metas.list.add(MOLECULES, MOLECULES, point, assert_not_in=False)
  12. @classmethod
  13. def get_direction_for_point(cls, context, point, molecule_type):
  14. flavour = context.molecules().get_flavour(point)
  15. molecule = flavour.get_molecule(category=MOLECULES_DIRECTION, type=molecule_type)
  16. distance = molecule.get_distance()
  17. around_molecule_filter = lambda around_molecule: around_molecule.get_distance() < distance
  18. around_molecules_points = cls._get_around_molecules(context, point, molecule_type,
  19. molecule_filter=around_molecule_filter)
  20. if not around_molecules_points:
  21. raise NoMolecule()
  22. shuffle(around_molecules_points)
  23. around_molecules_sorted = sorted(around_molecules_points, key=lambda x: x[1].get_intensity(), reverse=True)
  24. max_intensity = around_molecules_sorted[0][1].get_intensity()
  25. around_molecules_max = []
  26. for around_molecule_sorted in around_molecules_sorted:
  27. if around_molecule_sorted[1].get_intensity() == max_intensity:
  28. around_molecules_max.append(around_molecule_sorted)
  29. around_molecules_sorted_by_distance = sorted(around_molecules_max,
  30. key=lambda x: x[1].get_distance(),
  31. reverse=False)
  32. go_to_point = around_molecules_sorted_by_distance[0][0]
  33. direction_degrees = get_degree_from_north(point, go_to_point)
  34. direction = get_direction_for_degrees(direction_degrees)
  35. return direction
  36. @classmethod
  37. def _get_around_molecules(cls, context, reference_point, molecule_type,
  38. molecule_filter=lambda around_molecule: True):
  39. around_points = context.get_around_points_of_point(reference_point)
  40. around_molecules_points = []
  41. for around_point in around_points:
  42. flavour = context.molecules().get_flavour(around_point)
  43. try:
  44. around_molecule = flavour.get_molecule(category=MOLECULES_DIRECTION, type=molecule_type)
  45. if molecule_filter(around_molecule):
  46. around_molecules_points.append((around_point, around_molecule))
  47. except NoMolecule:
  48. pass # No molecule, ok continue to sniff around
  49. return around_molecules_points
  50. @classmethod
  51. def get_best_molecule_direction_in(cls, context, reference_point, points, molecule_type):
  52. around_molecules_points = []
  53. for around_point in points:
  54. flavour = context.molecules().get_flavour(around_point)
  55. try:
  56. around_molecule = flavour.get_molecule(category=MOLECULES_DIRECTION, type=molecule_type)
  57. around_molecules_points.append((around_point, around_molecule))
  58. except NoMolecule:
  59. pass # Ok, no molecule, continue to sniff around
  60. if not around_molecules_points:
  61. raise NoMolecule()
  62. shuffle(around_molecules_points)
  63. around_molecules_sorted = sorted(around_molecules_points, key=lambda x: x[1].get_intensity(), reverse=True)
  64. go_to_point = around_molecules_sorted[0][0]
  65. direction_degrees = get_degree_from_north(reference_point, go_to_point)
  66. direction = get_direction_for_degrees(direction_degrees)
  67. return direction