run.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, MoveTo
  25. from synergine2.config import Config
  26. from synergine2.log import get_default_logger
  27. from sandbox.engulf.subject import Cell, Grass
  28. from sandbox.engulf.const import COLLECTION_GRASS
  29. from synergine2.core import Core
  30. from synergine2.cycle import CycleManager
  31. from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
  32. from synergine2.xyz import Simulation
  33. from sandbox.engulf.simulation import EngulfSubjects
  34. from synergine2.xyz_utils import get_around_positions_of, get_distance_between_points
  35. class Engulf(Simulation):
  36. behaviours_classes = [
  37. GrassSpawnBehaviour,
  38. ]
  39. class GameTerminal(Terminal):
  40. subscribed_events = [
  41. GrassGrownUp,
  42. GrassSpawn,
  43. MoveTo,
  44. ]
  45. def __init__(self, *args, **kwargs):
  46. super().__init__(*args, **kwargs)
  47. self.gui = None
  48. self.asynchronous = False # TODO: config
  49. def receive(self, package: TerminalPackage):
  50. self.gui.before_received(package)
  51. super().receive(package)
  52. self.gui.after_received(package)
  53. def run(self):
  54. from sandbox.engulf import gui
  55. self.gui = gui.Game(self.config, self.logger, self)
  56. self.gui.run()
  57. def fill_with_random_cells(
  58. subjects: EngulfSubjects,
  59. count: int,
  60. start_position: tuple,
  61. end_position: tuple,
  62. ) -> None:
  63. cells = []
  64. while len(cells) < count:
  65. position = (
  66. randint(start_position[0], end_position[0]+1),
  67. randint(start_position[1], end_position[1]+1),
  68. randint(start_position[2], end_position[2]+1),
  69. )
  70. if position not in subjects.cell_xyz:
  71. cell = Cell(
  72. simulation=subjects.simulation,
  73. position=position,
  74. )
  75. cells.append(cell)
  76. subjects.append(cell)
  77. def fill_with_random_grass(
  78. subjects: EngulfSubjects,
  79. start_count: int,
  80. start_position: tuple,
  81. end_position: tuple,
  82. density: int=5,
  83. ) -> None:
  84. grasses = []
  85. while len(grasses) < start_count:
  86. position = (
  87. randint(start_position[0], end_position[0]+1),
  88. randint(start_position[1], end_position[1]+1),
  89. randint(start_position[2], end_position[2]+1),
  90. )
  91. if position not in subjects.grass_xyz:
  92. grass = Grass(
  93. simulation=subjects.simulation,
  94. position=position,
  95. )
  96. grasses.append(grass)
  97. subjects.append(grass)
  98. for grass in grasses:
  99. for around in get_around_positions_of(grass.position, distance=density):
  100. if around not in subjects.grass_xyz:
  101. new_grass = Grass(
  102. simulation=subjects.simulation,
  103. position=around,
  104. )
  105. distance = get_distance_between_points(around, grass.position)
  106. new_grass.density = 100 - round((distance * 100) / 7)
  107. subjects.append(new_grass)
  108. def main():
  109. seed(0)
  110. simulation = Engulf()
  111. subjects = EngulfSubjects(simulation=simulation)
  112. fill_with_random_cells(
  113. subjects,
  114. 30,
  115. (-34, -34, 0),
  116. (34, 34, 0),
  117. )
  118. fill_with_random_grass(
  119. subjects,
  120. 5,
  121. (-34, -34, 0),
  122. (34, 34, 0),
  123. )
  124. simulation.subjects = subjects
  125. config = Config()
  126. config.load_files(['sandbox/engulf/config.yaml'])
  127. logger = get_default_logger(level=logging.DEBUG)
  128. core = Core(
  129. config=config,
  130. logger=logger,
  131. simulation=simulation,
  132. cycle_manager=CycleManager(
  133. config=config,
  134. logger=logger,
  135. simulation=simulation,
  136. ),
  137. terminal_manager=TerminalManager(
  138. config=config,
  139. logger=logger,
  140. terminals=[GameTerminal(
  141. config,
  142. logger,
  143. asynchronous=False,
  144. )]
  145. ),
  146. cycles_per_seconds=1/config.core.cycle_duration,
  147. )
  148. core.run()
  149. if __name__ == '__main__':
  150. main()