physics.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.tmx_utils import fill_matrix
  10. from synergine2_xyz.utils import get_line_xy_path
  11. from synergine2_xyz.xyz import get_neighbor_positions
  12. class MoveCostComputer(object):
  13. def __init__(
  14. self,
  15. config: Config,
  16. ) -> None:
  17. self.config = config
  18. def for_subject(self, subject: XYZSubject):
  19. # TODO: Verifier ce que sont les parametres pour les nommer correctement
  20. def move_cost_func(previous_node: str, next_node: str, tile: XYZTile, unknown):
  21. return self.compute_move_cost(subject, tile, previous_node, next_node, unknown)
  22. return move_cost_func
  23. def compute_move_cost(
  24. self,
  25. subject: XYZSubject,
  26. tile: XYZTile,
  27. previous_node: str,
  28. next_node: str,
  29. unknown,
  30. ) -> float:
  31. return 1.0
  32. class Matrixes(object):
  33. _matrixes = shared.create('matrixes', value=lambda: {}) # type: typing.Dict[str, typing.List[typing.List[tuple]]]
  34. _value_structures = shared.create('value_structures', value=lambda: {}) # type: typing.List[str]
  35. def initialize_empty_matrix(
  36. self,
  37. name: str,
  38. matrix_width: int,
  39. matrix_height: int,
  40. value_structure: typing.List[str],
  41. ) -> None:
  42. self._matrixes[name] = []
  43. self._value_structures[name] = value_structure
  44. for y in range(matrix_height):
  45. x_list = []
  46. for x in range(matrix_width):
  47. x_list.append(tuple([0.0] * len(value_structure)))
  48. self._matrixes[name].append(x_list)
  49. def get_matrix(self, name: str) -> typing.List[typing.List[tuple]]:
  50. return self._matrixes[name]
  51. def update_matrix(self, name: str, x: int, y: int, value: tuple) -> None:
  52. matrix = self.get_matrix(name)
  53. matrix[y][x] = value
  54. # TODO: Test if working and needed ? This is not perf friendly ...
  55. # Force shared data update
  56. self._matrixes = dict(self._matrixes)
  57. def get_path_positions(
  58. self,
  59. from_: typing.Tuple[int, int],
  60. to: typing.Tuple[int, int],
  61. ) -> typing.List[typing.Tuple[int, int]]:
  62. return get_line_xy_path(from_, to)
  63. def get_values_for_path(
  64. self,
  65. name: str,
  66. path_positions: typing.List[typing.Tuple[int, int]],
  67. value_name: str=None,
  68. ):
  69. values = []
  70. value_name_position = None
  71. if value_name:
  72. value_name_position = self._value_structures[name].index(value_name)
  73. matrix = self.get_matrix(name)
  74. for path_position in path_positions:
  75. x, y = path_position
  76. if value_name_position is None:
  77. values.append(matrix[y][x])
  78. else:
  79. values.append(matrix[y][x][value_name_position])
  80. return values
  81. def get_value(self, matrix_name: str, x: int, y: int, value_name: str) -> float:
  82. matrix = self.get_matrix(matrix_name)
  83. values = matrix[y][x]
  84. value_position = self._value_structures[matrix_name].index(value_name)
  85. return values[value_position]
  86. class Physics(object):
  87. visibility_matrix = Matrixes
  88. move_cost_computer_class = MoveCostComputer
  89. def __init__(
  90. self,
  91. config: Config,
  92. ) -> None:
  93. self.config = config
  94. self.graph = Graph() # Graph of possible movements for dijkstar algorithm lib
  95. self.visibility_matrix = self.visibility_matrix()
  96. self.move_cost_computer = self.move_cost_computer_class(config)
  97. def load(self) -> None:
  98. pass
  99. def position_to_key(self, position: typing.Tuple[int, int]) -> str:
  100. return '{}.{}'.format(*position)
  101. def found_path(
  102. self,
  103. start: typing.Tuple[int, int],
  104. end: typing.Tuple[int, int],
  105. subject: XYZSubject,
  106. ) -> typing.List[typing.Tuple[int, int]]:
  107. start_key = self.position_to_key(start)
  108. end_key = self.position_to_key(end)
  109. found_path = find_path(self.graph, start_key, end_key, cost_func=self.move_cost_computer.for_subject(subject))
  110. regular_path = []
  111. for position in found_path[0][1:]:
  112. x, y = map(int, position.split('.'))
  113. regular_path.append((x, y))
  114. return regular_path
  115. class TMXPhysics(Physics):
  116. tmx_map_class = TMXMap
  117. matrixes_configuration = None # type: typing.Dict[str, typing.List[str]]
  118. def __init__(
  119. self,
  120. config: Config,
  121. map_file_path: str,
  122. ) -> None:
  123. super().__init__(config)
  124. self.map_file_path = map_file_path
  125. self.tmx_map = self.tmx_map_class(map_file_path)
  126. self.matrixes = Matrixes()
  127. def load(self) -> None:
  128. self.load_graph_from_map(self.map_file_path)
  129. self.load_matrixes_from_map()
  130. def load_graph_from_map(self, map_file_path: str) -> None:
  131. # TODO: tmx_map contient tout en cache, faire le dessous en exploitant tmx_map.
  132. for y in range(self.tmx_map.height):
  133. for x in range(self.tmx_map.width):
  134. position = self.position_to_key((x, y))
  135. for neighbor_position in get_neighbor_positions((x, y)):
  136. neighbor = '{}.{}'.format(*neighbor_position)
  137. neighbor_x, neighbor_y = neighbor_position
  138. if neighbor_x > self.tmx_map.width-1 or neighbor_x < 0:
  139. continue
  140. if neighbor_y > self.tmx_map.height-1 or neighbor_y < 0:
  141. continue
  142. # Note: movement consider future tile properties
  143. to_tile = self.tmx_map.layer_tiles('terrain')[neighbor]
  144. # Note: Voir https://pypi.python.org/pypi/Dijkstar/2.2
  145. self.graph.add_edge(position, neighbor, to_tile)
  146. def load_matrixes_from_map(self) -> None:
  147. if not self.matrixes_configuration:
  148. return
  149. for matrix_name, properties in self.matrixes_configuration.items():
  150. self.matrixes.initialize_empty_matrix(
  151. matrix_name,
  152. matrix_width=self.tmx_map.width,
  153. matrix_height=self.tmx_map.height,
  154. value_structure=properties,
  155. )
  156. fill_matrix(self.tmx_map, self.matrixes, 'terrain', matrix_name, properties)