AntPutBrainPart.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. @staticmethod
  49. def _is_available_position(context, position):
  50. if not context.position_is_penetrable(position):
  51. return False
  52. count_obj_here = len(context.metas.list.get(POSITIONS, position, allow_empty=True))
  53. if count_obj_here <= Core.get_configuration_manager().get('ant.put.max_objects_at_same_position', 5):
  54. return True
  55. return False
  56. def done(self, puted_object):
  57. # TODO: lancer le choix d'un nouveau mode dans le brain.
  58. if isinstance(puted_object, Food):
  59. self._host.get_brain().switch_to_mode(MODE_EXPLO)