AntPutBrainPart.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from intelligine.core.exceptions import CantFindWhereToPut
  2. from intelligine.cst import MOVE_MODE_EXPLO, TYPE_RESOURCE_EXPLOITABLE, CARRIED, MOVE_MODE_NURSE, TYPE_NURSERY, \
  3. MOVE_MODE_HOME, TYPE_RESOURCE_EATABLE, TYPE, MOVE_MODE, MOVE_MODE_GOHOME
  4. from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
  5. from intelligine.synergy.object.Food import Food
  6. from synergine_xyz.cst import POSITION, POSITIONS
  7. class AntPutBrainPart(TransportBrainPart):
  8. # TODO: methode __nit_ pour la classe ?
  9. _mode_matches = {
  10. MOVE_MODE_NURSE: [TYPE_NURSERY],
  11. MOVE_MODE_HOME: [TYPE_RESOURCE_EATABLE],
  12. MOVE_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. # TODO: Pour le moment on ne regarde pas si ce sont tous des obj identique
  49. count_obj_here = len(context.metas.list.get(POSITIONS, position, allow_empty=True))
  50. # TODO: 5 est hardcode; de plus cette cntrainte (not brain) devrait dependre de l'objet, du contexte ...
  51. if count_obj_here <= 5 and (context.position_is_penetrable(position) or position == (0, 0, 0)): # TODO TEST !!!
  52. return True
  53. return False
  54. def done(self, obj, puted_object, context):
  55. # TODO: Il faut refact/logique qqpart pour ca !! Genre Brain.done(PUT, ??)
  56. if isinstance(puted_object, Food):
  57. # TODO: Quel mode ? On vient de poser (ps forcement dans la colonie) cls._mode_swicth ?
  58. obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
  59. # TODO: TEST Depose au -1 pour des raisons de test. Plus tard ce sera des tas comme un autre !
  60. puted_object.set_position((-1, 0, 0))