run.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # coding: utf-8
  2. import logging
  3. import os
  4. import sys
  5. synergine2_ath = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
  6. sys.path.append(synergine2_ath)
  7. import collections
  8. from synergine2.config import Config
  9. from synergine2.log import get_default_logger
  10. from sandbox.life_game.simulation import Cell, LifeGame, \
  11. EmptyPositionWithLotOfCellAroundEvent
  12. from sandbox.life_game.simulation import Empty
  13. from sandbox.life_game.simulation import CellDieEvent
  14. from sandbox.life_game.simulation import CellBornEvent
  15. from sandbox.life_game.utils import get_subjects_from_str_representation
  16. from synergine2.core import Core
  17. from synergine2.cycle import CycleManager
  18. from synergine2.simulation import Event
  19. from synergine2.terminals import Terminal
  20. from synergine2.terminals import TerminalPackage
  21. from synergine2.terminals import TerminalManager
  22. from synergine2_xyz.utils import get_str_representation_from_positions
  23. class SimplePrintTerminal(Terminal):
  24. subscribed_events = [
  25. CellDieEvent,
  26. CellBornEvent,
  27. ]
  28. def __init__(self, *args, **kwargs):
  29. super().__init__(*args, **kwargs)
  30. self._cycle_born_count = 0
  31. self._cycle_die_count = 0
  32. self.register_event_handler(CellDieEvent, self.record_die)
  33. self.register_event_handler(CellBornEvent, self.record_born)
  34. def record_die(self, event: Event):
  35. self._cycle_die_count += 1
  36. def record_born(self, event: Event):
  37. self._cycle_born_count += 1
  38. def receive(self, package: TerminalPackage):
  39. self._cycle_born_count = 0
  40. self._cycle_die_count = 0
  41. super().receive(package)
  42. self.print_str_representation()
  43. def print_str_representation(self):
  44. items_positions = collections.defaultdict(list)
  45. for subject in self.subjects.values():
  46. if type(subject) == Cell:
  47. items_positions['1'].append(subject.position)
  48. if type(subject) == Empty:
  49. items_positions['0'].append(subject.position)
  50. print(get_str_representation_from_positions(
  51. items_positions,
  52. separator=' ',
  53. force_items_as=(('0', ' '),),
  54. # force_positions_as=(
  55. # ((-3, -10, 0), 'V'),
  56. # ((-2, -9, 0), 'X'),
  57. # )
  58. ))
  59. # Display current cycle events
  60. print('This cycle: {0} born, {1} dead'.format(
  61. self._cycle_born_count,
  62. self._cycle_die_count,
  63. ))
  64. print()
  65. class CocosTerminal(Terminal):
  66. subscribed_events = [
  67. CellDieEvent,
  68. CellBornEvent,
  69. EmptyPositionWithLotOfCellAroundEvent,
  70. ]
  71. def __init__(self, *args, **kwargs):
  72. super().__init__(*args, **kwargs)
  73. self.subjects = None
  74. self.gui = None
  75. def receive(self, package: TerminalPackage):
  76. self.gui.before_received(package)
  77. super().receive(package)
  78. self.gui.after_received(package)
  79. def run(self):
  80. from sandbox.life_game import gui
  81. self.gui = gui.LifeGameGui(self.config, self.logger, self)
  82. self.gui.run()
  83. def main():
  84. start_str_representation = """
  85. 0 0 0 0 0 0 0 0 0 0 0 0 0
  86. 0 0 0 1 1 1 1 0 0 0 0 0 0
  87. 0 0 0 1 0 0 1 0 0 0 0 0 0
  88. 0 1 1 1 0 0 1 1 1 0 0 0 0
  89. 0 1 0 0 0 0 0 0 1 0 0 0 0
  90. 0 1 0 0 0 0 0 0 1 0 0 0 0
  91. 0 1 1 1 0 0 1 1 1 0 0 0 0
  92. 0 0 0 1 0 0 1 0 0 0 0 0 0
  93. 0 0 0 1 1 1 1 0 0 0 0 0 0
  94. 0 0 0 0 0 0 0 0 0 0 0 0 0
  95. 0 0 0 0 0 0 0 0 0 0 0 0 0
  96. 0 0 0 0 0 0 0 0 0 0 0 0 0
  97. 0 0 0 0 0 0 0 0 0 0 0 0 0
  98. 0 0 0 0 0 0 0 0 0 0 0 0 0
  99. 0 0 0 0 0 1 1 1 0 0 0 0 0
  100. 0 0 0 0 0 0 0 1 0 0 0 0 0
  101. 0 0 0 0 0 0 1 0 0 0 0 0 0
  102. 0 0 0 0 0 0 0 0 0 0 0 0 0
  103. 0 0 0 0 0 0 0 0 0 0 0 0 0
  104. """
  105. config = Config()
  106. config.load_files(['sandbox/life_game/config.yaml'])
  107. logger = get_default_logger(level=logging.DEBUG)
  108. simulation = LifeGame(config)
  109. subjects = get_subjects_from_str_representation(
  110. start_str_representation,
  111. simulation,
  112. )
  113. simulation.subjects = subjects
  114. core = Core(
  115. config=config,
  116. logger=logger,
  117. simulation=simulation,
  118. cycle_manager=CycleManager(
  119. config=config,
  120. logger=logger,
  121. simulation=simulation,
  122. ),
  123. terminal_manager=TerminalManager(
  124. config=config,
  125. logger=logger,
  126. terminals=[
  127. CocosTerminal(config, logger),
  128. SimplePrintTerminal(config, logger),
  129. ]
  130. ),
  131. )
  132. core.run()
  133. if __name__ == '__main__':
  134. main()