Browse Source

Refactorize move action in pheromone move and standart move

Bastien Sevajol 9 years ago
parent
commit
1cec8dbf9c

+ 2 - 2
intelligine/synergy/Colony.py View File

1
 from synergine.synergy.collection.SynergyCollection import SynergyCollection
1
 from synergine.synergy.collection.SynergyCollection import SynergyCollection
2
-from intelligine.synergy.event.move.MoveAction import MoveAction
2
+from intelligine.synergy.event.move.PheromoneMoveAction import PheromoneMoveAction
3
 from intelligine.synergy.event.attack.NearAttackableAction import NearAttackableAction
3
 from intelligine.synergy.event.attack.NearAttackableAction import NearAttackableAction
4
 from intelligine.synergy.event.transport.TakeableAction import TakeableAction
4
 from intelligine.synergy.event.transport.TakeableAction import TakeableAction
5
 from intelligine.synergy.event.transport.PutableAction import PutableAction
5
 from intelligine.synergy.event.transport.PutableAction import PutableAction
10
 
10
 
11
     def __init__(self, configuration):
11
     def __init__(self, configuration):
12
         super().__init__(configuration)
12
         super().__init__(configuration)
13
-        self._actions = [MoveAction, NearAttackableAction, TakeableAction, PutableAction,
13
+        self._actions = [PheromoneMoveAction, NearAttackableAction, TakeableAction, PutableAction,
14
                          CycleAction]
14
                          CycleAction]
15
         self._start_position = configuration.get_start_position()
15
         self._start_position = configuration.get_start_position()
16
 
16
 

+ 20 - 65
intelligine/synergy/event/move/MoveAction.py View File

1
 from synergine.synergy.event.Action import Action
1
 from synergine.synergy.event.Action import Action
2
 from intelligine.synergy.event.move.MoveEvent import MoveEvent
2
 from intelligine.synergy.event.move.MoveEvent import MoveEvent
3
 from random import randint, choice, randrange
3
 from random import randint, choice, randrange
4
+from synergine.synergy.event.exception.ActionAborted import ActionAborted
4
 from xyzworld.cst import POSITION
5
 from xyzworld.cst import POSITION
5
-from intelligine.cst import PREVIOUS_DIRECTION, BLOCKED_SINCE, MOVE_MODE, MOVE_MODE_EXPLO, PHEROMONE_SEARCHING
6
-from intelligine.cst import COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING
7
-from intelligine.synergy.event.move.direction import directions_same_level, directions_modifiers, directions_slighty
6
+from intelligine.cst import PREVIOUS_DIRECTION, BLOCKED_SINCE
7
+from intelligine.synergy.event.move.direction import directions_same_level, directions_slighty
8
 from intelligine.synergy.event.move.direction import get_position_with_direction_decal
8
 from intelligine.synergy.event.move.direction import get_position_with_direction_decal
9
-from intelligine.core.exceptions import NoPheromone, BestPheromoneHere
10
-from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone
11
 
9
 
12
 
10
 
13
 class MoveAction(Action):
11
 class MoveAction(Action):
21
 
19
 
22
     def prepare(self, context):
20
     def prepare(self, context):
23
         object_point = context.metas.value.get(POSITION, self._object_id)
21
         object_point = context.metas.value.get(POSITION, self._object_id)
22
+        direction = self._get_prepared_direction(context, object_point)
23
+        self._set_prepared_direction(context, object_point, direction)
24
 
24
 
25
-        try:
26
-            direction = self._get_direction_with_pheromones(context, object_point)
27
-        except NoPheromone:
28
-            direction = self._get_random_direction(context)
25
+    def _get_prepared_direction(self, context, object_point):
26
+        return self._get_random_direction(context)
29
 
27
 
28
+    def _set_prepared_direction(self, context, object_point, direction):
30
         move_to_point = get_position_with_direction_decal(direction, object_point)
29
         move_to_point = get_position_with_direction_decal(direction, object_point)
31
         if self._direction_point_is_possible(context, move_to_point):
30
         if self._direction_point_is_possible(context, move_to_point):
32
             self._move_to_point = move_to_point
31
             self._move_to_point = move_to_point
35
             # TODO: mettre self._dont_move = True ?
34
             # TODO: mettre self._dont_move = True ?
36
             pass
35
             pass
37
 
36
 
38
-    def _get_direction_with_pheromones(self, context, object_point):
39
-        pheromone_type = context.metas.value.get(PHEROMONE_SEARCHING, self._object_id)
40
-        try:
41
-            direction = self._get_pheromone_direction_for_point(context, object_point, pheromone_type)
42
-        except NoPheromone:
43
-            try:
44
-                direction = self._get_direction_of_pheromone(context, object_point, pheromone_type)
45
-            except NoPheromone:
46
-                raise
47
-        return direction
48
-
49
-    @staticmethod
50
-    def _get_pheromone_direction_for_point(context, point, pheromone_type):
51
-        return DirectionPheromone.get_direction_for_point(context, point, pheromone_type)
52
-
53
-    @staticmethod
54
-    def _get_direction_of_pheromone(context, point, pheromone_type):
55
-        search_pheromone_in_points = context.get_arround_points_of(point, distance=1)
56
-        try:
57
-            best_pheromone_direction = DirectionPheromone.get_best_pheromone_direction_in(context,
58
-                                                                                          point,
59
-                                                                                          search_pheromone_in_points,
60
-                                                                                          pheromone_type)
61
-            return best_pheromone_direction
62
-        except NoPheromone as err:
63
-            raise err
64
-
65
     def _get_random_direction(self, context):
37
     def _get_random_direction(self, context):
66
         try:
38
         try:
67
             blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
39
             blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
93
         return context.position_is_penetrable(direction_point)
65
         return context.position_is_penetrable(direction_point)
94
 
66
 
95
     def run(self, obj, context, synergy_manager):
67
     def run(self, obj, context, synergy_manager):
96
-        if self._move_to_point is not None and self._move_to_direction != 14: # TODO: il ne faut pas choisir une direction 14.
97
-            obj.set_position(self._move_to_point)
98
-            context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
99
-            context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)
100
-            self._appose_pheromone(obj, context)
101
-
102
-            # TEST: le temps de tout tester
103
-            if self._move_to_point == obj.get_colony().get_start_position() and obj.is_carrying():
104
-                obj_transported = obj.get_carried()
105
-                obj_transported.set_carried_by(None)
106
-                obj.put_carry(obj_transported, (-1, 0, 0))
107
-                obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
108
-                context.metas.collections.add_remove(obj.get_id(),
109
-                                                     COL_TRANSPORTER_NOT_CARRYING,
110
-                                                     COL_TRANSPORTER_CARRYING)
111
-        else:
112
-            try:
113
-                blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
114
-            except:
115
-                blocked_since = 0
68
+        try:
69
+            self._apply_move(obj, context)
70
+        except ActionAborted:
71
+            blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id, allow_empty=True, empty_value=0)
116
             context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)
72
             context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)
117
 
73
 
118
-    @staticmethod
119
-    def _appose_pheromone(obj, context):
120
-        # TODO: Cette action de pheromone doit etre une surcharge de Move afin d'avoir une Action Move generique.
121
-        try:
122
-            DirectionPheromone.appose(context,
123
-                                      obj.get_position(),
124
-                                      obj.get_movement_pheromone_gland().get_movement_molecules())
125
-        except BestPheromoneHere as best_pheromone_here:
126
-            obj.get_brain().set_distance_from_objective(best_pheromone_here.get_best_distance())
74
+    def _apply_move(self, obj, context):
75
+        # TODO: il ne faut pas choisir une direction 14.
76
+        if self._move_to_point is None or self._move_to_direction == 14:
77
+            raise ActionAborted()
78
+
79
+        obj.set_position(self._move_to_point)
80
+        context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
81
+        context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)

+ 67 - 0
intelligine/synergy/event/move/PheromoneMoveAction.py View File

1
+from intelligine.synergy.event.move.MoveAction import MoveAction
2
+from intelligine.cst import PHEROMONE_SEARCHING, MOVE_MODE_EXPLO
3
+from intelligine.cst import COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING
4
+from intelligine.core.exceptions import NoPheromone, BestPheromoneHere
5
+from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone
6
+
7
+
8
+class PheromoneMoveAction(MoveAction):
9
+
10
+    def _get_prepared_direction(self, context, object_point):
11
+        try:
12
+            return self._get_direction_with_pheromones(context, object_point)
13
+        except NoPheromone:
14
+            return super()._get_prepared_direction(context, object_point)
15
+
16
+    def _get_direction_with_pheromones(self, context, object_point):
17
+        pheromone_type = context.metas.value.get(PHEROMONE_SEARCHING, self._object_id)
18
+        try:
19
+            direction = self._get_pheromone_direction_for_point(context, object_point, pheromone_type)
20
+        except NoPheromone:
21
+            try:
22
+                direction = self._get_direction_of_pheromone(context, object_point, pheromone_type)
23
+            except NoPheromone:
24
+                raise
25
+        return direction
26
+
27
+    @staticmethod
28
+    def _get_pheromone_direction_for_point(context, point, pheromone_type):
29
+        return DirectionPheromone.get_direction_for_point(context, point, pheromone_type)
30
+
31
+    @staticmethod
32
+    def _get_direction_of_pheromone(context, point, pheromone_type):
33
+        search_pheromone_in_points = context.get_arround_points_of(point, distance=1)
34
+        try:
35
+            best_pheromone_direction = DirectionPheromone.get_best_pheromone_direction_in(context,
36
+                                                                                          point,
37
+                                                                                          search_pheromone_in_points,
38
+                                                                                          pheromone_type)
39
+            return best_pheromone_direction
40
+        except NoPheromone as err:
41
+            raise err
42
+
43
+    def _apply_move(self, obj, context):
44
+        super()._apply_move(obj, context)
45
+        self._appose_pheromone(obj, context)
46
+
47
+        # TEST: le temps de tout tester
48
+        if self._move_to_point == obj.get_colony().get_start_position() and obj.is_carrying():
49
+            obj_transported = obj.get_carried()
50
+            obj_transported.set_carried_by(None)
51
+            obj.put_carry(obj_transported, (-1, 0, 0))
52
+            obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
53
+            context.metas.collections.add_remove(obj.get_id(),
54
+                                                 COL_TRANSPORTER_NOT_CARRYING,
55
+                                                 COL_TRANSPORTER_CARRYING)
56
+
57
+    @staticmethod
58
+    def _appose_pheromone(obj, context):
59
+        # TODO: Cette action de pheromone doit etre une surcharge de Move afin d'avoir une Action Move generique.
60
+        try:
61
+            DirectionPheromone.appose(context,
62
+                                      obj.get_position(),
63
+                                      obj.get_movement_pheromone_gland().get_movement_molecules())
64
+        except BestPheromoneHere as best_pheromone_here:
65
+            obj.get_brain().set_distance_from_objective(best_pheromone_here.get_best_distance())
66
+
67
+

+ 2 - 2
intelligine/tests/simulation/mode/TestChangeMode.py View File

10
 from intelligine.synergy.Colony import Colony
10
 from intelligine.synergy.Colony import Colony
11
 from intelligine.synergy.Simulation import Simulation
11
 from intelligine.synergy.Simulation import Simulation
12
 from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
12
 from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
13
-from intelligine.synergy.event.move.MoveAction import MoveAction
13
+from intelligine.synergy.event.move.PheromoneMoveAction import PheromoneMoveAction
14
 from intelligine.synergy.event.move.direction import NORTH, SOUTH
14
 from intelligine.synergy.event.move.direction import NORTH, SOUTH
15
 from intelligine.tests.src.event.MoveAction import MoveAction as TestMoveAction
15
 from intelligine.tests.src.event.MoveAction import MoveAction as TestMoveAction
16
 from synergine.synergy.collection.SynergyCollection import SynergyCollection
16
 from synergine.synergy.collection.SynergyCollection import SynergyCollection
43
         class TestColony(Colony):
43
         class TestColony(Colony):
44
             def __init__(self, configuration):
44
             def __init__(self, configuration):
45
                 super().__init__(configuration)
45
                 super().__init__(configuration)
46
-                self._actions.remove(MoveAction)
46
+                self._actions.remove(PheromoneMoveAction)
47
                 TestMoveAction.force_direction = test_case._force_move
47
                 TestMoveAction.force_direction = test_case._force_move
48
                 self._actions.append(TestMoveAction)
48
                 self._actions.append(TestMoveAction)
49
         return TestColony(self._get_colony_configuration())
49
         return TestColony(self._get_colony_configuration())

+ 1 - 1
intelligine/tests/src/event/MoveAction.py View File

1
-from intelligine.synergy.event.move.MoveAction import MoveAction as BaseMoveAction
1
+from intelligine.synergy.event.move.PheromoneMoveAction import PheromoneMoveAction as BaseMoveAction
2
 from intelligine.synergy.event.move.direction import NORTH
2
 from intelligine.synergy.event.move.direction import NORTH
3
 
3
 
4
 
4