Procházet zdrojové kódy

grass distance for engulf

Bastien Sevajol před 7 roky
rodič
revize
867e99a49d
3 změnil soubory, kde provedl 44 přidání a 3 odebrání
  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 Zobrazit soubor

@@ -20,12 +20,13 @@ Engulf is simulation containing:
20 20
 """
21 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 24
 from synergine2.core import Core
25 25
 from synergine2.cycle import CycleManager
26 26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
27 27
 from synergine2.xyz import Simulation
28 28
 from sandbox.engulf.simulation import EngulfSubjects
29
+from synergine2.xyz_utils import get_around_positions_of
29 30
 
30 31
 
31 32
 class Engulf(Simulation):
@@ -78,7 +79,7 @@ def fill_with_random_grass(
78 79
     start_count: int,
79 80
     start_position: tuple,
80 81
     end_position: tuple,
81
-    density: int=10,
82
+    density: int=5,
82 83
 ) -> None:
83 84
     grasses = []
84 85
 
@@ -88,6 +89,7 @@ def fill_with_random_grass(
88 89
             randint(start_position[1], end_position[1]+1),
89 90
             randint(start_position[2], end_position[2]+1),
90 91
         )
92
+
91 93
         if position not in subjects.grass_xyz:
92 94
             grass = Grass(
93 95
                 simulation=subjects.simulation,
@@ -96,7 +98,17 @@ def fill_with_random_grass(
96 98
             grasses.append(grass)
97 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 114
 def main():

+ 2 - 0
sandbox/engulf/subject.py Zobrazit soubor

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

+ 27 - 0
synergine2/xyz_utils.py Zobrazit soubor

@@ -171,3 +171,30 @@ def get_around_positions_of_positions(position, exclude_start_position=True) ->
171 171
     if not exclude_start_position:
172 172
         points.append(position)
173 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