|
@@ -1,233 +0,0 @@
|
1
|
|
-# coding: utf-8
|
2
|
|
-import time
|
3
|
|
-import typing
|
4
|
|
-
|
5
|
|
-from synergine2.config import Config
|
6
|
|
-from synergine2.simulation import SimulationBehaviour
|
7
|
|
-from synergine2.simulation import Simulation
|
8
|
|
-from synergine2.simulation import Event
|
9
|
|
-from synergine2.simulation import SubjectMechanism
|
10
|
|
-from synergine2.simulation import SubjectBehaviour
|
11
|
|
-from synergine2_xyz.move.intention import MoveToIntention
|
12
|
|
-from synergine2_xyz.simulation import XYZSimulation
|
13
|
|
-
|
14
|
|
-
|
15
|
|
-class RequestMoveBehaviour(SimulationBehaviour):
|
16
|
|
- move_intention_class = MoveToIntention
|
17
|
|
-
|
18
|
|
- @classmethod
|
19
|
|
- def merge_data(cls, new_data, start_data=None):
|
20
|
|
- # TODO: behaviour/Thing dedicated to Gui -> Simulation ?
|
21
|
|
- pass # This behaviour is designed to be launch by terminal
|
22
|
|
-
|
23
|
|
- def __init__(
|
24
|
|
- self,
|
25
|
|
- config: Config,
|
26
|
|
- simulation: Simulation,
|
27
|
|
- ):
|
28
|
|
- super().__init__(config, simulation)
|
29
|
|
- self.simulation = typing.cast(XYZSimulation, self.simulation)
|
30
|
|
-
|
31
|
|
- def run(self, data):
|
32
|
|
- # TODO: behaviour/Thing dedicated to Gui -> Simulation ?
|
33
|
|
- pass # This behaviour is designed to be launch by terminal
|
34
|
|
-
|
35
|
|
- def action(self, data) -> typing.List[Event]:
|
36
|
|
- subject_id = data['subject_id']
|
37
|
|
- move_to = data['move_to']
|
38
|
|
-
|
39
|
|
- try:
|
40
|
|
- subject = self.simulation.subjects.index[subject_id]
|
41
|
|
- subject.intentions.set(self.move_intention_class(
|
42
|
|
- move_to,
|
43
|
|
- start_time=time.time(),
|
44
|
|
- gui_action=data['gui_action'],
|
45
|
|
- ))
|
46
|
|
- except KeyError:
|
47
|
|
- # TODO: log error here
|
48
|
|
- pass
|
49
|
|
-
|
50
|
|
- return []
|
51
|
|
-
|
52
|
|
-
|
53
|
|
-class MoveToMechanism(SubjectMechanism):
|
54
|
|
- def run(self):
|
55
|
|
- # TODO: Si move to: Si nouveau: a*, si bloque, a*, sinon rien
|
56
|
|
- # TODO: pourquoi un mechanism plutot que dans run du behaviour ? faire en sorte que lorsque on calcule,
|
57
|
|
- # si un subject est déjà passé par là et qu'il va au même endroit, ne pas recalculer.
|
58
|
|
- try:
|
59
|
|
- # TODO: MoveToIntention doit être configurable
|
60
|
|
- move = self.subject.intentions.get(MoveToIntention)
|
61
|
|
- move = typing.cast(MoveToIntention, move)
|
62
|
|
- new_path = None
|
63
|
|
-
|
64
|
|
- if not move.path:
|
65
|
|
- move.path = self.simulation.physics.found_path(
|
66
|
|
- start=self.subject.position,
|
67
|
|
- end=move.move_to,
|
68
|
|
- subject=self.subject,
|
69
|
|
- )
|
70
|
|
-
|
71
|
|
- # Note: We are in process, move change will be lost
|
72
|
|
- new_path = move.path
|
73
|
|
-
|
74
|
|
- # move.path = []
|
75
|
|
- # new_path = move.path
|
76
|
|
- # for i in range(20):
|
77
|
|
- # move.path.append((
|
78
|
|
- # self.subject.position[0],
|
79
|
|
- # self.subject.position[1] + i,
|
80
|
|
- # ))
|
81
|
|
-
|
82
|
|
- next_move = move.path[move.path_progression + 1]
|
83
|
|
- # TODO: fin de path
|
84
|
|
- if not self.simulation.is_possible_position(next_move):
|
85
|
|
- # TODO: refaire le path
|
86
|
|
- new_path = ['...']
|
87
|
|
-
|
88
|
|
- return {
|
89
|
|
- 'new_path': new_path,
|
90
|
|
- 'last_intention_time': move.last_intention_time,
|
91
|
|
- 'just_reach': move.just_reach,
|
92
|
|
- 'initial': move.initial,
|
93
|
|
- 'gui_action': move.gui_action,
|
94
|
|
- }
|
95
|
|
-
|
96
|
|
- except IndexError: # TODO: Specialize ? No movement left
|
97
|
|
- return {
|
98
|
|
- 'finished': True,
|
99
|
|
- }
|
100
|
|
- except KeyError: # TODO: Specialize ? No MoveIntention
|
101
|
|
- return None
|
102
|
|
-
|
103
|
|
-
|
104
|
|
-class FinishMoveEvent(Event):
|
105
|
|
- def __init__(
|
106
|
|
- self,
|
107
|
|
- subject_id: int,
|
108
|
|
- from_position: typing.Tuple[int, int],
|
109
|
|
- to_position: typing.Tuple[int, int],
|
110
|
|
- gui_action: typing.Any,
|
111
|
|
- move_duration: float=0.0,
|
112
|
|
- *args,
|
113
|
|
- **kwargs
|
114
|
|
- ):
|
115
|
|
- super().__init__(*args, **kwargs)
|
116
|
|
- self.subject_id = subject_id
|
117
|
|
- self.from_position = from_position
|
118
|
|
- self.to_position = to_position
|
119
|
|
- self.gui_action = gui_action
|
120
|
|
- self.move_duration = move_duration
|
121
|
|
-
|
122
|
|
- def repr_debug(self) -> str:
|
123
|
|
- return '{}: subject_id:{}, from_position:{} to_position: {}'.format(
|
124
|
|
- type(self).__name__,
|
125
|
|
- self.subject_id,
|
126
|
|
- self.from_position,
|
127
|
|
- self.to_position,
|
128
|
|
- )
|
129
|
|
-
|
130
|
|
-
|
131
|
|
-class StartMoveEvent(Event):
|
132
|
|
- def __init__(
|
133
|
|
- self,
|
134
|
|
- subject_id: int,
|
135
|
|
- from_position: typing.Tuple[int, int],
|
136
|
|
- to_position: typing.Tuple[int, int],
|
137
|
|
- gui_action: typing.Any,
|
138
|
|
- move_duration: float=0.0,
|
139
|
|
- *args,
|
140
|
|
- **kwargs
|
141
|
|
- ):
|
142
|
|
- super().__init__(*args, **kwargs)
|
143
|
|
- self.subject_id = subject_id
|
144
|
|
- self.from_position = from_position
|
145
|
|
- self.to_position = to_position
|
146
|
|
- self.gui_action = gui_action
|
147
|
|
- self.move_duration = move_duration
|
148
|
|
-
|
149
|
|
- def repr_debug(self) -> str:
|
150
|
|
- return '{}: subject_id:{}, from_position:{} to_position: {}'.format(
|
151
|
|
- type(self).__name__,
|
152
|
|
- self.subject_id,
|
153
|
|
- self.from_position,
|
154
|
|
- self.to_position,
|
155
|
|
- )
|
156
|
|
-
|
157
|
|
-
|
158
|
|
-class MoveToBehaviour(SubjectBehaviour):
|
159
|
|
- move_to_mechanism = MoveToMechanism
|
160
|
|
- use = [move_to_mechanism]
|
161
|
|
-
|
162
|
|
- def run(self, data):
|
163
|
|
- move_to_data = data[self.move_to_mechanism]
|
164
|
|
- if move_to_data:
|
165
|
|
- if move_to_data.get('finished'):
|
166
|
|
- return move_to_data
|
167
|
|
-
|
168
|
|
- if self._can_move_to_next_step(move_to_data):
|
169
|
|
- move_to_data['reach_next'] = True
|
170
|
|
- return move_to_data
|
171
|
|
-
|
172
|
|
- if self._is_fresh_new_step(move_to_data):
|
173
|
|
- move_to_data['reach_next'] = False
|
174
|
|
- return move_to_data
|
175
|
|
-
|
176
|
|
- return False
|
177
|
|
-
|
178
|
|
- def _can_move_to_next_step(self, move_to_data: dict) -> bool:
|
179
|
|
- raise NotImplementedError()
|
180
|
|
-
|
181
|
|
- def _is_fresh_new_step(self, move_to_data: dict) -> bool:
|
182
|
|
- return move_to_data['just_reach'] or move_to_data['initial']
|
183
|
|
-
|
184
|
|
- def finalize_event(self, move_to_data: dict, event: Event) -> None:
|
185
|
|
- pass
|
186
|
|
-
|
187
|
|
- def action(self, data) -> [Event]:
|
188
|
|
- # TODO: MoveToIntention doit être configurable
|
189
|
|
- try:
|
190
|
|
- if data.get('finished'):
|
191
|
|
- self.subject.intentions.remove(MoveToIntention)
|
192
|
|
- return []
|
193
|
|
- move = self.subject.intentions.get(MoveToIntention)
|
194
|
|
- except KeyError: # TODO: Specialize exception
|
195
|
|
- # Action don't exist anymore
|
196
|
|
- return []
|
197
|
|
-
|
198
|
|
- move = typing.cast(MoveToIntention, move)
|
199
|
|
- new_path = data['new_path']
|
200
|
|
- if new_path:
|
201
|
|
- move.path = new_path
|
202
|
|
- move.path_progression = -1
|
203
|
|
-
|
204
|
|
- previous_position = self.subject.position
|
205
|
|
- new_position = move.path[move.path_progression + 1]
|
206
|
|
-
|
207
|
|
- if data['reach_next']:
|
208
|
|
- # TODO: fin de path
|
209
|
|
- move.path_progression += 1
|
210
|
|
- self.subject.position = new_position
|
211
|
|
- move.last_intention_time = time.time()
|
212
|
|
- move.just_reach = True
|
213
|
|
- event = FinishMoveEvent(
|
214
|
|
- self.subject.id,
|
215
|
|
- previous_position,
|
216
|
|
- new_position,
|
217
|
|
- gui_action=move.gui_action,
|
218
|
|
- )
|
219
|
|
- else:
|
220
|
|
- move.just_reach = False
|
221
|
|
- event = StartMoveEvent(
|
222
|
|
- self.subject.id,
|
223
|
|
- previous_position,
|
224
|
|
- new_position,
|
225
|
|
- gui_action=move.gui_action,
|
226
|
|
- )
|
227
|
|
-
|
228
|
|
- move.initial = False
|
229
|
|
- # Note: Need to explicitly set to update shared data
|
230
|
|
- self.subject.intentions.set(move)
|
231
|
|
- self.finalize_event(data, event)
|
232
|
|
-
|
233
|
|
- return [event]
|