subject.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # coding: utf-8
  2. import typing
  3. from synergine2.simulation import SubjectBehaviourSelector
  4. from synergine2.simulation import SubjectBehaviour
  5. from opencombat.user_action import UserAction
  6. from synergine2_xyz.move.intention import MoveToIntention
  7. from opencombat.const import COLLECTION_ALIVE
  8. from opencombat.const import COMBAT_MODE_DEFEND
  9. from opencombat.simulation.base import BaseSubject
  10. from opencombat.simulation.move import MoveBehaviour
  11. from opencombat.simulation.move import MoveWithRotationBehaviour
  12. from opencombat.simulation.behaviour import EngageOpponent
  13. from opencombat.simulation.behaviour import LookAroundBehaviour
  14. from synergine2.share import shared
  15. class TileBehaviourSelector(SubjectBehaviourSelector):
  16. def reduce_behaviours(
  17. self,
  18. behaviours: typing.Dict[typing.Type[SubjectBehaviour], object],
  19. ) -> typing.Dict[typing.Type[SubjectBehaviour], object]:
  20. return behaviours
  21. class TileSubject(BaseSubject):
  22. start_collections = [
  23. COLLECTION_ALIVE,
  24. ]
  25. visible_opponent_ids = shared.create_self('visible_opponent_ids', lambda: [])
  26. combat_mode = shared.create_self('combat_mode', COMBAT_MODE_DEFEND)
  27. behaviour_selector_class = TileBehaviourSelector
  28. direction = shared.create_self('direction', 0)
  29. moving_to = shared.create_self('moving_to', (-1, -1))
  30. move_duration = shared.create_self('move_duration', -1)
  31. start_move = shared.create_self('start_move', -1)
  32. rotate_to = shared.create_self('rotate_to', -1)
  33. rotate_duration = shared.create_self('rotate_duration', -1)
  34. start_rotation = shared.create_self('start_rotation', -1)
  35. def __init__(self, *args, **kwargs):
  36. super().__init__(*args, **kwargs)
  37. self._walk_ref_time = float(self.config.resolve('game.move.walk_ref_time'))
  38. self._run_ref_time = float(self.config.resolve('game.move.run_ref_time'))
  39. self._crawl_ref_time = float(self.config.resolve('game.move.crawl_ref_time'))
  40. self._rotate_ref_time = float(self.config.resolve('game.move.rotate_ref_time'))
  41. self.direction = kwargs.get('direction', 0)
  42. @property
  43. def global_move_coeff(self) -> float:
  44. return 1
  45. @property
  46. def run_duration(self) -> float:
  47. """
  48. :return: move to tile time (s) when running
  49. """
  50. return self._run_ref_time * self.global_move_coeff
  51. @property
  52. def walk_duration(self) -> float:
  53. """
  54. :return: move to tile time (s) when walking
  55. """
  56. return self._walk_ref_time * self.global_move_coeff
  57. @property
  58. def crawl_duration(self) -> float:
  59. """
  60. :return: move to tile time (s) when crawling
  61. """
  62. return self._crawl_ref_time * self.global_move_coeff
  63. def get_rotate_duration(self, angle: float) -> float:
  64. return angle * self._rotate_ref_time
  65. def get_move_duration(self, move: MoveToIntention) -> float:
  66. gui_action = move.gui_action
  67. if gui_action == UserAction.ORDER_MOVE:
  68. return self.walk_duration
  69. if gui_action == UserAction.ORDER_MOVE_FAST:
  70. return self.run_duration
  71. if gui_action == UserAction.ORDER_MOVE_CRAWL:
  72. return self.crawl_duration
  73. raise NotImplementedError(
  74. 'Gui action {} unknown'.format(move.gui_action)
  75. )
  76. class ManSubject(TileSubject):
  77. behaviours_classes = [
  78. MoveBehaviour,
  79. LookAroundBehaviour,
  80. EngageOpponent,
  81. ] # type: typing.List[SubjectBehaviour]
  82. class TankSubject(TileSubject):
  83. behaviours_classes = [
  84. MoveWithRotationBehaviour,
  85. LookAroundBehaviour,
  86. EngageOpponent,
  87. ] # type: typing.List[SubjectBehaviour]
  88. def __init__(self, *args, **kwargs) -> None:
  89. super().__init__(*args, **kwargs)
  90. # TODO BS 2018-01-26: This coeff will be dependent of real
  91. # unit type (tiger 2, etc)
  92. self._global_move_coeff = self.config.resolve(
  93. 'game.move.subject.tank1.global_move_coeff',
  94. 3,
  95. )
  96. self._rotate_ref_time = float(self.config.resolve(
  97. 'game.move.subject.tank1.rotate_ref_time',
  98. 0.1111,
  99. ))
  100. @property
  101. def global_move_coeff(self) -> float:
  102. return self._global_move_coeff