|
@@ -2,7 +2,9 @@
|
2
|
2
|
import typing
|
3
|
3
|
|
4
|
4
|
from synergine2.config import Config
|
5
|
|
-from synergine2.simulation import SimulationBehaviour, SubjectBehaviour
|
|
5
|
+from synergine2.simulation import SimulationBehaviour
|
|
6
|
+from synergine2.simulation import SubjectBehaviour
|
|
7
|
+from synergine2.simulation import SubjectMechanism
|
6
|
8
|
from synergine2.simulation import Intention
|
7
|
9
|
from synergine2.simulation import Simulation
|
8
|
10
|
from synergine2.simulation import Event
|
|
@@ -13,7 +15,7 @@ class MoveToIntention(Intention):
|
13
|
15
|
def __init__(self, move_to: typing.Tuple[int, int]) -> None:
|
14
|
16
|
self.move_to = move_to
|
15
|
17
|
self.path = [] # type: typing.List[typing.Tuple[int, int]]
|
16
|
|
- self.path_progression = None # type: int
|
|
18
|
+ self.path_progression = -1 # type: int
|
17
|
19
|
|
18
|
20
|
|
19
|
21
|
class RequestMoveBehaviour(SimulationBehaviour):
|
|
@@ -42,7 +44,7 @@ class RequestMoveBehaviour(SimulationBehaviour):
|
42
|
44
|
|
43
|
45
|
try:
|
44
|
46
|
subject = self.simulation.subjects.index[subject_id]
|
45
|
|
- subject.intentions.append(self.move_intention_class(move_to))
|
|
47
|
+ subject.intentions.set(self.move_intention_class(move_to))
|
46
|
48
|
except KeyError:
|
47
|
49
|
# TODO: log error here
|
48
|
50
|
pass
|
|
@@ -50,11 +52,87 @@ class RequestMoveBehaviour(SimulationBehaviour):
|
50
|
52
|
return []
|
51
|
53
|
|
52
|
54
|
|
|
55
|
+class MoveToMechanism(SubjectMechanism):
|
|
56
|
+ def run(self):
|
|
57
|
+ # TODO: Si move to: Si nouveau: a*, si bloque, a*, sinon rien
|
|
58
|
+ # TODO: pourquoi un mechanism plutot que dans run du behaviour ? faire en sorte que lorsque on calcule,
|
|
59
|
+ # si un subject est déjà passé par là et qu'il va au même endroit, ne pas recalculer.
|
|
60
|
+ try:
|
|
61
|
+ # TODO: MoveToIntention doit être configurable
|
|
62
|
+ move = self.subject.intentions.get(MoveToIntention)
|
|
63
|
+ move = typing.cast(MoveToIntention, move)
|
|
64
|
+ new_path = move.path or None
|
|
65
|
+
|
|
66
|
+ if not move.path:
|
|
67
|
+ # TODO: Fake to test
|
|
68
|
+ new_path = []
|
|
69
|
+ for i in range(20):
|
|
70
|
+ new_path.append((
|
|
71
|
+ self.subject.position[0],
|
|
72
|
+ self.subject.position[1] + i,
|
|
73
|
+ ))
|
|
74
|
+
|
|
75
|
+ next_move = new_path[move.path_progression + 1]
|
|
76
|
+ # TODO: fin de path
|
|
77
|
+ if not self.simulation.is_possible_position(next_move):
|
|
78
|
+ # TODO: refaire le path
|
|
79
|
+ new_path = ['...']
|
|
80
|
+
|
|
81
|
+ return {
|
|
82
|
+ 'new_path': new_path,
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ except KeyError:
|
|
86
|
+ return None
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+class MoveEvent(Event):
|
|
90
|
+ def __init__(self, subject_id: int, position: tuple, *args, **kwargs):
|
|
91
|
+ super().__init__(*args, **kwargs)
|
|
92
|
+ self.subject_id = subject_id
|
|
93
|
+ self.position = position
|
|
94
|
+
|
|
95
|
+ def repr_debug(self) -> str:
|
|
96
|
+ return '{}: subject_id:{}, position:{}'.format(
|
|
97
|
+ type(self).__name__,
|
|
98
|
+ self.subject_id,
|
|
99
|
+ self.position,
|
|
100
|
+ )
|
|
101
|
+
|
|
102
|
+
|
53
|
103
|
class MoveToBehaviour(SubjectBehaviour):
|
|
104
|
+ use = [MoveToMechanism]
|
|
105
|
+ move_to_mechanism = MoveToMechanism
|
|
106
|
+
|
54
|
107
|
def run(self, data):
|
55
|
|
- # TODO: progresser dans l'intention (comment implementer ça?)
|
56
|
|
- raise NotImplementedError()
|
|
108
|
+ # TODO: on fait vraiment rien ici ? Note: meme si il n'y a pas de new_path, l'action doit s'effectuer
|
|
109
|
+ # du moment qu'il y a une intention de move
|
|
110
|
+ move_to_data = data[self.move_to_mechanism]
|
|
111
|
+ if move_to_data:
|
|
112
|
+ return move_to_data
|
|
113
|
+ return False
|
57
|
114
|
|
58
|
115
|
def action(self, data) -> [Event]:
|
59
|
116
|
# TODO: effectuer un move vers une nouvelle position ou faire progresser "l'entre-deux"
|
60
|
|
- raise NotImplementedError()
|
|
117
|
+ new_path = data['new_path']
|
|
118
|
+ try:
|
|
119
|
+ # TODO: MoveToIntention doit être configurable
|
|
120
|
+ move = self.subject.intentions.get(MoveToIntention)
|
|
121
|
+ move = typing.cast(MoveToIntention, move)
|
|
122
|
+
|
|
123
|
+ if new_path:
|
|
124
|
+ move.path = new_path
|
|
125
|
+ move.path_progression = -1
|
|
126
|
+
|
|
127
|
+ # TODO: progression et lorsque "vraiment avance d'une case" envoyer le Move
|
|
128
|
+ # pour le moment on move direct
|
|
129
|
+ # TODO: fin de path
|
|
130
|
+ move.path_progression += 1 # BUG: ca progresse pas ?
|
|
131
|
+ new_position = move.path[move.path_progression]
|
|
132
|
+ self.subject.position = new_position
|
|
133
|
+
|
|
134
|
+ return [MoveEvent(self.subject.id, new_position)]
|
|
135
|
+
|
|
136
|
+ except KeyError:
|
|
137
|
+ # TODO: log ? Il devrait y avoir un move puisque data du run/mechanism !
|
|
138
|
+ pass
|