visualisation.py 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import pygame
  2. from intelligine.display.map import get_map_connector
  3. from synergine_xyz.display.PygameImageRotate import PygameImageRotate
  4. from synergine_xyz.display.PygameVisualisation import PygameVisualisation
  5. from synergine_xyz.display.object.pygame.PygameImage import PygameImage
  6. from intelligine.synergy.object.ant.Ant import Ant
  7. from intelligine.synergy.object.ant.Egg import Egg
  8. from os import getcwd
  9. from synergine_xyz.cst import PREVIOUS_DIRECTION
  10. SURFACE_PHEROMONE_HOME = 'molecule_home'
  11. SURFACE_PHEROMONE_EXPLORATION = 'molecule_exploration'
  12. SURFACE_SMELL_EGG = 'smell_egg'
  13. SURFACE_SMELL_FOOD = 'smell_food'
  14. # TODO: Analyser les procedes ici pour proposer des outils dans le framework
  15. # ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  16. # food = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/food.png')
  17. # hole = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/hole.png')
  18. # dead_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_ant.png')
  19. # red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/red_ant.png')
  20. # green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/green_ant.png')
  21. # blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/blue_ant.png')
  22. # dead_red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_red_ant.png')
  23. # dead_green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_green_ant.png')
  24. # dead_blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_blue_ant.png')
  25. # bug = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  26. # rock = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/rock.png')
  27. egg = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg.png')
  28. eggc2 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c2.png')
  29. eggc3 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c3.png')
  30. eggc4 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c4.png')
  31. eggc5 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c5.png')
  32. eggc7 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c7.png')
  33. phee = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/phee.png')
  34. pheh = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/pheh.png')
  35. smell = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/smell.png')
  36. #
  37. # directions_ant = DirectionnedImage(ant)
  38. # directions_red_ant = DirectionnedImage(red_ant)
  39. # directions_blue_ant = DirectionnedImage(blue_ant)
  40. # directions_green_ant = DirectionnedImage(green_ant)
  41. #
  42. # def bug_direction(bug, context):
  43. # if bug.get_life_points() <= 0:
  44. # return dead_ant
  45. # try:
  46. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  47. # except KeyError:
  48. # previous_direction = 14
  49. # return directions_ant.get_for_direction(previous_direction)
  50. #
  51. # def red_ant_direction(bug, context):
  52. # if bug.get_life_points() <= 0:
  53. # return dead_red_ant
  54. # try:
  55. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  56. # except KeyError:
  57. # previous_direction = 14
  58. # return directions_red_ant.get_for_direction(previous_direction)
  59. #
  60. # def blue_ant_direction(bug, context):
  61. # if bug.get_life_points() <= 0:
  62. # return dead_blue_ant
  63. # try:
  64. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  65. # except KeyError:
  66. # previous_direction = 14
  67. # return directions_blue_ant.get_for_direction(previous_direction)
  68. #
  69. # def green_ant_direction(bug, context):
  70. # if bug.get_life_points() <= 0:
  71. # return dead_green_ant
  72. # try:
  73. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  74. # except KeyError:
  75. # previous_direction = 14
  76. # return directions_green_ant.get_for_direction(previous_direction)
  77. #
  78. #
  79. def for_position(position, objects, context):
  80. # TODO: DEV TMP: refact, etc
  81. eggs = []
  82. for obj in objects:
  83. if isinstance(obj, Egg):
  84. eggs.append(obj)
  85. if len(eggs) == 2:
  86. return (eggc2, eggs)
  87. if len(eggs) == 3:
  88. return (eggc3, eggs)
  89. if len(eggs) == 4:
  90. return (eggc4, eggs)
  91. if len(eggs) > 4:
  92. return (eggc7, eggs)
  93. return (None, [])
  94. #
  95. # visualisation = {
  96. # 'window': {},
  97. # 'callbacks': {
  98. # 'position': for_position
  99. # },
  100. # 'surfaces': {
  101. # SURFACE_PHEROMONE_EXPLORATION: {
  102. # 'default': phee,
  103. # 'callbacks': []
  104. # },
  105. # SURFACE_PHEROMONE_HOME: {
  106. # 'default': pheh,
  107. # 'callbacks': []
  108. # },
  109. # },
  110. # 'objects': {
  111. # RedAnt: {
  112. # 'default': red_ant,
  113. # 'callbacks': [red_ant_direction]
  114. # },
  115. # BlueAnt: {
  116. # 'default': blue_ant,
  117. # 'callbacks': [blue_ant_direction]
  118. # },
  119. # GreenAnt: {
  120. # 'default': green_ant,
  121. # 'callbacks': [green_ant_direction]
  122. # },
  123. # Ant: {
  124. # 'default': ant,
  125. # 'callbacks': [bug_direction]
  126. # },
  127. # Bug: {
  128. # 'default': bug,
  129. # 'callbacks': [bug_direction]
  130. # },
  131. # Egg: {
  132. # 'default': egg
  133. # },
  134. # Rock: {
  135. # 'default': rock
  136. # },
  137. # Food: {
  138. # 'default': food
  139. # },
  140. # Hole: {
  141. # 'default': hole
  142. # }
  143. # }
  144. # }
  145. #############################
  146. # Behind, new
  147. #############################
  148. image_rotate = PygameImageRotate()
  149. def dead_ant_callback_container(map_connector, production_class):
  150. tile_set_id = map_connector.get_dynamic_classes().get_production_class_collection_id(production_class)
  151. pil_image = map_connector.extract_image_with_class(tile_set_id, 'dead_ant')
  152. image_bytes = pil_image.tobytes()
  153. pygame_surface = pygame.image.fromstring(image_bytes, pil_image.size, pil_image.mode)
  154. pygame_image = PygameImage(pygame_surface)
  155. def dead_ant_callback(obj, context):
  156. # TODO: obj dans le col ALIVE truc comme ça
  157. if obj.get_life_points() <= 0:
  158. return pygame_image
  159. # TODO: raise, ou dans objectVisual.get_visual tests si bool plutot que is
  160. return False
  161. return dead_ant_callback
  162. def ant_direction_modifier(obj, context, visual):
  163. try:
  164. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, obj.get_id())
  165. except KeyError:
  166. previous_direction = 14
  167. return image_rotate.get_for_direction(visual, previous_direction)
  168. def get_standard_extract_from_map(map_file_path, map_config):
  169. map_connector = get_map_connector(map_file_path, map_config)
  170. visualisation = PygameVisualisation.get_default_visualisation()
  171. simulations = map_connector.create_simulations()
  172. visualizer = PygameVisualisation(visualisation)
  173. objects_images = map_connector.extract_objects_images()
  174. visualizer.update_objects_images(objects_images)
  175. map_connector.add_object_callback_to_visualisation(visualizer, [Ant], dead_ant_callback_container)
  176. ant_production_classes = map_connector.get_dynamic_classes().get_production_classes(Ant)
  177. for ant_production_class in ant_production_classes:
  178. visualizer.add_modifier(ant_production_class, ant_direction_modifier)
  179. visualisation.update({
  180. 'callbacks': {
  181. 'position': for_position
  182. },
  183. 'surfaces': {
  184. SURFACE_PHEROMONE_EXPLORATION: {
  185. 'default': phee,
  186. 'callbacks': []
  187. },
  188. SURFACE_PHEROMONE_HOME: {
  189. 'default': pheh,
  190. 'callbacks': []
  191. },
  192. SURFACE_SMELL_EGG: {
  193. 'default': smell,
  194. 'callbacks': []
  195. },
  196. SURFACE_SMELL_FOOD: {
  197. 'default': smell,
  198. 'callbacks': []
  199. },
  200. }
  201. })
  202. return simulations, visualisation