PutableEvent.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from intelligine.core.exceptions import NearNothingFound, CantFindWhereToPut
  2. from intelligine.synergy.event.src.NearEvent import NearEvent
  3. from synergine.core.exceptions import NotConcernedEvent
  4. from intelligine.cst import CANT_PUT_STILL, COL_TRANSPORTER_CARRYING, TRANSPORTABLE, BRAIN_SCHEMA, BRAIN_PART_PUT
  5. from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
  6. class PutableEvent(NearEvent):
  7. """
  8. TODO: Refactorise with TakableEvent
  9. """
  10. PARAM_PUT = 'put'
  11. PARAM_PUT_TO = 'put_to'
  12. concern = COL_TRANSPORTER_CARRYING
  13. _near_name = 'objects_ids_putable'
  14. _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, TRANSPORTABLE)
  15. def __init__(self, actions):
  16. super().__init__(actions)
  17. self._mechanism = ArroundMechanism
  18. def _prepare(self, object_id, context, parameters={}):
  19. if not self._can_put(object_id, context):
  20. raise NotConcernedEvent()
  21. try:
  22. self.map(context, parameters, stop_at_first=True)
  23. except NearNothingFound:
  24. raise NotConcernedEvent()
  25. object_near_id = parameters[self._near_name][0]
  26. brain_part = self._get_brain_part(context, object_id, BRAIN_PART_PUT)
  27. if not brain_part.can_put(context, object_id, object_near_id):
  28. raise NotConcernedEvent()
  29. try:
  30. put_position = brain_part.get_put_position(context, object_id, object_near_id)
  31. except CantFindWhereToPut:
  32. raise NotConcernedEvent()
  33. parameters[self.PARAM_PUT] = parameters[self._near_name][0]
  34. parameters[self.PARAM_PUT_TO] = put_position
  35. return parameters
  36. @staticmethod
  37. def _can_put(object_id, context):
  38. return not context.metas.value.get(CANT_PUT_STILL, object_id, allow_empty=True)
  39. @classmethod
  40. def _object_can_put(cls, object_id, context, object_to_put_id):
  41. object_take_brain_part = cls._get_brain_part(context, object_id, BRAIN_PART_PUT)
  42. return object_take_brain_part.can_put(context, object_id, object_to_put_id)