run.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # coding: utf-8
  2. import os
  3. import sys
  4. import logging
  5. from synergine2.config import Config
  6. from synergine2.log import get_default_logger
  7. synergine2_ath = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
  8. sys.path.append(synergine2_ath)
  9. import collections
  10. from sandbox.life_game.simulation import Cell, LotOfCellsSignalBehaviour, 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 Simulation
  19. from synergine2.simulation import Event
  20. from synergine2.terminals import Terminal
  21. from synergine2.terminals import TerminalPackage
  22. from synergine2.terminals import TerminalManager
  23. from synergine2.xyz_utils import get_str_representation_from_positions
  24. class SimplePrintTerminal(Terminal):
  25. subscribed_events = [
  26. CellDieEvent,
  27. CellBornEvent,
  28. ]
  29. def __init__(self, *args, **kwargs):
  30. super().__init__(*args, **kwargs)
  31. self._cycle_born_count = 0
  32. self._cycle_die_count = 0
  33. self.register_event_handler(CellDieEvent, self.record_die)
  34. self.register_event_handler(CellBornEvent, self.record_born)
  35. def record_die(self, event: Event):
  36. self._cycle_die_count += 1
  37. def record_born(self, event: Event):
  38. self._cycle_born_count += 1
  39. def receive(self, package: TerminalPackage):
  40. self._cycle_born_count = 0
  41. self._cycle_die_count = 0
  42. super().receive(package)
  43. self.print_str_representation()
  44. def print_str_representation(self):
  45. items_positions = collections.defaultdict(list)
  46. for subject in self.subjects.values():
  47. if type(subject) == Cell:
  48. items_positions['1'].append(subject.position)
  49. if type(subject) == Empty:
  50. items_positions['0'].append(subject.position)
  51. print(get_str_representation_from_positions(
  52. items_positions,
  53. separator=' ',
  54. force_items_as=(('0', ' '),),
  55. # force_positions_as=(
  56. # ((-3, -10, 0), 'V'),
  57. # ((-2, -9, 0), 'X'),
  58. # )
  59. ))
  60. # Display current cycle events
  61. print('This cycle: {0} born, {1} dead'.format(
  62. self._cycle_born_count,
  63. self._cycle_die_count,
  64. ))
  65. print()
  66. class CocosTerminal(Terminal):
  67. subscribed_events = [
  68. CellDieEvent,
  69. CellBornEvent,
  70. EmptyPositionWithLotOfCellAroundEvent,
  71. ]
  72. def __init__(self, *args, **kwargs):
  73. super().__init__(*args, **kwargs)
  74. self.subjects = None
  75. self.gui = None
  76. def receive(self, package: TerminalPackage):
  77. self.gui.before_received(package)
  78. super().receive(package)
  79. self.gui.after_received(package)
  80. def run(self):
  81. from sandbox.life_game import gui
  82. self.gui = gui.LifeGameGui(self.config, self.logger, self)
  83. self.gui.run()
  84. def main():
  85. start_str_representation = """
  86. 0 0 0 0 0 0 0 0 0 0 0 0 0
  87. 0 0 0 1 1 1 1 0 0 0 0 0 0
  88. 0 0 0 1 0 0 1 0 0 0 0 0 0
  89. 0 1 1 1 0 0 1 1 1 0 0 0 0
  90. 0 1 0 0 0 0 0 0 1 0 0 0 0
  91. 0 1 0 0 0 0 0 0 1 0 0 0 0
  92. 0 1 1 1 0 0 1 1 1 0 0 0 0
  93. 0 0 0 1 0 0 1 0 0 0 0 0 0
  94. 0 0 0 1 1 1 1 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 0 0 0 0 0 0 0 0
  100. 0 0 0 0 0 1 1 1 0 0 0 0 0
  101. 0 0 0 0 0 0 0 1 0 0 0 0 0
  102. 0 0 0 0 0 0 1 0 0 0 0 0 0
  103. 0 0 0 0 0 0 0 0 0 0 0 0 0
  104. 0 0 0 0 0 0 0 0 0 0 0 0 0
  105. """
  106. simulation = LifeGame()
  107. subjects = get_subjects_from_str_representation(
  108. start_str_representation,
  109. simulation,
  110. )
  111. simulation.subjects = subjects
  112. config = Config()
  113. logger = get_default_logger(level=logging.DEBUG)
  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()