ソースを参照

engulf: limited exp zone

Bastien Sevajol 8 年 前
コミット
14e2028167
共有4 個のファイルを変更した53 個の追加21 個の削除を含む
  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 ファイルの表示

3
 from random import choice
3
 from random import choice
4
 
4
 
5
 from sandbox.engulf.const import COLLECTION_GRASS
5
 from sandbox.engulf.const import COLLECTION_GRASS
6
+from sandbox.engulf.exceptions import NotFoundWhereToGo
6
 from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour, SubjectBehaviourSelector
7
 from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour, SubjectBehaviourSelector
7
 from synergine2.simulation import Event
8
 from synergine2.simulation import Event
8
 from synergine2.utils import ChunkManager
9
 from synergine2.utils import ChunkManager
223
         return True  # for now, want move every time
224
         return True  # for now, want move every time
224
 
225
 
225
     def action(self, data) -> [Event]:
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
     def get_random_direction(self):
252
     def get_random_direction(self):
234
         if not self.subject.previous_direction:
253
         if not self.subject.previous_direction:

+ 3 - 12
sandbox/engulf/run.py ファイルの表示

18
       * alone/not alone: - be alone + not alone
18
       * alone/not alone: - be alone + not alone
19
 
19
 
20
 """
20
 """
21
+import logging
21
 import os
22
 import os
22
 import sys
23
 import sys
23
 
24
 
24
-import logging
25
-
26
 synergine2_ath = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
25
 synergine2_ath = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
27
 sys.path.append(synergine2_ath)
26
 sys.path.append(synergine2_ath)
28
 
27
 
29
 from random import randint, seed
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
 from synergine2.config import Config
31
 from synergine2.config import Config
33
 from synergine2.log import get_default_logger
32
 from synergine2.log import get_default_logger
34
 from sandbox.engulf.subject import Cell, Grass
33
 from sandbox.engulf.subject import Cell, Grass
35
-from sandbox.engulf.const import COLLECTION_GRASS
36
 from synergine2.core import Core
34
 from synergine2.core import Core
37
 from synergine2.cycle import CycleManager
35
 from synergine2.cycle import CycleManager
38
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
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
 from synergine2.xyz_utils import get_around_positions_of, get_distance_between_points
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
 class GameTerminal(Terminal):
41
 class GameTerminal(Terminal):
51
     subscribed_events = [
42
     subscribed_events = [
52
         GrassGrownUp,
43
         GrassGrownUp,

+ 23 - 1
sandbox/engulf/simulation.py ファイルの表示

1
 # coding: utf-8
1
 # coding: utf-8
2
+from sandbox.engulf.behaviour import GrassSpawnBehaviour
2
 from sandbox.engulf.subject import Cell, Grass
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
 __author__ = 'bux'
6
 __author__ = 'bux'
6
 
7
 
30
 
31
 
31
         if isinstance(p_object, Grass):
32
         if isinstance(p_object, Grass):
32
             self.grass_xyz[p_object.position] = p_object
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 ファイルの表示

224
         if self.have_to_check_position_is_possible() \
224
         if self.have_to_check_position_is_possible() \
225
            and not self.simulation.is_possible_subject_position(p_object, p_object.position):
225
            and not self.simulation.is_possible_subject_position(p_object, p_object.position):
226
             raise PositionNotPossible('Position {} for {} is not possible'.format(
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
         self.xyz[p_object.position] = p_object
231
         self.xyz[p_object.position] = p_object