AntMoveBrainPart.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from intelligine.simulation.object.brain.part.MoveBrainPart import MoveBrainPart
  2. from xyzworld.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. # TEST: le temps de tout tester
  43. if obj.get_position() == obj.get_colony().get_start_position() and obj.is_carrying():
  44. obj_transported = obj.get_carried()
  45. obj_transported.set_carried_by(None)
  46. obj.put_carry(obj_transported, (-1, 0, 0))
  47. obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
  48. context.metas.collections.add_remove(obj.get_id(),
  49. COL_TRANSPORTER_NOT_CARRYING,
  50. COL_TRANSPORTER_CARRYING)
  51. @staticmethod
  52. def _appose_pheromone(obj):
  53. obj.get_movement_pheromone_gland().appose()