|
@@ -2,18 +2,18 @@
|
2
|
2
|
import typing
|
3
|
3
|
from random import choice
|
4
|
4
|
|
5
|
|
-from sandbox.engulf.const import COLLECTION_GRASS
|
|
5
|
+from sandbox.engulf.const import COLLECTION_GRASS, COLLECTION_CELL
|
6
|
6
|
from sandbox.engulf.exceptions import NotFoundWhereToGo
|
7
|
7
|
from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour, SubjectBehaviourSelector
|
8
|
8
|
from synergine2.simulation import Event
|
9
|
9
|
from synergine2.utils import ChunkManager
|
10
|
|
-from synergine2.xyz import ProximitySubjectMechanism, DIRECTIONS, DIRECTION_SLIGHTLY, DIRECTION_FROM_NORTH_DEGREES, \
|
11
|
|
- get_direction_from_north_degree
|
12
|
|
-from synergine2.xyz_utils import get_around_positions_of_positions, get_around_positions_of, get_position_for_direction
|
|
10
|
+from synergine2.xyz import ProximitySubjectMechanism, DIRECTIONS, DIRECTION_SLIGHTLY, get_direction_from_north_degree
|
|
11
|
+from synergine2.xyz_utils import get_around_positions_of_positions, get_position_for_direction
|
13
|
12
|
|
14
|
13
|
|
15
|
14
|
if False:
|
16
|
15
|
from sandbox.engulf.subject import Grass
|
|
16
|
+ from sandbox.engulf.subject import PreyCell
|
17
|
17
|
|
18
|
18
|
|
19
|
19
|
class GrassGrownUp(Event):
|
|
@@ -129,7 +129,7 @@ class GrassSpawnBehaviour(SimulationBehaviour):
|
129
|
129
|
return events
|
130
|
130
|
|
131
|
131
|
|
132
|
|
-class EatableDirectProximityMechanism(ProximitySubjectMechanism):
|
|
132
|
+class GrassEatableDirectProximityMechanism(ProximitySubjectMechanism):
|
133
|
133
|
distance = 1.41
|
134
|
134
|
feel_collections = [COLLECTION_GRASS]
|
135
|
135
|
|
|
@@ -137,6 +137,16 @@ class EatableDirectProximityMechanism(ProximitySubjectMechanism):
|
137
|
137
|
return subject.density >= self.config.simulation.eat_grass_required_density
|
138
|
138
|
|
139
|
139
|
|
|
140
|
+class PreyEatableDirectProximityMechanism(ProximitySubjectMechanism):
|
|
141
|
+ distance = 1.41
|
|
142
|
+ feel_collections = [COLLECTION_CELL]
|
|
143
|
+
|
|
144
|
+ def acceptable_subject(self, subject: 'PreyCell') -> bool:
|
|
145
|
+
|
|
146
|
+ from sandbox.engulf.subject import PreyCell
|
|
147
|
+ return isinstance(subject, PreyCell)
|
|
148
|
+
|
|
149
|
+
|
140
|
150
|
class MoveTo(Event):
|
141
|
151
|
def __init__(self, subject_id: int, position: tuple, *args, **kwargs):
|
142
|
152
|
super().__init__(*args, **kwargs)
|
|
@@ -159,20 +169,27 @@ class EatEvent(Event):
|
159
|
169
|
self.eaten_new_density = eaten_new_density
|
160
|
170
|
|
161
|
171
|
|
162
|
|
-class SearchFood(SubjectBehaviour):
|
|
172
|
+class AttackEvent(Event):
|
|
173
|
+ def __init__(self, attacker_id: int, attacked_id: int, *args, **kwargs):
|
|
174
|
+ super().__init__(*args, **kwargs)
|
|
175
|
+ self.attacker_id = attacker_id
|
|
176
|
+ self.attacked_id = attacked_id
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+class SearchGrass(SubjectBehaviour):
|
163
|
180
|
"""
|
164
|
181
|
Si une nourriture a une case de distance et cellule non rassasié, move dans sa direction.
|
165
|
182
|
"""
|
166
|
|
- use = [EatableDirectProximityMechanism]
|
|
183
|
+ use = [GrassEatableDirectProximityMechanism]
|
167
|
184
|
|
168
|
185
|
def run(self, data):
|
169
|
186
|
if self.subject.appetite < self.config.simulation.search_food_appetite_required:
|
170
|
187
|
return False
|
171
|
188
|
|
172
|
|
- if not data[EatableDirectProximityMechanism]:
|
|
189
|
+ if not data[GrassEatableDirectProximityMechanism]:
|
173
|
190
|
return False
|
174
|
191
|
|
175
|
|
- direction_degrees = [d['direction'] for d in data[EatableDirectProximityMechanism]]
|
|
192
|
+ direction_degrees = [d['direction'] for d in data[GrassEatableDirectProximityMechanism]]
|
176
|
193
|
return get_direction_from_north_degree(choice(direction_degrees))
|
177
|
194
|
|
178
|
195
|
def action(self, data) -> [Event]:
|
|
@@ -184,7 +201,7 @@ class SearchFood(SubjectBehaviour):
|
184
|
201
|
return [MoveTo(self.subject.id, position)]
|
185
|
202
|
|
186
|
203
|
|
187
|
|
-class Eat(SubjectBehaviour):
|
|
204
|
+class EatGrass(SubjectBehaviour):
|
188
|
205
|
"""
|
189
|
206
|
Prduit un immobilisme si sur une case de nourriture, dans le cas ou la cellule n'est as rassasié.
|
190
|
207
|
"""
|
|
@@ -269,12 +286,25 @@ class Hungry(SubjectBehaviour):
|
269
|
286
|
return choice(DIRECTION_SLIGHTLY[self.subject.previous_direction])
|
270
|
287
|
|
271
|
288
|
|
|
289
|
+class Attack(SubjectBehaviour):
|
|
290
|
+ use = [PreyEatableDirectProximityMechanism]
|
|
291
|
+
|
|
292
|
+ def run(self, data):
|
|
293
|
+ if data[PreyEatableDirectProximityMechanism]:
|
|
294
|
+ return choice(data[PreyEatableDirectProximityMechanism])
|
|
295
|
+ return False
|
|
296
|
+
|
|
297
|
+ def action(self, data) -> [Event]:
|
|
298
|
+
|
|
299
|
+ return [AttackEvent(attacker_id=self.subject.id, attacked_id=data['subject'].id)]
|
|
300
|
+
|
|
301
|
+
|
272
|
302
|
class CellBehaviourSelector(SubjectBehaviourSelector):
|
273
|
303
|
|
274
|
304
|
behaviour_hierarchy = (
|
275
|
305
|
(
|
276
|
|
- Eat,
|
277
|
|
- SearchFood,
|
|
306
|
+ EatGrass,
|
|
307
|
+ SearchGrass,
|
278
|
308
|
Explore,
|
279
|
309
|
),
|
280
|
310
|
)
|