Ver código fonte

Engulf: Slighty move

Bastien Sevajol 8 anos atrás
pai
commit
51750db617

+ 12 - 7
sandbox/engulf/behaviour.py Ver arquivo

1
 # coding: utf-8
1
 # coding: utf-8
2
-from random import randint
2
+from random import choice
3
 
3
 
4
 from sandbox.engulf.const import COLLECTION_GRASS
4
 from sandbox.engulf.const import COLLECTION_GRASS
5
 from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
5
 from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
6
 from synergine2.simulation import Event
6
 from synergine2.simulation import Event
7
 from synergine2.utils import ChunkManager
7
 from synergine2.utils import ChunkManager
8
-from synergine2.xyz import ProximitySubjectMechanism
9
-from synergine2.xyz_utils import get_around_positions_of_positions, get_around_positions_of
8
+from synergine2.xyz import ProximitySubjectMechanism, DIRECTIONS, DIRECTION_SLIGHTLY
9
+from synergine2.xyz_utils import get_around_positions_of_positions, get_around_positions_of, get_position_for_direction
10
 
10
 
11
 
11
 
12
 class GrassGrownUp(Event):
12
 class GrassGrownUp(Event):
165
     use = []
165
     use = []
166
 
166
 
167
     def action(self, data) -> [Event]:
167
     def action(self, data) -> [Event]:
168
-        position = self.get_random_around_position()
168
+        direction = self.get_random_direction()
169
+        position = get_position_for_direction(self.subject.position, direction)
170
+        self.subject.position = position
171
+        self.subject.previous_direction = direction
172
+
169
         return [MoveTo(self.subject.id, position)]
173
         return [MoveTo(self.subject.id, position)]
170
 
174
 
171
     def run(self, data):
175
     def run(self, data):
172
         return True  # for now, want move every time
176
         return True  # for now, want move every time
173
 
177
 
174
-    def get_random_around_position(self):
175
-        around_positions = get_around_positions_of(self.subject.position)
176
-        return around_positions[randint(0, len(around_positions)-1)]
178
+    def get_random_direction(self):
179
+        if not self.subject.previous_direction:
180
+            return choice(DIRECTIONS)
181
+        return choice(DIRECTION_SLIGHTLY[self.subject.previous_direction])

+ 2 - 2
sandbox/engulf/run.py Ver arquivo

82
         position = (
82
         position = (
83
             randint(start_position[0], end_position[0]+1),
83
             randint(start_position[0], end_position[0]+1),
84
             randint(start_position[1], end_position[1]+1),
84
             randint(start_position[1], end_position[1]+1),
85
-            randint(start_position[2], end_position[2]+1),
85
+            0,
86
         )
86
         )
87
         if position not in subjects.cell_xyz:
87
         if position not in subjects.cell_xyz:
88
             cell = Cell(
88
             cell = Cell(
106
         position = (
106
         position = (
107
             randint(start_position[0], end_position[0]+1),
107
             randint(start_position[0], end_position[0]+1),
108
             randint(start_position[1], end_position[1]+1),
108
             randint(start_position[1], end_position[1]+1),
109
-            randint(start_position[2], end_position[2]+1),
109
+            0,
110
         )
110
         )
111
 
111
 
112
         if position not in subjects.grass_xyz:
112
         if position not in subjects.grass_xyz:

+ 1 - 0
sandbox/engulf/subject.py Ver arquivo

11
         COLLECTION_ALIVE,
11
         COLLECTION_ALIVE,
12
         COLLECTION_EATABLE,
12
         COLLECTION_EATABLE,
13
     ]
13
     ]
14
+    # TODO: Mettre en place la "selection/choix": car il y a deux move possible chaque cycle ci-dessous.
14
     behaviours_classes = [
15
     behaviours_classes = [
15
         SearchFood,
16
         SearchFood,
16
         Eat,
17
         Eat,

+ 66 - 2
synergine2/xyz.py Ver arquivo

5
 
5
 
6
 from synergine2.simulation import SubjectMechanism, Subjects, Subject
6
 from synergine2.simulation import SubjectMechanism, Subjects, Subject
7
 from synergine2.simulation import Simulation as BaseSimulation
7
 from synergine2.simulation import Simulation as BaseSimulation
8
-from synergine2.xyz_utils import get_distance_between_points
9
 
8
 
10
 
9
 
11
 """
10
 """
26
 
25
 
27
 COLLECTION_XYZ = 'COLLECTION_XYZ'
26
 COLLECTION_XYZ = 'COLLECTION_XYZ'
28
 
27
 
28
+NORTH = 11
29
+NORTH_EST = 12
30
+EST = 15
31
+SOUTH_EST = 18
32
+SOUTH = 17
33
+SOUTH_WEST = 16
34
+WEST = 13
35
+NORTH_WEST = 10
36
+
37
+DIRECTIONS = (
38
+    NORTH,
39
+    NORTH_EST,
40
+    EST,
41
+    SOUTH_EST,
42
+    SOUTH,
43
+    SOUTH_WEST,
44
+    WEST,
45
+    NORTH_WEST,
46
+)
47
+
48
+DIRECTION_FROM_NORTH_DEGREES = {
49
+    (0, 22.5): NORTH,
50
+    (22.5, 67): NORTH_EST,
51
+    (67, 112.5): EST,
52
+    (112.5, 157.5): SOUTH_EST,
53
+    (157.5, 202.5): SOUTH,
54
+    (202.5, 247.5): SOUTH_WEST,
55
+    (247.5, 292.5): WEST,
56
+    (292.5, 337.5): NORTH_WEST,
57
+    (337.5, 360): NORTH,
58
+    (337.5, 0): NORTH
59
+}
60
+
61
+DIRECTION_SLIGHTLY = {
62
+    NORTH: (NORTH_WEST, NORTH, NORTH_EST),
63
+    NORTH_EST: (NORTH, NORTH_EST, EST),
64
+    EST: (NORTH_EST, EST, SOUTH_EST),
65
+    SOUTH_EST: (EST, SOUTH_EST, SOUTH),
66
+    SOUTH: (SOUTH_EST, SOUTH, SOUTH_WEST),
67
+    SOUTH_WEST: (SOUTH, SOUTH_WEST, WEST),
68
+    WEST: (SOUTH_WEST, WEST, NORTH_WEST),
69
+    NORTH_WEST: (WEST, NORTH_WEST, NORTH),
70
+}
71
+
72
+DIRECTION_MODIFIERS = {
73
+    NORTH_WEST: (-1, -1, 0),
74
+    NORTH: (0, -1, 0),
75
+    NORTH_EST: (1, -1, 0),
76
+    WEST: (-1, 0, 0),
77
+    EST: (1, 0, 0),
78
+    SOUTH_WEST: (-1, 1, 0),
79
+    SOUTH: (0, 1, 0),
80
+    SOUTH_EST: (1, 1, 0),
81
+}
82
+
29
 
83
 
30
 def get_degree_from_north(a, b):
84
 def get_degree_from_north(a, b):
31
     if a == b:
85
     if a == b:
57
         """
111
         """
58
         :param position: tuple with (x, y, z)
112
         :param position: tuple with (x, y, z)
59
         """
113
         """
60
-        self.position = kwargs.pop('position')
114
+        self._position = kwargs.pop('position')
115
+        self.previous_direction = None
61
         super().__init__(*args, **kwargs)
116
         super().__init__(*args, **kwargs)
62
 
117
 
118
+    @property
119
+    def position(self):
120
+        return self._position
121
+
122
+    @position.setter
123
+    def position(self, value):
124
+        self._position = value
125
+
63
 
126
 
64
 class ProximityMixin(object):
127
 class ProximityMixin(object):
65
     distance = 1
128
     distance = 1
107
 
170
 
108
     @classmethod
171
     @classmethod
109
     def get_distance_of(cls, position, subject: XYZSubjectMixin):
172
     def get_distance_of(cls, position, subject: XYZSubjectMixin):
173
+        from synergine2.xyz_utils import get_distance_between_points  # cyclic import
110
         return get_distance_between_points(
174
         return get_distance_between_points(
111
             position,
175
             position,
112
             subject.position,
176
             subject.position,

+ 12 - 1
synergine2/xyz_utils.py Ver arquivo

2
 from math import sqrt
2
 from math import sqrt
3
 import collections
3
 import collections
4
 
4
 
5
+from synergine2.xyz import DIRECTION_MODIFIERS
6
+
5
 
7
 
6
 def get_positions_from_str_representation(str_representation):
8
 def get_positions_from_str_representation(str_representation):
7
     # TODO: Manage z axis (like ------------ as separator)
9
     # TODO: Manage z axis (like ------------ as separator)
203
 
205
 
204
 
206
 
205
 def get_distance_between_points(a: tuple, b: tuple) -> float:
207
 def get_distance_between_points(a: tuple, b: tuple) -> float:
206
-    return abs(sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2))
208
+    return abs(sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2))
209
+
210
+
211
+def get_position_for_direction(from_position: tuple, direction: int) -> tuple:
212
+    modifier = DIRECTION_MODIFIERS[direction]
213
+    return (
214
+        from_position[0] + modifier[0],
215
+        from_position[1] + modifier[1],
216
+        from_position[2] + modifier[2],
217
+    )