AntMoveBrainPart.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
  2. from synergine_xyz.cst import POSITION
  3. from intelligine.core.exceptions import NoPheromone
  4. from intelligine.cst import PHEROMONE_SEARCHING, MOVE_MODE_EXPLO, COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING
  5. from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone
  6. class AntMoveBrainPart(MoveBrainPart):
  7. @classmethod
  8. def get_direction(cls, context, object_id):
  9. try:
  10. return cls._get_direction_with_pheromones(context, object_id)
  11. except NoPheromone:
  12. return super().get_direction(context, object_id)
  13. @classmethod
  14. def _get_direction_with_pheromones(cls, context, object_id):
  15. object_point = context.metas.value.get(POSITION, object_id)
  16. pheromone_type = context.metas.value.get(PHEROMONE_SEARCHING, object_id)
  17. try:
  18. direction = cls._get_pheromone_direction_for_point(context, object_point, pheromone_type)
  19. except NoPheromone:
  20. try:
  21. direction = cls._get_direction_of_pheromone(context, object_point, pheromone_type)
  22. except NoPheromone:
  23. raise
  24. return direction
  25. @staticmethod
  26. def _get_pheromone_direction_for_point(context, point, pheromone_type):
  27. return DirectionPheromone.get_direction_for_point(context, point, pheromone_type)
  28. @staticmethod
  29. def _get_direction_of_pheromone(context, point, pheromone_type):
  30. search_pheromone_in_points = context.get_around_points_of(point, distance=1)
  31. try:
  32. best_pheromone_direction = DirectionPheromone.get_best_pheromone_direction_in(context,
  33. point,
  34. search_pheromone_in_points,
  35. pheromone_type)
  36. return best_pheromone_direction
  37. except NoPheromone as err:
  38. raise err
  39. def done(self, obj, context):
  40. super().done(obj, context)
  41. self._appose_pheromone(obj)
  42. @staticmethod
  43. def _appose_pheromone(obj):
  44. if obj.get_movement_pheromone_gland().is_enabled():
  45. obj.get_movement_pheromone_gland().appose()