AntMoveBrainPart.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from intelligine.simulation.object.brain.part.move.AntStar.ByPass import ByPass
  2. from intelligine.simulation.object.brain.part.move.AntStar.Host import Host
  3. from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
  4. from intelligine.synergy.event.move.direction import directions_modifiers, get_position_with_direction_decal
  5. from synergine_xyz.cst import POSITION
  6. from intelligine.core.exceptions import NoMolecule, NoTypeInMolecule
  7. from intelligine.cst import MOLECULE_SEARCHING, MOVE_MODE_EXPLO, MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, \
  8. EXPLORATION_VECTOR, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG
  9. from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
  10. class AntMoveBrainPart(MoveBrainPart):
  11. def __init__(self, host_brain):
  12. super().__init__(host_brain)
  13. self._exploration_vector = (0, 0)
  14. def _set_exploration_vector(self, new_vector):
  15. self._exploration_vector = new_vector
  16. # TODO: On devrais donner le context aux brain parts
  17. self._host_brain.get_context().metas.value.set(EXPLORATION_VECTOR,
  18. self._host_brain.get_host().get_id(),
  19. new_vector)
  20. @classmethod
  21. def get_direction(cls, context, object_id):
  22. move_mode = context.metas.value.get(MOVE_MODE, object_id)
  23. if move_mode == MOVE_MODE_GOHOME:
  24. return cls._get_direction_with_exploration_vector(context, object_id)
  25. else:
  26. try:
  27. return cls._get_direction_with_molecules(context, object_id)
  28. except NoMolecule:
  29. return super().get_direction(context, object_id)
  30. return super().get_direction(context, object_id)
  31. @classmethod
  32. def _get_direction_with_molecules(cls, context, object_id):
  33. object_point = context.metas.value.get(POSITION, object_id)
  34. molecule_type = context.metas.value.get(MOLECULE_SEARCHING, object_id)
  35. try:
  36. direction = cls._get_molecule_direction_for_point(context, object_point, molecule_type)
  37. except NoMolecule:
  38. try:
  39. direction = cls._get_direction_of_molecule(context, object_point, molecule_type)
  40. except NoMolecule:
  41. raise
  42. return direction
  43. @staticmethod
  44. def _get_molecule_direction_for_point(context, point, molecule_type):
  45. return DirectionMolecule.get_direction_for_point(context, point, molecule_type)
  46. @staticmethod
  47. def _get_direction_of_molecule(context, point, molecule_type):
  48. search_molecule_in_points = context.get_around_points_of_point(point)
  49. try:
  50. best_molecule_direction = DirectionMolecule.get_best_molecule_direction_in(context,
  51. point,
  52. search_molecule_in_points,
  53. molecule_type)
  54. return best_molecule_direction
  55. except NoMolecule as err:
  56. raise err
  57. @classmethod
  58. def _get_direction_with_exploration_vector(cls, context, object_id):
  59. ant_star = cls._get_by_pass_brain(context, object_id)
  60. ant_star.advance()
  61. return ant_star.get_host().get_moved_to_direction()
  62. @classmethod
  63. def _get_by_pass_brain(cls, context, object_id):
  64. # We use an adaptation of AntStar
  65. exploration_vector = context.metas.value.get(EXPLORATION_VECTOR, object_id)
  66. home_vector = (-exploration_vector[0], -exploration_vector[1])
  67. ant_host = Host(context, object_id)
  68. return ByPass(ant_host, home_vector, context, object_id)
  69. # TODO: obj pas necessaire, il est dans _host
  70. def done(self, obj, context):
  71. super().done(obj, context)
  72. self._appose_molecule(obj)
  73. self._check_context(obj, context)
  74. self._apply_context(obj, context)
  75. @staticmethod
  76. def _appose_molecule(obj):
  77. if obj.get_movement_molecule_gland().is_enabled():
  78. obj.get_movement_molecule_gland().appose()
  79. def _check_context(self, obj, context):
  80. """
  81. If was in exploration, and just found home smell;
  82. -> home mode (then, in this mode: no update vector, put food if food, etc)
  83. If was in home, and just loose home smell:
  84. -> exploration mode
  85. :param obj:
  86. :param context:
  87. :return:
  88. """
  89. movement_mode = self._host_brain.get_movement_mode()
  90. if movement_mode == MOVE_MODE_GOHOME and self._on_home_smell(context, obj.get_id()):
  91. self._host_brain.switch_to_mode(MOVE_MODE_HOME)
  92. ant_star = self._get_by_pass_brain(context, obj.get_id())
  93. ant_star.erase()
  94. # TODO: on change les molecule recherché (Food => SmellFood, definis dans Take, en fct de ce qui est take)
  95. elif movement_mode == MOVE_MODE_HOME and not self._on_home_smell(context, obj.get_id()):
  96. self._host_brain.switch_to_mode(MOVE_MODE_EXPLO)
  97. self._start_new_exploration()
  98. elif movement_mode == MOVE_MODE_EXPLO and self._on_home_smell(context, obj.get_id()):
  99. self._start_new_exploration() # TODO: rename en reinit_explo
  100. # TODO: sitch explo si rien a faire (rien a poser par exemple) et HOME
  101. # TODO: Poser sur StockedFood
  102. @classmethod
  103. def _on_home_smell(cls, context, object_id):
  104. current_position = context.metas.value.get(POSITION, object_id)
  105. flavour = context.molecules().get_flavour(current_position)
  106. # TODO: Idem, liste de smell_type ...
  107. for smell_type in (SMELL_FOOD, SMELL_EGG):
  108. try:
  109. molecule = flavour.get_molecule(category=MOLECULES_DIRECTION, type=smell_type)
  110. return True
  111. except NoMolecule:
  112. pass # C'est qu'elle y est pas ^^
  113. return False
  114. def _update_exploration_vector(self):
  115. # TODO: add tuple as vectors ?
  116. just_move_vector = directions_modifiers[self._host_brain.get_host().get_previous_direction()]
  117. self._set_exploration_vector((self._exploration_vector[0] + just_move_vector[1],
  118. self._exploration_vector[1] + just_move_vector[2]))
  119. def _apply_context(self, obj, context):
  120. movement_mode = self._host_brain.get_movement_mode()
  121. if movement_mode == MOVE_MODE_EXPLO or movement_mode == MOVE_MODE_GOHOME:
  122. self._update_exploration_vector()
  123. def _start_new_exploration(self):
  124. # On vient de rentrer dans le monde exterieur, le vecteur de départ pointe vers la case précedente
  125. # qui est une case dans la forteresse.
  126. self._exploration_vector = get_position_with_direction_decal(self.get_host().get_previous_direction())