behaviour.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
  2. from synergine2.simulation import Event
  3. from synergine2.utils import ChunkManager
  4. from synergine2.xyz_utils import get_around_positions_of_positions
  5. class GrassGrownUp(Event):
  6. def __init__(self, subject_id, density, *args, **kwargs):
  7. super().__init__(*args, **kwargs)
  8. self.subject_id = subject_id
  9. self.density = density
  10. class GrassSpawn(Event):
  11. def __init__(self, subject_id, position, density, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. self.subject_id = subject_id
  14. self.position = position
  15. self.density = density
  16. class GrassSpotablePositionsMechanism(SimulationMechanism):
  17. parallelizable = True
  18. def run(self, process_number: int=None, process_count: int=None):
  19. chunk_manager = ChunkManager(process_count)
  20. positions = list(self.simulation.subjects.grass_xyz.keys())
  21. positions_chunks = chunk_manager.make_chunks(positions)
  22. spotables = []
  23. for position in positions_chunks[process_number]:
  24. arounds = get_around_positions_of_positions(position)
  25. from_subject = self.simulation.subjects.grass_xyz[position]
  26. around_data = {
  27. 'from_subject': from_subject,
  28. 'around': [],
  29. }
  30. for around in arounds:
  31. if around not in self.simulation.subjects.grass_xyz:
  32. around_data['around'].append(around)
  33. if around_data['around']:
  34. spotables.append(around_data)
  35. return spotables
  36. class GrowUp(SubjectBehaviour):
  37. frequency = 20
  38. def run(self, data):
  39. return True
  40. def action(self, data) -> [Event]:
  41. self.subject.density += 1
  42. return [GrassGrownUp(
  43. self.subject.id,
  44. self.subject.density,
  45. )]
  46. class GrassSpawnBehaviour(SimulationBehaviour):
  47. frequency = 100
  48. use = [GrassSpotablePositionsMechanism]
  49. def run(self, data):
  50. spawns = []
  51. for around_data in data[GrassSpotablePositionsMechanism]:
  52. from_subject = around_data['from_subject']
  53. arounds = around_data['around']
  54. if from_subject.density >= 40:
  55. spawns.extend(arounds)
  56. return spawns
  57. @classmethod
  58. def merge_data(cls, new_data, start_data=None):
  59. start_data = start_data or []
  60. start_data.extend(new_data)
  61. return start_data
  62. def action(self, data) -> [Event]:
  63. from sandbox.engulf.subject import Grass # cyclic
  64. events = []
  65. for position in data:
  66. if position not in list(self.simulation.subjects.grass_xyz.keys()):
  67. new_grass = Grass(
  68. self.simulation,
  69. position=position,
  70. density=20,
  71. )
  72. self.simulation.subjects.append(new_grass)
  73. events.append(GrassSpawn(
  74. new_grass.id,
  75. new_grass.position,
  76. new_grass.density,
  77. ))
  78. return events