Explorar el Código

Working home vector integration!

Bastien Sevajol hace 9 años
padre
commit
6fa3c3b444

+ 19 - 0
intelligine/display/Pygame.py Ver fichero

@@ -1,4 +1,5 @@
1 1
 from intelligine.core.exceptions import NoMolecule
2
+from intelligine.synergy.object.ant.Ant import Ant
2 3
 from synergine_xyz.display.Pygame import Pygame as XyzPygame
3 4
 import pygame
4 5
 from intelligine.cst import PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO, MOLECULES, \
@@ -13,6 +14,7 @@ class Pygame(XyzPygame):
13 14
         super().__init__(config, context, synergy_manager)
14 15
         self._is_display_molecules = False
15 16
         self._is_display_smells = False
17
+        self._draw_callbacks = []
16 18
 
17 19
     def receive(self, actions_done):
18 20
         super().receive(actions_done)
@@ -75,3 +77,20 @@ class Pygame(XyzPygame):
75 77
                 self._is_display_molecules = False
76 78
             else:
77 79
                 self._is_display_molecules = True
80
+
81
+    def draw_object(self, obj, point):
82
+        super().draw_object(obj, point)
83
+        # TODO: DEBUG
84
+        if isinstance(obj, Ant):
85
+            myfont = pygame.font.SysFont("monospace", 15)
86
+            label = myfont.render(str(obj.get_id()), 1, (255,255,0))
87
+            self._draw_callbacks.append(lambda: self._screen.blit(label, point))
88
+
89
+    def start_of_cycle(self):
90
+        super().start_of_cycle()
91
+        self._draw_callbacks = []
92
+
93
+    def end_of_cycle(self):
94
+        for draw_callback in self._draw_callbacks:
95
+            draw_callback()
96
+        super().end_of_cycle()

+ 6 - 0
intelligine/simulation/object/brain/part/BrainPart.py Ver fichero

@@ -3,5 +3,11 @@ class BrainPart():
3 3
     def __init__(self, host_brain):
4 4
         self._host_brain = host_brain
5 5
 
6
+    def get_host_brain(self):
7
+        return self._host_brain
8
+
9
+    def get_host(self):
10
+        return self.get_host_brain().get_host()
11
+
6 12
     def done(self, obj, context):
7 13
         pass

+ 4 - 2
intelligine/simulation/object/brain/part/move/AntMoveBrainPart.py Ver fichero

@@ -1,7 +1,7 @@
1 1
 from intelligine.simulation.object.brain.part.move.AntStar.ByPass import ByPass
2 2
 from intelligine.simulation.object.brain.part.move.AntStar.Host import Host
3 3
 from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
4
-from intelligine.synergy.event.move.direction import directions_modifiers
4
+from intelligine.synergy.event.move.direction import directions_modifiers, get_position_with_direction_decal
5 5
 from synergine_xyz.cst import POSITION
6 6
 from intelligine.core.exceptions import NoMolecule, NoTypeInMolecule
7 7
 from intelligine.cst import MOLECULE_SEARCHING, MOVE_MODE_EXPLO, MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, \
@@ -147,4 +147,6 @@ class AntMoveBrainPart(MoveBrainPart):
147 147
             self._update_exploration_vector()
148 148
 
149 149
     def _start_new_exploration(self):
150
-        self._exploration_vector = (0, 0)
150
+        # On vient de rentrer dans le monde exterieur, le vecteur de départ pointe vers la case précedente
151
+        # qui est une case dans la forteresse.
152
+        self._exploration_vector = get_position_with_direction_decal(self.get_host().get_previous_direction())

+ 57 - 0
intelligine/simulation/object/brain/part/move/AntStar/ByPass.py Ver fichero

@@ -0,0 +1,57 @@
1
+from antstar.GlueWallAntBrain import GlueWallAntBrain
2
+from intelligine.cst import EXPLORATION_VECTOR, MOVE_BYBASS, MOVE_BYBASS_DISTANCE, MOVE_BYBASS_MEMORY, MOVE_BYBASS_WALL, MOVE_BYBASS_RE_WALKING
3
+
4
+
5
+class ByPass(GlueWallAntBrain):
6
+
7
+    def __init__(self, host, home_vector, context, object_id):
8
+        """
9
+
10
+        Note: We broke Liskov principle here.
11
+
12
+        :param host:
13
+        :param home_vector:
14
+        :param context:
15
+        :param object_id:
16
+        :return:
17
+        """
18
+        super().__init__(host, home_vector)
19
+        self._context = context
20
+        self._object_id = object_id
21
+        self._memory_since_blocked = context.metas.value.get(MOVE_BYBASS_MEMORY, object_id, allow_empty=True,
22
+                                                             empty_value=[])
23
+        self._by_passing = context.metas.value.get(MOVE_BYBASS, object_id, allow_empty=True, empty_value=False)
24
+        self._distance_when_blocked = context.metas.value.get(MOVE_BYBASS_DISTANCE, object_id, allow_empty=True,
25
+                                                              empty_value=None)
26
+        self._current_wall_square_position = context.metas.value.get(MOVE_BYBASS_WALL,
27
+                                                                     object_id,
28
+                                                                     allow_empty=True,
29
+                                                                     empty_value=None)
30
+        self._is_re_walking = context.metas.value.get(MOVE_BYBASS_RE_WALKING,
31
+                                                      object_id,
32
+                                                      allow_empty=True,
33
+                                                      empty_value=False)
34
+
35
+    def _set_home_vector(self, home_vector):
36
+        super()._set_home_vector(home_vector)
37
+        self._context.metas.value.set(EXPLORATION_VECTOR, self._object_id, home_vector)
38
+
39
+    def _set_memory_since_blocked(self, memory_since_blocked):
40
+        super()._set_memory_since_blocked(memory_since_blocked)
41
+        self._context.metas.value.set(MOVE_BYBASS_MEMORY, self._object_id, memory_since_blocked)
42
+
43
+    def _set_by_passing(self, by_passing):
44
+        super()._set_by_passing(by_passing)
45
+        self._context.metas.value.set(MOVE_BYBASS, self._object_id, by_passing)
46
+
47
+    def _set_distance_when_blocked(self, distance):
48
+        super()._set_distance_when_blocked(distance)
49
+        self._context.metas.value.set(MOVE_BYBASS_DISTANCE, self._object_id, distance)
50
+
51
+    def _set_current_wall_square(self, position_of_square):
52
+        super()._set_current_wall_square(position_of_square)
53
+        self._context.metas.value.set(MOVE_BYBASS_WALL, self._object_id, position_of_square)
54
+
55
+    def _set_is_re_walking(self, is_re_walking):
56
+        super()._set_is_re_walking(is_re_walking)
57
+        self._context.metas.value.set(MOVE_BYBASS_RE_WALKING, self._object_id, is_re_walking)

+ 24 - 0
intelligine/simulation/object/brain/part/move/AntStar/Host.py Ver fichero

@@ -0,0 +1,24 @@
1
+from intelligine.simulation.object.brain.part.move.AntStar.HostFeeler import HostFeeler
2
+from synergine_xyz.cst import POSITION
3
+
4
+
5
+class Host:
6
+
7
+    def __init__(self, context, object_id):
8
+        self._context = context
9
+        self._object_id = object_id
10
+        self._feeler = HostFeeler(context, object_id)
11
+        self._moved_to_direction = None
12
+
13
+    def get_position(self):
14
+        current_position = self._context.metas.value.get(POSITION, self._object_id)
15
+        return current_position[1], current_position[2]
16
+
17
+    def get_feeler(self):
18
+        return self._feeler
19
+
20
+    def move_to(self, direction):
21
+        self._moved_to_direction = direction
22
+
23
+    def get_moved_to_direction(self):
24
+        return self._moved_to_direction

+ 24 - 0
intelligine/simulation/object/brain/part/move/AntStar/HostFeeler.py Ver fichero

@@ -0,0 +1,24 @@
1
+from intelligine.synergy.event.move.direction import get_position_with_direction_decal
2
+from synergine_xyz.cst import POSITION
3
+from synergine_xyz.geometry import distance_from_points
4
+
5
+
6
+class HostFeeler:
7
+
8
+    def __init__(self, context, object_id):
9
+        self._context = context
10
+        self._object_id = object_id
11
+        self._current_position = context.metas.value.get(POSITION, self._object_id)
12
+
13
+    def direction_is_free(self, direction_of_home):
14
+        position_will_be = get_position_with_direction_decal(direction_of_home, self._current_position)
15
+        return self._context.position_is_penetrable(position_will_be)
16
+
17
+    def position_is_free(self, position):
18
+        threed_position = (0, position[0], position[1])
19
+        points_distance = distance_from_points(threed_position, self._current_position)
20
+        if points_distance > 1:
21
+            raise Exception("Can't feel so far (%s to %s: %s)" % (str(self._current_position),
22
+                                                                  str(position),
23
+                                                                  str(points_distance)))
24
+        return self._context.position_is_penetrable(threed_position)