Browse Source

grass distance for engulf

Bastien Sevajol 8 years ago
parent
commit
867e99a49d
3 changed files with 44 additions and 3 deletions
  1. 15 3
      sandbox/engulf/run.py
  2. 2 0
      sandbox/engulf/subject.py
  3. 27 0
      synergine2/xyz_utils.py

+ 15 - 3
sandbox/engulf/run.py View File

20
 """
20
 """
21
 from random import randint
21
 from random import randint
22
 
22
 
23
-from sandbox.engulf.subject import Cell, Grass
23
+from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
24
 from synergine2.core import Core
24
 from synergine2.core import Core
25
 from synergine2.cycle import CycleManager
25
 from synergine2.cycle import CycleManager
26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
27
 from synergine2.xyz import Simulation
27
 from synergine2.xyz import Simulation
28
 from sandbox.engulf.simulation import EngulfSubjects
28
 from sandbox.engulf.simulation import EngulfSubjects
29
+from synergine2.xyz_utils import get_around_positions_of
29
 
30
 
30
 
31
 
31
 class Engulf(Simulation):
32
 class Engulf(Simulation):
78
     start_count: int,
79
     start_count: int,
79
     start_position: tuple,
80
     start_position: tuple,
80
     end_position: tuple,
81
     end_position: tuple,
81
-    density: int=10,
82
+    density: int=5,
82
 ) -> None:
83
 ) -> None:
83
     grasses = []
84
     grasses = []
84
 
85
 
88
             randint(start_position[1], end_position[1]+1),
89
             randint(start_position[1], end_position[1]+1),
89
             randint(start_position[2], end_position[2]+1),
90
             randint(start_position[2], end_position[2]+1),
90
         )
91
         )
92
+
91
         if position not in subjects.grass_xyz:
93
         if position not in subjects.grass_xyz:
92
             grass = Grass(
94
             grass = Grass(
93
                 simulation=subjects.simulation,
95
                 simulation=subjects.simulation,
96
             grasses.append(grass)
98
             grasses.append(grass)
97
             subjects.append(grass)
99
             subjects.append(grass)
98
 
100
 
99
-        # TODO: density
101
+    for grass in subjects.simulation.collections[COLLECTION_GRASS][:]:
102
+        a = 1
103
+        for around in get_around_positions_of(grass.position, distance=density):
104
+            if around not in subjects.grass_xyz:
105
+                grass = Grass(
106
+                    simulation=subjects.simulation,
107
+                    position=around,
108
+                )
109
+                grasses.append(grass)
110
+                subjects.append(grass)
111
+                # TODO: valeur nutritive regressive avec eloignement
100
 
112
 
101
 
113
 
102
 def main():
114
 def main():

+ 2 - 0
sandbox/engulf/subject.py View File

4
 COLLECTION_CELL = 'CELL'
4
 COLLECTION_CELL = 'CELL'
5
 COLLECTION_ALIVE = 'ALIVE'
5
 COLLECTION_ALIVE = 'ALIVE'
6
 COLLECTION_EATABLE = 'EATABLE'
6
 COLLECTION_EATABLE = 'EATABLE'
7
+COLLECTION_GRASS = 'GRASS'
7
 
8
 
8
 
9
 
9
 class Cell(XYZSubjectMixin, Subject):
10
 class Cell(XYZSubjectMixin, Subject):
17
 class Grass(XYZSubjectMixin, Subject):
18
 class Grass(XYZSubjectMixin, Subject):
18
     collections = [
19
     collections = [
19
         COLLECTION_EATABLE,
20
         COLLECTION_EATABLE,
21
+        COLLECTION_GRASS,
20
     ]
22
     ]

+ 27 - 0
synergine2/xyz_utils.py View File

171
     if not exclude_start_position:
171
     if not exclude_start_position:
172
         points.append(position)
172
         points.append(position)
173
     return points
173
     return points
174
+
175
+
176
+def get_around_positions_of(
177
+        position,
178
+        distance=1,
179
+        exclude_start_point=True,
180
+) -> list:
181
+    """
182
+    Return positions around a point.
183
+    :param position: (x, y, z) tuple
184
+    :param distance: Distance to compute
185
+    :return: list of (x, y, z) positions
186
+    """
187
+    start_x = position[0] - distance
188
+    start_y = position[1] - distance
189
+    # start_z = position[0] - distance
190
+    positions = []
191
+    range_distance = (distance * 2) + 1
192
+    for dx in range(range_distance):
193
+        for dy in range(range_distance):
194
+            # for dz in range(range_distance):
195
+            # points.append((start_z+dz, start_x+dx, start_y+dy))
196
+            positions.append((start_x + dx, start_y + dy, position[2]))
197
+    if exclude_start_point:
198
+        positions.remove(position)
199
+
200
+    return positions