run.py 3.9KB

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