Kaynağa Gözat

improve brain

Bastien Sevajol 9 yıl önce
ebeveyn
işleme
631cc3aabd

+ 1 - 0
intelligine/cst.py Dosyayı Görüntüle

@@ -58,6 +58,7 @@ COL_TRANSPORTER_NOT_CARRYING = IncrementedNamedInt.get('intelligine.col.transpor
58 58
 COL_EATABLE = IncrementedNamedInt.get('intelligine.col.eatable')
59 59
 COL_SMELL = IncrementedNamedInt.get('intelligine.col.smell')
60 60
 
61
+BRAIN = IncrementedNamedInt.get('intelligine.brain')
61 62
 BRAIN_SCHEMA = IncrementedNamedInt.get('intelligine.brain_schema')
62 63
 BRAIN_PART_MOVE = IncrementedNamedInt.get('intelligine.brain.part.move')
63 64
 BRAIN_PART_TAKE = IncrementedNamedInt.get('intelligine.brain.part.take')

+ 11 - 0
intelligine/simulation/molecule/MoleculeFlavour.py Dosyayı Görüntüle

@@ -36,6 +36,17 @@ class MoleculeFlavour():
36 36
             raise NoTypeInMolecule()
37 37
         return types[type]
38 38
 
39
+    def get_molecules(self, category):
40
+        """
41
+
42
+        :param category:
43
+        :return: Molecules dict or empty dict of no molecules
44
+        """
45
+        try:
46
+            return self.get_types(category)
47
+        except NoCategoryInMolecule:
48
+            return {}
49
+
39 50
     def get_types(self, category):
40 51
         if category not in self._flavour:
41 52
             raise NoCategoryInMolecule()

+ 8 - 0
intelligine/simulation/object/brain/AntBrain.py Dosyayı Görüntüle

@@ -90,3 +90,11 @@ class AntBrain(Brain):
90 90
             if isinstance(obj, take_class):
91 91
                 return self._taken_smell_matches[take_class]
92 92
         raise NotFound()
93
+
94
+    @classmethod
95
+    def get_home_smells(cls):
96
+        """
97
+        Note: Actually return all know smells. Not really HOME smells.
98
+        :return:
99
+        """
100
+        return cls._taken_smell_matches.values()

+ 11 - 8
intelligine/simulation/object/brain/part/move/AntMoveBrainPart.py Dosyayı Görüntüle

@@ -1,3 +1,4 @@
1
+from intelligine.shorcut.brain import get_brain_class
1 2
 from intelligine.simulation.object.brain.part.move.AntStar.ByPass import ByPass
2 3
 from intelligine.simulation.object.brain.part.move.AntStar.Host import Host
3 4
 from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
@@ -5,7 +6,7 @@ from intelligine.synergy.event.move.direction import directions_modifiers, get_p
5 6
 from synergine_xyz.cst import POSITION
6 7
 from intelligine.core.exceptions import NoMolecule
7 8
 from intelligine.cst import MOLECULE_SEARCHING, MOVE_MODE_EXPLO, MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, \
8
-    EXPLORATION_VECTOR, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG
9
+    EXPLORATION_VECTOR, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG, BRAIN_SCHEMA, BRAIN
9 10
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
10 11
 
11 12
 
@@ -123,17 +124,19 @@ class AntMoveBrainPart(MoveBrainPart):
123 124
     def _on_home_smell(cls, context, object_id):
124 125
         current_position = context.metas.value.get(POSITION, object_id)
125 126
         flavour = context.molecules().get_flavour(current_position)
126
-        # TODO: Idem, liste de smell_type ...
127
-        for smell_type in (SMELL_FOOD, SMELL_EGG):
128
-            try:
129
-                molecule = flavour.get_molecule(category=MOLECULES_DIRECTION, type=smell_type)
127
+        molecules = flavour.get_molecules(MOLECULES_DIRECTION)
128
+
129
+        if not molecules:
130
+            return False
131
+
132
+        brain_class = get_brain_class(context, object_id)
133
+        for smell_type in brain_class.get_home_smells():
134
+            if smell_type in molecules:
130 135
                 return True
131
-            except NoMolecule:
132
-                pass  # C'est qu'elle y est pas ^^
136
+
133 137
         return False
134 138
 
135 139
     def _update_exploration_vector(self):
136
-        # TODO: add tuple as vectors ?
137 140
         just_move_vector = directions_modifiers[self._host_brain.get_host().get_previous_direction()]
138 141
         self._set_exploration_vector((self._exploration_vector[0] + just_move_vector[1],
139 142
                                       self._exploration_vector[1] + just_move_vector[2]))

+ 4 - 5
intelligine/synergy/object/BaseBug.py Dosyayı Görüntüle

@@ -1,6 +1,6 @@
1 1
 from intelligine.core.exceptions import BodyPartAlreadyExist
2 2
 from intelligine.synergy.object.Transportable import Transportable
3
-from intelligine.cst import COL_ALIVE, COLONY, ACTION_DIE
3
+from intelligine.cst import COL_ALIVE, COLONY, ACTION_DIE, BRAIN
4 4
 from intelligine.simulation.object.brain.Brain import Brain
5 5
 from intelligine.cst import ALIVE, ATTACKABLE
6 6
 from synergine.core.Signals import Signals
@@ -9,6 +9,7 @@ from synergine.core.Signals import Signals
9 9
 class BaseBug(Transportable):
10 10
 
11 11
     _body_parts = {}
12
+    _brain_class = Brain
12 13
 
13 14
     def __init__(self, collection, context):
14 15
         super().__init__(collection, context)
@@ -18,7 +19,8 @@ class BaseBug(Transportable):
18 19
         self._life_points = 10
19 20
         self._alive = True
20 21
         self._movements_count = -1
21
-        self._brain = self._get_brain_instance()
22
+        self._brain = self._brain_class(self._context, self)
23
+        self._context.metas.value.set(BRAIN, self.get_id(), self._brain_class)
22 24
         self._parts = {}
23 25
         self._init_parts()
24 26
 
@@ -62,8 +64,5 @@ class BaseBug(Transportable):
62 64
     def get_movements_count(self):
63 65
         return self._movements_count
64 66
 
65
-    def _get_brain_instance(self):
66
-        return Brain(self._context, self)
67
-
68 67
     def get_brain(self):
69 68
         return self._brain

+ 1 - 3
intelligine/synergy/object/ant/Ant.py Dosyayı Görüntüle

@@ -13,6 +13,7 @@ class Ant(Bug):
13 13
     _body_parts = {
14 14
         BODY_PART_PHEROMONE_GLAND: MovementMoleculeGland
15 15
     }
16
+    _brain_class = AntBrain
16 17
 
17 18
     def __init__(self, collection, context):
18 19
         super().__init__(collection, context)
@@ -33,9 +34,6 @@ class Ant(Bug):
33 34
         self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
34 35
         self._remove_col(COL_FIGHTER)
35 36
 
36
-    def _get_brain_instance(self):
37
-        return AntBrain(self._context, self)
38
-
39 37
     def get_movement_molecule_gland(self):
40 38
         return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
41 39