Browse Source

Working on smell

Bastien Sevajol 8 years ago
parent
commit
02f0a85e93

+ 2 - 0
TODO View File

@@ -1,3 +1,5 @@
1 1
 * Fourmis (map.tmx) pas en mode nurse (-> algo de choix d'activité plus tard)
2 2
 * Pygame: smell, pheromons: rendre + generique
3 3
 * Smell: Calcul de la distance max
4
+* Ant: MOVE_MODE_HOME && rien à faire (pas de truc à stocker, switch to mode EXPLO)
5
+* Plus de pheromone deposer a l'explo !

+ 3 - 0
intelligine/cst.py View File

@@ -20,6 +20,7 @@ MOVE_MODE = IncrementedNamedInt.get('intelligine.basebug.move.mode')
20 20
 MOVE_MODE_EXPLO = IncrementedNamedInt.get('intelligine.basebug.move.mode.explo')
21 21
 MOVE_MODE_GOHOME = IncrementedNamedInt.get('intelligine.basebug.move.mode.gohome')
22 22
 MOVE_MODE_NURSE = IncrementedNamedInt.get('intelligine.basebug.move.mode.nurse')
23
+MOVE_MODE_HOME = IncrementedNamedInt.get('intelligine.basebug.move.mode.home')
23 24
 
24 25
 TYPE = IncrementedNamedInt.get('intelligine.object.type')
25 26
 TYPE_RESOURCE_EXPLOITABLE = IncrementedNamedInt.get('intelligine.object.type.resource.transformable')
@@ -32,7 +33,9 @@ PHEROMON_DIRECTION = IncrementedNamedInt.get('intelligine.phero.direction')
32 33
 PHEROMON_DIR_NONE = IncrementedNamedInt.get('intelligine.phero.direction.none')
33 34
 PHEROMON_DIR_EXPLO = IncrementedNamedInt.get('intelligine.phero.direction.explo')
34 35
 PHEROMON_DIR_HOME = IncrementedNamedInt.get('intelligine.phero.direction.home')
36
+EXPLORATION_VECTOR = IncrementedNamedInt.get('intelligine.exploration_vector')
35 37
 
38
+SMELL = IncrementedNamedInt.get('intelligine.smell')
36 39
 OBJ_SMELL = IncrementedNamedInt.get('intelligine.obj_smell')
37 40
 SMELL_FOOD = IncrementedNamedInt.get('intelligine.smell.food')
38 41
 SMELL_EGG = IncrementedNamedInt.get('intelligine.smell.egg')

+ 46 - 46
intelligine/sandbox/exploration/map.tmx View File

@@ -311,6 +311,7 @@
311 311
    <tile gid="241"/>
312 312
    <tile gid="241"/>
313 313
    <tile gid="241"/>
314
+   <tile gid="243"/>
314 315
    <tile gid="0"/>
315 316
    <tile gid="0"/>
316 317
    <tile gid="0"/>
@@ -329,8 +330,7 @@
329 330
    <tile gid="0"/>
330 331
    <tile gid="0"/>
331 332
    <tile gid="0"/>
332
-   <tile gid="0"/>
333
-   <tile gid="0"/>
333
+   <tile gid="242"/>
334 334
    <tile gid="0"/>
335 335
    <tile gid="0"/>
336 336
    <tile gid="0"/>
@@ -513,6 +513,7 @@
513 513
    <tile gid="241"/>
514 514
    <tile gid="0"/>
515 515
    <tile gid="0"/>
516
+   <tile gid="1"/>
516 517
    <tile gid="0"/>
517 518
    <tile gid="0"/>
518 519
    <tile gid="0"/>
@@ -529,8 +530,7 @@
529 530
    <tile gid="0"/>
530 531
    <tile gid="0"/>
531 532
    <tile gid="0"/>
532
-   <tile gid="0"/>
533
-   <tile gid="0"/>
533
+   <tile gid="242"/>
534 534
    <tile gid="0"/>
535 535
    <tile gid="0"/>
536 536
    <tile gid="0"/>
@@ -730,7 +730,7 @@
730 730
    <tile gid="0"/>
731 731
    <tile gid="0"/>
732 732
    <tile gid="0"/>
733
-   <tile gid="0"/>
733
+   <tile gid="242"/>
734 734
    <tile gid="0"/>
735 735
    <tile gid="0"/>
736 736
    <tile gid="0"/>
@@ -930,7 +930,7 @@
930 930
    <tile gid="0"/>
931 931
    <tile gid="0"/>
932 932
    <tile gid="0"/>
933
-   <tile gid="0"/>
933
+   <tile gid="242"/>
934 934
    <tile gid="0"/>
935 935
    <tile gid="0"/>
936 936
    <tile gid="0"/>
@@ -1126,7 +1126,7 @@
1126 1126
    <tile gid="0"/>
1127 1127
    <tile gid="0"/>
1128 1128
    <tile gid="0"/>
1129
-   <tile gid="0"/>
1129
+   <tile gid="242"/>
1130 1130
    <tile gid="0"/>
1131 1131
    <tile gid="0"/>
1132 1132
    <tile gid="242"/>
@@ -1327,7 +1327,7 @@
1327 1327
    <tile gid="0"/>
1328 1328
    <tile gid="0"/>
1329 1329
    <tile gid="0"/>
1330
-   <tile gid="0"/>
1330
+   <tile gid="242"/>
1331 1331
    <tile gid="0"/>
1332 1332
    <tile gid="0"/>
1333 1333
    <tile gid="0"/>
@@ -1528,7 +1528,7 @@
1528 1528
    <tile gid="0"/>
1529 1529
    <tile gid="0"/>
1530 1530
    <tile gid="0"/>
1531
-   <tile gid="0"/>
1531
+   <tile gid="242"/>
1532 1532
    <tile gid="0"/>
1533 1533
    <tile gid="0"/>
1534 1534
    <tile gid="0"/>
@@ -2123,7 +2123,7 @@
2123 2123
    <tile gid="0"/>
2124 2124
    <tile gid="0"/>
2125 2125
    <tile gid="0"/>
2126
-   <tile gid="0"/>
2126
+   <tile gid="242"/>
2127 2127
    <tile gid="0"/>
2128 2128
    <tile gid="0"/>
2129 2129
    <tile gid="0"/>
@@ -2312,15 +2312,15 @@
2312 2312
    <tile gid="241"/>
2313 2313
    <tile gid="241"/>
2314 2314
    <tile gid="0"/>
2315
+   <tile gid="242"/>
2315 2316
    <tile gid="0"/>
2317
+   <tile gid="242"/>
2316 2318
    <tile gid="0"/>
2319
+   <tile gid="242"/>
2317 2320
    <tile gid="0"/>
2321
+   <tile gid="242"/>
2318 2322
    <tile gid="0"/>
2319
-   <tile gid="0"/>
2320
-   <tile gid="0"/>
2321
-   <tile gid="0"/>
2322
-   <tile gid="0"/>
2323
-   <tile gid="0"/>
2323
+   <tile gid="242"/>
2324 2324
    <tile gid="0"/>
2325 2325
    <tile gid="242"/>
2326 2326
    <tile gid="0"/>
@@ -2425,14 +2425,14 @@
2425 2425
    <tile gid="0"/>
2426 2426
    <tile gid="242"/>
2427 2427
    <tile gid="0"/>
2428
-   <tile gid="241"/>
2429
-   <tile gid="241"/>
2430
-   <tile gid="241"/>
2431
-   <tile gid="241"/>
2432
-   <tile gid="241"/>
2433
-   <tile gid="241"/>
2434 2428
    <tile gid="0"/>
2435
-   <tile gid="241"/>
2429
+   <tile gid="0"/>
2430
+   <tile gid="0"/>
2431
+   <tile gid="0"/>
2432
+   <tile gid="0"/>
2433
+   <tile gid="0"/>
2434
+   <tile gid="0"/>
2435
+   <tile gid="0"/>
2436 2436
    <tile gid="0"/>
2437 2437
    <tile gid="0"/>
2438 2438
    <tile gid="0"/>
@@ -2525,14 +2525,14 @@
2525 2525
    <tile gid="0"/>
2526 2526
    <tile gid="0"/>
2527 2527
    <tile gid="0"/>
2528
-   <tile gid="241"/>
2529
-   <tile gid="243"/>
2530 2528
    <tile gid="0"/>
2531 2529
    <tile gid="0"/>
2532 2530
    <tile gid="0"/>
2533
-   <tile gid="241"/>
2534 2531
    <tile gid="0"/>
2535
-   <tile gid="241"/>
2532
+   <tile gid="0"/>
2533
+   <tile gid="0"/>
2534
+   <tile gid="0"/>
2535
+   <tile gid="0"/>
2536 2536
    <tile gid="0"/>
2537 2537
    <tile gid="0"/>
2538 2538
    <tile gid="0"/>
@@ -2625,14 +2625,14 @@
2625 2625
    <tile gid="0"/>
2626 2626
    <tile gid="0"/>
2627 2627
    <tile gid="0"/>
2628
-   <tile gid="241"/>
2629 2628
    <tile gid="0"/>
2630 2629
    <tile gid="0"/>
2631 2630
    <tile gid="0"/>
2632 2631
    <tile gid="0"/>
2633
-   <tile gid="241"/>
2634 2632
    <tile gid="0"/>
2635
-   <tile gid="241"/>
2633
+   <tile gid="0"/>
2634
+   <tile gid="0"/>
2635
+   <tile gid="0"/>
2636 2636
    <tile gid="0"/>
2637 2637
    <tile gid="0"/>
2638 2638
    <tile gid="0"/>
@@ -2725,14 +2725,14 @@
2725 2725
    <tile gid="0"/>
2726 2726
    <tile gid="0"/>
2727 2727
    <tile gid="0"/>
2728
-   <tile gid="241"/>
2729 2728
    <tile gid="0"/>
2730 2729
    <tile gid="0"/>
2731
-   <tile gid="1"/>
2732 2730
    <tile gid="0"/>
2733
-   <tile gid="241"/>
2734 2731
    <tile gid="0"/>
2735
-   <tile gid="241"/>
2732
+   <tile gid="0"/>
2733
+   <tile gid="0"/>
2734
+   <tile gid="0"/>
2735
+   <tile gid="0"/>
2736 2736
    <tile gid="0"/>
2737 2737
    <tile gid="0"/>
2738 2738
    <tile gid="0"/>
@@ -2825,14 +2825,14 @@
2825 2825
    <tile gid="0"/>
2826 2826
    <tile gid="0"/>
2827 2827
    <tile gid="0"/>
2828
-   <tile gid="241"/>
2829 2828
    <tile gid="0"/>
2830 2829
    <tile gid="0"/>
2831 2830
    <tile gid="0"/>
2832 2831
    <tile gid="0"/>
2833
-   <tile gid="241"/>
2834 2832
    <tile gid="0"/>
2835
-   <tile gid="241"/>
2833
+   <tile gid="0"/>
2834
+   <tile gid="0"/>
2835
+   <tile gid="0"/>
2836 2836
    <tile gid="0"/>
2837 2837
    <tile gid="0"/>
2838 2838
    <tile gid="0"/>
@@ -2925,14 +2925,14 @@
2925 2925
    <tile gid="0"/>
2926 2926
    <tile gid="0"/>
2927 2927
    <tile gid="0"/>
2928
-   <tile gid="241"/>
2929 2928
    <tile gid="0"/>
2930 2929
    <tile gid="0"/>
2931 2930
    <tile gid="0"/>
2932 2931
    <tile gid="0"/>
2933 2932
    <tile gid="0"/>
2934 2933
    <tile gid="0"/>
2935
-   <tile gid="241"/>
2934
+   <tile gid="0"/>
2935
+   <tile gid="0"/>
2936 2936
    <tile gid="0"/>
2937 2937
    <tile gid="0"/>
2938 2938
    <tile gid="0"/>
@@ -3025,14 +3025,14 @@
3025 3025
    <tile gid="0"/>
3026 3026
    <tile gid="0"/>
3027 3027
    <tile gid="0"/>
3028
-   <tile gid="241"/>
3029
-   <tile gid="241"/>
3030
-   <tile gid="241"/>
3031
-   <tile gid="241"/>
3032
-   <tile gid="241"/>
3033
-   <tile gid="241"/>
3034
-   <tile gid="241"/>
3035
-   <tile gid="241"/>
3028
+   <tile gid="0"/>
3029
+   <tile gid="0"/>
3030
+   <tile gid="0"/>
3031
+   <tile gid="0"/>
3032
+   <tile gid="0"/>
3033
+   <tile gid="0"/>
3034
+   <tile gid="0"/>
3035
+   <tile gid="0"/>
3036 3036
    <tile gid="0"/>
3037 3037
    <tile gid="0"/>
3038 3038
    <tile gid="0"/>

+ 7 - 2
intelligine/simulation/object/brain/AntBrain.py View File

@@ -2,7 +2,7 @@ from intelligine.simulation.object.brain.Brain import Brain
2 2
 from intelligine.simulation.object.brain.part.attack.AttackBrainPart import AttackBrainPart
3 3
 from intelligine.simulation.object.brain.part.move.AntMoveBrainPart import AntMoveBrainPart
4 4
 from intelligine.cst import MOVE_MODE, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO, \
5
-    BRAIN_PART_TAKE, BRAIN_PART_PUT, MOVE_MODE_NURSE, PHEROMON_DIR_NONE, BRAIN_PART_ATTACK
5
+    BRAIN_PART_TAKE, BRAIN_PART_PUT, MOVE_MODE_NURSE, PHEROMON_DIR_NONE, BRAIN_PART_ATTACK, MOVE_MODE_HOME
6 6
 from intelligine.cst import PHEROMONE_SEARCHING
7 7
 from intelligine.cst import BRAIN_PART_MOVE
8 8
 from intelligine.simulation.object.brain.part.transport.AntPutBrainPart import AntPutBrainPart
@@ -21,11 +21,12 @@ class AntBrain(Brain):
21 21
 
22 22
     def __init__(self, context, host):
23 23
         super().__init__(context, host)
24
-        self._movement_mode = MOVE_MODE_EXPLO
24
+        self._movement_mode = MOVE_MODE_HOME
25 25
         self._distance_from_objective = 0  # TODO rename: distance_since_objective
26 26
         self._pheromone_searching = PHEROMON_DIR_EXPLO
27 27
 
28 28
     def switch_to_mode(self, mode):
29
+        # TODO: Mode explo: reinit exploration vector
29 30
         self._movement_mode = mode
30 31
         self._update_pheromone_gland(mode)
31 32
         self._context.metas.value.set(MOVE_MODE, self._host.get_id(), mode)
@@ -39,6 +40,8 @@ class AntBrain(Brain):
39 40
             pheromone_direction_type = PHEROMON_DIR_EXPLO
40 41
         elif mode == MOVE_MODE_NURSE:
41 42
             pheromone_direction_type = None
43
+        elif mode == MOVE_MODE_HOME:
44
+            pheromone_direction_type = None
42 45
         else:
43 46
             raise NotImplementedError()
44 47
 
@@ -55,6 +58,8 @@ class AntBrain(Brain):
55 58
             pheromone_searching = PHEROMON_DIR_HOME
56 59
         elif mode == MOVE_MODE_NURSE:
57 60
             pheromone_searching = PHEROMON_DIR_NONE
61
+        elif mode == MOVE_MODE_HOME:
62
+            return
58 63
         else:
59 64
             raise NotImplementedError()
60 65
         self._pheromone_searching = pheromone_searching

+ 6 - 0
intelligine/simulation/object/brain/Brain.py View File

@@ -33,3 +33,9 @@ class Brain():
33 33
         # TODO: N'est-ce pas un schema appartenant a la classe ? Ne suffirai t-il pas de stocker ce schema par classe
34 34
         # plutôt que par objet ?
35 35
         self._context.metas.value.set(BRAIN_SCHEMA, self._host.get_id(), self._schema)
36
+
37
+    def get_context(self):
38
+        return self._context
39
+
40
+    def get_host(self):
41
+        return self._host

+ 83 - 5
intelligine/simulation/object/brain/part/move/AntMoveBrainPart.py View File

@@ -1,18 +1,38 @@
1 1
 from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
2
+from intelligine.synergy.event.move.direction import directions_modifiers, get_direction_for_degrees
2 3
 from synergine_xyz.cst import POSITION
3 4
 from intelligine.core.exceptions import NoPheromone
4
-from intelligine.cst import PHEROMONE_SEARCHING, MOVE_MODE_EXPLO, COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING
5
+from intelligine.cst import PHEROMONE_SEARCHING, MOVE_MODE_EXPLO, \
6
+    MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, EXPLORATION_VECTOR, POINTS_SMELL
5 7
 from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone
8
+from synergine_xyz.geometry import get_degree_from_north
6 9
 
7 10
 
8 11
 class AntMoveBrainPart(MoveBrainPart):
9 12
 
13
+    def __init__(self, host_brain):
14
+        super().__init__(host_brain)
15
+        self._exploration_vector = (0, 0)
16
+
17
+    def _set_exploration_vector(self, new_vector):
18
+        self._exploration_vector = new_vector
19
+        # TODO: On devrais donner le context aux brain parts
20
+        self._host_brain.get_context().metas.value.set(EXPLORATION_VECTOR,
21
+                                                       self._host_brain.get_host().get_id(),
22
+                                                       new_vector)
23
+
10 24
     @classmethod
11 25
     def get_direction(cls, context, object_id):
12
-        try:
13
-            return cls._get_direction_with_pheromones(context, object_id)
14
-        except NoPheromone:
15
-            return super().get_direction(context, object_id)
26
+        move_mode = context.metas.value.get(MOVE_MODE, object_id)
27
+        if move_mode == MOVE_MODE_EXPLO:
28
+            try:
29
+                return cls._get_direction_with_pheromones(context, object_id)
30
+            except NoPheromone:
31
+                return super().get_direction(context, object_id)
32
+        elif move_mode == MOVE_MODE_GOHOME:
33
+            return cls._get_direction_with_exploration_vector(context, object_id)
34
+
35
+        return super().get_direction(context, object_id)
16 36
 
17 37
     @classmethod
18 38
     def _get_direction_with_pheromones(cls, context, object_id):
@@ -43,12 +63,70 @@ class AntMoveBrainPart(MoveBrainPart):
43 63
         except NoPheromone as err:
44 64
             raise err
45 65
 
66
+    @staticmethod
67
+    def _get_direction_with_exploration_vector(context, object_id):
68
+        current_position = context.metas.value.get(POSITION, object_id)
69
+        exploration_vector = context.metas.value.get(EXPLORATION_VECTOR, object_id)
70
+        # TODO: inverser
71
+        home_vector = (exploration_vector[0] - (exploration_vector[0]*2),
72
+                       exploration_vector[1] - (exploration_vector[1]*2))
73
+        home_position = (0, current_position[1]+home_vector[0], current_position[2]+home_vector[1])
74
+        degree_from_north = get_degree_from_north(current_position, home_position)
75
+        direction_for_home_vector = get_direction_for_degrees(degree_from_north)
76
+
77
+        return direction_for_home_vector
78
+
79
+    # TODO: obj pas necessaire, il est dans _host
46 80
     def done(self, obj, context):
47 81
         super().done(obj, context)
48 82
         self._appose_pheromone(obj)
83
+        self._check_context(obj, context)
84
+        self._apply_context(obj, context)
49 85
 
50 86
     @staticmethod
51 87
     def _appose_pheromone(obj):
52 88
         if obj.get_movement_pheromone_gland().is_enabled():
53 89
             obj.get_movement_pheromone_gland().appose()
54 90
 
91
+    def _check_context(self, obj, context):
92
+        """
93
+
94
+        If was in exploration, and just found home smell;
95
+            -> home mode (then, in this mode: no update vector, put food if food, etc)
96
+        If was in home, and just loose home smell:
97
+            -> exploration mode
98
+
99
+        :param obj:
100
+        :param context:
101
+        :return:
102
+        """
103
+        if self._host_brain.get_movement_mode() == MOVE_MODE_EXPLO and self._on_home_smell(context, obj.get_id()):
104
+            self._host_brain.switch_to_mode(MOVE_MODE_HOME)
105
+
106
+        if self._host_brain.get_movement_mode() == MOVE_MODE_HOME and not self._on_home_smell(context, obj.get_id()):
107
+            # TODO: sitwh explo que si rien a faire (rien a poser par exemple)
108
+            self._host_brain.switch_to_mode(MOVE_MODE_EXPLO)
109
+            self._start_new_exploration()
110
+
111
+    @classmethod
112
+    def _on_home_smell(cls, context, object_id):
113
+        current_position = context.metas.value.get(POSITION, object_id)
114
+        smell_points = context.metas.value.get(POINTS_SMELL, POINTS_SMELL, allow_empty=True, empty_value={})
115
+        if current_position in smell_points:
116
+            return True
117
+        return False
118
+
119
+    def _update_exploration_vector(self):
120
+        # TODO: add tuple as vectors ?
121
+        just_move_vector = directions_modifiers[self._host_brain.get_host().get_previous_direction()]
122
+        self._set_exploration_vector((self._exploration_vector[0] + just_move_vector[1],
123
+                                      self._exploration_vector[1] + just_move_vector[2]))
124
+
125
+
126
+    def _apply_context(self, obj, context):
127
+        movement_mode = self._host_brain.get_movement_mode()
128
+        if movement_mode == MOVE_MODE_EXPLO or movement_mode == MOVE_MODE_GOHOME:
129
+            self._update_exploration_vector()
130
+
131
+    def _start_new_exploration(self):
132
+        self._exploration_vector = (0, 0)

+ 1 - 1
intelligine/simulation/object/pheromone/MovementPheromoneGland.py View File

@@ -7,6 +7,6 @@ class MovementPheromoneGland(PheromoneGland):
7 7
 
8 8
     def get_pheromone(self):
9 9
         """
10
-        :return: pheromone_type, distance_from_objective
10
+        :return: Pheromone
11 11
         """
12 12
         return Pheromone(PHEROMON_DIRECTION, self._pheromone_type, self._host.get_brain().get_distance_from_objective(), 1)

+ 2 - 1
intelligine/synergy/event/move/direction.py View File

@@ -117,6 +117,7 @@ directions_degrees = {
117 117
     (202.5, 247.5): 16,
118 118
     (247.5, 292.5): 13,
119 119
     (292.5, 337.5): 10,
120
+    (337.5, 360): 11,
120 121
     (337.5, 0): 11
121 122
 }
122 123
 
@@ -127,7 +128,7 @@ def get_direction_for_degrees(degrees):
127 128
     for plage in directions_degrees:
128 129
         if plage[0] <= degrees <= plage[1]:
129 130
             return directions_degrees[plage]
130
-    raise Exception("Unknow plage for degree \"" + degrees + '"')
131
+    raise Exception("Unknow plage for degree \"" + str(degrees) + '"')
131 132
 
132 133
 
133 134
 """

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

@@ -2,7 +2,7 @@ from intelligine.core.exceptions import PheromoneException
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, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, BODY_PART_PHEROMONE_GLAND, TYPE, TYPE_ANT, \
5
-    COL_TRANSPORTER_CARRYING, MOVE_MODE_NURSE
5
+    COL_TRANSPORTER_CARRYING, MOVE_MODE_NURSE, MOVE_MODE_HOME
6 6
 from intelligine.synergy.object.Food import Food
7 7
 from intelligine.simulation.object.pheromone.MovementPheromoneGland import MovementPheromoneGland
8 8
 from intelligine.simulation.object.brain.AntBrain import AntBrain
@@ -21,7 +21,7 @@ class Ant(Bug):
21 21
                                                            COL_TRANSPORTER_NOT_CARRYING,
22 22
                                                            COL_FIGHTER])
23 23
         self._carried = None
24
-        self._brain.switch_to_mode(MOVE_MODE_EXPLO)
24
+        self._brain.switch_to_mode(MOVE_MODE_HOME)
25 25
         context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
26 26
 
27 27
     def die(self):