123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
-
- import typing
-
- from dijkstar import find_path
-
- from synergine2.config import Config
- from synergine2.simulation import SimulationBehaviour
- from synergine2.simulation import SubjectBehaviour
- from synergine2.simulation import SubjectMechanism
- from synergine2.simulation import Intention
- from synergine2.simulation import Simulation
- from synergine2.simulation import Event
- from synergine2_xyz.simulation import XYZSimulation
-
-
- class MoveToIntention(Intention):
- def __init__(self, move_to: typing.Tuple[int, int]) -> None:
- self.move_to = move_to
- self.path = []
- self.path_progression = -1
-
-
- class RequestMoveBehaviour(SimulationBehaviour):
- move_intention_class = MoveToIntention
-
- @classmethod
- def merge_data(cls, new_data, start_data=None):
-
- pass
-
- def __init__(
- self,
- config: Config,
- simulation: Simulation,
- ):
- super().__init__(config, simulation)
- self.simulation = typing.cast(XYZSimulation, self.simulation)
-
- def run(self, data):
-
- pass
-
- def action(self, data) -> typing.List[Event]:
- subject_id = data['subject_id']
- move_to = data['move_to']
-
- try:
- subject = self.simulation.subjects.index[subject_id]
- subject.intentions.set(self.move_intention_class(move_to))
- except KeyError:
-
- pass
-
- return []
-
-
- class MoveToMechanism(SubjectMechanism):
- def run(self):
-
-
-
- try:
-
- move = self.subject.intentions.get(MoveToIntention)
- move = typing.cast(MoveToIntention, move)
- new_path = None
-
- if not move.path:
-
- start = '{}.{}'.format(*self.subject.position)
- end = '{}.{}'.format(*move.move_to)
-
- found_path = find_path(self.simulation.graph, start, end)
- move.path = []
-
- for position in found_path[0]:
- x, y = map(int, position.split('.'))
- move.path.append((x, y))
-
-
- new_path = move.path
-
-
-
-
-
-
-
-
-
- next_move = move.path[move.path_progression + 1]
-
- if not self.simulation.is_possible_position(next_move):
-
- new_path = ['...']
-
- return {
- 'new_path': new_path,
- }
-
- except IndexError:
- return None
- except KeyError:
- return None
-
-
- class MoveEvent(Event):
- def __init__(self, subject_id: int, position: tuple, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.subject_id = subject_id
- self.position = position
-
- def repr_debug(self) -> str:
- return '{}: subject_id:{}, position:{}'.format(
- type(self).__name__,
- self.subject_id,
- self.position,
- )
-
-
- class MoveToBehaviour(SubjectBehaviour):
- use = [MoveToMechanism]
- move_to_mechanism = MoveToMechanism
-
- def run(self, data):
-
-
- move_to_data = data[self.move_to_mechanism]
- if move_to_data:
- return move_to_data
- return False
-
- def action(self, data) -> [Event]:
-
- new_path = data['new_path']
- try:
-
- move = self.subject.intentions.get(MoveToIntention)
- move = typing.cast(MoveToIntention, move)
-
- if new_path:
- move.path = new_path
- move.path_progression = -1
-
-
-
-
- move.path_progression += 1
- new_position = move.path[move.path_progression]
- self.subject.position = new_position
-
- return [MoveEvent(self.subject.id, new_position)]
-
- except KeyError:
-
- pass
|