run.py 4.8KB

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