AntPutBrainPart.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from intelligine.core.exceptions import CantFindWhereToPut
  2. from intelligine.simulation.object.brain.part.transport.TakeBrainPart import TakeBrainPart
  3. from intelligine.cst import MOVE_MODE_EXPLO, MOVE_MODE, TYPE_RESOURCE_TRANSFORMABLE, TYPE, CARRIED, \
  4. COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING
  5. from xyzworld.cst import POSITION, POSITIONS
  6. class AntPutBrainPart(TakeBrainPart):
  7. # TODO: methode __nit_ pour la classe ?
  8. _mode_matches = {
  9. MOVE_MODE_EXPLO: [TYPE_RESOURCE_TRANSFORMABLE],
  10. }
  11. @classmethod
  12. def can_put(cls, context, object_id, object_near_id):
  13. object_carried_id = context.metas.value.get(CARRIED, object_id)
  14. #  Pour le moment on considere qu'un objet peut-etre depose a cote d'un objet qui a un type identique
  15. return cls._objects_have_same_type(context, object_carried_id, object_near_id)
  16. @classmethod
  17. def get_put_position(cls, context, object_id, object_near_id):
  18. """
  19. Maybe part "found available position arround" should be in other class.
  20. :param context:
  21. :param object_id:
  22. :param object_near_id:
  23. :return:
  24. """
  25. obj_near_position = context.metas.value.get(POSITION, object_near_id)
  26. if cls._is_available_position(context, obj_near_position):
  27. return obj_near_position
  28. obj_transporter_position = context.metas.value.get(POSITION, object_id)
  29. obj_transporter_around_positions = context.get_around_points_of_point(obj_transporter_position)
  30. obj_near_around_positions = context.get_around_points_of_point(obj_near_position)
  31. # For each position between target and current transporter
  32. for pos_around_target in obj_near_around_positions:
  33. if pos_around_target in obj_transporter_around_positions:
  34. if cls._is_available_position(context, pos_around_target):
  35. return pos_around_target
  36. raise CantFindWhereToPut()
  37. @staticmethod
  38. def _is_available_position(context, position):
  39. # TODO: Pour le moment on ne regarde pas si ce sont tous des obj identique
  40. count_obj_here = len(context.metas.list.get(POSITIONS, position, allow_empty=True))
  41. # TODO: 5 est hardcode; de plus cette cntrainte (not brain) devrait dependre de l'objet, du contexte ...
  42. if count_obj_here <= 5 and (context.position_is_penetrable(position)): # TODO TEST !!!
  43. return True
  44. return False
  45. def done(self, obj, puted_object, context):
  46. # TODO: Depose au -1 pour des raisons de test. Plus tard ce sera des tas comme un autre !
  47. puted_object.set_position((-1, 0, 0))
  48. obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)