Browse Source

refactorise function who return around pheromones

Bastien Sevajol 9 years ago
parent
commit
421f8902e8
1 changed files with 21 additions and 13 deletions
  1. 21 13
      intelligine/simulation/pheromone/DirectionPheromone.py

+ 21 - 13
intelligine/simulation/pheromone/DirectionPheromone.py View File

@@ -12,23 +12,15 @@ class DirectionPheromone():
12 12
         context.pheromones().increment_with_pheromone(point, pheromone)
13 13
         context.metas.list.add(PHEROMON_POSITIONS, PHEROMON_POSITIONS, point, assert_not_in=False)
14 14
 
15
-    @staticmethod
16
-    def get_direction_for_point(context, point, pheromone_type):
15
+    @classmethod
16
+    def get_direction_for_point(cls, context, point, pheromone_type):
17 17
         flavour = context.pheromones().get_flavour(point)
18 18
         pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
19 19
 
20 20
         distance = pheromone.get_distance()
21
-        around_points = context.get_around_points_of(point)
22
-        # TODO: Cet algo around a mettre ailleurs
23
-        around_pheromones_points = []
24
-        for around_point in around_points:
25
-            flavour = context.pheromones().get_flavour(around_point)
26
-            try:
27
-                around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
28
-                if around_pheromone.get_distance() < distance:
29
-                    around_pheromones_points.append((around_point, around_pheromone))
30
-            except NoPheromone:
31
-                pass  # No pheromone, ok continue to sniff around
21
+        around_pheromone_filter = lambda around_pheromone: around_pheromone.get_distance() < distance
22
+        around_pheromones_points = cls._get_around_pheromones(context, point, pheromone_type,
23
+                                                              pheromone_filter=around_pheromone_filter)
32 24
 
33 25
         if not around_pheromones_points:
34 26
             raise NoPheromone()
@@ -54,6 +46,22 @@ class DirectionPheromone():
54 46
         return direction
55 47
 
56 48
     @staticmethod
49
+    def _get_around_pheromones(context, reference_point, pheromone_type,
50
+                               pheromone_filter=lambda around_pheromone: True):
51
+        around_points = context.get_around_points_of(reference_point)
52
+        around_pheromones_points = []
53
+        for around_point in around_points:
54
+            flavour = context.pheromones().get_flavour(around_point)
55
+            try:
56
+                around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
57
+                if pheromone_filter(around_pheromone):
58
+                    around_pheromones_points.append((around_point, around_pheromone))
59
+            except NoPheromone:
60
+                pass  # No pheromone, ok continue to sniff around
61
+
62
+        return around_pheromones_points
63
+
64
+    @staticmethod
57 65
     def get_best_pheromone_direction_in(context, reference_point, points, pheromone_type):
58 66
         around_pheromones_points = []
59 67
         for around_point in points: