Browse Source

wip on movements

Bastien Sevajol 8 years ago
parent
commit
1042ef55fd

+ 7 - 0
intelligine/cst.py View File

23
 MOVE_MODE_NURSE = IncrementedNamedInt.get('intelligine.basebug.move.mode.nurse')
23
 MOVE_MODE_NURSE = IncrementedNamedInt.get('intelligine.basebug.move.mode.nurse')
24
 MOVE_MODE_HOME = IncrementedNamedInt.get('intelligine.basebug.move.mode.home')
24
 MOVE_MODE_HOME = IncrementedNamedInt.get('intelligine.basebug.move.mode.home')
25
 
25
 
26
+MOVE_BYBASS = IncrementedNamedInt.get('intelligine.basebug.move.bypass')
27
+MOVE_BYBASS_DISTANCE = IncrementedNamedInt.get('intelligine.basebug.move.bypass.distance')
28
+MOVE_BYBASS_MEMORY = IncrementedNamedInt.get('intelligine.basebug.move.bypass.memory')
29
+MOVE_BYBASS_WALL = IncrementedNamedInt.get('intelligine.basebug.move.bypass.wall')
30
+MOVE_BYBASS_RE_WALKING = IncrementedNamedInt.get('intelligine.basebug.move.bypass.re_walking')
31
+
26
 TYPE = IncrementedNamedInt.get('intelligine.object.type')
32
 TYPE = IncrementedNamedInt.get('intelligine.object.type')
27
 TYPE_RESOURCE_EXPLOITABLE = IncrementedNamedInt.get('intelligine.object.type.resource.transformable')
33
 TYPE_RESOURCE_EXPLOITABLE = IncrementedNamedInt.get('intelligine.object.type.resource.transformable')
34
+TYPE_RESOURCE_EATABLE = IncrementedNamedInt.get('intelligine.object.type.resource.eatable')
28
 TYPE_NURSERY = IncrementedNamedInt.get('intelligine.object.type.nursery')
35
 TYPE_NURSERY = IncrementedNamedInt.get('intelligine.object.type.nursery')
29
 TYPE_ANT = IncrementedNamedInt.get('intelligine.object.type.ant')
36
 TYPE_ANT = IncrementedNamedInt.get('intelligine.object.type.ant')
30
 
37
 

+ 8 - 1
intelligine/display/Pygame.py View File

38
                 pass # No molecule here
38
                 pass # No molecule here
39
 
39
 
40
             try:
40
             try:
41
-                point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_EXPLO)
41
+                molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_EXPLO)
42
                 self.draw_surface(point, molecule_exploration_surface)
42
                 self.draw_surface(point, molecule_exploration_surface)
43
+
44
+                adapted_point = self._get_real_pixel_position_of_position(point)
45
+                myfont = pygame.font.SysFont("monospace", 15)
46
+                label = myfont.render(str(molecule.get_distance()), 1, (128,255,128))
47
+                self._screen.blit(label, adapted_point)
48
+
43
             except NoMolecule:
49
             except NoMolecule:
44
                 pass # No molecule here
50
                 pass # No molecule here
45
 
51
 
48
                 self.draw_surface(point, smell_food_surface)
54
                 self.draw_surface(point, smell_food_surface)
49
 
55
 
50
                 adapted_point = self._get_real_pixel_position_of_position(point)
56
                 adapted_point = self._get_real_pixel_position_of_position(point)
57
+                adapted_point = (adapted_point[0]+10, adapted_point[1]+10)
51
                 myfont = pygame.font.SysFont("monospace", 15)
58
                 myfont = pygame.font.SysFont("monospace", 15)
52
                 label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
59
                 label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
53
                 self._screen.blit(label, adapted_point)
60
                 self._screen.blit(label, adapted_point)

+ 2 - 2
intelligine/simulation/object/brain/AntBrain.py View File

37
         self._movement_mode = mode
37
         self._movement_mode = mode
38
         self._update_molecule_gland(mode)
38
         self._update_molecule_gland(mode)
39
         self._context.metas.value.set(MOVE_MODE, self._host.get_id(), mode)
39
         self._context.metas.value.set(MOVE_MODE, self._host.get_id(), mode)
40
-        self._distance_from_objective = 0
41
         self._update_molecule_searching(mode)
40
         self._update_molecule_searching(mode)
42
 
41
 
43
     def _update_molecule_gland(self, mode):
42
     def _update_molecule_gland(self, mode):
45
             molecule_direction_type = PHEROMON_DIR_HOME
44
             molecule_direction_type = PHEROMON_DIR_HOME
46
         elif mode == MOVE_MODE_GOHOME:
45
         elif mode == MOVE_MODE_GOHOME:
47
             molecule_direction_type = PHEROMON_DIR_EXPLO
46
             molecule_direction_type = PHEROMON_DIR_EXPLO
47
+            self._distance_from_objective = 0
48
         elif mode == MOVE_MODE_NURSE:
48
         elif mode == MOVE_MODE_NURSE:
49
             molecule_direction_type = None
49
             molecule_direction_type = None
50
         elif mode == MOVE_MODE_HOME:
50
         elif mode == MOVE_MODE_HOME:
51
-            molecule_direction_type = None
51
+            molecule_direction_type = PHEROMON_DIR_EXPLO
52
         else:
52
         else:
53
             raise NotImplementedError()
53
             raise NotImplementedError()
54
 
54
 

+ 26 - 21
intelligine/simulation/object/brain/part/move/AntMoveBrainPart.py View File

1
-from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
1
+from intelligine.simulation.object.brain.part.move.AntStar.ByPass import ByPass
2
+from intelligine.simulation.object.brain.part.move.AntStar.Host import Host
2
 from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
3
 from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
3
-from intelligine.synergy.event.move.direction import directions_modifiers, get_direction_for_degrees
4
+from intelligine.synergy.event.move.direction import directions_modifiers
4
 from synergine_xyz.cst import POSITION
5
 from synergine_xyz.cst import POSITION
5
 from intelligine.core.exceptions import NoMolecule, NoTypeInMolecule
6
 from intelligine.core.exceptions import NoMolecule, NoTypeInMolecule
6
-from intelligine.cst import MOLECULE_SEARCHING, MOVE_MODE_EXPLO, \
7
-    MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, EXPLORATION_VECTOR, POINTS_SMELL, MOLECULES, MOLECULES_DIRECTION, \
8
-    SMELL_FOOD, SMELL_EGG
7
+from intelligine.cst import MOLECULE_SEARCHING, MOVE_MODE_EXPLO, MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, \
8
+    EXPLORATION_VECTOR, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG
9
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
9
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
10
-from synergine_xyz.geometry import get_degree_from_north
11
 
10
 
12
 
11
 
13
 class AntMoveBrainPart(MoveBrainPart):
12
 class AntMoveBrainPart(MoveBrainPart):
58
         search_molecule_in_points = context.get_around_points_of_point(point)
57
         search_molecule_in_points = context.get_around_points_of_point(point)
59
         try:
58
         try:
60
             best_molecule_direction = DirectionMolecule.get_best_molecule_direction_in(context,
59
             best_molecule_direction = DirectionMolecule.get_best_molecule_direction_in(context,
61
-                                                                                          point,
62
-                                                                                          search_molecule_in_points,
63
-                                                                                          molecule_type)
60
+                                                                                       point,
61
+                                                                                       search_molecule_in_points,
62
+                                                                                       molecule_type)
64
             return best_molecule_direction
63
             return best_molecule_direction
65
         except NoMolecule as err:
64
         except NoMolecule as err:
66
             raise err
65
             raise err
67
 
66
 
68
-    @staticmethod
69
-    def _get_direction_with_exploration_vector(context, object_id):
70
-        current_position = context.metas.value.get(POSITION, object_id)
71
-        exploration_vector = context.metas.value.get(EXPLORATION_VECTOR, object_id)
72
-        # TODO: inverser (math)
73
-        home_vector = (exploration_vector[0] - (exploration_vector[0]*2),
74
-                       exploration_vector[1] - (exploration_vector[1]*2))
75
-        home_position = (0, current_position[1]+home_vector[0], current_position[2]+home_vector[1])
76
-        degree_from_north = get_degree_from_north(current_position, home_position)
77
-        direction_for_home_vector = get_direction_for_degrees(degree_from_north)
67
+    @classmethod
68
+    def _get_direction_with_exploration_vector(cls, context, object_id):
69
+        ant_star = cls._get_by_pass_brain(context, object_id)
70
+        ant_star.advance()
71
+        ant_star.has_moved()
72
+        return ant_star.get_host().get_moved_to_direction()
78
 
73
 
79
-        return direction_for_home_vector
74
+    @classmethod
75
+    def _get_by_pass_brain(cls, context, object_id):
76
+        # We use an adaptation of AntStar
77
+        exploration_vector = context.metas.value.get(EXPLORATION_VECTOR, object_id)
78
+        home_vector = (-exploration_vector[0], -exploration_vector[1])
79
+        ant_host = Host(context, object_id)
80
+        return ByPass(ant_host, home_vector, context, object_id)
80
 
81
 
81
     # TODO: obj pas necessaire, il est dans _host
82
     # TODO: obj pas necessaire, il est dans _host
82
     def done(self, obj, context):
83
     def done(self, obj, context):
106
 
107
 
107
         if movement_mode == MOVE_MODE_GOHOME and self._on_home_smell(context, obj.get_id()):
108
         if movement_mode == MOVE_MODE_GOHOME and self._on_home_smell(context, obj.get_id()):
108
             self._host_brain.switch_to_mode(MOVE_MODE_HOME)
109
             self._host_brain.switch_to_mode(MOVE_MODE_HOME)
110
+            ant_star = self._get_by_pass_brain(context, obj.get_id())
111
+            ant_star.erase()
109
             # TODO: on change les molecule recherché (Food => SmellFood, definis dans Take, en fct de ce qui est take)
112
             # TODO: on change les molecule recherché (Food => SmellFood, definis dans Take, en fct de ce qui est take)
110
 
113
 
111
         elif movement_mode == MOVE_MODE_HOME and not self._on_home_smell(context, obj.get_id()):
114
         elif movement_mode == MOVE_MODE_HOME and not self._on_home_smell(context, obj.get_id()):
112
             self._host_brain.switch_to_mode(MOVE_MODE_EXPLO)
115
             self._host_brain.switch_to_mode(MOVE_MODE_EXPLO)
113
             self._start_new_exploration()
116
             self._start_new_exploration()
114
 
117
 
118
+        elif movement_mode == MOVE_MODE_EXPLO and self._on_home_smell(context, obj.get_id()):
119
+            self._start_new_exploration()  # TODO: rename en reinit_explo
120
+
115
         # TODO: sitch explo si rien a faire (rien a poser par exemple) et HOME
121
         # TODO: sitch explo si rien a faire (rien a poser par exemple) et HOME
116
 
122
 
117
         # TODO: Poser sur StockedFood
123
         # TODO: Poser sur StockedFood
135
         self._set_exploration_vector((self._exploration_vector[0] + just_move_vector[1],
141
         self._set_exploration_vector((self._exploration_vector[0] + just_move_vector[1],
136
                                       self._exploration_vector[1] + just_move_vector[2]))
142
                                       self._exploration_vector[1] + just_move_vector[2]))
137
 
143
 
138
-
139
     def _apply_context(self, obj, context):
144
     def _apply_context(self, obj, context):
140
         movement_mode = self._host_brain.get_movement_mode()
145
         movement_mode = self._host_brain.get_movement_mode()
141
         if movement_mode == MOVE_MODE_EXPLO or movement_mode == MOVE_MODE_GOHOME:
146
         if movement_mode == MOVE_MODE_EXPLO or movement_mode == MOVE_MODE_GOHOME:

+ 16 - 5
intelligine/simulation/object/brain/part/transport/AntPutBrainPart.py View File

1
 from intelligine.core.exceptions import CantFindWhereToPut
1
 from intelligine.core.exceptions import CantFindWhereToPut
2
-from intelligine.cst import MOVE_MODE_EXPLO, TYPE_RESOURCE_EXPLOITABLE, CARRIED
2
+from intelligine.cst import MOVE_MODE_EXPLO, TYPE_RESOURCE_EXPLOITABLE, CARRIED, MOVE_MODE_NURSE, TYPE_NURSERY, \
3
+    MOVE_MODE_HOME, TYPE_RESOURCE_EATABLE, TYPE, MOVE_MODE, MOVE_MODE_GOHOME
3
 from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
4
 from intelligine.simulation.object.brain.part.transport.TransportBrainPart import TransportBrainPart
4
 from intelligine.synergy.object.Food import Food
5
 from intelligine.synergy.object.Food import Food
5
 from synergine_xyz.cst import POSITION, POSITIONS
6
 from synergine_xyz.cst import POSITION, POSITIONS
9
 
10
 
10
     # TODO: methode __nit_ pour la classe ?
11
     # TODO: methode __nit_ pour la classe ?
11
     _mode_matches = {
12
     _mode_matches = {
12
-        MOVE_MODE_EXPLO: [TYPE_RESOURCE_EXPLOITABLE],
13
+        MOVE_MODE_NURSE: [TYPE_NURSERY],
14
+        MOVE_MODE_HOME: [TYPE_RESOURCE_EATABLE],
15
+        MOVE_MODE_GOHOME: []
16
+    }
17
+
18
+    _types_matches = {
19
+        TYPE_RESOURCE_EXPLOITABLE: [TYPE_RESOURCE_EATABLE]
13
     }
20
     }
14
 
21
 
15
     @classmethod
22
     @classmethod
16
     def can_put(cls, context, object_id, object_near_id):
23
     def can_put(cls, context, object_id, object_near_id):
17
-        object_carried_id = context.metas.value.get(CARRIED, object_id)
18
-        #  Pour le moment on considere qu'un objet peut-etre depose a cote d'un objet qui a un type identique
19
-        return cls._objects_have_same_type(context, object_carried_id, object_near_id)
24
+        # Si l'objet à coté fait partie des objets concernés par le mode du porteur
25
+        if cls._match_with_mode(context, object_id, object_near_id):
26
+            # Et si les objet sont rangeable enssemble:
27
+            object_carried_id = context.metas.value.get(CARRIED, object_id)
28
+            return cls._objects_types_match(context, object_carried_id, object_near_id)
29
+        return False
20
 
30
 
21
     @classmethod
31
     @classmethod
22
     def get_put_position(cls, context, object_id, object_near_id):
32
     def get_put_position(cls, context, object_id, object_near_id):
54
     def done(self, obj, puted_object, context):
64
     def done(self, obj, puted_object, context):
55
         # TODO: Il faut refact/logique qqpart pour ca !! Genre Brain.done(PUT, ??)
65
         # TODO: Il faut refact/logique qqpart pour ca !! Genre Brain.done(PUT, ??)
56
         if isinstance(puted_object, Food):
66
         if isinstance(puted_object, Food):
67
+            # TODO: Quel mode ? On vient de poser (ps forcement dans la colonie) cls._mode_swicth ?
57
             obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
68
             obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
58
             # TODO: TEST Depose au -1 pour des raisons de test. Plus tard ce sera des tas comme un autre !
69
             # TODO: TEST Depose au -1 pour des raisons de test. Plus tard ce sera des tas comme un autre !
59
             puted_object.set_position((-1, 0, 0))
70
             puted_object.set_position((-1, 0, 0))

+ 23 - 6
intelligine/simulation/object/brain/part/transport/TransportBrainPart.py View File

5
 class TransportBrainPart(BrainPart):
5
 class TransportBrainPart(BrainPart):
6
 
6
 
7
     _mode_matches = {}
7
     _mode_matches = {}
8
+    _types_matches = {}
8
 
9
 
9
     @classmethod
10
     @classmethod
10
     def _match_with_mode(cls, context, object_id, concerned_object_id):
11
     def _match_with_mode(cls, context, object_id, concerned_object_id):
15
         return False
16
         return False
16
 
17
 
17
     @classmethod
18
     @classmethod
18
-    def _objects_have_same_type(cls, context, object_carried_id, object_to_put_id):
19
-        object_carried_types = context.metas.list.get(TYPE, object_carried_id)
20
-        for object_carried_type in object_carried_types:
21
-            if context.metas.list.have(TYPE, object_to_put_id, object_carried_type):
22
-                return True
23
-        return False
19
+    def _objects_types_match(cls, context, object_a_id, object_b_id):
20
+        """
21
+
22
+        Retourne vrai si un des type de l'objet b se trouve dans la lise de type définis dans _types_matches pour
23
+        l'un des types de l'objet a.
24
+
25
+        :param context:
26
+        :param object_a_id:
27
+        :param object_b_id:
28
+        :return: bool
29
+        """
30
+        object_a_types = context.metas.list.get(TYPE, object_a_id)
31
+        object_b_types = context.metas.list.get(TYPE, object_b_id)
32
+
33
+        for object_a_type in object_a_types:
34
+            if object_a_type in cls._types_matches:
35
+                wanted_types = cls._types_matches[object_a_type]
36
+                for object_b_type in object_b_types:
37
+                    if object_b_type in wanted_types:
38
+                        return True
39
+
40
+        return False

+ 3 - 0
intelligine/synergy/event/move/direction.py View File

17
 directions_under_level = (1, 2, 3, 4, 5, 6, 7, 8, 9)
17
 directions_under_level = (1, 2, 3, 4, 5, 6, 7, 8, 9)
18
 directions_same_level = (10, 11, 12, 13, 15, 16, 17, 18)
18
 directions_same_level = (10, 11, 12, 13, 15, 16, 17, 18)
19
 directions_upper_level = (19, 20, 21, 22, 23, 24, 25, 26, 27)
19
 directions_upper_level = (19, 20, 21, 22, 23, 24, 25, 26, 27)
20
+
21
+directions_by_levels = (directions_under_level, directions_same_level, directions_upper_level)
22
+
20
 directions_modifiers = {
23
 directions_modifiers = {
21
     # (z, x, y)
24
     # (z, x, y)
22
     1: (-1, -1, -1),
25
     1: (-1, -1, -1),

+ 2 - 1
intelligine/synergy/object/StockedFood.py View File

1
-from intelligine.cst import TYPE, TYPE_RESOURCE_EXPLOITABLE, COL_EATABLE, COL_SMELL, SMELL_FOOD
1
+from intelligine.cst import TYPE, TYPE_RESOURCE_EXPLOITABLE, COL_EATABLE, COL_SMELL, SMELL_FOOD, TYPE_RESOURCE_EATABLE
2
 from intelligine.synergy.object.Food import Food
2
 from intelligine.synergy.object.Food import Food
3
 
3
 
4
 
4
 
7
     def __init__(self, collection, context):
7
     def __init__(self, collection, context):
8
         super().__init__(collection, context)
8
         super().__init__(collection, context)
9
         context.metas.list.remove(TYPE, self.get_id(), TYPE_RESOURCE_EXPLOITABLE)
9
         context.metas.list.remove(TYPE, self.get_id(), TYPE_RESOURCE_EXPLOITABLE)
10
+        context.metas.list.add(TYPE, self.get_id(), TYPE_RESOURCE_EATABLE)
10
         self._add_col(COL_EATABLE)
11
         self._add_col(COL_EATABLE)
11
         self._add_col(COL_SMELL)
12
         self._add_col(COL_SMELL)
12
         self._set_smell(SMELL_FOOD)
13
         self._set_smell(SMELL_FOOD)

+ 1 - 0
requirements.txt View File

1
 synergine>=0.0.1
1
 synergine>=0.0.1
2
 synergine_xyz>=0.0.1
2
 synergine_xyz>=0.0.1
3
+git+https://github.com/buxx/AntStar.git

+ 1 - 0
tests_requirements.txt View File

1
 git+http://github.com/buxx/synergine.git
1
 git+http://github.com/buxx/synergine.git
2
 git+http://github.com/buxx/synergine_xyz.git
2
 git+http://github.com/buxx/synergine_xyz.git
3
+git+https://github.com/buxx/AntStar.git