AntPutBrainPart.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. }
  17. @classmethod
  18. def can_put(cls, context, object_id, object_near_id):
  19. # Si l'objet à coté fait partie des objets concernés par le mode du porteur
  20. if cls._match_with_mode(context, object_id, object_near_id):
  21. # Et si les objet sont rangeable enssemble:
  22. object_carried_id = context.metas.value.get(CARRIED, object_id)
  23. return cls._objects_types_match(context, object_carried_id, object_near_id)
  24. return False
  25. @classmethod
  26. def get_put_position(cls, context, object_id, object_near_id):
  27. """
  28. Maybe part "found available position arround" should be in other class.
  29. :param context:
  30. :param object_id:
  31. :param object_near_id:
  32. :return:
  33. """
  34. obj_near_position = context.metas.value.get(POSITION, object_near_id)
  35. if cls._is_available_position(context, obj_near_position):
  36. return obj_near_position
  37. obj_transporter_position = context.metas.value.get(POSITION, object_id)
  38. obj_transporter_around_positions = context.get_around_points_of_point(obj_transporter_position)
  39. obj_near_around_positions = context.get_around_points_of_point(obj_near_position)
  40. # For each position between target and current transporter
  41. for pos_around_target in obj_near_around_positions:
  42. if pos_around_target in obj_transporter_around_positions:
  43. if cls._is_available_position(context, pos_around_target):
  44. return pos_around_target
  45. raise CantFindWhereToPut()
  46. @staticmethod
  47. def _is_available_position(context, position):
  48. if not context.position_is_penetrable(position):
  49. return False
  50. count_obj_here = len(context.metas.list.get(POSITIONS, position, allow_empty=True))
  51. if count_obj_here <= Core.get_configuration_manager().get('ant.put.max_objects_at_same_position', 5):
  52. return True
  53. return False
  54. def done(self, puted_object):
  55. # TODO: lancer le choix d'un nouveau mode dans le brain.
  56. self._host.get_brain().switch_to_mode(MODE_EXPLO)