Browse Source

remove pheromones

Bastien Sevajol 9 years ago
parent
commit
b7f83923a5

+ 2 - 1
intelligine/cst.py View File

9
 TRANSPORTABLE = IncrementedNamedInt.get('intelligine.transportable')
9
 TRANSPORTABLE = IncrementedNamedInt.get('intelligine.transportable')
10
 TRANSPORTER = IncrementedNamedInt.get('intelligine.transporter')
10
 TRANSPORTER = IncrementedNamedInt.get('intelligine.transporter')
11
 CARRYING = IncrementedNamedInt.get('intelligine.carrying')
11
 CARRYING = IncrementedNamedInt.get('intelligine.carrying')
12
-CARRIED = IncrementedNamedInt.get('intelligine.carried')
12
+CARRIED_BY = IncrementedNamedInt.get('intelligine.carried_by')
13
+CARRY = IncrementedNamedInt.get('intelligine.carry')
13
 CANT_CARRY_STILL = IncrementedNamedInt.get('intelligine.cantcarry.still')
14
 CANT_CARRY_STILL = IncrementedNamedInt.get('intelligine.cantcarry.still')
14
 CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
15
 CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
15
 ACTION_DIE = IncrementedNamedInt.get('intelligine.basebug.action.die')
16
 ACTION_DIE = IncrementedNamedInt.get('intelligine.basebug.action.die')

+ 33 - 6
intelligine/simulation/molecule/Evaporation.py View File

4
 
4
 
5
 class Evaporation:
5
 class Evaporation:
6
 
6
 
7
-    def __init__(self, context, intensity_decrement, molecules_exclude_types, molecule_minimum_age):
7
+    def __init__(self, context,
8
+                 intensity_decrement=1,
9
+                 molecule_minimum_age=0,
10
+                 molecules_exclude_types=None,
11
+                 molecules_include_types=None):
8
         self._context = context
12
         self._context = context
9
         self._intensity_decrement = intensity_decrement
13
         self._intensity_decrement = intensity_decrement
10
         self._molecules_manager = MoleculesManager(context)
14
         self._molecules_manager = MoleculesManager(context)
11
-        self._molecules_exclude_types = molecules_exclude_types
12
         self._molecule_minimum_age = molecule_minimum_age
15
         self._molecule_minimum_age = molecule_minimum_age
16
+        self._molecules_exclude_types = molecules_exclude_types
17
+        self._molecules_include_types = molecules_include_types
13
 
18
 
14
     def evaporate(self):
19
     def evaporate(self):
15
         for position, flavour in self._get_flavours():
20
         for position, flavour in self._get_flavours():
16
             self._decrease_flavour(flavour)
21
             self._decrease_flavour(flavour)
17
             self._molecules_manager.set_flavour(position, flavour)
22
             self._molecules_manager.set_flavour(position, flavour)
18
 
23
 
24
+    def remove(self):
25
+        for position, flavour in self._get_flavours():
26
+            self._remove_molecule(flavour)
27
+            self._molecules_manager.set_flavour(position, flavour)
28
+
19
     def _get_flavours(self):
29
     def _get_flavours(self):
20
-        molecules_points = self._context.metas.list.get(MOLECULES, MOLECULES)
30
+        molecules_points = self._context.metas.list.get(MOLECULES, MOLECULES, allow_empty=True)
21
         for molecule_point in molecules_points:
31
         for molecule_point in molecules_points:
22
             yield molecule_point, self._molecules_manager.get_flavour(molecule_point)
32
             yield molecule_point, self._molecules_manager.get_flavour(molecule_point)
23
 
33
 
24
     def _decrease_flavour(self, flavour):
34
     def _decrease_flavour(self, flavour):
35
+        for direction_molecule in self._get_molecules_from_flavour(flavour):
36
+            direction_molecule.increment_intensity(-self._intensity_decrement)
37
+            flavour.set_molecule(direction_molecule)
38
+
39
+    def _get_molecules_from_flavour(self, flavour):
40
+        molecules = []
25
         for direction_molecule in flavour.get_molecules(MOLECULES_DIRECTION):
41
         for direction_molecule in flavour.get_molecules(MOLECULES_DIRECTION):
26
             if not self._is_recent_molecule(direction_molecule) \
42
             if not self._is_recent_molecule(direction_molecule) \
27
                and not self._is_excluded_molecule_type(direction_molecule):
43
                and not self._is_excluded_molecule_type(direction_molecule):
28
-                direction_molecule.increment_intensity(-self._intensity_decrement)
29
-                flavour.set_molecule(direction_molecule)
44
+                molecules.append(direction_molecule)
45
+        return molecules
46
+
47
+    def _remove_molecule(self, flavour):
48
+        for direction_molecule in self._get_molecules_from_flavour(flavour):
49
+            flavour.remove_molecule(direction_molecule)
30
 
50
 
31
     def _is_recent_molecule(self, molecule):
51
     def _is_recent_molecule(self, molecule):
32
         return (self._context.get_cycle() - molecule.get_cycle_age()) < self._molecule_minimum_age
52
         return (self._context.get_cycle() - molecule.get_cycle_age()) < self._molecule_minimum_age
33
 
53
 
34
     def _is_excluded_molecule_type(self, molecule):
54
     def _is_excluded_molecule_type(self, molecule):
35
-        return molecule.get_type() in self._molecules_exclude_types
55
+        if not self._molecules_exclude_types and not self._molecules_include_types:
56
+            return False
57
+
58
+        if self._molecules_exclude_types and not self._molecules_include_types:
59
+            return molecule.get_type() in self._molecules_exclude_types
60
+
61
+        if not self._molecules_exclude_types and self._molecules_include_types:
62
+            return molecule.get_type() not in self._molecules_include_types

+ 1 - 1
intelligine/simulation/molecule/Molecule.py View File

26
         return self._cycle_age
26
         return self._cycle_age
27
 
27
 
28
     def increment_intensity(self, increment_value):
28
     def increment_intensity(self, increment_value):
29
-        self._intensity += increment_value
29
+        self._intensity += increment_value

+ 6 - 1
intelligine/simulation/molecule/MoleculeFlavour.py View File

68
         if category not in self._flavour:
68
         if category not in self._flavour:
69
             self._flavour[category] = {}
69
             self._flavour[category] = {}
70
 
70
 
71
-        self._flavour[category][type] = molecule
71
+        self._flavour[category][type] = molecule
72
+
73
+    def remove_molecule(self, molecule):
74
+        category = molecule.get_category()
75
+        type = molecule.get_type()
76
+        del(self._flavour[category][type])

+ 3 - 3
intelligine/simulation/object/brain/part/transport/AntPutBrainPart.py View File

1
 from intelligine.synergy.object.Food import Food
1
 from intelligine.synergy.object.Food import Food
2
 from synergine.core.Core import Core
2
 from synergine.core.Core import Core
3
 from intelligine.core.exceptions import CantFindWhereToPut
3
 from intelligine.core.exceptions import CantFindWhereToPut
4
-from intelligine.cst import MODE_EXPLO, TYPE_RESOURCE_EXPLOITABLE, CARRIED, MODE_NURSE, TYPE_NURSERY, \
5
-    MODE_HOME, TYPE_RESOURCE_EATABLE, MODE_GOHOME
4
+from intelligine.cst import MODE_EXPLO, TYPE_RESOURCE_EXPLOITABLE, MODE_NURSE, TYPE_NURSERY, \
5
+    MODE_HOME, TYPE_RESOURCE_EATABLE, MODE_GOHOME, CARRY
6
 from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
6
 from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
7
 from synergine_xyz.cst import POSITION, POSITIONS
7
 from synergine_xyz.cst import POSITION, POSITIONS
8
 
8
 
26
         # Si l'objet à coté fait partie des objets concernés par le mode du porteur
26
         # Si l'objet à coté fait partie des objets concernés par le mode du porteur
27
         if cls._match_with_mode(context, object_id, object_near_id):
27
         if cls._match_with_mode(context, object_id, object_near_id):
28
             # Et si les objet sont rangeable enssemble:
28
             # Et si les objet sont rangeable enssemble:
29
-            object_carried_id = context.metas.value.get(CARRIED, object_id)
29
+            object_carried_id = context.metas.value.get(CARRY, object_id)
30
             return cls._objects_types_match(context, object_carried_id, object_near_id)
30
             return cls._objects_types_match(context, object_carried_id, object_near_id)
31
         return False
31
         return False
32
 
32
 

+ 5 - 11
intelligine/synergy/event/smell/SmellAction.py View File

1
 from intelligine.core.exceptions import BestMoleculeHere
1
 from intelligine.core.exceptions import BestMoleculeHere
2
-from intelligine.cst import POINT_SMELL, POINTS_SMELL, MOLECULES_INFOS, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG
2
+from intelligine.cst import POINT_SMELL, POINTS_SMELL, MOLECULES_INFOS, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG, \
3
+    PHEROMON_DIR_EXPLO
3
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
4
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
5
+from intelligine.simulation.molecule.Evaporation import Evaporation
4
 from intelligine.simulation.molecule.Molecule import Molecule
6
 from intelligine.simulation.molecule.Molecule import Molecule
5
 from intelligine.synergy.event.smell.SmellEvent import SmellEvent
7
 from intelligine.synergy.event.smell.SmellEvent import SmellEvent
6
 from synergine.synergy.event.Action import Action
8
 from synergine.synergy.event.Action import Action
12
 
14
 
13
     @classmethod
15
     @classmethod
14
     def cycle_pre_run(cls, context, synergy_manager):
16
     def cycle_pre_run(cls, context, synergy_manager):
15
-        smell_positions = context.metas.list.get(POINTS_SMELL, POINTS_SMELL, allow_empty=True)
16
-        for smell_position in smell_positions:
17
-            # TODO: Remonter ca dans MoleculeManager ?
18
-            flavour_raw_data = context.metas.value.get(MOLECULES_INFOS, smell_position)
19
-            # TODO: Calculer ou definir qqpart la liste des smells
20
-            for smell_type in (SMELL_FOOD, SMELL_EGG):
21
-                if smell_type in flavour_raw_data:
22
-                    del(flavour_raw_data[smell_type])
23
-                context.metas.value.set(MOLECULES_INFOS, smell_position, flavour_raw_data)
24
-        context.metas.list.unset(POINTS_SMELL, POINTS_SMELL, allow_empty=True)
17
+        evaporation = Evaporation(context, molecules_include_types=[SMELL_FOOD, SMELL_EGG])
18
+        evaporation.remove()
25
 
19
 
26
     def run(self, obj, context, synergy_manager):
20
     def run(self, obj, context, synergy_manager):
27
 
21
 

+ 6 - 2
intelligine/synergy/event/smell/SmellEvent.py View File

1
-from intelligine.cst import COL_SMELL
1
+from intelligine.cst import COL_SMELL, CARRIED_BY
2
 from intelligine.mechanism.TraversableDistanceFromMechanism import TraversableDistanceFromMechanism
2
 from intelligine.mechanism.TraversableDistanceFromMechanism import TraversableDistanceFromMechanism
3
 from intelligine.synergy.event.Event import Event
3
 from intelligine.synergy.event.Event import Event
4
 from synergine.core.exceptions import NotConcernedEvent
4
 from synergine.core.exceptions import NotConcernedEvent
12
     _first_cycle_force = True
12
     _first_cycle_force = True
13
 
13
 
14
     def _prepare(self, object_id, context, parameters={}):
14
     def _prepare(self, object_id, context, parameters={}):
15
-        if not parameters['points_distances']:
15
+        if not parameters['points_distances'] or not self._concerned_object(context, object_id):
16
             raise NotConcernedEvent()
16
             raise NotConcernedEvent()
17
 
17
 
18
         return parameters
18
         return parameters
19
+
20
+    def _concerned_object(self, context, object_id):
21
+        # TODO: Un peu hardcodé etant donné que cet event concerne tout les COL_SMELL et pas que les transportable ...
22
+        return not context.metas.value.get(CARRIED_BY, object_id, allow_empty=True, empty_value=False)

+ 3 - 3
intelligine/synergy/object/Transportable.py View File

1
-from intelligine.cst import TRANSPORTABLE, CARRIED
1
+from intelligine.cst import TRANSPORTABLE, CARRIED_BY, CARRY
2
 from intelligine.synergy.object.SynergyObject import SynergyObject
2
 from intelligine.synergy.object.SynergyObject import SynergyObject
3
 
3
 
4
 
4
 
34
     def set_is_carried(self, is_carried, by_obj):
34
     def set_is_carried(self, is_carried, by_obj):
35
         self._is_carried = bool(is_carried)
35
         self._is_carried = bool(is_carried)
36
         if self._is_carried:
36
         if self._is_carried:
37
-            self._context.metas.value.set(CARRIED, subject=by_obj.get_id(), value=self.get_id())
37
+            self._context.metas.value.set(CARRIED_BY, self.get_id(), by_obj.get_id())
38
         else:
38
         else:
39
-            self._context.metas.value.unset(CARRIED, subject=by_obj.get_id())
39
+            self._context.metas.value.unset(CARRIED_BY, self.get_id())

+ 4 - 2
intelligine/synergy/object/ant/Ant.py View File

2
 from intelligine.synergy.object.Bug import Bug
2
 from intelligine.synergy.object.Bug import Bug
3
 from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
3
 from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
4
     COL_FIGHTER, MODE_EXPLO, MODE_GOHOME, BODY_PART_PHEROMONE_GLAND, TYPE, TYPE_ANT, \
4
     COL_FIGHTER, MODE_EXPLO, MODE_GOHOME, BODY_PART_PHEROMONE_GLAND, TYPE, TYPE_ANT, \
5
-    COL_TRANSPORTER_CARRYING, MODE_NURSE, MODE_HOME
5
+    COL_TRANSPORTER_CARRYING, MODE_NURSE, MODE_HOME, CARRY
6
 from intelligine.synergy.object.Food import Food
6
 from intelligine.synergy.object.Food import Food
7
 from intelligine.simulation.object.molecule.MovementMoleculeGland import MovementMoleculeGland
7
 from intelligine.simulation.object.molecule.MovementMoleculeGland import MovementMoleculeGland
8
 from intelligine.simulation.object.brain.AntBrain import AntBrain
8
 from intelligine.simulation.object.brain.AntBrain import AntBrain
23
                                                            COL_FIGHTER])
23
                                                            COL_FIGHTER])
24
         self._carried = None
24
         self._carried = None
25
         #  TODO: Comme pour lorsque une action put est faite, lancer un algo de choix de la mission a suivre.
25
         #  TODO: Comme pour lorsque une action put est faite, lancer un algo de choix de la mission a suivre.
26
-        self._brain.switch_to_mode(MODE_EXPLO)
26
+        self._brain.switch_to_mode(MODE_NURSE)
27
         context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
27
         context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
28
 
28
 
29
     def die(self):
29
     def die(self):
45
         obj.set_position(position)
45
         obj.set_position(position)
46
         obj.set_is_carried(False, self)
46
         obj.set_is_carried(False, self)
47
         self._context.metas.states.remove(self.get_id(), CARRYING)
47
         self._context.metas.states.remove(self.get_id(), CARRYING)
48
+        self._context.metas.value.unset(CARRY, self.get_id())
48
         self._add_col(COL_TRANSPORTER_NOT_CARRYING)
49
         self._add_col(COL_TRANSPORTER_NOT_CARRYING)
49
         self._remove_col(COL_TRANSPORTER_CARRYING)
50
         self._remove_col(COL_TRANSPORTER_CARRYING)
50
 
51
 
57
         self._add_col(COL_TRANSPORTER_CARRYING)
58
         self._add_col(COL_TRANSPORTER_CARRYING)
58
         self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
59
         self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
59
         obj.set_is_carried(True, self)
60
         obj.set_is_carried(True, self)
61
+        self._context.metas.value.set(CARRY, self.get_id(), obj.get_id())
60
         # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
62
         # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
61
         if isinstance(obj, Food):
63
         if isinstance(obj, Food):
62
             self.get_brain().switch_to_mode(MODE_GOHOME)
64
             self.get_brain().switch_to_mode(MODE_GOHOME)