Browse Source

clean code of MoveAction

Bastien Sevajol 9 years ago
parent
commit
65037bc93b
1 changed files with 11 additions and 89 deletions
  1. 11 89
      intelligine/synergy/event/move/MoveAction.py

+ 11 - 89
intelligine/synergy/event/move/MoveAction.py View File

@@ -5,6 +5,7 @@ from xyzworld.cst import POSITION
5 5
 from intelligine.cst import PREVIOUS_DIRECTION, BLOCKED_SINCE, MOVE_MODE, MOVE_MODE_EXPLO
6 6
 from intelligine.cst import COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING
7 7
 from intelligine.synergy.event.move.direction import directions_same_level, directions_modifiers, directions_slighty
8
+from intelligine.synergy.event.move.direction import get_position_with_direction_decal
8 9
 from intelligine.core.exceptions import NoPheromone, BestPheromoneHere
9 10
 from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone
10 11
 
@@ -26,7 +27,7 @@ class MoveAction(Action):
26 27
         except NoPheromone:
27 28
             direction = self._get_random_direction(context)
28 29
 
29
-        move_to_point = self._get_point_for_direction(object_point, direction)
30
+        move_to_point = get_position_with_direction_decal(direction, object_point)
30 31
         if self._direction_point_is_possible(context, move_to_point):
31 32
             self._move_to_point = move_to_point
32 33
             self._move_to_direction = direction
@@ -35,13 +36,15 @@ class MoveAction(Action):
35 36
             pass
36 37
 
37 38
     def _get_direction_with_pheromones(self, context, object_point):
38
-        # TODO: Placer directnement pheromone type dans les metas
39 39
         object_movement_mode = context.metas.value.get(MOVE_MODE, self._object_id)
40 40
         pheromone_type = DirectionPheromone.get_pheromone_type_for_move_mode(object_movement_mode)
41 41
         try:
42 42
             direction = self._get_pheromone_direction_for_point(context, object_point, pheromone_type)
43 43
         except NoPheromone:
44
-            direction = self._get_direction_of_pheromone(context, object_point, pheromone_type)
44
+            try:
45
+                direction = self._get_direction_of_pheromone(context, object_point, pheromone_type)
46
+            except NoPheromone:
47
+                raise
45 48
         return direction
46 49
 
47 50
     @staticmethod
@@ -50,10 +53,8 @@ class MoveAction(Action):
50 53
 
51 54
     @staticmethod
52 55
     def _get_direction_of_pheromone(context, point, pheromone_type):
53
-        search_pheromone_distance = 1  # TODO: config
54
-        search_pheromone_in_points = context.get_arround_points_of(point, distance=search_pheromone_distance)
56
+        search_pheromone_in_points = context.get_arround_points_of(point, distance=1)
55 57
         try:
56
-            # TODO: ? Avoir plutot un DirectionPheromone.get_best_pheromone_direction_in ?
57 58
             best_pheromone_direction = DirectionPheromone.get_best_pheromone_direction_in(context,
58 59
                                                                                           point,
59 60
                                                                                           search_pheromone_in_points,
@@ -62,13 +63,6 @@ class MoveAction(Action):
62 63
         except NoPheromone as err:
63 64
             raise err
64 65
 
65
-    # def _get_random_direction_point(self, context, reference_point):
66
-    #     z, x, y = reference_point
67
-    #     direction_name = self._get_random_direction_name(context)
68
-    #     directions_modifier = directions_modifiers[direction_name]
69
-    #     new_position = (z + directions_modifier[0], x + directions_modifier[1], y + directions_modifier[2])
70
-    #     return (direction_name, new_position)
71
-
72 66
     def _get_random_direction(self, context):
73 67
         try:
74 68
             blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
@@ -96,21 +90,12 @@ class MoveAction(Action):
96 90
         return direction_name
97 91
 
98 92
     @staticmethod
99
-    def _get_point_for_direction(reference_point, direction):
100
-        # TODO: mettre une fonction cote direction.py pour appliquer le modifier.
101
-        z, x, y = reference_point
102
-        directions_modifier = directions_modifiers[direction]
103
-        return z + directions_modifier[0], x + directions_modifier[1], y + directions_modifier[2]
104
-
105
-    @staticmethod
106 93
     def _direction_point_is_possible(context, direction_point):
107 94
         return context.position_is_penetrable(direction_point)
108 95
 
109 96
     def run(self, obj, context, synergy_manager):
110 97
         if self._move_to_point is not None and self._move_to_direction != 14: # TODO: il ne faut pas choisir une direction 14.
111 98
             obj.set_position(self._move_to_point)
112
-            #direction_from = directions_opposites[self._move_to_direction]
113
-            #obj.set_direction_from(direction_from)
114 99
             context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
115 100
             context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)
116 101
             self._appose_pheromone(obj, context)
@@ -122,18 +107,8 @@ class MoveAction(Action):
122 107
                 obj.put_carry(obj_transported, (-1, 0, 0))
123 108
                 obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
124 109
                 context.metas.collections.add_remove(obj.get_id(),
125
-                    COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING)
126
-
127
-            # TMP: Devra etre un event et une action
128
-            # if self._move_to_point == (0, 5, 5):  # position de depart
129
-            #     if len(obj._carried):
130
-            #         obj_transported = obj.get_carried()
131
-            #         obj_transported.set_carried_by(None)
132
-            #         obj.put_carry(obj_transported, (-1, 5, 5))
133
-            #         context.metas.collections.add_remove(obj.get_id(), COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING)
134
-            #     obj.set_movement_mode(MOVE_MODE_EXPLO)
135
-
136
-
110
+                                                     COL_TRANSPORTER_NOT_CARRYING,
111
+                                                     COL_TRANSPORTER_CARRYING)
137 112
         else:
138 113
             try:
139 114
                 blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
@@ -141,61 +116,8 @@ class MoveAction(Action):
141 116
                 blocked_since = 0
142 117
             context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)
143 118
 
144
-    # def _get_pheromone_direction_point(self, context, object_point):
145
-    #     try:
146
-    #         blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
147
-    #     except KeyError:
148
-    #         blocked_since = 0
149
-    #     if blocked_since > 3:
150
-    #         raise NoPheromoneMove()
151
-    #     # Si on explore, on cherche pheromone d'explo
152
-    #     # si on rentre a home, on cherche pheromone d'home...
153
-    #     # TODO: code arrache
154
-    #     object_movement_mode = context.metas.value.get(MOVE_MODE, self._object_id)
155
-    #     if object_movement_mode == MOVE_MODE_EXPLO:
156
-    #         pheromone_direction_type = PHEROMON_DIR_EXPLO
157
-    #     elif object_movement_mode == MOVE_MODE_GOHOME:
158
-    #         pheromone_direction_type = PHEROMON_DIR_HOME
159
-    #
160
-    #     sniff_points = context.get_arround_points_of(object_point, distance=0)
161
-    #     # Faire un compile des infos de pheromones
162
-    #
163
-    #     directions = {}
164
-    #     for sniff_point in sniff_points:
165
-    #         info = context.pheromones().get_info(sniff_point,
166
-    #                                              [PHEROMON_DIRECTION, pheromone_direction_type],
167
-    #                                              allow_empty=True,
168
-    #                                              empty_value={})
169
-    #         for direction in info:
170
-    #             if direction not in directions:
171
-    #                 directions[direction] = info[direction]
172
-    #             else:
173
-    #                 directions[direction] += info[direction]
174
-    #     if len(directions):
175
-    #         sorted_directions = sorted(directions.items(), key=operator.itemgetter(1))
176
-    #         sorted_directions.reverse()
177
-    #         #best_direction_name = sorted_directions[0][0]
178
-    #         best_direction_level = sorted_directions[0][1]
179
-    #         best_direction_names = [direction for direction in directions \
180
-    #                                if directions[direction] == best_direction_level]
181
-    #         # Si plusieurs best directions, choisir mm direction que la precedente
182
-    #         # si y a pas, au hasard.
183
-    #         last_dir_name = context.metas.value.get(PREVIOUS_DIRECTION, self._object_id)
184
-    #         if last_dir_name in best_direction_names:
185
-    #             direction_name = last_dir_name
186
-    #         else:
187
-    #             direction_name = choice(best_direction_names)
188
-    #
189
-    #         # DRY
190
-    #         z, x, y = object_point
191
-    #         directions_modifier = directions_modifiers[direction_name]
192
-    #         new_position = (z + directions_modifier[0], x + directions_modifier[1], y + directions_modifier[2])
193
-    #         return (direction_name, new_position)
194
-    #
195
-    #         pass
196
-    #     raise NoPheromoneMove()
197
-
198
-    def _appose_pheromone(self, obj, context):
119
+    @staticmethod
120
+    def _appose_pheromone(obj, context):
199 121
         # TODO: Cette action de pheromone doit etre une surcharge de Move afin d'avoir une Action Move generique.
200 122
         obj.get_brain().host_moved()
201 123
         try: