AntPutBrainPart.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from synergine.core.Core import Core
  2. from intelligine.core.exceptions import CantFindWhereToPut
  3. from intelligine.cst import MODE_EXPLO, TYPE_RESOURCE_EXPLOITABLE, CARRIED, MODE_NURSE, TYPE_NURSERY, \
  4. MODE_HOME, TYPE_RESOURCE_EATABLE, MODE_GOHOME
  5. from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
  6. from synergine_xyz.cst import POSITION, POSITIONS
  7. class AntPutBrainPart(TransportBrainPart):
  8. _mode_matches = TransportBrainPart._mode_matches.copy()
  9. _mode_matches.update({
  10. MODE_NURSE: [TYPE_NURSERY],
  11. MODE_HOME: [TYPE_RESOURCE_EATABLE],
  12. MODE_GOHOME: []
  13. })
  14. _types_matches = {
  15. TYPE_RESOURCE_EXPLOITABLE: [TYPE_RESOURCE_EATABLE],
  16. TYPE_NURSERY: [TYPE_NURSERY]
  17. }
  18. @classmethod
  19. def can_put(cls, context, object_id, object_near_id):
  20. # Si l'objet à coté fait partie des objets concernés par le mode du porteur
  21. if cls._match_with_mode(context, object_id, object_near_id):
  22. # Et si les objet sont rangeable enssemble:
  23. object_carried_id = context.metas.value.get(CARRIED, object_id)
  24. return cls._objects_types_match(context, object_carried_id, object_near_id)
  25. return False
  26. @classmethod
  27. def get_put_position(cls, context, object_id, object_near_id):
  28. """
  29. Maybe part "found available position arround" should be in other class.
  30. :param context:
  31. :param object_id:
  32. :param object_near_id:
  33. :return:
  34. """
  35. obj_near_position = context.metas.value.get(POSITION, object_near_id)
  36. if cls._is_available_position(context, obj_near_position):
  37. return obj_near_position
  38. obj_transporter_position = context.metas.value.get(POSITION, object_id)
  39. obj_transporter_around_positions = context.get_around_points_of_point(obj_transporter_position)
  40. obj_near_around_positions = context.get_around_points_of_point(obj_near_position)
  41. # For each position between target and current transporter
  42. for pos_around_target in obj_near_around_positions:
  43. if pos_around_target in obj_transporter_around_positions:
  44. if cls._is_available_position(context, pos_around_target):
  45. return pos_around_target
  46. raise CantFindWhereToPut()
  47. @staticmethod
  48. def _is_available_position(context, position):
  49. if not context.position_is_penetrable(position):
  50. return False
  51. count_obj_here = len(context.metas.list.get(POSITIONS, position, allow_empty=True))
  52. if count_obj_here <= Core.get_configuration_manager().get('ant.put.max_objects_at_same_position', 5):
  53. return True
  54. return False
  55. def done(self, puted_object):
  56. # TODO: lancer le choix d'un nouveau mode dans le brain.
  57. self._host.get_brain().switch_to_mode(MODE_EXPLO)