run.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """
  2. Engulf is simulation containing:
  3. * Subjects who need to:
  4. * eat
  5. * low energy ground stuff
  6. * other alive subjects
  7. * other dead subjects
  8. * sleep
  9. * want to be not alone
  10. * with non aggressive subjects
  11. * want to be alone
  12. * reproduce
  13. * with non aggressive subjects
  14. * and transmit tendencies because their cultures can be
  15. * eat: - eat background stuff, + eat subjects
  16. * alone/not alone: - be alone + not alone
  17. """
  18. import os
  19. import sys
  20. import logging
  21. synergine2_ath = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
  22. sys.path.append(synergine2_ath)
  23. from random import randint, seed
  24. from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn, GrassSpawnBehaviour
  25. from synergine2.config import Config
  26. from synergine2.log import get_default_logger
  27. from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
  28. from synergine2.core import Core
  29. from synergine2.cycle import CycleManager
  30. from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
  31. from synergine2.xyz import Simulation
  32. from sandbox.engulf.simulation import EngulfSubjects
  33. from synergine2.xyz_utils import get_around_positions_of, get_distance_between_points
  34. class Engulf(Simulation):
  35. behaviours_classes = [
  36. GrassSpawnBehaviour,
  37. ]
  38. class GameTerminal(Terminal):
  39. subscribed_events = [
  40. GrassGrownUp,
  41. GrassSpawn,
  42. ]
  43. def __init__(self, *args, **kwargs):
  44. super().__init__(*args, **kwargs)
  45. self.gui = None
  46. def receive(self, package: TerminalPackage):
  47. self.gui.before_received(package)
  48. super().receive(package)
  49. self.gui.after_received(package)
  50. def run(self):
  51. from sandbox.engulf import gui
  52. self.gui = gui.Game(self)
  53. self.gui.run()
  54. def fill_with_random_cells(
  55. subjects: EngulfSubjects,
  56. count: int,
  57. start_position: tuple,
  58. end_position: tuple,
  59. ) -> None:
  60. cells = []
  61. while len(cells) < count:
  62. position = (
  63. randint(start_position[0], end_position[0]+1),
  64. randint(start_position[1], end_position[1]+1),
  65. randint(start_position[2], end_position[2]+1),
  66. )
  67. if position not in subjects.cell_xyz:
  68. cell = Cell(
  69. simulation=subjects.simulation,
  70. position=position,
  71. )
  72. cells.append(cell)
  73. subjects.append(cell)
  74. def fill_with_random_grass(
  75. subjects: EngulfSubjects,
  76. start_count: int,
  77. start_position: tuple,
  78. end_position: tuple,
  79. density: int=5,
  80. ) -> None:
  81. grasses = []
  82. while len(grasses) < start_count:
  83. position = (
  84. randint(start_position[0], end_position[0]+1),
  85. randint(start_position[1], end_position[1]+1),
  86. randint(start_position[2], end_position[2]+1),
  87. )
  88. if position not in subjects.grass_xyz:
  89. grass = Grass(
  90. simulation=subjects.simulation,
  91. position=position,
  92. )
  93. grasses.append(grass)
  94. subjects.append(grass)
  95. for grass in grasses:
  96. for around in get_around_positions_of(grass.position, distance=density):
  97. if around not in subjects.grass_xyz:
  98. new_grass = Grass(
  99. simulation=subjects.simulation,
  100. position=around,
  101. )
  102. distance = get_distance_between_points(around, grass.position)
  103. new_grass.density = 100 - round((distance * 100) / 7)
  104. subjects.append(new_grass)
  105. def main():
  106. seed(0)
  107. simulation = Engulf()
  108. subjects = EngulfSubjects(simulation=simulation)
  109. fill_with_random_cells(
  110. subjects,
  111. 30,
  112. (-34, -34, 0),
  113. (34, 34, 0),
  114. )
  115. fill_with_random_grass(
  116. subjects,
  117. 5,
  118. (-34, -34, 0),
  119. (34, 34, 0),
  120. )
  121. simulation.subjects = subjects
  122. config = Config()
  123. logger = get_default_logger(level=logging.DEBUG)
  124. logger.debug('HELLO')
  125. core = Core(
  126. config=config,
  127. logger=logger,
  128. simulation=simulation,
  129. cycle_manager=CycleManager(
  130. config=config,
  131. logger=logger,
  132. simulation=simulation,
  133. ),
  134. terminal_manager=TerminalManager(
  135. config=config,
  136. logger=logger,
  137. terminals=[GameTerminal(
  138. asynchronous=False,
  139. )]
  140. ),
  141. cycles_per_seconds=1,
  142. )
  143. core.run()
  144. if __name__ == '__main__':
  145. main()