|
@@ -1,5 +1,7 @@
|
1
|
|
-from synergine2.simulation import SubjectBehaviour
|
|
1
|
+from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
|
2
|
2
|
from synergine2.simulation import Event
|
|
3
|
+from synergine2.utils import ChunkManager
|
|
4
|
+from synergine2.xyz_utils import get_around_positions_of_positions
|
3
|
5
|
|
4
|
6
|
|
5
|
7
|
class GrassGrownUp(Event):
|
|
@@ -9,6 +11,40 @@ class GrassGrownUp(Event):
|
9
|
11
|
self.density = density
|
10
|
12
|
|
11
|
13
|
|
|
14
|
+class GrassSpawn(Event):
|
|
15
|
+ def __init__(self, subject_id, position, density, *args, **kwargs):
|
|
16
|
+ super().__init__(*args, **kwargs)
|
|
17
|
+ self.subject_id = subject_id
|
|
18
|
+ self.position = position
|
|
19
|
+ self.density = density
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+class GrassSpotablePositionsMechanism(SimulationMechanism):
|
|
23
|
+ parallelizable = True
|
|
24
|
+
|
|
25
|
+ def run(self, process_number: int=None, process_count: int=None):
|
|
26
|
+ chunk_manager = ChunkManager(process_count)
|
|
27
|
+ positions = list(self.simulation.subjects.grass_xyz.keys())
|
|
28
|
+ positions_chunks = chunk_manager.make_chunks(positions)
|
|
29
|
+ spotables = []
|
|
30
|
+
|
|
31
|
+ for position in positions_chunks[process_number]:
|
|
32
|
+ arounds = get_around_positions_of_positions(position)
|
|
33
|
+ from_subject = self.simulation.subjects.grass_xyz[position]
|
|
34
|
+ around_data = {
|
|
35
|
+ 'from_subject': from_subject,
|
|
36
|
+ 'around': [],
|
|
37
|
+ }
|
|
38
|
+ for around in arounds:
|
|
39
|
+ if around not in self.simulation.subjects.grass_xyz:
|
|
40
|
+ around_data['around'].append(around)
|
|
41
|
+
|
|
42
|
+ if around_data['around']:
|
|
43
|
+ spotables.append(around_data)
|
|
44
|
+
|
|
45
|
+ return spotables
|
|
46
|
+
|
|
47
|
+
|
12
|
48
|
class GrowUp(SubjectBehaviour):
|
13
|
49
|
frequency = 20
|
14
|
50
|
|
|
@@ -21,3 +57,45 @@ class GrowUp(SubjectBehaviour):
|
21
|
57
|
self.subject.id,
|
22
|
58
|
self.subject.density,
|
23
|
59
|
)]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+class GrassSpawnBehaviour(SimulationBehaviour):
|
|
63
|
+ frequency = 100
|
|
64
|
+ use = [GrassSpotablePositionsMechanism]
|
|
65
|
+
|
|
66
|
+ def run(self, data):
|
|
67
|
+ spawns = []
|
|
68
|
+
|
|
69
|
+ for around_data in data[GrassSpotablePositionsMechanism]:
|
|
70
|
+ from_subject = around_data['from_subject']
|
|
71
|
+ arounds = around_data['around']
|
|
72
|
+ if from_subject.density >= 40:
|
|
73
|
+ spawns.extend(arounds)
|
|
74
|
+
|
|
75
|
+ return spawns
|
|
76
|
+
|
|
77
|
+ @classmethod
|
|
78
|
+ def merge_data(cls, new_data, start_data=None):
|
|
79
|
+ start_data = start_data or []
|
|
80
|
+ start_data.extend(new_data)
|
|
81
|
+ return start_data
|
|
82
|
+
|
|
83
|
+ def action(self, data) -> [Event]:
|
|
84
|
+ from sandbox.engulf.subject import Grass
|
|
85
|
+ events = []
|
|
86
|
+
|
|
87
|
+ for position in data:
|
|
88
|
+ if position not in list(self.simulation.subjects.grass_xyz.keys()):
|
|
89
|
+ new_grass = Grass(
|
|
90
|
+ self.simulation,
|
|
91
|
+ position=position,
|
|
92
|
+ density=20,
|
|
93
|
+ )
|
|
94
|
+ self.simulation.subjects.append(new_grass)
|
|
95
|
+ events.append(GrassSpawn(
|
|
96
|
+ new_grass.id,
|
|
97
|
+ new_grass.position,
|
|
98
|
+ new_grass.density,
|
|
99
|
+ ))
|
|
100
|
+
|
|
101
|
+ return events
|