physics.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # coding: utf-8
  2. import typing
  3. from dijkstar import Graph
  4. from dijkstar import find_path
  5. from synergine2.config import Config
  6. from synergine2.share import shared
  7. from synergine2_xyz.map import TMXMap, XYZTile
  8. from synergine2_xyz.subjects import XYZSubject
  9. from synergine2_xyz.utils import get_line_xy_path
  10. from synergine2_xyz.xyz import get_neighbor_positions
  11. class MoveCostComputer(object):
  12. def __init__(
  13. self,
  14. config: Config,
  15. ) -> None:
  16. self.config = config
  17. def for_subject(self, subject: XYZSubject):
  18. # TODO: Verifier ce que sont les parametres pour les nommer correctement
  19. def move_cost_func(previous_node: str, next_node: str, tile: XYZTile, unknown):
  20. return self.compute_move_cost(subject, tile, previous_node, next_node, unknown)
  21. return move_cost_func
  22. def compute_move_cost(
  23. self,
  24. subject: XYZSubject,
  25. tile: XYZTile,
  26. previous_node: str,
  27. next_node: str,
  28. unknown,
  29. ) -> float:
  30. return 1.0
  31. class VisibilityMatrix(object):
  32. _matrixes = shared.create('matrixes', value=lambda: {}) # type: typing.Dict[str, typing.Dict[float, typing.List[typing.List[float]]]] # nopep8
  33. def initialize_empty_matrix(self, name: str, height: float, matrix_width: int, matrix_height: int) -> None:
  34. self._matrixes[name] = {}
  35. self._matrixes[name][height] = []
  36. for y in range(matrix_height):
  37. x_list = []
  38. for x in range(matrix_width):
  39. x_list.append(0.0)
  40. self._matrixes[name][height].append(x_list)
  41. def get_matrix(self, name: str, height: float) -> typing.List[typing.List[float]]:
  42. return self._matrixes[name][height]
  43. def update_matrix(self, name: str, height: float, x: int, y: int, value: float) -> None:
  44. matrix = self.get_matrix(name, height)
  45. matrix[y][x] = value
  46. # TODO: Test if working and needed ? This is not perf friendly ...
  47. # Force shared data update
  48. self._matrixes = dict(self._matrixes)
  49. def get_path_positions(
  50. self,
  51. from_: typing.Tuple[int, int],
  52. to: typing.Tuple[int, int],
  53. ) -> typing.List[typing.Tuple[int, int]]:
  54. return get_line_xy_path(from_, to)
  55. def get_values_for_path(self, name: str, height: float, path_positions: typing.List[typing.Tuple[int, int]]):
  56. values = []
  57. matrix = self.get_matrix(name, height)
  58. for path_position in path_positions:
  59. x, y = path_position
  60. values.append(matrix[y][x])
  61. return values
  62. class Physics(object):
  63. visibility_matrix = VisibilityMatrix
  64. move_cost_computer_class = MoveCostComputer
  65. def __init__(
  66. self,
  67. config: Config,
  68. ) -> None:
  69. self.config = config
  70. self.graph = Graph() # Graph of possible movements for dijkstar algorithm lib
  71. self.visibility_matrix = self.visibility_matrix()
  72. self.move_cost_computer = self.move_cost_computer_class(config)
  73. def load(self) -> None:
  74. pass
  75. def position_to_key(self, position: typing.Tuple[int, int]) -> str:
  76. return '{}.{}'.format(*position)
  77. def found_path(
  78. self,
  79. start: typing.Tuple[int, int],
  80. end: typing.Tuple[int, int],
  81. subject: XYZSubject,
  82. ) -> typing.List[typing.Tuple[int, int]]:
  83. start_key = self.position_to_key(start)
  84. end_key = self.position_to_key(end)
  85. found_path = find_path(self.graph, start_key, end_key, cost_func=self.move_cost_computer.for_subject(subject))
  86. regular_path = []
  87. for position in found_path[0][1:]:
  88. x, y = map(int, position.split('.'))
  89. regular_path.append((x, y))
  90. return regular_path
  91. class TMXPhysics(Physics):
  92. tmx_map_class = TMXMap
  93. def __init__(
  94. self,
  95. config: Config,
  96. map_file_path: str,
  97. ) -> None:
  98. super().__init__(config)
  99. self.map_file_path = map_file_path
  100. self.tmx_map = self.tmx_map_class(map_file_path)
  101. def load(self) -> None:
  102. self.load_graph_from_map(self.map_file_path)
  103. def load_graph_from_map(self, map_file_path: str) -> None:
  104. # TODO: tmx_map contient tout en cache, faire le dessous en exploitant tmx_map.
  105. for y in range(self.tmx_map.height):
  106. for x in range(self.tmx_map.width):
  107. position = self.position_to_key((x, y))
  108. for neighbor_position in get_neighbor_positions((x, y)):
  109. neighbor = '{}.{}'.format(*neighbor_position)
  110. neighbor_x, neighbor_y = neighbor_position
  111. if neighbor_x > self.tmx_map.width-1 or neighbor_x < 0:
  112. continue
  113. if neighbor_y > self.tmx_map.height-1 or neighbor_y < 0:
  114. continue
  115. # Note: movement consider future tile properties
  116. to_tile = self.tmx_map.layer_tiles('terrain')[neighbor]
  117. # Note: Voir https://pypi.python.org/pypi/Dijkstar/2.2
  118. self.graph.add_edge(position, neighbor, to_tile)