Browse Source

remove pheromones

Bastien Sevajol 8 years ago
parent
commit
b7f83923a5

+ 2 - 1
intelligine/cst.py View File

@@ -9,7 +9,8 @@ COLONY = IncrementedNamedInt.get('intelligine.colony')
9 9
 TRANSPORTABLE = IncrementedNamedInt.get('intelligine.transportable')
10 10
 TRANSPORTER = IncrementedNamedInt.get('intelligine.transporter')
11 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 14
 CANT_CARRY_STILL = IncrementedNamedInt.get('intelligine.cantcarry.still')
14 15
 CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
15 16
 ACTION_DIE = IncrementedNamedInt.get('intelligine.basebug.action.die')

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

@@ -4,32 +4,59 @@ from intelligine.synergy.stigmergy.MoleculesManager import MoleculesManager
4 4
 
5 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 12
         self._context = context
9 13
         self._intensity_decrement = intensity_decrement
10 14
         self._molecules_manager = MoleculesManager(context)
11
-        self._molecules_exclude_types = molecules_exclude_types
12 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 19
     def evaporate(self):
15 20
         for position, flavour in self._get_flavours():
16 21
             self._decrease_flavour(flavour)
17 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 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 31
         for molecule_point in molecules_points:
22 32
             yield molecule_point, self._molecules_manager.get_flavour(molecule_point)
23 33
 
24 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 41
         for direction_molecule in flavour.get_molecules(MOLECULES_DIRECTION):
26 42
             if not self._is_recent_molecule(direction_molecule) \
27 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 51
     def _is_recent_molecule(self, molecule):
32 52
         return (self._context.get_cycle() - molecule.get_cycle_age()) < self._molecule_minimum_age
33 53
 
34 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,4 +26,4 @@ class Molecule():
26 26
         return self._cycle_age
27 27
 
28 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,4 +68,9 @@ class MoleculeFlavour():
68 68
         if category not in self._flavour:
69 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,8 +1,8 @@
1 1
 from intelligine.synergy.object.Food import Food
2 2
 from synergine.core.Core import Core
3 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 6
 from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
7 7
 from synergine_xyz.cst import POSITION, POSITIONS
8 8
 
@@ -26,7 +26,7 @@ class AntPutBrainPart(TransportBrainPart):
26 26
         # Si l'objet à coté fait partie des objets concernés par le mode du porteur
27 27
         if cls._match_with_mode(context, object_id, object_near_id):
28 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 30
             return cls._objects_types_match(context, object_carried_id, object_near_id)
31 31
         return False
32 32
 

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

@@ -1,6 +1,8 @@
1 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 4
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
5
+from intelligine.simulation.molecule.Evaporation import Evaporation
4 6
 from intelligine.simulation.molecule.Molecule import Molecule
5 7
 from intelligine.synergy.event.smell.SmellEvent import SmellEvent
6 8
 from synergine.synergy.event.Action import Action
@@ -12,16 +14,8 @@ class SmellAction(Action):
12 14
 
13 15
     @classmethod
14 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 20
     def run(self, obj, context, synergy_manager):
27 21
 

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

@@ -1,4 +1,4 @@
1
-from intelligine.cst import COL_SMELL
1
+from intelligine.cst import COL_SMELL, CARRIED_BY
2 2
 from intelligine.mechanism.TraversableDistanceFromMechanism import TraversableDistanceFromMechanism
3 3
 from intelligine.synergy.event.Event import Event
4 4
 from synergine.core.exceptions import NotConcernedEvent
@@ -12,7 +12,11 @@ class SmellEvent(Event):
12 12
     _first_cycle_force = True
13 13
 
14 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 16
             raise NotConcernedEvent()
17 17
 
18 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,4 +1,4 @@
1
-from intelligine.cst import TRANSPORTABLE, CARRIED
1
+from intelligine.cst import TRANSPORTABLE, CARRIED_BY, CARRY
2 2
 from intelligine.synergy.object.SynergyObject import SynergyObject
3 3
 
4 4
 
@@ -34,6 +34,6 @@ class Transportable(SynergyObject):
34 34
     def set_is_carried(self, is_carried, by_obj):
35 35
         self._is_carried = bool(is_carried)
36 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 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,7 +2,7 @@ from intelligine.core.exceptions import MoleculeException
2 2
 from intelligine.synergy.object.Bug import Bug
3 3
 from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
4 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 6
 from intelligine.synergy.object.Food import Food
7 7
 from intelligine.simulation.object.molecule.MovementMoleculeGland import MovementMoleculeGland
8 8
 from intelligine.simulation.object.brain.AntBrain import AntBrain
@@ -23,7 +23,7 @@ class Ant(Bug):
23 23
                                                            COL_FIGHTER])
24 24
         self._carried = None
25 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 27
         context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
28 28
 
29 29
     def die(self):
@@ -45,6 +45,7 @@ class Ant(Bug):
45 45
         obj.set_position(position)
46 46
         obj.set_is_carried(False, self)
47 47
         self._context.metas.states.remove(self.get_id(), CARRYING)
48
+        self._context.metas.value.unset(CARRY, self.get_id())
48 49
         self._add_col(COL_TRANSPORTER_NOT_CARRYING)
49 50
         self._remove_col(COL_TRANSPORTER_CARRYING)
50 51
 
@@ -57,6 +58,7 @@ class Ant(Bug):
57 58
         self._add_col(COL_TRANSPORTER_CARRYING)
58 59
         self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
59 60
         obj.set_is_carried(True, self)
61
+        self._context.metas.value.set(CARRY, self.get_id(), obj.get_id())
60 62
         # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
61 63
         if isinstance(obj, Food):
62 64
             self.get_brain().switch_to_mode(MODE_GOHOME)