PutableEvent._prepare()   C
last analyzed

Complexity

Conditions 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 5.5
cc 7
1
from intelligine.core.exceptions import NearNothingFound, CantFindWhereToPut
2
from intelligine.shorcut.brain import get_brain_part
3
from intelligine.simulation.object.brain.part.move.AntMoveBrainPart import AntMoveBrainPart
4
from intelligine.synergy.event.src.NearEvent import NearEvent
5
from synergine.core.exceptions import NotConcernedEvent
6
from intelligine.cst import CANT_PUT_STILL, COL_TRANSPORTER_CARRYING, TRANSPORTABLE, BRAIN_SCHEMA, BRAIN_PART_PUT
7
from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
8
9
10
class PutableEvent(NearEvent):
11
    """
12
    TODO: Refactorise with TakableEvent
13
    """
14
15
    PARAM_PUT = 'put'
16
    PARAM_PUT_TO = 'put_to'
17
    PARAM_HOME_FAIL = 'home_fail'
18
    _mechanism = AroundMechanism
19
    _concern = COL_TRANSPORTER_CARRYING
20
    _near_name = 'objects_ids_putable'
21
    _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, TRANSPORTABLE)
22
23
    def _prepare(self, object_id, context, parameters={}):
24
        if not self._can_put(object_id, context):
25
            raise NotConcernedEvent()
26
27
        try:
28
            self.map(context, parameters, stop_at_first=False)
29
        except NearNothingFound:
30
            raise NotConcernedEvent()
31
32
        brain_part = get_brain_part(context, object_id, BRAIN_PART_PUT)
33
        parameters[self.PARAM_PUT] = []
34
        parameters[self.PARAM_PUT_TO] = []
35
        parameters[self.PARAM_HOME_FAIL] = False
36
37
        for object_near_id in parameters[self._near_name]:
38
            if brain_part.can_put(context, object_id, object_near_id):
39
                try:
40
                    put_position = brain_part.get_put_position(context, object_id, object_near_id)
41
                    parameters[self.PARAM_PUT].append(object_near_id)
42
                    parameters[self.PARAM_PUT_TO].append(put_position)
43
                    return parameters  # Si a terme on veut tous les calculer a l'avance, ne pas retourner ici
44
                except CantFindWhereToPut:
45
                    pass  # On continu la booucle
46
47
        # Si a terme on veut tous les calculer a l'avance, raise que si rien trouve
48
        if AntMoveBrainPart._on_home_smell(context, object_id):  # TODO: Reanger code (._ acces protege)
49
            parameters[self.PARAM_HOME_FAIL] = True
50
        return parameters
51
52
    @staticmethod
53
    def _can_put(object_id, context):
54
        return not context.metas.value.get(CANT_PUT_STILL, object_id, allow_empty=True)
55
56
    @classmethod
57
    def _object_can_put(cls, object_id, context, object_to_put_id):
58
        object_take_brain_part = get_brain_part(context, object_id, BRAIN_PART_PUT)
59
        return object_take_brain_part.can_put(context, object_id, object_to_put_id)
60