AntPutBrainPart.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from intelligine.core.exceptions import CantFindWhereToPut
  2. from intelligine.cst import MOVE_MODE_EXPLO, TYPE_RESOURCE_TRANSFORMABLE, CARRIED
  3. from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
  4. from intelligine.synergy.object.Food import Food
  5. from synergine_xyz.cst import POSITION, POSITIONS
  6. class AntPutBrainPart(TransportBrainPart):
  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) or position == (0, 0, 0)): # TODO TEST !!!
  43. return True
  44. return False
  45. def done(self, obj, puted_object, context):
  46. # TODO: Il faut refact/logique qqpart pour ca !! Genre Brain.done(PUT, ??)
  47. if isinstance(puted_object, Food):
  48. obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
  49. # TODO: TEST Depose au -1 pour des raisons de test. Plus tard ce sera des tas comme un autre !
  50. puted_object.set_position((-1, 0, 0))