AntPutBrainPart.py 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from intelligine.synergy.object.Food import Food
  2. from synergine.core.Core import Core
  3. from intelligine.core.exceptions import CantFindWhereToPut
  4. from intelligine.cst import MODE_EXPLO, TYPE_RESOURCE_EXPLOITABLE, MODE_NURSE, TYPE_NURSERY, \
  5. MODE_HOME, TYPE_RESOURCE_EATABLE, MODE_GOHOME, CARRY
  6. from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
  7. from synergine_xyz.cst import POSITION, POSITIONS
  8. class AntPutBrainPart(TransportBrainPart):
  9. _mode_matches = TransportBrainPart._mode_matches.copy()
  10. _mode_matches.update({
  11. MODE_NURSE: [TYPE_NURSERY],
  12. MODE_HOME: [TYPE_RESOURCE_EATABLE],
  13. MODE_GOHOME: []
  14. })
  15. _types_matches = {
  16. TYPE_RESOURCE_EXPLOITABLE: [TYPE_RESOURCE_EATABLE],
  17. TYPE_NURSERY: [TYPE_NURSERY]
  18. }
  19. @classmethod
  20. def can_put(cls, context, object_id, object_near_id):
  21. # Si l'objet à coté fait partie des objets concernés par le mode du porteur
  22. if cls._match_with_mode(context, object_id, object_near_id):
  23. # Et si les objet sont rangeable enssemble:
  24. object_carried_id = context.metas.value.get(CARRY, object_id)
  25. return cls._objects_types_match(context, object_carried_id, object_near_id)
  26. return False
  27. @classmethod
  28. def get_put_position(cls, context, object_id, object_near_id):
  29. """
  30. Maybe part "found available position arround" should be in other class.
  31. :param context:
  32. :param object_id:
  33. :param object_near_id:
  34. :return:
  35. """
  36. obj_near_position = context.metas.value.get(POSITION, object_near_id)
  37. if cls._is_available_position(context, obj_near_position):
  38. return obj_near_position
  39. obj_transporter_position = context.metas.value.get(POSITION, object_id)
  40. obj_transporter_around_positions = context.get_around_points_of_point(obj_transporter_position)
  41. obj_near_around_positions = context.get_around_points_of_point(obj_near_position)
  42. # For each position between target and current transporter
  43. for pos_around_target in obj_near_around_positions:
  44. if pos_around_target in obj_transporter_around_positions:
  45. if cls._is_available_position(context, pos_around_target):
  46. return pos_around_target
  47. raise CantFindWhereToPut()
  48. @classmethod
  49. def _is_available_position(cls, context, position):
  50. if not context.position_is_penetrable(position):
  51. return False
  52. if not cls._position_is_enought_place(context, position):
  53. return False
  54. if not cls._position_have_free_space_around(context, position):
  55. return False
  56. return True
  57. @classmethod
  58. def _position_is_enought_place(cls, context, position):
  59. count_obj_here = len(context.metas.list.get(POSITIONS, position, allow_empty=True))
  60. if count_obj_here < Core.get_configuration_manager().get('ant.put.max_objects_at_same_position', 5):
  61. return True
  62. return False
  63. @classmethod
  64. def _position_have_free_space_around(cls, context, position):
  65. # TODO: On regarde ces cases qui ne sont pas forcement visible par l'individu. On presupose ici qu'elle peut les
  66. # voir. Pour etre au top il faudrait une logie propre a l'individu qui definis ce qu'il voie.
  67. around_positions = context.get_around_points_of_point(position)
  68. for around_position in around_positions:
  69. if not context.position_is_penetrable(around_position):
  70. return False
  71. return True
  72. def done(self, puted_object):
  73. # TODO: lancer le choix d'un nouveau mode dans le brain.
  74. if isinstance(puted_object, Food):
  75. self._host.get_brain().switch_to_mode(MODE_EXPLO)