Quellcode durchsuchen

engulf: limited exp zone

Bastien Sevajol vor 7 Jahren
Ursprung
Commit
14e2028167
4 geänderte Dateien mit 53 neuen und 21 gelöschten Zeilen
  1. 25 6
      sandbox/engulf/behaviour.py
  2. 3 12
      sandbox/engulf/run.py
  3. 23 1
      sandbox/engulf/simulation.py
  4. 2 2
      synergine2/xyz.py

+ 25 - 6
sandbox/engulf/behaviour.py Datei anzeigen

@@ -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:

+ 3 - 12
sandbox/engulf/run.py Datei anzeigen

@@ -18,35 +18,26 @@ Engulf is simulation containing:
18 18
       * alone/not alone: - be alone + not alone
19 19
 
20 20
 """
21
+import logging
21 22
 import os
22 23
 import sys
23 24
 
24
-import logging
25
-
26 25
 synergine2_ath = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
27 26
 sys.path.append(synergine2_ath)
28 27
 
29 28
 from random import randint, seed
30
-from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn, GrassSpawnBehaviour, MoveTo, EatEvent
29
+from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn, MoveTo, EatEvent
31 30
 
32 31
 from synergine2.config import Config
33 32
 from synergine2.log import get_default_logger
34 33
 from sandbox.engulf.subject import Cell, Grass
35
-from sandbox.engulf.const import COLLECTION_GRASS
36 34
 from synergine2.core import Core
37 35
 from synergine2.cycle import CycleManager
38 36
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
39
-from synergine2.xyz import XYZSimulation
40
-from sandbox.engulf.simulation import EngulfSubjects
37
+from sandbox.engulf.simulation import EngulfSubjects, Engulf
41 38
 from synergine2.xyz_utils import get_around_positions_of, get_distance_between_points
42 39
 
43 40
 
44
-class Engulf(XYZSimulation):
45
-    behaviours_classes = [
46
-        GrassSpawnBehaviour,
47
-    ]
48
-
49
-
50 41
 class GameTerminal(Terminal):
51 42
     subscribed_events = [
52 43
         GrassGrownUp,

+ 23 - 1
sandbox/engulf/simulation.py Datei anzeigen

@@ -1,6 +1,7 @@
1 1
 # coding: utf-8
2
+from sandbox.engulf.behaviour import GrassSpawnBehaviour
2 3
 from sandbox.engulf.subject import Cell, Grass
3
-from synergine2.xyz import XYZSubjects, XYZSubjectMixin
4
+from synergine2.xyz import XYZSubjects, XYZSubjectMixin, XYZSimulation
4 5
 
5 6
 __author__ = 'bux'
6 7
 
@@ -30,3 +31,24 @@ class EngulfSubjects(XYZSubjects):
30 31
 
31 32
         if isinstance(p_object, Grass):
32 33
             self.grass_xyz[p_object.position] = p_object
34
+
35
+
36
+class Engulf(XYZSimulation):
37
+    behaviours_classes = [
38
+        GrassSpawnBehaviour,
39
+    ]
40
+
41
+    def is_possible_position(self, position: tuple) -> bool:
42
+        top_left = (-35, -35, 0)
43
+        bottom_right = (35, 35, 0)
44
+
45
+        pos_x = position[0]
46
+        pos_y = position[1]
47
+
48
+        if pos_x < top_left[0] or pos_x > bottom_right[0]:
49
+            return False
50
+
51
+        if pos_y < top_left[1] or pos_y > bottom_right[1]:
52
+            return False
53
+
54
+        return True

+ 2 - 2
synergine2/xyz.py Datei anzeigen

@@ -224,8 +224,8 @@ class XYZSubjects(Subjects):
224 224
         if self.have_to_check_position_is_possible() \
225 225
            and not self.simulation.is_possible_subject_position(p_object, p_object.position):
226 226
             raise PositionNotPossible('Position {} for {} is not possible'.format(
227
-                p_object.position,
228
-                p_object,
227
+                str(p_object.position),
228
+                str(p_object),
229 229
             ))
230 230
 
231 231
         self.xyz[p_object.position] = p_object