AntBrain.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
  2. from intelligine.simulation.object.brain.Brain import Brain
  3. from intelligine.simulation.object.brain.part.attack.AttackBrainPart import AttackBrainPart
  4. from intelligine.cst import MODE, MODE_EXPLO, MODE_GOHOME, PHEROMON_DIR_EXPLO, \
  5. BRAIN_PART_TAKE, BRAIN_PART_PUT, MODE_NURSE, PHEROMON_DIR_NONE, BRAIN_PART_ATTACK, MODE_HOME, \
  6. SMELL_FOOD, SMELL_EGG, MODE_GO_OUTSIDE, MOLECULE_SEARCHING_WAY, MODE_SEARCH_AROUND
  7. from intelligine.cst import MOLECULE_SEARCHING
  8. from intelligine.cst import BRAIN_PART_MOVE
  9. from intelligine.simulation.object.brain.part.transport.AntPutBrainPart import AntPutBrainPart
  10. from intelligine.simulation.object.brain.part.transport.AntTakeBrainPart import AntTakeBrainPart
  11. from intelligine.synergy.object.Food import Food
  12. from intelligine.synergy.object.ant.Egg import Egg
  13. from intelligine.simulation.object.brain.part.move.AntMoveBrainPart import AntMoveBrainPart
  14. from synergine.core.exceptions import NotFound
  15. class AntBrain(Brain):
  16. _brain_parts = Brain._brain_parts.copy()
  17. _brain_parts.update({
  18. BRAIN_PART_MOVE: AntMoveBrainPart,
  19. BRAIN_PART_TAKE: AntTakeBrainPart,
  20. BRAIN_PART_PUT: AntPutBrainPart,
  21. BRAIN_PART_ATTACK: AttackBrainPart
  22. })
  23. _taken_smell_matches = {
  24. Food: SMELL_FOOD,
  25. Egg: SMELL_EGG
  26. }
  27. """ Correspondance entre ce qui est ramassé et où ce doit être stocké """
  28. def __init__(self, context, host):
  29. super().__init__(context, host)
  30. self._movement_mode = MODE_HOME
  31. self._distance_from_objective = 0
  32. self._molecule_searching = PHEROMON_DIR_EXPLO
  33. def switch_to_mode(self, mode):
  34. self._movement_mode = mode
  35. self._update_molecule_gland(mode)
  36. self._context.metas.value.set(MODE, self._host.get_id(), mode)
  37. self._update_molecule_searching(mode)
  38. def _update_molecule_gland(self, mode):
  39. if mode == MODE_EXPLO:
  40. molecule_direction_type = None
  41. elif mode == MODE_GOHOME:
  42. molecule_direction_type = PHEROMON_DIR_EXPLO
  43. self._distance_from_objective = 0
  44. elif mode == MODE_NURSE:
  45. molecule_direction_type = None
  46. elif mode == MODE_HOME:
  47. molecule_direction_type = PHEROMON_DIR_EXPLO
  48. elif mode == MODE_GO_OUTSIDE:
  49. molecule_direction_type = None
  50. elif mode == MODE_SEARCH_AROUND:
  51. molecule_direction_type = PHEROMON_DIR_EXPLO
  52. else:
  53. raise NotImplementedError()
  54. if molecule_direction_type:
  55. self._host.get_movement_molecule_gland().set_molecule_type(molecule_direction_type)
  56. self._host.get_movement_molecule_gland().enable()
  57. else:
  58. self._host.get_movement_molecule_gland().disable()
  59. def _update_molecule_searching(self, mode):
  60. way = DirectionMolecule.WAY_UP
  61. if mode == MODE_EXPLO:
  62. molecule_searching = PHEROMON_DIR_EXPLO
  63. elif mode == MODE_GOHOME:
  64. molecule_searching = PHEROMON_DIR_NONE
  65. elif mode == MODE_NURSE:
  66. molecule_searching = PHEROMON_DIR_NONE
  67. elif mode == MODE_HOME:
  68. molecule_searching = self.get_part(BRAIN_PART_TAKE).get_smell_target()
  69. elif mode == MODE_GO_OUTSIDE:
  70. # TODO: Dans l'idée c'est sortir, donc il faudrait remonter toutes les smells ...
  71. molecule_searching = self.get_part(BRAIN_PART_TAKE).get_smell_target()
  72. way = DirectionMolecule.WAY_DOWN
  73. elif mode == MODE_SEARCH_AROUND:
  74. molecule_searching = PHEROMON_DIR_NONE
  75. else:
  76. raise NotImplementedError()
  77. self._molecule_searching = molecule_searching
  78. self._molecule_searching_way = way
  79. self._context.metas.value.set(MOLECULE_SEARCHING, self._host.get_id(), molecule_searching)
  80. self._context.metas.value.set(MOLECULE_SEARCHING_WAY, self._host.get_id(), way)
  81. def get_movement_mode(self):
  82. return self._movement_mode
  83. def host_moved(self, distance=1):
  84. self._distance_from_objective += distance
  85. def set_distance_from_objective(self, distance):
  86. self._distance_from_objective = distance
  87. def get_distance_from_objective(self):
  88. return self._distance_from_objective
  89. def get_smell_for_object_taken(self, obj):
  90. for take_class in self._taken_smell_matches:
  91. if isinstance(obj, take_class):
  92. return self._taken_smell_matches[take_class]
  93. raise NotFound()
  94. @classmethod
  95. def get_home_smells(cls):
  96. """
  97. Note: Actually return all know smells. Not really HOME smells.
  98. :return:
  99. """
  100. return cls._taken_smell_matches.values()