|
@@ -3,6 +3,7 @@ import typing
|
3
|
3
|
from random import choice
|
4
|
4
|
|
5
|
5
|
from sandbox.engulf.const import COLLECTION_GRASS
|
|
6
|
+from sandbox.engulf.exceptions import NotFoundWhereToGo
|
6
|
7
|
from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour, SubjectBehaviourSelector
|
7
|
8
|
from synergine2.simulation import Event
|
8
|
9
|
from synergine2.utils import ChunkManager
|
|
@@ -223,12 +224,30 @@ class Explore(SubjectBehaviour):
|
223
|
224
|
return True # for now, want move every time
|
224
|
225
|
|
225
|
226
|
def action(self, data) -> [Event]:
|
226
|
|
- direction = self.get_random_direction()
|
227
|
|
- position = get_position_for_direction(self.subject.position, direction)
|
228
|
|
- self.subject.position = position
|
229
|
|
- self.subject.previous_direction = direction
|
230
|
|
-
|
231
|
|
- return [MoveTo(self.subject.id, position)]
|
|
227
|
+ try:
|
|
228
|
+ position, direction = self.get_random_position_and_direction()
|
|
229
|
+ self.subject.position = position
|
|
230
|
+ self.subject.previous_direction = direction
|
|
231
|
+ return [MoveTo(self.subject.id, position)]
|
|
232
|
+ except NotFoundWhereToGo:
|
|
233
|
+ return []
|
|
234
|
+
|
|
235
|
+ def get_random_position_and_direction(self) -> tuple:
|
|
236
|
+ attempts = 0
|
|
237
|
+
|
|
238
|
+ while attempts <= 5:
|
|
239
|
+ attempts += 1
|
|
240
|
+ direction = self.get_random_direction()
|
|
241
|
+ position = get_position_for_direction(self.subject.position, direction)
|
|
242
|
+
|
|
243
|
+ if self.simulation.is_possible_subject_position(self.subject, position):
|
|
244
|
+ return position, direction
|
|
245
|
+
|
|
246
|
+ # If blocked, permit any direction (not slightly)
|
|
247
|
+ if attempts >= 3:
|
|
248
|
+ self.subject.previous_direction = None
|
|
249
|
+
|
|
250
|
+ raise NotFoundWhereToGo()
|
232
|
251
|
|
233
|
252
|
def get_random_direction(self):
|
234
|
253
|
if not self.subject.previous_direction:
|