Browse Source

refactorise apose pheromone and add movement mode to ant

Bastien Sevajol 9 years ago
parent
commit
4b977387d8

+ 4 - 0
intelligine/cst.py View File

@@ -14,6 +14,10 @@ CARRYING = IncrementedNamedInt.get('intelligine.carrying')
14 14
 CANT_CARRY_STILL = IncrementedNamedInt.get('intelligine.cantcarry.still')
15 15
 CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
16 16
 ACTION_DIE = IncrementedNamedInt.get('intelligine.basebug.action.die')
17
+
18
+MOVE_MODE_EXPLO = IncrementedNamedInt.get('intelligine.basebug.move.mode.explo')
19
+MOVE_MODE_GOHOME = IncrementedNamedInt.get('intelligine.basebug.move.mode.gohome')
20
+
17 21
 PHEROMON_POSITIONS = IncrementedNamedInt.get('intelligine.phero.positions')
18 22
 PHEROMON_INFOS = IncrementedNamedInt.get('intelligine.phero.infos')
19 23
 PHEROMON_DIRECTION = IncrementedNamedInt.get('intelligine.phero.direction')

+ 22 - 40
intelligine/synergy/event/pheromone/ApposeDirection.py View File

@@ -1,9 +1,10 @@
1 1
 from synergine.synergy.event.Action import Action
2 2
 from intelligine.synergy.event.pheromone.PheromoneEvent import PheromoneEvent
3 3
 from xyzworld.cst import POSITION
4
-from intelligine.cst import PHEROMON_DIRECTION, PHEROMON_DIR_EXPLO, PHEROMON_POSITIONS
5
-from xyzworld.geometry import get_direction_degrees, get_degree_from_north
4
+from intelligine.cst import PHEROMON_DIRECTION, PHEROMON_DIR_EXPLO, PHEROMON_POSITIONS, MOVE_MODE_EXPLO
5
+from xyzworld.geometry import get_degree_from_north
6 6
 from intelligine.synergy.event.move.direction import get_direction_for_degrees, get_direction_opposite
7
+from intelligine.core.exceptions import SamePosition
7 8
 
8 9
 
9 10
 class ApposeDirection(Action):
@@ -26,54 +27,35 @@ class ApposeDirection(Action):
26 27
         On effectue l'incrementation dans le process principal. Beaucoups plus lourd que de le faie dans les
27 28
         process. Mais si on le fait dans les process on va rater des infos ...
28 29
         met a jour les pts (incremente)
29
-        :param obj:
30
-        :param context:
31
-        :param synergy_manager:
32
-        :return:
33 30
         """
34
-        # TODO: Remonter ca dans une objet
35
-
36
-        # a = get_direction_degrees((0, 0, 0), (0, -1, -1)) # 315
37
-        # a = get_direction_degrees((0, 0, 0), (0, 0, -1))  # 0
38
-        # a = get_direction_degrees((0, 0, 0), (0, 1, -1))  # 45
39
-        # a = get_direction_degrees((0, 0, 0), (0, -1, 0))  # 270
40
-        # a = get_direction_degrees((0, 0, 0), (0, 1, 0))   # 90
41
-        # a = get_direction_degrees((0, 0, 0), (0, -1, 1))  # 225
42
-        # a = get_direction_degrees((0, 0, 0), (0, 0, 1))   # 180
43
-        # a = get_direction_degrees((0, 0, 0), (0, 1, 1))   # 135
44
-
45
-        # a = get_degree_from_north((0, 0, 0), (0, -1, -1)) # 315
46
-        # a = get_degree_from_north((0, 0, 0), (0, 0, -1))  # 0
47
-        # a = get_degree_from_north((0, 0, 0), (0, 1, -1))  # 45
48
-        # a = get_degree_from_north((0, 0, 0), (0, -1, 0))  # 270
49
-        # a = get_degree_from_north((0, 0, 0), (0, 1, 0))   # 90
50
-        # a = get_degree_from_north((0, 0, 0), (0, -1, 1))  # 225
51
-        # a = get_degree_from_north((0, 0, 0), (0, 0, 1))   # 180
52
-        # a = get_degree_from_north((0, 0, 0), (0, 1, 1))   # 135
53 31
         self._appose_pheromones(obj, context, synergy_manager)
54 32
 
55 33
     def _appose_pheromones(self, obj, context, synergy_manager):
56
-        obj_position = obj.get_position()
57
-        obj_last_pheromon_position = obj.get_last_pheromone_point(PHEROMON_DIRECTION)
58 34
         try:
59
-            direction_degrees = get_degree_from_north(obj_last_pheromon_position, obj_position)
60
-        except ZeroDivisionError:
35
+            from_direction = self._get_from_direction(obj)
36
+        except SamePosition:
61 37
             return
62
-        direction = get_direction_for_degrees(direction_degrees)
63
-        from_direction = get_direction_opposite(direction)
64
-        depose_intensity = 1 # TODO: config
65 38
 
39
+        depose_intensity = 1 # TODO: config
40
+        pheromone_direction_type = self._get_pheromone_direction_type(obj)
66 41
         for affected_point in self._parameters['concerned_points']:
67
-            self._appose_pheromone(context, affected_point, direction, depose_intensity)
42
+            context.pheromones().increment(affected_point, [PHEROMON_DIRECTION,
43
+                                                            pheromone_direction_type,
44
+                                                            from_direction], depose_intensity)
68 45
             context.metas.list.add(PHEROMON_POSITIONS, PHEROMON_POSITIONS, affected_point, assert_not_in=False)
69 46
 
70 47
         obj.set_last_pheromone_point(PHEROMON_DIRECTION, obj.get_position())
71 48
 
72
-    def _appose_pheromone(self, context, affected_point, direction, depose_intensity):
73
-        context.pheromones().increment(affected_point, [PHEROMON_DIRECTION,
74
-                                                        PHEROMON_DIR_EXPLO,
75
-                                                        direction], depose_intensity)
76
-
77
-
78
-
49
+    def _get_from_direction(self, obj):
50
+        obj_position = obj.get_position()
51
+        obj_last_pheromon_position = obj.get_last_pheromone_point(PHEROMON_DIRECTION)
52
+        try:
53
+            direction_degrees = get_degree_from_north(obj_last_pheromon_position, obj_position)
54
+        except ZeroDivisionError:
55
+            raise SamePosition()
56
+        direction = get_direction_for_degrees(direction_degrees)
57
+        return get_direction_opposite(direction)
79 58
 
59
+    def _get_pheromone_direction_type(self, obj):
60
+        if obj.get_movement_mode() == MOVE_MODE_EXPLO:
61
+            return PHEROMON_DIR_EXPLO

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

@@ -1,6 +1,7 @@
1 1
 from intelligine.synergy.object.Bug import Bug
2 2
 from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, \
3
-   COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, COL_FIGHTER
3
+                            COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
4
+                            COL_FIGHTER, MOVE_MODE_EXPLO
4 5
 
5 6
 
6 7
 class Ant(Bug):
@@ -13,6 +14,7 @@ class Ant(Bug):
13 14
                                                            COL_FIGHTER])
14 15
         self._carried = []
15 16
         self._last_pheromones_points = {}
17
+        self._movement_mode = MOVE_MODE_EXPLO
16 18
 
17 19
     def put_carry(self, obj, position=None):
18 20
         if position is None:
@@ -48,3 +50,9 @@ class Ant(Bug):
48 50
 
49 51
     def set_last_pheromone_point(self, pheromone_name, position):
50 52
         self._last_pheromones_points[pheromone_name] = position
53
+
54
+    def get_movement_mode(self):
55
+        return self._movement_mode
56
+
57
+    def set_movement_mode(self, movement_mode):
58
+        self._movement_mode = movement_mode