Kaynağa Gözat

Engulf: Slighty move

Bastien Sevajol 7 yıl önce
ebeveyn
işleme
51750db617

+ 12 - 7
sandbox/engulf/behaviour.py Dosyayı Görüntüle

@@ -1,12 +1,12 @@
1 1
 # coding: utf-8
2
-from random import randint
2
+from random import choice
3 3
 
4 4
 from sandbox.engulf.const import COLLECTION_GRASS
5 5
 from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
6 6
 from synergine2.simulation import Event
7 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 12
 class GrassGrownUp(Event):
@@ -165,12 +165,17 @@ class Explore(SubjectBehaviour):
165 165
     use = []
166 166
 
167 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 173
         return [MoveTo(self.subject.id, position)]
170 174
 
171 175
     def run(self, data):
172 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 Dosyayı Görüntüle

@@ -82,7 +82,7 @@ def fill_with_random_cells(
82 82
         position = (
83 83
             randint(start_position[0], end_position[0]+1),
84 84
             randint(start_position[1], end_position[1]+1),
85
-            randint(start_position[2], end_position[2]+1),
85
+            0,
86 86
         )
87 87
         if position not in subjects.cell_xyz:
88 88
             cell = Cell(
@@ -106,7 +106,7 @@ def fill_with_random_grass(
106 106
         position = (
107 107
             randint(start_position[0], end_position[0]+1),
108 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 112
         if position not in subjects.grass_xyz:

+ 1 - 0
sandbox/engulf/subject.py Dosyayı Görüntüle

@@ -11,6 +11,7 @@ class Cell(XYZSubjectMixin, Subject):
11 11
         COLLECTION_ALIVE,
12 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 15
     behaviours_classes = [
15 16
         SearchFood,
16 17
         Eat,

+ 66 - 2
synergine2/xyz.py Dosyayı Görüntüle

@@ -5,7 +5,6 @@ from math import acos
5 5
 
6 6
 from synergine2.simulation import SubjectMechanism, Subjects, Subject
7 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,6 +25,61 @@ Y
26 25
 
27 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 84
 def get_degree_from_north(a, b):
31 85
     if a == b:
@@ -57,9 +111,18 @@ class XYZSubjectMixin(object, metaclass=XYZSubjectMixinMetaClass):
57 111
         """
58 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 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 127
 class ProximityMixin(object):
65 128
     distance = 1
@@ -107,6 +170,7 @@ class ProximityMixin(object):
107 170
 
108 171
     @classmethod
109 172
     def get_distance_of(cls, position, subject: XYZSubjectMixin):
173
+        from synergine2.xyz_utils import get_distance_between_points  # cyclic import
110 174
         return get_distance_between_points(
111 175
             position,
112 176
             subject.position,

+ 12 - 1
synergine2/xyz_utils.py Dosyayı Görüntüle

@@ -2,6 +2,8 @@
2 2
 from math import sqrt
3 3
 import collections
4 4
 
5
+from synergine2.xyz import DIRECTION_MODIFIERS
6
+
5 7
 
6 8
 def get_positions_from_str_representation(str_representation):
7 9
     # TODO: Manage z axis (like ------------ as separator)
@@ -203,4 +205,13 @@ def get_around_positions_of(
203 205
 
204 206
 
205 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
+    )