move.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # coding: utf-8
  2. import typing
  3. from synergine2.config import Config
  4. from synergine2.simulation import SimulationBehaviour
  5. from synergine2.simulation import SubjectBehaviour
  6. from synergine2.simulation import SubjectMechanism
  7. from synergine2.simulation import Intention
  8. from synergine2.simulation import Simulation
  9. from synergine2.simulation import Event
  10. from synergine2_xyz.simulation import XYZSimulation
  11. class MoveToIntention(Intention):
  12. def __init__(self, move_to: typing.Tuple[int, int]) -> None:
  13. self.move_to = move_to
  14. self.path = [] # type: typing.List[typing.Tuple[int, int]]
  15. self.path_progression = -1 # type: int
  16. class RequestMoveBehaviour(SimulationBehaviour):
  17. move_intention_class = MoveToIntention
  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. def __init__(
  23. self,
  24. config: Config,
  25. simulation: Simulation,
  26. ):
  27. super().__init__(config, simulation)
  28. self.simulation = typing.cast(XYZSimulation, self.simulation)
  29. def run(self, data):
  30. # TODO: behaviour/Thing dedicated to Gui -> Simulation ?
  31. pass # This behaviour is designed to be launch by terminal
  32. def action(self, data) -> typing.List[Event]:
  33. subject_id = data['subject_id']
  34. move_to = data['move_to']
  35. try:
  36. subject = self.simulation.subjects.index[subject_id]
  37. subject.intentions.set(self.move_intention_class(move_to))
  38. except KeyError:
  39. # TODO: log error here
  40. pass
  41. return []
  42. class MoveToMechanism(SubjectMechanism):
  43. def run(self):
  44. # TODO: Si move to: Si nouveau: a*, si bloque, a*, sinon rien
  45. # TODO: pourquoi un mechanism plutot que dans run du behaviour ? faire en sorte que lorsque on calcule,
  46. # si un subject est déjà passé par là et qu'il va au même endroit, ne pas recalculer.
  47. try:
  48. # TODO: MoveToIntention doit être configurable
  49. move = self.subject.intentions.get(MoveToIntention)
  50. move = typing.cast(MoveToIntention, move)
  51. new_path = move.path or None
  52. if not move.path:
  53. # TODO: Fake to test
  54. new_path = []
  55. for i in range(20):
  56. new_path.append((
  57. self.subject.position[0],
  58. self.subject.position[1] + i,
  59. ))
  60. next_move = new_path[move.path_progression + 1]
  61. # TODO: fin de path
  62. if not self.simulation.is_possible_position(next_move):
  63. # TODO: refaire le path
  64. new_path = ['...']
  65. return {
  66. 'new_path': new_path,
  67. }
  68. except KeyError:
  69. return None
  70. class MoveEvent(Event):
  71. def __init__(self, subject_id: int, position: tuple, *args, **kwargs):
  72. super().__init__(*args, **kwargs)
  73. self.subject_id = subject_id
  74. self.position = position
  75. def repr_debug(self) -> str:
  76. return '{}: subject_id:{}, position:{}'.format(
  77. type(self).__name__,
  78. self.subject_id,
  79. self.position,
  80. )
  81. class MoveToBehaviour(SubjectBehaviour):
  82. use = [MoveToMechanism]
  83. move_to_mechanism = MoveToMechanism
  84. def run(self, data):
  85. # TODO: on fait vraiment rien ici ? Note: meme si il n'y a pas de new_path, l'action doit s'effectuer
  86. # du moment qu'il y a une intention de move
  87. move_to_data = data[self.move_to_mechanism]
  88. if move_to_data:
  89. return move_to_data
  90. return False
  91. def action(self, data) -> [Event]:
  92. # TODO: effectuer un move vers une nouvelle position ou faire progresser "l'entre-deux"
  93. new_path = data['new_path']
  94. try:
  95. # TODO: MoveToIntention doit être configurable
  96. move = self.subject.intentions.get(MoveToIntention)
  97. move = typing.cast(MoveToIntention, move)
  98. if new_path:
  99. move.path = new_path
  100. move.path_progression = -1
  101. # TODO: progression et lorsque "vraiment avance d'une case" envoyer le Move
  102. # pour le moment on move direct
  103. # TODO: fin de path
  104. move.path_progression += 1 # BUG: ca progresse pas ?
  105. new_position = move.path[move.path_progression]
  106. self.subject.position = new_position
  107. return [MoveEvent(self.subject.id, new_position)]
  108. except KeyError:
  109. # TODO: log ? Il devrait y avoir un move puisque data du run/mechanism !
  110. pass