Bastien Sevajol 9 vuotta sitten
vanhempi
commit
beffa75a94

+ 3 - 1
socialintengine/cst.py Näytä tiedosto

@@ -1,4 +1,6 @@
1 1
 from synergine.lib.eint import IncrementedNamedInt
2 2
 
3 3
 ALIVE = IncrementedNamedInt.get('socialintengine.alive')
4
-IMPENETRABLE = IncrementedNamedInt.get('socialintengine.impenetrable')
4
+IMPENETRABLE = IncrementedNamedInt.get('socialintengine.impenetrable')
5
+PREVIOUS_DIRECTION = IncrementedNamedInt.get('socialintengine.previous_direction')
6
+BLOCKED_SINCE = IncrementedNamedInt.get('socialintengine.blocked_since')

BIN
socialintengine/display/pygame/image/ant.png Näytä tiedosto


+ 19 - 6
socialintengine/display/pygame/visualisation.py Näytä tiedosto

@@ -1,22 +1,35 @@
1 1
 from xyworld.display.object.pygame.PygameImage import PygameImage
2
+from xyworld.display.object.pygame.DirectionnedImage import DirectionnedImage
2 3
 from socialintengine.synergy.object.Bug import Bug
3 4
 from socialintengine.synergy.object.ant.Ant import Ant
4 5
 from socialintengine.synergy.object.Rock import Rock
5 6
 from os import getcwd
7
+from synergine.metas import metas
8
+from socialintengine.cst import PREVIOUS_DIRECTION
6 9
 
7
-# TODO: Url relative au fichier
8
-ant = PygameImage(getcwd()+'/socialintengine/display/pygame/image/ant2.png')
9
-bug = PygameImage(getcwd()+'/socialintengine/display/pygame/image/ant2.png')
10
-rock = PygameImage(getcwd()+'/socialintengine/display/pygame/image/rock.png')
10
+ant = PygameImage.from_filepath(getcwd()+'/socialintengine/display/pygame/image/ant.png')
11
+bug = PygameImage.from_filepath(getcwd()+'/socialintengine/display/pygame/image/ant.png')
12
+rock = PygameImage.from_filepath(getcwd()+'/socialintengine/display/pygame/image/rock.png')
13
+
14
+directions_ant = DirectionnedImage(ant)
15
+
16
+def bug_direction(bug):
17
+    try:
18
+        previous_direction = metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
19
+    except KeyError:
20
+        previous_direction = 14
21
+    return directions_ant.get_for_direction(previous_direction)
11 22
 
12 23
 visualisation = {
13 24
     'window': {},
14 25
     'objects': {
15 26
         Ant: {
16
-            'default': ant
27
+            'default': ant,
28
+            'callbacks': [bug_direction]
17 29
         },
18 30
         Bug: {
19
-            'default': bug
31
+            'default': bug,
32
+            'callbacks': [bug_direction]
20 33
         },
21 34
         Rock: {
22 35
             'default': rock

+ 42 - 12
socialintengine/synergy/event/move/MoveAction.py Näytä tiedosto

@@ -1,9 +1,10 @@
1 1
 from synergine.synergy.event.Action import Action
2 2
 from socialintengine.synergy.event.move.MoveEvent import MoveEvent
3
-from random import randint
3
+from random import randint, choice
4 4
 from xyzworld.cst import POSITION, POSITIONS
5
-from socialintengine.cst import IMPENETRABLE
5
+from socialintengine.cst import IMPENETRABLE, PREVIOUS_DIRECTION, BLOCKED_SINCE
6 6
 from synergine.synergy.Simulation import Simulation
7
+from socialintengine.synergy.event.move.direction import directions_same_level, directions_modifiers, directions_slighty
7 8
 
8 9
 
9 10
 class MoveAction(Action):
@@ -12,20 +13,41 @@ class MoveAction(Action):
12 13
 
13 14
     def __init__(self, object_id, parameters):
14 15
       super().__init__(object_id, parameters)
15
-      self._move_to = None
16
+      self._move_to_point = None
17
+      self._move_to_direction = None
16 18
 
17 19
     def prepare(self, context):
18 20
       object_point = context.metas.value.get(POSITION, self._object_id)
19
-      choosed_direction_point = self._get_random_direction_point(object_point)
21
+      choosed_direction_name, choosed_direction_point = self._get_random_direction_point(context, object_point)
20 22
       if self._direction_point_is_possible(context, choosed_direction_point):
21
-        self._move_to = choosed_direction_point
23
+        self._move_to_point = choosed_direction_point
24
+        self._move_to_direction = choosed_direction_name
22 25
 
23
-    def _get_random_direction_point(self, reference_point):
26
+    def _get_random_direction_point(self, context, reference_point):
24 27
         z, x, y = reference_point
25
-        new_z = z
26
-        new_x = x + randint(-1, 1)
27
-        new_y = y + randint(-1, 1)
28
-        return (new_z, new_x, new_y)
28
+        direction_name = self._get_random_direction_name(context)
29
+        directions_modifier = directions_modifiers[direction_name]
30
+        new_position = (z + directions_modifier[0], x + directions_modifier[1], y + directions_modifier[2])
31
+        return (direction_name, new_position)
32
+
33
+    def _get_random_direction_name(self, context):
34
+        try:
35
+            blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
36
+        except KeyError:
37
+            blocked_since = 0
38
+        direction_name = None
39
+        if blocked_since <= 3:  #TODO: config
40
+            try:
41
+                previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, self._object_id)
42
+                directions_list = directions_slighty[previous_direction]
43
+                direction_name = choice(directions_list)
44
+            except KeyError:
45
+                pass
46
+
47
+        if not direction_name:
48
+            direction_name = randint(directions_same_level[0], directions_same_level[1])
49
+
50
+        return direction_name
29 51
 
30 52
     def _direction_point_is_possible(self, context, direction_point):
31 53
         objects_ids_on_this_point = context.metas.list.get(POSITIONS, direction_point, allow_empty=True)
@@ -35,5 +57,13 @@ class MoveAction(Action):
35 57
         return True
36 58
 
37 59
     def run(self, obj, collection, context):
38
-        if self._move_to is not None:
39
-            obj.add_trace(self._move_to)
60
+        if self._move_to_point is not None:
61
+            obj.add_trace(self._move_to_point)
62
+            context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
63
+            context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)
64
+        else:
65
+            try:
66
+                blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
67
+            except:
68
+                blocked_since = 0
69
+            context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)

+ 2 - 2
socialintengine/synergy/event/move/MoveEvent.py Näytä tiedosto

@@ -1,5 +1,5 @@
1 1
 from synergine.synergy.event.Event import Event
2
-from xyzworld.mechanism.PositionedArroundMechanism import PositionedArroundMechanism
2
+from synergine.core.simulation.mechanism.Mechanism import Mechanism
3 3
 
4 4
 
5 5
 class MoveEvent(Event):
@@ -9,7 +9,7 @@ class MoveEvent(Event):
9 9
 
10 10
     def __init__(self, actions):
11 11
         super().__init__(actions)
12
-        self._mechanism = PositionedArroundMechanism
12
+        self._mechanism = Mechanism
13 13
 
14 14
     def _object_match(self, object_id, context, parameters={}):
15 15
       return True

+ 99 - 0
socialintengine/synergy/event/move/direction.py Näytä tiedosto

@@ -0,0 +1,99 @@
1
+
2
+"""
3
+Directions identifiers 3D, central position is 14.
4
+niv -1:  1   2  3
5
+         4   5  6
6
+         7   8  9
7
+
8
+niv 0:   10 11 12
9
+         13 14 15
10
+         16 17 18
11
+
12
+niv 1:   19 20 21
13
+         22 23 24
14
+         25 26 27
15
+"""
16
+
17
+directions = (0, 27)
18
+directions_under_level = (0, 9)
19
+directions_same_level = (10, 18)
20
+directions_upper_level = (19, 27)
21
+directions_modifiers = {
22
+  #  (z, x, y)
23
+  1: (-1, -1, -1),
24
+  2: (-1, 0, -1),
25
+  3: (-1, 1, -1),
26
+  4: (-1, -1, 0),
27
+  5: (-1, 0, 0),
28
+  6: (-1, 1, 0),
29
+  7: (-1, -1, 1),
30
+  8: (-1, 0, 1),
31
+  9: (-1, 1, 1),
32
+  #  (z, x, y)
33
+  10: (0, -1, -1),
34
+  11: (0, 0, -1),
35
+  12: (0, 1, -1),
36
+  13: (0, -1, 0),
37
+  14: (0, 0, 0),
38
+  15: (0, 1, 0),
39
+  16: (0, -1, 1),
40
+  17: (0, 0, 1),
41
+  18: (0, 1, 1),
42
+  #  (z, x, y)
43
+  19: (1, -1, -1),
44
+  20: (1, 0, -1),
45
+  21: (1, 1, -1),
46
+  22: (1, -1, 0),
47
+  23: (1, 0, 0),
48
+  24: (1, 1, 0),
49
+  25: (1, -1, 1),
50
+  26: (1, 0, 1),
51
+  27: (1, 1, 1),
52
+}
53
+
54
+"""
55
+Directions identifiers 3D, central position is 14.
56
+niv -1:  1   2  3
57
+         4   5  6
58
+         7   8  9
59
+
60
+niv 0:   10 11 12
61
+         13 14 15
62
+         16 17 18
63
+
64
+niv 1:   19 20 21
65
+         22 23 24
66
+         25 26 27
67
+"""
68
+
69
+directions_slighty = {
70
+  1: (1, 2, 4, 13, 10, 11),
71
+  2: (1, 2, 3, 10, 11, 12),
72
+  3: (2, 3, 6, 11, 12, 15),
73
+  4: (1, 4, 7, 10, 13, 16),
74
+  5: (1, 2, 3, 4, 5, 6, 7, 8, 9),
75
+  6: (2, 3, 6, 12, 15, 18),
76
+  7: (4, 7, 8, 13, 16, 17),
77
+  8: (7, 8, 9, 16, 17, 18),
78
+  9: (6, 9, 8, 15, 18, 17),
79
+  #  (z, x, y)
80
+  10: (13, 10, 11),
81
+  11: (10, 11, 12),
82
+  12: (11, 12, 15),
83
+  13: (10, 13, 16),
84
+  14: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27),
85
+  15: (12, 15, 18),
86
+  16: (13, 16, 17),
87
+  17: (16, 17, 18),
88
+  18: (15, 18, 17),
89
+  #  (z, x, y)
90
+  19: (22, 19, 20, 13, 10, 11),
91
+  20: (19, 20, 21, 10, 11, 12),
92
+  21: (20, 21, 24, 11, 12, 15),
93
+  22: (19, 22, 25, 10, 13, 16),
94
+  23: (19, 20, 21, 22, 23, 24, 25, 26, 27),
95
+  24: (21, 24, 27, 12, 15, 18),
96
+  25: (22, 25, 26, 13, 16, 17),
97
+  26: (25, 26, 27, 16, 17, 18),
98
+  27: (24, 27, 26, 15, 18, 17),
99
+}