behaviour.py 3.1KB

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