physics.py 6.6KB

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