TakeableEvent.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from intelligine.core.exceptions import NearNothingFound
  2. from synergine.core.exceptions import NotConcernedEvent
  3. from intelligine.synergy.event.src.NearEvent import NearEvent
  4. from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
  5. from intelligine.cst import TRANSPORTABLE, CANT_CARRY_STILL, COL_TRANSPORTER_NOT_CARRYING, BRAIN_SCHEMA, BRAIN_PART_TAKE
  6. class TakeableEvent(NearEvent):
  7. PARAM_TAKE = 'take'
  8. concern = COL_TRANSPORTER_NOT_CARRYING
  9. _near_name = 'objects_ids_transportable'
  10. _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, TRANSPORTABLE)
  11. def __init__(self, actions):
  12. super().__init__(actions)
  13. self._mechanism = ArroundMechanism
  14. def _prepare(self, object_id, context, parameters={}):
  15. if not self._can_carry(object_id, context):
  16. raise NotConcernedEvent()
  17. try:
  18. self.map(context, parameters, stop_at_first=True)
  19. except NearNothingFound:
  20. raise NotConcernedEvent()
  21. if not self._object_can_take(object_id, context, parameters[self._near_name][0]):
  22. raise NotConcernedEvent()
  23. parameters[self.PARAM_TAKE] = parameters[self._near_name][0]
  24. return parameters
  25. @staticmethod
  26. def _can_carry(object_id, context):
  27. return not context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True)
  28. @staticmethod
  29. def _object_can_take(object_id, context, object_to_take_id):
  30. object_brain_schema = context.metas.value.get(BRAIN_SCHEMA, object_id)
  31. object_take_brain_part = object_brain_schema[BRAIN_PART_TAKE]
  32. return object_take_brain_part.can_take(context, object_id, object_to_take_id)